Implicit parameter constraints not inferred
Wolfgang Thaller
wolfgang.thaller at gmx.net
Sat Nov 6 11:57:33 EST 2004
Benjamin Franksen wrote:
>> main = let ?b = True in use_b
>
>> --use_b :: (?g::Bool) => IO ()
>> use_b = print ?b
>
> It isn't: ghc -fimplicit-params says
>
> Unbound implicit parameter (?b :: a)
> arising from use of implicit parameter `?b' at TestBug.hs:4
> In the first argument of `print', namely `?b'
> In the definition of `use_b': use_b = print ?b
>
> It works if I uncomment the signature.
>
> Using ghc-6.2.2, btw.
>
> My question: Is this as it should be or is it a bug?
The Monomorphism Restriction.
It's intended to prevent people from shooting themselves in the foot by
defining things that look like constants (like use_b) but really are
polymorphic values that still depend on some context and therefore are
not constant.
So in this case (use_b defined with no arguments), you need to specify
a type signature.
The monomorphism restriction would be useful in situations like:
foo = expensiveOp bar
How often will foo be evaluated?
With the monomorphism restriction, at most once. Without it, well, that
depends on the type of bar. With bar :: (?g :: Bool) => Int, foo also
gets the (?g :: Bool) => context, and it's reevaluated whenever it's
used; but you won't be able to tell that by looking at foo alone.
Cheers,
Wolfgang
More information about the Glasgow-haskell-users
mailing list