Writing Mathematica programs in Macsyma .. was Re: [Maxima] "Delayed" problem in mfunction/mmacro definition



Some notes in case the OP still wants to translate after RJF's posting:

[Richard Fateman <fateman@cs.berkeley.edu>, Fri, 19 Nov 2004 10:09:22 -0800]:
> 1. If you have a Mathematica program written using only the
> simplest kind of features, e.g. ordinary Fortran-like assignment
> statements, then you can probably just take them and put them
> into Macsyma/Maxima by changing some [] to (), and inserting * some places.

Well, you can actually make your life quite a bit easier than that:

If you have access to Mathematica, you can actually tell it to write
out the definitions in InputForm, and things like the asterisks should
appear.  --- You also have to take into account Mathematica's
packages: you certainly want Global`f and `f and f to be the same
symbol, but you certainly also want Foo`f and Bar`Private`f to be
different things; again, if you have a working Mathematica, you can
set $Context and $ContextPath to something non-existing and to an
empty list, respectively, before dumping the definitions, so that all
symbols are fully qualified with their packages.

Get rid of single underscores.

Check for any use of the Hold... attributes, for which you probably
need a macro calling a function with quoted arguments.

If you have multiple definitions for the same Head, it is probably
easiest to turn each of them into a function of its own (with an
uninterned symbol) and provide a dispatch function that accepts any
number of arguments and calls the correct function; e.g.,

f[x_] := x /; x > 0
f[x_] := 1 /; x < 0
f[_] := 2

can become 

f1(x) := x $
f2(x) := 1 $
f3(x) := 2 $
f([x]) := if length(x) = 1
          then
            if errcatch(x > 0) = true
            then funmake(f1, x)
            else
              if errcatch(x < 0) = true
              then funmake(f2, x)
              else f3(x)
           else
             buildq([x], f(splice(x))) $

, except that f1, f2, f3 should be uninterned.

On the other hand, this does not work with the standard trick of

fac[0] := 1
fac[n_] := fac[n] = n fac[n-1]

; and while this particular case can easily be incorporated as you
know how to order the definitions that are added at run-time, more
complicated examples will be quite hopeless.

I would not even want to think about Orderless or Flat functions,
whereas Listable should be easy as it does not affect pattern
matching.

> 2. If you try to do this automatically, without looking at
> the code, you are unlikely to succeed.  You will have to touch it up
> in some places.

But you can understand and define what exactly you know how to
translate, and refuse to do anything else.

For example, unless you know how to simulate Mathematica's pattern
matching (which should *really* be nasty once Flat and OneIdentity
enter the picture), anything involving .., ..., __, ___ must be
rejected.  Simple conditions on the arguments (with ? and /; in the
various places) should not be too difficult, though, v.s..

Of course, you need a good understanding of the working of Mathematica
for that (and somehow I doubt Wolfram Research and MathGroup are going
to be very helpful).  And doing this without a working installation of
Mathematica in the first place (so that you can dump things in a
well-defined way) is probably not a good idea.

Whether there are legal issues I cannot know.

Whether it is generally useful to do the conversion if it relies on a
working Mathematica, I am not sure of, either.

But at any rate I think you can actually go quite far when dealing
with packages only (no need to mimic the precise sequence of the
definitions - Definition will do that for you) as most of the users
never get very far with the Mathematica language, and most of the code
seems to be quite primitive, using pattern matching as an excuse not
to use the "runic syntax" (well, actually they should use real
Function[] expressions with named arguments).

Albert.