Skip to content

Commit f5bd1b6

Browse files
committed
Backwards compat option
1 parent e318b91 commit f5bd1b6

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

src/Lean/Meta/Match/Match.lean

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public section
1717

1818
namespace Lean.Meta.Match
1919

20+
register_builtin_option backwards.match.rowMajor : Bool := {
21+
defValue := true
22+
group := "bootstrap"
23+
descr := "If true (the default), match compilation will split the discrimnants based \
24+
on position of the first constructor pattern in the first alternative. If false, \
25+
it splits them from left to right, which can lead to unnecessary code bloat."
26+
}
27+
28+
2029
private def mkIncorrectNumberOfPatternsMsg [ToMessageData α]
2130
(discrepancyKind : String) (expected actual : Nat) (pats : List α) :=
2231
let patternsMsg := MessageData.joinSep (pats.map toMessageData) ", "
@@ -818,19 +827,20 @@ private partial def process (p : Problem) : StateRefT State MetaM Unit := do
818827
process p
819828
return
820829

821-
match firstRefutablePattern p with
822-
| some i =>
823-
if i > 0 then
824-
traceStep ("move var to front")
825-
process (moveToFront p i)
826-
return
827-
| none =>
828-
if 1 < p.alts.length then
829-
traceStep ("drop all but first alt")
830-
-- all patterns are irrefutable, we can drop all other alts
831-
let p := { p with alts := p.alts.take 1 }
832-
process p
833-
return
830+
if backwards.match.rowMajor.get (← getOptions) then
831+
match firstRefutablePattern p with
832+
| some i =>
833+
if i > 0 then
834+
traceStep ("move var to front")
835+
process (moveToFront p i)
836+
return
837+
| none =>
838+
if 1 < p.alts.length then
839+
traceStep ("drop all but first alt")
840+
-- all patterns are irrefutable, we can drop all other alts
841+
let p := { p with alts := p.alts.take 1 }
842+
process p
843+
return
834844

835845
if (← isNatValueTransition p) then
836846
traceStep ("nat value to constructor")

tests/lean/run/issue10749.lean

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,22 @@ fun motive x x_1 x_2 x_3 x_4 h_1 h_2 h_3 h_4 h_5 h_6 =>
8989
-/
9090
#guard_msgs in
9191
#print test4.match_1
92+
93+
-- Just testing the backwards compatibility option
94+
95+
set_option match.ignoreUnusedAlts true in
96+
set_option backwards.match.rowMajor false in
97+
def testOld (a : List Nat) : Nat :=
98+
match a with
99+
| _ => 3
100+
| [] => 4
101+
102+
-- Has unnecessary `casesOn`
103+
104+
/--
105+
info: def testOld.match_1.{u_1} : (motive : List Nat → Sort u_1) →
106+
(a : List Nat) → ((x : List Nat) → motive x) → (Unit → motive []) → motive a :=
107+
fun motive a h_1 h_2 => List.casesOn a (h_1 []) fun head tail => h_1 (head :: tail)
108+
-/
109+
#guard_msgs in
110+
#print testOld.match_1

0 commit comments

Comments
 (0)