Foreign Import

There are lot of examples in the lib directory, from which you can easily discover that Erlang code and Hamler code are in the same directory with same filename, when defining a foreign function.

For example, in Data.Eq we defined Eq instances for different types. These are done with Erlang Code.

In Eq.erl we have:

  1. eqCharImpl(C1, C2) -> C1 =:= C2.

This is a function we defined to compare two Chars.

In Eq we can easily import this with:

  1. instance Eq Char where
  2. eq = eqCharImpl
  3. foreign import eqCharImpl :: Char -> Char -> Boolean

The nice thing is that we can give eqCharImpl a type; however there is no way that Hamler can check this against the actual code you’ve written in Erlang. So when you are doing something not pure, remember to wrap the output with IO.

  1. foreign import readFile :: String -> IO String