
Hint: Runtime Haskell Interpreter (GHC API Wrapper) - guifortaine
https://github.com/mvdan/hint
======
chriswarbo
I've played with Hint, but it can be tricky to use since it's built on the GHC
API which is pretty gnarly.

I tried using the GHC API in a project (
[http://chriswarbo.net/essays/repos/cabal2db.html](http://chriswarbo.net/essays/repos/cabal2db.html)
), and everything kept crashing saying "the impossible happened". You need to
be very careful in initialising GHC's environment just right (mainly setting
the "dyn flags"). Unless the code you're interpreting is very narrowly
constrained, your application will need to expose a whole bunch of options to
be able to handle things like language extensions, library paths, package
databases, preprocessors, template haskell, etc. at which point you're
basically reimplementing the GHC command. Trying to infer these options
basically ends up reimplementing Cabal.

In the end I wrote a GHC core-to-core plugin (
[http://chriswarbo.net/essays/repos/ast-
plugin.html](http://chriswarbo.net/essays/repos/ast-plugin.html) ) which
serialises ASTs into s-expressions and dumps them to stderr, so I can
manipulate them without hitting landmines in the GHC API.

Also, make sure not to evaluate datastructures too early. For example, the
same AST datatypes are used to represent code before and after type checking;
before type checking, locations which should describe types contain undefined,
so evaluating the whole AST (e.g. to serialise it) will cause a crash. This is
particularly unfortunate since these ASTs involve _lots_ of datatypes, so it's
natural to traverse them using generic programming approaches (e.g. Scrap Your
Boilerplate) which, by their nature, will traverse all nodes :(

I really like Haskell, and GHC is certainly an impressive piece of work. It
would be nice if it were more modular though, so it's easier to run a
particular piece on its own (e.g. 'given these modules, infer an instance of
"Foo Bar"')

EDIT: BTW, since the article is Hint, I've actually ended up using my own
alternative, available at [http://chriswarbo.net/essays/repos/nix-
eval.html](http://chriswarbo.net/essays/repos/nix-eval.html) (it's also in
Hackage, but the API's been changing a lot recently). It's basically a
convenience wrapper which calls the `runhaskell` shell command, piping Strings
of Haskell into stdin and spitting back stdout. Whilst very hacky, this seems
to be more robust than Hint and, crucially, it allows dependencies to be
downloaded, compiled and made available via `nix-shell`.

I've also made a layer on top which uses phantom types, but haven't rolled
that into the library yet.

------
ocharles
hint is a really fun little project. I used it in a toy project to add Haskell
as function language to PostgreSQL here:
[http://github.com/ocharles/plhaskell](http://github.com/ocharles/plhaskell).

It's really messy code, and I have to do a bunch of hacking to invent a type
from the PostgreSQL function metadata, but once I do that I can invoke hint:
[https://github.com/ocharles/plhaskell/blob/master/PlHaskell....](https://github.com/ocharles/plhaskell/blob/master/PlHaskell.hs#L174)

