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
}
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))
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)))
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')
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.
/
whithout knowing it. Could you show the full code ? – mb14