# Maple, Assignment, Partial Evaluation, Lazy Evaluation Anonymous Functions

Discussion in 'Maple' started by John Creighton, Apr 2, 2005.

1. ### John CreightonGuest

I've been told Maples rules of assignment are intuitive and not much
different then one would expect coming from a language like c. I would
like to explore this further. I can type

x:='y'+1

and I get:

y+1

But if I tytpe:

x:=x+1

It behaves like the c language if x is defined and if x is not defined
I get:

??? Error using ==> maple
Error, too many levels of recursion

If I try blocking the evaluation by typing

x:='x'+1;

I get error

??? Error using ==> maple
Error, recursive assignment

I don't see why x can't be assigned the expression, so the expression
can be later extracted using eval(x,1). Anyway, maple doesn't allow
this but it does allow:

x:=''x''+1;
Or equivalently
x:=''x'+1';
and returns:
x := ('x')+1

This is interesting because it gives a convent way to represent
iteration of the same expression. For instance if one typed
x:=''x''+1;
expand(eval(x,6));
They would get:
x^8+4*x^6+8*x^4+8*x^2+5
To me it is not at all obvious why if you use a variable that is on
the left of assignment you need two quotes but if you use a different
variable you need one quote.

Furthing thinking of how this might be useful one could evaluate a
factorial like :
x:=''x*(x-1)''';
eval(subs(x=3,eval(x,3)));

One can use this to generate weird sequences. Consider:

x:=''x''+'y'+1;
y:=''y''-'x'+1;
maple('eval(x,n)');

Clearly maples rules of assignment provide a handy way of doing
substitution but the delayed evaluation is not as powerful as lazy
evaluation. For instance I can't recursively define an infite list (in
the lazy evaluation sense) by typing
x:=[''op(x)'',''nop(x)+1'']
it is not obvious how to do this without anonymous functions but you
cannot easily right an anonyms function that you can partial evaluate.
For instance the expression if x is set to
x:=x->`if`(x<1,1,x*(x-1))
then x cannot be partially evaluated
x(2)
ans =
2
eval(x(2),n) give
x(2) for any n
and eval(x(2)) gives
2
One could try to delay the evaluation with a complicated expression
like
x:=z->((y->`if`(y<=1,1,y* ''x('' y-1 '')''))(eval(z)))
but then maple will only perform partial evaluation. To fully evaluate
the expression for x(4) you must type
eval(eval(eval(eval(eval(eval(x(4)))))))
(note that this is an example of continuation)
The lack of consistency really has me baffled as to the motivation
behind maples design. I see can see how you can use the rules of
assignment to get maple to do what you want but I don't understand the
motivation behind there choices and consequently I don't have a good
grasp about the most basic principles of how maple works.

John Creighton, Apr 2, 2005

2. ### Robert IsraelGuest

It has to be rather different, because c doesn't have symbolic values.
The point is that if you did assign the value x+1 to x, this could
never be fully evaluated, because in order to evaluate x you would have
to first evaluate x.
No, it would just be x+3. Evaluation to one level gives 'x'+1, two
levels gives x+1, three levels 'x'+2, etc.
It's not particularly interesting: just a question of how deeply you
have to hide the x to fool Maple's mechanism for detecting recursive
assignments.
I have no idea what you're trying to do here. It's easy enough to
define legitimate recursive functions without resorting to subterfuge.
You're just confusing yourself by using the same letter for
the name of the procedure and the formal parameter. This is the
same as

x:= t -> `if`(t<1, 1, t*(t-1));

Robert Israel
Department of Mathematics http://www.math.ubc.ca/~israel
University of British Columbia Vancouver, BC, Canada

Robert Israel, Apr 4, 2005

3. ### JohnCreighton_Guest

opps, I ment
x:=''x''^2+1

JohnCreighton_, Apr 4, 2005
4. ### devoreGuest

Note that each assignment performs one level of evaluation (i.e.,
removes one level of quotes).
intended to write. Anyway, the problem with using this for iteration
or recursion is that the expressions are unstable. Every time you use
them, they change. For example:

x:= ''x''^2+1;
x := 'x'^2+1
%;
x^2+1
%;
(x^2+1)^2+1
%;
((x^2+1)^2+1)^2+1

What you are trying to achieve can be done better as

x:= x-> x^2+1:
([email protected]@3)(x);
((x^2+1)^2+1)^2+1

I did not need to use x on both sides of the := to do that; I was
following your style; any symbol would work.
If you evaluate this further (to more than 3 levels), you do get a
recursive function, but it is not factorial. One again, it is very
easy write a one-line factorial using legitimate Maple syntax.

F:= x-> x*F(x-1): F(0):= 1:
F(40);
815915283247897734345611269596115894272000000000
What is the 'maple' command? There is no command by that name in my
Maple.
evaluate.

x:= x-> [op(x), nops(x)+1]:
([email protected]@4)([]);
[1, 2, 3, 4]
My guess is that it was not at all designed to do these things. The
fact that it works sometimes is just a coincidence. Maple usually does
not waste a lot of time checking user input. If they cared to take the
time, I believe the designers would have disallowed recursive
assignments entirely.
Most things can be figured out by using the commands addressof,
disassemble, pointto, assemble, ToInert, and FromInert and by reading
the appendices of the Advanced Programming Guide.

devore, Apr 5, 2005