Sorry for the newbies question, but I really don't understand the overloaded values:
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Prelude> let x = 1
Prelude> let y = 2
Both xand yhave a type Num a => a. But Numno (/)! So why does a type expression x / ycheck?
Prelude> x / y
0.5
(/)defined in class Fractional. Is there an implicit conversion from Numto Fractional?
UPD
So, as expected, I got answers in which people claim to x / y aspecialize in Fractional a => a.
I created my own hierarchy of numbers:
data MyInt = MyInt Integer deriving Show
data MyDouble = MyDouble Double deriving Show
class MyNum a where
(+
myFromInteger :: MyNum a => Integer -> a
class MyNum a => MyFractional a where
(/
instance MyNum MyInt where
(MyInt a) +
myFromInteger i = MyInt i
instance MyNum MyDouble where
(MyDouble a) +
myFromInteger i = MyDouble (fromInteger i)
instance MyFractional MyDouble where
(MyDouble a) /
If everything in the attributes is true, then a similar code, where Numreplaced by MyNum, should also work. But ghci reports an error:
Prelude> :load myint.hs
*Main> let x = myFromInteger 1
*Main> let y = myFromInteger 2
*Main> x /
<interactive>:14:1:
No instance for (MyFractional a0) arising from a use of `it'
The type variable `a0' is ambiguous
Note: there is a potential instance available:
instance MyFractional MyDouble -- Defined at myint.hs:19:10
In the first argument of `print', namely `it'
In a stmt of an interactive GHCi command: print it