[Haskell-cafe] How to understand the 'forall' ?
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
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
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) ->
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe