MaxEvalDepth | set the maximum evaluation depth |
Hold | keep expression unevaluated |
Eval | force evaluation of expression |
While | loop while a condition is met |
Until | loop until a condition is met |
If | branch point |
SystemCall | pass a command to the shell |
Function | declare or define a function |
Use | load a file, but not twice |
For | C-style for loop |
ForEach | loop over all entries in list |
Apply | apply a function to arguments |
MapArgs | apply a function to all top-level arguments |
Subst | perform a substitution |
WithValue | temporary assignment during an evaluation |
/:, /:: | local simplification rules |
SetHelpBrowser | set the HTML browser to use for help |
TraceStack | show calling stack after an error occurs |
TraceExp | evaluate with tracing enabled |
TraceRule | turn on tracing for a particular function |
MaxEvalDepth(n) |
The point of having a maximum evaluation depth is to catch any infinite recursion. For example, after the definition f(x) := f(x), evaluating the expression f(x) would call f(x), which would call f(x), etcetera. The interpreter will halt if the maximum evaluation depth is reached. Also indirect recursion, e.g. the pair of definitions f(x) := g(x) and g(x) := f(x), will be caught.
In> f(x) := f(x) Out> True; In> f(x) Error on line 1 in file [CommandLine] Max evaluation stack depth reached. Please use MaxEvalDepth to increase the stack size as needed. |
However, a long calculation may cause the maximum evaluation depth to be reached without the presence of infinite recursion. The function MaxEvalDepth is meant for these cases.
In> 10 # g(0) <-- 1; Out> True; In> 20 # g(n_IsPositiveInteger) <-- \ 2 * g(n-1); Out> True; In> g(1001); Error on line 1 in file [CommandLine] Max evaluation stack depth reached. Please use MaxEvalDepth to increase the stack size as needed. In> MaxEvalDepth(10000); Out> True; In> g(1001); Out> 21430172143725346418968500981200036211228096234 1106721488750077674070210224987224498639675763139171 6255189345835106293650374290571384628087196915514939 7149607869135549648461970842149210124742283755908364 3060929499671638825347975351183310878921541258291423 92955373084335320859663305248773674411336138752; |
Hold(expr) |
The function UnList() also leaves its result unevaluated. Both functions stop the process of evaluation (no more rules will be applied).
In> Echo({ Hold(1+1), "=", 1+1 }); 1+1 = 2 Out> True; |
Eval(expr) |
In> a := x; Out> x; In> x := 5; Out> 5; In> a; Out> x; In> Eval(a); Out> 5; |
The variable a is bound to x, and x is bound to 5. Hence evaluating a will give x. Only when an extra evaluation of a is requested, the value 5 is returned.
Note that the behaviour would be different if we had exchanged the assignments. If the assignment a := x were given while x had the value 5, the variable a would also get the value 5 because the assignment operator := evaluates the right-hand side.
While(pred) body |
body -- expression to loop over
In particular, if "pred" immediately evaluates to False, the body is never executed. While is the fundamental looping construct on which all other loop commands are based. It is equivalent to the while command in the programming language C.
In> x := 0; Out> 0; In> While (x! < 10^6) \ [ Echo({x, x!}); x++; ]; 0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 Out> True; |
Until(pred) body |
body -- expression to loop over
The main difference with While is that Until always evaluates the body at least once, but While may not evaluate the body at all. Besides, the meaning of the predicate is reversed: While stops if "pred" is False while Until stops if "pred" is True. The command Until(pred) body; is equivalent to pred; While(Not pred) body;. In fact, the implementation of Until is based on the internal command While. The Until command can be compared to the do ... while construct in the programming language C.
In> x := 0; Out> 0; In> Until (x! > 10^6) \ [ Echo({x, x!}); x++; ]; 0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 Out> True; |
If(pred, then) If(pred, then, else) |
then -- expression to evaluate if "pred" is True
else -- expression to evaluate if "pred" is False
In> mysign(x) := If (IsPositiveReal(x), 1, -1); Out> True; In> mysign(Pi); Out> 1; In> mysign(-2.5); Out> -1; |
Note that this will give incorrect results, if "x" cannot be numerically approximated.
In> mysign(a); Out> -1; |
Hence a better implementation would be
In> mysign(_x)_IsNumber(N(x)) <-- If \ (IsPositiveReal(x), 1, -1); Out> True; |
SystemCall(str) |
This command is not allowed in the body of the Secure command and will lead to an error.
Function() func(arglist) Function() func(arglist, ...) Function("op", {arglist}) body Function("op", {arglist, ...}) body |
"op" -- string, name of the function
{arglist} -- list of atoms, formal arguments to the function
... -- literal ellipsis symbol "..." used to denote a variable number of arguments
body -- expression comprising the body of the function
The number of arguments of the new function and their names are determined by the list arglist. If the ellipsis "..." follows the last atom in arglist, a function with a variable number of arguments is declared (using RuleBaseListed). Note that the ellipsis cannot be the only element of arglist and must be preceded by an atom.
A function with variable number of arguments can take more arguments than elements in arglist; in this case, it obtains its last argument as a list containing all extra arguments.
The short form of the Function call merely declares a RuleBase for the new function but does not define any function body. This is a convenient shorthand for RuleBase and RuleBaseListed, when definitions of the function are to be supplied by rules. If the new function has been already declared with the same number of arguments (with or without variable arguments), Function returns false and does nothing.
The second, longer form of the Function call declares a function and also defines a function body. It is equivalent to a single rule such as op(_arg1, _arg2) <-- body. The rule will be declared at precedence 1025. Any previous rules associated with "op" (with the same arity) will be discarded. More complicated functions (with more than one body) can be defined by adding more rules.
In> Function() f1(x,y,...); Out> True; In> Function() f1(x,y); Out> False; |
This defines a function FirstOf which returns the first element of a list. Equivalent definitions would be FirstOf(_list) <-- list[1] or FirstOf(list) := list[1].
In> Function("FirstOf", {list}) list[1]; Out> True; In> FirstOf({a,b,c}); Out> a; |
The following function will print all arguments to a string:
In> Function("PrintAll",{x, ...}) If(IsList(x), PrintList(x), ToString()Write(x)); Out> True; In> PrintAll(1): Out> " 1"; In> PrintAll(1,2,3); Out> " 1 2 3"; |
Use(name) |
The purpose of this function is to make sure that the file will at least have been loaded, but is not loaded twice.
For(init, pred, incr) body |
pred -- predicate deciding whether to continue the loop
incr -- expression to increment the counter
body -- expression to loop over
This command is most often used in a form such as For(i=1, i<=10, i++) body, which evaluates body with i subsequently set to 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.
The expression For(init, pred, incr) body is equivalent to init; While(pred) [body; incr;].
In> For (i:=1, i<=10, i++) Echo({i, i!}); 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880 10 3628800 Out> True; |
ForEach(var, list) body |
list -- list of values to assign to "var"
body -- expression to evaluate with different values of "var"
In> ForEach(i,{2,3,5,7,11}) Echo({i, i!}); 2 2 3 6 5 120 7 5040 11 39916800 Out> True; |
Apply(fn, arglist) |
arglist -- list of arguments
An shorthand for Apply is provided by the @ operator.
In> Apply("+", {5,9}); Out> 14; In> Apply({{x,y}, x-y^2}, {Cos(a), Sin(a)}); Out> Cos(a)-Sin(a)^2; |
MapArgs(expr, fn) |
fn -- an operation to perform on each argument
In> MapArgs(f(x,y,z),"Sin"); Out> f(Sin(x),Sin(y),Sin(z)); In> MapArgs({3,4,5,6}, {{x},x^2}); Out> {9,16,25,36}; |
Subst(from, to) expr |
to -- expression to substitute for "from"
expr -- expression in which the substitution takes place
In> Subst(x, Sin(y)) x^2+x+1; Out> Sin(y)^2+Sin(y)+1; In> Subst(a+b, x) a+b+c; Out> x+c; In> Subst(b+c, x) a+b+c; Out> a+b+c; |
The explanation for the last result is that the expression a+b+c is internally stored as (a+b)+c. Hence a+b is a subexpression, but b+c is not.
WithValue(var, val, expr) WithValue({var,...}, {val,...}, expr) |
val -- value to be assigned to "var"
expr -- expression to evaluate with "var" equal to "val"
The second calling sequence assigns the first element in the list of values to the first element in the list of variables, the second value to the second variable, etcetera.
In> WithValue(x, 3, x^2+y^2+1); Out> y^2+10; In> WithValue({x,y}, {3,2}, x^2+y^2+1); Out> 14; |
expression /: patterns expressions /:: patterns |
patterns -- a list of patterns
In> Sin(x)*Ln(a*b) Out> Sin(x)*Ln(a*b); In> % /: { Ln(_x*_y) <- Ln(x)+Ln(y) } Out> Sin(x)*(Ln(a)+Ln(b)); |
A whole list of simplification rules can be built up in the list, and they will be applied to the expression on the left hand side of /: .
The forms the patterns can have are one of:
pattern <- replacement {pattern,replacement} {pattern,postpredicate,replacement} |
Note that for these local rules, <- should be used instead of <-- which would be used in a global rule.
The /: operator traverses an expression much as Subst does, that is, top down, trying to apply the rules from the begining of the list of rules to the end of the list of rules. If the rules cannot be applied to an expression, it will try subexpressions of that expression and so on.
It might be necessary sometimes to use the /:: operator, which repeatedly applies the /: operator until the result doesn't change any more. Caution is required, since rules can contradict each other, which could result in an infinite loop. To detect this situation, just use /: repeatedly on the expression. The repetitive nature should become apparent.
In> Sin(u)*Ln(a*b) /: {Ln(_x*_y) <- Ln(x)+Ln(y)} Out> Sin(u)*(Ln(a)+Ln(b)); In> Sin(u)*Ln(a*b) /:: { a <- 2, b <- 3 } Out> Sin(u)*Ln(6); |
SetHelpBrowser(helpbrowser) |
In> SetHelpBrowser("netscape") Out> "netscape"; In> ?? |
TraceStack(expression) |
For each stack frame, it shows if the function evaluated was a built-in function or a user-defined function, and for the user-defined function, the number of the rule it is trying whether it was evaluating the pattern matcher of the rule, or the body code of the rule.
This functionality is not offered by default because it slows down the evaluation code.
In> f(x):=f(Sin(x)) Out> True; In> TraceStack(f(2)) Debug> 982 : f (Rule # 0 in body) Debug> 983 : f (Rule # 0 in body) Debug> 984 : f (Rule # 0 in body) Debug> 985 : f (Rule # 0 in body) Debug> 986 : f (Rule # 0 in body) Debug> 987 : f (Rule # 0 in body) Debug> 988 : f (Rule # 0 in body) Debug> 989 : f (Rule # 0 in body) Debug> 990 : f (Rule # 0 in body) Debug> 991 : f (Rule # 0 in body) Debug> 992 : f (Rule # 0 in body) Debug> 993 : f (Rule # 0 in body) Debug> 994 : f (Rule # 0 in body) Debug> 995 : f (User function) Debug> 996 : Sin (Rule # 0 in pattern) Debug> 997 : IsList (Internal function) Error on line 1 in file [CommandLine] Max evaluation stack depth reached. Please use MaxEvalDepth to increase the stack size as needed. |
TraceExp(expr) |
Note that this command usually generates huge amounts of output. A more specific form of tracing (eg. TraceRule) is probably more useful for all but very simple expressions.
In> TraceExp(2+3); TrEnter(2+3); TrEnter(2); TrLeave(2, 2); TrEnter(3); TrLeave(3, 3); TrEnter(IsNumber(x)); TrEnter(x); TrLeave(x, 2); TrLeave(IsNumber(x),True); TrEnter(IsNumber(y)); TrEnter(y); TrLeave(y, 3); TrLeave(IsNumber(y),True); TrEnter(True); TrLeave(True, True); TrEnter(MathAdd(x,y)); TrEnter(x); TrLeave(x, 2); TrEnter(y); TrLeave(y, 3); TrLeave(MathAdd(x,y),5); TrLeave(2+3, 5); Out> 5; |
TraceRule(template) expr |
expr -- expression to evaluate with tracing on
This is useful for tracing a function that is called from within another function. This way you can see how your function behaves in the environment it is used in.
In> TraceRule(x+y) 2+3*5+4; TrEnter(2+3*5+4); TrEnter(2+3*5); TrArg(2, 2); TrArg(3*5, 15); TrLeave(2+3*5, 17); TrArg(2+3*5, 17); TrArg(4, 4); TrLeave(2+3*5+4, 21); Out> 21; |