Commit graph

11 commits

Author SHA1 Message Date
Rob23oba
5b9567b144
fix: complete overhaul of structural recursion on inductives predicates (#9995)
This PR almost completely rewrites the inductive predicate recursion
algorithm; in particular `IndPredBelow` to function more consistently.
Historically, the `brecOn` generation through `IndPredBelow` has been
very error-prone -- this should be fixed now since the new algorithm is
very direct and doesn't rely on tactics or meta-variables at all.
Additionally, the new structural recursion procedure for inductive
predicates shares more code with regular structural recursion and thus
allows for mutual and nested recursion in the same way it was possible
with regular structural recursion. For example, the following works now:
```lean-4
mutual

inductive Even : Nat → Prop where
  | zero : Even 0
  | succ (h : Odd n) : Even n.succ

inductive Odd : Nat → Prop where
  | succ (h : Even n) : Odd n.succ

end

mutual

theorem Even.exists (h : Even n) : ∃ a, n = 2 * a :=
  match h with
  | .zero => ⟨0, rfl⟩
  | .succ h =>
    have ⟨a, ha⟩ := h.exists
    ⟨a + 1, congrArg Nat.succ ha⟩
termination_by structural h

theorem Odd.exists (h : Odd n) : ∃ a, n = 2 * a + 1 :=
  match h with
  | .succ h =>
    have ⟨a, ha⟩ := h.exists
    ⟨a, congrArg Nat.succ ha⟩
termination_by structural h

end
```

Closes #1672
Closes #10004
2025-09-01 08:17:58 +00:00
Leonardo de Moura
4ee131981d feat: in an inductive family the longest fixed prefix of indices is now promoted to parameters
This modification is relevant for fixing regressions on recent changes
to the auto implicit behavior for inductive families.

The following declarations are now accepted:
```lean
inductive HasType : Fin n → Vector Ty n → Ty → Type where
  | stop : HasType 0 (ty :: ctx) ty
  | pop  : HasType k ctx ty → HasType k.succ (u :: ctx) ty

inductive Sublist : List α → List α → Prop
  | slnil : Sublist [] []
  | cons l₁ l₂ a : Sublist l₁ l₂ → Sublist l₁ (a :: l₂)
  | cons2 l₁ l₂ a : Sublist l₁ l₂ → Sublist (a :: l₁) (a :: l₂)

inductive Lst : Type u → Type u
  | nil  : Lst α
  | cons : α → Lst α → Lst α
```

TODO: universe inference for `inductive` should be improved. The
current approach is not good enough when we have auto implicits.

TODO: allow implicit fixed indices that do not depend on indices that
cannot be moved to become parameters.
2022-03-08 17:56:34 -08:00
Daniel Fabian
b7ecc1acc3 refactor: Make the non-below version of a premise in the below type for inductive predicates implicit.
Since it is always fully implied by the below version thereof, it carries no real information and shouldn't be used in pattern matching.
2021-06-06 06:40:09 -07:00
Daniel Fabian
822c551aa2 test: Add a bunch of test for structural recursion on predicates. 2021-06-06 06:40:09 -07:00
Daniel Fabian
c426a816a1 refactor: Make the non-below version of a premise in the below type for inductive predicates implicit.
Since it is always fully implied by the below version thereof, it carries no real information and shouldn't be used in pattern matching.
2021-05-22 18:09:32 -07:00
Daniel Fabian
ab0ef229ac feat: add getBelowIndices. 2021-05-19 07:28:14 -07:00
Leonardo de Moura
56d5d6c564 chore: fix tests 2021-05-04 15:42:03 -07:00
Daniel Fabian
1f05f5bf11 chore: rename ProofBelow to below. 2021-04-26 20:33:21 +02:00
Daniel Fabian
eda4bdd337 test: extend inductive_pred.lean with tests using the new construction. 2021-04-25 20:02:22 -07:00
Leonardo de Moura
964fd3f520 chore: fixes tests
closes #405
2021-04-22 20:22:43 -07:00
Leonardo de Moura
f631bd8df9 test: inductive predicate example 2021-04-02 16:21:54 -07:00