source
stringlengths
17
118
lean4
stringlengths
0
335k
.lake/packages/mathlib/MathlibTest/TautoSet.lean
import Mathlib.Tactic.TautoSet variable {α : Type} {A B C D E : Set α} example (h : B ∪ C ⊆ A ∪ A) : B ⊆ A := by tauto_set example (h : B ∩ B ∩ C ⊇ A) : A ⊆ B := by tauto_set example (hABC : A ⊆ B ∪ C) (hCD : C ⊆ D): A ⊆ B ∪ D := by tauto_set example (h : A = Aᶜ) : B = ∅ := by tauto_set example (h : A = Aᶜ) : B = C := by tauto_set example (h : A ⊆ Aᶜ \ B) : A = ∅ := by tauto_set example (h1 : A ⊆ B \ C) : A ⊆ B := by tauto_set example (h : Set.univ ⊆ ((A ∪ B) ∩ C) ∩ ((Aᶜ ∩ Bᶜ) ∪ Cᶜ)) : D \ B ⊆ E ∩ Aᶜ := by tauto_set example (h : A ∩ B ⊆ C) (h2 : C ∩ D ⊆ E) : A ∩ B ∩ D ⊆ E := by tauto_set example (h : E = Aᶜᶜ ∩ Cᶜᶜᶜ ∩ D) : D ∩ (B ∪ Cᶜ) ∩ A = E ∪ (A ∩ Dᶜᶜ ∩ B)ᶜᶜ := by tauto_set example (h : E ⊇ Aᶜᶜ ∩ Cᶜᶜᶜ ∩ D) : D ∩ (B ∪ Cᶜ) ∩ A ⊆ E ∪ (A ∩ Dᶜᶜ ∩ B)ᶜᶜ := by tauto_set example (h1 : A = B) : A = B := by tauto_set example (h1 : A = B) (h2 : B ⊆ C): A ⊆ C := by tauto_set example (h1 : A ∩ B = Set.univ) : A = Set.univ := by tauto_set example (h1 : A ∪ B = ∅) : A = ∅ := by tauto_set example (h: Aᶜ ⊆ ∅) : A = Set.univ := by tauto_set example (h: Set.univ ⊆ Aᶜ) : A = ∅ := by tauto_set example : A ∩ ∅ = ∅ := by tauto_set example : A ∪ Set.univ = Set.univ := by tauto_set example : ∅ ⊆ A := by tauto_set example : A ⊆ Set.univ := by tauto_set example (hAB : A ⊆ B) (hBA: B ⊆ A) : A = B := by tauto_set example : A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) := by tauto_set example : A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) := by tauto_set example : A ∩ (B ∪ C) ⊆ (A ∩ B) ∪ (A ∩ C) := by tauto_set example : A ⊆ (A ∪ B) ∪ C := by tauto_set example : A ∩ B ⊆ A := by tauto_set example : A ⊆ A ∪ B := by tauto_set example (hBA : B ⊆ A) (hB : Set.univ ⊆ B): Set.univ = A := by tauto_set example (hAB : A ⊆ B) (hCD : C ⊆ D) : C \ B ⊆ D \ A := by tauto_set example (hAB : Disjoint A B) (hCA : C ⊆ A) : Disjoint C (B \ D) := by tauto_set example : Aᶜᶜᶜ = Aᶜ := by tauto_set example : Aᶜᶜ = A := by tauto_set example (hAB : A ⊆ B) (hBC : B ⊆ C) : A ⊆ C := by tauto_set example : (Aᶜ ∩ B ∩ Cᶜᶜ)ᶜᶜᶜᶜᶜ = Cᶜ ∪ Bᶜ ∪ ∅ ∪ A ∪ ∅ := by tauto_set example : D ∩ (B ∪ Cᶜ) ∩ A = (Aᶜᶜ ∩ Cᶜᶜᶜ ∩ D) ∪ (A ∩ Dᶜᶜ ∩ B)ᶜᶜ := by tauto_set example (hAB : A ⊆ B) (hBC : B ⊆ C) (hCD : C ⊆ D) (hDE : D = E) (hEA : E ⊆ A) : (Aᶜ ∩ B ∪ (C ∩ Bᶜ)ᶜ ∩ (Eᶜ ∪ A))ᶜ ∩ (B ∪ Eᶜᶜ)ᶜ = (Dᶜ ∩ C ∪ (B ∩ Aᶜ)ᶜ ∩ (Eᶜ ∪ E))ᶜ ∩ (D ∪ Cᶜᶜ)ᶜ := by tauto_set /- Examples from the Matroid Decomposition Theorem Verification, see https://github.com/Ivan-Sergeyev/seymour, and in particular https://github.com/Ivan-Sergeyev/seymour/blob/d8fcfa23336efe50b09fa0939e8a4ec3a5601ae9/Seymour/ForMathlib/SetTheory.lean -/ -- setminus_inter_union_eq_union example : A \ (A ∩ B) ∪ B = A ∪ B := by tauto_set -- sub_parts_eq example (hA : A ⊆ B ∪ C) : (A ∩ B) ∪ (A ∩ C) = A := by tauto_set -- elem_notin_set_minus_singleton example (a : α) : a ∉ A \ {a} := by tauto_set -- sub_union_diff_sub_union example (hA : A ⊆ B \ C) : A ⊆ B := by tauto_set -- singleton_inter_subset_left example (hAB : A ∩ B = {a}) : {a} ⊆ A := by tauto_set -- singleton_inter_subset_right example (hAB : A ∩ B = {a}) : {a} ⊆ B := by tauto_set -- diff_subset_parent example (hAB : A ⊆ C) : A \ B ⊆ C := by tauto_set -- inter_subset_parent_left example (hAC : A ⊆ C) : A ∩ B ⊆ C := by tauto_set -- inter_subset_parent_right example (hBC : B ⊆ C) : A ∩ B ⊆ C := by tauto_set -- inter_subset_union example : A ∩ B ⊆ A ∪ B := by tauto_set -- subset_diff_empty_eq example (hAB : A ⊆ B) (hBA : B \ A = ∅) : A = B := by tauto_set -- Disjoint.ni_of_in example (hAB : Disjoint A B) (ha : a ∈ A) : a ∉ B := by tauto_set -- disjoint_of_singleton_inter_left_wo example (hAB : A ∩ B = {a}) : Disjoint (A \ {a}) B := by tauto_set -- disjoint_of_singleton_inter_right_wo example (hAB : A ∩ B = {a}) : Disjoint A (B \ {a}) := by tauto_set -- disjoint_of_singleton_inter_both_wo example (hAB : A ∩ B = {a}) : Disjoint (A \ {a}) (B \ {a}) := by tauto_set -- union_subset_union_iff example (hAC : Disjoint A C) (hBC : Disjoint B C) : A ∪ C ⊆ B ∪ C ↔ A ⊆ B := by constructor <;> (intro; tauto_set) -- symmDiff_eq_alt example : symmDiff A B = (A ∪ B) \ (A ∩ B) := by tauto_set -- symmDiff_disjoint_inter example : Disjoint (symmDiff A B) (A ∩ B) := by tauto_set -- symmDiff_empty_eq example : symmDiff A ∅ = A := by tauto_set -- empty_symmDiff_eq example : symmDiff ∅ A = A := by tauto_set -- symmDiff_subset_ground_right example (hC : symmDiff A B ⊆ C) (hA : A ⊆ C) : B ⊆ C := by tauto_set -- symmDiff_subset_ground_left example (hC : symmDiff A B ⊆ C) (hB : B ⊆ C) : A ⊆ C := by tauto_set
.lake/packages/mathlib/MathlibTest/enat_to_nat.lean
import Mathlib.Tactic.ENatToNat example (a b : ENat) (h : a = b) : a - b = b - a := by enat_to_nat omega example (a b : ENat) (h : a ≤ b) : a - b < b + 1 := by enat_to_nat omega example (a b : ENat) (h : a ≤ b) : a - 2 * b ≤ b + 1 := by enat_to_nat omega example (a b c : ENat) (hab : a ≥ b) (hbc : b ≥ c) : a ≥ c := by enat_to_nat omega example (a b : ENat) (h : a = b) : a - b = b - a := by -- to test if the tactic works with inaccessible names let a : ℤ := 42 let b : ℤ := 32 enat_to_nat omega
.lake/packages/mathlib/MathlibTest/says.lean
import Mathlib.Tactic.Says import Aesop -- removing changes to the `simp` set after this test was created attribute [-simp] Nat.add_left_cancel_iff Nat.add_right_cancel_iff set_option autoImplicit true /-- info: Try this: [apply] (show_term exact 37) says exact 37 -/ #guard_msgs in example : Nat := by (show_term exact 37) says -- Note that this implicitly tests that we have turned off `linter.unreachableTactic`. example : Nat := by (show_term exact 37) says exact 37 /-- info: Try this: [apply] simp? says simp only [List.length_append] -/ #guard_msgs in example (x y : List α) : (x ++ y).length = x.length + y.length := by simp? says example (x y : List α) : (x ++ y).length = x.length + y.length := by simp? says simp only [List.length_append] /-- error: Tactic `have := 0` did not produce a 'Try this:' suggestion. -/ #guard_msgs in example : true := by have := 0 says /-- error: Tactic `(run_tac do Lean.logInfo "hi!")` did not produce a 'Try this:' suggestion. -/ #guard_msgs in example : true := by (run_tac do Lean.logInfo "hi!") says -- Check that `says` does not reverify the right-hand-side. set_option says.no_verify_in_CI true in example (x y : List α) : (x ++ y).length = x.length + y.length := by simp? says skip simp -- Check that with `says.verify` `says` will reverify that the left-hand-side constructs -- the right-hand-side. set_option says.verify true in /-- error: Tactic `simp?` produced `simp only [List.length_append]`, but was expecting it to produce `simp only []`! You can reproduce this error locally using `set_option says.verify true`. -/ #guard_msgs in example (x y : List α) : (x ++ y).length = x.length + y.length := by simp? says simp only [] set_option linter.unreachableTactic false set_option linter.unusedTactic false in -- Now we check that `says` does not consume following tactics unless they are indented. /-- error: Tactic `simp` did not produce a 'Try this:' suggestion. -/ #guard_msgs in example : True := by simp says trivial set_option says.verify true in example (x y : List α) : (x ++ y).length = x.length + y.length := by simp? says simp only [List.length_append] -- This is a comment to test that `says` ignores following comments. set_option says.no_verify_in_CI true in example : True := by simp says trivial set_option says.verify true in /-- error: Tactic `simp` did not produce a 'Try this:' suggestion. -/ #guard_msgs in example : True := by simp says trivial -- Check that if the CI environment variable is set, we reverify all `says` statements. example : True := by fail_if_success run_tac do guard (← IO.getEnv "CI").isSome simp says trivial trivial -- Check that verification works even with multi-line suggestions, as produced by aesop def P : Prop := True def Q : Prop := True @[simp] def very_long_lemma_name_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : Q → P := fun _ => trivial @[simp] def very_long_lemma_name_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb : Q := trivial /-- info: Try this: [apply] aesop? says simp_all only [very_long_lemma_name_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, very_long_lemma_name_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] -/ #guard_msgs in example : P := by aesop? says
.lake/packages/mathlib/MathlibTest/ForbiddenModuleNames.lean
import Mathlib.Tactic.Linter.TextBased /-! # Unit tests for the module name compatibility checks in the text-based linter -/ open Lean.Linter Mathlib.Linter.TextBased /-- Some unit tests for `modulesOSForbidden` -/ def testModulesOSForbidden : IO Unit := do -- Explicitly enable the linter, although it is enabled by default. let opts : LinterOptions := { toOptions := linter.modulesUpperCamelCase.set {} true linterSets := {} } assert!((← modulesOSForbidden opts #[]) == 0) assert!((← modulesOSForbidden opts #[`Mathlib.Aux.Foo, `Mathlib.Algebra.Con]) == 2) assert!((← modulesOSForbidden opts #[`Mathlib.Foo.Bar, `Aux.Algebra.Con.Bar]) == 1) assert!((← modulesOSForbidden opts #[`Com1.prn.Aux, `LPT.Aux]) == 2) assert!((← modulesOSForbidden opts #[`Mathlib.Foo.«Foo*»]) == 1) assert!((← modulesOSForbidden opts #[`Mathlib.«B>ar<»]) == 1) assert!((← modulesOSForbidden opts #[`Mathlib.«Bar!»]) == 1) assert!((← modulesOSForbidden opts #[`Mathlib.«Qu<x!!»]) == 1) assert!((← modulesOSForbidden opts #[`Mathlib.«Ba dname»]) == 1) assert!((← modulesOSForbidden opts #[`Mathlib.«Bar dname»]) == 1) assert!((← modulesOSForbidden opts #[`Mathlib.«Bar dname»]) == 1) /-- info: error: module name 'Mathlib.Aux.Foo' contains component '[Aux]', which is forbidden in Windows filenames. error: module name 'Mathlib.Algebra.Con' contains component '[Con]', which is forbidden in Windows filenames. error: module name 'Aux.Algebra.Con.Bar' contains components '[Aux, Con]', which are forbidden in Windows filenames. error: module name 'Com1.prn.Aux' contains components '[Com1, prn, Aux]', which are forbidden in Windows filenames. error: module name 'LPT.Aux' contains component '[Aux]', which is forbidden in Windows filenames. error: module name 'Mathlib.Foo.«Foo*»' contains character '[*]', which is forbidden in Windows filenames. error: module name 'Mathlib.«B>ar<»' contains characters '[>, <]', which are forbidden in Windows filenames. error: module name 'Mathlib.Bar!' contains forbidden character '!' error: module name 'Mathlib.«Qu<x!!»' contains forbidden character '!' error: module name 'Mathlib.«Qu<x!!»' contains character '[<]', which is forbidden in Windows filenames. error: module name 'Mathlib.«Ba dname»' contains a whitespace character error: module name 'Mathlib.«Bar dname»' contains a whitespace character error: module name 'Mathlib.«Bar dname»' contains a whitespace character -/ #guard_msgs in #eval testModulesOSForbidden
.lake/packages/mathlib/MathlibTest/nontriviality.lean
import Mathlib.Tactic.Nontriviality import Mathlib.Algebra.Order.Ring.Defs import Mathlib.Algebra.Ring.Nat private axiom test_sorry : ∀ {α}, α /-! ### Test `nontriviality` with inequality hypotheses -/ set_option autoImplicit true example {R : Type} [Ring R] [PartialOrder R] [IsOrderedRing R] {a : R} (h : 0 < a) : 0 < a := by nontriviality rename_i inst; guard_hyp inst : Nontrivial R assumption /-! ### Test `nontriviality` with equality or non-strict inequality goals -/ example {R : Type} [CommRing R] {r s : R} : r * s = s * r := by nontriviality rename_i inst; guard_hyp inst : Nontrivial R apply mul_comm /-! ### Test deducing `nontriviality` by instance search -/ example {R : Type} [Ring R] [PartialOrder R] [IsOrderedRing R] : 0 ≤ (1 : R) := by nontriviality R rename_i inst; guard_hyp inst : Nontrivial R exact zero_le_one example {R : Type} [Ring R] [PartialOrder R] [IsOrderedRing R] : 0 ≤ (1 : R) := by nontriviality ℕ rename_i inst; guard_hyp inst : Nontrivial ℕ exact zero_le_one example {R : Type} [Ring R] [PartialOrder R] [IsOrderedRing R] : 0 ≤ (2 : R) := by fail_if_success nontriviality PUnit exact zero_le_two example {R : Type} [Ring R] [PartialOrder R] [IsOrderedRing R] {a : R} (_ : 0 < a) : 2 ∣ 4 := by nontriviality R rename_i inst; guard_hyp inst : Nontrivial R decide /-! Test using `@[nontriviality]` lemmas in `nontriviality` and custom `simp` lemmas -/ def EmptyOrUniv {α : Type _} (s : Set α) : Prop := s = ∅ ∨ s = Set.univ theorem Subsingleton.set_empty_or_univ {α} [Subsingleton α] (s : Set α) : s = ∅ ∨ s = Set.univ := test_sorry theorem Subsingleton.set_empty_or_univ' {α} [Subsingleton α] (s : Set α) : EmptyOrUniv s := Subsingleton.set_empty_or_univ s theorem Set.empty_union (a : Set α) : ∅ ∪ a = a := test_sorry example {α : Type _} (s : Set α) (hs : s = ∅ ∪ Set.univ) : EmptyOrUniv s := by fail_if_success nontriviality α rw [Set.empty_union] at hs exact Or.inr hs section attribute [local nontriviality] Subsingleton.set_empty_or_univ example {α : Type _} (s : Set α) (hs : s = ∅ ∪ Set.univ) : EmptyOrUniv s := by fail_if_success nontriviality α nontriviality α using Subsingleton.set_empty_or_univ' rw [Set.empty_union] at hs exact Or.inr hs end attribute [local nontriviality] Subsingleton.set_empty_or_univ' example {α : Type _} (s : Set α) (hs : s = ∅ ∪ Set.univ) : EmptyOrUniv s := by nontriviality α rw [Set.empty_union] at hs exact Or.inr hs /-! Test with nonatomic type argument -/ example (α : ℕ → Type) (a b : α 0) (h : a = b) : a = b := by nontriviality α 0 using Nat.zero_lt_one rename_i inst; guard_hyp inst : Nontrivial (α 0) exact h class Foo (α : Type) : Prop instance : Foo α := {} example (α : Type) : Foo α := by nontriviality α; infer_instance -- simulate the type of MvPolynomial def R : Type u → Type v → Sort (max (u+1) (v+1)) := test_sorry noncomputable instance : CommRing (R c d) := test_sorry example (p : R PUnit.{u+1} PUnit.{v+1}) : p = p := by nontriviality exact test_sorry
.lake/packages/mathlib/MathlibTest/finset_repr.lean
import Mathlib.Data.Finset.Sort run_cmd Lean.Elab.Command.liftTermElabM do unsafe guard <| (repr (0 : Multiset (List ℕ)) |>.pretty 15) = "0" unsafe guard <| (repr ({[1, 2, 3], [4, 5, 6]} : Multiset (List ℕ)) |>.pretty 15) = "{[1, 2, 3],\n [4, 5, 6]}" unsafe guard <| (repr (∅ : Finset (List ℕ)) |>.pretty 15) = "∅" unsafe guard <| (repr ({[1, 2, 3], [4, 5, 6]} : Finset (List ℕ)) |>.pretty 15) = "{[1, 2, 3],\n [4, 5, 6]}"
.lake/packages/mathlib/MathlibTest/positivity.lean
import Mathlib.Tactic.Positivity import Mathlib.Analysis.Complex.Trigonometric import Mathlib.Data.Real.Sqrt import Mathlib.Data.ENNReal.Basic import Mathlib.Analysis.Normed.Group.Basic import Mathlib.Analysis.SpecialFunctions.Pow.Real import Mathlib.Analysis.SpecialFunctions.Log.Basic import Mathlib.Analysis.SpecialFunctions.Trigonometric.Arctan import Mathlib.MeasureTheory.Integral.Bochner.Basic import Mathlib.NumberTheory.ArithmeticFunction import Mathlib.Topology.Algebra.InfiniteSum.Order /-! # Tests for the `positivity` tactic This tactic proves goals of the form `0 ≤ a` and `0 < a`. -/ set_option autoImplicit true open Finset Function Nat NNReal ENNReal variable {ι α β E : Type*} /- ## Numeric goals -/ example : 0 ≤ 0 := by positivity example : 0 ≤ 3 := by positivity example : 0 < 3 := by positivity example : (0 : EReal) < 2 := by positivity example : 0 < (2 : EReal) := by positivity example : (0 : EReal) < 2 := by positivity example : (0 : ℝ≥0∞) ≤ 1 := by positivity example : (0 : ℝ≥0∞) ≤ 0 := by positivity example : (0 : EReal) ≤ 0 := by positivity example : 0 ≤ (2 : EReal) := by positivity /- ## Goals working directly from a hypothesis -/ section FromHypothesis -- set_option trace.Meta.debug true -- sudo set_option trace.Tactic.positivity true example {a : ℤ} (ha : 0 < a) : 0 < a := by positivity example {a : ℤ} (ha : 0 < a) : 0 ≤ a := by positivity example {a : ℤ} (ha : 0 < a) : a ≠ 0 := by positivity example {a : ℤ} (ha : 0 ≤ a) : 0 ≤ a := by positivity example {a : ℤ} (ha : a ≠ 0) : a ≠ 0 := by positivity example {a : ℤ} (ha : a = 0) : 0 ≤ a := by positivity section variable [Zero α] [PartialOrder α] {a : α} example (ha : 0 < a) : 0 < a := by positivity example (ha : 0 < a) : 0 ≤ a := by positivity example (ha : 0 < a) : a ≠ 0 := by positivity example (ha : 0 ≤ a) : 0 ≤ a := by positivity example (ha : a ≠ 0) : a ≠ 0 := by positivity example (ha : a = 0) : 0 ≤ a := by positivity end /- ### Reversing hypotheses -/ example {a : ℤ} (ha : a > 0) : 0 < a := by positivity example {a : ℤ} (ha : a > 0) : 0 ≤ a := by positivity example {a : ℤ} (ha : a > 0) : a ≥ 0 := by positivity example {a : ℤ} (ha : a > 0) : a ≠ 0 := by positivity example {a : ℤ} (ha : a ≥ 0) : 0 ≤ a := by positivity example {a : ℤ} (ha : 0 ≠ a) : a ≠ 0 := by positivity example {a : ℤ} (ha : 0 < a) : a > 0 := by positivity example {a : ℤ} (ha : 0 < a) : a ≥ 0 := by positivity example {a : ℤ} (ha : 0 < a) : 0 ≠ a := by positivity example {a : ℤ} (ha : 0 ≤ a) : a ≥ 0 := by positivity example {a : ℤ} (ha : a ≠ 0) : 0 ≠ a := by positivity example {a : ℤ} (ha : a = 0) : a ≥ 0 := by positivity example {a : ℤ} (ha : 0 = a) : 0 ≤ a := by positivity example {a : ℤ} (ha : 0 = a) : a ≥ 0 := by positivity end FromHypothesis /- ### Calling `norm_num` -/ example {a : ℤ} (ha : 3 = a) : 0 ≤ a := by positivity example {a : ℤ} (ha : 3 = a) : a ≠ 0 := by positivity example {a : ℤ} (ha : 3 = a) : 0 < a := by positivity example {a : ℤ} (ha : a = -1) : a ≠ 0 := by positivity example {a : ℤ} (ha : 3 ≤ a) : 0 ≤ a := by positivity example {a : ℤ} (ha : 3 ≤ a) : a ≠ 0 := by positivity example {a : ℤ} (ha : 3 ≤ a) : 0 < a := by positivity example {a : ℤ} (ha : 3 < a) : 0 ≤ a := by positivity example {a : ℤ} (ha : 3 < a) : a ≠ 0 := by positivity example {a : ℤ} (ha : 3 < a) : 0 < a := by positivity example {a b : ℤ} (h : 0 ≤ a + b) : 0 ≤ a + b := by positivity example {a : ℤ} (hlt : 0 ≤ a) (hne : a ≠ 0) : 0 < a := by positivity example {a b c d : ℤ} (ha : c < a) (hb : d < b) : 0 < (a - c) * (b - d) := by positivity [sub_pos_of_lt ha, sub_pos_of_lt hb] section variable [Field α] [LinearOrder α] [IsStrictOrderedRing α] example : (1/4 - 2/3 : ℚ) ≠ 0 := by positivity example : (1/4 - 2/3 : α) ≠ 0 := by positivity end /- ### `ArithmeticFunction.sigma` and `ArithmeticFunction.zeta` -/ example (a b : ℕ) (hb : 0 < b) : 0 < ArithmeticFunction.sigma a b := by positivity example (a : ℕ) (ha : 0 < a) : 0 < ArithmeticFunction.zeta a := by positivity /- ## Test for meta-variable instantiation Reported on https://leanprover.zulipchat.com/#narrow/stream/239415-metaprogramming-.2F-tactics/topic/New.20tactic.3A.20.60positivity.60/near/300639970 -/ example : 0 ≤ 0 := by apply le_trans _ (le_refl _); positivity /- ## Tests of the @[positivity] plugin tactics (addition, multiplication, division) -/ -- example [Nonempty ι] [Zero α] {a : α} (ha : a ≠ 0) : const ι a ≠ 0 := by positivity -- example [Zero α] [PartialOrder α] {a : α} (ha : 0 < a) : 0 ≤ const ι a := by positivity -- example [Zero α] [PartialOrder α] {a : α} (ha : 0 ≤ a) : 0 ≤ const ι a := by positivity -- example [Nonempty ι] [Zero α] [PartialOrder α] {a : α} (ha : 0 < a) : 0 < const ι a := by -- positivity section ite variable {p : Prop} [Decidable p] {a b : ℤ} example (ha : 0 < a) (hb : 0 < b) : 0 < ite p a b := by positivity example (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ ite p a b := by positivity example (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ ite p a b := by positivity example (ha : 0 < a) (hb : b ≠ 0) : ite p a b ≠ 0 := by positivity example (ha : a ≠ 0) (hb : 0 < b) : ite p a b ≠ 0 := by positivity example (ha : a ≠ 0) (hb : b ≠ 0) : ite p a b ≠ 0 := by positivity end ite section MinMax example {a b : ℚ} (ha : 0 < a) (hb : 0 < b) : 0 < min a b := by positivity example {a b : ℚ} (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ min a b := by positivity example {a b : ℚ} (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ min a b := by positivity example {a b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ min a b := by positivity example {a b : ℚ} (ha : 0 < a) (hb : b ≠ 0) : min a b ≠ 0 := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : 0 < b) : min a b ≠ 0 := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : b ≠ 0) : min a b ≠ 0 := by positivity example {a b : ℚ} (ha : 0 < a) : 0 < max a b := by positivity example {a b : ℚ} (hb : 0 < b) : 0 < max a b := by positivity example {a b : ℚ} (ha : 0 ≤ a) : 0 ≤ max a b := by positivity example {a b : ℚ} (hb : 0 ≤ b) : 0 ≤ max a b := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : b ≠ 0) : max a b ≠ 0 := by positivity example : 0 ≤ max 3 4 := by positivity example : 0 ≤ max (0 : ℤ) (-3) := by positivity example : 0 ≤ max (-3 : ℤ) 5 := by positivity end MinMax example {a b : ℚ} (ha : 0 < a) (hb : 0 < b) : 0 < a * b := by positivity example {a b : ℚ} (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a * b := by positivity example {a b : ℚ} (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a * b := by positivity example {a b : ℚ} (ha : 0 < a) (hb : b ≠ 0) : a * b ≠ 0 := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : 0 < b) : a * b ≠ 0 := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := by positivity example {a b : ℚ} (ha : 0 < a) (hb : 0 < b) : 0 < a / b := by positivity example {a b : ℚ} (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a / b := by positivity example {a b : ℚ} (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a / b := by positivity example {a b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a / b := by positivity example {a b : ℚ} (ha : 0 < a) (hb : b ≠ 0) : a / b ≠ 0 := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : 0 < b) : a / b ≠ 0 := by positivity example {a b : ℚ} (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := by positivity example {a b : ℤ} (ha : 0 < a) (hb : 0 < b) : 0 ≤ a / b := by positivity example {a : ℤ} (ha : 0 < a) : 0 < a / a := by positivity example {a b : ℤ} (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a / b := by positivity example {a b : ℤ} (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a / b := by positivity example {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a / b := by positivity example {a : ℚ} (ha : 0 < a) : 0 < a⁻¹ := by positivity example {a : ℚ} (ha : 0 ≤ a) : 0 ≤ a⁻¹ := by positivity example {a : ℚ} (ha : a ≠ 0) : a⁻¹ ≠ 0 := by positivity example {a : ℚ} (n : ℕ) (ha : 0 < a) : 0 < a ^ n := by positivity example {a : ℚ} (n : ℕ) (ha : 0 ≤ a) : 0 ≤ a ^ n := by positivity example {a : ℚ} (n : ℕ) (ha : a ≠ 0) : a ^ n ≠ 0 := by positivity example {a : ℚ} : 0 ≤ a ^ 18 := by positivity example {a : ℚ} (ha : a ≠ 0) : 0 < a ^ 18 := by positivity example {a : ℚ} (ha : 0 < a) : 0 < |a| := by positivity example {a : ℚ} (ha : a ≠ 0) : 0 < |a| := by positivity example (a : ℚ) : 0 ≤ |a| := by positivity example {a : ℤ} {b : ℚ} (ha : 0 < a) (hb : 0 < b) : 0 < a • b := by positivity example {a : ℤ} {b : ℚ} (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a • b := by positivity example {a : ℤ} {b : ℚ} (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a • b := by positivity example {a : ℤ} {b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a • b := by positivity example {a : ℤ} {b : ℚ} (ha : 0 < a) (hb : b ≠ 0) : a • b ≠ 0 := by positivity example {a : ℤ} {b : ℚ} (ha : a ≠ 0) (hb : 0 < b) : a • b ≠ 0 := by positivity example {a : ℤ} {b : ℚ} (ha : a ≠ 0) (hb : b ≠ 0) : a • b ≠ 0 := by positivity -- Test that the positivity extension for `a • b` can handle universe polymorphism. example {R M : Type*} [Semiring R] [PartialOrder R] [IsOrderedRing R] [Semiring M] [PartialOrder M] [IsStrictOrderedRing M] [SMulWithZero R M] [PosSMulStrictMono R M] {a : R} {b : M} (ha : 0 < a) (hb : 0 < b) : 0 < a • b := by positivity example {a : ℤ} (ha : 3 < a) : 0 ≤ a + a := by positivity example {a b : ℤ} (ha : 3 < a) (hb : 4 ≤ b) : 0 ≤ 3 + a + b + b + 14 := by positivity example {H : Type*} [AddCommGroup H] [LinearOrder H] [IsOrderedAddMonoid H] {a b : H} (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a + a + b := by positivity example {a : ℤ} (ha : 3 < a) : 0 < a + a := by positivity example {a b : ℚ} (ha : 3 < a) (hb : 4 ≤ b) : 0 < 3 + a * b / 7 + b + 7 + 14 := by positivity example {a b : ℤ} (ha : 3 < a) (hb : 4 ≤ b) : 0 < 3 + a * b / 7 + b + 7 + 14 := by positivity section variable {q : ℚ} example (hq : q ≠ 0) : q.num ≠ 0 := by positivity example : 0 < q.den := by positivity example (hq : 0 < q) : 0 < q := by positivity example (hq : 0 ≤ q) : 0 ≤ q.num := by positivity end example (a b : ℕ) (ha : a ≠ 0) : 0 < a.gcd b := by positivity example (a b : ℤ) (ha : a ≠ 0) : 0 < a.gcd b := by positivity example (a b : ℕ) (hb : b ≠ 0) : 0 < a.gcd b := by positivity example (a b : ℤ) (hb : b ≠ 0) : 0 < a.gcd b := by positivity example (a b : ℕ) (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a.lcm b := by positivity example (a b : ℤ) (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a.lcm b := by positivity example (a : ℕ) (ha : a ≠ 0) : 0 < a.sqrt := by positivity example (a : ℕ) (ha : a ≠ 0) : 0 < a.totient := by positivity section ENNReal variable {a b : ℝ≥0∞} example : (0 : ℝ≥0∞) < 1 := by positivity example : (0 : ℝ≥0∞) < 2 := by positivity example : 0 ≤ a := by positivity example (ha : a ≠ 0) : 0 < a := by positivity example : 0 ≤ a + b := by positivity example (ha : a ≠ 0) : 0 < a + b := by positivity example : 0 < a + 5 := by positivity example : 0 < 2 * a + 3 := by positivity example (ha : 0 < a) : 0 < a + b := by positivity example : 0 ≤ a * b := by positivity example (ha : a ≠ 0) : 0 < 2 * a := by positivity example (ha : a ≠ 0) : 0 < a * 37 := by positivity example (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a * b := by positivity example (ha : a ≠ 0) : 0 ≤ a * b := by positivity end ENNReal section EReal variable {a b : EReal} example (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b := by positivity example (ha : 0 < a) (hb : 0 ≤ b) : 0 < a + b := by positivity example (ha : 0 ≤ a) (hb : 0 < b) : 0 < a + b := by positivity example (ha : 0 < a) (hb : 0 < b) : 0 < a + b := by positivity example (ha : 0 ≤ a) : 0 ≤ 2 + a := by positivity example (ha : 0 < a) : 0 < a + 2 := by positivity example (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := by positivity example (ha : 0 < a) (hb : 0 ≤ b) : 0 ≤ a * b := by positivity example (ha : 0 ≤ a) (hb : 0 < b) : 0 ≤ a * b := by positivity example (ha : 0 < a) (hb : 0 < b) : 0 < a * b := by positivity example (ha : 0 ≤ a) : 0 ≤ 2 * a := by positivity example (ha : 0 < a) : 0 < a * 2 := by positivity example : 0 < (5 : EReal) := by positivity example (_ha : 0 ≤ a) : 0 < a + 5 := by positivity example (_ha : 0 ≤ a) : 0 < 2 * a + 3 := by positivity end EReal /-! ### Powers -/ section Powers example [Semiring α] [PartialOrder α] [IsOrderedRing α] [Nontrivial α] (a : α) : 0 < a ^ 0 := by positivity example [Ring α] [LinearOrder α] [IsStrictOrderedRing α] (a : α) : 0 ≤ a ^ 18 := by positivity example [Semiring α] [PartialOrder α] [IsOrderedRing α] {a : α} {n : ℕ} (ha : 0 ≤ a) : 0 ≤ a ^ n := by positivity example [Semiring α] [PartialOrder α] [IsStrictOrderedRing α] {a : α} {n : ℕ} (ha : 0 < a) : 0 < a ^ n := by positivity example [Semifield α] [LinearOrder α] [IsStrictOrderedRing α] (a : α) : 0 < a ^ (0 : ℤ) := by positivity example [Field α] [LinearOrder α] [IsStrictOrderedRing α] (a : α) : 0 ≤ a ^ (18 : ℤ) := by positivity example [Field α] [LinearOrder α] [IsStrictOrderedRing α] (a : α) : 0 ≤ a ^ (-34 : ℤ) := by positivity example [Semifield α] [LinearOrder α] [IsStrictOrderedRing α] {a : α} {n : ℤ} (ha : 0 ≤ a) : 0 ≤ a ^ n := by positivity example [Semifield α] [LinearOrder α] [IsStrictOrderedRing α] {a : α} {n : ℤ} (ha : 0 < a) : 0 < a ^ n := by positivity -- example {a b : Cardinal.{u}} (ha : 0 < a) : 0 < a ^ b := by positivity -- example {a b : Ordinal.{u}} (ha : 0 < a) : 0 < a ^ b := by positivity example {a b : ℝ} (ha : 0 ≤ a) : 0 ≤ a ^ b := by positivity example {a b : ℝ} (ha : 0 < a) : 0 < a ^ b := by positivity example {a : ℝ≥0} {b : ℝ} : 0 ≤ a ^ b := by positivity example {a : ℝ≥0} {b : ℝ} (ha : 0 < a) : 0 < a ^ b := by positivity example {a : ℝ≥0∞} {b : ℝ} : 0 ≤ a ^ b := by positivity example {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hb : 0 ≤ b) : 0 < a ^ b := by positivity example {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hb : 0 < b) : 0 < a ^ b := by positivity example {a : ℝ≥0∞} : 0 < a ^ 0 := by positivity example {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hat : a ≠ ⊤) : 0 < a ^ b := by positivity example {a : ℝ} : 0 < a ^ 0 := by positivity example {a : ℝ≥0∞} {b : ℝ} (ha : 0 < a) (hat : a ≠ ⊤) : 0 < a ^ b := by positivity [] example {a b c d : ℝ} (hab : 0 < a * b) (hb : 0 ≤ b) (hcd : c < d) : 0 < a ^ c + 1 / (d - c) := by positivity [sub_pos_of_lt hcd, pos_of_mul_pos_left hab hb] example {a : ℤ} (ha : 3 < a) : 0 ≤ a ^ 2 + a := by positivity example {a : ℤ} (ha : 3 < a) : 0 ≤ a ^ 3 + a := by positivity example {a : ℤ} (ha : 3 < a) : 0 < a ^ 2 + a := by positivity example {a b : ℤ} (ha : 3 < a) (hb : b ≥ 4) : 0 ≤ 3 * a ^ 2 * b + b * 7 + 14 := by positivity example {a b : ℤ} (ha : 3 < a) (hb : b ≥ 4) : 0 < 3 * a ^ 2 * b + b * 7 + 14 := by positivity example {a b : ℤ} (ha : 3 < a) : 0 ≤ min a (b ^ 2) := by positivity example {b : ℤ} : 0 ≤ max (-3) (b ^ 2) := by positivity example {b : ℤ} : 0 ≤ max (b ^ 2) 0 := by positivity end Powers section FloorCeil example {a : ℝ} (ha : 0 < a) : 0 ≤ ⌊a⌋ := by positivity example {a : ℝ} (ha : 0 ≤ a) : 0 ≤ ⌊a⌋ := by positivity example {a : ℝ} (ha : 0 < a) : 0 < ⌈a⌉₊ := by positivity example {a : ℝ} (ha : 0 < a) : 0 < ⌈a⌉ := by positivity example {a : ℝ} (ha : 0 ≤ a) : 0 ≤ ⌈a⌉ := by positivity end FloorCeil section Abs example {a : ℤ} : 0 ≤ |a| := by positivity example {a : ℤ} : 0 < |a| + 3 := by positivity example {n : ℤ} (hn : 0 < n) : 0 < n.natAbs := by positivity example {n : ℤ} (hn : n ≠ 0) : 0 < n.natAbs := by positivity example {n : ℤ} : 0 ≤ n.natAbs := by positivity example {a : ℤ} (ha : 1 < a) : 0 < |(3:ℤ) + a| := by positivity example (a : ℤ) : 0 ≤ a⁺ := by positivity example (a : ℤ) (ha : 0 < a) : 0 < a⁺ := by positivity example (a : ℤ) : 0 ≤ a⁻ := by positivity end Abs -- -- test that the tactic can ignore arithmetic operations whose associated extension tactic -- -- requires more typeclass assumptions than are available example {R : Type*} [Zero R] [Div R] [LinearOrder R] {a b c : R} (_h1 : 0 < a) (_h2 : 0 < b) (h3 : 0 < c) : 0 < max (a / b) c := by positivity example [Semiring α] [PartialOrder α] [IsOrderedRing α] [AddCommMonoid β] [PartialOrder β] [IsOrderedAddMonoid β] [SMulWithZero α β] [PosSMulMono α β] {a : α} (ha : 0 < a) {b : β} (hb : 0 < b) : 0 ≤ a • b := by positivity example (n : ℕ) : 0 < n.succ := by positivity example (n : ℕ+) : 0 < (↑n : ℕ) := by positivity example (n : ℕ) : 0 < n ! := by positivity example (n k : ℕ) : 0 < (n + 1).ascFactorial k := by positivity example {α : Type*} (s : Finset α) (hs : s.Nonempty) : 0 < #s := by positivity example {α : Type*} [Fintype α] [Nonempty α] : 0 < Fintype.card α := by positivity section Norms example [SeminormedGroup E] {a : E} (_ha : a ≠ 1) : 0 ≤ ‖a‖ := by positivity example [NormedGroup E] {a : E} : 0 ≤ ‖a‖ := by positivity example [NormedGroup E] {a : E} (ha : a ≠ 1) : 0 < ‖a‖ := by positivity example [SeminormedAddGroup E] {a : E} (_ha : a ≠ 0) : 0 ≤ ‖a‖ := by positivity example [NormedAddGroup E] {a : E} : 0 ≤ ‖a‖ := by positivity example [NormedAddGroup E] {a : E} (ha : a ≠ 0) : 0 < ‖a‖ := by positivity example [MetricSpace α] (x y : α) : 0 ≤ dist x y := by positivity example [MetricSpace α] {s : Set α} : 0 ≤ Metric.diam s := by positivity example {E : Type*} [AddGroup E] {p : AddGroupSeminorm E} {x : E} : 0 ≤ p x := by positivity example {E : Type*} [Group E] {p : GroupSeminorm E} {x : E} : 0 ≤ p x := by positivity end Norms -- example {r : α → β → Prop} [∀ a, DecidablePred (r a)] {s : Finset α} {t : Finset β} : -- 0 ≤ Rel.edgeDensity r s t := by positivity -- example {G : SimpleGraph α} [DecidableRel G.adj] {s t : finset α} : -- 0 ≤ G.edgeDensity s t := by positivity /-! ### Special functions -/ section SpecialFunctions example {r : ℝ} : 0 < Real.exp r := by positivity example {n : ℕ} : 0 ≤ Real.log n := by positivity example {n : ℤ} : 0 ≤ Real.log n := by positivity example : 0 < Real.log 2 := by positivity example : 0 < Real.log 1.01 := by positivity example : 0 ≠ Real.log 0.99 := by positivity example : 0 < Real.log (-2) := by positivity example : 0 < Real.log (-1.01) := by positivity example : 0 ≠ Real.log (-0.99) := by positivity example : 0 ≤ Real.log 1 := by positivity example : 0 ≤ Real.log 0 := by positivity example : 0 ≤ Real.log (-1) := by positivity example : 0 < Real.arctan 1.1 := by positivity example {r : ℝ} (hr : 0 ≤ r) : 0 ≤ Real.arctan r := by positivity example {r : ℝ} (hr : r ≠ 0) : Real.arctan r ≠ 0 := by positivity example (r : ℝ) : 0 < Real.cos (Real.arctan r) := by positivity example {r : ℝ} (hr : 0 < r) : 0 < Real.sin (Real.arctan r) := by positivity example {r : ℝ} (hr : r ≠ 0) : Real.sin (Real.arctan r) ≠ 0 := by positivity example {r : ℝ} (hr : 0 ≤ r) : 0 ≤ Real.sin (Real.arctan r) := by positivity end SpecialFunctions /-! ### `sqrt` on `ℝ` and `ℝ≥0` -/ section Sqrt example : 0 < NNReal.sqrt 5 := by positivity example : 0 ≤ Real.sqrt (-5) := by positivity example (x : ℝ) : 0 ≤ Real.sqrt x := by positivity example : 0 < Real.sqrt 5 := by positivity example {a : ℝ} (_ha : 0 ≤ a) : 0 ≤ Real.sqrt a := by positivity example {a : ℝ} (ha : 0 ≤ a) : 0 < Real.sqrt (a + 3) := by positivity end Sqrt /- ### Canonical orders -/ example {a : ℕ} : 0 ≤ a := by positivity example {a : ℚ≥0} : 0 ≤ a := by positivity example {a : ℝ≥0} : 0 ≤ a := by positivity example {a : ℝ≥0∞} : 0 ≤ a := by positivity /- ### Coercions -/ section Coercions example {a : ℕ} : (0 : ℤ) ≤ a := by positivity example {a : ℕ} : (0 : ℚ) ≤ a := by positivity example {a : ℕ} : (0 : EReal) ≤ a := by positivity example {a : ℕ} (ha : 0 < a) : (0 : ℤ) < a := by positivity example {a : ℕ} (ha : 0 < a) : (0 : ℚ) < a := by positivity example {a : ℕ} (ha : 0 < a) : (0 : EReal) < a := by positivity example {a : ℤ} (ha : a ≠ 0) : (a : ℚ) ≠ 0 := by positivity example {a : ℤ} (ha : 0 ≤ a) : (0 : ℚ) ≤ a := by positivity example {a : ℤ} (ha : 0 < a) : (0 : ℚ) < a := by positivity example {a : ℚ} (ha : a ≠ 0) : (a : ℝ) ≠ 0 := by positivity example {a : ℚ} (ha : 0 ≤ a) : (0 : ℝ) ≤ a := by positivity example {a : ℚ} (ha : 0 < a) : (0 : ℝ) < a := by positivity example {a : ℚ≥0} (ha : a ≠ 0) : (a : ℝ≥0) ≠ 0 := by positivity example {a : ℚ≥0} : (0 : ℝ≥0) ≤ a := by positivity example {a : ℚ≥0} (ha : 0 < a) : (0 : ℝ≥0) < a := by positivity example {r : ℝ≥0} : (0 : ℝ) ≤ r := by positivity example {r : ℝ≥0} (hr : 0 < r) : (0 : ℝ) < r := by positivity example : (0 : ℝ) < ↑(3 : ℝ≥0) := by positivity example {r : ℝ≥0} (hr : 0 < r) : (0 : ℝ≥0∞) < r := by positivity example {r : ℝ≥0} : (0 : EReal) ≤ r := by positivity -- TODO: Handle `coe_trans` example {r : ℝ≥0} (hr : 0 < r) : (0 : EReal) < r := by positivity example {r : ℝ} (hr : 0 ≤ r) : (0 : EReal) ≤ r := by positivity example {r : ℝ} (hr : 0 < r) : (0 : EReal) < r := by positivity -- example {r : ℝ} (hr : 0 ≤ r) : (0 : Hyperreal) ≤ r := by positivity -- example {r : ℝ} (hr : 0 < r) : (0 : Hyperreal) < r := by positivity example {r : ℝ≥0∞} : (0 : EReal) ≤ r := by positivity example {r : ℝ≥0∞} (hr : 0 < r) : (0 : EReal) < r := by positivity -- example {α : Type*} [OrderedRing α] {n : ℤ} : 0 ≤ ((n ^ 2 : ℤ) : α) := by positivity example {r : ℝ≥0} : 0 ≤ ((r : ℝ) : EReal) := by positivity example {r : ℝ≥0} : 0 < ((r + 1 : ℝ) : EReal) := by positivity end Coercions /- ## Integrals -/ section Integral open MeasureTheory variable {D : Type*} [NormedAddCommGroup D] [NormedSpace ℝ D] {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [MeasurableSpace D] [BorelSpace D] (μ : Measure D) example (f : D → E) : 0 ≤ ∫ x, ‖f x‖ ∂μ := by positivity example (f g : D → E) : 0 ≤ ∫ x, ‖f x‖ + ‖g x‖ ∂μ := by positivity example (f : D → E) (c : ℝ) (hc : 0 < c): 0 ≤ ∫ x, c * ‖f x‖ ∂μ := by positivity end Integral /-! ## Infinite Sums -/ example (f : ℕ → ℝ) : 0 ≤ ∑' n, f n ^ 2 := by positivity example (f : ℕ → ℝ≥0) (c : ℝ) (hc : 0 < c) : 0 ≤ ∑' n, c * f n := by positivity example [Field α] [LinearOrder α] [IsStrictOrderedRing α] [TopologicalSpace α] [OrderClosedTopology α] (f : ℚ → α) : 0 ≤ ∑' q, (f q)^2 := by positivity -- Make sure that the extension doesn't produce an invalid term by accidentally unifying `?n` with -- `0` because of the `hf` assumption set_option linter.unusedVariables false in example (f : ℕ → ℕ) (hf : 0 ≤ f 0) : 0 ≤ ∑' n, f n := by positivity /-! ## Big operators -/ section BigOperators example (n : ℕ) (f : ℕ → ℤ) : 0 ≤ ∑ j ∈ range n, f j ^ 2 := by positivity example (f : ULift.{2} ℕ → ℤ) (s : Finset (ULift.{2} ℕ)) : 0 ≤ ∑ j ∈ s, f j ^ 2 := by positivity example (n : ℕ) (f : ℕ → ℤ) : 0 ≤ ∑ j : Fin 8, ∑ i ∈ range n, (f j ^ 2 + i ^ 2) := by positivity example (n : ℕ) (f : ℕ → ℤ) : 0 < ∑ j : Fin (n + 1), (f j ^ 2 + 1) := by positivity example (f : Empty → ℤ) : 0 ≤ ∑ j : Empty, f j ^ 2 := by positivity example (f : ℕ → ℤ) : 0 < ∑ j ∈ ({1} : Finset ℕ), (f j ^ 2 + 1) := by positivity example (s : Finset ℕ) : 0 ≤ ∑ j ∈ s, j := by positivity example (s : Finset ℕ) : 0 ≤ s.sum id := by positivity example (s : Finset ℕ) (f : ℕ → ℕ) (a : ℕ) : 0 ≤ s.sum (f a) := by positivity -- Make sure that the extension doesn't produce an invalid term by accidentally unifying `?n` with -- `0` because of the `hf` assumption set_option linter.unusedVariables false in example (f : ℕ → ℕ) (hf : 0 ≤ f 0) : 0 ≤ ∑ n ∈ Finset.range 10, f n := by positivity set_option linter.unusedVariables false in example (n : ℕ) : ∏ j ∈ range n, (-1) ≠ 0 := by positivity example (n : ℕ) (a : ℕ → ℤ) : 0 ≤ ∏ j ∈ range n, a j^2 := by positivity example (a : ULift.{2} ℕ → ℤ) (s : Finset (ULift.{2} ℕ)) : 0 ≤ ∏ j ∈ s, a j^2 := by positivity example (n : ℕ) (a : ℕ → ℤ) : 0 ≤ ∏ j : Fin 8, ∏ i ∈ range n, (a j^2 + i ^ 2) := by positivity example (n : ℕ) (a : ℕ → ℤ) : 0 < ∏ j : Fin (n + 1), (a j^2 + 1) := by positivity example (a : ℕ → ℤ) : 0 < ∏ j ∈ ({1} : Finset ℕ), (a j^2 + 1) := by positivity example (s : Finset ℕ) : 0 ≤ ∏ j ∈ s, j := by positivity example (s : Finset ℕ) : 0 ≤ s.sum id := by positivity example (s : Finset ℕ) (f : ℕ → ℕ) (a : ℕ) : 0 ≤ s.sum (f a) := by positivity -- Make sure that the extension doesn't produce an invalid term by accidentally unifying `?n` with -- `0` because of the `hf` assumption set_option linter.unusedVariables false in example (f : ℕ → ℕ) (hf : 0 ≤ f 0) : 0 ≤ ∏ n ∈ Finset.range 10, f n := by positivity -- Make sure that `positivity` isn't too greedy by trying to prove that a product is positive -- because its body is even if multiplication isn't strictly monotone example [CommSemiring α] [PartialOrder α] [IsOrderedRing α] {a : α} (ha : 0 < a) : 0 ≤ ∏ _i ∈ {(0 : α)}, a := by positivity end BigOperators /- ## Other extensions -/ example [Zero β] [PartialOrder β] [FunLike F α β] [NonnegHomClass F α β] (f : F) (x : α) : 0 ≤ f x := by positivity example [Semiring S] [PartialOrder S] [IsOrderedRing S] [Semiring R] (abv : R → S) [IsAbsoluteValue abv] (x : R) : 0 ≤ abv x := by positivity
.lake/packages/mathlib/MathlibTest/right_actions.lean
import Mathlib.Algebra.GroupWithZero.Action.Opposite open MulOpposite renaming op → mop open AddOpposite renaming op → aop variable {α β : Type*} [SMul α β] [SMul αᵐᵒᵖ β] [VAdd α β] [VAdd αᵃᵒᵖ β] {a a₁ a₂ a₃ a₄ : α} {b : β} section delaborators section without_scope /-- info: a • b : β -/ #guard_msgs in #check a • b /-- info: MulOpposite.op a • b : β -/ #guard_msgs in #check mop a • b /-- info: a +ᵥ b : β -/ #guard_msgs in #check a +ᵥ b /-- info: AddOpposite.op a +ᵥ b : β -/ #guard_msgs in #check aop a +ᵥ b end without_scope section with_scope open scoped RightActions /-- info: a •> b : β -/ #guard_msgs in #check a • b /-- info: b <• a : β -/ #guard_msgs in #check mop a • b /-- info: a +ᵥ> b : β -/ #guard_msgs in #check a +ᵥ b /-- info: b <+ᵥ a : β -/ #guard_msgs in #check aop a +ᵥ b end with_scope end delaborators open scoped RightActions example : a •> b = a • b := rfl example : b <• a = mop a • b := rfl example : a +ᵥ> b = a +ᵥ b := rfl example : b <+ᵥ a = aop a +ᵥ b := rfl -- Left actions right-associate, right actions left-associate example : a₁ •> a₂ •> b = a₁ •> (a₂ •> b) := rfl example : b <• a₂ <• a₁ = (b <• a₂) <• a₁ := rfl example : a₁ +ᵥ> a₂ +ᵥ> b = a₁ +ᵥ> (a₂ +ᵥ> b) := rfl example : b <+ᵥ a₂ <+ᵥ a₁ = (b <+ᵥ a₂) <+ᵥ a₁ := rfl -- When left and right actions coexist, they associate to the left example : a₁ •> b <• a₂ = (a₁ •> b) <• a₂ := rfl example : a₁ •> a₂ •> b <• a₃ <• a₄ = ((a₁ •> (a₂ •> b)) <• a₃) <• a₄ := rfl example : a₁ +ᵥ> b <+ᵥ a₂ = (a₁ +ᵥ> b) <+ᵥ a₂ := rfl example : a₁ +ᵥ> a₂ +ᵥ> b <+ᵥ a₃ <+ᵥ a₄ = ((a₁ +ᵥ> (a₂ +ᵥ> b)) <+ᵥ a₃) <+ᵥ a₄ := rfl -- association is chosen to match multiplication and addition example {M} [Mul M] {x y z : M} : x •> y <• z = x * y * z := rfl example {A} [Add A] {x y z : A} : x +ᵥ> y <+ᵥ z = x + y + z := rfl
.lake/packages/mathlib/MathlibTest/format_table.lean
import Mathlib.Util.FormatTable /-- info: | first | second header | third \| header | | :-------------- | :-------------: | --------------: | | item number one | item two | item c | | the fourth item | the fourth item | escape \| this | | align left | align center | align right | -/ #guard_msgs in run_cmd Lean.logInfo ("\n" ++ formatTable #["first", "second header", "third | header"] #[#["item number one", "item two", "item c"], #["the fourth item", "the fourth item", "escape | this"], #["align left", "align center", "align right"]] (.some #[.left, .center, .right]))
.lake/packages/mathlib/MathlibTest/NthRewrite.lean
import Mathlib.Tactic.NthRewrite import Mathlib.Algebra.Group.Defs import Mathlib.Data.Vector.Defs import Mathlib.Algebra.Ring.Nat set_option autoImplicit true open Mathlib example [AddZeroClass G] {a : G} (h : a = a): a = (a + 0) := by nth_rewrite 2 [← add_zero a] at h exact h example [AddZeroClass G] {a : G} : a + a = a + (a + 0) := by nth_rw 2 [← add_zero a] structure F where (a : ℕ) (v : List.Vector ℕ a) (p : v.val = []) example (f : F) : f.v.val = [] := by nth_rw 1 [f.p] structure Cat where (O : Type) (H : O → O → Type) (i : (o : O) → H o o) (c : {X Y Z : O} → (f : H X Y) → (g : H Y Z) → H X Z) (li : ∀ {X Y : O} (f : H X Y), c (i X) f = f) (ri : ∀ {X Y : O} (f : H X Y), c f (i Y) = f) (a : ∀ {W X Y Z : O} (f : H W X) (g : H X Y) (h : H Y Z), c (c f g) h = c f (c g h)) example (C : Cat) (W X Y Z : C.O) (f : C.H X Y) (g : C.H W X) (h _k : C.H Y Z) : C.c (C.c g f) h = C.c g (C.c f h) := by nth_rw 1 [C.a] example (C : Cat) (X Y : C.O) (f : C.H X Y) : C.c f (C.i Y) = f := by nth_rw 1 [C.ri] example (x y z : ℕ) (h1 : x = y) (h2 : y = z) : x + x + x + y = y + y + x + x := by nth_rewrite 3 [h1, h2] -- h2 *is* used, this is different from mathlib3 nth_rewrite 3 [h2] rw [h1] rw [h2] axiom foo : [1] = [2] example : [[1], [1], [1]] = [[1], [2], [1]] := by nth_rw 2 [foo] axiom foo' : [6] = [7] axiom bar' : [[5],[5]] = [[6],[6]] example : [[7],[6]] = [[5],[5]] := by nth_rewrite 1 [foo'] nth_rewrite 1 [bar'] nth_rewrite 1 [← foo'] nth_rewrite 1 [← foo'] rfl example (a b c : ℕ) : c + a + b = a + c + b := by nth_rewrite 4 [add_comm] rfl axiom wowzer : (3, 3) = (5, 2) axiom kachow (n : ℕ) : (4, n) = (5, n) axiom pchew (n : ℕ) : (n, 5) = (5, n) axiom smash (n m : ℕ) : (n, m) = (1, 1) example : [(3, 3), (5, 9), (5, 9)] = [(4, 5), (3, 6), (1, 1)] := by nth_rewrite 1 [wowzer] nth_rewrite 3 [← pchew] nth_rewrite 1 [pchew] nth_rewrite 1 [smash] nth_rewrite 2 [smash] nth_rewrite 3 [smash] nth_rewrite 4 [smash] nth_rewrite 5 [smash] nth_rewrite 6 [smash] rfl example (x y : Prop) (h₁ : x ↔ y) (h₂ : x ↔ x ∧ x) : x ∧ x ↔ x := by nth_rewrite 3 [h₁] at h₂ nth_rewrite 1 [← h₁] at h₂ nth_rewrite 3 [h₂] rfl example (x y : ℕ) (h₁ : x = y) (h₂ : x = x + x) : x + x = x := by nth_rewrite 3 [h₁] at h₂ nth_rewrite 1 [← h₁] at h₂ nth_rewrite 3 [h₂] rfl example (x y : ℕ) (h : x = y) : x + x + x = x + y + y := by nth_rw 2 3 [h]
.lake/packages/mathlib/MathlibTest/vec_notation.lean
/- Manually ported from https://github.com/leanprover-community/mathlib/blob/fee91d74414e681a8b72cb7160e6b5ef0ec2cc0b/test/vec_notation.lean -/ import Mathlib.Data.Fin.VecNotation open Lean open Lean.Meta open Qq set_option linter.style.setOption false in set_option pp.unicode.fun false /-- info: ![1, 1 + 1, 1 + 1 + 1] : Fin (Nat.succ 2) → ℤ -/ #guard_msgs in #check by_elab return PiFin.mkLiteralQ ![q(1 : ℤ), q(1 + 1), q(1 + 1 + 1)] /-! These tests are testing `PiFin.toExpr` and fail with `local attribute [-instance] PiFin.toExpr` -/ run_elab do let x : Fin 0 → ℕ := ![] guard (← isDefEq (toExpr x) q((![] : Fin 0 → ℕ))) run_elab do let x := ![1, 2, 3] guard (← isDefEq (toExpr x) q(![1, 2, 3])) run_elab do let x := ![![1, 2], ![3, 4]] guard (← isDefEq (toExpr x) q(![![1, 2], ![3, 4]])) /-! These tests are testing `PiFin.repr` -/ #guard (toString (repr (![] : _ → ℕ)) = "![]") #guard (toString (repr ![1, 2, 3]) = "![1, 2, 3]") #guard (toString (repr ![![1, 2], ![3, 4]]) = "![![1, 2], ![3, 4]]") /-! These tests are testing delaborators -/ set_option linter.style.commandStart false /-- info: fun x => ![0, 1] x : Fin 2 → ℕ -/ #guard_msgs in #check fun x : Fin 2 => (![0, 1] : Fin 2 → ℕ) x /-- info: fun x => ![] x : Fin 0 → ℕ -/ #guard_msgs in #check fun x : Fin 0 => (![] : Fin 0 → ℕ) x section variable {a b c d e f g h : α} example : ![a] 0 = a := by dsimp only [Matrix.cons_val] example : ![a] 1 = a := by dsimp only [Matrix.cons_val] example : ![a] 2 = a := by dsimp only [Matrix.cons_val] example : ![a] (-1) = a := by dsimp only [Matrix.cons_val] example : ![a, b, c] 0 = a := by dsimp only [Matrix.cons_val] example : ![a, b, c] 1 = b := by dsimp only [Matrix.cons_val] example : ![a, b, c] 2 = c := by dsimp only [Matrix.cons_val] example : ![a, b, c, d] 0 = a := by dsimp only [Matrix.cons_val] example : ![a, b, c, d] 1 = b := by dsimp only [Matrix.cons_val] example : ![a, b, c, d] 2 = c := by dsimp only [Matrix.cons_val] example : ![a, b, c, d] 3 = d := by dsimp only [Matrix.cons_val] example : ![a, b, c, d] 42 = c := by dsimp only [Matrix.cons_val] example : ![a, b, c, d] (-2) = c := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 0 = a := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 1 = b := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 2 = c := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 3 = d := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 4 = e := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 5 = a := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 6 = b := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 7 = c := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 8 = d := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 9 = e := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 10 = a := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 123 = d := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e] 123456789 = e := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e, f, g, h] 5 = f := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e, f, g, h] (5 : Fin (4 + 4)) = f := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e, f, g, h] 7 = h := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e, f, g, h] 37 = f := by dsimp only [Matrix.cons_val] example : ![a, b, c, d, e, f, g, h] 99 = d := by dsimp only [Matrix.cons_val] end open Matrix example {a b c : α} {f : Fin 3 → α} : vecCons a (vecCons b (vecCons c f)) 0 = a := by dsimp only [Matrix.cons_val] example {a b c : α} {f : Fin 3 → α} : vecCons a (vecCons b (vecCons c f)) 2 = c := by dsimp only [Matrix.cons_val] example {a b c : α} {f : Fin 3 → α} : vecCons a (vecCons b (vecCons c f)) (-1) = f 2 := by dsimp only [Matrix.cons_val] example {a b c : α} {f : Fin 3 → α} : vecCons a (vecCons b (vecCons c f)) 8 = c := by dsimp only [Matrix.cons_val] example {a b c : α} {n : ℕ} {f : Fin n → α} : vecCons a (vecCons b (vecCons c f)) 0 = a := by dsimp only [Matrix.cons_val] example {a b c : α} {n : ℕ} {f : Fin n → α} : vecCons a (vecCons b (vecCons c f)) 2 = c := by dsimp only [Matrix.cons_val] example {a b c : α} {n : ℕ} {f : Fin n.succ → α} : vecCons a (vecCons b (vecCons c f)) 3 = f 0 := by dsimp only [Matrix.cons_val] example {a b c : α} {n : ℕ} {f : Fin n.succ → α} : vecCons a (vecCons b (vecCons c f)) 3 = f 0 := by dsimp only [Matrix.cons_val] example {a b c : α} {n : ℕ} {f : Fin (n + 2) → α} : vecCons a (vecCons b (vecCons c f)) 4 = f 1 := by dsimp only [Matrix.cons_val] -- these won't be true by `dsimp`, so need a separate simproc -- example {a b c : α} {n : ℕ} {f : Fin n.succ → α} : -- vecCons a (vecCons b (vecCons c f)) (-1) = f (-1) := by simp -- example {a b c : α} {n : ℕ} {f : Fin (n + 2) → α} : -- vecCons a (vecCons b (vecCons c f)) (-2) = f (-2) := by simp
.lake/packages/mathlib/MathlibTest/abel.lean
import Mathlib.Tactic.Abel set_option linter.unusedVariables false variable {α : Type _} {a b : α} example [AddCommMonoid α] : a + (b + a) = a + a + b := by abel example [AddCommGroup α] : (a + b) - ((b + a) + a) = -a := by abel example [AddCommGroup α] (x : α) : x - 0 = x := by abel example [AddCommMonoid α] : (3 : ℕ) • a = a + (2 : ℕ) • a := by abel example [AddCommGroup α] : (3 : ℤ) • a = a + (2 : ℤ) • a := by abel example [AddCommGroup α] (a b : α) : a-2•b = a -2•b := by abel example [AddCommMonoid α] (a b : α) : a + (b + a) = a + a + b := by abel1 example [AddCommGroup α] (a b : α) : (a + b) - ((b + a) + a) = -a := by abel1 example [AddCommGroup α] (x : α) : x - 0 = x := by abel1 example [AddCommMonoid α] (a : α) : (3 : ℕ) • a = a + (2 : ℕ) • a := by abel1 example [AddCommGroup α] (a : α) : (3 : ℤ) • a = a + (2 : ℤ) • a := by abel1 example [AddCommGroup α] (a b : α) : a - 2•b = a - 2•b := by abel1 example [AddCommGroup α] (a : α) : 0 + a = a := by abel1 example [AddCommGroup α] (n : ℕ) (a : α) : n • a = n • a := by abel1 example [AddCommGroup α] (n : ℕ) (a : α) : 0 + n • a = n • a := by abel1 example [AddCommMonoid α] (a b c d e : α) : a + (b + (a + (c + (a + (d + (a + e)))))) = e + d + c + b + 4 • a := by abel1 example [AddCommGroup α] (a b c d e : α) : a + (b + (a + (c + (a + (d + (a + e)))))) = e + d + c + b + 4 • a := by abel1 example [AddCommGroup α] (a b c d : α) : a + b + (c + d - a) = b + c + d := by abel1 example [AddCommGroup α] (a b c : α) : a + b + c + (c - a - a) = (-1)•a + b + 2•c := by abel1 -- Make sure we fail on some non-equalities. example [AddCommMonoid α] (a b c d e : α) : a + (b + (a + (c + (a + (d + (a + e)))))) = e + d + c + b + 3 • a ∨ True := by fail_if_success left; abel1 right; trivial example [AddCommGroup α] (a b c d e : α) : a + (b + (a + (c + (a + (d + (a + e)))))) = e + d + c + b + 3 • a ∨ True := by fail_if_success left; abel1 right; trivial example [AddCommGroup α] (a b c d : α) : a + b + (c + d - a) = b + c - d ∨ True := by fail_if_success left; abel1 right; trivial example [AddCommGroup α] (a b c : α) : a + b + c + (c - a - a) = (-1)•a + b + c ∨ True := by fail_if_success left; abel1 right; trivial /-- `MyTrue` should be opaque to `abel`. -/ private def MyTrue := True /-- error: abel_nf made no progress on goal -/ #guard_msgs in example : MyTrue := by abel_nf -- `abel!` should see through terms that are definitionally equal, def id' (x : α) := x example [AddCommGroup α] (a b : α) : a + b - b - id' a = 0 := by fail_if_success abel1 abel1! -- https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Interaction.20of.20abel.20with.20casting/near/319895001 example [AddCommGroup α] : True := by have : ∀ (p q r s : α), s + p - q = s - r - (q - r - p) := by intro p q r s abel trivial -- https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Interaction.20of.20abel.20with.20casting/near/319897374 example [AddCommGroup α] (x y z : α) : y = x + z - (x - y + z) := by have : True := trivial abel -- https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/abel.20bug.3F/near/368707560 example [AddCommGroup α] (a b s : α) : -b + (s - a) = s - b - a := by abel_nf -- inspired by automated testing /-- error: abel_nf made no progress on goal -/ #guard_msgs in example : True := by have := 0 abel_nf /-- error: abel_nf made no progress on goal -/ #guard_msgs in example : False := by abel_nf /-- error: abel_nf made no progress at w -/ #guard_msgs in example [AddCommGroup α] (x y z : α) (w : x = y + z) : False := by abel_nf at w example [AddCommGroup α] (x y z : α) (h : False) (w : x - x = y + z) : False := by abel_nf at w guard_hyp w : 0 = y + z assumption -- regression test for the issue fixed in PR #29778 example [AddCommGroup α] {a b : α} {P : α → Prop} (h : P a) : P (a - b + b) := by abel_nf guard_target = P a exact h /- Test that when `abel_nf` is run at multiple locations, it uses the same atom ordering in each location. -/ example [AddCommGroup α] {a b c : α} (h1 : a + b + c = 0) (h2 : b + a + c = 0) : c + a + b = 0 := by abel_nf at * guard_hyp h1 : c + (a + b) = 0 guard_hyp h2 : c + (a + b) = 0 guard_target = c + (a + b) = 0 exact h1 /-- error: abel_nf made no progress anywhere -/ #guard_msgs in example [AddCommGroup α] (x y z : α) (_w : x = y + z) : False := by abel_nf at * -- Prior to https://github.com/leanprover/lean4/pull/2917 this would fail -- (the `at *` would close the goal, -- and then error when trying to work on the hypotheses because there was no goal.) example [AddCommGroup α] (x y z : α) (_w : x = y + z) : x - x = 0 := by abel_nf at * /-- error: abel_nf made no progress at w -/ #guard_msgs in example [AddCommGroup α] (x y z : α) (w : x = y + z) : x - x = 0 := by abel_nf at w ⊢ /-- error: abel_nf made no progress on goal -/ #guard_msgs in example [AddCommGroup α] (x y z : α) (w : x - x = y + z) : x = 0 := by abel_nf at w ⊢ example [AddCommGroup α] (x y z : α) (h : False) (w : x - x = y + z) : False := by abel_nf at * guard_hyp w : 0 = y + z assumption section abbrev myId (a : ℤ) : ℤ := a /- Test that when `abel_nf` normalizes multiple expressions which contain a particular atom, it uses a form for that atom which is consistent between expressions. We can't use `guard_hyp h :ₛ` here, as while it does tell apart `x` and `myId x`, it also complains about differing instance paths. -/ /-- trace: α : Type _ a b : α x : ℤ R : ℤ → ℤ → Prop hR : Reflexive R h : R (2 • myId x) (2 • myId x) ⊢ True -/ #guard_msgs (trace) in set_option pp.mvars false in example (x : ℤ) (R : ℤ → ℤ → Prop) (hR : Reflexive R) : True := by have h : R (myId x + x) (x + myId x) := hR .. abel_nf at h trace_state trivial end -- Test that `abel_nf` doesn't unfold local let expressions, and `abel_nf!` does example [AddCommGroup α] (x : α) (f : α → α) : True := by let y := x have : x = y := by fail_if_success abel_nf abel_nf! have : x - y = 0 := by abel_nf abel_nf! have : f x = f y := by fail_if_success abel_nf abel_nf! have : f x - f y = 0 := by abel_nf abel_nf! trivial
.lake/packages/mathlib/MathlibTest/DeriveFintype.lean
import Mathlib.Tactic.DeriveFintype import Mathlib.Data.Fintype.Prod import Mathlib.Data.Fintype.Pi set_option autoImplicit true namespace tests /- Tests that the enumerable types succeed, even with universe levels. -/ inductive A | x | y | z deriving Fintype /-- info: tests.A.enumList : List A -/ #guard_msgs in #check A.enumList inductive A' : Type u | x | y | z deriving Fintype /-- info: tests.A'.enumList.{u} : List A' -/ #guard_msgs in #check A'.enumList inductive A'' : Type 1 | x | y | z deriving Fintype /-- info: tests.A''.enumList : List A'' -/ #guard_msgs in #check A''.enumList /- Now for general inductive types -/ -- The empty type needs a little special handling due to needing `nomatch` syntactically inductive B deriving Fintype example : (Finset.univ : Finset B) = ∅ := rfl inductive C (α : Type _) | c (x : α) deriving Fintype, DecidableEq /-- info: instFintypeC -/ #guard_msgs in #synth Fintype (C Bool) example : (Finset.univ : Finset (C Bool)) = Finset.univ.image .c := rfl inductive C' (P : Prop) : Type | c (h : P) deriving Fintype /-- info: instFintypeC'OfDecidable -/ #guard_msgs in #synth Fintype (C' (1 = 2)) example : Fintype.card (C' (1 = 2)) = 0 := rfl inductive C'' (P : Prop) | c (h : P) (b : Bool) deriving Fintype example : Fintype.card (C'' (1 = 2)) = 0 := rfl section variable [Fintype α] /-- info: instFintypeC -/ #guard_msgs in #synth Fintype (C α) end inductive D (p : Prop) : Type | d (h : p) deriving Fintype section variable [Decidable q] /-- info: instFintypeDOfDecidable -/ #guard_msgs in #synth Fintype (D q) end inductive baz | a : Bool → baz deriving Fintype inductive bar (n : Nat) (α : Type) | a | b : Bool → bar n α | c (x : Fin n) : Fin x → bar n α | d : Bool → α → bar n α deriving Fintype inductive Y : Nat → Type | x (n : Nat) : Y n deriving Fintype structure S where (x y z : Bool) deriving Fintype example : Fintype.card S = 8 := rfl inductive MyOption (α : Type _) | none | some (x : α) deriving Fintype example : Fintype.card (MyOption Bool) = 3 := rfl -- Check implicits work inductive I {α : Type _} | a | b {x : α} deriving Fintype inductive I' {α : Type _} | a | b {x : α} : @I' α deriving Fintype /- `derive_fintype%` -/ def myBoolInst := derive_fintype% Bool example : Fintype Bool := myBoolInst def myBoolInst' : Fintype Bool := derive_fintype% _ example : Fintype Bool := myBoolInst' def myProdInst [Fintype α] [Fintype β] : Fintype (α × β) := derive_fintype% _ structure MySubtype (s : Set α) where val : α mem : val ∈ s --deriving Fintype -- fails instance (s : Set α) [Fintype α] [DecidablePred (· ∈ s)] : Fintype (MySubtype s) := derive_fintype% _ /- Tests from mathlib 3 -/ inductive Alphabet | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z deriving Fintype example : Fintype.card Alphabet = 52 := rfl inductive foo | A (x : Bool) | B (y : Unit) | C (z : Fin 37) deriving Fintype example : Fintype.card foo = 2 + 1 + 37 := rfl inductive foo2 (α : Type) | A : α → foo2 α | B : α → α → foo2 α | C : α × α → foo2 α | D : foo2 α deriving Fintype inductive foo3 (α β : Type) (n : ℕ) | A : (α → β) → foo3 α β n | B : Fin n → foo3 α β n --deriving Fintype -- won't work because missing decidable instance instance (α β : Type) [DecidableEq α] [Fintype α] [Fintype β] (n : ℕ) : Fintype (foo3 α β n) := derive_fintype% _ structure foo4 {m n : Type} (b : m → ℕ) where (x : m × n) (y : m × n) (h : b x.1 = b y.1) deriving Fintype class MyClass (M : Type _) where (one : M) (eq_one : ∀ x : M, x = one) instance {M : Type _} [Fintype M] [DecidableEq M] : Fintype (MyClass M) := derive_fintype% _ end tests
.lake/packages/mathlib/MathlibTest/UnsetOption.lean
import Mathlib.Tactic.UnsetOption set_option linter.style.setOption false set_option linter.unusedTactic false set_option pp.all true example : True := by run_tac let t : Option Bool := (← Lean.MonadOptions.getOptions).get? `pp.all -- should be true as set guard (t == true) trivial section unset_option pp.all example : True := by run_tac let t : Option Bool := (← Lean.MonadOptions.getOptions).get? `pp.all -- should be none as unset guard (t == Option.none) trivial end example : True := by run_tac let t : Option Bool := (← Lean.MonadOptions.getOptions).get? `pp.all -- should be true as only unset within section guard (t == true) trivial
.lake/packages/mathlib/MathlibTest/dfinsupp_notation.lean
import Mathlib.Algebra.Group.Int.Defs import Mathlib.Data.DFinsupp.Notation example : (fun₀ | 1 => 3 : Π₀ i, Fin (i + 10)) 1 = 3 := by simp example : (fun₀ | 1 | 2 | 3 => 3 | 3 => 4 : Π₀ i, Fin (i + 10)) 1 = 3 := by simp example : (fun₀ | 1 | 2 | 3 => 3 | 3 => 4 : Π₀ i, Fin (i + 10)) 2 = 3 := by simp example : (fun₀ | 1 | 2 | 3 => 3 | 3 => 4 : Π₀ i, Fin (i + 10)) 3 = 4 := by simp section Repr /-- info: fun₀ | 1 => 3 | 2 => 3 : Π₀ (i : ℕ), Fin (i + 10) -/ #guard_msgs in #check (fun₀ | 1 => 3 | 2 => 3 : Π₀ i, Fin (i + 10)) /-- info: fun₀ | 2 => 7 -/ #guard_msgs in #eval ((fun₀ | 1 => 3 | 2 => 3) + (fun₀ | 1 => -3 | 2 => 4) : Π₀ _, ℤ) /-- info: fun₀ | ["there are five words here", "and five more words here"] => 5 | ["there are seven words but only here"] => 7 | ["just two"] => 2 -/ #guard_msgs in #eval (fun₀ | ["there are five words here", "and five more words here"] => 5 | ["there are seven words but only here"] => 7 | ["just two"] => 2 : Π₀ _ : List String, ℕ) end Repr section PrettyPrinter /-- info: fun₀ | ["there are five words here", "and five more words here"] => 5 | ["there are seven words but only here"] => 7 | ["just two"] => 2 : Π₀ (i : List String), ℕ -/ #guard_msgs in #check (fun₀ | ["there are five words here", "and five more words here"] => 5 | ["there are seven words but only here"] => 7 | ["just two"] => 2 : Π₀ _ : List String, ℕ) end PrettyPrinter
.lake/packages/mathlib/MathlibTest/RefinedDiscrTree.lean
import Mathlib.Lean.Meta.RefinedDiscrTree.Encode import Mathlib open Qq Lean Meta RefinedDiscrTree macro "#log_keys" e:term : command => `(command| run_meta do for keys in ← encodeExprWithEta (labelledStars := true) q($e) do logInfo m! "{← keysAsPattern keys}") -- eta reduction: /-- info: Int.succ -/ #guard_msgs in #log_keys fun x => Int.succ x -- potential eta reduction: /-- info: @Function.Bijective ℤ ℤ Int.succ --- info: @Function.Bijective ℤ ℤ (λ, Int.succ *) -/ #guard_msgs in run_meta do let m ← mkFreshExprMVarQ q(ℤ → ℤ) for keys in ← encodeExprWithEta (labelledStars := true) q(Function.Bijective fun x : Int => Int.succ ($m x)) do logInfo m! "{← keysAsPattern keys}" /-- info: And (@Function.Bijective ℤ ℤ Int.succ) (@Function.Bijective ℤ ℤ Int.succ) --- info: And (@Function.Bijective ℤ ℤ Int.succ) (@Function.Bijective ℤ ℤ (λ, Int.succ *)) --- info: And (@Function.Bijective ℤ ℤ (λ, Int.succ *)) (@Function.Bijective ℤ ℤ Int.succ) --- info: And (@Function.Bijective ℤ ℤ (λ, Int.succ *)) (@Function.Bijective ℤ ℤ (λ, Int.succ *)) -/ #guard_msgs in run_meta do let m ← mkFreshExprMVarQ q(ℤ → ℤ) for keys in ← encodeExprWithEta (labelledStars := true) q((Function.Bijective fun x : Int => Int.succ ($m x)) ∧ Function.Bijective fun x : Int => Int.succ ($m x)) do logInfo m! "{← keysAsPattern keys}" /-- info: @Eq *0 (@HAdd.hAdd *0 *0 * * (@HAdd.hAdd *0 *0 * * *1 *1) *2) (@HAdd.hAdd *0 *0 * * *1 a) -/ #guard_msgs in run_meta do let t ← mkFreshExprMVarQ q(Type) let _ ← mkFreshExprMVarQ q(Add $t) let m ← mkFreshExprMVarQ q($t) let m' ← mkFreshExprMVarQ q($t) withLocalDeclDQ `a q($t) fun n => do for keys in ← encodeExprWithEta (labelledStars := true) q($m+$m + $m' = $m + $n) do logInfo m! "{← keysAsPattern keys}" /-- info: @Function.Bijective *0 *0 (@HAdd.hAdd *0 *0 * * *) --- info: @Function.Bijective *0 *0 (λ, @HAdd.hAdd *0 *0 * * * *) -/ #guard_msgs in run_meta do let t ← mkFreshExprMVarQ q(Type) let _ ← mkFreshExprMVarQ q(Add $t) let m ← mkFreshExprMVarQ q($t → $t) let m' ← mkFreshExprMVarQ q($t → $t) for keys in ← encodeExprWithEta (labelledStars := true) q(Function.Bijective fun x => $m x + $m' x) do logInfo m! "{← keysAsPattern keys}" /-- info: @OfNat.ofNat ℕ 2 * -/ #guard_msgs in #log_keys let x := 2; x -- unfolding reducible constants: /-- info: @LE.le ℕ * 3 2 -/ #guard_msgs in #log_keys 2 ≥ 3 /-- info: ℕ → @Eq ℕ #0 5 -/ #guard_msgs in #log_keys ∀ x : Nat, x = 5 /-- info: ℕ → ℤ -/ #guard_msgs in #log_keys Nat → Int /-- info: λ, #0 -/ #guard_msgs in #log_keys fun x : Nat => x open Finset in /-- info: @Finset.sum ℕ ℕ * (range 10) (λ, #0) -/ #guard_msgs in #log_keys ∑ i ∈ range 10, i /-- info: @Nat.fold ℕ 10 (λ, λ, λ, @HAdd.hAdd ℕ ℕ * * #0 #2) 1 -/ #guard_msgs in #log_keys (10).fold (init := 1) (fun x _ y => y + x) /-- info: @HAdd.hAdd (ℕ → ℕ) (ℕ → ℕ) * * (@id ℕ) (λ, #0) 4 -/ #guard_msgs in #log_keys ((@id Nat) + fun x : Nat => x) 4 /-- info: Nat.sqrt (@HAdd.hAdd (ℕ → ℕ) (ℕ → ℕ) * * (@id ℕ) (λ, #0) 4) -/ #guard_msgs in #log_keys Nat.sqrt $ ((@id Nat) + fun x : Nat => x) 4 /-- info: Nat.sqrt (@HPow.hPow (ℕ → ℕ) ℕ * * (@id ℕ) 3 6) -/ #guard_msgs in #log_keys Nat.sqrt $ (id ^ 3 : Nat → Nat) 6 /-- info: Nat.sqrt (@HVAdd.hVAdd ℕ (ℕ → ℕ) * * 4 (@id ℕ) 6) -/ #guard_msgs in #log_keys Nat.sqrt $ (4 +ᵥ id : Nat → Nat) 6 /-- info: Int.sqrt (@Neg.neg (ℤ → ℤ) * (@id ℤ) 6) -/ #guard_msgs in #log_keys Int.sqrt $ (-id : Int → Int) 6 /-- info: @Function.Bijective ℕ ℕ (λ, @HAdd.hAdd ℕ ℕ * * (@HMul.hMul ℕ ℕ * * #0 3) (@HDiv.hDiv ℕ ℕ * * 4 (@HPow.hPow ℕ ℕ * * (@HVAdd.hVAdd ℕ ℕ * * 3 (@HSMul.hSMul ℕ ℕ * * 2 5)) #0))) -/ #guard_msgs in #log_keys Function.Bijective fun x => x*3+4/(3+ᵥ2•5)^x /-- info: Nat.sqrt (@HAdd.hAdd (ℕ → ℕ) (ℕ → ℕ) * * (@HVAdd.hVAdd ℕ (ℕ → ℕ) * * (@HSMul.hSMul ℕ ℕ * * 2 1) (@id ℕ)) (@HDiv.hDiv (ℕ → ℕ) (ℕ → ℕ) * * (@HMul.hMul (ℕ → ℕ) (ℕ → ℕ) * * 4 5) (@HPow.hPow (ℕ → ℕ) ℕ * * (@id ℕ) 9)) 5) -/ #guard_msgs in #log_keys Nat.sqrt $ ((2•1+ᵥid)+4*5/id^9 : Nat → Nat) 5 /-- info: @Function.Bijective ℕ ℕ (λ, @HPow.hPow ℕ ℕ * * #0 #0) -/ #guard_msgs in #log_keys Function.Bijective fun x => x^x /-- info: @Function.Bijective ℕ ℕ (λ, @HSMul.hSMul ℕ ℕ * * #0 5) -/ #guard_msgs in #log_keys Function.Bijective fun x : Nat => x•5 /-- info: @Function.Bijective ℕ ℕ (λ, @HVAdd.hVAdd ℕ ℕ * * #0 #0) -/ #guard_msgs in #log_keys Function.Bijective fun x : Nat => x+ᵥx /-- info: @id (Sort → (Ring #0) → #1) (λ, λ, @HAdd.hAdd #1 #1 * * 1 2) -/ #guard_msgs in #log_keys id fun (α : Type) [Ring α] => (1+2 : α) /-- info: @id (Sort → (Ring #0) → #1) (λ, λ, @HSMul.hSMul ℕ #1 * * 2 3) -/ #guard_msgs in #log_keys id fun (α : Type) [Ring α] => (2•3 : α) /-- info: @Function.Bijective ℕ ℕ (λ, 4) -/ #guard_msgs in #log_keys Function.Bijective fun _ : Nat => 4 /-- info: λ, @OfNat.ofNat ℕ 4 * -/ #guard_msgs in #log_keys fun _ : Nat => 4 -- metavariables in a lambda body /-- info: @Function.Bijective ℕ ℕ (λ, *0) -/ #guard_msgs in run_meta do let m ← mkFreshExprMVarQ q(ℕ) for keys in ← encodeExprWithEta (labelledStars := true) q(Function.Bijective fun _ : Nat => $m) do logInfo m! "{← keysAsPattern keys}" /-- info: λ, *0 -/ #guard_msgs in run_meta do let m ← mkFreshExprMVarQ q(ℕ) for keys in ← encodeExprWithEta (labelledStars := true) q(fun _ : Nat => $m) do logInfo m! "{← keysAsPattern keys}" /-- info: @Function.Bijective ℕ ℕ (λ, *) -/ #guard_msgs in run_meta do let m ← mkFreshExprMVarQ q(ℕ → ℕ → ℕ) for keys in ← encodeExprWithEta (labelledStars := true) q(Function.Bijective fun x : Nat => $m x x) do logInfo m! "{← keysAsPattern keys}" /-- info: λ, * -/ #guard_msgs in run_meta do let m ← mkFreshExprMVarQ q(ℕ → ℕ → ℕ) for keys in ← encodeExprWithEta (labelledStars := true) q(fun x : Nat => $m x x) do logInfo m! "{← keysAsPattern keys}"
.lake/packages/mathlib/MathlibTest/runCmd.lean
import Lean.Elab.Tactic.ElabTerm open Lean Elab Tactic example : True := by run_tac evalApplyLikeTactic MVarId.apply (← `(True.intro)) example : True := by_elab Term.elabTerm (← `(True.intro)) none
.lake/packages/mathlib/MathlibTest/ClearExclamation.lean
import Mathlib.Tactic.ClearExclamation set_option linter.unusedVariables false -- Most basic test example (delete_this : Nat) (_delete_this_dep : delete_this = delete_this) : Nat := by clear! delete_this fail_if_success assumption exact 0 -- Confirms clear! deletes class instances example [delete_this : Inhabited Nat] : Inhabited Nat := by clear! delete_this fail_if_success assumption infer_instance -- Confirms clear! can clear the dependencies of multiple hypotheses example (delete_this : Nat) (delete_this2 : Nat) (_delete_this_dep : delete_this = delete_this2) : Nat := by clear! delete_this delete_this2 fail_if_success assumption exact 0 -- Confirms that clear! does not delete independent hypotheses example (delete_this : Nat) (dont_delete_this : Int) : Nat := by clear! delete_this fail_if_success assumption exact dont_delete_this.toNat -- Confirms that clear! only deletes dependencies in the right direction example (dont_delete_this : Nat) (delete_this : dont_delete_this = dont_delete_this) : Nat := by clear! delete_this assumption
.lake/packages/mathlib/MathlibTest/coe.lean
import Mathlib.Tactic.Coe example : { n : Nat // n > 3 } → Nat := (↑)
.lake/packages/mathlib/MathlibTest/Equiv.lean
import Mathlib.Data.Int.Order.Units import Mathlib.GroupTheory.Perm.Finite example : orderOf (-1 : ℤˣ) = 2 := orderOf_eq_prime (Int.units_sq _) (by decide)
.lake/packages/mathlib/MathlibTest/nomatch.lean
set_option autoImplicit true example : False → α := nofun example : False → α := by nofun example : ¬ False := nofun example : ¬ False := by nofun example : ¬ ¬ 0 = 0 := nofun example (h : False) : α := nomatch h example (h : Nat → False) : Nat := nomatch h 1 def ComplicatedEmpty : Bool → Type | false => Empty | true => PEmpty example (h : ComplicatedEmpty b) : α := nomatch b, h example (h : Nat → ComplicatedEmpty b) : α := nomatch b, h 1
.lake/packages/mathlib/MathlibTest/norm_num_ordinal.lean
import Mathlib.Tactic.NormNum.Ordinal universe u example : (2 : Ordinal.{0}) * 3 + 4 = 10 := by norm_num example : ¬(15 : Ordinal.{4}) / 4 % 34 < 3 := by norm_num example : (12 : Ordinal.{u}) ^ (2 : Ordinal.{u}) ^ (2 : ℕ) ≤ (12 : Ordinal.{u}) ^ (2 : Ordinal.{u}) ^ (2 : ℕ) := by norm_num
.lake/packages/mathlib/MathlibTest/norm_num_abs.lean
import Mathlib.Tactic.NormNum import Mathlib.Data.Real.Basic example : |(4 : ℤ)| = 4 := by norm_num1 example : |(4 : ℚ)| = 4 := by norm_num1 example : |(4 : ℝ)| = 4 := by norm_num1 example : |(-11 : ℤ)| = 11 := by norm_num1 example : |(3 : ℚ) / 7| = 3 / 7 := by norm_num1 example : |(1 : ℚ) / 5| < 1 / 3 := by norm_num1 example : |-(2 : ℚ) / 7| = 2 / 7 := by norm_num1
.lake/packages/mathlib/MathlibTest/ProdAssoc.lean
import Mathlib.Tactic.ProdAssoc variable {α β γ δ : Type*} example : (α × β) × (γ × δ) ≃ α × (β × γ) × δ := by exact (prod_assoc% : (α × β) × (γ × δ) ≃ α × (β × γ) × δ) example : (α × β) × (γ × δ) ≃ α × (β × γ) × δ := prod_assoc% example : (α × β) × (γ × δ) ≃ α × (β × γ) × δ := (prod_assoc% : _ ≃ α × β × γ × δ).trans prod_assoc% example {α β γ δ : Type*} (x : (α × β) × (γ × δ)) : α × (β × γ) × δ := prod_assoc% x example : Nat ≃ Nat := prod_assoc% example (x : α) (y : β) (z : γ) (w : δ) : prod_assoc% ((x,y),(z,w)) = (x,y,z,w) := rfl example (x : α) (y : β) (z : γ) (w : δ) : prod_assoc% ((x,y),(z,w)) = (x,(y,z),w) := rfl example : (α × β) × (γ × δ) → α × (β × γ) × δ := prod_assoc%
.lake/packages/mathlib/MathlibTest/instance_diamonds.lean
import Mathlib.Algebra.EuclideanDomain.Field import Mathlib.Algebra.Field.ZMod import Mathlib.Algebra.GroupWithZero.Action.Prod import Mathlib.Algebra.GroupWithZero.Action.Units import Mathlib.Algebra.Module.Pi import Mathlib.Algebra.Polynomial.Basic import Mathlib.Data.ZMod.Basic import Mathlib.LinearAlgebra.Complex.Module import Mathlib.RingTheory.Algebraic.Pi import Mathlib.RingTheory.TensorProduct.Basic /-! # Tests that instances do not form diamonds -/ /-! ## Scalar action instances -/ section SMul open scoped Polynomial example : (SubNegMonoid.toZSMul : SMul ℤ ℂ) = (Complex.SMul.instSMulRealComplex : SMul ℤ ℂ) := by with_reducible_and_instances rfl example : RestrictScalars.module ℝ ℂ ℂ = Complex.instModule := by with_reducible_and_instances rfl -- fails `with_reducible_and_instances` https://github.com/leanprover-community/mathlib4/issues/10906 example : RestrictScalars.algebra ℝ ℂ ℂ = Complex.instAlgebraOfReal := by rfl example (α β : Type _) [AddMonoid α] [AddMonoid β] : (Prod.instSMul : SMul ℕ (α × β)) = AddMonoid.toNatSMul := by with_reducible_and_instances rfl example (α β : Type _) [SubNegMonoid α] [SubNegMonoid β] : (Prod.instSMul : SMul ℤ (α × β)) = SubNegMonoid.toZSMul := by with_reducible_and_instances rfl example (α : Type _) (β : α → Type _) [∀ a, AddMonoid (β a)] : (Pi.instSMul : SMul ℕ (∀ a, β a)) = AddMonoid.toNatSMul := by with_reducible_and_instances rfl example (α : Type _) (β : α → Type _) [∀ a, SubNegMonoid (β a)] : (Pi.instSMul : SMul ℤ (∀ a, β a)) = SubNegMonoid.toZSMul := by with_reducible_and_instances rfl namespace TensorProduct open scoped TensorProduct open Complex /- `TensorProduct.Algebra.module` forms a diamond with `instSMulOfMul` and `algebra.tensor_product.tensor_product.semiring`. Given a commutative semiring `A` over a commutative semiring `R`, we get two mathematically different scalar actions of `A ⊗[R] A` on itself. -/ noncomputable def f : ℂ ⊗[ℝ] ℂ →ₗ[ℝ] ℝ := TensorProduct.lift { toFun := fun z => z.re • reLm map_add' := fun z w => by simp [add_smul] map_smul' := fun r z => by simp [mul_smul] } @[simp] theorem f_apply (z w : ℂ) : f (z ⊗ₜ[ℝ] w) = z.re * w.re := by simp [f] unseal Algebra.TensorProduct.mul in /- `TensorProduct.Algebra.module` forms a diamond with `instSMulOfMul` and `algebra.tensor_product.tensor_product.semiring`. Given a commutative semiring `A` over a commutative semiring `R`, we get two mathematically different scalar actions of `A ⊗[R] A` on itself. -/ example : instSMulOfMul (α := ℂ ⊗[ℝ] ℂ) ≠ (@TensorProduct.Algebra.module ℝ ℂ ℂ (ℂ ⊗[ℝ] ℂ) _ _ _ _ _ _ _ _ _ _ _ _).toSMul := by have contra : I ⊗ₜ[ℝ] I ≠ (-1) ⊗ₜ[ℝ] 1 := fun c => by simpa using congr_arg f c contrapose! contra rw [SMul.ext_iff, SMul.smul_eq_hSMul, @SMul.smul_eq_hSMul _ _ (_)] at contra replace contra := congr_fun (congr_fun contra (1 ⊗ₜ I)) (I ⊗ₜ 1) rw [TensorProduct.Algebra.smul_def (R := ℝ) (1 : ℂ) I (I ⊗ₜ[ℝ] (1 : ℂ))] at contra simpa only [Algebra.id.smul_eq_mul, Algebra.TensorProduct.tmul_mul_tmul, one_mul, mul_one, one_smul, TensorProduct.smul_tmul', I_mul_I] using contra end TensorProduct section Units example (α : Type _) [Monoid α] : (Units.instMulAction : MulAction αˣ (α × α)) = Prod.mulAction := by with_reducible_and_instances rfl example (R α : Type _) (β : α → Type _) [Monoid R] [∀ i, MulAction R (β i)] : (Units.instMulAction : MulAction Rˣ (∀ i, β i)) = Pi.mulAction _ := by with_reducible_and_instances rfl example (R α : Type _) [Monoid R] [Semiring α] [DistribMulAction R α] : (Units.instDistribMulAction : DistribMulAction Rˣ α[X]) = Polynomial.distribMulAction := by with_reducible_and_instances rfl /-! TODO: https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/units.2Emul_action'.20diamond/near/246402813 ```lean example {α : Type*} [CommMonoid α] : (Units.mulAction' : MulAction αˣ αˣ) = Monoid.toMulAction _ := rfl -- fails ``` -/ end Units end SMul /-! ## `Multiplicative` instances -/ section Multiplicative example : @Monoid.toMulOneClass (Multiplicative ℕ) CommMonoid.toMonoid = Multiplicative.mulOneClass := by with_reducible_and_instances rfl end Multiplicative /-! ## `Finsupp` instances -/ section Finsupp open Finsupp /-- `Finsupp.comapSMul` can form a non-equal diamond with `Finsupp.smulZeroClass` -/ example {k : Type _} [Semiring k] [Nontrivial k] : (Finsupp.comapSMul : SMul k (k →₀ k)) ≠ Finsupp.smulZeroClass.toSMul := by obtain ⟨u : k, hu⟩ := exists_ne (1 : k) intro h simp only [SMul.ext_iff, @SMul.smul_eq_hSMul _ _ (_), funext_iff, DFunLike.ext_iff] at h replace h := h u (Finsupp.single 1 1) u classical rw [comapSMul_single, smul_apply, smul_eq_mul, mul_one, single_eq_same, smul_eq_mul, single_eq_of_ne hu, MulZeroClass.mul_zero] at h exact one_ne_zero h /-- `Finsupp.comapSMul` can form a non-equal diamond with `Finsupp.smulZeroClass` even when the domain is a group. -/ example {k : Type _} [Semiring k] [Nontrivial kˣ] : (Finsupp.comapSMul : SMul kˣ (kˣ →₀ k)) ≠ Finsupp.smulZeroClass.toSMul := by obtain ⟨u : kˣ, hu⟩ := exists_ne (1 : kˣ) haveI : Nontrivial k := Units.val_injective.nontrivial intro h simp only [SMul.ext_iff, @SMul.smul_eq_hSMul _ _ (_), funext_iff, DFunLike.ext_iff] at h replace h := h u (Finsupp.single 1 1) u classical rw [comapSMul_single, smul_apply, Units.smul_def, smul_eq_mul, mul_one, single_eq_same, smul_eq_mul, single_eq_of_ne hu, MulZeroClass.mul_zero] at h exact one_ne_zero h end Finsupp /-! ## `Polynomial` instances -/ section Polynomial variable (R A : Type _) open scoped Polynomial open Polynomial /-- `Polynomial.hasSMulPi` forms a diamond with `Pi.instSMul`. -/ example [Semiring R] [Nontrivial R] : Polynomial.hasSMulPi _ _ ≠ (Pi.instSMul : SMul R[X] (R → R[X])) := by intro h simp_rw [SMul.ext_iff, @SMul.smul_eq_hSMul _ _ (_), funext_iff, Polynomial.ext_iff] at h simpa using h X 1 1 0 /-- `Polynomial.hasSMulPi'` forms a diamond with `Pi.instSMul`. -/ example [CommSemiring R] [Nontrivial R] : Polynomial.hasSMulPi' _ _ _ ≠ (Pi.instSMul : SMul R[X] (R → R[X])) := by intro h simp_rw [SMul.ext_iff, @SMul.smul_eq_hSMul _ _ (_), funext_iff, Polynomial.ext_iff] at h simpa using h X 1 1 0 -- fails `with_reducible_and_instances` https://github.com/leanprover-community/mathlib4/issues/10906 /-- `Polynomial.hasSMulPi'` is consistent with `Polynomial.hasSMulPi`. -/ example [CommSemiring R] [Nontrivial R] : Polynomial.hasSMulPi' _ _ _ = (Polynomial.hasSMulPi _ _ : SMul R[X] (R → R[X])) := rfl -- fails `with_reducible_and_instances` https://github.com/leanprover-community/mathlib4/issues/10906 /-- `Polynomial.algebraOfAlgebra` is consistent with `Semiring.toNatAlgebra`. -/ example [Semiring R] : (Polynomial.algebraOfAlgebra : Algebra ℕ R[X]) = Semiring.toNatAlgebra := rfl -- fails `with_reducible_and_instances` https://github.com/leanprover-community/mathlib4/issues/10906 /-- `Polynomial.algebraOfAlgebra` is consistent with `Ring.toIntAlgebra`. -/ example [Ring R] : (Polynomial.algebraOfAlgebra : Algebra ℤ R[X]) = Ring.toIntAlgebra _ := rfl end Polynomial /-! ## `Subtype` instances -/ section Subtype -- this diamond is the reason that `Fintype.toLocallyFiniteOrder` is not an instance example {α} [Preorder α] [LocallyFiniteOrder α] [Fintype α] [DecidableLT α] [DecidableLE α] (p : α → Prop) [DecidablePred p] : Subtype.instLocallyFiniteOrder p = Fintype.toLocallyFiniteOrder := by fail_if_success rfl exact Subsingleton.elim _ _ end Subtype /-! ## `ZMod` instances -/ section ZMod variable {p : ℕ} [Fact p.Prime] example : @EuclideanDomain.toCommRing _ (@Field.toEuclideanDomain _ (ZMod.instField p)) = ZMod.commRing p := by with_reducible_and_instances rfl -- We need `open Fin.CommRing`, as otherwise `Fin.instCommRing` is not an instance, -- so `with_reducible_and_instances` doesn't have the desired effect. open Fin.CommRing in example (n : ℕ) : ZMod.commRing (n + 1) = Fin.instCommRing (n + 1) := by with_reducible_and_instances rfl example : ZMod.commRing 0 = Int.instCommRing := by with_reducible_and_instances rfl end ZMod /-! ## Instances involving structures over `ℝ` and `ℂ` Given a scalar action on `ℝ`, we have an instance which produces the corresponding scalar action on `ℂ`. In the other direction, if there is a scalar action of `ℂ` on some type, we can get a corresponding action of `ℝ` on that type via `RestrictScalars`. Obviously, this has the potential to cause diamonds when we can go in both directions. This shows that at least some potential diamonds are avoided. -/ section complexToReal -- fails `with_reducible_and_instances` https://github.com/leanprover-community/mathlib4/issues/10906 -- the two ways to get `Algebra ℝ ℂ` are definitionally equal example : (Algebra.id ℂ).complexToReal = Complex.instAlgebraOfReal := rfl -- fails `with_reducible_and_instances` https://github.com/leanprover-community/mathlib4/issues/10906 /- The complexification of an `ℝ`-algebra `A` (i.e., `ℂ ⊗[ℝ] A`) is a `ℂ`-algebra. Viewing this as an `ℝ`-algebra by restricting scalars agrees with the existing `ℝ`-algebra structure on the tensor product. -/ open Algebra TensorProduct in example {A : Type*} [Ring A] [Algebra ℝ A]: (leftAlgebra : Algebra ℂ (ℂ ⊗[ℝ] A)).complexToReal = leftAlgebra := by rfl end complexToReal /-! ## Instances on `ℚ≥0` -/ /-- This diamond arose because the semifield structure on `NNRat` needs to be defined as early as possible, before `Nonneg.zpow` becomes available; `Nonneg.zpow` is used to then define the `LinearOrderedCommGroupWithZero` instance. -/ example : (inferInstanceAs (Semifield ℚ≥0)).toCommGroupWithZero = (inferInstanceAs (LinearOrderedCommGroupWithZero ℚ≥0)).toCommGroupWithZero := rfl
.lake/packages/mathlib/MathlibTest/tryAtEachStep.lean
import Mathlib.Tactic.Common import Mathlib.Tactic.TacticAnalysis.Declarations set_option linter.tacticAnalysis.tryAtEachStepAesop true /-- info: `rfl` can be replaced with `aesop` -/ #guard_msgs in theorem foo : 2 + 2 = 4 := by rfl -- Set the fraction sufficiently high that nothing will ever run. set_option linter.tacticAnalysis.tryAtEachStep.fraction 1_000_000_000_000 #guard_msgs in theorem bar : 2 + 2 = 4 := by rfl
.lake/packages/mathlib/MathlibTest/set_like.lean
import Mathlib.Algebra.Field.Subfield.Basic import Mathlib.Algebra.Star.Subalgebra import Mathlib.Algebra.Star.SelfAdjoint import Mathlib.Algebra.Star.NonUnitalSubalgebra set_option autoImplicit true section Delab variable {M : Type u} [Monoid M] (S S' : Submonoid M) set_option linter.style.commandStart false /-- info: ↥S → ↥S' : Type u -/ #guard_msgs in #check S → S' /-- info: ↥S : Type u -/ #guard_msgs in #check {x // x ∈ S} /-- info: { x // 1 * x ∈ S } : Type u -/ #guard_msgs in #check {x // 1 * x ∈ S} end Delab example [Ring R] (S : Subring R) (hx : x ∈ S) (hy : y ∈ S) (hz : z ∈ S) (n m : ℕ) : n • x ^ 3 - 2 • y + z ^ m ∈ S := by aesop example [Ring R] (S : Set R) (hx : x ∈ S) (hy : y ∈ S) (hz : z ∈ S) (n m : ℕ) : n • x ^ 3 - y + z ^ m ∈ Subring.closure S := by aesop example [CommRing R] [Ring A] [Algebra R A] (r : R) (a b c : A) (n : ℕ) : -b + (algebraMap R A r) + a ^ n * c ∈ Algebra.adjoin R {a, b, c} := by aesop example [CommRing R] [Ring A] [Algebra R A] [StarRing R] [StarRing A] [StarModule R A] (r : R) (a b c : A) (n : ℕ) : -b + star (algebraMap R A r) + a ^ n * c ∈ StarAlgebra.adjoin R {a, b, c} := by aesop example [Monoid M] (x : M) (n : ℕ) : x ^ n ∈ Submonoid.closure {x} := by aesop example [Monoid M] (x y z w : M) (n : ℕ) : (x * y) ^ n * w ∈ Submonoid.closure {x, y, z, w} := by aesop example [Group M] (x : M) (n : ℤ) : x ^ n ∈ Subgroup.closure {x} := by aesop example [Monoid M] (x y z : M) (S₁ S₂ : Submonoid M) (h : S₁ ≤ S₂) (hx : x ∈ S₁) (hy : y ∈ S₁) (hz : z ∈ S₂) : x * y * z ∈ S₂ := by aesop example [Monoid M] (x y z : M) (S₁ S₂ : Submonoid M) (h : S₁ ≤ S₂) (hx : x ∈ S₁) (hy : y ∈ S₁) (hz : z ∈ S₂) : x * y * z ∈ S₁ ⊔ S₂ := by aesop example [Monoid M] (x y z : M) (S : Submonoid M) (hxy : x * y ∈ S) (hz : z ∈ S) : z * (x * y) ∈ S := by aesop example [Field F] (S : Subfield F) (q : ℚ) : (q : F) ∈ S := by aesop example [Field F] (S : Subfield F) : (1.2 : F) ∈ S := by aesop example [Field F] (S : Subfield F) (x : F) (hx : x ∈ S) : ((12e-100 : F) • x⁻¹) ∈ S := by aesop
.lake/packages/mathlib/MathlibTest/norm_num_rpow.lean
import Mathlib.Analysis.SpecialFunctions.Pow.Real example : (2 : ℝ) ^ (3 : ℝ) = 8 := by norm_num1 example : (1 : ℝ) ^ (20 : ℝ) = 1 := by norm_num1 example : (-2 : ℝ) ^ (3 : ℝ) = -8 := by norm_num1 example : (1/5 : ℝ) ^ (2 : ℝ) = 1/25 := by norm_num1 example : (-1/3 : ℝ) ^ (-3 : ℝ) = -27 := by norm_num1 example : (1/2 : ℝ) ^ (-3 : ℝ) = 8 := by norm_num1 example : (2 : ℝ) ^ (-3 : ℝ) = 1/8 := by norm_num1 example : (-2 : ℝ) ^ (-3 : ℝ) = -1/8 := by norm_num1 example : (8 : ℝ) ^ (2 / 6 : ℝ) = 2 := by norm_num1 example : (0 : ℝ) ^ (1 / 3 : ℝ) = 0 := by norm_num1 example : (8 / 27 : ℝ) ^ (1 / 3 : ℝ) = 2 / 3 := by norm_num1 example : (8 : ℝ) ^ (-1 / 3 : ℝ) = 1 / 2 := by norm_num1 example : (8 / 27 : ℝ) ^ (-1 / 3 : ℝ) = 3 / 2 := by norm_num1 example : (1 / 27 : ℝ) ^ (-1 / 3 : ℝ) = 3 := by norm_num1 example : (0 : ℝ) ^ (0 : ℝ) = 1 := by norm_num1 example : (0 : ℝ) ^ (1 / 3 : ℝ) = 0 := by norm_num1 example : (0 : ℝ) ^ (-1 / 3 : ℝ) = 0 := by norm_num1 -- The following example is incorrect with the current Mathlib definition. /-- error: unsolved goals ⊢ (-8) ^ (1 / 3) = -2 -/ #guard_msgs in example : (-8 : ℝ) ^ (1 / 3 : ℝ) = -2 := by norm_num1
.lake/packages/mathlib/MathlibTest/Polynomial.lean
import Mathlib.Algebra.Polynomial.Basic import Mathlib.Algebra.Module.ULift open Polynomial def p0 : ℕ[X] := ⟨⟨{}, Pi.single 0 0, by intro; simp [Pi.single, Function.update_apply]⟩⟩ example : reprStr p0 = "0" := by native_decide example : reprStr (Option.some p0) = "some 0" := by native_decide example : (reprPrec p0 65).pretty = "0" := by native_decide def pX : ℕ[X] := ⟨⟨{1}, Pi.single 1 1, by intro; simp [Pi.single, Function.update_apply]⟩⟩ example : reprStr pX = "X" := by native_decide example : reprStr (Option.some pX) = "some X" := by native_decide def pXsq : ℕ[X] := ⟨⟨{2}, Pi.single 2 1, by intro; simp [Pi.single, Function.update_apply]⟩⟩ example : reprStr pXsq = "X ^ 2" := by native_decide example : reprStr (Option.some pXsq) = "some (X ^ 2)" := by native_decide example : (reprPrec pXsq 65).pretty = "X ^ 2" := by native_decide example : (reprPrec pXsq 70).pretty = "X ^ 2" := by native_decide example : (reprPrec pXsq 80).pretty = "(X ^ 2)" := by native_decide def p1 : ℕ[X] := ⟨⟨{1}, Pi.single 1 37, by intro; simp [Pi.single, Function.update_apply]⟩⟩ example : reprStr p1 = "C 37 * X" := by native_decide example : reprStr (Option.some p1) = "some (C 37 * X)" := by native_decide example : (reprPrec p1 65).pretty = "C 37 * X" := by native_decide example : (reprPrec p1 70).pretty = "(C 37 * X)" := by native_decide def p2 : ℕ[X] := ⟨⟨{0, 2}, Pi.single 0 57 + Pi.single 2 22, by intro; simp [Pi.single, Function.update_apply]; tauto⟩⟩ example : reprStr p2 = "C 57 + C 22 * X ^ 2" := by native_decide example : (reprPrec p2 65).pretty = "(C 57 + C 22 * X ^ 2)" := by native_decide -- test that parens are added inside `C` def pu1 : (ULift.{1} ℕ)[X] := ⟨⟨{1}, Pi.single 1 (ULift.up 37), by intro; simp [Pi.single, Function.update_apply, ←ULift.down_inj]⟩⟩ example : reprStr pu1 = "C (ULift.up 37) * X" := by native_decide
.lake/packages/mathlib/MathlibTest/Expr.lean
import Mathlib.Lean.Expr.ReplaceRec import Mathlib.Data.Nat.Notation import Lean.Elab.Command open Lean Meta Elab Command section replaceRec /-! Test the implementation of `Expr.replaceRec` -/ /-- Reorder the last two arguments of every function in the expression. (The resulting term will generally not be a type-correct) -/ def reorderLastArguments : Expr → Expr := Expr.replaceRec fun r e ↦ let n := e.getAppNumArgs if n ≥ 2 then mkAppN e.getAppFn <| e.getAppArgs.map r |>.swapIfInBounds (n - 1) (n - 2) else none def foo (f : ℕ → ℕ → ℕ) (n₁ n₂ n₃ n₄ : ℕ) : ℕ := f (f n₁ n₂) (f n₃ n₄) def bar (f : ℕ → ℕ → ℕ) (n₁ n₂ n₃ n₄ : ℕ) : ℕ := f (f n₄ n₃) (f n₂ n₁) set_option pp.unicode.fun true in /-- info: before: fun f n₁ n₂ n₃ n₄ ↦ f (f n₁ n₂) (f n₃ n₄) --- info: after: fun f n₁ n₂ n₃ n₄ ↦ f (f n₄ n₃) (f n₂ n₁) --- info: new type: (ℕ → ℕ → ℕ) → ℕ → ℕ → ℕ → ℕ → ℕ --- info: after: fun f n₁ n₂ n₃ n₄ ↦ f (f n₄ n₃) (f n₂ n₁) -/ #guard_msgs in run_cmd liftTermElabM <| do let d ← getConstInfo `foo let e := d.value! logInfo m!"before: {e}" let e := reorderLastArguments e logInfo m!"after: {e}" let t ← Meta.inferType e logInfo m!"new type: {t}" let d ← getConstInfo `bar logInfo m!"after: {d.value!}" guard <| e == d.value! end replaceRec
.lake/packages/mathlib/MathlibTest/FieldSimp.lean
import Mathlib.Tactic.Field import Mathlib.Tactic.Positivity import Mathlib.Tactic.Ring import Mathlib.Data.Real.Basic /-! # Tests for the `field_simp` and `field` tactics -/ private axiom test_sorry : ∀ {α}, α open Lean Elab Tactic in elab "test_field_simp" : tactic => do evalTactic <| ← `(tactic | field_simp) liftMetaFinishingTactic fun g ↦ do let ty ← g.getType logInfo ty g.assign (← Meta.mkAppOptM ``test_sorry #[ty]) set_option linter.unusedVariables false /-! ## Documenting "field_simp normal form" -/ section variable {P : ℚ → Prop} {x y z : ℚ} /-- error: field_simp made no progress on goal -/ #guard_msgs in example : P (1 : ℚ) := by test_field_simp /-! ### One atom -/ /-- info: P 1 -/ #guard_msgs in example : P (x ^ 0) := by test_field_simp /-- info: P x -/ #guard_msgs in example : P (x ^ 1) := by test_field_simp /-- error: field_simp made no progress on goal -/ #guard_msgs in example : P x := by test_field_simp /-- info: P (x ^ 2) -/ #guard_msgs in example : P (x ^ 2) := by test_field_simp /-- info: P (x ^ 3) -/ #guard_msgs in example : P (x ^ 1 * x ^ 2) := by test_field_simp /-- info: P (x ^ 2) -/ #guard_msgs in example : P (x * x) := by test_field_simp /-- info: P (x ^ 45) -/ #guard_msgs in example : P (x ^ 3 * x ^ 42) := by test_field_simp /-- info: P (x ^ k * x ^ 2) -/ #guard_msgs in example {k : ℤ} : P (x ^ k * x ^ 2) := by test_field_simp /-- info: P (1 / x ^ 3) -/ #guard_msgs in example : P (x ^ (-1 : ℤ) * x ^ (-2 : ℤ)) := by test_field_simp -- Cancellation: if x could be zero, we cannot cancel x * x⁻¹. /-- info: P (1 / x) -/ #guard_msgs in example : P (x⁻¹) := by test_field_simp /-- info: P (x / x) -/ #guard_msgs in example : P (x * x⁻¹) := by test_field_simp /-- info: P (x / x) -/ #guard_msgs in example : P (x⁻¹ * x) := by test_field_simp /-- info: P x -/ #guard_msgs in example : P (x * x * x⁻¹) := by test_field_simp /-- info: P (x / x) -/ #guard_msgs in example : P (x / x) := by test_field_simp /-- info: P x -/ #guard_msgs in example : P (x ^ 2 * x⁻¹) := by test_field_simp /-- info: P (x ^ 2) -/ #guard_msgs in example : P (x ^ 3 * x⁻¹) := by test_field_simp /-- info: P (1 / x ^ 3) -/ #guard_msgs in example : P (x / x ^ 4) := by test_field_simp /-- info: P (x ^ 6) -/ #guard_msgs in example : P ((x ^ (2:ℤ)) ^ 3) := by test_field_simp /-- info: P (1 / x ^ 6) -/ #guard_msgs in example : P ((x ^ (-2:ℤ)) ^ 3) := by test_field_simp -- Even if we cannot cancel, we can simplify the exponent. /-- info: P (x / x) -/ #guard_msgs in example : P (x ^ 2 * x ^ (-2 : ℤ)) := by test_field_simp /-- info: P (x / x) -/ #guard_msgs in example : P (x ^ (-37 : ℤ) * x ^ 37) := by test_field_simp -- If x is non-zero, we do cancel. /-- info: P 1 -/ #guard_msgs in example {hx : x ≠ 0} : P (x * x⁻¹) := by test_field_simp /-- info: P 1 -/ #guard_msgs in example {hx : x ≠ 0} : P (x⁻¹ * x) := by test_field_simp /-- info: P 1 -/ #guard_msgs in example {hx : x ≠ 0} : P (x ^ (-17 : ℤ) * x ^ 17) := by test_field_simp /-- info: P 1 -/ #guard_msgs in example {hx : x ≠ 0} : P (x ^ 17 / x ^ 17) := by test_field_simp /-- info: P 1 -/ #guard_msgs in example {hx : x ≠ 0} : P (x / x) := by test_field_simp /-- info: P (x ^ 2) -/ #guard_msgs in example {hx : x ≠ 0} : P (x ^ 3 * x⁻¹) := by test_field_simp /-- info: P (1 / x ^ 3) -/ #guard_msgs in example {hx : x ≠ 0} : P (x / x ^ 4) := by test_field_simp -- When a term is subtracted from itself, -- we normalize to the product of the common factor with a difference of constants. -- The old (pre-2025) `field_simp` implementation subsumed `simp`, -- so it would clean up such terms further. -- But such simplifications are not necessarily in scope for `field_simp`. /-- info: P (x * (1 - 1)) -/ #guard_msgs in example : P (x - x) := by test_field_simp /-- info: P (x * (-1 + 1)) -/ #guard_msgs in example : P (-x + x) := by test_field_simp /-- info: P (x * (1 - 1)) -/ #guard_msgs in example : P (1 * x - x) := by test_field_simp /-- info: P (2 * (1 - 1) * x) -/ #guard_msgs in example : P ((2 - 2) * x) := by test_field_simp /-- info: P (↑a * x * (1 - 1)) -/ #guard_msgs in example {a : Nat} : P (a* x - a * x) := by test_field_simp /-! ### Two atoms -/ /-- error: field_simp made no progress on goal -/ #guard_msgs in example : P (x + y) := by test_field_simp /-- info: P (x * y) -/ #guard_msgs in example : P (x * y) := by test_field_simp /-- info: P (1 / (x * y)) -/ #guard_msgs in example : P ((x * y)⁻¹) := by test_field_simp /-- info: P (x * y / (x * y)) -/ #guard_msgs in example : P ((x * y) / (y * x)) := by test_field_simp /-- info: P (x * y * (1 + 1)) -/ #guard_msgs in example : P (x * y + y * x) := by test_field_simp /-- info: P (x * (y + 1)) -/ #guard_msgs in example : P (x * y + x * 1) := by test_field_simp /-- info: P (x * y / (x * y)) -/ #guard_msgs in example : P ((x * y) * (y * x)⁻¹) := by test_field_simp /-- info: P y -/ #guard_msgs in example : P (x ^ (0:ℤ) * y) := by test_field_simp /-- info: P (y ^ 2) -/ #guard_msgs in example : P (y * (y + x) ^ (0:ℤ) * y) := by test_field_simp /-- info: P (x / y) -/ #guard_msgs in example : P (x / y) := by test_field_simp /-- info: P (-(x / y)) -/ #guard_msgs in example : P (x / -y) := by test_field_simp /-- info: P (-(x / y)) -/ #guard_msgs in example : P (-x / y) := by test_field_simp /-- info: P ((x + (x + 1) * y / (y + 1)) / (x + 1)) -/ #guard_msgs in example (hx : x + 1 ≠ 0) : P (x / (x + 1) + y / (y + 1)) := by test_field_simp /-- info: P ((x * (y + 1) + (x + 1) * y) / ((x + 1) * (y + 1))) -/ #guard_msgs in example (hx : 0 < x + 1) (hy : 0 < y + 1) : P (x / (x + 1) + y / (y + 1)) := by test_field_simp /-- info: P (x ^ 2 / y) -/ #guard_msgs in example : P (x / (y / x)) := by test_field_simp /-- info: P (x ^ 2 * y ^ 3) -/ #guard_msgs in example : P (x / (y ^ (-3:ℤ) / x)) := by test_field_simp /-- info: P (x ^ 2 * y ^ 3) -/ #guard_msgs in example : P ((x / y ^ (-3:ℤ)) * x) := by test_field_simp /-- info: P (x ^ 3 * y ^ 4) -/ #guard_msgs in example : P (x ^ 1 * y * x ^ 2 * y ^ 3) := by test_field_simp /-- info: P (x ^ 3 * y / y) -/ #guard_msgs in example : P (x ^ 1 * y * x ^ 2 * y⁻¹) := by test_field_simp /-- info: P (1 / y) -/ #guard_msgs in example (hx : x ≠ 0) : P (x / (x * y)) := by test_field_simp /-- info: P 1 -/ #guard_msgs in example (hx : x ≠ 0) (hy : y ≠ 0) : P ((x * y) / (y * x)) := by test_field_simp /-- info: P 1 -/ #guard_msgs in example (hx : x ≠ 0) (hy : y ≠ 0) : P ((x * y) * (y * x)⁻¹) := by test_field_simp /-- info: P (x ^ 3) -/ #guard_msgs in example (hy : y ≠ 0) : P (x ^ 1 * y * x ^ 2 * y⁻¹) := by test_field_simp /-- info: P (x / (1 - y + y)) -/ #guard_msgs in example (hy : 1 - y ≠ 0): P (x / (1 - y) / (1 + y / (1 - y))) := by test_field_simp -- test `conv` tactic example (hy : 1 - y ≠ 0) : P (x / (1 - y) / (1 + y / (1 - y))) := by conv => enter [1]; field_simp guard_target = P (x / (1 - y + y)) exact test_sorry /-! ### Three atoms -/ /-- info: P (x * y * z) -/ #guard_msgs in example : P (x * y * z) := by test_field_simp /-- info: P (x * (y + z)) -/ #guard_msgs in example : P (x * y + x * z) := by test_field_simp /-- info: P (1 / (y + z)) -/ #guard_msgs in example (hx : x ≠ 0) : P (x / (x * y + x * z)) := by test_field_simp /-- info: P (x / (x * (y + z))) -/ #guard_msgs in example : P (x / (x * y + x * z)) := by test_field_simp /-! ### Constants and addition/subtraction -/ -- We do not simplify literals. /-- info: P (x * (2 - 1)) -/ #guard_msgs in example : P (2 * x - 1 * x) := by test_field_simp /-- info: P (x * (2 - 1 - 1)) -/ #guard_msgs in example : P (2 * x - x - x) := by test_field_simp /-- info: P (x * (2 - 1)) -/ #guard_msgs in example : P (2 * x - x) := by test_field_simp /-- info: P (x * (3 - 2 - 1)) -/ #guard_msgs in example : P ((3 - 2) * x - x) := by test_field_simp -- There is no special handling of zero, -- in particular multiplication with a zero literal is not simplified. /-- info: P (0 * x) -/ #guard_msgs in example : P (0 * x) := by test_field_simp /-- info: P (0 * (x * y + 1)) -/ #guard_msgs in example : P (0 * x * y + 0) := by test_field_simp /-- info: P (x * y * (1 - 1) * z) -/ #guard_msgs in example : P ((x * y - y * x) * z) := by test_field_simp -- Iterated negation is simplified. /-- info: P x -/ #guard_msgs in example : P (-(-x)) := by test_field_simp -- Subtraction from zero is not simplified. /-- info: P (0 - (0 + -x)) -/ #guard_msgs in example : P (0 -(0 + (-x))) := by test_field_simp /-! ### Transparency As is standard in Mathlib tactics for algebra, `field_simp` respects let-bindings and identifies atoms only up to reducible defeq. -/ /-- info: P (x * (y + a)) -/ #guard_msgs in example : True := by let a := y suffices P (x * y + x * a) from test_sorry test_field_simp /-- info: P (x * y * (1 + 1)) -/ #guard_msgs in example : P (x * y + x * (fun t ↦ t) y) := by test_field_simp /-- info: P (x * (y + id y)) -/ #guard_msgs in example : P (x * y + x * id y) := by test_field_simp end /-! ## Cancel denominators from equalities -/ /-! ### Finishing tactic The `field` tactic is a finishing tactic for equalities in fields. Effectively it runs `field_simp` to clear denominators, then hands the result to `ring1`. -/ example : (1:ℚ) / 3 + 1 / 6 = 1 / 2 := by field example {x : ℚ} (hx : x ≠ 0) : x * x⁻¹ = 1 := by field example {a b : ℚ} (h : b ≠ 0) : a / b + 2 * a / b + (-a) / b + (- (2 * a)) / b = 0 := by field -- example from the `field` docstring example {x y : ℚ} (hx : x + y ≠ 0) : x / (x + y) + y / (x + y) = 1 := by field example {x : ℚ} : x ^ 2 / (x ^ 2 + 1) + 1 / (x ^ 2 + 1) = 1 := by field example {x y : ℚ} (hx : 0 < x) : ((x ^ 2 - y ^ 2) / (x ^ 2 + y ^ 2)) ^ 2 + (2 * x * y / (x ^ 2 + y ^ 2)) ^ 2 = 1 := by field example {K : Type*} [Field K] (a b c d x y : K) (hx : x ≠ 0) (hy : y ≠ 0) : a + b / x + c / x ^ 2 + d / x ^ 3 = a + x⁻¹ * (y * b / y + (d / x + c) / x) := by field -- example from the `field` docstring example {a b : ℚ} (ha : a ≠ 0) : a / (a * b) - 1 / b = 0 := by field example {x : ℚ} : x ^ 2 * x⁻¹ = x := by field -- example from `field` docstring example {K : Type*} [Field K] (hK : ∀ x : K, x ^ 2 + 1 ≠ 0) (x : K) : 1 / (x ^ 2 + 1) + x ^ 2 / (x ^ 2 + 1) = 1 := by field [hK] -- testing that mdata is cleared before parsing goal example {x : ℚ} (hx : x ≠ 0) : x * x⁻¹ = 1 := by have : 1 = 1 := rfl field -- `field` will suggest `field_simp` on failure, if `field_simp` does anything. /-- info: Try this: [apply] field_simp --- error: unsolved goals x y z : ℚ hx : x + y ≠ 0 ⊢ 1 = z -/ #guard_msgs in example {x y z : ℚ} (hx : x + y ≠ 0) : x / (x + y) + y / (x + y) = z := by field -- If `field` fails but `field_simp` also fails, we just throw an error. /-- error: ring failed, ring expressions not equal x y z : ℚ ⊢ x + y = z -/ #guard_msgs in example {x y z : ℚ} : x + y = z := by field /- The `field` tactic differs slightly from `field_simp; ring1` in that it clears denominators only at the top level, not recursively in subexpressions. (`ring1` acts only at the top level, so for consistency we also clear denominators only at the top level.) -/ /-- info: Try this: [apply] field_simp --- error: unsolved goals a b : ℚ f : ℚ → ℚ ⊢ f (a * b) * (1 - 1) = 0 -/ #guard_msgs in example (a b : ℚ) (f : ℚ → ℚ) : f (a ^ 2 * b / a) - f (b ^ 2 * a / b) = 0 := by field -- (Compare with the example above: this is out of scope for `field`.) example (a b : ℚ) (f : ℚ → ℚ) : f (a ^ 2 * b / a) - f (b ^ 2 * a / b) = 0 := by field_simp ring1 /-! ### Mid-proof use -/ example {K : Type*} [Semifield K] (x y : K) (hy : y + 1 ≠ 0) : 2 * x / (y + 1) = x := by field_simp guard_target = 2 * x = x * (y + 1) exact test_sorry example {K : Type*} [Semifield K] (x y : K) : 2 * x / (y + 1) = x := by have hy : y + 1 ≠ 0 := test_sorry -- test mdata, context updating, etc field_simp guard_target = 2 * x = x * (y + 1) exact test_sorry example {x y z w : ℚ} (h : x / y = z / w) (hy : y ≠ 0) (hw : w ≠ 0) : True := by field_simp at h guard_hyp h : x * w = y * z exact trivial example {K : Type*} [Field K] (x y z : K) (hy : 1 - y ≠ 0) : x / (1 - y) / (1 + y / (1 - y)) = z := by field_simp guard_target = x = (1 - y + y) * z exact test_sorry example {K : Type*} [Field K] (x y z : K) (hy : 1 - y ≠ 0) : x / (1 - y) / (1 + y / (1 - y)) = z := by simp [field] guard_target = x = z exact test_sorry example {x y z : ℚ} (hy : y ≠ 0) (hz : z ≠ 0) : x / y = x / z := by field_simp guard_target = x * z = x * y exact test_sorry example {x y z : ℚ} (hy : y ≠ 0) (hz : z ≠ 0) (hx : x ≠ 0) : x / y = x / z := by field_simp guard_target = z = y exact test_sorry example {x y z : ℚ} (h : x * y = x * z) : True := by field_simp at h guard_hyp h : x * y = x * z exact trivial example {x y a b : ℚ} (hx : 0 < x) (hy : 0 < y) (ha : 0 < a) (hb : 0 < b) : (a * x + b * y)⁻¹ ≤ a * x⁻¹ + b * y⁻¹ := by field_simp guard_target = x * y ≤ (a * x + b * y) * (a * y + x * b) exact test_sorry -- vary `<` vs `≤`, `≥` vs `≤` example {x y a b : ℚ} (hx : x > 0) (hy : 0 < y) (ha : 0 ≤ a) (hb : 0 < b) : a * x⁻¹ + b * y⁻¹ ≥ (a * x + b * y)⁻¹ := by field_simp guard_target = x * y ≤ (a * x + b * y) * (a * y + x * b) exact test_sorry example {x y a b : ℚ} (hx : 0 < x) (hy : 0 < y) (ha : 0 < a) (hb : 0 < b) : (a * x + b * y)⁻¹ < a * x⁻¹ + b * y⁻¹ := by field_simp guard_target = x * y < (a * x + b * y) * (a * y + x * b) exact test_sorry example {x y : ℚ} (hx : 0 < x) : ((x ^ 2 - y ^ 2) / (x ^ 2 + y ^ 2)) ^ 2 + (2 * x * y / (x ^ 2 + y ^ 2)) ^ 2 ≤ 1 := by field_simp guard_target = (x ^ 2 - y ^ 2) ^ 2 + x ^ 2 * y ^ 2 * 2 ^ 2 ≤ (x ^ 2 + y ^ 2) ^ 2 exact test_sorry example {x y : ℚ} (hx : 0 < x) : ((x ^ 2 - y ^ 2) / (x ^ 2 + y ^ 2)) ^ 2 + (2 * x * y / (x ^ 2 + y ^ 2)) ^ 2 ≤ 1 := by simp only [field] guard_target = (x ^ 2 - y ^ 2) ^ 2 + x ^ 2 * y ^ 2 * 2 ^ 2 ≤ (x ^ 2 + y ^ 2) ^ 2 exact test_sorry example {x y : ℚ} (hx : 0 < x) : ((x ^ 2 - y ^ 2) / (x ^ 2 + y ^ 2)) ^ 2 + (2 * x * y / (x ^ 2 + y ^ 2)) ^ 2 < 1 := by field_simp guard_target = (x ^ 2 - y ^ 2) ^ 2 + x ^ 2 * y ^ 2 * 2 ^ 2 < (x ^ 2 + y ^ 2) ^ 2 exact test_sorry example {x y : ℚ} (hx : 0 < x) : ((x ^ 2 - y ^ 2) / (x ^ 2 + y ^ 2)) ^ 2 + (2 * x * y / (x ^ 2 + y ^ 2)) ^ 2 < 1 := by simp only [field] guard_target = (x ^ 2 - y ^ 2) ^ 2 + x ^ 2 * y ^ 2 * 2 ^ 2 < (x ^ 2 + y ^ 2) ^ 2 exact test_sorry -- used in `field_simp` docstring example {K : Type*} [Field K] {x : K} (hx : x ^ 5 = 1) (hx0 : x ≠ 0) (hx1 : x - 1 ≠ 0) : (x + 1 / x) ^ 2 + (x + 1 / x) = 1 := by field_simp guard_target = (x ^ 2 + 1) * (x ^ 2 + 1 + x) = x ^ 2 calc (x ^ 2 + 1) * (x ^ 2 + 1 + x) = (x ^ 5 - 1) / (x - 1) + x ^ 2 := by field _ = x ^ 2 := by simp [hx] -- used in `field` simproc-set docstring example {K : Type*} [Field K] {x : K} (hx : x ^ 5 = 1) (hx0 : x ≠ 0) (hx1 : x - 1 ≠ 0) : (x + 1 / x) ^ 2 + (x + 1 / x) = 1 := by simp only [field] guard_target = (x ^ 2 + 1) * (x ^ 2 + 1 + x) = x ^ 2 calc (x ^ 2 + 1) * (x ^ 2 + 1 + x) = (x ^ 5 - 1) / (x - 1) + x ^ 2 := by field _ = x ^ 2 := by simp [hx] section -- TODO (new implementation): do we want `field_simp` to reduce this to `⊢ x * y = z * y ^ 2`? -- Or perhaps to `⊢ x / y / y = z / y`? example {x y z : ℚ} : x / y ^ 2 = z / y := by field_simp guard_target = x / y ^ 2 = z / y exact test_sorry -- why the first idea could work example {x y z : ℚ} : (x / y ^ 2 = z / y) ↔ (x * y = z * y ^ 2) := by obtain rfl | hy := eq_or_ne y 0 · simp field_simp -- why the second idea could work example {x y z : ℚ} : (x / y ^ 2 = z / y) ↔ (x / y / y = z / y) := by obtain rfl | hy := eq_or_ne y 0 · simp ring_nf end /-! Sometimes it takes iterated alternation betweeen `ring_nf` and `field_simp` in order to normalize properly. It is not clear whether or not this iterated alternation always achieves the "obvious" normalization eventually. Nor is it clear whether, if so, there are any bounds on how many iterations are needed. -/ -- modified from 2021 American Mathematics Competition 12B, problem 9 section example (P : ℝ → Prop) {x y : ℝ} (hx : 0 < x) (hy : 0 < y) : P ((4 * x + y) / x / (x / (3 * x + y)) - (5 * x + y) / x / (x / (2 * x + y))) := by ring_nf fail_if_success (guard_target = P 2) -- this simply records current behaviour, delete if needed field_simp fail_if_success (guard_target = P 2) -- this simply records current behaviour, delete if needed ring_nf fail_if_success (guard_target = P 2) -- this simply records current behaviour, delete if needed field_simp guard_target = P 2 exact test_sorry example (P : ℝ → Prop) {x y : ℝ} (hx : 0 < x) (hy : 0 < y) : P ((4 * x + y) / x / (x / (3 * x + y)) - (5 * x + y) / x / (x / (2 * x + y))) := by field_simp fail_if_success (guard_target = P 2) -- this simply records current behaviour, delete if needed ring_nf fail_if_success (guard_target = P 2) -- this simply records current behaviour, delete if needed field_simp guard_target = P 2 exact test_sorry end section -- This example is used in the `field` docstring. example {a b : ℚ} (H : b + a ≠ 0) : a / (a + b) + b / (b + a) = 1 := by ring_nf at * field /-- info: Try this: [apply] field_simp --- error: unsolved goals a b : ℚ H : b + a ≠ 0 ⊢ (a + b) / (a + b) = 1 -/ #guard_msgs in example {a b : ℚ} (H : b + a ≠ 0) : a / (a + b) + b / (b + a) = 1 := by ring_nf field /-- info: Try this: [apply] field_simp --- error: unsolved goals a b : ℚ H : b + a ≠ 0 ⊢ a * (b + a) / (a + b) + b = b + a -/ #guard_msgs in example {a b : ℚ} (H : b + a ≠ 0) : a / (a + b) + b / (b + a) = 1 := by field example {a b : ℚ} (H : a + b + 1 ≠ 0) : a / (a + (b + 1) ^ 2 / (b + 1)) + (b + 1) / (b + a + 1) = 1 := by field_simp ring_nf at * field /-- info: Try this: [apply] field_simp --- error: unsolved goals a b : ℚ H : 1 + a + b ≠ 0 ⊢ a * (1 + a + b) / (a + b * 2 / (1 + b) + b ^ 2 / (1 + b) + 1 / (1 + b)) + b + 1 = 1 + a + b -/ #guard_msgs in example {a b : ℚ} (H : a + b + 1 ≠ 0) : a / (a + (b + 1) ^ 2 / (b + 1)) + (b + 1) / (b + a + 1) = 1 := by ring_nf at * field /-- info: Try this: [apply] field_simp --- error: unsolved goals a b : ℚ H : a + b + 1 ≠ 0 ⊢ a / (a + (b + 1)) + (b + 1) / (b + a + 1) = 1 -/ #guard_msgs in example {a b : ℚ} (H : a + b + 1 ≠ 0) : a / (a + (b + 1) ^ 2 / (b + 1)) + (b + 1) / (b + a + 1) = 1 := by field end /-! From PluenneckeRuzsa: new `field_simp` doesn't handle variable exponents -/ example (x y : ℚ≥0) (n : ℕ) (hx : x ≠ 0) : y * ((y / x) ^ n * x) = (y / x) ^ (n + 1) * x * x := by field_simp guard_target = y * (y / x) ^ n = x * (y / x) ^ (n + 1) exact test_sorry example (x y : ℚ≥0) (n : ℕ) (hx : x ≠ 0) : y * ((y / x) ^ n * x) = (y / x) ^ (n + 1) * x * x := by simp [field, pow_add] /- /-- Specify a simp config. -/ -- this feature was dropped in the August 2025 `field_simp` refactor example (x : ℚ) (h₀ : x ≠ 0) : (4 / x)⁻¹ * ((3 * x ^ 3) / x) ^ 2 * ((1 / (2 * x))⁻¹) ^ 3 = 18 * x ^ 8 := by fail_if_success field_simp (maxSteps := 0) field_simp (config := {}) ring -/ /-! ### check that `field_simp` closes goals when the equality reduces to an identity -/ example {x y : ℚ} (h : x + y ≠ 0) : x / (x + y) + y / (x + y) = 1 := by field_simp example {x : ℚ} (hx : x ≠ 0) : x * x⁻¹ = 1 := by field_simp example {x : ℚ} : x ^ 2 * x⁻¹ = x := by field_simp /-! TODO: cancel denominators from disequalities and inequalities -/ -- example (hx : x ≠ 0) (h : x ^ 2 * x⁻¹ ≠ x) : True := by field_simp at h /-! ## Tactic operating recursively -/ example {x y : ℚ} (hx : y ≠ 0) {f : ℚ → ℚ} (hf : ∀ t, f t ≠ 0) : f (x * y / y) / f (x / y * y) = 1 := by field_simp [hf] -- test for consistent atom ordering across subterms example {x y : ℚ} (hx : y ≠ 0) {f : ℚ → ℚ} (hf : ∀ t, f t ≠ 0) : f (y * x * y / y) / f (x * y / y * y) = 1 := by field_simp [hf] example {x y z : ℚ} (hx : y ≠ 0) {f : ℚ → ℚ} (hf : ∀ t, f t ≠ 0) : f (y * x / (y ^ 2 / z)) / f (z / (y / x)) = 1 := by field_simp [hf] open Finset in example (n : ℕ) : ∏ i ∈ range n, (1 - (i + 2 : ℚ)⁻¹) < 1 := by field_simp guard_target = ∏ x ∈ range n, ((x:ℚ) + 2 - 1) / (x + 2) < 1 exact test_sorry /-! ## Performance -/ -- from `InnerProductGeometry.cos_angle_sub_add_angle_sub_rev_eq_neg_cos_angle` -- old implementation: 19983 heartbeats!!! -- new implementation: 2979 heartbeats example {V : Type*} [AddCommGroup V] (F : V → ℚ) {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) (hxn : F x ≠ 0) (hyn : F y ≠ 0) (hxyn : F (x - y) ≠ 0) : (F x * F x - (F x * F x + F y * F y - F (x - y) * F (x - y)) / 2) / (F x * F (x - y)) * ((F y * F y - (F x * F x + F y * F y - F (x - y) * F (x - y)) / 2) / (F y * F (x - y))) * F x * F y * F (x - y) * F (x - y) - (F x * F x * (F y * F y) - (F x * F x + F y * F y - F (x - y) * F (x - y)) / 2 * ((F x * F x + F y * F y - F (x - y) * F (x - y)) / 2)) = -((F x * F x + F y * F y - F (x - y) * F (x - y)) / 2 / (F x * F y)) * F x * F y * F (x - y) * F (x - y) := by field_simp exact test_sorry /-! ## Discharger -/ /-- Test that the discharger can clear nontrivial denominators in ℚ. -/ example (x : ℚ) (h₀ : x ≠ 0) : (4 / x)⁻¹ * ((3 * x ^ 3) / x) ^ 2 * ((1 / (2 * x))⁻¹) ^ 3 = 18 * x ^ 8 := by field_simp ring /-- Use a custom discharger -/ example (x : ℚ) (h₀ : x ≠ 0) : (4 / x)⁻¹ * ((3 * x ^ 3) / x) ^ 2 * ((1 / (2 * x))⁻¹) ^ 3 = 18 * x ^ 8 := by field_simp (discharger := simp; assumption) ring /-- warning: Custom `field_simp` dischargers do not make use of the `field_simp` arguments list -/ #guard_msgs in example (x : ℚ) (h₀ : x ≠ 0) : (4 / x)⁻¹ * ((3 * x ^ 3) / x) ^ 2 * ((1 / (2 * x))⁻¹) ^ 3 = 18 * x ^ 8 := by field_simp (discharger := simp; assumption) [h₀] ring -- mimic discharger example {K : Type*} [Field K] (n : ℕ) (w : K) (h0 : w ≠ 0) : w ^ n ≠ 0 := by simp [h0] example {K : Type*} [Field K] (n : ℕ) (w : K) (h0 : w ≠ 0) : w ^ n / w ^ n = n := by field_simp guard_target = (1 : K) = n exact test_sorry section variable {K : Type*} [Field K] [LinearOrder K] [IsStrictOrderedRing K] -- mimic discharger example (hK : ∀ ξ : K, ξ + 1 ≠ 0) (x : K) : |x + 1| ≠ 0 := by simp [hK x] example (hK : ∀ ξ : K, ξ + 1 ≠ 0) (x : K) : 1 / |x + 1| = 5 := by field_simp [hK x] guard_target = 1 = |x + 1| * 5 exact test_sorry /-! the `positivity` part of the discharger can't take help from user-provided terms -/ -- mimic discharger /-- error: failed to prove positivity/nonnegativity/nonzeroness -/ #guard_msgs in example (hK : ∀ ξ : K, 0 < ξ + 1) (x : K) : x + 1 ≠ 0 := by positivity /-- error: unsolved goals K : Type u_1 inst✝² : Field K inst✝¹ : LinearOrder K inst✝ : IsStrictOrderedRing K hK : ∀ (ξ : K), 0 < ξ + 1 x : K ⊢ 1 / (x + 1) = 5 -/ #guard_msgs in example (hK : ∀ ξ : K, 0 < ξ + 1) (x : K) : 1 / (x + 1) = 5 := by field_simp [hK x] -- works when the hypothesis is brought out for use by `positivity` example (hK : ∀ ξ : K, 0 < ξ + 1) (x : K) : 1 / (x + 1) = 5 := by have := hK x field_simp guard_target = 1 = (x + 1) * 5 exact test_sorry /-- Test that the discharger can handle some casting -/ example (n : ℕ) (h : n ≠ 0) : 1 / (n : K) * n = 1 := by field_simp /-- Test that the discharger can handle some casting -/ example (n : ℕ) (h : n ≠ 0) : 1 / (n : ℝ) * n = 1 := by field_simp -- Minimised from Fourier/AddCircle.lean example (n : ℕ) (T : ℝ) {hT : T ≠ 0} (hn : n ≠ 0) {a : ℝ} : (2 * a / T * (n * (T / 2 / n))) = a := by field_simp end /- Bug (some would say "feature") of the old implementation: the implementation used the `field_simp` discharger on the side conditions of other simp-lemmas, not just the `field_simp` simp set. Such behaviour can be invoked in the new implementation by running `simp` with the `field_simp` simprocs and a discharger. -/ example (m n : ℕ) (h : m ≤ n) (hm : (2:ℚ) < n - m) : (n:ℚ) / (n - m) = 1 / ↑(n - m) * n := by simp [field] guard_target = (n:ℚ) = ↑n * (↑n - ↑m) / ↑(n - m) exact test_sorry example (m n : ℕ) (h : m ≤ n) (hm : (2:ℚ) < n - m) : (n:ℚ) / (n - m) = 1 / ↑(n - m) * n := by simp (disch := assumption) [field] /-! ### Non-confluence issues We need to ensure that the "normal form" of the simproc `field` does not conflict with the direction of any Mathlib simp-lemmas, otherwise we can get infinite loops. -/ -- Mathlib simp-lemmas `neg_mul` and `mul_neg` example {K : Type*} [Field K] {a b c x : K} : -(c * a * x) + -b = 7 := by simp [field] fail_if_success rw [neg_mul] fail_if_success rw [mul_neg] exact test_sorry -- Mathlib simp-lemma `one_div` example (a b : ℚ) : a * b⁻¹ = 7 := by simp [field] fail_if_success rw [one_div] exact test_sorry -- Mathlib simp-lemma `mul_inv_rev` -- from `Analysis.SpecialFunctions.Stirling` example (m n : ℚ) : (m * n)⁻¹ = 7 := by simp [field] fail_if_success rw [mul_inv_rev] exact test_sorry -- undiagnosed non-confluence -- from `LinearAlgebra.QuadraticForm.Real` /-- error: Tactic `simp` failed with a nested error: maximum recursion depth has been reached use `set_option maxRecDepth <num>` to increase limit use `set_option diagnostics true` to get diagnostic information -/ #guard_msgs in example {t : ℚ} (ht : t ≠ 0) (a : ∀ t, t ≠ 0 → ℚ) : (if h : t = 0 then 1 else a t h) = 1 := by simp only [field] /-! ## Units of a ring, partial division This feature was dropped in the August 2025 `field_simp` refactor. -/ /- /- Check that `field_simp` works for units of a ring. -/ section CommRing variable {R : Type*} [CommRing R] (a b c d e f g : R) (u₁ u₂ : Rˣ) /-- Check that `divp_add_divp_same` takes priority over `divp_add_divp`. -/ example : a /ₚ u₁ + b /ₚ u₁ = (a + b) /ₚ u₁ := by field_simp /-- Check that `divp_sub_divp_same` takes priority over `divp_sub_divp`. -/ example : a /ₚ u₁ - b /ₚ u₁ = (a - b) /ₚ u₁ := by field_simp /- Combining `eq_divp_iff_mul_eq` and `divp_eq_iff_mul_eq`. -/ example : a /ₚ u₁ = b /ₚ u₂ ↔ a * u₂ = b * u₁ := by field_simp /-- Making sure inverses of units are rewritten properly. -/ example : ↑u₁⁻¹ = 1 /ₚ u₁ := by field_simp /-- Checking arithmetic expressions. -/ example : (f - (e + c * -(a /ₚ u₁) * b + d) - g) = (f * u₁ - (e * u₁ + c * (-a) * b + d * u₁) - g * u₁) /ₚ u₁ := by field_simp /-- Division of units. -/ example : a /ₚ (u₁ / u₂) = a * u₂ /ₚ u₁ := by field_simp example : a /ₚ u₁ /ₚ u₂ = a /ₚ (u₂ * u₁) := by field_simp end CommRing -/ /-! ## Algebraic structures weaker than `Field` -/ example {K : Type} [CommGroupWithZero K] {x y : K} : y / x * x ^ 3 * y ^ 3 = x ^ 2 * y ^ 5 / y := by field_simp example {K : Type} [Semifield K] {x y : K} (h : x + y ≠ 0) : x / (x + y) + y / (x + y) = 1 := by field_simp -- Extracted from `Analysis/SpecificLimits/Basic.lean` -- `field_simp` assumes commutativity: in its absence, it does nothing. /-- error: field_simp made no progress on goal -/ #guard_msgs in example {K : Type*} [DivisionRing K] {n' x : K} (h : n' ≠ 0) (h' : n' + x ≠ 0) : 1 / (1 + x / n') = n' / (n' + x) := by field_simp -- For comparison: the same test passes when working over a field. example {K : Type*} [Field K] {n' x : K} (hn : n' ≠ 0) : 1 / (1 + x / n') = n' / (n' + x) := by field_simp
.lake/packages/mathlib/MathlibTest/hint.lean
import Mathlib.Tactic.Hint /-- error: No suggestions available -/ #guard_msgs in example (h : 1 < 0) : False := by hint register_hint 1000 trivial /-- info: Try these: [apply] 🎉 trivial -/ #guard_msgs in example (h : 1 < 0) : False := by hint register_hint 1001 contradiction /-- info: Try these: [apply] 🎉 contradiction -/ #guard_msgs in example (h : 1 < 0) : False := by hint register_hint 999 exact? /-- info: Try these: [apply] 🎉 contradiction -/ #guard_msgs in example (h : 1 < 0) : False := by hint register_hint 1002 exact? /-- info: Try these: [apply] 🎉 exact Nat.not_succ_le_zero 1 h -/ #guard_msgs in example (h : 1 < 0) : False := by hint
.lake/packages/mathlib/MathlibTest/Qify.lean
import Mathlib.Algebra.Order.Field.Rat import Mathlib.Data.Int.CharZero import Mathlib.Tactic.Qify example (a b : ℕ) : (a : ℚ) ≤ b ↔ a ≤ b := by qify example (a b : ℕ) : (a : ℚ) < b ↔ a < b := by qify example (a b : ℕ) : (a : ℚ) = b ↔ a = b := by qify example (a b : ℕ) : (a : ℚ) ≠ b ↔ a ≠ b := by qify example (a b : ℤ) : (a : ℚ) ≤ b ↔ a ≤ b := by qify example (a b : ℤ) : (a : ℚ) < b ↔ a < b := by qify example (a b : ℤ) : (a : ℚ) = b ↔ a = b := by qify example (a b : ℤ) : (a : ℚ) ≠ b ↔ a ≠ b := by qify example (a b : ℚ≥0) : (a : ℚ) ≤ b ↔ a ≤ b := by qify example (a b : ℚ≥0) : (a : ℚ) < b ↔ a < b := by qify example (a b : ℚ≥0) : (a : ℚ) = b ↔ a = b := by qify example (a b : ℚ≥0) : (a : ℚ) ≠ b ↔ a ≠ b := by qify example (a b c : ℕ) (h : a - b = c) (hab : b ≤ a) : a = c + b := by qify [hab] at h ⊢ -- `zify` does the same thing here. exact sub_eq_iff_eq_add.1 h example (a b c : ℚ≥0) (h : a - b = c) (hab : b ≤ a) : a = c + b := by qify [hab] at h ⊢ exact sub_eq_iff_eq_add.1 h example (a b c : ℤ) (h : a / b = c) (hab : b ∣ a) (hb : b ≠ 0) : a = c * b := by qify [hab] at h hb ⊢ exact (div_eq_iff hb).1 h -- Regression test for https://github.com/leanprover-community/mathlib4/issues/7480 example (a b c : ℕ) (h : a / b = c) (hab : b ∣ a) (hb : b ≠ 0) : a = c * b := by qify [hab] at h hb ⊢ exact (div_eq_iff hb).1 h
.lake/packages/mathlib/MathlibTest/FinCoercions.lean
-- We verify that after importing Mathlib, -- we have not introduced a global coercion from `Nat` to `Fin n`. -- Such coercions introduce unexpected invisible wrap-around arithmetic. -- `open Fin.CommRing ...` *does* introduce such a coercion. import Mathlib set_option pp.mvars false -- We first verify that there is no global coercion from `Nat` to `Fin n`. -- Such a coercion would frequently introduce unexpected modular arithmetic. /-- error: Type mismatch n has type ℕ but is expected to have type Fin 3 --- info: fun n => sorry : (n : ℕ) → ?_ n -/ #guard_msgs in #check fun (n : Nat) => (n : Fin 3) -- This coercion is available via `open Fin.CommRing in ...` section open Fin.CommRing variable (m : Nat) (n : Fin 3) /-- info: n < ↑m : Prop -/ #guard_msgs in #check n < m end example (x : Fin (n + 1)) (h : x < n) : Fin (n + 1) := x.succ.castLT (by simp [h])
.lake/packages/mathlib/MathlibTest/Subsingleton.lean
import Mathlib.Tactic.Subsingleton private axiom test_sorry : ∀ {α}, α /-! Basic subsingleton instances -/ example (x y : Unit) : x = y := by subsingleton example (x y : Empty) : x = y := by subsingleton example {α : Type} [Subsingleton α] (x y : α) : x = y := by subsingleton /-! Proof irrelevance -/ example (p : Prop) (h h' : p) : h = h' := by subsingleton /-! HEq proof irrelevance -/ example (p q : Prop) (h : p) (h' : q) : h ≍ h' := by subsingleton /-! Does intros. -/ example : ∀ {α : Type} [Subsingleton α] (x y : α), x = y := by subsingleton /-! Does intros, and turns HEq into Eq if possible. -/ example : ∀ {α : Type} [Subsingleton α] (x y : α), x ≍ y := by subsingleton section AvoidSurprise /-- error: tactic 'subsingleton' could not prove equality since it could not synthesize Subsingleton α -/ #guard_msgs in example (α : Sort _) (x y : α) : x = y := by subsingleton instance (α : Sort _) (x y : α) : Decidable (x = y) := isTrue (Subsingleton.elim _ _) /-- error: tactic 'subsingleton' could not prove equality since it could not synthesize Subsingleton α -/ #guard_msgs in instance (α : Sort _) (x y : α) : Decidable (x = y) := isTrue (by subsingleton) end AvoidSurprise /-! Handles `BEq` instances if there are `LawfulBEq` instances for each. -/ example (α : Type) (inst1 inst2 : BEq α) [@LawfulBEq α inst1] [@LawfulBEq α inst2] : inst1 = inst2 := by subsingleton /-! `subsingleton` suggests `rfl` when it fails -/ /-- info: Try this: [apply] rfl -/ #guard_msgs in example : 1 + 1 = 2 := by subsingleton /-! `subsingleton` does not itself try `rfl` if it's not in error recovery mode -/ example : 1 + 1 = 2 := by try subsingleton guard_target =ₛ 1 + 1 = 2 rfl /-- info: Try this: [apply] (intros; rfl) -/ #guard_msgs in example : ∀ (n : Nat), n = n := by subsingleton /-! Passing subsingleton instances to the tactic. -/ example {α : Type} (x y : α) : x = y := by subsingleton [(test_sorry : Subsingleton α)] /-! No linting yet for unused instances. -/ #guard_msgs in example {α : Type} (x y : α) : x = y := by subsingleton [(test_sorry : Subsingleton α), (test_sorry : Subsingleton α)] /-! Abstraction, with specified universe levels. -/ example {α : Type} (x y : α) : x = y := by subsingleton [(test_sorry : Subsingleton.{1} _)] /-! Abstraction, with unspecified universe levels. -/ #guard_msgs in example {α : Type} (x y : α) : x = y := by subsingleton [(test_sorry : Subsingleton _)] /-! Not an instance. -/ /-- error: Not an instance. Term has type Bool -/ #guard_msgs in example {α : Type} (x y : α) : x = y := by subsingleton [true] /-! When abstracting, metavariables become instance implicit if they're for classes. -/ example {α : Type} [BEq α] (f : BEq α → Subsingleton α) (x y : α) : x = y := by subsingleton [f _] /-! This too abstracts some metavariables and ensures that `BEq` is instance implicit. -/ example {α : Type} [BEq α] (f : ∀ {β : Type} [BEq β], Subsingleton β) (x y : α) : x = y := by subsingleton [f] /-! The same, but now there's a universe level metavariable. -/ def fdef : ∀ {β : Type _} [BEq β], Subsingleton β := test_sorry example {α : Type} [BEq α] (x y : α) : x = y := by subsingleton [fdef]
.lake/packages/mathlib/MathlibTest/factorial.lean
import Mathlib.Data.Nat.Factorial.Basic /-! We check that ``1_000_000 !` evaluates in less than 10 seconds. (This should be a conservative upper bound that will work on most hardware, but still tight enough that we couldn't achieve it without binary splitting.) -/ open Nat /-- info: 18488884 -/ #guard_msgs in run_elab show Lean.Elab.TermElabM Unit from do let start ← IO.monoNanosNow let result ← Lean.Elab.Term.evalTerm ℕ Lean.Nat.mkType (← `(1_000_000 ! |>.log2)) IO.println result let finish ← IO.monoNanosNow guard (finish - start < 10_000_000_000)
.lake/packages/mathlib/MathlibTest/zmod.lean
import Mathlib.Data.ZMod.Defs -- A Mersenne exponent, chosen so the test below takes no more than a few seconds -- but should be enough to trigger a stack overflow from a non-tail-recursive implementation -- of exponentiation by repeated squaring. def k := 11213 def p := 2^k - 1 #adaptation_note /-- 2025-06-23 in the new compiler, the csimp lemma does not appear to apply, and the interpreter has a stack overflow. -/ /- set_option exponentiation.threshold 100000 in -- We ensure here that the `@[csimp]` attribute successfully replaces (at runtime) -- the default value of `npowRec` for the exponentiation operation in `Monoid` -- with a tail-recursive implementation by repeated squaring. /-- info: 1 -/ #guard_msgs in #eval (37 : ZMod p)^(p-1) -/
.lake/packages/mathlib/MathlibTest/InstanceTransparency.lean
import Mathlib.Data.Real.Basic /-! # Test transparency level of `Div` field in `DivInvMonoid` It is desirable that particular `DivInvMonoid`s have their `Div` instance not unfold at `.instance` transparency level, in the same way that the `Div` field of a generic `DivInvMonoid` does not. To ensure this, in examples where the `Div` field is defined as `fun a b ↦ a * b⁻¹`, we hide this under one layer of other function (so for example the `Div` instance for `Rat` is defined to be `⟨Rat.div⟩`, where `Rat.div` is defined to be `fun a b ↦ a * b⁻¹`). This file checks that this and similar tricks have had the desired effect: `with_reducible_and_instances apply mul_le_mul` fails although `apply mul_le_mul` succeeds. -/ private axiom test_sorry : ∀ {α}, α set_option autoImplicit true example {a b : α} [Field α] [LinearOrder α] [IsStrictOrderedRing α] : a / 2 ≤ b / 2 := by fail_if_success with_reducible_and_instances apply mul_le_mul -- fails, as desired exact test_sorry example {a b : ℚ} : a / 2 ≤ b / 2 := by fail_if_success with_reducible_and_instances apply mul_le_mul -- fails, as desired apply mul_le_mul repeat exact test_sorry example {a b : ℝ} : a / 2 ≤ b / 2 := by fail_if_success with_reducible_and_instances apply mul_le_mul -- fails, as desired apply mul_le_mul repeat exact test_sorry
.lake/packages/mathlib/MathlibTest/lift.lean
import Mathlib.Tactic.Lift import Batteries.Tactic.PermuteGoals import Mathlib.Tactic.Coe import Mathlib.Data.Set.Defs import Mathlib.Order.WithBot import Mathlib.Algebra.Group.WithOne.Defs import Mathlib.Data.Set.Image import Mathlib.Data.Set.List import Mathlib.Data.Rat.Defs import Mathlib.Data.PNat.Defs /-! Some tests of the `lift` tactic. -/ example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by lift n to ℕ guard_target =ₛ 0 ≤ n swap; guard_target =ₛ 0 ≤ (n : Int) + 1; swap · exact hn · exact Int.le_add_one hn example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by lift n to ℕ using hn guard_target =ₛ 0 ≤ (n : Int) + 1 exact Int.le_add_one (Int.ofNat_zero_le _) example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by have hn' := hn lift n to ℕ using hn with k hk guard_target =ₛ 0 ≤ (k : Int) + 1 guard_hyp hk : (k : Int) = n exact Int.le_add_one hn' example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by have hn' := hn lift n to ℕ using hn with k guard_target =ₛ 0 ≤ (k : Int) + 1 exact Int.le_add_one hn' example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by lift n to ℕ using hn with k hk hn guard_target =ₛ 0 ≤ (k : Int) + 1 guard_hyp hn : 0 ≤ (k : Int) guard_hyp hk : k = n exact Int.le_add_one hn example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by lift n to ℕ using hn with k rfl hn guard_target =ₛ 0 ≤ (k : Int) + 1 guard_hyp hn : 0 ≤ (k : Int) exact Int.le_add_one hn example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by have hn' := hn lift n to ℕ using hn with k rfl guard_target =ₛ 0 ≤ (k : Int) + 1 exact Int.le_add_one hn' example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by have hn' := hn lift n to ℕ using hn with n guard_target =ₛ 0 ≤ (n : Int) + 1 exact Int.le_add_one hn' example (n : ℤ) (hn : 0 ≤ n) : 0 ≤ n + 1 := by -- Should fail because we didn't provide a variable name when lifting an expression fail_if_success lift n + 1 to ℕ using (Int.le_add_one hn) -- Now it should succeed lift n + 1 to ℕ using (Int.le_add_one hn) with k hk exact of_decide_eq_true rfl -- test lift of functions example (α : Type _) (f : α → ℤ) (hf : ∀ a, 0 ≤ f a) (hf' : ∀ a, f a < 1) (a : α) : 0 ≤ 2 * f a := by lift f to α → ℕ using hf guard_target =ₛ (0 : ℤ) ≤ 2 * (fun i : α ↦ (f i : ℤ)) a guard_hyp hf' : ∀ a, ((fun i : α ↦ (f i : ℤ)) a) < 1 constructor example (α : Type _) (f : α → ℤ) (hf : ∀ a, 0 ≤ f a) (_ : ∀ a, f a < 1) (a : α) : 0 ≤ 2 * f a := by lift f to α → ℕ using hf with g hg guard_target =ₛ 0 ≤ 2 * (g a : Int) guard_hyp hg : (fun i => (g i : Int)) = f constructor example (n m k x : ℤ) (hn : 0 < n) (hk : 0 ≤ k + n) (h : k + n = 2 + x) (hans : k + n = m + x) (hans2 : 0 ≤ m) : k + n = m + x := by lift n to ℕ using le_of_lt hn guard_target =ₛ k + ↑n = m + x; guard_hyp hn : (0 : ℤ) < ↑n lift m to ℕ guard_target =ₛ 0 ≤ m; swap; guard_target =ₛ k + ↑n = ↑m + x lift (k + n) to ℕ using hk with l hl exact hans exact hans2 -- fail gracefully when the lifted variable is a local definition example (h : False) : let n : ℤ := 3; n = 3 := by intro n fail_if_success lift n to ℕ exfalso; exact h instance canLift_unit : CanLift Unit Unit id (fun _ ↦ true) := ⟨fun x _ ↦ ⟨x, rfl⟩⟩ set_option linter.unusedVariables false in example (n : ℤ) (hn : 0 < n) : True := by fail_if_success lift n to ℕ using hn fail_if_success lift (n : Option ℤ) to ℕ trivial set_option linter.unusedVariables false in example (n : ℤ) : ℕ := by fail_if_success lift n to ℕ exact 0 instance canLift_subtype (R : Type _) (s : Set R) : CanLift R {x // x ∈ s} ((↑) : {x // x ∈ s} → R) (fun x => x ∈ s) := { prf := fun x hx => ⟨⟨x, hx⟩, rfl⟩ } example {R : Type _} {P : R → Prop} (x : R) (hx : P x) : P x := by lift x to {x // P x} using hx with y hy hx guard_target =ₛ P y guard_hyp hy : y = x guard_hyp hx : P y exact hx example (n : ℤ) (h_ans : n = 5) (hn : 0 ≤ 1 * n) : n = 5 := by lift n to ℕ using by { simpa [Int.one_mul] using hn } with k hk guard_target =ₛ (k : Int) = 5 guard_hyp hk : k = n guard_hyp hn : 0 ≤ 1 * (k : Int) guard_hyp h_ans : (k : Int) = 5 exact h_ans example (n : WithOne Unit) (hn : n ≠ 1) : True := by lift n to Unit · guard_target =ₛ n ≠ 1 exact hn guard_hyp n : Unit guard_hyp hn : (n : WithOne Unit) ≠ 1 trivial example (n : WithZero Unit) (hn : n ≠ 0) : True := by lift n to Unit · guard_target =ₛ n ≠ 0 exact hn guard_hyp n : Unit guard_hyp hn : (n : WithZero Unit) ≠ 0 trivial example (s : Set ℤ) (h : ∀ x ∈ s, 0 ≤ x) : True := by lift s to Set ℕ · guard_target =ₛ (∀ x ∈ s, 0 ≤ x) exact h guard_hyp s : Set ℕ guard_hyp h : ∀ (x : ℤ), x ∈ (fun (n : ℕ) => (n : ℤ)) '' s → 0 ≤ x trivial example (l : List ℤ) (h : ∀ x ∈ l, 0 ≤ x) : True := by lift l to List ℕ · guard_target =ₛ (∀ x ∈ l, 0 ≤ x) exact h guard_hyp l : List ℕ guard_hyp h : ∀ (x : ℤ), x ∈ List.map (fun (n : ℕ) => (n : ℤ)) l → 0 ≤ x trivial example (q : ℚ) (h : q.den = 1) : True := by lift q to ℤ · guard_target =ₛ q.den = 1 exact h guard_hyp q : ℤ guard_hyp h : (q : ℚ).den = 1 trivial example (x : WithTop Unit) (h : x ≠ ⊤) : True := by lift x to Unit · guard_target =ₛ x ≠ ⊤ exact h guard_hyp x : Unit guard_hyp h : (x : WithTop Unit) ≠ ⊤ trivial example (x : WithBot Unit) (h : x ≠ ⊥) : True := by lift x to Unit · guard_target =ₛ x ≠ ⊥ exact h guard_hyp x : Unit guard_hyp h : (x : WithBot Unit) ≠ ⊥ trivial example (n : ℕ) (hn : 0 < n) : True := by lift n to ℕ+ · guard_target =ₛ 0 < n exact hn guard_hyp n : ℕ+ guard_hyp hn : 0 < (n : ℕ) trivial example (n : ℕ) : n = 0 ∨ ∃ p : ℕ+, n = p := by by_cases hn : 0 < n · lift n to ℕ+ using hn right exact ⟨n, rfl⟩ · left exact Nat.eq_zero_of_not_pos hn example (n : ℤ) (hn : 0 < n) : True := by lift n to ℕ+ · guard_target =ₛ 0 < n exact hn guard_hyp n : ℕ+ guard_hyp hn : 0 < (n : ℤ) trivial -- https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Bug.20in.20.60lift.60.20tactic.3F/near/508521400 /-- trace: x : WithTop ℕ P : WithTop ℕ → Prop u : ℕ hu : ↑u = x h : P ↑u ⊢ P ↑u -/ #guard_msgs in example {x : WithTop ℕ} (hx : x ≠ ⊤) (P : WithTop ℕ → Prop) (h : P x) : P x := by lift x to ℕ using hx with u hu trace_state exact h /-! Test that the `h` in `using h` is not cleared if the goal depends on it. -/ set_option linter.unusedVariables false in def foo (n : Int) (hn : 0 ≤ n) : Int := n example (n : Int) (hn : 0 ≤ n) : foo n hn = n := by lift n to Nat using hn rfl
.lake/packages/mathlib/MathlibTest/DeprecatedModule.lean
import Mathlib.Tactic.Linter.DeprecatedModule import Mathlib.Tactic.Linter.DocPrime import Mathlib.Tactic.Linter.DocString deprecated_module (since := "2025-04-10") /-- info: Deprecated modules 'MathlibTest.DeprecatedModule' deprecates to #[Mathlib.Tactic.Linter.DocPrime, Mathlib.Tactic.Linter.DocString] with no message -/ #guard_msgs in #show_deprecated_modules -- Deprecating the current module is possible and allows to add more deprecation information. deprecated_module "We can also give more details about the deprecation" (since := "2025-04-10") /-- info: Deprecated modules 'MathlibTest.DeprecatedModule' deprecates to #[Mathlib.Tactic.Linter.DocPrime, Mathlib.Tactic.Linter.DocString] with message 'We can also give more details about the deprecation' 'MathlibTest.DeprecatedModule' deprecates to #[Mathlib.Tactic.Linter.DocPrime, Mathlib.Tactic.Linter.DocString] with no message -/ #guard_msgs in #show_deprecated_modules /- Commenting out the following test, since it does not work in CI besides, it suggests the current date, so it should *not* be uncommented until that is also fixed! /-- error: Invalid date: the expected format is "2025-04-14" -/ #guard_msgs in deprecated_module "Text" (since := "2025-02-31") -/ /-- info: Deprecated modules 'MathlibTest.DeprecatedModule' deprecates to #[Mathlib.Tactic.Linter.DocPrime, Mathlib.Tactic.Linter.DocString] with message 'We can also give more details about the deprecation' 'MathlibTest.DeprecatedModule' deprecates to #[Mathlib.Tactic.Linter.DocPrime, Mathlib.Tactic.Linter.DocString] with no message -/ #guard_msgs in #show_deprecated_modules
.lake/packages/mathlib/MathlibTest/oldObtain.lean
import Mathlib.Tactic.Linter.OldObtain /-! Tests for the `oldObtain` linter. -/ set_option linter.oldObtain true -- These cases are fine. theorem foo : True := by obtain := trivial obtain _h := trivial obtain : True := trivial obtain _h : True := trivial trivial -- These cases are linted against. /-- warning: Please remove stream-of-consciousness `obtain` syntax Note: This linter can be disabled with `set_option linter.oldObtain false` -/ #guard_msgs in theorem foo' : True := by obtain : True · trivial trivial /-- warning: Please remove stream-of-consciousness `obtain` syntax Note: This linter can be disabled with `set_option linter.oldObtain false` -/ #guard_msgs in theorem foo'' : True := by obtain h : True · trivial trivial
.lake/packages/mathlib/MathlibTest/HaveLetLinter.lean
import Mathlib.Tactic.Linter.HaveLetLinter import Mathlib.Tactic.Tauto -- #adaptation_note -- The haveLet linter started failing after https://github.com/leanprover/lean4/pull/6299 -- Disabling aync elaboration fixes it, but of course we're not going to do that globally. set_option Elab.async false set_option linter.haveLet 1 /-- A tactic that adds a vacuous `sorry`. Useful for testing the chattiness of the `haveLet` linter. -/ elab "noise" : tactic => do Lean.Elab.Tactic.evalTactic (← `(tactic| have : 0 = 0 := sorry; clear this )) set_option linter.haveLet 2 in #guard_msgs in -- check that `tauto`, `replace`, `classical` are ignored example : True := by classical let zero' := 0 replace _zero := zero' let eq := (rfl : 0 = 0) replace _eq := eq tauto /-- warning: declaration uses 'sorry' --- warning: '_zero : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in example : True := by noise have ⟨_zero, _⟩ : Fin 1 := ⟨0, Nat.zero_lt_one⟩ exact .intro /-- warning: '_zero : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in set_option linter.haveLet 2 in example : True := by have ⟨_zero, _⟩ : Fin 1 := ⟨0, Nat.zero_lt_one⟩ exact .intro #guard_msgs in example : True := by have ⟨_zero, _⟩ : Fin 1 := ⟨0, Nat.zero_lt_one⟩ exact .intro /-- warning: declaration uses 'sorry' --- warning: '_zero : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in example : True := by have ⟨_zero, _⟩ : Fin 1 := ⟨0, Nat.zero_lt_one⟩ noise exact .intro /-- warning: declaration uses 'sorry' --- warning: '_zero : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in example : True := by have ⟨_zero, _⟩ : Fin 1 := ⟨0, Nat.zero_lt_one⟩ noise exact .intro /-- warning: declaration uses 'sorry' --- warning: '_a : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` --- warning: '_b : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` --- warning: '_oh : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` --- warning: '_b : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in example : True := by noise have _a := 0 have _b : Nat := 0 have _b : 0 = 0 := rfl have _oh : Nat := 0 have _b : Nat := 2 tauto set_option linter.haveLet 0 in set_option linter.haveLet 1 in /-- warning: declaration uses 'sorry' --- warning: 'this : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in example : True := by have := Nat.succ ?_; noise exact .intro exact 0 /-- warning: declaration uses 'sorry' -/ #guard_msgs in example : True := by have := And.intro (Nat.add_comm ?_ ?_) (Nat.add_comm ?_ ?_) apply True.intro noise repeat exact 0 /-- warning: declaration uses 'sorry' -/ #guard_msgs in example (h : False) : True := by have : False := h noise exact .intro set_option linter.haveLet 0 in set_option linter.haveLet 1 in /-- warning: declaration uses 'sorry' --- warning: 'this : ℕ' is a Type and not a Prop. Consider using 'let' instead of 'have'. Note: This linter can be disabled with `set_option linter.haveLet 0` -/ #guard_msgs in theorem ghi : True := by noise have : Nat := Nat.succ 1; exact .intro
.lake/packages/mathlib/MathlibTest/linear_combination.lean
import Mathlib.Tactic.Abel import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.LinearCombination import Mathlib.Tactic.Linarith import Mathlib.Tactic.Module set_option autoImplicit true private axiom test_sorry : ∀ {α}, α -- We deliberately mock R here so that we don't have to import the deps axiom Real : Type notation "ℝ" => Real @[instance] axiom Real.field : Field ℝ @[instance] axiom Real.linearOrder : LinearOrder ℝ @[instance] axiom Real.isStrictOrderedRing : IsStrictOrderedRing ℝ /-! ### Simple Cases with ℤ and two or less equations -/ example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) : 3 * x + 2 * y = 10 := by linear_combination 1 * h1 example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) : 3 * x + 2 * y = 10 := by linear_combination h1 example (x y : ℤ) (h1 : x + 2 = -3) (_h2 : y = 10) : 2 * x + 4 = -6 := by linear_combination 2 * h1 example (x y : ℤ) (h1 : x * y + 2 * x = 1) (h2 : x = y) : x * y = -2 * y + 1 := by linear_combination 1 * h1 - 2 * h2 example (x y : ℤ) (h1 : x * y + 2 * x = 1) (h2 : x = y) : x * y = -2 * y + 1 := by linear_combination -2 * h2 + h1 example (x y : ℤ) (h1 : x + 2 = -3) (h2 : y = 10) : 2 * x + 4 - y = -16 := by linear_combination 2 * h1 + -1 * h2 example (x y : ℤ) (h1 : x + 2 = -3) (h2 : y = 10) : -y + 2 * x + 4 = -16 := by linear_combination -h2 + 2 * h1 example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) : 11 * y = -11 := by linear_combination -2 * h1 + 3 * h2 example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) : -11 * y = 11 := by linear_combination 2 * h1 - 3 * h2 example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) : -11 * y = 11 + 1 - 1 := by linear_combination 2 * h1 + -3 * h2 example (x y : ℤ) (h1 : 10 = 3 * x + 2 * y) (h2 : 3 = 2 * x + 5 * y) : 11 + 1 - 1 = -11 * y := by linear_combination 2 * h1 - 3 * h2 /-! ### More complicated cases with two equations -/ example (x y : ℤ) (h1 : x + 2 = -3) (h2 : y = 10) : -y + 2 * x + 4 = -16 := by linear_combination 2 * h1 - h2 example (x y : ℚ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) : -11 * y + 1 = 11 + 1 := by linear_combination 2 * h1 - 3 * h2 example (a b : ℝ) (ha : 2 * a = 4) (hab : 2 * b = a - b) : b = 2 / 3 := by linear_combination ha / 6 + hab / 3 /-! ### Cases with more than 2 equations -/ example (a b : ℝ) (ha : 2 * a = 4) (hab : 2 * b = a - b) (hignore : 3 = a + b) : b = 2 / 3 := by linear_combination 1 / 6 * ha + 1 / 3 * hab + 0 * hignore example (x y z : ℝ) (ha : x + 2 * y - z = 4) (hb : 2 * x + y + z = -2) (hc : x + 2 * y + z = 2) : -3 * x - 3 * y - 4 * z = 2 := by linear_combination ha - hb - 2 * hc example (x y z : ℝ) (ha : x + 2 * y - z = 4) (hb : 2 * x + y + z = -2) (hc : x + 2 * y + z = 2) : 6 * x = -10 := by linear_combination 1 * ha + 4 * hb - 3 * hc example (x y z : ℝ) (ha : x + 2 * y - z = 4) (hb : 2 * x + y + z = -2) (hc : x + 2 * y + z = 2) : 10 = 6 * -x := by linear_combination ha + 4 * hb - 3 * hc example (w x y z : ℝ) (h1 : x + 2.1 * y + 2 * z = 2) (h2 : x + 8 * z + 5 * w = -6.5) (h3 : x + y + 5 * z + 5 * w = 3) : x + 2.2 * y + 2 * z - 5 * w = -8.5 := by linear_combination 2 * h1 + 1 * h2 - 2 * h3 example (w x y z : ℝ) (h1 : x + 2.1 * y + 2 * z = 2) (h2 : x + 8 * z + 5 * w = -6.5) (h3 : x + y + 5 * z + 5 * w = 3) : x + 2.2 * y + 2 * z - 5 * w = -8.5 := by linear_combination 2 * h1 + h2 - 2 * h3 example (a b c d : ℚ) (h1 : a = 4) (h2 : 3 = b) (h3 : c * 3 = d) (h4 : -d = a) : 2 * a - 3 + 9 * c + 3 * d = 8 - b + 3 * d - 3 * a := by linear_combination 2 * h1 - 1 * h2 + 3 * h3 - 3 * h4 example (a b c d : ℚ) (h1 : a = 4) (h2 : 3 = b) (h3 : c * 3 = d) (h4 : -d = a) : 6 - 3 * c + 3 * a + 3 * d = 2 * b - d + 12 - 3 * a := by linear_combination 2 * h2 - h3 + 3 * h1 - 3 * h4 /-! ### Cases with non-hypothesis inputs -/ axiom qc : ℚ axiom hqc : qc = 2 * qc example (a b : ℚ) (h : ∀ p q : ℚ, p = q) : 3 * a + qc = 3 * b + 2 * qc := by linear_combination 3 * h a b + hqc axiom bad (q : ℚ) : q = 0 example (a b : ℚ) : a + b ^ 3 = 0 := by linear_combination bad a + b * bad (b * b) /-! ### Cases with arbitrary coefficients -/ example (a b : ℤ) (h : a = b) : a * a = a * b := by linear_combination a * h example (a b c : ℤ) (h : a = b) : a * c = b * c := by linear_combination c * h example (a b c : ℤ) (h1 : a = b) (h2 : b = 1) : c * a + b = c * b + 1 := by linear_combination c * h1 + h2 example (x y : ℚ) (h1 : x + y = 3) (h2 : 3 * x = 7) : x * x * y + y * x * y + 6 * x = 3 * x * y + 14 := by linear_combination x * y * h1 + 2 * h2 example {α} [h : CommRing α] {a b c d e f : α} (h1 : a * d = b * c) (h2 : c * f = e * d) : c * (a * f - b * e) = 0 := by linear_combination e * h1 + a * h2 example (x y z w : ℚ) (hzw : z = w) : x * z + 2 * y * z = x * w + 2 * y * w := by linear_combination (x + 2 * y) * hzw example (x y : ℤ) (h : x = 0) : y ^ 2 * x = 0 := by linear_combination y ^ 2 * h /-! ### Scalar multiplication -/ section variable {K V : Type*} section variable [AddCommGroup V] [Field K] [CharZero K] [Module K V] {a b μ ν : K} {v w x y : V} example (h : a ^ 2 + b ^ 2 = 1) : a • (a • x - b • y) + (b • a • y + b • b • x) = x := by linear_combination (norm := module) h • x example (h1 : a • x + b • y = 0) (h2 : a • μ • x + b • ν • y = 0) : (μ - ν) • a • x = 0 := by linear_combination (norm := module) h2 - ν • h1 example (h₁ : x - y = -(v - w)) (h₂ : x + y = v + w) : x = w := by linear_combination (norm := module) (2:K)⁻¹ • h₁ + (2:K)⁻¹ • h₂ example (h : a + b ≠ 0) (H : a • x = b • y) : x = (b / (a + b)) • (x + y) := by linear_combination (norm := match_scalars) (a + b)⁻¹ • H · field_simp ring · ring end example [CommSemiring K] [PartialOrder K] [IsOrderedRing K] [AddCommMonoid V] [PartialOrder V] [IsOrderedCancelAddMonoid V] [Module K V] [PosSMulStrictMono K V] {x y r : V} (hx : x < r) (hy : y < r) {a b : K} (ha : 0 < a) (hb : 0 ≤ b) (hab : a + b = 1) : a • x + b • y < r := by linear_combination (norm := skip) a • hx + b • hy + hab • r apply le_of_eq module example [CommSemiring K] [PartialOrder K] [IsOrderedRing K] [AddCommMonoid V] [PartialOrder V] [IsOrderedCancelAddMonoid V] [Module K V] [PosSMulMono K V] {x y z : V} (hyz : y ≤ z) {a b : K} (hb : 0 ≤ b) (hab : a + b = 1) (H : z ≤ a • x + b • y) : a • z ≤ a • x := by linear_combination (norm := skip) b • hyz + hab • z + H apply le_of_eq module example [CommRing K] [PartialOrder K] [IsOrderedRing K] [AddCommGroup V] [PartialOrder V] [IsOrderedAddMonoid V] [Module K V] [IsStrictOrderedModule K V] {x y : V} (hx : 0 < x) (hxy : x < y) {a b c : K} (hc : 0 < c) (hac : c < a) (hab : a + b ≤ 1) : c • x + b • y < y := by have := hx.trans hxy linear_combination (norm := skip) hab • y + hac • y + c • hxy apply le_of_eq module end /-! ### Tests in semirings -/ example (a _b : ℕ) (h1 : a = 3) : a = 3 := by linear_combination h1 example {a b : ℕ} (h1 : a = b + 4) (h2 : b = 2) : a = 6 := by linear_combination h1 + h2 example {a : ℕ} (h : a = 3) : 3 = a := by linear_combination -h example {a b : ℕ} (h1 : 3 * a = b + 5) (h2 : 2 * a = b + 3) : a = 2 := by linear_combination h1 - h2 /- Note: currently negation/subtraction is handled differently in "constants" than in "proofs", so in particular negation/subtraction does not "distribute". The following four tests record the current behaviour, without taking a stance on whether this should be considered a feature or a bug. -/ example {a : ℕ} (h : a = 3) : a ^ 2 + 3 = 4 * a := by linear_combination a * h - h /-- error: ring failed, ring expressions not equal a b : ℕ h : a = 3 ⊢ 3 + a ^ 2 + (a - 1) * 3 = a * 4 + a * (a - 1) -/ #guard_msgs in example {a b : ℕ} (h : a = 3) : a ^ 2 + 3 = 4 * a := by linear_combination (a - 1) * h example {a b c : ℕ} (h1 : c = 1) (h2 : a - b = 4) : (a - b) * c = 4 := by linear_combination (a - b) * h1 + h2 /-- error: ring failed, ring expressions not equal a b c : ℕ h1 : c = 1 h2 : a - b = 4 ⊢ 4 + (a - b) * c + c * b + a = 4 + (a - b) + c * a + b -/ #guard_msgs in example {a b c : ℕ} (h1 : c = 1) (h2 : a - b = 4) : (a - b) * c = 4 := by linear_combination a * h1 - b * h1 + h2 /-! ### Cases that explicitly use a config -/ example (x y : ℚ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) : -11 * y + 1 = 11 + 1 := by linear_combination (norm := ring) 2 * h1 - 3 * h2 example (x y : ℚ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) : -11 * y + 1 = 11 + 1 := by linear_combination (norm := ring1) 2 * h1 + -3 * h2 example (a b : ℝ) (ha : 2 * a = 4) (hab : 2 * b = a - b) : b = 2 / 3 := by linear_combination (norm := ring_nf) 1 / 6 * ha + 1 / 3 * hab example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) : 3 * x + 2 * y = 10 := by linear_combination (norm := skip) h1 ring1 /-! ### Cases that have linear_combination skip normalization -/ example (a b : ℝ) (ha : 2 * a = 4) (hab : 2 * b = a - b) : b = 2 / 3 := by linear_combination (norm := skip) 1 / 6 * ha + 1 / 3 * hab linarith example (x y : ℤ) (h1 : x = -3) (_h2 : y = 10) : 2 * x = -6 := by linear_combination (norm := skip) 2 * h1 ring1 /-! ### Cases without any arguments provided -/ -- the corner case is "just apply the normalization procedure". example {x y z w : ℤ} (_h₁ : 3 * x = 4 + y) (_h₂ : x + 2 * y = 1) : z + w = w + z := by linear_combination example (x : ℤ) : x ^ 2 = x ^ 2 := by linear_combination -- this interacts as expected with options example {x y z w : ℤ} (_h₁ : 3 * x = 4 + y) (_h₂ : x + 2 * y = 1) : z + w = w + z := by linear_combination (norm := skip) guard_target = z + w + 0 - (w + z + 0) = 0 simp [add_comm] example {x y z w : ℤ} (_h₁ : 3 * x = 4 + y) (_h₂ : x + 2 * y = 1) : z + w = w + z := by linear_combination (norm := simp [add_comm]) /-! ### Cases where the goal is not closed -/ example (x y : ℚ) (h1 : x + y = 3) (h2 : 3 * x = 7) : x * x * y + y * x * y + 6 * x = 3 * x * y + 14 := by linear_combination (norm := ring_nf) x * y * h1 + h2 guard_target = -7 + x * 3 = 0 linear_combination h2 example (a b c d : ℚ) (h1 : a = 4) (h2 : 3 = b) (h3 : c * 3 = d) (h4 : -d = a) : 6 - 3 * c + 3 * a + 3 * d = 2 * b - d + 12 - 3 * a := by linear_combination (norm := ring_nf) 2 * h2 linear_combination (norm := ring_nf) -h3 linear_combination (norm := ring_nf) 3 * h1 linear_combination (norm := ring_nf) -3 * h4 example (x y : ℤ) (h1 : x * y + 2 * x = 1) (h2 : x = y) : x * y = -2 * y + 1 := by linear_combination (norm := ring_nf) linear_combination h1 - 2 * h2 example {A : Type*} [AddCommGroup A] {x y z : A} (h1 : x + y = 10 • z) (h2 : x - y = 6 • z) : 2 • x = 2 • (8 • z) := by linear_combination (norm := abel) h1 + h2 /-! ### Cases that should fail -/ /-- error: ring failed, ring expressions not equal a : ℤ ha : a = 1 ⊢ -1 = 0 -/ #guard_msgs in example (a : ℤ) (ha : a = 1) : a = 2 := by linear_combination ha /-- error: ring failed, ring expressions not equal a : ℚ ha : a = 1 ⊢ -1 = 0 -/ #guard_msgs in example (a : ℚ) (ha : a = 1) : a = 2 := by linear_combination ha -- This should fail because the second coefficient has a different type than -- the equations it is being combined with. This was a design choice for the -- sake of simplicity, but the tactic could potentially be modified to allow -- this behavior. /-- error: Application type mismatch: The argument 0 has type ℝ but is expected to have type ℤ in the application Mathlib.Tactic.LinearCombination.mul_const_eq h2 0 -/ #guard_msgs in example (x y : ℤ) (h1 : x * y + 2 * x = 1) (h2 : x = y) : x * y + 2 * x = 1 := by linear_combination h1 + (0 : ℝ) * h2 set_option linter.unusedVariables false in example (a b : ℤ) (x y : ℝ) (hab : a = b) (hxy : x = y) : 2 * x = 2 * y := by fail_if_success linear_combination 2 * hab linear_combination 2 * hxy /-- warning: this constant has no effect on the linear combination; it can be dropped from the term -/ #guard_msgs in example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) : 3 * x + 2 * y = 10 := by linear_combination h1 + 3 /-- warning: this constant has no effect on the linear combination; it can be dropped from the term -/ #guard_msgs in example (x : ℤ) : x ^ 2 = x ^ 2 := by linear_combination x ^ 2 /-- error: 'linear_combination' supports only linear operations -/ #guard_msgs in example {x y : ℤ} (h : x = y) : x ^ 2 = y ^ 2 := by linear_combination h * h /-- error: 'linear_combination' supports only linear operations -/ #guard_msgs in example {x y : ℤ} (h : x = y) : 3 / x = 3 / y := by linear_combination 3 / h /-! ### Cases with exponent -/ example (x y z : ℚ) (h : x = y) (h2 : x * y = 0) : x + y*z = 0 := by linear_combination (exp := 2) (-y * z ^ 2 + x) * h + (z ^ 2 + 2 * z + 1) * h2 example (x y z : ℚ) (h : x = y) (h2 : x * y = 0) : y*z = -x := by linear_combination (norm := skip) (exp := 2) (-y * z ^ 2 + x) * h + (z ^ 2 + 2 * z + 1) * h2 ring example (K : Type) [Field K] [CharZero K] {x y z : K} (h₂ : y ^ 3 + x * (3 * z ^ 2) = 0) (h₁ : x ^ 3 + z * (3 * y ^ 2) = 0) (h₀ : y * (3 * x ^ 2) + z ^ 3 = 0) (h : x ^ 3 * y + y ^ 3 * z + z ^ 3 * x = 0) : x = 0 := by linear_combination (exp := 6) 2 * y * z ^ 2 * h₂ / 7 + (x ^ 3 - y ^ 2 * z / 7) * h₁ - x * y * z * h₀ + y * z * h / 7 /-! ### Linear inequalities -/ example : (3:ℤ) ≤ 4 := by linear_combination example (x : ℚ) (hx : x ≤ 3) : x - 1 ≤ 5 := by linear_combination hx example (x : ℝ) (hx : x ≤ 3) : x - 1 ≤ 5 := by linear_combination hx example (a b : ℚ) (h1 : a ≤ 1) (h2 : b ≤ 1) : a + b ≤ 2 := by linear_combination h1 + h2 example (a b : ℚ) (h1 : a ≤ 1) (h2 : b = 1) : a + b < 3 := by linear_combination h1 + h2 example (a b : ℚ) (h1 : a ≤ 1) (h2 : b ≥ 2) : a ≤ b := by linear_combination h1 + h2 example (a : ℚ) (ha : 0 ≤ a) : 0 ≤ 2 * a := by linear_combination 2 * ha example {x y : ℚ} (h : x + 1 < y) : x < y := by linear_combination h example {x y : ℚ} (h : x < y) : x < y := by linear_combination h example (a b : ℚ) (h1 : a ≤ 1) (h2 : b = 1) : (a + b) / 2 ≤ 1 := by linear_combination (h1 + h2) / 2 example {x y : ℤ} (hx : x + 3 ≤ 2) (hy : y + 2 * x ≥ 3) : y > 3 := by linear_combination hy + 2 * hx example {x y : ℕ} (hx : x + 3 ≤ 2) (hy : y + 2 * x ≥ 3) : y > 3 := by linear_combination hy + 2 * hx example {x y : ℤ} (h : x + 1 ≤ y) : x < y := by linear_combination h example {x y z : ℚ} (h1 : 4 * x + y + 3 * z ≤ 25) (h2 : -x + 2 * y + z = 3) (h3 : 5 * x + 7 * z = 43) : x ≤ 4 := by linear_combination (14 * h1 - 7 * h2 - 5 * h3) / 38 example {a b c d e : ℚ} (h1 : 3 * a + 4 * b - 2 * c + d = 15) (h2 : a + 2 * b + c - 2 * d + 2 * e ≤ 3) (h3 : 5 * a + 5 * b - c + d + 4 * e = 31) (h4 : 8 * a + b - c - 2 * d + 2 * e = 8) (h5 : 1 - 2 * b + 3 * c - 4 * d + 5 * e = -4) : a ≤ 1 := by linear_combination (-155 * h1 + 68 * h2 + 49 * h3 + 59 * h4 - 90 * h5) / 320 example {a b c d e : ℚ} (h1 : 3 * a + 4 * b - 2 * c + d = 15) (h2 : a + 2 * b + c - 2 * d + 2 * e ≤ 3) (h3 : 5 * a + 5 * b - c + d + 4 * e = 31) (h4 : 8 * a + b - c - 2 * d + 2 * e = 8) (h5 : 1 - 2 * b + 3 * c - 4 * d + 5 * e > -4) : a < 1 := by linear_combination (-155 * h1 + 68 * h2 + 49 * h3 + 59 * h4 + 90 * h5) / 320 /-- error: comparison failed, LHS is larger a b : ℚ h1 : a ≤ 1 h2 : b ≥ 0 ⊢ 1 ≤ 0 -/ #guard_msgs in example (a b : ℚ) (h1 : a ≤ 1) (h2 : b ≥ 0) : a ≤ b := by linear_combination h1 + h2 /-- error: ring failed, ring expressions not equal up to an additive constant a b : ℚ h1 : a ≤ 1 h2 : b ≥ 0 ⊢ 1 - b ≤ 0 -/ #guard_msgs in example (a b : ℚ) (h1 : a ≤ 1) (h2 : b ≥ 0) : a ≤ b := by linear_combination h1 /-- error: coefficients of inequalities in 'linear_combination' must be nonnegative -/ #guard_msgs in example (x y : ℤ) (h : x ≤ y) : -x ≤ -y := by linear_combination 4 - h /-! ### Nonlinear inequalities -/ example {a b : ℝ} (ha : 0 ≤ a) (hb : b < 1) : a * b ≤ a := by linear_combination a * hb example {a b : ℝ} (ha : 0 ≤ a) (hb : b < 1) : a * b ≤ a := by linear_combination hb * a /-- error: could not establish the nonnegativity of a -/ #guard_msgs in example {a b : ℝ} (hb : b < 1) : a * b ≤ a := by linear_combination a * hb example {u v x y A B : ℝ} (_ : 0 ≤ u) (_ : 0 ≤ v) (h2 : A ≤ 1) (h3 : 1 ≤ B) (h4 : x ≤ B) (h5 : y ≤ B) (h8 : u < A) (h9 : v < A) : u * y + v * x + u * v < 3 * A * B := by linear_combination v * h2 + v * h3 + v * h4 + u * h5 + (v + B) * h8 + 2 * B * h9 example {t : ℚ} (ht : t ≥ 10) : t ^ 2 - 3 * t - 17 ≥ 5 := by linear_combination (t + 7) * ht example {n : ℤ} (hn : n ≥ 5) : n ^ 2 > 2 * n + 11 := by linear_combination (n + 3) * hn example {a b : ℚ} : a * b ≤ (a ^ 2 + b ^ 2) / 2 := by linear_combination sq_nonneg (a - b) / 2 example {a b c : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) (hc : 0 ≤ c) : a * b * c ≤ (a ^ 3 + b ^ 3 + c ^ 3) / 3 := by have h : (a - b) ^ 2 + (b - c) ^ 2 + (c - a) ^ 2 ≥ 0 := by positivity linear_combination (a + b + c) * h / 6 example {a b c x : ℚ} (h : a * x ^ 2 + b * x + c = 0) : b ^ 2 ≥ 4 * a * c := by linear_combination 4 * a * h + sq_nonneg (2 * a * x + b) /-! ### Regression tests -/ def g (a : ℤ) : ℤ := a ^ 2 example (h : g a = g b) : a ^ 4 = b ^ 4 := by dsimp [g] at h linear_combination (a ^ 2 + b ^ 2) * h example {r s a b : ℕ} (h₁ : (r : ℤ) = a + 1) (h₂ : (s : ℤ) = b + 1) : r * s = (a + 1 : ℤ) * (b + 1) := by linear_combination (↑b + 1) * h₁ + ↑r * h₂ -- Implementation at the time of the port (Nov 2022) was 110,000 heartbeats. -- Eagerly elaborating leaf nodes brings this to 7,540 heartbeats. set_option maxHeartbeats 10000 in example (K : Type*) [Field K] [CharZero K] {x y z p q : K} (h₀ : 3 * x ^ 2 + z ^ 2 * p = 0) (h₁ : z * (2 * y) = 0) (h₂ : -y ^ 2 + p * x * (2 * z) + q * (3 * z ^ 2) = 0) : ((27 * q ^ 2 + 4 * p ^ 3) * x) ^ 4 = 0 := by linear_combination (norm := skip) (256 / 3 * p ^ 12 * x ^ 2 + 128 * q * p ^ 11 * x * z + 2304 * q ^ 2 * p ^ 9 * x ^ 2 + 2592 * q ^ 3 * p ^ 8 * x * z - 64 * q * p ^ 10 * y ^ 2 + 23328 * q ^ 4 * p ^ 6 * x ^ 2 + 17496 * q ^ 5 * p ^ 5 * x * z - 1296 * q ^ 3 * p ^ 7 * y ^ 2 + 104976 * q ^ 6 * p ^ 3 * x ^ 2 + 39366 * q ^ 7 * p ^ 2 * x * z - 8748 * q ^ 5 * p ^ 4 * y ^ 2 + 177147 * q ^ 8 * x ^ 2 - 19683 * q ^ 7 * p * y ^ 2) * h₀ + (-(64 / 3 * p ^ 12 * x * y) + 32 * q * p ^ 11 * z * y - 432 * q ^ 2 * p ^ 9 * x * y + 648 * q ^ 3 * p ^ 8 * z * y - 2916 * q ^ 4 * p ^ 6 * x * y + 4374 * q ^ 5 * p ^ 5 * z * y - 6561 * q ^ 6 * p ^ 3 * x * y + 19683 / 2 * q ^ 7 * p ^ 2 * z * y) * h₁ + (-(128 / 3 * p ^ 12 * x * z) - 192 * q * p ^ 10 * x ^ 2 - 864 * q ^ 2 * p ^ 9 * x * z - 3888 * q ^ 3 * p ^ 7 * x ^ 2 - 5832 * q ^ 4 * p ^ 6 * x * z - 26244 * q ^ 5 * p ^ 4 * x ^ 2 - 13122 * q ^ 6 * p ^ 3 * x * z - 59049 * q ^ 7 * p * x ^ 2) * h₂ exact test_sorry /- When `linear_combination` is used to prove inequalities, its speed is very sensitive to how much typeclass inference is demanded by the lemmas it orchestrates. This example took 2146 heartbeats (and 73 ms on a good laptop) on an implementation with "minimal" typeclasses everywhere, e.g. lots of `CovariantClass`/`ContravariantClass`, and takes 206 heartbeats (10 ms on a good laptop) on the implementation at the time of joining Mathlib (November 2024). -/ set_option maxHeartbeats 1200 in example {a b : ℝ} (h : a < b) : 0 < b - a := by linear_combination (norm := skip) h exact test_sorry
.lake/packages/mathlib/MathlibTest/fun_prop2.lean
import Mathlib.Analysis.Calculus.ContDiff.Basic import Mathlib.Analysis.SpecialFunctions.Log.Deriv import Mathlib.MeasureTheory.MeasurableSpace.Basic import Mathlib.MeasureTheory.Measure.Haar.OfBasis import Mathlib.MeasureTheory.Function.StronglyMeasurable.AEStronglyMeasurable noncomputable def foo (x : ℝ) := x * (Real.log x) ^ 2 - Real.exp x / x example : ContinuousOn foo {0}ᶜ := by unfold foo; fun_prop (disch := aesop) example (y : ℝ) (hy : y ≠ 0) : ContinuousAt (fun x => x * (Real.log x) ^ 2 - Real.exp x / x) y := by fun_prop (disch := aesop) example : DifferentiableOn ℝ foo {0}ᶜ := by unfold foo; fun_prop (disch:=aesop) example (y : ℝ) (hy : y ≠ 0) : DifferentiableAt ℝ foo y := by unfold foo; fun_prop (disch:=aesop) example {n} : ContDiffOn ℝ n foo {0}ᶜ := by unfold foo; fun_prop (disch:=aesop) example {n} (y : ℝ) (hy : y ≠ 0) : ContDiffAt ℝ n foo y := by unfold foo; fun_prop (disch:=aesop) example : Continuous fun ((x, _, _) : ℝ × ℝ × ℝ) ↦ x := by fun_prop example : Continuous fun ((_, y, _) : ℝ × ℝ × ℝ) ↦ y := by fun_prop example : Continuous fun ((_, _, z) : ℝ × ℝ × ℝ) ↦ z := by fun_prop -- This theorem is meant to work together with `measurable_of_continuousOn_compl_singleton` -- Unification of `(hf : ContinuousOn f {a}ᶜ)` with this theorem determines the point `a` to be `0` @[fun_prop] theorem ContinuousOn.log' : ContinuousOn Real.log {0}ᶜ := ContinuousOn.log (by fun_prop) (by aesop) -- Notice that no theorems about measuability of log are used. It is inferred from continuity. example : Measurable (fun x => x * (Real.log x) ^ 2 - Real.exp x / x) := by fun_prop -- Notice that no theorems about measuability of log are used. It is inferred from continuity. example : AEMeasurable (fun x => x * (Real.log x) ^ 2 - Real.exp x / x) := by fun_prop (maxTransitionDepth := 2) private noncomputable def S (a b c d : ℝ) : ℝ := a / (a + b + d) + b / (a + b + c) + c / (b + c + d) + d / (a + c + d) private noncomputable def T (t : ℝ) : ℝ := S 1 (1 - t) t (t * (1 - t)) example : ContinuousOn T (Set.Icc 0 1) := by unfold T S fun_prop (disch:=(rintro x ⟨a,b⟩; nlinarith)) example : DifferentiableOn ℝ T (Set.Icc 0 1) := by unfold T S fun_prop (disch:=(rintro x ⟨a,b⟩; nlinarith)) example {n}: ContDiffOn ℝ n T (Set.Icc 0 1) := by unfold T S fun_prop (disch:=(rintro x ⟨a,b⟩; nlinarith)) example : Measurable T := by unfold T S fun_prop example : AEMeasurable T := by unfold T S fun_prop private theorem t1 : (5: ℕ) + (1 : ℕ∞) ≤ (12 : WithTop ℕ∞) := by norm_cast example {f : ℝ → ℝ} (hf : ContDiff ℝ 12 f) : Differentiable ℝ (iteratedDeriv 5 (fun x ↦ f (2 * (f (x + x))) + x)) := by fun_prop (disch := (exact t1)) -- This example used to panic due to loose bvars before #31001. -- TODO: this still fails because `fun_prop` cannot use `hl`. /-- error: `fun_prop` was unable to prove `MeasureTheory.AEStronglyMeasurable l.prod μ` Issues: No theorems found for `f` in order to prove `MeasureTheory.AEStronglyMeasurable (fun a => f a) μ` -/ #guard_msgs in example {α : Type*} {m₀ : MeasurableSpace α} {μ : MeasureTheory.Measure α} {M : Type*} [CommMonoid M] [TopologicalSpace M] [ContinuousMul M] (l : Multiset (α → M)) (hl : ∀ f ∈ l, MeasureTheory.AEStronglyMeasurable f μ) : MeasureTheory.AEStronglyMeasurable l.prod μ := by fun_prop (disch := assumption)
.lake/packages/mathlib/MathlibTest/jacobiSym.lean
import Mathlib.NumberTheory.LegendreSymbol.JacobiSymbol import Mathlib.Tactic.NormNum.Prime -- TODO(kmill): remove once the mathlib ToExpr Int instance is removed set_option eval.pp false section Csimp /-! We test that `@[csimp]` replaces `jacobiSym` (i.e. `J(· | ·)`) and `legendreSym` with `jacobiSym.fastJacobiSym` at runtime. -/ open scoped NumberTheorySymbols /-- info: -1 -/ #guard_msgs in #eval J(123 | 335) /-- info: -1 -/ #guard_msgs in #eval J(-2345 | 6789) /-- info: 1 -/ #guard_msgs in #eval J(-1 | 1655801) /-- info: -1 -/ #guard_msgs in #eval J(-102334155 | 165580141) /-- info: -1 -/ #guard_msgs in #eval J(58378362899022564339483801989973056405585914719065 | 53974350278769849773003214636618718468638750007307) instance prime_1000003 : Fact (Nat.Prime 1000003) := ⟨by norm_num1⟩ /-- info: -1 -/ #guard_msgs in #eval @legendreSym 1000003 prime_1000003 7 -- Should replace `legendreSym` with `fastJacobiSym` without using `Fact p.Prime` /-- info: 1 --- warning: declaration uses 'sorry' -/ #guard_msgs in #eval! @legendreSym (2 ^ 11213 - 1) sorry 7 end Csimp
.lake/packages/mathlib/MathlibTest/slim_check.lean
import Mathlib.Algebra.Group.Fin.Basic import Mathlib.Data.DFinsupp.Defs import Mathlib.Data.Finsupp.Notation import Mathlib.Data.Nat.Prime.Defs import Mathlib.Data.PNat.Basic import Mathlib.Tactic.Have import Mathlib.Tactic.SuccessIfFailWithMsg import Mathlib.Testing.Plausible.Functions import Mathlib.Testing.Plausible.Sampleable import Plausible private axiom test_sorry : ∀ {α}, α /-- error: =================== Found a counter-example! x := 104 guard: ⋯ issue: 104 < 100 does not hold (0 shrinks) ------------------- -/ #guard_msgs in example : ∀ x : ℕ, 2 ∣ x → x < 100 := by plausible (config := { randomSeed := some 257, maxSize := 200 }) -- example (xs : List ℕ) (w : ∃ x ∈ xs, x < 3) : true := by -- have : ∀ y ∈ xs, y < 5 -- success_if_fail_with_msg -- " -- =================== -- Found problems! -- xs := [5, 5, 0, 1] -- x := 0 -- y := 5 -- issue: 5 < 5 does not hold -- (5 shrinks) -- ------------------- -- " -- plausible (config := { randomSeed := some 257 }) -- admit -- trivial example (x : ℕ) (_h : 2 ∣ x) : true := by have : x < 100 := by success_if_fail_with_msg " =================== Found a counter-example! x := 104 guard: ⋯ issue: 104 < 100 does not hold (0 shrinks) ------------------- " plausible (config := { randomSeed := some 257, maxSize := 200 }) exact test_sorry trivial open Function Plausible -- Porting note: the "small" functor provided in mathlib3's `Sampleable.lean` was not ported, -- so we should not expect this to work. -- example (f : ℤ → ℤ) (h : Injective f) : true := by -- have : Monotone (f ∘ small.mk) -- success_if_fail_with_msg -- " -- =================== -- Found problems! -- f := [2 ↦ 3, 3 ↦ 9, 4 ↦ 6, 5 ↦ 4, 6 ↦ 2, 8 ↦ 5, 9 ↦ 8, x ↦ x] -- x := 3 -- y := 4 -- guard: 3 ≤ 4 (by construction) -- issue: 9 ≤ 6 does not hold -- (5 shrinks) -- ------------------- -- " -- plausible (config := { randomSeed := some 257 }) -- admit -- trivial /-- error: =================== Found a counter-example! f := [x ↦ x] guard: ⋯ (by construction) g := [0 => 0, 1 => 3, 2 => 1, 3 => 2, x ↦ x] guard: ⋯ (by construction) i := 1 issue: 1 = 3 does not hold (2 shrinks) ------------------- -/ #guard_msgs in example (f : ℤ → ℤ) (_h : Injective f) (g : ℤ → ℤ) (_h : Injective g) (i : ℤ) : f i = g i := by plausible (config := { randomSeed := some 257 }) /-- error: =================== Found a counter-example! f := [-2 => 8, -3 => -5, -5 => -3, 8 => -2, x ↦ x] guard: ⋯ (by construction) x := -2 y := 0 guard: -2 ≤ 0 issue: 8 ≤ 0 does not hold (7 shrinks) ------------------- -/ #guard_msgs in example (f : ℤ → ℤ) (_h : Injective f) : Monotone f := by plausible (config := { randomSeed := some 257 }) /-- error: =================== Found a counter-example! f := [_ => 0] x := 0 y := 1 guard: 0 = 0 issue: 0 = 1 does not hold (5 shrinks) ------------------- -/ #guard_msgs in example (f : ℤ → ℤ) : Injective f := by plausible (config := { randomSeed := some 257 }) /-- error: =================== Found a counter-example! f := [-2 => 5, -4 => 0, _ => 0] x := -2 y := 1 guard: -2 ≤ 1 issue: 5 ≤ 0 does not hold (3 shrinks) ------------------- -/ #guard_msgs in example (f : ℤ → ℤ) : Monotone f := by plausible (config := { randomSeed := some 257 }) -- TODO: fails without this line! attribute [-instance] Finsupp.instRepr in example (f : ℕ →₀ ℕ) : true := by have : f = 0 := by success_if_fail_with_msg " =================== Found a counter-example! f := [1 => 1, _ => 0] issue: ⋯ does not hold (1 shrinks) ------------------- " plausible (config := { randomSeed := some 257 }) exact test_sorry trivial example (f : Π₀ _n : ℕ, ℕ) : true := by have : f.update 0 0 = 0 := by success_if_fail_with_msg " =================== Found a counter-example! f := [1 => 1, _ => 0] issue: ⋯ does not hold (1 shrinks) ------------------- " plausible (config := { randomSeed := some 257 }) exact test_sorry trivial example (n : ℕ) : true := by have : ∑ f : Unit → Fin (n + 1), f () = 0 := by success_if_fail_with_msg " =================== Found a counter-example! n := 1 issue: 1 = 0 does not hold (0 shrinks) ------------------- " plausible (config := { randomSeed := some 257 }) exact test_sorry trivial -- https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/slim_check.20question/near/412709012 /-- info: Unable to find a counter-example --- warning: declaration uses 'sorry' -/ #guard_msgs in example (q : ℕ) : q = 0 ∨ q ≥ 2 ∨ 8 = ∑ k ∈ Finset.range 2, 5 ^ k * Nat.choose (2 * q + 1) (2 * k + 1) := by plausible -- https://github.com/leanprover-community/mathlib4/issues/12565 -- Make `plausible` handle `Fact` instances. /-- error: =================== Found a counter-example! a := 7 guard: ⋯ issue: ⋯ does not hold issue: ⋯ does not hold (0 shrinks) ------------------- -/ #guard_msgs in example {a : ℕ} [Fact a.Prime] : (a + 1).Prime ∨ (a + 2).Prime := by plausible (config := { randomSeed := some 257 }) /-- error: =================== Found a counter-example! x := 4 issue: 64 < 42 does not hold (0 shrinks) ------------------- -/ #guard_msgs in example (x : PNat) : x^3 < 2*x^2 + 10:= by plausible (config := { randomSeed := some 257 })
.lake/packages/mathlib/MathlibTest/cc.lean
import Mathlib.Algebra.Group.Basic import Mathlib.Algebra.Ring.Defs import Mathlib.Data.Set.Insert import Mathlib.Data.Vector.Defs import Mathlib.Tactic.CC set_option linter.unusedVariables false /-- warning: The tactic `cc` is deprecated since 2025-07-31, please use `grind` instead. Please report any regressions at https://github.com/leanprover/lean4/issues/. Note that `cc` supports some goals that `grind` doesn't, but these rely on higher-order unification and can result in unpredictable performance. If a downstream library is relying on this functionality, please report this in an issue and we'll help find a solution. -/ #guard_msgs in example (a b : Nat) : a = b → a = b := by cc -- Turn off the warning for the rest of the file set_option mathlib.tactic.cc.warning false section CC1 open List (Vector) open Mathlib.Tactic.CC open Lean Meta Elab Tactic /-- info: {{b, if b > 0 then a else b, c, a}, {ℕ, ℕ}, {True, f (b + b) b = f (a + c) c, False}, {f (b + b), f (a + c)}, {f (b + b) b, f (a + c) c}, {0, d + if b > 0 then a else b}, {b + b, a + c}} --- info: >>> Equivalence roots [b, ℕ, True, f (b + b), f (b + b) b, 0, b + b] --- info: >>> b's equivalence class [a, c, if b > 0 then a else b, b] -/ #guard_msgs in example (a b c d : Nat) (f : Nat → Nat → Nat) : a = b → b = c → d + (if b > 0 then a else b) = 0 → f (b + b) b ≠ f (a + c) c → False := by intro _ _ _ h run_tac withMainContext do let s ← CCState.mkUsingHs logInfo (toMessageData s) let some (_, t₁, t₂) ← liftM <| getFVarFromUserName `h >>= inferType >>= matchNe? | failure let b ← getFVarFromUserName `b let d ← getFVarFromUserName `d guard s.inconsistent guard (s.eqcSize b = 4) guard !(s.inSingletonEqc b) guard (s.inSingletonEqc d) logInfo (m!">>> Equivalence roots" ++ .ofFormat .line ++ toMessageData s.roots) logInfo (m!">>> b's equivalence class" ++ .ofFormat .line ++ toMessageData (s.eqcOf b)) let pr ← s.eqvProof t₁ t₂ let spr ← Term.exprToSyntax pr evalTactic <| ← `(tactic| have h := $spr; contradiction) example (a b : Nat) (f : Nat → Nat) : a = b → f a = f b := by cc example (a b : Nat) (f : Nat → Nat) : a = b → f a ≠ f b → False := by cc example (a b : Nat) (f : Nat → Nat) : a = b → f (f a) ≠ f (f b) → False := by cc example (a b c : Nat) (f : Nat → Nat) : a = b → c = b → f (f a) ≠ f (f c) → False := by cc example (a b c : Nat) (f : Nat → Nat → Nat) : a = b → c = b → f (f a b) a ≠ f (f c c) c → False := by cc example (a b c : Nat) (f : Nat → Nat → Nat) : a = b → c = b → f (f a b) a = f (f c c) c := by cc example (a b c d : Nat) : a ≍ b → b = c → c ≍ d → a ≍ d := by cc example (a b c d : Nat) : a = b → b = c → c ≍ d → a ≍ d := by cc example (a b c d : Nat) : a = b → b ≍ c → c ≍ d → a ≍ d := by cc example (a b c d : Nat) : a ≍ b → b ≍ c → c = d → a ≍ d := by cc example (a b c d : Nat) : a ≍ b → b = c → c = d → a ≍ d := by cc example (a b c d : Nat) : a = b → b = c → c = d → a ≍ d := by cc example (a b c d : Nat) : a = b → b ≍ c → c = d → a ≍ d := by cc axiom f₁ : {α : Type} → α → α → α axiom g₁ : Nat → Nat example (a b c : Nat) : a = b → g₁ a ≍ g₁ b := by cc example (a b c : Nat) : a = b → c = b → f₁ (f₁ a b) (g₁ c) = f₁ (f₁ c a) (g₁ b) := by cc example (a b c d e x y : Nat) : a = b → a = x → b = y → c = d → c = e → c = b → a = e := by cc example (f : ℕ → ℕ) (x : ℕ) (H1 : f (f (f x)) = x) (H2 : f (f (f (f (f x)))) = x) : f x = x := by cc end CC1 section CC2 axiom f₂ (a b : Nat) : a > b → Nat axiom g₂ : Nat → Nat example (a₁ a₂ b₁ b₂ c d : Nat) (H₁ : a₁ > b₁) (H₂ : a₂ > b₂) : a₁ = c → a₂ = c → b₁ = d → d = b₂ → g₂ (g₂ (f₂ a₁ b₁ H₁)) = g₂ (g₂ (f₂ a₂ b₂ H₂)) := by cc example (a₁ a₂ b₁ b₂ c d : Nat) : a₁ = c → a₂ = c → b₁ = d → d = b₂ → a₁ + b₁ + a₁ = a₂ + b₂ + c := by cc example (a b c : Prop) : (a ↔ b) → ((a ∧ (c ∨ b)) ↔ (b ∧ (c ∨ a))) := by cc example (a b c d : Prop) [d₁ : Decidable a] [d₂ : Decidable b] [d₃ : Decidable c] [d₄ : Decidable d] : (a ↔ b) → (c ↔ d) → ((if (a ∧ c) then True else False) ↔ (if (b ∧ d) then True else False)) := by cc example (a b c d : Prop) (x y z : Nat) [d₁ : Decidable a] [d₂ : Decidable b] [d₃ : Decidable c] [d₄ : Decidable d] : (a ↔ b) → (c ↔ d) → x = y → ((if (a ∧ c ∧ a) then x else y) = (if (b ∧ d ∧ b) then y else x)) := by cc end CC2 section CC3 example (a b : Nat) : (a = b ↔ a = b) := by cc example (a b : Nat) : (a = b) = (b = a) := by cc example (a b : Nat) : (a = b) ≍ (b = a) := by cc example (p : Nat → Nat → Prop) (f : Nat → Nat) (a b c d : Nat) : p (f a) (f b) → a = c → b = d → b = c → p (f c) (f c) := by cc example (p : Nat → Nat → Prop) (a b c d : Nat) : p a b → a = c → b = d → p c d := by cc example (p : Nat → Nat → Prop) (f : Nat → Nat) (a b c d : Nat) : p (f (f (f (f (f (f a)))))) (f (f (f (f (f (f b)))))) → a = c → b = d → b = c → p (f (f (f (f (f (f c)))))) (f (f (f (f (f (f c)))))) := by cc axiom R : Nat → Nat → Prop example (a b c : Nat) : a = b → R a b → R a a := by cc example (a b c : Prop) : a = b → b = c → (a ↔ c) := by cc example (a b c : Prop) : a = b → b ≍ c → (a ↔ c) := by cc example (a b c : Nat) : a ≍ b → b = c → a ≍ c := by cc example (a b c : Nat) : a ≍ b → b = c → a = c := by cc example (a b c d : Nat) : a ≍ b → b ≍ c → c ≍ d → a = d := by cc example (a b c d : Nat) : a ≍ b → b = c → c ≍ d → a = d := by cc example (a b c : Prop) : a = b → b = c → (a ↔ c) := by cc example (a b c : Prop) : a ≍ b → b = c → (a ↔ c) := by cc example (a b c d : Prop) : a ≍ b → b ≍ c → c ≍ d → (a ↔ d) := by cc def foo (a b c d : Prop) : a ≍ b → b = c → c ≍ d → (a ↔ d) := by cc example (a b c : Nat) (f : Nat → Nat) : a ≍ b → b = c → f a ≍ f c := by cc example (a b c : Nat) (f : Nat → Nat) : a ≍ b → b = c → f a = f c := by cc example (a b c d : Nat) (f : Nat → Nat) : a ≍ b → b = c → c ≍ f d → f a = f (f d) := by cc end CC3 section CC4 universe u open Mathlib axiom app : {α : Type u} → {n m : Nat} → List.Vector α m → List.Vector α n → List.Vector α (m + n) example (n1 n2 n3 : Nat) (v1 w1 : List.Vector Nat n1) (w1' : List.Vector Nat n3) (v2 w2 : List.Vector Nat n2) : n1 = n3 → v1 = w1 → w1 ≍ w1' → v2 = w2 → app v1 v2 ≍ app w1' w2 := by cc example (n1 n2 n3 : Nat) (v1 w1 : List.Vector Nat n1) (w1' : List.Vector Nat n3) (v2 w2 : List.Vector Nat n2) : n1 ≍ n3 → v1 = w1 → w1 ≍ w1' → v2 ≍ w2 → app v1 v2 ≍ app w1' w2 := by cc example (n1 n2 n3 : Nat) (v1 w1 v : List.Vector Nat n1) (w1' : List.Vector Nat n3) (v2 w2 w : List.Vector Nat n2) : n1 ≍ n3 → v1 = w1 → w1 ≍ w1' → v2 ≍ w2 → app w1' w2 ≍ app v w → app v1 v2 = app v w := by cc end CC4 section CC5 namespace LocalAxioms axiom A : Type axiom B : A → Type axiom C : (a : A) → B a → Type axiom D : (a : A) → (ba : B a) → C a ba → Type axiom E : (a : A) → (ba : B a) → (cba : C a ba) → D a ba cba → Type axiom F : (a : A) → (ba : B a) → (cba : C a ba) → (dcba : D a ba cba) → E a ba cba dcba → Type axiom C_ss : ∀ a ba, Lean.Meta.FastSubsingleton (C a ba) axiom a1 : A axiom a2 : A axiom a3 : A axiom mk_B1 : (a : _) → B a axiom mk_B2 : (a : _) → B a axiom mk_C1 : {a : _} → (ba : _) → C a ba axiom mk_C2 : {a : _} → (ba : _) → C a ba axiom tr_B : {a : _} → B a → B a axiom x : A → A axiom y : A → A axiom z : A → A axiom f : {a : A} → {ba : B a} → (cba : C a ba) → D a ba cba axiom f' : {a : A} → {ba : B a} → (cba : C a ba) → D a ba cba axiom g : {a : A} → {ba : B a} → {cba : C a ba} → (dcba : D a ba cba) → E a ba cba dcba axiom h : {a : A} → {ba : B a} → {cba : C a ba} → {dcba : D a ba cba} → (edcba : E a ba cba dcba) → F a ba cba dcba edcba attribute [instance] C_ss example : ∀ (a a' : A), a ≍ a' → mk_B1 a ≍ mk_B1 a' := by cc example : ∀ (a a' : A), a ≍ a' → mk_B2 a ≍ mk_B2 a' := by cc example : ∀ (a a' : A) (h : a = a') (b : B a), h ▸ b ≍ b := by cc example : a1 ≍ y a2 → mk_B1 a1 ≍ mk_B1 (y a2) := by cc example : a1 ≍ x a2 → a2 ≍ y a1 → mk_B1 (x (y a1)) ≍ mk_B1 (x (y (x a2))) := by cc example : a1 ≍ y a2 → mk_B1 a1 ≍ mk_B2 (y a2) → f (mk_C1 (mk_B2 a1)) ≍ f (mk_C2 (mk_B1 (y a2))) := by cc example : a1 ≍ y a2 → tr_B (mk_B1 a1) ≍ mk_B2 (y a2) → f (mk_C1 (mk_B2 a1)) ≍ f (mk_C2 (tr_B (mk_B1 (y a2)))) := by cc example : a1 ≍ y a2 → mk_B1 a1 ≍ (mk_B2 (y a2)) → g (f (mk_C1 (mk_B2 a1))) ≍ g (f (mk_C2 (mk_B1 (y a2)))) := by cc example : a1 ≍ y a2 → tr_B (mk_B1 a1) ≍ mk_B2 (y a2) → g (f (mk_C1 (mk_B2 a1))) ≍ g (f (mk_C2 (tr_B (mk_B1 (y a2))))) := by cc example : a1 ≍ y a2 → a2 ≍ z a3 → a3 ≍ x a1 → mk_B1 a1 ≍ mk_B2 (y (z (x a1))) → f (mk_C1 (mk_B2 (y (z (x a1))))) ≍ f' (mk_C2 (mk_B1 a1)) → g (f (mk_C1 (mk_B2 (y (z (x a1)))))) ≍ g (f' (mk_C2 (mk_B1 a1))) := by cc example : a1 ≍ y a2 → a2 ≍ z a3 → a3 ≍ x a1 → mk_B1 a1 ≍ mk_B2 (y (z (x a1))) → f (mk_C1 (mk_B2 (y (z (x a1))))) ≍ f' (mk_C2 (mk_B1 a1)) → f' (mk_C1 (mk_B1 a1)) ≍ f (mk_C2 (mk_B2 (y (z (x a1))))) → g (f (mk_C1 (mk_B1 (y (z (x a1)))))) ≍ g (f' (mk_C2 (mk_B2 a1))) := by cc example : a1 ≍ y a2 → a2 ≍ z a3 → a3 ≍ x a1 → tr_B (mk_B1 a1) ≍ mk_B2 (y (z (x a1))) → f (mk_C1 (mk_B2 (y (z (x a1))))) ≍ f' (mk_C2 (tr_B (mk_B1 a1))) → f' (mk_C1 (tr_B (mk_B1 a1))) ≍ f (mk_C2 (mk_B2 (y (z (x a1))))) → g (f (mk_C1 (tr_B (mk_B1 (y (z (x a1))))))) ≍ g (f' (mk_C2 (mk_B2 a1))) := by cc end LocalAxioms end CC5 section CC6 example (a b c a' b' c' : Nat) : a = a' → b = b' → c = c' → a + b + c + a = a' + b' + c' + a' := by cc example (a b : Unit) : a = b := by cc example (a b : Nat) (h₁ : a = 0) (h₂ : b = 0) : a = b → h₁ ≍ h₂ := by cc axiom inv' : (a : Nat) → a ≠ 0 → Nat example (a b : Nat) (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a = b → inv' a h₁ = inv' b h₂ := by cc example (C : Nat → Type) (f : (n : _) → C n → C n) (n m : Nat) (c : C n) (d : C m) : f n ≍ f m → c ≍ d → n ≍ m → f n c ≍ f m d := by cc end CC6 section CC7 example (f g : {α : Type} → α → α → α) (h : Nat → Nat) (a b : Nat) : h = f a → h b = f a b := by cc example (f g : {α : Type} → (a b : α) → {x : α // x ≠ b}) (h : (b : Nat) → {x : Nat // x ≠ b}) (a b₁ b₂ : Nat) : h = f a → b₁ = b₂ → h b₁ ≍ f a b₂ := by cc example (f : Nat → Nat → Nat) (a b c d : Nat) : c = d → f a = f b → f a c = f b d := by cc example (f : Nat → Nat → Nat) (a b c d : Nat) : c ≍ d → f a ≍ f b → f a c ≍ f b d := by cc end CC7 section CCAC1 example (a b c : Nat) (f : Nat → Nat) : f (a + b + c) = f (c + b + a) := by cc example (a b c : Nat) (f : Nat → Nat) : a + b = c → f (c + c) = f (a + b + c) := by cc end CCAC1 section CCAC2 example (a b c d : Nat) (f : Nat → Nat → Nat) : b + a = d → f (a + b + c) a = f (c + d) a := by cc end CCAC2 section CCAC3 example (a b c d e : Nat) (f : Nat → Nat → Nat) : b + a = d → b + c = e → f (a + b + c) (a + b + c) = f (c + d) (a + e) := by cc example (a b c d e : Nat) (f : Nat → Nat → Nat) : b + a = d + d → b + c = e + e → f (a + b + c) (a + b + c) = f (c + d + d) (e + a + e) := by cc section universe u variable {α : Type u} variable (op : α → α → α) variable [Std.Associative op] variable [Std.Commutative op] lemma ex₁ (a b c d e : α) (f : α → α → α) : op b a = op d d → op b c = op e e → f (op a (op b c)) (op (op a b) c) = f (op (op c d) d) (op e (op a e)) := by cc end end CCAC3 section CCAC4 section universe u variable {α : Type u} example (a b c d₁ d₂ e₁ e₂ : Set α) (f : Set α → Set α → Set α) : b ∪ a = d₁ ∪ d₂ → b ∪ c = e₂ ∪ e₁ → f (a ∪ b ∪ c) (a ∪ b ∪ c) = f (c ∪ d₂ ∪ d₁) (e₂ ∪ a ∪ e₁) := by cc end end CCAC4 section CCAC5 universe u variable {α : Type u} variable [CommRing α] example (x1 x2 x3 x4 x5 x6 : α) : x1*x4 = x1 → x3*x6 = x5*x5 → x5 = x4 → x6 = x2 → x1 = x1*(x6*x3) := by cc example (y1 y2 x2 x3 x4 x5 x6 : α) : (y1 + y2)*x4 = (y2 + y1) → x3*x6 = x5*x5 → x5 = x4 → x6 = x2 → (y2 + y1) = (y1 + y2)*(x6*x3) := by cc example (y1 y2 y3 x2 x3 x4 x5 x6 : α) : (y1 + y2)*x4 = (y3 + y1) → x3*x6 = x5*x5 → x5 = x4 → x6 = x2 → y2 = y3 → (y2 + y1) = (y1 + y3)*(x6*x3) := by cc end CCAC5 section CCConstructors example (a b : Nat) (s t : List Nat) : a :: s = b :: t → a ≠ b → False := by cc example (a b : Nat) (s t : List Nat) : a :: s = b :: t → t ≠ s → False := by cc example (a c b : Nat) (s t : List Nat) : a :: s = b :: t → a ≠ c → c = b → False := by cc example (a c b : Nat) (s t : List Nat) : a :: a :: s = a :: b :: t → a ≠ c → c = b → False := by cc example (a b : Nat) (s t r : List Nat) : a :: s = r → r = b :: t → a ≠ b → False := by cc example (a b : Nat) (s t r : List Nat) : a :: s = r → r = b :: t → a = b := by cc example (a b : Nat) (s t r : List Nat) : List.cons a = List.cons b → a = b := by intro h1 /- In the current implementation, `cc` does not "complete" partially applied constructor applications. So, the following one should fail. -/ try cc /- Complete it manually. TODO(Leo): we can automate it for inhabited types. -/ have h := congr_fun h1 [] cc inductive Foo | mk1 : Nat → Nat → Foo | mk2 : Nat → Nat → Foo example (a b : Nat) : Foo.mk1 a = Foo.mk2 b → False := by intro h1 /- In the current implementation, `cc` does not "complete" partially applied constructor applications. So, the following one should fail. -/ try cc have h := congr_fun h1 0 cc universe u inductive Vec (α : Type u) : Nat → Type (max 1 u) | nil : Vec α 0 | cons : ∀ {n}, α → Vec α n → Vec α (Nat.succ n) example (α : Type u) (a b c d : α) (n : Nat) (s t : Vec α n) : Vec.cons a s = Vec.cons b t → a ≠ b → False := by cc example (α : Type u) (a b c d : α) (n : Nat) (s t : Vec α n) : Vec.cons a s = Vec.cons b t → t ≠ s → False := by cc example (α : Type u) (a b c d : α) (n : Nat) (s t : Vec α n) : Vec.cons a (Vec.cons a s) = Vec.cons a (Vec.cons b t) → b ≠ c → c = a → False := by cc end CCConstructors section CCProj example (a b c d : Nat) (f : Nat → Nat × Nat) : (f d).1 ≠ a → f d = (b, c) → b = a → False := by cc def ex₂ (a b c d : Nat) (f : Nat → Nat × Nat) : (f d).2 ≠ a → f d = (b, c) → c = a → False := by cc example (a b c : Nat) (f : Nat → Nat) : (f b, c).1 ≠ f a → f b = f c → a = c → False := by cc end CCProj section CCValue example (a b : Nat) : a = 1 → b = 2 → a = b → False := by cc example (a b c : Int) : a = 1 → c = -2 → a = b → c = b → False := by cc example (a b : Char) : a = 'h' → b = 'w' → a = b → False := by cc example (a b : String) : a = "hello" → b = "world" → a = b → False := by cc example (a b c : String) : a = c → a = "hello" → c = "world" → c = b → False := by cc local instance instOfNatNat' (n : ℕ) : OfNat ℕ n where ofNat := n example : @OfNat.ofNat ℕ (nat_lit 0) (instOfNatNat _) = @OfNat.ofNat ℕ (nat_lit 0) (instOfNatNat' _) := by cc end CCValue section Config /-! Tests for the configuration options -/ /-- `ignoreInstances := false` means instances won't be unified. -/ example {G : Type*} [AddCommMonoid G] (a b : G) : @HAdd.hAdd _ _ _ (@instHAdd _ (@AddSemigroup.toAdd _ AddCommSemigroup.toAddSemigroup)) a b = @HAdd.hAdd _ _ _ (@instHAdd _ (@AddSemigroup.toAdd _ AddMonoid.toAddSemigroup)) a b := by success_if_fail_with_msg "cc tactic failed" cc -ignoreInstances -ac cc -ac end Config section Lean3Issue1442 def Rel : ℤ × ℤ → ℤ × ℤ → Prop | (n₁, d₁), (n₂, d₂) => n₁ * d₂ = n₂ * d₁ def mul' : ℤ × ℤ → ℤ × ℤ → ℤ × ℤ | (n₁, d₁), (n₂, d₂) => ⟨n₁ * n₂, d₁ * d₂⟩ example : ∀ (a b c d : ℤ × ℤ), Rel a c → Rel b d → Rel (mul' a b) (mul' c d) := fun (n₁, d₁) (n₂, d₂) (n₃, d₃) (n₄, d₄) => fun (h₁ : n₁ * d₃ = n₃ * d₁) (h₂ : n₂ * d₄ = n₄ * d₂) => show (n₁ * n₂) * (d₃ * d₄) = (n₃ * n₄) * (d₁ * d₂) by cc end Lean3Issue1442 section Lean3Issue1608 example {α : Type} {a b : α} (h : ¬ (a = b)) : b ≠ a := by cc example {α : Type} {a b : α} (h : ¬ (a = b)) : ¬ (b = a) := by cc end Lean3Issue1608 section lit example : nat_lit 0 = nat_lit 0 := by cc example : "Miyahara Kō" = "Miyahara Kō" := by cc end lit section CCPanic example (n k : ℤ) (hnk : n = 2 * k + 1) (hk : (2 * k + 1) * (2 * k + 1 + 1) = 2 * ((2 * k + 1) * (k + 1))) : n * (n + 1) = 2 * ((2 * k + 1) * (k + 1)) := by cc end CCPanic
.lake/packages/mathlib/MathlibTest/push.lean
import Mathlib.Tactic.Push import Mathlib.Data.Nat.Cast.Basic import Mathlib.Data.Set.Basic import Mathlib.Data.Set.Insert import Mathlib.Analysis.SpecialFunctions.Log.Basic private axiom test_sorry : ∀ {α}, α section logic variable {p q r : Prop} /-- info: (q ∧ (p ∨ q)) ∧ r ∧ (p ∨ r) -/ #guard_msgs in #push Or False ∧ p ∨ q ∧ r /-- info: (p ∨ q) ∧ (p ∨ r) -/ #guard_msgs in #push Or (p ∨ q) ∧ (p ∨ r) /-- info: (p ∧ q ∨ q) ∨ p ∧ r ∨ r -/ #guard_msgs in #push And (p ∨ True) ∧ (q ∨ r) example {r : ℕ → Prop} : ∀ n : ℕ, p ∨ r n ∧ q ∧ n = 1 := by push ∀ n, _ guard_target =ₛ p ∨ (∀ n, r n) ∧ q ∧ ∀ n : ℕ, n = 1 pull ∀ n, _ guard_target =ₛ ∀ n : ℕ, p ∨ r n ∧ q ∧ n = 1 exact test_sorry example {r : ℕ → Prop} : ∃ n : ℕ, p ∨ r n ∨ q ∧ n = 1 := by push ∃ n, _ guard_target =ₛ p ∨ (∃ n, r n) ∨ q ∧ True -- the lemmas `exists_or_left`/`exist_and_left` don't exist, so they can't be tagged for `pull` fail_if_success pull ∃ n, _ exact test_sorry /-- info: p ∨ ∃ x, q ∧ x = 1 -/ #guard_msgs in #pull Exists p ∨ q ∧ ∃ n : ℕ, n = 1 /-- info: DiscrTree branch for Or: (node (* => (node (False => (node #[or_false:1000])) (And => (node (* => (node (* => (node #[or_and_left:1000])))))) (True => (node #[or_true:1000])))) (False => (node (* => (node #[false_or:1000])))) (And => (node (* => (node (* => (node (* => (node #[and_or_right:1000])))))))) (True => (node (* => (node #[true_or:1000]))))) -/ #guard_msgs in #push_discr_tree Or end logic section lambda example : (fun x : ℕ ↦ x ^ 2 + 1 * 0 - 5 • 6) = id ^ 2 + 1 * 0 - 5 • 6 := by push fun x ↦ _ with_reducible rfl example : (fun x : ℕ ↦ x ^ 2 + 1 * 0 - 5 • 6) = id ^ 2 + 1 * 0 - 5 • 6 := by simp only [pushFun] example : (fun x : ℕ ↦ x ^ 2 + 1 * 0 - 5 • 6) = id ^ 2 + 1 * 0 - 5 • 6 := by pull fun _ ↦ _ with_reducible rfl example : (fun x : ℕ ↦ x ^ 2 + 1 * 0 - 5 • 6) = id ^ 2 + 1 * 0 - 5 • 6 := by simp only [pullFun] end lambda
.lake/packages/mathlib/MathlibTest/TermCongr.lean
import Mathlib.Tactic.TermCongr /-! `congr(...)` tests needing no additional imports -/ namespace Tests set_option autoImplicit true --set_option trace.Elab.congr true section congr_thms /-! Tests that `congr(...)` subsumes `congr_arg`/`congr_fun`/`congr`. Uses `(... :)` to make sure `congr(...)` is not seeing expected types. -/ example {α β : Sort _} (f : α → β) (x y : α) (h : x = y) : f x = f y := (congr(f $h) :) example {α : Sort _} {β : α → Sort _} (f g : (x : α) → β x) (h : f = g) (x : α) : f x = g x := (congr($h x) :) example {α β : Sort _} (f g : α → β) (hf : f = g) (x y : α) (hx : x = y) : f x = g y := (congr($hf $hx) :) example {α β : Sort _} (f : α → β) (x y : α) (h : x = y) : congr_arg f h = congr(f $h) := rfl example {α : Sort _} {β : α → Sort _} (f g : (x : α) → β x) (h : f = g) (x : α) : congr_fun h x = congr($h x) := rfl example {α β : Sort _} (f g : α → β) (hf : f = g) (x y : α) (hx : x = y) : congr hf hx = congr($hf $hx) := rfl end congr_thms example (f : Nat → Nat) (x y : Nat) (h : x = y) : f x = f y := by have h := congr(f $h) -- if `replace` instead of `have`, like `apply_fun f at h` guard_hyp h :ₛ f x = f y exact h example (x y : Nat) (h : x = y) : 1 + x = 1 + y := congr(_ + $h) example (x y : Nat) (h : x = y) : True := by have : 1 + x = _ := congr(_ + $h) guard_hyp this : 1 + x = 1 + y trivial example : 0 + 1 = 1 := congr(_) example : 0 + 1 = 1 := congr(_ + 1) example [Decidable p] (x y : Nat) (h : x = y) : True := by have := congr(if p then 1 else $h) guard_hyp this :ₛ (if p then 1 else x) = (if p then 1 else y) trivial example [Decidable p] (x y : Nat) (h : x = y) : True := by have := congr(if p then $h else 1) guard_hyp this :ₛ (if p then x else 1) = (if p then y else 1) trivial example (x y z w : Nat) (h : x = y) (h' : z = w) : 1 + x * z^2 = 1 + y * w^2 := by refine congr(1 + $(?_) * $(?_)^2) · exact h · exact h' example (x y z w : Nat) (h : x = y) (h' : z = w) : 1 + x * z^2 = 1 + y * w^2 := by refine congr(1 + $(?foo) * $(?bar)^2) case foo => exact h case bar => exact h' example (p q : Prop) (h : p = q) : p ↔ q := congr($h) example (p q : Prop) (h : p = q) : p ↔ q := by refine congr($(?_)) guard_target = p ↔ q exact congr($h) example (p p' q q' : Prop) (hp : p ↔ p') (hq : q ↔ q') : (p → q) ↔ (p' → q') := congr($hp → $hq) example (p q : Prop) (h : p = q) : Nonempty p = Nonempty q := congr(Nonempty $h) example (p q : Prop) (h : p ↔ q) : Nonempty p = Nonempty q := congr(Nonempty $h) example (p q : Prop) (h : p ↔ q) : Nonempty p ↔ Nonempty q := Iff.of_eq <| congr(Nonempty $h) example (p q : Prop) (h : p ↔ q) : Nonempty p ↔ Nonempty q := congr(Nonempty $h) example (a b c d e f : Nat) (hab : a = b) (hcd : c = d) (hef : e = f) : True := by have := congr(1 + $hab + $hcd * $hef) guard_hyp this : 1 + a + c * e = 1 + b + d * f trivial example (f g : Nat → Nat) (h : ∀ n, f n = g n) : (fun n => 1 + f n) = (fun n => 1 + g n) := congr(fun n => 1 + $(h n)) example (f g : Nat → Nat) (h : ∀ n, f n = g n) : (fun n => 1 + f n) = (fun n => 1 + g n) := by refine congr(fun n => 1 + $(?_)) guard_target = f n = g n apply h structure Foo where (x y : Nat) example (s : Foo) (h : 1 = 2) : True := by have := congr(({s with x := $h} : Foo)) guard_hyp this : ({ x := 1, y := s.y } : Foo) = { x := 2, y := s.y } trivial example {s t : α → Prop} (h : s = t) : (∀ (_ : Subtype s), True) ↔ (∀ (_ : Subtype t), True) := congr(∀ (_ : Subtype $h), True) example (f g : Nat → Nat → Prop) (h : f = g) : (∀ x, ∃ y, f x y) ↔ (∀ x, ∃ y, g x y) := congr(∀ x, ∃ y, $h x y) example (f g : Nat → Nat → Prop) (h : f = g) : (∀ x, ∃ y, f x y) ↔ (∀ x, ∃ y, g x y) := congr(∀ _, ∃ _, $h _ _) example (f g : Nat → Nat → Prop) (h : f = g) : (∀ x, ∃ y, f x y) ↔ (∀ x, ∃ y, g x y) := congr(∀ _, ∃ _, $(by rw [h])) namespace Overapplied /-! Overapplied functions need to be handled too. This one is for `Subtype.val`, which has arity 3, but in examples (3) and (4) it's used with arity 4. -/ def T1 (A : Nat → Type) := ∀ n, A n def T2 (A : Nat → Type) := { f : ∀ n, A n // f = f } example {A : Nat → Type} (f g : ∀ n, A n) (h : f = g) (n : Nat) : f n = g n := by have := congr($h n) -- (1) worked, not overapplied exact this example {A : Nat → Type} (f g : T1 A) (h : f = g) (n : Nat) : f n = g n := by have := congr($h n) -- (2) worked, not overapplied exact this example {A : Nat → Type} (f g : T2 A) (h : f = g) (n : Nat) : f.1 n = g.1 n := by have := congr($h.1 n) -- (3) didn't work, is overapplied exact this example {A : Nat → Type} (f g : T2 A) (h : f = g) (n : Nat) : f.1 n = g.1 n := by have hh := congr($h.1) -- works have := congr($hh n) -- (4) didn't work, is overapplied exact this /-! A couple more over-applied functions. -/ axiom fn {α : Type} (x : α) (h : x = x) : α example (f g : Nat → Nat) (h : f = g) (m n : Nat) (h' : m = n) : fn f rfl m = fn g rfl n := congr(fn $h _ $h') example (f g : Nat → Nat) (h : f = g) (m n : Nat) (h' : m = n) : fn (fn f rfl) rfl m = fn (fn g rfl) rfl n := congr(fn (fn $h _) _ $h') end Overapplied namespace SubsingletonDependence /-! The congruence theorem generator had a bug that leaked fvars. Reported at https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/what.20the.20heq.2C.20PosSemidef/near/434202080 -/ def f {α : Type} [DecidableEq α] (n : α) (_ : n = n) : α := n lemma test (n n' : Nat) (h : n = n') (hn : n = n) (hn' : n' = n') : f n hn = f n' hn' := by have := congr(f $h ‹_›) -- without expected type guard_hyp this :ₛ f n hn = f n' hn' exact congr(f $h _) -- with expected type /-! Regression test for https://github.com/leanprover-community/mathlib4/issues/25851. Make sure hcongr doesn't force the final result to be an equality. -/ example (a a' : Nat) (h : a = a') (n : Fin a) (n' : Fin a') (hn : HEq n n') : HEq (f n rfl) (f n' rfl) := congr(f (α := Fin $h) $hn rfl) end SubsingletonDependence section limitations /-! Tests to document current limitations. -/ /-- error: Cannot generate congruence because we need Subtype s to be definitionally equal to Subtype t -/ #guard_msgs in example {s t : α → Prop} (h : s = t) : (fun (n : Subtype s) => true) ≍ (fun (n : Subtype t) => true) := congr(fun (n : Subtype $h) => true) /-- error: Cannot generate congruence because we need Subtype s to be definitionally equal to Subtype t -/ #guard_msgs in example {s t : α → Prop} (h : s = t) (p : α → Prop) : (∀ (n : Subtype s), p n) ↔ (∀ (n : Subtype t), p n) := congr(∀ (n : Subtype $h), p n) end limitations end Tests
.lake/packages/mathlib/MathlibTest/ComputeDegree.lean
import Mathlib.Tactic.ComputeDegree open Polynomial variable {R : Type*} section native_mathlib4_tests variable {n : ℕ} {z : ℤ} {f : ℤ[X]} (hn : natDegree f ≤ 5) (hd : degree f ≤ 5) /-- Flows through all the matches in `compute_degree`, with a `natDegree _ ≤ _` goal. -/ example : natDegree (- C z * X ^ 5 + (monomial 2 5) ^ 2 - 0 + 1 + IntCast.intCast 1 + NatCast.natCast 1 + (z : ℤ[X]) + (n : ℤ[X]) + f) ≤ 5 := by compute_degree! example [Semiring R] : natDegree (OfNat.ofNat (OfNat.ofNat 0) : R[X]) ≤ 0 := by compute_degree /-- Flows through all the matches in `compute_degree`, with a `degree _ ≤ _` goal. -/ example : degree (- C z * X ^ 5 + (C 0 + monomial 2 5) ^ 2 - 0 + 1 + IntCast.intCast 1 + NatCast.natCast 1 + (z : ℤ[X]) + (n : ℤ[X]) + f) ≤ 5 := by set k := f with _h₀ compute_degree! /-- Flows through all the matches in `compute_degree`, with a `natDegree _ = _` goal. -/ example : natDegree (- C 1 * X ^ 5 + (C 0 + monomial 2 5) ^ 2 - 0 + 1 + IntCast.intCast 1 + NatCast.natCast 1 + (z : ℤ[X]) + (n : ℤ[X])) = 5 := by set k := f with _h₀ compute_degree! /-- Flows through all the matches in `compute_degree`, with a `degree _ = _` goal. -/ example : degree (- C 1 * X ^ 5 + (C 0 + monomial 2 5) ^ 2 - 0 + 1 + IntCast.intCast 1 + NatCast.natCast 1 + (z : ℤ[X]) + (n : ℤ[X])) = 5 := by set k := f with _h₀ compute_degree! example : degree ((C 1 * X ^ 2 + C 2 * X + C 3) * (C 0 * X ^ 0 + C 2 * X ^ 1 + C 1 * X ^ 5) ^ 4) = 22 := by compute_degree! -- Tests `Nat.cast_withBot` example [Semiring R] [Nontrivial R] {n : ℕ} : degree (X ^ n : R[X]) = n := by compute_degree! example [Nontrivial R] [Ring R] : degree (1 + X + X ^ 2 - X ^ 5 - X ^ 6 - 2 * X ^ 7 - X ^ 8 - X ^ 9 + X ^ 12 + X ^ 13 + X ^ 14 + X ^ 15 + X ^ 16 + X ^ 17 - X ^ 20 - X ^ 22 - X ^ 24 - X ^ 26 - X ^ 28 + X ^ 31 + X ^ 32 + X ^ 33 + X ^ 34 + X ^ 35 + X ^ 36 - X ^ 39 - X ^ 40 - 2 * X ^ 41 - X ^ 42 - X ^ 43 + X ^ 46 + X ^ 47 + X ^ 48 : R[X]) = 48 := by compute_degree! /-- Flows through all the matches in `compute_degree`, with a `degree _ ≤ _` goal. -/ example [Ring R] (g : R[X]) (hg : degree g ≤ 5) : degree (- C (z : R) * X ^ 5 + (monomial 2 5) ^ 2 - 0 + 1 + IntCast.intCast 1 + NatCast.natCast 1 + (z : R[X]) + (n : R[X]) + g) ≤ 5 := by set k := g with _h₀ compute_degree! example {N : WithBot ℕ} (nN : n ≤ N) : degree (- C z * X ^ n) ≤ N := by compute_degree! example [Ring R] : coeff (1 : R[X]) 0 = 1 := by compute_degree! example [Ring R] : coeff (1 : R[X]) 2 = 0 := by compute_degree! example [Ring R] : coeff (1 : R[X]) n = if n = 0 then 1 else 0 := by compute_degree! example [Ring R] (h : (0 : R) = 6) : coeff (1 : R[X]) 1 = 6 := by compute_degree! /-! Test error messages -/ /-- error: The given degree is '0'. However, * the coefficient of degree 0 may be zero * there is at least one term of naïve degree 2 * there may be a term of naïve degree 2 -/ #guard_msgs in example : natDegree (X + X ^ 2 : ℕ[X]) = 0 := by compute_degree! /-- error: 'compute_degree' inapplicable. The goal X.natDegree ≠ 0 is expected to be '≤', '<' or '='. -/ #guard_msgs in example : natDegree (X : ℕ[X]) ≠ 0 := by compute_degree! /-- error: 'compute_degree' inapplicable. The LHS must be an application of 'natDegree', 'degree', or 'coeff'. -/ #guard_msgs in example : 0 ≤ 0 := by compute_degree! /-! The following examples exhaust all the match-leaves in `direct`. -/ -- OfNat.ofNat 0 example : natDegree (0 : ℤ[X]) ≤ 5 := by compute_degree! -- OfNat.ofNat (non-zero) example : natDegree (1 : ℤ[X]) ≤ 5 := by compute_degree! -- NatCast.natCast example : natDegree (NatCast.natCast 4 : ℤ[X]) ≤ 5 := by compute_degree! -- Nat.cast example : natDegree (Nat.cast n : ℤ[X]) ≤ 5 := by compute_degree! -- IntCast.intCast example : natDegree (IntCast.intCast 4 : ℤ[X]) ≤ 5 := by compute_degree! -- Int.cast example : natDegree (Int.cast z : ℤ[X]) ≤ 5 := by compute_degree! -- Polynomial.X example : natDegree (X : ℤ[X]) ≤ 5 := by compute_degree! -- Polynomial.C example : natDegree (C n) ≤ 5 := by compute_degree! -- Polynomial.monomial example (h : n ≤ 5) : natDegree (monomial n (5 + n)) ≤ 5 := by compute_degree! -- Expr.fvar example {f : ℕ[X]} : natDegree f ≤ natDegree f := by compute_degree variable [Ring R] -- OfNat.ofNat 0 example : natDegree (0 : R[X]) ≤ 5 := by compute_degree! -- OfNat.ofNat (non-zero) example : natDegree (1 : R[X]) ≤ 5 := by compute_degree! -- NatCast.natCast example : natDegree (NatCast.natCast 4 : R[X]) ≤ 5 := by compute_degree! -- Nat.cast example : natDegree (n : R[X]) ≤ 5 := by compute_degree! -- IntCast.intCast example : natDegree (IntCast.intCast 4 : R[X]) ≤ 5 := by compute_degree! -- Int.cast example : natDegree (z : R[X]) ≤ 5 := by compute_degree! -- Polynomial.X example : natDegree (X : R[X]) ≤ 5 := by compute_degree! -- Polynomial.C example : natDegree (C n) ≤ 5 := by compute_degree! -- Polynomial.monomial example (h : n ≤ 5) : natDegree (monomial n (5 + n : R)) ≤ 5 := by compute_degree! -- Expr.fvar example {f : R[X]} : natDegree f ≤ natDegree f := by compute_degree example {R} [Semiring R] [Nontrivial R] : Monic (1 * X ^ 5 + X ^ 6 * monomial 10 1 : R[X]) := by monicity! end native_mathlib4_tests section tests_from_mathlib3 variable {R : Type _} [Semiring R] {a b c d e : R} -- test that `mdata` does not get in the way of the tactic example : natDegree (7 * X : R[X]) ≤ 1 := by have : 0 ≤ 1 := zero_le_one compute_degree -- possibly only a vestigial test from mathlib3: maybe to check for `instantiateMVars`? example {R : Type _} [Ring R] (h : ∀ {p q : R[X]}, p.natDegree ≤ 0 → (p * q).natDegree = 0) : natDegree (- 1 * 1 : R[X]) = 0 := by apply h _ compute_degree -- check for making sure that `compute_degree` is `focus`ed example : Polynomial.natDegree (Polynomial.C 4) ≤ 1 ∧ True := by constructor compute_degree! trivial example {R : Type _} [Ring R] : natDegree (- 1 * 1 : R[X]) ≤ 0 := by compute_degree example {F} [Ring F] {a : F} : natDegree (X ^ 9 - C a * X ^ 10 : F[X]) ≤ 10 := by compute_degree example : degree (X + (X * monomial 2 1 + X * X) ^ 2) ≤ 10 := by compute_degree! example : natDegree (7 * X : R[X]) ≤ 1 := by compute_degree example : natDegree (0 : R[X]) ≤ 0 := by compute_degree example : natDegree (1 : R[X]) ≤ 0 := by compute_degree example : natDegree (2 : R[X]) ≤ 0 := by compute_degree example {n : ℕ} : natDegree ((n : Nat) : R[X]) ≤ 0 := by compute_degree example {R} [Ring R] {n : ℤ} : natDegree ((n : ℤ) : R[X]) ≤ 0 := by compute_degree example {R} [Ring R] {n : ℕ} : natDegree ((- n : ℤ) : R[X]) ≤ 0 := by compute_degree example : natDegree (monomial 5 c * monomial 1 c + monomial 7 d + C a * X ^ 0 + C b * X ^ 5 + C c * X ^ 2 + X ^ 10 + C e * X) ≤ 10 := by compute_degree example : natDegree (monomial 0 c * (monomial 0 c * C 1) + monomial 0 d + C 1 + C a * X ^ 0) ≤ 0 := by compute_degree example {F} [Ring F] : natDegree (X ^ 4 + 3 : F[X]) ≤ 4 := by compute_degree example {F} [Ring F] : natDegree ((-1) • X ^ 4 + 3 : F[X]) ≤ 4 := by compute_degree example : natDegree ((5 * X * C 3 : _root_.Rat[X]) ^ 4) ≤ 4 := by compute_degree example : natDegree ((C a * X) ^ 4) ≤ 4 := by compute_degree example : degree ((X : ℤ[X]) ^ 4) ≤ 4 := by compute_degree example : natDegree ((X : ℤ[X]) ^ 4) ≤ 40 := by compute_degree! example : natDegree (C a * C b + X + monomial 3 4 * X) ≤ 4 := by compute_degree example {F} [Ring F] {a : F} : natDegree (X ^ 3 + C a * X ^ 10 : F[X]) ≤ 10 := by compute_degree example : natDegree (7 * X : R[X]) ≤ 1 := by compute_degree example {a : R} : natDegree (a • X ^ 5 : R[X]) ≤ 5 := by compute_degree example : natDegree (2 • X ^ 5 : R[X]) ≤ 5 := by compute_degree example {a : R} (a0 : a ≠ 0) : natDegree (a • X ^ 5 + X : R[X]) = 5 := by compute_degree! example {a : R} (a0 : a ≠ 0) : degree (a • X ^ 5 + X ^ 2 : R[X]) = 5 := by compute_degree!; rfl end tests_from_mathlib3 variable [CommRing R] [Nontrivial R] in example : (X ^ 2 + 2 • X + C 1 : R[X]).natDegree = 2 := by compute_degree! simp [coeff_X] example : (C 1 + X * 3 * (X + 3) ^ 4 : Polynomial ℤ).natDegree < 10 := by compute_degree! example : (C 1 + X * 3 * (X + 3) ^ 4 : Polynomial ℤ).degree < 10 := by compute_degree! variable [CommRing R] in example : (X ^ 2 + 2 • X + C 1 : R[X]).natDegree < 3 := by compute_degree!
.lake/packages/mathlib/MathlibTest/StacksAttribute.lean
import Mathlib.Tactic.StacksAttribute import Mathlib.Util.ParseCommand /-- info: No tags found. -/ #guard_msgs in #stacks_tags namespace X @[stacks A04Q "A comment", kerodon B15R "Also a comment"] theorem tagged : True := .intro end X /-- info: some ([Stacks Tag A04Q](https://stacks.math.columbia.edu/tag/A04Q) (A comment) [Kerodon Tag B15R](https://kerodon.net/tag/B15R) (Also a comment)) -/ #guard_msgs in run_cmd Lean.logInfo m!"{← Lean.findDocString? (← Lean.getEnv) `X.tagged}" #guard_msgs in @[stacks 0BR2, kerodon 0X12] example : True := .intro @[stacks 0BR2, stacks 0X14 "I can also have a comment"] example : True := .intro @[stacks 0X14 "I can also have a comment"] example : True := .intro @[stacks 0BR2, stacks 0X14 "I can also have a comment"] example : True := .intro @[stacks 0X14 "I can also have a comment"] example : True := .intro /-- error: <input>:1:3: Stacks tags must be exactly 4 characters -/ #guard_msgs in #parse Mathlib.StacksTag.stacksTagFn => "A05" /-- error: <input>:1:4: Stacks tags must consist only of digits and uppercase letters. -/ #guard_msgs in #parse Mathlib.StacksTag.stacksTagFn => "A05b" /-- info: 0BD5 -/ #guard_msgs in #parse Mathlib.StacksTag.stacksTagFn => "0BD5" /-- info: [Stacks Tag A04Q](https://stacks.math.columbia.edu/tag/A04Q) corresponds to declaration 'X.tagged'. (A comment) -/ #guard_msgs in #stacks_tags /-- info: [Stacks Tag A04Q](https://stacks.math.columbia.edu/tag/A04Q) corresponds to declaration 'X.tagged'. (A comment) True -/ #guard_msgs in #stacks_tags! /-- info: [Stacks Tag B15R](https://kerodon.net/tag/B15R) corresponds to declaration 'X.tagged'. (Also a comment) True -/ #guard_msgs in #kerodon_tags! section errors open Lean Parser Mathlib.StacksTag def captureException (env : Environment) (s : ParserFn) (input : String) : Except String Syntax := let ictx := mkInputContext input "<input>" let s := s.run ictx { env, options := {} } (getTokenTable env) (mkParserState input) if !s.allErrors.isEmpty then .error (s.toErrorMsg ictx) else if ictx.atEnd s.pos then .ok s.stxStack.back else .error ((s.mkError "end of input").toErrorMsg ictx) /-- error: <input>:1:3: Stacks tags must be exactly 4 characters -/ #guard_msgs in run_cmd do let _ ← Lean.ofExcept <| captureException (← getEnv) stacksTagFn "A05" /-- error: <input>:1:4: Stacks tags must consist only of digits and uppercase letters. -/ #guard_msgs in run_cmd do let _ ← Lean.ofExcept <| captureException (← getEnv) stacksTagFn "aaaa" /-- error: <input>:1:0: expected stacks tag -/ #guard_msgs in run_cmd do let env ← getEnv let _ ← Lean.ofExcept <| captureException env stacksTagFn "\"A04Q\"" end errors
.lake/packages/mathlib/MathlibTest/ClearValue.lean
import Mathlib.Tactic.Basic example : let x := 22; 0 ≤ x := by intro x clear_value x fail_if_success clear_value x exact Nat.zero_le _ example : let x := 22; let y : Fin x := 0; y.1 < x := by intro x y fail_if_success clear_value x clear_value y clear_value x fail_if_success clear_value x fail_if_success clear_value y exact y.2 example : let x := 22; let y : Fin x := 0; y.1 < x := by intro x y fail_if_success clear_value x -- 0 depends on `x = Nat.succ _` clear_value y x fail_if_success clear_value x fail_if_success clear_value y exact y.2 example : let x := 22; let y : Fin x := 0; y.1 < x := by intro x y fail_if_success clear_value x clear_value x y fail_if_success clear_value x fail_if_success clear_value y exact y.2 example : let x := 22; let y : Nat := x; let z : Fin (y + 1) := 0; z.1 < y + 1 := by intro x y z clear_value x -- `0` depends on `x` but its OK exact z.2 example : let x := 22; let y : Nat := x; let z : Fin (y + 1) := 0; z.1 < y + 1 := by intro x y z clear_value y -- `0` depends on `y` but its OK exact z.2
.lake/packages/mathlib/MathlibTest/Contrapose.lean
import Mathlib.Tactic.Basic import Mathlib.Tactic.Contrapose example (p q : Prop) (h : ¬q → ¬p) : p → q := by contrapose guard_target = ¬q → ¬p exact h example (p q : Prop) (h : p) (hpq : ¬q → ¬p) : q := by contrapose h guard_target = ¬p exact hpq h example (p q : Prop) (h : p) (hpq : ¬q → ¬p) : q := by contrapose h with h' guard_target = ¬p exact hpq h' example (p q : Prop) (h : q → p) : ¬p → ¬q := by contrapose! guard_target = q → p exact h example (p q : Prop) (h : ¬p) (hpq : q → p) : ¬q := by contrapose! h guard_target = p exact hpq h example (p q : Prop) (h : ¬p) (hpq : q → p) : ¬q := by contrapose! h with h' guard_target = p exact hpq h' example (p : Prop) (h : p) : p := by fail_if_success { contrapose } exact h example (p q : Type) (h : p → q) : p → q := by fail_if_success { contrapose } exact h
.lake/packages/mathlib/MathlibTest/finiteness.lean
import Mathlib.Analysis.SpecialFunctions.Pow.NNReal import Mathlib.Data.ENNReal.Inv import Mathlib.MeasureTheory.Measure.Typeclasses.Finite import Mathlib.Tactic.Finiteness open MeasureTheory Set open scoped ENNReal NNReal example : (1 : ℝ≥0∞) < ∞ := by finiteness example : (3 : ℝ≥0∞) ≠ ∞ := by finiteness example (a : ℝ) (b : ℕ) : ENNReal.ofReal a + b < ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) : a + 3 < ∞ := by finiteness example {a : ℝ≥0∞} (ha : a < ∞) : a + 3 < ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) : a ^ 10 ≠ ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) : a / 10 + 5 ≠ ∞ := by finiteness example {a b : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≠ 0) : a / b ≠ ∞ := by finiteness example {a : ℝ≥0∞} {b : ℝ≥0} (ha : a ≠ ∞) (hb : b ≠ 0) : a / b ≠ ∞ := by finiteness example {a b : ℝ≥0∞} (ha : a ≠ ∞) : a / (b + 5) ≠ ∞ := by finiteness example {a b : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≠ ∞) : a * b ≠ ∞ := by finiteness example {a : ℝ≥0∞} (ha : 0 < a) : a⁻¹ ≠ ∞ := by finiteness -- not supported yet -- example {t a : ℝ≥0∞} (ht : t ∈ Ioo 0 1) (this : a ≠ 0) : t * a ≠ ∞ := by finiteness -- example {t a : ℝ≥0∞} (ht : t ∈ Ioo 0 1) (this : a ≠ 0) : a * t ≠ ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) : a ^ 10 ≠ ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) (ha' : a ≠ 0) : a ^ (10 : ℤ) ≠ ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) : a ^ (10 : ℝ) ≠ ∞ := by finiteness example : (0 : ℝ≥0∞) ^ (10 : ℝ) ≠ ∞ := by finiteness example {a : ℝ≥0∞} (ha : a ≠ 0) (ha' : a ≠ ∞) : a ^ (-10 : ℝ) ≠ ∞ := by finiteness example {a : ℝ} : (10 : ℝ≥0∞) ^ a ≠ ∞ := by finiteness example {a : ℝ≥0∞} {t : ℝ} (ha : a ≠ 0) (ha : a ≠ ∞) : a ^ t ≠ ∞ := by finiteness example {a : ℝ≥0∞} : a * a⁻¹ ≠ ∞ := by finiteness example {a : ℝ≥0∞} : a⁻¹ * a ≠ ∞ := by finiteness example {a : ℝ≥0∞} : 2 * (a⁻¹ * a) ≠ ∞ := by finiteness example {a : ℝ≥0∞} : 2 * a⁻¹ * a ≠ ∞ := by rw [mul_assoc]; finiteness example {a : ℝ≥0∞} (ha : a ≠ ∞) : max a 37 ≠ ∞ := by finiteness example {a b : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≠ ∞) : max a b < ∞ := by finiteness /-- Test that `finiteness_nonterminal` makes progress but does not fail on not closing the goal. -/ example {a : ℝ≥0∞} (ha : a = 0) : a + 3 < ∞ := by finiteness_nonterminal; simp [ha] example (a : ℝ) : (ENNReal.ofReal (1 + a ^ 2))⁻¹ < ∞ := by finiteness example {α : Type*} (f : α → ℕ) : ∀ i, (f i : ℝ≥0∞) ≠ ∞ := by finiteness example {α} {_ : MeasurableSpace α} (μ : Measure α) [IsFiniteMeasure μ] (s : Set α) : μ s ≠ ∞ := by finiteness
.lake/packages/mathlib/MathlibTest/MinImports.lean
import Mathlib.Tactic.NormNum.Basic import Mathlib.Tactic.FunProp.Attr open Lean.Elab.Command Mathlib.Command.MinImports in run_cmd liftTermElabM do guard ([`A, `A.B.C_3, `A.B.C_2, `A.B.C_1, `A.B.C_0, `A.B.C].map previousInstName == [`A, `A.B.C_2, `A.B.C_1, `A.B.C, `A.B.C, `A.B.C]) run_cmd let stx ← `(variable (a : Nat) in theorem TestingAttributes : a = a := rfl) let nm ← Mathlib.Command.MinImports.getDeclName stx if `TestingAttributes != nm.eraseMacroScopes then Lean.logWarning "Possible misparsing of declaration modifiers!" /-- info: import Mathlib.Tactic.FunProp.Attr -/ #guard_msgs in #min_imports in (← `(declModifiers|@[fun_prop])) -- check that `scoped` attributes do not cause elaboration problems /-- info: import Lean.Parser.Command -/ #guard_msgs in #min_imports in @[scoped simp] theorem XX.YY : 0 = 0 := rfl namespace X /-- info: import Mathlib.Algebra.Ring.Nat -/ #guard_msgs in #min_imports in protected def xxx : Semiring Nat := inferInstance end X /-- info: import Mathlib.Algebra.Ring.Nat -/ #guard_msgs in #min_imports in -- If `#min_imports` were parsing just the syntax, the imports would be `Mathlib/Algebra/Ring/Defs.lean`. instance : Semiring Nat := inferInstance /-- info: import Mathlib.Algebra.Ring.Nat -/ #guard_msgs in #min_imports in instance withName : Semiring Nat := inferInstance /-- info: import Mathlib.Algebra.Ring.Nat -/ #guard_msgs in #min_imports in noncomputable instance : Semiring Nat := inferInstance /-- info: ℤ : Type --- info: import Mathlib.Data.Int.Notation -/ #guard_msgs in #min_imports in #check ℤ /-- info: import Mathlib.Data.Int.Notation -/ #guard_msgs in #min_imports in ℤ /-- info: import Batteries.Tactic.Init import Batteries.Tactic.PermuteGoals import Mathlib.Tactic.ExtractGoal -/ #guard_msgs in #min_imports in (← `(tactic| extract_goal; on_goal 1 => _)) /-- info: import Mathlib.Tactic.NormNum.Basic -/ #guard_msgs in #min_imports in lemma uses_norm_num : (0 + 1 : ℕ) = 1 := by norm_num /-- info: import Mathlib.Tactic.NormNum.Basic -/ #guard_msgs in #min_imports in uses_norm_num /-- info: import Mathlib.Tactic.Lemma import Mathlib.Data.Nat.Notation --- info: theorem hi.extracted_1_1 (n : ℕ) : n = n := sorry -/ #guard_msgs in #min_imports in lemma hi (n : ℕ) : n = n := by extract_goal; rfl section Variables /-- info: import Mathlib.Data.Nat.Notation -/ #guard_msgs in #min_imports in def confusableName : (1 : ℕ) = 1 := rfl variable {R : Type*} [Semiring R] -- Don't get confused by unused variables. variable {K : Type*} [Field K] namespace Namespace -- The dependency on `Semiring` is only found in the `variable` declaration. -- We find it by looking up the declaration by name and checking the term, -- which used to get confused if running in a namespace. /-- info: import Mathlib.Algebra.Ring.Defs -/ #guard_msgs in #min_imports in protected def confusableName : (1 : R) = 1 := rfl end Namespace end Variables section Linter.MinImports set_option linter.minImports.increases false set_option linter.minImports false /-- info: Counting imports from here. -/ #guard_msgs in #import_bumps /-- warning: Imports increased to [Init.Guard, Mathlib.Data.Int.Notation] New imports: [Init.Guard, Mathlib.Data.Int.Notation] Note: This linter can be disabled with `set_option linter.minImports false` -/ #guard_msgs in #guard (0 : ℤ) = 0 #guard_msgs in -- no new imports needed here, so no message #guard (0 : ℤ) = 0 set_option linter.minImports false in #reset_min_imports /-- warning: Imports increased to [Init.Guard, Mathlib.Data.Int.Notation] New imports: [Init.Guard, Mathlib.Data.Int.Notation] Note: This linter can be disabled with `set_option linter.minImports false` -/ #guard_msgs in -- again, the imports pick-up, after the reset #guard (0 : ℤ) = 0 /-- warning: Imports increased to [Mathlib.Tactic.Linter.MinImports] New imports: [Mathlib.Tactic.Linter.MinImports] Note: This linter can be disabled with `set_option linter.minImports false` -/ #guard_msgs in #reset_min_imports /-- warning: Imports increased to [Mathlib.Tactic.NormNum.Basic] New imports: [Mathlib.Tactic.NormNum.Basic] Now redundant: [Mathlib.Tactic.Linter.MinImports] Note: This linter can be disabled with `set_option linter.minImports false` -/ #guard_msgs in run_cmd let _ ← `(declModifiers|@[fun_prop]) let _ ← `(tactic|apply @Mathlib.Meta.NormNum.evalNatDvd <;> extract_goal) end Linter.MinImports section Linter.UpstreamableDecl set_option linter.upstreamableDecl true /-- warning: Consider moving this declaration to the module Mathlib.Data.Nat.Notation. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in theorem propose_to_move_this_theorem : (0 : ℕ) = 0 := rfl -- By default, the linter does not warn on definitions. def dont_propose_to_move_this_def : ℕ := 0 set_option linter.upstreamableDecl.defs true in /-- warning: Consider moving this declaration to the module Mathlib.Data.Nat.Notation. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in def propose_to_move_this_def : ℕ := 0 -- This theorem depends on a local definition, so should not be moved. #guard_msgs in theorem theorem_with_local_def : propose_to_move_this_def = 0 := rfl -- This definition depends on definitions in two different files, so should not be moved. /-- warning: Consider moving this declaration to the module Mathlib.Tactic.NormNum.Basic. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in theorem theorem_with_multiple_dependencies : True := let _ := Mathlib.Meta.FunProp.funPropAttr let _ := Mathlib.Meta.NormNum.evalNatDvd trivial -- Private declarations shouldn't get a warning by default. private theorem private_theorem : (0 : ℕ) = 0 := rfl -- But we can enable the option. set_option linter.upstreamableDecl.private true in /-- warning: Consider moving this declaration to the module Mathlib.Data.Nat.Notation. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in private theorem propose_to_move_this_private_theorem : (0 : ℕ) = 0 := rfl -- Private declarations shouldn't get a warning by default, even if definitions get a warning. set_option linter.upstreamableDecl.defs true in private def private_def : ℕ := 0 -- But if we enable both options, they should. set_option linter.upstreamableDecl.defs true in set_option linter.upstreamableDecl.private true in /-- warning: Consider moving this declaration to the module Mathlib.Data.Nat.Notation. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in private def propose_to_move_this_private_def : ℕ := 0 /-! Structures and inductives should be treated just like definitions: no warnings by default, unless we enable the option. -/ structure DontProposeToMoveThisStructure where foo : ℕ inductive DontProposeToMoveThisInductive where | foo : ℕ → DontProposeToMoveThisInductive | bar : ℕ → DontProposeToMoveThisInductive → DontProposeToMoveThisInductive set_option linter.upstreamableDecl.defs true /-- warning: Consider moving this declaration to the module Mathlib.Data.Nat.Notation. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in structure ProposeToMoveThisStructure where foo : ℕ /-- warning: Consider moving this declaration to the module Mathlib.Data.Nat.Notation. Note: This linter can be disabled with `set_option linter.upstreamableDecl false` -/ #guard_msgs in inductive ProposeToMoveThisInductive where | foo : ℕ → ProposeToMoveThisInductive | bar : ℕ → ProposeToMoveThisInductive → ProposeToMoveThisInductive -- This theorem depends on a local inductive, so should not be moved. #guard_msgs in theorem theorem_with_local_inductive : Nonempty ProposeToMoveThisInductive := ⟨.foo 0⟩ end Linter.UpstreamableDecl
.lake/packages/mathlib/MathlibTest/Use.lean
import Mathlib.Tactic.Use import Mathlib.Tactic.Basic namespace UseTests set_option autoImplicit true example : ∃ x : Nat, x = x := by use 42 -- Since `Eq` is an inductive type, `use` naturally handles it when applying the constructor. /-- error: too many arguments supplied to `use` -/ #guard_msgs in example : ∃ x : Nat, x = x := by use 42, rfl example : ∃ x : Nat, x = x := by use! 42 example (h : 42 = y) : ∃ x : Nat, x = y := by use 42, h -- `trivial` uses `assumption` example (h : 42 = y) : ∃ x : Nat, x = y := by use 42 -- Check that `use` inserts a coercion: /-- info: Try this: [apply] Exists.intro (↑n) (Eq.refl ↑n) -/ #guard_msgs (info) in example (n : Fin 3) : ∃ x : Nat, x = x := show_term by use n example : ∃ x : Nat, ∃ y : Nat, x = y := by use 42, 42 /-- error: failed to synthesize OfNat (Nat × Nat) 42 numerals are polymorphic in Lean, but the numeral `42` cannot be used in a context where the expected type is Nat × Nat due to the absence of the instance above Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in example : ∃ p : Nat × Nat, p.1 = p.2 := by use 42; sorry example : ∃ p : Nat × Nat, p.1 = p.2 := by use! 42, 42 example : ∃ p : Nat × Nat, p.1 = p.2 := by use! (42, 42) example (r : Nat → Nat → Prop) (h : ∀ x, r x x) : ∃ p : Nat × Nat, r p.1 p.2 := by use! 42; use! 42; apply h example (r : Nat → Nat → Prop) (h : ∀ x, r x x) : ∃ p : Nat × Nat, r p.1 p.2 := by use! 42, 42; apply h example (r : Nat → Nat → Prop) (h : ∀ x, r x x) : ∃ p : Nat × Nat, r p.1 p.2 := by use! 42, 42, h _ example : ∃ x : String × String, x.1 = x.2 := by use ("a", "a") example : ∃ x : String × String, x.1 = x.2 := by use! "a", "a" -- This example is why `use` always tries applying the constructor before refining. example : ∃ x : Nat, x = x := by use ?_ exact 42 example (α : Type) : ∃ S : List α, S = S := by use ∅ example : ∃ x : Int, x = x := by use 42 example : ∃ a b c : Int, a + b + c = 6 := by use 1, 2, 3 rfl example : ∃ p : Int × Int, p.1 = 1 := by use ⟨1, 42⟩ example : ∃ p : Int × Int, p.1 = 1 := by use! 1, 42 example : ∃ n : Int, n * 3 = 3 * 2 := by use 2 rfl example : Σ _x _y : Int, Int × Int × Int := by use 1, 2, 3, 4, 5 example : Σ _x _y : Int, (Int × Int) × Int := by use! 1, 2, 3, 4, 5 -- There are two constructors, so applying a constructor fails and it tries to just refine /-- error: failed to synthesize OfNat (Option Nat) 1 numerals are polymorphic in Lean, but the numeral `1` cannot be used in a context where the expected type is Option Nat due to the absence of the instance above Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in example : Option Nat := by use 1 /-- error: failed to synthesize OfNat (Nat → Nat) 1 numerals are polymorphic in Lean, but the numeral `1` cannot be used in a context where the expected type is Nat → Nat due to the absence of the instance above Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in example : Nat → Nat := by use 1 inductive foo | mk : Nat → Bool × Nat → Nat → foo example : foo := by use 100, ⟨true, 4⟩, 3 example : foo := by use! 100, true, 4, 3 /-- info: Try this: [apply] foo.mk 100 (true, 4) 3 -/ #guard_msgs in -- `use` puts placeholders after the current goal example : foo := show_term by use ?x, ⟨?b, 4⟩ exact (3 : Nat) exact (100 : Nat) exact true /-- info: Try this: [apply] foo.mk 100 (true, 4) 3 -/ #guard_msgs in -- `use` puts placeholders after the current goal example : foo := show_term by -- Type ascriptions keep refinement from occurring before applying the constructor use! (?x : Nat), (?b : Bool), 4 exact (3 : Nat) exact (100 : Nat) exact true -- `use!` is helpful for auto-flattening quantifiers over subtypes example : ∃ p : {x : Nat // 0 < x}, 1 < p.1 := by use! 2 <;> decide inductive Baz : Nat → Nat → Prop | cons (x : Nat) : Baz 0 x example : Baz 0 3 := by use _ example : Baz 0 3 := by use 3 /-- error: argument is not definitionally equal to inferred value 3 -/ #guard_msgs in example : Baz 0 3 := by use 4 -- Could not apply constructor due to defeq check /-- error: Type mismatch 3 has type Nat of sort `Type` but is expected to have type Baz 1 3 of sort `Prop` -/ #guard_msgs in example : Baz 1 3 := by use (3 : Nat) structure DecidableType where (α : Type) [deq : DecidableEq α] -- Auto-synthesizes instance example : DecidableType := by use Nat example (β : Type) [DecidableEq β] : DecidableType := by use β -- Leaves instances as extra goals when synthesis fails noncomputable example (β : Type) : DecidableType := by use β guard_target = DecidableEq β apply Classical.typeDecidableEq -- https://github.com/leanprover-community/mathlib4/issues/5072 example (n : Nat) : Nat := by use n structure Embedding (α β : Sort _) where toFun : α → β inj : ∀ x y, toFun x = toFun y → x = y example (α : Type u) : Embedding α α × Unit := by constructor -- testing that `use` actually focuses on the main goal use id · simp constructor -- FIXME Failing tests ported from mathlib3 -- Note(kmill): mathlib3 `use` would try to rewrite any lingering existentials with -- `exists_prop` to turn them into conjunctions. It did not do this recursively. set_option linter.style.longLine false in -- example : ∃ (n : Nat) (h : n > 0), n = n := by -- use 1 -- -- goal should now be `1 > 0 ∧ 1 = 1`, whereas it would be `∃ (H : 1 > 0), 1 = 1` after existsi 1. -- guard_target = 1 > 0 ∧ 1 = 1 -- exact ⟨Nat.zero_lt_one, rfl⟩ -- This feature might not be useful anymore since bounded `Exists` already uses a conjunction. example : ∃ n > 0, n = n := by use (discharger := skip) 1 guard_target = 1 > 0 ∧ 1 = 1 decide -- The discharger knows about `exists_prop`. example (h1 : 1 > 0) : ∃ (n : Nat) (_h : n > 0), n = n := by use 1 -- Regression test: `use` needs to ensure it does calculations inside the correct local contexts example : let P : Nat → Prop := fun _x => ∃ _n : Nat, True; P 1 := by intro P use 1 /-- error: invalid occurrence of `·` notation, it must be surrounded by parentheses (e.g. `(· + 1)`) --- error: unsolved goals case h ⊢ sorry 1 = 1 -/ #guard_msgs in example : ∃ f : Nat → Nat, f 1 = 1 := by use · example : ∃ f : Nat → Nat, f 1 = 1 := by use (·) end UseTests
.lake/packages/mathlib/MathlibTest/Variable.lean
import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.Field.Defs import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Ring.Regular import Mathlib.Algebra.Module.LinearMap.Basic import Mathlib.RingTheory.UniqueFactorizationDomain.Basic import Mathlib.Tactic.Variable set_option autoImplicit true namespace Tests -- Note about tests: these are just testing how `variable?` works, and for the algebra hierarchy -- there is no guarantee that we are testing for the correct typeclass instances. Results -- might have instances that somehow overlap (for example having multiple incompatible operations). -- set_option trace.variable? true section -- works like `variable` when there are no missing instances /-- info: Try this: [apply] variable? (x : Nat) [DecidableEq α] => (x : Nat) [DecidableEq α] -/ #guard_msgs in variable? (x : Nat) [DecidableEq α] example : Nat := x example : DecidableEq α := inferInstance end section /-- info: Try this: [apply] variable? [Module R M] => [Semiring R] [AddCommMonoid M] [Module R M] -/ #guard_msgs in variable? [Module R M] example : Semiring R := inferInstance example : AddCommMonoid M := inferInstance example : Module R M := inferInstance end section #guard_msgs in variable? [Module R M] => [Semiring R] [AddCommMonoid M] [Module R M] example : Semiring R := inferInstance example : AddCommMonoid M := inferInstance example : Module R M := inferInstance end section /-- warning: Calculated binders do not match the expected binders given after `=>`. --- info: Try this: [apply] variable? [Module R M] => [Semiring R] [AddCommMonoid M] [Module R M] -/ #guard_msgs in variable? [Module R M] => [Ring R] [AddCommMonoid M] [Module R M] end section /-- error: failed to synthesize Semiring R Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in variable? [Module R M] => [Module R M] end section /-- warning: Calculated binders do not match the expected binders given after `=>`. --- info: Try this: [apply] variable? [Semiring R] => [Semiring R] -/ #guard_msgs in variable? [Semiring R] => [Ring R] end namespace foo class A (α : Type) class B (α : Type) [A α] class C (α : Type) [A α] [B α] /-- info: Try this: [apply] variable? [C α] => [A α] [B α] [C α] -/ #guard_msgs in variable? [C α] /-- info: Try this: [apply] variable? [(α : Type) → C α] => [(α : Type) → A α] [(α : Type) → B α] [(α : Type) → C α] -/ #guard_msgs in variable? [(α : Type) → C α] end foo section -- Example of some bad instances (TODO?) -- There are two different add operations on `A`. -- See also the next test. /-- info: Try this: [apply] variable? [Module R A] [Algebra S A] => [Semiring R] [AddCommMonoid A] [Module R A] [CommSemiring S] [Semiring A] [Algebra S A] -/ #guard_msgs in variable? [Module R A] [Algebra S A] end section -- Similar to the previous test, but this time there is only a single add operation on `A`. /-- info: Try this: [apply] variable? [Algebra S A] [Module R A] => [CommSemiring S] [Semiring A] [Algebra S A] [Semiring R] [Module R A] -/ #guard_msgs in variable? [Algebra S A] [Module R A] end section /-- info: Try this: [apply] variable? (f : Nat → Type) [∀ i, Module R (f i)] => (f : Nat → Type) [Semiring R] [(i : ℕ) → AddCommMonoid (f i)] [∀ i, Module R (f i)] -/ #guard_msgs in variable? (f : Nat → Type) [∀ i, Module R (f i)] example : ∀ i, AddCommMonoid (f i) := inferInstance end section /-- warning: Instance argument can be inferred from earlier arguments. f : ℕ → Type inst✝ : (i : ℕ) → AddCommMonoid (f i) ⊢ (i : ℕ) → Module ℕ (f i) --- info: Try this: [apply] variable? (f : Nat → Type) [∀ i, Module Nat (f i)] => (f : Nat → Type) [(i : ℕ) → AddCommMonoid (f i)] -/ #guard_msgs in variable? (f : Nat → Type) [∀ i, Module Nat (f i)] -- Warning since [(i : ℕ) → AddCommMonoid (f i)] is sufficient end section /-- info: Try this: [apply] variable? [Algebra k V] => [CommSemiring k] [Semiring V] [Algebra k V] -/ #guard_msgs in variable? [Algebra k V] end section -- Checking that `Algebra` doesn't introduce its own `CommSemiring k`. /-- info: Try this: [apply] variable? [Field k] [Algebra k A] => [Field k] [Semiring A] [Algebra k A] -/ #guard_msgs in variable? [Field k] [Algebra k A] example : (inferInstance : Field k).toCommSemiring = (inferInstance : CommSemiring k) := rfl end /-- Alias for introducing a vector space using `variable?`. -/ @[variable_alias] structure VectorSpace (k V : Type _) [Field k] [AddCommGroup V] [Module k V] section /-- info: Try this: [apply] variable? [VectorSpace R M] => [Field R] [AddCommGroup M] [Module R M] -/ #guard_msgs in variable? [VectorSpace R M] example : Field R := inferInstance example : AddCommGroup M := inferInstance example : Module R M := inferInstance end section /-- info: Try this: [apply] variable? [VectorSpace k V] [Algebra k V] => [Field k] [AddCommGroup V] [Module k V] [Semiring V] [Algebra k V] -/ #guard_msgs in variable? [VectorSpace k V] [Algebra k V] example : Field k := inferInstance example : AddCommGroup V := inferInstance example : Module k V := inferInstance example : Semiring V := inferInstance example : Algebra k V := inferInstance end section #guard_msgs in variable? [VectorSpace k V] [Algebra k V] => [Field k] [AddCommGroup V] [Module k V] [Semiring V] [Algebra k V] end /-- `variable?` alias for a representation of an algebra over a field. -/ @[variable_alias] structure Rep (k A V : Type _) [Field k] [AddCommGroup A] [Ring A] [Algebra k A] [AddCommGroup V] [Module k V] [DistribMulAction A V] [SMulCommClass k A V] section /-- info: Try this: [apply] variable? [Rep k A V] => [Field k] [AddCommGroup A] [Ring A] [Algebra k A] [AddCommGroup V] [Module k V] [DistribMulAction A V] [SMulCommClass k A V] -/ #guard_msgs in variable? [Rep k A V] end section /-- info: Try this: [apply] variable? [VectorSpace k A] [Rep k A V] => [Field k] [AddCommGroup A] [Module k A] [Ring A] [Algebra k A] [AddCommGroup V] [Module k V] [DistribMulAction A V] [SMulCommClass k A V] -/ #guard_msgs in variable? [VectorSpace k A] [Rep k A V] end section -- This fails due to requiring `Module k A` with two different sets of instance arguments (TODO?) -- variable? [Rep k A V] [VectorSpace k A] end @[variable_alias] structure UniqueFactorizationDomain (R : Type _) [CommRing R] [IsDomain R] [UniqueFactorizationMonoid R] section /-- info: Try this: [apply] variable? [UniqueFactorizationDomain R] => [CommRing R] [IsDomain R] [UniqueFactorizationMonoid R] -/ #guard_msgs in variable? [UniqueFactorizationDomain R] end section #guard_msgs in variable? [UniqueFactorizationDomain R] => [CommRing R] [IsDomain R] [UniqueFactorizationMonoid R] end section /-- error: invalid binder annotation, type is not a class instance UniqueFactorizationDomain R Note: Use the command `set_option checkBinderAnnotations false` to disable the check -/ #guard_msgs in variable? [UniqueFactorizationDomain R] => [CommRing R] [IsDomain R] [UniqueFactorizationMonoid R] [UniqueFactorizationDomain R] end section /-! Test that unused variables are not reported for the variable list either before or after `=>`. -/ /-- info: Try this: [apply] variable? {α : _} => {α : _} -/ #guard_msgs in variable? {α : _} #guard_msgs in variable? {α : _} => {α : _} end section /-! Test that `Sort*` works. This creates new universe level variables, so we need to be sure that the state is reset when testing what comes after the `=>`. -/ class foo (β : Nat → Sort*) [CoeFun Nat (fun _ ↦ ∀ a : Nat, β a)] where #guard_msgs in variable? {β : Sort*} [i : foo fun _ ↦ β] => {β : Sort*} [CoeFun Nat fun _ ↦ (a : Nat) → (fun _ ↦ β) a] [i : foo fun _ ↦ β] end end Tests
.lake/packages/mathlib/MathlibTest/Find.lean
import Mathlib.Tactic.Find theorem add_comm_zero {n} : 0 + n = n + 0 := Nat.add_comm _ _ -- These results are too volatile to test with `#guard_msgs`, -- we just suppress them for now. #guard_msgs (drop info) in #find _ + _ = _ + _ #guard_msgs (drop info) in #find ?n + _ = _ + ?n #guard_msgs (drop info) in #find (_ : Nat) + _ = _ + _ #guard_msgs (drop info) in #find Nat → Nat #guard_msgs (drop info) in #find ?n ≤ ?m → ?n + _ ≤ ?m + _
.lake/packages/mathlib/MathlibTest/DualNumber.lean
import Mathlib.Algebra.DualNumber open DualNumber -- TODO(kmill): make a ToExpr instance for DualNumber set_option eval.pp false /-- info: 0 + 0*ε -/ #guard_msgs in #eval (0 : ℕ[ε]) /-- info: 2 + 0*ε -/ #guard_msgs in #eval (2 : ℕ[ε]) /-- info: 6 + 0*ε -/ #guard_msgs in #eval (2 + 4 : ℕ[ε]) /-- info: 2 + 0*ε + (0 + 0*ε)*ε -/ #guard_msgs in #eval (2 : (ℕ[ε])[ε])
.lake/packages/mathlib/MathlibTest/ImplicitUniverses.lean
import Mathlib.Tactic.TypeStar import Mathlib.Tactic.SuccessIfFailWithMsg /-- error: Type mismatch _y has type Type u_2 of sort `Type (u_2 + 1)` but is expected to have type Type u_1 of sort `Type (u_1 + 1)` -/ #guard_msgs in noncomputable example (_x _y : Type*) : sorry := by exact _x = _y /-- error: Type mismatch Prop has type Type of sort `Type 1` but is expected to have type Sort u_1 of sort `Type u_1` -/ #guard_msgs in noncomputable example (_x : Sort*) : sorry := by exact _x = Prop /-- error: Type mismatch y has type Type u_2 of sort `Type (u_2 + 1)` but is expected to have type Type u_1 of sort `Type (u_1 + 1)` -/ #guard_msgs in noncomputable example : sorry := by exact ∀ x y : Type*, x = y /-- error: Type mismatch Prop has type Type of sort `Type 1` but is expected to have type Sort u_1 of sort `Type u_1` -/ #guard_msgs in noncomputable example : sorry := by exact ∀ x : Sort*, x = Prop
.lake/packages/mathlib/MathlibTest/random.lean
import Mathlib.Control.Random import Mathlib.Tactic.NormNum import Batteries.Lean.LawfulMonad open Random -- TODO(kmill): remove once the mathlib ToExpr Int instance is removed set_option eval.pp false section Rand /-- info: 33 -/ #guard_msgs in #eval do IO.runRandWith 257 (show Rand _ from do let i ← randBound Int 3 30 (by norm_num) let j ← randBound Int 4 40 (by norm_num) return i.1 + j.1) -- using higher universes /-- info: ULift.up 19 -/ #guard_msgs in #eval do IO.runRandWith 257 (show Rand _ from do let i ← rand (ULift.{3} (Fin 20)) return i) end Rand -- using the monad transformer section RandT /-- info: Got 6 Got 27 --- info: 33 -/ #guard_msgs in #eval show IO _ from do IO.runRandWith 257 (do let i ← randBound Int 3 30 (by norm_num) IO.println s!"Got {i}" let j ← randBound Int 4 40 (by norm_num) IO.println s!"Got {j}" return i.1 + j.1) /-- info: Got 6 Got 27 --- info: 33 -/ #guard_msgs in #eval show Lean.Meta.MetaM _ from do IO.runRandWith 257 (do let i ← randBound Int 3 30 (by norm_num) IO.println s!"Got {i}" let j ← randBound Int 4 40 (by norm_num) IO.println s!"Got {j}" return i.1 + j.1) -- test that `MetaM` can access the global random number generator /-- info: Got 4 Got 4 --- info: 8 -/ #guard_msgs in #eval show Lean.Meta.MetaM _ from do IO.runRand (do -- since we don't know the seed, we use a trivial range here for determinism let i ← randBound Int 4 4 (by norm_num) IO.println s!"Got {i}" let j ← randBound Int 4 4 (by norm_num) IO.println s!"Got {i}" return i.1 + j.1) /-- info: GOOD -/ #guard_msgs in #eval show IO _ from do IO.runRandWith 257 <| do let mut count := 0 for _ in [:10000] do if (← randFin : Fin 3) == 1 then count := count + 1 if Float.abs (0.333 - (count / Nat.toFloat 10000)) < 0.01 then IO.println "GOOD" else IO.println "BAD" /-- info: GOOD -/ #guard_msgs in #eval show IO _ from do IO.runRandWith 257 <| do let mut count := 0 for _ in [:10000] do if (← randFin : Fin 2) == 0 then count := count + 1 if Float.abs (0.5 - (count / Nat.toFloat 10000)) < 0.01 then IO.println "GOOD" else IO.println "BAD" /-- info: GOOD -/ #guard_msgs in #eval show IO _ from do IO.runRandWith 257 <| do let mut count := 0 for _ in [:10000] do if (← randFin : Fin 10) == 5 then count := count + 1 if Float.abs (0.1 - (count / Nat.toFloat 10000)) < 0.01 then IO.println "GOOD" else IO.println "BAD" end RandT
.lake/packages/mathlib/MathlibTest/MoveAdd.lean
import Mathlib.Tactic.MoveAdd import Mathlib.Algebra.Ring.Nat import Mathlib.Data.Nat.Basic universe u variable {R : Type u} section add section semigroup variable [AddCommSemigroup R] {a b c d e f g h : R} example (h : a + b + c = d) : b + (a + c) = d := by move_add [← a] assumption example : let k := c + (a + b); k = a + b + c := by move_add [← a, c] rfl example (h : b + a = b + c + a) : a + b = a + b + c := by move_add [a]; assumption example [Mul R] [Neg R] : a + (b + c + a) * (- (d + e) + e) + f + g = (c + b + a) * (e + - (e + d)) + g + f + a := by move_add [b, d, g, f, a, e]; rfl example (h : d + b + a = b + a → d + c + a = c + a) : a + d + b = b + a → d + c + a = a + c := by move_add [a] assumption example [DecidableEq R] : if b + a = c + a then a + b = c + a else a + b ≠ c + a := by move_add [← a] split_ifs with h <;> exact h example (h : a + c + b = a + d + b) : c + b + a = b + a + d := by move_add [← a, b] -- Goal before `exact h`: `a + c + b = a + d + b` exact h example [Mul R] (h : a * c + c + b * c = a * d + d + b * d) : c + b * c + a * c = a * d + d + b * d := by -- the first input `_ * c` unifies with `b * c` and moves to the right -- the second input `_ * c` unifies with `a * c` and moves to the left move_add [_ * c, ← _ * c] -- Goal before `exact h`: `a * c + c + b * c = a * d + d + b * d` exact h end semigroup variable [AddCommMonoidWithOne R] [Mul R] {f g h X r s t u : R} (C D E : R → R) example (he : E (C r * D X + D X * h + 7 + 42 + f) = C r * D X + h * D X + 7 + 42 + g) : E (7 + f + (C r * D X + 42) + D X * h) = C r * D X + h * D X + g + 7 + 42 := by -- move `7, 42, f, g` to the right of their respective sides move_add [(7 : R), (42 : R), f, g] assumption end add section mul example [CommSemigroup R] (a b c d : R) (h : a * b * c = d) : b * (a * c) = d := by move_mul [← a] assumption example (a b : ℕ) : a + max a b = max b a + a := by move_oper Max.max [← a] move_oper HAdd.hAdd [a] rfl example {R : Type u} [CommSemigroup R] {a b : R} : ∀ x : R, ∃ y : R, a * x * b * y = x * y * b * a := by move_mul [a, b] exact fun x ↦ ⟨x, rfl⟩ example {R : Type u} [Add R] [CommSemigroup R] {a b c d e f g : R} : a * (b * c * a) * ((d * e) * e) * f * g = (c * b * a) * (e * (e * d)) * g * f * a := by move_mul [a, a, b, c, d, e, f] rfl /- # Sample usage of `move_oper` -/ example (a b c : Prop) : a ∧ b ∧ c ↔ b ∧ c ∧ a := by move_oper And [a] rfl example (a b c : Prop) : a ∨ b ∨ c ↔ b ∨ c ∨ a := by move_oper Or [a] rfl example {R} [LinearOrder R] (a b c : R) : max (max a b) c = max (max b c) a := by move_oper Max.max [a] rfl example {R} [LinearOrder R] (a b c : R) : min (min a b) c = min (min b c) a := by move_oper Min.min [a] rfl end mul section left_assoc example {a b c d e : Prop} (h : a ∧ b ∧ c ∧ d ∧ e) : a ∧ c ∧ e ∧ b ∧ d := by move_oper And [a, b, c, d, e] exact h example {a b c d e : Prop} (h : a ∨ b ∨ c ∨ d ∨ e) : a ∨ c ∨ e ∨ b ∨ d := by move_oper Or [a, b, c, d, e] exact h end left_assoc example (k : ℕ) (h0 : 0 + 2 = 9 + 0) (h9 : k + 2 = k + 9) : k + 2 = 9 + k := by induction k with | zero => exact h0 | succ n _ => move_add [9] exact h9 -- Testing internals of the tactic `move_add`. section tactic open Mathlib.MoveAdd #guard (uniquify [ 0, 1, 0, 1, 0, 3] = [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (3, 0)]) #guard (let dat := [(0, true), (1, false), (2, true)] #[0, 1, 2, 3, 4].qsort (weight dat · ≤ weight dat ·) = #[0, 2, 3, 4, 1]) #guard false = ( reorderUsing [0, 1, 2] [(0, false)] = [1, 2, 0] && reorderUsing [0, 1, 2] [(1, true)] = [1, 0, 2] && reorderUsing [0, 1, 2] [(1, true), (0, false)] != [1, 2, 0]) #guard reorderUsing [1, 5, 4, 3, 2, 1] [(3, true), (2, false), (1, false)] = [3, 5, 4, 1, 2, 1] end tactic
.lake/packages/mathlib/MathlibTest/linarith.lean
import Mathlib.Tactic.Linarith import Mathlib.Tactic.Linarith.Oracle.FourierMotzkin import Mathlib.Algebra.BigOperators.Group.Finset.Pi import Mathlib.Algebra.Order.Ring.Rat import Mathlib.Order.Interval.Finset.Nat private axiom test_sorry : ∀ {α}, α set_option linter.unusedVariables false set_option autoImplicit false open Lean.Elab.Tactic in def testSorryTac : TacticM Unit := do let e ← getMainTarget let t ← `(test_sorry) closeMainGoalUsing `sorry fun _ _ => elabTerm t e example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] {a b : α} (h : a < b) (w : b < a) : False := by linarith example {α : Type} (_inst : (a : Prop) → Decidable a) [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] {a b c : α} (ha : a < 0) (hb : ¬b = 0) (hc' : c = 0) (h : (1 - a) * (b * b) ≤ 0) (hc : 0 ≤ 0) (w : -(a * -b * -b + b * -b + 0) = (1 - a) * (b * b)) (h : (1 - a) * (b * b) ≤ 0) : 0 < 1 - a := by linarith example (e b c a v0 v1 : Rat) (h1 : v0 = 5 * a) (h2 : v1 = 3 * b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (e b c a v0 v1 : α) (h1 : v0 = 5 * a) (h2 : v1 = 3 * b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example (h : (1 : ℤ) < 0) (g : ¬ (37 : ℤ) < 42) (_k : True) (l : (-7 : ℤ) < 5): (3 : ℤ) < 7 := by linarith [(rfl : 0 = 0)] example (u v r s t : Rat) (h : 0 < u * (t * v + t * r + s)) : 0 < (t * (r + v) + s) * 3 * u := by linarith example (A B : Rat) (h : 0 < A * B) : 0 < 8*A*B := by linarith example (A B : Rat) (h : 0 < A * B) : 0 < A*8*B := by linarith example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (x : α) : 0 ≤ x := by have h : 0 ≤ x := test_sorry linarith example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (x : α) : 0 ≤ x := by have h : 0 ≤ x := test_sorry linarith [h] example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (u v r s t : α) (h : 0 < u * (t * v + t * r + s)) : 0 < (t*(r + v) + s)*3*u := by linarith example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (A B : α) (h : 0 < A * B) : 0 < 8*A*B := by linarith example (s : Set ℕ) (_h : s = ∅) : 0 ≤ 1 := by linarith section cancel_denoms example (A B : Rat) (h : 0 < A * B) : 0 < A*B/8 := by linarith example (A B : Rat) (h : 0 < A * B) : 0 < A/8*B := by linarith example (ε : Rat) (h1 : ε > 0) : ε / 2 + ε / 3 + ε / 7 < ε := by linarith example (x y z : Rat) (h1 : 2 * x < 3 * y) (h2 : -4 * x + z / 2 < 0) (h3 : 12 * y - z < 0) : False := by linarith example (ε : Rat) (h1 : 0 < ε) : ε / 2 < ε := by linarith example (ε : Rat) (h1 : 0 < ε) : ε / 3 + ε / 3 + ε / 3 = ε := by linarith -- Make sure special case for division by 1 is handled: example (x : Rat) (h : 0 < x) : 0 < x/1 := by linarith -- Make sure can divide by -1 example (x : Rat) (h : x < 0) : 0 < x/(-1) := by linarith example (x : Rat) (h : x < 0) : 0 < x/(-2) := by linarith example (x : Rat) (h : x < 0) : 0 < x/(-4/5) := by linarith example (x : Rat) (h : 0 < x) : 0 < x/2/3 := by linarith example (x : Rat) (h : 0 < x) : 0 < x/(2/3) := by linarith variable {K : Type*} [Field K] [LinearOrder K] [IsStrictOrderedRing K] example (a : K) (ha : 10 / (8 + 2) ≤ a) : 1 ≤ a := by linarith example (a : K) (ha : 10 / 10 ^ 1 ≤ a) : 1 ≤ a := by linarith example (a : K) (ha : 10⁻¹ * 10 ≤ a) : 1 ≤ a := by linarith example (a : K) (ha : 1.0 ≤ a) : 1 ≤ a := by linarith end cancel_denoms example (a b c : Rat) (h2 : b + 2 > 3 + b) : False := by linarith (config := {discharger := do Lean.Elab.Tactic.evalTactic (←`(tactic| ring1))}) example (a b c : Rat) (h2 : b + 2 > 3 + b) : False := by linarith -- We haven't implemented `restrict_type` yet. -- example (a b c : ℚ) (x y : ℤ) (h1 : x ≤ 3*y) (h2 : b + 2 > 3 + b) : false := -- by linarith (restrict_type := ℚ) example (g v V c h : Rat) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0) (h5 : 0 ≤ c) (h6 : c < 1) : v ≤ V := by linarith example (x y z : ℤ) (h1 : 2 * x < 3 * y) (h2 : -4 * x + 2 * z < 0) (h3 : 12 * y - 4 * z < 0) : False := by linarith example (x y z : ℤ) (h1 : 2 * x < 3 * y) (h2 : -4 * x + 2 * z < 0) (h3 : x * y < 5) (h3 : 12 * y - 4 * z < 0) : False := by linarith example (a b c : Rat) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10) (h4 : a + b - c < 3) : False := by linarith example (a b c : Rat) (h2 : b > 0) (h3 : ¬ b ≥ 0) : False := by linarith example (x y z : Rat) (hx : x ≤ 3 * y) (h2 : y ≤ 2 * z) (h3 : x ≥ 6 * z) : x = 3*y := by linarith example (x y z : ℤ) (h1 : 2 * x < 3 * y) (h2 : -4 * x + 2 * z < 0) (h3 : x * y < 5) : ¬ 12*y - 4* z < 0 := by linarith example (x y z : Rat) (hx : ¬ x > 3 * y) (h2 : ¬ y > 2 * z) (h3 : x ≥ 6 * z) : x = 3 * y := by linarith example (x y : Rat) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x ≥ 0) (h'' : (6 + 3 * y) * y ≥ 0) : False := by linarith example (a : Rat) (ha : 0 ≤ a) : 0 * 0 ≤ 2 * a := by linarith example (x y : Rat) (h : x < y) : x ≠ y := by linarith example (x y : Rat) (h : x < y) : ¬ x = y := by linarith -- Check `linarith!` works as expected example (x : Rat) : id x ≥ x := by fail_if_success linarith linarith! opaque Nat.prime : ℕ → Prop example (x y z : Rat) (h1 : 2 * x + ((-3) * y) < 0) (h2 : (-4) * x + 2 * z < 0) (h3 : 12 * y + (-4) * z < 0) (h4 : Nat.prime 7) : False := by linarith example (x y z : Rat) (h1 : 2 * 1 * x + (3) * (y * (-1)) < 0) (h2 : (-2) * x * 2 < -(z + z)) (h3 : 12 * y + (-4) * z < 0) (h4 : Nat.prime 7) : False := by linarith example (w x y z : ℤ) (h1 : 4 * x + (-3) * y + 6 * w ≤ 0) (h2 : (-1) * x < 0) (h3 : y < 0) (h4 : w ≥ 0) (h5 : Nat.prime x.natAbs) : False := by linarith section term_arguments example (x : Rat) (hx : x > 0) (h : x.num < 0) : False := by linarith [Rat.num_pos.mpr hx, h] example (x : Rat) (hx : x > 0) (h : x.num < 0) : False := by fail_if_success linarith fail_if_success linarith only [h] fail_if_success linarith only [Rat.num_pos.mpr hx] linarith only [Rat.num_pos.mpr hx, h] end term_arguments example (i n : ℕ) (h : (2 : ℤ) ^ i ≤ 2 ^ n) : (0 : ℤ) ≤ 2 ^ n - 2 ^ i := by linarith -- Check we use `exfalso` on non-comparison goals. example (a b c : Rat) (h2 : b > 0) (h3 : b < 0) : Nat.prime 10 := by linarith example (a b c : Rat) (h2 : (2 : Rat) > 3) : a + b - c ≥ 3 := by linarith -exfalso -- Verify that we split conjunctions in hypotheses. example (x y : Rat) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3 ∧ (x + 4) * x ≥ 0 ∧ (6 + 3 * y) * y ≥ 0) : False := by fail_if_success linarith -splitHypotheses linarith example (h : 1 < 0) (g : ¬ 37 < 42) (k : True) (l : (-7 : ℤ) < 5) : 3 < 7 := by linarith [(rfl : 0 = 0)] example (h : 1 < 0) : 3 = 7 := by linarith [Int.zero_lt_one] example (h1 : (1 : ℕ) < 1) : False := by linarith example (a b c : ℕ) : a + b ≥ a := by linarith example (a b i : ℕ) (h1 : ¬ a < i) (h2 : b < i) (h3 : a ≤ b) : False := by linarith example (x y : ℕ) (h : x < 3 * y) : True := by zify at h trivial example : (Nat.cast 2 : ℤ) = 2 := Nat.cast_ofNat example (x y z : ℕ) (hx : x ≤ 3 * y) (h2 : y ≤ 2 * z) (h3 : x ≥ 6 * z) : x = 3 * y := by linarith example (a b c : ℕ) : ¬ a + b < a := by linarith example (n : ℕ) (h1 : n ≤ 3) (h2 : n > 2) : n = 3 := by linarith example (z : ℕ) (hz : ¬ z ≥ 2) (h2 : ¬ z + 1 ≤ 2) : False := by linarith example (z : ℕ) (hz : ¬ z ≥ 2) : z + 1 ≤ 2 := by linarith example (i : ℤ) (hi : i > 5) : 2 * i + 3 > 11 := by linarith example (m : ℕ) : m * m + m + (2 * m + 2) = m * m + m + (m + 1) + (m + 1) := by linarith example (mess : ℕ → ℕ) (S n : ℕ) : mess S + (n * mess S + n * 2 + 1) < n * mess S + mess S + (n * 2 + 2) := by linarith example (p n p' n' : ℕ) (h : p + n' = p' + n) : n + p' = n' + p := by linarith example (a b c : ℚ) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c := by linarith example (N : ℕ) (n : ℕ) (Hirrelevant : n > N) (A : Rat) (l : Rat) (h : A - l ≤ -(A - l)) (h_1 : ¬A ≤ -A) (h_2 : ¬l ≤ -l) (h_3 : -(A - l) < 1) : A < l + 1 := by linarith example (d : Rat) (q n : ℕ) (h1 : ((q : Rat) - 1) * n ≥ 0) (h2 : d = 2 / 3 * (((q : Rat) - 1) * n)) : d ≤ ((q : Rat) - 1) * n := by linarith example (d : Rat) (q n : ℕ) (h1 : ((q : Rat) - 1) * n ≥ 0) (h2 : d = 2 / 3 * (((q : Rat) - 1) * n)) : ((q : Rat) - 1)*n - d = 1/3 * (((q : Rat) - 1) * n) := by linarith example (x y z : ℚ) (hx : x < 5) (hx2 : x > 5) (hy : y < 5000000000) (hz : z > 34 * y) : false := by linarith only [hx, hx2] example (x y z : ℚ) (hx : x < 5) (hy : y < 5000000000) (hz : z > 34 * y) : x ≤ 5 := by linarith only [hx] /- The speed of `linarith` is very sensitive to how much typeclass inference is demanded by the lemmas it orchestrates. This example took 3318 heartbeats (and 110 ms on a good laptop) on an earlier implementation, which in several places used off-the-shelf library lemmas requiring low-level typeclasses, e.g. `Add{Left,Right}{Strict}Mono`. After a tweak to rely only on custom `linarith` clones of these lemmas taking high-level typeclasses, this now (November 2024) takes 1647 heartbeats (63 ms on a good laptop). -/ set_option maxHeartbeats 2000 in example {K : Type*} [Field K] [LinearOrder K] [IsStrictOrderedRing K] {x y : K} (h : x + y = 10) (h' : x + 2 * y ≤ 18) (h : x < 2) : False := by linarith (config := {discharger := testSorryTac}) example (u v x y A B : ℚ) (a : 0 < A) (a_1 : 0 <= 1 - A) (a_2 : 0 <= B - 1) (a_3 : 0 <= B - x) (a_4 : 0 <= B - y) (a_5 : 0 <= u) (a_6 : 0 <= v) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : u * y + v * x + u * v < 3 * A * B := by nlinarith example (u v x y A B : ℚ) : (0 < A) → (A ≤ 1) → (1 ≤ B) → (x ≤ B) → (y ≤ B) → (0 ≤ u ) → (0 ≤ v ) → (u < A) → (v < A) → (u * y + v * x + u * v < 3 * A * B) := by intros nlinarith example (u v x y A B : Rat) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : (0 <= A * (1 - A)) -> (0 <= A * (B - 1)) -> (0 < A * (A - u)) -> (0 <= (B - 1) * (A - u)) -> (0 <= (B - 1) * (A - v)) -> (0 <= (B - x) * v) -> (0 <= (B - y) * u) -> (0 <= u * (A - v)) -> u * y + v * x + u * v < 3 * A * B := by intros linarith example (u v x y A B : Rat) (a : 0 < A) (a_1 : 0 <= 1 - A) (a_2 : 0 <= B - 1) (a_3 : 0 <= B - x) (a_4 : 0 <= B - y) (a_5 : 0 <= u) (a_6 : 0 <= v) (a_7 : 0 < A - u) (a_8 : 0 < A - v) : (0 < A * A) -> (0 <= A * (1 - A)) -> (0 <= A * (B - 1)) -> (0 <= A * (B - x)) -> (0 <= A * (B - y)) -> (0 <= A * u) -> (0 <= A * v) -> (0 < A * (A - u)) -> (0 < A * (A - v)) -> (0 <= (1 - A) * A) -> (0 <= (1 - A) * (1 - A)) -> (0 <= (1 - A) * (B - 1)) -> (0 <= (1 - A) * (B - x)) -> (0 <= (1 - A) * (B - y)) -> (0 <= (1 - A) * u) -> (0 <= (1 - A) * v) -> (0 <= (1 - A) * (A - u)) -> (0 <= (1 - A) * (A - v)) -> (0 <= (B - 1) * A) -> (0 <= (B - 1) * (1 - A)) -> (0 <= (B - 1) * (B - 1)) -> (0 <= (B - 1) * (B - x)) -> (0 <= (B - 1) * (B - y)) -> (0 <= (B - 1) * u) -> (0 <= (B - 1) * v) -> (0 <= (B - 1) * (A - u)) -> (0 <= (B - 1) * (A - v)) -> (0 <= (B - x) * A) -> (0 <= (B - x) * (1 - A)) -> (0 <= (B - x) * (B - 1)) -> (0 <= (B - x) * (B - x)) -> (0 <= (B - x) * (B - y)) -> (0 <= (B - x) * u) -> (0 <= (B - x) * v) -> (0 <= (B - x) * (A - u)) -> (0 <= (B - x) * (A - v)) -> (0 <= (B - y) * A) -> (0 <= (B - y) * (1 - A)) -> (0 <= (B - y) * (B - 1)) -> (0 <= (B - y) * (B - x)) -> (0 <= (B - y) * (B - y)) -> (0 <= (B - y) * u) -> (0 <= (B - y) * v) -> (0 <= (B - y) * (A - u)) -> (0 <= (B - y) * (A - v)) -> (0 <= u * A) -> (0 <= u * (1 - A)) -> (0 <= u * (B - 1)) -> (0 <= u * (B - x)) -> (0 <= u * (B - y)) -> (0 <= u * u) -> (0 <= u * v) -> (0 <= u * (A - u)) -> (0 <= u * (A - v)) -> (0 <= v * A) -> (0 <= v * (1 - A)) -> (0 <= v * (B - 1)) -> (0 <= v * (B - x)) -> (0 <= v * (B - y)) -> (0 <= v * u) -> (0 <= v * v) -> (0 <= v * (A - u)) -> (0 <= v * (A - v)) -> (0 < (A - u) * A) -> (0 <= (A - u) * (1 - A)) -> (0 <= (A - u) * (B - 1)) -> (0 <= (A - u) * (B - x)) -> (0 <= (A - u) * (B - y)) -> (0 <= (A - u) * u) -> (0 <= (A - u) * v) -> (0 < (A - u) * (A - u)) -> (0 < (A - u) * (A - v)) -> (0 < (A - v) * A) -> (0 <= (A - v) * (1 - A)) -> (0 <= (A - v) * (B - 1)) -> (0 <= (A - v) * (B - x)) -> (0 <= (A - v) * (B - y)) -> (0 <= (A - v) * u) -> (0 <= (A - v) * v) -> (0 < (A - v) * (A - u)) -> (0 < (A - v) * (A - v)) -> u * y + v * x + u * v < 3 * A * B := by intros linarith example (A B : ℚ) : (0 < A) → (1 ≤ B) → (0 < A / 8 * B) := by intros nlinarith example (x y : ℚ) : 0 ≤ x ^2 + y ^2 := by nlinarith example (x y : ℚ) : 0 ≤ x*x + y*y := by nlinarith example (x y : ℚ) : x = 0 → y = 0 → x*x + y*y = 0 := by intros nlinarith lemma norm_eq_zero_iff {x y : ℚ} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := by constructor · intro constructor <;> nlinarith · intro; nlinarith lemma norm_zero_left {x y : ℚ} (h1 : x * x + y * y = 0) : x = 0 := by nlinarith lemma norm_nonpos_right {x y : ℚ} (h1 : x * x + y * y ≤ 0) : y = 0 := by nlinarith lemma norm_nonpos_left (x y : ℚ) (h1 : x * x + y * y ≤ 0) : x = 0 := by nlinarith section variable {E : Type _} [AddGroup E] example (f : ℤ → E) (h : 0 = f 0) : 1 ≤ 2 := by nlinarith example (a : E) (h : a = a) : 1 ≤ 2 := by nlinarith end -- Note: This takes a huge amount of time with the Fourier-Motzkin oracle example (a1 a2 a3 b1 b2 b3 c1 c2 c3 d1 d2 d3 : ℕ) (h1 : a1 + a2 + a3 = b1 + b2 + b3) (h2 : c1 + c2 + c3 = d1 + d2 + d3) : a1 * c1 + a2 * c1 + a3 * c1 + a1 * c2 + a2 * c2 + a3 * c2 + a1 * c3 + a2 * c3 + a3 * c3 = b1 * d1 + b2 * d1 + b3 * d1 + b1 * d2 + b2 * d2 + b3 * d2 + b1 * d3 + b2 * d3 + b3 * d3 := by nlinarith --(oracle := some .fourierMotzkin) -- This should not be slower than the example below with the Fourier-Motzkin oracle example (p q r s t u v w : ℕ) (h1 : p + u = q + t) (h2 : r + w = s + v) : p * r + q * s + (t * w + u * v) = p * s + q * r + (t * v + u * w) := by nlinarith example (p q r s t u v w : ℕ) (h1 : p + u = q + t) (h2 : r + w = s + v) : p * r + q * s + (t * w + u * v) = p * s + q * r + (t * v + u * w) := by nlinarith (oracle := .fourierMotzkin) section -- Tests involving a norm, including that squares in a type where `sq_nonneg` does not apply -- do not cause an exception variable {R : Type _} [Ring R] (abs : R → ℚ) axiom abs_nonneg' : ∀ r, 0 ≤ abs r example (t : R) (a b : ℚ) (h : a ≤ b) : abs (t^2) * a ≤ abs (t^2) * b := by nlinarith [abs_nonneg' abs (t^2)] example (t : R) (a b : ℚ) (h : a ≤ b) : a ≤ abs (t^2) + b := by linarith [abs_nonneg' abs (t^2)] example (t : R) (a b : ℚ) (h : a ≤ b) : abs t * a ≤ abs t * b := by nlinarith [abs_nonneg' abs t] end axiom T : Type @[instance] axiom T.ring : Ring T @[instance] axiom T.partialOrder : PartialOrder T @[instance] axiom T.isOrderedRing : IsOrderedRing T namespace T axiom zero_lt_one : (0 : T) < 1 lemma works {a b : ℕ} (hab : a ≤ b) (h : b < a) : false := by linarith end T example (a b c : ℚ) (h : a ≠ b) (h3 : b ≠ c) (h2 : a ≥ b) : b ≠ c := by linarith +splitNe example (a b c : ℚ) (h : a ≠ b) (h2 : a ≥ b) (h3 : b ≠ c) : a > b := by linarith +splitNe example (a b : ℕ) (h1 : b ≠ a) (h2 : b ≤ a) : b < a := by linarith +splitNe example (a b : ℕ) (h1 : b ≠ a) (h2 : ¬a < b) : b < a := by linarith +splitNe section -- Regression test for issue that splitNe didn't see `¬ a = b` example (a b : Nat) (h1 : a < b + 1) (h2 : a ≠ b) : a < b := by linarith +splitNe example (a b : Nat) (h1 : a < b + 1) (h2 : ¬ a = b) : a < b := by linarith +splitNe end -- Checks that splitNe handles metavariables and also that conjunction splitting occurs -- before splitNe splitting example (r : ℚ) (h' : 1 = r * 2) : 1 = 0 ∨ r = 1 / 2 := by by_contra! h'' linarith +splitNe example (x y : ℚ) (h₁ : 0 ≤ y) (h₂ : y ≤ x) : y * x ≤ x * x := by nlinarith example (x y : ℚ) (h₁ : 0 ≤ y) (h₂ : y ≤ x) : y * x ≤ x ^ 2 := by nlinarith axiom foo {x : Int} : 1 ≤ x → 1 ≤ x*x lemma bar (x y : Int) (h : 0 ≤ y ∧ 1 ≤ x) : 1 ≤ y + x * x := by linarith [foo h.2] -- -- issue https://github.com/leanprover-community/mathlib4/pull/9822 -- lemma mytest (j : ℕ) (h : 0 < j) : j-1 < j := by linarith example {α} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (h : ∃ x : α, 0 ≤ x) : True := by obtain ⟨x, h⟩ := h have : 0 ≤ x := by linarith trivial -- At one point, this failed, due to `mdata` interfering with `Expr.isEq`. example (a : Int) : a = a := by have h : True := True.intro linarith example (n : Nat) (h1 : ¬n = 1) (h2 : n ≥ 1) : n ≥ 2 := by by_contra h3 suffices n = 1 by exact h1 this linarith example (n : Nat) (h1 : ¬n = 1) (h2 : n ≥ 1) : n ≥ 2 := by have h4 : n ≥ 1 := h2 by_contra h3 suffices n = 1 by exact h1 this linarith universe u v -- simulate the type of MvPolynomial def P : Type u → Type v → Sort (max (u+1) (v+1)) := test_sorry noncomputable instance {c d} : Field (P c d) := test_sorry noncomputable instance {c d} : LinearOrder (P c d) := test_sorry noncomputable instance {c d} : IsStrictOrderedRing (P c d) := test_sorry example (p : P PUnit.{u + 1} PUnit.{v + 1}) (h : 0 < p) : 0 < 2 * p := by linarith example (n : Nat) : n + 1 ≥ (1 / 2 : ℚ) := by linarith example {α : Type} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (n : Nat) : (5 : α) - (n : α) ≤ (6 : α) := by linarith example {α : Type} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (n : Nat) : -(n : α) ≤ 0 := by linarith example {α : Type} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (n : Nat) (a : α) (h : a ≥ 2) : a * (n : α) + 5 ≥ 4 := by nlinarith example (x : ℚ) (h : x * (2⁻¹ + 2 / 3) = 1) : x = 6 / 7 := by linarith example {α} [CommSemiring α] [LinearOrder α] [IsStrictOrderedRing α] (x : α) (_ : 0 ≤ x) : 0 ≤ 1 := by linarith example (k : ℤ) (h : k < 1) (h₁ : -1 < k) : k = 0 := by -- Make h₁'s type be a metavariable. At one point this caused the strengthenStrictInt -- linarith preprocessor to fail. change _ at h₁ linarith /-- error: Unknown identifier `garbage` -/ #guard_msgs in example (q : Prop) (p : ∀ (x : ℤ), q → 1 = 2) : 1 = 2 := by linarith [p _ garbage] /-- error: Unknown identifier `garbage` -/ #guard_msgs in example (q : Prop) (p : ∀ (x : ℤ), q → 1 = 2) : 1 = 2 := by nlinarith [p _ garbage] /-- error: don't know how to synthesize placeholder for argument `x` context: q : Prop p : ∀ (x : ℤ), 1 = 2 ⊢ ℤ --- error: unsolved goals q : Prop p : ∀ (x : ℤ), 1 = 2 ⊢ 1 = 2 -/ #guard_msgs in example (q : Prop) (p : ∀ (x : ℤ), 1 = 2) : 1 = 2 := by linarith [p _] /-- error: Argument passed to linarith has metavariables: p ?a -/ #guard_msgs in example (q : Prop) (p : ∀ (x : ℤ), 1 = 2) : 1 = 2 := by linarith [p ?a] /-- error: Argument passed to nlinarith has metavariables: p ?a -/ #guard_msgs in example (q : Prop) (p : ∀ (x : ℤ), 1 = 2) : 1 = 2 := by nlinarith [p ?a] example (h : False): True := by have : ∑ k ∈ Finset.empty, k^2 = 0 := by contradiction have : ∀ k : Nat, 0 ≤ k := by intro h -- this should not panic: nlinarith trivial example (x : Nat) : 0 ≤ x ^ 9890 := by fail_if_success linarith -- this should not stack overflow apply zero_le /-- https://github.com/leanprover-community/mathlib4/issues/8875 -/ example (a b c d e : ℚ) (ha : 2 * a + b + c + d + e = 4) (hb : a + 2 * b + c + d + e = 5) (hc : a + b + 2 * c + d + e = 6) (hd : a + b + c + 2 * d + e = 7) (he : a + b + c + d + 2 * e = 8) : e = 3 := by linarith /-- https://github.com/leanprover-community/mathlib4/issues/2717 -/ example {x1 x2 x3 x4 x5 x6 x7 x8 : ℚ} : (3 * x4 - x3 - x2 - x1 : ℚ) < 0 → x5 - x4 < 0 → 2 * (x5 - x4) < 0 → -x6 + x3 < 0 → -x6 + x2 < 0 → 2 * (x6 - x5) < 0 → x8 - x7 < 0 → -x8 + x2 < 0 → -x8 + x7 - x5 + x1 < 0 → x7 - x5 < 0 → False := by intros; linarith #guard_msgs in /-- https://github.com/leanprover-community/mathlib4/issues/8875 -/ example (a b c d e : ℚ) (ha : 2 * a + b + c + d + e = 4) (hb : a + 2 * b + c + d + e = 5) (hc : a + b + 2 * c + d + e = 6) (hd : a + b + c + 2 * d + e = 7) (he : a + b + c + d + 2 * e = 8) : e = 3 := by linarith (oracle := .fourierMotzkin) #guard_msgs in /-- https://github.com/leanprover-community/mathlib4/issues/2717 -/ example {x1 x2 x3 x4 x5 x6 x7 x8 : ℚ} : (3 * x4 - x3 - x2 - x1 : ℚ) < 0 → x5 - x4 < 0 → 2 * (x5 - x4) < 0 → -x6 + x3 < 0 → -x6 + x2 < 0 → 2 * (x6 - x5) < 0 → x8 - x7 < 0 → -x8 + x2 < 0 → -x8 + x7 - x5 + x1 < 0 → x7 - x5 < 0 → False := by intros linarith (oracle := .fourierMotzkin) section findSquares private abbrev wrapped (z : ℤ) : ℤ := z /-- the `findSquares` preprocessor can look through reducible defeq -/ example (x : ℤ) : 0 ≤ x * wrapped x := by nlinarith private def tightlyWrapped (z : ℤ) : ℤ := z /-- error: linarith failed to find a contradiction case h x : ℤ a✝ : x * tightlyWrapped x < 0 ⊢ False failed -/ #guard_msgs in example (x : ℤ) : 0 ≤ x * tightlyWrapped x := by nlinarith end findSquares -- `Expr.mdata` should be ignored by linarith example (x : Int) (h : x = -2) : x = no_index(-2) := by linarith [h] /-! ### `linarith?` suggestions -/ /-- info: Try this: [apply] linarith only [h] -/ #guard_msgs in example (a : ℚ) (h : a < 0) : a ≤ 0 := by linarith? /-- info: Try this: [apply] linarith only [h₂, h₁] -/ #guard_msgs in example (a b : ℚ) (h₁ : a ≤ b) (h₂ : b < a) : False := by linarith? only [h₁, h₂] /-- info: Try this: [apply] linarith only [h₁] -/ #guard_msgs in example (a b c : ℚ) (h₁ : a < 0) (h₂ : b ≤ c) : a ≤ 0 := by linarith? only [h₁, h₂] /-- info: Try this: [apply] linarith only [h₁] -/ #guard_msgs in example (a b c d : ℚ) (h₁ : a < 0) (h₂ : b ≤ c) (h₃ : c ≤ d) : a ≤ 0 := by linarith? only [h₁, h₂, h₃] /-- info: Try this: [apply] linarith only [h₂, h₁] -/ #guard_msgs in example (a b c d : ℚ) (h₁ : a ≤ b) (h₂ : b < a) (h₃ : c ≤ d) (h₄ : d ≤ c) : False := by linarith? only [h₁, h₂, h₃, h₄] /-- info: Try this: [apply] linarith only [h₄, h₂, h₁] -/ #guard_msgs in example (x y : ℚ) (h₁ : x ≤ 0) (h₂ : y ≤ 0) (h₃ : x + y ≤ 0) (h₄ : x + y > 0) : False := by linarith? only [h₁, h₂, h₃, h₄] /-- info: Try this: [apply] linarith only [h₄, h₂, h₁] -/ #guard_msgs in example (x y : ℚ) (h₁ : x ≤ 0) (h₂ : y ≤ 0) (h₃ : x + y ≤ 0) (h₄ : x + y > 0) : False := by linarith? -minimize only [h₁, h₂, h₃, h₄] /-! From https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Adding.20an.20extra.20hypothesis.20breaks.20linarith/near/533973472 -/ namespace metavariables theorem foo {i m : ℕ} : i ∣ 2 ^ m → m = m := fun _ => rfl variable (k a i : ℕ) theorem base (h : (a : ℤ) * 2 ^ k = 2 ^ i) : True := by have := foo ⟨2^k, by linarith⟩ guard_hyp this : i = i trivial -- It's not clear which of the following should succeed and which should fail. -- For now this primarily serves to record behavior changes. theorem before_i (useless : i ≤ i) (h : (a : ℤ) * 2 ^ k = 2 ^ i) : True := by have := foo ⟨2^k, by linarith⟩ guard_hyp this : i = i trivial theorem before_k (useless : k ≤ k) (h : (a : ℤ) * 2 ^ k = 2 ^ i) : True := by have := foo ⟨2^k, by linarith⟩ guard_hyp this : i = i trivial theorem after_i_fails (h : (a : ℤ) * 2 ^ k = 2 ^ i) (useless : i ≤ i) : True := by fail_if_success have := foo ⟨2^k, by linarith⟩ trivial theorem after_k_fails (h : (a : ℤ) * 2 ^ k = 2 ^ i) (useless : k ≤ k) : True := by fail_if_success have := foo ⟨2^k, by linarith⟩ trivial end metavariables
.lake/packages/mathlib/MathlibTest/choose.lean
import Batteries.Util.ExtendedBinder import Mathlib.Tactic.Choose /-! # Tests for the `choose` tactic -/ set_option autoImplicit true example {α : Type} (h : ∀ n m : α, ∀ (h : n = m), ∃ i j : α, i ≠ j ∧ h = h) : True := by choose! i j _x _y using h trivial example (h : ∀ n m : Nat, ∃ i j, m = n + i ∨ m + j = n) : True := by choose i j h using h guard_hyp i : Nat → Nat → Nat guard_hyp j : Nat → Nat → Nat guard_hyp h : ∀ (n m : Nat), m = n + i n m ∨ m + j n m = n trivial example (h : ∀ i : Nat, i < 7 → ∃ j, i < j ∧ j < i+i) : True := by choose! f h h' using h guard_hyp f : Nat → Nat guard_hyp h : ∀ (i : Nat), i < 7 → i < f i guard_hyp h' : ∀ (i : Nat), i < 7 → f i < i + i trivial /- choice -/ example (h : ∀ n m : Nat, n < m → ∃ i j, m = n + i ∨ m + j = n) : True := by choose i j h using h guard_hyp i : ∀ n m : Nat, n < m → Nat guard_hyp j : ∀ n m : Nat, n < m → Nat guard_hyp h : ∀ (n m : Nat) (h : n < m), m = n + i n m h ∨ m + j n m h = n trivial -- `choose!` eliminates dependencies on props, whenever possible example (h : ∀ n m : Nat, n < m → ∃ i j, m = n + i ∨ m + j = n) : True := by choose! i j h using h guard_hyp i : Nat → Nat → Nat guard_hyp j : Nat → Nat → Nat guard_hyp h : ∀ (n m : Nat), n < m → m = n + i n m ∨ m + j n m = n trivial -- without the `using hyp` syntax, `choose` will intro the hyp first example : (∀ m : Nat, ∃ i, ∀ n : Nat, ∃ j, m = n + i ∨ m + j = n) → True := by choose i j h guard_hyp i : Nat → Nat guard_hyp j : Nat → Nat → Nat guard_hyp h : ∀ (m k : Nat), m = k + i m ∨ m + j m k = k trivial example (h : ∀ _n m : Nat, ∃ i, ∀ n:Nat, ∃ j, m = n + i ∨ m + j = n) : True := by choose i j h using h guard_hyp i : Nat → Nat → Nat guard_hyp j : Nat → Nat → Nat → Nat guard_hyp h : ∀ (n m k : Nat), m = k + i n m ∨ m + j n m k = k trivial -- Test `simp only [exists_prop]` gets applied after choosing. -- Because of this simp, we need a non-rfl goal example (h : ∀ n, ∃ k ≥ 0, n = k) : ∀ _ : Nat, 1 = 1 := by choose u hu using h guard_hyp hu : ∀ n, u n ≥ 0 ∧ n = u n intro; rfl -- test choose with conjunction example (h : ∀ i : Nat, ∃ j, i < j ∧ j < i+i) : True := by choose f h h' using h guard_hyp f : Nat → Nat guard_hyp h : ∀ (i : Nat), i < f i guard_hyp h' : ∀ (i : Nat), f i < i + i trivial instance : ∀ [Nonempty α], Nonempty (α × α) := @fun ⟨a⟩ => ⟨(a, a)⟩ -- test choose with nonempty instances instance : ∀ [Nonempty α] [Nonempty β], Nonempty (α × β) | ⟨a⟩, ⟨b⟩ => ⟨(a, b)⟩ example {α : Type u} (p : α → Prop) (h : ∀ i : α, p i → ∃ j : α × α, p j.1) : True := by choose! f h using h guard_hyp f : α → α × α guard_hyp h : ∀ (i : α), p i → p (f i).1 trivial
.lake/packages/mathlib/MathlibTest/Real.lean
/- ! This file was ported from Lean 3 source file `test/real.lean` ! leanprover-community/mathlib commit d7943885503965d07ccaeb390d65fbc3f45962e6 ! Please do not edit these lines, except to modify the commit id ! if you have ported upstream changes. -/ import Mathlib.Data.Real.Basic private axiom test_sorry : ∀ {α}, α unsafe def testRepr (r : ℝ) (s : String) : Lean.Elab.Command.CommandElabM Unit := unless toString (repr r) = s do throwError "got {repr r}" run_cmd unsafe testRepr 0 "Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)" run_cmd unsafe testRepr 1 "Real.ofCauchy (sorry /- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... -/)" run_cmd unsafe testRepr (37 : ℕ) "Real.ofCauchy (sorry /- 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, ... -/)" run_cmd unsafe testRepr (2 + 3) "Real.ofCauchy (sorry /- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, ... -/)" run_cmd unsafe testRepr ⟨CauSeq.Completion.mk <| ⟨fun n ↦ 2^(-n:ℤ), test_sorry⟩⟩ "Real.ofCauchy (sorry /- 1, (1 : Rat)/2, (1 : Rat)/4, (1 : Rat)/8, \ (1 : Rat)/16, (1 : Rat)/32, (1 : Rat)/64, (1 : Rat)/128, (1 : Rat)/256, \ (1 : Rat)/512, ... -/)"
.lake/packages/mathlib/MathlibTest/fail_if_no_progress.lean
import Mathlib.Tactic.FailIfNoProgress import Mathlib.Tactic.Basic set_option linter.unusedVariables false set_option linter.style.setOption false set_option pp.unicode.fun true section success example : 1 = 1 := by fail_if_no_progress rfl example (h : 1 = 1) : True := by fail_if_no_progress simp at h trivial example : let x := 1; x = x := by intro x fail_if_no_progress clear_value x rfl -- New fvarids. This is not easily avoided without remapping fvarids manually. example : let x := 1; x = x := by intro x fail_if_no_progress revert x intro x rfl open Lean Elab Tactic in example : let x := id 0; x = x := by intro x fail_if_no_progress -- Reduce the value of `x` to `Nat.zero` run_tac do let g ← getMainGoal let decl ← g.getDecl let some d := decl.lctx.findFromUserName? `x | throwError "no x" let lctx := decl.lctx.modifyLocalDecl d.fvarId fun d => d.setValue (.const ``Nat.zero []) let g' ← Meta.mkFreshExprMVarAt lctx decl.localInstances decl.type g.assign g' replaceMainGoal [g'.mvarId!] guard_hyp x : Nat :=ₛ Nat.zero rfl end success section failure /-- error: no progress made on x : Bool h : x = true ⊢ x = true -/ #guard_msgs in example (x : Bool) (h : x = true) : x = true := by fail_if_no_progress skip /-- error: no progress made on x : Bool h : x = true ⊢ x = true -/ #guard_msgs in example (x : Bool) (h : x = true) : x = true := by fail_if_no_progress simp -failIfUnchanged /-- error: no progress made on x : Bool h : x = true ⊢ True -/ #guard_msgs in example (x : Bool) (h : x = true) : True := by fail_if_no_progress simp -failIfUnchanged at h /-- error: no progress made on x : Nat := (fun x ↦ x) Nat.zero ⊢ x = x -/ #guard_msgs in open Lean Elab Tactic in example : let x := (fun x => x) Nat.zero; x = x := by intro x fail_if_no_progress -- Reduce the value of `x` to `Nat.zero` run_tac do let g ← getMainGoal let decl ← g.getDecl let some d := decl.lctx.findFromUserName? `x | throwError "no x" let lctx := decl.lctx.modifyLocalDecl d.fvarId fun d => d.setValue (.const ``Nat.zero []) let g' ← Meta.mkFreshExprMVarAt lctx decl.localInstances decl.type g.assign g' replaceMainGoal [g'.mvarId!] guard_hyp x : Nat :=ₛ Nat.zero end failure
.lake/packages/mathlib/MathlibTest/DeprecatedSyntaxLinter.lean
import Mathlib.Tactic.Cases import Mathlib.Tactic.Linter.DeprecatedSyntaxLinter set_option linter.style.refine true /-- warning: The `refine'` tactic is discouraged: please strongly consider using `refine` or `apply` instead. Note: This linter can be disabled with `set_option linter.style.refine false` --- warning: The `refine'` tactic is discouraged: please strongly consider using `refine` or `apply` instead. Note: This linter can be disabled with `set_option linter.style.refine false` -/ #guard_msgs in example : True := by refine' (by refine' .intro) set_option linter.style.refine false -- This is quiet because `linter.style.refine` is now false example : True := by refine' (by refine' .intro) set_option linter.style.refine false in -- This is quiet because `linter.style.refine` is now false, even using `in`. example : True := by refine' (by refine' .intro) set_option linter.style.cases true /-- warning: The `cases'` tactic is discouraged: please strongly consider using `obtain`, `rcases` or `cases` instead. Note: This linter can be disabled with `set_option linter.style.cases false` --- warning: The `cases'` tactic is discouraged: please strongly consider using `obtain`, `rcases` or `cases` instead. Note: This linter can be disabled with `set_option linter.style.cases false` -/ #guard_msgs in example (a : (True ∨ True) ∨ (True ∨ True)): True := by cases' a with b b <;> cases' b <;> trivial set_option linter.style.cases false -- This is quiet because `linter.style.cases` is now false example (a : (True ∨ True) ∨ (True ∨ True)): True := by cases' a with b b <;> cases' b <;> trivial set_option linter.style.induction true /-- warning: The `induction'` tactic is discouraged: please strongly consider using `induction` instead. Note: This linter can be disabled with `set_option linter.style.induction false` -/ #guard_msgs in example {n : Nat} : n < 2 ^ n := by induction' n with n ih · simp · grind set_option linter.style.induction false -- This is quiet because `linter.style.induction` is now false example {n : Nat} : n < 2 ^ n := by induction' n with n ih · simp · grind set_option linter.style.admit true /-- warning: declaration uses 'sorry' --- warning: The `admit` tactic is discouraged: please strongly consider using the synonymous `sorry` instead. Note: This linter can be disabled with `set_option linter.style.admit false` -/ #guard_msgs in example : False := by admit /-- warning: declaration uses 'sorry' --- warning: The `admit` tactic is discouraged: please strongly consider using the synonymous `sorry` instead. Note: This linter can be disabled with `set_option linter.style.admit false` --- warning: The `admit` tactic is discouraged: please strongly consider using the synonymous `sorry` instead. Note: This linter can be disabled with `set_option linter.style.admit false` --- warning: The `admit` tactic is discouraged: please strongly consider using the synonymous `sorry` instead. Note: This linter can be disabled with `set_option linter.style.admit false` --- warning: The `admit` tactic is discouraged: please strongly consider using the synonymous `sorry` instead. Note: This linter can be disabled with `set_option linter.style.admit false` -/ #guard_msgs in example : True ∧ True := by have : True := by · admit let foo : Nat := by admit refine ⟨?_, ?_⟩ · admit · admit set_option linter.style.admit false /-- warning: declaration uses 'sorry' -/ #guard_msgs in example : False := by admit set_option linter.style.nativeDecide true /-- warning: Using `native_decide` is not allowed in mathlib: because it trusts the entire Lean compiler (not just the Lean kernel), it could quite possibly be used to prove false. Note: This linter can be disabled with `set_option linter.style.nativeDecide false` -/ #guard_msgs in example : 1 + 1 = 2 := by native_decide /-- warning: Using `decide +native` is not allowed in mathlib: because it trusts the entire Lean compiler (not just the Lean kernel), it could quite possibly be used to prove false. Note: This linter can be disabled with `set_option linter.style.nativeDecide false` -/ #guard_msgs in example : 1 + 1 = 2 := by decide +native #guard_msgs in example : 1 + 1 = 2 := by decide -native /-- warning: Using `decide +native` is not allowed in mathlib: because it trusts the entire Lean compiler (not just the Lean kernel), it could quite possibly be used to prove false. Note: This linter can be disabled with `set_option linter.style.nativeDecide false` -/ #guard_msgs in theorem foo : 1 + 1 = 2 := by decide -native +native #guard_msgs in example : 1 + 1 = 2 := by decide +native -native /-- warning: Using `decide +native` is not allowed in mathlib: because it trusts the entire Lean compiler (not just the Lean kernel), it could quite possibly be used to prove false. Note: This linter can be disabled with `set_option linter.style.nativeDecide false` -/ #guard_msgs in example : 1 + 1 = 2 := by decide +native -native +native +native #guard_msgs in example : 1 + 1 = 2 := by decide +native -native +kernel +native -kernel +native -native /-- warning: Using `decide +native` is not allowed in mathlib: because it trusts the entire Lean compiler (not just the Lean kernel), it could quite possibly be used to prove false. Note: This linter can be disabled with `set_option linter.style.nativeDecide false` -/ #guard_msgs in example : 1 + 1 = 2 := by decide (config := { native := true }) example : 1 + 1 = 2 := by decide (config := { native := false }) -- Not handled yet: since mathlib hardly uses the old config syntax and this linter is purely -- for user information (and not hard guarantees), we deem this acceptable. example : 1 + 1 = 2 := by decide (config := { native := true, kernel := false }) set_option linter.style.nativeDecide false set_option linter.style.maxHeartbeats true /-- warning: Please, add a comment explaining the need for modifying the maxHeartbeat limit, as in set_option maxHeartbeats 10 in -- reason for change ... Note: This linter can be disabled with `set_option linter.style.maxHeartbeats false` -/ #guard_msgs in set_option maxHeartbeats 10 in section /-- warning: Please, add a comment explaining the need for modifying the maxHeartbeat limit, as in set_option maxHeartbeats 10 in -- reason for change ... Note: This linter can be disabled with `set_option linter.style.maxHeartbeats false` -/ #guard_msgs in set_option maxHeartbeats 10 in set_option maxHeartbeats 10 in section /-- warning: Please, add a comment explaining the need for modifying the maxHeartbeat limit, as in set_option synthInstance.maxHeartbeats 10 in -- reason for change ... Note: This linter can be disabled with `set_option linter.style.maxHeartbeats false` -/ #guard_msgs in set_option synthInstance.maxHeartbeats 10 in section /-- warning: Please, add a comment explaining the need for modifying the maxHeartbeat limit, as in set_option maxHeartbeats 10 in -- reason for change ... Note: This linter can be disabled with `set_option linter.style.maxHeartbeats false` -/ #guard_msgs in set_option maxHeartbeats 10 in set_option synthInstance.maxHeartbeats 10 in /- The comment here is not enough to silence the linter: the *first* `maxHeartbeats` option should have a comment. -/ section #guard_msgs in set_option maxHeartbeats 10 in /- The comment here is enough to silence the linter: the *first* `maxHeartbeats` has a comment, to remaining ones are free to be commentless. -/ set_option synthInstance.maxHeartbeats 10 in section #guard_msgs in set_option maxHeartbeats 10 in -- no reason, but has a comment section /-- warning: Please, add a comment explaining the need for modifying the maxHeartbeat limit, as in set_option maxHeartbeats 10 in -- reason for change ... Note: This linter can be disabled with `set_option linter.style.maxHeartbeats false` -/ #guard_msgs in set_option maxHeartbeats 10 in /-- Doc-strings for the following command do not silence the linter. -/ example : True := trivial
.lake/packages/mathlib/MathlibTest/DifferentialGeometry/Notation.lean
import Mathlib.Geometry.Manifold.Notation import Mathlib.Geometry.Manifold.VectorBundle.Basic set_option pp.unicode.fun true open Bundle Filter Function Topology open scoped Manifold variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] section variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) {M : Type*} [TopologicalSpace M] [ChartedSpace H M] variable {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] variable (F : Type*) [NormedAddCommGroup F] [NormedSpace 𝕜 F] -- `F` model fiber (n : WithTop ℕ∞) (V : M → Type*) [TopologicalSpace (TotalSpace F V)] [∀ x, AddCommGroup (V x)] [∀ x, Module 𝕜 (V x)] [∀ x : M, TopologicalSpace (V x)] [∀ x, IsTopologicalAddGroup (V x)] [∀ x, ContinuousSMul 𝕜 (V x)] [FiberBundle F V] [VectorBundle 𝕜 F V] -- `V` vector bundle /-! Tests for the `T%` elaborator, inserting calls to `TotalSpace.mk'` automatically. -/ section TotalSpace variable {σ : Π x : M, V x} {σ' : (x : E) → Trivial E E' x} {σ'' : (y : E) → Trivial E E' y} {s : E → E'} /-- info: fun x ↦ TotalSpace.mk' F x (σ x) : M → TotalSpace F V -/ #guard_msgs in #check T% σ -- Note how the name of the bound variable `x` resp. `y` is preserved. /-- info: fun x ↦ TotalSpace.mk' E' x (σ' x) : E → TotalSpace E' (Trivial E E') -/ #guard_msgs in #check T% σ' /-- info: fun y ↦ TotalSpace.mk' E' y (σ'' y) : E → TotalSpace E' (Trivial E E') -/ #guard_msgs in #check T% σ'' /-- info: fun a ↦ TotalSpace.mk' E' a (s a) : E → TotalSpace E' (Trivial E E') -/ #guard_msgs in #check T% s variable (X : (m : M) → TangentSpace I m) [IsManifold I 1 M] /-- info: fun m ↦ TotalSpace.mk' E m (X m) : M → TotalSpace E (TangentSpace I) -/ #guard_msgs in #check T% X variable {x : M} -- Testing precedence. section precedence /-- info: (fun x ↦ TotalSpace.mk' F x (σ x)) x : TotalSpace F V -/ #guard_msgs in #check (T% σ) x /-- info: (fun x ↦ TotalSpace.mk' F x (σ x)) x : TotalSpace F V -/ #guard_msgs in #check T% σ x -- Nothing happening, as expected. /-- info: σ x : V x -/ #guard_msgs in #check T% (σ x) -- Testing precedence when applied to a family of section. variable {ι j : Type*} -- Partially applied. /-- info: fun a ↦ TotalSpace.mk' ((x : M) → V x) a (s a) : ι → TotalSpace ((x : M) → V x) (Trivial ι ((x : M) → V x)) -/ #guard_msgs in variable {s : ι → (x : M) → V x} in #check T% s /-- info: (fun a ↦ TotalSpace.mk' (ι → (x : M) → V x) a (s a)) i : TotalSpace (ι → (x : M) → V x) (Trivial ι (ι → (x : M) → V x)) -/ #guard_msgs in variable {s : ι → ι → (x : M) → V x} {i : ι} in #check T% s i variable {X : ι → Π x : M, TangentSpace I x} {i : ι} -- Error message is okay, but not great. /-- error: Could not find a model with corners for `ι` -/ #guard_msgs in #check MDiffAt (T% X) x end precedence example : (fun m ↦ (X m : TangentBundle I M)) = (fun m ↦ TotalSpace.mk' E m (X m)) := rfl -- Applying a section to an argument. TODO: beta-reduce instead! /-- info: (fun m ↦ TotalSpace.mk' E m (X m)) x : TotalSpace E (TangentSpace I) -/ #guard_msgs in #check (T% X) x -- Applying the same elaborator twice is fine (and idempotent). /-- info: (fun m ↦ TotalSpace.mk' E m (X m)) x : TotalSpace E (TangentSpace I) -/ #guard_msgs in #check (T% (T% X)) x end TotalSpace /-! Tests for the elaborators for `MDifferentiable{WithinAt,At,On}`. -/ section differentiability variable {EM' : Type*} [NormedAddCommGroup EM'] [NormedSpace 𝕜 EM'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 EM' H') {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] /-! Some basic tests: a simple function, both in applied and unapplied form -/ section basic -- General case: a function between two manifolds. variable {f : M → M'} {s : Set M} {m : M} /-- info: MDifferentiableWithinAt I I' f s : M → Prop -/ #guard_msgs in #check MDiffAt[s] f /-- info: MDifferentiableWithinAt I I' f s m : Prop -/ #guard_msgs in #check MDiffAt[s] f m /-- info: MDifferentiableAt I I' f : M → Prop -/ #guard_msgs in #check MDiffAt f /-- info: MDifferentiableAt I I' f m : Prop -/ #guard_msgs in #check MDiffAt f m /-- info: MDifferentiableOn I I' f s : Prop -/ #guard_msgs in #check MDiff[s] f -- A partial homeomorphism or partial equivalence. variable {φ : OpenPartialHomeomorph M E} {ψ : PartialEquiv M E} /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) (↑φ) s : M → Prop -/ #guard_msgs in #check MDiffAt[s] φ /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) (↑ψ) s : M → Prop -/ #guard_msgs in #check MDiffAt[s] ψ -- Testing an error message. section /-- error: Function expected at MDifferentiableOn I I' f s but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check MDiff[s] f m /-- error: Function expected at MDifferentiableOn I I' f s but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check MDifferentiableOn I I' f s m /-- info: MDifferentiable I I' f : Prop -/ #guard_msgs in #check MDiff f /-- error: Function expected at MDifferentiable I I' f but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check MDiff f m end -- Testing the precedence of parsing of the "within" set: regression test. section variable {ι : Type} {i : ι} {s : ι → Set M} /-- info: MDifferentiableOn I I' f (s i) : Prop -/ #guard_msgs in #check MDiff[s i] f /-- info: MDifferentiableWithinAt I I' f (s i) m : Prop -/ #guard_msgs in #check MDiffAt[s i] f m /-- info: ContMDiffOn I I' 2 f (s i) : Prop -/ #guard_msgs in #check CMDiff[s i] 2 f /-- info: ContMDiffWithinAt I I' 0 f (s i) m : Prop -/ #guard_msgs in #check CMDiffAt[s i] 0 f m /-- info: mfderivWithin I I' f (s i) m : TangentSpace I m →L[𝕜] TangentSpace I' (f m) -/ #guard_msgs in #check mfderiv[s i] f m end -- Function from a manifold into a normed space. variable {g : M → E} /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) g s : M → Prop -/ #guard_msgs in #check MDiffAt[s] g /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) g s m : Prop -/ #guard_msgs in #check MDiffAt[s] g m /-- info: MDifferentiableAt I 𝓘(𝕜, E) g : M → Prop -/ #guard_msgs in #check MDiffAt g /-- info: MDifferentiableAt I 𝓘(𝕜, E) g m : Prop -/ #guard_msgs in #check MDiffAt g m /-- info: MDifferentiableOn I 𝓘(𝕜, E) g s : Prop -/ #guard_msgs in #check MDiff[s] g -- TODO: fix and enable! #check MDiff[s] g m /-- info: MDifferentiable I 𝓘(𝕜, E) g : Prop -/ #guard_msgs in #check MDiff g -- TODO: fix and enable! #check MDiff g m -- From a manifold into a field. variable {h : M → 𝕜} /-- info: MDifferentiableWithinAt I 𝓘(𝕜, 𝕜) h s : M → Prop -/ #guard_msgs in #check MDiffAt[s] h /-- info: MDifferentiableWithinAt I 𝓘(𝕜, 𝕜) h s m : Prop -/ #guard_msgs in #check MDiffAt[s] h m /-- info: MDifferentiableAt I 𝓘(𝕜, 𝕜) h : M → Prop -/ #guard_msgs in #check MDiffAt h /-- info: MDifferentiableAt I 𝓘(𝕜, 𝕜) h m : Prop -/ #guard_msgs in #check MDiffAt h m /-- info: MDifferentiableOn I 𝓘(𝕜, 𝕜) h s : Prop -/ #guard_msgs in #check MDiff[s] h -- TODO: fix and enable! #check MDiff[s] h m /-- info: MDifferentiable I 𝓘(𝕜, 𝕜) h : Prop -/ #guard_msgs in #check MDiff h -- TODO: fix and enable! #check MDiff h m -- The following tests are more spotty, as most code paths are already covered above. -- Add further details as necessary. -- From a normed space into a manifold. variable {f : E → M'} {s : Set E} {x : E} /-- info: MDifferentiableWithinAt 𝓘(𝕜, E) I' f s : E → Prop -/ #guard_msgs in #check MDiffAt[s] f /-- info: MDifferentiableAt 𝓘(𝕜, E) I' f x : Prop -/ #guard_msgs in #check MDiffAt f x -- TODO: fix and enable! #check MDiff[s] f x /-- info: MDifferentiable 𝓘(𝕜, E) I' f : Prop -/ #guard_msgs in #check MDiff f -- TODO: should this error? if not, fix and enable! #check MDiff f x -- same! #check MDifferentiable% f x -- Between normed spaces. variable {f : E → E'} {s : Set E} {x : E} /-- info: MDifferentiableAt 𝓘(𝕜, E) 𝓘(𝕜, E') f x : Prop -/ #guard_msgs in #check MDiffAt f x /-- info: MDifferentiableAt 𝓘(𝕜, E) 𝓘(𝕜, E') f : E → Prop -/ #guard_msgs in #check MDiffAt f -- should this error or not? #check MDiff[s] f x /-- info: MDifferentiableWithinAt 𝓘(𝕜, E) 𝓘(𝕜, E') f s : E → Prop -/ #guard_msgs in #check MDiffAt[s] f /-- info: MDifferentiableOn 𝓘(𝕜, E) 𝓘(𝕜, E') f s : Prop -/ #guard_msgs in #check MDiff[s] f -- Normed space to a field. variable {f : E → 𝕜} {s : Set E} {x : E} /-- info: MDifferentiableAt 𝓘(𝕜, E) 𝓘(𝕜, 𝕜) f x : Prop -/ #guard_msgs in #check MDiffAt f x -- Field into a manifold. variable {f : 𝕜 → M'} {u : Set 𝕜} {a : 𝕜} /-- info: MDifferentiableAt 𝓘(𝕜, 𝕜) I' f a : Prop -/ #guard_msgs in #check MDiffAt f a /-- info: MDifferentiableOn 𝓘(𝕜, 𝕜) I' f u : Prop -/ #guard_msgs in #check MDiff[u] f -- Field into a normed space. variable {f : 𝕜 → E'} {u : Set 𝕜} {a : 𝕜} /-- info: MDifferentiableAt 𝓘(𝕜, 𝕜) 𝓘(𝕜, E') f a : Prop -/ #guard_msgs in #check MDiffAt f a /-- info: MDifferentiableOn 𝓘(𝕜, 𝕜) 𝓘(𝕜, E') f u : Prop -/ #guard_msgs in #check MDiff[u] f -- On a field. variable {f : 𝕜 → 𝕜} {u : Set 𝕜} {a : 𝕜} /-- info: MDifferentiableAt 𝓘(𝕜, 𝕜) 𝓘(𝕜, 𝕜) f a : Prop -/ #guard_msgs in #check MDiffAt f a /-- info: MDifferentiableOn 𝓘(𝕜, 𝕜) 𝓘(𝕜, 𝕜) f u : Prop -/ #guard_msgs in #check MDiff[u] f end basic variable {σ : Π x : M, V x} {σ' : (x : E) → Trivial E E' x} {s : E → E'} variable (X : (m : M) → TangentSpace I m) [IsManifold I 1 M] /-! (Extended) charts -/ section variable {φ : OpenPartialHomeomorph M H} {ψ : PartialEquiv M E} {s : Set M} /-- info: ContMDiff I I 37 ↑φ : Prop -/ #guard_msgs in #check CMDiff 37 φ /-- info: MDifferentiable I I ↑φ : Prop -/ #guard_msgs in #check MDiff φ /-- info: MDifferentiable I 𝓘(𝕜, E) ↑ψ : Prop -/ #guard_msgs in #check MDiff ψ /-- info: MDifferentiableWithinAt I I (↑φ) s : M → Prop -/ #guard_msgs in #check MDiffAt[s] φ /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) (↑ψ) s : M → Prop -/ #guard_msgs in #check MDiffAt[s] ψ end /-! Error messages in case of a forgotten `T%`. -/ section /-- error: Term `X` is a dependent function, of type `(m : M) → TangentSpace I m` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiff X /-- error: Term `σ` is a dependent function, of type `(x : M) → V x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiff σ /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiff σ' /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiff[s] σ' /-- error: Term `X` is a dependent function, of type `(m : M) → TangentSpace I m` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiffAt (X) /-- error: Term `σ` is a dependent function, of type `(x : M) → V x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiffAt ((σ)) /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiff[s] σ' /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiffAt σ' /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiffAt[s] σ' end end differentiability /-! Tests for the custom elaborators for `ContMDiff{WithinAt,At,On}` -/ section smoothness -- Copy-pasted the tests for differentiability mutatis mutandis. -- Start with some basic tests: a simple function, both in applied and unapplied form. variable {EM' : Type*} [NormedAddCommGroup EM'] [NormedSpace 𝕜 EM'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 EM' H') {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] -- TODO: add tests for the error message when smoothness hypotheses are missing -- General case: a function between two manifolds. variable {f : M → M'} {s : Set M} {m : M} variable [IsManifold I 1 M] [IsManifold I' 1 M'] -- Testing error messages when forgetting the smoothness exponent or swapping arguments. section error -- yields a parse error, "unexpected token '/--'; expected term" -- TODO: make this parse, but error in the elaborator -- #check CMDiffAt[s] f /-- error: Type mismatch f has type M → M' of sort `Type (max u_10 u_4)` but is expected to have type WithTop ℕ∞ of sort `Type` --- error: Expected m of type M to be a function, or to be coercible to a function -/ #guard_msgs in #check CMDiffAt[s] f m /-- error: Type mismatch f has type M → M' of sort `Type (max u_10 u_4)` but is expected to have type WithTop ℕ∞ of sort `Type` --- error: Expected m of type M to be a function, or to be coercible to a function -/ #guard_msgs in #check CMDiff[s] f m -- yields a parse error, "unexpected token '/--'; expected term" -- #check CMDiffAt f /-- error: Type mismatch f has type M → M' of sort `Type (max u_10 u_4)` but is expected to have type WithTop ℕ∞ of sort `Type` --- error: Expected n of type Option ℕ∞ to be a function, or to be coercible to a function -/ #guard_msgs in #check CMDiff f n end error /-- info: ContMDiffWithinAt I I' 1 f s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 1 f /-- info: ContMDiffWithinAt I I' 1 f s m : Prop -/ #guard_msgs in #check CMDiffAt[s] 1 f m /-- info: ContMDiffWithinAt I I' 1 f s m : Prop -/ #guard_msgs in #check CMDiffAt[s] 1 f m /-- info: ContMDiffAt I I' 1 f : M → Prop -/ #guard_msgs in #check CMDiffAt 1 f /-- info: ContMDiffAt I I' 2 f m : Prop -/ #guard_msgs in #check CMDiffAt 2 f m /-- info: ContMDiffOn I I' 37 f s : Prop -/ #guard_msgs in #check CMDiff[s] 37 f -- Testing an error message. section /-- error: Function expected at ContMDiffOn I I' 2 f s but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check CMDiff[s] 2 f m variable {n : WithTop ℕ∞} /-- error: Function expected at ContMDiffOn I I' n f s but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check ContMDiffOn I I' n f s m /-- info: MDifferentiable I I' f : Prop -/ #guard_msgs in #check MDiff f /-- error: Function expected at ContMDiff I I' n f but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check CMDiff n f m end /-! Tests for coercions from `ℕ` or `ℕ∞` to `WithTop ℕ∞` -/ section coercions variable {k : ℕ} {k' : ℕ∞} /-- info: ContMDiffWithinAt I I' 0 f s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 0 f /-- info: ContMDiffWithinAt I I' 1 f s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 1 f /-- info: ContMDiffWithinAt I I' 37 f s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 37 f /-- info: ContMDiffWithinAt I I' (↑k) f s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] k f /-- info: ContMDiffWithinAt I I' (↑k') f s m : Prop -/ #guard_msgs in #check CMDiffAt[s] k' f m /-- info: ContMDiffWithinAt I I' n f s m : Prop -/ #guard_msgs in #check CMDiffAt[s] n f m /-- info: ContMDiffAt I I' (↑k) f : M → Prop -/ #guard_msgs in #check CMDiffAt k f /-- info: ContMDiffAt I I' (↑k') f m : Prop -/ #guard_msgs in #check CMDiffAt k' f m /-- info: ContMDiffOn I I' (↑k) f s : Prop -/ #guard_msgs in #check CMDiff[s] k f /-- error: Function expected at ContMDiffOn I I' (↑k') f s but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check CMDiff[s] k' f m /-- info: ContMDiff I I' (↑k) f : Prop -/ #guard_msgs in #check CMDiff k f /-- error: Function expected at ContMDiff I I' (↑k') f but this term has type Prop Note: Expected a function because this term is being applied to the argument m -/ #guard_msgs in #check CMDiff k' f m end coercions /-! Error messages for a missing `T%` elaborator. -/ section dependent variable {σ : Π x : M, V x} {σ' : (x : E) → Trivial E E' x} {s : E → E'} variable {ι : Type*} {i : ι} (X : (m : M) → TangentSpace I m) [IsManifold I 1 M] (X' : ι → (m : M) → TangentSpace I m) /-- error: Term `X` is a dependent function, of type `(m : M) → TangentSpace I m` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiff 0 X /-- error: Term `σ` is a dependent function, of type `(x : M) → V x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiff 0 σ /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiff 0 σ' /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiff[s] 0 σ' /-- error: Term `X` is a dependent function, of type `(m : M) → TangentSpace I m` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiffAt 0 (X) /-- error: Term `σ` is a dependent function, of type `(x : M) → V x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiffAt 0 ((σ)) /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiff[s] 0 σ' /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiffAt 0 σ' /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check CMDiffAt[s] 0 σ' /-- error: Term `X' i` is a dependent function, of type `(m : M) → TangentSpace I m` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check MDiffAt ((X' i)) x -- This error message is not great: this is missing *both* a T% elaborator -- and an argument i. /-- error: Could not find a model with corners for `ι` -/ #guard_msgs in #check MDiffAt X' x end dependent -- Function from a manifold into a normed space. variable {g : M → E} /-- info: ContMDiffWithinAt I 𝓘(𝕜, E) 1 g s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 1 g /-- info: ContMDiffWithinAt I 𝓘(𝕜, E) 0 g s m : Prop -/ #guard_msgs in #check CMDiffAt[s] 0 g m /-- info: ContMDiffAt I 𝓘(𝕜, E) 1 g : M → Prop -/ #guard_msgs in #check CMDiffAt 1 g /-- info: ContMDiffAt I 𝓘(𝕜, E) 1 g m : Prop -/ #guard_msgs in #check CMDiffAt 1 g m /-- info: ContMDiffOn I 𝓘(𝕜, E) n g s : Prop -/ #guard_msgs in #check CMDiff[s] n g -- TODO: fix and enable! #check CMDiff[s] n g m /-- info: ContMDiff I 𝓘(𝕜, E) n g : Prop -/ #guard_msgs in #check CMDiff n g -- TODO: fix and enable! #check CMDiff n g m -- From a manifold into a field. variable {h : M → 𝕜} /-- info: ContMDiffWithinAt I 𝓘(𝕜, 𝕜) 0 h s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 0 h /-- info: ContMDiffWithinAt I 𝓘(𝕜, 𝕜) 1 h s m : Prop -/ #guard_msgs in #check CMDiffAt[s] 1 h m /-- info: ContMDiffAt I 𝓘(𝕜, 𝕜) 2 h : M → Prop -/ #guard_msgs in #check CMDiffAt 2 h /-- info: ContMDiffAt I 𝓘(𝕜, 𝕜) n h m : Prop -/ #guard_msgs in #check CMDiffAt n h m /-- info: ContMDiffOn I 𝓘(𝕜, 𝕜) n h s : Prop -/ #guard_msgs in #check CMDiff[s] n h -- TODO: fix and enable! #check CMDiff[s] n h m /-- info: ContMDiff I 𝓘(𝕜, 𝕜) 37 h : Prop -/ #guard_msgs in #check CMDiff 37 h -- TODO: fix and enable! #check CMDiff 0 h m -- The following tests are more spotty, as most code paths are already covered above. -- Add further details as necessary. -- This list mirrors some of the tests for `MDifferentiable{WithinAt,At,On}`, but not all. -- From a normed space into a manifold. variable {f : E → M'} {s : Set E} {x : E} /-- info: ContMDiffWithinAt 𝓘(𝕜, E) I' 2 f s : E → Prop -/ #guard_msgs in #check CMDiffAt[s] 2 f /-- info: ContMDiffAt 𝓘(𝕜, E) I' 3 f x : Prop -/ #guard_msgs in #check CMDiffAt 3 f x -- TODO: fix and enable! #check CMDiff[s] 1 f x /-- info: ContMDiff 𝓘(𝕜, E) I' 1 f : Prop -/ #guard_msgs in #check CMDiff 1 f -- TODO: should this error? if not, fix and enable! #check CMDiff 1 f x -- same! #check MDifferentiable% f x -- Between normed spaces. variable {f : E → E'} {s : Set E} {x : E} /-- info: ContMDiffAt 𝓘(𝕜, E) 𝓘(𝕜, E') 2 f x : Prop -/ #guard_msgs in #check CMDiffAt 2 f x /-- info: ContMDiffAt 𝓘(𝕜, E) 𝓘(𝕜, E') 2 f : E → Prop -/ #guard_msgs in #check CMDiffAt 2 f -- should this error or not? #check CMDiff[s] 2 f x /-- info: ContMDiffWithinAt 𝓘(𝕜, E) 𝓘(𝕜, E') 2 f s : E → Prop -/ #guard_msgs in #check CMDiffAt[s] 2 f /-- info: ContMDiffOn 𝓘(𝕜, E) 𝓘(𝕜, E') 2 f s : Prop -/ #guard_msgs in #check CMDiff[s] 2 f end smoothness -- Inferring the type of `x` for all ContMDiff/MDifferentiable{Within}At elaborators. section variable {EM' : Type*} [NormedAddCommGroup EM'] [NormedSpace 𝕜 EM'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 EM' H') {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] {f : M → M'} {s : Set M} /-- info: {x | MDifferentiableAt I I' f x} : Set M -/ #guard_msgs in #check {x | MDiffAt f x} /-- info: {x | MDifferentiableWithinAt I I' f s x} : Set M -/ #guard_msgs in #check {x | MDiffAt[s] f x} /-- info: {x | ContMDiffAt I I' ⊤ f x} : Set M -/ #guard_msgs in #check {x | CMDiffAt ⊤ f x} /-- info: {x | ContMDiffWithinAt I I' 2 f s x} : Set M -/ #guard_msgs in #check {x | CMDiffAt[s] 2 f x} open ContDiff in -- for the ∞ notation /-- info: {x | ContMDiffAt I I' ∞ f x} : Set M -/ #guard_msgs in #check {x | CMDiffAt ∞ f x} /-- info: {x | Injective ⇑(mfderiv I I' f x)} : Set M -/ #guard_msgs in #check {x | Function.Injective (mfderiv% f x) } /-- info: {x | Surjective ⇑(mfderivWithin I I' f s x)} : Set M -/ #guard_msgs in #check {x | Function.Surjective (mfderiv[s] f x) } end section trace /- Test that basic tracing works. -/ set_option trace.Elab.DiffGeo true variable {f : Unit → Unit} /-- error: Could not find a model with corners for `Unit` --- trace: [Elab.DiffGeo.MDiff] Finding a model for: Unit [Elab.DiffGeo.MDiff] ❌️ TotalSpace [Elab.DiffGeo.MDiff] Failed with error: `Unit` is not a `Bundle.TotalSpace`. [Elab.DiffGeo.MDiff] ❌️ TangentBundle [Elab.DiffGeo.MDiff] Failed with error: `Unit` is not a `TangentBundle` [Elab.DiffGeo.MDiff] ❌️ NormedSpace [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `NormedSpace` structure on `Unit` among local instances. [Elab.DiffGeo.MDiff] ❌️ Manifold [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H M` [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `ChartedSpace` structure on `Unit` among local instances, and `Unit` is not the charted space of some type in the local context either. [Elab.DiffGeo.MDiff] ❌️ ContinuousLinearMap [Elab.DiffGeo.MDiff] Failed with error: `Unit` is not a space of continuous linear maps [Elab.DiffGeo.MDiff] ❌️ RealInterval [Elab.DiffGeo.MDiff] Failed with error: `Unit` is not a coercion of a set to a type [Elab.DiffGeo.MDiff] ❌️ UpperHalfPlane [Elab.DiffGeo.MDiff] Failed with error: `Unit` is not the complex upper half plane [Elab.DiffGeo.MDiff] ❌️ NormedField [Elab.DiffGeo.MDiff] Failed with error: failed to synthesize NontriviallyNormedField Unit ⏎ Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #check mfderiv% f /-- info: fun a ↦ TotalSpace.mk' Unit a (f a) : Unit → TotalSpace Unit (Trivial Unit Unit) --- trace: [Elab.DiffGeo.TotalSpaceMk] Section of a trivial bundle as a non-dependent function -/ #guard_msgs in #check T% f end trace
.lake/packages/mathlib/MathlibTest/DifferentialGeometry/NotationAdvanced.lean
import Mathlib.Analysis.Complex.UpperHalfPlane.Manifold import Mathlib.Geometry.Manifold.Instances.Real import Mathlib.Geometry.Manifold.Notation import Mathlib.Geometry.Manifold.VectorBundle.SmoothSection import Mathlib.Geometry.Manifold.VectorBundle.Tangent import Mathlib.Geometry.Manifold.MFDeriv.FDeriv import Mathlib.Geometry.Manifold.MFDeriv.SpecificFunctions import Mathlib.Geometry.Manifold.BumpFunction import Mathlib.Geometry.Manifold.VectorBundle.MDifferentiable import Mathlib.Geometry.Manifold.VectorField.LieBracket /-! # Tests for the differential geometry elaborators which require stronger imports -/ set_option pp.unicode.fun true open Bundle Filter Function Topology open scoped Manifold variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] section variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) {M : Type*} [TopologicalSpace M] [ChartedSpace H M] variable {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] variable (F : Type*) [NormedAddCommGroup F] [NormedSpace 𝕜 F] -- `F` model fiber (n : WithTop ℕ∞) (V : M → Type*) [TopologicalSpace (TotalSpace F V)] [∀ x, AddCommGroup (V x)] [∀ x, Module 𝕜 (V x)] [∀ x : M, TopologicalSpace (V x)] [∀ x, IsTopologicalAddGroup (V x)] [∀ x, ContinuousSMul 𝕜 (V x)] [FiberBundle F V] [VectorBundle 𝕜 F V] -- `V` vector bundle /-! Additional tests for the elaborators for `MDifferentiable{WithinAt,At,On}`. -/ section differentiability variable {EM' : Type*} [NormedAddCommGroup EM'] [NormedSpace 𝕜 EM'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 EM' H') {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] /-! A partial homeomorphism or partial equivalence. More generally, this works for any type with a coercion to (possibly dependent) functions. -/ section coercion variable {s : Set M} {m : M} variable {φ : OpenPartialHomeomorph M E} {ψ : PartialEquiv M E} /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) (↑φ) s : M → Prop -/ #guard_msgs in #check MDiffAt[s] φ /-- info: MDifferentiableWithinAt I 𝓘(𝕜, E) (↑ψ) s : M → Prop -/ #guard_msgs in #check MDiffAt[s] ψ /-- info: MDifferentiableAt I 𝓘(𝕜, E) ↑φ : M → Prop -/ #guard_msgs in #check MDiffAt φ /-- info: MDifferentiableAt I 𝓘(𝕜, E) ↑ψ : M → Prop -/ #guard_msgs in #check MDiffAt ψ /-- info: MDifferentiableOn I 𝓘(𝕜, E) (↑φ) s : Prop -/ #guard_msgs in #check MDiff[s] φ /-- info: MDifferentiableOn I 𝓘(𝕜, E) (↑ψ) s : Prop -/ #guard_msgs in #check MDiff[s] ψ /-- info: MDifferentiable I 𝓘(𝕜, E) ↑φ : Prop -/ #guard_msgs in #check MDiff φ /-- info: ContMDiffWithinAt I 𝓘(𝕜, E) 2 (↑ψ) s : M → Prop -/ #guard_msgs in #check CMDiffAt[s] 2 ψ /-- info: ContMDiffOn I 𝓘(𝕜, E) 2 (↑φ) s : Prop -/ #guard_msgs in #check CMDiff[s] 2 φ /-- info: ContMDiffAt I 𝓘(𝕜, E) 2 ↑φ : M → Prop -/ #guard_msgs in #check CMDiffAt 2 φ /-- info: ContMDiff I 𝓘(𝕜, E) 2 ↑ψ : Prop -/ #guard_msgs in #check CMDiff 2 ψ /-- info: mfderiv I 𝓘(𝕜, E) ↑φ : (x : M) → TangentSpace I x →L[𝕜] TangentSpace 𝓘(𝕜, E) (↑φ x) -/ #guard_msgs in #check mfderiv% φ /-- info: mfderivWithin I 𝓘(𝕜, E) (↑ψ) s : (x : M) → TangentSpace I x →L[𝕜] TangentSpace 𝓘(𝕜, E) (↑ψ x) -/ #guard_msgs in #check mfderiv[s] ψ /-- info: mfderivWithin I 𝓘(𝕜, E) (↑ψ) s : (x : M) → TangentSpace I x →L[𝕜] TangentSpace 𝓘(𝕜, E) (↑ψ x) -/ #guard_msgs in variable {f : ContMDiffSection I F n V} in #check mfderiv[s] ψ /-- info: mfderiv I I' ⇑g : (x : M) → TangentSpace I x →L[𝕜] TangentSpace I' (g x) -/ #guard_msgs in variable {g : ContMDiffMap I I' M M' n} in #check mfderiv% g -- An example of "any type" which coerces to functions. /-- info: mfderiv I I' ⇑g : (x : M) → TangentSpace I x →L[𝕜] TangentSpace I' (g x) -/ #guard_msgs in variable {g : Equiv M M'} in #check mfderiv% g end coercion variable {σ : Π x : M, V x} {σ' : (x : E) → Trivial E E' x} {s : E → E'} variable (X : (m : M) → TangentSpace I m) [IsManifold I 1 M] /-! These elaborators can be combined with the total space elaborator. -/ section interaction -- Note: these tests might be incomplete; extend as needed! /-- info: MDifferentiableAt I (I.prod 𝓘(𝕜, E)) fun m ↦ TotalSpace.mk' E m (X m) : M → Prop -/ #guard_msgs in #check MDiffAt (T% X) /-- info: MDifferentiableAt I (I.prod 𝓘(𝕜, F)) fun x ↦ TotalSpace.mk' F x (σ x) : M → Prop -/ #guard_msgs in #check MDiffAt (T% σ) /-- info: MDifferentiableAt 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) fun x ↦ TotalSpace.mk' E' x (σ' x) : E → Prop -/ #guard_msgs in #check MDiffAt (T% σ') end interaction -- Total space over the tangent space and tangent bundle. section variable [IsManifold I 2 M] variable {h : Bundle.TotalSpace F (TangentSpace I : M → Type _) → F} {h' : TangentBundle I M → F} -- Test the inference of a model with corners on a trivial bundle over the tangent space of a -- manifold. (This code path is not covered by the other tests, hence should be kept.) -- Stating smoothness this way does not make sense, but finding a model with corners should work. /-- error: failed to synthesize TopologicalSpace (TotalSpace F (TangentSpace I)) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. --- trace: [Elab.DiffGeo.MDiff] Finding a model for: TotalSpace F (TangentSpace I) [Elab.DiffGeo.MDiff] ✅️ TotalSpace [Elab.DiffGeo.MDiff] ❌️ From base info [Elab.DiffGeo.MDiff] Failed with error: No `baseInfo` provided [Elab.DiffGeo.MDiff] ✅️ TangentSpace [Elab.DiffGeo.MDiff] `TangentSpace I` is the total space of the `TangentBundle` of `M` [Elab.DiffGeo.MDiff] Found model: `I.prod I.tangent` [Elab.DiffGeo.MDiff] Found model: `I.prod I.tangent` [Elab.DiffGeo.MDiff] Finding a model for: F [Elab.DiffGeo.MDiff] ❌️ TotalSpace [Elab.DiffGeo.MDiff] Failed with error: `F` is not a `Bundle.TotalSpace`. [Elab.DiffGeo.MDiff] ❌️ TangentBundle [Elab.DiffGeo.MDiff] Failed with error: `F` is not a `TangentBundle` [Elab.DiffGeo.MDiff] ✅️ NormedSpace [Elab.DiffGeo.MDiff] `F` is a normed space over the field `𝕜` [Elab.DiffGeo.MDiff] Found model: `𝓘(𝕜, F)` -/ #guard_msgs in set_option trace.Elab.DiffGeo true in #check MDiff h -- The reason this test fails is that Bundle.TotalSpace F (TangentSpace I : M → Type _) is not -- the way to state smoothness. /-- error: failed to synthesize TopologicalSpace (TotalSpace F (TangentSpace I)) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #synth IsManifold I.tangent 1 (Bundle.TotalSpace F (TangentSpace I : M → Type _)) -- The correct way is this. /-- info: TotalSpace.isManifold E (TangentSpace I) -/ #guard_msgs in #synth IsManifold I.tangent 1 (TangentBundle I M) /-- info: MDifferentiable I.tangent 𝓘(𝕜, F) h' : Prop -/ #guard_msgs in #check MDifferentiable I.tangent 𝓘(𝕜, F) h' /-- info: MDifferentiable (I.prod 𝓘(𝕜, E)) 𝓘(𝕜, F) h' : Prop -/ #guard_msgs in #check MDifferentiable (I.prod (𝓘(𝕜, E))) 𝓘(𝕜, F) h' /-- info: MDifferentiable I.tangent 𝓘(𝕜, F) h' : Prop -/ #guard_msgs in #check MDiff h' end -- Inferring a model with corners on a space of continuous linear maps between normed spaces section variable {f : M → E →L[𝕜] E'} in /-- info: MDifferentiable I 𝓘(𝕜, E →L[𝕜] E') f : Prop -/ #guard_msgs in #check MDiff f variable {f : M → E →L[𝕜] E'} in /-- info: ContMDiff I 𝓘(𝕜, E →L[𝕜] E') 2 f : Prop -/ #guard_msgs in #check CMDiff 2 f section -- And the same test if E is a real normed space and E' is a normed space over a field R' which is -- definitionally equal to ℝ, but not at reducible transparency: this is meant to test the -- transparency handling in the definitional equality check in the model inference. def RealCopy := ℝ noncomputable instance : NormedField RealCopy := inferInstanceAs (NormedField ℝ) noncomputable instance : NontriviallyNormedField RealCopy := inferInstanceAs (NontriviallyNormedField ℝ) variable {E'' E''' : Type*} [NormedAddCommGroup E''] [NormedAddCommGroup E'''] [NormedSpace ℝ E''] [NormedSpace RealCopy E'''] def id' : ℝ →+* RealCopy := RingHom.id ℝ set_option trace.Elab.DiffGeo.MDiff true in variable {f : M → E'' →SL[id'] E'''} in /-- error: Could not find a model with corners for `E'' →SL[id'] E'''` --- trace: [Elab.DiffGeo.MDiff] Finding a model for: M [Elab.DiffGeo.MDiff] ❌️ TotalSpace [Elab.DiffGeo.MDiff] Failed with error: `M` is not a `Bundle.TotalSpace`. [Elab.DiffGeo.MDiff] ❌️ TangentBundle [Elab.DiffGeo.MDiff] Failed with error: `M` is not a `TangentBundle` [Elab.DiffGeo.MDiff] ❌️ NormedSpace [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `NormedSpace` structure on `M` among local instances. [Elab.DiffGeo.MDiff] ✅️ Manifold [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H M` [Elab.DiffGeo.MDiff] `M` is a charted space over `H` via `inst✝²²` [Elab.DiffGeo.MDiff] Found model: `I` [Elab.DiffGeo.MDiff] Finding a model for: E'' →SL[id'] E''' [Elab.DiffGeo.MDiff] ❌️ TotalSpace [Elab.DiffGeo.MDiff] Failed with error: `E'' →SL[id'] E'''` is not a `Bundle.TotalSpace`. [Elab.DiffGeo.MDiff] ❌️ TangentBundle [Elab.DiffGeo.MDiff] Failed with error: `E'' →SL[id'] E'''` is not a `TangentBundle` [Elab.DiffGeo.MDiff] ❌️ NormedSpace [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `NormedSpace` structure on `E'' →SL[id'] E'''` among local instances. [Elab.DiffGeo.MDiff] ❌️ Manifold [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H M` [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H' M'` [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `ChartedSpace` structure on `E'' →SL[id'] E'''` among local instances, and `E'' →SL[id'] E'''` is not the charted space of some type in the local context either. [Elab.DiffGeo.MDiff] ❌️ ContinuousLinearMap [Elab.DiffGeo.MDiff] `E'' →SL[id'] E'''` is a space of continuous linear maps [Elab.DiffGeo.MDiff] Failed with error: Coefficients `ℝ` and `RealCopy` of `E'' →SL[id'] E'''` are not reducibly definitionally equal [Elab.DiffGeo.MDiff] ❌️ RealInterval [Elab.DiffGeo.MDiff] Failed with error: `E'' →SL[id'] E'''` is not a coercion of a set to a type [Elab.DiffGeo.MDiff] ❌️ UpperHalfPlane [Elab.DiffGeo.MDiff] Failed with error: `E'' →SL[id'] E'''` is not the complex upper half plane [Elab.DiffGeo.MDiff] ❌️ NormedField [Elab.DiffGeo.MDiff] Failed with error: failed to synthesize NontriviallyNormedField (E'' →SL[id'] E''') ⏎ Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #check MDiff f variable {f : (E'' →SL[id'] E''') → E''} in /-- error: Could not find a model with corners for `E'' →SL[id'] E'''` -/ #guard_msgs in #check MDiff f variable {f : M → E'' →SL[id'] E'''} in /-- error: Could not find a model with corners for `E'' →SL[id'] E'''` -/ #guard_msgs in #check CMDiff 2 f end end /-! Inferring a model with corners on a real interval -/ section interval -- Make a new real manifold N with model J. -- TODO: change this line to modify M and E instead (thus testing if everything -- still works in the presence of two instances over different fields). variable {E'' : Type*} [NormedAddCommGroup E''] [NormedSpace ℝ E''] {J : ModelWithCorners ℝ E'' H} {N : Type} [TopologicalSpace N] [ChartedSpace H N] [IsManifold J 2 N] -- Types match, but no fact x < y can be inferred: mostly testing error messages. variable {x y : ℝ} {g : Set.Icc x y → N} {h : E'' → Set.Icc x y} {k : Set.Icc x y → ℝ} /-- error: failed to synthesize ChartedSpace (EuclideanHalfSpace 1) ↑(Set.Icc 0 2) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in variable {g : Set.Icc (0 : ℝ) (2 : ℝ) → M} in #check CMDiff 2 g /-- error: failed to synthesize ChartedSpace (EuclideanHalfSpace 1) ↑(Set.Icc x y) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #check CMDiff 2 g /-- error: failed to synthesize ChartedSpace (EuclideanHalfSpace 1) ↑(Set.Icc x y) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #check MDiffAt h /-- error: failed to synthesize ChartedSpace (EuclideanHalfSpace 1) ↑(Set.Icc x y) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #check MDiffAt k ⟨x, by linarith⟩ -- A singleton interval: this also should not synthesize. /-- error: failed to synthesize ChartedSpace (EuclideanHalfSpace 1) ↑(Set.Icc x x) Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in variable {k : Set.Icc x x → ℝ} in #check MDiff k /-- error: failed to synthesize Preorder α Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in variable {α : Type*} {x' y' : α} {k : Set.Icc x' y' → ℝ} in #check MDiff k /-- error: Could not find a model with corners for `↑(Set.Icc x' y')` -/ #guard_msgs in variable {α : Type*} [Preorder α] {x' y' : α} {k : ℝ → Set.Icc x' y'} in #check CMDiff 2 k -- Now, with a fact about x < y: these should behave well. variable {x y : ℝ} [Fact (x < y)] {g : Set.Icc x y → N} {h : E'' → Set.Icc x y} {k : Set.Icc x y → ℝ} /-- info: MDifferentiable (𝓡∂ 1) J g : Prop -/ #guard_msgs in variable [h: Fact ((0 : ℝ) < (2 : ℝ))] {g : Set.Icc (0 : ℝ) (2 : ℝ) → M} in #check MDiff g /-- info: MDifferentiable (𝓡∂ 1) J g : Prop -/ #guard_msgs in #check MDiff g /-- info: ContMDiff (𝓡∂ 1) J 2 g : Prop -/ #guard_msgs in #check CMDiff 2 g /-- info: MDifferentiableAt 𝓘(ℝ, E'') (𝓡∂ 1) h : E'' → Prop -/ #guard_msgs in #check MDiffAt h variable (h : x ≤ y) in /-- info: MDifferentiableAt (𝓡∂ 1) 𝓘(ℝ, ℝ) k ⟨x, ⋯⟩ : Prop -/ #guard_msgs in #check MDiffAt k ⟨x, by simp; linarith⟩ -- Test for the definitional equality check: for this type, `isDefEq` would succeed, but -- `withReducible <| isDefEq` does not. We do not want to consider a type synonym the same, -- so inferring a model with corners in this case should fail. def RealCopy' := ℝ instance : Preorder RealCopy' := inferInstanceAs (Preorder ℝ) instance : TopologicalSpace RealCopy' := inferInstanceAs (TopologicalSpace ℝ) -- Repeat the same test for an interval in RealCopy. variable {x y : RealCopy'} {g : Set.Icc x y → N} {h : E'' → Set.Icc x y} {k : Set.Icc x y → ℝ} [Fact (x < y)] noncomputable instance : ChartedSpace (EuclideanHalfSpace 1) ↑(Set.Icc x y) := instIccChartedSpace x y set_option trace.Elab.DiffGeo.MDiff true in /-- error: Could not find a model with corners for `↑(Set.Icc x y)` --- trace: [Elab.DiffGeo.MDiff] Finding a model for: ↑(Set.Icc x y) [Elab.DiffGeo.MDiff] ❌️ TotalSpace [Elab.DiffGeo.MDiff] Failed with error: `↑(Set.Icc x y)` is not a `Bundle.TotalSpace`. [Elab.DiffGeo.MDiff] ❌️ TangentBundle [Elab.DiffGeo.MDiff] Failed with error: `↑(Set.Icc x y)` is not a `TangentBundle` [Elab.DiffGeo.MDiff] ❌️ NormedSpace [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `NormedSpace` structure on `↑(Set.Icc x y)` among local instances. [Elab.DiffGeo.MDiff] ❌️ Manifold [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H M` [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H' M'` [Elab.DiffGeo.MDiff] considering instance of type `ChartedSpace H N` [Elab.DiffGeo.MDiff] Failed with error: Couldn't find a `ChartedSpace` structure on `↑(Set.Icc x y)` among local instances, and `↑(Set.Icc x y)` is not the charted space of some type in the local context either. [Elab.DiffGeo.MDiff] ❌️ ContinuousLinearMap [Elab.DiffGeo.MDiff] Failed with error: `↑(Set.Icc x y)` is not a space of continuous linear maps [Elab.DiffGeo.MDiff] ❌️ RealInterval [Elab.DiffGeo.MDiff] Failed with error: `Set.Icc x y` is a closed interval of type `RealCopy'`, which is not reducibly definitionally equal to ℝ [Elab.DiffGeo.MDiff] ❌️ UpperHalfPlane [Elab.DiffGeo.MDiff] Failed with error: `↑(Set.Icc x y)` is not the complex upper half plane [Elab.DiffGeo.MDiff] ❌️ NormedField [Elab.DiffGeo.MDiff] Failed with error: failed to synthesize NontriviallyNormedField ↑(Set.Icc x y) ⏎ Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command. -/ #guard_msgs in #check MDiffAt g /-- error: Could not find a model with corners for `↑(Set.Icc x y)` -/ #guard_msgs in #check MDiff h /-- error: Could not find a model with corners for `↑(Set.Icc x y)` -/ #guard_msgs in #check CMDiff 2 k end interval section UpperHalfPlane open scoped UpperHalfPlane -- Make a new complex manifold N with model J. -- TODO: change this line to modify M and E instead (thus testing if everything -- still works in the presence of two instances over different fields). variable {E'' : Type*} [NormedAddCommGroup E''] [NormedSpace ℂ E''] {J : ModelWithCorners ℂ E'' H} {N : Type} [TopologicalSpace N] [ChartedSpace H N] [IsManifold J 2 N] variable {g : ℍ → N} {h : E'' → ℍ} {k : ℍ → ℂ} {y : ℍ} /-- info: ContMDiff 𝓘(ℂ, ℂ) J 2 g : Prop -/ #guard_msgs in variable {g : ℍ → M} in #check CMDiff 2 g /-- info: ContMDiff 𝓘(ℂ, ℂ) J 2 g : Prop -/ #guard_msgs in #check CMDiff 2 g /-- info: MDifferentiableAt 𝓘(ℂ, E'') 𝓘(ℂ, ℂ) h : E'' → Prop -/ #guard_msgs in #check MDiffAt h /-- info: MDifferentiableAt 𝓘(ℂ, ℂ) 𝓘(ℂ, ℂ) k y : Prop -/ #guard_msgs in #check MDiffAt k y end UpperHalfPlane end differentiability /-! Tests for the custom elaborators for `mfderiv` and `mfderivWithin` -/ section mfderiv variable {EM' : Type*} [NormedAddCommGroup EM'] [NormedSpace 𝕜 EM'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 EM' H') {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] variable {f : M → M'} {s : Set M} {m : M} /-- info: mfderiv I I' f : (x : M) → TangentSpace I x →L[𝕜] TangentSpace I' (f x) -/ #guard_msgs in #check mfderiv% f /-- info: mfderiv I I' f m : TangentSpace I m →L[𝕜] TangentSpace I' (f m) -/ #guard_msgs in #check mfderiv% f m /-- info: mfderivWithin I I' f s : (x : M) → TangentSpace I x →L[𝕜] TangentSpace I' (f x) -/ #guard_msgs in #check mfderiv[s] f /-- info: mfderivWithin I I' f s m : TangentSpace I m →L[𝕜] TangentSpace I' (f m) -/ #guard_msgs in #check mfderiv[s] f m variable {f : E → EM'} {s : Set E} {m : E} /-- info: mfderiv 𝓘(𝕜, E) 𝓘(𝕜, EM') f : (x : E) → TangentSpace 𝓘(𝕜, E) x →L[𝕜] TangentSpace 𝓘(𝕜, EM') (f x) -/ #guard_msgs in #check mfderiv% f /-- info: mfderiv 𝓘(𝕜, E) 𝓘(𝕜, EM') f m : TangentSpace 𝓘(𝕜, E) m →L[𝕜] TangentSpace 𝓘(𝕜, EM') (f m) -/ #guard_msgs in #check mfderiv% f m /-- info: mfderivWithin 𝓘(𝕜, E) 𝓘(𝕜, EM') f s : (x : E) → TangentSpace 𝓘(𝕜, E) x →L[𝕜] TangentSpace 𝓘(𝕜, EM') (f x) -/ #guard_msgs in #check mfderiv[s] f /-- info: mfderivWithin 𝓘(𝕜, E) 𝓘(𝕜, EM') f s m : TangentSpace 𝓘(𝕜, E) m →L[𝕜] TangentSpace 𝓘(𝕜, EM') (f m) -/ #guard_msgs in #check mfderiv[s] f m variable {σ : Π x : M, V x} {σ' : (x : E) → Trivial E E' x} {s : E → E'} variable (X : (m : M) → TangentSpace I m) [IsManifold I 1 M] {x : M} /-- info: mfderiv I (I.prod 𝓘(𝕜, E)) (fun m ↦ TotalSpace.mk' E m (X m)) x : TangentSpace I x →L[𝕜] TangentSpace (I.prod 𝓘(𝕜, E)) (TotalSpace.mk' E x (X x)) -/ #guard_msgs in #check mfderiv% (T% X) x /-- info: mfderiv I (I.prod 𝓘(𝕜, F)) (fun x ↦ TotalSpace.mk' F x (σ x)) x : TangentSpace I x →L[𝕜] TangentSpace (I.prod 𝓘(𝕜, F)) (TotalSpace.mk' F x (σ x)) -/ #guard_msgs in #check mfderiv% (T% σ) x variable {t : Set E} {p : E} /-- info: mfderivWithin 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (fun x ↦ TotalSpace.mk' E' x (σ' x)) t p : TangentSpace 𝓘(𝕜, E) p →L[𝕜] TangentSpace (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (TotalSpace.mk' E' p (σ' p)) -/ #guard_msgs in #check mfderiv[t] (T% σ') p /-- info: mfderivWithin 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (fun x ↦ TotalSpace.mk' E' x (σ' x)) t : (x : E) → TangentSpace 𝓘(𝕜, E) x →L[𝕜] TangentSpace (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (TotalSpace.mk' E' x (σ' x)) -/ #guard_msgs in #check mfderiv[t] (T% σ') section errors -- Test an error message, about mismatched types. variable {s' : Set M} {m' : M} /-- error: Application type mismatch: The argument m' has type M of sort `Type u_4` but is expected to have type E of sort `Type u_2` in the application mfderiv 𝓘(𝕜, E) 𝓘(𝕜, EM') f m' --- info: mfderiv 𝓘(𝕜, E) 𝓘(𝕜, EM') f sorry : TangentSpace 𝓘(𝕜, E) sorry →L[𝕜] TangentSpace 𝓘(𝕜, EM') (f sorry) -/ #guard_msgs in #check mfderiv% f m' -- Error messages: argument s has mismatched type. /-- error: The domain `E` of `f` is not definitionally equal to the carrier type of the set `s'` : `Set M` -/ #guard_msgs in #check mfderiv[s'] f /-- error: The domain `E` of `f` is not definitionally equal to the carrier type of the set `s'` : `Set M` -/ #guard_msgs in #check mfderiv[s'] f m end errors section /-- error: Term `X` is a dependent function, of type `(m : M) → TangentSpace I m` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check mfderiv% X x /-- error: Term `σ` is a dependent function, of type `(x : M) → V x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check mfderiv% σ x variable {t : Set E} {p : E} /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check mfderiv[t] σ' p /-- error: Term `σ'` is a dependent function, of type `(x : E) → Trivial E E' x` Hint: you can use the `T%` elaborator to convert a dependent function to a non-dependent one -/ #guard_msgs in #check mfderiv[t] σ' end end mfderiv /-! Tests for the custom elaborators for `HasMFDeriv` and `HasMFDerivWithin` -/ section HasMFDeriv variable {EM' : Type*} [NormedAddCommGroup EM'] [NormedSpace 𝕜 EM'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 EM' H') {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] variable {f : M → M'} {s : Set M} {m : M} {f' : TangentSpace I m →L[𝕜] TangentSpace I' (f m)} /-- info: HasMFDerivAt I I' f m f' : Prop -/ #guard_msgs in #check HasMFDerivAt% f m f' /-- info: HasMFDerivWithinAt I I' f s m f' : Prop -/ #guard_msgs in #check HasMFDerivAt[s] f m f' variable {f : E → EM'} {s : Set E} {m : E} -- #check mfderiv% f m tells us the type of f :-) {f' : TangentSpace 𝓘(𝕜, E) m →L[𝕜] TangentSpace 𝓘(𝕜, EM') (f m)} /-- info: HasMFDerivAt 𝓘(𝕜, E) 𝓘(𝕜, EM') f m f' : Prop -/ #guard_msgs in #check HasMFDerivAt% f m f' /-- info: HasMFDerivWithinAt 𝓘(𝕜, E) 𝓘(𝕜, EM') f s m f' : Prop -/ #guard_msgs in #check HasMFDerivAt[s] f m f' variable {σ : Π x : M, V x} {σ' : (x : E) → Trivial E E' x} {s : E → E'} variable (X : (m : M) → TangentSpace I m) [IsManifold I 1 M] {x : M} /-- info: mfderiv I (I.prod 𝓘(𝕜, E)) (fun m ↦ TotalSpace.mk' E m (X m)) x : TangentSpace I x →L[𝕜] TangentSpace (I.prod 𝓘(𝕜, E)) (TotalSpace.mk' E x (X x)) -/ #guard_msgs in #check mfderiv% (T% X) x variable {dXm : TangentSpace I x →L[𝕜] TangentSpace (I.prod 𝓘(𝕜, E)) (TotalSpace.mk' E x (X x))} /-- info: HasMFDerivAt I (I.prod 𝓘(𝕜, E)) (fun m ↦ TotalSpace.mk' E m (X m)) x dXm : Prop -/ #guard_msgs in #check HasMFDerivAt% (T% X) x dXm /-- info: HasMFDerivWithinAt I (I.prod 𝓘(𝕜, E)) (fun m ↦ TotalSpace.mk' E m (X m)) t x dXm : Prop -/ #guard_msgs in variable {t : Set M} in #check HasMFDerivAt[t] (T% X) x dXm /-- info: mfderiv I (I.prod 𝓘(𝕜, F)) (fun x ↦ TotalSpace.mk' F x (σ x)) x : TangentSpace I x →L[𝕜] TangentSpace (I.prod 𝓘(𝕜, F)) (TotalSpace.mk' F x (σ x)) -/ #guard_msgs in #check mfderiv% (T% σ) x variable {dσm : TangentSpace I x →L[𝕜] TangentSpace (I.prod 𝓘(𝕜, F)) (TotalSpace.mk' F x (σ x))} /-- info: HasMFDerivAt I (I.prod 𝓘(𝕜, F)) (fun x ↦ TotalSpace.mk' F x (σ x)) x dσm : Prop -/ #guard_msgs in #check HasMFDerivAt% (T% σ) x dσm /-- info: HasMFDerivWithinAt I (I.prod 𝓘(𝕜, F)) (fun x ↦ TotalSpace.mk' F x (σ x)) t x dσm : Prop -/ #guard_msgs in variable {t : Set M} in #check HasMFDerivAt[t] (T% σ) x dσm variable {t : Set E} {p : E} /-- info: mfderivWithin 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (fun x ↦ TotalSpace.mk' E' x (σ' x)) t p : TangentSpace 𝓘(𝕜, E) p →L[𝕜] TangentSpace (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (TotalSpace.mk' E' p (σ' p)) -/ #guard_msgs in #check mfderiv[t] (T% σ') p variable {dσ'p : TangentSpace 𝓘(𝕜, E) p →L[𝕜] TangentSpace (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (TotalSpace.mk' E' p (σ' p))} /-- info: HasMFDerivAt 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (fun x ↦ TotalSpace.mk' E' x (σ' x)) p dσ'p : Prop -/ #guard_msgs in #check HasMFDerivAt% (T% σ') p dσ'p /-- info: HasMFDerivWithinAt 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (fun x ↦ TotalSpace.mk' E' x (σ' x)) t p dσ'p : Prop -/ #guard_msgs in #check HasMFDerivAt[t] (T% σ') p dσ'p /-- info: mfderivWithin 𝓘(𝕜, E) (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (fun x ↦ TotalSpace.mk' E' x (σ' x)) t : (x : E) → TangentSpace 𝓘(𝕜, E) x →L[𝕜] TangentSpace (𝓘(𝕜, E).prod 𝓘(𝕜, E')) (TotalSpace.mk' E' x (σ' x)) -/ #guard_msgs in #check mfderiv[t] (T% σ') -- TODO: skipped the test about error messages (analogous to mfderiv(Within)) end HasMFDeriv
.lake/packages/mathlib/MathlibTest/Simproc/ExistsAndEq.lean
import Mathlib.Tactic.Simproc.ExistsAndEq universe u v variable (α : Type u) (β : Type v) example (P Q : α → Prop) (a : α) (hp : P a) (hq : Q a) : ∃ b : α, (P b ∧ b = a) ∧ Q b := by simp only [existsAndEq] guard_target = (P a ∧ True) ∧ Q a exact ⟨⟨hp, trivial⟩, hq⟩ example (a : α) : ∃ b : α, b = a := by simp only [existsAndEq] /-- error: `simp` made no progress -/ #guard_msgs in example (f : α → α) : ∃ a : α, a = f a := by simp only [existsAndEq] /-- error: `simp` made no progress -/ #guard_msgs in example {β : α → Type v} (a : α) : ∃ x, ∃ y : β x, x = a := by simp only [existsAndEq] example (f : β → α) {P Q : β → Prop} : (∃ y b, P b ∧ f b = y ∧ Q b) ↔ ∃ b, P b ∧ Q b := by simp only [existsAndEq, true_and] example (f : β → α) {P Q : β → Prop} : (∃ x b, P b ∧ (∃ c, f c = x) ∧ (∃ d, Q d ∧ f d = x) ∧ Q b) = ∃ b c, P b ∧ f c = f c ∧ (∃ d, Q d ∧ f d = f c) ∧ Q b := by simp only [existsAndEq] example (f : β → α) {P : α → Prop} : (∃ a, P a ∧ ∃ b, a = f b) ↔ ∃ b, P (f b) := by simp only [existsAndEq, and_true] -- The simproc should not trigger on `a = a'` when `a'` depends on `a` /-- error: `simp` made no progress -/ #guard_msgs in example {α : Type} : ∃ a : α, ∃ b : α → α, b a = a := by simp only [existsAndEq] -- lemmas like `Subtype.exists` and `Prod.exists` prevent `existsAndEq` -- from working as a post simproc, so it is a pre simproc. /-- error: unsolved goals α : Type u β : Type v P Q : α × β → Prop a : α × β ⊢ (∃ a_1 b, (P (a_1, b) ∧ (a_1, b) = a) ∧ Q (a_1, b)) ↔ P a ∧ Q a -/ #guard_msgs in set_option linter.unusedSimpArgs false in example (P Q : α × β → Prop) (a : α × β) : (∃ b : (α × β), (P b ∧ b = a) ∧ Q b) ↔ P a ∧ Q a := by simp only [Prod.exists, existsAndEq] example (P Q : α × β → Prop) (a : α × β) : (∃ b : (α × β), (P b ∧ b = a) ∧ Q b) ↔ P a ∧ Q a := by simp
.lake/packages/mathlib/MathlibTest/Simproc/Divisors.lean
import Mathlib.Tactic.Simproc.Divisors import Mathlib.NumberTheory.Primorial open Nat example : Nat.divisors 1710 = {1, 2, 3, 5, 6, 9, 10, 15, 18, 19, 30, 38, 45, 57, 90, 95, 114, 171, 190, 285, 342, 570, 855, 1710} := by simp only [Nat.divisors_ofNat] example : Nat.divisors 57 = {1, 3, 19, 57} := by simp only [Nat.divisors_ofNat] example : (Nat.divisors <| 6 !).card = 30 := by simp [Nat.divisors_ofNat, Nat.factorial_succ] example : (Nat.divisors <| primorial 12).card = 32 := by --TODO(Paul-Lez): seems like we need a norm_num extension for computing `primorial`! have : primorial 12 = 2310 := rfl simp [Nat.divisors_ofNat, this] example : (Nat.divisors <| 2^13).card = 14 := by simp [Nat.divisors_ofNat] example : 2 ≤ Finset.card (Nat.divisors 3) := by simp [Nat.divisors_ofNat] /-- error: `simp` made no progress -/ #guard_msgs in example (n : ℕ) (hn : n ≠ 0) : 1 ≤ Finset.card (Nat.divisors n) := by simp only [Nat.divisors_ofNat] example : Nat.properDivisors 1710 = {1, 2, 3, 5, 6, 9, 10, 15, 18, 19, 30, 38, 45, 57, 90, 95, 114, 171, 190, 285, 342, 570, 855} := by simp only [Nat.properDivisors_ofNat] example : Nat.properDivisors 57 = {1, 3, 19} := by simp only [Nat.properDivisors_ofNat] example : 2 ≤ Finset.card (Nat.divisors 3) := by simp [Nat.divisors_ofNat]
.lake/packages/mathlib/MathlibTest/Simproc/ProdUnivMany.lean
import Mathlib.Data.Fin.Tuple.Reflection @[to_additive] lemma prod_test (R : Type) [CommMonoid R] (f : Fin 10 → R) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 * f 6 * f 7 * f 8 * f 9 := by simp only [Fin.prod_univ_ofNat] /-- info: sum_test (R : Type) [AddCommMonoid R] (f : Fin 10 → R) : ∑ i, f i = f 0 + f 1 + f 2 + f 3 + f 4 + f 5 + f 6 + f 7 + f 8 + f 9 -/ #guard_msgs in #check sum_test example (R : Type) [AddCommMonoid R] (f : Fin 10 → R) : ∑ i, f i = f 0 + f 1 + f 2 + f 3 + f 4 + f 5 + f 6 + f 7 + f 8 + f 9 := by simp only [Fin.sum_univ_ofNat]
.lake/packages/mathlib/MathlibTest/Util/Qq.lean
import Mathlib.Util.Qq import Mathlib.Data.Finset.Basic open Qq Lean Elab Meta section mkSetLiteralQ /-- info: {1, 2, 3} : Finset ℕ -/ #guard_msgs in #check by_elab return mkSetLiteralQ q(Finset ℕ) [q(1), q(2), q(3)] /-- info: {1, 2, 3} : Multiset ℕ -/ #guard_msgs in #check by_elab return mkSetLiteralQ q(Multiset ℕ) [q(1), q(2), q(3)] /-- info: {1, 2, 3} : Set ℕ -/ #guard_msgs in #check by_elab return mkSetLiteralQ q(Set ℕ) [q(1), q(2), q(3)] /-- info: {1, 2, 3} : List ℕ -/ #guard_msgs in #check by_elab return mkSetLiteralQ q(List ℕ) [q(1), q(2), q(3)] /-- info: {0 ^ 2, 1 ^ 2, 2 ^ 2, 3 ^ 2} : Finset ℕ -/ #guard_msgs in #check by_elab return mkSetLiteralQ q(Finset ℕ) (List.range 4 |>.map fun n : ℕ ↦ q($n^2)) end mkSetLiteralQ
.lake/packages/mathlib/MathlibTest/Util/PrintSorries.lean
import Mathlib.Util.PrintSorries set_option pp.mvars false /-! Direct use of `sorry` -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in theorem thm1 : 1 = 2 := by sorry /-- info: thm1 has sorry of type 1 = 2 -/ #guard_msgs in #print sorries thm1 /-! Indirect use of `sorry` -/ theorem thm2 : 1 = 2 := thm1 /-- info: thm1 has sorry of type 1 = 2 -/ #guard_msgs in #print sorries thm2 /-! Print all uses of `sorry` in the current module. -/ /-- info: thm1 has sorry of type 1 = 2 -/ #guard_msgs in #print sorries /-! Don't overreport. Remembers that `thm1` already reported the `sorry`. -/ /-- info: thm1 has sorry of type 1 = 2 -/ #guard_msgs in #print sorries thm1 thm2 /-! Reports sorries (indirectly) appearing in the types of theorems. -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in def f (n : Nat) : Nat := sorry theorem thm3 : f 1 = f 2 := rfl -- (!) This works since it's a fixed `sorry : Nat` /-- info: f has sorry of type Nat -/ #guard_msgs in #print sorries thm3 /-! If `sorry` appears in the type of a theorem, when it's used, it reports the theorem with the `sorry`, even though `sorry` appears in the theorem that used it as well. -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in theorem thm_sorry (n : Nat) : n = sorry := sorry /-- warning: declaration uses 'sorry' -/ #guard_msgs in theorem thm_use_it (m n : Nat) : m = n := by rw [thm_sorry m, thm_sorry n] /-- info: thm_sorry has sorry of type Nat thm_sorry has sorry of type n = sorry -/ #guard_msgs in #print sorries thm_use_it /-! Reports synthetic sorries specially. -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in def f' : Nat → Nat := sorry /-- error: Not a definitional equality: the left-hand side f' 1 is not definitionally equal to the right-hand side f' 2 --- error: Type mismatch rfl has type ?_ = ?_ but is expected to have type f' 1 = f' 2 -/ #guard_msgs in theorem thm3' : f' 1 = f' 2 := rfl -- fails as expected, `f'` is an unknown `sorry : Nat → Nat` /-- info: f' has sorry of type Nat → Nat thm3' has sorry (from error) of type f' 1 = f' 2 -/ #guard_msgs in #print sorries thm3' /-! Unfolding functions can lead to many copies of `sorry` in a term. This proof contains 4 sorry terms. -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in theorem thm : True := by let f : Nat → Nat := sorry have : f 1 = f 2 := sorry unfold f at this change id _ at this trivial /-- info: thm has sorry of type Nat → Nat thm has sorry of type f 1 = f 2 -/ #guard_msgs in #print sorries thm /-! Raw, unlabeled sorry. Its "go to definition" unfortunately goes to `sorryAx` itself. -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in theorem thm' : True := sorryAx _ false /-- info: thm' has sorry of type True -/ #guard_msgs in #print sorries thm' /-! The `sorry` pretty printing can handle free variables. -/ /-- warning: declaration uses 'sorry' -/ #guard_msgs in def g (α : Type) : α := sorry /-- info: g has sorry of type α -/ #guard_msgs in #print sorries g /-! `#print sorries in` -/ /-- info: in_test_1 has sorry of type True --- warning: declaration uses 'sorry' -/ #guard_msgs in #print sorries in theorem in_test_1 : True := sorry /-- info: Declarations are sorry-free! -/ #guard_msgs in #print sorries in theorem in_test_2 : True := trivial /-! ### Other sorry-producing commands Check that `admit` and `stop` are correctly handled -/ /-- info: thm4 has sorry of type True --- warning: declaration uses 'sorry' -/ #guard_msgs in #print sorries in theorem thm4 : True := by admit /-- info: thm5 has sorry of type True --- warning: declaration uses 'sorry' -/ #guard_msgs in #print sorries in theorem thm5 : True := by stop admit
.lake/packages/mathlib/MathlibTest/Nat/log.lean
import Mathlib.Data.Nat.Log /-! This used to fail (ran out of heartbeats) but with a new faster `Nat.logC` tagged `csimp`, it succeeds. -/ /-- info: 10000000 -/ #guard_msgs in #eval Nat.log 2 (2 ^ 10000000)
.lake/packages/mathlib/MathlibTest/CategoryTheory/Coherence.lean
import Mathlib.Tactic.CategoryTheory.Coherence open CategoryTheory universe w v u section Monoidal variable {C : Type u} [Category.{v} C] [MonoidalCategory C] open scoped MonoidalCategory -- Internal tactics example (X₁ X₂ : C) : ((λ_ (𝟙_ C)).inv ⊗ₘ 𝟙 (X₁ ⊗ X₂)) ≫ (α_ (𝟙_ C) (𝟙_ C) (X₁ ⊗ X₂)).hom ≫ (𝟙 (𝟙_ C) ⊗ₘ (α_ (𝟙_ C) X₁ X₂).inv) = 𝟙 (𝟙_ C) ⊗ₘ ((λ_ X₁).inv ⊗ₘ 𝟙 X₂) := by pure_coherence -- This is just running: -- change projectMap id _ _ (LiftHom.lift (((λ_ (𝟙_ C)).inv ⊗ 𝟙 (X₁ ⊗ X₂)) ≫ -- (α_ (𝟙_ C) (𝟙_ C) (X₁ ⊗ X₂)).hom ≫ (𝟙 (𝟙_ C) ⊗ (α_ (𝟙_ C) X₁ X₂).inv))) = -- projectMap id _ _ (LiftHom.lift (𝟙 (𝟙_ C) ⊗ ((λ_ X₁).inv ⊗ 𝟙 X₂))) -- exact congrArg _ (Subsingleton.elim _ _) example {Y Z : C} (f : Y ⟶ Z) (g) (w : false) : (λ_ _).hom ≫ f = g := by liftable_prefixes guard_target = (𝟙 _ ≫ (λ_ _).hom) ≫ f = (𝟙 _) ≫ g cases w -- `coherence` example (f : 𝟙_ C ⟶ _) : f ≫ (λ_ (𝟙_ C)).hom = f ≫ (ρ_ (𝟙_ C)).hom := by coherence example (f) : (λ_ (𝟙_ C)).hom ≫ f ≫ (λ_ (𝟙_ C)).hom = (ρ_ (𝟙_ C)).hom ≫ f ≫ (ρ_ (𝟙_ C)).hom := by coherence example {U : C} (f : U ⟶ 𝟙_ C) : f ≫ (ρ_ (𝟙_ C)).inv ≫ (λ_ (𝟙_ C)).hom = f := by coherence example (W X Y Z : C) (f) : ((α_ W X Y).hom ⊗ₘ 𝟙 Z) ≫ (α_ W (X ⊗ Y) Z).hom ≫ (𝟙 W ⊗ₘ (α_ X Y Z).hom) ≫ f ≫ (α_ (W ⊗ X) Y Z).hom ≫ (α_ W X (Y ⊗ Z)).hom = (α_ (W ⊗ X) Y Z).hom ≫ (α_ W X (Y ⊗ Z)).hom ≫ f ≫ ((α_ W X Y).hom ⊗ₘ 𝟙 Z) ≫ (α_ W (X ⊗ Y) Z).hom ≫ (𝟙 W ⊗ₘ (α_ X Y Z).hom) := by coherence example {U V W X Y : C} (f : U ⟶ V ⊗ (W ⊗ X)) (g : (V ⊗ W) ⊗ X ⟶ Y) : f ⊗≫ g = f ≫ (α_ _ _ _).inv ≫ g := by coherence example : (λ_ (𝟙_ C)).hom = (ρ_ (𝟙_ C)).hom := by coherence example : (λ_ (𝟙_ C)).inv = (ρ_ (𝟙_ C)).inv := by coherence example (X Y Z : C) : (α_ X Y Z).inv ≫ (α_ X Y Z).hom = 𝟙 (X ⊗ Y ⊗ Z) := by coherence example (X Y Z W : C) : (𝟙 X ⊗ₘ (α_ Y Z W).hom) ≫ (α_ X Y (Z ⊗ W)).inv ≫ (α_ (X ⊗ Y) Z W).inv = (α_ X (Y ⊗ Z) W).inv ≫ ((α_ X Y Z).inv ⊗ₘ 𝟙 W) := by coherence example (X Y : C) : (𝟙 X ⊗ₘ (λ_ Y).inv) ≫ (α_ X (𝟙_ C) Y).inv = (ρ_ X).inv ⊗ₘ 𝟙 Y := by coherence example (X Y : C) (f : 𝟙_ C ⟶ X) (g : X ⟶ Y) (_w : false) : (λ_ (𝟙_ C)).hom ≫ f ≫ 𝟙 X ≫ g = (ρ_ (𝟙_ C)).hom ≫ f ≫ g := by coherence example (X₁ X₂ : C) : (α_ (𝟙_ C) (𝟙_ C) (X₁ ⊗ X₂)).hom ≫ (𝟙 (𝟙_ C) ⊗ₘ (α_ (𝟙_ C) X₁ X₂).inv) ≫ (𝟙 (𝟙_ C) ⊗ₘ (λ_ _).hom ≫ (ρ_ X₁).inv ⊗ₘ 𝟙 X₂) ≫ (𝟙 (𝟙_ C) ⊗ₘ (α_ X₁ (𝟙_ C) X₂).hom) ≫ (α_ (𝟙_ C) X₁ (𝟙_ C ⊗ X₂)).inv ≫ ((λ_ X₁).hom ≫ (ρ_ X₁).inv ⊗ₘ 𝟙 (𝟙_ C ⊗ X₂)) ≫ (α_ X₁ (𝟙_ C) (𝟙_ C ⊗ X₂)).hom ≫ (𝟙 X₁ ⊗ₘ 𝟙 (𝟙_ C) ⊗ₘ (λ_ X₂).hom ≫ (ρ_ X₂).inv) ≫ (𝟙 X₁ ⊗ₘ (α_ (𝟙_ C) X₂ (𝟙_ C)).inv) ≫ (𝟙 X₁ ⊗ₘ (λ_ X₂).hom ≫ (ρ_ X₂).inv ⊗ₘ 𝟙 (𝟙_ C)) ≫ (𝟙 X₁ ⊗ₘ (α_ X₂ (𝟙_ C) (𝟙_ C)).hom) ≫ (α_ X₁ X₂ (𝟙_ C ⊗ 𝟙_ C)).inv = (((λ_ (𝟙_ C)).hom ⊗ₘ 𝟙 (X₁ ⊗ X₂)) ≫ (λ_ (X₁ ⊗ X₂)).hom ≫ (ρ_ (X₁ ⊗ X₂)).inv) ≫ (𝟙 (X₁ ⊗ X₂) ⊗ₘ (λ_ (𝟙_ C)).inv) := by coherence end Monoidal section Bicategory open scoped Bicategory variable {B : Type u} [Bicategory.{w, v} B] {a b c d e : B} example {a : B} (f : a ⟶ a) : 𝟙 f ▷ f = 𝟙 (f ≫ f) := by whisker_simps example : (λ_ (𝟙 a)).hom = (ρ_ (𝟙 a)).hom := by bicategory_coherence example : (λ_ (𝟙 a)).inv = (ρ_ (𝟙 a)).inv := by bicategory_coherence example (f : a ⟶ b) (g : b ⟶ c) (h : c ⟶ d) : (α_ f g h).inv ≫ (α_ f g h).hom = 𝟙 (f ≫ g ≫ h) := by bicategory_coherence example (f : a ⟶ b) (g : b ⟶ c) (h : c ⟶ d) (i : d ⟶ e) : f ◁ (α_ g h i).hom ≫ (α_ f g (h ≫ i)).inv ≫ (α_ (f ≫ g) h i).inv = (α_ f (g ≫ h) i).inv ≫ (α_ f g h).inv ▷ i := by bicategory_coherence example (f : a ⟶ b) (g : b ⟶ c) : f ◁ (λ_ g).inv ≫ (α_ f (𝟙 b) g).inv = (ρ_ f).inv ▷ g := by bicategory_coherence example : 𝟙 (𝟙 a ≫ 𝟙 a) ≫ (λ_ (𝟙 a)).hom = 𝟙 (𝟙 a ≫ 𝟙 a) ≫ (ρ_ (𝟙 a)).hom := by bicategory_coherence set_option linter.unusedVariables false in example (f g : a ⟶ a) (η : 𝟙 a ⟶ f) (θ : f ⟶ g) (w : false) : (λ_ (𝟙 a)).hom ≫ η ≫ θ = (ρ_ (𝟙 a)).hom ≫ η ≫ θ := by coherence example (f₁ : a ⟶ b) (f₂ : b ⟶ c) : (α_ (𝟙 a) (𝟙 a) (f₁ ≫ f₂)).hom ≫ 𝟙 a ◁ (α_ (𝟙 a) f₁ f₂).inv ≫ 𝟙 a ◁ ((λ_ f₁).hom ≫ (ρ_ f₁).inv) ▷ f₂ ≫ 𝟙 a ◁ (α_ f₁ (𝟙 b) f₂).hom ≫ (α_ (𝟙 a) f₁ (𝟙 b ≫ f₂)).inv ≫ ((λ_ f₁).hom ≫ (ρ_ f₁).inv) ▷ (𝟙 b ≫ f₂) ≫ (α_ f₁ (𝟙 b) (𝟙 b ≫ f₂)).hom ≫ f₁ ◁ 𝟙 b ◁ ((λ_ f₂).hom ≫ (ρ_ f₂).inv) ≫ f₁ ◁ (α_ (𝟙 b) f₂ (𝟙 c)).inv ≫ f₁ ◁ ((λ_ f₂).hom ≫ (ρ_ f₂).inv) ▷ 𝟙 c ≫ (f₁ ◁ (α_ f₂ (𝟙 c) (𝟙 c)).hom) ≫ (α_ f₁ f₂ (𝟙 c ≫ 𝟙 c)).inv = ((λ_ (𝟙 a)).hom ▷ (f₁ ≫ f₂) ≫ (λ_ (f₁ ≫ f₂)).hom ≫ (ρ_ (f₁ ≫ f₂)).inv) ≫ (f₁ ≫ f₂) ◁ (λ_ (𝟙 c)).inv := by pure_coherence end Bicategory
.lake/packages/mathlib/MathlibTest/CategoryTheory/Elementwise.lean
import Mathlib.Tactic.CategoryTheory.Elementwise import Mathlib.Algebra.Category.MonCat.Basic set_option autoImplicit true namespace ElementwiseTest open CategoryTheory namespace HasForget attribute [simp] Iso.hom_inv_id Iso.inv_hom_id IsIso.hom_inv_id IsIso.inv_hom_id attribute [local instance] HasForget.instFunLike HasForget.hasCoeToSort @[elementwise] theorem ex1 [Category C] [HasForget C] (X : C) (f g h : X ⟶ X) (h' : g ≫ h = h ≫ g) : f ≫ g ≫ h = f ≫ h ≫ g := by rw [h'] -- If there is already a `HasForget` instance, do not add a new argument. example : ∀ C [Category C] [HasForget C] (X : C) (f g h : X ⟶ X) (_ : g ≫ h = h ≫ g) (x : X), h (g (f x)) = g (h (f x)) := @ex1_apply @[elementwise] theorem ex2 [Category C] (X : C) (f g h : X ⟶ X) (h' : g ≫ h = h ≫ g) : f ≫ g ≫ h = f ≫ h ≫ g := by rw [h'] -- If there is not already a `ConcreteCategory` instance, insert a new argument. example : ∀ C [Category C] (X : C) (f g h : X ⟶ X) (_ : g ≫ h = h ≫ g) {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory C FC] (x : ToType X), h (g (f x)) = g (h (f x)) := @ex2_apply -- Need nosimp on the following `elementwise` since the lemma can be proved by simp anyway. @[elementwise nosimp] theorem ex3 [Category C] {X Y : C} (f : X ≅ Y) : f.hom ≫ f.inv = 𝟙 X := Iso.hom_inv_id _ example : ∀ C [Category C] (X Y : C) (f : X ≅ Y) {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory C FC] (x : ToType X), f.inv (f.hom x) = x := @ex3_apply -- Make sure there's no `id x` in there: example : ∀ C [Category C] (X Y : C) (f : X ≅ Y) {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory C FC] (x : ToType X), f.inv (f.hom x) = x := by intros; simp only [ex3_apply] @[elementwise] lemma foo [Category C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) : f ≫ 𝟙 N ≫ g = h := by simp [w] @[elementwise] lemma foo' [Category C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) : f ≫ 𝟙 N ≫ g = h := by simp [w] lemma bar [Category C] {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory C FC] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w example {M N K : Type} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by have := elementwise_of% w guard_hyp this : ∀ (x : M), g (f x) = h x exact this x example {M N K : Type} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := (elementwise_of% w) x example [Category C] [HasForget C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by have := elementwise_of% w guard_hyp this : ∀ (x : M), g (f x) = h x exact this x -- `elementwise_of%` allows a level metavariable for its `ConcreteCategory` instance. -- Previously this example did not specify that the universe levels of `C` and `D` (inside `h`) -- were the same, and this constraint was added post-hoc by the proof term. -- After https://github.com/leanprover/lean4/pull/4493 this no longer works (happily!). example {C : Type u} [Category.{v} C] {FC : C → C → Type _} {CC : C → Type w} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory.{w} C FC] (h : ∀ (D : Type u) [Category.{v} D] (X Y : D) (f : X ⟶ Y) (g : Y ⟶ X), f ≫ g = 𝟙 X) {M N : C} {f : M ⟶ N} {g : N ⟶ M} (x : M) : g (f x) = x := by have := elementwise_of% h guard_hyp this : ∀ D [Category D] (X Y : D) (f : X ⟶ Y) (g : Y ⟶ X) {FD : D → D → Type _} {CD : D → Type*} [∀ X Y, FunLike (FD X Y) (CD X) (CD Y)] [ConcreteCategory D FD] (x : ToType X), g (f x) = x rw [this] section Mon lemma bar' {M N K : MonCat} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by exact foo_apply w x lemma bar'' {M N K : MonCat} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w lemma bar''' {M N K : MonCat} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w example (M N K : MonCat) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) : g (f m) = h m := by rw [elementwise_of% w] example (M N K : MonCat) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) : g (f m) = h m := by replace w := elementwise_of% w apply w end Mon example {α β : Type} (f g : α ⟶ β) (w : f = g) (a : α) : f a = g a := by replace w := elementwise_of% w guard_hyp w : ∀ (x : α), f x = g x rw [w] example {α β : Type} (f g : α ⟶ β) (w : f ≫ 𝟙 β = g) (a : α) : f a = g a := by replace w := elementwise_of% w guard_hyp w : ∀ (x : α), f x = g x rw [w] variable {C : Type*} [Category C] def f (X : C) : X ⟶ X := 𝟙 X def g (X : C) : X ⟶ X := 𝟙 X def h (X : C) : X ⟶ X := 𝟙 X lemma gh (X : C) : g X = h X := rfl @[elementwise] theorem fh (X : C) : f X = h X := gh X variable (X : C) {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] variable [ConcreteCategory C FC] (x : X) -- Prior to https://github.com/leanprover-community/mathlib4/pull/13413 this would produce -- `fh_apply X x : (g X) x = (h X) x`. /-- info: fh_apply X x : (ConcreteCategory.hom (f X)) x = (ConcreteCategory.hom (h X)) x -/ #guard_msgs in #check fh_apply X x end HasForget namespace ConcreteCategory attribute [simp] Iso.hom_inv_id Iso.inv_hom_id IsIso.hom_inv_id IsIso.inv_hom_id attribute [simp] Iso.hom_inv_id Iso.inv_hom_id IsIso.hom_inv_id IsIso.inv_hom_id variable {C : Type*} {FC : C → C → Type*} {CC : C → Type*} variable [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] @[elementwise] theorem ex1 [Category C] [ConcreteCategory C FC] (X : C) (f g h : X ⟶ X) (h' : g ≫ h = h ≫ g) : f ≫ g ≫ h = f ≫ h ≫ g := by rw [h'] -- If there is already a `ConcreteCategory` instance, do not add a new argument. example : ∀ C {FC : C → C → Type*} {CC : C → Type*} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [Category C] [ConcreteCategory C FC] (X : C) (f g h : X ⟶ X) (_ : g ≫ h = h ≫ g) (x : ToType X), h (g (f x)) = g (h (f x)) := @ex1_apply @[elementwise] lemma foo [Category C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) : f ≫ 𝟙 N ≫ g = h := by simp [w] @[elementwise] lemma foo' [Category C] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) : f ≫ 𝟙 N ≫ g = h := by simp [w] lemma bar [Category C] {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory C FC] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : ToType M) : g (f x) = h x := by apply foo_apply w example {M N K : Type} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by have := elementwise_of% w guard_hyp this : ∀ (x : M), g (f x) = h x exact this x example {M N K : Type} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := (elementwise_of% w) x example [Category C] {FC : C → C → Type _} {CC : C → Type _} [∀ X Y, FunLike (FC X Y) (CC X) (CC Y)] [ConcreteCategory C FC] {M N K : C} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : ToType M) : g (f x) = h x := by have := elementwise_of% w guard_hyp this : ∀ (x : ToType M), g (f x) = h x exact this x section Mon lemma bar' {M N K : MonCat} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by exact foo_apply w x lemma bar'' {M N K : MonCat} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w lemma bar''' {M N K : MonCat} {f : M ⟶ N} {g : N ⟶ K} {h : M ⟶ K} (w : f ≫ g = h) (x : M) : g (f x) = h x := by apply foo_apply w example (M N K : MonCat) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) : g (f m) = h m := by rw [elementwise_of% w] example (M N K : MonCat) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) : g (f m) = h m := by replace w := elementwise_of% w apply w end Mon example {α β : Type} (f g : α ⟶ β) (w : f = g) (a : α) : f a = g a := by replace w := elementwise_of% w guard_hyp w : ∀ (x : α), f x = g x rw [w] example {α β : Type} (f g : α ⟶ β) (w : f ≫ 𝟙 β = g) (a : α) : f a = g a := by replace w := elementwise_of% w guard_hyp w : ∀ (x : α), f x = g x rw [w] end ConcreteCategory end ElementwiseTest
.lake/packages/mathlib/MathlibTest/CategoryTheory/Reassoc.lean
import Mathlib.Tactic.CategoryTheory.IsoReassoc open CategoryTheory namespace Tests.Reassoc universe v₁ v₂ v₃ u₁ u₂ u₃ variable {C : Type u₁} {D : Type u₂} {E : Type u₃} [Category.{v₁} C] [Category.{v₂} D] [Category.{v₃} E] {F : C ⥤ D} {G : D ⥤ E} @[reassoc] lemma foo {x y z : C} (f : x ⟶ y) (g : y ⟶ z) (h : x ⟶ z) (w : f ≫ g = h) : f ≫ g = h := w @[reassoc] lemma foo_iso {x y z : C} (f : x ≅ y) (g : y ≅ z) (h : x ≅ z) (w : f ≪≫ g = h) : f ≪≫ g = h := w /-- info: Tests.Reassoc.foo_assoc.{v₁, u₁} {C : Type u₁} [Category.{v₁, u₁} C] {x y z : C} (f : x ⟶ y) (g : y ⟶ z) (h : x ⟶ z) (w : f ≫ g = h) {Z : C} (h✝ : z ⟶ Z) : f ≫ g ≫ h✝ = h ≫ h✝ -/ #guard_msgs in #check foo_assoc /-- info: Tests.Reassoc.foo_iso_assoc.{v₁, u₁} {C : Type u₁} [Category.{v₁, u₁} C] {x y z : C} (f : x ≅ y) (g : y ≅ z) (h : x ≅ z) (w : f ≪≫ g = h) {Z : C} (h✝ : z ≅ Z) : f ≪≫ g ≪≫ h✝ = h ≪≫ h✝ -/ #guard_msgs in #check foo_iso_assoc /-! Test that `reassoc_of% foo` works even though the category is not yet known. -/ example {x y z w : C} (f : x ⟶ y) (g : y ⟶ z) (h' : z ⟶ w) (h : x ⟶ z) (hfg : f ≫ g = h) : f ≫ g ≫ h' = h ≫ h' := by rw [reassoc_of% foo] exact hfg /-! Test that `reassoc_of% foo_iso` works even though the category is not yet known. -/ example {x y z w : C} (f : x ≅ y) (g : y ≅ z) (h' : z ≅ w) (h : x ≅ z) (hfg : f ≪≫ g = h) : f ≪≫ g ≪≫ h' = h ≪≫ h' := by rw [reassoc_of% foo_iso] exact hfg /-- error: `reassoc` can only be used on terms about equality of (iso)morphisms -/ #guard_msgs in @[reassoc] def one : Nat := 1 /-- error: `reassoc` can only be used on terms about equality of (iso)morphisms -/ #guard_msgs in @[reassoc] def one_plus_one : 1 + 1 = 2 := rfl @[reassoc] lemma foo_functor {x y z : C} (f : x ≅ y) (g : y ≅ z) (h : x ≅ z) (w : F.mapIso (f ≪≫ g) = F.mapIso h) : F.mapIso (f ≪≫ g) = F.mapIso h := w /-- info: Tests.Reassoc.foo_functor_assoc.{v₁, v₂, u₁, u₂} {C : Type u₁} {D : Type u₂} [Category.{v₁, u₁} C] [Category.{v₂, u₂} D] {F : C ⥤ D} {x y z : C} (f : x ≅ y) (g : y ≅ z) (h : x ≅ z) (w : F.mapIso (f ≪≫ g) = F.mapIso h) {Z : D} (h✝ : F.obj z ≅ Z) : F.mapIso f ≪≫ F.mapIso g ≪≫ h✝ = F.mapIso h ≪≫ h✝ -/ #guard_msgs in #check foo_functor_assoc @[reassoc] lemma foo_functor' {x y z : C} (f : x ≅ y) (g : y ≅ z) (h : x ≅ z) (w : F.mapIso (f ≪≫ g) = F.mapIso h) {Z : D} (e : F.obj z ≅ Z) : F.mapIso f ≪≫ F.mapIso g ≪≫ e = F.mapIso h ≪≫ e := (reassoc_of% w) e -- checking that _assoc expressions are indeed right_associated: /-- info: Tests.Reassoc.foo_functor'_assoc.{v₁, v₂, u₁, u₂} {C : Type u₁} {D : Type u₂} [Category.{v₁, u₁} C] [Category.{v₂, u₂} D] {F : C ⥤ D} {x y z : C} (f : x ≅ y) (g : y ≅ z) (h : x ≅ z) (w : F.mapIso (f ≪≫ g) = F.mapIso h) {Z : D} (e : F.obj z ≅ Z) {Z✝ : D} (h✝ : Z ≅ Z✝) : F.mapIso f ≪≫ F.mapIso g ≪≫ e ≪≫ h✝ = F.mapIso h ≪≫ e ≪≫ h✝ -/ #guard_msgs in #check foo_functor'_assoc end Tests.Reassoc
.lake/packages/mathlib/MathlibTest/CategoryTheory/ToApp.lean
import Mathlib.Tactic.CategoryTheory.ToApp import Mathlib.CategoryTheory.Bicategory.Functor.Prelax universe w v u namespace CategoryTheory.ToAppTest open Bicategory Category variable {B : Type u} [Bicategory.{w, v} B] {a b c d e : B} @[to_app] theorem whiskerLeft_hom_inv (f : a ⟶ b) {g h : b ⟶ c} (η : g ≅ h) : f ◁ η.hom ≫ f ◁ η.inv = 𝟙 (f ≫ g) := by rw [← Bicategory.whiskerLeft_comp, Iso.hom_inv_id, Bicategory.whiskerLeft_id] example {a b c : Cat} (f : a ⟶ b) {g h : b ⟶ c} (η : g ≅ h) (X : a) : η.hom.app (f.obj X) ≫ η.inv.app (f.obj X) = 𝟙 ((f ≫ g).obj X) := whiskerLeft_hom_inv_app f η X @[to_app] theorem pentagon_hom_hom_inv_inv_hom (f : a ⟶ b) (g : b ⟶ c) (h : c ⟶ d) (i : d ⟶ e) : (α_ f (g ≫ h) i).hom ≫ f ◁ (α_ g h i).hom ≫ (α_ f g (h ≫ i)).inv = (α_ f g h).inv ▷ i ≫ (α_ (f ≫ g) h i).hom := eq_of_inv_eq_inv (by simp) example {a b c d e : Cat} (f : a ⟶ b) (g : b ⟶ c) (h : c ⟶ d) (i : d ⟶ e) (X : a) : True := by have hyp := pentagon_hom_hom_inv_inv_hom_app f g h i X guard_hyp hyp : 𝟙 (i.obj (h.obj (g.obj (f.obj X)))) = i.map (𝟙 (h.obj (g.obj (f.obj X)))) trivial @[to_app] theorem testThm {C : Type*} [Bicategory C] (F : PrelaxFunctor B C) {a b : B} {f g : a ⟶ b} (η : f ⟶ g) : F.map₂ η ≫ F.map₂ (𝟙 g) = F.map₂ η := by simp example {B : Type u_1} [Bicategory B] (F : PrelaxFunctor B Cat) {a b : B} {f g : a ⟶ b} (η : f ⟶ g) (X : ↑(F.obj a)) : (F.map₂ η).app X ≫ (F.map₂ (𝟙 g)).app X = (F.map₂ η).app X := testThm_app F η X end CategoryTheory.ToAppTest
.lake/packages/mathlib/MathlibTest/CategoryTheory/PrettyPrinting.lean
import Mathlib.CategoryTheory.Functor.Basic /-! # Tests that terms used in category theory pretty-print as expected -/ section open Opposite /-- info: Opposite.op_unop.{u} {α : Sort u} (x : αᵒᵖ) : op (unop x) = x -/ #guard_msgs in #check Opposite.op_unop end section open CategoryTheory /-- info: CategoryTheory.Functor.map_id.{v₁, v₂, u₁, u₂} {C : Type u₁} [Category.{v₁, u₁} C] {D : Type u₂} [Category.{v₂, u₂} D] (self : C ⥤ D) (X : C) : self.map (𝟙 X) = 𝟙 (self.obj X) -/ #guard_msgs in #check CategoryTheory.Functor.map_id end
.lake/packages/mathlib/MathlibTest/CategoryTheory/MarkovCategory.lean
import Mathlib.CategoryTheory.MarkovCategory.Basic import Mathlib.CategoryTheory.CopyDiscardCategory.Cartesian import Mathlib.CategoryTheory.Monoidal.Types.Basic /-! # Tests for Markov Categories Tests for the Markov category and copy-discard category implementation. ## Tests included * Instance inference for cartesian and copy-discard structures * Comonoid laws: comultiplication and counit axioms * Markov category laws: naturality of discard and terminal unit * Simp automation for comonoid and discard naturality -/ universe u open CategoryTheory MonoidalCategory CopyDiscardCategory ComonObj IsCommComonObj section BasicTests /-- CartesianMonoidalCategory instance exists for Type* -/ example : CartesianMonoidalCategory (Type u) := inferInstance /-- CopyDiscardCategory instance for Type* via cartesian structure -/ example : CopyDiscardCategory (Type u) := CartesianCopyDiscard.ofCartesianMonoidalCategory end BasicTests section ComonoidLaws variable {C : Type u} [Category.{u} C] [MonoidalCategory.{u} C] [CopyDiscardCategory C] /-- Comultiplication is commutative -/ example (X : C) : Δ[X] ≫ (β_ X X).hom = Δ[X] := comul_comm X /-- Left counit law: copy then discard left component recovers object -/ example (X : C) : Δ[X] ≫ (ε[X] ▷ X) = (λ_ X).inv := ComonObj.counit_comul X /-- Right counit law: copy then discard right component recovers object -/ example (X : C) : Δ[X] ≫ (X ◁ ε[X]) = (ρ_ X).inv := ComonObj.comul_counit X /-- Comultiplication is coassociative -/ example (X : C) : Δ[X] ≫ (X ◁ Δ[X]) = Δ[X] ≫ (Δ[X] ▷ X) ≫ (α_ X X X).hom := ComonObj.comul_assoc X end ComonoidLaws section MarkovLaws variable {C : Type u} [Category.{u} C] [MonoidalCategory.{u} C] [MarkovCategory C] /-- Discard is natural -/ example {X Y : C} (f : X ⟶ Y) : f ≫ ε[Y] = ε[X] := MarkovCategory.discard_natural f /-- Any morphism to the unit equals discard -/ example (X : C) (f : X ⟶ 𝟙_ C) : f = ε[X] := MarkovCategory.eq_discard X f /-- The monoidal unit is terminal -/ example : Limits.IsTerminal (𝟙_ C : C) := MarkovCategory.isTerminalUnit /-- Any two morphisms to the unit are equal (subsingleton instance) -/ example (X : C) (f g : X ⟶ 𝟙_ C) : f = g := Subsingleton.elim f g end MarkovLaws section SimpLemmas variable {C : Type u} [Category.{u} C] [MonoidalCategory C] [CopyDiscardCategory C] /-- Simp automation for counit laws -/ example (X : C) : Δ[X] ≫ (ε[X] ▷ X) = (λ_ X).inv := by simp /-- Simp automation for comultiplication commutativity -/ example (X : C) : Δ[X] ≫ (β_ X X).hom = Δ[X] := by simp end SimpLemmas section MarkovSimpLemmas variable {C : Type u} [Category.{u} C] [MonoidalCategory.{u} C] [MarkovCategory C] /-- Simp automation for naturality of discard -/ example {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ g ≫ ε[Z] = f ≫ ε[Y] := by simp end MarkovSimpLemmas
.lake/packages/mathlib/MathlibTest/CategoryTheory/SubstHomLift.lean
import Mathlib.CategoryTheory.FiberedCategory.HomLift universe u₁ v₁ u₂ v₂ open CategoryTheory Category variable {𝒮 : Type u₁} {𝒳 : Type u₂} [Category.{v₁} 𝒳] [Category.{v₂} 𝒮] (p : 𝒳 ⥤ 𝒮) /-- Testing simple substitution -/ example {R S : 𝒮} {a b : 𝒳} (f : R ⟶ S) (φ : a ⟶ b) [p.IsHomLift f φ] : f = f := by subst_hom_lift p f φ rename_i h guard_hyp h : p.IsHomLift (p.map φ) φ guard_target = p.map φ = p.map φ trivial /-- Test substitution with more complicated expression -/ example {R S T : 𝒮} {a b c : 𝒳} (f : R ⟶ S) (g : S ⟶ T) (φ : a ⟶ b) (ψ : b ⟶ c) [p.IsHomLift f (φ ≫ ψ)] : f = f := by subst_hom_lift p f (φ ≫ ψ) rename_i h guard_hyp h : p.IsHomLift (p.map (φ ≫ ψ)) (φ ≫ ψ) guard_target = p.map (φ ≫ ψ) = p.map (φ ≫ ψ) trivial
.lake/packages/mathlib/MathlibTest/CategoryTheory/CheckCompositions.lean
import Mathlib.CategoryTheory.Limits.Preserves.Limits import Mathlib.Tactic.Recall universe v₁ v₂ v₃ v u₁ u₂ u₃ u open CategoryTheory Limits variable {J : Type u} [Category.{v} J] {C : Type u₁} [Category.{v₁} C] {D : Type u₂} [Category.{v₂} D] {E : Type u₃} [Category.{v₃} E] (F : J ⥤ C) (G : C ⥤ D) (H : D ⥤ E) variable [HasColimitsOfShape J C] [HasColimitsOfShape J E] [PreservesColimit F (G ⋙ H)] /-- info: In composition colimit.ι ((F ⋙ G) ⋙ H) j ≫ (preservesColimitIso (G ⋙ H) F).inv the source of (preservesColimitIso (G ⋙ H) F).inv is colimit (F ⋙ G ⋙ H) but should be colimit ((F ⋙ G) ⋙ H) -/ #guard_msgs in set_option linter.unusedTactic false in example (j : J) : colimit.ι ((F ⋙ G) ⋙ H) j ≫ (preservesColimitIso (G ⋙ H) F).inv = H.map (G.map (colimit.ι F j)) := by -- We know which lemma we want to use, and it's even a simp lemma, but `rw` won't let us apply it fail_if_success rw [ι_preservesColimitIso_inv] fail_if_success rw [ι_preservesColimitIso_inv (G ⋙ H)] fail_if_success simp only [ι_preservesColimitIso_inv] -- This would work: -- erw [ι_preservesColimitIso_inv (G ⋙ H)] -- `check_compositions` checks if the two morphisms we're composing are composed by abusing defeq, -- and indeed it tells us that we are abusing definitional associativity of composition of -- functors here! check_compositions -- In this case, we can "fix" this by reassociating in the goal, but usually at this point the -- right thing to do is to back off and check how we ended up with a bad goal in the first place. dsimp only [Functor.assoc] -- This would work now, but it is not needed, because simp works as well -- rw [ι_preservesColimitIso_inv] simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/MonoidalComp.lean
import Mathlib.Tactic.CategoryTheory.MonoidalComp universe v u noncomputable section namespace CategoryTheory open scoped MonoidalCategory variable {C : Type u} [Category.{v} C] [MonoidalCategory C] example (X : C) : X ≅ (X ⊗ (𝟙_ C ⊗ 𝟙_ C)) := monoidalIso _ _ example (X1 X2 X3 X4 X5 X6 X7 X8 X9 : C) : (𝟙_ C ⊗ (X1 ⊗ X2 ⊗ ((X3 ⊗ X4) ⊗ X5)) ⊗ X6 ⊗ (X7 ⊗ X8 ⊗ X9)) ≅ (X1 ⊗ (X2 ⊗ X3) ⊗ X4 ⊗ (X5 ⊗ (𝟙_ C ⊗ X6) ⊗ X7) ⊗ X8 ⊗ X9) := monoidalIso _ _ example {U V W X Y : C} (f : U ⟶ V ⊗ (W ⊗ X)) (g : (V ⊗ W) ⊗ X ⟶ Y) : U ⟶ Y := f ⊗≫ g -- To automatically insert unitors/associators at the beginning or end, -- you can use `f ⊗≫ 𝟙 _` example {W X Y Z : C} (f : W ⟶ (X ⊗ Y) ⊗ Z) : W ⟶ X ⊗ (Y ⊗ Z) := f ⊗≫ 𝟙 _ example {U V W X Y : C} (f : U ⟶ V ⊗ (W ⊗ X)) (g : (V ⊗ W) ⊗ X ⟶ Y) : f ⊗≫ g = f ≫ (α_ _ _ _).inv ≫ g := by simp [monoidalComp] end CategoryTheory
.lake/packages/mathlib/MathlibTest/CategoryTheory/TransfiniteComposition.lean
import Mathlib.CategoryTheory.Types.Monomorphisms import Mathlib.CategoryTheory.MorphismProperty.TransfiniteComposition universe v u namespace CategoryTheory.Types open MorphismProperty example : MorphismProperty.IsStableUnderTransfiniteComposition.{v} (monomorphisms (Type u)) := inferInstance end CategoryTheory.Types
.lake/packages/mathlib/MathlibTest/CategoryTheory/Slice.lean
import Mathlib.Tactic.CategoryTheory.Slice open CategoryTheory variable (C : Type) [Category C] (X Y Z W U : C) variable (f₁ f₂ : X ⟶ Y) (g g₁ g₂ : Y ⟶ Z) (h : Z ⟶ W) (l : W ⟶ U) set_option linter.unusedTactic false in example (hyp : f₁ ≫ g₁ = f₂ ≫ g₂) : f₁ ≫ g₁ ≫ h ≫ l = (f₂ ≫ g₂) ≫ (h ≫ l) := by conv => rhs slice 2 3 show f₁ ≫ g₁ ≫ h ≫ l = f₂ ≫ (g₂ ≫ h) ≫ l conv => lhs slice 1 2 rw [hyp] show ((f₂ ≫ g₂) ≫ h) ≫ l = f₂ ≫ (g₂ ≫ h) ≫ l conv => lhs slice 2 3 example (hyp : f₁ ≫ g₁ = f₂ ≫ g₂) : f₁ ≫ g₁ ≫ h ≫ l = (f₂ ≫ g₂) ≫ (h ≫ l) := by slice_lhs 1 2 => { rw [hyp] }; slice_rhs 1 2 => skip example (h₁ : f₁ = f₂) : f₁ ≫ g ≫ h ≫ l = ((f₂ ≫ g) ≫ h) ≫ l := by slice_lhs 1 1 => rw [h₁] example (h₁ : f₁ = f₂) : ((f₂ ≫ g) ≫ h) ≫ l = f₁ ≫ g ≫ h ≫ l := by slice_rhs 1 1 => rw [h₁]
.lake/packages/mathlib/MathlibTest/CategoryTheory/Mon_.lean
import Mathlib.CategoryTheory.Monoidal.Internal.Limits import Mathlib.CategoryTheory.Limits.Preserves.Shapes.Terminal import Mathlib.CategoryTheory.Limits.Preserves.Shapes.BinaryProducts open CategoryTheory Limits noncomputable section -- We verify that we have successfully created special shapes of limits in `Mon C`, -- assuming that only those special shapes existed in `C`. -- To avoid unnecessarily adding imports in `Mathlib/CategoryTheory/Monoidal/Internal/Limits.lean` -- this check lives in the test suite. example (D : Type u) [Category.{v} D] [MonoidalCategory D] [HasTerminal D] : (⊤_ (Mon D)).X ≅ (⊤_ D) := PreservesTerminal.iso (Mon.forget D) example (D : Type u) [Category.{v} D] [MonoidalCategory D] [HasBinaryProducts D] (A B : Mon D) : (prod A B).X ≅ prod A.X B.X := PreservesLimitPair.iso (Mon.forget D) A B
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/Alg.lean
import Mathlib universe v u open CategoryTheory AlgCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 variable (R : Type u) [CommRing R] /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example (X : Type u) [Ring X] [Algebra R X] : ⇑(𝟙 (of R X)) = id := by simp example {X Y : Type v} [Ring X] [Algebra R X] [Ring Y] [Algebra R Y] (f : X →ₐ[R] Y) : ⇑(ofHom f) = ⇑f := by simp example {X Y : Type v} [Ring X] [Algebra R X] [Ring Y] [Algebra R Y] (f : X →ₐ[R] Y) (x : X) : (ofHom f) x = f x := by simp example {X Y Z : AlgCat R} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type v} [Ring X] [Algebra R X] [Ring Y] [Algebra R Y] [Ring Z] [Algebra R Z] (f : X →ₐ[R] Y) (g : Y →ₐ[R] Z) : ⇑(ofHom f ≫ ofHom g) = g ∘ f := by simp example {X Y : Type v} [Ring X] [Algebra R X] [Ring Y] [Algebra R Y] {Z : AlgCat R} (f : X →ₐ[R] Y) (g : of R Y ⟶ Z) : ⇑(ofHom f ≫ g) = g ∘ f := by simp example {X Y : AlgCat R} {Z : Type v} [Ring Z] [Algebra R Z] (f : X ⟶ Y) (g : Y ⟶ of R Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : AlgCat R} {X : Type v} [Ring X] [Algebra R X] (f : of R X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : AlgCat R} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : AlgCat R} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : AlgCat R} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : AlgCat R) : ⇑(𝟙 X) = id := by simp example {M N : AlgCat.{v} R} (f : M ⟶ N) (x y : M) : f (x + y) = f x + f y := by simp example {M N : AlgCat.{v} R} (f : M ⟶ N) : f 0 = 0 := by simp example {M N : AlgCat.{v} R} (f : M ⟶ N) (r : R) (m : M) : f (r • m) = r • f m := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/Ring.lean
import Mathlib.Algebra.Category.Ring.Basic universe v u open CategoryTheory SemiRingCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example (X : Type u) [Semiring X] : ⇑(𝟙 (of X)) = id := by simp example {X Y : Type u} [Semiring X] [Semiring Y] (f : X →+* Y) : ⇑(ofHom f) = ⇑f := by simp example {X Y : Type u} [Semiring X] [Semiring Y] (f : X →+* Y) (x : X) : (ofHom f) x = f x := by simp example {X Y Z : SemiRingCat} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type u} [Semiring X] [Semiring Y] [Semiring Z] (f : X →+* Y) (g : Y →+* Z) : ⇑(ofHom f ≫ ofHom g) = g ∘ f := by simp example {X Y : Type u} [Semiring X] [Semiring Y] {Z : SemiRingCat} (f : X →+* Y) (g : of Y ⟶ Z) : ⇑(ofHom f ≫ g) = g ∘ f := by simp example {X Y : SemiRingCat} {Z : Type u} [Semiring Z] (f : X ⟶ Y) (g : Y ⟶ of Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : SemiRingCat} {X : Type u} [Semiring X] (f : of X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : SemiRingCat} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : SemiRingCat} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : SemiRingCat} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : SemiRingCat) : ⇑(𝟙 X) = id := by simp example {X : Type*} [Semiring X] : ⇑(RingHom.id X) = id := by simp example {M N : SemiRingCat} (f : M ⟶ N) (x y : M) : f (x + y) = f x + f y := by simp example {M N : SemiRingCat} (f : M ⟶ N) (x y : M) : f (x * y) = f x * f y := by simp example {M N : SemiRingCat} (f : M ⟶ N) : f 0 = 0 := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/Semigrp.lean
import Mathlib.Algebra.Category.Semigrp.Basic universe v u open CategoryTheory Semigrp set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 /- We test if all the coercions and `map_mul` lemmas trigger correctly. -/ example (X : Type u) [Semigroup X] : ⇑(𝟙 (of X)) = id := by simp example {X Y : Type u} [Semigroup X] [Semigroup Y] (f : X →ₙ* Y) : ⇑(ofHom f) = ⇑f := by simp example {X Y : Type u} [Semigroup X] [Semigroup Y] (f : X →ₙ* Y) (x : X) : (ofHom f) x = f x := by simp example {X Y Z : Semigrp} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type u} [Semigroup X] [Semigroup Y] [Semigroup Z] (f : X →ₙ* Y) (g : Y →ₙ* Z) : ⇑(ofHom f ≫ ofHom g) = g ∘ f := by simp example {X Y : Type u} [Semigroup X] [Semigroup Y] {Z : Semigrp} (f : X →ₙ* Y) (g : of Y ⟶ Z) : ⇑(ofHom f ≫ g) = g ∘ f := by simp example {X Y : Semigrp} {Z : Type u} [Semigroup Z] (f : X ⟶ Y) (g : Y ⟶ of Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : Semigrp} {X : Type u} [Semigroup X] (f : of X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : Semigrp} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : Semigrp} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : Semigrp} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : Semigrp) : ⇑(𝟙 X) = id := by simp example {X : Type*} [Semigroup X] : ⇑(MulHom.id X) = id := by simp example {M N : Semigrp} (f : M ⟶ N) (x y : M) : f (x * y) = f x * f y := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/ProfiniteGrp.lean
import Mathlib universe v u open CategoryTheory ProfiniteGrp set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 variable {X Y Z : Type u} [Group X] [TopologicalSpace X] [IsTopologicalGroup X] [CompactSpace X] [TotallyDisconnectedSpace X] [Group Y] [TopologicalSpace Y] [IsTopologicalGroup Y] [CompactSpace Y] [TotallyDisconnectedSpace Y] [Group Z] [TopologicalSpace Z] [IsTopologicalGroup Z] [CompactSpace Z] [TotallyDisconnectedSpace Z] /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example : ⇑(𝟙 (of X)) = id := by simp example (f : ContinuousMonoidHom X Y) : ⇑(ofHom f) = ⇑f := by simp example (f : ContinuousMonoidHom X Y) (x : X) : (ofHom f) x = f x := by simp example {U V W : ProfiniteGrp} (f : U ⟶ V) (g : V ⟶ W) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example (f : ContinuousMonoidHom X Y) (g : ContinuousMonoidHom Y Z) : ⇑(ofHom f ≫ ofHom g) = g ∘ f := by simp example {W : ProfiniteGrp} (f : ContinuousMonoidHom X Y) (g : of Y ⟶ W) : ⇑(ofHom f ≫ g) = g ∘ f := by simp example {U V : ProfiniteGrp} (f : U ⟶ V) (g : V ⟶ of Z) : ⇑(f ≫ g) = g ∘ f := by simp example {V W : ProfiniteGrp} (f : of X ⟶ V) (g : V ⟶ W) : ⇑(f ≫ g) = g ∘ f := by simp example {U V W : ProfiniteGrp} (f : U ⟶ V) (g : V ⟶ W) (u : U) : (f ≫ g) u = g (f u) := by simp example {U V : ProfiniteGrp} (e : U ≅ V) (u : U) : e.inv (e.hom u) = u := by simp example {U V : ProfiniteGrp} (e : U ≅ V) (v : V) : e.hom (e.inv v) = v := by simp example (U : ProfiniteGrp) : ⇑(𝟙 U) = id := by simp example {M N : ProfiniteGrp.{u}} (f : M ⟶ N) (x y : M) : f (x * y) = f x * f y := by simp example {M N : ProfiniteGrp.{u}} (f : M ⟶ N) : f 1 = 1 := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/MonCat.lean
import Mathlib.Algebra.Category.MonCat.Basic universe v u open CategoryTheory MonCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 /- We test if all the coercions and `map_mul` lemmas trigger correctly. -/ example (X : Type u) [Monoid X] : ⇑(𝟙 (of X)) = id := by simp example {X Y : Type u} [Monoid X] [Monoid Y] (f : X →* Y) : ⇑(ofHom f) = ⇑f := by simp example {X Y : Type u} [Monoid X] [Monoid Y] (f : X →* Y) (x : X) : (ofHom f) x = f x := by simp example {X Y Z : MonCat} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type u} [Monoid X] [Monoid Y] [Monoid Z] (f : X →* Y) (g : Y →* Z) : ⇑(ofHom f ≫ ofHom g) = g ∘ f := by simp example {X Y : Type u} [Monoid X] [Monoid Y] {Z : MonCat} (f : X →* Y) (g : of Y ⟶ Z) : ⇑(ofHom f ≫ g) = g ∘ f := by simp example {X Y : MonCat} {Z : Type u} [Monoid Z] (f : X ⟶ Y) (g : Y ⟶ of Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : MonCat} {X : Type u} [Monoid X] (f : of X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : MonCat} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : MonCat} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : MonCat} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : MonCat) : ⇑(𝟙 X) = id := by simp example {X : Type*} [Monoid X] : ⇑(MonoidHom.id X) = id := by simp example {M N : MonCat} (f : M ⟶ N) (x y : M) : f (x * y) = f x * f y := by simp example {M N : MonCat} (f : M ⟶ N) : f 1 = 1 := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/TopModuleCat.lean
import Mathlib.Algebra.Category.ModuleCat.Topology.Basic universe v u open CategoryTheory TopModuleCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 variable (R : Type u) [CommRing R] [TopologicalSpace R] [IsTopologicalRing R] /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example (X : Type v) [AddCommGroup X] [TopologicalSpace X] [ContinuousAdd X] [Module R X] [ContinuousSMul R X] : ⇑(𝟙 (of R X)) = id := by simp example {X Y : Type v} [AddCommGroup X] [Module R X] [TopologicalSpace X] [ContinuousAdd X] [ContinuousSMul R X] [AddCommGroup Y] [Module R Y] [TopologicalSpace Y] [ContinuousAdd Y] [ContinuousSMul R Y] (f : X →L[R] Y) : ⇑(TopModuleCat.ofHom f) = ⇑f := by simp example {X Y : Type v} [AddCommGroup X] [Module R X] [TopologicalSpace X] [ContinuousAdd X] [ContinuousSMul R X] [AddCommGroup Y] [Module R Y] [TopologicalSpace Y] [ContinuousAdd Y] [ContinuousSMul R Y] (f : X →L[R] Y) (x : X) : (TopModuleCat.ofHom f) x = f x := by simp example {X Y Z : TopModuleCat R} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type v} [AddCommGroup X] [Module R X] [TopologicalSpace X] [ContinuousAdd X] [ContinuousSMul R X] [AddCommGroup Y] [Module R Y] [TopologicalSpace Y] [ContinuousAdd Y] [ContinuousSMul R Y] [AddCommGroup Z] [Module R Z] [TopologicalSpace Z] [ContinuousAdd Z] [ContinuousSMul R Z] (f : X →L[R] Y) (g : Y →L[R] Z) : ⇑(TopModuleCat.ofHom f ≫ TopModuleCat.ofHom g) = g ∘ f := by simp example {X Y : Type v} [AddCommGroup X] [Module R X] [TopologicalSpace X] [ContinuousAdd X] [ContinuousSMul R X] [AddCommGroup Y] [Module R Y] [TopologicalSpace Y] [ContinuousAdd Y] [ContinuousSMul R Y] {Z : TopModuleCat R} (f : X →L[R] Y) (g : of R Y ⟶ Z) : ⇑(TopModuleCat.ofHom f ≫ g) = g ∘ f := by simp example {Y Z : TopModuleCat R} {X : Type v} [AddCommGroup X] [Module R X] [TopologicalSpace X] [ContinuousAdd X] [ContinuousSMul R X] (f : of R X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X : Type v} [AddCommGroup X] [Module R X] [TopologicalSpace X] [ContinuousAdd X] [ContinuousSMul R X] {Y Z : TopModuleCat R} (f : X →L[R] Y) (g : of R Y ⟶ Z) : ⇑(TopModuleCat.ofHom f ≫ g) = g ∘ f := by simp example {X Y Z : TopModuleCat R} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : TopModuleCat R} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : TopModuleCat R} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : TopModuleCat R) : ⇑(𝟙 X) = id := by simp example {M N : TopModuleCat.{v} R} (f : M ⟶ N) (x y : M) : f (x + y) = f x + f y := by simp example {M N : TopModuleCat.{v} R} (f : M ⟶ N) : f 0 = 0 := by simp example {M N : TopModuleCat.{v} R} (f : M ⟶ N) (r : R) (m : M) : f (r • m) = r • f m := by simp example {M N : TopModuleCat.{v} R} (f : M ⟶ N) : Continuous f := by continuity example {M N : TopModuleCat.{v} R} (f : M ⟶ N) : Continuous f := by fun_prop
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/CommAlgCat.lean
import Mathlib.Algebra.Category.CommAlgCat.Basic universe v u open CategoryTheory CommAlgCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 variable (R : Type u) [CommRing R] /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example (X : Type v) [CommRing X] [Algebra R X] : ⇑(𝟙 (of R X)) = id := by simp example {X Y : Type v} [CommRing X] [Algebra R X] [CommRing Y] [Algebra R Y] (f : X →ₐ[R] Y) : ⇑(CommAlgCat.ofHom f) = ⇑f := by simp example {X Y : Type v} [CommRing X] [Algebra R X] [CommRing Y] [Algebra R Y] (f : X →ₐ[R] Y) (x : X) : (CommAlgCat.ofHom f) x = f x := by simp example {X Y Z : CommAlgCat R} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type v} [CommRing X] [Algebra R X] [CommRing Y] [Algebra R Y] [CommRing Z] [Algebra R Z] (f : X →ₐ[R] Y) (g : Y →ₐ[R] Z) : ⇑(CommAlgCat.ofHom f ≫ CommAlgCat.ofHom g) = g ∘ f := by simp example {X Y : Type v} [CommRing X] [Algebra R X] [CommRing Y] [Algebra R Y] {Z : CommAlgCat R} (f : X →ₐ[R] Y) (g : of R Y ⟶ Z) : ⇑(CommAlgCat.ofHom f ≫ g) = g ∘ f := by simp example {X Y : CommAlgCat R} {Z : Type v} [CommRing Z] [Algebra R Z] (f : X ⟶ Y) (g : Y ⟶ of R Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : CommAlgCat R} {X : Type v} [CommRing X] [Algebra R X] (f : of R X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : CommAlgCat R} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : CommAlgCat R} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : CommAlgCat R} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : CommAlgCat R) : ⇑(𝟙 X) = id := by simp example {M N : CommAlgCat.{v} R} (f : M ⟶ N) (x y : M) : f (x + y) = f x + f y := by simp example {M N : CommAlgCat.{v} R} (f : M ⟶ N) : f 0 = 0 := by simp example {M N : CommAlgCat.{v} R} (f : M ⟶ N) (r : R) (m : M) : f (r • m) = r • f m := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/ModuleCat.lean
import Mathlib.Algebra.Category.ModuleCat.Basic universe v u open CategoryTheory ModuleCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 variable (R : Type u) [CommRing R] /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example (X : Type v) [AddCommGroup X] [Module R X] : ⇑(𝟙 (of R X)) = id := by simp example {X Y : Type v} [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] (f : X →ₗ[R] Y) : ⇑(ModuleCat.ofHom f) = ⇑f := by simp example {X Y : Type v} [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] (f : X →ₗ[R] Y) (x : X) : (ModuleCat.ofHom f) x = f x := by simp example {X Y Z : ModuleCat R} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type v} [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] [Ring Z] [Algebra R Z] (f : X →ₗ[R] Y) (g : Y →ₗ[R] Z) : ⇑(ModuleCat.ofHom f ≫ ModuleCat.ofHom g) = g ∘ f := by simp example {X Y : Type v} [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] {Z : ModuleCat R} (f : X →ₗ[R] Y) (g : of R Y ⟶ Z) : ⇑(ModuleCat.ofHom f ≫ g) = g ∘ f := by simp example {X Y : ModuleCat R} {Z : Type v} [Ring Z] [Algebra R Z] (f : X ⟶ Y) (g : Y ⟶ of R Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : ModuleCat R} {X : Type v} [AddCommGroup X] [Module R X] (f : of R X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : ModuleCat R} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : ModuleCat R} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : ModuleCat R} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : ModuleCat R) : ⇑(𝟙 X) = id := by simp example {M N : ModuleCat.{v} R} (f : M ⟶ N) (x y : M) : f (x + y) = f x + f y := by simp example {M N : ModuleCat.{v} R} (f : M ⟶ N) : f 0 = 0 := by simp example {M N : ModuleCat.{v} R} (f : M ⟶ N) (r : R) (m : M) : f (r • m) = r • f m := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/ConcreteCategory/Grp.lean
import Mathlib.Algebra.Category.Grp.Basic universe v u open CategoryTheory GrpCat set_option maxHeartbeats 10000 set_option synthInstance.maxHeartbeats 2000 /- We test if all the coercions and `map_add` lemmas trigger correctly. -/ example (X : Type u) [Group X] : ⇑(𝟙 (of X)) = id := by simp example {X Y : Type u} [Group X] [Group Y] (f : X →* Y) : ⇑(ofHom f) = ⇑f := by simp example {X Y : Type u} [Group X] [Group Y] (f : X →* Y) (x : X) : (ofHom f) x = f x := by simp example {X Y Z : GrpCat} (f : X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = ⇑g ∘ ⇑f := by simp example {X Y Z : Type u} [Group X] [Group Y] [Group Z] (f : X →* Y) (g : Y →* Z) : ⇑(ofHom f ≫ ofHom g) = g ∘ f := by simp example {X Y : Type u} [Group X] [Group Y] {Z : GrpCat} (f : X →* Y) (g : of Y ⟶ Z) : ⇑(ofHom f ≫ g) = g ∘ f := by simp example {X Y : GrpCat} {Z : Type u} [Group Z] (f : X ⟶ Y) (g : Y ⟶ of Z) : ⇑(f ≫ g) = g ∘ f := by simp example {Y Z : GrpCat} {X : Type u} [Group X] (f : of X ⟶ Y) (g : Y ⟶ Z) : ⇑(f ≫ g) = g ∘ f := by simp example {X Y Z : GrpCat} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := by simp example {X Y : GrpCat} (e : X ≅ Y) (x : X) : e.inv (e.hom x) = x := by simp example {X Y : GrpCat} (e : X ≅ Y) (y : Y) : e.hom (e.inv y) = y := by simp example (X : GrpCat) : ⇑(𝟙 X) = id := by simp example {X : Type*} [Group X] : ⇑(MonoidHom.id X) = id := by simp example {M N : GrpCat} (f : M ⟶ N) (x y : M) : f (x * y) = f x * f y := by simp example {M N : GrpCat} (f : M ⟶ N) : f 1 = 1 := by simp
.lake/packages/mathlib/MathlibTest/CategoryTheory/Sites/PreservesSheafification.lean
import Mathlib.Algebra.Category.ModuleCat.Colimits import Mathlib.Algebra.Category.ModuleCat.Limits import Mathlib.Algebra.Category.ModuleCat.FilteredColimits import Mathlib.CategoryTheory.Sites.Equivalence universe u open CategoryTheory GrothendieckTopology attribute [local instance] Types.instFunLike Types.instConcreteCategory section Small variable {C : Type u} [SmallCategory C] (J : GrothendieckTopology C) (R : Type u) [Ring R] example : PreservesSheafification J (forget (ModuleCat.{u} R)) := inferInstance end Small section Large variable {C : Type (u + 1)} [LargeCategory C] (J : GrothendieckTopology C) example (R : Type (u + 1)) [Ring R] : PreservesSheafification J (forget (ModuleCat.{u+1} R)) := inferInstance variable [EssentiallySmall.{u} C] example (R : Type u) [Ring R] : PreservesSheafification J (forget (ModuleCat.{u} R)) := inferInstance end Large
.lake/packages/mathlib/MathlibTest/CategoryTheory/Sites/Whiskering.lean
import Mathlib.Algebra.Category.ModuleCat.Colimits import Mathlib.Algebra.Category.ModuleCat.Limits import Mathlib.Algebra.Category.ModuleCat.FilteredColimits import Mathlib.CategoryTheory.Sites.Equivalence universe u open CategoryTheory GrothendieckTopology section Small variable {C : Type u} [SmallCategory C] (J : GrothendieckTopology C) (R : Type u) [Ring R] example : HasSheafCompose J (forget (ModuleCat.{u} R)) := inferInstance end Small section Large variable {C : Type (u + 1)} [LargeCategory C] (J : GrothendieckTopology C) example (R : Type (u + 1)) [Ring R] : HasSheafCompose J (forget (ModuleCat.{u+1} R)) := inferInstance variable [EssentiallySmall.{u} C] example (R : Type u) [Ring R] : HasSheafCompose J (forget (ModuleCat.{u} R)) := inferInstance end Large
.lake/packages/mathlib/MathlibTest/CategoryTheory/Sites/ConcreteSheafification.lean
import Mathlib.Algebra.Category.ModuleCat.Colimits import Mathlib.Algebra.Category.ModuleCat.Limits import Mathlib.Algebra.Category.ModuleCat.FilteredColimits import Mathlib.CategoryTheory.Sites.Equivalence universe u open CategoryTheory attribute [local instance] Types.instFunLike Types.instConcreteCategory section Small variable {C : Type u} [SmallCategory C] (J : GrothendieckTopology C) example : HasSheafify J (Type u) := inferInstance example (R : Type u) [Ring R] : HasSheafify J (ModuleCat.{u} R) := inferInstance end Small section Large variable {C : Type (u + 1)} [LargeCategory C] (J : GrothendieckTopology C) example : HasSheafify J (Type (u+1)) := inferInstance example (R : Type (u + 1)) [Ring R] : HasSheafify J (ModuleCat.{u+1} R) := inferInstance variable [EssentiallySmall.{u} C] example : HasSheafify J (Type u) := hasSheafifyEssentiallySmallSite _ _ example (R : Type u) [Ring R] : HasSheafify J (ModuleCat.{u} R) := hasSheafifyEssentiallySmallSite _ _ end Large
.lake/packages/mathlib/MathlibTest/CategoryTheory/Monoidal/MonTauto.lean
import Mathlib.CategoryTheory.Monoidal.Mon_ open CategoryTheory MonoidalCategory open scoped MonObj variable {C : Type*} [Category C] [MonoidalCategory C] {M N : C} [MonObj M] [MonObj N] example : η ▷ M ≫ μ = (λ_ M).hom := by simp only [mon_tauto] example : M ◁ η ≫ μ = (ρ_ M).hom := by simp only [mon_tauto] example : μ ▷ M ≫ μ = (α_ M M M).hom ≫ M ◁ μ ≫ μ := by simp only [mon_tauto] example : M ◁ μ ≫ μ = (α_ M M M).inv ≫ μ ▷ M ≫ μ := by simp only [mon_tauto] example : M ◁ M ◁ ((ρ_ (M ⊗ M)).inv ≫ (M ⊗ M) ◁ η) ≫ (α_ _ _ _).inv ≫ (M ⊗ M) ◁ (μ ▷ M) ≫ μ ▷ (M ⊗ M) ≫ M ◁ μ ≫ μ = (α_ _ _ _).inv ≫ (μ ⊗ₘ μ) ≫ μ := by simp only [mon_tauto] example : (α_ (𝟙_ C) M M).hom ▷ M ≫ (α_ (𝟙_ C) (M ⊗ M) M).hom ≫ (𝟙_ C) ◁ (α_ M M M).hom ≫ (λ_ _).hom ≫ M ◁ μ ≫ μ = (α_ ((𝟙_ C) ⊗ M) M M).hom ≫ (α_ (𝟙_ C) M (M ⊗ M)).hom ≫ (λ_ _).hom ≫ M ◁ μ ≫ μ := by simp only [mon_tauto] example : (α_ M (𝟙_ _) (M ⊗ M)).hom ≫ M ◁ (λ_ (M ⊗ M)).hom ≫ M ◁ μ ≫ μ = (ρ_ M).hom ▷ (M ⊗ M) ≫ M ◁ μ ≫ μ := by simp only [mon_tauto] example : M ◁ (λ_ (M ⊗ M)).inv ≫ (α_ M (𝟙_ _) (M ⊗ M)).inv ≫ (ρ_ M).hom ▷ (M ⊗ M) ≫ _ ◁ μ ≫ μ = _ ◁ μ ≫ μ := by simp only [mon_tauto] variable [BraidedCategory C] [IsCommMonObj M] [IsCommMonObj N] example : (β_ M M).hom ≫ μ = μ := by simp only [mon_tauto] example : (β_ M M).inv ≫ μ = μ := by simp only [mon_tauto] example : tensorμ M M M M ≫ (μ ⊗ₘ μ) ≫ μ = (μ ⊗ₘ μ) ≫ μ := by simp only [mon_tauto] example : tensorδ M M M M ≫ (μ ⊗ₘ μ) ≫ μ = (μ ⊗ₘ μ) ≫ μ := by simp only [mon_tauto]