bless – bless a function as belonging to a MuPAD domain
bless(f, mydomain) blesses the function f as belonging to the MuPAD domain mydomain
Call:
bless(f, mydomain)
Parameters:
f: |
a MuPAD function |
mydomain: |
a MuPAD domain |
Return Value:
a copy of f, as a member of the domain mydomain
Details:
The call bless(f, mydomain) returns a copy of f which as been blessed as a member of the domain mydomain. That is, the value of dom inside of the copy of f is set to mydomain exactly as if f had been assigned to the domain with mydomain::dummy := f.
Ideally, there would never be a need for using this function. However, due to the specific behaviour of dom which does not have a standard lexical scope, there are some cases where it is helpful for the programmer to be able to set it explicitly. This kind of situation appears typically when generating code on the fly, or when having a table of functions in a domain.
We create a domain, with a power function, and a function curry which returns a specialization of this function for a given n. Of course, this simple example is artificial, but this kind of situation appears naturally when generating code on the fly:
domain mydomain
power := (n,x) -> x^n;
curry :=
proc(n)
option escape;
begin
x -> dom::power(n,x)
end_proc;
end_domain:
power2 := mydomain::curry(2):
power2(3)
While this code works as written with MuPAD 3.0 (it does not work with 2.5.3 and earlier, which had slightly different rules for “inheriting” dom), the following code requires bless, since dom is not set automatically for the functions inside the table:
domain mydomain3
new :=
proc(x)
begin
if contains(dom::newFunctions, domtype(x)) then
dom::newFunctions[domtype(x)](x);
else
error("Don't know how to convert x");
end_if;
end_proc;
newFunctions :=
map(table(
DOM_INT = (n -> new(dom, [$1..n])),
DOM_LIST = (l -> new(dom, l))
),
prog::bless, dom);
end_domain:
mydomain3(3);
mydomain3([1,2,3])