On 12/21/07, Oliver Kullmann <O.Kullmann at swansea.ac.uk> wrote:
> So "load" is the equivalent of "#include" in the C/C++ world.
> However, apparently there is no functionality concerned with
> preventing multiple inclusions (especially circular inclusions)?
>
> I wonder how other users deal with that problem --- everybody writes
> his little utilities to handle this, I guess? Or are some underlying
> clisp-mechanisms used?
I've tinkered with a namespace mechanism for Maxima which is
based on the Lisp package system. The current version of it is in cvs.
http://maxima.cvs.sourceforge.net/maxima/maxima/share/contrib/namespaces
I am hoping this could be useful for encapsulating add-on programs.
Here is an example session.
(%i1) load (namespaces)$
(%i2) in_namespace ();
(%o2) #<MAXIMA package>
(%i3) in_namespace (Foo);
(%o3) #<$Foo package>
($%i4) aa : 1234;
(%o4) 1234
($%i5) f (x) := aa - x;
(%o5) f(x) := aa - x
($%i6) export (f);
(%o6) [maxima|true]
($%i7) in_namespace (maxima);
(%o7) #<MAXIMA package>
(%i8) functions;
(%o8) [Foo|f(Foo|x)]
(%i9) values;
(%o9) [Foo|aa]
(%i10) symbols (Foo);
(%o10) [Foo|, Foo|%i3, Foo|%i4, Foo|%i5, Foo|%i6, Foo|%i7,
Foo|%o3, Foo|%o4, Foo|%o5, Foo|%o6, Foo|aa, Foo|f, Foo|x]
(%i11) external_symbols (Foo);
(%o11) [Foo|f]
(%i12) aa : 9876;
(%o12) 9876
(%i13) Foo|aa;
(%o13) 1234
(%i14) values;
(%o14) [Foo|aa, aa]
(%i15) f (1);
(%o15) f(1)
(%i16) import (Foo);
(%o16) [[Foo|f]]
(%i17) f (1);
(%o17) 1233
There are some rough edges but I think it is not too far from workable.
In particular the stuff about exporting symbols from the :maxima
package is probably confused; either too many symbols or too few,
I'm not sure which. Someone who understands the Lisp package
system better than me is invited to make suggestions about that
or anything else.
Specifically about loading something just once, there is
load_namespace(foo) which calls load(foo) only if there is not
already a namespace named foo. Dunno if it handles circular
dependencies correctly.
Comments in namespaces.lisp provide more info and examples,
and there is a test script rtest_namespaces.mac as well.
HTH
Robert Dodier