source stringlengths 17 118 | lean4 stringlengths 0 335k |
|---|---|
.lake/packages/mathlib/MathlibTest/AssertImported.lean | import Mathlib
/-
This file checks that every declaration and import that have been flagged with
`assert_not_exists` or `assert_not_imported` is present in the environment once
all of `Mathlib` is available.
-/
#check_assertions! |
.lake/packages/mathlib/MathlibTest/apply_congr.lean | import Mathlib.Tactic.ApplyCongr
import Mathlib.Algebra.BigOperators.Group.Finset.Powerset
example (f g : ℤ → ℤ) (S : Finset ℤ) (h : ∀ m ∈ S, f m = g m) :
Finset.sum S f = Finset.sum S g := by
conv_lhs =>
-- If we just call `congr` here, in the second goal we're helpless,
-- because we are only given the opportunity to rewrite `f`.
-- However `apply_congr` uses the appropriate `@[congr]` lemma,
-- so we get to rewrite `f x`, in the presence of the crucial `H : x ∈ S` hypothesis.
apply_congr
· skip
· simp [*] |
.lake/packages/mathlib/MathlibTest/Split.lean | import Mathlib.Tactic.Basic
example : (α : Type) × List α := by
constructor
· exact [0,1]
-- example : (α : Type) × List α := by
-- fsplit
-- - exact ℕ
-- - exact [0,1] |
.lake/packages/mathlib/MathlibTest/push_neg.lean | import Mathlib.Order.Defs.LinearOrder
import Mathlib.Data.Set.Basic
import Mathlib.Tactic.Push
private axiom test_sorry : ∀ {α}, α
set_option autoImplicit true
variable {α β : Type} [LinearOrder β] {p q : Prop} {p' q' : α → Prop}
example : ¬ False := by
push_neg
example (h : ¬ True) : False := by
push_neg at h
exact h
example : (¬p ∧ ¬q) → ¬(p ∨ q) := by
intro h
push_neg
guard_target = ¬p ∧ ¬q
exact h
example : ¬(p ∧ q) → (p → ¬q) := by
intro h
push_neg at h
guard_hyp h : p → ¬q
exact h
example : (∀ (x : α), ¬ p' x) → ¬ ∃ (x : α), p' x := by
intro h
push_neg
guard_target = ∀ (x : α), ¬p' x
exact h
example : (¬ ∀ (x : α), p' x) → (∃ (x : α), ¬ p' x) := by
intro h
push_neg at h
guard_hyp h : ∃ (x : α), ¬p' x
exact h
example (p : Bool) : decide (¬ ¬ p) = p := by
push_neg
guard_target = decide p = p
cases p <;> rfl
example : ((fun x => x+x) 1) = 2 := by
push_neg
guard_target = 1 + 1 = 2
simp
example : ¬ ¬ p = p := by
push_neg
guard_target = p = p
rfl
example (x y : β) (h : y < x) : ¬(x ≤ y) := by
push_neg
guard_target = y < x
exact h
example (a b : β) (h : a ≤ b) : ¬ a > b := by
push_neg
guard_target = a ≤ b
exact h
example (x y : α) (h : x = y) : ¬ (x ≠ y) := by
push_neg
guard_target = x = y
exact h
example : ¬∃ (y : Unit), (y ≠ ()) := by
push_neg
guard_target = ∀ (y : Unit), y = ()
simp
example (h : ∃ y : Nat, ¬(y=1)): ¬∀ (y : Nat), (y = 1) := by
push_neg
guard_target = ∃ (y : Nat), y ≠ 1
exact h
example (x y : β) (h : y < x) : ¬¬¬ (x ≤ y) := by
push_neg
guard_target = y < x
exact h
set_option linter.unusedVariables false in
example (x y : β) (h₁ : ¬¬¬(x < y)) (h₂ : ¬∃ (x y : Nat), x = y) : ¬ ∀ (x y : Nat), x = y := by
push_neg at *
guard_target = ∃ (x y : Nat), x ≠ y
guard_hyp h₁ : y ≤ x
guard_hyp h₂ : ∀ (x y : Nat), x ≠ y
exact ⟨0, 1, by simp⟩
set_option linter.unusedVariables false in
example (x y : β) (h₁ : ¬¬¬(x < y)) (h₂ : ¬∃ (x y : Nat), x = y) : ¬ ∀ (x y : Nat), x = y := by
push_neg at h₁ h₂ ⊢
guard_target = ∃ (x y : Nat), x ≠ y
guard_hyp h₁ : y ≤ x
guard_hyp h₂ : ∀ (x y : Nat), x ≠ y
exact ⟨0, 1, by simp⟩
example (h : p → ¬ q) : ¬ (p ∧ q) := by
push_neg
guard_target = p → ¬q
exact h
example (a : β) : ¬ ∀ x : β, x < a → ∃ y : β, (y < a) ∧ ∀ z : β, x = z := by
push_neg
guard_target = ∃ x, x < a ∧ ∀ (y : β), y < a → ∃ z, x ≠ z
exact test_sorry
set_option linter.unusedVariables false in
example {α} [Preorder α] (m n : α) (h : ¬(∃ k : α, m ≤ k)) (h₂ : m ≤ n) : m ≤ n := by
push_neg at h
guard_hyp h : ∀ k, ¬(m ≤ k)
exact h₂
set_option linter.unusedVariables false in
example {α} [Preorder α] (m n : α) (h : ¬(∃ k : α, m < k)) (h₂ : m ≤ n) : m ≤ n := by
push_neg at h
guard_hyp h : ∀ k, ¬(m < k)
exact h₂
example (r : LinearOrder α) (s : Preorder α) (a b : α) : ¬(s.lt a b → r.lt a b) := by
push_neg
guard_target = s.lt a b ∧ r.le b a
exact test_sorry
example (r : LinearOrder α) (s : Preorder α) (a b : α) : ¬(r.lt a b → s.lt a b) := by
push_neg
guard_target = r.lt a b ∧ ¬ s.lt a b
exact test_sorry
-- check that `push_neg` does not expand `let` definitions
example (h : p ∧ q) : ¬¬(p ∧ q) := by
let r := p ∧ q
change ¬¬r
push_neg
guard_target =ₛ r
exact h
-- new error message as of https://github.com/leanprover-community/mathlib4/issues/27562
/-- error: push made no progress anywhere -/
#guard_msgs in
example {P : Prop} (h : P) : P := by push_neg at *
-- new behaviour as of https://github.com/leanprover-community/mathlib4/issues/27562
-- (Previously, because of a metavariable instantiation issue, the tactic succeeded as a no-op.)
/-- error: push made no progress at h -/
#guard_msgs in
example {x y : ℕ} : True := by
have h : x ≤ y := test_sorry
push_neg at h
-- new behaviour as of https://github.com/leanprover-community/mathlib4/issues/27562 (previously the tactic succeeded as a no-op)
/-- error: cannot run push at inductive_proof, it is an implementation detail -/
#guard_msgs in
def inductive_proof : True := by
push_neg at inductive_proof
trivial
section use_distrib
set_option push_neg.use_distrib true
example (h : ¬ p ∨ ¬ q): ¬ (p ∧ q) := by
push_neg
guard_target = ¬p ∨ ¬q
exact h
example : p → ¬ ¬ ¬ ¬ ¬ ¬ p := by
push_neg
guard_target = p → p
exact id
example (h : x = 0 ∧ y ≠ 0) : ¬(x = 0 → y = 0) := by
push_neg
guard_target = x = 0 ∧ y ≠ 0
exact h
end use_distrib
example (a : α) (o : Option α) (h : ¬∀ hs, o.get hs ≠ a) : ∃ hs, o.get hs = a := by
push_neg at h
exact h
example (s : Set α) (h : ¬s.Nonempty) : s = ∅ := by
push_neg at h
exact h
example (s : Set α) (h : ¬ s = ∅) : s.Nonempty := by
push_neg at h
exact h
example (s : Set α) (h : s ≠ ∅) : s.Nonempty := by
push_neg at h
exact h
example (s : Set α) (h : ∅ ≠ s) : s.Nonempty := by
push_neg at h
exact h
namespace no_proj
structure G (V : Type) where
Adj : V → V → Prop
def g : G Nat where
Adj a b := (a ≠ b) ∧ ((a ∣ b) ∨ (b ∣ a))
example {p q : Nat} : ¬ g.Adj p q := by
rw [g]
guard_target =ₛ ¬ G.Adj { Adj := fun a b => (a ≠ b) ∧ ((a ∣ b) ∨ (b ∣ a)) } p q
fail_if_success push_neg
guard_target =ₛ ¬ G.Adj { Adj := fun a b => (a ≠ b) ∧ ((a ∣ b) ∨ (b ∣ a)) } p q
dsimp only
guard_target =ₛ ¬ ((p ≠ q) ∧ ((p ∣ q) ∨ (q ∣ p)))
push_neg
guard_target =ₛ p ≠ q → ¬p ∣ q ∧ ¬q ∣ p
exact test_sorry
end no_proj
-- test that binder names are preserved by `push_neg`
/-- info: ∀ (a b : ℕ), ∃ c d, a + b ≠ c + d -/
#guard_msgs in
#push_neg ¬ ∃ a b : Nat, ∀ c d : Nat, a + b = c + d |
.lake/packages/mathlib/MathlibTest/Deriv.lean | import Mathlib
/-! Test that `simp` can prove some lemmas about derivatives. -/
open Real
example (x : ℝ) : deriv (fun x => cos x + 2 * sin x) x = -sin x + 2 * cos x := by
simp
example (x : ℝ) :
deriv (fun x ↦ cos (sin x) * exp x) x = (cos (sin x) - sin (sin x) * cos x) * exp x := by
simp; ring
/- for more complicated examples (with more nested functions) you need to increase the
`maxDischargeDepth`. -/
example (x : ℝ) :
deriv (fun x ↦ sin (sin (sin x)) + sin x) x =
cos (sin (sin x)) * (cos (sin x) * cos x) + cos x := by
simp (maxDischargeDepth := 3)
example (x : ℝ) :
deriv (fun x ↦ sin (sin (sin x)) ^ 10 + sin x) x =
10 * sin (sin (sin x)) ^ 9 * (cos (sin (sin x)) * (cos (sin x) * cos x)) + cos x := by
simp (maxDischargeDepth := 4) |
.lake/packages/mathlib/MathlibTest/recover.lean | import Mathlib.Tactic.Recover
set_option linter.unusedTactic false
/-- problematic tactic for testing recovery -/
elab "this" "is" "a" "problem" : tactic =>
Lean.Elab.Tactic.setGoals []
/- The main test -/
example : 1 = 1 := by
recover this is a problem
rfl
/- Tests that recover does no harm -/
example : 3 < 4 := by
recover decide
example : 1 = 1 := by
recover skip; rfl
example : 2 = 2 := by
recover skip
rfl |
.lake/packages/mathlib/MathlibTest/Simps.lean | import Mathlib.Algebra.Group.Defs
import Mathlib.Tactic.Simps.Basic
import Mathlib.Lean.Exception
import Mathlib.Logic.Equiv.Defs
import Mathlib.Data.Prod.Basic
import Mathlib.Tactic.Common
-- set_option trace.simps.debug true
-- set_option trace.simps.verbose true
-- set_option pp.universes true
set_option autoImplicit true
-- A few times, fields in this file are manually aligned. While there is some consensus this
-- is not desired, it's not important enough to change right now.
set_option linter.style.commandStart false
open Lean Meta Elab Term Command Simps
/-- Tests whether `declName` has the `@[simp]` attribute in `env`. -/
def hasSimpAttribute (env : Environment) (declName : Name) : Bool :=
simpExtension.getState env |>.lemmaNames.contains <| .decl declName
structure Foo1 : Type where
Projone : Nat
two : Bool
three : Nat → Bool
four : 1 = 1
five : 2 = 1
initialize_simps_projections Foo1 (Projone → toNat, two → toBool, three → coe, as_prefix coe,
-toBool)
run_cmd liftTermElabM <| do
let env ← getEnv
let state := ((Simps.structureExt.getState env).find? `Foo1).get!
guard <| state.1 == []
guard <| state.2.map (·.1) == #[`toNat, `toBool, `coe, `four, `five]
liftMetaM <| guard (← isDefEq (state.2[0]!.2) (← elabTerm (← `(Foo1.Projone)) none))
liftMetaM <| guard (← isDefEq (state.2[1]!.2) (← elabTerm (← `(Foo1.two)) none))
guard <| state.2.map (·.3) == (Array.range 5).map ([·])
guard <| state.2.map (·.4) == #[true, false, true, false, false]
guard <| state.2.map (·.5) == #[false, false, true, false, false]
pure ()
structure Foo2 (α : Type _) : Type _ where
elim : α × α
def Foo2.Simps.elim (α : Type _) : Foo2 α → α × α := fun x ↦ (x.elim.1, x.elim.2)
initialize_simps_projections Foo2
/--
trace: [simps.verbose] The projections for this structure have already been initialized by a previous invocation of `initialize_simps_projections` or `@[simps]`.
Generated projections for Foo2:
Projection elim: fun α x => (x.elim.1, x.elim.2)
-/
#guard_msgs in
initialize_simps_projections Foo2
@[simps]
def Foo2.foo2 : Foo2 Nat := ⟨(0, 0)⟩
-- run_cmd do
-- logInfo m!"{Simps.structureExt.getState (← getEnv) |>.find? `Foo2 |>.get!}"
structure Left (α : Type _) extends Foo2 α where
moreData1 : Nat
moreData2 : Nat
initialize_simps_projections Left
structure Right (α : Type u) (β : Type v) extends Foo2 α where
otherData : β
initialize_simps_projections Right (elim → newProjection, -otherData, +toFoo2)
run_cmd liftTermElabM <| do
let env ← getEnv
let state := ((Simps.structureExt.getState env).find? `Right).get!
-- logInfo m!"{state}"
guard <| state.1 == [`u, `v]
guard <| state.2.map (·.1) == #[`toFoo2, `otherData, `newProjection]
guard <| state.2.map (·.3) == #[[0], [1], [0,0]]
guard <| state.2.map (·.4) == #[true, false, true]
guard <| state.2.map (·.5) == #[false, false, false]
structure Top (α β : Type _) extends Left α, Right α β
initialize_simps_projections Top
structure NewTop (α β : Type _) extends Right α β, Left α
def NewTop.Simps.newElim {α β : Type _} (x : NewTop α β) : α × α := x.elim
initialize_simps_projections NewTop (elim → newElim)
run_cmd liftCoreM <| successIfFail <| getRawProjections .missing `DoesntExist
class Something (α : Type _) where
op : α → α → α → α
instance {α : Type _} [Something α] : Add α :=
⟨fun x y ↦ Something.op x y y⟩
initialize_simps_projections Something
universe v u w
structure Equiv' (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
(left_inv : invFun.LeftInverse toFun)
(right_inv : invFun.RightInverse toFun)
infix:25 (priority := default+1) " ≃ " => Equiv'
/- Since `prod` and `PProd` are a special case for `@[simps]`, we define a new structure to test
the basic functionality. -/
structure MyProd (α β : Type _) where (fst : α) (snd : β)
def MyProd.map {α α' β β'} (f : α → α') (g : β → β') (x : MyProd α β) : MyProd α' β' :=
⟨f x.1, g x.2⟩
namespace foo
@[simps] protected def rfl {α} : α ≃ α :=
⟨id, fun x ↦ x, fun _ ↦ rfl, fun _ ↦ rfl⟩
/- simps adds declarations -/
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `foo.rfl_toFun |>.isSome
guard <| env.find? `foo.rfl_invFun |>.isSome
guard <| env.find? `foo.rfl_left_inv |>.isNone
guard <| env.find? `foo.rfl_right_inv |>.isNone
guard <| simpsAttr.getParam? env `foo.rfl == #[`foo.rfl_toFun, `foo.rfl_invFun]
example (n : ℕ) : foo.rfl.toFun n = n := by rw [foo.rfl_toFun, id]
example (n : ℕ) : foo.rfl.invFun n = n := by rw [foo.rfl_invFun]
/- the declarations are `simp` lemmas -/
@[simps] def bar : ℕ × ℤ := (1, 2)
-- note: in Lean 4 the first test succeeds without `@[simps]`, however, the remaining tests don't
example : bar.1 = 1 := by simp
example {a : ℕ} {h : 1 = a} : bar.1 = a := by rw [bar_fst, h]
example {a : ℕ} {h : 1 = a} : bar.1 = a := by simp; rw [h]
example {a : ℤ} {h : 2 = a} : bar.2 = a := by simp; rw [h]
example {a : ℕ} {h : 1 = a} : bar.1 = a := by dsimp; rw [h] -- check that dsimp also unfolds
example {a : ℤ} {h : 2 = a} : bar.2 = a := by dsimp; rw [h]
example {α} (x y : α) (h : x = y) : foo.rfl.toFun x = y := by simp; rw [h]
example {α} (x y : α) (h : x = y) : foo.rfl.invFun x = y := by simp; rw [h]
-- example {α} (x y : α) (h : x = y) : foo.rfl.toFun = @id α := by { successIfFail {simp}, rfl }
/- check some failures -/
def bar1 : ℕ := 1 -- type is not a structure
noncomputable def bar2 {α} : α ≃ α :=
Classical.choice ⟨foo.rfl⟩
run_cmd liftCoreM <| do
_ ← successIfFail <| simpsTac .missing `foo.bar1 { rhsMd := .default, simpRhs := true }
-- "Invalid `simps` attribute. Target Nat is not a structure"
_ ← successIfFail <| simpsTac .missing `foo.bar2 { rhsMd := .default, simpRhs := true }
-- "Invalid `simps` attribute. The body is not a constructor application:
-- Classical.choice (_ : Nonempty (α ≃ α))"
pure ()
/- test that if a non-constructor is given as definition, then
`{rhsMd := .default, simpRhs := true}` is applied automatically. -/
@[simps!] def rfl2 {α} : α ≃ α := foo.rfl
example {α} (x : α) : rfl2.toFun x = x ∧ rfl2.invFun x = x := by
dsimp
guard_target = x = x ∧ x = x
exact ⟨rfl, rfl⟩
example {α} (x : α) : rfl2.toFun x = x ∧ rfl2.invFun x = x := by
dsimp only [rfl2_toFun, rfl2_invFun]
guard_target = x = x ∧ x = x
exact ⟨rfl, rfl⟩
/- test `fullyApplied` option -/
@[simps -fullyApplied]
def rfl3 {α} : α ≃ α := ⟨id, fun x ↦ x, fun _ ↦ rfl, fun _ ↦ rfl⟩
end foo
/- we reduce the type when applying [simps] -/
def my_equiv := Equiv'
@[simps] def baz : my_equiv ℕ ℕ := ⟨id, fun x ↦ x, fun _ ↦ rfl, fun _ ↦ rfl⟩
/- todo: test that name clashes gives an error -/
/- check projections for nested structures -/
namespace CountNested
@[simps]
def nested1 : MyProd ℕ <| MyProd ℤ ℕ :=
⟨2, -1, 1⟩
@[simps -isSimp]
def nested2 : ℕ × MyProd ℕ ℕ :=
⟨2, MyProd.map Nat.succ Nat.pred ⟨1, 2⟩⟩
end CountNested
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `CountNested.nested1_fst |>.isSome
guard <| env.find? `CountNested.nested1_snd_fst |>.isSome
guard <| env.find? `CountNested.nested1_snd_snd |>.isSome
guard <| env.find? `CountNested.nested2_fst |>.isSome
guard <| env.find? `CountNested.nested2_snd |>.isSome
guard <| simpsAttr.getParam? env `CountNested.nested1 ==
#[`CountNested.nested1_fst, `CountNested.nested1_snd_fst, `CountNested.nested1_snd_snd]
guard <| simpsAttr.getParam? env `CountNested.nested2 ==
#[`CountNested.nested2_fst, `CountNested.nested2_snd]
-- todo: test that another attribute can be added (not working yet)
guard <| hasSimpAttribute env `CountNested.nested1_fst -- simp attribute is global
guard <| not <| hasSimpAttribute env `CountNested.nested2_fst
-- `- isSimp` doesn't add simp lemma
-- todo: maybe test that there are no other lemmas generated
-- guard <| 7 = env.fold 0
-- (fun d n ↦ n + if d.to_name.components.init.ilast = `CountNested then 1 else 0)
-- testing with arguments
@[simps] def bar {_ : Type _} (n m : ℕ) : ℕ × ℤ :=
⟨n - m, n + m⟩
structure EquivPlusData (α β) extends α ≃ β where
P : (α → β) → Prop
data : P toFun
structure ComplicatedEquivPlusData (α) extends α ⊕ α ≃ α ⊕ α where
P : (α ⊕ α → α ⊕ α) → Prop
data : P toFun
extra : Bool → MyProd ℕ ℕ
/-- Test whether structure-eta-reduction is working correctly. -/
@[simps!]
def rflWithData {α} : EquivPlusData α α :=
{ foo.rfl with
P := fun f ↦ f = id
data := rfl }
@[simps!]
def rflWithData' {α} : EquivPlusData α α :=
{ P := fun f ↦ f = id
data := rfl
toEquiv' := foo.rfl }
/- test whether eta expansions are reduced correctly -/
@[simps!]
def test {α} : ComplicatedEquivPlusData α :=
{ foo.rfl with
P := fun f ↦ f = id
data := rfl
extra := fun _ ↦ ⟨(⟨3, 5⟩ : MyProd _ _).1, (⟨3, 5⟩ : MyProd _ _).2⟩ }
/- test whether this is indeed rejected as a valid eta expansion -/
@[simps!]
def test_sneaky {α} : ComplicatedEquivPlusData α :=
{ foo.rfl with
P := fun f ↦ f = id
data := rfl
extra := fun _ ↦ ⟨(3,5).1,(3,5).2⟩ }
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `rflWithData_toFun |>.isSome
guard <| env.find? `rflWithData'_toFun |>.isSome
guard <| env.find? `test_extra_fst |>.isSome
guard <| simpsAttr.getParam? env `test ==
#[`test_P, `test_extra_fst, `test_extra_snd, `test_toFun, `test_invFun]
guard <| env.find? `test_sneaky_extra_fst |>.isSome
guard <| env.find? `rflWithData_toEquiv_toFun |>.isNone
guard <| env.find? `rflWithData'_toEquiv_toFun |>.isNone
guard <| env.find? `test_sneaky_extra |>.isNone
structure PartiallyAppliedStr where
(data : ℕ → MyProd ℕ ℕ)
/- if we have a partially applied constructor, we treat it as if it were eta-expanded -/
@[simps]
def partially_applied_term : PartiallyAppliedStr := ⟨MyProd.mk 3⟩
@[simps]
def another_term : PartiallyAppliedStr := ⟨fun n ↦ ⟨n + 1, n + 2⟩⟩
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `partially_applied_term_data_fst |>.isSome
guard <| env.find? `partially_applied_term_data_snd |>.isSome
guard <| simpsAttr.getParam? env `partially_applied_term ==
#[`partially_applied_term_data_fst, `partially_applied_term_data_snd]
structure VeryPartiallyAppliedStr where
(data : ∀ β, ℕ → β → MyProd ℕ β)
/- if we have a partially applied constructor, we treat it as if it were eta-expanded.
(this is not very useful, and we could remove this behavior if convenient) -/
@[simps]
def very_partially_applied_term : VeryPartiallyAppliedStr := ⟨@MyProd.mk ℕ⟩
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `very_partially_applied_term_data_fst |>.isSome
guard <| env.find? `very_partially_applied_term_data_snd |>.isSome
@[simps] def let1 : ℕ × ℤ :=
let n := 3; ⟨n + 4, 5⟩
@[simps] def let2 : ℕ × ℤ :=
let n := 3; let m := 4; let k := 5; ⟨n + m, k⟩
@[simps] def let3 : ℕ → ℕ × ℤ :=
fun n ↦ let m := 4; let k := 5; ⟨n + m, k⟩
@[simps] def let4 : ℕ → ℕ × ℤ :=
let m := 4; let k := 5; fun n ↦ ⟨n + m, k⟩
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `let1_fst |>.isSome
guard <| env.find? `let2_fst |>.isSome
guard <| env.find? `let3_fst |>.isSome
guard <| env.find? `let4_fst |>.isSome
guard <| env.find? `let1_snd |>.isSome
guard <| env.find? `let2_snd |>.isSome
guard <| env.find? `let3_snd |>.isSome
guard <| env.find? `let4_snd |>.isSome
namespace specify
-- todo: error when naming arguments
@[simps fst] def specify1 : ℕ × ℕ × ℕ := (1, 2, 3)
@[simps snd] def specify2 : ℕ × ℕ × ℕ := (1, 2, 3)
@[simps snd_fst] def specify3 : ℕ × ℕ × ℕ := (1, 2, 3)
@[simps snd snd_snd] def specify4 : ℕ × ℕ × ℕ := (1, 2, 3) -- last argument is ignored
@[simps] noncomputable def specify5 : ℕ × ℕ × ℕ := (1, Classical.choice ⟨(2, 3)⟩)
end specify
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `specify.specify1_fst |>.isSome
guard <| env.find? `specify.specify2_snd |>.isSome
guard <| env.find? `specify.specify3_snd_fst |>.isSome
guard <| env.find? `specify.specify4_snd_snd |>.isSome
guard <| env.find? `specify.specify4_snd |>.isSome
guard <| env.find? `specify.specify5_fst |>.isSome
guard <| env.find? `specify.specify5_snd |>.isSome
guard <| simpsAttr.getParam? env `specify.specify1 == #[`specify.specify1_fst]
guard <| simpsAttr.getParam? env `specify.specify4 ==
#[`specify.specify4_snd_snd, `specify.specify4_snd]
guard <| simpsAttr.getParam? env `specify.specify5 ==
#[`specify.specify5_fst, `specify.specify5_snd]
_ ← successIfFail <| simpsTac .missing `specify.specify1 {} [("fst_fst", .missing)]
-- "Invalid simp lemma specify.specify1_fst_fst.
-- Projection fst doesn't exist, because target is not a structure."
_ ← successIfFail <| simpsTac .missing `specify.specify1 {} [("foo_fst", .missing)]
-- "Invalid simp lemma specify.specify1_foo_fst. Structure prod does not have projection foo.
-- The known projections are:
-- [fst, snd]
-- You can also see this information by running
-- `initialize_simps_projections? prod`.
-- Note: these projection names might not correspond to the projection names of the structure."
_ ← successIfFail <| simpsTac .missing `specify.specify1 {} [("snd_bar", .missing)]
-- "Invalid simp lemma specify.specify1_snd_bar. Structure prod does not have projection bar.
-- The known projections are:
-- [fst, snd]
-- You can also see this information by running
-- `initialize_simps_projections? prod`.
-- Note: these projection names might not correspond to the projection names of the structure."
_ ← successIfFail <| simpsTac .missing `specify.specify5 { rhsMd := .default, simpRhs := true }
[("snd_snd", .missing)]
-- "Invalid simp lemma specify.specify5_snd_snd.
-- The given definition is not a constructor application:
-- Classical.choice specify.specify5._proof_1"
/- We also eta-reduce if we explicitly specify the projection. -/
attribute [simps extra] test
example {α} {b : Bool} {x} (h : (⟨3, 5⟩ : MyProd _ _) = x) : (@test α).extra b = x := by
dsimp
rw [h]
/- check simpRhs option -/
@[simps +simpRhs] def Equiv'.trans {α β γ} (f : α ≃ β) (g : β ≃ γ) : α ≃ γ :=
⟨ g.toFun ∘ f.toFun,
f.invFun ∘ g.invFun,
(by intro x; simp [Equiv'.left_inv _ _]),
(by intro x; simp [Equiv'.right_inv _ _])⟩
example {α β γ : Type} (f : α ≃ β) (g : β ≃ γ) (x : α) {z : γ} (h : g.toFun (f.toFun x) = z) :
(f.trans g).toFun x = z := by
dsimp only [Equiv'.trans_toFun]
rw [h]
attribute [local simp] Nat.zero_add Nat.one_mul Nat.mul_one
@[simps +simpRhs] def myNatEquiv : ℕ ≃ ℕ :=
⟨fun n ↦ 0 + n, fun n ↦ 1 * n * 1, by intro n; simp, by intro n; simp⟩
example (n : ℕ) : myNatEquiv.toFun (myNatEquiv.toFun <| myNatEquiv.invFun n) = n := by
{ /-successIfFail { rfl },-/ simp only [myNatEquiv_toFun, myNatEquiv_invFun] }
@[simps +simpRhs] def succeed_without_simplification_possible : ℕ ≃ ℕ :=
⟨fun n ↦ n, fun n ↦ n, by intro n; rfl, by intro n; rfl⟩
/- test that we don't recursively take projections of `prod` and `PProd` -/
@[simps] def pprodEquivProd2 : PProd ℕ ℕ ≃ ℕ × ℕ :=
{ toFun := fun x ↦ ⟨x.1, x.2⟩
invFun := fun x ↦ ⟨x.1, x.2⟩
left_inv := fun ⟨_, _⟩ ↦ rfl
right_inv := fun ⟨_, _⟩ ↦ rfl }
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `pprodEquivProd2_toFun |>.isSome
guard <| env.find? `pprodEquivProd2_invFun |>.isSome
attribute [simps toFun_fst invFun_snd] pprodEquivProd2
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `pprodEquivProd2_toFun_fst |>.isSome
guard <| env.find? `pprodEquivProd2_invFun_snd |>.isSome
-- we can disable this behavior with the option `notRecursive`.
@[simps! (notRecursive := [])] def pprodEquivProd22 : PProd ℕ ℕ ≃ ℕ × ℕ :=
pprodEquivProd2
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `pprodEquivProd22_toFun_fst |>.isSome
guard <| env.find? `pprodEquivProd22_toFun_snd |>.isSome
guard <| env.find? `pprodEquivProd22_invFun_fst |>.isSome
guard <| env.find? `pprodEquivProd22_invFun_snd |>.isSome
/- Tests with universe levels -/
class has_hom (obj : Type u) : Type (max u (v+1)) where
(hom : obj → obj → Type v)
infixr:10 " ⟶ " => has_hom.hom -- type as \h
class CategoryStruct (obj : Type u) : Type (max u (v+1)) extends has_hom.{v} obj where
(id : ∀ X : obj, hom X X)
(comp : ∀ {X Y Z : obj}, (X ⟶ Y) → (Y ⟶ Z) → (X ⟶ Z))
notation "𝟙" => CategoryStruct.id -- type as \b1
infixr:80 " ≫ " => CategoryStruct.comp -- type as \gg
namespace types
@[simps] instance : CategoryStruct (Type u) :=
{ hom := fun a b ↦ (a → b)
id := fun _ ↦ id
comp := fun f g ↦ g ∘ f }
end types
/--
info: types.comp_def.{u} {X✝ Y✝ Z✝ : Type u} (f : X✝ → Y✝) (g : Y✝ → Z✝) (a✝ : X✝) : (f ≫ g) a✝ = (g ∘ f) a✝
-/
#guard_msgs in
#check types.comp_def
@[ext] theorem types.ext {X Y : Type u} {f g : X ⟶ Y} : (∀ x, f x = g x) → f = g := funext
example (X : Type u) {x : Type u} (h : (X → X) = x) : (X ⟶ X) = x := by simp; rw [h]
example (X : Type u) {f : X → X} (h : ∀ x, f x = x) : 𝟙 X = f := by ext; simp; rw [h]
example (X Y Z : Type u) (f : X ⟶ Y) (g : Y ⟶ Z) {k : X → Z} (h : ∀ x, g (f x) = k x) :
f ≫ g = k := by ext; simp; rw [h]
-- Ensure that a universe polymorphic projection like `PLift.down`
-- doesn't cause `simp` lemmas about proofs.
instance discreteCategory : CategoryStruct Nat where
hom X Y := ULift (PLift (X = Y))
id _ := ULift.up (PLift.up rfl)
comp f g := ⟨⟨f.1.1.trans g.1.1⟩⟩
structure Prefunctor (C : Type u) [CategoryStruct C] (D : Type v) [CategoryStruct D] where
/-- The action of a (pre)functor on vertices/objects. -/
obj : C → D
/-- The action of a (pre)functor on edges/arrows/morphisms. -/
map : ∀ {X Y : C}, (X ⟶ Y) → (obj X ⟶ obj Y)
@[simps]
def IdentityPreunctor : Prefunctor (Type u) Nat where
obj _ := 5
map _ := ⟨⟨rfl⟩⟩
/-- error: Unknown identifier `IdentityPreunctor_map_down_down` -/
#guard_msgs in
#check IdentityPreunctor_map_down_down
namespace coercing
structure FooStr where
(c : Type)
(x : c)
instance : CoeSort FooStr Type := ⟨FooStr.c⟩
@[simps] def foo : FooStr := ⟨ℕ, 3⟩
@[simps] def foo2 : FooStr := ⟨ℕ, 34⟩
example {x : Type} (h : ℕ = x) : foo = x := by simp only [foo_c]; rw [h]
example {x : ℕ} (h : (3 : ℕ) = x) : foo.x = x := by simp only [foo_x]; rw [h]
structure VooStr (n : ℕ) where
(c : Type)
(x : c)
instance (n : ℕ) : CoeSort (VooStr n) Type := ⟨VooStr.c⟩
@[simps] def voo : VooStr 7 := ⟨ℕ, 3⟩
@[simps] def voo2 : VooStr 4 := ⟨ℕ, 34⟩
example {x : Type} (h : ℕ = x) : voo = x := by simp only [voo_c]; rw [h]
example {x : ℕ} (h : (3 : ℕ) = x) : voo.x = x := by simp only [voo_x]; rw [h]
structure Equiv2 (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
(left_inv : invFun.LeftInverse toFun)
(right_inv : invFun.RightInverse toFun)
instance {α β} : CoeFun (Equiv2 α β) (fun _ ↦ α → β) := ⟨Equiv2.toFun⟩
@[simps] protected def rfl2 {α} : Equiv2 α α :=
⟨fun x ↦ x, fun x ↦ x, fun _ ↦ rfl, fun _ ↦ rfl⟩
example {α} (x x' : α) (h : x = x') : coercing.rfl2 x = x' := by rw [coercing.rfl2_toFun, h]
example {α} (x x' : α) (h : x = x') : coercing.rfl2 x = x' := by simp; rw [h]
example {α} (x x' : α) (h : x = x') : coercing.rfl2.invFun x = x' := by simp; rw [h]
@[simps] protected def Equiv2.symm {α β} (f : Equiv2 α β) : Equiv2 β α :=
⟨f.invFun, f, f.right_inv, f.left_inv⟩
@[simps] protected def Equiv2.symm2 {α β} (f : Equiv2 α β) : Equiv2 β α :=
⟨f.invFun, f.toFun, f.right_inv, f.left_inv⟩
@[simps -fullyApplied] protected def Equiv2.symm3 {α β} (f : Equiv2 α β) : Equiv2 β α :=
⟨f.invFun, f, f.right_inv, f.left_inv⟩
example {α β} (f : Equiv2 α β) (y : β) {x} (h : f.invFun y = x) : f.symm y = x := by simp; rw [h]
example {α β} (f : Equiv2 α β) (x : α) {z} (h : f x = z) : f.symm.invFun x = z := by simp; rw [h]
-- example {α β} (f : Equiv2 α β) {x} (h : f = x) : f.symm.invFun = x :=
-- by { /-successIfFail {simp <;> rw [h]} <;>-/ rfl }
example {α β} (f : Equiv2 α β) {x} (h : f = x) : f.symm3.invFun = x := by simp; rw [h]
class Semigroup (G : Type u) extends Mul G where
mul_assoc : ∀ a b c : G, a * b * c = a * (b * c)
@[simps] instance {α β} [Semigroup α] [Semigroup β] : Semigroup (α × β) :=
{ mul := fun x y ↦ (x.1 * y.1, x.2 * y.2)
mul_assoc := fun _ _ _ ↦ Prod.ext (Semigroup.mul_assoc ..) (Semigroup.mul_assoc ..) }
example {α β} [Semigroup α] [Semigroup β] (x y : α × β) : x * y = (x.1 * y.1, x.2 * y.2) := by simp
example {α β} [Semigroup α] [Semigroup β] (x y : α × β) : (x * y).1 = x.1 * y.1 := by simp
structure BSemigroup where
(G : Type _)
(op : G → G → G)
-- (infix:60 " * " => op) -- this seems to be removed
(op_assoc : ∀ (x y z : G), op (op x y) z = op x (op y z))
namespace BSemigroup
instance : CoeSort BSemigroup (Type _) := ⟨BSemigroup.G⟩
-- We could try to generate lemmas with this `HMul` instance, but it is unused in mathlib3/mathlib4.
-- Therefore, this is ignored.
instance (G : BSemigroup) : Mul G := ⟨G.op⟩
protected def prod (G H : BSemigroup) : BSemigroup :=
{ G := G × H
op := fun x y ↦ (x.1 * y.1, x.2 * y.2)
op_assoc := fun _ _ _ ↦ Prod.ext (BSemigroup.op_assoc ..) (BSemigroup.op_assoc ..) }
end BSemigroup
class ExtendingStuff (G : Type u) extends Mul G, Zero G, Neg G, HasSubset G where
new_axiom : ∀ x : G, x * - 0 ⊆ - x
@[simps!] def bar : ExtendingStuff ℕ :=
{ neg := Nat.succ
Subset := fun _ _ ↦ True
new_axiom := fun _ ↦ trivial }
section
attribute [local instance] bar
example (x : ℕ) : x * - 0 ⊆ - x := by simp
end
class new_ExtendingStuff (G : Type u) extends Mul G, Zero G, Neg G, HasSubset G where
new_axiom : ∀ x : G, x * - 0 ⊆ - x
@[simps!] def new_bar : new_ExtendingStuff ℕ :=
{ neg := Nat.succ
Subset := fun _ _ ↦ True
new_axiom := fun _ ↦ trivial }
section
attribute [local instance] new_bar
example (x : ℕ) : x * - 0 ⊆ - x := by simp
end
end coercing
namespace ManualCoercion
structure Equiv (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => ManualCoercion.Equiv
variable {α β γ : Sort _}
instance : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv.toFun⟩
def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
/-- See Note [custom simps projection] -/
def Equiv.Simps.invFun (e : α ≃ β) : β → α := e.symm
/-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/
@[simps +simpRhs]
protected def Equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β)⟩
example (e₁ : α ≃ β) (e₂ : β ≃ γ) (x : γ) {z} (h : e₁.symm (e₂.symm x) = z) :
(e₁.trans e₂).symm x = z := by
simp only [Equiv.trans_invFun]; rw [h]
end ManualCoercion
namespace FaultyManualCoercion
structure Equiv (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => FaultyManualCoercion.Equiv
variable {α β γ : Sort _}
/-- See Note [custom simps projection] -/
noncomputable def Equiv.Simps.invFun (e : α ≃ β) : β → α := Classical.choice ⟨e.invFun⟩
run_cmd liftTermElabM <| do
successIfFail (getRawProjections .missing `FaultyManualCoercion.Equiv)
-- "Invalid custom projection:
-- fun {α : Sort u_1} {β : Sort u_2} (e : α ≃ β) ↦ Classical.choice _
-- Expression is not definitionally equal to
-- fun (α : Sort u_1) (β : Sort u_2) (x : α ≃ β) ↦ x.invFun"
end FaultyManualCoercion
namespace ManualInitialize
/- defining a manual coercion. -/
variable {α β γ : Sort _}
structure Equiv (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => ManualInitialize.Equiv
instance : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv.toFun⟩
def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
/-- See Note [custom simps projection] -/
def Equiv.Simps.invFun (e : α ≃ β) : β → α := e.symm
initialize_simps_projections Equiv
-- run_cmd has_attribute `_simps_str `ManualInitialize.Equiv
/-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/
@[simps +simpRhs]
protected def Equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β)⟩
example (e₁ : α ≃ β) (e₂ : β ≃ γ) (x : γ) {z} (h : e₁.symm (e₂.symm x) = z) :
(e₁.trans e₂).symm x = z := by
simp only [Equiv.trans_invFun]; rw [h]
end ManualInitialize
namespace FaultyUniverses
variable {α β γ : Sort _}
structure Equiv (α : Sort u) (β : Sort v) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => FaultyUniverses.Equiv
instance : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv.toFun⟩
def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
/-- See Note [custom simps projection] -/
def Equiv.Simps.invFun {α : Type u} {β : Type v} (e : α ≃ β) : β → α := e.symm
run_cmd liftTermElabM <| do
successIfFail (getRawProjections .missing `FaultyUniverses.Equiv)
-- "Invalid custom projection:
-- fun {α} {β} e => (Equiv.symm e).toFun
-- Expression has different type than FaultyUniverses.Equiv.invFun. Given type:
-- {α : Type u} → {β : Type v} → α ≃ β → β → α
-- Expected type:
-- (α : Sort u) → (β : Sort v) → α ≃ β → β → α
-- Note: make sure order of implicit arguments is exactly the same."
end FaultyUniverses
namespace ManualUniverses
variable {α β γ : Sort _}
structure Equiv (α : Sort u) (β : Sort v) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => ManualUniverses.Equiv
instance : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv.toFun⟩
def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
/-- See Note [custom simps projection] -/
-- test: intentionally using different universe levels for Equiv.symm than for Equiv
def Equiv.Simps.invFun {α : Sort w} {β : Sort u} (e : α ≃ β) : β → α := e.symm
-- check whether we can generate custom projections even if the universe names don't match
initialize_simps_projections Equiv
end ManualUniverses
namespace ManualProjectionNames
structure Equiv (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => ManualProjectionNames.Equiv
variable {α β γ : Sort _}
instance : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv.toFun⟩
def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
/-- See Note [custom simps projection] -/
def Equiv.Simps.symm_apply (e : α ≃ β) : β → α := e.symm
initialize_simps_projections Equiv (toFun → apply, invFun → symm_apply)
run_cmd liftTermElabM <| do
let data ← getRawProjections .missing `ManualProjectionNames.Equiv
guard <| data.2.map (·.name) == #[`apply, `symm_apply]
@[simps +simpRhs]
protected def Equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β)⟩
example (e₁ : α ≃ β) (e₂ : β ≃ γ) (x : α) {z} (h : e₂ (e₁ x) = z) : (e₁.trans e₂) x = z := by
simp only [Equiv.trans_apply]; rw [h]
example (e₁ : α ≃ β) (e₂ : β ≃ γ) (x : γ) {z} (h : e₁.symm (e₂.symm x) = z) :
(e₁.trans e₂).symm x = z := by
simp only [Equiv.trans_symm_apply]; rw [h]
-- the new projection names are parsed correctly (the old projection names won't work anymore)
@[simps apply symm_apply] protected def Equiv.trans2 (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β)⟩
end ManualProjectionNames
namespace PrefixProjectionNames
structure Equiv (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => PrefixProjectionNames.Equiv
variable {α β γ : Sort _}
instance : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv.toFun⟩
def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
/-- See Note [custom simps projection] -/
def Equiv.Simps.symm_apply (e : α ≃ β) : β → α := e.symm
initialize_simps_projections Equiv (toFun → coe, as_prefix coe, invFun → symm_apply)
run_cmd liftTermElabM <| do
let data ← getRawProjections .missing `PrefixProjectionNames.Equiv
guard <| data.2.map (·.name) = #[`coe, `symm_apply]
guard <| data.2.map (·.isPrefix) = #[true, false]
@[simps +simpRhs]
protected def Equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β)⟩
example (e₁ : α ≃ β) (e₂ : β ≃ γ) (x : α) {z} (h : e₂ (e₁ x) = z) : (e₁.trans e₂) x = z := by
simp only [Equiv.coe_trans]
rw [h]
-- the new projection names are parsed correctly
@[simps coe symm_apply] protected def Equiv.trans2 (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β)⟩
-- it interacts somewhat well with multiple projections (though the generated name is not great)
@[simps! snd_coe_fst] def foo {α β γ δ : Type _} (x : α) (e₁ : α ≃ β) (e₂ : γ ≃ δ) :
α × (α × γ ≃ β × δ) :=
⟨x, Prod.map e₁ e₂, Prod.map e₁.symm e₂.symm⟩
example {α β γ δ : Type _} (x : α) (e₁ : α ≃ β) (e₂ : γ ≃ δ) (z : α × γ) {y} (h : e₁ z.1 = y) :
((foo x e₁ e₂).2 z).1 = y := by
simp only [coe_foo_snd_fst]
rw [h]
end PrefixProjectionNames
-- test transparency setting
structure SetPlus (α : Type) where
(s : Set α)
(x : α)
(h : x ∈ s)
@[simps] def Nat.SetPlus1 : SetPlus ℕ := ⟨Set.univ, 1, trivial⟩
example {x : Set ℕ} (h : Set.univ = x) : Nat.SetPlus1.s = x := by
dsimp only [Nat.SetPlus1_s]
rw [h]
@[simps (typeMd := .default)]
def Nat.SetPlus2 : SetPlus ℕ := ⟨Set.univ, 1, trivial⟩
example {x : Set ℕ} (h : Set.univ = x) : Nat.SetPlus2.s = x := by
fail_if_success { rw [h] }
exact h
@[simps (rhsMd := .default)]
def Nat.SetPlus3 : SetPlus ℕ := Nat.SetPlus1
example {x : Set ℕ} (h : Set.univ = x) : Nat.SetPlus3.s = x := by
dsimp only [Nat.SetPlus3_s]
rw [h]
namespace NestedNonFullyApplied
structure Equiv (α : Sort _) (β : Sort _) where
(toFun : α → β)
(invFun : β → α)
local infix:25 (priority := high) " ≃ " => NestedNonFullyApplied.Equiv
variable {α β γ : Sort _}
@[simps] def Equiv.symm (e : α ≃ β) : β ≃ α := ⟨e.invFun, e.toFun⟩
@[simps (rhsMd := .default) -fullyApplied]
def Equiv.symm2 : (α ≃ β) ≃ (β ≃ α) :=
⟨Equiv.symm, Equiv.symm⟩
example (e : α ≃ β) {x : β → α} (h : e.invFun = x) : (Equiv.symm2.invFun e).toFun = x := by
dsimp only [Equiv.symm2_invFun_toFun]
rw [h]
/- do not prematurely unfold `Equiv.symm`, unless necessary -/
@[simps (rhsMd := .default) toFun toFun_toFun] def Equiv.symm3 : (α ≃ β) ≃ (β ≃ α) :=
Equiv.symm2
-- this fails in Lean 4, not sure what is going on
-- example (e : α ≃ β) (y : β) : (Equiv.symm3.toFun e).toFun y = e.invFun y ∧
-- (Equiv.symm3.toFun e).toFun y = e.invFun y := by
-- constructor
-- { dsimp only [Equiv.symm3_toFun]
-- guard_target = e.symm.toFun y = e.invFun y
-- rfl }
-- { dsimp only [Equiv.symm3_toFun_toFun]
-- guard_target = e.invFun y = e.invFun y
-- rfl }
end NestedNonFullyApplied
-- test that type classes which are props work
class PropClass (n : ℕ) : Prop where
has_true : True
instance has_PropClass (n : ℕ) : PropClass n := ⟨trivial⟩
structure NeedsPropClass (n : ℕ) [PropClass n] where
(t : True)
@[simps] def test_PropClass : NeedsPropClass 1 :=
{ t := trivial }
/- check that when the coercion is given in eta-expanded form, we can also find the coercion. -/
structure AlgHom (R A B : Type _) where
(toFun : A → B)
instance (R A B : Type _) : CoeFun (AlgHom R A B) (fun _ ↦ A → B) := ⟨fun f ↦ f.toFun⟩
@[simps] def myAlgHom : AlgHom Unit Bool Bool :=
{ toFun := id }
example (x : Bool) {z} (h : id x = z) : myAlgHom x = z := by
simp only [myAlgHom_toFun]
rw [h]
structure RingHom (A B : Type _) where
toFun : A → B
instance (A B : Type _) : CoeFun (RingHom A B) (fun _ ↦ A → B) := ⟨fun f ↦ f.toFun⟩
@[simps] def myRingHom : RingHom Bool Bool :=
{ toFun := id }
example (x : Bool) {z} (h : id x = z) : myRingHom x = z := by
simp only [myRingHom_toFun]
rw [h]
/- check interaction with the `@[to_additive]` attribute -/
-- set_option trace.simps.debug true
@[to_additive (attr := simps)]
instance Prod.instMul {M N} [Mul M] [Mul N] : Mul (M × N) := ⟨fun p q ↦ ⟨p.1 * q.1, p.2 * q.2⟩⟩
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `Prod.mul_def |>.isSome
guard <| env.find? `Prod.add_def |>.isSome
-- hasAttribute `to_additive `Prod.instMul
-- hasAttribute `to_additive `Prod.mul_def
guard <| hasSimpAttribute env `Prod.mul_def
guard <| hasSimpAttribute env `Prod.add_def
example {M N} [Mul M] [Mul N] (p q : M × N) : p * q = ⟨p.1 * q.1, p.2 * q.2⟩ := by simp
example {M N} [Add M] [Add N] (p q : M × N) : p + q = ⟨p.1 + q.1, p.2 + q.2⟩ := by simp
/- The names of the generated simp lemmas for the additive version are not great if the definition
had a custom additive name -/
@[to_additive (attr := simps) my_add_instance]
instance my_instance {M N} [One M] [One N] : One (M × N) := ⟨(1, 1)⟩
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `my_instance_one |>.isSome
guard <| env.find? `my_add_instance_zero |>.isSome
-- hasAttribute `to_additive `my_instance -- todo
-- hasAttribute `to_additive `my_instance_one
guard <| hasSimpAttribute env `my_instance_one
guard <| hasSimpAttribute env `my_add_instance_zero
example {M N} [One M] [One N] : (1 : M × N) = ⟨1, 1⟩ := by simp
example {M N} [Zero M] [Zero N] : (0 : M × N) = ⟨0, 0⟩ := by simp
section
/-! Test `dsimp, simp` with the option `simpRhs` -/
attribute [local simp] Nat.add
structure MyType where
(A : Type)
@[simps +simpRhs] def myTypeDef : MyType :=
⟨{ _x : Fin (Nat.add 3 0) // 1 + 1 = 2 }⟩
-- todo: this fails in Lean 4, not sure what is going on
set_option linter.unusedVariables false in
example (h : false) (x y : { x : Fin (Nat.add 3 0) // 1 + 1 = 2 }) : myTypeDef.A = Unit := by
simp only [myTypeDef_A]
guard_target = { _x : Fin 3 // True } = Unit
/- note: calling only one of `simp` or `dsimp` does not produce the current target
as the following tests show. -/
fail_if_success { guard_hyp x : { _x : Fin 3 // true } }
dsimp at x
fail_if_success { guard_hyp x : { _x : Fin 3 // true } }
simp at y
fail_if_success { guard_hyp y : { _x : Fin 3 // true } }
simp at x
guard_hyp x : { _x : Fin 3 // True }
guard_hyp y : { _x : Fin 3 // True }
contradiction
-- test that `to_additive` works with a custom name
@[to_additive (attr := simps) some_test2]
def some_test1 (M : Type _) [CommMonoid M] : Subtype (fun _ : M ↦ True) := ⟨1, trivial⟩
run_cmd liftTermElabM <| do
let env ← getEnv
guard <| env.find? `some_test2_coe |>.isSome
end
/- Test custom compositions of projections. -/
section comp_projs
instance {α β} : CoeFun (α ≃ β) (fun _ ↦ α → β) := ⟨Equiv'.toFun⟩
@[simps] protected def Equiv'.symm {α β} (f : α ≃ β) : β ≃ α :=
⟨f.invFun, f, f.right_inv, f.left_inv⟩
structure DecoratedEquiv (α : Sort _) (β : Sort _) extends Equiv' α β where
(P_toFun : Function.Injective toFun)
(P_invFun : Function.Injective invFun)
instance {α β} : CoeFun (DecoratedEquiv α β) (fun _ ↦ α → β) := ⟨fun f ↦ f.toEquiv'⟩
def DecoratedEquiv.symm {α β : Sort _} (e : DecoratedEquiv α β) : DecoratedEquiv β α :=
{ toEquiv' := e.toEquiv'.symm
P_toFun := e.P_invFun
P_invFun := e.P_toFun }
def DecoratedEquiv.Simps.apply {α β : Sort _} (e : DecoratedEquiv α β) : α → β := e
def DecoratedEquiv.Simps.symm_apply {α β : Sort _} (e : DecoratedEquiv α β) : β → α := e.symm
initialize_simps_projections DecoratedEquiv (toFun → apply, invFun → symm_apply, -toEquiv')
@[simps] def foo (α : Type) : DecoratedEquiv α α :=
{ toFun := fun x ↦ x
invFun := fun x ↦ x
left_inv := fun _ ↦ rfl
right_inv := fun _ ↦ rfl
P_toFun := fun _ _ h ↦ h
P_invFun := fun _ _ h ↦ h }
example {α : Type} (x z : α) (h : x = z) : (foo α).symm x = z := by
dsimp
guard_target = x = z
rw [h]
@[simps! toEquiv' apply symm_apply] def foo2 (α : Type) : DecoratedEquiv α α :=
{ foo.rfl with
P_toFun := fun _ _ h ↦ h
P_invFun := fun _ _ h ↦ h }
example {α : Type} (x z : α) (h : foo.rfl x = z) : (foo2 α).toEquiv' x = z := by
dsimp only [foo2_toEquiv']
guard_target = foo.rfl x = z
rw [h]
example {α : Type} (x z : α) (h : x = z) : (foo2 α).toEquiv' x = z := by
dsimp only [foo2_apply]
guard_target = x = z
rw [h]
example {α : Type} (x z : α) (h : x = z) : foo2 α x = z := by
dsimp
guard_target = x = z
rw [h]
structure FurtherDecoratedEquiv (α : Sort _) (β : Sort _) extends DecoratedEquiv α β where
(Q_toFun : Function.Surjective toFun)
(Q_invFun : Function.Surjective invFun)
instance {α β} : CoeFun (FurtherDecoratedEquiv α β) (fun _ ↦ α → β) :=
⟨fun f ↦ f.toDecoratedEquiv⟩
def FurtherDecoratedEquiv.symm {α β : Sort _} (e : FurtherDecoratedEquiv α β) :
FurtherDecoratedEquiv β α :=
{ toDecoratedEquiv := e.toDecoratedEquiv.symm
Q_toFun := e.Q_invFun
Q_invFun := e.Q_toFun }
def FurtherDecoratedEquiv.Simps.apply {α β : Sort _} (e : FurtherDecoratedEquiv α β) : α → β := e
def FurtherDecoratedEquiv.Simps.symm_apply {α β : Sort _} (e : FurtherDecoratedEquiv α β) :
β → α := e.symm
initialize_simps_projections FurtherDecoratedEquiv
(toFun → apply, invFun → symm_apply, -toDecoratedEquiv, toEquiv' → toEquiv', -toEquiv')
@[simps] def ffoo (α : Type) : FurtherDecoratedEquiv α α :=
{ toFun := fun x ↦ x
invFun := fun x ↦ x
left_inv := fun _ ↦ rfl
right_inv := fun _ ↦ rfl
P_toFun := fun _ _ h ↦ h
P_invFun := fun _ _ h ↦ h
Q_toFun := fun y ↦ ⟨y, rfl⟩
Q_invFun := fun y ↦ ⟨y, rfl⟩ }
example {α : Type} (x z : α) (h : x = z) : (ffoo α).symm x = z := by
dsimp
guard_target = x = z
rw [h]
@[simps!] def ffoo3 (α : Type) : FurtherDecoratedEquiv α α :=
{ foo α with Q_toFun := fun y ↦ ⟨y, rfl⟩, Q_invFun := fun y ↦ ⟨y, rfl⟩ }
@[simps! apply toEquiv' toEquiv'_toFun toDecoratedEquiv_apply]
def ffoo4 (α : Type) : FurtherDecoratedEquiv α α :=
{ Q_toFun := fun y ↦ ⟨y, rfl⟩, Q_invFun := fun y ↦ ⟨y, rfl⟩, toDecoratedEquiv := foo α }
structure OneMore (α : Sort _) (β : Sort _) extends FurtherDecoratedEquiv α β
instance {α β} : CoeFun (OneMore α β) (fun _ ↦ α → β) :=
⟨fun f ↦ f.toFurtherDecoratedEquiv⟩
def OneMore.symm {α β : Sort _} (e : OneMore α β) :
OneMore β α :=
{ toFurtherDecoratedEquiv := e.toFurtherDecoratedEquiv.symm }
def OneMore.Simps.apply {α β : Sort _} (e : OneMore α β) : α → β := e
def OneMore.Simps.symm_apply {α β : Sort _} (e : OneMore α β) : β → α := e.symm
initialize_simps_projections OneMore (toFun → apply, invFun → symm_apply,
-toFurtherDecoratedEquiv, toDecoratedEquiv → to_dequiv, -to_dequiv)
@[simps] def fffoo (α : Type) : OneMore α α :=
{ toFun := fun x ↦ x
invFun := fun x ↦ x
left_inv := fun _ ↦ rfl
right_inv := fun _ ↦ rfl
P_toFun := fun _ _ h ↦ h
P_invFun := fun _ _ h ↦ h
Q_toFun := fun y ↦ ⟨y, rfl⟩
Q_invFun := fun y ↦ ⟨y, rfl⟩ }
example {α : Type} (x : α) : (fffoo α).symm x = x := by dsimp
@[simps! apply to_dequiv_apply toFurtherDecoratedEquiv_apply to_dequiv]
def fffoo2 (α : Type) : OneMore α α := fffoo α
/- test the case where a projection takes additional arguments. -/
variable {ι : Type _} [DecidableEq ι] (A : ι → Type _)
structure ZeroHom (M N : Type _) [Zero M] [Zero N] where
(toFun : M → N)
(map_zero' : toFun 0 = 0)
structure AddHom (M N : Type _) [Add M] [Add N] where
(toFun : M → N)
(map_add' : ∀ x y, toFun (x + y) = toFun x + toFun y)
structure AddMonoidHom (M N : Type _) [AddMonoid M] [AddMonoid N]
extends ZeroHom M N, AddHom M N
infixr:25 " →+ " => AddMonoidHom
instance (M N : Type _) [AddMonoid M] [AddMonoid N] : CoeFun (M →+ N) (fun _ ↦ M → N) := ⟨(·.toFun)⟩
class AddHomPlus [Add ι] [∀ i, AddCommMonoid (A i)] where
(myMul {i} : A i →+ A i)
def AddHomPlus.Simps.apply [Add ι] [∀ i, AddCommMonoid (A i)] [AddHomPlus A] {i : ι} (x : A i) :
A i :=
AddHomPlus.myMul x
initialize_simps_projections AddHomPlus (myMul_toFun → apply, -myMul)
class AddHomPlus2 [Add ι] where
(myMul {i j} : A i ≃ (A j ≃ A (i + j)))
def AddHomPlus2.Simps.mul [Add ι] [AddHomPlus2 A] {i j : ι} (x : A i) (y : A j) : A (i + j) :=
AddHomPlus2.myMul x y
initialize_simps_projections AddHomPlus2 (-myMul, myMul_toFun_toFun → mul)
attribute [ext] Equiv'
@[simps]
def thing (h : Bool ≃ (Bool ≃ Bool)) : AddHomPlus2 (fun _ : ℕ ↦ Bool) :=
{ myMul :=
{ toFun := fun b ↦
{ toFun := h b
invFun := (h b).symm
left_inv := (h b).left_inv
right_inv := (h b).right_inv }
invFun := h.symm
left_inv := h.left_inv -- definitional eta
right_inv := h.right_inv } } -- definitional eta
example (h : Bool ≃ (Bool ≃ Bool)) (i j : ℕ) (b1 b2 : Bool) {x} (h2 : h b1 b2 = x) :
@AddHomPlus2.myMul _ _ _ (thing h) i j b1 b2 = x := by
simp only [thing_mul]
rw [h2]
end comp_projs
section
/-! Check that the tactic also works if the elaborated type of `type` reduces to `Sort _`, but is
not `Sort _` itself. -/
structure MyFunctor (C D : Type _) where
(obj : C → D)
local infixr:26 " ⥤ " => MyFunctor
@[simps]
noncomputable def fooSum {I J : Type _} (C : I → Type _) {D : J → Type _} :
(∀ i, C i) ⥤ (∀ j, D j) ⥤ (∀ s : I ⊕ J, Sum.rec C D s) :=
{ obj := fun f ↦ { obj := fun g s ↦ Sum.rec f g s }}
end
/-! Test that we deal with classes whose names are prefixes of other classes -/
class MyDiv (α : Type _) extends Div α
class MyDivInv (α : Type _) extends MyDiv α
class MyGroup (α : Type _) extends MyDivInv α
initialize_simps_projections MyGroup
/-! Test that the automatic projection module doesn't throw an error if we have a projection name
unrelated to one of the classes. -/
class MyGOne {ι} [Zero ι] (A : ι → Type _) where
/-- The term `one` of grade 0 -/
one : A 0
initialize_simps_projections MyGOne
class Artificial (n : Nat) where
/-- The term `one` of grade 0 -/
one : Nat
initialize_simps_projections Artificial
namespace UnderScoreDigit
/-!
We do not consider `field` to be a prefix of `field_1`, as the latter is often
a different field with an auto-generated name.
-/
structure Foo where
field : Nat
field_9 : Nat × Nat
field_2 : Nat
@[simps field field_2 field_9_fst]
def myFoo : Foo := ⟨1, ⟨1, 1⟩, 1⟩
structure Prod (X Y : Type _) extends _root_.Prod X Y
structure Prod2 (X Y : Type _) extends toProd_1 : Prod X Y
initialize_simps_projections Prod2 (toProd → myName, toProd_1 → myOtherName)
structure Prod3 (X Y : Type _) extends toProd_1 : Prod X Y
@[simps] def foo : Prod3 Nat Nat := { fst := 1, snd := 3 }
@[simps toProd_1] def foo' : Prod3 Nat Nat := { fst := 1, snd := 3 }
end UnderScoreDigit
namespace Grind
@[simps (attr := grind =) -isSimp]
def foo := (2, 3)
example : foo.1 = 2 := by grind
example : foo.1 = 2 := by
fail_if_success simp
rfl
end Grind |
.lake/packages/mathlib/MathlibTest/derive_encodable.lean | import Mathlib.Tactic.DeriveEncodable
/-!
# Tests for the `Encodable` deriving handler
-/
/-!
Structures
-/
structure Struct where
x : Nat
y : Bool
deriving Encodable
example : Encodable Struct := inferInstance
/-!
Inductive types with parameters
-/
inductive T (α : Type) where
| a (x : α) (y : Bool) (z : T α)
| b
deriving Encodable, Repr
example : Encodable (T Nat) := inferInstance
/--
error: failed to synthesize
Encodable (ℕ → Bool)
Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.
-/
#guard_msgs in example : Encodable (Nat → Bool) := inferInstance
/-- info: 96964472478917 -/
#guard_msgs in
#eval Encodable.encode <| T.a 3 true T.b
/-- info: some (T.a 3 true (T.b)) -/
#guard_msgs in
#eval (Encodable.decode 96964472478917 : Option <| T Nat)
/-!
Mutually recursive types
-/
mutual
inductive T1 where
| a
| b (x : T1) (y : T2)
deriving Encodable
inductive T2 where
| c (n : Nat)
| d (x y : T1)
deriving Encodable
end
example : Encodable T1 := inferInstance
example : Encodable T2 := inferInstance
/-!
Not supported: indexed types
-/
/-- error: None of the deriving handlers for class `Encodable` applied to `Idx` -/
#guard_msgs in
inductive Idx : Nat → Type where
| a (i : Nat) (j : Nat) : Idx (i + j)
deriving Encodable
/-!
Not supported: nested inductive types
-/
/-- error: None of the deriving handlers for class `Encodable` applied to `Nested` -/
#guard_msgs in
inductive Nested where
| mk (xs : List Nested)
deriving Encodable
/-!
Not supported: reflexive inductive types
-/
/-- error: None of the deriving handlers for class `Encodable` applied to `Reflex` -/
#guard_msgs in
inductive Reflex where
| mk (f : Bool → Reflex)
deriving Encodable |
.lake/packages/mathlib/MathlibTest/tfae.lean | import Mathlib.Tactic.TFAE
import Mathlib.Tactic.SuccessIfFailWithMsg
open List
set_option autoImplicit true
section zeroOne
example : TFAE [] := by tfae_finish
example : TFAE [P] := by tfae_finish
end zeroOne
namespace two
axiom P : Prop
axiom Q : Prop
axiom pq : P → Q
axiom qp : Q → P
example : TFAE [P, Q] := by
tfae_have 1 → 2 := pq
tfae_have 2 → 1 := qp
tfae_finish
example : TFAE [P, Q] := by
tfae_have 1 ↔ 2 := Iff.intro pq qp
tfae_finish
example : TFAE [P, Q] := by
tfae_have 2 ← 1 := pq
guard_hyp tfae_2_from_1 : P → Q
tfae_have 1 ← 2 := qp
tfae_finish
end two
namespace three
axiom P : Prop
axiom Q : Prop
axiom R : Prop
axiom pq : P → Q
axiom qr : Q → R
axiom rp : R → P
example : TFAE [P, Q, R] := by
tfae_have 1 → 2 := pq
tfae_have 2 → 3 := qr
tfae_have 3 → 1 := rp
tfae_finish
example : TFAE [P, Q, R] := by
tfae_have 1 ↔ 2 := Iff.intro pq (rp ∘ qr)
tfae_have 3 ↔ 2 := Iff.intro (pq ∘ rp) qr
tfae_finish
example : TFAE [P, Q, R] := by
tfae_have 1 → 2 := pq
tfae_have 2 → 1 := rp ∘ qr
tfae_have 2 ↔ 3 := Iff.intro qr (pq ∘ rp)
tfae_finish
end three
section seven
axiom P₁ : Prop
axiom P₂ : Prop
axiom P₃ : Prop
axiom P₄ : Prop
axiom P₅ : Prop
axiom P₆ : Prop
axiom P₇ : Prop
axiom h₁ : P₁ ↔ P₂
axiom h₂ : P₁ → P₆
axiom h₃ : P₆ → P₇
axiom h₄ : P₇ → P₄
axiom h₅ : P₄ → P₅
axiom h₆ : P₅ → P₃
axiom h₇ : P₃ → P₂
example : TFAE [P₁, P₂, P₃, P₄, P₅, P₆, P₇] := by
tfae_have test : 1 ↔ 2 := h₁
guard_hyp test : _
tfae_have 1 → 6 := h₂
tfae_have 6 → 7 := h₃
tfae_have 7 → 4 := h₄
tfae_have 4 → 5 := h₅
tfae_have 5 → 3 := h₆
tfae_have 3 → 2 := h₇
tfae_finish
end seven
section context
example (n : ℕ) : List.TFAE [n = 1, n + 1 = 2] := by
generalize n = m
tfae_have 1 ↔ 2 := by simp
tfae_finish
example (h₁ : P → Q) (h₂ : Q → P) : TFAE [P, Q] := by
tfae_finish
end context
section term
axiom P : Prop
axiom Q : Prop
axiom pq : P → Q
axiom qp : Q → P
example : TFAE [P, Q] := by
tfae_have h : 1 → 2 := pq
guard_hyp h : P → Q
tfae_have _ : 1 ← 2 := qp
tfae_finish
example : TFAE [P, Q] := by
have n : ℕ := 4
tfae_have 1 → 2 := by
guard_hyp n : ℕ -- hypotheses are accessible (context is correct)
guard_target =ₛ P → Q -- expected type is known
exact pq
tfae_have 1 ← 2 := qp
tfae_finish
example : TFAE [P, Q] := by
have n : ℕ := 3
tfae_have 2 ← 1 := fun p => ?Qgoal
case Qgoal => exact pq p
refine ?a
fail_if_success (tfae_have 1 ← 2 := ((?a).out 1 2 sorry sorry).mpr)
tfae_have 2 → 1 := qp
tfae_finish
example : TFAE [P, Q] := by
tfae_have 1 → 2
| p => pq p
tfae_have 2 → 1
| q => qp q
tfae_finish
example : TFAE [P, Q] := by
tfae_have ⟨mp, mpr⟩ : 1 ↔ 2 := ⟨pq, qp⟩
tfae_finish
end term
section deprecation
/-! This section tests the deprecation of "goal-style" `tfae_have` syntax. -/
/--
warning: "Goal-style" syntax 'tfae_have 1 → 2' is deprecated in favor of 'tfae_have 1 → 2 := ...'.
To turn this warning off, use set_option Mathlib.Tactic.TFAE.useDeprecated true
-/
#guard_msgs in
example : TFAE [P, Q] := by
tfae_have 1 → 2; exact pq
tfae_have 2 → 1 := qp
tfae_finish
set_option Mathlib.Tactic.TFAE.useDeprecated true in
example : TFAE [P, Q] := by
tfae_have 1 → 2; exact pq
tfae_have 2 → 1 := qp
tfae_finish
end deprecation |
.lake/packages/mathlib/MathlibTest/Rename.lean | import Mathlib.Tactic.Rename
example (a : Nat) (b : Int) : Int × Nat := by
rename' a => c, b => d
exact (d, c)
example (a : Nat) (b : Int) : Int × Nat := by
rename' a => b, b => a
exact (a, b) |
.lake/packages/mathlib/MathlibTest/propose.lean | import Mathlib.Tactic.Propose
import Mathlib.Tactic.GuardHypNums
import Mathlib.Algebra.Ring.Associated
import Mathlib.Data.Set.Subsingleton
import Batteries.Data.List.Lemmas
-- For debugging, you may find these options useful:
-- set_option trace.Tactic.propose true
-- set_option trace.Meta.Tactic.solveByElim true
set_option autoImplicit true
set_option linter.unusedVariables false
theorem foo (L M : List α) (w : L.Disjoint M) (m : a ∈ L) : a ∉ M := fun h => w m h
/--
info: Try this:
[apply] have : M.Disjoint L := List.disjoint_symm w
---
info: Try this:
[apply] have : K.Disjoint M := List.disjoint_of_subset_left m w
-/
#guard_msgs in
example (K L M : List α) (w : L.Disjoint M) (m : K ⊆ L) : True := by
have? using w
-- have : List.Disjoint K M := List.disjoint_of_subset_left m w
-- have : List.Disjoint M L := List.disjoint_symm w
trivial
/--
info: Try this:
[apply] have : K.Disjoint M := List.disjoint_of_subset_left m w
---
info: Try this:
[apply] have : K.Disjoint M := List.disjoint_of_subset_left m w
-/
#guard_msgs in
example (K L M : List α) (w : L.Disjoint M) (m : K ⊆ L) : True := by
have? using w, m
-- have : List.Disjoint K M := List.disjoint_of_subset_left m w
have?! using w, m
guard_hyp List.disjoint_of_subset_left : List.Disjoint K M :=
_root_.List.disjoint_of_subset_left m w
fail_if_success
have : M.Disjoint L := by assumption
have : K.Disjoint M := by assumption
trivial
def bar (n : Nat) (x : String) : Nat × String := (n + x.length, x)
/--
info: Try this:
[apply] let a : ℕ × String := bar p.1 p.2
---
info: Try this:
[apply] let _ : ℕ × String := bar p.1 p.2
-/
#guard_msgs in
set_option maxHeartbeats 400000 in
example (p : Nat × String) : True := by
fail_if_success have? using p
have? a : Nat × String using p.1, p.2
have? : Nat × _ using p.1, p.2
trivial
/--
info: Try this:
[apply] have : M.Disjoint L := List.disjoint_symm w
---
info: Try this:
[apply] have : a ∉ M := foo L M w m
-/
#guard_msgs in
example (_K L M : List α) (w : L.Disjoint M) (m : a ∈ L) : True := by
have?! using w
guard_hyp List.disjoint_symm : List.Disjoint M L := _root_.List.disjoint_symm w
have : a ∉ M := by assumption
trivial
/--
info: Try this:
[apply] have : IsUnit p := isUnit_of_dvd_one h
---
info: Try this:
[apply] have : ¬IsUnit p := not_unit hp
---
info: Try this:
[apply] have : p ∣ p * p ↔ p ∣ p ∨ p ∣ p := Prime.dvd_mul hp
---
info: Try this:
[apply] have : p ∣ p ∨ p ∣ p := dvd_or_dvd hp (Exists.intro p (Eq.refl (p * p)))
---
info: Try this:
[apply] have : ¬p ∣ 1 := not_dvd_one hp
---
info: Try this:
[apply] have : IsPrimal p := isPrimal hp
---
info: Try this:
[apply] have : p ≠ 0 := ne_zero hp
---
info: Try this:
[apply] have : p ≠ 1 := ne_one hp
-/
#guard_msgs in
-- From Mathlib.Algebra.Associated:
variable {α : Type} [CommMonoidWithZero α] in
open Prime in
theorem dvd_of_dvd_pow (hp : Prime p) {a : α} {n : ℕ} (h : p ∣ a ^ n) : p ∣ a := by
induction n with
| zero =>
rw [pow_zero] at h
-- In mathlib, we proceed by two `have` statements:
-- have := isUnit_of_dvd_one h
-- have := not_unit hp
-- `propose!` successfully guesses them both:
have?! using h
guard_hyp isUnit_of_dvd_one : IsUnit p := _root_.isUnit_of_dvd_one h
have?! using hp
guard_hyp Prime.not_unit : ¬IsUnit p := not_unit hp
contradiction
| succ n ih =>
rw [pow_succ'] at h
obtain dvd_a | dvd_pow := dvd_or_dvd hp h
· assumption
exact ih dvd_pow |
.lake/packages/mathlib/MathlibTest/antidiagonal.lean | import Mathlib.Algebra.Order.Antidiag.Finsupp
import Mathlib.Data.Finset.Sort
import Mathlib.Data.Finsupp.Notation
import Mathlib.Data.Fin.Tuple.NatAntidiagonal
/-!
# Testing computability (and runtime) of antidiagonal
-/
open Finset
section
-- set_option trace.profiler true
set_option linter.style.commandStart false
-- `antidiagonalTuple` is faster than `finAntidiagonal` by a small constant factor
/-- info: 23426 -/
#guard_msgs in #eval #(finAntidiagonal 4 50)
/-- info: 23426 -/
#guard_msgs in #eval #(Finset.Nat.antidiagonalTuple 4 50)
end
/--
info: {fun₀ | "C" => 3,
fun₀ | "B" => 1 | "C" => 2,
fun₀ | "B" => 2 | "C" => 1,
fun₀ | "B" => 3,
fun₀ | "A" => 1 | "C" => 2,
fun₀ | "A" => 1 | "B" => 1 | "C" => 1,
fun₀ | "A" => 1 | "B" => 2,
fun₀ | "A" => 2 | "C" => 1,
fun₀ | "A" => 2 | "B" => 1,
fun₀ | "A" => 3}
-/
#guard_msgs in
#eval finsuppAntidiag {"A", "B", "C"} 3 |
.lake/packages/mathlib/MathlibTest/DocPrime.lean | import Mathlib.Tactic.Linter.DocPrime
import Mathlib.Tactic.Lemma
set_option linter.docPrime true
-- no warning on a primed-declaration with a doc-string containing `'`
/-- X' has a doc-string -/
def X' := 0
-- no warning on a declaration whose name contains a `'` *and does not end with it*
def X'X := 0
-- A list of universe names in the declaration is handled correctly, i.e. warns.
/--
warning: `Y'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
def Y'.{u} := ULift.{u} Nat
namespace X
/--
warning: `ABC.thm_no_doc1'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
theorem _root_.ABC.thm_no_doc1' : True := .intro
/--
warning: `X.thm_no_doc2'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
theorem thm_no_doc2' : True := .intro
end X
/--
warning: `thm_no_doc'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
theorem thm_no_doc' : True := .intro
/--
warning: `thm_with_attr_no_doc'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
@[simp]
theorem thm_with_attr_no_doc' : True := .intro
/--
warning: `inst_no_doc'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
instance inst_no_doc' : True := .intro
/--
warning: `abbrev_no_doc'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
abbrev abbrev_no_doc' : True := .intro
/--
warning: `def_no_doc'` is missing a doc-string, please add one.
Declarations whose name ends with a `'` are expected to contain an explanation for the presence of a `'` in their doc-string. This may consist of discussion of the difference relative to the unprimed version, or an explanation as to why no better naming scheme is possible.
Note: This linter can be disabled with `set_option linter.docPrime false`
-/
#guard_msgs in
def def_no_doc' : True := .intro
-- Anonymous declarations in a primed namespace should not get flagged by the linter.
namespace Foo'
example : True := .intro
instance : True := .intro
end Foo' |
.lake/packages/mathlib/MathlibTest/ArithMult.lean | import Mathlib.NumberTheory.ArithmeticFunction
open ArithmeticFunction
-- access notation `ζ`, `μ` and `σ`
open scoped zeta Moebius sigma
variable {R : Type*} [Field R]
set_option linter.unusedVariables false
example : IsMultiplicative μ := by arith_mult
example : IsMultiplicative (ζ * ζ) := by arith_mult
example {R : Type*} [Field R] (f : ArithmeticFunction R) (hf : IsMultiplicative f) :
IsMultiplicative ((ζ : ArithmeticFunction R).pdiv f) := by arith_mult
example (f g : ArithmeticFunction R) (hf : IsMultiplicative f) :
IsMultiplicative (prodPrimeFactors g |>.pmul f) := by arith_mult
example (n : ℕ) : IsMultiplicative <| (σ n * pow (n + 3)).ppow 2 := by arith_mult |
.lake/packages/mathlib/MathlibTest/interval_cases.lean | import Mathlib.Tactic.IntervalCases
import Mathlib.Tactic.Set
set_option linter.unusedVariables false in
example (n : ℕ) : True := by
fail_if_success interval_cases n
trivial
example (n : ℕ) (_ : 2 ≤ n) : True := by
fail_if_success interval_cases n
trivial
example (n m : ℕ) (_ : n ≤ m) : True := by
fail_if_success interval_cases n
trivial
example (n : ℕ) (w₂ : n < 0) : False := by interval_cases n
example (n : ℕ) (w₂ : n < 1) : n = 0 := by
interval_cases n
rfl -- done for free in the mathlib3 version
example (n : ℕ) (w₂ : n < 2) : n = 0 ∨ n = 1 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : 1 ≤ n) (w₂ : n < 3) : n = 1 ∨ n = 2 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : 1 ≤ n) (w₂ : n < 3) : n = 1 ∨ n = 2 := by
interval_cases using w₁, w₂
· left; rfl
· right; rfl
-- make sure we only pick up bounds on the specified variable:
example (n m : ℕ) (w₁ : 1 ≤ n) (w₂ : n < 3) (_ : m < 2) : n = 1 ∨ n = 2 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : 1 < n) (w₂ : n < 4) : n = 2 ∨ n = 3 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : n ≥ 3) (w₂ : n < 5) : n = 3 ∨ n = 4 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₀ : n ≥ 2) (w₁ : n ≥ 3) (w₂ : n < 5) : n = 3 ∨ n = 4 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : n > 2) (w₂ : n < 5) : n = 3 ∨ n = 4 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : n > 2) (w₂ : n ≤ 4) : n = 3 ∨ n = 4 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (w₁ : 2 < n) (w₂ : 4 ≥ n) : n = 3 ∨ n = 4 := by
interval_cases n
· left; rfl
· right; rfl
example (n : ℕ) (h1 : 4 < n) (h2 : n ≤ 6) : n < 20 := by
interval_cases n
· guard_target =ₛ 5 < 20; norm_num
· guard_target =ₛ 6 < 20; norm_num
example (n : ℕ) (w₁ : n % 3 < 1) : n % 3 = 0 := by
interval_cases h : n % 3
· guard_hyp h : n % 3 = 0
rfl
example (n : ℕ) (w₁ : n % 3 < 1) : n % 3 = 0 := by
interval_cases n % 3
rfl
-- the Lean 3 version had a different goal state after the `interval_cases`
-- the `n % 3` was not substituted, instead there was a hypothesis `h : n % 3 = 0` provided
-- so the proof was:
-- assumption
example (n : ℕ) (h1 : 4 ≤ n) (h2 : n < 10) : n < 20 := by
interval_cases using h1, h2
all_goals { norm_num }
-- example (n : ℕ+) (w₂ : n < 1) : False := by interval_cases n
-- example (n : ℕ+) (w₂ : n < 2) : n = 1 := by interval_cases n
-- example (n : ℕ+) (h1 : 4 ≤ n) (h2 : n < 5) : n = 4 := by interval_cases n
-- example (n : ℕ+) (w₁ : 2 < n) (w₂ : 4 ≥ n) : n = 3 ∨ n = 4 := by
-- interval_cases n,
-- { guard_target' (3 : ℕ+) = 3 ∨ (3 : ℕ+) = 4, left; rfl },
-- { guard_target' (4 : ℕ+) = 3 ∨ (4 : ℕ+) = 4, right; rfl },
-- example (n : ℕ+) (w₁ : 1 < n) (w₂ : n < 4) : n = 2 ∨ n = 3 := by
-- { interval_cases n, { left; rfl }, { right; rfl }, }
-- example (n : ℕ+) (w₂ : n < 3) : n = 1 ∨ n = 2 := by
-- { interval_cases n, { left; rfl }, { right; rfl }, }
-- example (n : ℕ+) (w₂ : n < 4) : n = 1 ∨ n = 2 ∨ n = 3 := by
-- { interval_cases n, { left; rfl }, { right, left; rfl }, { right, right; rfl }, }
example (z : ℤ) (h1 : z ≥ -3) (h2 : z < 2) : z < 20 := by
interval_cases using h1, h2
all_goals { norm_num }
example (z : ℤ) (h1 : z ≥ -3) (h2 : z < 2) : z < 20 := by
interval_cases z
· guard_target =ₛ (-3 : ℤ) < 20
norm_num
· guard_target =ₛ (-2 : ℤ) < 20
norm_num
· guard_target =ₛ (-1 : ℤ) < 20
norm_num
· guard_target =ₛ (0 : ℤ) < 20
norm_num
· guard_target =ₛ (1 : ℤ) < 20
norm_num
example (n : ℕ) : n % 2 = 0 ∨ n % 2 = 1 := by
set r := n % 2 with hr
have h2 : r < 2 := by
exact Nat.mod_lt _ (by decide)
interval_cases hrv : r
· left; exact hrv.symm.trans hrv
--^ hover says `hrv : r = 0` and jumps to `hrv :` above
· right; exact hrv.symm.trans hrv
--^ hover says `hrv : r = 1` and jumps to `hrv :` above
/- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/interval_cases.20bug -/
example {x : ℕ} (hx2 : x < 2) (h : False) : False := by
have _this : x ≤ 1 := by
-- `interval_cases` deliberately not focused,
-- this is testing that the `interval_cases` only acts on `have` side goal, not on both
interval_cases x
· exact zero_le_one
· rfl -- done for free in the mathlib3 version
exact h
/-
In Lean 3 this one didn't work! It reported:
`deep recursion was detected at 'expression equality test'`
-/
example (n : ℕ) (w₁ : n > 1000000) (w₁ : n < 1000002) : n < 2000000 := by
interval_cases n
norm_num
section
variable (d : ℕ)
example (h : d ≤ 0) : d = 0 := by
interval_cases d
rfl
end |
.lake/packages/mathlib/MathlibTest/factors.lean | import Mathlib.Tactic.Simproc.Factors
example : Nat.primeFactorsList 0 = [] := by simp only [Nat.primeFactorsList_ofNat]
example : Nat.primeFactorsList 1 = [] := by simp only [Nat.primeFactorsList_ofNat]
example : Nat.primeFactorsList 2 = [2] := by simp only [Nat.primeFactorsList_ofNat]
example : Nat.primeFactorsList 3 = [3] := by simp only [Nat.primeFactorsList_ofNat]
example : Nat.primeFactorsList 4 = [2, 2] := by simp only [Nat.primeFactorsList_ofNat]
example : Nat.primeFactorsList 12 = [2, 2, 3] := by simp only [Nat.primeFactorsList_ofNat]
example : Nat.primeFactorsList 221 = [13, 17] := by simp only [Nat.primeFactorsList_ofNat] |
.lake/packages/mathlib/MathlibTest/HigherOrder.lean | import Mathlib.Tactic.HigherOrder
set_option autoImplicit true
namespace HigherOrderTest
@[higher_order map_comp_pure]
theorem map_pure' {f : Type u → Type v} [Applicative f] [LawfulApplicative f]
{α β : Type u} (g : α → β) (x : α) : g <$> (pure x : f α) = pure (g x) := map_pure g x
example {f : Type u → Type v} [Applicative f] [LawfulApplicative f]
{α β : Type u} (g : α → β) : Functor.map g ∘ (pure : α → f α) = pure ∘ g := by
apply map_comp_pure
end HigherOrderTest |
.lake/packages/mathlib/MathlibTest/BinPow.lean | import Mathlib.Data.ZMod.Defs
-- A 1024-bit prime number
set_option linter.style.longLine false in
abbrev M : Nat := 0xb10b8f96a080e01dde92de5eae5d54ec52c99fbcfb06a3c69a6a9dca52d23b616073e28675a23d189838ef1e2ee652c013ecb4aea906112324975c3cd49b83bfaccbdd7d90c4bd7098488e9c219a73724effd6fae5644738faa31a4ff55bccc0a151af5f0dc8b4bd45bf37df365c1a65e68cfda76d4da708df1fb2bc2e4a4371
set_option linter.style.longLine false in
abbrev g : Nat := 0xa4d1cbd5c3fd34126765a442efb99905f8104dd258ac507fd6406cff14266d31266fea1e5c41564b777e690f5504f213160217b4b01b886a5e91547f9e2749f4d7fbd7d3b9a92ee1909d0d2263f80a76a6a24c087a091f531dbf0a0169b6a28ad662a4d18e73afa32d779d5918d08bc8858f4dcef97c2a24855e6eeb22b3b2e5
-- This should evaluate within a few seconds compared to never(ish) previously
/-- info: 1 -/
#guard_msgs in
#eval (g : ZMod M) ^ (M - 1) |
.lake/packages/mathlib/MathlibTest/MaxPowDiv.lean | import Mathlib.NumberTheory.Padics.PadicVal.Basic
/--
info: 100000
-/
#guard_msgs in
/- Previously this would hang -/
#eval padicValNat 2 (2 ^ 100000) |
.lake/packages/mathlib/MathlibTest/irreducibleDef.lean | import Mathlib.Tactic.IrreducibleDef
import Mathlib.Util.WhatsNew
set_option autoImplicit true
/-- Add two natural numbers, but not during unification. -/
irreducible_def frobnicate (a b : Nat) :=
a + b
example : frobnicate a 0 = a := by
simp [frobnicate]
example : frobnicate a 0 = a :=
frobnicate_def a 0
irreducible_def justAsArbitrary (lemma := myLemma) [Inhabited α] : α :=
default
example : justAsArbitrary = 0 := myLemma
irreducible_def withoutType := 42
irreducible_def withEquations : Nat → Nat
| 0 => 42
| _n + 1 => 314
irreducible_def withUniv.{u, v} := (Type v, Type u)
example : withUniv.{u, v} = (Type v, Type u) := by rw [withUniv]
namespace Foo
protected irreducible_def foo : Nat := 42
end Foo
example : Foo.foo = 42 := by rw [Foo.foo]
protected irreducible_def Bar.bar : Nat := 42
protected noncomputable irreducible_def Nat.evenMoreArbitrary : Nat :=
Classical.choice inferInstance
private irreducible_def Real.zero := 42
example : Real.zero = 42 := Real.zero_def
irreducible_def y : Nat := let x := 42; x
example : y = 42 := @y_def |
.lake/packages/mathlib/MathlibTest/Change.lean | import Mathlib.Tactic.Change
set_option linter.style.setOption false
set_option pp.unicode.fun true
set_option autoImplicit true
/--
info: Try this:
[apply] change 0 = 1
---
info: Try this:
[apply] change (fun x ↦ x) 0 = 1
---
info: Try this:
[apply] change (fun x ↦ x) 0 = 1
---
error: The term
1 = 0
is not defeq to the goal:
(fun x ↦ x) 0 = 1
-/
#guard_msgs in
example : (fun x : Nat => x) 0 = 1 := by
change? 0 = _ -- change 0 = 1
change? -- change (fun x ↦ x) 0 = 1
change? _ -- change (fun x ↦ x) 0 = 1
change? 1 = 0
-- The term
-- 1 = 0
-- is not defeq to the goal:
-- (fun x ↦ x) 0 = 1 |
.lake/packages/mathlib/MathlibTest/CountHeartbeats.lean | import Mathlib.Util.CountHeartbeats
import Mathlib.Util.SleepHeartbeats
/-- info: Used approximately 0 heartbeats, which is less than the current maximum of 200000. -/
#guard_msgs in
#count_heartbeats approximately in
example (a : Nat) : a = a := rfl
/-- info: Used approximately 0 heartbeats, which is less than the minimum of 200000. -/
#guard_msgs in
guard_min_heartbeats approximately in
example (a : Nat) : a = a := rfl
/-- info: Used approximately 0 heartbeats, which is less than the minimum of 2000. -/
#guard_msgs in
guard_min_heartbeats approximately 2000 in
example (a : Nat) : a = a := rfl
guard_min_heartbeats approximately 1 in
example (a : Nat) : a = a := rfl
/-!
# Tests for the `countHeartbeats` linter
-/
section using_count_heartbeats
-- sets the `countHeartbeats` both linter option and the `approximate` option to `true`
#count_heartbeats approximately
mutual -- mutual declarations get ignored
theorem XY : True := trivial
end
/-- info: Used approximately 1000 heartbeats, which is less than the current maximum of 200000. -/
#guard_msgs in
-- we use two nested `set_option ... in` to test that the `heartBeats` linter enters both.
set_option linter.unusedTactic false in
set_option linter.unusedTactic false in
example : True := by
sleep_heartbeats 1000 -- on top of these heartbeats, a few more are used by the rest of the proof
trivial
/-- info: Used approximately 0 heartbeats, which is less than the current maximum of 200000. -/
#guard_msgs in
example : True := trivial
/-- info: 'YX' used approximately 0 heartbeats, which is less than the current maximum of 200000. -/
#guard_msgs in
set_option linter.unusedTactic false in
set_option linter.unusedTactic false in
theorem YX : True := trivial
end using_count_heartbeats
section using_linter_option
set_option linter.countHeartbeats true
set_option linter.countHeartbeatsApprox true
mutual -- mutual declarations get ignored
theorem XY' : True := trivial
end
/-- info: Used approximately 0 heartbeats, which is less than the current maximum of 200000. -/
#guard_msgs in
-- we use two nested `set_option ... in` to test that the `heartBeats` linter enters both.
set_option linter.unusedTactic false in
set_option linter.unusedTactic false in
example : True := trivial
/-- info: Used approximately 0 heartbeats, which is less than the current maximum of 200000. -/
#guard_msgs in
example : True := trivial
/--
info: 'YX'' used approximately 0 heartbeats, which is less than the current maximum of 200000.
-/
#guard_msgs in
set_option linter.unusedTactic false in
set_option linter.unusedTactic false in
theorem YX' : True := trivial
end using_linter_option |
.lake/packages/mathlib/MathlibTest/apply_fun.lean | import Mathlib.Data.Nat.Notation
import Mathlib.Tactic.Basic
import Mathlib.Tactic.ApplyFun
import Mathlib.LinearAlgebra.Matrix.ConjTranspose
import Mathlib.Logic.Function.Defs
private axiom test_sorry : ∀ {α}, α
set_option autoImplicit true
set_option linter.unusedVariables false
open Function
example (f : ℕ → ℕ) (h : f x = f y) : x = y := by
apply_fun f
· guard_target = f x = f y
assumption
· guard_target = Injective f
exact test_sorry
example (f : ℕ → ℕ → ℕ) (h : f 1 x = f 1 y) (hinj : ∀ n, Injective (f n)) : x = y := by
apply_fun f ?foo
guard_target = f ?foo x = f ?foo y
case foo => exact 1
· exact h
· apply hinj
-- Uses `refine`-style rules for placeholders:
example (f : ℕ → ℕ → ℕ) : x = y := by
fail_if_success apply_fun f _
exact test_sorry
example (f : ℕ → ℕ → ℕ) (h : f 1 x = f 1 y) (hinj : Injective (f 1)) : x = y := by
apply_fun f _ using hinj
-- Solves for the hole using unification since it makes use of the `using` clause.
guard_target = f 1 x = f 1 y
assumption
-- A test to show a perhaps unexpected consequence of how injectivity is auto-proved:
example (f : ℕ → ℕ → ℕ) (h : f 1 x = f 1 y) (hinj : Injective (f 1)) : x = y := by
apply_fun f _
-- Solves for the hole using unification since `hinj` is pulled in by `assumption`.
guard_target = f 1 x = f 1 y
assumption
-- A test to show a perhaps unexpected consequence of how injectivity is auto-proved:
example (f : ℕ → ℕ) (h : f x = f y) (hinj : Injective f) : x = y := by
apply_fun _
guard_target = f x = f y
assumption
-- Make sure named holes generate new goals for `≠`
example (f : ℕ → ℕ → ℕ) (h : f 1 x ≠ f 1 y) : x ≠ y := by
apply_fun f ?foo
guard_target = f ?foo x ≠ f ?foo y
case foo => exact 1
assumption
example (X Y Z : Type) (f : X → Y) (g : Y → Z) (H : Injective <| g ∘ f) : Injective f := by
intro x x' h
apply_fun g at h
exact H h
example (x : Int) (h : x = 1) : 1 = 1 := by
apply_fun (fun p => p) at h
rfl
example (a b : Int) (h : a = b) : a + 1 = b + 1 := by
-- Make sure that we infer the type of the function only after we see the hypothesis:
apply_fun (fun n => n + 1) at h
-- check that `h` was β-reduced
guard_hyp h :ₛ a + 1 = b + 1
exact h
-- Verify failure when applying a dependently typed function.
example (P : Nat → Type) (Q : (n : Nat) -> P n) (a b : Nat) (h : a = b) : True := by
fail_if_success apply_fun Q at h
trivial
example (f : ℕ → ℕ) (a b : ℕ) (monof : Monotone f) (h : a ≤ b) : f a ≤ f b := by
apply_fun f at h using monof
assumption
example (f : ℕ → ℕ) (a b : ℕ) (monof : Monotone f) (h : a ≤ b) : f a ≤ f b := by
apply_fun f at h
· assumption
· assumption
example (n m : ℕ) (f : ℕ → ℕ) (h : f n ≠ f m) : n ≠ m := by
apply_fun f
exact h
example (n m : ℕ) (f : ℕ ≃ ℕ) (h : f n ≠ f m) : n ≠ m := by
apply_fun f
exact h
example (n m : ℕ) (f : ℕ → ℕ) (w : Function.Injective f) (h : f n = f m) : n = m := by
apply_fun f
assumption
example (n m : ℕ) (f : ℕ → ℕ) (w : Function.Injective f) (h : f n = f m) : n = m := by
apply_fun f using w
assumption
example (n m : ℕ) (f : ℕ → ℕ) (w : Function.Injective f ∧ true) (h : f n = f m) : n = m := by
apply_fun f using w.1
assumption
example (f : ℕ ≃ ℕ) (h : f x = f y) : x = y := by
apply_fun f
assumption
example (f : ℕ ≃ ℕ) (h : f x = f y) : x = y := by
apply_fun f using f.injective
assumption
example {x y : ℕ} (h : Equiv.refl ℕ x = Equiv.refl ℕ y) : x = y := by
apply_fun Equiv.refl ℕ
assumption
example (a b : List α) (P : a = b) : True := by
apply_fun List.length at P
trivial
example (a b : ℕ) (h : a ≤ b) : a + 1 ≤ b + 1 := by
apply_fun (· + 1 : ℕ → ℕ) at h -- TODO shouldn't need type ascription here
· exact h
· exact Monotone.add_const monotone_id 1
example (a b : ℕ) (h : a < b) : a + 1 < b + 1 := by
apply_fun (· + 1 : ℕ → ℕ) at h
· exact h
· exact StrictMono.add_const strictMono_id 1
example (a b : ℕ) (h : a < b) : a + 1 < b + 1 := by
apply_fun (· + 1 : ℕ → ℕ) at h using StrictMono.add_const strictMono_id 1
· exact h
example (a b : ℕ) (h : a ≠ b) : a + 1 ≠ b + 1 := by
apply_fun (· + 1 : ℕ → ℕ) at h
· exact h
· exact add_left_injective 1
-- TODO
-- -- monotonicity will be proved by `mono` in the next example
-- example (a b : ℕ) (h : a ≤ b) : a + 1 ≤ b + 1 := by
-- apply_fun (fun n ↦ n+1) at h
-- exact h
example {n : Type} [Fintype n] {X : Type} [Semiring X]
(f : Matrix n n X → Matrix n n X) (A B : Matrix n n X) (h : A * B = 0) : f (A * B) = f 0 := by
apply_fun f at h
-- check that our β-reduction didn't mess things up:
-- (previously `apply_fun` was producing `f (A.mul B) = f 0`)
guard_hyp h :ₛ f (A * B) = f 0
exact h
-- TODO
-- -- Verify that `apply_fun` works with `Fin.castSucc`, even though it has an implicit argument.
-- example (n : ℕ) (a b : Fin n) (H : a ≤ b) : a.castSucc ≤ b.castSucc :=
-- apply_fun Fin.castSucc at H
-- exact H
example (n m : ℕ) (f : ℕ ≃ ℕ) (h : f n = f m) : n = m := by
apply_fun f
assumption
example (n m : ℕ) (f : ℕ ≃o ℕ) (h : f n ≤ f m) : n ≤ m := by
apply_fun f
assumption
example (n m : ℕ) (f : ℕ ≃o ℕ) (h : f n < f m) : n < m := by
apply_fun f
assumption
example : ∀ m n : ℕ, m = n → (m < 2) = (n < 2) := by
refine fun m n h => ?_
apply_fun (· < 2) at h
exact h
example : ∀ m n : ℕ, m = n → (m < 2) = (n < 2) := by
intro m n h
apply_fun (· < 2) at h
exact h
example (f : ℕ ≃ ℕ) (a b : ℕ) (h : a = b) : True := by
apply_fun f at h
guard_hyp h : f a = f b
trivial
example (f : ℤ ≃ ℤ) (a b : ℕ) (h : a = b) : True := by
apply_fun f at h
guard_hyp h : f a = f b
trivial
example (f : ℤ ≃ ℤ) (a b : α) (h : a = b) : True := by
fail_if_success apply_fun f at h
trivial
example (f : ℕ → ℕ) (a b : ℕ) (h : a = b) : True := by
apply_fun f at h
guard_hyp h : f a = f b
trivial
example (f : {i : Nat} → Fin i → ℕ) (a b : Fin 37) (h : a = b) : True := by
apply_fun f at h
guard_hyp h : f a = f b
trivial
example (f : (p : Prop) → [Decidable p] → Nat) (p q : Prop) (h : p = q)
(h' : {n m : Nat} → n = m → True) : True := by
classical
apply_fun f at h
apply h'
exact h
example (f : (p : Prop) → [Decidable p] → Nat) (p q : Prop) (h : p = q)
(h' : {n m : Nat} → n = m → True) : True := by
classical
apply_fun (fun x [Decidable x] => f x) at h
apply h'
exact h
example (a b : ℕ) (h : a = b) : True := by
apply_fun (fun i => i + ?_) at h
· trivial
· exact 37
-- Check that it can solve congruence (needs Subsingleton.elim for the fintype instances)
example (α β : Type u) [Fintype α] [Fintype β] (h : α = β) : True := by
apply_fun Fintype.card at h
guard_hyp h : Fintype.card α = Fintype.card β
trivial
-- Check that metavariables in the goal do not prevent apply_fun from detecting the relation
set_option linter.unusedTactic false in
example (f : α ≃ β) (x y : α) (h : f x = f y) : x = y := by
change _
-- now the goal is a metavariable
apply_fun f
exact h
-- Check that lack of WHNF does not prevent apply_fun_from detecting the relation
example (f : α ≃ β) (x y : α) (h : f x = f y) : (fun s => s) (x = y) := by
apply_fun f
exact h
-- check that `apply_fun` uses the function provided to help elaborate the injectivity lemma
example (x : ℕ) : x = x := by
apply_fun (Nat.cast : ℕ → ℚ) using Nat.cast_injective
rfl
-- Check that locals are elaborated properly in apply_fun
example : 1 = 1 := by
let f := fun (x : Nat) => x + 1
-- clearly false but for demo purposes only
have g : ∀ (f : ℕ → ℕ), Function.Injective f := test_sorry
apply_fun f using (g f)
rfl
def funFamily (_i : ℕ) : Bool → Bool := id
-- `apply_fun` should not silence errors in `assumption`
set_option linter.unreachableTactic false in
/--
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 (error) in
example (_h₁ : Function.Injective (funFamily ((List.range 128).map (fun _ => 0)).sum)) :
true = true := by
apply_fun funFamily 0 |
.lake/packages/mathlib/MathlibTest/interactiveUnfold.lean | import Mathlib
/-!
The `unfold?` tactic is used interactively, so it is tricky to test directly.
In order to test it, we use the `#unfold?` command.
-/
/--
info: Unfolds for 0:
· ↑0
· ↑0
· Rat.ofInt ↑0
-/
#guard_msgs in
#unfold? (0 : Rat)
/--
info: Unfolds for -42:
· Int.negOfNat 42
· Int.negSucc 41
-/
#guard_msgs in
#unfold? -42
-- Rat.mk is private, so it doesn't show up here
/--
info: Unfolds for 42:
· ↑42
· ↑42
· Rat.ofInt ↑42
-/
#guard_msgs in
#unfold? (42 : ℚ)
/--
info: Unfolds for 1 + 1:
· Nat.add 1 1
· 2
-/
#guard_msgs in
#unfold? 1 + 1
/--
info: Unfolds for 5 / 3:
· Nat.div 5 3
· 1
-/
#guard_msgs in
#unfold? 5 / 3
/--
info: Unfolds for 1 + 1:
· Ordinal.type (Sum.Lex EmptyRelation EmptyRelation)
· ⟦{ α := PUnit.{u_1 + 1} ⊕ PUnit.{u_1 + 1}, r := Sum.Lex EmptyRelation EmptyRelation, wo := ⋯ }⟧
· Quot.mk ⇑Ordinal.isEquivalent
{ α := PUnit.{u_1 + 1} ⊕ PUnit.{u_1 + 1}, r := Sum.Lex EmptyRelation EmptyRelation, wo := ⋯ }
-/
#guard_msgs in
#unfold? (1 : Ordinal) + 1
/--
info: Unfolds for 3 ∈ {1, 2, 3}:
· {1, 2, 3}.Mem 3
· {1, 2, 3} 3
· Set.insert 1 {2, 3} 3
· {b | b = 1 ∨ b ∈ {2, 3}} 3
· 3 = 1 ∨ 3 ∈ {2, 3}
-/
#guard_msgs in
#unfold? 3 ∈ ({1, 2, 3} : Set ℕ)
/--
info: Unfolds for Function.Injective fun x => x:
· ∀ ⦃a₁ a₂ : ℕ⦄, (fun x => x) a₁ = (fun x => x) a₂ → a₁ = a₂
-/
#guard_msgs in
#unfold? Function.Injective (fun x : Nat => x)
variable (A B : Set Nat) (n : Nat)
/--
info: Unfolds for 1 ∈ A ∪ B:
· (A ∪ B).Mem 1
· (A ∪ B) 1
· A.union B 1
· {a | a ∈ A ∨ a ∈ B} 1
· 1 ∈ A ∨ 1 ∈ B
-/
#guard_msgs in
#unfold? 1 ∈ A ∪ B
/--
info: Unfolds for (fun x => x) (1 + 1):
· 1 + 1
· Nat.add 1 1
· 2
-/
#guard_msgs in
#unfold? (fun x => x) (1 + 1)
/--
info: Unfolds for fun x => id x:
· id
· fun a => a
-/
#guard_msgs in
#unfold? fun x => id x |
.lake/packages/mathlib/MathlibTest/mod_cases.lean | import Mathlib.Tactic.ModCases
private axiom test_sorry : ∀ {a}, a
example (n : ℤ) : 3 ∣ n ^ 3 - n := by
mod_cases n % 3
· guard_hyp H :ₛ n ≡ 0 [ZMOD 3]; guard_target = 3 ∣ n ^ 3 - n; exact test_sorry
· guard_hyp H :ₛ n ≡ 1 [ZMOD 3]; guard_target = 3 ∣ n ^ 3 - n; exact test_sorry
· guard_hyp H :ₛ n ≡ 2 [ZMOD 3]; guard_target = 3 ∣ n ^ 3 - n; exact test_sorry
example (n : ℕ) : 3 ∣ n ^ 3 + n := by
mod_cases n % 3
· guard_hyp H :~ n ≡ 0 [MOD 3]; guard_target = 3 ∣ n ^ 3 + n; exact test_sorry
· guard_hyp H :~ n ≡ 1 [MOD 3]; guard_target = 3 ∣ n ^ 3 + n; exact test_sorry
· guard_hyp H :~ n ≡ 2 [MOD 3]; guard_target = 3 ∣ n ^ 3 + n; exact test_sorry
-- test case for https://github.com/leanprover-community/mathlib4/issues/1851
example (n : ℕ) (z : ℤ) : n = n := by
induction n with
| zero => rfl
| succ n _ih =>
mod_cases _h : z % 2
· exact test_sorry
· exact test_sorry |
.lake/packages/mathlib/MathlibTest/Perm.lean | import Mathlib.GroupTheory.Perm.Cycle.Concrete
import Mathlib.Tactic.DeriveFintype
open Equiv
#guard with_decl_name% ex₁ unsafe (reprStr (1 : Perm (Fin 4))) == "1"
#guard with_decl_name% ex₂ unsafe (reprStr (c[0, 1] : Perm (Fin 4))) == "c[0, 1]"
#guard with_decl_name% ex₃
unsafe (reprStr (c[0, 1] * c[2, 3] : Perm (Fin 4))) == "c[0, 1] * c[2, 3]"
#guard with_decl_name% ex₄
unsafe (reprPrec (c[0, 1] * c[2, 3] : Perm (Fin 4)) 70).pretty == "(c[0, 1] * c[2, 3])"
#guard with_decl_name% ex₅
unsafe (reprPrec (c[0, 1] * c[1, 2] : Perm (Fin 4)) 70).pretty == "c[0, 1, 2]"
example : (c[0, 1] * c[1, 2] : Perm (Fin 4)).cycleType = {3} := by decide |
.lake/packages/mathlib/MathlibTest/linear_combination'.lean | import Mathlib.Tactic.LinearCombination'
import Mathlib.Tactic.Linarith
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 : ℤ) : x ^ 2 = x ^ 2 := by linear_combination' x ^ 2
example (x y : ℤ) (h : x = 0) : y ^ 2 * x = 0 := by linear_combination' y ^ 2 * h
/-! ### 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 := simp) h1
/-! ### 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
simp +decide
/-! ### 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'
-- 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 - (w + z) - (0 - 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
/-! ### 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
-- 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'.c_mul_pf 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
-- This fails because the linear_combination' tactic requires the equations
-- and coefficients to use a type that fulfills the add_group condition,
-- and ℕ does not.
example (a _b : ℕ) (h1 : a = 3) : a = 3 := by
fail_if_success linear_combination' h1
linear_combination2 h1
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
/-! ### 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
/-! ### 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 8000 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 |
.lake/packages/mathlib/MathlibTest/choose_reduction.lean | import Mathlib.Data.Finset.Basic
/-!
Tests that `List.choose` and friends all reduce appropriately.
Previously this was blocked by reducibility issues with `And.rec`
(https://leanprover.zulipchat.com/#narrow/stream/236446-Type-theory/topic/And.2Erec/near/398483665).
-/
namespace List
lemma foo : ∃ x, x ∈ ([0,3] : List (Fin 5)) ∧ x = 3 := by
use 3
simp
example : choose _ _ foo = 3 := by rfl
example : choose _ _ foo = 3 := rfl
example : choose _ _ foo = 3 := by eq_refl
example : choose _ _ foo = 3 := by exact rfl
example : choose _ _ foo = 3 := by decide
end List
namespace Multiset
lemma foo : ∃! x, x ∈ ({0,3} : Multiset (Fin 5)) ∧ x = 3 := by
use 3
simp
example : choose _ _ foo = 3 := by rfl
example : choose _ _ foo = 3 := rfl
example : choose _ _ foo = 3 := by eq_refl
example : choose _ _ foo = 3 := by exact rfl
example : choose _ _ foo = 3 := by decide
end Multiset
namespace Finset
lemma foo : ∃! x, x ∈ ({0,3} : Finset (Fin 5)) ∧ x = 3 := by
use 3
simp
example : choose _ _ foo = 3 := by rfl
example : choose _ _ foo = 3 := rfl
example : choose _ _ foo = 3 := by eq_refl
example : choose _ _ foo = 3 := by exact rfl
example : choose _ _ foo = 3 := by decide
end Finset |
.lake/packages/mathlib/MathlibTest/Have.lean | import Mathlib.Tactic.Have
example : Nat := by
have h : Nat
exact 5
exact h
example : Nat := by
have : Nat
· exact 5
exact this
example {a : Nat} : a = a := by
have h : a = a
· rfl
exact h
example {a : Nat} : a = a := by
have : a = a
· rfl
exact this
example : True := by
let _N; -- FIXME: https://github.com/leanprover/lean4/issues/1670
exact Nat
have
· exact 0
have _h : Nat
· exact this
have _h' x : x < x + 1
· exact Nat.lt.base x
have _h'' (x : Nat) : x < x + 1
· exact Nat.lt.base x
let _m
· exact 6
let _m' x (y : Nat) : x + y = y + x
rw [Nat.add_comm]
have _q
· exact 6
simp
/--
error: type expected, got
(Nat.zero : Nat)
-/
#guard_msgs in
example : True := by have h : Nat.zero |
.lake/packages/mathlib/MathlibTest/deriving_countable.lean | import Mathlib.Tactic.DeriveCountable
import Mathlib.Data.Countable.Basic
import Mathlib.Logic.Equiv.List
/-!
# Tests of the `Countable` deriving handler
-/
namespace Test
/-!
Structure
-/
structure S (α β : Type*) where
x : α
y : List β
deriving Countable
example : Countable (S Nat Bool) := inferInstance
/-!
Recursive inductive type, no indices
-/
inductive T (α : Type)
| a (n : Option α)
| b (n m : α) (t : T α)
| c
deriving Countable
example : Countable (T Nat) := inferInstance
/-!
Motivating example for writing the deriving handler
-/
inductive PreCantor' : Type
| zero : PreCantor'
| oadd : PreCantor' → ℕ+ → PreCantor' → PreCantor'
deriving Countable
example : Countable PreCantor' := inferInstance
/-!
Sort
-/
inductive PList (α : Sort _) where
| nil
| cons (x : α) (xs : PList α)
deriving Countable
example : Countable (PList (1 = 2)) := inferInstance
/-!
Recursive type with indices
-/
inductive I : Nat → Type
| a (n : Nat) : I n
| b (n : Nat) (x : Nat) (c : I x) : I (n + 1)
deriving Countable
example : Countable (I 37) := inferInstance
example : Countable (Σ i, I i) := inferInstance
/-!
Mutually recursive types
-/
mutual
inductive T1 where
| a
| b (x : T1) (y : T2)
deriving Countable
inductive T2 where
| c (n : Nat)
| d (x y : T1)
deriving Countable
end
example : Countable T1 := inferInstance
example : Countable T2 := inferInstance
/-!
Not supported: nested inductive types
-/
/-- error: None of the deriving handlers for class `Countable` applied to `Nested` -/
#guard_msgs in
inductive Nested where
| mk (xs : List Nested)
deriving Countable
/-!
Not supported: reflexive inductive types
-/
/-- error: None of the deriving handlers for class `Countable` applied to `Reflex` -/
#guard_msgs in
inductive Reflex where
| mk (f : Bool → Reflex)
deriving Countable |
.lake/packages/mathlib/MathlibTest/order.lean | import Mathlib.LinearAlgebra.Matrix.Rank
import Mathlib.Tactic.Order
example (a b c : Nat) (h1 : a ≤ b) (h2 : b ≤ c) : a ≤ c := by
order
example (a b c d e : Nat) (h1 : a ≤ b) (h2 : b ≤ c) (h3 : c ≤ d) (h4 : d ≤ e) (h5 : b ≠ d) :
a < e := by
order
example (a b c : Nat) (h1 : a = b) (h2 : b = c) : a = c := by
order
example (a b : Int) (h1 : ¬(a < b)) (h2 : ¬(b < a)) : a = b := by
order
variable {α : Type*}
example [LinearOrder α] (a b : α) (h1 : ¬(a < b)) (h2 : ¬(b < a)) : a = b := by
order
example [PartialOrder α] (a b c d e : α) (h1 : a ≤ b) (h2 : b ≤ c) (h3 : c ≤ d) (h4 : d ≤ e) (h5 : b ≠ d) :
a < e := by
order
example [PartialOrder α] (s t x y : α) (h1 : s ≤ x) (h2 : x ≤ t) (h3 : s ≤ y)
(h4 : y ≤ t) (h5 : x ≠ y) :
s < t := by
order
example [PartialOrder α] (a b c d : α) (h1 : a ≤ b) (h2 : b ≤ c) (h3 : ¬(a < c))
(h4 : a ≤ d) :
c ≤ d := by
order
example [PartialOrder α] (a : α) : ¬ (a < a) := by
order
example (a b : α) [PartialOrder α] (h1 : a < b ∧ b < a) : False := by
order
example (a b : α) [LinearOrder α] : a ≤ b ∨ b ≤ a := by
order
example (a b : α) [Preorder α] (h : ∃ c, a < c ∧ c < b) : a ≠ b := by
order
example {n : Nat} (A B C : Matrix (Fin n) (Fin n) ℚ) : (A * B * C).rank ≤ A.rank ⊓ C.rank := by
order [Matrix.rank_mul_le A B, Matrix.rank_mul_le (A * B) C]
example (L : Type) [Lattice L] :
(∀ a b c : L, a ⊔ (b ⊓ c) = (a ⊔ b) ⊓ (a ⊔ c)) ↔
(∀ a b c : L, a ⊓ (b ⊔ c) = (a ⊓ b) ⊔ (a ⊓ c)) := by
refine ⟨fun h a b c ↦ ?_, fun h a b c ↦ ?_⟩
· order only [h (a ⊓ b) c a, h c a b]
· order only [h (a ⊔ b) c a, h c a b]
example [Preorder α] (a b c d : α) (h1 : a ≤ b) (h2 : b ≤ c) (h3 : ¬(a < c))
(h4 : a ≤ d) :
c ≤ d := by
order
example [Preorder α] (a b : α) (h1 : a < b) : b > a := by
order
example [Preorder α] (a b : α) (h1 : a > b) : b < a := by
order
example [PartialOrder α] [OrderTop α] (a : α) (h1 : ⊤ ≤ a) : a = ⊤ := by
order
example [Preorder α] [OrderTop α] (a : α) (h1 : a > ⊤) : a < a := by
order
example [Preorder α] [OrderBot α] [OrderTop α] : (⊥ : α) ≤ ⊤ := by
order
example (a b : α) [PartialOrder α] [OrderBot α] [OrderTop α] (h : (⊥ : α) = ⊤) : a = b := by
order
example (a b : α) [SemilatticeSup α] : a ≤ a ⊔ b := by
order
example (a b c : α) [SemilatticeSup α] (h1 : a ≤ c) (h2 : b ≤ c) : a ⊔ b ≤ c := by
order
example (a b c : α) [SemilatticeSup α] (h1 : a ≤ b) : a ⊔ c ≤ b ⊔ c := by
order
example (a b : α) [Lattice α] : a ⊓ b ≤ a ⊔ b := by
order
example (a b : α) [Lattice α] : a ⊓ b ≤ a ⊔ b := by
order
example (a b : α) [Lattice α] : a ⊔ b = b ⊔ a := by
order
example (a b c : α) [Lattice α] : a ⊓ (b ⊔ c) ≥ (a ⊓ b) ⊔ (a ⊓ c) := by
order
set_option trace.order true in
/--
error: No contradiction found.
Additional diagnostic information may be available using the `set_option trace.order true` command.
---
trace:
[order] Working on type α (partial order)
[order] Collected atoms:
#0 := a ⊓ (b ⊔ c)
#1 := a
#2 := b ⊔ c
#3 := b
#4 := c
#5 := a ⊓ b ⊔ a ⊓ c
#6 := a ⊓ b
#7 := a ⊓ c
[order] Collected facts:
#2 := #3 ⊔ #4
#0 := #1 ⊓ #2
#6 := #1 ⊓ #3
#7 := #1 ⊓ #4
#5 := #6 ⊔ #7
¬ #0 ≤ #5
[order] Processed facts:
#3 ≤ #2
#4 ≤ #2
#2 := #3 ⊔ #4
#0 ≤ #1
#0 ≤ #2
#0 := #1 ⊓ #2
#6 ≤ #1
#6 ≤ #3
#6 := #1 ⊓ #3
#7 ≤ #1
#7 ≤ #4
#7 := #1 ⊓ #4
#6 ≤ #5
#7 ≤ #5
#5 := #6 ⊔ #7
#0 ≠ #5
¬ #0 < #5
-/
#guard_msgs in
example (a b c : α) [Lattice α] : a ⊓ (b ⊔ c) ≤ (a ⊓ b) ⊔ (a ⊓ c) := by
order
-- This used to work when a different matching strategy was used in `order`.
-- This example is now considered outside the scope of the `order` tactic.
/--
error: No contradiction found.
Additional diagnostic information may be available using the `set_option trace.order true` command.
-/
#guard_msgs in
example (a b c : Set α) : a ∩ (b ∪ c) ≥ (a ∩ b) ∪ (a ∩ c) := by
order
-- Contrived example for universes not of the form `Sort (u + 1)`.
example {α : Sort (max (v + 1) (u + 1))} [LinearOrder α] {a b c : α} (hab : a < b)
(habc : min a b ≤ c) (hcba : min c b ≤ a) : a = c := by
order
-- worst case for the current algorithm
example [PartialOrder α]
(x1 y1 : α)
(x2 y2 : α)
(x3 y3 : α)
(x4 y4 : α)
(x5 y5 : α)
(x6 y6 : α)
(x7 y7 : α)
(x8 y8 : α)
(x9 y9 : α)
(x10 y10 : α)
(x11 y11 : α)
(x12 y12 : α)
(x13 y13 : α)
(x14 y14 : α)
(x15 y15 : α)
(x16 y16 : α)
(x17 y17 : α)
(x18 y18 : α)
(x19 y19 : α)
(x20 y20 : α)
(x21 y21 : α)
(x22 y22 : α)
(x23 y23 : α)
(x24 y24 : α)
(x25 y25 : α)
(x26 y26 : α)
(x27 y27 : α)
(x28 y28 : α)
(x29 y29 : α)
(x30 y30 : α)
(h0 : y1 ≤ x1)
(h1 : ¬(y1 < x1)) (h2 : y2 ≤ x1) (h3 : y1 ≤ x2)
(h4 : ¬(y2 < x2)) (h5 : y3 ≤ x2) (h6 : y2 ≤ x3)
(h7 : ¬(y3 < x3)) (h8 : y4 ≤ x3) (h9 : y3 ≤ x4)
(h10 : ¬(y4 < x4)) (h11 : y5 ≤ x4) (h12 : y4 ≤ x5)
(h13 : ¬(y5 < x5)) (h14 : y6 ≤ x5) (h15 : y5 ≤ x6)
(h16 : ¬(y6 < x6)) (h17 : y7 ≤ x6) (h18 : y6 ≤ x7)
(h19 : ¬(y7 < x7)) (h20 : y8 ≤ x7) (h21 : y7 ≤ x8)
(h22 : ¬(y8 < x8)) (h23 : y9 ≤ x8) (h24 : y8 ≤ x9)
(h25 : ¬(y9 < x9)) (h26 : y10 ≤ x9) (h27 : y9 ≤ x10)
(h28 : ¬(y10 < x10)) (h29 : y11 ≤ x10) (h30 : y10 ≤ x11)
(h31 : ¬(y11 < x11)) (h32 : y12 ≤ x11) (h33 : y11 ≤ x12)
(h34 : ¬(y12 < x12)) (h35 : y13 ≤ x12) (h36 : y12 ≤ x13)
(h37 : ¬(y13 < x13)) (h38 : y14 ≤ x13) (h39 : y13 ≤ x14)
(h40 : ¬(y14 < x14)) (h41 : y15 ≤ x14) (h42 : y14 ≤ x15)
(h43 : ¬(y15 < x15)) (h44 : y16 ≤ x15) (h45 : y15 ≤ x16)
(h46 : ¬(y16 < x16)) (h47 : y17 ≤ x16) (h48 : y16 ≤ x17)
(h49 : ¬(y17 < x17)) (h50 : y18 ≤ x17) (h51 : y17 ≤ x18)
(h52 : ¬(y18 < x18)) (h53 : y19 ≤ x18) (h54 : y18 ≤ x19)
(h55 : ¬(y19 < x19)) (h56 : y20 ≤ x19) (h57 : y19 ≤ x20)
(h58 : ¬(y20 < x20)) (h59 : y21 ≤ x20) (h60 : y20 ≤ x21)
(h61 : ¬(y21 < x21)) (h62 : y22 ≤ x21) (h63 : y21 ≤ x22)
(h64 : ¬(y22 < x22)) (h65 : y23 ≤ x22) (h66 : y22 ≤ x23)
(h67 : ¬(y23 < x23)) (h68 : y24 ≤ x23) (h69 : y23 ≤ x24)
(h70 : ¬(y24 < x24)) (h71 : y25 ≤ x24) (h72 : y24 ≤ x25)
(h73 : ¬(y25 < x25)) (h74 : y26 ≤ x25) (h75 : y25 ≤ x26)
(h76 : ¬(y26 < x26)) (h77 : y27 ≤ x26) (h78 : y26 ≤ x27)
(h79 : ¬(y27 < x27)) (h80 : y28 ≤ x27) (h81 : y27 ≤ x28)
(h82 : ¬(y28 < x28)) (h83 : y29 ≤ x28) (h84 : y28 ≤ x29)
(h85 : ¬(y29 < x29)) (h86 : y30 ≤ x29) (h87 : y29 ≤ x30)
(h88 : ¬(y30 < x30)) : x30 = y30 := by
order
-- Tests for linear order with lattice operations
example {α : Type*} [LinearOrder α] {a b c : α} (hab : a < b)
(habc : min a b ≤ c) (hcba : min c b ≤ a) : a = c := by
order
example {α : Type*} [LinearOrder α] {a b : α} (h : a ≠ max a b) : b = max a b := by
order
example {α : Type*} [LinearOrder α] {a b : α} (h1 : min a b ≠ a) (h2 : max a b ≠ a) : False := by
order
-- Note: `order` does not use distributivity in general
example {α : Type*} [LinearOrder α] {a b c : α} : max a (min b c) = min (max a b) (max a c) := by
order
example {α : Type*} [LinearOrder α] [BoundedOrder α] {a b : α} (h1 : a ⊔ b = ⊤)
(h2 : b ≠ ⊤) : a = ⊤ := by
order
example {α : Type*} [LinearOrder α] [BoundedOrder α] [Nontrivial α] {a b c d : α} (h1 : a ⊓ b = ⊥)
(h2 : c ⊓ d = ⊥) (h3 : a ⊔ c = ⊤) (h4 : b ⊔ d = ⊤) (h5 : a ⊔ d = ⊤) (h6 : b ⊔ c = ⊤) : False := by
have : (⊥ : α) < ⊤ := bot_lt_top -- TODO: detect `Nontrivial` instance in `order` and add this
-- fact automatically
order
example
(x₀ x₁ y₀ y₁ t f : ℤ)
(htf : f < t)
(hxf : x₀ ⊓ x₁ ≤ f)
(hyf : y₀ ⊓ y₁ ≤ f)
(c1 : x₁ ⊔ y₁ ≥ t)
(c2 : x₁ ⊔ y₀ ≥ t)
(c3 : x₀ ⊔ y₁ ≥ t)
(c4 : x₀ ⊔ y₀ ≥ t)
: False := by
omega
example {α : Type*} [LinearOrder α]
(x₀ x₁ y₀ y₁ t f : α)
(htf : f < t)
(hxf : x₀ ⊓ x₁ ≤ f)
(hyf : y₀ ⊓ y₁ ≤ f)
(c1 : x₁ ⊔ y₁ ≥ t)
(c2 : x₁ ⊔ y₀ ≥ t)
(c3 : x₀ ⊔ y₁ ≥ t)
(c4 : x₀ ⊔ y₀ ≥ t)
: False := by
order |
.lake/packages/mathlib/MathlibTest/AssertExists.lean | import Mathlib.Util.AssertExists
/-- info: No assertions made. -/
#guard_msgs in
#check_assertions
#check_assertions!
assert_not_exists Nats
theorem Nats : True := .intro
/--
info:
✅️ 'Nats' (declaration) asserted in 'MathlibTest.AssertExists'.
---
✅️ means the declaration or import exists.
❌️ means the declaration or import does not exist.
-/
#guard_msgs in
#check_assertions
#check_assertions!
/--
warning: the module 'Lean.Elab.Command' is (transitively) imported via
Lean.Elab.Command,
which is imported by Lean.Linter.Sets,
which is imported by Mathlib.Init,
which is imported by Mathlib.Util.AssertExistsExt,
which is imported by Mathlib.Util.AssertExists,
which is imported by this file.
-/
#guard_msgs in
assert_not_imported
Mathlib.Tactic.Common
Lean.Elab.Command
I_do_not_exist
/--
warning:
✅️ 'Nats' (declaration) asserted in 'MathlibTest.AssertExists'.
❌️ 'I_do_not_exist' (module) asserted in 'MathlibTest.AssertExists'.
❌️ 'Mathlib.Tactic.Common' (module) asserted in 'MathlibTest.AssertExists'.
---
✅️ means the declaration or import exists.
❌️ means the declaration or import does not exist.
-/
#guard_msgs in
#check_assertions
/--
warning:
❌️ 'I_do_not_exist' (module) asserted in 'MathlibTest.AssertExists'.
❌️ 'Mathlib.Tactic.Common' (module) asserted in 'MathlibTest.AssertExists'.
---
✅️ means the declaration or import exists.
❌️ means the declaration or import does not exist.
-/
#guard_msgs in
#check_assertions!
/--
error: Declaration commandAssert_not_imported_ is not allowed to be imported by this file.
It is defined in Mathlib.Util.AssertExists,
which is imported by this file.
These invariants are maintained by `assert_not_exists` statements, and exist in order to ensure that "complicated" parts of the library are not accidentally introduced as dependencies of "simple" parts of the library.
---
error: Declaration commandAssert_not_exists_ is not allowed to be imported by this file.
It is defined in Mathlib.Util.AssertExists,
which is imported by this file.
These invariants are maintained by `assert_not_exists` statements, and exist in order to ensure that "complicated" parts of the library are not accidentally introduced as dependencies of "simple" parts of the library.
-/
#guard_msgs in
assert_not_exists
commandAssert_not_imported_
commandAssert_not_exists_
I_do_not_exist |
.lake/packages/mathlib/MathlibTest/simp_intro.lean | import Mathlib.Tactic.SimpIntro
set_option autoImplicit true
example : x + 0 = y → x = y := by simp_intro h₁
example : x + 0 ≠ y → x ≠ y := by simp_intro h₁ h₂ -- h₂ is bound but not needed
example : x + 0 ≠ y → x ≠ y := by simp_intro h₁ h₂ h₃ -- h₃ is not bound
example (h : x = z) : x + 0 = y → x = z := by simp_intro [h]
example (h : y = z) : x + 0 = y → x = z := by
simp_intro
guard_target = x = y → x = z
simp_intro .. [h]
example (h : y = z) : x + 0 = y → x = z := by simp_intro _; exact h
example : ∀ (r : Nat → Prop), (∀ x, x > 0 → r x) → ∀ y z, y = 0 → z > y → r z := by
simp_intro ..
example : ∀ (r : Nat → Prop), (∀ x, x > 0 → r x) → ∀ y z, y = 0 → z > y → r z := by
simp_intro r hr y z hy hz |
.lake/packages/mathlib/MathlibTest/Monotonicity.lean | import Mathlib.Algebra.Order.Ring.Defs
import Mathlib.Data.List.Defs
import Mathlib.Tactic.Monotonicity
import Mathlib.Tactic.NormNum
private axiom test_sorry : ∀ {α}, α
open List Set
example (x y z k : ℕ)
(h : 3 ≤ (4 : ℕ))
(h' : z ≤ y) :
(k + 3 + x) - y ≤ (k + 4 + x) - z := by
mono
-- norm_num
example (x y z k : ℤ)
(h : 3 ≤ (4 : ℤ))
(h' : z ≤ y) :
(k + 3 + x) - y ≤ (k + 4 + x) - z := by
mono
-- norm_num
example (x y z a b : ℕ)
(h : a ≤ (b : ℕ))
(h' : z ≤ y) :
(1 + a + x) - y ≤ (1 + b + x) - z := by
transitivity (1 + a + x - z)
· mono
· mono
-- mono
-- mono
example (x y z a b : ℤ)
(h : a ≤ (b : ℤ))
(h' : z ≤ y) :
(1 + a + x) - y ≤ (1 + b + x) - z := by
apply @le_trans ℤ _ _ (1 + a + x - z)
-- transitivity (1 + a + x - z)
· mono
· mono
-- mono
-- mono
example (x y z : ℤ)
(h' : z ≤ y) :
(1 + 3 + x) - y ≤ (1 + 4 + x) - z := by
apply @le_trans ℤ _ _ (1 + 3 + x - z)
-- transitivity (1 + 3 + x - z)
· mono
· mono
-- mono
norm_num
example {x y z : ℕ} : true := by
have : y + x ≤ y + z := by
mono
guard_target = x ≤ z
exact test_sorry
trivial
example {x y z : ℕ} : true := by
suffices _this : x + y ≤ z + y by trivial
mono
guard_target = x ≤ z
exact test_sorry
example {x y z w : ℕ} : true := by
have : x + y ≤ z + w := by
mono
guard_target = x ≤ z; exact test_sorry
guard_target = y ≤ w; exact test_sorry
trivial
-- example
-- (h : 3 + 6 ≤ 4 + 5)
-- : 1 + 3 + 2 + 6 ≤ 4 + 2 + 1 + 5 :=
-- begin
-- ac_mono,
-- end
-- example
-- (h : 3 ≤ (4 : ℤ))
-- (h' : 5 ≤ (6 : ℤ))
-- : (1 + 3 + 2) - 6 ≤ (4 + 2 + 1 : ℤ) - 5 :=
-- begin
-- ac_mono,
-- mono,
-- end
-- example
-- (h : 3 ≤ (4 : ℤ))
-- (h' : 5 ≤ (6 : ℤ))
-- : (1 + 3 + 2) - 6 ≤ (4 + 2 + 1 : ℤ) - 5 :=
-- begin
-- transitivity (1 + 3 + 2 - 5 : ℤ),
-- { ac_mono },
-- { ac_mono },
-- end
-- example (x y z : ℤ)
-- (h : 3 ≤ (4 : ℤ))
-- (h' : z ≤ y)
-- : (1 + 3 + x) - y ≤ (1 + 4 + x) - z :=
-- begin
-- ac_mono, mono*
-- end
-- @[simp]
-- def List.le' {α : Type _} [LE α] : List α → List α → Prop
-- | (x::xs) (y::ys) => x ≤ y ∧ List.le' xs ys
-- | [] [] => true
-- | _ _ => false
-- @[simp]
-- instance list_has_le {α : Type _} [LE α] : LE (List α) :=
-- ⟨ List.le' ⟩
-- lemma list.le_refl {α : Type _} [Preorder α] {xs : LE α} : xs ≤ xs := by
-- induction' xs with x xs
-- · trivial
-- · simp [has_le.le,list.le]
-- split
-- · exact le_rfl
-- · apply xs_ih
-- -- @[trans]
-- lemma List.le_trans {α : Type _} [Preorder α]
-- {xs zs : List α} (ys : List α)
-- (h : xs ≤ ys)
-- (h' : ys ≤ zs) :
-- xs ≤ zs := by
-- revert ys zs
-- induction' xs with x xs
-- ; intro ys zs h h'
-- ; cases ys with y ys
-- ; cases zs with z zs
-- ; try { cases h; cases h'; done },
-- { apply list.le_refl },
-- { simp [has_le.le,list.le],
-- split,
-- apply le_trans h.left h'.left,
-- apply xs_ih _ h.right h'.right, }
-- @[mono]
-- lemma list_le_mono_left {α : Type*} [preorder α] {xs ys zs : list α}
-- (h : xs ≤ ys) :
-- xs ++ zs ≤ ys ++ zs := by
-- revert ys
-- induction xs with x xs; intro ys h
-- · cases ys; apply list.le_refl; cases h
-- · cases ys with y ys; cases h; simp [has_le.le,list.le] at *
-- revert h; apply and.imp_right
-- apply xs_ih
-- @[mono]
-- lemma list_le_mono_right {α : Type*} [preorder α] {xs ys zs : list α}
-- (h : xs ≤ ys) :
-- zs ++ xs ≤ zs ++ ys := by
-- revert ys zs
-- induction xs with x xs; intro ys zs h
-- · cases ys
-- · simp; apply list.le_refl
-- · cases h
-- · cases ys with y ys; cases h; simp [has_le.le,list.le] at *
-- suffices : list.le' ((zs ++ [x]) ++ xs) ((zs ++ [y]) ++ ys)
-- · refine cast _ this; simp
-- apply list.le_trans (zs ++ [y] ++ xs)
-- · apply list_le_mono_left
-- induction zs with z zs
-- · simp [has_le.le,list.le]; apply h.left
-- · simp [has_le.le,list.le]; split; exact le_rfl
-- apply zs_ih
-- · apply xs_ih h.right
-- lemma bar_bar'
-- (h : [] ++ [3] ++ [2] ≤ [1] ++ [5] ++ [4])
-- : [] ++ [3] ++ [2] ++ [2] ≤ [1] ++ [5] ++ ([4] ++ [2]) :=
-- begin
-- ac_mono,
-- end
-- lemma bar_bar''
-- (h : [3] ++ [2] ++ [2] ≤ [5] ++ [4] ++ [])
-- : [1] ++ ([3] ++ [2]) ++ [2] ≤ [1] ++ [5] ++ ([4] ++ []) :=
-- begin
-- ac_mono,
-- end
-- lemma bar_bar
-- (h : [3] ++ [2] ≤ [5] ++ [4])
-- : [1] ++ [3] ++ [2] ++ [2] ≤ [1] ++ [5] ++ ([4] ++ [2]) :=
-- begin
-- ac_mono,
-- end
-- def P (x : ℕ) := 7 ≤ x
-- def Q (x : ℕ) := x ≤ 7
-- @[mono]
-- lemma P_mono {x y : ℕ}
-- (h : x ≤ y) :
-- P x → P y := by
-- intro h'
-- apply le_trans h' h
-- @[mono]
-- lemma Q_mono {x y : ℕ}
-- (h : y ≤ x) :
-- Q x → Q y := by
-- apply le_trans h
-- example (x y z : ℕ)
-- (h : x ≤ y)
-- : P (x + z) → P (z + y) :=
-- begin
-- ac_mono,
-- ac_mono,
-- end
-- example (x y z : ℕ)
-- (h : y ≤ x)
-- : Q (x + z) → Q (z + y) :=
-- begin
-- ac_mono,
-- ac_mono,
-- end
-- example (x y z k m n : ℤ)
-- (h₀ : z ≤ 0)
-- (h₁ : y ≤ x)
-- : (m + x + n) * z + k ≤ z * (y + n + m) + k :=
-- begin
-- ac_mono,
-- ac_mono,
-- ac_mono,
-- end
-- example (x y z k m n : ℕ)
-- (h₀ : z ≥ 0)
-- (h₁ : x ≤ y)
-- : (m + x + n) * z + k ≤ z * (y + n + m) + k :=
-- begin
-- ac_mono,
-- ac_mono,
-- ac_mono,
-- end
-- example (x y z k m n : ℕ)
-- (h₀ : z ≥ 0)
-- (h₁ : x ≤ y)
-- : (m + x + n) * z + k ≤ z * (y + n + m) + k :=
-- begin
-- ac_mono,
-- -- ⊢ (m + x + n) * z ≤ z * (y + n + m)
-- ac_mono,
-- -- ⊢ m + x + n ≤ y + n + m
-- ac_mono,
-- end
-- example (x y z k m n : ℕ)
-- (h₀ : z ≥ 0)
-- (h₁ : x ≤ y)
-- : (m + x + n) * z + k ≤ z * (y + n + m) + k :=
-- by { ac_mono* := h₁ }
-- example (x y z k m n : ℕ)
-- (h₀ : z ≥ 0)
-- (h₁ : m + x + n ≤ y + n + m)
-- : (m + x + n) * z + k ≤ z * (y + n + m) + k :=
-- by { ac_mono* := h₁ }
-- example (x y z k m n : ℕ)
-- (h₀ : z ≥ 0)
-- (h₁ : n + x + m ≤ y + n + m)
-- : (m + x + n) * z + k ≤ z * (y + n + m) + k :=
-- begin
-- ac_mono* : m + x + n ≤ y + n + m,
-- transitivity; [skip , apply h₁],
-- apply le_of_eq,
-- ac_refl,
-- end
-- example (x y z k m n : ℤ)
-- (h₁ : x ≤ y)
-- : true :=
-- begin
-- have : (m + x + n) * z + k ≤ z * (y + n + m) + k,
-- { ac_mono,
-- success_if_fail { ac_mono },
-- admit },
-- trivial
-- end
-- example (x y z k m n : ℕ)
-- (h₁ : x ≤ y)
-- : true :=
-- begin
-- have : (m + x + n) * z + k ≤ z * (y + n + m) + k,
-- { ac_mono*,
-- change 0 ≤ z, apply nat.zero_le, },
-- trivial
-- end
-- example (x y z k m n : ℕ)
-- (h₁ : x ≤ y)
-- : true :=
-- begin
-- have : (m + x + n) * z + k ≤ z * (y + n + m) + k,
-- { ac_mono,
-- change (m + x + n) * z ≤ z * (y + n + m),
-- admit },
-- trivial,
-- end
-- example (x y z k m n i j : ℕ)
-- (h₁ : x + i = y + j)
-- : (m + x + n + i) * z + k = z * (j + n + m + y) + k :=
-- begin
-- ac_mono^3,
-- cc
-- end
-- example (x y z k m n i j : ℕ)
-- (h₁ : x + i = y + j) :
-- z * (x + i + n + m) + k = z * (y + j + n + m) + k := by
-- congr
-- -- simp [h₁]
-- example (x y z k m n i j : ℕ)
-- (h₁ : x + i = y + j)
-- : (m + x + n + i) * z + k = z * (j + n + m + y) + k :=
-- begin
-- ac_mono*,
-- cc,
-- end
-- example (x y : ℕ)
-- (h : x ≤ y)
-- : true :=
-- begin
-- (do v ← mk_mvar,
-- p ← to_expr ```(%%v + x ≤ y + %%v),
-- assert `h' p),
-- ac_mono := h,
-- trivial,
-- exact 1,
-- end
-- example {x y z w : ℕ} : true := by
-- have : x * y ≤ z * w := by
-- mono with [0 ≤ z,0 ≤ y]
-- · guard_target = 0 ≤ z; admit
-- · guard_target = 0 ≤ y; admit
-- guard_target' = x ≤ z; admit
-- guard_target' = y ≤ w; admit
-- trivial
-- example {x y z w : Prop} : true := by
-- have : x ∧ y → z ∧ w := by
-- mono
-- guard_target = x → z; admit
-- guard_target = y → w; admit
-- trivial
-- example {x y z w : Prop} : true := by
-- have : x ∨ y → z ∨ w := by
-- mono
-- guard_target = x → z; admit
-- guard_target = y → w; admit
-- trivial
-- example {x y z w : ℤ} : true := by
-- suffices : x + y < w + z; trivial
-- have : x < w; admit
-- have : y ≤ z; admit
-- mono right
-- example {x y z w : ℤ} : true := by
-- suffices : x * y < w * z; trivial
-- have : x < w; admit
-- have : y ≤ z; admit
-- mono right
-- · guard_target = 0 < y; admit
-- · guard_target = 0 ≤ w; admit
-- example (x y : ℕ)
-- (h : x ≤ y)
-- : true :=
-- begin
-- (do v ← mk_mvar,
-- p ← to_expr ```(%%v + x ≤ y + %%v),
-- assert `h' p),
-- ac_mono := h,
-- trivial,
-- exact 3
-- end
-- example {α} [LinearOrder α]
-- (a b c d e : α) :
-- max a b ≤ e → b ≤ e := by
-- mono
-- apply le_max_right
-- example (a b c d e : Prop)
-- (h : d → a) (h' : c → e) :
-- (a ∧ b → c) ∨ d → (d ∧ b → e) ∨ a := by
-- mono
-- mono
-- mono
-- import Mathlib.MeasureTheory.Function.LocallyIntegrable
-- import Mathlib.MeasureTheory.Measure.Lebesgue.Integral
-- example : ∫ x in Icc 0 1, real.exp x ≤ ∫ x in Icc 0 1, real.exp (x+1) := by
-- mono
-- · exact real.continuous_exp.locally_integrable.integrable_on_is_compact is_compact_Icc
-- · exact (real.continuous_exp.comp <| continuous_add_right 1)
-- .locally_integrable.integrable_on_is_compact is_compact_Icc
-- intro x
-- dsimp only
-- mono
-- linarith |
.lake/packages/mathlib/MathlibTest/cancel_denoms.lean | import Mathlib.Algebra.Order.Field.Rat
import Mathlib.Tactic.CancelDenoms
import Mathlib.Tactic.Ring
private axiom test_sorry : ∀ {α}, α
universe u
section
variable {α : Type u} [Field α] [LinearOrder α] [IsStrictOrderedRing α] (a b c d : α)
-- prior to https://github.com/leanprover-community/mathlib4/pull/12083, `cancel_denoms` would not make progress on this
example : ¬ (4 / 2 : ℚ) = 3 := by cancel_denoms
example (h : a / 5 + b / 4 < c) : 4*a + 5*b < 20*c := by
cancel_denoms at h
exact h
example (h : a > 0) : a / 5 > 0 := by
cancel_denoms
exact h
example (h : a + b = c) : a/5 + d*(b/4) = c - 4*a/5 + b*2*d/8 - b := by
cancel_denoms
rw [← h]
ring
example (h : 0 < a) : a / (3/2) > 0 := by
cancel_denoms
exact h
example (h : 0 < a): 0 < a / 1 := by
cancel_denoms
exact h
example (h : a < 0): 0 < a / -1 := by
cancel_denoms
exact h
example (h : -a < 2 * b): a / -2 < b := by
cancel_denoms
exact h
example (h : a < 6 * a) : a / 2 / 3 < a := by
cancel_denoms
exact h
example (h : a < 9 * a) : a / 3 / 3 < a := by
cancel_denoms
exact h
end
-- Some tests with a concrete type universe.
section
variable {α : Type} [Field α] [LinearOrder α] [IsStrictOrderedRing α] (a b c d : α)
example (h : a / 5 + b / 4 < c) : 4*a + 5*b < 20*c := by
cancel_denoms at h
exact h
example (h : a > 0) : a / 5 > 0 := by
cancel_denoms
exact h
variable {α : Type} [Field α] [CharZero α] (a b c d : α)
example (h : a + b = c) : a/5 + d*(b/4) = c - 4*a/5 + b*2*d/8 - b := by
cancel_denoms
rw [← h]
ring
end
-- Some tests with a concrete type.
section
variable (a b c d : ℚ)
-- inspired by automated testing
example : (1 : ℚ) > 0 := by
have := 0
cancel_denoms
example (h : a / 5 + b / 4 < c) : 4*a + 5*b < 20*c := by
cancel_denoms at h
exact h
example (h : a > 0) : a / 5 > 0 := by
cancel_denoms
exact h
example (h : a + b = c) : a/5 + d*(b/4) = c - 4*a/5 + b*2*d/8 - b := by
cancel_denoms
rw [← h]
ring
example (h : 2 * (4 * a + d * 5 * b) ≠ (40 * c - 32 * a + b * 2 * 5 * d - 40 * b)) :
a/5 + d*(b/4) ≠ c - 4*a/5 + b*2*d/8 - b := by
cancel_denoms
assumption
example (h : 27 ≤ (a + 3) ^ 3) : 1 ≤ (a / 3 + 1) ^ 3 := by
cancel_denoms
assumption
example (h : a > 2) : 1 < 2⁻¹ * a := by
cancel_denoms
assumption
example (h : 6 * b = a⁻¹ * 3 + c * 2): b = a⁻¹ * 2⁻¹ + c * 3⁻¹ := by
cancel_denoms
assumption
example (h : a * 5 + b * 6 = 30 * c) : a * 2⁻¹ * 3⁻¹ + b * 5⁻¹ = c := by
cancel_denoms
assumption
example (h : 5 * a ^ 2 + 4 * b ^ 3 = 0) : a ^ 2 / 4 + b ^ 3 / 5 = 0 := by
cancel_denoms
assumption
example (h : 5 * a ^ 3 * b ^ 2 = 72 * c) : (a / 2) ^ 3 * (b / 3) ^ 2 = c / 5 := by
cancel_denoms
assumption
example (h : (5 * a ^ 3 + 8) ^ 2 = 1600 * c) : ((a / 2) ^ 3 + 1 / 5) ^ 2 = c := by
cancel_denoms
assumption
end
section
-- simulate the type of complex numbers
def C : Type := test_sorry
noncomputable instance : Field C := test_sorry
instance : CharZero C := test_sorry
variable (a b c d : C)
example (h : a + b = c) : a/5 + d*(b/4) = c - 4*a/5 + b*2*d/8 - b := by
cancel_denoms
rw [← h]
ring
end |
.lake/packages/mathlib/MathlibTest/wlog.lean | import Mathlib.Tactic.WLOG
import Mathlib.Algebra.Ring.Nat
example {x y : ℕ} : True := by
wlog h : x ≤ y
· guard_hyp h : ¬x ≤ y
guard_hyp this : ∀ {x y : ℕ}, x ≤ y → True -- `wlog` generalizes by default
guard_target =ₛ True
trivial
· guard_hyp h : x ≤ y
guard_target =ₛ True
trivial
example {x y : ℕ} : True := by
wlog h : x ≤ y generalizing x with H
· guard_hyp h : ¬x ≤ y
guard_hyp H : ∀ {x : ℕ}, x ≤ y → True -- only `x` was generalized
guard_target =ₛ True
trivial
· guard_hyp h : x ≤ y
guard_target =ₛ True
trivial
example {x y z : ℕ} : True := by
wlog h : x ≤ y + z with H
· guard_hyp h : ¬ x ≤ y + z
guard_hyp H : ∀ {x y z : ℕ}, x ≤ y + z → True -- wlog-claim is named `H` instead of `this`
guard_target =ₛ True
trivial
· guard_hyp h : x ≤ y + z
guard_target =ₛ True
trivial
example : ∀ _ _ : ℕ, True := by
intro x y
wlog h : x ≤ y -- `wlog` finds new variables
· trivial
· trivial
example {x y : ℕ} : True := by
wlog h : x ≤ y generalizing y x with H
· guard_hyp h : ¬ x ≤ y
guard_hyp H : ∀ {x y : ℕ}, x ≤ y → True -- order of ids in `generalizing` is ignored
trivial
· trivial
-- metadata doesn't cause a problem
example (α : Type := ℕ) (x : Option α := none) (y : Option α := by exact 0) : True := by
wlog h : x = y with H
· guard_hyp h : ¬ x = y
guard_hyp H : ∀ α, ∀ {x y : Option α}, x = y → True
trivial
· guard_hyp h : x = y
guard_target =ₛ True
trivial
-- inaccessible names work
example {x y : ℕ} : True := by
wlog _ : x ≤ y
case _ h => -- if these hypotheses weren't inaccessible, they wouldn't be renamed by `case`
guard_hyp h : ¬x ≤ y
guard_hyp this : ∀ {x y : ℕ}, x ≤ y → True
guard_target =ₛ True
trivial
case _ h =>
guard_hyp h : x ≤ y
guard_target =ₛ True
trivial
-- Handle ldecls properly:
example (x y : ℕ) : True := by
let z := 0
wlog hxy' : z ≤ y with H
· trivial
· trivial |
.lake/packages/mathlib/MathlibTest/pnat_to_nat.lean | import Mathlib.Tactic.PNatToNat
example (a b : PNat) (h : a < b) : 1 < b := by
pnat_to_nat
omega
example (a b : PNat) (h : a = b) : b = a := by
-- to test if the tactic works with inaccessible names
let a : ℤ := 42
pnat_to_nat
omega
example (a b : PNat) (h : a < b) : 1 < b := by
have := a.pos
pnat_to_nat
omega |
.lake/packages/mathlib/MathlibTest/superscript.lean | import Mathlib.Util.Superscript
import Mathlib.Tactic.UnsetOption
import Lean.PrettyPrinter
section
local syntax:arg term:max noWs superscript(term) : term
local macro_rules | `($a:term$b:superscript) => `($a ^ $b)
variable (foo : Nat)
example : 2² = 4 := rfl
example : 2¹⁶ = 65536 := rfl
example (n : Nat) : n⁽²⁻¹⁾ ⁺ ⁶ /- not done yet... -/ ⁺ ᶠᵒᵒ = n ^ (7 + foo) := rfl
example : (fun n => 2ⁿ⁺¹) 15 = 2¹⁶ := rfl
/--
info: aⁱ
---
info: a³⁷
---
info: a⁽¹ ⁺ ¹⁾
-/
#guard_msgs in
run_cmd do
let a := Lean.mkIdent `a
let i := Lean.mkIdent `i
Lean.logInfo <| ← `(term| $a$i:superscript)
let lit ← `(term| 37)
Lean.logInfo <| ← `(term| $a$lit:superscript)
let one_plus_one ← `(term| (1 + 1))
Lean.logInfo <| ← `(term| $a$one_plus_one:superscript)
-- TODO: fix this
/--
error: Not a superscript: 'α'
---
info: aα
-/
#guard_msgs in
run_cmd Lean.Elab.Command.liftTermElabM do
let a := Lean.mkIdent `a
let α := Lean.mkIdent `α
-- note: this only raises the error if we format non-lazily
Lean.logInfo <| ← Lean.PrettyPrinter.ppTerm <| ← `(term| $a$α:superscript)
end
section
local macro:arg a:term:max b:subscript(term) : term => `($a $(⟨b.raw[0]⟩))
example (a : Nat → Nat) (h : ∀ i, 1 ≤ (a)ᵢ) : 2 ≤ a ₀ + a ₁ :=
Nat.add_le_add h₍₀₎ (h)₁
/--
info: (a)ᵢ
---
info: (a)₃₇
---
info: (a)₍₁ ₊ ₁₎
-/
#guard_msgs in
run_cmd do
let a := Lean.mkIdent `a
let i := Lean.mkIdent `i
Lean.logInfo <| ← `(term| ($a)$i:subscript)
let lit ← `(term| 37)
Lean.logInfo <| ← `(term| ($a)$lit:subscript)
let one_plus_one ← `(term| (1 + 1))
Lean.logInfo <| ← `(term| ($a)$one_plus_one:subscript)
/--
error: Not a subscript: 'α'
---
info: (a)α
-/
#guard_msgs in
run_cmd Lean.Elab.Command.liftTermElabM do
let a := Lean.mkIdent `a
let α := Lean.mkIdent `α
-- note: this only raises the error if we format non-lazily
Lean.logInfo <| ← Lean.PrettyPrinter.ppTerm <| ← `(term| ($a)$α:subscript)
end
section delab
open Lean PrettyPrinter.Delaborator SubExpr
open Mathlib.Tactic (delabSubscript delabSuperscript)
private def checkSubscript {α : Type} (_ : α) := ()
local syntax:arg "testsub(" noWs subscript(term) noWs ")" : term
local macro_rules | `(testsub($a:subscript)) => `(checkSubscript $a)
private def checkSuperscript {α : Type} (_ : α) := ()
local syntax:arg "testsup(" noWs superscript(term) noWs ")" : term
local macro_rules | `(testsup($a:superscript)) => `(checkSuperscript $a)
@[app_delab checkSubscript]
private def delabCheckSubscript : Delab := withOverApp 2 do
let sub ← withAppArg delabSubscript
`(testsub($sub:subscript))
@[app_delab checkSuperscript]
private def delabCheckSuperscript : Delab := withOverApp 2 do
let sup ← withAppArg delabSuperscript
`(testsup($sup:superscript))
universe u v
/-- `α` cannot be subscripted or superscripted. -/
private def α {γ : Type u} {δ : Type v} : γ → δ → δ := fun _ ↦ id
/-- `β` can be both subscripted and superscripted. -/
private def β {γ : Type u} {δ : Type v} : γ → δ → δ := fun _ ↦ id
/-- `d` cannot be subscripted, so we create an alias for `id`. -/
private abbrev ID {γ : Sort u} := @id γ
variable (n : Nat)
/-- info: checkSubscript testsub(₁) : Unit -/
#guard_msgs in #check checkSubscript (checkSubscript 1)
/-- info: checkSubscript testsup(¹) : Unit -/
#guard_msgs in #check checkSubscript (checkSuperscript 1)
/-- info: checkSuperscript testsup(¹) : Unit -/
#guard_msgs in #check checkSuperscript (checkSuperscript 1)
/-- info: checkSuperscript testsub(₁) : Unit -/
#guard_msgs in #check checkSuperscript (checkSubscript 1)
section subscript
/-- info: testsub(₁₂₃₄₅₆₇₈₉₀ ₌₌ ₁₂₃₄₅₆₇₈₉₀) : Unit -/
#guard_msgs in #check testsub(₁₂₃₄₅₆₇₈₉₀ ₌₌ ₁₂₃₄₅₆₇₈₉₀)
/-- info: testsub(ᵦ ₙ ₍₁ ₊ ₂ ₋ ₃ ₌ ₀₎) : Unit -/
#guard_msgs in #check testsub(ᵦ ₙ ₍₁ ₊ ₂ ₋ ₃ ₌ ₀₎)
/-- info: testsub(ᵦ) : Unit -/
#guard_msgs in #check testsub(ᵦ)
/-- info: testsub(ᵦ ₍₎) : Unit -/
#guard_msgs in #check testsub(ᵦ ₍₎)
/-- info: testsub(ᵦ ᵦ ᵦ ᵦ) : Unit -/
#guard_msgs in #check testsub(ᵦ ᵦ ᵦ ᵦ)
/-- info: testsub(ɪᴅ ɪᴅ ₃₇) : Unit -/
#guard_msgs in #check testsub(ɪᴅ ɪᴅ ₃₇)
end subscript
section superscript
/-- info: testsup(¹²³⁴⁵⁶⁷⁸⁹⁰ ⁼⁼ ¹²³⁴⁵⁶⁷⁸⁹⁰) : Unit -/
#guard_msgs in #check testsup(¹²³⁴⁵⁶⁷⁸⁹⁰ ⁼⁼ ¹²³⁴⁵⁶⁷⁸⁹⁰)
/-- info: testsup(ᵝ ⁿ ⁽¹ ⁺ ² ⁻ ³ ⁼ ⁰⁾) : Unit -/
#guard_msgs in #check testsup(ᵝ ⁿ ⁽¹ ⁺ ² ⁻ ³ ⁼ ⁰⁾)
/-- info: testsup(ᵝ) : Unit -/
#guard_msgs in #check testsup(ᵝ)
/-- info: testsup(ᵝ ⁽⁾) : Unit -/
#guard_msgs in #check testsup(ᵝ ⁽⁾)
/-- info: testsup(ᵝ ᵝ ᵝ ᵝ) : Unit -/
#guard_msgs in #check testsup(ᵝ ᵝ ᵝ ᵝ)
/-- info: testsup(ⁱᵈ ⁱᵈ ³⁷) : Unit -/
#guard_msgs in #check testsup(ⁱᵈ ⁱᵈ ³⁷)
end superscript
private def card {γ : Sort u} := @id γ
local prefix:arg "#" => card
private def factorial {γ : Sort u} := @id γ
local notation:10000 n "!" => factorial n
abbrev Nat' := Nat
def Nat'.γ : Nat' → Nat' := id
variable (n x_x : Nat')
section no_subscript
/-- info: checkSubscript x_x : Unit -/
#guard_msgs in #check checkSubscript x_x
/-- info: checkSubscript (α 0 0) : Unit -/
#guard_msgs in #check checkSubscript (α 0 0)
/-- info: checkSubscript (0 * 1) : Unit -/
#guard_msgs in #check checkSubscript (0 * 1)
/-- info: checkSubscript (0 ^ 1) : Unit -/
#guard_msgs in #check checkSubscript (0 ^ 1)
/-- info: checkSubscript [1] : Unit -/
#guard_msgs in #check checkSubscript [1]
/-- info: checkSubscript #n : Unit -/
#guard_msgs in #check checkSubscript #n
/-- info: checkSubscript 2! : Unit -/
#guard_msgs in #check checkSubscript 2!
/- The delaborator should reject dot notation. -/
open Nat' (γ) in
/-- info: checkSubscript n.γ : Unit -/
#guard_msgs in #check testsub(ᵧ ₙ)
/- The delaborator should reject metavariables. -/
set_option pp.mvars false in
/-- info: checkSubscript ?_ : Unit -/
#guard_msgs in #check checkSubscript ?_
/- The delaborator should reject because `n` is shadowed and `✝` cannot be
subscripted. -/
variable {x} (hx : x = testsub(ₙ)) (n : True) in
/-- info: hx : x = checkSubscript n✝ -/
#guard_msgs in #check hx
end no_subscript
section no_superscript
/-- info: checkSuperscript x_x : Unit -/
#guard_msgs in #check checkSuperscript x_x
/-- info: checkSuperscript (α 0 0) : Unit -/
#guard_msgs in #check checkSuperscript (α 0 0)
/-- info: checkSuperscript (0 * 1) : Unit -/
#guard_msgs in #check checkSuperscript (0 * 1)
/-- info: checkSuperscript (0 ^ 1) : Unit -/
#guard_msgs in #check checkSuperscript (0 ^ 1)
/-- info: checkSuperscript [1] : Unit -/
#guard_msgs in #check checkSuperscript [1]
/-- info: checkSuperscript #n : Unit -/
#guard_msgs in #check checkSuperscript #n
/-- info: checkSuperscript 2! : Unit -/
#guard_msgs in #check checkSuperscript 2!
/- The delaborator should reject dot notation. -/
open Nat' (γ) in
/-- info: checkSuperscript n.γ : Unit -/
#guard_msgs in #check testsup(ᵞ ⁿ)
/- The delaborator should reject metavariables. -/
set_option pp.mvars false in
/-- info: checkSuperscript ?_ : Unit -/
#guard_msgs in #check checkSuperscript ?_
/- The delaborator should reject because `n` is shadowed and `✝` cannot be
superscripted. -/
variable {x} (hx : x = testsup(ⁿ)) (n : True) in
/-- info: hx : x = checkSuperscript n✝ -/
#guard_msgs in #check hx
end no_superscript
end delab |
.lake/packages/mathlib/MathlibTest/delaborators.lean | import Mathlib.Util.Delaborators
import Mathlib.Data.Set.Lattice
section PiNotation
variable (P : Nat → Prop) (α : Nat → Type) (s : Set ℕ)
/-- info: ∀ x > 0, P x : Prop -/
#guard_msgs in
#check ∀ x, x > 0 → P x
/-- info: ∀ x > 0, P x : Prop -/
#guard_msgs in
#check ∀ x > 0, P x
/-- info: ∀ x ≥ 0, P x : Prop -/
#guard_msgs in
#check ∀ x, x ≥ 0 → P x
/-- info: ∀ x ≥ 0, P x : Prop -/
#guard_msgs in
#check ∀ x ≥ 0, P x
/-- info: ∀ x < 0, P x : Prop -/
#guard_msgs in
#check ∀ x < 0, P x
/-- info: ∀ x < 0, P x : Prop -/
#guard_msgs in
#check ∀ x, x < 0 → P x
/-- info: ∀ x ≤ 0, P x : Prop -/
#guard_msgs in
#check ∀ x ≤ 0, P x
/-- info: ∀ x ≤ 0, P x : Prop -/
#guard_msgs in
#check ∀ x, x ≤ 0 → P x
/-- info: ∀ x ∈ s, P x : Prop -/
#guard_msgs in
#check ∀ x ∈ s, P x
/-- info: ∀ x ∈ s, P x : Prop -/
#guard_msgs in
#check ∀ x, x ∈ s → P x
/-- info: ∀ x ∉ s, P x : Prop -/
#guard_msgs in
#check ∀ x ∉ s,P x
/-- info: ∀ x ∉ s, P x : Prop -/
#guard_msgs in
#check ∀ x, x ∉ s → P x
variable (Q : Set ℕ → Prop)
/-- info: ∀ t ⊆ s, Q t : Prop -/
#guard_msgs in
#check ∀ t ⊆ s, Q t
/-- info: ∀ t ⊆ s, Q t : Prop -/
#guard_msgs in
#check ∀ t, t ⊆ s → Q t
/-- info: ∀ t ⊂ s, Q t : Prop -/
#guard_msgs in
#check ∀ t ⊂ s, Q t
/-- info: ∀ t ⊂ s, Q t : Prop -/
#guard_msgs in
#check ∀ t, t ⊂ s → Q t
/-- info: ∀ t ⊇ s, Q t : Prop -/
#guard_msgs in
#check ∀ t ⊇ s, Q t
/-- info: ∀ t ⊇ s, Q t : Prop -/
#guard_msgs in
#check ∀ t, t ⊇ s → Q t
/-- info: ∀ t ⊃ s, Q t : Prop -/
#guard_msgs in
#check ∀ t ⊃ s, Q t
/-- info: ∀ t ⊃ s, Q t : Prop -/
#guard_msgs in
#check ∀ t, t ⊃ s → Q t
/-- info: (x : ℕ) → α x : Type -/
#guard_msgs in
#check (x : Nat) → α x
/-!
Disabling binder predicates
-/
/-- info: ∀ x > 0, P x : Prop -/
#guard_msgs in
#check ∀ x > 0, P x
/-- info: ∀ (x : ℕ), x > 0 → P x : Prop -/
#guard_msgs in
set_option pp.mathlib.binderPredicates false in
#check ∀ x > 0, P x
/-- info: ∃ x > 0, P x : Prop -/
#guard_msgs in
#check ∃ x > 0, P x
/-- info: ∃ x, x > 0 ∧ P x : Prop -/
#guard_msgs in
set_option pp.mathlib.binderPredicates false in
#check ∃ x > 0, P x
/-!
Opening the `PiNotation` namespace enables `Π` notation.
-/
open PiNotation
/-- info: Π (x : ℕ), α x : Type -/
#guard_msgs in
#check (x : Nat) → α x
/-- info: ∀ (x : ℕ), P x : Prop -/
#guard_msgs in
#check (x : Nat) → P x
/-!
Note that the implementation of the `Π` delaborator in `Mathlib/Util/Delaborators.lean`
does not (yet?) make use of binder predicates.
-/
/-- info: Π (x : ℕ), x > 0 → α x : Type -/
#guard_msgs in
#check Π x > 0, α x
/-- info: Π (x : ℕ), x > 0 → α x : Type -/
#guard_msgs in
set_option pp.mathlib.binderPredicates false in
#check Π x > 0, α x
end PiNotation
section UnionInter
variable (s : ℕ → Set ℕ) (u : Set ℕ) (p : ℕ → Prop)
/-- info: ⋃ n ∈ u, s n : Set ℕ -/
#guard_msgs in
#check ⋃ n ∈ u, s n
/-- info: ⋂ n ∈ u, s n : Set ℕ -/
#guard_msgs in
#check ⋂ n ∈ u, s n
end UnionInter
section CompleteLattice
/-- info: ⨆ i ∈ Set.univ, (i = i) : Prop -/
#guard_msgs in
#check ⨆ (i : Nat) (_ : i ∈ Set.univ), (i = i)
/-- info: ⨅ i ∈ Set.univ, (i = i) : Prop -/
#guard_msgs in
#check ⨅ (i : Nat) (_ : i ∈ Set.univ), (i = i)
end CompleteLattice
section existential
/-- info: ∃ i ≥ 3, i = i : Prop -/
#guard_msgs in
#check ∃ (i : Nat), i ≥ 3 ∧ i = i
/-- info: ∃ i > 3, i = i : Prop -/
#guard_msgs in
#check ∃ (i : Nat), i > 3 ∧ i = i
/-- info: ∃ i ≤ 3, i = i : Prop -/
#guard_msgs in
#check ∃ (i : Nat), i ≤ 3 ∧ i = i
/-- info: ∃ i < 3, i = i : Prop -/
#guard_msgs in
#check ∃ (i : Nat), i < 3 ∧ i = i
variable (s : Set ℕ) (P : ℕ → Prop) (Q : Set ℕ → Prop)
/-- info: ∃ x ∉ s, P x : Prop -/
#guard_msgs in
#check ∃ x ∉ s, P x
/-- info: ∃ x ∉ s, P x : Prop -/
#guard_msgs in
#check ∃ x, x ∉ s ∧ P x
variable (Q : Set ℕ → Prop)
/-- info: ∃ t ⊆ s, Q t : Prop -/
#guard_msgs in
#check ∃ t ⊆ s, Q t
/-- info: ∃ t ⊆ s, Q t : Prop -/
#guard_msgs in
#check ∃ t, t ⊆ s ∧ Q t
/-- info: ∃ t ⊂ s, Q t : Prop -/
#guard_msgs in
#check ∃ t ⊂ s, Q t
/-- info: ∃ t ⊂ s, Q t : Prop -/
#guard_msgs in
#check ∃ t, t ⊂ s ∧ Q t
/-- info: ∃ t ⊇ s, Q t : Prop -/
#guard_msgs in
#check ∃ t ⊇ s, Q t
/-- info: ∃ t ⊇ s, Q t : Prop -/
#guard_msgs in
#check ∃ t, t ⊇ s ∧ Q t
/-- info: ∃ t ⊃ s, Q t : Prop -/
#guard_msgs in
#check ∃ t ⊃ s, Q t
/-- info: ∃ t ⊃ s, Q t : Prop -/
#guard_msgs in
#check ∃ t, t ⊃ s ∧ Q t
/-- info: ∃ n k, n = k : Prop -/
#guard_msgs in
#check ∃ n k, n = k
/-- info: ∃ n k, n = k : Prop -/
#guard_msgs in
#check ∃ n, ∃ k, n = k
section merging
/-- info: ∃ (_ : True), True : Prop -/
#guard_msgs in #check ∃ (_ : True), True
/-- info: ∃ (_ : True), ∃ x, x = x : Prop -/
#guard_msgs in #check ∃ (_ : True) (x : Nat), x = x
/-- info: ∃ (_ : True), ∃ x y, x = y : Prop -/
#guard_msgs in #check ∃ (_ : True) (x y : Nat), x = y
/-- info: ∃ (_ : True) (_ : False), True : Prop -/
#guard_msgs in #check ∃ (_ : True) (_ : False), True
set_option pp.funBinderTypes true in
/-- info: ∃ (x : ℕ) (x : ℕ), True : Prop -/
#guard_msgs in #check ∃ (_ : Nat) (_ : Nat), True
end merging
end existential
section prod
variable (x : ℕ × ℕ)
/-- info: x.1 : ℕ -/
#guard_msgs in
#check x.1
variable (p : (ℕ → ℕ) × (ℕ → ℕ))
/-- info: p.1 22 : ℕ -/
#guard_msgs in
#check p.1 22
set_option pp.numericProj.prod false in
/-- info: x.fst : ℕ -/
#guard_msgs in
#check x.1
set_option pp.numericProj.prod false in
/-- info: x.snd : ℕ -/
#guard_msgs in
#check x.2
set_option pp.explicit true in
/-- info: @Prod.fst Nat Nat x : Nat -/
#guard_msgs in
#check x.1
set_option pp.explicit true in
/-- info: @Prod.snd Nat Nat x : Nat -/
#guard_msgs in
#check x.2
set_option pp.fieldNotation false in
/-- info: Prod.fst x : ℕ -/
#guard_msgs in
#check x.1
set_option pp.fieldNotation false in
/-- info: Prod.snd x : ℕ -/
#guard_msgs in
#check x.2
end prod |
.lake/packages/mathlib/MathlibTest/DeriveToExpr.lean | import Lean
namespace DeriveToExprTests
open Lean
-- set_option trace.Elab.Deriving.toExpr true
inductive MyMaybe (α : Type u)
| none | some (x : α)
deriving ToExpr
run_cmd Elab.Command.liftTermElabM do
guard <| "MyMaybe.some 2" == s!"{← Lean.PrettyPrinter.ppExpr <| toExpr (MyMaybe.some 2)}"
run_cmd Elab.Command.liftTermElabM do
Meta.check <| toExpr (MyMaybe.some 2)
deriving instance ToExpr for ULift
run_cmd Elab.Command.liftTermElabM do
let e := toExpr (MyMaybe.none : MyMaybe (ULift.{1,0} Nat))
let ty := toTypeExpr (MyMaybe (ULift.{1,0} Nat))
Meta.check e
Meta.check ty
guard <| ← Meta.isDefEq (← Meta.inferType e) ty
guard <| (← Meta.getLevel ty) == Level.zero.succ.succ
run_cmd Elab.Command.liftTermElabM do
Meta.check <| toExpr <| (MyMaybe.some (ULift.up 2) : MyMaybe (ULift.{1,0} Nat))
deriving instance ToExpr for List
inductive Foo
| l (x : List Foo)
deriving ToExpr
run_cmd Elab.Command.liftTermElabM <|
Meta.check <| toExpr (Foo.l [Foo.l [], Foo.l [Foo.l []]])
/--
error: failed to synthesize
ToExpr (Bool → Nat)
Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.
-/
#guard_msgs in
inductive Bar
| func (x : Bool → Nat)
deriving ToExpr
def boolFunHelper (x y : α) (b : Bool) : α := if b then x else y
instance {α : Type u} [ToExpr α] [ToLevel.{u+1}] : ToExpr (Bool → α) where
toExpr g :=
mkApp3 (.const ``boolFunHelper [toLevel.{u+1}])
(toTypeExpr α) (toExpr <| g true) (toExpr <| g false)
toTypeExpr := .forallE `x (.const ``Bool []) (toTypeExpr α) .default
deriving instance ToExpr for Bar
example : True := by
run_tac do
let f : Bool → Nat | false => 0 | true => 1
let e := toExpr <| Bar.func f
Meta.check e
guard <| ← Meta.isDefEq (← Meta.inferType e) (toTypeExpr Bar)
trivial
mutual
inductive A
| a
deriving ToExpr
inductive B
| b
deriving ToExpr
end
run_cmd Elab.Command.liftTermElabM do
Meta.check <| toExpr A.a
Meta.check <| toExpr B.b
mutual
inductive C
| c (x : List D)
deriving ToExpr
inductive D
| b (y : List C)
deriving ToExpr
end
-- An incomplete `ToExpr` instance just to finish deriving `ToExpr` for `Expr`.
instance : ToExpr MData where
toExpr _ := mkConst ``MData.empty
toTypeExpr := mkConst ``MData
deriving instance ToExpr for FVarId
deriving instance ToExpr for MVarId
deriving instance ToExpr for LevelMVarId
deriving instance ToExpr for Level
deriving instance ToExpr for BinderInfo
deriving instance ToExpr for Literal
deriving instance ToExpr for Expr
run_cmd Elab.Command.liftTermElabM do
Meta.check <| toExpr <| toExpr [1,2,3]
end DeriveToExprTests |
.lake/packages/mathlib/MathlibTest/eqns.lean | import Mathlib.Tactic.Eqns
def transpose {m n} (A : m → n → Nat) : n → m → Nat
| i, j => A j i
theorem transpose_apply {m n} (A : m → n → Nat) (i j) : transpose A i j = A j i := rfl
attribute [eqns transpose_apply] transpose
theorem transpose_const {m n} (c : Nat) :
transpose (fun (_i : m) (_j : n) => c) = fun _j _i => c := by
fail_if_success {rw [transpose]}
fail_if_success {simp [transpose]}
funext i j -- the rw below does not work without this line
rw [transpose]
def t : Nat := 0 + 1
theorem t_def : t = 1 := rfl
-- this rw causes lean to generate equations itself for t before the user can register them
theorem t_def' : t = 1 := by rw [t]
-- We used to test that this generated an error,
-- but with asynchronous elaboration, it now longer does!
-- `eqns` still seems to work (via `irreducible_def`) as expected,
-- so for now we just comment out the test...
-- #guard_msgs(error) in
-- attribute [eqns t_def] t
/--
warning: declaration uses 'sorry'
-/
#guard_msgs in
-- the above should error as the above equation would not have changed the output of the below
example (n : Nat) : t = n := by
rw [t]
admit |
.lake/packages/mathlib/MathlibTest/symm.lean | import Mathlib.Algebra.Group.Hom.Defs
import Mathlib.Logic.Equiv.Basic
set_option autoImplicit true
-- testing that the attribute is recognized
@[symm] def eq_symm {α : Type} (a b : α) : a = b → b = a := Eq.symm
example (a b : Nat) : a = b → b = a := by intros; symm; assumption
example (a b : Nat) : a = b → True → b = a := by intro h _; symm at h; assumption
def sameParity : Nat → Nat → Prop
| n, m => n % 2 = m % 2
@[symm] def sameParity_symm (n m : Nat) : sameParity n m → sameParity m n := Eq.symm
example (a b : Nat) : sameParity a b → sameParity b a := by intros; symm; assumption
set_option linter.unusedTactic false in
example (a b c : Nat) (ab : a = b) (bc : b = c) : c = a := by
symm_saturate
-- Run twice to check that we don't add repeated copies.
-- Unfortunately `guard_hyp_nums` doesn't seem to work so I haven't made an assertion.
symm_saturate
apply Eq.trans <;> assumption
structure MulEquiv (M N : Type u) [Mul M] [Mul N] extends M ≃ N, M →ₙ* N
/-- info: MulEquiv : (M N : Type u_1) → [Mul M] → [Mul N] → Type u_1 -/
#guard_msgs in
#check @MulEquiv
infixl:25 " ≃* " => MulEquiv
@[symm]
def foo_symm {M N : Type _} [Mul M] [Mul N] (h : M ≃* N) : N ≃* M :=
{ h.toEquiv.symm with map_mul' :=
(h.toMulHom.inverse h.toEquiv.symm h.left_inv h.right_inv).map_mul }
def MyEq (n m : Nat) := ∃ k, n + k = m ∧ m + k = n
@[symm] lemma MyEq.symm {n m : Nat} (h : MyEq n m) : MyEq m n := by
rcases h with ⟨k, h1, h2⟩
exact ⟨k, h2, h1⟩
example {n m : Nat} (h : MyEq n m) : MyEq m n := by
symm
assumption |
.lake/packages/mathlib/MathlibTest/Traversable.lean | import Mathlib.Tactic.DeriveTraversable
import Mathlib.Control.Traversable.Instances
set_option linter.style.commandStart false
namespace Testing
universe u
structure MyStruct (α : Type) where
y : ℤ
deriving LawfulTraversable
#guard_msgs (drop info) in #synth LawfulTraversable MyStruct
inductive Either (α : Type u)
| left : α → ℤ → Either α
| right : α → Either α
deriving LawfulTraversable
#guard_msgs (drop info) in #synth LawfulTraversable Either
structure MyStruct2 (α : Type u) : Type u where
x : α
y : ℤ
η : List α
k : List (List α)
deriving LawfulTraversable
#guard_msgs (drop info) in #synth LawfulTraversable MyStruct2
inductive RecData (α : Type u) : Type u
| nil : RecData α
| cons : ℕ → α → RecData α → RecData α → RecData α
deriving LawfulTraversable
#guard_msgs (drop info) in #synth LawfulTraversable RecData
unsafe structure MetaStruct (α : Type u) : Type u where
x : α
y : ℤ
z : List α
k : List (List α)
w : Lean.Expr
deriving Traversable
#guard_msgs (drop info) in #synth Traversable MetaStruct
inductive MyTree (α : Type)
| leaf : MyTree α
| node : MyTree α → MyTree α → α → MyTree α
deriving LawfulTraversable
#guard_msgs (drop info) in #synth LawfulTraversable MyTree
inductive MyTree' (α : Type)
| leaf : MyTree' α
| node : MyTree' α → α → MyTree' α → MyTree' α
deriving LawfulTraversable
#guard_msgs (drop info) in #synth LawfulTraversable MyTree'
section
open MyTree hiding traverse
def x : MyTree (List Nat) :=
node
leaf
(node
(node leaf leaf [1,2,3])
leaf
[3,2])
[1]
/-- demonstrate the nested use of `traverse`. It traverses each node of the tree and
in each node, traverses each list. For each `ℕ` visited, apply an action `ℕ → StateM (List ℕ) Unit`
which adds its argument to the state. -/
def ex : StateM (List ℕ) (MyTree <| List Unit) := do
let xs ← traverse (traverse fun a => modify <| List.cons a) x
return xs
example : (ex.run []).1 = node leaf (node (node leaf leaf [(), (), ()]) leaf [(), ()]) [()] := rfl
example : (ex.run []).2 = [1, 2, 3, 3, 2, 1] := rfl
end
end Testing |
.lake/packages/mathlib/MathlibTest/Inhabit.lean | import Mathlib.Tactic.Inhabit
namespace InhabitTests
universe u
-- Most basic test (prop)
noncomputable example {p : Prop} [Nonempty p] : Inhabited p := by
inhabit p
assumption
-- Most basic test (nonprop)
noncomputable example {α : Type} [Nonempty α] : Inhabited α := by
inhabit α
assumption
-- Confirms inhabit can handle higher type universes
noncomputable example {α : Type 3} [Nonempty α] : Inhabited α := by
inhabit α
assumption
-- Confirms that inhabit can find Nonempty instances even if they're in the local context
noncomputable example {α : Type} : Nonempty α → Inhabited α := by
intro nonempty_α
inhabit α
assumption
-- Confirms that inhabit assigns to the correct name when given one
noncomputable example {p : Prop} [Nonempty p] : Inhabited p := by
inhabit p_inhabited : p
exact p_inhabited
-- Test for the issue that required generalizing `nonempty_to_inhabited` to `Sort`.
section Unique
structure Unique (α : Sort u) extends Inhabited α where
uniq : ∀ a : α, a = default
/-- Construct `unique` from `inhabited` and `subsingleton`. Making this an instance would create
a loop in the class inheritance graph. -/
abbrev Unique.mk' (α : Sort u) [h₁ : Inhabited α] [Subsingleton α] : Unique α :=
{ h₁ with uniq := fun _ => Subsingleton.elim _ _ }
noncomputable example {α : Sort u} [Subsingleton α] [Nonempty α] : Unique α := by
inhabit α
exact Unique.mk' α
end Unique
axiom α : Type
axiom a : α
instance : Nonempty α := Nonempty.intro a
-- Confirms that inhabit can find Nonempty instances that aren't in the local context
noncomputable example : Inhabited α := by
inhabit α
assumption
end InhabitTests |
.lake/packages/mathlib/MathlibTest/meta.lean | import Lean.Elab.Tactic.ElabTerm
import Lean.Elab.Tactic.Rfl
import Mathlib.Lean.Expr.Basic
import Mathlib.Lean.Meta.Basic
import Mathlib.Tactic.Relation.Rfl
/-! # Tests for mathlib extensions to `Lean.Expr` and `Lean.Meta` -/
namespace Tests
open Lean Meta
private axiom test_sorry : ∀ {α}, α
set_option linter.style.setOption false in
set_option pp.unicode.fun true
def eTrue := Expr.const ``True []
def eFalse := Expr.const ``False []
def eNat := Expr.const ``Nat []
def eNatZero := Expr.const ``Nat.zero []
def eNatOne := mkApp (Expr.const ``Nat.succ []) eNatZero
/-! ### `Lean.Expr.getAppApps` -/
#guard Expr.getAppApps (.const `f []) == #[]
#guard Expr.getAppApps (mkAppN (.const `f []) #[eTrue]) == #[.app (.const `f []) eTrue]
#guard Expr.getAppApps (mkAppN (.const `f []) #[eTrue, eFalse]) ==
#[.app (.const `f []) eTrue, .app (.app (.const `f []) eTrue) eFalse]
/-! ### `Lean.Expr.reduceProjStruct?` -/
/-- info: none -/
#guard_msgs in #eval Expr.reduceProjStruct? eTrue
/-- info: none -/
#guard_msgs in #eval Expr.reduceProjStruct? (.const ``Prod.fst [levelOne, levelOne])
/-- info: some (Lean.Expr.app (Lean.Expr.const `Nat.succ []) (Lean.Expr.const `Nat.zero [])) -/
#guard_msgs in #eval Expr.reduceProjStruct? <|
mkAppN (.const ``Prod.fst [levelOne, levelOne])
#[eNat, eNat, mkAppN (.const ``Prod.mk [levelOne, levelOne]) #[eNat, eNat, eNatOne, eNatZero]]
/-- info: some (Lean.Expr.const `Nat.zero []) -/
#guard_msgs in #eval Expr.reduceProjStruct? <|
mkAppN (.const ``Prod.snd [levelOne, levelOne])
#[eNat, eNat, mkAppN (.const ``Prod.mk [levelOne, levelOne]) #[eNat, eNat, eNatOne, eNatZero]]
/--
error: ill-formed expression, Prod.fst is the 1-th projection function
but Prod.mk does not have enough arguments
-/
#guard_msgs (error, drop info) in #eval Expr.reduceProjStruct? <|
mkAppN (.const ``Prod.fst [levelOne, levelOne])
#[eNat, eNat, mkAppN (.const ``Prod.mk [levelOne, levelOne]) #[eNat, eNat]]
/-! ### `Lean.Expr.forallNot_of_notExists` -/
open Elab Tactic in
elab "test_forallNot_of_notExists" t:term : tactic => do
let e ← elabTerm t none
let ety ← instantiateMVars (← inferType e)
let .app (.const ``Not []) ety' := ety | throwError "Type of expression must be not"
let (ety', e') ← Expr.forallNot_of_notExists ety' e
unless ← isDefEq ety' (← inferType e') do throwError "bad proof"
logInfo m!"{ety'}"
set_option linter.unusedTactic false
set_option linter.unusedVariables false
/-- info: ∀ (x : Nat), ¬0 < x -/
#guard_msgs in
example (h : ¬ ∃ x, 0 < x) : False := by
test_forallNot_of_notExists h
exact test_sorry
/-- info: ∀ (x x_1 : Nat), ¬x_1 < x -/
#guard_msgs in
example (h : ¬ ∃ x y : Nat, y < x) : False := by
test_forallNot_of_notExists h
exact test_sorry
/-- error: failed -/
#guard_msgs in
example (h : ¬ 0 < 1) : False := by
test_forallNot_of_notExists h
/-! ### `Lean.Meta.mkRel` -/
/-- info: true -/
#guard_msgs in
#eval do return (← mkRel ``Eq eNatZero eNatOne) == (← mkAppM ``Eq #[eNatZero, eNatOne])
/-- info: true -/
#guard_msgs in
#eval do return (← mkRel ``Iff eTrue eFalse) == (← mkAppM ``Iff #[eTrue, eFalse])
/-- info: true -/
#guard_msgs in
#eval do return (← mkRel ``HEq eTrue eFalse) == (← mkAppM ``HEq #[eTrue, eFalse])
/-- info: true -/
#guard_msgs in
#eval do return (← mkRel ``LT.lt eNatZero eNatOne) == (← mkAppM ``LT.lt #[eNatZero, eNatOne])
/-! ### `Lean.Expr.relSidesIfRefl?` -/
/--
info: some (`Eq, Lean.Expr.const `Nat.zero [],
Lean.Expr.app (Lean.Expr.const `Nat.succ []) (Lean.Expr.const `Nat.zero []))
-/
#guard_msgs in #eval do Expr.relSidesIfRefl? (← mkRel ``Eq eNatZero eNatOne)
/-- info: some (`Iff, Lean.Expr.const `True [], Lean.Expr.const `False []) -/
#guard_msgs in #eval do Expr.relSidesIfRefl? (← mkRel ``Iff eTrue eFalse)
/-- info: some (`HEq, Lean.Expr.const `True [], Lean.Expr.const `False []) -/
#guard_msgs in #eval do Expr.relSidesIfRefl? (← mkRel ``HEq eTrue eFalse)
/-- info: none -/
#guard_msgs in #eval do Expr.relSidesIfRefl? (← mkRel ``LT.lt eNatZero eNatOne)
attribute [refl] Nat.le_refl
/--
info: some (`LE.le, Lean.Expr.const `Nat.zero [],
Lean.Expr.app (Lean.Expr.const `Nat.succ []) (Lean.Expr.const `Nat.zero []))
-/
#guard_msgs in #eval do Expr.relSidesIfRefl? (← mkRel ``LE.le eNatZero eNatOne)
end Tests |
.lake/packages/mathlib/MathlibTest/ClearExcept.lean | import Mathlib.Tactic.ClearExcept
set_option linter.unusedTactic false
set_option linter.unusedVariables false
-- Most basic test
example (_delete_this : Nat) (dont_delete_this : Int) : Nat := by
clear * - dont_delete_this
fail_if_success assumption
exact dont_delete_this.toNat
-- Confirms that clearExcept does not delete class instances
example [dont_delete_this : Inhabited Nat] (dont_delete_this2 : Prop) : Inhabited Nat := by
clear * - dont_delete_this2
assumption
-- Confirms that clearExcept can clear hypotheses even when they have dependencies
example (delete_this : Nat) (_delete_this2 : delete_this = delete_this) (dont_delete_this : Int) :
Nat := by
clear * - dont_delete_this
fail_if_success assumption
exact dont_delete_this.toNat
-- Confirms that clearExcept does not clear hypotheses
-- when they have dependencies that should not be cleared
example (dont_delete_this : Nat) (dont_delete_this2 : dont_delete_this = dont_delete_this) :
Nat := by
clear * - dont_delete_this2
exact dont_delete_this
-- Confirms that clearExcept can preserve multiple identifiers
example (_delete_this : Nat) (dont_delete_this : Int) (dont_delete_this2 : Int) : Nat := by
clear * - dont_delete_this dont_delete_this2
fail_if_success assumption
exact dont_delete_this.toNat + dont_delete_this2.toNat |
.lake/packages/mathlib/MathlibTest/congr.lean | import Mathlib.Tactic.CongrExclamation
import Mathlib.Algebra.BigOperators.Ring.List
import Mathlib.Algebra.Group.Basic
import Mathlib.Data.Subtype
private axiom test_sorry : ∀ {α}, α
set_option autoImplicit true
-- Useful for debugging the generated congruence theorems
--set_option trace.Meta.CongrTheorems true
theorem ex1 (a b c : Nat) (h : a = b) : a + c = b + c := by
congr!
theorem ex2 (a b : Nat) (h : a = b) : ∀ c, a + c = b + c := by
congr!
theorem ex3 (a b : Nat) (h : a = b) : (fun c => a + c) = (fun c => b + c) := by
congr!
theorem ex4 (a b : Nat) : Fin (a + b) = Fin (b + a) := by
congr! 1
guard_target = a + b = b + a
apply Nat.add_comm
theorem ex5 : ((a : Nat) → Fin (a + 1)) = ((a : Nat) → Fin (1 + a)) := by
congr! 2 with a
guard_target = a + 1 = 1 + a
apply Nat.add_comm
theorem ex6 : ((a : Nat) × Fin (a + 1)) = ((a : Nat) × Fin (1 + a)) := by
congr! 3 with a
guard_target = a + 1 = 1 + a
apply Nat.add_comm
theorem ex7 (p : Prop) (h1 h2 : p) : h1 = h2 := by
congr!
theorem ex8 (p q : Prop) (h1 : p) (h2 : q) : h1 ≍ h2 := by
congr!
theorem ex9 (a b : Nat) (h : a = b) : a + 1 ≤ b + 1 := by
congr!
theorem ex10 (x y : Unit) : x = y := by
congr!
theorem ex11 (p q r : Nat → Prop) (h : q = r) : (∀ n, p n → q n) ↔ (∀ n, p n → r n) := by
congr!
theorem ex12 (p q : Prop) (h : p ↔ q) : p = q := by
congr!
theorem ex13 (x y : α) (h : x = y) (f : α → Nat) : f x = f y := by
congr!
theorem ex14 {α : Type} (f : Nat → Nat) (h : ∀ x, f x = 0) (z : α) (hz : z ≍ 0) :
f ≍ fun (_ : α) => z := by
congr!
· guard_target = Nat = α
exact type_eq_of_heq hz.symm
next n x _ =>
guard_target = f n ≍ z
rw [h]
exact hz.symm
theorem ex15 (p q : Nat → Prop) :
(∀ ε > 0, p ε) ↔ ∀ ε > 0, q ε := by
congr! 2 with ε hε
guard_hyp hε : ε > 0
guard_target = p ε ↔ q ε
exact test_sorry
/- Generating type equalities is OK if it's possible they're the same type. -/
example (s t : Set α) : (ℕ × Subtype s) = (ℕ × Subtype t) := by
congr! 1
guard_target = Subtype s = Subtype t
congr! 1
guard_target = s = t
exact test_sorry
/- `Subtype s = Subtype t` is plausible -/
example (s t : Set α) (f : Subtype s → α) (g : Subtype t → α) :
Set.image f Set.univ = Set.image g Set.univ := by
congr!
· guard_target = s = t
exact test_sorry
· guard_target = f ≍ g
exact test_sorry
set_option linter.unusedTactic false in
/- `ι = κ` is not plausible -/
-- This test does not work unless we specify that `ι` and `κ` lie in the same universe.
-- Prior to https://github.com/leanprover/lean4/pull/4493 it did,
-- because previously bodies of `example`s were (confusingly!) allowed to
-- affect the elaboration of the signature!
example {ι κ : Type u} (f : ι → α) (g : κ → α) :
Set.image f Set.univ = Set.image g Set.univ := by
congr!
guard_target = Set.image f Set.univ = Set.image g Set.univ
congr! +typeEqs
· guard_target = ι = κ
exact test_sorry
· guard_target = f ≍ g
exact test_sorry
/- Generating type equalities is not OK if they're not likely to be the same type. -/
example (s : Set α) (t : Set β) : (ℕ × Subtype s) = (ℕ × Subtype t) := by
congr!
guard_target = Subtype s = Subtype t
exact test_sorry
/- Congruence here is OK since `Fin m = Fin n` is plausible to prove. -/
example (m n : Nat) (h : m = n) (x : Fin m) (y : Fin n) : x + x ≍ y + y := by
congr!
guard_target = x ≍ y
exact test_sorry
guard_target = x ≍ y
exact test_sorry
/- Props are types, but prop equalities are totally plausible. -/
example (p q r : Prop) : p ∧ q ↔ p ∧ r := by
congr!
guard_target = q ↔ r
exact test_sorry
set_option linter.unusedTactic false in
/- Congruence here is not OK by default since `α = β` is not generally plausible. -/
example (α β) [inst1 : Add α] [inst2 : Add β] (x : α) (y : β) : x + x ≍ y + y := by
congr!
guard_target = x + x ≍ y + y
-- But with typeEqs we can get it to generate the congruence anyway:
have : α = β := test_sorry
have : inst1 ≍ inst2 := test_sorry
congr! +typeEqs
guard_target = x ≍ y
exact test_sorry
guard_target = x ≍ y
exact test_sorry
example (prime : Nat → Prop) (n : Nat) :
prime (2 * n + 1) = prime (n + n + 1) := by
congr!
· guard_target =ₛ (HMul.hMul : Nat → Nat → Nat) = HAdd.hAdd
exact test_sorry
· guard_target = 2 = n
exact test_sorry
example (prime : Nat → Prop) (n : Nat) :
prime (2 * n + 1) = prime (n + n + 1) := by
congr! +etaExpand
· guard_target =ₛ (fun (x y : Nat) => x * y) = (fun (x y : Nat) => x + y)
exact test_sorry
· guard_target = 2 = n
exact test_sorry
example (prime : Nat → Prop) (n : Nat) :
prime (2 * n + 1) = prime (n + n + 1) := by
congr! 2
guard_target = 2 * n = n + n
exact test_sorry
example (prime : Nat → Prop) (n : Nat) :
prime (2 * n + 1) = prime (n + n + 1) := by
congr! (config := .unfoldSameFun)
guard_target = 2 * n = n + n
exact test_sorry
opaque partiallyApplied (p : Prop) [Decidable p] : Nat → Nat
-- Partially applied dependent functions
example : partiallyApplied (True ∧ True) = partiallyApplied True := by
congr!
decide
inductive walk (α : Type) : α → α → Type
| nil (n : α) : walk α n n
def walk.map (f : α → β) (w : walk α x y) : walk β (f x) (f y) :=
match x, y, w with
| _, _, .nil n => .nil (f n)
example (w : walk α x y) (w' : walk α x' y') (f : α → β) : w.map f ≍ w'.map f := by
congr!
guard_target = x = x'
exact test_sorry
guard_target = y = y'
exact test_sorry
-- get x = y and y = y' in context for `w ≍ w'` goal.
have : x = x' := by assumption
have : y = y' := by assumption
guard_target = w ≍ w'
exact test_sorry
example (w : walk α x y) (w' : walk α x' y') (f : α → β) : w.map f ≍ w'.map f := by
congr! with rfl rfl
guard_target = x = x'
exact test_sorry
guard_target = y = y'
exact test_sorry
guard_target = w = w'
exact test_sorry
def MySet (α : Type _) := α → Prop
def MySet.image (f : α → β) (s : MySet α) : MySet β := fun y => ∃ x, s x ∧ f x = y
-- Testing for equality between what are technically partially applied functions
example (s t : MySet α) (f g : α → β) (h1 : s = t) (h2 : f = g) :
MySet.image f s = MySet.image g t := by
congr!
example (c : Prop → Prop → Prop → Prop) (x x' y z z' : Prop)
(h₀ : x ↔ x') (h₁ : z ↔ z') : c x y z ↔ c x' y z' := by
congr!
example {α β γ δ} {F : ∀ {α β}, (α → β) → γ → δ} {f g : α → β} {s : γ} (h : ∀ (x : α), f x = g x) :
F f s = F g s := by
congr!
funext
apply h
example {α β} {f : _ → β} {x y : {x : {x : α // x = x} // x = x}} (h : x.1 = y.1) :
f x = f y := by
congr! 1
ext1
exact h
example {α β} {F : _ → β} {f g : {f : α → β // f = f}}
(h : ∀ x : α, (f : α → β) x = (g : α → β) x) :
F f = F g := by
congr!
ext x
apply h
set_option linter.unusedVariables false in
example {ls : List ℕ} :
ls.map (fun x => (ls.map (fun y => 1 + y)).sum + 1) =
ls.map (fun x => (ls.map (fun y => Nat.succ y)).sum + 1) := by
congr! 6 with - y
guard_target = 1 + y = y.succ
rw [Nat.add_comm]
example {ls : List ℕ} {f g : ℕ → ℕ} {h : ∀ x, f x = g x} :
ls.map (fun x => f x + 3) = ls.map (fun x => g x + 3) := by
congr! 3 with x -- it's a little too powerful and will get to `f = g`
exact h x
-- succeed when either `ext` or `congr` can close the goal
example : () = () := by congr!
example : 0 = 0 := by congr!
example {α} (a : α) : a = a := by congr!
example {α} (a b : α) (h : false) : a = b := by
fail_if_success { congr! }
cases h
def g (x : Nat) : Nat := x + 1
example (x y z : Nat) (h : x = z) (hy : y = 2) : 1 + x + y = g z + 2 := by
congr!
guard_target = HAdd.hAdd 1 = g
funext
simp [g, Nat.add_comm]
set_option linter.unusedTactic false in
example (Fintype : Type → Type)
(α β : Type) (inst : Fintype α) (inst' : Fintype β) : inst ≍ inst' := by
congr!
guard_target = inst ≍ inst'
exact test_sorry
/- Here, `Fintype` is a subsingleton class so the `HEq` reduces to `Fintype α = Fintype β`.
Since these are explicit type arguments with no forward dependencies, this reduces to `α = β`.
Generating a type equality seems like the right thing to do in this context.
Usually `inst ≍ inst'` wouldn't be generated as a subgoal with the default `typeEqs := false`. -/
example (Fintype : Type → Type) [∀ γ, Subsingleton (Fintype γ)]
(α β : Type) (inst : Fintype α) (inst' : Fintype β) : inst ≍ inst' := by
congr!
guard_target = α = β
exact test_sorry
example : n = m → 3 + n = m + 3 := by
congr! 0 with rfl
guard_target = 3 + n = n + 3
apply add_comm
example (x y x' y' : Nat) (hx : x = x') (hy : y = y') : x + y = x' + y' := by
congr! -closePre -closePost
exact hx
exact hy
example (x y x' : Nat) (hx : id x = id x') : x + y = x' + y := by
congr!
example (x y x' : Nat) (hx : id x = id x') : x + y = x' + y := by
congr! -closePost
exact hx
set_option linter.unusedTactic false in
example : { f : Nat → Nat // f = id } :=
⟨?_, by
-- prevents `rfl` from solving for `?m` in `?m = id`:
congr! -closePre -closePost
ext x
exact Nat.zero_add x⟩
-- Regression test. From fixing a "declaration has metavariables" bug
example (h : z = y) : (x = y ∨ x = z) → x = y := by
congr! with (rfl|rfl)
example {α} [AddCommMonoid α] [PartialOrder α] {a b c d e f g : α} :
(a + b) + (c + d) + (e + f) + g ≤ a + d + e + f + c + g + b := by
ac_change a + d + e + f + c + g + b ≤ _; rfl
example {α} [AddCommMonoid α] [PartialOrder α] {a b c d e f g : α} :
(a + b) + (c + d) + (e + f) + g ≤ a + d + e + f + c + b + g := by
ac_change a + d + e + f + c + g + b ≤ a + d + e + f + c + g + b
rfl
/-!
Lawful BEq instances are "subsingletons".
-/
example (inst1 : BEq α) [LawfulBEq α] (inst2 : BEq α) [LawfulBEq α] (xs : List α) (x : α) :
@List.erase _ inst1 xs x = @List.erase _ inst2 xs x := by
congr!
/--
error: unsolved goals
case h.e'_2
α : Type
inst1 : BEq α
inst✝¹ : LawfulBEq α
inst2 : BEq α
inst✝ : LawfulBEq α
xs : List α
x : α
⊢ inst1 = inst2
-/
#guard_msgs in
example
{α : Type} (inst1 : BEq α) [LawfulBEq α] (inst2 : BEq α) [LawfulBEq α] (xs : List α) (x : α) :
@List.erase _ inst1 xs x = @List.erase _ inst2 xs x := by
congr! -beqEq
/-!
Check that congruence theorem generator operates at default transparency.
Fixes error reported on Zulip:
https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/congr!.20internal.20error/near/464820779
-/
def F := ∀ x : ℕ, x = 0 → ℕ
def F.A (_ : F) : ℕ := 0
def F.B (_ : F) : ℕ := 0
theorem bug (H : F) (hp : H.A = 0) (hp' : H.B = 0) :
H H.A hp = H H.B hp' := by with_reducible congr! |
.lake/packages/mathlib/MathlibTest/Recall.lean | import Mathlib.Tactic.Recall
import Mathlib.Analysis.Calculus.Deriv.Basic
import Mathlib.Analysis.Complex.Trigonometric
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
set_option linter.style.setOption false
-- Remark: When the test is run by make/CI, this option is not set, so we set it here.
set_option pp.unicode.fun true
set_option autoImplicit true
/-
Motivating examples from the initial Zulip thread:
https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/recall.20command
-/
section
variable {𝕜 : Type _} [NontriviallyNormedField 𝕜]
variable {E : Type _} [AddCommGroup E] [Module 𝕜 E] [TopologicalSpace E]
variable {F : Type _} [AddCommGroup F] [Module 𝕜 F] [TopologicalSpace F]
recall HasFDerivAt (f : E → F) (f' : E →L[𝕜] F) (x : E) :=
HasFDerivAtFilter f f' x (nhds x)
end
/--
error: value mismatch
Complex.exp
has value
id
but is expected to have value
fun z ↦ (Complex.exp' z).lim
-/
#guard_msgs in recall Complex.exp : ℂ → ℂ := id
/--
error: value mismatch
Real.pi
has value
0
but is expected to have value
2 * Classical.choose Real.exists_cos_eq_zero
-/
#guard_msgs in recall Real.pi : ℝ := 0
/-
Other example tests
-/
recall id (x : α) : α := x
/--
error: Type mismatch
@id
has type
{α : Sort u_1} → α → α → ℕ
of sort `Type u_1` but is expected to have type
{α : Sort u} → α → α
of sort `Sort (imax (u + 1) u)`
-/
#guard_msgs in recall id (_x _y : α) : ℕ := 0
recall id (_x : α) : α
def foo := 22
recall foo := 22
recall foo : Nat
/--
error: value mismatch
foo
has value
23
but is expected to have value
22
-/
#guard_msgs in recall foo := 23
recall Nat.add_comm (n m : Nat) : n + m = m + n
-- Caveat: the binder kinds are not checked
recall Nat.add_comm {n m : Nat} : n + m = m + n
-- https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/recall.20command/near/376648750
recall add_assoc {G : Type _} [AddSemigroup G] (a b c : G) : a + b + c = a + (b + c)
recall add_assoc
/-- error: unknown constant 'nonexistent' -/
#guard_msgs in recall nonexistent
axiom bar : Nat
/-- error: constant 'bar' has no defined value -/
#guard_msgs in recall bar := bar
recall List.cons_append (a : α) (as bs : List α) : (a :: as) ++ bs = a :: (as ++ bs) := rfl |
.lake/packages/mathlib/MathlibTest/ExistsI.lean | import Mathlib.Tactic.ExistsI
example : ∃ x : Nat, x = x := by
existsi 42
rfl
example : ∃ x : Nat, ∃ y : Nat, x = y := by
existsi 42, 42
rfl |
.lake/packages/mathlib/MathlibTest/MonCat.lean | import Mathlib.Algebra.Category.MonCat.Basic
-- We verify that the coercions of morphisms to functions work correctly:
example {R S : MonCat} (f : R ⟶ S) : ↑R → ↑S := f
-- It's essential that simp lemmas for `→*` apply to morphisms.
example {R S : MonCat} (i : R ⟶ S) (r : R) (h : r = 1) : i r = 1 := by simp [h]
example {R S : CommMonCat} (f : R ⟶ S) : ↑R → ↑S := f
example {R S : CommMonCat} (i : R ⟶ S) (r : R) (h : r = 1) : i r = 1 := by simp [h]
-- We verify that when constructing a morphism in `CommMonCat`,
-- when we construct the `toFun` field, the types are presented as `↑R`.
example (R : CommMonCat.{u}) : R ⟶ R := CommMonCat.ofHom
{ toFun := fun x => by
match_target (R : Type u)
guard_hyp x : (R : Type u)
exact x * x
map_one' := by simp
map_mul' := fun x y => by
rw [mul_assoc x y (x * y), ← mul_assoc y x y, mul_comm y x, mul_assoc, mul_assoc] } |
.lake/packages/mathlib/MathlibTest/CalcQuestionMark.lean | import Mathlib.Tactic.Widget.Calc
/-!
Note that while the suggestions look incorrectly indented here,
this is an artifact of the rendering to a string for `guard_msgs` (https://github.com/leanprover/lean4/issues/7191).
When used from the widget that appears in VSCode, they insert correctly-indented code.
-/
/--
info: Create calc tactic:
[apply] calc
1 = 1 := by sorry
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example : 1 = 1 := by
have := 0
calc?
/--
info: Create calc tactic:
[apply] calc
a ≤ a := by sorry
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example (a : Nat) : a ≤ a := by
calc?
-- an indented `calc?`
/--
info: Create calc tactic:
[apply] calc
a ≤ a := by sorry
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example (a : Nat) : a ≤ a := by
all_goals
calc?
-- a deliberately long line
/--
info: Create calc tactic:
[apply] calc
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 +
1 +
1 =
8 + 8 + 8 + 8 :=
by sorry
---
warning: declaration uses 'sorry'
-/
#guard_msgs in
example :
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = 8 + 8 + 8 + 8 := by
calc? |
.lake/packages/mathlib/MathlibTest/NoncommRing.lean | import Mathlib.GroupTheory.GroupAction.Ring
import Mathlib.Tactic.NoncommRing
local infix:70 " ⚬ " => fun a b => a * b + b * a
variable {R : Type _} [Ring R]
variable (a b c : R)
example : 0 = 0 := by noncomm_ring
example : a = a := by noncomm_ring
example : (a + b) * c = a * c + b * c := by noncomm_ring
example : a * (b + c) = a * b + a * c := by noncomm_ring
example : a - b = a + -b := by noncomm_ring
example : a * b * c = a * (b * c) := by noncomm_ring
example : a + a = 2 * a := by noncomm_ring
example : a + a = a * 2 := by noncomm_ring
example : -a - a = a * (-2) := by noncomm_ring
example : -a = (-1) * a := by noncomm_ring
example : a + a + a = 3 * a := by noncomm_ring
example : a ^ 2 = a * a := by noncomm_ring
example : a ^ 3 = a * a * a := by noncomm_ring
example : (-a) * b = -(a * b) := by noncomm_ring
example : a * (-b) = -(a * b) := by noncomm_ring
example : a * (b + c + b + c - 2*b) = 2*a*c := by noncomm_ring
example : a * (b + c + b + c - (2 : ℕ) • b) = 2*a*c := by noncomm_ring
example : (a + b)^2 = a^2 + a*b + b*a + b^2 := by noncomm_ring
example : (a - b)^2 = a^2 - a*b - b*a + b^2 := by noncomm_ring
example : (a + b)^3 = a^3 + a^2*b + a*b*a + a*b^2 + b*a^2 + b*a*b + b^2*a + b^3 := by noncomm_ring
example : (a - b)^3 = a^3 - a^2*b - a*b*a + a*b^2 - b*a^2 + b*a*b + b^2*a - b^3 := by noncomm_ring
example : ⁅a, a⁆ = 0 := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a, b⁆ = -⁅b, a⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a + b, c⁆ = ⁅a, c⁆ + ⁅b, c⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a, b + c⁆ = ⁅a, b⁆ + ⁅a, c⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a, ⁅b, c⁆⁆ + ⁅b, ⁅c, a⁆⁆ + ⁅c, ⁅a, b⁆⁆ = 0 := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅⁅a, b⁆, c⁆ + ⁅⁅b, c⁆, a⁆ + ⁅⁅c, a⁆, b⁆ = 0 := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a, ⁅b, c⁆⁆ = ⁅⁅a, b⁆, c⁆ + ⁅b, ⁅a, c⁆⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅⁅a, b⁆, c⁆ = ⁅⁅a, c⁆, b⁆ + ⁅a, ⁅b, c⁆⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a * b, c⁆ = a * ⁅b, c⁆ + ⁅a, c⁆ * b := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a, b * c⁆ = ⁅a, b⁆ * c + b * ⁅a, c⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅3 * a, a⁆ = 0 := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a * -5, a⁆ = 0 := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a^3, a⁆ = 0 := by simp only [Ring.lie_def]; noncomm_ring
example : a ⚬ a = 2*a^2 := by noncomm_ring
example : (2 * a) ⚬ a = 4*a^2 := by noncomm_ring
example : a ⚬ b = b ⚬ a := by noncomm_ring
example : a ⚬ (b + c) = a ⚬ b + a ⚬ c := by noncomm_ring
example : (a + b) ⚬ c = a ⚬ c + b ⚬ c := by noncomm_ring
example : (a ⚬ b) ⚬ (a ⚬ a) = a ⚬ (b ⚬ (a ⚬ a)) := by noncomm_ring
example : ⁅a, b ⚬ c⁆ = ⁅a, b⁆ ⚬ c + b ⚬ ⁅a, c⁆ := by simp only [Ring.lie_def]; noncomm_ring
example : ⁅a ⚬ b, c⁆ = a ⚬ ⁅b, c⁆ + ⁅a, c⁆ ⚬ b := by simp only [Ring.lie_def]; noncomm_ring
example : (a ⚬ b) ⚬ c - a ⚬ (b ⚬ c) = -⁅⁅a, b⁆, c⁆ + ⁅a, ⁅b, c⁆⁆ := by
simp only [Ring.lie_def]; noncomm_ring
example : a + -b = -b + a := by
-- This should print "`noncomm_ring` simp lemmas don't apply; try `abel` instead"
-- but I don't know how to test for this:
-- See https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/.60fail.60.20that.20doesn't.20print.20the.20goal.3F/near/382280010
fail_if_success noncomm_ring
abel
example : a ^ 50 * a ^ 37 = a ^ 23 * a ^ 64 := by noncomm_ring
/- some examples using arguments -/
example (h : ∀ a : R, (2 : ℤ) • a = 0) : (a + 1) ^ 2 = a ^ 2 + 1 := by
noncomm_ring [h]
set_option linter.unusedVariables false in
example (h : a = b) (h2 : a = c) : a = c := by
fail_if_success noncomm_ring [h]
noncomm_ring [h2] |
.lake/packages/mathlib/MathlibTest/ValuedCSP.lean | import Mathlib.Algebra.Order.AbsoluteValue.Basic
import Mathlib.Algebra.Order.Ring.Rat
import Mathlib.Combinatorics.Optimization.ValuedCSP
import Mathlib.Data.Fin.Tuple.Curry
import Mathlib.Data.Fin.VecNotation
import Mathlib.Tactic.Positivity
/-!
# VCSP examples
This file shows two simple examples of General-Valued Constraint Satisfaction Problems (see
[ValuedCSP definition](Mathlib/Combinatorics/Optimization/ValuedCSP.lean)).
The first example is an optimization problem. The second example is a decision problem.
-/
def ValuedCSP.unaryTerm {D C : Type} [AddCommMonoid C] [PartialOrder C] [IsOrderedAddMonoid C]
{Γ : ValuedCSP D C} {ι : Type*} {f : D → C}
(ok : ⟨1, Function.OfArity.uncurry f⟩ ∈ Γ) (i : ι) : Γ.Term ι :=
⟨1, Function.OfArity.uncurry f, ok, ![i]⟩
def ValuedCSP.binaryTerm {D C : Type} [AddCommMonoid C] [PartialOrder C] [IsOrderedAddMonoid C]
{Γ : ValuedCSP D C} {ι : Type*} {f : D → D → C}
(ok : ⟨2, Function.OfArity.uncurry f⟩ ∈ Γ) (i j : ι) : Γ.Term ι :=
⟨2, Function.OfArity.uncurry f, ok, ![i, j]⟩
-- ## Example: minimize `|x| + |y|` where `x` and `y` are rational numbers
private def absRat : (Fin 1 → ℚ) → ℚ :=
Function.OfArity.uncurry (@abs ℚ Rat.instLattice Rat.addGroup)
private def exampleAbs : Σ (n : ℕ), (Fin n → ℚ) → ℚ := ⟨1, absRat⟩
private def exampleFiniteValuedCSP : ValuedCSP ℚ ℚ := {exampleAbs}
private lemma abs_in : ⟨1, absRat⟩ ∈ exampleFiniteValuedCSP := rfl
private def exampleFiniteValuedInstance : exampleFiniteValuedCSP.Instance (Fin 2) :=
{ValuedCSP.unaryTerm abs_in 0, ValuedCSP.unaryTerm abs_in 1}
example : exampleFiniteValuedInstance.IsOptimumSolution ![(0 : ℚ), (0 : ℚ)] := by
intro s
convert_to 0 ≤ exampleFiniteValuedInstance.evalSolution s
· simp [exampleFiniteValuedInstance, ValuedCSP.Instance.evalSolution]
exact Rat.zero_add 0
rw [ValuedCSP.Instance.evalSolution, exampleFiniteValuedInstance]
convert_to 0 ≤ |s 0| + |s 1|
· simp [ValuedCSP.unaryTerm, ValuedCSP.Term.evalSolution, Function.OfArity.uncurry]
rfl
positivity
-- ## Example: B ≠ A ≠ C ≠ D ≠ B ≠ C with three available labels (i.e., 3-coloring of K₄⁻)
private def Bool_add_le_add_left (a b : Bool) :
(a ≤ b) → ∀ (c : Bool), ((c || a) ≤ (c || b)) := by
intro hab c
cases a <;> cases b <;> cases c <;> trivial
-- For simpler implementation, we treat `false` as "satisfied" and `true` as "wrong" here.
private instance crispCodomainZero : Zero Bool where zero := false
private instance crispCodomainAdd : Add Bool where add a b := a || b
private instance crispCodomainAddCommMonoid : AddCommMonoid Bool where
add_assoc := Bool.or_assoc
zero_add (_ : Bool) := rfl
add_zero := Bool.or_false
add_comm := Bool.or_comm
nsmul := nsmulRec
private instance crispCodomain : IsOrderedAddMonoid Bool where
add_le_add_left := Bool_add_le_add_left
private def beqBool : (Fin 2 → Fin 3) → Bool :=
Function.OfArity.uncurry (fun (a b : Fin 3) => a == b)
private def exampleEquality : Σ (n : ℕ), (Fin n → Fin 3) → Bool := ⟨2, beqBool⟩
private def exampleCrispCSP : ValuedCSP (Fin 3) Bool := {exampleEquality}
private lemma beqBool_mem : ⟨2, beqBool⟩ ∈ exampleCrispCSP := rfl
private def exampleTermAB : exampleCrispCSP.Term (Fin 4) :=
ValuedCSP.binaryTerm beqBool_mem 0 1
private def exampleTermBC : exampleCrispCSP.Term (Fin 4) :=
ValuedCSP.binaryTerm beqBool_mem 1 2
private def exampleTermCA : exampleCrispCSP.Term (Fin 4) :=
ValuedCSP.binaryTerm beqBool_mem 2 0
private def exampleTermBD : exampleCrispCSP.Term (Fin 4) :=
ValuedCSP.binaryTerm beqBool_mem 1 3
private def exampleTermCD : exampleCrispCSP.Term (Fin 4) :=
ValuedCSP.binaryTerm beqBool_mem 2 3
private def exampleCrispCspInstance : exampleCrispCSP.Instance (Fin 4) :=
Multiset.ofList [exampleTermAB, exampleTermBC, exampleTermCA, exampleTermBD, exampleTermCD]
/-
0
/ \
1---2
\ /
0
-/
private def exampleSolutionCorrect0 : Fin 4 → Fin 3 := ![0, 1, 2, 0]
example : exampleCrispCspInstance.IsOptimumSolution exampleSolutionCorrect0 :=
fun _ => Bool.false_le _
/-
1
/ \
2---0
\ /
1
-/
private def exampleSolutionCorrect1 : Fin 4 → Fin 3 := ![1, 2, 0, 1]
example : exampleCrispCspInstance.IsOptimumSolution exampleSolutionCorrect1 :=
fun _ => Bool.false_le _
/-
2
/ \
0---1
\ /
2
-/
private def exampleSolutionCorrect2 : Fin 4 → Fin 3 := ![2, 0, 1, 2]
example : exampleCrispCspInstance.IsOptimumSolution exampleSolutionCorrect2 :=
fun _ => Bool.false_le _
/-
0
/ \
2---1
\ /
0
-/
private def exampleSolutionCorrect3 : Fin 4 → Fin 3 := ![0, 2, 1, 0]
example : exampleCrispCspInstance.IsOptimumSolution exampleSolutionCorrect3 :=
fun _ => Bool.false_le _
/-
1
/ \
0---2
\ /
1
-/
private def exampleSolutionCorrect4 : Fin 4 → Fin 3 := ![1, 0, 2, 1]
example : exampleCrispCspInstance.IsOptimumSolution exampleSolutionCorrect4 :=
fun _ => Bool.false_le _
/-
2
/ \
1---0
\ /
2
-/
private def exampleSolutionCorrect5 : Fin 4 → Fin 3 := ![2, 1, 0, 2]
example : exampleCrispCspInstance.IsOptimumSolution exampleSolutionCorrect5 :=
fun _ => Bool.false_le _
/-
0
/ \
0---0
\ /
0
-/
private def exampleSolutionIncorrect0 : Fin 4 → Fin 3 := ![0, 0, 0, 0]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect0 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
1
/ \
0---0
\ /
0
-/
private def exampleSolutionIncorrect1 : Fin 4 → Fin 3 := ![1, 0, 0, 0]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect1 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
0
/ \
2---0
\ /
0
-/
private def exampleSolutionIncorrect2 : Fin 4 → Fin 3 := ![0, 2, 0, 0]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect2 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
0
/ \
1---0
\ /
2
-/
private def exampleSolutionIncorrect3 : Fin 4 → Fin 3 := ![0, 1, 0, 2]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect3 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
2
/ \
2---0
\ /
1
-/
private def exampleSolutionIncorrect4 : Fin 4 → Fin 3 := ![2, 2, 0, 1]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect4 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
0
/ \
1---2
\ /
1
-/
private def exampleSolutionIncorrect5 : Fin 4 → Fin 3 := ![0, 1, 2, 1]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect5 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
1
/ \
0---0
\ /
1
-/
private def exampleSolutionIncorrect6 : Fin 4 → Fin 3 := ![1, 0, 0, 1]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect6 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl
/-
2
/ \
2---0
\ /
2
-/
private def exampleSolutionIncorrect7 : Fin 4 → Fin 3 := ![2, 2, 0, 2]
example : ¬exampleCrispCspInstance.IsOptimumSolution exampleSolutionIncorrect7 := by
unfold ValuedCSP.Instance.IsOptimumSolution
push_neg
use exampleSolutionCorrect0
rfl |
.lake/packages/mathlib/MathlibTest/MathlibTestExecutable.lean | import Lean
import Std
import Qq
import Batteries
import Aesop
import ProofWidgets
import Mathlib
import Plausible
def main : IO Unit := do
IO.println "Verified that an executable importing all of Mathlib and its upstream dependencies can be built and executed." |
.lake/packages/mathlib/MathlibTest/TermCongr2.lean | import Mathlib.Tactic.TermCongr
import Mathlib.Data.Fintype.Card
import Mathlib.Tactic.Ring
/-!
`congr(...)` tests with heavier imports (`TermCongr` is in `Mathlib/Tactic/Common.lean`)
-/
namespace Tests
set_option autoImplicit true
-- set_option trace.Elab.congr true
example [Fintype α] [Fintype β] (h : α = β) : Fintype.card α = Fintype.card β :=
congr(Fintype.card $h)
example (s t : Set α) [Fintype s] [Fintype t] (h : s = t) : Fintype.card s = Fintype.card t :=
congr(Fintype.card $h)
example (x y : ℤ) (h1 : 3 * x + 2 * y = 10) (h2 : 2 * x + 5 * y = 3) :
11*y = -11 := by
have := congr(-2*$h1 + 3*$h2)
guard_hyp this : -2*(3*x + 2*y) + 3*(2*x + 5*y) = -2*10 + 3*3
ring_nf at this ⊢
exact this
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
have := congr(2*$h1 -1*$h2 +3*$h3 -3*$h4)
guard_hyp this : 2*a - 1*3 + 3*(c*3) -3*(-d) = 2*4 - 1*b + 3*d - 3*a
ring_nf at this ⊢
exact this
end Tests |
.lake/packages/mathlib/MathlibTest/reduce_mod_char.lean | import Mathlib.Tactic.ReduceModChar
/-!
# Tests for `reduce_mod_char` tactic
-/
open Polynomial
-- Numerals:
example : (5 : ZMod 4) = 1 := by reduce_mod_char
-- Negation:
example : (-5 : ZMod 4) = 3 := by reduce_mod_char
example : (-5 : (ZMod 4)[X]) = 3 := by reduce_mod_char
example : (-X : (ZMod 4)[X]) = 3 * X := by reduce_mod_char
-- Polynomials:
example : (4 * X + 3 : (ZMod 4)[X]) = 3 := by reduce_mod_char
example : (5 * X ^ 2 + 4 * X + 3 : (ZMod 4)[X]) = X ^ 2 + 3 := by reduce_mod_char
-- Negation:
example : (X ^ 2 - 3 : (ZMod 4)[X]) = X ^ 2 + 1 := by reduce_mod_char
-- Cleaning up `1 * X` and `X + 0`:
example : (5 * X ^ 2 - 3 * X + 4 : (ZMod 4)[X]) = X ^ 2 + X := by reduce_mod_char
-- Exponentiation:
example : (11 : ZMod 987654319) ^ 987654318 = 1 := by reduce_mod_char
example : (-126432 : ZMod 1235412223) ^ 12355342321 = 1001528716 := by reduce_mod_char
example : (((((5 : ZMod 1235412223) ^ 5) ^ 5) ^ 5) ^ 5) ^ 5 = 806432269 := by reduce_mod_char
example : (2 : ZMod 75) ^ 0 = 1 := by reduce_mod_char
example : (0 : ZMod 34523432) ^ 0 = 1 := by reduce_mod_char
example : (0 : ZMod 56) ^ 90000000000000000 = 0 := by reduce_mod_char
example : (1 : ZMod 119) ^ 433440000000000000000000 = 1 := by reduce_mod_char
example : (75 : ZMod 111) ^ 1 = 75 := by reduce_mod_char
example : (23 : ZMod 19) ^ 1 = 4 := by reduce_mod_char
example : (1923423451 : ZMod 2) ^ 81000000000000000000 = 1 := by reduce_mod_char
example : (19 : ZMod 1) ^ 810000000000000000000 = 0 := by reduce_mod_char
example : (23423432049230423 : ZMod 0) ^ 0 = 1 := by reduce_mod_char
example : (23423432049230423 : ZMod 1) ^ 0 = 0 := by reduce_mod_char
-- A 1024-bit prime number
set_option linter.style.longLine false in
abbrev p : Nat := 0xb10b8f96a080e01dde92de5eae5d54ec52c99fbcfb06a3c69a6a9dca52d23b616073e28675a23d189838ef1e2ee652c013ecb4aea906112324975c3cd49b83bfaccbdd7d90c4bd7098488e9c219a73724effd6fae5644738faa31a4ff55bccc0a151af5f0dc8b4bd45bf37df365c1a65e68cfda76d4da708df1fb2bc2e4a4371
set_option linter.style.longLine false in
abbrev g : Nat := 0xa4d1cbd5c3fd34126765a442efb99905f8104dd258ac507fd6406cff14266d31266fea1e5c41564b777e690f5504f213160217b4b01b886a5e91547f9e2749f4d7fbd7d3b9a92ee1909d0d2263f80a76a6a24c087a091f531dbf0a0169b6a28ad662a4d18e73afa32d779d5918d08bc8858f4dcef97c2a24855e6eeb22b3b2e5
example : (g : ZMod p) ^ (p - 1) = 1 := by reduce_mod_char
-- The 20th Mersenne prime
abbrev q : Nat := 2 ^ 4423 - 1
-- This takes a little time but the 21st Mersenne prime is too large
set_option exponentiation.threshold 10000 in
example : (3 : ZMod q) ^ (q - 1) = 1 := by reduce_mod_char
-- We don't unfold semi-reducible definitions
def q' := 2
/--
error: unsolved goals
⊢ 3 ^ (q' - 1) = 1
-/
#guard_msgs in
example : (3 : ZMod q') ^ (q' - 1) = 1 := by reduce_mod_char
-- Rewriting hypotheses:
example (a : ZMod 7) (h : a + 7 = 2) : a = 2 := by
reduce_mod_char at h
assumption
example (a : ZMod 7) (h : a + 14 = 2) : a + 7 = 2 := by
reduce_mod_char at *
assumption
-- A stress test:
example (a b : ZMod 37) : (a + b)^37 = a^37 + b^37 := by ring_nf; reduce_mod_char
-- From the zulip thread:
-- https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/tactic.20for.20easy.20calculations.20in.20ZMod.20p.3F
lemma foo (a b : ZMod 7) (h : a + 3 * b = 0) : a = 4 * b := by
rw [eq_sub_of_add_eq h]
reduce_mod_char
section Assumption
/-! `reduce_mod_char!` uses `CharP R n` hypotheses it finds in the local context. -/
-- From the Zulip thread:
-- https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/reduce_mod_char.20doesn't.20work
example {R} [CommRing R] [CharP R 3] (x : R) : x + x + x + x = x := by
ring_nf
reduce_mod_char!
example {R} [CommRing R] [CharP R 2] (x y : R) : (x + y) ^ 2 = x ^ 2 + y ^ 2 := by
ring_nf
reduce_mod_char!
example {R : Type*} [Ring R] [CharP R 2] : (2 : R) = 0 := by
reduce_mod_char!
end Assumption
section InferInstance
/-! `reduce_mod_char!` can't do instance synthesis for `CharP R n` if `n` is not known,
so we demonstrate a workaround using `inferInstance`. -/
def ZMod' (n : ℕ) := ZMod n
instance : CommRing (ZMod' n) := inferInstanceAs (CommRing (ZMod n))
instance ZMod'.instCharP : CharP (ZMod' n) n := inferInstanceAs (CharP (ZMod n) n)
example : (2 : ZMod' 2) = 0 := by
have : CharP (ZMod' 2) 2 := inferInstance
reduce_mod_char!
end InferInstance
def test (_ : ZMod 37) : Type := ℕ
-- wildcard `*` is supposed to ignore dependent and non-`Prop` hypotheses
set_option linter.unusedVariables false in
set_option linter.unusedTactic false in
example (a : test 100) : ℕ := by
let x : ZMod 2 := 100
reduce_mod_char at *
guard_hyp a : test 100
exact 0 |
.lake/packages/mathlib/MathlibTest/Clean.lean | import Mathlib.Tactic.Clean
namespace Tests
def x : Id Nat := by dsimp [Id]; exact 1
def x' : Id Nat := clean% by dsimp [Id]; exact 1
/--
info: def Tests.x : Id Nat :=
id 1
-/
#guard_msgs in #print x
/--
info: def Tests.x' : Id Nat :=
1
-/
#guard_msgs in #print x'
-- def x : Id Nat := 1
theorem withClean : 2 + 2 = 4 := clean% by exact id rfl
theorem withoutClean : 2 + 2 = 4 := by exact id rfl
/--
info: theorem Tests.withClean : 2 + 2 = 4 :=
rfl
-/
#guard_msgs in
#print Tests.withClean
/--
info: theorem Tests.withoutClean : 2 + 2 = 4 :=
id rfl
-/
#guard_msgs in
#print Tests.withoutClean
example : True := by
let x : id Nat := by dsimp; exact 1
guard_hyp x :ₛ id Nat := id (1 : Nat)
let x' : id Nat := clean% by dsimp; exact 1
guard_hyp x' :ₛ id Nat := (1 : Nat)
let y := show Nat from 1
guard_hyp y :ₛ Nat := have this := 1; this
let y' := clean% show Nat from 1
guard_hyp y' :ₛ Nat := 1
-- Not a tautological have:
let z := clean% have x := 1; x + x
guard_hyp z :ₛ Nat := have x := 1; x + x
trivial
end Tests |
.lake/packages/mathlib/MathlibTest/DFinsuppMultiLinear.lean | import Mathlib.LinearAlgebra.Multilinear.DFinsupp
import Mathlib.LinearAlgebra.Multilinear.Pi
import Mathlib.Data.DFinsupp.Notation
/--
info: fun₀
| !["hello", "complicated", "world"] => 20
| !["hello", "complicated", "test file"] => 30
| !["goodbye", "complicated", "world"] => -20
| !["goodbye", "complicated", "test file"] => -30
-/
#guard_msgs in
#eval MultilinearMap.dfinsuppFamily
(κ := fun _ => String)
(M := fun _ _ => ℤ)
(N := fun _ => ℤ)
(R := ℤ)
(f := fun _ => MultilinearMap.mkPiAlgebra ℤ (Fin 3) ℤ) ![
fun₀ | "hello" => 1 | "goodbye" => -1,
fun₀ | "complicated" => 10,
fun₀ | "world" => 2 | "test file" => 3] |
.lake/packages/mathlib/MathlibTest/ApplyAt.lean | import Mathlib.Tactic.ApplyAt
import Mathlib.Algebra.Group.Basic
import Mathlib.Data.Real.Basic
example {α β : Type*} (f : α → β) (a : α) : β := by
apply f at a
guard_hyp a :ₛ β
exact a
/-- `apply at` cannot clear mvarid if still used. -/
example {α : Type} (γ : α → Type) (a : α) (f : α → γ a) : γ a := by
apply f at a
rename_i a₂
guard_hyp a :ₛ γ a₂
exact a
example {α β : Type*} (f : α → β) (a b : α) (h : a = b) : f a = f b := by
apply congr_arg f at h
guard_hyp h :ₛ f a = f b
exact h
example (a b : ℕ) (h : a + 1 = b + 1) : a = b := by
apply Nat.succ.inj at h
guard_hyp h :ₛ a = b
exact h
example {G : Type*} [Group G] (a b c : G) (h : a * c = b * c) : a = b := by
apply mul_right_cancel at h
guard_hyp h :ₛ a = b
exact h
example {G : Type*} [Monoid G] (a b c : G) (h : a * c = b * c)
(hh : ∀ x y z : G, x * z = y * z → x = y): a = b := by
apply mul_right_cancel at h
guard_hyp h :ₛ a = b
· exact h
· guard_target = IsRightCancelMul G
constructor
intro a b c
apply hh
example {α β γ δ : Type*} (f : α → β → γ → δ) (a : α) (b : β) (g : γ) : δ := by
apply f at g
guard_hyp g :ₛ δ
assumption'
example {α γ : Type*} {β : α → Type*} {a : α}
(f : {a : α} → β a → γ) (b : β a) : γ := by
apply f at b
guard_hyp b :ₛ γ
exact b
example {α β γ δ : Type*} (f : {_ : α} → β → {_ : γ} → δ) (g : γ) (a : α) (b : β) :
δ := by
apply f at g
guard_hyp g :ₛ δ
assumption'
example {α β γ δ : Type*} (f : {_ : α} → {_ : β} → (g : γ) → δ) (g : γ) (a : α) (b : β) :
δ := by
apply f at g
guard_hyp g :ₛ δ
assumption'
/--
error: Failed to find γ as the type of a parameter of α → β.
-/
#guard_msgs in
example {α β γ : Type*} (f : α → β) (_g : γ) : β × γ := by
apply f at _g
/--
error: Failed: α is not the type of a function.
-/
#guard_msgs in
example {α β : Type*} (a : α) (_b : β) : α × β := by
apply a at _b
example {α β γ : Type*} (f : α → β) (g : γ) (a : α) : β × γ := by
fail_if_success apply f at g
apply f at a
guard_hyp a :ₛ β
exact (a, g)
example {α β : Type*} (a : α) (b : β) : α × β := by
fail_if_success apply a at b
exact (a, b)
example {α β : Type*} (a : α) (b : β) : α × β := by
fail_if_success apply a at b
exact (a, b)
-- testing field notation
example {A B : Prop} (h : A ↔ B) : A → B := by
intro hA
apply h.mp at hA
assumption
example (a : ℝ) (h3 : a + 1 = 0) : a = -1 := by
apply (congrArg (fun x => x - 1)) at h3
simp at h3
assumption
example (a b : ℝ) (h : -a * b = 0) : a = 0 ∨ b = 0 := by
apply (congrArg (fun x => x / 1)) at h
simp at h
assumption
/-- `apply H at h` when type of `H h` depends on proof of `h` (https://github.com/leanprover-community/mathlib4/issues/20623) -/
example (h : True) : True := by
have H (h : True) : h = h := rfl
apply H at h
simp at h
exact h
/-- `apply H at h` when type of `H h` depends on proof of `h` (https://github.com/leanprover-community/mathlib4/issues/20623) -/
example (a : List Nat) (k : Nat) (hk : k < a.length) : True := by
have H (k : Nat) {xs ys : List Nat} (hk: k < xs.length)
(h : xs = ys) : xs[k] = ys[k]'(h ▸ hk) := h ▸ rfl
have h : a = a.map id := by simp
apply H k hk at h
simp at h
exact h |
.lake/packages/mathlib/MathlibTest/globalAttributeIn.lean | import Mathlib.Tactic.Linter.GlobalAttributeIn
/-! Tests for the `globalAttributeIn` linter. -/
-- Test disabling the linter.
set_option linter.globalAttributeIn false
set_option autoImplicit false in
attribute [instance] Int.add in
instance : Inhabited Int where
default := 0
set_option linter.globalAttributeIn true
set_option linter.globalAttributeIn false in
attribute [instance] Int.add in
instance : Inhabited Int where
default := 0
-- Global instances with `in`, are linted, as they are a footgun.
/--
warning: Despite the `in`, the attribute 'instance 1100' is added globally to 'Int.add'
please remove the `in` or make this a `local instance 1100`
Note: This linter can be disabled with `set_option linter.globalAttributeIn false`
-/
#guard_msgs in
set_option autoImplicit false in
attribute [instance 1100] Int.add in
set_option autoImplicit false in
instance : Inhabited Int where
default := 0
/--
warning: Despite the `in`, the attribute 'instance' is added globally to 'Int.add'
please remove the `in` or make this a `local instance`
Note: This linter can be disabled with `set_option linter.globalAttributeIn false`
-/
#guard_msgs in
attribute [instance] Int.add in
instance : Inhabited Int where
default := 0
/--
warning: Despite the `in`, the attribute 'simp' is added globally to 'Int.add'
please remove the `in` or make this a `local simp`
Note: This linter can be disabled with `set_option linter.globalAttributeIn false`
-/
#guard_msgs in
attribute [simp] Int.add in
instance : Inhabited Int where
default := 0
namespace X
-- Here's another example, with nested attributes.
/-- warning: declaration uses 'sorry' -/
#guard_msgs in
theorem foo (x y : Nat) : x = y := sorry
/--
warning: Despite the `in`, the attribute 'simp' is added globally to 'foo'
please remove the `in` or make this a `local simp`
Note: This linter can be disabled with `set_option linter.globalAttributeIn false`
---
warning: Despite the `in`, the attribute 'ext' is added globally to 'foo'
please remove the `in` or make this a `local ext`
Note: This linter can be disabled with `set_option linter.globalAttributeIn false`
-/
#guard_msgs in
attribute [simp, local simp, ext, scoped instance, -simp, -ext] foo in
def bar := False
#guard_msgs in
-- `local instance` is allowed with `in`
attribute [local instance] Int.add in
instance : Inhabited Int where
default := 0
#guard_msgs in
-- `local instance priority` is allowed with `in`
attribute [local instance 42] Int.add in
instance : Inhabited Int where
default := 0
#guard_msgs in
-- `scoped instance` is allowed with `in`
attribute [scoped instance] Int.add in
instance : Inhabited Int where
default := 0
#guard_msgs in
-- `scoped instance priority` is allowed with `in`
attribute [scoped instance 42] Int.add in
instance : Inhabited Int where
default := 0
end X
-- Omitting the `in` is also fine.
attribute [local instance 42] X.foo
-- Global instance without the `in` are also left alone.
attribute [instance 20000] X.foo
namespace X
attribute [scoped instance 0] foo |
.lake/packages/mathlib/MathlibTest/Rify.lean | import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Rify
set_option linter.unusedVariables false
example {n : ℕ} {k : ℤ} (hn : 8 ≤ n) (hk : 2 * k ≤ n + 2) :
(0 : ℝ) < n - k - 1 := by
rify at hn hk
linarith
example {n : ℕ} {k : ℤ} (hn : 8 ≤ n) (hk : (2 : ℚ) * k ≤ n + 2) :
(0 : ℝ) < n - k - 1 := by
rify at hn hk
linarith
example (a b c : ℕ) (h : a - b < c) (hab : b ≤ a) : a < b + c := by
rify [hab] at h ⊢
linarith
example {n : ℕ} (h : 8 ≤ n) : (0 : ℝ) < n - 1 := by
rify at h
linarith
example {n k : ℕ} (h : 2 * k ≤ n + 2) (h' : 8 ≤ n) : (0 : ℝ) ≤ 3 * n - 4 - 4 * k := by
rify at *
linarith
example {n k : ℕ} (h₁ : 8 ≤ n) (h₂ : 2 * k > n) (h₃ : k + 1 < n) :
n - (k + 1) + 3 ≤ n := by
rify [h₃] at *
linarith
example {n k : ℕ} (h₁ : 8 ≤ n) (h₂ : 2 * k > n) (h₃ : k + 1 < n) :
n - (n - (k + 1)) = k + 1 := by
have f₁ : k + 1 ≤ n := by linarith
have f₂ : n - (k + 1) ≤ n := by rify [f₁]; linarith
rify [f₁, f₂] at *
linarith |
.lake/packages/mathlib/MathlibTest/Tauto.lean | import Mathlib.Tactic.Tauto
import Mathlib.Tactic.SplitIfs
import Mathlib.Data.Part
set_option autoImplicit true
section tauto₀
variable (p q r : Prop)
variable (h : p ∧ q ∨ p ∧ r)
example : p ∧ p := by tauto
end tauto₀
section tauto₁
variable (α : Type) (p q r : α → Prop)
variable (h : (∃ x, p x ∧ q x) ∨ (∃ x, p x ∧ r x))
example : ∃ x, p x := by tauto
end tauto₁
section tauto₂
variable (α : Type)
variable (x : α)
variable (p q r : α → Prop)
variable (h₀ : (∀ x, p x → q x → r x) ∨ r x)
variable (h₁ : p x)
variable (h₂ : q x)
example : ∃ x, r x := by tauto
end tauto₂
section tauto₃
example (p : Prop) : p ∧ True ↔ p := by tauto
example (p : Prop) : p ∨ False ↔ p := by tauto
example (p q : Prop) (h : p ≠ q) : ¬ p ↔ q := by tauto
example (p q : Prop) (h : ¬ p = q) : ¬ p ↔ q := by tauto
example (p q r : Prop) : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto
example (p q r : Prop) : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto
example (p q : Prop) (h : ¬ (p ↔ q)) (h' : ¬ p) : q := by tauto
example (p q : Prop) (h : ¬ (p ↔ q)) (h' : p) : ¬ q := by tauto
example (p q : Prop) (h : ¬ (p ↔ q)) (h' : q) : ¬ p := by tauto
example (p q : Prop) (h : ¬ (p ↔ q)) (h' : ¬ q) : p := by tauto
example (p q : Prop) (h : ¬ (p ↔ q)) (h' : ¬ q) (h'' : ¬ p) : False := by
tauto
example (p q r : Prop) (h : p ↔ q) (h' : r ↔ q) (h'' : ¬ r) : ¬ p := by
tauto
example (p q r : Prop) (h : p ↔ q) (h' : r ↔ q) : p ↔ r := by tauto
example (p q r : Prop) (h : ¬ p = q) (h' : r = q) : p ↔ ¬ r := by tauto
example (p : Prop) : p → ¬ (p → ¬ p) := by tauto
example (p : Prop) (em : p ∨ ¬ p) : ¬ (p ↔ ¬ p) := by tauto
-- reported at https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/weird.20tactic.20bug/near/370505747
open scoped Classical in
example (hp : p) (hq : q) : (if p ∧ q then 1 else 0) = 1 := by
-- split_ifs creates a hypothesis with a type that's a metavariable
split_ifs
· rfl
· tauto
example (hp : p) (hq : q) (h : ¬ (p ∧ q)) : False := by
-- causes `h'` to have a type that's a metavariable:
have h' := h
clear h
tauto
example (p : Prop) (h : False) : p := by
-- causes `h'` to have a type that's a metavariable:
have h' := h
clear h
tauto
example (hp : p) (hq : q) : p ∧ q := by
-- causes goal to have a type that's a metavariable:
suffices h : ?foo by exact h
tauto
-- Checking `tauto` can deal with annotations:
example (hp : ¬ p) (hq : ¬ (q ↔ p) := by sorry) : q := by
tauto
example (P : Nat → Prop) (n : Nat) : P n → n = 7 ∨ n = 0 ∨ ¬ (n = 7 ∨ n = 0) ∧ P n := by
tauto
section implementation_detail_ldecl
variable (a b c : Nat)
/--
Mathlib.Tactic.Tauto.distribNot must ignore any LocalDecl where isImplementationDetail
is true. Otherwise, this example yields an error saying "well-founded recursion cannot
be used".
-/
example : ¬(¬a ≤ b ∧ a ≤ c ∨ ¬a ≤ c ∧ a ≤ b) ↔ a ≤ b ∧ a ≤ c ∨ ¬a ≤ c ∧ ¬a ≤ b := by
tauto
end implementation_detail_ldecl
section modulo_symmetry
variable {p q r : Prop} {α : Type} {x y : α}
variable (h : x = y)
variable (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q))
example (h' : ¬ y = x) : p ∧ q := by tauto
/-
example (h' : p ∧ ¬ y = x) : p ∧ q := by tauto
example : y = x := by tauto
example (h' : ¬ x = y) : p ∧ q := by tauto
example : x = y := by tauto
-/
end modulo_symmetry
section pair_eq_pair_iff
variable (α : Type)
variable (x y z w : α)
-- This example is taken from pair_eq_pair_iff in Data.Set.Basic.
-- It currently doesn't work because `tauto` does not apply `symm`.
--example : ((x = z ∨ x = w) ∧ (y = z ∨ y = w)) ∧
-- (z = x ∨ z = y) ∧ (w = x ∨ w = y) → x = z ∧ y = w ∨ x = w ∧ y = z := by
-- tauto
end pair_eq_pair_iff
end tauto₃
/-
section closer
example {α : Type*} {β : Type*} (a : α) {s_1 : Set α} :
(∃ (a_1 : α), a_1 = a ∨ a_1 ∈ s_1) := by
tauto {closer := `[simp]}
variable {p q r : Prop} {α : Type} {x y z w : α}
variable (h : x = y) (h₁ : y = z) (h₂ : z = w)
variable (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q))
-- include h h₁ h₂ h''
example : (((r ∧ p ↔ r ∨ q) ∧ (q ∨ r)) → (p ∧ (x = w) ∧ (¬ x = w → p ∧ q ∧ r))) := by
tauto {closer := `[cc]}
end closer
-/
/- Zulip discussion:
https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/tauto!.20fails.20on.20ne
-/
example {x y : Nat} (h : ¬x ≠ y) : x = y := by
tauto
/-
Test the case where the goal depends on a hypothesis
https://github.com/leanprover-community/mathlib4/issues/10590
-/
section goal_depends_on_hyp
open Part
example (p : Prop) (o : Part α) (h : p → o.Dom) (a : α) :
a ∈ restrict p o h ↔ p ∧ a ∈ o := by
dsimp [restrict, mem_eq]
tauto
end goal_depends_on_hyp |
.lake/packages/mathlib/MathlibTest/ImportHeavyFlexibleLinter.lean | import Mathlib.Tactic.Linter.FlexibleLinter
import Mathlib.Data.ENNReal.Operations
import Mathlib.Tactic.Abel
import Mathlib.Tactic.Continuity
import Mathlib.Tactic.ContinuousFunctionalCalculus
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.Field
import Mathlib.Tactic.Finiteness
import Mathlib.Tactic.Group
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.LinearCombination
import Mathlib.Tactic.Measurability
import Mathlib.Tactic.Positivity
import Mathlib.Tactic.Module
import Mathlib.Tactic.Ring
import Mathlib.MeasureTheory.MeasurableSpace.Instances
import Mathlib.Topology.Continuous
import Mathlib.Topology.Instances.Nat
set_option linter.flexible true
set_option linter.unusedVariables false
/-! # Advanced tests for the flexible linter
This file contains tests for the flexible linter which require heavier imports, such as the
mathlib tactics `norm_num`, `ring`, `group`, `positivity` or `module`.
In essence, it verifies how certain mathlib tactics are treated.
-/
-- `norm_num` is allowed after `simp`.
#guard_msgs in
example : (0 + 2 : Rat) + 1 = 3 := by
simp
norm_num
/-! ## further flexible tactics -/
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [add_comm]' uses '⊢'!
-/
#guard_msgs in
-- `norm_num` is allowed after `simp`, but "passes along the stain".
example {a : Rat} : a + (0 + 2 + 1 : Rat) = 3 + a := by
simp
norm_num
rw [add_comm]
#guard_msgs in
example {V : Type*} [AddCommMonoid V] {x y : V} : 0 + x + (y + x) = x + x + y := by
simp
module
-- `grind` is another flexible tactic, as are `cfc_tac` and `finiteness`.
#guard_msgs in
example {x y : ℕ} : 0 + x + (y + x) = x + x + y := by
simp
grind
#guard_msgs in
example (h : False) : False ∧ True := by
simp
cfc_tac
-- Currently, `positivity` is not marked as flexible (as it only applies to goals in a very
-- particular shape). We use this test to record the current behaviour.
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'positivity' uses '⊢'!
-/
#guard_msgs in
example {k l : ℤ} : 0 ≤ k ^ 2 + 4 * l * 0 := by
simp
positivity
open scoped ENNReal
#guard_msgs in
example {a b c : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≠ ∞) : a * b ≠ ∞ := by
simp
finiteness
-- `abel` and `abel!` are allowed `simp`-followers.
#guard_msgs in
example {a b : Nat} : a + b = b + a + 0 := by
simp
abel
#guard_msgs in
example {a b : Nat} : a + b = b + a + 0 := by
simp
abel!
-- Test that `continuity` is also a flexible tactic: the goal must be solvable by continuity,
-- but require some simplication first.
example {X : Type*} [TopologicalSpace X] {f : X → ℕ} {g : ℕ → X}
(hf : Continuous f) (hg : Continuous g) :
Continuous (fun x ↦ (f ∘ g) x + 0) := by
simp
continuity
-- Currently, `fun_prop` is *not* marked as flexible (as it is rather structural on the exact
-- shape of the goal, and e.g. changing the goal to a defeq one could break the proof).
-- This test documents this behaviour.
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'fun_prop' uses '⊢'!
-/
#guard_msgs in
example {X : Type*} [TopologicalSpace X] {f : X → ℕ} {g : ℕ → X}
(hf : Continuous f) (hg : Continuous g) :
Continuous (fun x ↦ (f ∘ g) x + 0) := by
simp
fun_prop
-- A similar example for the `measurability` tactic.
example {α : Type*} [MeasurableSpace α] {f : α → ℚ} (hf : Measurable f) :
Measurable (fun x ↦ f x + 0) := by
simp
measurability
-- `ring`, `ring1`, `ring!` and `ring1!` are allowed `simp`-followers.
#guard_msgs in
example {a b : Nat} : a + b = b + a + 0 := by
simp
ring
#guard_msgs in
example {a b : Nat} : a + b = b + a + 0 := by
simp
ring!
#guard_msgs in
example {a b : Nat} : a + b = b + a + 0 := by
simp
ring1
#guard_msgs in
example {a b : Nat} : a + b = b + a + 0 := by
simp
ring1!
-- So are ring1_nf and ring1_nf!.
#guard_msgs in
example {a b : Nat} (h : a + b = 1 + a + b) : a + b = b + a + 0 := by
simp
ring1_nf
#guard_msgs in
example {a b : Nat} (h : a + b = 1 + a + b) : a + b = b + a + 0 := by
simp
ring1_nf!
-- Test that `linear_combination` is accepted as a follower of `simp`.
example {a b : ℤ} (h : a + 1 = b) : a + 1 + 0 = b := by
simp
linear_combination h
-- Test that `linarith` is accepted as a follower of `simp`.
#guard_msgs in
example {a b : ℤ} (h : a + 1 = b) : a + 1 + 0 = b := by
simp
linarith
-- Test that `nlinarith` is accepted as a follower of `simp`.
#guard_msgs in
example {a b : ℤ} (h : a + 1 = b) : a + 1 + 0 = b := by
simp
nlinarith
-- Test that `field_simp` is accepted as a follower of `simp`.
#guard_msgs in
example {K : Type*} [Field K] (x y z : K) (hy : 1 - y ≠ 0) (h : z = y) :
x / (1 - y) / (1 + y / (1 - z)) = x := by
simp [h]
field_simp
simp
/-! ## followers/rigidifiers -/
-- `abel_nf` is a `rigidifier`: the "stain" of `simp` does not continue past `abel_nf`.
#guard_msgs in
example {a b : Nat} (h : a + b = a + (b + 1)) : a + b = b + a + 0 + 1 := by
simp
abel_nf
assumption
-- So are `abel_nf!` and `group`.
#guard_msgs in
example {a b : Nat} (h : a + b = a + (b + 1)) : a + b = b + a + 0 + 1 := by
simp
abel_nf!
assumption
#guard_msgs in
example {a b : Nat} (h : a + b = a + (b + 1)) : a + b = b + a + 0 + 1 := by
simp
group at h ⊢
assumption
#guard_msgs in
example {a b : ℝ≥0∞} (ha : a = 0) (hb : b = a) : a + b + 3 < ∞ := by
simp [hb]
finiteness_nonterminal; simp [ha]
-- `field_simp` is a rigidifier
#guard_msgs in
example {K : Type*} [Field K] (x y z : K) (hy : 1 - y ≠ 0) (h : x = z) (h' : (1 - y + y) = 1) :
x / (1 - y) / (1 + y / (1 - y)) = z := by
field_simp
rw [h', one_mul, h]
example {K : Type*} [Field K] (x y : K) (h : x + y = x + (y + 1)) : x + y = y + x + 0 + 1 := by
simp [h]
field
-- `ring_nf` is a `rigidifier`: the "stain" of `simp` does not continue past `ring_nf`.
-- So are `ring_nf!`, `ring1_nf` and `ring1_nf!`.
#guard_msgs in
example {a b : Nat} (h : a + b = 1 + a + b) : a + b = b + a + 0 + 1 := by
simp
ring_nf
assumption
#guard_msgs in
example {a b : Nat} (h : a + b = 1 + a + b) : a + b = b + a + 0 + 1 := by
simp
ring_nf!
assumption |
.lake/packages/mathlib/MathlibTest/itauto.lean | import Mathlib.Tactic.ITauto
section ITauto₀
variable (p q r : Prop)
variable (h : p ∧ q ∨ p ∧ r)
example : p ∧ p := by itauto
end ITauto₀
section ITauto₃
example (p : Prop) : ¬(p ↔ ¬p) := by itauto
example (p : Prop) : ¬p = ¬p := by itauto
example (p : Prop) : p ≠ ¬p := by itauto
example (p : Prop) : p ∧ True ↔ p := by itauto
example (p : Prop) : p ∨ False ↔ p := by itauto
example (p q : Prop) (h0 : q) : p → q := by itauto
example (p q r : Prop) : p ∨ q ∧ r → (p ∨ q) ∧ (r ∨ p ∨ r) := by itauto
example (p q r : Prop) : p ∨ q ∧ r → (p ∨ q) ∧ (r ∨ p ∨ r) := by itauto
example (p q r : Prop) (h : p) : (p → q ∨ r) → q ∨ r := by itauto
example (p q : Prop) (h : ¬(p ↔ q)) (h' : p) : ¬q := by itauto
example (p q : Prop) (h : ¬(p ↔ q)) (h' : q) : ¬p := by itauto
example (p q : Prop) (h : ¬(p ↔ q)) (h' : ¬q) (h'' : ¬p) : False := by itauto
example (p q r : Prop) (h : p ↔ q) (h' : r ↔ q) (h'' : ¬r) : ¬p := by itauto
example (p q r : Prop) (h : p ↔ q) (h' : r ↔ q) : p ↔ r := by itauto
example (p q : Prop) : Xor' p q → (p ↔ ¬q) := by itauto
example (p q : Prop) : Xor' p q → Xor' q p := by itauto
example (p q r : Prop) (h : ¬(p ↔ q)) (h' : r ↔ q) : ¬(p ↔ r) := by itauto
example (p : Prop) : p → ¬(p → ¬p) := by itauto
example (p : Prop) (em : p ∨ ¬p) : ¬(p ↔ ¬p) := by itauto
example (p : Prop) [Decidable p] : p ∨ ¬p := by itauto *
example (p : Prop) [Decidable p] : ¬(p ↔ ¬p) := by itauto
example (p q r : Prop) [Decidable p] : (p → q ∨ r) → (p → q) ∨ (p → r) := by itauto *
example (p q r : Prop) [Decidable q] : (p → q ∨ r) → (p → q) ∨ (p → r) := by itauto [q]
example (xl yl zl xr yr zr : Prop) :
(xl ∧ yl ∨ xr ∧ yr) ∧ zl ∨ (xl ∧ yr ∨ xr ∧ yl) ∧ zr ↔
xl ∧ (yl ∧ zl ∨ yr ∧ zr) ∨ xr ∧ (yl ∧ zr ∨ yr ∧ zl) := by itauto
example : 0 < 1 ∨ ¬0 < 1 := by itauto *
example (p : Prop) (h : 0 < 1 → p) (h2 : ¬0 < 1 → p) : p := by itauto *
example (b : Bool) : ¬b ∨ b := by itauto *
example (p : Prop) : ¬p ∨ p := by itauto! [p]
example (p : Prop) : ¬p ∨ p := by itauto! *
set_option linter.unusedVariables false in
set_option linter.unusedTactic false in
-- failure tests
example (p q r : Prop) : True := by
haveI : p ∨ ¬p := by (fail_if_success itauto); sorry
clear this; haveI : ¬(p ↔ q) → ¬p → q := by (fail_if_success itauto); sorry
clear this; haveI : ¬(p ↔ q) → (r ↔ q) → (p ↔ ¬r) := by (fail_if_success itauto); sorry
trivial
example (P : Nat → Prop) (n : Nat)
(h : ¬(n = 7 ∨ n = 0) ∧ P n) : ¬(P n → n = 7 ∨ n = 0) := by itauto
section ModuloSymmetry
variable {p q r : Prop} {α : Type} {x y : α}
variable (h : x = y)
variable (h'' : (p ∧ q ↔ q ∨ r) ↔ (r ∧ p ↔ r ∨ q))
example (h' : ¬x = y) : p ∧ q := by itauto
example : x = y := by itauto
end ModuloSymmetry
end ITauto₃
example (p1 p2 p3 p4 p5 p6 f : Prop)
(h : (
(p1 ∧ p2 ∧ p3 ∧ p4 ∧ p5 ∧ p6 ∧ True) ∨
(((p1 → f) → f) → f) ∨
(p2 → f) ∨
(p3 → f) ∨
(p4 → f) ∨
(p5 → f) ∨
(p6 → f) ∨
False
) → f) : f := by itauto |
.lake/packages/mathlib/MathlibTest/FlexibleLinter.lean | import Mathlib.Tactic.Linter.FlexibleLinter
import Mathlib.Tactic.Linter.UnusedTactic
import Batteries.Linter.UnreachableTactic
import Batteries.Tactic.PermuteGoals
set_option linter.flexible true
set_option linter.unusedVariables false
/-! # Basic tests for the flexible linter
This file contains basic tests for the flexible linter, which do not require any advanced imports.
Anything which requires groups, rings or algebraic structures is considered advanced, and
tests for these can be found in `MathlibTest/ImportHeavyFlexibleLinter.lean`
-/
/--
warning: 'simp at h' is a flexible tactic modifying 'h'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'exact h' uses 'h'!
-/
#guard_msgs in
example (h : 0 + 0 = 0) : True := by
simp at h
try exact h
-- `subst` does not use the goal
#guard_msgs in
example {a b : Nat} (h : a = b) : a + 0 = b := by
simp
subst h
rfl
-- `by_cases` does not use the goal
#guard_msgs in
example {a b : Nat} (h : a = b) : a + 0 = b := by
simp
by_cases a = b
subst h; rfl
subst h; rfl
-- `induction` does not use the goal
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'assumption' uses '⊢'!
---
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'assumption' uses '⊢'!
-/
#guard_msgs in
example {a b : Nat} (h : a = b) : a + 0 = b := by
simp
induction a <;> assumption
/--
warning: 'simp at h' is a flexible tactic modifying 'h'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'exact h' uses 'h'!
-/
#guard_msgs in
example (h : 0 = 0 ∨ 0 = 0) : True := by
cases h <;>
rename_i h <;>
simp at h
· exact h
· assumption --exact h
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'on_goal 2 => · contradiction' uses '⊢'!
---
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'contradiction' uses '⊢'!
-/
#guard_msgs in
example (h : 0 = 1 ∨ 0 = 1) : 0 = 1 ∧ 0 = 1 := by
cases h <;> simp
on_goal 2 => · contradiction
· contradiction
-- `omega` is a follower and `all_goals` is a `combinatorLike`
#guard_msgs in
example {a : Nat} : a + 1 + 0 = 1 + a := by simp; all_goals omega
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'contradiction' uses '⊢'!
---
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'contradiction' uses '⊢'!
-/
#guard_msgs in
example (h : 0 = 1 ∨ 0 = 1) : 0 = 1 ∧ 0 = 1 := by
cases h <;> simp
· contradiction
· contradiction
/--
warning: 'simp at h k' is a flexible tactic modifying 'k'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [← Classical.not_not (a := True)] at k' uses 'k'!
---
warning: 'simp at h k' is a flexible tactic modifying 'h'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [← Classical.not_not (a := True)] at h' uses 'h'!
-/
#guard_msgs in
-- `simp at h` stains `h` but not other locations
example {h : 0 = 0} {k : 1 = 1} : True := by
simp at h k;
rw [← Classical.not_not (a := True)]
-- flag the two below vvv do not above ^^^
rw [← Classical.not_not (a := True)] at k
rw [← Classical.not_not (a := True)] at h
assumption
-- `specialize` does not touch, by default, the target
#guard_msgs in
example {a b : Nat} (h : ∀ c, c + a + b = a + c) : (0 + 2 + 1 + a + b) = a + 3 := by
simp
specialize h 3
simp_all
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'exact h.symm' uses '⊢'!
-/
#guard_msgs in
-- `congr` is allowed after `simp`, but "passes along the stain".
example {a b : Nat} (h : a = b) : a + b + 0 = b + a := by
simp
congr
exact h.symm
-- `done` is an allowed follower
#guard_msgs in
example (h : False) : 0 ≠ 0 := by
try (simp; done)
exact h.elim
-- `grind` is another flexible tactic,
#guard_msgs in
example {x y : Nat} : 0 + x + (y + x) = x + x + y := by
simp
grind
-- as are its `grobner` and `cutsat` front-ends.
#guard_msgs in
example {x y : Nat} : 0 + x + (y + x) = x + x + y := by
simp
grobner
-- `cutsat` is another flexible tactic.
#guard_msgs in
example {x y : Nat} : 0 + x + (y + x) = x + x + y := by
simp
cutsat
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'contradiction' uses '⊢'!
-/
#guard_msgs in
example (h : 0 = 1 ∨ 0 = 1) : 0 = 1 ∧ 0 = 1 := by
cases h <;> simp
· simp_all
· contradiction
-- forget stained locations, once the corresponding goal is closed
#guard_msgs in
example (n : Nat) : n + 1 = 1 + n := by
by_cases 0 = 0
· simp_all
omega
· have : 0 ≠ 1 := by
intro h
-- should not flag `cases`!
cases h
-- should not flag `exact`!
exact Nat.add_comm ..
/--
warning: 'simp at h' is a flexible tactic modifying 'h'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [← Classical.not_not (a := True)] at h' uses 'h'!
-/
#guard_msgs in
-- `simp at h` stains `h` but not other locations
example {h : 0 = 0} {k : 1 = 1} : ¬ ¬ True := by
simp at h
rw [← Nat.add_zero 1] at k
-- flag below vvv do not flag above ^^^
rw [← Classical.not_not (a := True)] at h
--exact h -- <-- flagged
assumption
/--
warning: 'simp at h k' is a flexible tactic modifying 'k'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [← Classical.not_not (a := True)] at k' uses 'k'!
---
warning: 'simp at h k' is a flexible tactic modifying 'h'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [← Classical.not_not (a := True)] at h' uses 'h'!
-/
#guard_msgs in
-- `simp at h` stains `h` but not other locations
example {h : 0 = 0} {k : 1 = 1} : True := by
simp at h k
rw [← Classical.not_not (a := True)]
-- flag the two below vvv do not above ^^^
rw [← Classical.not_not (a := True)] at k
rw [← Classical.not_not (a := True)] at h
assumption
/--
warning: 'simp at h' is a flexible tactic modifying 'h'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rw [← Classical.not_not (a := True)] at h' uses 'h'!
-/
#guard_msgs in
-- `simp at h` stains `h` but not other locations
example {h : 0 = 0} : True := by
simp at h
rw [← Classical.not_not (a := True)]
-- flag below vvv do not flag above ^^^
rw [← Classical.not_not (a := True)] at h
assumption
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rwa [← Classical.not_not (a := False)]' uses '⊢'!
-/
#guard_msgs in
example {h : False} : 0 = 1 := by
simp
rw [← Classical.not_not (a := False)] at h
-- flag below vvv do not flag above ^^^
rwa [← Classical.not_not (a := False)]
/--
warning: 'simp' is a flexible tactic modifying '⊢'…
Note: This linter can be disabled with `set_option linter.flexible false`
---
info: … and 'rwa [← Classical.not_not (a := False)]' uses '⊢'!
-/
#guard_msgs in
example {h : False} : 0 = 1 ∧ 0 = 1 := by
constructor
· simpa
. simp
rw [← Classical.not_not (a := False)] at h
rwa [← Classical.not_not (a := False)]
section test_internals
open Lean Mathlib.Linter Flexible
/-- `flex? tac` logs an info `true` if the tactic is flexible, logs a warning `false` otherwise. -/
elab "flex? " tac:tactic : command => do
match flexible? tac with
| true => logWarningAt tac m!"{flexible? tac}"
| false => logInfoAt tac m!"{flexible? tac}"
section
set_option linter.unusedTactic false
set_option linter.unreachableTactic false
/-- info: false -/#guard_msgs in
flex? done
/-- info: false -/#guard_msgs in
flex? simp only
/-- info: false -/#guard_msgs in
flex? simp_all only
/-- warning: true -/#guard_msgs in
flex? simp
/-- warning: true -/#guard_msgs in
flex? simp_all
end
/-- info: #[h] -/ #guard_msgs in
#eval show CoreM _ from do
let h := mkIdent `h
let hc ← `(Lean.Parser.Tactic.elimTarget|$h:ident)
IO.println s!"{(toStained (← `(tactic| cases $hc))).toArray}" |
.lake/packages/mathlib/MathlibTest/depRewrite.lean | import Mathlib.Tactic.DepRewrite
/-! ## Basic tests for `rewrite!`. -/
private axiom test_sorry : ∀ {α}, α
/-- Turn a term into a sort for testing. -/
private axiom P.{u} {α : Sort u} : α → Prop
/-- Non-deprecated copy of `Fin.ofNat` for testing. -/
private def finOfNat (n : Nat) (a : Nat) : Fin (n + 1) :=
⟨a % (n+1), Nat.mod_lt _ (Nat.zero_lt_succ _)⟩
open Lean Elab Term in
/-- Produce the annotation ``.mdata .. e`` for testing. -/
elab "mdata% " e:term : term => do
let e ← elabTerm e none
return .mdata ({ : MData}.insert `abc "def") e
open Lean Elab Meta Term in
/-- Produce the projection ``.proj `Prod 0 e`` for testing.
Standard elaborators may represent projections as ordinary functions instead. -/
elab "fst% " e:term : term => do
let u ← mkFreshLevelMVar
let v ← mkFreshLevelMVar
let α ← mkFreshExprMVar (some <| .sort u)
let β ← mkFreshExprMVar (some <| .sort v)
let tp := mkApp2 (.const ``Prod [u, v]) α β
let e ← elabTerm e tp
return .proj ``Prod 0 e
/-! ## Tests for proof-only mode. -/
variable {n m : Nat} (eq : n = m)
-- Rewrite a term annotated with `mdata`.
example (f : (k : Nat) → 0 < k → Type) (lt : 0 < n) : P (mdata% f n lt) := by
rewrite! [eq]
guard_target =ₐ P (mdata% f m (eq ▸ lt))
exact test_sorry
-- Rewrite the function in an application into a non-function.
example (any : (α : Type) → α) (eq : (Nat → Nat) = Bool) :
P (any (Nat → Nat) 0) := by
rewrite! [eq]
guard_target =ₐ P ((eq.symm ▸ any Bool) 0)
exact test_sorry
-- Rewrite the argument in an application.
example (f : (k : Nat) → Fin k → Type) (lt : 0 < n) : P (f n ⟨0, lt⟩) := by
rewrite! [eq]
guard_target =ₐ P (f m ⟨0, eq ▸ lt⟩)
exact test_sorry
-- Rewrite the structure in a projection.
example (lt : 0 < n) : P (fst% ((⟨0, lt⟩, ()) : Fin n × Unit)) := by
rewrite! [eq]
guard_target =ₐ P (fst% ((⟨0, eq ▸ lt⟩, ()) : Fin m × Unit))
exact test_sorry
private def prod (α : Type) := α × Nat
-- Ensure projection structures are detected modulo reduction.
example (tup : (α : Type) → α → prod Nat) :
P (fst% tup Nat n) := by
rewrite! [eq]
guard_target =ₐ P (fst% tup Nat m)
exact test_sorry
-- Rewrite the structure in a projection into a non-projectible structure.
example (any : (α : Type) → α) (eq : (Nat × Nat) = Nat) :
P (fst% any (Nat × Nat)) := by
rewrite! [eq]
guard_target =ₐ P (fst% (Eq.rec (motive := fun T _ => T) (any Nat) eq.symm))
exact test_sorry
-- Rewrite the value of a dependent let-binding.
example (lt : 0 < n) :
let A : Type := Fin n
P (@id A ⟨0, lt⟩) := by
rewrite! [eq]
guard_target =ₐ
let A := Fin m
P (@id A ⟨0, eq ▸ lt⟩)
exact test_sorry
-- Rewrite the type of a nondependent let-binding.
example (lt : 0 < n) :
let +nondep x : Fin n := ⟨0, lt⟩
P (@id (Fin n) x) := by
rewrite! [eq]
guard_target =ₐ
let +nondep x : Fin m := ⟨0, eq ▸ lt⟩
P (@id (Fin m) x)
exact test_sorry
-- Rewrite the type of a let-binding whose value is a proof.
example (lt' : 0 < n) : P (have lt : 0 < n := lt'; @Fin.mk n 0 (@id (0 < n) lt)) := by
rewrite! [eq]
guard_target =ₐ P <|
have lt : 0 < m := eq ▸ lt'
@Fin.mk m 0 (@id (0 < m) lt)
exact test_sorry
-- Rewrite in the argument type of a function.
example : P fun (y : Fin n) => y := by
rewrite! [eq]
guard_target =ₐ P fun (y : Fin m) => y
exact test_sorry
-- Rewrite in a function with a proof argument.
example : P fun (lt : 0 < n) => @Fin.mk n 0 (@id (0 < n) lt) := by
rewrite! [eq]
guard_target =ₐ P fun (lt : 0 < m) => @Fin.mk m 0 (@id (0 < m) lt)
exact test_sorry
-- Rewrite in a quantifier.
example : P (forall (lt : 0 < n), @Eq (Fin n) ⟨0, lt⟩ ⟨0, lt⟩) := by
rewrite! [eq]
guard_target =ₐ P (forall (lt : 0 < m), @Eq (Fin m) ⟨0, lt⟩ ⟨0, lt⟩)
exact test_sorry
-- Attempt to cast a non-proof in proof-only cast mode.
/-- error: Will not cast
y
in cast mode 'proofs'. If inserting more casts is acceptable, use `rw! (castMode := .all)`. -/
#guard_msgs in
example (Q : Fin n → Prop) (q : (x : Fin n) → Q x) :
P fun y : Fin n => q y := by
rewrite! [eq]
exact test_sorry
-- Attempt to cast a non-proof in proof-only cast mode.
/-- error:
Will not cast
⟨0, ⋯⟩
in cast mode 'proofs'. If inserting more casts is acceptable, use `rw! (castMode := .all)`. -/
#guard_msgs in
example (f : (k : Nat) → Fin k → Type) (lt : 0 < n) : P (f n ⟨0, lt⟩) := by
conv in Fin.mk .. => rewrite! [eq]
exact test_sorry
-- Ensure we traverse proof terms (ordinary `rw` succeeds here).
example
(R : (n : Nat) → Prop)
(Q : Prop)
(r : (n : Nat) → R n)
(q : (n : Nat) → R n → Q)
(t : Q → Prop) :
t (q n (r n)) := by
rewrite! [eq]
guard_target =ₐ t (q m (r m))
exact test_sorry
-- Rewrite a more complex term (not just an fvar).
variable {foo : Nat → Nat} {bar : Nat → Nat} (eq : foo n = bar m) in
example (f : (k : Nat) → Fin k → Type) (lt : 0 < foo n) : P (f (foo n) ⟨0, lt⟩) := by
rewrite! [eq]
guard_target =ₐ P (f (bar m) ⟨0, eq ▸ lt⟩)
exact test_sorry
/-! ## Tests for all-casts mode. -/
variable (B : Nat → Type)
-- Rewrite the arguments to a polymorphic function.
example (f : (k : Nat) → B k → Nat) (b : B n) : P (f n b) := by
rewrite! (castMode := .all) [eq]
guard_target =ₐ P (f m (eq ▸ b))
exact test_sorry
-- Rewrite the argument to a monomorphic function.
example (f : B n → Nat) (b : (k : Nat) → B k) : P (f (b n)) := by
rewrite! (castMode := .all) [eq]
guard_target =ₐ P (f (eq ▸ b m))
exact test_sorry
-- Rewrite a type-valued lambda.
example (f : B n → Nat) : P fun y : B n => f y := by
rewrite! (castMode := .all) [eq]
guard_target =ₐ P fun y : B m => f (eq ▸ y)
exact test_sorry
-- Rewrite in contravariant position in a higher-order application.
example (F : (f : Fin n → Nat) → Nat) :
P (F fun y : Fin n => y.1) := by
rewrite! (castMode := .all) [eq]
guard_target =ₐ P (F (eq ▸ fun y : Fin m => y.1))
exact test_sorry
-- Rewrite in covariant position in a higher-order application.
example (F : (f : Nat → Fin (n+1)) → Nat) :
P (F fun k : Nat => finOfNat n k) := by
rewrite! (castMode := .all) [eq]
guard_target =ₐ P (F (eq ▸ fun k : Nat => finOfNat m k))
exact test_sorry
-- Rewrite in invariant position in a higher-order application.
example (b : (k : Nat) → B k) (F : (f : (k : Fin n) → B k.1) → Nat) :
P (F fun k : Fin n => b k.1) := by
rewrite! (castMode := .all) [eq]
guard_target =ₐ P (F (eq ▸ fun k : Fin m => b k.1))
exact test_sorry
-- Attempt to rewrite with an LHS that does not appear in the target,
-- but does appear in types of the target's subterms.
/--
error: Tactic `depRewrite` failed: did not find instance of the pattern in the target expression
n
n m : Nat
eq : n = m
B : Nat → Type
f : B n → Nat
b : B n
⊢ f b = f b
-/
#guard_msgs in
example (f : B n → Nat) (b : B n) :
f b = f b := by
rewrite! [eq]
exact test_sorry
-- Test casting twice (from the LHS to `x` and back).
theorem bool_dep_test
(b : Bool)
(β : Bool → Sort u)
(f : ∀ b, β (b && false))
(h : false = b) :
@P (β false) (f false) := by
rewrite! (castMode := .all) [h]
guard_target =
@P (β b) (h.rec (motive := fun x _ => β x) <|
h.symm.rec (motive := fun x _ => β (x && false)) <|
f b)
exact test_sorry
-- Rewrite a `let` binding that requires generalization.
theorem let_defeq_test (b : Nat) (eq : 1 = b) (f : (n : Nat) → n = 1 → Nat) :
let n := 1; P (f n rfl) := by
rewrite! [eq]
guard_target = let n := b; P (f n _)
exact test_sorry
-- Test definitional equalities that get broken by rewriting.
example (b : Bool) (h : true = b)
(s : Bool → Prop)
(q : (c : Bool) → s c → Prop)
(f : (h : s (true || !false)) → q true h → Bool) :
∀ (i : s (true || true)) (u : q (true || !true) i), s (f i u) := by
rewrite! [h]
guard_target = ∀ (i : s (b || b)) (u : q (b || !b) _), s (f _ _)
exact test_sorry
-- As above.
example (b : Bool) (h : true = b)
(s : Bool → Prop)
(q : (c : Bool) → s c → Prop)
(f : (h : s (true || !false)) → q true h → Bool)
(j : (h : s (true || false)) → (i : q (!false) h) → (k : f h i = true) → False) :
∀ (i : s (true || true)) (u : q (true || !true) i)
(k : f i u = true), False.elim.{1} (j i u k) := by
rewrite! [h]
guard_target = ∀ (i : s (b || b)) (u : q (b || !b) _) (k : f _ _ = b), False.elim.{1} _
exact test_sorry
-- As above.
example (b : Bool) (h : true = b)
(s : Bool → Prop)
(q : (c : Bool) → s c → Prop)
(f : (h : s (true || !false)) → q true h → Bool)
(j : (c : Bool) → (h : s (true || c)) → (i : q (!false) h) → (k : f h i = !c) → False) :
∀ (i : s (true || true)) (u : q (true || !true) i)
(k : f i u = true), False.elim.{1} (j (!true) i u k) := by
rewrite! [h]
guard_target = ∀ (i : s (b || b)) (u : q (b || !b) _) (k : f _ _ = b), False.elim.{1} _
exact test_sorry
-- Rewrite in nested lets whose values and types depend on prior lets.
#guard_msgs in
example
(F : Nat → Type)
(G : (n : Nat) → F n → Type)
(r : (n : Nat) → (f : F n) → G n f → Nat)
(f : F n) (g : G n f) :
P <|
let a : Nat := n
let B : Type := G a f
let c : B := g
let c' : G n f := g
r n f c = r n f c' := by
rewrite! (castMode := .all) [eq]
exact test_sorry
/-! ## Tests for `occs` -/
-- Test `.pos`.
example (f : Nat → Nat → Nat) : P (f (id n) (id n)) := by
rewrite! (occs := .pos [1]) [eq]
guard_target =ₐ P (f (id m) (id n))
exact test_sorry
-- Test `.neg`.
example (f : Nat → Nat → Nat) : P (f (id n) (id n)) := by
rewrite! (occs := .neg [1]) [eq]
guard_target =ₐ P (f (id n) (id m))
exact test_sorry |
.lake/packages/mathlib/MathlibTest/Simp.lean | import Mathlib.Algebra.Algebra.Pi
import Mathlib.Algebra.Algebra.Defs
/-!
Tests for the behavior of `simp`.
-/
/- Taken from [Zulip](https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/
topic/simp.20.5BX.5D.20fails.2C.20rw.20.5BX.5D.20works) -/
-- Example 1: succeeds
example {α R : Type*} [CommRing R] (f : α → R) (r : R) (a : α) :
(r • f) a = r • (f a) := by
simp only [Pi.smul_apply] -- succeeds
-- Example 2: used to fail, now succeeds!
example {α R : Type*} [CommRing R] (f : α → R) (r : R) (a : α) :
(r • f) a = r • (f a) := by
let _ : SMul R R := SMulZeroClass.toSMul
simp only [Pi.smul_apply] |
.lake/packages/mathlib/MathlibTest/finsupp_notation.lean | import Mathlib.Data.Finsupp.Notation
import Mathlib.Data.DFinsupp.Notation -- to ensure it does not interfere
import Mathlib.Data.Nat.Factorization.Basic
example : (fun₀ | 1 => 3) 1 = 3 := by
simp
example : (fun₀ | 1 | 2 | 3 => 3 | 3 => 4) 1 = 3 := by
simp
example : (fun₀ | 1 | 2 | 3 => 3 | 3 => 4) 2 = 3 := by
simp
example : (fun₀ | 1 | 2 | 3 => 3 | 3 => 4) 3 = 4 := by
simp
section repr
/-- info: fun₀ | 1 => 3 | 2 => 3 -/
#guard_msgs in
#eval (Finsupp.mk {1, 2} (fun | 1 | 2 => 3 | _ => 0) (fun x => by aesop))
/--
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 Finsupp.mk
{["there are five words here", "and five more words here"],
["there are seven words but only here"],
["just two"]}
(fun
| ["there are five words here", "and five more words here"] => 5
| ["there are seven words but only here"] => 7
| ["just two"] => 2
| _ => 0)
(fun x => by aesop)
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 : 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
end PrettyPrinter
/-! ## (computable) number theory examples -/
/-- info: fun₀ | 2 => 2 | 7 => 1 -/
#guard_msgs in
#eval Nat.factorization 28
/-- info: fun₀ | 3 => 2 | 5 => 1 -/
#guard_msgs in
#eval (Nat.factorization 720).filter Odd |
.lake/packages/mathlib/MathlibTest/HashCommandLinter.lean | import Lean.Elab.GuardMsgs
import Mathlib.Tactic.AdaptationNote
import Mathlib.Tactic.Linter.HashCommandLinter
set_option linter.hashCommand true
section ignored_commands
-- As a consequence of https://github.com/leanprover/lean4/pull/5644, `#guard_msgs in`, even without a doc-string,
-- does not triggers the `#`-command linter.
#guard_msgs in
#guard_msgs in
#adaptation_note /-- testing that the hashCommand linter ignores this. -/
/-- info: 0 -/
#guard_msgs in
-- emits a message -- the linter allows it
#eval 0
/-- info: constructor PUnit.unit.{u} : PUnit -/
#guard_msgs in
-- emits a message -- the linter allows it
#print PUnit.unit
/-- info: 0 : Nat -/
#guard_msgs in
-- emits a message -- the linter allows it
#check 0
/--
warning: `#`-commands, such as '#eval', are not allowed in 'Mathlib'
Note: This linter can be disabled with `set_option linter.hashCommand false`
-/
#guard_msgs in
-- emits an empty message -- the linter allows it
#eval show Lean.MetaM _ from do guard true
-- not a `#`-command and not emitting a message: the linter allows it
run_cmd if false then Lean.logInfo "0"
end ignored_commands
section linted_commands
/--
warning: `#`-commands, such as '#guard', are not allowed in 'Mathlib'
Note: This linter can be disabled with `set_option linter.hashCommand false`
-/
#guard_msgs in
#guard true
set_option linter.unusedTactic false in
/--
warning: `#`-commands, such as '#check_tactic', are not allowed in 'Mathlib'
Note: This linter can be disabled with `set_option linter.hashCommand false`
-/
#guard_msgs in
#check_tactic True ~> True by skip
-- Testing that the linter enters `in` recursively.
/--
warning: `#`-commands, such as '#guard', are not allowed in 'Mathlib'
Note: This linter can be disabled with `set_option linter.hashCommand false`
-/
#guard_msgs in
variable (n : Nat) in
#guard true
/--
warning: `#`-commands, such as '#guard', are not allowed in 'Mathlib'
Note: This linter can be disabled with `set_option linter.hashCommand false`
-/
#guard_msgs in
open Nat in
variable (n : Nat) in
variable (n : Nat) in
#guard true
/--
warning: `#`-commands, such as '#guard', are not allowed in 'Mathlib'
Note: This linter can be disabled with `set_option linter.hashCommand false`
-/
#guard_msgs in
open Nat in
variable (n : Nat) in
#guard true
-- a test for `withSetOptionIn'`
set_option linter.unusedVariables false in
example {n : Nat} : Nat := 0
section warningAsError
-- if `warningAsError = true`, log an info (instead of a warning) on all `#`-commands, noisy or not
set_option warningAsError true
/--
info: 0
---
info: `#`-commands, such as '#eval', are not allowed in 'Mathlib' [linter.hashCommand]
-/
#guard_msgs in
#eval 0
/--
info: `#`-commands, such as '#guard', are not allowed in 'Mathlib' [linter.hashCommand]
-/
#guard_msgs in
#guard true
end warningAsError
end linted_commands |
.lake/packages/mathlib/MathlibTest/SimpRw.lean | import Mathlib.Tactic.SimpRw
private axiom test_sorry : ∀ {α}, α
-- `simp_rw` can perform rewrites under binders:
example : (fun (x y : Nat) ↦ x + y) = (fun x y ↦ y + x) := by simp_rw [Nat.add_comm]
-- `simp_rw` can apply reverse rules:
example (f : Nat → Nat) {a b c : Nat} (ha : f b = a) (hc : f b = c) : a = c := by simp_rw [← ha, hc]
-- `simp_rw` applies rewrite rules multiple times:
example (a b c d : Nat) : a + (b + (c + d)) = ((d + c) + b) + a := by simp_rw [Nat.add_comm]
-- `simp_rw` can also rewrite in assumptions:
example (p : Nat → Prop) (a b : Nat) (h : p (a + b)) : p (b + a) := by
simp_rw [Nat.add_comm a b] at h; exact h
-- or at multiple assumptions:
example (p : Nat → Prop) (a b : Nat) (h₁ : p (b + a) → p (a + b)) (h₂ : p (a + b)) : p (b + a) := by
simp_rw [Nat.add_comm a b] at h₁ h₂; exact h₁ h₂
-- or everywhere:
example (p : Nat → Prop) (a b : Nat) (h₁ : p (b + a) → p (a + b)) (h₂ : p (a + b)) : p (a + b) := by
simp_rw [Nat.add_comm a b] at *; exact h₁ h₂
-- `simp` and `rw`, alone, can't close this goal. But `simp_rw` can
example {a : Nat}
(h1 : ∀ a b : Nat, a - 1 ≤ b ↔ a ≤ b + 1)
(h2 : ∀ a b : Nat, a ≤ b ↔ ∀ c, c < a → c < b) :
(∀ b, a - 1 ≤ b) = ∀ b c : Nat, c < a → c < b + 1 := by
simp_rw [h1, h2]
set_option linter.unusedTactic false in
-- `simp_rw` respects config options
example : 1 = 2 := by
let a := 2
show 1 = a
simp_rw -zeta []
guard_target =ₛ 1 = a
exact test_sorry
/--
error: No goals to be solved
-/
-- check that `simp_rw` does not "spill over" goals
#guard_msgs in
example {n : Nat} (hn : n = 0) : (n = 0) ∧ (n = 0) := by
constructor
simp_rw [hn, hn] -- does this work? |
.lake/packages/mathlib/MathlibTest/InferParam.lean | import Mathlib.Tactic.InferParam
namespace InferParamTest
theorem zero_le_add (a : Nat) (ha : 0 ≤ a := Nat.zero_le a) : 0 ≤ a + a := calc
0 ≤ a := ha
_ ≤ a + a := Nat.le_add_left _ _
theorem zero_le_add' (a : Nat) (ha : 0 ≤ a := by decide) : 0 ≤ a + a := zero_le_add a ha
example : 0 ≤ 2 + 2 := by
fail_if_success infer_param
decide
example : 0 ≤ 2 + 2 := by
apply zero_le_add
infer_param
example : 0 ≤ 2 + 2 := by
apply zero_le_add'
infer_param
end InferParamTest |
.lake/packages/mathlib/MathlibTest/Continuity.lean | import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
import Mathlib.Topology.Basic
import Mathlib.Topology.ContinuousMap.Basic
set_option autoImplicit true
section basic
variable [TopologicalSpace W] [TopologicalSpace X] [TopologicalSpace Y] [TopologicalSpace Z]
variable {I : Type _} {X' : I → Type _} [∀ i, TopologicalSpace (X' i)]
example : Continuous (id : X → X) := by continuity
example {f : X → Y} {g : Y → X} (hf : Continuous f) (hg : Continuous g) :
Continuous (fun x => f (g x)) := by continuity
example {f : X → Y} {g : Y → X} (hf : Continuous f) (hg : Continuous g) :
Continuous (f ∘ g ∘ f) := by continuity
example {f : X → Y} {g : Y → X} (hf : Continuous f) (hg : Continuous g) :
Continuous (f ∘ g) := by continuity
example (y : Y) : Continuous (fun (_ : X) ↦ y) := by continuity
example {f : Y → Y} (y : Y) : Continuous (f ∘ (fun (_ : X) => y)) := by continuity
example {g : X → X} (y : Y) : Continuous ((fun _ ↦ y) ∘ g) := by continuity
example {f : X → Y} (x : X) : Continuous (fun (_ : X) ↦ f x) := by continuity
example (f₁ f₂ : X → Y) (hf₁ : Continuous f₁) (hf₂ : Continuous f₂)
(g : Y → ℝ) (hg : Continuous g) : Continuous (fun x => (max (g (f₁ x)) (g (f₂ x))) + 1) := by
continuity
example {κ ι : Type}
(K : κ → Type) [∀ k, TopologicalSpace (K k)] (I : ι → Type) [∀ i, TopologicalSpace (I i)]
(e : κ ≃ ι) (F : Π k, Homeomorph (K k) (I (e k))) :
Continuous (fun (f : Π k, K k) (i : ι) => F (e.symm i) (f (e.symm i))) := by
continuity
open Real
example : Continuous (fun x : ℝ => exp ((max x (-x)) + sin x)^2) := by
continuity
example : Continuous (fun x : ℝ => exp ((max x (-x)) + sin (cos x))^2) := by
continuity
-- Examples taken from `Topology.ContinuousMap.Basic`:
example (b : Y) : Continuous (fun _ : X => b) := by continuity
example (f : C(X, Y)) (g : C(Y, Z)) : Continuous (g ∘ f) := by continuity
example (f : C(X, Y)) (g : C(X, Z)) : Continuous (fun x => (f x, g x)) := by continuity
example (f : C(X, Y)) (g : C(W, Z)) : Continuous (Prod.map f g) := by continuity
example (f : ∀ i, C(X, X' i)) : Continuous (fun a i => f i a) := by continuity
example (s : Set X) (f : C(X, Y)) : Continuous (f ∘ ((↑) : s → X)) := by continuity
-- Examples taken from `Topology.CompactOpen`:
example (b : Y) : Continuous (Function.const X b) := --by continuity
continuous_const
example (b : Y) : Continuous (@Prod.mk Y X b) := by continuity
example (f : C(X × Y, Z)) (a : X) : Continuous (Function.curry f a) := --by continuity
f.continuous.comp (continuous_const.prodMk continuous_id)
end basic
/-! Some tests of the `comp_of_eq` lemmas -/
example {α β : Type _} [TopologicalSpace α] [TopologicalSpace β] {x₀ : α} (f : α → α → β)
(hf : ContinuousAt (Function.uncurry f) (x₀, x₀)) :
ContinuousAt (fun x ↦ f x x) x₀ := by
fail_if_success { exact hf.comp (continuousAt_id.prod continuousAt_id) }
exact hf.comp_of_eq (continuousAt_id.prodMk continuousAt_id) rfl |
.lake/packages/mathlib/MathlibTest/norm_num_flt.lean | import Mathlib.Tactic.NormNum
-- From https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/Proving.20FLT.20with.20norm_num/near/429746342
variable (ignore_me_please : ∀ a b c n : ℕ, a ^ n + b ^ n ≠ c ^ n)
-- previously this proof would work!
/--
error: unsolved goals
n : ℕ
hn : n > 2
a b c : ℕ
⊢ ¬a ^ n + b ^ n = c ^ n
-/
#guard_msgs in
example (n) (hn : n > 2) (a b c : ℕ) : a ^ n + b ^ n ≠ c ^ n := by
clear ignore_me_please -- I promise not to use this, it would be cheating
norm_num [*] |
.lake/packages/mathlib/MathlibTest/slow_instances.lean | import Mathlib
variable {K : Type*} [Field K] {x : K}
set_option maxHeartbeats 1000 in -- uses about 850 as of 2025-09-10
/--
error: failed to synthesize
Lean.Grind.NoNatZeroDivisors K
Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.
-/
#guard_msgs in
#synth Lean.Grind.NoNatZeroDivisors K
set_option maxHeartbeats 3000 in -- uses about 2300 as of 2025-09-11
example : x ^ 3 * x ^ 42 = x ^ 45 := by grind
-- This one is dismally slow (~0.5s, 18_000 heartbeats). However it doesn't affect `grind` directly.
set_option maxHeartbeats 20_000 in
/--
error: failed to synthesize
NoZeroSMulDivisors ℕ K
Hint: Additional diagnostic information may be available using the `set_option diagnostics true` command.
-/
#guard_msgs in
#synth NoZeroSMulDivisors ℕ K |
.lake/packages/mathlib/MathlibTest/apply_with.lean | import Mathlib.Tactic.ApplyWith
set_option autoImplicit true
example (f : ∀ x : Nat, x = x → α) : α := by
apply (config := {}) f
apply rfl
apply 1
example (f : ∀ x : Nat, x = x → α) : α := by
apply (config := { newGoals := .nonDependentOnly }) f
apply @rfl _ 1
example (f : ∀ x : Nat, x = x → α) : α := by
apply (config := { newGoals := .all }) f
apply 1
apply rfl |
.lake/packages/mathlib/MathlibTest/says_whitespace.lean | import Aesop
import Mathlib.Tactic.Says
set_option says.verify true
variable {X Y Z : Type}
open Function
example {f : X → Y} {g : Y → Z} (hgfinj : Injective (g ∘ f)) : Injective f := by
rw [Injective]
aesop? says
intro a₁ a₂ a
apply hgfinj
simp_all only [comp_apply] |
.lake/packages/mathlib/MathlibTest/rewrites.lean | import Mathlib.Algebra.Ring.Nat
import Mathlib.Data.Nat.Prime.Defs
import Mathlib.CategoryTheory.Category.Basic
import Mathlib.Data.List.InsertIdx
import Mathlib.Algebra.Group.Basic
-- This is partially duplicative with the tests for `rw?` in Lean.
-- It's useful to re-test here with a larger environment.
private axiom test_sorry : ∀ {α}, α
-- To see the (sorted) list of lemmas that `rw?` will try rewriting by, use:
-- set_option trace.Tactic.rewrites.lemmas true
set_option autoImplicit true
/--
info: Try this:
[apply] rw [List.map_append]
-- no goals
-/
#guard_msgs in
example (f : α → β) (L M : List α) : (L ++ M).map f = L.map f ++ M.map f := by
rw?
open CategoryTheory
/--
info: Try this:
[apply] rw [Category.id_comp]
-- no goals
-/
#guard_msgs in
example [Category C] {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) : f ≫ 𝟙 _ ≫ g = f ≫ g := by
rw?
/--
info: Try this:
[apply] rw [mul_eq_right]
-- no goals
-/
#guard_msgs in
example [Group G] (h : G) : 1 * h = h := by
rw? [-mul_left_eq_self] -- exclude deprecated name for mul_eq_right, it is found first otherwise
#adaptation_note /-- nightly-2024-03-27
`rw?` upstream no longer uses `MVarId.applyRefl`, so it can't deal with `Iff` goals.
I'm out of time to deal with this, so I'll just drop the test for now.
This may need to wait until the next release. -/
-- /--
-- info: Try this: rw [Nat.prime_iff]
-- -- "no goals"
-- -/
-- #guard_msgs in
-- lemma prime_of_prime (n : ℕ) : Prime n ↔ Nat.Prime n := by
-- rw?
#guard_msgs(drop info) in
example [Group G] (h : G) (hyp : g * 1 = h) : g = h := by
rw? at hyp
assumption
#guard_msgs(drop info) in
example : ∀ (x y : ℕ), x ≤ y := by
intro x y
rw? -- Used to be an error here https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/panic.20and.20error.20with.20rw.3F/near/370495531
exact test_sorry
example : ∀ (x y : ℕ), x ≤ y := by
-- Used to be a panic here https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/panic.20and.20error.20with.20rw.3F/near/370495531
success_if_fail_with_msg "Could not find any lemmas which can rewrite the goal" rw?
exact test_sorry
axiom K : Type
@[instance] axiom K.ring : Ring K
noncomputable def foo : K → K := test_sorry
#guard_msgs(drop info) in
example : foo x = 1 ↔ ∃ k : ℤ, x = k := by
rw? -- Used to panic, see https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/panic.20and.20error.20with.20rw.3F/near/370598036
exact test_sorry
lemma six_eq_seven : 6 = 7 := test_sorry
-- This test also verifies that we are removing duplicate results;
-- it previously also reported `Nat.cast_ofNat`
#guard_msgs(drop info) in
example : ∀ (x : ℕ), x ≤ 6 := by
rw?
guard_target = ∀ (x : ℕ), x ≤ 7
exact test_sorry
#guard_msgs(drop info) in
example : ∀ (x : ℕ) (_w : x ≤ 6), x ≤ 8 := by
rw?
guard_target = ∀ (x : ℕ) (_w : x ≤ 7), x ≤ 8
exact test_sorry
-- check we can look inside let expressions
#guard_msgs(drop info) in
example (n : ℕ) : let y := 3; n + y = 3 + n := by
rw?
axiom α : Type
axiom f : α → α
axiom z : α
axiom f_eq (n) : f n = z
-- Check that the same lemma isn't used multiple times.
-- This used to report two redundant copies of `f_eq`.
-- It be lovely if `rw?` could produce two *different* rewrites by `f_eq` here!
#guard_msgs(drop info) in
lemma test : f n = f m := by
fail_if_success rw? [-f_eq] -- Check that we can forbid lemmas.
rw?
rw [f_eq]
-- Check that we can rewrite by local hypotheses.
#guard_msgs(drop info) in
example (h : 1 = 2) : 2 = 1 := by
rw?
def testConst : Nat := 4
-- This used to (incorrectly!) succeed because `rw?` would try `rfl`,
-- rather than `withReducible` `rfl`.
#guard_msgs(drop info) in
example : testConst = 4 := by
rw?
exact test_sorry
-- Discharge side conditions from local hypotheses.
/--
info: Try this:
[apply] rw [h p]
-- no goals
-/
#guard_msgs in
example {P : Prop} (p : P) (h : P → 1 = 2) : 2 = 1 := by
rw?
-- Use `solve_by_elim` to discharge side conditions.
/--
info: Try this:
[apply] rw [h (f p)]
-- no goals
-/
#guard_msgs in
example {P Q : Prop} (p : P) (f : P → Q) (h : Q → 1 = 2) : 2 = 1 := by
rw?
-- Rewrite in reverse, discharging side conditions from local hypotheses.
/--
info: Try this:
[apply] rw [← h₁ p]
-- Q a
-/
#guard_msgs in
example {P : Prop} (p : P) (Q : α → Prop) (a b : α) (h₁ : P → a = b) (w : Q a) : Q b := by
rw?
exact w |
.lake/packages/mathlib/MathlibTest/DocString.lean | import Mathlib.Tactic.AdaptationNote
import Mathlib.Tactic.Linter.DocString
/-! Tests for the `docstring` linter -/
set_option linter.style.docString true
#adaptation_note /--This comment is not inspected by the `docString` linter.-/
example : True := by
#adaptation_note /-- This comment is not inspected by the `docString` linter.
-/
trivial
/--
warning: warning: this doc-string is empty
Note: This linter can be disabled with `set_option linter.style.docString.empty false`
-/
#guard_msgs in
/---/
example : Nat := 0
/--
warning: warning: this doc-string is empty
Note: This linter can be disabled with `set_option linter.style.docString.empty false`
-/
#guard_msgs in
/--
-/
example : Nat := 0
set_option linter.style.docString.empty false
/---/
example : Nat := 0
set_option linter.style.docString.empty true
set_option linter.style.docString false
#guard_msgs in
/--Missing space -/
example : Nat := 1
set_option linter.style.docString true
/--
warning: error: doc-strings should start with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/--Missing space -/
example : Nat := 1
/--
warning: error: doc-strings should end with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/-- Missing ending space-/
example : Nat := 1
/--
warning: error: doc-strings should start with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/-- Two starting spaces -/
example : Nat := 1
/--
warning: error: doc-strings should end with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/--
Two ending spaces -/
example : Nat := 1
/--
warning: error: doc-strings should end with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/-- Let's give an example.
```lean4
def foo : Bool := by
sorry
```
-/
example : Nat := 1
/--
warning: error: doc-strings should not end with a comma
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/-- Let's give an example ending in a comma, -/
example : Nat := 1
/--
warning: error: doc-strings should start with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
---
warning: error: doc-strings should start with a single space or newline
Note: This linter can be disabled with `set_option linter.style.docString false`
-/
#guard_msgs in
/-- The structure `X`. -/
structure X where
/-- A field of `X`.
-/
x : Unit
z : Unit
/--
A field of `X`
spanning multiple lines.
-/
y : Unit |
.lake/packages/mathlib/MathlibTest/Set.lean | import Mathlib.Tactic.Set
import Mathlib.Tactic.Basic
import Mathlib.Util.SleepHeartbeats
import Qq
example (x : Nat) (h : x = x) : x = x := by
set! p := h
set q : x = x := p
apply q
example (x : Nat) (h : x + x - x = 3) : x + x - x = 3 := by
set! y := x with ← h2
set w := x
guard_hyp y := x
guard_hyp w := x
guard_hyp h : w + w - w = 3
guard_hyp h2 : w = y
set z := w with _h3
set a := 3
guard_target = z + z - z = a
set i'm_the_goal : Prop := z + z - z = a
guard_target = i'm_the_goal
apply h
example (x : Nat) (h : x - x = 0) : x = x := by
set y : Nat := x
set! z := y + 1 with ← _eq1
set! p : x - x = 0 := h with _eq2
rfl
example : True := by
set g : Nat → Int := (fun ε => ε) with _h
trivial
-- simulate a slow to elaborate term
open Qq in
elab "test" : term => do
sleepAtLeastHeartbeats (1000 * 1000)
return q((1 : Nat))
-- this will timeout if test is elaborated multiple times
set_option maxHeartbeats 3000 in
example {_a _b _c _d _e _f _g _h : Nat} : 1 = 1 := by
set a : Nat := test with _h
trivial |
.lake/packages/mathlib/MathlibTest/CommDiag.lean | import Mathlib.Tactic.Widget.CommDiag
import ProofWidgets.Component.Panel.SelectionPanel
import ProofWidgets.Component.Panel.GoalTypePanel
/-! ## Example use of commutative diagram widgets -/
universe u
namespace CategoryTheory
open ProofWidgets
/-- Local instance to make examples work. -/
local instance : Category (Type u) where
Hom α β := α → β
id _ := id
comp f g := g ∘ f
id_comp _ := rfl
comp_id _ := rfl
assoc _ _ _ := rfl
example {f g : Nat ⟶ Bool}: f = g → (f ≫ 𝟙 Bool) = (g ≫ 𝟙 Bool) := by
with_panel_widgets [GoalTypePanel]
intro h
exact h
example {fButActuallyTheNameIsReallyLong g : Nat ⟶ Bool}: fButActuallyTheNameIsReallyLong = g →
fButActuallyTheNameIsReallyLong = (g ≫ 𝟙 Bool) := by
with_panel_widgets [GoalTypePanel]
intro h
conv =>
rhs
enter [1]
rw [← h]
rfl
-- from Sina Hazratpour
example {X Y Z : Type} {f g : X ⟶ Y} {k : Y ⟶ Y} {f' : Y ⟶ Z} {i : X ⟶ Z}
(h' : g ≫ f' = i) :
(f ≫ k) = g → ((f ≫ k) ≫ f') = (g ≫ 𝟙 Y ≫ f') := by
with_panel_widgets [GoalTypePanel]
intro h
rw [
h,
← Category.assoc g (𝟙 Y) f',
h',
Category.comp_id g, h'
]
example {X Y Z : Type} {f i : X ⟶ Y}
{g j : Y ⟶ Z} {h : X ⟶ Z} :
h = f ≫ g →
i ≫ j = h →
f ≫ g = i ≫ j := by
with_panel_widgets [SelectionPanel]
intro h₁ h₂
rw [← h₁, h₂]
end CategoryTheory |
.lake/packages/mathlib/MathlibTest/slow_simp.lean | import Mathlib
import Mathlib.Topology.Category.TopCat.Basic
/-!
This test file serves as a sentinel against bad simp lemmas.
When this test file was first setup,
the final declaration of this file took 12,000 heartbeats
with the minimal import of `Mathlib/Topology/Category/TopCat/Basic.lean`,
but took over 260,000 heartbeats with `import Mathlib`.
After deleting some bad simp lemmas that were being tried everywhere
(discovered using `set_option diagnostics true`):
* Mathlib.MeasureTheory.coeFn_comp_toFiniteMeasure_eq_coeFn
* LightProfinite.hasForget_forget_obj
* CategoryTheory.sum_comp_inl
* CategoryTheory.sum_comp_inr
it is back down to 19,000 heartbeats even with `import Mathlib`.
-/
open CategoryTheory
structure PointedSpace where
carrier : Type
[inst : TopologicalSpace carrier]
base : carrier
attribute [instance] PointedSpace.inst
namespace PointedSpace
structure Hom (X Y : PointedSpace) where
map : ContinuousMap X.carrier Y.carrier
base : map X.base = Y.base
attribute [simp] Hom.base
namespace Hom
def id (X : PointedSpace) : Hom X X := ⟨ContinuousMap.id _, rfl⟩
def comp {X Y Z : PointedSpace} (f : Hom X Y) (g : Hom Y Z) : Hom X Z :=
⟨g.map.comp f.map, by simp⟩
end Hom
instance : Category PointedSpace where
Hom := Hom
id := Hom.id
comp := Hom.comp
end PointedSpace
set_option maxHeartbeats 20000 in
def PointedSpaceEquiv_inverse : Under (TopCat.of Unit) ⥤ PointedSpace where
obj := fun X =>
{ carrier := X.right
base := X.hom () }
map := fun f =>
{ map := f.right.hom
base := by
have := f.w
replace this := CategoryTheory.congr_fun this ()
simp [-Under.w] at this
simp
exact this.symm }
map_comp := by intros; simp_all; rfl -- This is the slow step. |
.lake/packages/mathlib/MathlibTest/Quaternion.lean | import Mathlib.Algebra.Quaternion
import Mathlib.Data.Real.Basic
import Mathlib.NumberTheory.Zsqrtd.GaussianInt
open Quaternion
/--
info: { re := 0, imI := 0, imJ := 0, imK := 0 }
-/
#guard_msgs in
#eval (0 : ℍ[ℚ])
/--
info: { re := 1, imI := 0, imJ := 0, imK := 0 }
-/
#guard_msgs in
#eval (1 : ℍ[ℚ])
/--
info: { re := 4, imI := 0, imJ := 0, imK := 0 }
-/
#guard_msgs in
#eval (4 : ℍ[ℚ])
/--
info: { re := Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/), imI := Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/), imJ := Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/), imK := Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) }
-/
#guard_msgs in
#eval (⟨0, 0, 0, 0⟩ : ℍ[ℝ])
/--
info: { re := Real.ofCauchy (sorry /- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... -/), imI := Real.ofCauchy (sorry /- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, ... -/), imJ := Real.ofCauchy (sorry /- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ... -/), imK := Real.ofCauchy (sorry /- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ... -/) }
-/
#guard_msgs in
#eval (⟨1, 2, 3, 4⟩ : ℍ[ℝ])
/--
info: { re := ⟨0, 0⟩, imI := ⟨0, 0⟩, imJ := ⟨0, 0⟩, imK := ⟨0, 0⟩ }
-/
#guard_msgs in
#eval (0 : ℍ[GaussianInt]) |
.lake/packages/mathlib/MathlibTest/norm_num_ext.lean | import Mathlib.Tactic.NormNum.BigOperators
import Mathlib.Tactic.NormNum.GCD
import Mathlib.Tactic.NormNum.IsCoprime
import Mathlib.Tactic.NormNum.DivMod
import Mathlib.Tactic.NormNum.ModEq
import Mathlib.Tactic.NormNum.NatFactorial
import Mathlib.Tactic.NormNum.NatFib
import Mathlib.Tactic.NormNum.NatLog
import Mathlib.Tactic.NormNum.NatSqrt
import Mathlib.Tactic.NormNum.Parity
import Mathlib.Tactic.NormNum.Prime
import Mathlib.Algebra.Order.Floor.Semifield
import Mathlib.Data.NNRat.Floor
import Mathlib.Data.Rat.Floor
import Mathlib.Tactic.NormNum.LegendreSymbol
import Mathlib.Tactic.NormNum.Pow
import Mathlib.Tactic.NormNum.RealSqrt
import Mathlib.Tactic.NormNum.Irrational
import Mathlib.Tactic.Simproc.Factors
/-!
# Tests for `norm_num` extensions
Some tests of unported extensions are still commented out.
-/
-- The default is very low, and we want to test performance on large numbers.
set_option exponentiation.threshold 2000
-- set_option profiler true
-- set_option trace.profiler true
-- set_option trace.Tactic.norm_num true
-- coverage tests
example : Nat.sqrt 0 = 0 := by norm_num1
example : Nat.sqrt 1 = 1 := by norm_num1
example : Nat.sqrt 2 = 1 := by norm_num1
example : Nat.sqrt 3 = 1 := by norm_num1
example : Nat.sqrt 4 = 2 := by norm_num1
example : Nat.sqrt 8 = 2 := by norm_num1
example : Nat.sqrt 9 = 3 := by norm_num1
example : Nat.sqrt 10 = 3 := by norm_num1
example : Nat.sqrt 99 = 9 := by norm_num1
example : Nat.sqrt 100 = 10 := by norm_num1
example : Nat.sqrt 120 = 10 := by norm_num1
example : Nat.sqrt 121 = 11 := by norm_num1
example : Nat.sqrt 122 = 11 := by norm_num1
example : Nat.sqrt (123456^2) = 123456 := by norm_num1
example : Nat.sqrt (123456^2 + 123456) = 123456 := by norm_num1
theorem ex11 : Nat.Coprime 1 2 := by norm_num1
theorem ex12 : Nat.Coprime 2 1 := by norm_num1
theorem ex13 : ¬ Nat.Coprime 0 0 := by norm_num1
theorem ex14 : ¬ Nat.Coprime 0 3 := by norm_num1
theorem ex15 : ¬ Nat.Coprime 2 0 := by norm_num1
theorem ex16 : Nat.Coprime 2 3 := by norm_num1
theorem ex16' : Nat.Coprime 3 2 := by norm_num1
theorem ex17 : ¬ Nat.Coprime 2 4 := by norm_num1
theorem ex21 : Nat.gcd 1 2 = 1 := by norm_num1
theorem ex22 : Nat.gcd 2 1 = 1 := by norm_num1
theorem ex23 : Nat.gcd 0 0 = 0 := by norm_num1
theorem ex24 : Nat.gcd 0 3 = 3 := by norm_num1
theorem ex25 : Nat.gcd 2 0 = 2 := by norm_num1
theorem ex26 : Nat.gcd 2 3 = 1 := by norm_num1
theorem ex27 : Nat.gcd 2 4 = 2 := by norm_num1
theorem ex31 : Nat.lcm 1 2 = 2 := by norm_num1
theorem ex32 : Nat.lcm 2 1 = 2 := by norm_num1
theorem ex33 : Nat.lcm 0 0 = 0 := by norm_num1
theorem ex34 : Nat.lcm 0 3 = 0 := by norm_num1
theorem ex35 : Nat.lcm 2 0 = 0 := by norm_num1
theorem ex36 : Nat.lcm 2 3 = 6 := by norm_num1
theorem ex37 : Nat.lcm 2 4 = 4 := by norm_num1
theorem ex41 : Int.gcd 2 3 = 1 := by norm_num1
theorem ex42 : Int.gcd (-2) 3 = 1 := by norm_num1
theorem ex43 : Int.gcd 2 (-3) = 1 := by norm_num1
theorem ex44 : Int.gcd (-2) (-3) = 1 := by norm_num1
theorem ex51 : Int.lcm 2 3 = 6 := by norm_num1
theorem ex52 : Int.lcm (-2) 3 = 6 := by norm_num1
theorem ex53 : Int.lcm 2 (-3) = 6 := by norm_num1
theorem ex54 : Int.lcm (-2) (-3) = 6 := by norm_num1
theorem ex61 : Nat.gcd (553105253 * 776531401) (553105253 * 920419823) = 553105253 := by norm_num1
theorem ex62 : Nat.gcd (2^1000 - 1) (2^1001 - 1) = 1 := by norm_num1
theorem ex62' : Nat.gcd (2^1001 - 1) (2^1000 - 1) = 1 := by norm_num1
theorem ex63 : Nat.gcd (2^500 - 1) (2^510 - 1) = 2^10 - 1 := by norm_num1
theorem ex64 : Int.gcd (1 - 2^500) (2^510 - 1) = 2^10 - 1 := by norm_num1
theorem ex64' : Int.gcd (1 - 2^500) (2^510 - 1) + 1 = 2^10 := by norm_num1
example : IsCoprime (1 : ℤ) 2 := by norm_num1
example : IsCoprime (2 : ℤ) 1 := by norm_num1
example : ¬ IsCoprime (0 : ℤ) 0 := by norm_num1
example : ¬ IsCoprime (0 : ℤ) 3 := by norm_num1
example : ¬ IsCoprime (2 : ℤ) 0 := by norm_num1
example : IsCoprime (2 : ℤ) 3 := by norm_num1
example : IsCoprime (3 : ℤ) 2 := by norm_num1
example : ¬ IsCoprime (2 : ℤ) 4 := by norm_num1
example : ¬ Nat.Prime 0 := by norm_num1
example : ¬ Nat.Prime 1 := by norm_num1
example : Nat.Prime 2 := by norm_num1
example : Nat.Prime 3 := by norm_num1
example : ¬ Nat.Prime 4 := by norm_num1
example : Nat.Prime 5 := by norm_num1
example : Nat.Prime 109 := by norm_num1
example : Nat.Prime 1277 := by norm_num1
example : ¬ Nat.Prime (10 ^ 1000) := by norm_num1
example : Nat.Prime (2 ^ 19 - 1) := by norm_num1
set_option maxRecDepth 8000 in
example : Nat.Prime (2 ^ 25 - 39) := by norm_num1
example : ¬ Nat.Prime ((2 ^ 19 - 1) * (2 ^ 25 - 39)) := by norm_num1
example : Nat.Prime 317 := by norm_num -decide
example : Nat.minFac 0 = 2 := by norm_num1
example : Nat.minFac 1 = 1 := by norm_num1
example : Nat.minFac (9 - 7) = 2 := by norm_num1
example : Nat.minFac 3 = 3 := by norm_num1
example : Nat.minFac 4 = 2 := by norm_num1
example : Nat.minFac 121 = 11 := by norm_num1
example : Nat.minFac 221 = 13 := by norm_num1
example : Nat.minFac (2 ^ 19 - 1) = 2 ^ 19 - 1 := by norm_num1
-- randomized tests
example : Nat.gcd 35 29 = 1 := by norm_num1
example : Int.gcd 35 29 = 1 := by norm_num1
example : Nat.lcm 35 29 = 1015 := by norm_num1
example : Int.gcd 35 29 = 1 := by norm_num1
example : Nat.Coprime 35 29 := by norm_num1
example : Nat.gcd 80 2 = 2 := by norm_num1
example : Int.gcd 80 2 = 2 := by norm_num1
example : Nat.lcm 80 2 = 80 := by norm_num1
example : Int.gcd 80 2 = 2 := by norm_num1
example : ¬ Nat.Coprime 80 2 := by norm_num1
example : Nat.gcd 19 17 = 1 := by norm_num1
example : Int.gcd 19 17 = 1 := by norm_num1
example : Nat.lcm 19 17 = 323 := by norm_num1
example : Int.gcd 19 17 = 1 := by norm_num1
example : Nat.Coprime 19 17 := by norm_num1
example : Nat.gcd 11 18 = 1 := by norm_num1
example : Int.gcd 11 18 = 1 := by norm_num1
example : Nat.lcm 11 18 = 198 := by norm_num1
example : Int.gcd 11 18 = 1 := by norm_num1
example : Nat.Coprime 11 18 := by norm_num1
example : Nat.gcd 23 73 = 1 := by norm_num1
example : Int.gcd 23 73 = 1 := by norm_num1
example : Nat.lcm 23 73 = 1679 := by norm_num1
example : Int.gcd 23 73 = 1 := by norm_num1
example : Nat.Coprime 23 73 := by norm_num1
example : Nat.gcd 73 68 = 1 := by norm_num1
example : Int.gcd 73 68 = 1 := by norm_num1
example : Nat.lcm 73 68 = 4964 := by norm_num1
example : Int.gcd 73 68 = 1 := by norm_num1
example : Nat.Coprime 73 68 := by norm_num1
example : Nat.gcd 28 16 = 4 := by norm_num1
example : Int.gcd 28 16 = 4 := by norm_num1
example : Nat.lcm 28 16 = 112 := by norm_num1
example : Int.gcd 28 16 = 4 := by norm_num1
example : ¬ Nat.Coprime 28 16 := by norm_num1
example : Nat.gcd 44 98 = 2 := by norm_num1
example : Int.gcd 44 98 = 2 := by norm_num1
example : Nat.lcm 44 98 = 2156 := by norm_num1
example : Int.gcd 44 98 = 2 := by norm_num1
example : ¬ Nat.Coprime 44 98 := by norm_num1
example : Nat.gcd 21 79 = 1 := by norm_num1
example : Int.gcd 21 79 = 1 := by norm_num1
example : Nat.lcm 21 79 = 1659 := by norm_num1
example : Int.gcd 21 79 = 1 := by norm_num1
example : Nat.Coprime 21 79 := by norm_num1
example : Nat.gcd 93 34 = 1 := by norm_num1
example : Int.gcd 93 34 = 1 := by norm_num1
example : Nat.lcm 93 34 = 3162 := by norm_num1
example : Int.gcd 93 34 = 1 := by norm_num1
example : Nat.Coprime 93 34 := by norm_num1
example : ¬ Nat.Prime 912 := by norm_num1
example : Nat.minFac 912 = 2 := by norm_num1
example : ¬ Nat.Prime 681 := by norm_num1
example : Nat.minFac 681 = 3 := by norm_num1
example : ¬ Nat.Prime 728 := by norm_num1
example : Nat.minFac 728 = 2 := by norm_num1
example : Nat.primeFactorsList 728 = [2, 2, 2, 7, 13] := by simp
example : ¬ Nat.Prime 248 := by norm_num1
example : Nat.minFac 248 = 2 := by norm_num1
example : Nat.primeFactorsList 248 = [2, 2, 2, 31] := by simp
example : ¬ Nat.Prime 682 := by norm_num1
example : Nat.minFac 682 = 2 := by norm_num1
example : Nat.primeFactorsList 682 = [2, 11, 31] := by simp
example : ¬ Nat.Prime 115 := by norm_num1
example : Nat.minFac 115 = 5 := by norm_num1
example : Nat.primeFactorsList 115 = [5, 23] := by simp
example : ¬ Nat.Prime 824 := by norm_num1
example : Nat.minFac 824 = 2 := by norm_num1
example : Nat.primeFactorsList 824 = [2, 2, 2, 103] := by simp
example : ¬ Nat.Prime 942 := by norm_num1
example : Nat.minFac 942 = 2 := by norm_num1
example : Nat.primeFactorsList 942 = [2, 3, 157] := by simp
example : ¬ Nat.Prime 34 := by norm_num1
example : Nat.minFac 34 = 2 := by norm_num1
example : Nat.primeFactorsList 34 = [2, 17] := by simp
example : ¬ Nat.Prime 754 := by norm_num1
example : Nat.minFac 754 = 2 := by norm_num1
example : Nat.primeFactorsList 754 = [2, 13, 29] := by simp
example : ¬ Nat.Prime 663 := by norm_num1
example : Nat.minFac 663 = 3 := by norm_num1
example : Nat.primeFactorsList 663 = [3, 13, 17] := by simp
example : ¬ Nat.Prime 923 := by norm_num1
example : Nat.minFac 923 = 13 := by norm_num1
example : Nat.primeFactorsList 923 = [13, 71] := by simp
example : ¬ Nat.Prime 77 := by norm_num1
example : Nat.minFac 77 = 7 := by norm_num1
example : Nat.primeFactorsList 77 = [7, 11] := by simp
example : ¬ Nat.Prime 162 := by norm_num1
example : Nat.minFac 162 = 2 := by norm_num1
example : Nat.primeFactorsList 162 = [2, 3, 3, 3, 3] := by simp
example : ¬ Nat.Prime 669 := by norm_num1
example : Nat.minFac 669 = 3 := by norm_num1
example : Nat.primeFactorsList 669 = [3, 223] := by simp
example : ¬ Nat.Prime 476 := by norm_num1
example : Nat.minFac 476 = 2 := by norm_num1
example : Nat.primeFactorsList 476 = [2, 2, 7, 17] := by simp
example : Nat.Prime 251 := by norm_num1
example : Nat.minFac 251 = 251 := by norm_num1
example : Nat.primeFactorsList 251 = [251] := by simp
example : ¬ Nat.Prime 129 := by norm_num1
example : Nat.minFac 129 = 3 := by norm_num1
example : Nat.primeFactorsList 129 = [3, 43] := by simp
example : ¬ Nat.Prime 471 := by norm_num1
example : Nat.minFac 471 = 3 := by norm_num1
example : Nat.primeFactorsList 471 = [3, 157] := by simp
example : ¬ Nat.Prime 851 := by norm_num1
example : Nat.minFac 851 = 23 := by norm_num1
example : Nat.primeFactorsList 851 = [23, 37] := by simp
/-
example : ¬ Squarefree 0 := by norm_num1
example : Squarefree 1 := by norm_num1
example : Squarefree 2 := by norm_num1
example : Squarefree 3 := by norm_num1
example : ¬ Squarefree 4 := by norm_num1
example : Squarefree 5 := by norm_num1
example : Squarefree 6 := by norm_num1
example : Squarefree 7 := by norm_num1
example : ¬ Squarefree 8 := by norm_num1
example : ¬ Squarefree 9 := by norm_num1
example : Squarefree 10 := by norm_num1
example : Squarefree (2*3*5*17) := by norm_num1
example : ¬ Squarefree (2*3*5*5*17) := by norm_num1
example : Squarefree 251 := by norm_num1
example : Squarefree (3 : ℤ) := by
-- `norm_num` should fail on this example, instead of producing an incorrect proof.
fail_if_success norm_num1
exact Irreducible.squarefree (Prime.irreducible
(Int.prime_iff_natAbs_prime.mpr (by norm_num)))
example : @Squarefree ℕ Multiplicative.monoid 1 := by
-- `norm_num` should fail on this example, instead of producing an incorrect proof.
-- fail_if_success norm_num1
-- the statement was deliberately wacky, let's fix it
change Squarefree (Multiplicative.ofAdd 1 : Multiplicative ℕ)
rintro x ⟨dx, hd⟩
revert x dx
rw [Multiplicative.ofAdd.surjective.forall₂]
intro x dx h
simp_rw [← ofAdd_add, Multiplicative.ofAdd.injective.eq_iff] at h
cases x
· simp [isUnit_one]
· simp only [Nat.succ_add, Nat.add_succ] at h
cases h
-/
section NatLog
example : Nat.log 0 0 = 0 := by norm_num1
example : Nat.log 0 1 = 0 := by norm_num1
example : Nat.log 0 100 = 0 := by norm_num1
example : Nat.log 1 0 = 0 := by norm_num1
example : Nat.log 1 1 = 0 := by norm_num1
example : Nat.log 1 100 = 0 := by norm_num1
example : Nat.log 10 0 = 0 := by norm_num1
example : Nat.log 10 3 = 0 := by norm_num1
example : Nat.log 2 2 = 1 := by norm_num1
example : Nat.log 2 256 = 8 := by norm_num1
example : Nat.log 10 10000000 = 7 := by norm_num1
example : Nat.log 10 (10 ^ 7 + 2) + Nat.log 2 (2 ^ 30 + 3) = 7 + 30 := by norm_num1
example : Nat.clog 0 0 = 0 := by norm_num1
example : Nat.clog 0 1 = 0 := by norm_num1
example : Nat.clog 0 100 = 0 := by norm_num1
example : Nat.clog 1 0 = 0 := by norm_num1
example : Nat.clog 1 1 = 0 := by norm_num1
example : Nat.clog 1 100 = 0 := by norm_num1
example : Nat.clog 10 0 = 0 := by norm_num1
example : Nat.clog 10 3 = 1 := by norm_num1
example : Nat.clog 2 2 = 1 := by norm_num1
example : Nat.clog 2 256 = 8 := by norm_num1
example : Nat.clog 10 10000000 = 7 := by norm_num1
example : Nat.clog 10 (10 ^ 7 + 2) + Nat.clog 2 (2 ^ 30 + 3) = 8 + 31 := by norm_num1
end NatLog
example : Nat.fib 0 = 0 := by norm_num1
example : Nat.fib 1 = 1 := by norm_num1
example : Nat.fib 2 = 1 := by norm_num1
example : Nat.fib 3 = 2 := by norm_num1
example : Nat.fib 4 = 3 := by norm_num1
example : Nat.fib 5 = 5 := by norm_num1
example : Nat.fib 6 = 8 := by norm_num1
example : Nat.fib 7 = 13 := by norm_num1
example : Nat.fib 8 = 21 := by norm_num1
example : Nat.fib 9 = 34 := by norm_num1
example : Nat.fib 10 = 55 := by norm_num1
example : Nat.fib 37 = 24157817 := by norm_num1
example : Nat.fib 63 = 6557470319842 := by norm_num1
example : Nat.fib 64 = 10610209857723 := by norm_num1
example : Nat.fib 65 = 17167680177565 := by norm_num1
example : Nat.fib 100 + Nat.fib 101 = Nat.fib 102 := by norm_num1
example : Nat.fib 1000 + Nat.fib 1001 = Nat.fib 1002 := by norm_num1
section big_operators
variable {α : Type _} [CommRing α]
-- Lists:
-- `by decide` closes the three goals below.
example : ([1, 2, 1, 3]).sum = 7 := by norm_num +decide only
example : (List.range 10).sum = 45 := by norm_num +decide only
example : (List.finRange 10).sum = 45 := by norm_num +decide only
example : (([1, 2, 1, 3] : List ℚ).map (fun i => i^2)).sum = 15 := by norm_num
-- Multisets:
-- `by decide` closes the three goals below.
example : (1 ::ₘ 2 ::ₘ 1 ::ₘ 3 ::ₘ {}).sum = 7 := by norm_num +decide only
example : ((1 ::ₘ 2 ::ₘ 1 ::ₘ 3 ::ₘ {}).map (fun i => i^2)).sum = 15 := by
norm_num +decide only
example : (Multiset.range 10).sum = 45 := by norm_num +decide only
example : (↑[1, 2, 1, 3] : Multiset ℕ).sum = 7 := by norm_num +decide only
example : (({1, 2, 1, 3} : Multiset ℚ).map (fun i => i^2)).sum = 15 := by
norm_num
-- Finsets:
example : Finset.prod (Finset.cons 2 ∅ (Finset.notMem_empty _)) (fun x ↦ x) = 2 := by norm_num1
example : Finset.prod
(Finset.cons 6 (Finset.cons 2 ∅ (Finset.notMem_empty _)) (by norm_num))
(fun x ↦ x) =
12 := by norm_num1
example (f : ℕ → α) : ∏ i ∈ Finset.range 0, f i = 1 := by norm_num1
example (f : Fin 0 → α) : ∏ i : Fin 0, f i = 1 := by norm_num1
example (f : Fin 0 → α) : ∑ i : Fin 0, f i = 0 := by norm_num1
example (f : ℕ → α) : ∑ i ∈ (∅ : Finset ℕ), f i = 0 := by norm_num1
example : ∑ _ : Fin 3, 1 = 3 := by norm_num1
/-
example : ∑ i : Fin 3, (i : ℕ) = 3 := by norm_num1
example : ((0 : Fin 3) : ℕ) = 0 := by norm_num1
example (f : Fin 3 → α) : ∑ i : Fin 3, f i = f 0 + f 1 + f 2 := by norm_num <;> ring
example (f : Fin 4 → α) : ∑ i : Fin 4, f i = f 0 + f 1 + f 2 + f 3 := by norm_num <;> ring
example (f : ℕ → α) : ∑ i ∈ {0, 1, 2}, f i = f 0 + f 1 + f 2 := by norm_num; ring
example (f : ℕ → α) : ∑ i ∈ {0, 2, 2, 3, 1, 0}, f i = f 0 + f 1 + f 2 + f 3 := by norm_num; ring
example (f : ℕ → α) : ∑ i ∈ {0, 2, 2 - 3, 3 - 1, 1, 0}, f i = f 0 + f 1 + f 2 := by norm_num; ring
-/
example : ∑ i ∈ Finset.range 10, i = 45 := by norm_num1
example : ∑ i ∈ Finset.range 10, (i^2 : ℕ) = 285 := by norm_num1
example : ∏ i ∈ Finset.range 4, ((i+1)^2 : ℕ) = 576 := by norm_num1
/-
example : (∑ i ∈ Finset.Icc 5 10, (i^2 : ℕ)) = 355 := by norm_num
example : (∑ i ∈ Finset.Ico 5 10, (i^2 : ℕ)) = 255 := by norm_num
example : (∑ i ∈ Finset.Ioc 5 10, (i^2 : ℕ)) = 330 := by norm_num
example : (∑ i ∈ Finset.Ioo 5 10, (i^2 : ℕ)) = 230 := by norm_num
example : (∑ i ∈ Finset.Ioo (-5) 5, i^2) = 60 := by norm_num
example (f : ℕ → α) : ∑ i ∈ Finset.mk {0, 1, 2} dec_trivial, f i = f 0 + f 1 + f 2 := by
norm_num; ring
-/
-- Combined with other `norm_num` extensions:
example : ∏ i ∈ Finset.range 9, Nat.sqrt (i + 1) = 96 := by norm_num1
-- example : ∏ i ∈ {1, 4, 9, 16}, Nat.sqrt i = 24 := by norm_num1
-- example : ∏ i ∈ Finset.Icc 0 8, Nat.sqrt (i + 1) = 96 := by norm_num1
-- Nested operations:
-- example : ∑ i : Fin 2, ∑ j : Fin 2, ![![0, 1], ![2, 3]] i j = 6 := by norm_num1
end big_operators
section floor
section Semiring
variable (R : Type*) [Semiring R] [LinearOrder R] [IsStrictOrderedRing R] [FloorSemiring R]
example : ⌊(2 : R)⌋₊ = 2 := by norm_num1
example : ⌈(2 : R)⌉₊ = 2 := by norm_num1
end Semiring
section Ring
variable (R : Type*) [Ring R] [LinearOrder R] [IsStrictOrderedRing R] [FloorRing R]
example : ⌊(-1 : R)⌋ = -1 := by norm_num1
example : ⌊(2 : R)⌋ = 2 := by norm_num1
example : ⌈(-1 : R)⌉ = -1 := by norm_num1
example : ⌈(2 : R)⌉ = 2 := by norm_num1
example : ⌊(-2 : R)⌋₊ = 0 := by norm_num1
example : ⌈(-3 : R)⌉₊ = 0 := by norm_num1
example : round (1 : R) = 1 := by norm_num1
example : Int.fract (2 : R) = 0 := by norm_num1
example : round (-3 : R) = -3 := by norm_num1
example : Int.fract (-3 : R) = 0 := by norm_num1
end Ring
section Semifield
variable (K : Type*) [Semifield K] [LinearOrder K] [IsStrictOrderedRing K] [FloorSemiring K]
example : ⌊(35 / 16 : K)⌋₊ = 2 := by norm_num1
example : ⌈(35 / 16 : K)⌉₊ = 3 := by norm_num1
end Semifield
section Field
variable (K : Type*) [Field K] [LinearOrder K] [IsStrictOrderedRing K] [FloorRing K]
example : ⌊(15 / 16 : K)⌋ + 1 = 1 := by norm_num1
example : ⌊(-15 / 16 : K)⌋ + 1 = 0 := by norm_num1
example : ⌈(15 / 16 : K)⌉ + 1 = 2 := by norm_num1
example : ⌈(-15 / 16 : K)⌉ + 1 = 1 := by norm_num1
example : ⌊(-35 / 16 : K)⌋₊ = 0 := by norm_num1
example : ⌈(-35 / 16 : K)⌉₊ = 0 := by norm_num1
example : round (-35 / 16 : K) = -2 := by norm_num1
example : Int.fract (16 / 15 : K) = 1 / 15 := by norm_num1
example : Int.fract (-35 / 16 : K) = 13 / 16 := by norm_num1
example : Int.fract (3.7 : ℚ) = 0.7 := by norm_num1
example : Int.fract (-3.7 : ℚ) = 0.3 := by norm_num1
end Field
end floor
section jacobi
-- Jacobi and Legendre symbols
open scoped NumberTheorySymbols
example : J(123 | 335) = -1 := by norm_num1
example : J(-2345 | 6789) = -1 := by norm_num1
example : J(-1 | 1655801) = 1 := by norm_num1
example : J(-102334155 | 165580141) = -1 := by norm_num1
example : J(58378362899022564339483801989973056405585914719065 |
53974350278769849773003214636618718468638750007307) = -1 := by norm_num1
example : J(3 + 4 | 3 * 5) = -1 := by norm_num1
example : J(J(-1 | 7) | 11) = -1 := by norm_num1
instance prime_1000003 : Fact (Nat.Prime 1000003) := ⟨by norm_num1⟩
example : legendreSym 1000003 7 = -1 := by norm_num1
end jacobi
section even_odd
example : Even 16 := by norm_num1
example : ¬Even 17 := by norm_num1
example : Even (16 : ℤ) := by norm_num1
example : ¬Even (17 : ℤ) := by norm_num1
example : Even (-20 : ℤ) := by norm_num1
example : ¬Even (-21 : ℤ) := by norm_num1
example : Odd 5 := by norm_num1
example : ¬Odd 4 := by norm_num1
example : Odd (5 : ℤ) := by norm_num1
example : ¬Odd (4 : ℤ) := by norm_num1
example : Odd (-5 : ℤ) := by norm_num1
example : ¬Odd (-4 : ℤ) := by norm_num1
end even_odd
section mod
example : (5 : ℕ) % 4 = 1 := by norm_num1
example : (3 : ℕ) % 2 = 1 := by norm_num1
example : 3 + (42 : ℕ) % 5 = 5 := by norm_num1
example : (5 : ℤ) % 4 = 1 := by norm_num1
example : (2 : ℤ) % 2 = 0 := by norm_num1
example : (3 : ℤ) % 2 = 1 := by norm_num1
example : (3 : ℤ) % 4 = 3 := by norm_num1
example : (-3 : ℤ) % 4 = 1 := by norm_num1
example : (3 : ℤ) % -4 = 3 := by norm_num1
example : 3 + (42 : ℤ) % 5 = 5 := by norm_num1
example : 2 ∣ 4 := by norm_num1
example : ¬ 2 ∣ 5 := by norm_num1
example : 553105253 ∣ 553105253 * 776531401 := by norm_num1
example : ¬ 553105253 ∣ 553105253 * 776531401 + 1 := by norm_num1
example : (2 : ℤ) ∣ 4 := by norm_num1
example : ¬ (2 : ℤ) ∣ 5 := by norm_num1
example : (553105253 : ℤ) ∣ 553105253 * 776531401 := by norm_num1
example : ¬ (553105253 : ℤ) ∣ 553105253 * 776531401 + 1 := by norm_num1
example : 10 ≡ 7 [MOD 3] := by norm_num1
example : ¬ (10 ≡ 7 [MOD 5]) := by norm_num1
example : 10 ≡ 7 [ZMOD 3] := by norm_num1
example : ¬ (10 ≡ 7 [ZMOD 5]) := by norm_num1
example : -3 ≡ 7 [ZMOD 5] := by norm_num1
example : ¬ (-3 ≡ 7 [ZMOD 50]) := by norm_num1
example : 10 ≡ 7 [ZMOD -3] := by norm_num1
example : ¬ (10 ≡ 7 [ZMOD -5]) := by norm_num1
example : -3 ≡ 7 [ZMOD -5] := by norm_num1
example : ¬ (-3 ≡ 7 [ZMOD -50]) := by norm_num1
end mod
section num_den
example : (6 / 15 : ℚ).num = 2 := by norm_num1
example : (6 / 15 : ℚ).den = 5 := by norm_num1
example : (-6 / 15 : ℚ).num = -2 := by norm_num1
example : (-6 / 15 : ℚ).den = 5 := by norm_num1
end num_den
section real_sqrt
example : Real.sqrt 25 = 5 := by norm_num
example : Real.sqrt (25 / 16) = 5 / 4 := by norm_num
example : Real.sqrt (0.25) = 1/2 := by norm_num
example : NNReal.sqrt 25 = 5 := by norm_num
example : NNReal.sqrt (25 / 16) = 5 / 4 := by norm_num
example : Real.sqrt (-37) = 0 := by norm_num
example : Real.sqrt (-5 / 3) = 0 := by norm_num
example : Real.sqrt 0 = 0 := by norm_num
example : NNReal.sqrt 0 = 0 := by norm_num
end real_sqrt
section Factorial
open Nat
example : 0! = 1 := by norm_num1
example : 1! = 1 := by norm_num1
example : 2! = 2 := by norm_num1
example : 3! = 6 := by norm_num1
example : 4! = 24 := by norm_num1
example : 10! = 3628800 := by norm_num1
example : 1000! / 999! = 1000 := by norm_num1
example : (Nat.sqrt 1024)! = 32! := by norm_num1
example : (1 : ℚ) / 0 ! + 1 / 1 ! + 1 / 2 ! + 1 / 3! + 1 / 4! = 65 / 24 := by norm_num1
example : (4 + 2).ascFactorial 3 = 336 := by norm_num1
example : (5 + 5).descFactorial 2 = 90 := by norm_num1
example : (1000000).descFactorial 1000001 = 0 := by norm_num1
example : (200 : ℕ) ! / (10 ^ 370) = 78865 := by norm_num1
end Factorial
section irrational
example : Irrational √2 := by norm_num1
example : Irrational √(5 - 2) := by norm_num1
example : Irrational √(7/4) := by norm_num1
example : Irrational √(4/7) := by norm_num1
example : Irrational √(1/2 + 1/2 + 1/3) := by norm_num1
example : Irrational (100 ^ (1/3 : ℝ)) := by norm_num1
example : Irrational ((27/38) ^ (1/3 : ℝ)) := by norm_num1
example : Irrational ((87/6) ^ (54/321 : ℝ)) := by norm_num1
-- Large prime number
-- The current implementation should run in O(log n) time
example : Irrational
√5210644015679228794060694325390955853335898483908056458352183851018372555735221 := by norm_num1
-- Large numerator does not affect performance.
-- We only need to check that it is coprime with the denominator.
example : Irrational (100 ^ ((10^1000 + 10^500) / 3 : ℝ)) := by norm_num1
end irrational |
.lake/packages/mathlib/MathlibTest/MkIffOfInductive.lean | import Mathlib.Tactic.MkIffOfInductiveProp
import Mathlib.Data.List.Perm.Lattice
mk_iff_of_inductive_prop List.IsChain test.chain_iff
example {α : Type _} (R : α → α → Prop) (al : List α) :
List.IsChain R al ↔
al = List.nil ∨ (∃ a, al = [a]) ∨
∃ (a b : α) (l : List α), R a b ∧ List.IsChain R (b :: l) ∧ al = a :: b :: l :=
test.chain_iff R al
/--
info: test.chain_iff.{u_1} {α : Type u_1} (R : α → α → Prop) (a✝ : List α) :
List.IsChain R a✝ ↔ a✝ = [] ∨ (∃ a, a✝ = [a]) ∨ ∃ a b l, R a b ∧ List.IsChain R (b :: l) ∧ a✝ = a :: b :: l
-/
#guard_msgs in
#check test.chain_iff
mk_iff_of_inductive_prop False test.false_iff
example : False ↔ False := test.false_iff
mk_iff_of_inductive_prop True test.true_iff
example : True ↔ True := test.true_iff
universe u
mk_iff_of_inductive_prop Nonempty test.non_empty_iff
example (α : Sort u) : Nonempty α ↔ ∃ (_ : α), True := test.non_empty_iff α
mk_iff_of_inductive_prop And test.and_iff
example (p q : Prop) : And p q ↔ p ∧ q := test.and_iff p q
mk_iff_of_inductive_prop Or test.or_iff
example (p q : Prop) : Or p q ↔ p ∨ q := test.or_iff p q
mk_iff_of_inductive_prop Eq test.eq_iff
example (α : Sort u) (a b : α) : a = b ↔ b = a := test.eq_iff a b
mk_iff_of_inductive_prop HEq test.heq_iff
example {α : Sort u} (a : α) {β : Sort u} (b : β) : a ≍ b ↔ β = α ∧ b ≍ a := test.heq_iff a b
mk_iff_of_inductive_prop List.Perm test.perm_iff
open scoped List in
example {α : Type _} (a b : List α) :
a ~ b ↔
a = List.nil ∧ b = List.nil ∨
(∃ (x : α) (l₁ l₂ : List α), l₁ ~ l₂ ∧ a = x :: l₁ ∧ b = x :: l₂) ∨
(∃ (x y : α) (l : List α), a = y :: x :: l ∧ b = x :: y :: l) ∨
∃ (l₂ : List α), a ~ l₂ ∧ l₂ ~ b := test.perm_iff a b
mk_iff_of_inductive_prop List.Pairwise test.pairwise_iff
example {α : Type} (R : α → α → Prop) (al : List α) :
List.Pairwise R al ↔
al = List.nil ∨
∃ (a : α) (l : List α), (∀ (a' : α), a' ∈ l → R a a') ∧ List.Pairwise R l ∧ al = a :: l := test.pairwise_iff R al
inductive test.is_true (p : Prop) : Prop
| triviality (h : p) : test.is_true p
mk_iff_of_inductive_prop test.is_true test.is_true_iff
example (p : Prop) : test.is_true p ↔ p := test.is_true_iff p
@[mk_iff]
structure foo (m n : Nat) : Prop where
equal : m = n
sum_eq_two : m + n = 2
example (m n : Nat) : foo m n ↔ m = n ∧ m + n = 2 := foo_iff m n
@[mk_iff bar]
structure foo2 (m n : Nat) : Prop where
equal : m = n
sum_eq_two : m + n = 2
example (m n : Nat) : foo2 m n ↔ m = n ∧ m + n = 2 := bar m n
@[mk_iff]
inductive ReflTransGen {α : Type _} (r : α → α → Prop) (a : α) : α → Prop
| refl : ReflTransGen r a a
| tail {b c} : ReflTransGen r a b → r b c → ReflTransGen r a c
example {α : Type} (r : α → α → Prop) (a c : α) :
ReflTransGen r a c ↔ c = a ∨ ∃ b : α, ReflTransGen r a b ∧ r b c :=
reflTransGen_iff r a c |
.lake/packages/mathlib/MathlibTest/finset_builder.lean | import Mathlib.Order.Interval.Finset.Basic
/-!
# Examples of finset builder notation
-/
open Finset
variable {α : Type*} (p : α → Prop) [DecidablePred p]
/-! ## `Data.Finset.Basic` -/
example (s : Finset α) : {x ∈ s | p x} = s.filter p := rfl
/-! ## `Data.Fintype.Basic` -/
section Fintype
variable [Fintype α]
example : ({x | p x} : Finset α) = univ.filter p := rfl
example : ({x : α | p x} : Finset α) = univ.filter p := rfl
-- If the type of `s` (or the entire expression) is `Finset ?α`, elaborate as `Finset`;
-- otherwise as `Set`
example (s : Finset α) : {x ∈ s | p x} = s.filter p := rfl
example (s : Finset α) : ({x ∈ s | p x} : Set α) = setOf fun x => x ∈ s ∧ p x := rfl
example (s : Finset α) : {x ∈ (s : Set α) | p x} = setOf fun x => x ∈ s ∧ p x := rfl
-- elaborate as `Set` if no expected type present
example : {x | p x} = setOf p := rfl
example : {x : α | p x} = setOf p := rfl
variable [DecidableEq α]
example (s : Finset α) : {x ∉ s | p x} = sᶜ.filter p := rfl
example (a : α) : ({x ≠ a | p x} : Finset α) = ({a}ᶜ : Finset α).filter p := rfl
-- elaborate as `Set` if the `s` or the expected type is not `Finset ?α`
example (s : Set α) : {x ∉ s | p x} = setOf fun x => x ∉ s ∧ p x := rfl
example (a : α) : {x ≠ a | p x} = setOf fun x => x ≠ a ∧ p x := rfl
end Fintype
/-! ## `Order.Interval.Finset.Basic` -/
section LocallyFiniteOrderBot
variable [Preorder α] [LocallyFiniteOrderBot α]
example (a : α) : ({x ≤ a | p x} : Finset α) = (Iic a).filter p := rfl
example (a : α) : ({x < a | p x} : Finset α) = (Iio a).filter p := rfl
-- elaborate as `Set` if the expected type is not `Finset ?α`
example (a : α) : {x ≤ a | p x} = setOf fun x => x ≤ a ∧ p x := rfl
example (a : α) : {x < a | p x} = setOf fun x => x < a ∧ p x := rfl
end LocallyFiniteOrderBot
section LocallyFiniteOrderTop
variable [Preorder α] [LocallyFiniteOrderTop α]
example (a : α) : ({x ≥ a | p x} : Finset α) = (Ici a).filter p := rfl
example (a : α) : ({x > a | p x} : Finset α) = (Ioi a).filter p := rfl
-- elaborate as `Set` if the expected type is not `Finset ?α`
example (a : α) : {x ≥ a | p x} = setOf fun x => x ≥ a ∧ p x := rfl
example (a : α) : {x > a | p x} = setOf fun x => x > a ∧ p x := rfl
end LocallyFiniteOrderTop |
.lake/packages/mathlib/MathlibTest/Header.lean | import Mathlib.Tactic.Linter.Header
import Lake
import Mathlib.Tactic.Linter.Header
import /- -/ Mathlib.Tactic -- the `TextBased` linter does not flag this `broadImport`
import Mathlib.Tactic.Have
import Mathlib.Deprecated.Aliases
/--
warning: In the past, importing 'Lake' in mathlib has led to dramatic slow-downs of the linter (see e.g. https://github.com/leanprover-community/mathlib4/pull/13779). Please consider carefully if this import is useful and make sure to benchmark it. If this is fine, feel free to silence this linter.
Note: This linter can be disabled with `set_option linter.style.header false`
---
warning: Files in mathlib cannot import the whole tactic folder.
Note: This linter can be disabled with `set_option linter.style.header false`
---
warning: 'Mathlib.Tactic.Have' defines a deprecated form of the 'have' tactic; please do not use it in mathlib.
Note: This linter can be disabled with `set_option linter.style.header false`
---
warning: Duplicate imports: 'Mathlib.Tactic.Linter.Header' already imported
Note: This linter can be disabled with `set_option linter.style.header false`
---
warning: The module doc-string for a file should be the first command after the imports.
Please, add a module doc-string before `/-!# Tests for the `docModule` linter
-/
`.
Note: This linter can be disabled with `set_option linter.style.header false`
-/
#guard_msgs in
set_option linter.style.header true in
/-!
# Tests for the `docModule` linter
-/
/--
A convenience function that replaces `/` with `|`.
This converts `/-` and `-/` into `|-` and `-|` that no longer interfere with the ending of the
`#guard_msgs` doc-string.
-/
def replaceMultilineComments (s : String) : String :=
s.replace "/" "|"
open Lean Elab Command in
/--
`#check_copyright cop` takes as input the `String` `cop`, expected to be a copyright statement.
It logs details of what the linter would report if the `cop` is "malformed".
-/
elab "#check_copyright " copStx:str : command => do
let cop := copStx.getString
let offset := copStx.raw.getPos?.get!.increaseBy 1
for (s, m) in Mathlib.Linter.copyrightHeaderChecks cop do
if let some rg := s.getRange? then
logInfoAt (.ofRange ({start := rg.start.offsetBy offset, stop := rg.stop.offsetBy offset}))
m!"Text: `{replaceMultilineComments s.getAtomVal}`\n\
Range: {(rg.start, rg.stop)}\n\
Message: '{replaceMultilineComments m}'"
-- well-formed
#check_copyright
"
/--
info: Text: ` |-`
Range: (0, 3)
Message: 'Malformed or missing copyright header: `|-` should be alone on its own line.'
-/
#guard_msgs in
#check_copyright
"
"
-- The last line does not end with a newline.
/--
info: Text: `-|`
Range: (149, 151)
Message: 'Malformed or missing copyright header: `-|` should be alone on its own line.'
-/
#guard_msgs in
#check_copyright
/--
info: Text: `Authors: Name LastName
Here comes an implicit docstring which doesn't belong here!`
Range: (126, 209)
Message: 'If an authors line spans multiple lines, each line but the last must end with a trailing comma'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `Authors: Name LastName
Here comes an implicit docstring which shouldn't be here!`
Range: (126, 206)
Message: 'If an authors line spans multiple lines, each line but the last must end with a trailing comma'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `-|`
Range: (126, 128)
Message: 'Copyright too short!'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `-|`
Range: (1, 3)
Message: 'Copyright too short!'
-/
#guard_msgs in
#check_copyright ""
/--
info: Text: `-|`
Range: (1, 3)
Message: 'Copyright too short!'
-/
#guard_msgs in
#check_copyright ""
/--
info: Text: `Cpyright (c) 202`
Range: (3, 19)
Message: 'Copyright line should start with 'Copyright (c) YYYY''
-/
#guard_msgs in
#check_copyright
"/-
Cpyright (c) 2024 Damiano Testa. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Name LastName
-/
"
/--
info: Text: `a. All rights reserve.`
Range: (34, 56)
Message: 'Copyright line should end with '. All rights reserved.''
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `Rleased under Apache 2.0 license as described in the file LICENSE.`
Range: (58, 124)
Message: 'Second copyright line should be
"Released under Apache 2.0 license as described in the file LICENSE."'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `A uthors:`
Range: (126, 135)
Message: 'The authors line should begin with 'Authors: ''
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: ` `
Range: (139, 141)
Message: 'Double spaces are not allowed.'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `.`
Range: (148, 149)
Message: 'Please, do not end the authors' line with a period.'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: ` `
Range: (149, 151)
Message: 'Double spaces are not allowed.'
-/
#guard_msgs in
#check_copyright
"
-- The `Copyright` and `Authors` lines are allowed to overflow.
#check_copyright
"
-- However, in this case the wrapped lines must end with a comma.
/--
info: Text: `(c) 2024 Damiano Testa`
Range: (13, 35)
Message: 'Copyright line should end with '. All rights reserved.''
---
info: Text: `Released under Apache 2.0 license as described in the file LICENSE.
Authors: Name LastName
and others.`
Range: (83, 187)
Message: 'If an authors line spans multiple lines, each line but the last must end with a trailing comma'
---
info: Text: `Released `
Range: (83, 92)
Message: 'The authors line should begin with 'Authors: ''
---
info: Text: ` `
Range: (174, 176)
Message: 'Double spaces are not allowed.'
---
info: Text: ` and `
Range: (175, 180)
Message: 'Please, do not use 'and'; use ',' instead.'
---
info: Text: `.`
Range: (106, 107)
Message: 'Please, do not end the authors' line with a period.'
---
info: Text: ` and many other authors. All rights reserved.`
Range: (36, 82)
Message: 'Second copyright line should be "Released under Apache 2.0 license as described in the file LICENSE."'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `Authors:`
Range: (140, 148)
Message: 'The authors line should begin with 'Authors: ''
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `. All rights reserved.`
Range: (21, 43)
Message: ''Copyright (c) YYYY' should be followed by a space'
-/
#guard_msgs in
#check_copyright
"
/--
info: Text: `. All rights reserved.`
Range: (22, 44)
Message: 'There should be at least one copyright author, separated from the year by exactly one space.'
-/
#guard_msgs in
#check_copyright
"
-- We don't do further validation of names.
#guard_msgs in
#check_copyright
"
#guard_msgs in
#check_copyright
"
#guard_msgs in
#check_copyright
" |
.lake/packages/mathlib/MathlibTest/LibraryRewrite.lean | import Mathlib.Tactic.Widget.LibraryRewrite
import Mathlib.Algebra.Group.Nat.Defs
import Mathlib.Data.Subtype
import Batteries.Data.Nat.Gcd
-- set_option trace.profiler true
-- set_option trace.rw?? true
variable (n : Nat) (p q : Prop)
/--
info: Pattern ∀ (p : P), P → Q p
· p → q
forall_self_imp
Pattern a → b → c
· p ∧ p → q
and_imp
Pattern ∀ (hp : p) (hq : q), r hp hq
· p ∧ p → q
forall_and_index'
Pattern ∀ (x : α), p x → b
· (∃ x, p) → q
exists_imp
Pattern ∀ (x : α) (h : p x), q x h
· ∀ (x : { a // p }), q
Subtype.forall'
Pattern a → b
· ¬p ∨ (p → q)
imp_iff_not_or
· (p → q) ∨ ¬p
imp_iff_or_not
· ¬(p → q) → ¬p
not_imp_not
· p → q ↔ p ∨ (p → q)
iff_or_self
· p → q ↔ (p → q) ∨ p
iff_self_or
· p ↔ (p → q) ∧ p
iff_and_self
· p ↔ p ∧ (p → q)
iff_self_and
· Nonempty p → p → q
Nonempty.imp
· ¬(p ∧ ¬(p → q))
not_and_not_right
· (p → q) ∨ p ↔ p → q
or_iff_left_iff_imp
· p ∧ (p → q) ↔ p
and_iff_left_iff_imp
· p ∨ (p → q) ↔ p → q
or_iff_right_iff_imp
· (p → q) ∧ p ↔ p
and_iff_right_iff_imp
· ¬p
⊢ ¬(p → q)
imp_iff_not
· p → q
⊢ p
imp_iff_right
Pattern ∀ (p : P), Q p
· p → p → p → q
forall_self_imp
· ¬∃ x, ¬(p → q)
Classical.not_exists_not
· True
⊢ p → (p → q ↔ True)
forall_true_iff'
· p → q
⊢ p
forall_prop_of_true
-/
#guard_msgs in
#rw?? p → p → q
/--
info: Pattern n + 1
· n.succ
Nat.add_one
· Std.PRange.succ n
Std.PRange.Nat.succ_eq
· (*...=n).size
Std.PRange.Nat.size_Ric
· (↑n + 1).toNat
Int.toNat_natCast_add_one
Pattern n + m
· 1 + n
Nat.add_comm
· n.add 1
Nat.add_eq
· n.succ + 1 - 1
Nat.succ_add_sub_one
· n + Nat.succ 1 - 1
Nat.add_succ_sub_one
· max (n + 1) 1
Nat.add_left_max_self
· max 1 (n + 1)
Nat.max_add_left_self
· max (n + 1) n
Nat.add_right_max_self
· max n (n + 1)
Nat.max_add_right_self
Pattern a + b
· 1 + n
add_comm
· [n, 1].sum
List.sum_pair
-/
#guard_msgs in
#rw?? n + 1
/--
info: Pattern n / 2
· n >>> 1
Nat.shiftRight_one
Pattern x / y
· if 0 < 2 ∧ 2 ≤ n then (n - 2) / 2 + 1 else 0
Nat.div_eq
· (n - n % 2) / 2
Nat.div_eq_sub_mod_div
· 0
⊢ n < 2
Nat.div_eq_of_lt
· (n + 1) / 2
⊢ ¬2 ∣ n + 1
Nat.succ_div_of_not_dvd
· (n - 2) / 2 + 1
⊢ 0 < 2
⊢ 2 ≤ n
Nat.div_eq_sub_div
-/
#guard_msgs in
#rw?? n/2
/--
info: Pattern n.gcd n
· n
Nat.gcd_self
Pattern m.gcd n
· (n % n).gcd n
Nat.gcd_rec
· if n = 0 then n else (n % n).gcd n
Nat.gcd_def
· (n + n).gcd n
Nat.gcd_add_self_left
· n.gcd (n + n)
Nat.gcd_add_self_right
· (↑n).gcd ↑n
Int.gcd_natCast_natCast
· (n.gcd n).gcd n
Nat.gcd_gcd_self_left_left
· n.gcd (n.gcd n)
Nat.gcd_gcd_self_right_left
· n
⊢ n ∣ n
Nat.gcd_eq_left
· 1
⊢ n.Coprime n
Nat.Coprime.gcd_eq_one
· (n - n).gcd n
⊢ n ≤ n
Nat.gcd_self_sub_left
· n.gcd (n - n)
⊢ n ≤ n
Nat.gcd_self_sub_right
-/
#guard_msgs in
#rw?? Nat.gcd n n
def atZero (f : Int → Int) : Int := f 0
theorem atZero_neg (f : Int → Int) : atZero (fun x => - f x) = - atZero f := rfl
theorem neg_atZero_neg (f : Int → Int) : - atZero (fun x => - f x) = atZero f := Int.neg_neg (f 0)
theorem atZero_add (f g : Int → Int) : atZero (fun x => f x + g x) = atZero f + atZero g := rfl
theorem atZero_add_const (f : Int → Int) (c : Int) : atZero (fun x => f x + c) = atZero f + c := rfl
/--
info: Pattern atZero fun x => f x + c
· (atZero fun x => x) + 1
atZero_add_const
Pattern atZero fun x => f x + g x
· (atZero fun x => x) + atZero fun x => 1
atZero_add
Pattern atZero f
· -atZero fun x => -(x + 1)
neg_atZero_neg
-/
#guard_msgs in
#rw?? atZero fun x => x + 1
/--
info: Pattern atZero fun x => -f x
· -atZero fun x => x
atZero_neg
Pattern atZero f
· -atZero fun x => - -x
neg_atZero_neg
-/
#guard_msgs in
#rw?? atZero (Neg.neg : Int → Int)
-- `Nat.Coprime` is reducible, so we might get matches with the pattern `n = 1`.
-- This doesn't work with the `rw` tactic though, so we make sure to avoid them.
/--
info: Pattern n.Coprime m
· Nat.Coprime 3 2
Nat.coprime_comm
· Nat.gcd 2 3 = 1
Nat.coprime_iff_gcd_eq_one
-/
#guard_msgs in
#rw?? Nat.Coprime 2 3 |
.lake/packages/mathlib/MathlibTest/spread.lean | import Mathlib.Tactic.Spread
set_option autoImplicit true
class Foo (α : Type) where
bar : True
class Something where
bar : True
instance : Something where
bar := by trivial
instance : Foo α where
__ := instSomething -- include fields from `instSomething`
example : Foo α := {
__ := instSomething -- include fields from `instSomething`
}
structure A (α : Type) where
default : α
x : α
axiom mkDefault (α : Type) : Inhabited α
noncomputable example (α : Type) : A α where
__ := mkDefault α
x := default |
.lake/packages/mathlib/MathlibTest/casesm.lean | import Mathlib.Tactic.CasesM
set_option autoImplicit true
set_option linter.unusedVariables false
set_option linter.unusedTactic false in
example (h : a ∧ b ∨ c ∧ d) (h2 : e ∧ f) : True := by
casesm* _∨_, _∧_
· clear ‹a› ‹b› ‹e› ‹f›; (fail_if_success clear ‹c›); trivial
· clear ‹c› ‹d› ‹e› ‹f›; trivial
example (h : a ∧ b ∨ c ∧ d) : True := by
fail_if_success casesm* _∧_ -- no match expected
clear ‹a ∧ b ∨ c ∧ d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
casesm* _∨_
· clear ‹a ∧ b›; trivial
· clear ‹c›; trivial
· clear ‹d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
casesm _∨_
· clear ‹a ∧ b›; trivial
· clear ‹c ∨ d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
cases_type And Or
· clear ‹a ∧ b›; trivial
· clear ‹c ∨ d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
fail_if_success cases_type* And -- no match expected
· clear ‹a ∧ b ∨ c ∨ d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
cases_type Or
· clear ‹a ∧ b›; trivial
· clear ‹c ∨ d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
cases_type* Or
· clear ‹a ∧ b›; trivial
· clear ‹c›; trivial
· clear ‹d›; trivial
example (h : a ∧ b ∨ c ∨ d) : True := by
fail_if_success cases_type!* And Or -- no match expected
· clear ‹a ∧ b ∨ c ∨ d›; trivial
example (h : a ∧ b ∧ (c ∨ d)) : True := by
cases_type! And Or
· clear ‹a› ‹b ∧ (c ∨ d)›; trivial
example (h : a ∧ b ∧ (c ∨ d)) : True := by
cases_type!* And Or
· clear ‹a› ‹b› ‹c ∨ d›; trivial
inductive Test : Nat → Prop
| foo : Test 0
| bar : False → Test (n + 1)
example (_ : Test n) (h2 : Test (m + 1)) : True := by
cases_type!* Test
· clear ‹Test n› ‹False›; trivial
example (_ : Test n) (h2 : Test (m + 1)) : True := by
cases_type Test
· clear ‹Test (m + 1)›; trivial
· clear ‹False› ‹Test (m + 1)›; trivial
example (_ : Test n) (h2 : Test (m + 1)) : True := by
cases_type* Test
· clear ‹False›; trivial
· clear ‹False›; clear ‹False›; trivial
example : True ∧ True ∧ True := by
fail_if_success constructorm* True, _∨_ -- no match expected
guard_target = True ∧ True ∧ True
constructorm _∧_
· guard_target = True; constructorm True
· guard_target = True ∧ True; constructorm* True, _∧_
example (n : Nat) : True := by
fail_if_success casesm! Nat -- two constructors, so `casesm!` doesn't fire
trivial
example (h : Array Nat) : True := by
casesm! Array _
trivial
example (h : Array Nat) : True := by
casesm Array _
-- user facing name is preserved:
guard_hyp h : List Nat
trivial
example (n : Nat) (h : n = 0) : True := by
casesm Nat
· trivial
· -- user facing name is preserved:
guard_hyp h : n + 1 = 0
trivial
example (h : P ∧ Q) : True := by
casesm _ ∧ _
-- user facing name is not used here, because there are multiple new hypotheses.
fail_if_success guard_hyp h : P
rename_i p q
guard_hyp p : P
guard_hyp q : Q
trivial
section AuxDecl
variable {p q r : Prop}
variable (h : p ∧ q ∨ p ∧ r)
include h
-- Make sure that we don't try to work on auxiliary declarations.
-- In this case, there will be an auxiliary recursive declaration for
-- `foo` itself that `casesm (_ ∧ _)` could potentially match.
theorem foo : p ∧ p := by
cases h
· casesm (_ ∧ _)
constructor <;> assumption
· casesm (_ ∧ _)
constructor <;> assumption
end AuxDecl |
.lake/packages/mathlib/MathlibTest/trace.lean | import Mathlib.Tactic.Trace
set_option linter.unusedTactic false
/--
info: 7
-/
#guard_msgs in
example : True := by
trace 2 + 2 + 3
trivial
/--
info: hello world
-/
#guard_msgs in
example : True := by
trace "hello" ++ " world"
trivial |
.lake/packages/mathlib/MathlibTest/CommandStart.lean | import Aesop.Frontend.Attribute
import Mathlib.Tactic.Linter.CommandStart
import Mathlib.Tactic.Lemma
set_option linter.style.commandStart true
/--
warning: missing space in the source
This part of the code
'example: True'
should be written as
'example : True'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example: True := trivial
-- Constructs that are ignored by the linter, and (former) false positives.
section noFalsePositives
-- Explicit name literals: used to error (and the suggested replacement is invalid syntax).
structure foo (name: Lean.Name) where
#guard_msgs in
def bar (_param : List (foo ``String)) := 1
-- This example would trigger the linter if we did not special case
-- `where` in `Mathlib.Linter.Style.CommandStart.getUnlintedRanges`.
/-- A -/
example := aux
where
/-- A -/
aux : Unit := ()
-- For structure fields, all field definitions are linted.
-- TODO: currently, only the first field is linted
/--
warning: extra space in the source
This part of the code
'field1 : Nat'
should be written as
'field1 : Nat'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
structure A where
field1 : Nat
field2 : Nat
-- TODO: this is not linted yet!
structure B where
field1 : Nat
field2 : Nat
-- TODO: this is not linted yet!
structure C where
field1 : Nat
field2 : Nat
-- Note that the linter does not attempt to recognise or respect manual alignment of fields:
-- this is often brittle and should usually be removed.
/--
warning: extra space in the source
This part of the code
'field1 : '
should be written as
'field1 : Nat'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
structure D where
field1 : Nat
field2 : Nat
-- This also applies to consecutive declarations.
/--
warning: declaration uses 'sorry'
---
warning: extra space in the source
This part of the code
'instance {R}'
should be written as
'instance {R} :'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
instance {R} : Add R := sorry
/--
warning: declaration uses 'sorry'
---
warning: extra space in the source
This part of the code
'instance {R}'
should be written as
'instance {R} :'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
instance {R} : Add R := sorry
-- Strings are ignored by the linter.
variable (a : String := " ")
-- The linter skips double-quoted names.
variable (d : Lean.Name := ``Nat) in open Nat
-- Code inside `run_cmd` is not checked at all.
run_cmd
for _ in [0] do
let _ ← `(
end)
def Card : Type → Nat := fun _ => 0
/-- Symbols for use by all kinds of grammars. -/
inductive Symbol (T N : Type)
/-- Terminal symbols (of the same type as the language) -/
| terminal (t : T) : Symbol T N
/-- Nonterminal symbols (must not be present when the word being generated is finalized) -/
| nonterminal (n : N) : Symbol T N
deriving
DecidableEq, Repr
-- embedded comments do not cause problems!
#guard_msgs in
open Nat in -- hi
example : True := trivial
-- embedded comments do not cause problems!
#guard_msgs in
open Nat in
-- hi
example : True := trivial
-- embedded comments do not cause problems!
#guard_msgs in
open Nat in
-- hi
example : True := trivial
structure X where
/-- A doc -/
x : Nat
open Nat in /- hi -/
example : True := trivial
-- The notation `0::[]` disables the linter
variable (h : 0::[] = [])
/-- A doc string -/
-- comment
example : True := trivial
-- Test that `Prop` and `Type` that are not escaped with `«...»` do not cause problems.
def Prop.Hello := 0
def Type.Hello := 0
/--
warning: extra space in the source
This part of the code
'F : True'
should be written as
'F : True'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
lemma F : True := trivial
namespace List
variable {α β : Type} (r : α → α → Prop) (s : β → β → Prop)
-- The two infix are needed. They "hide" a `quotPrecheck`
local infixl:50 " ≼ " => r
-- The two infix are needed. They "hide" a `quotPrecheck`
local infixl:50 " ≼ " => s
/--
warning: The `commandStart` linter had some parsing issues: feel free to silence it and report this error!
Note: This linter can be disabled with `set_option linter.style.commandStart.verbose false`
-/
#guard_msgs in
set_option linter.style.commandStart.verbose true in
example {a : α} (_ : a ≼ a) : 0 = 0 := rfl
end List
end noFalsePositives
-- Miscellaneous constructs: variable, include, omit statements; aesop rulesets
section misc
variable [h : Add Nat] [Add Nat]
/--
warning: extra space in the source
This part of the code
'variable [ h'
should be written as
'variable [h :'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
---
warning: extra space in the source
This part of the code
'[ h '
should be written as
'[h : Add'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
---
warning: extra space in the source
This part of the code
'h : Add'
should be written as
'[h : Add'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
---
warning: extra space in the source
This part of the code
'Nat ] ['
should be written as
'Nat] [Add'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
---
warning: extra space in the source
This part of the code
'[ Add'
should be written as
'[Add Nat]'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
variable [ h : Add Nat ] [ Add Nat]
/--
warning: extra space in the source
This part of the code
'omit [h :'
should be written as
'omit [h :'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
---
warning: extra space in the source
This part of the code
'Nat] [Add'
should be written as
' [Add'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
omit [h : Add Nat] [Add Nat]
-- Include statements are not linted.
include h
/--
warning: extra space in the source
This part of the code
'@[aesop (rule_sets'
should be written as
'@[aesop (rule_sets'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
@[aesop (rule_sets := [builtin]) safe apply] example : True := trivial
end misc
/--
warning: 'section' starts on column 1, but all commands should start at the beginning of the line.
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
section
/--
warning: extra space in the source
This part of the code
'example : True'
should be written as
'example : True'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example : True := trivial
-- Additional spaces after the colon are not linted yet.
#guard_msgs in
example : True := trivial
/--
warning: extra space in the source
This part of the code
'example : True'
should be written as
'example : True'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example : True :=trivial
/--
warning: missing space in the source
This part of the code
'(a: Nat)'
should be written as
'(a : Nat)'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
variable (a: Nat)
/--
warning: missing space in the source
This part of the code
'(_a: Nat)'
should be written as
'(_a : Nat)'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example (_a: Nat) : True := trivial
/--
warning: missing space in the source
This part of the code
'{a: Nat}'
should be written as
'{a : Nat}'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example {a: Nat} : a = a := rfl
/--
a
b
c
d -/
example (_a : Nat) (_b : Int) : True := trivial
/--
warning: missing space in the source
This part of the code
':Nat}'
should be written as
': Nat}'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example {a :Nat} : a = a := rfl
/--
warning: extra space in the source
This part of the code
'example {a :Nat}'
should be written as
'example {a :'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
---
warning: missing space in the source
This part of the code
':Nat}'
should be written as
': Nat}'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example {a :Nat} : a = a := rfl
/--
warning: unused variable `b`
Note: This linter can be disabled with `set_option linter.unusedVariables false`
---
warning: missing space in the source
This part of the code
'Nat}{b :'
should be written as
'Nat} {b :'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example {a : Nat}{b : Nat} : a = a := rfl
/--
warning: extra space in the source
This part of the code
'Nat} : a'
should be written as
'Nat} : a ='
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example {a : Nat} : a = a := rfl
/--
warning: extra space in the source
This part of the code
'alpha ] {a'
should be written as
'alpha] {a'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
example {alpha} [Neg alpha ] {a : Nat} : a = a := rfl
/--
warning: extra space in the source
This part of the code
'example : True'
should be written as
'example : True'
Note: This linter can be disabled with `set_option linter.style.commandStart false`
-/
#guard_msgs in
/-- Check that doc/strings do not get removed as comments. -/
example : True := trivial
-- Unit tests for internal functions in the linter.
section internal
/--
info: #[srcNat: 12, srcPos: 12, fmtPos: 2, msg: extra space, length: 7
, srcNat: 4, srcPos: 4, fmtPos: 1, msg: extra space, length: 3
]
-/
#guard_msgs in
#eval
let s := "example f g"
let t := "example fg"
Mathlib.Linter.parallelScan s t
/--
info: #[srcNat: 19, srcPos: 19, fmtPos: 21, msg: extra space, length: 1
, srcNat: 16, srcPos: 16, fmtPos: 19, msg: extra space, length: 2
, srcNat: 7, srcPos: 7, fmtPos: 12, msg: missing space, length: 1
]
-/
#guard_msgs in
#eval
let s := "example : True :=trivial"
let t := "example : True :=
trivial"
Mathlib.Linter.parallelScan s t
/--
info: #[srcNat: 4, srcPos: 4, fmtPos: 5, msg: missing space, length: 1
, srcNat: 2, srcPos: 2, fmtPos: 1, msg: extra space, length: 1
]
-/
#guard_msgs in
#eval
let l := "hac d"
let m := "h acd"
Mathlib.Linter.parallelScan l m
-- Starting from `c` (due to the `"d ef gh".length` input), form a "window" of successive sizes
-- `1, 2,..., 6`. The output is trimmed and contains only full words, even partially overlapping
-- with the given lengths.
#guard Mathlib.Linter.Style.CommandStart.mkWindow "ab cd ef gh" "d ef gh".length 1 == "cd"
#guard Mathlib.Linter.Style.CommandStart.mkWindow "ab cd ef gh" "d ef gh".length 2 == "cd"
#guard Mathlib.Linter.Style.CommandStart.mkWindow "ab cd ef gh" "d ef gh".length 3 == "cd ef"
#guard Mathlib.Linter.Style.CommandStart.mkWindow "ab cd ef gh" "d ef gh".length 4 == "cd ef"
#guard Mathlib.Linter.Style.CommandStart.mkWindow "ab cd ef gh" "d ef gh".length 5 == "cd ef"
#guard Mathlib.Linter.Style.CommandStart.mkWindow "ab cd ef gh" "d ef gh".length 6 == "cd ef gh"
end internal |
.lake/packages/mathlib/MathlibTest/GRewrite.lean | import Mathlib.Data.Int.ModEq
import Mathlib.Order.Antisymmetrization
import Mathlib.Tactic.GRewrite
import Mathlib.Tactic.GCongr
import Mathlib.Tactic.NormNum
import Mathlib.Algebra.Order.Ring.Abs
import Mathlib.Algebra.Order.BigOperators.Ring.Finset
/- In many examples in this module, we rewrite expressions which do not make it into the final term.
This only happens because we are writing tests where we resolve the goal with a universal axiom, it
would not happen in real proofs, so we disable the resulting linter warnings.
-/
set_option linter.unusedVariables false
private axiom test_sorry : ∀ {α}, α
variable {α : Type*} [CommRing α] [LinearOrder α] [IsStrictOrderedRing α] (a b c d e : α)
section inequalities
example (h₁ : a ≤ b) (h₂ : b ≤ c) : a + 5 ≤ c + 6 := by
grw [h₁, h₂]
guard_target =ₛ c + 5 ≤ c + 6
grw [show (5 : α) < 6 by norm_num]
example (h₁ : a ≤ b) (h₂ : b ≤ c) : c + 6 > a + 5 := by
grw [h₁, h₂]
guard_target =ₛ c + 6 > c + 5
exact test_sorry
example (h₁ : c ≤ b) : a + 5 ≤ b + 6 := by
grw [← h₁]
guard_target =ₛ a + 5 ≤ c + 6
exact test_sorry
example (h₁ : c ≤ b) (h₂ : a + 5 < c + 6) : a + 5 < b + 6 := by
grw [← h₁]
guard_target =ₛ a + 5 < c + 6
exact h₂
example (h₁ : a + e ≤ b + e) (h₂ : b < c) (h₃ : c ≤ d) : a + e ≤ d + e := by
grw [h₂, h₃] at h₁
guard_hyp h₁ :ₛ a + e ≤ d + e
exact h₁
example (f g : α → α) (h : ∀ x : α, f x ≤ g x) (h₂ : g a + g b ≤ 5) : f a + f b ≤ 5 := by
grw [h]
guard_target =ₛ g a + f b ≤ 5
grw [h]
guard_target =ₛ g a + g b ≤ 5
grw [h₂]
example (f g : α → α) (h : ∀ x : α, f x < g x) : f a ≤ g b := by
grw [h, ← h b]
guard_target =ₛ g a ≤ f b
exact test_sorry
example (h₁ : a ≥ b) (h₂ : 0 ≤ c) : a * c ≥ 100 - a := by
grw [h₁]
guard_target =ₛ b * c ≥ 100 - b
exact test_sorry
example {n : ℕ} (bound : n ≤ 5) : n ≤ 10 := by
have h' : 5 ≤ 10 := by decide
grw [h'] at bound
assumption
example (h₁ : a ≤ b) : a + 5 ≤ b + 6 := by grw [h₁, show (5 : α) ≤ 6 by norm_num]
example (h₁ : a ≤ b) : a * 5 ≤ b * 5 := by grw [h₁]
example (h₁ : a ≤ b) (h₂ : c ≥ 0) : a * c ≤ b * c := by grw [h₁]
example (h₁ : a ≤ b) : a * c ≤ b * c := by
grw [h₁]
guard_target =ₛ 0 ≤ c
exact test_sorry
/- This example has behaviour which might be weaker than some users would desire: it would be
mathematically sound to transform the goal here to `2 * y ≤ z`, not `2 * y < z`.
However, the current behavior is easier to implement, and preserves the form of the goal (`?_ < z`),
which is a useful invariant. -/
example {x y z : ℤ} (hx : x < y) : 2 * x < z := by
grw [hx]
guard_target =ₛ 2 * y < z
exact test_sorry
end inequalities
section subsets
variable (X Y Z W : Set α)
example (h₁ : X ⊆ Y) (h₂ : Y ⊆ Z) (h₃ : a ∈ X) : False := by
grw [h₁] at h₃
guard_hyp h₃ :ₛ a ∈ Y
grw [h₂] at h₃
guard_hyp h₃ :ₛ a ∈ Z
exact test_sorry
example (h₁ : Y ⊇ W) : X ⊂ (Y ∪ Z) := by
grw [h₁]
guard_target =ₛ X ⊂ (W ∪ Z)
exact test_sorry
example (h₁ : W ⊂ Y) (h₂ : X ⊂ (W ∪ Z)) : X ⊂ (Y ∪ Z) := by
grw [← h₁]
guard_target =ₛ X ⊂ (W ∪ Z)
exact h₂
example {a b : Nat} (h : a < b) (f : Nat → Nat) (hf : ∀ i, 0 ≤ f i) :
∑ i ∈ ({x | x ≤ a} : Set Nat), f i ≤ ∑ i ∈ ({x | x ≤ b} : Set Nat), f i := by
grw [h]
end subsets
section rationals
example (x x' y z w : ℚ) (h0 : x' = x) (h₁ : x < z) (h₂ : w ≤ y + 4) (h₃ : z + 1 < 5 * w) :
x' + 1 < 5 * (y + 4) := by
grw [h0, h₁, ← h₂]
exact h₃
example {x y z : ℚ} (f g : ℚ → ℚ) (h : ∀ t, f t = g t) : 2 * f x * f y * f x ≤ z := by
grw [h]
guard_target =ₛ 2 * g x * f y * g x ≤ z
exact test_sorry
example {x y a b : ℚ} (h : x ≤ y) (h1 : a ≤ 3 * x) : 2 * x ≤ b := by
grw [h] at h1
guard_hyp h1 :ₛ a ≤ 3 * y
exact test_sorry
end rationals
section modeq
example {n : ℤ} (hn : n ≡ 1 [ZMOD 3]) : n ^ 3 + 7 * n ≡ 2 [ZMOD 3] := by grw [hn]; decide
example {a b : ℤ} (h1 : a ≡ 3 [ZMOD 5]) (h2 : b ≡ a ^ 2 + 1 [ZMOD 5]) :
a ^ 2 + b ^ 2 ≡ 4 [ZMOD 5] := by
grw [h1] at h2 ⊢
guard_hyp h2 :ₛ b ≡ 3 ^ 2 + 1 [ZMOD 5]
guard_target =ₛ 3 ^ 2 + b ^ 2 ≡ 4 [ZMOD 5]
grw [h2]
decide
end modeq
section dvd
example {a b c : ℤ} (h₁ : a ∣ b) (h₂ : b ∣ a ^ 2 * c) : a ∣ b ^ 2 * c := by
grw [h₁] at *
exact h₂
end dvd
section wildcard
/-! Rewriting at a wildcard `*`, i.e. `grw [h] at *`, will sometimes include a rewrite at `h` itself
and sometimes not, according to whether the generalized rewrite is valid or not; this is the case
approximately (not exactly) when the relation in the type of `h` is an equivalence relation. See
examples below of it occurring for `≡ [ZMOD 5]` and not occurring for `<`.
Having `grw [h] at *` rewrite at `h` itself is a bit weird, but is consistent with the behaviour of
`rw` (for the equivalence relation `=`).
-/
example {a b : ℤ} (h1 : a ≡ 3 [ZMOD 5]) (h2 : b ≡ a ^ 2 + 1 [ZMOD 5]) :
a ^ 2 + b ^ 2 ≡ 4 [ZMOD 5] := by
grw [h1] at *
guard_hyp h1 :ₛ 3 ≡ 3 [ZMOD 5] -- `grw [h1] at *` rewrites at `h1`
guard_hyp h2 :ₛ b ≡ 3 ^ 2 + 1 [ZMOD 5]
guard_target =ₛ 3 ^ 2 + b ^ 2 ≡ 4 [ZMOD 5]
grw [h2]
decide
example {x y a b : ℚ} (h : x < y) (h1 : a ≤ 3 * x) : 2 * x ≤ b := by
grw [h] at *
guard_hyp h :ₛ x < y -- `grw [h] at *` does not rewrite at `h`
guard_hyp h1 :ₛ a ≤ 3 * y
guard_target =ₛ 2 * y ≤ b
exact test_sorry
end wildcard
section nontransitive_grw_lemmas
example {s s' t : Set ℕ} (h : s' ⊆ s) (h2 : BddAbove (s ∩ t)) : BddAbove (s' ∩ t) := by
grw [h]; exact h2
example {s s' : Set ℕ} (h : s' ⊆ s) (h2 : BddAbove s) : BddAbove s' := by
grw [h]; exact h2
example {s s' t : Set α} (h : s ⊆ s') : (s' ∩ t).Nonempty := by
grw [← h]
guard_target =ₛ (s ∩ t).Nonempty
exact test_sorry
end nontransitive_grw_lemmas
section
/-! In the examples in this section, the proposed rewrite is not valid because the constructed
relation does not have its main goals proved by `gcongr` (in the two examples here this is because
the inequality goes in the wrong direction). -/
/--
error: Tactic `grewrite` failed: could not discharge x ≤ y using x ≥ y
case h₁.hbc
α : Type u_1
inst✝² : CommRing α
inst✝¹ : LinearOrder α
inst✝ : IsStrictOrderedRing α
a b✝ c d e : α
x y b : ℚ
h : x ≥ y
⊢ x ≤ y
-/
#guard_msgs in
example {x y b : ℚ} (h : x ≥ y) : 2 * x ≤ b := by
grw [h]
example {s s' t : Set α} (h : s' ⊆ s) : (s' ∩ t).Nonempty := by
fail_if_success grw [h]
exact test_sorry
end
example {x y a b : ℤ} (h1 : |x| ≤ a) (h2 : |y| ≤ b) :
|x ^ 2 + 2 * x * y| ≤ a ^ 2 + 2 * a * b := by
have : 0 ≤ a := by grw [← h1]; positivity
grw [abs_add_le, abs_mul, abs_mul, abs_pow, h1, h2, abs_of_nonneg]
norm_num
example {a b : ℚ} {P : Prop} (hP : P) (h : P → a < b) : False := by
have : 2 * a ≤ 2 * b := by grw [h]; exact hP
exact test_sorry
example {a b : ℚ} {P Q : Prop} (hP : P) (hQ : Q) (h : P → Q → a < b) : False := by
have : 2 * a ≤ 2 * b := by grw [h ?_ hQ]; exact hP
exact test_sorry
example {a a' : ℕ} {X : Set ℕ} (h₁ : a + 1 ∈ X) (h₂ : a = a') : False := by
grw [h₂] at h₁
guard_hyp h₁ :ₛ a' + 1 ∈ X
exact test_sorry
example {Prime : ℕ → Prop} {a a' : ℕ} (h₁ : Prime (a + 1)) (h₂ : a = a') : False := by
grw [h₂] at h₁
guard_hyp h₁ :ₛ Prime (a' + 1)
exact test_sorry
/--
error: Tactic `grewrite` failed: could not discharge b ≤ a using a ≤ b
case h₂.hbc
α : Type u_1
inst✝² : CommRing α
inst✝¹ : LinearOrder α
inst✝ : IsStrictOrderedRing α
a b c d e : α
h₁ : a ≤ b
h₂ : 0 ≤ c
⊢ b ≤ a
-/
#guard_msgs in
example (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≥ 100 + a := by
grw [h₁]
example (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≥ 100 + a + a := by
nth_grw 2 3 [h₁]
guard_target =ₛ a * c ≥ 100 + b + b
exact test_sorry
section apply
variable {p q r s : Prop}
example (n : Nat) (h : p → n=1) (h' : p) : n = 1 := by
apply_rw [← h]
exact h'
example (n : Nat) (h : p → n=1) (h' : r → s) : p ∧ r → n = 1 ∧ s := by
apply_rw [h, h']
exact id
example (n : Nat) (h : p → n=1) (h' : r → s) : p ∧ r → n = 1 ∧ s := by
grw [h]
apply_rw [h']
gcongr
intro _
rfl
exact test_sorry
example (h : p → q) (h' : q → r) : p → r := by
apply_rw [← h] at h'
exact h'
end apply
-- previously, `grw` failed to rewrite in expressions with syntheticOpaque metavariables
example : ∃ n, n < 2 := by
refine ⟨?_, ?_⟩
on_goal 2 => grw [← one_lt_two]
exact 0
refine zero_lt_one
variable {a b c d n : ℤ}
example (h : a ≡ b [ZMOD n]) : a ^ 2 ≡ b ^ 2 [ZMOD n] := by
grw [h]
example (h₁ : a ∣ b) (h₂ : b ∣ a * d) : a ∣ b * d := by
grw [h₁] at h₂ ⊢
exact h₂
namespace AntiSymmRelTest
variable {α : Type u} [Preorder α] {a b : α}
local infix:50 " ≈ " => AntisymmRel (· ≤ ·)
axiom f : α → α
@[gcongr]
axiom f_congr' : a ≤ b → f a ≤ f b
example (h : a ≈ b) : f a ≤ f b := by
grw [h]
example (h : b ≈ a) : f a ≤ f b := by
grw [h]
end AntiSymmRelTest
-- Test that `grw` works even in the presence of metadata.
example (a b : Nat) (h : Nat → no_index (a ≤ b)) : a ≤ b := by
grw [h]
exact 0
example (a b : Nat) (h : Nat → no_index (a ≤ b)) : a ≤ b := by
grw [h 0] |
.lake/packages/mathlib/MathlibTest/convert.lean | import Mathlib.Tactic.Convert
import Mathlib.Algebra.Group.Basic
import Mathlib.Data.Set.Image
private axiom test_sorry : ∀ {α}, α
set_option autoImplicit true
namespace Tests
example (P : Prop) (h : P) : P := by convert h
example (α β : Type) (h : α = β) (b : β) : α := by
convert b
example (α β : Type) (h : ∀ α β : Type, α = β) (b : β) : α := by
convert b
apply h
example (m n : Nat) (h : m = n) (b : Fin n) : Nat × Nat × Nat × Fin m := by
convert (37, 57, 2, b)
example (α β : Type) (h : α = β) (b : β) : Nat × α := by
-- type eq ok since arguments to `Prod` are explicit
convert (37, b)
example (α β : Type) (h : β = α) (b : β) : Nat × α := by
convert ← (37, b)
example (α β : Type) (h : α = β) (b : β) : Nat × Nat × Nat × α := by
convert (37, 57, 2, b)
example (α β : Type) (h : α = β) (b : β) : Nat × Nat × Nat × α := by
convert (37, 57, 2, b) using 2
guard_target = (Nat × α) = (Nat × β)
congr!
example {f : β → α} {x y : α} (h : x ≠ y) : f ⁻¹' {x} ∩ f ⁻¹' {y} = ∅ := by
have : {x} ∩ {y} = (∅ : Set α) := by simpa [ne_comm] using h
convert Set.preimage_empty
rw [← Set.preimage_inter, this]
section convert_to
example {α} [AddCommMonoid α] {a b c d : α} (H : a = c) (H' : b = d) : a + b = d + c := by
convert_to c + d = _ using 2
rw [add_comm]
example {α} [AddCommMonoid α] {a b c d : α} (H : a = c) (H' : b = d) : a + b = d + c := by
convert_to c + d = _ -- defaults to `using 1`
congr 2
rw [add_comm]
-- Check that `using 1` gives the same behavior as the default.
example {α} [AddCommMonoid α] {a b c d : α} (H : a = c) (H' : b = d) : a + b = d + c := by
convert_to c + d = _ using 1
congr 2
rw [add_comm]
end convert_to
example (prime : Nat → Prop) (n : Nat) (h : prime (2 * n + 1)) :
prime (n + n + 1) := by
convert h
· guard_target = (HAdd.hAdd : Nat → Nat → Nat) = HMul.hMul
exact test_sorry
· guard_target = n = 2
exact test_sorry
example (prime : Nat → Prop) (n : Nat) (h : prime (2 * n + 1)) :
prime (n + n + 1) := by
convert (config := .unfoldSameFun) h
guard_target = n + n = 2 * n
exact test_sorry
example (p q : Nat → Prop) (h : ∀ ε > 0, p ε) :
∀ ε > 0, q ε := by
convert h using 2 with ε hε
guard_hyp hε : ε > 0
guard_target = q ε ↔ p ε
exact test_sorry
class Fintype (α : Type _) where
card : Nat
axiom Fintype.foo (α : Type _) [Fintype α] : Fintype.card α = 2
axiom Fintype.foo' (α : Type _) [Fintype α] [Fintype (Option α)] : Fintype.card α = 2
axiom instFintypeBool : Fintype Bool
/- Would be "failed to synthesize instance Fintype ?m" without allowing TC failure. -/
example : @Fintype.card Bool instFintypeBool = 2 := by
convert Fintype.foo _
example : @Fintype.card Bool instFintypeBool = 2 := by
convert Fintype.foo' _ using 1
guard_target = Fintype (Option Bool)
exact test_sorry
example : True := by
convert_to ?x + ?y = ?z
case x => exact 1
case y => exact 2
case z => exact 3
all_goals try infer_instance
· simp
· simp
-- This test does not work unless we specify that `α` and `β` lie in the same universe.
-- Prior to https://github.com/leanprover/lean4/pull/4493 it did,
-- because previously bodies of `example`s were (confusingly!) allowed to
-- affect the elaboration of the signature!
set_option linter.unusedTactic false in
example {α β : Type u} [Fintype α] [Fintype β] : Fintype.card α = Fintype.card β := by
congr!
guard_target = Fintype.card α = Fintype.card β
congr! +typeEqs
· guard_target = α = β
exact test_sorry
· rename_i inst1 inst2 h
guard_target = inst1 ≍ inst2
have : Subsingleton (Fintype α) := test_sorry
congr!
example (x y z : Nat) (h : x + y = z) : y + x = z := by
convert_to y + x = _ at h
· rw [Nat.add_comm]
exact h
end Tests |
.lake/packages/mathlib/MathlibTest/StringDiagram.lean | import Mathlib.Tactic.Widget.StringDiagram
import ProofWidgets.Component.Panel.SelectionPanel
/-! ## Example use of string diagram widgets -/
open ProofWidgets Mathlib.Tactic.Widget
section MonoidalCategory
open CategoryTheory
open scoped MonoidalCategory
universe v u
variable {C : Type u} [Category.{v} C] [MonoidalCategory C]
section
lemma left_triangle {X Y : C} (η : 𝟙_ _ ⟶ X ⊗ Y) (ε : Y ⊗ X ⟶ 𝟙_ _) (w : False) :
η ▷ X ≫ (α_ _ _ _).hom ≫ X ◁ ε = (λ_ _).hom ≫ (ρ_ _).inv := by
/- Displays string diagrams for the both sides of the goal. -/
with_panel_widgets [StringDiagram]
/- Place the cursor here to see the string diagrams. -/
/- You can also see the string diagram of any 2-morphism in the goal or hyperthesis. -/
with_panel_widgets [SelectionPanel]
/- Place the cursor here and shift-click the 2-morphisms in the tactic state. -/
exact w.elim
/- Instead of writing `with_panel_widgets` everywhere, you can also use this command. -/
show_panel_widgets [local StringDiagram, local SelectionPanel]
lemma yang_baxter {V₁ V₂ V₃ : C} (R : ∀ V₁ V₂ : C, V₁ ⊗ V₂ ⟶ V₂ ⊗ V₁) (w : False) :
R V₁ V₂ ▷ V₃ ≫ (α_ _ ..).hom ≫ _ ◁ R _ _ ≫ (α_ _ ..).inv ≫ R _ _ ▷ _ ≫ (α_ _ ..).hom =
(α_ _ ..).hom ≫ V₁ ◁ R V₂ V₃ ≫ (α_ _ ..).inv ≫ R _ _ ▷ _ ≫ (α_ _ ..).hom ≫ _ ◁ R _ _ := by
/- Place the cursor here to see the string diagrams. -/
exact w.elim
lemma yang_baxter' {V₁ V₂ V₃ : C} (R : ∀ V₁ V₂ : C, V₁ ⊗ V₂ ⟶ V₂ ⊗ V₁) (w : False) :
R V₁ V₂ ▷ V₃ ⊗≫ V₂ ◁ R V₁ V₃ ⊗≫ R V₂ V₃ ▷ V₁ ⊗≫ 𝟙 _ =
𝟙 _ ⊗≫ V₁ ◁ R V₂ V₃ ⊗≫ R V₁ V₃ ▷ V₂ ⊗≫ V₃ ◁ R V₁ V₂ := by
exact w.elim
lemma yang_baxter'' {V₁ V₂ V₃ : C} (R : ∀ V₁ V₂ : C, V₁ ⊗ V₂ ⟶ V₂ ⊗ V₁) (w : False) :
(R V₁ V₂ ⊗ₘ 𝟙 V₃) ≫ (α_ _ ..).hom ≫
(𝟙 V₂ ⊗ₘ R V₁ V₃) ≫ (α_ _ ..).inv ≫
(R V₂ V₃ ⊗ₘ 𝟙 V₁) ≫ (α_ _ ..).hom =
(α_ _ ..).hom ≫ (𝟙 V₁ ⊗ₘ R V₂ V₃) ≫
(α_ _ ..).inv ≫ (R V₁ V₃ ⊗ₘ 𝟙 V₂) ≫
(α_ _ ..).hom ≫ (𝟙 V₃ ⊗ₘ R V₁ V₂) := by
exact w.elim
example {X Y : C} (f : X ⟶ Y) (g : X ⊗ X ⊗ Y ⟶ Y ⊗ X ⊗ Y) (w : False) : f ▷ (X ⊗ Y) = g := by
exact w.elim
example {X Y : C} (f : X ⟶ Y) (g : 𝟙_ C ⊗ X ⟶ 𝟙_ C ⊗ Y) (w : False) : 𝟙_ C ◁ f = g := by
exact w.elim
example {X₁ Y₁ X₂ Y₂ : C} (f : X₁ ⟶ Y₁) (g : X₂ ⟶ Y₂) : f ⊗ₘ g = X₁ ◁ g ≫ f ▷ Y₂ := by
rw [MonoidalCategory.whisker_exchange]
rw [MonoidalCategory.tensorHom_def]
end
set_option trace.string_diagram true
variable {C : Type u} [Category.{v} C] [i : MonoidalCategory C] {X Y : C}
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_1_1)
[string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_1_1)
-/
#guard_msgs (whitespace := lax) in
#string_diagram MonoidalCategory.whisker_exchange
/-- trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_0_0)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
[string_diagram] Penrose substance: Left(E_0_0_0, E_0_1_1)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_0_0)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
-/
#guard_msgs (whitespace := lax) in
#string_diagram MonoidalCategory.whisker_exchange_assoc
/--
trace: [string_diagram] Penrose substance:
[string_diagram] Penrose substance:
-/
#guard_msgs (whitespace := lax) in
#string_diagram MonoidalCategory.pentagon
/--
trace: [string_diagram] Penrose substance:
[string_diagram] Penrose substance:
-/
#guard_msgs (whitespace := lax) in
#string_diagram MonoidalCategory.whiskerLeft_id
/--
trace: [string_diagram] Penrose substance:
Left(E_1_0_0, E_1_0_2)
Left(E_2_0_0, E_2_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_2)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_1 := MakeString (E_1_0_0, E_2_1_1)
Mor1 f_1_4 := MakeString (E_1_0_2, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
[string_diagram] Penrose substance:
-/
#guard_msgs (whitespace := lax) in
#string_diagram left_triangle
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_2_2)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_2_2)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_0_0)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_2_2)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_1 := MakeString (E_1_0_0, E_2_1_1)
Mor1 f_1_4 := MakeString (E_1_2_2, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_0_0)
Mor1 f_2_3 := MakeString (E_2_1_1, E_3_2_2)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_1 := MakeString (E_3_0_0, E_4_1_1)
Mor1 f_3_4 := MakeString (E_3_2_2, E_4_2_2)
[string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_2_2)
Left(E_3_0_0, E_3_1_1)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_0_0)
Mor1 f_1_3 := MakeString (E_1_1_1, E_2_2_2)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_1 := MakeString (E_2_0_0, E_3_1_1)
Mor1 f_2_4 := MakeString (E_2_2_2, E_3_1_1)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_2 := MakeString (E_3_1_1, E_4_1_1)
Mor1 f_3_3 := MakeString (E_3_1_1, E_4_2_2)
-/
#guard_msgs (whitespace := lax) in
#string_diagram yang_baxter
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_2_2)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_2_2)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_0_0)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_2_2)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_1 := MakeString (E_1_0_0, E_2_1_1)
Mor1 f_1_4 := MakeString (E_1_2_2, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_0_0)
Mor1 f_2_3 := MakeString (E_2_1_1, E_3_2_2)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_1 := MakeString (E_3_0_0, E_4_1_1)
Mor1 f_3_4 := MakeString (E_3_2_2, E_4_2_2)
[string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_2_2)
Left(E_3_0_0, E_3_1_1)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_0_0)
Mor1 f_1_3 := MakeString (E_1_1_1, E_2_2_2)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_1 := MakeString (E_2_0_0, E_3_1_1)
Mor1 f_2_4 := MakeString (E_2_2_2, E_3_1_1)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_2 := MakeString (E_3_1_1, E_4_1_1)
Mor1 f_3_3 := MakeString (E_3_1_1, E_4_2_2)
-/
#guard_msgs (whitespace := lax) in
#string_diagram yang_baxter'
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_2_2)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_2_2)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_0_0)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_2_2)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_1 := MakeString (E_1_0_0, E_2_1_1)
Mor1 f_1_4 := MakeString (E_1_2_2, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_0_0)
Mor1 f_2_3 := MakeString (E_2_1_1, E_3_2_2)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_1 := MakeString (E_3_0_0, E_4_1_1)
Mor1 f_3_4 := MakeString (E_3_2_2, E_4_2_2)
[string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_2_2)
Left(E_3_0_0, E_3_1_1)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_0_0)
Mor1 f_1_3 := MakeString (E_1_1_1, E_2_2_2)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_1 := MakeString (E_2_0_0, E_3_1_1)
Mor1 f_2_4 := MakeString (E_2_2_2, E_3_1_1)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_2 := MakeString (E_3_1_1, E_4_1_1)
Mor1 f_3_3 := MakeString (E_3_1_1, E_4_2_2)
-/
#guard_msgs (whitespace := lax) in
#string_diagram yang_baxter''
/--
trace: [string_diagram] Penrose substance:
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
[string_diagram] Penrose substance:
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
-/
#guard_msgs (whitespace := lax) in
#string_diagram Category.assoc
/--
trace: [string_diagram] Penrose substance:
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
[string_diagram] Penrose substance:
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
-/
#guard_msgs (whitespace := lax) in
#string_diagram Functor.map_comp
/--
trace: [string_diagram] Penrose substance:
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
[string_diagram] Penrose substance:
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
-/
#guard_msgs (whitespace := lax) in
#string_diagram NatTrans.naturality
variable (f : 𝟙_ _ ⟶ X ⊗ Y) in
/--
trace: [string_diagram] Penrose substance:
Left(E_2_0_0, E_2_1_1)
Above(E_1_0_0, E_2_0_0)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_1 := MakeString (E_1_0_0, E_2_1_1)
-/
#guard_msgs (whitespace := lax) in
#string_diagram f
variable (g : Y ⊗ X ⟶ 𝟙_ _) in
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Above(E_0_0_0, E_1_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_0_0)
-/
#guard_msgs (whitespace := lax) in
#string_diagram g
abbrev yangBaxterLhs {V₁ V₂ V₃ : C} (R : ∀ V₁ V₂ : C, V₁ ⊗ V₂ ⟶ V₂ ⊗ V₁) :=
R V₁ V₂ ▷ V₃ ≫ (α_ _ ..).hom ≫ _ ◁ R _ _ ≫ (α_ _ ..).inv ≫ R _ _ ▷ _ ≫ (α_ _ ..).hom
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_0_1_1, E_0_2_2)
Left(E_1_0_0, E_1_2_2)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_2_2)
Left(E_4_0_0, E_4_1_1)
Left(E_4_1_1, E_4_2_2)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Above(E_3_0_0, E_4_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_0_0)
Mor1 f_0_4 := MakeString (E_0_2_2, E_1_2_2)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_1 := MakeString (E_1_0_0, E_2_1_1)
Mor1 f_1_4 := MakeString (E_1_2_2, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_0_0)
Mor1 f_2_3 := MakeString (E_2_1_1, E_3_2_2)
Mor1 f_3_0 := MakeString (E_3_0_0, E_4_0_0)
Mor1 f_3_1 := MakeString (E_3_0_0, E_4_1_1)
Mor1 f_3_4 := MakeString (E_3_2_2, E_4_2_2)
-/
#guard_msgs (whitespace := lax) in
#string_diagram yangBaxterLhs
end MonoidalCategory
section Bicategory
open CategoryTheory
set_option trace.string_diagram true
/--
trace: [string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_1_1)
[string_diagram] Penrose substance:
Left(E_0_0_0, E_0_1_1)
Left(E_1_0_0, E_1_1_1)
Left(E_2_0_0, E_2_1_1)
Left(E_3_0_0, E_3_1_1)
Above(E_0_0_0, E_1_0_0)
Above(E_1_0_0, E_2_0_0)
Above(E_2_0_0, E_3_0_0)
Mor1 f_0_0 := MakeString (E_0_0_0, E_1_0_0)
Mor1 f_0_2 := MakeString (E_0_1_1, E_1_1_1)
Mor1 f_1_0 := MakeString (E_1_0_0, E_2_0_0)
Mor1 f_1_2 := MakeString (E_1_1_1, E_2_1_1)
Mor1 f_2_0 := MakeString (E_2_0_0, E_3_0_0)
Mor1 f_2_2 := MakeString (E_2_1_1, E_3_1_1)
-/
#guard_msgs (whitespace := lax) in
#string_diagram Bicategory.whisker_exchange
end Bicategory |
.lake/packages/mathlib/MathlibTest/CompileInductive.lean | import Mathlib.Util.CompileInductive
import Mathlib.Data.Fin.Fin2
compile_inductive% Fin2
example := @Nat.rec
example := @List.rec
example := @Fin2.rec
example := @PUnit.rec
example := @PEmpty.rec
example := @Nat.recOn
example := @List.recOn
example := @Fin2.recOn
example := @PUnit.recOn
example := @PEmpty.recOn
example := @And.recOn
example := @False.recOn
example := @Empty.recOn
example := @Nat.brecOn
example := @List.brecOn
example := @Fin2.brecOn
example := @List._sizeOf_1
open Lean Elab Term
def tryToCompileAllInductives : TermElabM Unit := do
let ivs := (← getEnv).constants.toList.filterMap fun | (_, .inductInfo iv) => some iv | _ => none
let mut success : Nat := 0
for iv in ivs do
try
withCurrHeartbeats <| Mathlib.Util.compileInductive iv
success := success + 1
catch | e => logError m!"[{iv.name}] {e.toMessageData}"
modifyThe Core.State fun s =>
{ s with messages.unreported := s.messages.unreported.filter (·.severity != .warning) }
modifyThe Core.State fun s => { s with messages := s.messages.errorsToWarnings }
logInfo m!"{success} / {ivs.length}"
-- #eval Command.liftTermElabM tryToCompileAllInductives |
.lake/packages/mathlib/MathlibTest/Complex.lean | import Mathlib.Data.Complex.Basic
import Mathlib.Algebra.DualNumber
open DualNumber
/--
info: Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I
-/
#guard_msgs in
#eval (0 : ℂ)
/--
info: Real.ofCauchy (sorry /- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I
-/
#guard_msgs in
#eval (1 : ℂ)
/--
info: Real.ofCauchy (sorry /- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I
-/
#guard_msgs in
#eval (4 : ℂ)
/--
info: Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ... -/)*I
-/
#guard_msgs in
#eval (Complex.I : ℂ)
/--
info: Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I + (Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/) + Real.ofCauchy (sorry /- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ... -/)*I)*ε
-/
#guard_msgs in
#eval (0 : ℂ[ε]) |
.lake/packages/mathlib/MathlibTest/peel.lean | import Mathlib.Tactic.Peel
import Mathlib.Topology.Instances.Real.Lemmas
open Filter Topology
/-! ## General usage -/
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel 1 h₁
rename_i y
guard_target =ₐ q y
exact h₂ _ this
example (p q : Nat → Prop) (h₁ : ∀ {x}, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel 1 h₁
rename_i y
guard_target =ₐ q y
exact h₂ _ this
example (p q : Nat → Nat → Prop) (h₁ : ∀ {x y}, p x y) (h₂ : ∀ x y, p x y → q x y) :
∀ u v, q u v := by
peel 2 h₁
rename_i u v
guard_target =ₐ q u v
exact h₂ _ _ this
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel h₁
rename_i y
guard_target =ₐ q y
exact h₂ _ this
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel h₁ using h₂ _ this
example (p q : Nat → Nat → Prop) (h₁ : ∀ x y, p x y) (h₂ : ∀ x y, p x y → q x y) :
∀ u v, q u v := by
peel h₁
rename_i u v
guard_target =ₐ q u v
exact h₂ _ _ this
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel h₁ with foo
rename_i y
guard_target =ₐ q y
exact h₂ _ foo
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel h₁ with w foo
guard_target =ₐ q w
exact h₂ w foo
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel h₁ with _ foo
rename_i w
guard_target =ₐ q w
exact h₂ w foo
example (p q : Nat → Prop) (h₁ : ∀ x, p x) (h₂ : ∀ x, p x → q x) : ∀ y, q y := by
peel h₁ with w _
guard_target =ₐ q w
exact h₂ w this
example (p q : Nat → Nat → Prop) (h₁ : ∀ x y, p x y) (h₂ : ∀ x y, p x y → q x y) :
∀ u v, q u v := by
peel h₁ with s t h_peel
guard_target =ₐ q s t
exact h₂ s t h_peel
example (p q : Nat → Prop) (h : ∀ y, p y) (h₁ : ∀ z, p z → q z) : ∀ x, q x := by
peel h
rename_i x
guard_target =ₐ q x
exact h₁ _ <| by assumption
example (p q : Nat → Prop) (h : ∃ y, p y) (h₁ : ∀ z, p z → q z) : ∃ x, q x := by
peel h with a h
guard_target =ₐ q a
exact h₁ a h
example (x y : ℝ) (h : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x + n = y + ε) :
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x - ε = y - n := by
peel h with ε hε N n hn h_peel
guard_target =ₐ x - ε = y - n
linarith
set_option linter.unusedTactic false in
example (x y : ℝ) (h : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x + n = y + ε) :
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x - ε = y - n := by
peel h
fail_if_success peel 2 this
peel 0 this
fail_if_success peel this
linarith
example (p q : ℝ → ℝ → Prop) (h : ∀ ε > 0, ∃ δ > 0, p ε δ)
(hpq : ∀ x y, x > 0 → y > 0 → p x y → q x y) :
∀ ε > 0, ∃ δ > 0, q ε δ := by
peel h with ε hε δ hδ h
guard_target =ₐ q ε δ
exact hpq ε δ hε hδ h
example (p q : ℝ → ℝ → Prop) (h : ∀ ε > 0, ∃ δ > 0, p ε δ)
(hpq : ∀ x y, x > 0 → y > 0 → p x y → q x y) :
∀ ε > 0, ∃ δ > 0, q ε δ := by
peel h with ε hε δ hδ h using hpq ε δ hε hδ h
example (x y : ℝ) : (∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x + n = y + ε) ↔
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x - ε = y - n := by
peel 5
constructor
all_goals
intro
linarith
/-! ## Usage after other tactics and with multiple goals -/
example : (∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1 / (n + 1 : ℚ) < ε) ↔
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1 / (n + 1 : ℚ) ≤ ε := by
refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩
· peel 5 h
exact this.le
· intro ε hε
peel 3 h (ε / 2) (half_pos hε)
exact this.trans_lt (half_lt_self hε)
example : (∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1 / (n + 1 : ℚ) < ε) ↔
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1 / (n + 1 : ℚ) ≤ ε := by
refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩
· peel 5 h using this.le
· intro ε hε
peel 3 h (ε / 2) (half_pos hε) using this.trans_lt (half_lt_self hε)
/-! ## Use with `↔` goals -/
example (x y : ℚ) (h : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x + n = y + ε) :
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x - ε = y - n := by
intro ε hε
peel 3 (h ε hε)
rename_i _ n _
guard_hyp this : x + ↑n = y + ε
guard_target =ₐ x - ε = y - n
linarith
example (x y : ℝ) : (∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x + n = y + ε) ↔
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x - ε = y - n := by
peel with ε hε N n hn
constructor
all_goals
intro
linarith
example : (∃ k > 0, ∃ n ≥ k, n = k) ↔ ∃ k > 0, ∃ n ≥ k, k = n := by
peel 4
exact eq_comm
/-! ## Eventually and frequently -/
example {f : ℝ → ℝ} (h : ∀ x : ℝ, ∀ᶠ y in 𝓝 x, |f y - f x| ≤ |y - x|) :
∀ x : ℝ, ∀ᶠ y in 𝓝 x, |f y - f x| ^ 2 ≤ |y - x| ^ 2 := by
peel h with h_peel x y
gcongr
example (α : Type*) (f g : Filter α) (p q : α → α → Prop) (h : ∀ᶠ x in f, ∃ᶠ y in g, p x y)
(h₁ : ∀ x y, p x y → q x y) : ∀ᶠ x in f, ∃ᶠ y in g, q x y := by
peel h with x y h_peel
exact h₁ x y h_peel
example (α : Type*) (f : Filter α) (p q : α → Prop) (h : ∀ᶠ x in f, p x) (h₁ : ∀ x, p x → q x) :
∀ᶠ x in f, q x := by
peel h with x h_peel
exact h₁ x h_peel
/-! ## Type classes -/
example {R : Type*} [CommRing R] (h : ∀ x : R, ∃ y : R, x + y = 2) :
∀ x : R, ∃ y : R, (x + y) ^ 2 = 4 := by
peel 2 h
rw [this]
ring
example {G : Type*} [Group G] [TopologicalSpace G] [IsTopologicalGroup G]
(h : ∀ᶠ x in 𝓝 (1 : G), ∃ᶠ y in 𝓝 x, x * y⁻¹ = 1) :
∀ᶠ x in 𝓝 (1 : G), ∃ᶠ y in 𝓝 x, x ^ 2 = y ^ 2 := by
peel h with a b h_peel
observe : a = b⁻¹⁻¹
simp [this]
/-! ## Unfolding definitions -/
example {α β γ : Type*} {f : α → β} {g : β → γ} (h : Function.Injective (g ∘ f)) :
Function.Injective f := by
peel 2 h with x y _
intro hf
apply this
congrm(g $hf)
example {α β γ : Type*} {f : α → β} {g : β → γ} (h : Function.Surjective (g ∘ f)) :
Function.Surjective g := by
peel 1 h with y _
fail_if_success peel this
obtain ⟨x, rfl⟩ := this
exact ⟨f x, rfl⟩
def toInf (f : ℕ → ℕ) : Prop := ∀ m, ∃ n, ∀ n' ≥ n, m ≤ f n'
example (f : ℕ → ℕ) (h : toInf f) : toInf (fun n => 2 * f n) := by
peel h with this m n n' h
dsimp
linarith
/-! ## Error messages -/
/--
error: Tactic 'peel' could not match quantifiers in
x = y
and
x = y
-/
#guard_msgs in example (x y : ℝ) (h : x = y) : x = y := by
peel h
/--
error: Tactic 'peel' could not match quantifiers in
∃ y, ∀ (x : ℕ), x ≠ y
and
∀ (x : ℕ), ∃ y, x ≠ y
-/
#guard_msgs in
example (h : ∃ y : ℕ, ∀ x, x ≠ y) : ∀ x : ℕ, ∃ y, x ≠ y := by
peel h
/--
error: Tactic 'peel' could not match quantifiers in
∀ (n : ℕ), 0 ≤ n
and
∀ (n : ℤ), 0 ≤ n
-/
#guard_msgs in
example (h : ∀ n : ℕ, 0 ≤ n) : ∀ n : ℤ, 0 ≤ n := by
peel h
/--
error: Tactic 'peel' could not match quantifiers in
∃ n, 0 ≤ n
and
∃ n, 0 ≤ n
-/
#guard_msgs in
example (h : ∃ n : ℕ, 0 ≤ n) : ∃ n : ℤ, 0 ≤ n := by
peel 1 h
/--
error: Tactic 'peel' could not match quantifiers in
∃ᶠ (n : ℕ) in atTop, 0 ≤ n
and
∃ᶠ (n : ℕ) in atBot, 0 ≤ n
-/
#guard_msgs in
example (h : ∃ᶠ n : ℕ in atTop, 0 ≤ n) : ∃ᶠ n : ℕ in atBot, 0 ≤ n := by
peel 1 h
/--
error: Tactic 'peel' could not match quantifiers in
∀ᶠ (n : ℕ) in atTop, 0 ≤ n
and
∀ᶠ (n : ℕ) in atBot, 0 ≤ n
-/
#guard_msgs in
example (h : ∀ᶠ n : ℕ in atTop, 0 ≤ n) : ∀ᶠ n : ℕ in atBot, 0 ≤ n := by
peel 1 h
/-! Testing **gcongr** on peel goals -/
example (p q : ℝ → ℝ → Prop) (h : ∀ ε > 0, ∃ δ > 0, p ε δ)
(hpq : ∀ x y, x > 0 → y > 0 → p x y → q x y) :
∀ ε > 0, ∃ δ > 0, q ε δ := by
revert h
gcongr with ε hε δ hδ
exact hpq ε δ hε hδ
example (x y : ℚ) (h : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x + n = y + ε) :
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, x - ε = y - n := by
revert h
gcongr ∀ ε > 0, ∃ N, ∀ n ≥ _, ?_ with ε hε _ n hn
intro this
guard_hyp this : x + ↑n = y + ε
guard_target =ₐ x - ε = y - n
linarith
example {f : ℝ → ℝ} (h : ∀ x : ℝ, ∀ᶠ y in 𝓝 x, |f y - f x| ≤ |y - x|) :
∀ x : ℝ, ∀ᶠ y in 𝓝 x, |f y - f x| ^ 2 ≤ |y - x| ^ 2 := by
revert h
gcongr ∀ x, ∀ᶠ y in _, ?_
intro
gcongr
example (α : Type*) (f g : Filter α) (p q : α → α → Prop) (h : ∀ᶠ x in f, ∃ᶠ y in g, p x y)
(h₁ : ∀ x y, p x y → q x y) : ∀ᶠ x in f, ∃ᶠ y in g, q x y := by
revert h
gcongr
apply h₁ |
.lake/packages/mathlib/MathlibTest/renameBvar.lean | import Mathlib.Tactic.RenameBVar
import Lean
set_option linter.unusedVariables false
axiom test_sorry {α : Sort _} : α
/- This test is somewhat flaky since it depends on the pretty printer. -/
/--
trace: ℕ : Sort u_1
P : ℕ → ℕ → Prop
h : ∀ (n : ℕ), ∃ m, P n m
⊢ ∀ (l : ℕ), ∃ m, P l m
---
trace: ℕ : Sort u_1
P : ℕ → ℕ → Prop
h : ∀ (q : ℕ), ∃ m, P q m
⊢ ∀ (l : ℕ), ∃ m, P l m
---
trace: ℕ : Sort u_1
P : ℕ → ℕ → Prop
h : ∀ (q : ℕ), ∃ m, P q m
⊢ ∀ (l : ℕ), ∃ n, P l n
---
trace: ℕ : Sort u_1
P : ℕ → ℕ → Prop
h : ∀ (q : ℕ), ∃ m, P q m
⊢ ∀ (m : ℕ), ∃ n, P m n
-/
#guard_msgs in
example (P : ℕ → ℕ → Prop) (h : ∀ n, ∃ m, P n m) : ∀ l, ∃ m, P l m := by
trace_state
rename_bvar n → q at h
trace_state
rename_bvar m → n
trace_state
rename_bvar l → m
trace_state
exact h
/--
trace: a b c : Int
h2 : b ∣ c
k : Int
hk : b = a * k
⊢ ∃ k, c = a * k
---
trace: a b c : Int
h2 : b ∣ c
k : Int
hk : b = a * k
⊢ ∃ m, c = a * m
-/
#guard_msgs in
example (a b c : Int) (h1 : a ∣ b) (h2 : b ∣ c) : a ∣ c := by
rcases h1 with ⟨k, hk⟩
show ∃ k, c = a * k
trace_state
rename_bvar k → m
trace_state
exact test_sorry |
.lake/packages/mathlib/MathlibTest/Zify.lean | import Mathlib.Tactic.Zify
import Mathlib.Algebra.Ring.Int.Parity
import Mathlib.Algebra.Ring.Int.Units
set_option linter.unusedVariables false
private axiom test_sorry : ∀ {α}, α
example (a b c x y z : ℕ) (h : ¬ x*y*z < 0) : c < a + 3*b := by
zify
guard_target =~ (c : ℤ) < (a : ℤ) + 3 * (b : ℤ)
zify at h
guard_hyp h :~ ¬(x : ℤ) * (y : ℤ) * (z : ℤ) < (0 : ℤ)
exact test_sorry
-- TODO: These are verbatim copies of the tests from mathlib3. It would be nice to add more.
-- set_option pp.coercions false
example (a b c x y z : ℕ) (h : ¬ x*y*z < 0) (h2 : (c : ℤ) < a + 3 * b) : a + 3*b > c := by
zify at h ⊢
guard_hyp h :~ ¬↑x * ↑y * ↑z < (0 : ℤ) -- TODO: canonize instances?
guard_target =~ ↑c < (↑a : ℤ) + 3 * ↑b
exact h2
example (a b : ℕ) (h : (a : ℤ) ≤ b) : a ≤ b := by
zify
guard_target = (a : ℤ) ≤ b
exact h
/- example (a b : ℕ) (h : a = b ∧ b < a) : False := by
zify at h
rcases h with ⟨ha, hb⟩
-- Preorder for `ℤ` is missing
exact ne_of_lt hb ha -/
example (a b c : ℕ) (h : a - b < c) (hab : b ≤ a) : True := by
zify [hab] at h
guard_hyp h : (a : ℤ) - b < c
trivial
example (a b c : ℕ) (h : a + b ≠ c) : True := by
zify at h
guard_hyp h : (a + b : ℤ) ≠ c
trivial
example (a b c : ℕ) (h : a - b ∣ c) (h2 : b ≤ a) : True := by
zify [h2] at h
guard_hyp h : (a : ℤ) - b ∣ c
trivial |
.lake/packages/mathlib/MathlibTest/success_if_fail_with_msg.lean | import Mathlib.Tactic.SuccessIfFailWithMsg
example : True := by
success_if_fail_with_msg "No goals to be solved" trivial; trivial
trivial
example : Nat → Nat → True := by
success_if_fail_with_msg "No goals to be solved"
intro _ _
trivial
trivial
intros; trivial
/--
info: Update with tactic error message: ⏎
[apply] "No goals to be solved"
---
error: tactic '
intro _ _
trivial
trivial' failed, but got different error message:
No goals to be solved
-/
#guard_msgs in
example : Nat → Nat → True := by
success_if_fail_with_msg "No goals"
intro _ _
trivial
trivial
intros; trivial
def err (t : Bool) := if t then
"Invalid rewrite argument: Expected an equality or iff proof or definition name, but `Nat.le_succ n` is a proof of
n ≤ n.succ"
else
"not that message
⊢ True"
set_option linter.unusedVariables false in
example (n : Nat) : True := by
success_if_fail_with_msg (err true) rw [Nat.le_succ n]
trivial
example : True := by
success_if_fail_with_msg (err false) fail "not that message"
trivial
/- In the following, we use `success_if_fail_with_msg` to write tests for
`success_if_fail_with_msg`, since the inner one should fail with a certain message. -/
example : True := by
success_if_fail_with_msg "tactic 'trivial' succeeded, but was expected to fail"
success_if_fail_with_msg "message" trivial
trivial
def err₂ := "tactic 'fail \"different message!\"' failed, but got different error message:
different message!
⊢ True"
example : True := by
success_if_fail_with_msg err₂
success_if_fail_with_msg "message" fail "different message!"
trivial
open Lean Meta Mathlib Tactic
def alwaysFails : MetaM Unit := do throwError "I failed!"
def doesntFail : MetaM Unit := do
try successIfFailWithMessage "I failed!" alwaysFails
catch _ => throwError "I *really* failed."
#guard_msgs in
#eval doesntFail |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.