Skip to content

Commit ed35afa

Browse files
committed
doc: update use doc-string to distinguish exists / use / use! (leanprover-community#32699)
Zulip discussion: [#general > use vs. exists @ 💬](https://leanprover.zulipchat.com/#narrow/channel/113488-general/topic/use.20vs.2E.20exists/near/562650656)
1 parent 3bd92ff commit ed35afa

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

Mathlib/Tactic/Use.lean

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ example : ∃ x : Nat, x = x := by use 42
174174
175175
example : ∃ x : Nat, ∃ y : Nat, x = y := by use 42, 42
176176
177-
example : ∃ x : String × String, x.1 = x.2 := by use ("forty-two", "forty-two")
177+
example : Nonempty Nat := by use 5
178+
179+
example : Nonempty (PNat ≃ Nat) := by
180+
use PNat.natPred, Nat.succPNat
181+
· exact PNat.succPNat_natPred
182+
· intro; rfl
178183
```
179184
180185
`use! e₁, e₂, ⋯` is similar but it applies constructors everywhere rather than just for
@@ -184,7 +189,7 @@ leaves and nodes of the tree of constructors.
184189
With `use!` one can feed in each `42` one at a time:
185190
186191
```lean
187-
example : ∃ p : Nat × Nat, p.1 = p.2 := by use! 42, 42
192+
example : ∃ n : {n : Nat // n % 2 = 0}, n.val > 10 := by use! 20; simp
188193
189194
example : ∃ p : Nat × Nat, p.1 = p.2 := by use! (42, 42)
190195
```

MathlibTest/Use.lean

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Authors: Arthur Paulino
66

77
import Mathlib.Tactic.Use
88
import Mathlib.Tactic.Basic
9+
import Mathlib.Logic.Equiv.Defs
10+
import Mathlib.Data.PNat.Defs
911

1012
namespace UseTests
1113

@@ -37,9 +39,9 @@ example : ∃ x : Nat, ∃ y : Nat, x = y := by use 42, 42
3739

3840
/--
3941
error: failed to synthesize instance of type class
40-
OfNat (Nat × Nat) 42
42+
OfNat ( × ) 42
4143
numerals are polymorphic in Lean, but the numeral `42` cannot be used in a context where the expected type is
42-
Nat × Nat
44+
×
4345
due to the absence of the instance above
4446
4547
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
@@ -64,6 +66,15 @@ example : ∃ x : String × String, x.1 = x.2 := by use ("a", "a")
6466

6567
example : ∃ x : String × String, x.1 = x.2 := by use! "a", "a"
6668

69+
example : Nonempty Nat := by use 5
70+
71+
example : Nonempty (PNat ≃ Nat) := by
72+
use PNat.natPred, Nat.succPNat
73+
· exact PNat.succPNat_natPred
74+
· intro; rfl
75+
76+
example : ∃ n : {n : Nat // n % 2 = 0}, n.val > 10 := by use! 20; simp
77+
6778
-- This example is why `use` always tries applying the constructor before refining.
6879
example : ∃ x : Nat, x = x := by
6980
use ?_
@@ -94,9 +105,9 @@ example : Σ _x _y : Int, (Int × Int) × Int := by
94105
-- There are two constructors, so applying a constructor fails and it tries to just refine
95106
/--
96107
error: failed to synthesize instance of type class
97-
OfNat (Option Nat) 1
108+
OfNat (Option ) 1
98109
numerals are polymorphic in Lean, but the numeral `1` cannot be used in a context where the expected type is
99-
Option Nat
110+
Option
100111
due to the absence of the instance above
101112
102113
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
@@ -106,9 +117,9 @@ example : Option Nat := by use 1
106117

107118
/--
108119
error: failed to synthesize instance of type class
109-
OfNat (NatNat) 1
120+
OfNat () 1
110121
numerals are polymorphic in Lean, but the numeral `1` cannot be used in a context where the expected type is
111-
NatNat
122+
112123
due to the absence of the instance above
113124
114125
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
@@ -172,7 +183,7 @@ example : Baz 0 3 := by use 4
172183
error: Type mismatch
173184
3
174185
has type
175-
Nat
186+
176187
of sort `Type` but is expected to have type
177188
Baz 1 3
178189
of sort `Prop`

0 commit comments

Comments
 (0)