4
votes

I have a Haskell library with several executables (tests, benchmarks, etc), in total about six. When I do some refactoring in the library, I usually need to make some small change to each of the executables.

In my current workflow, I separately compile each executable (say, with GHCi) and fix each one up. This is tedious because I have to type out the path to each executable, and moreover have to reload all of the (very large) library, which even with GHCi takes some time.

My first thought to solve this issue was to create a single dummy module that imports the executable "Main" modules. However, this (of course) requires that the "Main" modules have a module name like module Executable1 where .... But now cabal complains when compiling the executable that it can't find a module called "Main" (despite explicitly listing "main-is" in the cabal file for each executable.)

I also tried ghci Exec1.hs Exec2.hs ..., but it complains module ‘main@main:Main’ is defined in multiple files.

Is there an easy way to load multiple "Main" modules at once with GHCi so I can typecheck them simultaneously?

1

1 Answers

7
votes

Cabal’s main-is option only tells Cabal what filename it should pass to GHC. Cabal does not care about it’s module name.

GHC itself has a flag, also called -main-is, documented here which tells the compiler what module conains the main function.

So this works:

executable foo
  main-is: Foo.hs
  ghc-options: -main-is Foo

Of course Foo.hs should start with module Foo where… and export main. As usual, the module name and file name needs to match.

This way, all executable can have different module names and you can load them all in GHCi.

If you also want to change the name of the main function, write ghc-options: -main-is Foo.fooMain. I would guess you could even have all executables have the same module but different main-functions this way.