
Jon Harrop wrote:
I noticed a recent thread about writing a Mathematica implementation in Haskell.
Yeah, that was me.
I think this is an excellent idea and would be a great project for a Haskell newbie.
Uh... I think it's actually a tad harder than it looks. [Understatement!]
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 Mathematica.
Are you serious?! o_O
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 arithmetic.
Now this is interesting. The claim is that Mathematica is the fastest, most powerful software on planet earth, second to none. Actually it turns out that at least for factoring moderately big integers, pari/gp seems to be a fair bit faster (50% or so). I have no idea about the rest. Note that (as I understand it) GHC implements Haskell's Integer type using the GMP. And for some reason or other, they want to remove this feature...
2. GUI: I would take our existing vector graphics software:
http://www.ffconsultancy.com/products/smoke_vector_graphics/ http://www.ffconsultancy.com/products/fsharp_for_visualization/
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).
Erm... have you seen Mathematica 6? That's OpenGL accelerated too. I've just been playing with it in fact - it's pretty fast as far as I can tell.
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.
Haskell has pattern matching, but what Mathematica does is much more sophisticated. I have tried to implement it several times, and failed. (But that was Pascal, this is Haskell...)
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.
What...the...hell...? Mathematica contains the largest, most comprehensive set of implementations of special functions anywhere in the world. It has a *vast* collection of identities and transformation rules constituting man-centuries of R&D work. It has cutting edge symbolic integration capabilities. It has multiple numerical solver algorithms. It has... Yeah, should only take 5 minutes or so to exceed those capabilities. Easy really...
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.
OK, so you're saying that in 4 days you wrote something that out-performs Mathematica, a program that has existed for decades and has a vast, highly-funded R&D effort behind it featuring some of the brightest minds in the field? I'm in a state of disbelief here.