[Haskell-beginners] Module import problem

Casey Rodarmor caseyrodarmor at gmail.com
Sun Sep 28 14:17:06 EDT 2008


On Sun, Sep 28, 2008 at 7:33 PM, Christian Cheng <chrycheng at gmail.com> wrote:
> On Mon, Sep 29, 2008 at 12:20 AM, Casey Rodarmor
> <caseyrodarmor at gmail.com> wrote:
>>
>> On Sun, Sep 28, 2008 at 6:08 PM, Chry Cheng <chrycheng at gmail.com> wrote:
>> > "Casey Rodarmor" <caseyrodarmor at gmail.com> writes:
>> >
>> >> Hi there!
>> >>
>> >> I have a problem with importing a module I'd like to use.
>> >>
>> >> My working directory, ~/proj, contains:
>> >>    ./Haskore -- a folder containing a version of haskore, this music thingy
>> >>    ./test.hs -- random stuff using haskore
>> >>
>> >> The main file in ~/proj/Haskore is ~/proj/Haskore/Haskore.hs, which
>> >> contains the following module declaration:
>> >>> module Haskore(module HaskoreLoader) where
>> >>> import HaskoreLoader
>> >>
>> >> I've tried to put all the following in ~/proj/test.hs, with no luck:
>> >>> import Haskore         -- Could not find module `Haskore':
>> >>> import Haskore.Haskore -- file name does not match module name `Haskore'
>> >>
>> >> Am I doing something wrong? Is there a way to place a module in an
>> >> arbitrary directory, without having to modify it?
>> >>
>> >> Thanks so much for your help!
>> >>
>> >> Best,
>> >> Casey Rodarmor
>> >> _______________________________________________
>> >> Beginners mailing list
>> >> Beginners at haskell.org
>> >> http://www.haskell.org/mailman/listinfo/beginners
>> >
>> > You have to tell GHC where to find Haskore.  To do this, call ghc with the i option:
>> >
>> > ghc -iHaskore/
>> >
>> > then, import using:
>> >
>> > import Haskore
>> >
>>
>> Hi Chry,
>>
>> Thanks for the answer, everything works now :-)
>>
>> I must admit, I'm a little disappointed if that's the only way to get
>> it to work. On the surface of things, I don't see why one can't just
>> put a Module in some/arbitrary/directory, and then import it as
>> some.arbitrary.directory.Module. The need to use a flag on the command
>> line seems a little unnecessary.
>>
>
> I beg to differ.  I don't think it's reasonable to expect the compiler
> to figure out on its own where we have stashed additional classes.
> Other programming languages have a similar requirement.  For Java,
> it's the class path (e.g., javac -classpath Haskore); C/C++, the
> include directories (IIRC, gcc -I Haskore), etc.

I was fooling around with Java, and you're totally right, the module
itself must know about its full name, so you can't have a java file
which declares itself as "package Foo.Thingy;" in Bar/Foo/Thingy.java,
and then do an "import Bar.Foo.Thingy".

It is worth noting that in C and C++, a double-quoted #include will
search relative to the current source file. Even though that doesn't
have anything to do with locating the object file that you'll probably
want to link in later...

The comparison I was thinking of was with python, where a module can
be placed in an arbitrary directory, and then accessed using a
relative path. If I have 'stuff.py' that contains class 'Foo', I can
move it to 'hello/module/stuff.py', and then import it is
hello.module.stuff. A python module doesn't need to know where it
lives.

I often find myself working on computers where I don't have
administrative privileges, which means I might not be able to install
libraries in the 'right' places. This approach also makes it simple to
create self-contained projects that can easily be moved from machine
to machine, where only the local directory structure is important.
Would there be any downside to this in haskell?

>
>> Can anyone give a little insight into why this decision was made?
>>
>> Best,
>> Casey
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list