module names

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

module names

Chad Scherrer-2
Something about the way module names are specified in a file seems
really strange to me. When I first started learning Haskell (I had used
OCaml previously), I tried things like

module Main where
    import A
    import B
    main = A.f >> B.f

  module A where
    f = ...

  module B where
    f = ...

in a single file. This example is straight from chapter 5 of the Report,
and no mention is made (that I could find) about modules needing to be
in separate files. But this won't load in ghci! (Even if ... is changed
to putStr "hi"). Eventually I figured out that it works fine if it's
split over three separate files.

So here's what I'm trying to figure out: If every file corresponds to
exactly one module (is that true?), then why must the module name be
given again in the text of the file? When I'm using ghci, I have lots of
modules that I sometimes want to load "as Main", and sometimes I only
want them loaded as a dependency from another module. Currently, I have
to go into each file to change the "module Foo where" line to do this.

Chad Scherrer
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: module names

S Koray Can
Scherrer, Chad wrote:

> module Main where
>     import A
>     import B
>     main = A.f >> B.f
>
>   module A where
>     f = ...
>
>   module B where
>     f = ...
>
> in a single file. This example is straight from chapter 5 of the Report,
> and no mention is made (that I could find) about modules needing to be
> in separate files. But this won't load in ghci! (Even if ... is changed
> to putStr "hi"). Eventually I figured out that it works fine if it's
> split over three separate files.

The report says that they make up a single program, so that does not
imply that they are in a single file. But, you are right: it's a good
idea to be explicit about this.

> So here's what I'm trying to figure out: If every file corresponds to
> exactly one module (is that true?), then why must the module name be
> given again in the text of the file? When I'm using ghci, I have lots of
> modules that I sometimes want to load "as Main", and sometimes I only
> want them loaded as a dependency from another module. Currently, I have
> to go into each file to change the "module Foo where" line to do this.

Section 9.5 of the report seems to show that <module> stands for the
compilation unit and it defines a single module. I suppose the standard
allows you to name your module freely no matter your filesystem allows,
but ghc requires (afaik) that module M exist in M.hs or M.lhs.
Why not do this: name none of those modules Main.hs, and have an empty
module Main.hs with only "import MainDeJour" and "main =
MainDeJour.main" so you can just edit just that file.

Cheers,
Koray
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: module names

Ketil Malde-2
In reply to this post by Chad Scherrer-2
"Scherrer, Chad" <[hidden email]> writes:

> module Main where
>   module A where
>   module B where

> in a single file. This example is straight from chapter 5 of the Report,
> and no mention is made (that I could find) about modules needing to be
> in separate files. But this won't load in ghci!

IIRC, the standard deliberately doesn't specify any requirements as to
the file organization of code, and leaves it up to implementations.  I
think the reason file names and module names must coincide (with the
exception of module Main, which may be in an arbitrarily named file),
is simply that it makes it easier for the compiler (and the programmer
as well) to locate them.

Perhaps now that we have hierarchical namespaces we could allow a
"module directory" (i.e. any prefix up to a dot) to be represented
either as a directory (like it is now), or as a multi-module file?

-k
--
If I haven't seen further, it is by standing in the footprints of giants

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: module names

Chad Scherrer-2
In reply to this post by S Koray Can
-----Original Message-----
From: S Koray Can [mailto:[hidden email]]

Why not do this: name none of those modules Main.hs, and have an empty
module Main.hs with only "import MainDeJour" and "main =
MainDeJour.main" so you can just edit just that file.

Cheers,
Koray

----------------------------------------------------------------------
Yeah, I like that approach. That saves me from having to remember which
file I most recent used as main. Seems easy enough to even set it up so
that
load MainDuJour
writes the file Main.hs with

import MainDuJour
main = MainDuJour.main

and then and then calls
ghc --make Main.hs -o mainDuJour

This will do for now, but still feels really kludgy, especially for Haskell.

-Chad
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: module names

Peter Simons
In reply to this post by Chad Scherrer-2
Scherrer, Chad writes:

 > When I'm using ghci, I have lots of modules that I
 > sometimes want to load "as Main", and sometimes I only
 > want them loaded as a dependency from another module.
 > Currently, I have to go into each file to change the
 > "module Foo where" line to do this.

Maybe the "-main-is" option can help to make your live
easier? You'll find more information here:

  http://haskell.org/ghc/docs/latest/html/users_guide/flag-reference.html#id3131936

Peter

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Re: module names

Bulat Ziganshin
In reply to this post by S Koray Can
Hello S,

Friday, December 16, 2005, 6:32:47 AM, you wrote:

SKC> Why not do this: name none of those modules Main.hs, and have an empty
SKC> module Main.hs with only "import MainDeJour" and "main =
SKC> MainDeJour.main" so you can just edit just that file.

ghc has option "-main-is" which is just what needed in this case


--
Best regards,
 Bulat                            mailto:[hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: RE: module names

Remi Turk
In reply to this post by Chad Scherrer-2
On Fri, Dec 16, 2005 at 07:55:50AM -0800, Scherrer, Chad wrote:

> From: S Koray Can [mailto:[hidden email]]
> Why not do this: name none of those modules Main.hs, and have an empty
> module Main.hs with only "import MainDeJour" and "main =
> MainDeJour.main" so you can just edit just that file.
>
> Cheers,
> Koray
>
> ----------------------------------------------------------------------
> Yeah, I like that approach. That saves me from having to remember which
> file I most recent used as main. Seems easy enough to even set it up so
> that
> load MainDuJour
> writes the file Main.hs with
>
> import MainDuJour
> main = MainDuJour.main

A rather late reply I realize, but this slightly less verbose
version also works:

> module Main where
>
> import MainDuJour

Remi
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: RE: module names

Chad Scherrer-2
Yes, good point. I suppose there's really no need to re-declare main once it's been imported. Thanks!

-----Original Message-----
From: Remi Turk [mailto:[hidden email]]
Sent: Thu 12/29/2005 4:34 PM
To: Scherrer, Chad
Cc: S Koray Can; [hidden email]
Subject: Re: [Haskell-cafe] RE: module names
 
On Fri, Dec 16, 2005 at 07:55:50AM -0800, Scherrer, Chad wrote:

> From: S Koray Can [mailto:[hidden email]]
> Why not do this: name none of those modules Main.hs, and have an empty
> module Main.hs with only "import MainDeJour" and "main =
> MainDeJour.main" so you can just edit just that file.
>
> Cheers,
> Koray
>
> ----------------------------------------------------------------------
> Yeah, I like that approach. That saves me from having to remember which
> file I most recent used as main. Seems easy enough to even set it up so
> that
> load MainDuJour
> writes the file Main.hs with
>
> import MainDuJour
> main = MainDuJour.main

A rather late reply I realize, but this slightly less verbose
version also works:

> module Main where
>
> import MainDuJour

Remi

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe