[Haskell-cafe] Design of a DSL in Haskell
Tillmann Rendel
rendel at informatik.uni-marburg.de
Mon Dec 3 23:40:17 CET 2012
Hi,
Joerg Fritsch wrote:
> 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.
You can already use Haskell in your DSL. A simple example:
main = runCWMWL $ do
eval ("isFib::", 500 + 500, ?BOOL)
The (+) operator is taken from Haskell, and it is available in your DSL
program. This use of Haskell is completely for free: You don't have to
do anything special with your DSL implementation to support it. I
consider this the main benefit of internal vs. external DSLs.
A more complex example:
main = runCWMWL $ do
foo <- eval ("isFib::", 1000, ?BOOL)
if foo
then return 27
else return 42
Here, you are using the Haskell if-then-else expression to decide which
DSL program to run. Note that this example also uses (>>=) and return,
so it only works because your DSL is monadic. Beyond writing the Monad
instance, you don't have to do anything special to support this. In
particular, you might not need an additional embed function if you've
already implemented return from the Monad type class. I consider this
the main benefit of the Monad type class.
Tillmann
More information about the Haskell-Cafe
mailing list