Skip to content

Commit 97d07c8

Browse files
committed
[ optimise ] Union double matching on one variable
1 parent 9183464 commit 97d07c8

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

src/Core/Case/Optimise.idr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ optimiseTree ps (Case idx el ty alts)
114114
Right p => case mapFst catMaybes $ optimiseAlts ps var p alts of
115115
([], def) => def
116116
(alts, Nothing) => Just $ Case idx el ty alts
117+
(alts, Just def@(Case idx' _ _ alts')) =>
118+
Just $ Case idx el ty $ alts ++
119+
if idx == idx' then alts' else [DefaultCase def]
117120
(alts, Just def) =>
118121
Just $ Case idx el ty $ alts ++ [DefaultCase def]
119122
optimiseTree _ tm = Just tm

tests/idris2/casetree/casetree006/expected

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ Compile time tree: case {arg:1} of
1010
First {e:25} {e:26} {e:27} => 1
1111
True => case {arg:5} of
1212
First {e:28} {e:29} {e:30} => 2
13-
_ => case {arg:3} of
14-
(::) {e:1} {e:2} {e:3} => case {arg:5} of
15-
First {e:4} {e:5} {e:6} => 5
13+
(::) {e:1} {e:2} {e:3} => case {arg:5} of
14+
First {e:4} {e:5} {e:6} => 5
1615
S {e:19} => case {arg:3} of
1716
Nil {e:31} => case {arg:4} of
1817
(::) {e:32} {e:33} {e:34} {e:35} => case {e:34} of
1918
False => case {arg:5} of
2019
Later {e:36} {e:37} {e:38} {e:39} {e:40} {e:41} => 3
2120
True => case {arg:5} of
2221
Later {e:42} {e:43} {e:44} {e:45} {e:46} {e:47} => 4
23-
_ => case {arg:3} of
24-
(::) {e:7} {e:8} {e:9} => case {arg:5} of
25-
Later {e:10} {e:11} {e:12} {e:13} {e:14} {e:15} => 6
22+
(::) {e:7} {e:8} {e:9} => case {arg:5} of
23+
Later {e:10} {e:11} {e:12} {e:13} {e:14} {e:15} => 6
2624
_ => case {arg:2} of
2725
Z => case {arg:3} of
2826
(::) {e:1} {e:2} {e:3} => case {arg:5} of
@@ -34,7 +32,7 @@ Erasable args: [0, 5]
3432
Detaggable arg types: [4, 5]
3533
Inferrable args: [0, 2]
3634
Compiled: \ {arg:1}, {arg:2}, {arg:3}, {arg:4} => case {arg:1} of
37-
{ _builtin.CONS {tag = 1} [cons] {e:8} {e:9} => case {arg:2} of { 0 => case {arg:3} of { _builtin.NIL {tag = 0} [nil] => case {arg:4} of { _builtin.CONS {tag = 1} [cons] {e:14} {e:15} => case {e:14} of { 0 => 1; 1 => 2}}; _ => case {arg:3} of { _builtin.CONS {tag = 1} [cons] {e:2} {e:3} => 5}}; _ => case {arg:3} of { _builtin.NIL {tag = 0} [nil] => case {arg:4} of { _builtin.CONS {tag = 1} [cons] {e:19} {e:20} => case {e:19} of { 0 => 3; 1 => 4}}; _ => case {arg:3} of { _builtin.CONS {tag = 1} [cons] {e:5} {e:6} => 6}}}
35+
{ _builtin.CONS {tag = 1} [cons] {e:8} {e:9} => case {arg:2} of { 0 => case {arg:3} of { _builtin.NIL {tag = 0} [nil] => case {arg:4} of { _builtin.CONS {tag = 1} [cons] {e:14} {e:15} => case {e:14} of { 0 => 1; 1 => 2}}; _builtin.CONS {tag = 1} [cons] {e:2} {e:3} => 5}; _ => case {arg:3} of { _builtin.NIL {tag = 0} [nil] => case {arg:4} of { _builtin.CONS {tag = 1} [cons] {e:19} {e:20} => case {e:19} of { 0 => 3; 1 => 4}}; _builtin.CONS {tag = 1} [cons] {e:5} {e:6} => 6}}
3836
; _ => case {arg:2} of
3937
{ 0 => case {arg:3} of
4038
{ _builtin.CONS {tag = 1} [cons] {e:2} {e:3} => 5

tests/idris2/total/total022/expected

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@ Arguments [{arg:1}, {arg:2}, {arg:3}, {arg:4}]
44
Compile time tree: case {arg:1} of
55
Nil {e:1} => case {arg:4} of
66
Nothing {e:2} => ()
7-
_ => case {arg:4} of
8-
Just {e:3} {e:4} => case {e:4} of
9-
Refl {e:5} {e:6} => f {arg:3} {arg:2} {arg:3} Nothing
7+
Just {e:3} {e:4} => case {e:4} of
8+
Refl {e:5} {e:6} => f {arg:3} {arg:2} {arg:3} Nothing
109
(::) {e:7} {e:8} {e:9} => case {arg:4} of
1110
Nothing {e:10} => f {e:9} {arg:2} {arg:3} Nothing
12-
_ => case {arg:4} of
13-
Just {e:3} {e:4} => case {e:4} of
14-
Refl {e:5} {e:6} => f {arg:3} {arg:2} {arg:3} Nothing
11+
Just {e:3} {e:4} => case {e:4} of
12+
Refl {e:5} {e:6} => f {arg:3} {arg:2} {arg:3} Nothing
1513
_ => case {arg:4} of
1614
Just {e:3} {e:4} => case {e:4} of
1715
Refl {e:5} {e:6} => f {arg:3} {arg:2} {arg:3} Nothing
1816
Compiled: \ {arg:1}, {arg:2}, {arg:3}, {arg:4} => case {arg:1} of
19-
{ _builtin.NIL {tag = 0} [nil] => case {arg:4} of { _builtin.NOTHING {tag = 0} [nothing] => ___; _ => case {arg:4} of { _builtin.JUST {tag = 1} [just] {e:4} => Main.f {arg:3} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing])}}
20-
; _builtin.CONS {tag = 1} [cons] {e:8} {e:9} => case {arg:4} of { _builtin.NOTHING {tag = 0} [nothing] => Main.f {e:9} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing]); _ => case {arg:4} of { _builtin.JUST {tag = 1} [just] {e:4} => Main.f {arg:3} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing])}}
17+
{ _builtin.NIL {tag = 0} [nil] => case {arg:4} of { _builtin.NOTHING {tag = 0} [nothing] => ___; _builtin.JUST {tag = 1} [just] {e:4} => Main.f {arg:3} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing])}
18+
; _builtin.CONS {tag = 1} [cons] {e:8} {e:9} => case {arg:4} of { _builtin.NOTHING {tag = 0} [nothing] => Main.f {e:9} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing]); _builtin.JUST {tag = 1} [just] {e:4} => Main.f {arg:3} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing])}
2119
; _ => case {arg:4} of
2220
{ _builtin.JUST {tag = 1} [just] {e:4} => Main.f {arg:3} {arg:2} {arg:3} (_builtin.NOTHING {tag = 0} [nothing])
2321
}

0 commit comments

Comments
 (0)