[Haskell] Re: Instances That Ignore Type Constraints? (HList-related)

oleg at pobox.com oleg at pobox.com
Thu Oct 27 04:04:49 EDT 2005


	Just to add one more example to Ralf's reply:

We get exactly the same kind of problem if we try

*Main> null [fromEnum]

<interactive>:1:6:
    Ambiguous type variable `a' in the constraint:
      `Enum a' arising from use of `fromEnum' at <interactive>:1:6-13
    Probable fix: add a type signature that fixes these type variable(s)

Indeed, fromEnum is a polymorphic function 
|fromEnum :: (Enum a) => a -> Int| The expression [fromEnum] has the 
type |(Enum a) => [a -> Int]| that carries the constraint Enum a. 
That constraint has to be eventually resolved somehow. For a top-level
expression (not a function), the constraint must be resolved. Granted,
the function 'null' could care less what the list is made of, and
whether all constraints are resolved. But Haskell does.

One may say that instead of inferring the type
	[fromEnum] :: forall a. (Enum a) => [a -> Int]
Haskell should have inferred
	[fromEnum] :: [forall a. (Enum a) => a -> Int]
and the problem would have been solved. There is no longer an
(outermost, top-level) constraint to resolve.  When GHC does implement
MLF and start explicitly supporting impredicative types, the problem
would be fixed indeed... Currently, the only way to achieve the same
effect is to do this manually:

	newtype W = W (forall a. (Enum a) => a -> Int)

*Main> null [W fromEnum]
False

	Perhaps indeed we should move to Cafe...



More information about the Haskell mailing list