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

HaskellPackagingShipping

Haskell Problem Overview


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

Haskell Solutions


Solution 1 - Haskell

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael FoxView Question on Stackoverflow
Solution 1 - HaskellHimanshu MishraView Answer on Stackoverflow