Skip to content

Commit

Permalink
Move applications inside Lets and Cases (#1659)
Browse files Browse the repository at this point in the history
* Move applications inside lets and cases

* make ormolu happy
  • Loading branch information
lukaszcz authored Dec 13, 2022
1 parent d9b020e commit bfadbae
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Juvix/Compiler/Core/Data/TransformationId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ data TransformationId
= LambdaLifting
| TopEtaExpand
| RemoveTypeArgs
| MoveApps
| Identity
deriving stock (Data)
8 changes: 7 additions & 1 deletion src/Juvix/Compiler/Core/Data/TransformationId/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pcompletions = do
TopEtaExpand -> strTopEtaExpand
Identity -> strIdentity
RemoveTypeArgs -> strRemoveTypeArgs
MoveApps -> strMoveApps

lexeme :: MonadParsec e Text m => m a -> m a
lexeme = L.lexeme L.hspace
Expand All @@ -61,13 +62,15 @@ transformation =
<|> symbol strIdentity $> Identity
<|> symbol strTopEtaExpand $> TopEtaExpand
<|> symbol strRemoveTypeArgs $> RemoveTypeArgs
<|> symbol strMoveApps $> MoveApps

allStrings :: [Text]
allStrings =
[ strLifting,
strTopEtaExpand,
strIdentity,
strRemoveTypeArgs
strRemoveTypeArgs,
strMoveApps
]

strLifting :: Text
Expand All @@ -81,3 +84,6 @@ strIdentity = "identity"

strRemoveTypeArgs :: Text
strRemoveTypeArgs = "remove-type-args"

strMoveApps :: Text
strMoveApps = "move-apps"
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Core/Transformation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Juvix.Compiler.Core.Transformation.Base
import Juvix.Compiler.Core.Transformation.Eta
import Juvix.Compiler.Core.Transformation.Identity
import Juvix.Compiler.Core.Transformation.LambdaLifting
import Juvix.Compiler.Core.Transformation.MoveApps
import Juvix.Compiler.Core.Transformation.RemoveTypeArgs
import Juvix.Compiler.Core.Transformation.TopEtaExpand

Expand All @@ -25,3 +26,4 @@ applyTransformations ts tbl = foldl' (flip appTrans) tbl ts
Identity -> identity
TopEtaExpand -> topEtaExpand
RemoveTypeArgs -> removeTypeArgs
MoveApps -> moveApps
37 changes: 37 additions & 0 deletions src/Juvix/Compiler/Core/Transformation/MoveApps.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Juvix.Compiler.Core.Transformation.MoveApps
( moveApps,
module Juvix.Compiler.Core.Transformation.Base,
)
where

import Juvix.Compiler.Core.Extra
import Juvix.Compiler.Core.Transformation.Base

convertNode :: Node -> Node
convertNode = dmap go
where
go :: Node -> Node
go node = case node of
NApp {} ->
let (tgt, args) = unfoldApps node
in case tgt of
NLet lt@(Let {..}) ->
NLet lt {_letBody = mkApps _letBody (map (second (shift 1)) args)}
NCase cs@(Case {..}) ->
NCase
cs
{ _caseBranches =
map
( \br@CaseBranch {..} ->
br
{ _caseBranchBody = mkApps _caseBranchBody (map (second (shift _caseBranchBindersNum)) args)
}
)
_caseBranches,
_caseDefault = fmap (`mkApps` args) _caseDefault
}
_ -> node
_ -> node

moveApps :: InfoTable -> InfoTable
moveApps tab = mapT (const convertNode) tab
7 changes: 6 additions & 1 deletion test/Core/Eval/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,10 @@ tests =
"Type application and abstraction"
"."
"test045.jvc"
"out/test045.out"
"out/test045.out",
PosTest
"Applications with lets and cases in function position"
"."
"test046.jvc"
"out/test046.out"
]
4 changes: 2 additions & 2 deletions test/Core/Transformation/RemoveTypeArgs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Core.Transformation.Base
import Juvix.Compiler.Core.Transformation

allTests :: TestTree
allTests = testGroup "Remove type arguments" (map liftTest Eval.tests)
allTests = testGroup "Move applications and remove type arguments" (map liftTest Eval.tests)

pipe :: [TransformationId]
pipe = [LambdaLifting, RemoveTypeArgs]
pipe = [LambdaLifting, MoveApps, RemoveTypeArgs]

liftTest :: Eval.PosTest -> TestTree
liftTest _testEval =
Expand Down
1 change: 1 addition & 0 deletions tests/Core/positive/out/test046.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
12 changes: 12 additions & 0 deletions tests/Core/positive/test046.jvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- applications with lets and cases in function position

inductive list {
nil : list;
cons : any -> list -> list;
};

def f := \l (case l of { cons x _ := x; nil := \x x } ) (let y := \x x in (let z := \x x in case l of { _ := \x x } z) y) 7;

def main := f (cons (\x \y x y + 2) nil);

main

0 comments on commit bfadbae

Please sign in to comment.