Skip to content

Commit

Permalink
chore: address @[elab_as_elim] porting notes (#17625)
Browse files Browse the repository at this point in the history
- Now Lean 4 supports complex discriminants, so this PR re-enables `@[elab_as_elim]` for these eliminators. These should be reviewed to decide whether they should be `@[elab_as_elim]`, since this elaborator requires an expected type to function, so is more restrictive.
- Functions that return something non-dependent should never be `@[elab_as_elim]`, since this case doesn't need anything special for successful elaboration. Plus, `@[elab_as_elim]` would make it more likely to fail.
  • Loading branch information
kmill committed Oct 11, 2024
1 parent 49e5607 commit 98e84e2
Show file tree
Hide file tree
Showing 11 changed files with 8 additions and 28 deletions.
9 changes: 3 additions & 6 deletions Mathlib/Computability/Reduce.lean
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,7 @@ protected theorem ind_on {C : ManyOneDegree → Prop} (d : ManyOneDegree)
(h : ∀ p : Set ℕ, C (of p)) : C d :=
Quotient.inductionOn' d h

/-- Lifts a function on sets of natural numbers to many-one degrees.
-/
-- @[elab_as_elim] -- Porting note: unexpected eliminator resulting type
/-- Lifts a function on sets of natural numbers to many-one degrees. -/
protected abbrev liftOn {φ} (d : ManyOneDegree) (f : Set ℕ → φ)
(h : ∀ p q, ManyOneEquiv p q → f p = f q) : φ :=
Quotient.liftOn' d f h
Expand All @@ -323,9 +321,8 @@ protected theorem liftOn_eq {φ} (p : Set ℕ) (f : Set ℕ → φ)
(h : ∀ p q, ManyOneEquiv p q → f p = f q) : (of p).liftOn f h = f p :=
rfl

/-- Lifts a binary function on sets of natural numbers to many-one degrees.
-/
@[reducible, simp] -- @[elab_as_elim] -- Porting note: unexpected eliminator resulting type
/-- Lifts a binary function on sets of natural numbers to many-one degrees. -/
@[reducible, simp]
protected def liftOn₂ {φ} (d₁ d₂ : ManyOneDegree) (f : Set ℕ → Set ℕ → φ)
(h : ∀ p₁ p₂ q₁ q₂, ManyOneEquiv p₁ p₂ → ManyOneEquiv q₁ q₂ → f p₁ q₁ = f p₂ q₂) : φ :=
d₁.liftOn (fun p => d₂.liftOn (f p) fun q₁ q₂ hq => h _ _ _ _ (by rfl) hq)
Expand Down
1 change: 0 additions & 1 deletion Mathlib/Computability/TuringMachine.lean
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ instance ListBlank.hasEmptyc {Γ} [Inhabited Γ] : EmptyCollection (ListBlank Γ

/-- A modified version of `Quotient.liftOn'` specialized for `ListBlank`, with the stronger
precondition `BlankExtends` instead of `BlankRel`. -/
-- Porting note: Removed `@[elab_as_elim]`
protected abbrev ListBlank.liftOn {Γ} [Inhabited Γ] {α} (l : ListBlank Γ) (f : List Γ → α)
(H : ∀ a b, BlankExtends a b → f a = f b) : α :=
l.liftOn' f <| by rintro a b (h | h) <;> [exact H _ _ h; exact (H _ _ h).symm]
Expand Down
2 changes: 0 additions & 2 deletions Mathlib/Data/Finmap.lean
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ lemma nodup_entries (f : Finmap β) : f.entries.Nodup := f.nodupKeys.nodup
/-! ### Lifting from AList -/

/-- Lift a permutation-respecting function on `AList` to `Finmap`. -/
-- @[elab_as_elim] Porting note: we can't add `elab_as_elim` attr in this type
def liftOn {γ} (s : Finmap β) (f : AList β → γ)
(H : ∀ a b : AList β, a.entries ~ b.entries → f a = f b) : γ := by
refine
Expand All @@ -108,7 +107,6 @@ theorem liftOn_toFinmap {γ} (s : AList β) (f : AList β → γ) (H) : liftOn
rfl

/-- Lift a permutation-respecting function on 2 `AList`s to 2 `Finmap`s. -/
-- @[elab_as_elim] Porting note: we can't add `elab_as_elim` attr in this type
def liftOn₂ {γ} (s₁ s₂ : Finmap β) (f : AList β → AList β → γ)
(H : ∀ a₁ b₁ a₂ b₂ : AList β,
a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries → f a₁ b₁ = f a₂ b₂) : γ :=
Expand Down
4 changes: 2 additions & 2 deletions Mathlib/Data/Ordmap/Ordset.lean
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,12 @@ theorem splitMax_eq :
| _, l, x, nil => rfl
| _, l, x, node ls ll lx lr => by rw [splitMax', splitMax_eq ls ll lx lr, findMax', eraseMax]

-- @[elab_as_elim] -- Porting note: unexpected eliminator resulting type
@[elab_as_elim]
theorem findMin'_all {P : α → Prop} : ∀ (t) (x : α), All P t → P x → P (findMin' t x)
| nil, _x, _, hx => hx
| node _ ll lx _, _, ⟨h₁, h₂, _⟩, _ => findMin'_all ll lx h₁ h₂

-- @[elab_as_elim] -- Porting note: unexpected eliminator resulting type
@[elab_as_elim]
theorem findMax'_all {P : α → Prop} : ∀ (x : α) (t), P x → All P t → P (findMax' x t)
| _x, nil, hx, _ => hx
| _, node _ _ lx lr, _, ⟨_, h₂, h₃⟩ => findMax'_all lx lr h₂ h₃
Expand Down
5 changes: 0 additions & 5 deletions Mathlib/Data/Quot.lean
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ theorem liftOn_mk (a : α) (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f
fun hf => hf.comp Quot.exists_rep, fun hf y => let ⟨x, hx⟩ := hf y; ⟨Quot.mk _ x, hx⟩⟩

/-- Descends a function `f : α → β → γ` to quotients of `α` and `β`. -/
-- Porting note: removed `@[elab_as_elim]`, gave "unexpected resulting type γ"
-- porting note (#11083): removed `@[reducible]` because it caused extremely slow `simp`
protected def lift₂ (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂)
(hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) (q₁ : Quot r) (q₂ : Quot s) : γ :=
Expand All @@ -123,7 +122,6 @@ theorem lift₂_mk (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ →
rfl

/-- Descends a function `f : α → β → γ` to quotients of `α` and `β` and applies it. -/
-- porting note (#11083): removed `@[elab_as_elim]`, gave "unexpected resulting type γ"
-- porting note (#11083): removed `@[reducible]` because it caused extremely slow `simp`
protected def liftOn₂ (p : Quot r) (q : Quot s) (f : α → β → γ)
(hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) : γ :=
Expand Down Expand Up @@ -451,7 +449,6 @@ protected theorem lift_mk (f : α → β) (c) (a : α) : lift f c (mk a) = f a :
rfl

/-- Lift a constant function on `q : Trunc α`. -/
-- Porting note: removed `@[elab_as_elim]` because it gave "unexpected eliminator resulting type"
-- porting note (#11083): removed `@[reducible]` because it caused extremely slow `simp`
protected def liftOn (q : Trunc α) (f : α → β) (c : ∀ a b : α, f a = f b) : β :=
lift f c q
Expand Down Expand Up @@ -560,7 +557,6 @@ theorem surjective_Quotient_mk'' : Function.Surjective (Quotient.mk'' : α → Q

/-- A version of `Quotient.liftOn` taking `{s : Setoid α}` as an implicit argument instead of an
instance argument. -/
-- Porting note: removed `@[elab_as_elim]` because it gave "unexpected eliminator resulting type"
-- porting note (#11083): removed `@[reducible]` because it caused extremely slow `simp`
protected def liftOn' (q : Quotient s₁) (f : α → φ) (h : ∀ a b, s₁ a b → f a = f b) :
φ :=
Expand All @@ -577,7 +573,6 @@ protected theorem liftOn'_mk'' (f : α → φ) (h) (x : α) :

/-- A version of `Quotient.liftOn₂` taking `{s₁ : Setoid α} {s₂ : Setoid β}` as implicit arguments
instead of instance arguments. -/
-- Porting note: removed `@[elab_as_elim]` because it gave "unexpected eliminator resulting type"
-- porting note (#11083): removed `@[reducible]` because it caused extremely slow `simp`
protected def liftOn₂' (q₁ : Quotient s₁) (q₂ : Quotient s₂) (f : α → β → γ)
(h : ∀ a₁ a₂ b₁ b₂, s₁ a₁ b₁ → s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ :=
Expand Down
1 change: 0 additions & 1 deletion Mathlib/FieldTheory/PerfectClosure.lean
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ theorem quot_mk_eq_mk (x : ℕ × K) : (Quot.mk (R K p) x : PerfectClosure K p)
variable {K p}

/-- Lift a function `ℕ × K → L` to a function on `PerfectClosure K p`. -/
-- Porting note: removed `@[elab_as_elim]` for "unexpected eliminator resulting type L"
def liftOn {L : Type*} (x : PerfectClosure K p) (f : ℕ × K → L)
(hf : ∀ x y, R K p x y → f x = f y) : L :=
Quot.liftOn x f hf
Expand Down
4 changes: 0 additions & 4 deletions Mathlib/GroupTheory/MonoidLocalization/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,6 @@ theorem ndrec_mk {p : Localization S → Sort u} (f : ∀ (a : M) (b : S), p (mk
/-- Non-dependent recursion principle for localizations: given elements `f a b : p`
for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`,
then `f` is defined on the whole `Localization S`. -/
-- Porting note: the attribute `elab_as_elim` fails with `unexpected eliminator resulting type p`
-- @[to_additive (attr := elab_as_elim)
@[to_additive
"Non-dependent recursion principle for `AddLocalization`s: given elements `f a b : p`
for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`,
Expand All @@ -293,8 +291,6 @@ theorem induction_on {p : Localization S → Prop} (x) (H : ∀ y : M × S, p (m
/-- Non-dependent recursion principle for localizations: given elements `f x y : p`
for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`,
then `f` is defined on the whole `Localization S`. -/
-- Porting note: the attribute `elab_as_elim` fails with `unexpected eliminator resulting type p`
-- @[to_additive (attr := elab_as_elim)
@[to_additive
"Non-dependent recursion principle for localizations: given elements `f x y : p`
for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`,
Expand Down
2 changes: 1 addition & 1 deletion Mathlib/LinearAlgebra/Determinant.lean
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ theorem det_toLin' (f : Matrix ι ι R) : LinearMap.det (Matrix.toLin' f) = Matr

/-- To show `P (LinearMap.det f)` it suffices to consider `P (Matrix.det (toMatrix _ _ f))` and
`P 1`. -/
-- @[elab_as_elim] -- Porting note: This attr can't be applied.
@[elab_as_elim]
theorem det_cases [DecidableEq M] {P : A → Prop} (f : M →ₗ[A] M)
(hb : ∀ (s : Finset M) (b : Basis s A M), P (Matrix.det (toMatrix b b f))) (h1 : P 1) :
P (LinearMap.det f) := by
Expand Down
2 changes: 0 additions & 2 deletions Mathlib/Logic/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@ noncomputable def decEq (α : Sort*) : DecidableEq α := by infer_instance

/-- Construct a function from a default value `H0`, and a function to use if there exists a value
satisfying the predicate. -/
-- @[elab_as_elim] -- FIXME
noncomputable def existsCases {α C : Sort*} {p : α → Prop} (H0 : C) (H : ∀ a, p a → C) : C :=
if h : ∃ a, p a then H (Classical.choose h) (Classical.choose_spec h) else H0

Expand Down Expand Up @@ -743,7 +742,6 @@ end Classical
/-- This function has the same type as `Exists.recOn`, and can be used to case on an equality,
but `Exists.recOn` can only eliminate into Prop, while this version eliminates into any universe
using the axiom of choice. -/
-- @[elab_as_elim] -- FIXME
noncomputable def Exists.classicalRecOn {α : Sort*} {p : α → Prop} (h : ∃ a, p a)
{C : Sort*} (H : ∀ a, p a → C) : C :=
H (Classical.choose h) (Classical.choose_spec h)
Expand Down
3 changes: 1 addition & 2 deletions Mathlib/Logic/Encodable/Lattice.lean
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ theorem iSup_decode₂ [CompleteLattice α] (f : β → α) :
theorem iUnion_decode₂ (f : β → Set α) : ⋃ (i : ℕ) (b ∈ decode₂ β i), f b = ⋃ b, f b :=
iSup_decode₂ f

/- Porting note: `@[elab_as_elim]` gives `unexpected eliminator resulting type`. -/
--@[elab_as_elim]
@[elab_as_elim]
theorem iUnion_decode₂_cases {f : β → Set α} {C : Set α → Prop} (H0 : C ∅) (H1 : ∀ b, C (f b)) {n} :
C (⋃ b ∈ decode₂ β n, f b) :=
match decode₂ β n with
Expand Down
3 changes: 1 addition & 2 deletions Mathlib/RingTheory/UniqueFactorizationDomain.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ theorem prime_pow_coprime_prod_of_coprime_insert [DecidableEq α] {s : Finset α
/-- If `P` holds for units and powers of primes,
and `P x ∧ P y` for coprime `x, y` implies `P (x * y)`,
then `P` holds on a product of powers of distinct primes. -/
-- @[elab_as_elim] Porting note: commented out
@[elab_as_elim]
theorem induction_on_prime_power {P : α → Prop} (s : Finset α) (i : α → ℕ)
(is_prime : ∀ p ∈ s, Prime p) (is_coprime : ∀ᵉ (p ∈ s) (q ∈ s), p ∣ q → p = q)
(h1 : ∀ {x}, IsUnit x → P x) (hpr : ∀ {p} (i : ℕ), Prime p → P (p ^ i))
Expand Down Expand Up @@ -1066,7 +1066,6 @@ theorem induction_on_coprime {P : α → Prop} (a : α) (h0 : P 0) (h1 : ∀ {x}

/-- If `f` maps `p ^ i` to `(f p) ^ i` for primes `p`, and `f`
is multiplicative on coprime elements, then `f` is multiplicative on all products of primes. -/
-- @[elab_as_elim] Porting note: commented out
theorem multiplicative_prime_power {f : α → β} (s : Finset α) (i j : α → ℕ)
(is_prime : ∀ p ∈ s, Prime p) (is_coprime : ∀ᵉ (p ∈ s) (q ∈ s), p ∣ q → p = q)
(h1 : ∀ {x y}, IsUnit y → f (x * y) = f x * f y)
Expand Down

0 comments on commit 98e84e2

Please sign in to comment.