"Delayed" problem in mfunction/mmacro definition



["Siver Andrey" <siver@sirius.ihep.su>, Mon, 15 Nov 2004 11:52:36 +0300]:
> > > This is important question for definition of Mathematica ":="
> > > (SetDelayed) function for MAXIMA (I thought that 'SetDelayed' could
> > > be interpreted as (MDEFINE) (or (MDEFMACRO)) but with second
> > > argument (MQUOTE)ed and with first argument 'a' --> (MEVAL a) at the
> > > evaluation time BUT found it's not a case).
> >
> > If you only want to do something like
> >     f[x_] := Sin[x]
> > so that
> >     f[2] --> Sin[2]
> >     f[1+1] --> Sin[2]
> > , you can simply use Maxima's ":=" which already does the right thing;
> > if you want the right hand side to be evaluated (as with Set) you have
> > to explicitly ask for that with, e.g., ''.
> 
> What do you mean by ''?

I was thinking of something like

x = .
foo = SomeVeryExpensiveOperationUsingXAsAFreeParameter
f[x_] = foo 

which would translate into something like

f(x) := ''some_very_expensive_operation('x)

> *** ex.1 ***
> a=1;
> f := a + 1;
> f
> => 2
> a=2;
> f
> => 3
> *** ex.2 ***
> a=1;
> f = a + 1;
> f
> => 2
> a=2;
> f
> => 2
> ***

Does this help you?

,----
| (%i1) a: 5;
| 
| (%o1)                                  5
| (%i2) f: 'a + 1;
| 
| (%o2)                                a + 1
| (%i3) f;
| 
| (%o3)                                a + 1
| (%i4) ev(f);
| 
| (%o4)                                  6
| (%i5) f, a:3;
| 
| (%o5)                                  4
| (%i6) a;
| 
| (%o6)                                  5
| (%i7) g: ''a+1;
| 
| (%o7)                                  6
| (%i8) g;
| 
| (%o8)                                  6
`----

I do not know how to make things work exactly as in Mathematica, but
there may be some way - maybe with simplification rules?  Anyway,
evaluation is very different in both systems as Mathematica aspires to
do "infinite" evaluation, i.e., it looks for something that looks like
a fixed point; Maxima only evaluates things once, which means that
quoting plays the role of both Hold and Unevaluated.  

There is also

,----
|  - special symbol: INFEVAL
|      leads to an "infinite evaluation" mode.  EV repeatedly evaluates
|      an expression until it stops changing.  To prevent a variable, say
|      X, from being evaluated away in this mode, simply include X='X as
|      an argument to EV.  Of course expressions such as
|      EV(X,X=X+1,INFEVAL); will generate an infinite loop.  CAVEAT
|      EVALUATOR.
`----

but you still have to call EV explicitly, and you do not need INFEVAL
for what you are doing here.

> How to see what exactly the expressions does MAXIMA keep for a symbol or a
> function?

To *really* see that, use the underlying Lisp system's describe:

,----
| (%i11) ?describe(f);
| 
| ((MPLUS SIMP) 1 |$a|) is a CONS.
| (%o11)                               FALSE
| (%i12) ?describe(g);
| 
| It is a composite number.
| (%o12)                               FALSE
`----

The closest to ?? in Mathematica, which shows you Up-, Down-, Sub-,
OwnValues, Attributes, rewrite rules etc. that I know of is to use
fundef and to look at the properties.

But then again, I have only been using Maxima for a short time,
looking only at what I need, so I may as well be missing something
terribly obvious.

HTH,

Albert.