Interpret haskell within haskell.

John Hughes
Fri, 20 Dec 2002 10:10:28 +0100 (MET)

On Fri, 20 Dec 2002, Matt Hellige wrote:

> [Christopher Milton <>]
> > --- David Sankel <> wrote:
> > > I was wondering if there is any project that aims to
> > > interpret haskell within haskell.
> [... snipped intro to ghci ...]
> > If you have defined functions in "myprog.hs":
> > :load myprog.hs
> > then the functions defined in the file are available,
> > or else you'll get error message(s) about problems
> > found parsing "myprog.hs".
> >
> I'm not sure this is quite what he's asking for. For many projects it
> is useful to be able to dynamically (and programatically) load and
> evaluate arbitrary chunks of code for one reason or another.

Maybe this isn't quite what he was asking for either, but...

When I want to do this kind of thing, I use hugs as a back-end. I write
the expressions I want to evaluate, or whatever, to a file of hugs
commands, and then run

	system "hugs <hugsinput >hugsoutput"

then read and process the output (choose your favourite filenames,
/tmp/... or whatever).

It's not the world's fastest solution, but on the other hand hugs provides
excellent "reflection" -- you can do much more than just evaluate
expressions, you can ask hugs about types, class instances, etc etc. I've
used this, for example, in an old prototype JavaDoc like tool which used
Hugs to determine the types and other properties of documented names, and
in a little script for use with QuickCheck, which finds all the property
names defined in a set of modules, loads the modules into hugs, and tests
the properties.

You may think this is a very simple-minded approach, but I would defend it
on several grounds:

* it is VERY simple, and provides "programmatic eval" without any semantic
  problems whatsoever.

* knowledge of the Haskell language is isolated where it belongs, in the
  hugs interpreter -- my tools only need to know how to work the hugs
  interface. As the language evolves, I can keep up just by installing a
  new version of hugs -- I have no parser and interpreter of my own to

Easy and effective -- if a bit slow.

John Hughes