[Haskell-cafe] forall (What does it do)

Alexander Solla ajs at 2piix.com
Thu May 6 04:13:19 EDT 2010


On May 5, 2010, at 9:52 PM, John Creighton wrote:

> I've seen forall used in a few places related to Haskell. I know their
> is a type extension call "explicit forall" but by the way it is
> documnted in some places, the documentation makes it sound like it
> does nothing usefull.
>
> However on Page 27 of Haskell's overlooked object system:
>
>
>> We define an existential envelope for shape data.
>> data OpaqueShape = forall x. Shape x => HideShape x
>
...
(presumably:)
class Shape x where area :: x -> Float ... etc...
>
> It seems that forall can be used as a form of up casting.

Yes, but you can do this without the type class magic, and have the  
same degree of safety.

 > data Shape = Square Int | Rectangle Int Int | Circle Float Float |  
etc...
 > data OpaqueShape = HideShape Shape
 > area :: OpaqueShape -> Float
 > area = ...

These constructs are structurally equivalent.  The extra layer of  
abstraction doesn't really add anything in either case.  HideShape (as  
a function) is an isomorphism onto Shapes in both cases.


More information about the Haskell-Cafe mailing list