Question: ... save a module containing "define_external" as a maple library ?

I have a module with an exported procedure defined by define_external, like this:

loadtest := module()
export hb;
option package;
hb:=define_external(
`hb`,
`MAPLE`,
`LIB`="/home/hans/system/maple/libhb.so");
end module:

This works if I put the code above into a .mpl file, and use read to load it. The problem is that I would like to save this module to a maple library:

> libname;
"/home/hans/system/maple", "/usr/local/maple8/lib"
> savelibname:="/home/hans/system/maple/loadtest.lib":
> march('create',savelibname,100);
> savelib('loadtest');
> map( march, [ 'gc', 'reindex', 'pack' ], savelibname ):

When I try to use this newly created library after restarting maple the following happens:

> A:=Matrix([[3,2,1,-2,-1,-4],[1,0,1,0,-1,0]]):
> with(loadtest);
[hb]
> hb(A);
[3 2 1 -2 -1 -4]
CALL_EXTERNAL([ ])
[1 0 1 0 -1 0]

I believe the problem here is that define_external needs to be reevaluated after each restart of maple, but it is only evaluated once, when the .mpl file is read, before creating the library. Loading the library doesn't cause reevaluation of the define_external definition. To force define_external to be reevaluated after each restart I tried changing my module to:

loadtest2 := module()
export hb;
local startup;
option package,load=startup;
startup:=proc()
hb:=define_external(
`hb`,
`MAPLE`,
`LIB`="/home/hans/system/maple/libhb.so");
end:
end module:

After reading this into maple, saving it as a library, quitting maple, starting maple again it resulted in:

> with(loadtest2);
Error, (in startup) attempting to assign to `loadtest2:-hb` which is protected

Why is it "protected" ?, because it is exported ?
So to test if it was OK to define a local procedure via load I did this:

loadtest3 := module()
export hb;
local hb_int,startup;
option package,load=startup;
hb:=proc()
return hb_int(args);
end:
startup:=proc()
hb_int:=define_external(
`hb`,
`MAPLE`,
`LIB`="/home/hans/system/maple/libhb.so");
end:
end module:

And the results were:

> with(loadtest3);
[hb]
> hb(A);
[[0, 1, 0, 1, 0, 0], [0, 0, 1, 0, 1, 0], [0, 2, 0, 0, 0, 1],
[1, 0, 0, 1, 1, 0], [1, 1, 0, 0, 1, 1], [2, 0, 0, 0, 2, 1]]

This is the desired output, hooray !

So if I take a "detour" via a local procedure, then I can use load to force reevaluation of define_external.

Is there no more elegant, simple or intuitive way to do this ?

The examples described above were tested (and gave the same result) on maple 8 and maple 9 under Linux.
Please Wait...