In general, there is no method that I know of that will allow you to get the name of the module to which an arbitrary module export belongs. However, there is a method that works for named modules. Normally, a module does not have a (sort of) canonical name, other than any names to which it may be assigned. However, by using an alternate module definition syntax, you can create a named module, which does have a "canonical" name. In this case, the canonical name is stored in the attributes of each of its exports.
For example, creating a module in the usual way, we see that there are no attributes on the export a.
> m := module() export a; end:
> attributes( m:-a );
However, by creating a named module, we get the following:
> module m1() export a; end:
> attributes( m1:-a );
modulename = m1
Note the different syntax. There is no assignment statement used in the creation of m1. We simply evaluate the module definition with the name m1 after the keyword module as shown, and the assignment takes place behind the scenes, as it were. (Note also that the name m1 is now protected.)
Notice that the export m1:-a has the attribute modulename = m1. Using this information, we can write a simple routine to pick up the name of a named module.
> GetModuleName := proc( e )
local a, pos;
a := [attributes]( e );
`if`( membertype( 'identical'( 'modulename' ) = 'name', a, 'pos' ),
a[ pos ], NULL )
> GetModuleName( m:-a );
> GetModuleName( m1:-a );
I should point out that there is another important, and much more common way in which a module may acquire a name. Whenever a module is saved to a repository (.mla file), it is given a name, that being the one under which it was saved. In this way, all the modules that are shipped with Maple as part of the standard library are, in fact, named modules.
> GetModuleName( LinearAlgebra:-Determinant );
Here, the export Determinant of the LinearAlgebra package has stored among its attributes the equation modulename = LinearAlgebra because LinearAlgebra is a named module by virtue of having been saved to a repository.
In general, named modules should be used with some care. For example, suppose that you assigned the named module m1 above to the name u, and then saved it in a repository using the name u, via a call such as savelib(u);. It would be saved just fine, and you could refer to it in other Maple sessions using the name u. However, the procedure GetModuleName defined above would return the original name m1 given to it when it was created. This is because it is the original name m1 that is stored in the attributes of the exports.
> attributes( u:-a );
modulename = m1
There is, therefore, much potential for confusion with named modules.