Let's ask GHC!
% echo 'main = print ((+) <$> Just 1 <*> Just 2)' > test.hs
% ghc -O2 -ddump-simpl test.hs
[1 of 1] Compiling Main ( test.hs, test.o )
==================== Tidy Core ====================
Result size of Tidy Core
= {terms: 42, types: 47, coercions: 9, joins: 0/0}
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
Main.main4 :: Integer
[GblId,
Caf=NoCafRefs,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 100 0}]
Main.main4 = 3
-- RHS size: {terms: 9, types: 11, coercions: 0, joins: 0/0}
Main.main3 :: [Char]
[GblId,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=False, ConLike=False,
WorkFree=False, Expandable=False, Guidance=IF_ARGS [] 60 30}]
Main.main3
= case GHC.Show.$w$cshowsPrec4 11# Main.main4 (GHC.Types.[] @ Char)
of
{ (# ww3_a23H, ww4_a23I #) ->
GHC.Types.: @ Char ww3_a23H ww4_a23I
}
-- RHS size: {terms: 3, types: 1, coercions: 0, joins: 0/0}
Main.main2 :: [Char]
[GblId,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=False, ConLike=False,
WorkFree=False, Expandable=False, Guidance=IF_ARGS [] 30 0}]
Main.main2 = ++ @ Char GHC.Show.$fShowMaybe1 Main.main3
-- RHS size: {terms: 4, types: 0, coercions: 0, joins: 0/0}
Main.main1
:: GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)
[GblId,
Arity=1,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 40 60}]
Main.main1
= GHC.IO.Handle.Text.hPutStr2
GHC.IO.Handle.FD.stdout Main.main2 GHC.Types.True
-- RHS size: {terms: 1, types: 0, coercions: 3, joins: 0/0}
main :: IO ()
[GblId,
Arity=1,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=0,unsat_ok=True,boring_ok=True)
Tmpl= Main.main1
`cast` (Sym (GHC.Types.N:IO[0] <()>_R)
:: ((GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)) :: *)
~R#
(IO () :: *))}]
main
= Main.main1
`cast` (Sym (GHC.Types.N:IO[0] <()>_R)
:: ((GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)) :: *)
~R#
(IO () :: *))
-- RHS size: {terms: 2, types: 1, coercions: 3, joins: 0/0}
Main.main5
:: GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)
[GblId,
Arity=1,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 20 60}]
Main.main5
= GHC.TopHandler.runMainIO1
@ ()
(Main.main1
`cast` (Sym (GHC.Types.N:IO[0] <()>_R)
:: ((GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)) :: *)
~R#
(IO () :: *)))
-- RHS size: {terms: 1, types: 0, coercions: 3, joins: 0/0}
:Main.main :: IO ()
[GblId,
Arity=1,
Unf=Unf{Src=InlineStable, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=0,unsat_ok=True,boring_ok=True)
Tmpl= Main.main5
`cast` (Sym (GHC.Types.N:IO[0] <()>_R)
:: ((GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)) :: *)
~R#
(IO () :: *))}]
:Main.main
= Main.main5
`cast` (Sym (GHC.Types.N:IO[0] <()>_R)
:: ((GHC.Prim.State# GHC.Prim.RealWorld
-> (# GHC.Prim.State# GHC.Prim.RealWorld, () #)) :: *)
~R#
(IO () :: *))
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
Main.$trModule4 :: GHC.Prim.Addr#
[GblId,
Caf=NoCafRefs,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 20 0}]
Main.$trModule4 = "main"#
-- RHS size: {terms: 2, types: 0, coercions: 0, joins: 0/0}
Main.$trModule3 :: GHC.Types.TrName
[GblId,
Caf=NoCafRefs,
Str=m1,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 10 20}]
Main.$trModule3 = GHC.Types.TrNameS Main.$trModule4
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
Main.$trModule2 :: GHC.Prim.Addr#
[GblId,
Caf=NoCafRefs,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 20 0}]
Main.$trModule2 = "Main"#
-- RHS size: {terms: 2, types: 0, coercions: 0, joins: 0/0}
Main.$trModule1 :: GHC.Types.TrName
[GblId,
Caf=NoCafRefs,
Str=m1,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 10 20}]
Main.$trModule1 = GHC.Types.TrNameS Main.$trModule2
-- RHS size: {terms: 3, types: 0, coercions: 0, joins: 0/0}
Main.$trModule :: GHC.Types.Module
[GblId,
Caf=NoCafRefs,
Str=m,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 10 30}]
Main.$trModule = GHC.Types.Module Main.$trModule3 Main.$trModule1
Linking test ...
As always, the core is a bit verbose. Let me show you a bit of eye-tracking as I tried to read it.
:Main.main
= Main.main5
`cast` ...
Main.main5
= ...runMainIO1 ... (Main.main1 `cast` ...)
Main.main1
= ...hPutStr2 ...stdout Main.main2 ...
Main.main2 = ++ ...fShowMaybe1 Main.main3
Main.main3
= case ...showsPrec4 11# Main.main4 ...
of
{- after a bit of squinting... -}
x -> x
Main.main4 = 3
To me, that 3
looks suspiciously like 1+2
. For paranoia, I tried replacing 1
and 2
with 10
and 20
, and indeed saw a similar structure with 30
at the bottom. So that looks like pretty convincing evidence that it does this computation at compile-time.
That said, I expect this is probably pretty specific to numeric computations on simple numeric types. Compile-time computation of more exciting types has a strange and confusing time-space tradeoff, and the usual way to handle that situation is to give the programmer control of it as much as possible.