[Haskell-cafe] Implementing Mathematica

Jon Harrop jon at ffconsultancy.com
Wed May 30 02:04:31 EDT 2007

I only just subscribed to this mailing list and I am a complete Haskell 
newbie, so forgive me if this is too OT.

I noticed a recent thread about writing a Mathematica implementation in 
Haskell. I think this is an excellent idea and would be a great project for a 
Haskell newbie. I wrote a toy Mathematica implementation in OCaml while I 
waited to be viva'd for my PhD. It garnered so much interest that Wolfram 
Research bought it from me for £4,500 and gave me several free copies of 

Regarding the specific points made:

1. Numerical libraries: you should be able to reuse existing libraries like 
GMP, BLAS, LAPACK, FFTW and so on. These are often much faster than 
Mathematica's. For example, FFTW was about 4x faster than Mathematica's FFT 
the last time I benchmarked it. However, they do not support interval 

2. GUI: I would take our existing vector graphics software:


and rewrite it in Haskell as the foundation. This would far exceed anything 
that Mathematica has to offer, in part because Mathematica's graphics are 
still evaluated via the completely generic rewrite engine which is extremely 
slow. Our code already implements high-performance hardware-accelerated 
vector graphics and it is probably one of the first things I would consider 
porting to Haskell (if there is any commercial interest in such a library).

3. The language: the hardest part of reimplementing Mathematica is inferring 
what it means (there are no formal evaluation semantics). Once you've done 
that it is just a case of implementing an extensible term rewriter and 
putting in about 20 core rules. The pattern matcher is well under 100 LOC and 
you can do various things to make it more efficient. There are two tricks 
that vastly improve performance of the rewriter: return physically identical 
results whenever possible, and perform substitution and evaluation at the 
same time rather than as two separate passes.

4. Libraries: You should have no trouble exceeding the capabilities of 
Mathematica by pulling in existing libraries. For example, Mathematica 
provides no wavelet transforms, no time-frequency transforms, no function 
minimization over an arbitrary number of variables etc.

Having worked in numerical computing for many years, I can say that 
Mathematica is an excellent tool for prototyping and for doing disposable 
analyses but many people reach for conventional languages when Mathematica's 
capabilities run dry.

You should easily be able to implement a rewriter for the language that is ten 
times faster and doesn't leak.

Incidentally, my implementation of Mathematica in OCaml took four days, and it 
was one of my first OCaml programs.

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists

More information about the Haskell-Cafe mailing list