在Haskell中创建Fractional Typeclass的一个实例 [英] Creating an instance of the Fractional Typeclass in Haskell

查看:115
本文介绍了在Haskell中创建Fractional Typeclass的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名自学成才的Haskell爱好者,我正在构建一种叫做 Physical 的新类型,我可以用它来表示物理量。它使用两个 Float 分别表示物理量的值和它的不确定性。

I am a self-taught Haskell enthusiast and I was building a new type called Physical which I could use to represent physical quantities. It uses two Floats representing the value of the physical quantity and its uncertainty respectively.

data Physical = Physical {
    value :: Float,
    err :: Float
}

我使用标准规则计算总和,不同和两个数量的乘积的不确定性,以定义 Num 类型类别的一个实例。

I used standard rules for the uncertainty of a sum, difference and a product of two quantities to define an instance of the Num typeclass.

instance Num Physical where
    (Physical v1 u1) + (Physical v2 u2) = (Physical (v1+v2) (u1+u2))
    (Physical v1 u1) - (Physical v2 u2) = (Physical (v1-v2) (u1+u2))
    (Physical v1 u1) * (Physical v2 u2) = (Physical (v1*v2) (sqrt((u1 /v1)^2 + (u2 /v2)^2)*v1*v2))

到目前为止,我没有任何问题让代码工作。然而,当我尝试制作 Physical 这个分数的实例时,就像

I had no trouble getting the code to work so far. However, when i tried to make Physical an instance of Fractional like this

instance Fractional Physical where
(Physical v1 u1) / (Physical v2 u2) = (Physical (v1 / v2) (sqrt((u1 /v1)^2 + (u2 /v2)^2)*(v1 /v2)))

加载我的代码时出现错误,指出存在模糊的事件(/)在我的代码(Main./)和(/)之间由Prelude(Prelude./)导出,无论我在代码中使用(/),尤其是在分数中的物理实例的定义内。

I get an error upon loading my code stating that there is an ambiguous occurrence between the (/) in my code (Main./) and (/) exported by Prelude (Prelude./) wherever i use (/) in my code, especially inside the definition of the instance of Physical in Fractional.

Physical.hs:23:19:
    Ambiguous occurrence `/'
    It could refer to either `Main./', defined at Physical.hs:15:18
                          or `Prelude./',
                             imported from `Prelude' at Physical.hs:1:1
                             (and originally defined in `GHC.Real')

这让我很困惑,因为运算符(+)的模糊出现没有问题,因为我正在使用它来创建Num物理实例,就像我使用(/)创建分数物理实例一样。

This confuses me a great deal, since there is no problem with an ambiguous occurrence of the operator (+) as I am using it to make an instance of Physical in Num the same way I am using (/) to create an instance of Physical in Fractional.

推荐答案

缺少缩进。缩进代码。

instance Fractional Physical where
(Physical v1 u1) / (Physical v2 u2) = (Physical (v1 / v2) (sqrt((u1 /v1)^2 + (u2 /v2)^2)*(v1 /v2)))



正确



Correct

instance Fractional Physical where
  (Physical v1 u1) / (Physical v2 u2) = (Physical (v1 / v2) (sqrt((u1 /v1)^2 + (u2 /v2)^2)*(v1 /v2)))

-- even a single space would be ok



< h3>解释

你实际上定义了一个新的顶层(/)。您的实际小数实例为空。如果不是模棱两可的事件,你会得到警告没有明确的实现。

Explanation

You actually define a new top level (/). Your actual Fractional instance is empty. If it wasn't for the ambiguous occurrences, you would get warnings for no explicit implementations.

这篇关于在Haskell中创建Fractional Typeclass的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