codegen,intrep2maple - Maple Help

codegen

 maple2intrep
 converts a Maple procedure to an abstract syntax tree
 intrep2maple
 converts an abstract syntax tree to a Maple procedure

 Calling Sequence maple2intrep(f) intrep2maple(t)

Parameters

 f - Maple procedure t - expression (an abstract syntax tree)

Description

 • Important: The maple2intrep and intrep2maple commands have been deprecated.  Use the superseding commands ToInert and FromInert instead to translate between Maple expressions and corresponding inert forms.
 • The maple2intrep function is used to convert a Maple procedure f into an intermediate representation for the procedure that is suitable for manipulating the code of f. The intermediate representation is an abstract syntax tree. The maple2intrep function also deduces the types of all parameters, local variables, and global variables in f. The intrep2maple function is used to convert an abstract syntax tree t to a Maple procedure.
 • The abstract syntax tree has the following structure. Express it by using a modified BNF grammar where:
 * indicates zero or more occurrences
 + indicates one or more occurrences
 [] indicate optional terms, and
 | indicates alternatives.

 tree ::= Proc( Name( declaration ), Parameters( declaration* ), Options( sequence ), Description( string ), Locals( declaration* ), Globals( declarations* ), StatSeq( statement* ) ) declaration ::= name :: type statseq ::= StatSeq( statement* ) statement ::= expression | Assign( name, expression ) | If( [condition, statseq]+, [statseq] ) | For( name, from, by, to, while, statseq ) | For( name, in, while, statseq ) | Return( sequence ) | Error( sequence ) | Break( ) | Next( ) | Read( string ) | Save( sequence ) | Comment( ... ) | Quote( expression ) | Ditto1( ) | Ditto2( ) | Ditto3( ) | tree | Nargs( ) | Args[i] | Args

 • In the abstract syntax tree, the names and Maple expressions become global and will evaluate.
 Note: In the event that the parameter or local names in the procedure have global values, and it is desirable to use local names instead (to prevent evaluation problems with respect to the variable names), the option $\mathrm{outputvars}=\mathrm{locals}$ can be used.
 When working with the abstract syntax tree, be careful not to evaluate parts of the tree, or else, use the tools/rename command to rename all symbols in the tree to unique names that do not evaluate. The library routine tools/unrename can be used to undo this renaming. An example is given below.

Examples

Important: The maple2intrep and intrep2maple commands have been deprecated.  Use the superseding commands ToInert and FromInert instead to translate between Maple expressions and corresponding inert forms.

 > $\mathrm{with}\left(\mathrm{codegen}\right):$
 > f := proc(x) local t; t := sin(x); x*t end proc:
 > $\mathrm{tree}≔\mathrm{maple2intrep}\left(f\right)$
 ${\mathrm{tree}}{≔}{\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{float}}\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}\right){,}{\mathrm{Options}}{}\left(\right){,}{\mathrm{Description}}{}\left(\right){,}{\mathrm{Locals}}{}\left({t}{..}{\mathrm{float}}\right){,}{\mathrm{Globals}}{}\left(\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}{\mathrm{sin}}{}\left({x}\right)\right){,}{x}{}{t}\right)\right)$ (1)
 > $\mathrm{op}\left(2,\mathrm{tree}\right)$
 ${\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}\right)$ (2)
 > $\mathrm{op}\left(5,\mathrm{tree}\right)$
 ${\mathrm{Locals}}{}\left({t}{..}{\mathrm{float}}\right)$ (3)
 > $\mathrm{op}\left(6,\mathrm{tree}\right)$
 ${\mathrm{Globals}}{}\left(\right)$ (4)
 > $\mathrm{intrep2maple}\left(\mathrm{tree}\right)$
 ${\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{\mathrm{sin}}{}\left({x}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{*}{t}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (5)

This example shows the problem of evaluation.

 > f := proc(x,n) local i,t,A;      A := Array(1..n); t := 1; for i to n do t := x*t;  A[i] := t; end do; A end proc:
 > $\mathrm{tree}≔\mathrm{maple2intrep}\left(f\right)$
 ${\mathrm{tree}}{≔}{\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}{,}{n}{..}{\mathrm{integer}}\right){,}{\mathrm{Options}}{}\left(\right){,}{\mathrm{Description}}{}\left(\right){,}{\mathrm{Locals}}{}\left({i}{..}{\mathrm{integer}}{,}{t}{..}{\mathrm{float}}{,}{A}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Globals}}{}\left(\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({A}{,}{\mathrm{Array}}{}\left({1}{..}{n}\right)\right){,}{\mathrm{Assign}}{}\left({t}{,}{1}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}{x}{}{t}\right){,}{\mathrm{Assign}}{}\left({{A}}_{{i}}{,}{t}\right)\right){,}{\mathrm{false}}\right){,}{A}\right)\right)$ (6)
 > $\mathrm{tree}$
 > $\mathrm{eval}\left(\mathrm{tree},1\right)$
 ${\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}{,}{n}{..}{\mathrm{integer}}\right){,}{\mathrm{Options}}{}\left(\right){,}{\mathrm{Description}}{}\left(\right){,}{\mathrm{Locals}}{}\left({i}{..}{\mathrm{integer}}{,}{t}{..}{\mathrm{float}}{,}{A}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Globals}}{}\left(\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({A}{,}{\mathrm{Array}}{}\left({1}{..}{n}\right)\right){,}{\mathrm{Assign}}{}\left({t}{,}{1}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}{x}{}{t}\right){,}{\mathrm{Assign}}{}\left({{A}}_{{i}}{,}{t}\right)\right){,}{\mathrm{false}}\right){,}{A}\right)\right)$ (7)
 > $\mathrm{intrep2maple}\left(\mathrm{eval}\left(\mathrm{tree},1\right)\right)$
 ${\mathbf{proc}}\left({x}{,}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{,}{i}{,}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{≔}{\mathrm{Array}}{}\left({1}{..}{n}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{x}{*}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{[}{i}{]}{≔}{t}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (8)

Rename all symbols in the program with new names.

 > $\mathrm{macro}\left(R=\mathrm{tools/rename}\right)$
 ${R}$ (9)
 > $\mathrm{macro}\left(U=\mathrm{tools/unrename}\right)$
 ${R}{,}{U}$ (10)
 > $\mathrm{tree}≔R\left(\mathrm{maple2intrep}\left(f\right)\right)$
 ${\mathrm{tree}}{≔}{\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}{,}{n}{..}{\mathrm{integer}}\right){,}{\mathrm{Options}}{}\left(\right){,}{\mathrm{Description}}{}\left(\right){,}{\mathrm{Locals}}{}\left({i}{..}{\mathrm{integer}}{,}{t}{..}{\mathrm{float}}{,}{A}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Globals}}{}\left(\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({A}{,}{\mathrm{Array}}{}\left({1}{..}{n}\right)\right){,}{\mathrm{Assign}}{}\left({t}{,}{1}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}{x}{}{t}\right){,}{\mathrm{Assign}}{}\left({{A}}_{{i}}{,}{t}\right)\right){,}{\mathrm{false}}\right){,}{A}\right)\right)$ (11)
 > $\mathrm{tree}$
 ${\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}{,}{n}{..}{\mathrm{integer}}\right){,}{\mathrm{Options}}{}\left(\right){,}{\mathrm{Description}}{}\left(\right){,}{\mathrm{Locals}}{}\left({i}{..}{\mathrm{integer}}{,}{t}{..}{\mathrm{float}}{,}{A}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Globals}}{}\left(\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({A}{,}{\mathrm{Array}}{}\left({1}{..}{n}\right)\right){,}{\mathrm{Assign}}{}\left({t}{,}{1}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}{x}{}{t}\right){,}{\mathrm{Assign}}{}\left({{A}}_{{i}}{,}{t}\right)\right){,}{\mathrm{false}}\right){,}{A}\right)\right)$ (12)
 > $\mathrm{has}\left(\mathrm{tree},A\right)$
 ${\mathrm{false}}$ (13)
 > $\mathrm{has}\left(\mathrm{tree},R\left(A\right)\right)$
 ${\mathrm{true}}$ (14)

