lean4-htt/tests/elab/order.lean
Sebastian Ullrich db6aa9d8d3
feat: move instance-class check to declaration site (#12325)
This PR adds a warning to any `def` of class type that does not also
declare an appropriate reducibility.

The warning check runs after elaboration (checking the actual
reducibility status via `getReducibilityStatus`) rather than
syntactically checking modifiers before elaboration. This is necessary
to accommodate patterns like `@[to_additive (attr :=
implicit_reducible)]` in Mathlib, where the reducibility attribute is
applied during `.afterCompilation` by another attribute, and would be
missed by a purely syntactic check.

---------

Co-authored-by: Paul Reichert <6992158+datokrat@users.noreply.github.com>
Co-authored-by: Kim Morrison <kim@tqft.net>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 03:23:27 +00:00

136 lines
4.1 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Init.Data.Order.PackageFactories
set_option warn.classDefReducibility false
variable {α : Type u}
opaque X : Type := Unit
namespace X
#guard_msgs(error, drop warning) in
opaque instLE : LE X := sorry
attribute [scoped instance] instLE
#guard_msgs(error, drop warning) in
@[scoped instance] opaque instDecidableLE : DecidableLE X := sorry
#guard_msgs(error, drop warning) in
@[instance] opaque instTotal : Std.Total (α := X) (· ≤ ·) := sorry
#guard_msgs(error, drop warning) in
@[instance] opaque instAntisymm : Std.Antisymm (α := X) (· ≤ ·) := sorry
#guard_msgs(error, drop warning) in
@[instance] opaque instTrans : Trans (α := X) (· ≤ ·) (· ≤ ·) (· ≤ ·) := sorry
namespace LinearOrderPackage
scoped instance packageOfLE : Std.LinearOrderPackage X := .ofLE X
example : instLE = (inferInstanceAs (Std.PreorderPackage X)).toLE := rfl
example : Std.IsLinearOrder X := inferInstance
example : Std.LawfulOrderLT X := inferInstance
example : Std.LawfulOrderOrd X := inferInstance
example : Std.LawfulOrderMin X := inferInstance
example : Std.LawfulOrderMax X := inferInstance
example : Std.LawfulOrderLeftLeaningMin X := inferInstance
example : Std.LawfulOrderLeftLeaningMax X := inferInstance
end LinearOrderPackage
namespace LinearPreorderPackage
scoped instance packageOfLE : Std.LinearPreorderPackage X := .ofLE X
scoped instance instMin : Min X := .leftLeaningOfLE X
scoped instance instMax : Max X := .leftLeaningOfLE X
example : instLE = (inferInstanceAs (Std.LinearPreorderPackage X)).toLE := rfl
example : Std.IsLinearPreorder X := inferInstance
example : Std.LawfulOrderLT X := inferInstance
example : Std.LawfulOrderOrd X := inferInstance
example : Std.LawfulOrderMin X := inferInstance
example : Std.LawfulOrderMax X := inferInstance
example : Std.LawfulOrderLeftLeaningMin X := inferInstance
example : Std.LawfulOrderLeftLeaningMax X := inferInstance
end LinearPreorderPackage
end X
section
@[implicit_reducible] def packageWithoutSynthesizableInstances : Std.LinearOrderPackage X := .ofLE X {
le := X.instLE
decidableLE := X.instDecidableLE }
end
section
attribute [local instance] X.LinearOrderPackage.packageOfLE
@[implicit_reducible] def packageWithoutSynthesizableInstances' : Std.LinearOrderPackage X := .ofLE X {
le := X.instLE
decidableLE := X.instDecidableLE
}
end
/--
error: could not synthesize default value for field 'lt_iff' of 'Std.Packages.PreorderOfLEArgs' using tactics
---
error: Failed to automatically prove that the `LE` and `LT` instances are compatible. Please ensure that a `LawfulOrderLT` instance can be synthesized or manually provide the field `lt_iff`.
α : Type u
inst✝² : LE α
inst✝¹ : DecidableLE α
inst✝ : LT α
this✝¹ : LE α := inferInstance
this✝ : LT α := inferInstance
⊢ ∀ (a b : α), a < b ↔ a ≤ b ∧ ¬b ≤ a
-/
#guard_msgs in
@[implicit_reducible] def packageOfLEOfLT1 [LE α] [DecidableLE α] [LT α] : Std.PreorderPackage α := .ofLE α {
le_refl := sorry
le_trans := sorry }
@[implicit_reducible] def packageOfLEOfLT2 [LE α] [DecidableLE α] [LT α] (h : ∀ a b : α, a < b ↔ a ≤ b ∧ ¬ b ≤ a) :
Std.PreorderPackage α := .ofLE α {
lt_iff := h
le_refl := sorry
le_trans := sorry }
namespace OrdTests
section WithoutSynthesizableInstances
#guard_msgs(error, drop warning) in
opaque _root_.X.instOrd : Ord X := sorry
#guard_msgs(error, drop warning) in
opaque _root_.X.instTransOrd : haveI := X.instOrd; Std.TransOrd X := sorry
#guard_msgs(error, drop warning) in
opaque _root_.X.instLawfulEqOrd : haveI := X.instOrd; Std.LawfulEqOrd X := sorry
@[implicit_reducible] def packageWithoutSynthesizableInstances : Std.LinearOrderPackage X := .ofOrd X {
ord := X.instOrd
transOrd := X.instTransOrd
eq_of_compare := by
extract_lets
intro a b
letI := X.instOrd
exact X.instLawfulEqOrd.eq_of_compare }
end WithoutSynthesizableInstances
section WithSynthesizableInstances
attribute [scoped instance] X.instOrd X.instTransOrd X.instLawfulEqOrd
@[implicit_reducible] def packageWithSynthesizableInstances : Std.LinearOrderPackage X := .ofOrd X
end WithSynthesizableInstances
end OrdTests