It seems that GHC ignores many lines beginning #!
. For example, Main.hs
:
#!This is a test
main = do
putStrLn "OK!"
#!This is a test
putStrLn "OK!"
#!This is a test
works fine with ghc Main.hs
(which is ghc-9.0.2 Main.hs
). Is that documented anywhere? I could not identify a reference to it in the Haskell Language Report or the GHC User’s Guide. I assume it is to accommodate ‘shebang lines’ on Unix-like operating systems (but I thought the #!
could only be the first two characters in the file).
I say ‘many’ because Main.hs
with:
main = do
#!This is a test
putStrLn "OK!"
results in:
[1 of 1] Compiling Main ( Main.hs, Main.o )
Main.hs:2:2: error: lexical error in pragma at character '!'
|
2 | #!This is a test
| ^
I do not follow the error message. The GHC User’s Guide says that all pragmas start {#
, so I would have expected the complaint to be about the initial #
, not the !
.
EDIT: I’ve answered my own question below, concluded that it is not documented as well as it could be, and raised a GHC issue #22300.