Make a simple transformation on the program represented in the intermediate representation.  Make the array A into a global variable.

 > $\mathrm{globals}≔R\left(\mathrm{Globals}\left(A\right)\right)$
 ${\mathrm{globals}}{≔}{\mathrm{Globals}}{}\left({A}\right)$ (15)
 > $\mathrm{locals}≔\mathrm{subsop}\left(3=\mathrm{NULL},\mathrm{op}\left(5,\mathrm{tree}\right)\right)$
 ${\mathrm{locals}}{≔}{\mathrm{Locals}}{}\left({i}{..}{\mathrm{integer}}{,}{t}{..}{\mathrm{float}}\right)$ (16)
 > $\mathrm{tree}≔\mathrm{subsop}\left(5=\mathrm{locals},6=\mathrm{globals},\mathrm{tree}\right)$
 ${\mathrm{tree}}{≔}{\mathrm{Proc}}{}\left({\mathrm{Name}}{}\left({f}{..}{\mathrm{List}}{}\left({1}{..}{n}{,}{\mathrm{float}}\right)\right){,}{\mathrm{Parameters}}{}\left({x}{..}{\mathrm{float}}{,}{n}{..}{\mathrm{integer}}\right){,}{\mathrm{Options}}{}\left(\right){,}{\mathrm{Description}}{}\left(\right){,}{\mathrm{Locals}}{}\left({i}{..}{\mathrm{integer}}{,}{t}{..}{\mathrm{float}}\right){,}{\mathrm{Globals}}{}\left({A}\right){,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({A}{,}{\mathrm{Array}}{}\left({1}{..}{n}\right)\right){,}{\mathrm{Assign}}{}\left({t}{,}{1}\right){,}{\mathrm{For}}{}\left({i}{,}{1}{,}{1}{,}{n}{,}{\mathrm{true}}{,}{\mathrm{StatSeq}}{}\left({\mathrm{Assign}}{}\left({t}{,}{x}{}{t}\right){,}{\mathrm{Assign}}{}\left({{A}}_{{i}}{,}{t}\right)\right){,}{\mathrm{false}}\right){,}{A}\right)\right)$ (17)
 > $\mathrm{intrep2maple}\left(U\left(\mathrm{tree}\right)\right)$
 ${\mathbf{proc}}\left({x}{,}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{,}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{global}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{≔}{\mathrm{Array}}{}\left({1}{..}{n}\right){;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{1}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{for}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{to}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{n}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{t}{≔}{x}{*}{t}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}{[}{i}{]}{≔}{t}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{A}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (18)