Generate Procedure - Maple Help

CodeTools[ProgramAnalysis]

 GenerateProcedure
 generate a Maple procedure from a ForLoop or WhileLoop

 Calling Sequence GenerateProcedure(loop)

Parameters

 loop - WhileLoop or ForLoop data structure

Description

 • The WhileLoops or ForLoops generated by CreateLoop can be transformed back into a Maple procedure using GenerateProcedure.
 • The returned procedure may not be the same as the original procedure processed using CreateLoop.  Statements that do not affect the loop variables are removed and the loops are put into a canonical form.

Examples

 > $\mathrm{with}\left(\mathrm{CodeTools}\left[\mathrm{ProgramAnalysis}\right]\right):$

Form a procedure:

 > p := proc (A, n)     local i;     for i from 1 to n do        A[i] := A[+1]^2:     end do: end proc;
 ${p}{≔}{\mathbf{proc}}\left({A}{,}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{;}\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}}{A}{[}{i}{]}{≔}{A}{[}{1}{]}{^}{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (1)

Create the related ForLoop data structure:

 > $\mathrm{loop}≔\mathrm{CreateLoop}\left(p\right)$
 ${\mathrm{loop}}{≔}{\mathrm{Record}}{}\left({\mathrm{ForLoop}}{,}{\mathrm{body}}{=}\left[\left[\left[\right]{,}\left[{{A}}_{{i}}{=}{{A}}_{{1}}^{{2}}\right]\right]\right]{,}{\mathrm{layers}}{=}\left[\left[{i}{,}{1}{,}{1}{,}{n}\right]\right]{,}{\mathrm{initialization}}{=}\left[\right]{,}{\mathrm{variables}}{=}\left[{i}\right]{,}{\mathrm{parameters}}{=}\left[{n}\right]{,}{\mathrm{arrays}}{=}\left[{A}\right]\right)$ (2)

Translate the ForLoop loop back into a procedure:

 > $\mathrm{GenerateProcedure}\left(\mathrm{loop}\right)$
 ${\mathbf{proc}}\left({A}{,}{n}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{i}{;}\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}}{A}{[}{i}{]}{≔}{A}{[}{1}{]}{^}{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end do}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (3)

Compatibility

 • The CodeTools[ProgramAnalysis][GenerateProcedure] command was introduced in Maple 2016.