main modules in GHC

Simon Peyton-Jones simonpj@microsoft.com
Mon, 23 Jun 2003 11:37:31 +0100


The question of conveniently naming a function to use as the 'main'
function has come before.
I've implemented a -main-is flag for GHC, so you can nominate either the
module containing the main function, or the main function name, or both.


The change is in the repository, and it's documented in the manual.   I
hope this'll help the test-suite folk.

Simon

| -----Original Message-----
| From: haskell-admin@haskell.org [mailto:haskell-admin@haskell.org] On
Behalf Of Hal Daume III
| Sent: 13 June 2003 21:07
| To: Matthew Donadio
| Cc: Graham Klyne; Haskell Mailing List
| Subject: Re: main modules in GHC, deriving differences between GHC and
Hugs
|=20
| Yes, but there's a problem with this solution.  Namely, if Foo.hs
takes a
| long time to compile, then you can't leverage having already created
Foo.o
| and Foo.hi when making the Main.
|=20
| The solution I use is a script that you call like:
|=20
|   ghcmake File
|=20
| which creates MainXXX.hs (where XXX is a random number) and this
contains:
|=20
|   module Main where { import qualified File (main) ; main =3D =
File.main
}
|=20
| It then runs ghc --make on that and deletes the MainXXX.hs file.
|=20
| This works okay, but isn't very satisfactory.
|=20
| Personally, I think this is stupid and that you should be able to
compile
| any module with a 'main :: IO a' function as an executable without
having
| to call it Main.  You can probably find a message from me to this
extent
| in the archives, as well as some response.
|=20
|  - Hal
|=20
| --
|  Hal Daume III                                   | hdaume@isi.edu
|  "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume
|=20
| On Fri, 13 Jun 2003, Matthew Donadio wrote:
|=20
| > Graham Klyne wrote:
| > > GHC seems to require a 'main' module in a file in order to
generate an exe
| > > file.   This makes it awkward to create unit test programs,
because I
| > > generally create one (to run stand-alone) for each major module.
Now I
| > > want to create a "master test" module that runs all the individual
module
| > > tests.  But if the module tests are all "main" modules it seems I
cannot
| > > bring them all together into a larger program.  Have I overlooked
any way
| > > to create an executable program from any module containing a main
function
| > > of the appropriate type?
| >
| > The easiest way to handle this is to run all the source through the
C
| > preprocessor, and put #ifdef's around main and the module name.
| > Something like
| >
| > #ifdef UNIT_TEST
| > module Main where
| > #else
| > module Foo where
| > #endif
| >
| > foo x y =3D x + y
| >
| > tests =3D [ foo 2 3 =3D=3D 5,
| >           foo 3 4 /=3D 6
| >         ]
| >
| > #ifdef UNIT_TEST
| > main =3D print $ and tests
| > #else
| > foo_test =3D and tests
| > #endif
| >
| > I haven't done this with Haskell, but I have done it with a lot of
my C
| > libraries.
| >
| > --
| > Matthew Donadio (m.p.donadio@ieee.org)
| > _______________________________________________
| > Haskell mailing list
| > Haskell@haskell.org
| > http://www.haskell.org/mailman/listinfo/haskell
| >
|=20
| _______________________________________________
| Haskell mailing list
| Haskell@haskell.org
| http://www.haskell.org/mailman/listinfo/haskell