[Haskell-beginners] Little question about "forall"

Baa aquagnu at gmail.com
Thu Dec 7 11:41:08 UTC 2017


Hello everyone!

Suppose I have:

  class C a where
    someA :: a

  f :: C a => a -> Int
  f a =
    let x = someA :: a in -- BUG!!
      0
 
BUG as I understand I due to `:: a` - this is another `a`, not the same as
in `f` singature. But seems that it's the same `a` if `f` is the "method"
of some instance. And such bug does not happen if I return it, so my question
is how to create such `x` of type `a` in the body of the `f`? How to use
`a` anywhere in the body of `f`? Is it possible?
 
I found a way if I change signature to `forall a. C a => a -> Int`. But there
are another questions in the case:
 
 - as I know all such params are under "forall" in Haskell by-default. Seems
   it's not true?
 - in method body seems they are under "forall" but in simple functions - not?
 - i'm not sure what exactly does this "forall", IMHO it unbounds early bound
   type's variables (parameters) by typechecker, but how `a` can be bound in the
   just begining of signature (where it occurs first time) ?!
 
As for me, looks that w/o "forall" all `a`, `b`, `c`, etc in the body of
simple functions always are different types. But w/ "forall" Haskell typechecker
makes something like (on type-level):
 
   let a = ANY-TYPE
   in
      ... here `a` can occur and will be bound to ANY-TYPE
  
Where am I right and where am I wrong? :)

===
Best regards, Paul


More information about the Beginners mailing list