[Haskell-cafe] functional dependencies question

Neil Brown nccb2 at kent.ac.uk
Thu Jul 1 08:32:47 EDT 2010


On 01/07/10 13:11, Patrick Browne wrote:
> Neil,
> Does the following sum up the situation?
> The class Num has subclasses containing various numeric types and the
> literal 1 is a value for one or more of those types.
> Hence the Haskell compiler says the instance 1) is OK.
> But at run time, without the quantified (1:Int), the 1 could of more
> than one type, which causes a problem.
>    
I think you have the rough gist, but a few clarifications are 
necessary.  I've added some angle brackets to show the differences:

The class Num has <instances for> various numeric types and the literal 
1 <is a shorthand for fromIntegral 1> which can be instantiated to any 
of those types.  At <compile time>, without the qualified (1::Int), the 
1 could be instatianted to any type, and so <the compiler cannot 
determine which instance is required, because it doesn't know an exact 
type for the literal 1>.

All this is at compile-time, not run-time.  The problem comes down to 
needing to know a specific type in order to pick a type-class instance 
-- the functional dependencies are actually irrelevant to this aspect.  
Let's say I have this simpler class:

class Big a where
   isBig :: a -> Bool

instance Big Int where
   isBig x = (x > 1000000) -- big for an Int

instance Big Integer where
   isBig x = (x > 1000000000000) -- big for an Integer

If you ask "isBig 99999999", the compiler needs to know which instance 
to use.  The literal 99999999 can be an Int or an Integer, so the 
instance is ambigious, and that will cause the error.  So the compiler 
needs some clarification to choose an instance.

Thanks,

Neil.


More information about the Haskell-Cafe mailing list