4.19 Modules

Mozart supports (predefined) abstract data structures through a module system. Modules can be loaded from files or URLs. These files contain precompiled Oz-programs and therefore carry the suffix .ozf instead of .oz.

We now illustrate all these concepts at the example of an ordinary stack which we will need already in the parsing example of the next Chapter. A stack is simply a list of values that can be muted by methods push and pop and be tested for emptiness. Push adds a new value in front of the list. Pop deletes the first value of the list and returns it. It raises an error if there is the stack is empty.

Every stack is a record of procedures of the following type:

 unit(
    push   : Value -> 
    pop    : -> Value
    isEmpty: -> Bool
    ... 
   ) 

Those kinds of abstract data structures that we will frequently use in the lecture are made available in the module ADS_Module that is located at the url ADS_URL shown below. This module is a record of functional procedures (classes) by which to create the different kinds of abstract data structure (objects). The creator for stacks, for instance, is available as ADS_Module.newStack.

%% define the ADS_URL
%% maybe change the ADS_URL to an appropriate local filename
declare AuthorURL = 'http://www.ps.uni-sb.de/~niehren' 
declare CourseDir = 'Web/Vorlesungen/Oz-NL-SS01/vorlesung' 
declare CourseURL = AuthorURL#'/'#CourseDir
declare ADS_URL= URL#'/Functors/Version.3.2/Abstract.ozf' 
 
%% load the module and use it to create a stack  
declare [ADS_Module] = {Module.link [ADS_URL]}
declare NewStack = ADS_Module.newStack  % (class)
declare Stack = {NewStack}              % (object)
 
%% now we can use this stack  
{Inspect {Stack.isEmpty}}
{Stack.push 5}
{Stack.push 27}
{Inspect {Stack.isEmpty}}
{Inspect {Stack.pop}}

The function Module.link maps a list of urls or filenames to the corresponding list of modules. This is the interactive way of loading and linking modules. Later we will discuss how to write your own module programs called functors: functors have import declarations which don't need to make any explicit calls to Module.link or anything like that, and are much simpler and more intuitive to use.

If you prefer, to download the module ADS_Module and to save it locally on your file system, you should change ADS_URL to the appropriate filename.


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)