[Haskell-cafe] Design of a DSL in Haskell

Brent Yorgey byorgey at seas.upenn.edu
Mon Dec 3 16:04:35 CET 2012


(Sorry, forgot to reply to the list initially; see conversation below.)

On Mon, Dec 03, 2012 at 03:49:00PM +0100, Joerg Fritsch wrote:
> Brent,
>
> I believe that inside the do-block (that basically calls my
> interpreter) I cannot call any other Haskell function that are not
> recognized by my parser and interpreter.

This seems to just require some sort of "escape mechanism" for
embedding arbitrary Haskell code into your language.  For example a
primitive

  embed :: a -> CWMWL a

(assuming CWMWL is the name of your monad).  Whether this makes sense,
how to implement embed, etc. depends entirely on your language and
interpreter.  

However, as you imply below, this may or may not be possible depending
on the type a.  In that case I suggest making embed a type class method.
Something like

  class Embeddable a where
    embed :: a -> CWMWL a

I still get the feeling, though, that I have not really understood
your question.

> I am also trying to learn how I could preserve state from one line
> of code of my DSL to the next. I understand that inside the
> interpreter one would use a combination of the state monad and the
> reader monad, but could not find any non trivial example.

Yes, you can use the state monad to preserve state from one line to
the next.  I am not sure what you mean by using a combination of state
and reader monads.  There is nothing magical about the combination.
You would use state + reader simply if you had some mutable state as
well as some read-only configuration to thread through your
interpreter.

xmonad is certainly a nontrivial example but perhaps it is a bit *too*
nontrivial.  If I think of any other good examples I'll let you know.

-Brent

> 
> 
> On Dec 3, 2012, at 1:23 PM, Brent Yorgey wrote:
> 
> > On Sun, Dec 02, 2012 at 03:01:46PM +0100, Joerg Fritsch wrote:
> >> This is probably a very basic question.
> >> 
> >> I am working on a DSL that eventuyally would allow me to say:
> >> 
> >> import language.cwmwl
> >> main = runCWMWL $ do
> >>    eval ("isFib::", 1000, ?BOOL)
> >> 
> >> I have just started to work on the interpreter-function runCWMWL and I wonder whether it is possible to escape to real Haskell somehow (and how?) either inside ot outside the do-block.
> > 
> > I don't think I understand the question.  The above already *is* real
> > Haskell.  What is there to escape?
> > 
> >> I thought of providing a defautl-wrapper for some required prelude
> >> functions (such as print) inside my interpreter but I wonder if
> >> there are more elegant ways to co-loacate a DSL and Haskell without
> >> falling back to being a normal library only.
> > 
> > I don't understand this sentence either.  Can you explain what you are
> > trying to do in more detail?
> > 
> > -Brent
> 
> 



More information about the Haskell-Cafe mailing list