First Class Modules

FirstClassModules provide a grouping mechanism in modern programming languages for both syntactic and semantic support in the construction of modular programs. Existing languages have modules that are stratified with the module system "sitting on top of " the core language.

External Links: Perl and FirstClassModules

Some languages with FirstClassModules:


MlLanguage modules always confused the heck out of me. I get the idea of a module. I get the idea of a 'functor', at least as used in the HaskellLanguage / CategoryTheory sense. I can understand templates and parametric polymorphism as implemented in JavaLanguage, HaskellLanguage, etc. But, I JustDontGetIt when it comes to ML's functors. Can anyone provide a tutorial or explanation InPlainEnglish? on this stuff? Online resources leave me more bewildered than when I go in; including the above linked PDF. --SamuelFalvo?

MlLanguage functors are a lot like C++ TemplateMetaprogramming (which is only a good comparison for me, I suppose, because I'm so intimately familiar with it).

Please ignore the butchery of ML syntax to come - I've only studied ML from the outside. Each 'module' in MlLanguage publicizes various types and operations, and types may be encapsulated by marking them private or exporting only their name. Thus, in one view, an ML Module is something like:

 { type t = int;
   compare : t->t->bool; }

while in another, it might be

 { type t;
   compare : t->t->bool;
   create args->t;
   etc. }

the latter wouldn't expose t.

Generally, one simply imports an ML module with the ML 'open' command, but in MlLanguage, you are also free to define scoped modules, in either one part (which exposes everything) or two parts (allowing you to still handle the signature and express privacy). For example,

 module
  type example_type = sig type t,
                          compare : t->t->bool
                      end.

and

 module
   example : example_type = struct
     let type t = int;
     let compare = ... end

In the earlier view, this allows you to create modules within another module of that same sort of essential property - a record with potentially encapsulated components (or optionally exposed) - each one very much like a record {type t; compare(t,t); etc.}

Thus ML modules are like records with slight access limitations - much like a CeePlusPlus class that lacks any instance data-structure (just a bunch of static methods and public typedefs - as is most common for TemplateMetaprogramming). It seems that it would be convenient to be able to create new ones. And they chose to do this by passing one module as a parameter to another, much like templates pass one class to another. A canonical example would be the sort. If you have a module that exposes 'type t' and 'compare : t -> t -> bool', then you can create another module that takes this first module and allows one to sort lists of t. One can do the same for maps and sets - and, indeed, one must do the same to utilize the ML standard maps and sets. To this end, one can even use anonymous modules:

 module
   t_set = Set.Make (struct type t=(type of choice), let compare = (ordering fn) end)

There are further similarities. Like C++ class/templates, ML functors are not designed for runtime use; thus it is reasonable to question whether they are truly FirstClassModules. I'm of two minds about including them above, but though I didn't provide the above definition, I'm satisfied with it (the one above stomped my own page creation of this name, but I liked it better), and ML clearly qualifies as having syntactic and semantic support for the construction and utilization of what it defines as 'module'.

Re-spaced for my own reading edification; although, I'm still somewhat confounded by how they work. I'll let it sink in for a bit, but the outlook isn't good. :) --SamuelFalvo?


EditText of this page (last edited July 9, 2010) or FindPage with title or text search