Chapter 13. Haskell from Eiffel

Table of Contents
Motivation
Theory--HaskellDirect
Our sample problem: Linear Regression
IDL: Writing the module interface
You mean HaskellDirect doesn't work?
C to Eiffel--Using SWIG to export the regression functions
Compiling the example
Sample program regression_test
Creating a user-callable library written in Haskell and called from Eiffel

The nature of functional languages dictates that a problem be broken down into functional units--functions which purely map a set of inputs to a single output. Haskell in particular allows elegant construction of complex functions, each of which is guaranteed not to have any side effects either on the operands or the world state. It would be a clean match, then, to be able to use Haskell functions from Eiffel; the HaskellDirect tool, in combination with our trusty SWIG, makes this possible

Motivation

Functional languages represent a fairly significant departure from traditional imperative languages. By viewing the entire process of program execution as the evaluation of a function, pure functional languages seek to remove many of the problems of imperative languages by introducing a fairly rigid and strict syntax. In Haskell, for example, a function purely maps a set of inputs into a single output without changing any of the input parameters or introducing any side effects into the overall world state.

Because of this, Haskell functions are extremely easy to reason about--since no side effects of any kind are involved, variables cannot be used (and so cannot be incorrectly updated), preventing loops (which cannot be improperly evaluated) and other constructs on which imperative programmers rely.

Unfortunately, this also makes control flow in functional programs somewhat difficult to reason about for the imperative programmer, particularly in highly interactive programs (we wrestled mightily with the concept of a "monad" before getting thoroughly confused...). It would be awfully nice if it were possible to get the advantages of pure, higher-order functions, but make those functions accessible to a more traditional imperative object-oriented language like Eiffel.