[Haskell-cafe] Is "let" special?

Max Bolingbroke batterseapower at hotmail.com
Tue Nov 2 05:20:49 EDT 2010


2010/11/2 Günther Schmidt <gue.schmidt at web.de>:
> I've been given a few hints over time when I asked question concerning DSLs
> but regretfully didn't follow them up.

I think this is probably to do with observable sharing. The problem in
DSLs is if you have:

fact :: Term -> Term
fact = Factorial

instance Num Term where
  fromIntegral = Int
  (+) = Plus

e = let x = fact 2 :: Term
    in x + x :: Term

(Lets say the terms of our deeply embedded DSL are of type Term). If
you pattern match on "e" you will get something like (Factorial (Int
2)) `Plus` (Factorial (Int 2)). This has lost the work sharing
implicit in the original term, where the Factorial computation was
shared.

To recover this sharing, you either need some sort of observable
sharing, or some common subexpression elimination (which risks
introducing space leaks if your DSL has lazy semantics). Quite a lot
of papers have mentioned this issue: the one that springs to mind is
Gill's "Type Safe Observable Sharing".

Cheers,
Max


More information about the Haskell-Cafe mailing list