为什么ghc会生成这么多无用的嵌套案例表达式?

时间:2013-08-12 16:35:19

标签: haskell

我有一些Haskell代码

indLoc index way = do
    Store p <- ask
    return $ plusPtr p localAddr
    where localAddr = (stHeader + stIndices + index + stIndices * way) * blockSize
--snip...
    (Just way, _) -> do
        liftIO $ pokeElemOff loc way a
        indLoc index way

这会产生一些相当奇怪的核心:

  case GHC.Prim.writeInt64OffAddr#
         @ GHC.Prim.RealWorld a4_s4UV i_s4UW x_s4UX new_s_s4UY
  of s2_s4V0 { __DEFAULT ->
  let {
    sat_s528 :: GHC.Ptr.Ptr b_a1UV
    [LclId]
    sat_s528 =
      case r_s4Us `cast` ... of _ { GHC.Ptr.Ptr addr_s4Vb ->
      case index_s4Um of _ { GHC.Types.I# y_s4Vc ->
      case ds1_s4Ub of _ { (blk1_s529, offs_s4V8) ->
      case offs_s4V8 of _ { GHC.Types.I# d_s4Vk ->
      case GHC.Prim.*# 2 i_s4UW of sat_s4Vf { __DEFAULT ->
      case GHC.Prim.+# 3 y_s4Vc of sat_s527 { __DEFAULT ->
      case GHC.Prim.+# sat_s527 sat_s4Vf of sat_s526 { __DEFAULT ->
      case GHC.Prim.*# sat_s526 4096 of sat_s4Vi { __DEFAULT ->
      case GHC.Prim.plusAddr# addr_s4Vb sat_s4Vi
      of sat_s525 { __DEFAULT ->
      case GHC.Prim.plusAddr# sat_s525 d_s4Vk of sat_s524 { __DEFAULT ->
      GHC.Ptr.Ptr @ b_a1UV sat_s524
      }
      }
      }
      }
      }
      }
      }
      }
      }
      } } in
  (# s2_s4V0, sat_s528 #)
  }

案例表达似乎是无操作。那是什么?严格?

1 个答案:

答案 0 :(得分:18)

case表达式强制评估。使用let将构建闭包。注意,GHC Core在case的语义上与常规Haskell不同。