14

我有以下程序:

data Peano = Zero | Succ Peano deriving (Show)

add Zero     b = b
add (Succ a) b = add a (Succ b)

mul Zero     b = Zero
mul (Succ a) b = add b (mul a b)

four x = let two = Succ (Succ Zero) in mul two two

我想从 GHC 得到这样的东西:

add =
  \ ds b ->
    case ds of
      Zero ->
        b
      Succ a ->
        add
          a
          (Succ b)

mul =
  \ ds b ->
    case ds of 
      Zero ->
        Zero
      Succ a ->
        add
          b
          (mul a b)

four =
    let
      two =
        Succ
           (Succ Zero)
    in
    mul two two

我设法得到的最好的是

ghci -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques foo.hs

但是它需要大量手动删除 GHC 生成的东西才能获得上面的代码。是否有用于 GHC 的开关或进行清理的第三方脚本?

有没有办法至少摆脱case {tick (main:Main, 8)} @ (State# RealWorld) of _ { __DEFAULT ->

4

1 回答 1

22

你很幸运!有一个工具可以完成这项工作:ghc-core

ghc-core 使用命令行包装器包装 ghc,该命令行包装器在寻呼机中以人类可读的彩色方式显示 GHC 的优化核心和程序集输出。

用法 - 只需替换ghcghc-core

   ghc-core A.hs  

   ghc-core -fvia-C -optc-O3 A.hs
于 2012-05-21T23:08:59.687 回答