Skip to content

Commit

Permalink
Tweak and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Dec 13, 2023
1 parent d8d6b2e commit 38dd883
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 219 deletions.
15 changes: 14 additions & 1 deletion smol-core/src/Smol/Core/Transform.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ transform ::
(Ord ann, Ord (dep Identifier)) =>
Expr dep ann ->
Expr dep ann
transform = removeUnused . etaReduce . betaReduce . floatDown . floatUp . flattenLets . inline
transform = transformPass . transformPass . transformPass

transformPass ::
(Ord ann, Ord (dep Identifier)) =>
Expr dep ann ->
Expr dep ann
transformPass =
removeUnused
. etaReduce
. floatDown
. flattenLets
. floatUp
. betaReduce
. inline
2 changes: 2 additions & 0 deletions smol-core/src/Smol/Core/Transform/BetaReduce.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ betaReduceInternal (EApp ann (EAnn _ _ (ELambda _ann ident body)) val) =
betaReduceInternal $ ELet ann ident val (betaReduceInternal body)
betaReduceInternal (EApp annA (EApp annB (ELambda _ identA (ELambda _ identB body)) valA) valB) =
betaReduceInternal $ ELet annA identA valA (ELet annB identB valB (betaReduceInternal body))
betaReduceInternal (EApp annA (EApp annB (EAnn _ _ (ELambda _ identA (ELambda _ identB body))) valA) valB) =
betaReduceInternal $ ELet annA identA valA (ELet annB identB valB (betaReduceInternal body))
betaReduceInternal (EIf _ (EPrim _ (PBool True)) thenExpr _) =
betaReduceInternal thenExpr
betaReduceInternal (EIf _ (EPrim _ (PBool False)) _ elseExpr) =
Expand Down
4 changes: 3 additions & 1 deletion smol-core/src/Smol/Core/Transform/FlattenLets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Smol.Core.Types

-- | We don't want `let a = (let b = 1 in b + 1) in a + 1`
-- instead we want `let b = 1; let a = b + 1; a + 1
flattenLets :: Expr var ann -> Expr var ann
flattenLets :: (Eq (dep Identifier)) => Expr dep ann -> Expr dep ann
flattenLets (ELet ann ident (ELet ann' ident' expr' body') body) =
flattenLets $
ELet
Expand All @@ -18,4 +18,6 @@ flattenLets (ELet ann ident (ELet ann' ident' expr' body') body) =
(flattenLets body')
(flattenLets body)
)
-- also, turn `let a = b in a` into `b`
flattenLets (ELet _ ident body (EVar _ ident')) | ident == ident' = body
flattenLets other = mapExpr flattenLets other
Loading

0 comments on commit 38dd883

Please sign in to comment.