ModuleApply - Maple Help

ModuleApply

function to apply when calling a module as if it were a command

 Calling Sequence module() local ModuleApply, ...; ... end module; module() export ModuleApply, ...; ... end module;

Description

 • If a module has a member named ModuleApply, the module name can be used as if it were a procedure name.  Calling M(args) invokes M:-ModuleApply(args).
 • There is no difference between making ModuleApply a local or export of the module when calling the procedure as M(args). It often makes more sense to make ModuleApply a local: that reinforces to the user of the module that they should use the M(args) rather than the M:-ModuleApply(args) calling sequence. This, in turn, leaves the programmer free to later change M back into a plain procedure without changing its user-facing behavior.
 • Maple's module data structure is close to the "class" concept in object oriented programming.  Due to the dynamic nature of Maple, modules can also be used to represent "objects".  One major difference between classes and objects are constructors.  Constructors can be nicely emulated using generated modules. ModuleApply provides the ability to treat a module as a function and make it "apply-able".

Examples

Example (1)

 > m := module() local ModuleApply;     ModuleApply := proc()         print("m called with args: ", [args]);     end proc; end module;
 ${m}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ModuleApply}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (1)
 > $m\left(1,2\right)$
 ${"m called with args:"}{,}\left[{1}{,}{2}\right]$ (2)

Example (2)

In this example, we create a generic function that can deal with objects that export an Evalf function.

 > MyEvalf := proc(f)     if type(f,function) and type(op(0,f),module(Evalf)) then         op(0,f):-Evalf(op(f));     else         error "cannot evaluate %1", f;     end if; end proc;
 ${\mathrm{MyEvalf}}{≔}{\mathbf{proc}}\left({f}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{type}}{}\left({f}{,}{\mathrm{function}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{and}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{type}}{}\left({\mathrm{op}}{}\left({0}{,}{f}\right){,}{\mathrm{module}}{}\left({\mathrm{Evalf}}\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{then}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{op}}{}\left({0}{,}{f}\right){:-}{\mathrm{Evalf}}{}\left({\mathrm{op}}{}\left({f}\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{else}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{error}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{"cannot evaluate %1"}{,}{f}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end if}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (3)

Now, create an object that returns unevaluated under certain conditions.  Teach the object how to behave in specific environments (like the environment of MyEvalf).

 > Sine := module() local ModuleApply; export Evalf;     ModuleApply := proc(x)         if type(x,float) then             sin(x);         else             'Sine'(x);         end if;     end proc;     Evalf := proc(x)         evalf/sin(x);     end proc; end module;
 ${\mathrm{Sine}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ModuleApply}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Evalf}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (4)

With floating-point data, a number is returned, otherwise Sine returns unevaluated.  This is achieved by calling the ModuleApply.

 > $\mathrm{Sine}\left(0.3\right)$
 ${0.2955202067}$ (5)
 > $\mathrm{Sine}\left(3\right)$
 ${\mathrm{Sine}}{}\left({3}\right)$ (6)

Applying MyEvalf to the unevaluated response causes MyEvalf to dispatch to the object itself to compute the value.

 > $\mathrm{MyEvalf}\left(\mathrm{Sine}\left(3\right)\right)$
 ${0.1411200081}$ (7)

The Cache module implements a ModuleApply function. Using Cache as a command creates a new cache table. For example:

 > $\mathrm{Cache}\left(128\right)$
 ${\mathrm{Cache}}{}\left({128}\right)$ (8)