[Haskell-cafe] How to understand the 'forall' ?

Ryan Ingram ryani.spam at gmail.com
Wed Sep 16 13:12:00 EDT 2009


Here's the difference between these two types:

test1 :: forall a. a -> Int
-- The caller of test1 determines the type for test1
test2 :: (forall a. a) -> Int
-- The internals of test2 determines what type, or types, to instantiate the
argument at

Or, to put it another way, since there are no non-bottom objects of type
(forall a. a):
test1 converts *anything* to an Int.
test2 converts *nothing* to an Int

-- type-correct implementation
-- instantiates the (forall a. a) argument at Int
test2 x = x

-- type error, the caller chose "a" and it is not necessarily Int
-- test1 x = x

-- type-correct implementation: ignore the argument
test1 _ = 1

  -- ryan

On Wed, Sep 16, 2009 at 1:04 AM, Cristiano Paris <frodo at theshire.org> wrote:

> On Tue, Sep 15, 2009 at 11:38 PM, Daniel Fischer
> <daniel.is.fischer at web.de> wrote:
> > ...
> >> foo :: forall a. a -> a
> >
> > This is exactly the same type as
> >
> > foo :: a -> a
> >
> > (unless you're using ScopedTypeVariables and there's a type variable a in
> scope), since
> > type signatures are implicitly forall'd.
>
> Yep, perhaps I used the wrong example. What about foo: (forall a. a) ->
> Int?
>
> Cristiano
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090916/5fa679dc/attachment.html


More information about the Haskell-Cafe mailing list