main modules in GHC, deriving differences between GHC and Hugs
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? There also seems to be a difference in the way that GHC and Hugs handle deriving clauses. I haven't pinned this down, but I can try to provide more information if this is something the compiler developers not already aware of. #g ------------------- Graham Klyne <GK@NineByNine.org> PGP: 0FAA 69FF C083 000B A2E9 A131 01B9 1C7A DBCA CB5E
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 = x + y tests = [ foo 2 3 == 5, foo 3 4 /= 6 ] #ifdef UNIT_TEST main = print $ and tests #else foo_test = 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)
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. The solution I use is a script that you call like: ghcmake File which creates MainXXX.hs (where XXX is a random number) and this contains: module Main where { import qualified File (main) ; main = File.main } It then runs ghc --make on that and deletes the MainXXX.hs file. This works okay, but isn't very satisfactory. 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. - Hal -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume On Fri, 13 Jun 2003, Matthew Donadio wrote:
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 = x + y
tests = [ foo 2 3 == 5, foo 3 4 /= 6 ]
#ifdef UNIT_TEST main = print $ and tests #else foo_test = 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
On Friday, 2003-06-13, 22:06, CEST, Hal Daume III wrote:
[...]
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.
[...]
I would even say that you should be able to use a "main variable" which is not named "main". You would have to specify its identifier at the command line. But I wouldn't allow arbitrary IO a types but only IO ().
- Hal
Wolfgang
participants (4)
-
Graham Klyne -
Hal Daume III -
Matthew Donadio -
Wolfgang Jeltsch