[Haskell-cafe] I love purity, but it's killing me.

Matthew Naylor mfn-haskell-cafe at cs.york.ac.uk
Sat Feb 9 14:07:08 EST 2008

Hi Tom,

> In addition to the sharing problem, another shortcoming of Haskell
> DSLs is they can not fully exploit the benefits of algebraic
> datatypes.  Specifically, pattern matching ADTs can only be used to
> control the compile-time configuration of the target, it can't be used
> to describe the target's behavior -- at least for DSLs that generate
> code that executes outside of Haskell's runtime.

you can embed algebraic data types and pattern matching in Haskell
with your own semantics, and retain type inference.  It goes something
like this:

  (nil, (|>)) = datatype (cons0 [] \/ cons2 (:))

  map f xs = match xs rules
      rules (x, xs) =
        [ nil  -->  nil
        , x |> xs  -->  f x |> map f xs

here, map :: (Term a -> Term b) -> Term [a] -> Term [b].

The main issue is that you have to quantify the free variables in
patterns, hence the "rules" function.  I don't know if this helps you.

> Writing a real compiler would solve both of these problems.  Is there
> any Haskell implementation that has a clean cut-point, from which I
> can start from a fully type-checked, type-annotated intermediate
> representation?

The Yhc.Core library is very simple to use and fairly mature (Neil's
been tweeking it for about 3 years now), but it doesn't meet your
type-annotated requirement. (Untyped core is still pretty useful,

If you go the real compiler route, would it not make sense to take the
DSL as the source language rather than Haskell?  Or are the DSL and
Haskell quite similar?  Or perhaps you are thinking of a two language
system, where some code is evaluated at compile time by Haskell, and
some is compiled to the target language?  If so, maybe you just want
domain specific syntax inside a Haskell program, in which case the
paper "Why it's nice to be quoted: quasiquoting for haskell" might be
relevant (it's actually supported in GHC I think).

Anyway, all very thought provoking!



Tom Hawkins wrote:
> Emil Axelsson wrote:
> > I know of a few of ways to express sharing in a pure language:
> >
> > 1) "Observable sharing", which, in general, is unsafe.
> > 2) Using Template Haskell
> > 3) Matthew Naylor has done some work on "expressible sharing", which has
> > 4) Use a monad (but I'm sure this is what you're trying to avoid).
> Or...
> 5) Forget embedding the DSL, and write a direct compiler.

Taking options 2 or 5 just to solve the sharing problem sounds to me
like a lot of hard work for little reward.  But don't worry, I won't
repeat my observable sharing speech. :-)

More information about the Haskell-Cafe mailing list