Skip to content

Commit 6dec072

Browse files
committed
fix(core): 🐛 Fix ToANF incorrectly handling lets
1 parent 9aa552f commit 6dec072

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Elara/Core/ToANF.hs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Elara.Core.Analysis (exprType)
88
import Elara.Core.Generic (Bind (..))
99
import Elara.Data.Pretty
1010
import Elara.Data.Unique
11-
import Elara.Logging (StructuredDebug, debugWith)
11+
import Elara.Logging (StructuredDebug, debug, debugWith)
1212
import Polysemy
1313

1414
{- | Convert a Core expression to ANF
@@ -26,12 +26,19 @@ becomes:
2626
-- toANF :: Core.CoreExpr -> Sem r (ANF.Expr Core.Var)
2727
-- toANF expr = (toANF' expr pure)
2828

29-
type ToANF r = (Members [UniqueGen, StructuredDebug] r, Pretty (Core.Expr Core.Var))
29+
type ToANF r =
30+
( Members [UniqueGen, StructuredDebug] r
31+
, Pretty (Core.Expr Core.Var)
32+
, Pretty Core.Var
33+
, Pretty (ANF.AExpr Core.Var)
34+
, Pretty (ANF.CExpr Core.Var)
35+
, Pretty (ANF.Expr Core.Var)
36+
)
3037

3138
toANF :: ToANF r => Core.CoreExpr -> Sem r (ANF.Expr Core.Var)
3239
toANF expr =
3340
debugWith ("toANF " <> pretty expr) $
34-
toANF' expr (\e -> pure (ANF.CExpr $ ANF.AExpr e))
41+
toANFRec expr (pure . ANF.CExpr)
3542

3643
toANFCont :: ToANF r => Core.CoreExpr -> ContT (ANF.Expr Core.Var) (Sem r) (ANF.AExpr Core.Var)
3744
toANFCont e = ContT $ \k -> toANF' e k
@@ -78,10 +85,12 @@ toANFRec (Core.Match bind as cases) k = evalContT $ do
7885
e' <- lift $ toANFRec e (pure . ANF.CExpr)
7986
pure (con, bs, e')
8087
k $ ANF.Match bind as cases'
81-
toANFRec (Core.Let (NonRecursive (b, e)) body) _ = evalContT $ do
88+
toANFRec (Core.Let (NonRecursive (b, e)) body) k = evalContT $ do
89+
lift $ debug $ "ToANF-ing" <> pretty b
8290
e' <- toANFCont e
83-
body' <- toANFCont body
84-
pure $ ANF.Let (NonRecursive (b, ANF.AExpr e')) (ANF.CExpr $ ANF.AExpr body')
91+
body' <- lift $ toANFRec body k
92+
lift $ debug $ "Let " <> pretty b <> " = " <> pretty e' <> " in " <> pretty body'
93+
pure $ ANF.Let (NonRecursive (b, ANF.AExpr e')) (body')
8594
toANFRec (Core.Let (Recursive bs) body) _ = evalContT $ do
8695
bs' <- for bs $ \(b, e) -> do
8796
e' <- toANFCont e

0 commit comments

Comments
 (0)