GHC是否减少了编译时不依赖IO的表达式?

时间:2018-03-19 12:38:41

标签: haskell ghc

如果我有像

这样的程序

main = print ( (+) <$> Just 1 <*> Just 2 )

编译器是否会决定减少程序中不依赖于IO的部分

( (+) <$> Just 1 <*> Just 2 ) => (Just 3)

或者该程序是否仍然从(+) <$> Just 1创建新功能,然后在运行时将其应用于Just 2

1 个答案:

答案 0 :(得分:8)

让我们问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 ...

与往常一样,核心有点冗长。当我试图阅读它时,让我向你展示一些眼睛跟踪。

: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

对我来说,3看起来很像1+2。对于偏执狂,我尝试将12替换为1020,并且确实在底部看到了类似的30结构。所以这看起来非常有说服力,证明它在编译时进行了这个计算。

那就是说,我希望这可能非常特定于简单数字类型的数值计算。更激动人心的类型的编译时计算有一个奇怪而混乱的时空权衡,而处理这种情况的通常的方法是让程序员尽可能地控制它。