lean4-htt/tests/lean/run/double_match.lean
Joachim Breitner d41f39fb10
perf: sparse case splitting in match compilation (#10823)
This PR lets the match compilation procedure use sparse case analysis
when the patterns only match on some but not all constructors of an
inductive type. This way, less code is produce. Before, code handling
each of the other cases was then optimized and commoned-up by later
compilation pipeline, but that is wasteful to do.

In some cases this will prevent Lean from noticing that a match
statement is complete
because it performs less case-splitting for the unreachable case. In
this case, give explicit
patterns to perform the deeper split with `by contradiction` as the
right-hand side.

At least temporarily, there is also the option to disable this behaviour
with
```
set_option backwards.match.sparseCases false
```
2025-11-06 13:46:35 +00:00

23 lines
556 B
Text

/--
trace: [Compiler.saveMono] size: 7
def foo f x : Option Nat :=
let _x.1 := f x;
cases _x.1 : Option Nat
| Option.some val.2 =>
let _x.3 := Nat.add val.2 val.2;
let _x.4 := some ◾ _x.3;
return _x.4
| _ =>
let _x.5 := none ◾;
return _x.5
-/
#guard_msgs in
set_option trace.Compiler.saveMono true in
def foo (f : Nat → Option Nat) (x : Nat) : Option Nat :=
if let some val := f x then
if let some val2 := f x then
some <| val + val2
else
none
else
none