Skip to content

Commit

Permalink
Fix: eta reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
Unisay committed Sep 10, 2024
1 parent bef2ba6 commit 833cec8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions .hspec-failures
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FailureReport {failureReportSeed = 1246204243, failureReportMaxSuccess = 100, failureReportMaxSize = 100, failureReportMaxDiscardRatio = 10, failureReportPaths = []}
5 changes: 2 additions & 3 deletions lib/Language/PureScript/Backend/IR/Optimizer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,12 @@ etaReduce ∷ RewriteRule Ann
etaReduce =
pure . \case
Abs _ (ParamNamed _ param) (App _ m (Ref _ (Local param') 0))
| param == param'
| param == param' && countFreeRef (Local param) m == 0
Rewritten Recurse m
_ NoChange

betaReduceUnusedParams RewriteRule Ann
betaReduceUnusedParams =
pure . \case
betaReduceUnusedParams = pure . \case
App _ (Abs _ (ParamUnused _) body) _arg
Rewritten Recurse body
_ NoChange
Expand Down
15 changes: 14 additions & 1 deletion test/ps/golden/Golden/Inline/Test.purs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module Golden.Inline.Test where
module Golden.Inline.Test
( main
, Mu
, runMu
, iMu
) where

main :: Int
main =
Expand All @@ -7,3 +12,11 @@ main =
in let y :: forall b. b -> Int
y _ = 2
in x y

newtype Mu a = MkMu (Mu a -> a)

runMu :: forall a. Mu a -> a
runMu mu@(MkMu f) = f mu

iMu :: Mu Int
iMu = MkMu runMu
2 changes: 1 addition & 1 deletion test/ps/output/Golden.Inline.Test/corefn.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"builtWith":"0.15.15","comments":[],"decls":[{"annotation":{"meta":null,"sourceSpan":{"end":[3,12],"start":[3,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[9,13],"start":[5,3]}},"binds":[{"annotation":{"meta":null,"sourceSpan":{"end":[5,30],"start":[5,7]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[5,30],"start":[5,7]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[6,14],"start":[6,13]}},"type":"Literal","value":{"literalType":"IntLiteral","value":1}},"type":"Abs"},"identifier":"x"}],"expression":{"annotation":{"meta":null,"sourceSpan":{"end":[9,13],"start":[7,6]}},"binds":[{"annotation":{"meta":null,"sourceSpan":{"end":[7,34],"start":[7,11]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[7,34],"start":[7,11]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[8,18],"start":[8,17]}},"type":"Literal","value":{"literalType":"IntLiteral","value":2}},"type":"Abs"},"identifier":"y"}],"expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[9,11],"start":[9,10]}},"type":"Var","value":{"identifier":"x","sourcePos":[5,7]}},"annotation":{"meta":null,"sourceSpan":{"end":[9,13],"start":[9,10]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[9,13],"start":[9,12]}},"type":"Var","value":{"identifier":"y","sourcePos":[7,11]}},"type":"App"},"type":"Let"},"type":"Let"},"identifier":"main"}],"exports":["main"],"foreign":[],"imports":[{"annotation":{"meta":null,"sourceSpan":{"end":[9,13],"start":[1,1]}},"moduleName":["Prim"]}],"moduleName":["Golden","Inline","Test"],"modulePath":"golden/Golden/Inline/Test.purs","reExports":{},"sourceSpan":{"end":[9,13],"start":[1,1]}}
{"builtWith":"0.15.15","comments":[],"decls":[{"annotation":{"meta":null,"sourceSpan":{"end":[16,32],"start":[16,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":{"metaType":"IsNewtype"},"sourceSpan":{"end":[16,32],"start":[16,1]}},"argument":"x","body":{"annotation":{"meta":null,"sourceSpan":{"end":[16,32],"start":[16,1]}},"type":"Var","value":{"identifier":"x","sourcePos":[0,0]}},"type":"Abs"},"identifier":"MkMu"},{"annotation":{"meta":null,"sourceSpan":{"end":[18,29],"start":[18,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[18,29],"start":[18,1]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[18,29],"start":[18,1]}},"caseAlternatives":[{"binders":[{"annotation":{"meta":null,"sourceSpan":{"end":[19,18],"start":[19,7]}},"binder":{"annotation":{"meta":{"metaType":"IsNewtype"},"sourceSpan":{"end":[19,17],"start":[19,11]}},"binderType":"ConstructorBinder","binders":[{"annotation":{"meta":null,"sourceSpan":{"end":[19,17],"start":[19,16]}},"binderType":"VarBinder","identifier":"f"}],"constructorName":{"identifier":"MkMu","moduleName":["Golden","Inline","Test"]},"typeName":{"identifier":"Mu","moduleName":["Golden","Inline","Test"]}},"binderType":"NamedBinder","identifier":"mu"}],"expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[19,22],"start":[19,21]}},"type":"Var","value":{"identifier":"f","sourcePos":[19,16]}},"annotation":{"meta":null,"sourceSpan":{"end":[19,25],"start":[19,21]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[19,25],"start":[19,23]}},"type":"Var","value":{"identifier":"mu","sourcePos":[19,7]}},"type":"App"},"isGuarded":false}],"caseExpressions":[{"annotation":{"meta":null,"sourceSpan":{"end":[19,25],"start":[19,1]}},"type":"Var","value":{"identifier":"v","sourcePos":[0,0]}}],"type":"Case"},"type":"Abs"},"identifier":"runMu"},{"annotation":{"meta":null,"sourceSpan":{"end":[8,12],"start":[8,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[14,13],"start":[10,3]}},"binds":[{"annotation":{"meta":null,"sourceSpan":{"end":[10,30],"start":[10,7]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[10,30],"start":[10,7]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[11,14],"start":[11,13]}},"type":"Literal","value":{"literalType":"IntLiteral","value":1}},"type":"Abs"},"identifier":"x"}],"expression":{"annotation":{"meta":null,"sourceSpan":{"end":[14,13],"start":[12,6]}},"binds":[{"annotation":{"meta":null,"sourceSpan":{"end":[12,34],"start":[12,11]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[12,34],"start":[12,11]}},"argument":"v","body":{"annotation":{"meta":null,"sourceSpan":{"end":[13,18],"start":[13,17]}},"type":"Literal","value":{"literalType":"IntLiteral","value":2}},"type":"Abs"},"identifier":"y"}],"expression":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[14,11],"start":[14,10]}},"type":"Var","value":{"identifier":"x","sourcePos":[10,7]}},"annotation":{"meta":null,"sourceSpan":{"end":[14,13],"start":[14,10]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[14,13],"start":[14,12]}},"type":"Var","value":{"identifier":"y","sourcePos":[12,11]}},"type":"App"},"type":"Let"},"type":"Let"},"identifier":"main"},{"annotation":{"meta":null,"sourceSpan":{"end":[21,14],"start":[21,1]}},"bindType":"NonRec","expression":{"abstraction":{"annotation":{"meta":{"metaType":"IsNewtype"},"sourceSpan":{"end":[22,11],"start":[22,7]}},"type":"Var","value":{"identifier":"MkMu","moduleName":["Golden","Inline","Test"]}},"annotation":{"meta":null,"sourceSpan":{"end":[22,17],"start":[22,7]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[22,17],"start":[22,12]}},"type":"Var","value":{"identifier":"runMu","moduleName":["Golden","Inline","Test"]}},"type":"App"},"identifier":"iMu"}],"exports":["main","runMu","iMu"],"foreign":[],"imports":[{"annotation":{"meta":null,"sourceSpan":{"end":[22,17],"start":[1,1]}},"moduleName":["Golden","Inline","Test"]},{"annotation":{"meta":null,"sourceSpan":{"end":[22,17],"start":[1,1]}},"moduleName":["Prim"]}],"moduleName":["Golden","Inline","Test"],"modulePath":"golden/Golden/Inline/Test.purs","reExports":{},"sourceSpan":{"end":[22,17],"start":[1,1]}}
23 changes: 21 additions & 2 deletions test/ps/output/Golden.Inline.Test/golden.ir
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
UberModule
{ uberModuleBindings = [], uberModuleForeigns = [], uberModuleExports =
[ ( Name "main", LiteralInt Nothing 1 ) ]
{ uberModuleBindings =
[ Standalone
( QName
{ qnameModuleName = ModuleName "Golden.Inline.Test", qnameName = Name "runMu"
}, Abs Nothing
( ParamNamed Nothing ( Name "v" ) )
( App Nothing
( Ref Nothing ( Local ( Name "v" ) ) 0 )
( Ref Nothing ( Local ( Name "v" ) ) 0 )
)
)
], uberModuleForeigns = [], uberModuleExports =
[
( Name "main", LiteralInt Nothing 1 ),
( Name "runMu", Ref Nothing
( Imported ( ModuleName "Golden.Inline.Test" ) ( Name "runMu" ) ) 0
),
( Name "iMu", Ref Nothing
( Imported ( ModuleName "Golden.Inline.Test" ) ( Name "runMu" ) ) 0
)
]
}
8 changes: 7 additions & 1 deletion test/ps/output/Golden.Inline.Test/golden.lua
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
return { main = 1 }
local M = {}
M.Golden_Inline_Test_runMu = function(v) return v(v) end
return {
main = 1,
runMu = M.Golden_Inline_Test_runMu,
iMu = M.Golden_Inline_Test_runMu
}

0 comments on commit 833cec8

Please sign in to comment.