Why does ghc generate so many useless-looking nested case expressions?

haskell

Solution

The `case` expression forces evaluation. Using `let` will build closures instead. Note, GHC Core differs from regular Haskell in the semantics of `case`.

Problem

I have some Haskell code ``` 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 ``` Which is generating some rather strange-looking core: ``` 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 #) } ``` The case expressions seem like no-ops. What are they there for? Strictness?

Original source

Related problems