Skip to content

Commit 98420d3

Browse files
committed
Fix bug with eta reduction rewrite rule
1 parent d2809e7 commit 98420d3

File tree

10 files changed

+77
-10
lines changed

10 files changed

+77
-10
lines changed

flake.nix

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
yamlfmt
6666
];
6767
};
68+
69+
crossPlatforms =
70+
p:
71+
pkgs.lib.optionals pkgs.stdenv.hostPlatform.isx86_64 (
72+
pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ p.musl64 ]
73+
);
6874
};
6975
})
7076
];

lib/Language/PureScript/Backend/IR/Optimizer.hs

+10-2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ idempotently = fix $ \i f a →
171171
let a' = f a
172172
in if a' == a then a else i f a'
173173

174+
-- if a' == a
175+
-- then tr "FIXPOINT" a a
176+
-- else tr "RETRYING" a' (i f a')
177+
-- where
178+
-- tr ∷ Show x ⇒ String → x → y → y
179+
-- tr l x y = trace ("\n\n" <> l <> "\n" <> (toString . pShow) x <> "\n") y
180+
174181
optimizeModule UberModule UberModule
175182
optimizeModule UberModule {..} =
176183
UberModule
@@ -271,8 +278,9 @@ betaReduce =
271278
etaReduce RewriteRule Ann
272279
etaReduce =
273280
pure . \case
274-
Abs _ (ParamNamed _ _param) (App _ m (Ref _ (Local _) 0))
275-
Rewritten Recurse m
281+
Abs _ (ParamNamed _ param) (App _ m (Ref _ (Local param') 0))
282+
| param == param'
283+
Rewritten Recurse m
276284
_ NoChange
277285

278286
betaReduceUnusedParams RewriteRule Ann
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"builtWith":"0.15.15","comments":[],"decls":[{"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[3,1]}},"bindType":"NonRec","expression":{"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[3,1]}},"argument":"f","body":{"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[3,1]}},"argument":"a","body":{"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[3,1]}},"argument":"b","body":{"abstraction":{"abstraction":{"annotation":{"meta":null,"sourceSpan":{"end":[3,15],"start":[3,14]}},"type":"Var","value":{"identifier":"f","sourcePos":[3,1]}},"annotation":{"meta":null,"sourceSpan":{"end":[3,17],"start":[3,14]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[3,17],"start":[3,16]}},"type":"Var","value":{"identifier":"b","sourcePos":[3,1]}},"type":"App"},"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[3,14]}},"argument":{"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[3,18]}},"type":"Var","value":{"identifier":"a","sourcePos":[3,1]}},"type":"App"},"type":"Abs"},"type":"Abs"},"type":"Abs"},"identifier":"flip"}],"exports":["flip"],"foreign":[],"imports":[{"annotation":{"meta":null,"sourceSpan":{"end":[3,19],"start":[1,1]}},"moduleName":["Prim"]}],"moduleName":["Golden","Bug2","Test"],"modulePath":"golden/Golden/Bug2/Test.purs","reExports":{},"sourceSpan":{"end":[3,19],"start":[1,1]}}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
UberModule
2+
{ uberModuleBindings = [], uberModuleForeigns = [], uberModuleExports =
3+
[
4+
( Name "flip", Abs Nothing
5+
( ParamNamed Nothing ( Name "f" ) )
6+
( Abs Nothing
7+
( ParamNamed Nothing ( Name "a" ) )
8+
( Abs Nothing
9+
( ParamNamed Nothing ( Name "b" ) )
10+
( App Nothing
11+
( App Nothing
12+
( Ref Nothing ( Local ( Name "f" ) ) 0 )
13+
( Ref Nothing ( Local ( Name "b" ) ) 0 )
14+
)
15+
( Ref Nothing ( Local ( Name "a" ) ) 0 )
16+
)
17+
)
18+
)
19+
)
20+
]
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
flip = function(f)
3+
return function(a) return function(b) return f(b)(a) end end
4+
end
5+
}

test/ps/output/Golden.NameShadowing.Test/golden.ir

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,20 @@ UberModule
5151
)
5252
)
5353
),
54-
( Name "c", Ref Nothing
55-
( Imported ( ModuleName "Golden.NameShadowing.Test" ) ( Name "f" ) ) 0
54+
( Name "c", Abs Nothing
55+
( ParamNamed Nothing ( Name "y" ) )
56+
( Abs Nothing
57+
( ParamNamed Nothing ( Name "x1" ) )
58+
( App Nothing
59+
( App Nothing
60+
( Ref Nothing
61+
( Imported ( ModuleName "Golden.NameShadowing.Test" ) ( Name "f" ) ) 0
62+
)
63+
( Ref Nothing ( Local ( Name "x1" ) ) 0 )
64+
)
65+
( Ref Nothing ( Local ( Name "y" ) ) 0 )
66+
)
67+
)
5668
)
5769
]
5870
}

test/ps/output/Golden.NameShadowing.Test/golden.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ return {
1010
return M.Golden_NameShadowing_Test_f(M.Golden_NameShadowing_Test_f(x)(x1))(M.Golden_NameShadowing_Test_f(42)(1))
1111
end
1212
end,
13-
c = M.Golden_NameShadowing_Test_f
13+
c = function(y)
14+
return function(x1) return M.Golden_NameShadowing_Test_f(x1)(y) end
15+
end
1416
}

test/ps/output/Golden.RecursiveBindings.Test/golden.ir

+14-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,20 @@ UberModule
9696
( Nothing, Name "z", LiteralInt Nothing 1 ) :|
9797
[ RecursiveGroup
9898
(
99-
( Nothing, Name "a", Ref Nothing ( Local ( Name "b" ) ) 0 ) :|
100-
[ ( Nothing, Name "b", Ref Nothing ( Local ( Name "a" ) ) 0 ) ]
99+
( Nothing, Name "a", Abs Nothing ( ParamUnused Nothing )
100+
( App Nothing
101+
( Ref Nothing ( Local ( Name "b" ) ) 0 )
102+
( Ref Nothing ( Local ( Name "z" ) ) 0 )
103+
)
104+
) :|
105+
[
106+
( Nothing, Name "b", Abs Nothing ( ParamUnused Nothing )
107+
( App Nothing
108+
( Ref Nothing ( Local ( Name "a" ) ) 0 )
109+
( Ref Nothing ( Local ( Name "z" ) ) 0 )
110+
)
111+
)
112+
]
101113
), Standalone
102114
( Nothing, Name "f", Abs Nothing ( ParamUnused Nothing )
103115
( Ref Nothing ( Local ( Name "a" ) ) 0 )

test/ps/output/Golden.RecursiveBindings.Test/golden.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ return {
5757
local z = 1
5858
local a
5959
local b
60-
a = b
61-
b = a
60+
a = function() return b(z) end
61+
b = function() return a(z) end
6262
local f = function() return a end
6363
local y = f(z)(z)
6464
return f(f(y)(y))(f(y)(0))

test/ps/spago.dhall

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{ name = "test-project"
2-
, dependencies = [ "effect", "prelude" ]
2+
, dependencies = [ "console", "effect", "prelude" ]
33
, packages = ./packages.dhall
44
, sources = [ "golden/**/*.purs" ]
55
}

0 commit comments

Comments
 (0)