This PR adds “non-branching case statements”: For each inductive
constructor `T.con` this adds a function `T.con.with` that is similar
`T.casesOn`, but has only one arm (the one for `con`), and an additional
`t.toCtorIdx = 12` assumption.
For example:
```lean
inductive Vec (α : Type) : Nat → Type where
| nil : Vec α 0
| cons {n} : α → Vec α n → Vec α (n + 1)
/--
info: @[reducible] protected def Vec.cons.elim.{u} : {α : Type} →
{motive : (a : Nat) → Vec α a → Sort u} →
{a : Nat} →
(t : Vec α a) →
t.ctorIdx = 1 → ({n : Nat} → (a : α) → (a_1 : Vec α n) → motive (n + 1) (Vec.cons a a_1)) → motive a t
-/
#guard_msgs in
#print sig Vec.cons.elim
```
This is a building block for non-quadratic implementations of `BEq` and
`DecidableEq` etc.
Builds on top of #9951.
The compiled code for a these functions could presumably, without
branching on the inductive value, directly access the fields. Achieving
this optimization (and achieving it without a quadratic compilation
cost) is not in scope for this PR.
26 lines
510 B
Text
26 lines
510 B
Text
import Lean
|
|
|
|
set_option compiler.checkTypes true
|
|
|
|
def f1 (x : Option Nat) (y : Nat) : Nat :=
|
|
let z := some y
|
|
if let (some x, some y) := (x, z) then
|
|
x + y
|
|
else
|
|
0
|
|
|
|
set_option compiler.checkTypes false -- disabled due to type checking withCtor
|
|
|
|
inductive Ty where
|
|
| c1 | c2 | c3 | c4 | c5
|
|
|
|
set_option compiler.checkTypes true
|
|
|
|
def f2 (a b : Ty) (n : Nat) : Nat :=
|
|
let x := match a with
|
|
| .c1 => 10 * n
|
|
| _ => 20 * n
|
|
let y := match b with
|
|
| .c2 => 10 + n
|
|
| _ => 20 + n
|
|
x + y
|