[Haskell-cafe] Designing DSL with explicit sharing [was: I love purity, but it's killing me]

David Menendez dave at zednenem.com
Thu Feb 14 01:04:17 EST 2008


On Feb 13, 2008 5:36 AM, Luke Palmer <lrpalmer at gmail.com> wrote:
> On Feb 13, 2008 9:33 AM,  <oleg at okmij.org> wrote:
> > The approach is based on the final tagless representation. Here is our
> > DSL:
> >
> > > class Exp repr where
> > >    constant :: Int -> repr Int
> > >    variable :: String -> repr Int
> > >    add :: repr Int -> repr Int -> repr Int
> > >    sub :: repr Int -> repr Int -> repr Int
>
> This is very nice.  May I ask, though, what is the purpose of all the
> Ints appearing as arguments to repr here?  Looking over this code, it
> seems that it would work just as well if they were all omitted.

In this example, they could be omitted. But let's say you had a
language more than one type:

class Exp repr where
    constant :: Int -> repr Int
    variable :: String -> repr Int
    add :: repr Int -> repr Int -> repr Int
    sub :: repr Int -> repr Int -> repr Int

    true :: repr Bool
    false :: repr Bool
    and :: repr Bool -> repr Bool -> repr Bool
    less_than :: repr Int -> repr Int -> repr Bool
    if_ :: repr Bool -> repr a -> repr a -> repr a

The argument to repr prevents you from writing ill-typed code like
"if_ (variable "x") (constant 0) false".

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list