Special Evaluation Rules - Maple Programming Help

Home : Support : Online Help : Mathematics : Evaluation : spec_eval_rules

Special Evaluation Rules

Description

 • Most Maple procedures have what are known as "standard evaluation rules." These rules govern the way in which arguments passed to a procedure are evaluated before being bound to the formal parameters of the procedure.
 Standard evaluation rules mean that arguments are evaluated in an unspecified order, in the scope of the caller, before parameter binding occurs. Both last name evaluation and one level evaluation of locals apply to the evaluation of the argument expressions. Thus, procedures called at the top level are passed their arguments after they have been fully evaluated, while those called within other procedures are passed their arguments after they have been evaluated one level.
 • Some Maple procedures have so-called "special evaluation rules." This means that the arguments are not evaluated according to the standard evaluation rules. Instead, some arguments may not be (fully) evaluated, a specific evaluation order may be prescribed, or a non-standard evaluator may be used to evaluate one or more arguments.
 • Most procedures that have special evaluation rules are builtin. It is possible, however, for a user to write a procedure with special evaluation rules by using an argument declared to be of one of the types uneval and evaln. This can only be done for a fixed number of named arguments. (There is no way to write a procedure that takes an arbitrary number of unevaluated arguments.) It is not possible to alter the order in which the arguments to a procedure are evaluated. (Often, however, this effect can be achieved by suppressing the evaluation of the arguments, and then evaluating the parameters explicitly within the body of the procedure in the desired order.)
 • The following builtin user procedures have special evaluation rules.

 • Writing procedures with special evaluation rules is generally to be avoided unless absolutely necessary for correct operation.

Examples

The following procedure $p$ has its arguments fully evaluated at the top level, but evaluated to only one level when called within the (anonymous) procedure shown.

 > p := proc( x ) print( x ); end proc;
 ${p}{≔}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{print}}{}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)
 > $a≔b$
 ${a}{≔}{b}$ (2)
 > $b≔2$
 ${b}{≔}{2}$ (3)
 > $p\left(a\right)$
 ${2}$ (4)
 > proc()     local a, b;     a := b;     b := 2;     p(a); end proc();
 ${b}$ (5)

Modifying the formal parameter $x$ of $p$ changes it so that it has special evaluation rules.

 > p := proc( x::uneval ) print( x ); end proc;
 ${p}{≔}{\mathbf{proc}}\left({x}{::}{\mathrm{uneval}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{print}}{}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (6)
 > $p\left(a\right)$
 ${a}$ (7)
 > proc()     local a, b;     a := b;     b := 2;     p(a); end proc();
 ${a}$ (8)

The procedure evalf has special evaluation rules because it ensures that its second argument (if provided) is evaluated before its first argument.

 > evalf( proc() print( FIRST ); Pi end(), proc() print( SECOND ); 15 end() );
 ${\mathrm{SECOND}}$
 ${\mathrm{FIRST}}$
 ${3.14159265358979}$ (9)

The procedure assigned uses a non-standard evaluator (evaln) when it evaluates its argument.

 > $\mathrm{assigned}\left(\mathrm{H2}\right)$
 ${\mathrm{false}}$ (10)
 > $\mathrm{evaln}\left(\mathrm{cat}\left('H',2\right)\right)$
 ${\mathrm{H2}}$ (11)
 > $\mathrm{assigned}\left(\mathrm{cat}\left('H',2\right)\right)$
 ${\mathrm{false}}$ (12)
 > $\mathrm{evaln}\left(H||2\right)$
 ${\mathrm{H2}}$ (13)
 > $\mathrm{assigned}\left(H||2\right)$
 ${\mathrm{false}}$ (14)
 > $\mathrm{H2}≔\mathrm{sin}:$
 > $\mathrm{evaln}\left(H||2\right)$
 ${\mathrm{H2}}$ (15)
 > $\mathrm{assigned}\left(H||2\right)$
 ${\mathrm{true}}$ (16)

You can control the order of argument evaluation by suppressing it entirely and making the evaluations explicit in the desired order.

 > p := proc( a::uneval, b::uneval )     local    ea, eb;     eb := eval( b ); # evaluate second     ea := eval( a ); # evaluate first     [ ea, eb ] end proc:
 > p( proc() print(FIRST); 2 end(), proc() print(SECOND); 3 end() );
 ${\mathrm{SECOND}}$
 ${\mathrm{FIRST}}$
 $\left[{2}{,}{3}\right]$ (17)