[Haskell-cafe] MultiParamTypeClasses, FunctionalDependencies and FlexibleInstances using GHCi

Bulat Ziganshin bulat.ziganshin at gmail.com
Fri May 14 09:32:10 EDT 2010


Hello Julian,

Friday, May 14, 2010, 4:18:42 PM, you wrote:

> Now, if I type
>> 3 + 4
> it does not work, and i really don't understand why. If i ask GHCi
> for 3's type ($ :t 3) it will answer "3 :: (Prelude.Num t) => t".
> But, if 3 and 4 are Prelude.Nums and there is an instanfe Num x x x
> for x of Prelude.Num - than why can't GHC deduce from the
> definitions that 3 and 4, both Prelude.Nums, can be used with (+)
> since there is an instance for Prelude.Num and my class Num - and
> the result will of course be something of Prelude.Num?

because 3 and 4 may have different types. Num is a class, Int is a
concrete type. 3 without additional type signature is polymorphic
value. usually type inference deduce types of numeric constants (that
all are polymorphic) from context but in your case it's impossible

your functional dependency allows to fix result type once parameter
types are known, but not other way

you appeal to *instance* definition but haskell/ghc type inference
can't use instance heads to deduce types since classes are open and
anyone can add later code that breaks your assumption (imagine that
ghc generates code for your module and later this module is imported by
someone else and additional instances are provided)

btw, quite popular problem, it arrives here each month or so :)

there are some ghc pragmas that somewhat break this rule, you may try
allow-indecidable-insances or so. but it's dangerous way


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list