 last name evaluation - Maple Help

Home : Support : Online Help : Mathematics : Evaluation : last name evaluation

Last Name Evaluation

 Maple supports the concept of "last name evaluation." This refers to the specific evaluation rules applied to certain kinds of expressions.
 Most Maple expressions are evaluated using full recursive evaluation. This implies that each name in the expression is fully evaluated to the last assigned expression in any chain of assignments. For example, names that are assigned integers are subject to normal, full evaluation:
 > b := a;
 ${b}{≔}{a}$ (1)
 > b;
 ${a}$ (2)
 > a := 2;
 ${a}{≔}{2}$ (3)
 > a;
 ${2}$ (4)
 > b;
 ${2}$ (5)
 However, a name assigned a value of one of the special types procedure, module, or table is not fully evaluated during normal evaluation. Assignment chains are only evaluated to the last name assigned in the chain (hence the name of this property).
 Note: The deprecated types array, matrix, and vector also use last name evaluation. These are superseded by the rtable-based Array, Matrix, and Vector, which do not use last name evaluation.
 > v := u;
 ${v}{≔}{u}$ (6)
 > v;
 ${u}$ (7)
 > u := table( [ 1 = "a", 2 = "b" ] );
 ${u}{≔}{table}{}\left(\left[{1}{=}{"a"}{,}{2}{=}{"b"}\right]\right)$ (8)
 > u;
 ${u}$ (9)
 > v;
 ${u}$ (10)
 To obtain the final expression to which the last name in an assignment chain is assigned, you can use the function eval.
 > u := table( [ "a" = 1, "b" = 2, "c" = 3 ] );
 ${u}{≔}{table}{}\left(\left[{"a"}{=}{1}{,}{"b"}{=}{2}{,}{"c"}{=}{3}\right]\right)$ (11)
 > u;
 ${u}$ (12)
 > eval( u );
 ${table}{}\left(\left[{"a"}{=}{1}{,}{"b"}{=}{2}{,}{"c"}{=}{3}\right]\right)$ (13)
 This is frequently required when returning such expressions from procedures. A procedure that returns a table or another procedure normally requires an evaluation at the return site, using the two-argument form of eval in which the second argument is 1.
 For example,
 > f := proc( n )     local p;     p := proc( s ) modp( 1 + s, n ) end;     p end proc:
 > f( 5 );
 ${p}$ (14)
 returns the escaped local variable name p, rather than the procedure that it is assigned. (Of course, the local variable p is still assigned the procedure.) By using a call to eval on the return expression,
 > f := proc( n )     local p;     p := proc( s ) modp( 1 + s, n ) end;     eval( p, 1 ) end proc:
 > f( 5 );
 ${\mathbf{proc}}\left({s}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{modp}}{}\left({s}{+}{1}{,}{5}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (15)
 the actual procedure is returned.
 Although Objects are a special form of module, they are not subject to last name evaluation. For example, a MutableSet is an object: a variable that is assigned a MutableSet object will evaluate to the object, not to the variable name:
 > ms := MutableSet(1, 2, 3);
 ${\mathrm{ms}}{≔}{\mathrm{MutableSet}}{}\left({1}{,}{2}{,}{3}\right)$ (16)
 > ms;
 ${\mathrm{MutableSet}}{}\left({1}{,}{2}{,}{3}\right)$ (17)
 > type(ms, 'object');
 ${\mathrm{true}}$ (18)
 The type last_name_eval can be used to test if a name is assigned a value subject to last name evaluation:
 > type(u, 'last_name_eval');
 ${\mathrm{true}}$ (19)
 > type(ms, 'last_name_eval');
 ${\mathrm{false}}$ (20)
 Last name evaluation rules for parameter or local names within procedures or modules, or exported names within modules, differ from the rules for global names:
 • Procedure parameter names are never subject to last name evaluation. Unlike local or global variables, parameters do not exist as variables, but only as values. Thus, a reference to a parameter always yields its value.
 • A local or exported variable whose value is a module always evaluates to that module when evaluated within the procedure or module (or nested procedure or module therein) in which the variable was declared. Last name evaluation applies only when the variable's value is of type procedure, table, array, matrix, or vector, or when the name is evaluated outside the scope in which was declared.
 • Fields of a packed Record, although conceptually exported module member names, are never subject to last name evaluation. Like procedure parameters, no actual names are allocated for packed Record fields, thus there is no "last name" to evaluate to.