Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Hint: Runtime Haskell Interpreter (GHC API Wrapper) (github.com/mvdan)
25 points by gfortaine on March 18, 2016 | hide | past | favorite | 2 comments


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 ), 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 ) 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 (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.


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.

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....




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: