[Haskell-cafe] How to understand the 'forall' ?
frodo at theshire.org
Wed Sep 16 14:58:11 EDT 2009
On Wed, Sep 16, 2009 at 7:12 PM, Ryan Ingram <ryani.spam at gmail.com> wrote:
> 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
I can easily understand your explanation for test2: the type var a is
closed under existential (?) quantification. I can't do the same for
test1, even if it seems that a is closed under universal (?)
quantification as well.
> Or, to put it another way, since there are no non-bottom objects of type
> (forall a. a):
> test1 converts *anything* to an Int.
Is the only possible implementation of test1 the one ignoring its
argument (apart from bottom of course)?
> 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
More information about the Haskell-Cafe