Push and pop in Maxima



On Sep 8, 2009, at 4:07 PM, Barton Willis wrote:

> A general comment: It's not clear that it's an improvement to take
> working Maxima code and re-write it in CL. More users can read, fix
> bugs, and write extensions to Maxima code than CL. See also:
>
>    http://www.math.utexas.edu/pipermail/maxima/2003/005838.html
>
> Barton

A valid argument at first, but between mathematica and maxima there  
are several important differences.

1) Mathematica is implemented in C, while Mathematica's langugage is  
a high level language in some ways similar to Lisp. It's obvious that  
a high level language requires less code and is more flexible,  
resulting in a cleaner, shorter code, which is favorable when it  
doesn't introduce any significant slowdowns. ( see also http:// 
en.wikipedia.org/wiki/Greenspun's_Tenth_Rule which is not really the  
case with mathematica - since it implements a very complete and  
sophisticated 'lisp')
2) Situation with Maxima is very different and is not going to change  
- it is written in CL which by itself is a very high level flexible  
language (and also has fast implementations available), but it offers  
an inferior language at user's level. Maxima's language is not very  
well suited for writing programs, it's more like a interpreted  
scripting language.
3) While Maxima's user level language is worse than Mathematica's,  
the Common Lisp platform offers a high level fast programming  
language that is much better than Mathematica's user level language.  
The fact that Maxima is written in Common Lisp is probably it's  
strongest point - code is small and it's really easy to extend (or  
would be with some documentation) - no need to use inferior user  
level language for anything other than scripting and experimentation.  
All 'fast' algorithms can be implemented in Common Lisp with ease,  
while one uses the user level language to merely tie the routines  
together.
4) This is not the case in Mathematica, where a large part of C core  
deals with things that are a part of Common Lisp implementations by  
specification - garbage collection, dynamic types, parsing - string  
operations... It would be foolish to implement anything but the speed  
critical parts in C because you have to deal with complex data by  
default, and the code gets very complicated for basic tasks. So one  
can look at Mathematica's user level language as a 'special Lisp  
dialect' made for mathematics with a very mathematics oriented library.

So I think one must consider these very important difference when  
looking at C<->Mathematica and CL<->Maxima. Mathematica is more of a  
(mathematics oriented) programming language implementation, while  
Maxima is more of a program running on Common Lisp.

Besides Lisp is getting popular today (perhaps Clojure more than  
Common Lisp) with quality free implementations available...

I would say it's much better to implement in Common Lisp than  
Maxima's language while the reverse would be true for Mathematica's  
case. One has to consider also the fact that Mathematica's user level  
language is much more optimized for performance (a lot of C code to  
achieve that of course) than Maxima's. Maxima is a really short (and  
simple) program compared to Mathematica thanks to Lisp.

This is the Maxima language implementation of push/pop:
prog1(statement1,[statements])::=
  buildq([statement1,statements,local:?gentemp()],
         lambda([local],splice(statements),local)(statement1))$

symbolcheck(x):=
  if symbolp(x) then x else error("arg must be a symbol",x)$

push(c,l)::=(symbolcheck(l),buildq([c,l],l:cons(c,l)))$

pop(l)::=(symbolcheck(l),buildq([l],prog1(first(l),l:rest(l))))$

It must be slow, 'push' has reversed arguments and I'm sure nobody  
even uses it, since it's not mentioned in documentation (much less,  
somebody will look at this code or improve it).

Regards,
Ziga