[Haskell-cafe] passing a polymorphic function as a parameter vs using it directly in a function definition

Brent Yorgey byorgey at seas.upenn.edu
Thu Jul 15 12:52:18 EDT 2010

On Thu, Jul 15, 2010 at 01:20:11PM +0100, Pasqualino Titto Assini wrote:
> Many thanks for the explanation.
> But I thought that GHC always derives the most generic type, why does
> it fix my 'a' to 'Int' ?

Note that this type

  evalAST2 :: forall a. (Expr a -> IO()) -> AST -> IO ()

means that the type a has to be chosen first, i.e. by the *caller* of
evalAST2.  So evalAST2 is not free to use its argument k at whatever
type it wants, it will be given a function of type (Expr T -> IO())
for some type T that it didn't get to choose.  Forcing the caller to
provide a polymorphic function, so that evalAST2 can choose at what
type(s) to use it, is exactly what is expressed by the rank-2 type

  evalAST2 :: (forall a. Expr a -> IO()) -> AST -> IO ()


More information about the Haskell-Cafe mailing list