SoC Stevenson | 10 Jul 2012 22:13

Fwd: Question about op, args.

bash-3.2$ maxima
Maxima 5.26.0 http://maxima.sourceforge.net
using Lisp SBCL 1.0.57
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
Good afternoon,

I was exploring my understanding of the internals. My idea was to take
something apart and put it back together.

(%i1) teste : a + b $

First surprise was that op(teste) is not a symbol but
nounify(op(teste)) is. That's fine.

(%i2) lop : nounify(op(teste));
(%o2)                                mplus

So here's the second surprise that args(teste) are reversed.

(%i3) largs: args(teste);
(%o3)                               [b, a]

(%i5) buildq( [mop : lop, margs : largs], mop ( splice(margs)));
(%o5)                             mplus(b, a)

My question is: Am I guaranteed that reverse(args( ... )) is the correct order?

--
(Continue reading)

Barton Willis | 11 Jul 2012 02:13
Favicon

Re: Fwd: Question about op, args.

> My question is: Am I guaranteed that reverse(args( ... )) is the correct order?

For addition and assuming that the option variable display_format_internal is false, the answer is yes.
The function inpart might be useful to you--to read the user documentation, enter ?? inpart. Also, to print
the internal representation of an expression, try the CL function print--either

(%i7) e : a + b + 42 * z;
(%o7) 42*z+b+a

(%i8) :lisp(print $e)
((MPLUS SIMP) $A $B ((MTIMES SIMP) 42 $Z)) 
((MPLUS SIMP) $A $B ((MTIMES SIMP) 42 $Z))

(%i8) ?print(e);
((MPLUS SIMP) $A $B ((MTIMES SIMP) 42 $Z)) 
(%o8) 42*z+b+a

--bw
Raymond Toy | 11 Jul 2012 03:47
Picon

Re: Fwd: Question about op, args.

On 7/10/12 5:13 PM, Barton Willis wrote:
>> My question is: Am I guaranteed that reverse(args( ... )) is the correct order?
> 
> For addition and assuming that the option variable display_format_internal is false, the answer is yes.
> The function inpart might be useful to you--to read the user documentation, enter ?? inpart. Also, to print
> the internal representation of an expression, try the CL function print--either
> 
> (%i7) e : a + b + 42 * z;
> (%o7) 42*z+b+a
> 
> (%i8) :lisp(print $e)

Nothing really to add, but it's less typing to do

:lisp $e

if you know the name of the variable and remember that all maxima
variables are prefixed with a $ in lisp.

Ray
Richard Fateman | 11 Jul 2012 02:24
Picon
Favicon

Re: Fwd: Question about op, args.

On 7/10/2012 1:13 PM, SoC Stevenson wrote:
> bash-3.2$ maxima
> Maxima 5.26.0 http://maxima.sourceforge.net
> using Lisp SBCL 1.0.57
> Distributed under the GNU Public License. See the file COPYING.
> Dedicated to the memory of William Schelter.
> The function bug_report() provides bug reporting information.
> Good afternoon,
>
> I was exploring my understanding of the internals. My idea was to take
> something apart and put it back together.
>
> (%i1) teste : a + b $
>
> First surprise was that op(teste) is not a symbol but
> nounify(op(teste)) is. That's fine.
um, this is all much easier in lisp, but
you could try  op(teste,0) to get "+".

>
> (%i2) lop : nounify(op(teste));
> (%o2)                                mplus
>
> So here's the second surprise that args(teste) are reversed.

try this:   testf:  a*b;

args(testf).

conventionally, humans expect sums to have reverse ordering from products,
(Continue reading)

SoC Stevenson | 11 Jul 2012 14:28

Re: Fwd: Question about op, args.

Thanks to Barton and Richard,

<snip>
>
> um, this is all much easier in lisp, but
> you could try  op(teste,0) to get "+".
...
> Depending on what you are really trying to do, my guess is you should just
> learn a little lisp.
> and also maybe read this:
>
> http://www.cs.berkeley.edu/~fateman/papers/simplifier.txt
>
> while it is about 33 years old, it is still fairly accurate.

Actually this was an experiment. I do know lisp, and I've been trying
to figure out the innards. In the process of research, one of the
emails on the Maxima site lamented that little of Maxima is written in
Maxima. As a recovering programming language person, this whets my
curiosity. So I decided to try an experiment and write the project I
have in mind completely in Maxima, even though I already have a lisp
version of the code.  Should be pretty straight forward, no?

Stay tuned :-)
Robert Dodier | 11 Jul 2012 04:47
Picon

Re: Fwd: Question about op, args.

On 2012-07-10, SoC Stevenson <steve <at> cs.clemson.edu> wrote:

> First surprise was that op(teste) is not a symbol but
> nounify(op(teste)) is. That's fine.

All operators which are handled specially by the parser are returned as
strings by op. That makes the results readable (i.e. the parser can read
the operator without its arguments).

> (%i5) buildq( [mop : lop, margs : largs], mop ( splice(margs)));
> (%o5)                             mplus(b, a)

apply("+", [a, b]) constructs b + a -- no need to nounify the operator
(in fact, you won't get a "+" expression if you do).

Here is a macro to take an expression apart and put it back together:

(%i1) display2d : false $
(%i2) foo (e) ::= buildq ([o : op (e), a : args (e)], apply (o, a)) $
(%i3) macroexpand (foo (a + b));
(%o3) apply("+",[b,a])
(%i4) foo (a + b);
(%o4) b+a

> My question is: Am I guaranteed that reverse(args( ... )) is the correct order?

The arguments of all commutative operators and functions are put in a
canonical ordering by Maxima's expression simplifier. 

For most commutative operators, the arguments are displayed in their
(Continue reading)


Gmane