[Haskell-beginners] Comfortable handling of module hierarchies

Karl Voelker ktvoelker at gmail.com
Sat Sep 15 18:00:55 CEST 2012


On Fri, Sep 14, 2012 at 6:23 PM, Christopher Howard
<christopher.howard at frigidcode.com> wrote:
>
> import qualified Plant as P
>
> P.Fruit.Raspberry.jam

Short answer: P.Fruit.Raspberry.jam would work if you said:

import qualified Plant.Fruit.Raspberry as P.Fruit.Raspberry

Long answer:

You can't have exactly what you want because the Haskell module
namespace isn't exactly heirarchical. Here's an excerpt from the
Haskell 98 report [1]:

"The name-space for modules themselves is flat, with each module being
associated with a unique module name (which are Haskell identifiers
beginning with a capital letter; i.e. modid)."

Notice that this doesn't allow for dots in module names. A
commonly-provided language extension allowed dots in module names, and
compilers took these dots as a signal to look for a module's source at
a particular place in the directory tree, but the semantics of the
language didn't have a heirarchy of modules.

Things haven't changed much in Haskell 2010, other than the existing
use of dots being formalized [2]:

"Module names can be thought of as being arranged in a hierarchy in
which appending a new component creates a child of the original module
name. For example, the module Control.Monad.ST is a child of the
Control.Monad sub-hierarchy. This is purely a convention, however, and
not part of the language definition; in this report a modid is treated
as a single identifier occupying a flat namespace."

In your code snippet, P.Fruit.Raspberry doesn't work because although
P refers to the same module as Plant, there isn't anything "inside" P
(or Plant) called Fruit.Raspberry.

-Karl

[1] http://www.haskell.org/onlinereport/modules.html
[2] http://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-980005



More information about the Beginners mailing list