Comfortable handling of module hierarchies

So, let's say I've got this hierarchy of modules: Plant `-- Vegetable | `-- Celery | `-- Lettuce `-- Fruit `-- Raspberry `-- Apple What I would like to be able to do is import and use modules like so: code: -------- import qualified Plant.Vegetable as V -- call a function in module Celery V.Celery.grow -------- and code: -------- import qualified Plant as P P.Fruit.Raspberry.jam -------- I tried using connecting modules, which I read about online: code (Plant.hs): -------- module Plant ( module Plant.Vegetable , module Plant.Fruit ) import Plant.Vegetable import Plant.Fruit -- (And likewise for Plant/Vegetable.hs and Plant/Fruit.hs.) However, this pools the functions from the sub-modules all into one name space, which does not allow me to make the sort of function calls I described above. Although sometime it is okay to have the functions all in one name space, it doesn't fit the style I'm aiming for, and it can be a real pain if multiple modules have a function of the same name (e.g., if all fruits and veggies have a "grow" function). Is it possible to do what I'm trying to do? -- frigidcode.com indicium.us

On Fri, Sep 14, 2012 at 6:23 PM, Christopher Howard
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

Do you know any document pointing out the rationale behind this decision
about modules taken by the Haskell designers?
Thanks.
-Damodar
On Sat, Sep 15, 2012 at 9:30 PM, Karl Voelker
On Fri, Sep 14, 2012 at 6:23 PM, Christopher Howard
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Excerpts from damodar kulkarni's message of Sun Sep 16 12:37:41 +0200 2012:
Do you know any document pointing out the rationale behind this decision about modules taken by the Haskell designers?
While I don't know about the rationale behind this particular decision, Haskell's module system is purposely quite simple. Languages like ML have more complicated module systems which allow the sort of things that you mentioned (and more!) but while they are more expressive they are also more complicated. Edward

A feature being "more complicated" for the users to use or for the
implementers to implement cannot be the SOLE reason for that feature to be
discarded. If we consider implementation concerns, then we can see that the
Haskell designers have included many features that are certainly candidates
deserving the title: "very much complicated";
e.g. various constructs from category theory, or
support for lazy evaluation, or
features being targeted for the "Data Parallel Haskell"
and so on
In particular, I am interested in knowing whether this feature is in direct
conflict with any other features the Haskell has.
- Damodar
On Sun, Sep 16, 2012 at 5:50 PM, Edward Z. Yang
Excerpts from damodar kulkarni's message of Sun Sep 16 12:37:41 +0200 2012:
Do you know any document pointing out the rationale behind this decision about modules taken by the Haskell designers?
While I don't know about the rationale behind this particular decision, Haskell's module system is purposely quite simple. Languages like ML have more complicated module systems which allow the sort of things that you mentioned (and more!) but while they are more expressive they are also more complicated.
Edward

On Sun, Sep 16, 2012 at 11:04 AM, damodar kulkarni
A feature being "more complicated" for the users to use or for the implementers to implement cannot be the SOLE reason
Haskell started out with a completely flat namespace; a minimal and minimally disruptive "hierarchical" namespace was added, without any special semantics and without any intent to define such semantics. Haskell does not have the concept of modules as containers that you seem to have; it never has had them; and there are ongoing and unresolved arguments over whether some ML-like module system should be adopted, so it's unlikely that they would be added in the absence of such resolution. You apparently believe such a thing is implicitly part of namespaces and that we need some reason to not have automatically created them to your specifications. Could you explain where you got this notion? -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (5)
-
Brandon Allbery
-
Christopher Howard
-
damodar kulkarni
-
Edward Z. Yang
-
Karl Voelker