
Am Freitag, 27. Mai 2005 15:00 schrieb Mirko Rahn:
Hi all,
an explicit given signature causes ghc to choose the right types for integer literals as in
{-# OPTIONS -fglasgow-exts #-}
import Data.Map
f :: Ord a => [a] -> Map a Int f xs = fromList $ zip xs [0..]
Here the Literal 0 is threated as (0::Int).
But the setting
{-# OPTIONS -fglasgow-exts #-}
import Data.Map
class New a b where new :: a -> b
instance Ord a => New [(a,b)] (Map a b) where new = fromList
g :: Ord a => [a] -> Map a Int g xs = new $ zip xs [0..]
causes the error message
Could not deduce (New [(a, b)] (Map a Int)) from the context (Ord a) arising from use of `new' at Why.hs:10:7-9
ghc seems to be unable to threat the Literal 0 as (0::Int) this time but I do not understand why :-(
Can anyone explain it?
Thanks,
The problem is that ghc can't know that 0 ought to be Int, as it is, nothing prevents you declaring an instance Ord a => New [(a, Double)] (Map a Int) where new = ? and then it's clear that ghc wouldn't know which instance to choose. To solve the problem, I have three suggestions - write [0 :: Int .. ], so ghc knows that New [(a,Int)] (Map a Int) is the required instance - add a fundep: class New a b | [a -> b,] b -> a where ..., and ghc can again deduce the instance - add an expression type signature in 'g' g xs = (new :: Ord a => [(a,Int)] -> Map a Int) $ zip xs [0 .. ] any of these will do, depending on what you want,I recommend one of the first two. Cheers, Daniel