Application Center - Maplesoft

App Preview:

Maple Programming: 2.6: Last name evaluation

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

2.06.mws

Programming in Maple

Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet

roger@calumet.purdue.edu

2.6. Last name evaluation

Maple uses the full evaluation rule most of the time, but as we have already seen, there are exceptions to this rule. Three exceptions that we have seen so far are delayed evaluation (i.e. using right quotes), forcing certain levels of evaluation with the eval  command, and not evaluating names on the left hand side of an assignment operator. Another exception to the full evaluation rule is the last name evaluation rule  that is used for the names of tables, arrays, procedures, and Maple functions. We have not discussed tables, arrays, and procedures yet, so for now we will only use Maple functions as examples for the last name evaluation rule. Here are some examples that demonstrate this rule. Let us define a Maple function named f .

>    f := x -> x^2-1;

f := proc (x) options operator, arrow; x^2-1 end proc

If we ask Maple to evaluate the name f , it just returns f .

>    f;

f

To find out the definition of f  we have to use the eval  command to force full evaluation of the name f .

>    eval( f );

proc (x) options operator, arrow; x^2-1 end proc

Now let h  be a name for g  and let g  be a name for f .

>    h := 'g';

>    g := 'f';

h := g

g := f

Now ask Maple to evaluate the names g  and h .

>    g;

f

>    h;

f

Notice that the evaluation of both g  and h  stopped with the name f . On the other hand, if we force full evaluation of either g  or h  then we get the definition of f .

>    eval( g );

>    eval( h );

proc (x) options operator, arrow; x^2-1 end proc

proc (x) options operator, arrow; x^2-1 end proc

Now we can state the last name evaluation rule. If the full evaluation of a name would return the definition of a Maple function (i.e., an expression that uses the arrow operator), then Maple will stop the evaluation at the last name it reaches just before the function definition and Maple will return that name. Hence the term "last name evaluation" for the evaluation rule of names that point to a function definition.

>   

But Maple does not always use last name evaluation for names that evaluate fully to a function definition. Here is a counter example.

>    f:='f': g:='g': h:='h': w:='w':

>    f := g;

f := g

>    g := h;

g := h

>    h := k;

h := k

>    k := x -> x^2;

k := proc (x) options operator, arrow; x^2 end proc

If we now ask Maple to evaluate just the name f , then it will use last name evaluation.

>    f;

k

But if we ask Maple to evaluate the functional notation f(w) , then Maple will not use last name evaluation.

>    f(w);

w^2

If Maple had used last name evaluation in this last command, then the result would have been k(w) . Maple instead used full evaluation on the name f  which returned the definition of k  which Maple then applied to the input of the function, w , and returned w^2 .

>   

Maple uses last name evaluation when it evaluates a name of a function in isolation. But if the function name is part of a functional notation, then Maple uses full evaluation of the function name so that it can get to the definition of the function (if there is one). We will say more about this in the next section.

>   

We end this section with an example where the eval  command uses last name evaluation when it is supposed to be using full evaluation. Make the following two assignments.

>    f := 'g'+4; g := x -> x^2;

f := g+4

g := proc (x) options operator, arrow; x^2 end proc

Now notice that the eval  command, which is supposed to do full evaluation, uses last name evaluation.

>    eval( f );

g+4

In this example, last name evaluation is the same as one level of evaluation.

>    eval( f, 1 );

g+4

We can get eval  to do full evaluation by forcing two levels of evaluation.

>    eval( f, 2 );

proc (x) options operator, arrow; x^2 end proc+4

>   

>   

>