Printer - Maple Help

Home : Support : Online Help : Programming : Code Generation Package : Language Definition : Printer : CodeGeneration/LanguageDefinition/Printer/AddFunction

Printer

 add function definition to Printer module
 GetFunction
 get function definition from Printer module

 Calling Sequence Printer:-AddFunction(fname, signature, translation, libs, opts) Printer:-AddFunctions(list1, list2, ...) Printer:-GetFunction(fname, signature)

Parameters

 Printer - Printer module fname - string; name of function signature - type signature for function fname translation - string or procedure; translation for function fname libs - (optional) equation of the form library=l opts - (optional) equations of the form integer=iprec, numeric=nprec list1, list2, ... - lists of the form [fname, signature, translation, libs, opts], with fname, signature, translation, libs, opts as defined above

Description

 • The AddFunction command defines an equivalent in a target language for a Maple function with name fname and function signature signature. The AddFunctions command defines multiple targets in a single calling sequence.
 • The GetFunction command returns a previously-defined target language equivalent for fname.
 • The optional parameter libs is an equation of the form library=l, where l is a string or list of strings corresponding to libraries needed for translation to function properly in the target language.  This is identical in effect to writing translation as a procedure, and including AddLibrary commands for each of the libraries in l in the body of translation.

Function Signatures

 A function signature is an expression which is either a type, a expression of the form $[\mathrm{type1},...,\mathrm{typen}]::\mathrm{type0}$, or an expression of the form anything::type0.  The meaning associated with each of these is as follows.
 • A type - This indicates that fname is a special constant (for example, Pi) with type signature.
 • An expression of the form $[\mathrm{type1},...,\mathrm{typen}]::\mathrm{type0}$ - This indicates that fname is a function accepting $n$ arguments, with types given by type1 to typen respectively, with return type type0.
 • An expression of the form anything::type0 - This indicates that fname is a function accepting an arbitrary number of arguments of any type, with return type type0.

Using Multiple Function Signatures

 • There can be multiple function signatures for a function name fname. If so, CodeGeneration attempts to match a given function call with the most appropriate signature, based on context.
 Note: Maple uses the set of recognized type signatures in its type deductions, so it is useful to include as many type signatures as possible, even when one type signature is a special case of another. For example, suppose an implementation of abs function can accept both integer and float input (returning integer or float output respectively). This is equivalent to the function signatures $\left[\mathrm{integer}\right]::\mathrm{integer}$ and $\left[\mathrm{numeric}\right]::\mathrm{numeric}$.  To avoid needless type coercions, both of these type signatures should be explicitly specified in the language definition.

Examples

Define target-language equivalents for the sin and csc functions.

 > $\mathrm{with}\left(\mathrm{CodeGeneration}\right):$
 > $\mathrm{Printer}≔\mathrm{LanguageDefinition}[\mathrm{DefaultPrinter}]\left(\right):$
 > $\mathrm{Printer}:-\mathrm{AddFunction}\left("sin",\left[\mathrm{numeric}\right]::\mathrm{numeric},"sin"\right)$
 $\left[{"sin"}{,}\left\{{}\right\}\right]$ (1)
 > Printer:-AddFunction("csc", [numeric]::numeric,     proc(a) Printer:-Print("1.0/sin(",a,")") end proc, numeric='double');
 $\left[{\mathbf{proc}}\left({a}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Printer}}{:-}{\mathrm{Print}}{}\left({"1.0/sin\left("}{,}{a}{,}{"\right)"}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}\left\{{}\right\}\right]$ (2)