[mercury-users] Nested Sub-Modules

Tyson Dowd trd at cs.mu.OZ.AU
Wed Sep 30 02:58:50 AEST 1998


On 29-Sep-1998, Dante Baldan <dba at info.fundp.ac.be> wrote:
> 
> I looked at this mailing-list archive, but I did not
> find any mail about nested sub-modules.
> I read the language reference manual, but it is not clear to
> me where nested submodules can be placed within
> parent module.
> 
> For example, I tried (Mercury 0.7.3) the following code:

Nested sub-modules were introduced after Mercury 0.7.3.
So it's almost certain that if you are using 0.7.3 you will
have problems getting it to work.

The documentation on the Mercury web page is for the very
latest release of the day, where sub-modules (mostly) work.
This is arguably a bug -- we should probably provide both the
latest documentation and the 0.7.3 documentation online.

The problems with your code are:
	- Your sub modules needs an interface if it wants the
	parent to be able to use "sum".  Parents cannot see inside
	a sub-modules implementation -- it is private to the sub-module.
	- Parents do not automatically :- import_module on
	their sub-modules.  You need to add
		:- import_module parent_mod:sub_mod.
	(but sub-modules do import their parents, and sub-modules can
	see inside the implementation of their parents, so the definition
	of list is OK).

The "visibility rules" section of the manual might be worth a read.
It's presently at
http://www.cs.mu.oz.au/research/mercury/information/doc/reference_manual_9.html#SEC39

I agree with you that a sample using sub-modules is worth having
(to remember how it works I sometimes have to go and look at the code
the CORBA binding generates!).  There are some test-cases, but not
samples intended for users.

Similarly, we need samples using typeclasses, and existential types.

I will attach a full working version of the program.

-- 
Those who would give up essential liberty to purchase a little temporary
safety deserve neither liberty nor safety.     - Benjamin Franklin

Tyson Dowd   <tyson at tyse.net>   http://tyse.net
-------------- next part --------------
:- module parent_mod.

:- interface.

:- import_module io.

:- pred main(io__state::di,io__state::uo) is det.

:- implementation.

:- type list(T) ---> [] ; [T|list(T)].

%---------------- sub mod ----------------------

:- module sub_mod.

:- interface.

:- func sum(list(int))= int.
:- mode sum(in) = out is det.

:- implementation.

:- import_module int.

sum([])=0.
sum([X|L])=N :- sum(L)=N1, N is N1 + X.

:- end_module sub_mod.

%---------------- end sub mod ----------------------

:- import_module parent_mod:sub_mod.

main --> { sum([1,2,3])=X }, io__write(X), 
          io__write_string("\n").

:- end_module parent_mod.



More information about the users mailing list