[Haskell-cafe] I want to write a compiler

wren ng thornton wren at freegeek.org
Sat Mar 7 23:07:24 EST 2009

Loup Vaillant wrote:
> -> support algebraic data types and case expressions (unless I can get
> away with encoding them as functions),

Which you always can,

     data Foo = A a1...an | B b1...bn |...


     type Foo :: forall r.
                 (a1->...->an -> r) ->
                 (b1->...->bn -> r) ->...
                 -> r

     A a1...an = \fa _... -> fa a1...an

     B b1...bn = \_ fb... -> fb b1...bn


The only trick is that you need to have closures (in order to build the 
Foos) and you need to have first-class functions (in order to pass the 
destructors in). If you're familiar with the STG machine, this is what 
they do under the covers anyways. At the core level, all you need is 
some primitive to force evaluation.

(Church Encoding)++

Live well,

More information about the Haskell-Cafe mailing list