tomjaguarpaw / tilapia

Improving all Haskell's programmer interfaces

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Myterious behaviour around `Main.main`

tomjaguarpaw opened this issue · comments

ghc has some strange behaviour regarding how it finds Main.main.

$ ghc Main.hs
[1 of 2] Compiling AModule          ( AModule.hs, AModule.o )
[2 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:1:1: error:
    Ambiguous occurrence ‘main’
    It could refer to
       either ‘AModule.main’,
              imported from ‘AModule’ at Main.hs:3:1-14
              (and originally defined at AModule.hs:4:1-4)
           or ‘Main.main’, defined at Main.hs:6:1
  |
1 | module Main (Main.main) where
  | ^

Changing the module line of Main.hs doesn't make any difference. module Main (Main.main) where gives exactly the same result. So does module Main () where, which suggests there's actually some special handling of Main.main in ghc (or more precisely, whatever is provided to -main-is).

(The only fix I've found is import qualified AModule, which is a bit unfortunate.)

app/AModule.hs

module AModule where

main :: IO ()
main = pure ()

app/Main.hs

module Main where

import AModule

main :: IO ()
main = AModule.main

The former is fine but the latter is not so I think GHC must use some method separate from the exported function list to determine what main is. That's rather confusing!

module Main (AModule.main) where

import AModule
module Main (AModule.main) where

import AModule

main :: Int
main = 1