
A toy Mathematica interpreter in Haskell - soofy
http://community.wolfram.com/groups/-/m/t/943405
======
denfromufa
Mathics is implementation of Mathematica (actually it is now called Wolfram
language) using sympy, ply, and django.

It also provides jupyter kernel.

The original author was hired by Wolfram, but the project still lives on!

[http://mathics.github.io](http://mathics.github.io)

Maxima is older than Mathematica and written in LISP. One of its contributors
has written MockMMA to implement Mathematica in LISP:

[https://people.eecs.berkeley.edu/~fateman/lisp/](https://people.eecs.berkeley.edu/~fateman/lisp/)

~~~
wiz21c
I've looked at the github project for Mathics and didn't see any use of ply...
Looks like they have written their own parser/lexer.

~~~
denfromufa
Yes, indeed - it is very weird! Because mathics still mentions ply in
change.rst, but not used in code anymore:

[https://github.com/mathics/Mathics/blob/master/CHANGES.rst](https://github.com/mathics/Mathics/blob/master/CHANGES.rst)

~~~
sn6uv
Hidden in there (under 1.0) is "new recursive descent parser #416". We've only
just finished 1.0 and haven't had a chance to announce it properly yet.

~~~
denfromufa
is this limitation in ply?

~~~
sn6uv
We previously used ply but ran into a few issues:

The main issue was that generating the tables was extremely slow (~15mins). We
solved this by packaging the tables, but when the version of ply changed a
table regeneration was forced on our users.

The recursive descent parser was also simpler and significantly more
performant.

There's a short write-up on the PR:
[https://github.com/mathics/Mathics/pull/416](https://github.com/mathics/Mathics/pull/416)

------
spitfire
I've written a Mathematica clone twice before. This guy got there much faster
( I was writing in C++).

However both times I got the basic language going then realized it was the
standard library that made Mathematica gold and abandoned it.

Kudos to him and it's good work!

~~~
wfunction
Are the solver algorithms part of the standard library or part of the
execution engine? i.e., are Solve/DSolve/etc. written in plain Mathematica
code, or are they special somehow?

~~~
tnecniv
You can call external code from Mathematica, so they can be part of the
standard lib and be special.

~~~
wfunction
OK, but that doesn't answer my question...

------
bertiewhykovich
This proves, once again, what a world-class, unbeatable tool Haskell is for
writing toy interpreters.

~~~
pjmlp
Any ML language is.

When I was at the university[0], we were forbidden to use Caml Light, Lisp or
Prolog in our compiler classes, because they would make the exercises too easy
and so we wouldn't learn properly.

[0] Haskell was still to be born and we had Miranda instead.

~~~
paulsutter
You might want to read his comment a little more carefully

~~~
pjmlp
> unbeatable tool Haskell is for writing toy interpreters.

 _unbeatable_

As if no other language has the same feature set.

~~~
lispm
focus your eye on the word TOY and think about it for a while

~~~
pjmlp
So explain me why Lisp, Prolog or any ML derivative aren't as capable as
Haskell for TOY interpreters.

Unless s/he was being sarcastic, but if so I don't get the joke.

~~~
lispm
Prepare for a facepalm.

It has nothing to do with Lisp or ML.

Read again:

> This proves, once again, what a world-class, unbeatable tool Haskell is for
> writing toy interpreters.

This somehow suggest a) that it is not quite exactly really there, where one
may want to develop a non-toy interpreter/compiler/... and/or b) that a lot of
examples are just toy programs which can be as well developed in any language
and one has now seen zillions of those in Haskell and everyone is convinced
that Haskell is good for implementing toy interpreters. and/or c) that the
quality of the code stays on toy level.

In this case it is a slightly more advanced toy interpreter, but the
implementation lacks any information about its architecture, there is no
documentation, stuff is hardcoded, etc. It's the typical slightly autistic
implementation where there is the code and a some tests...

The real challenge would be to develop a non-toy interpreter/compiler/... Is
there Haskell still a world-class, unbeatable tool?

Personally I'm not a Haskell user, but I like that people are writing code in
it. Though for such an application I would like to see a bit more effort on
the meta-information about the code. You can see that in some (!) Lisp
programs, people invest some effort to make them very expressive and readable.
Just read the PAIP code/book from Peter Norvig which implements some parts of
Macsyma in Common Lisp.

But there is the danger of not being able to really compete for many high-
level languages. Lisp had some attempts for actual products in this domain of
computer algebra: Macsyma, Reduce and Axiom were the most prominent. The were
developed as real products with actual end-users. They failed in the market
place against languages with C/C++-based implementations like Mathematica.
Though they are still being maintained as open source / free software
projects.

~~~
pjmlp
Thanks for taking the effort of writing it down.

Still, I don't agree with the snarky remark of the OP, hence why I wasn't
getting it, as there are several uses of Haskell in production.

Besides Microsoft, Facebook and Intel there are quite a few other companies
using it in production, specially in financial and insurance sector for data
modelling.

I am the other way, I have dabbled quite a few times with Lisp languages,
mostly Emacs Lisp, Guile and Clojure.

I am also a big fan of the old Lisp Machines, but having learned FP in Caml
Light, my brain got hardwired to ML derived languages.

~~~
lispm
Use in production - often in-house production - is slightly different than
development of a non-toy language implementation or a non-toy application
(here an interpreter for a clone of the Mathematica language).

Non-toy computer-algebra applications in Lisp were/are Macsyma/Maxima/Vaxima,
Reduce and Axiom. They were real products an end user could buy. There is an
extensive amount of literature about Macsyma:
[http://ftp.math.utah.edu/pub/tex/bib/macsyma.bib](http://ftp.math.utah.edu/pub/tex/bib/macsyma.bib)

But then one might want to survive in the market place.

Btw., people even use the C64 still in 'production'. ;-)

[http://sploid.gizmodo.com/this-old-ass-commodore-64-is-
still...](http://sploid.gizmodo.com/this-old-ass-commodore-64-is-still-being-
used-to-run-an-1787196319)

~~~
pjmlp
Well, here is a non-toy language implementation used by Intel for hardware
design implemented in Haskell:

[https://www.cs.ox.ac.uk/tom.melham/res/reflect.html](https://www.cs.ox.ac.uk/tom.melham/res/reflect.html)

~~~
lispm
Is it implemented in Haskell and not in C/C++? The papers I saw didn't say
anything about the implementation.

~~~
pjmlp
I guess it was the one they use for formal verification of their CPUs.

But there is also the Sigma rules engine used at Facebok as another possible
example,

[https://code.facebook.com/posts/745068642270222/fighting-
spa...](https://code.facebook.com/posts/745068642270222/fighting-spam-with-
haskell/)

~~~
lispm
Well, that's a rule engine embedded into Haskell. Great, but different from a
language implementation of the Mathematica language (syntax / parser, ... ,
interpreter).

Anyway, that what I said above. Inhouse use for special purpose applications.

~~~
pjmlp
It is still production code, with people being paid to write code in Haskell.

~~~
lispm
That still says nothing about the code.

------
kccqzy
I've written a toy TeX interpreter/macro-expander as my first "large" Haskell
program. It really made me think hard about both Haskell and TeX. I think this
is a great way to learn two languages at the same time if you already have
experience.

------
Mathnerd314
It would be interesting to benchmark it against the real thing.

------
mrcactu5
doesn't this turn Mathematica into a scripting language for Haskell?

~~~
gizmo686
Not any more than CPython is a scripting language for C. The Mathematica
program is entirely interpenetrated. It does not have access to the underlying
Haskell system.

~~~
yunruse
That's a bit of a shame, in some way. In the same way Hy [0] wraps Lisp's
notation style around Python, it would be quite powerful if there was a way to
'wrap' Mathematica around Haskell; being able to combine the two would be
interesting (and probably quite powerful).

[0] [http://docs.hylang.org](http://docs.hylang.org)

~~~
dekhn
you can use the Mathematica Kernel C API to make any language interface with
mathematica (I did this with Python a long time ago). This typically involves
the scripting langauge (or compiled language) invoking Mathematica functions,
I;'m not 100% if Mathematica can then call back into Haskell or other language
functions via a callback.

