Does Haskell 2010 guarantee to concatenate String literals at compile time?
If I have
"This is a " ++
"very long String that " ++
"spans several lines"
does the compiler treat it as
"This is a very long String that spans several lines"
I want to keep my source lines less than 80 characters long if possible, but I don't want to introduce run-time inefficiency.
Haskell 2010 guarantees that it's denotationally equivalent to the merged string, but has nothing to say about how it should be compiled. It's easy enough to check with the ghc-core
tool, though.
-- Test.hs
main = putStrLn $ "Hello " ++ "world"
and when we run ghc-core Test.hs
[1 of 1] Compiling Main ( Test.hs, Test.o )
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 19, types: 23, coercions: 9}
main2 :: [Char]
[GblId,
Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=0, Value=False,
ConLike=False, WorkFree=False, Expandable=False,
Guidance=IF_ARGS [] 60 0}]
main2 = unpackCString# "Hello world"
...
and see that the string has been merged in the Core intermediate language.
Edit: To emphasize my agreement with other answers, just because this particular program has a core dump with the merged string does not guarantee that the compiler will do it for all strings. Being compliant with the Haskell spec doesn't imply much at all about how things get compiled.