Michael Fox
Michael Fox

Reputation: 3642

How to ship an executable with Language.Haskell.Interpreter?

I was hoping to embed a Haskell interpreter using hint so that I could write plugins in Haskell to use with my program. I don't want to have to ship the entire Haskell platform for my executables.

Normally, Haskell executables are pretty self-contained. For example, erasing the PATH does not cause a problem:

$ PATH=. Hello
Hello world

However, a simple test program using runInterpreter bombs if I erase the PATH:

$ PATH=. TryHint
GhcException "panic! (the 'impossible' happened)\n  (GHC version 7.8.3 for x86_64-apple-darwin):\n\tDynamic linker not initialised\n\nPlease report this as a GHC bug:  http://www.haskell.org/ghc/reportabug\n"

What libraries or executables have to be available in the environment for it to work?

otool doesn't give much guidance:

otool -L TryHint
TryHint:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
    /usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
    /usr/local/lib/libgmp.10.dylib (compatibility version 13.0.0, current version 13.0.0)

The test code for TryHint does not do much:

import Control.Monad
import Language.Haskell.Interpreter

main = do
  f <- runInterpreter $ loadModules ["Test"] >> setTopLevelModules ["Test"] >> interpret "f" (as :: Int -> Int)
  case f of
    Left e -> print e
    Right r -> mapM_ (print . r) [1..10]

It just binds f to a function in Test.hs to be interpreted at run-time. Test.hs looks like this:

module Test where
f :: Int -> Int
f x = x + 1

Upvotes: 136

Views: 2289

Answers (1)

Himanshu Mishra
Himanshu Mishra

Reputation: 9456

Shipping an executable with Language.Haskell.Interpreter seems to go perfect with the way you have shown. You have to set your PATH to the script you want to execute.

And as of side note, as mentioned by @bennofs in comments, Statically linking the GHC API doesn't work with the new dynamic linker introduced in GHC 7.8, (interactive code execution now requires dynamic libraries).

Upvotes: 2

Related Questions