Skip to content

Commit 10b90b7

Browse files
committed
chore: rename register_tag_attr, and explain relation to registerTagAttribute (#2124)
Per [zulip](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/TagAttribute/near/325879024). Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent 10bfe18 commit 10b90b7

File tree

5 files changed

+113
-102
lines changed

5 files changed

+113
-102
lines changed

Mathlib.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ import Mathlib.Tactic.InferParam
969969
import Mathlib.Tactic.Inhabit
970970
import Mathlib.Tactic.IntervalCases
971971
import Mathlib.Tactic.IrreducibleDef
972+
import Mathlib.Tactic.LabelAttr
972973
import Mathlib.Tactic.LeftRight
973974
import Mathlib.Tactic.LibrarySearch
974975
import Mathlib.Tactic.Lift
@@ -1028,7 +1029,6 @@ import Mathlib.Tactic.Spread
10281029
import Mathlib.Tactic.Substs
10291030
import Mathlib.Tactic.SudoSetOption
10301031
import Mathlib.Tactic.SwapVar
1031-
import Mathlib.Tactic.TagAttr
10321032
import Mathlib.Tactic.Tauto
10331033
import Mathlib.Tactic.ToAdditive
10341034
import Mathlib.Tactic.Trace

Mathlib/Tactic/LabelAttr.lean

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/-
2+
Copyright (c) 2023 Scott Morrison. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Scott Morrison
5+
-/
6+
import Lean
7+
8+
/-!
9+
# "Label" attributes
10+
11+
Allow creating attributes using `register_label_attr`,
12+
and retrieving the array of `Name`s of declarations which have been tagged with such an attribute.
13+
14+
These differ slightly from the built-in "tag attributes" which can be initalized with the syntax:
15+
```
16+
initialize someName : TagAttribute ← registerTagAttribute `tagName "description"
17+
```
18+
in that a "tag attribute" can only be put on a declaration at the moment it is declared,
19+
and can not be modified by scoping commands.
20+
The "label atributes" constructed here adding (or locally removing) the attribute
21+
either at the moment of declaration, or later.
22+
23+
-/
24+
25+
namespace Mathlib.Tactic.LabelAttr
26+
27+
open Lean
28+
29+
/-- An environment extension that just tracks an array of names. -/
30+
abbrev LabelExtension := SimpleScopedEnvExtension Name (Array Name)
31+
32+
/-- The collection of all current `LabelExtension`s, indexed by name. -/
33+
abbrev LabelExtensionMap := HashMap Name LabelExtension
34+
35+
/-- Store the current `LabelExtension`s. -/
36+
initialize labelExtensionMapRef : IO.Ref LabelExtensionMap ← IO.mkRef {}
37+
38+
/-- Helper function for `registerLabelAttr`. -/
39+
def mkLabelExt (name : Name := by exact decl_name%) : IO LabelExtension :=
40+
registerSimpleScopedEnvExtension {
41+
name := name
42+
initial := #[]
43+
addEntry := fun d e => d.push e
44+
}
45+
46+
/-- Helper function for `registerLabelAttr`. -/
47+
def mkLabelAttr (attrName : Name) (attrDescr : String) (ext : LabelExtension)
48+
(ref : Name := by exact decl_name%) : IO Unit :=
49+
registerBuiltinAttribute {
50+
ref := ref
51+
name := attrName
52+
descr := attrDescr
53+
applicationTime := AttributeApplicationTime.afterCompilation
54+
add := fun declName _ _ =>
55+
ext.add declName
56+
erase := fun declName => do
57+
let s := ext.getState (← getEnv)
58+
modifyEnv fun env => ext.modifyState env fun _ => s.erase declName
59+
}
60+
61+
/--
62+
Construct a new "label attribute",
63+
which does nothing except keep track of the names of the declarations with that attribute.
64+
65+
Users will generally use the `register_label_attr` macro defined below.
66+
-/
67+
def registerLabelAttr (attrName : Name) (attrDescr : String)
68+
(ref : Name := by exact decl_name%) : IO LabelExtension := do
69+
let ext ← mkLabelExt ref
70+
mkLabelAttr attrName attrDescr ext ref
71+
labelExtensionMapRef.modify fun map => map.insert attrName ext
72+
return ext
73+
74+
/--
75+
Initialize a new "label" attribute.
76+
Declarations tagged with the attribute can be retrieved using `Mathlib.Tactic.LabelAttr.labelled`.
77+
-/
78+
macro (name := _root_.Lean.Parser.Command.registerLabelAttr) doc:(docComment)?
79+
"register_label_attr" id:ident : command => do
80+
let str := id.getId.toString
81+
let idParser := mkIdentFrom id (`Parser.Attr ++ id.getId)
82+
let descr := quote (removeLeadingSpaces
83+
(doc.map (·.getDocString) |>.getD s!"labelled declarations for {id.getId.toString}"))
84+
`($[$doc:docComment]? initialize ext : LabelExtension ←
85+
registerLabelAttr $(quote id.getId) $descr $(quote id.getId)
86+
$[$doc:docComment]? syntax (name := $idParser:ident) $(quote str):str : attr)
87+
88+
/-- When `attrName` is an attribute created using `register_labelled_attr`,
89+
return the names of all declarations labelled using that attribute. -/
90+
def labelled (attrName : Name) : MetaM (Array Name) := do
91+
match (← labelExtensionMapRef.get).find? attrName with
92+
| none => throwError "No extension named {attrName}"
93+
| some ext => pure <| ext.getState (← getEnv)
94+
95+
/-- A dummy label attribute, to ease testing. -/
96+
register_label_attr dummy_label_attr

Mathlib/Tactic/SolveByElim.lean

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Mathlib.Lean.LocalContext
1010
import Mathlib.Tactic.Relation.Symm
1111
import Mathlib.Control.Basic
1212
import Mathlib.Data.Sum.Basic
13-
import Mathlib.Tactic.TagAttr
13+
import Mathlib.Tactic.LabelAttr
1414

1515
/-!
1616
A work-in-progress replacement for Lean3's `solve_by_elim` tactic.
@@ -294,7 +294,7 @@ def _root_.Lean.MVarId.applyRules (cfg : Config) (lemmas : List Expr) (only : Bo
294294
solveByElim { cfg.noBackTracking with failAtMaxDepth := false } lemmas ctx [g]
295295

296296
open Lean.Parser.Tactic
297-
open Mathlib.Tactic.TagAttr
297+
open Mathlib.Tactic.LabelAttr
298298

299299
/--
300300
`mkAssumptionSet` builds a collection of lemmas for use in
@@ -351,12 +351,12 @@ def mkAssumptionSet (noDefaults star : Bool) (add remove : List Term) (use : Arr
351351

352352
let defaults : List (TermElabM Expr) :=
353353
[← `(rfl), ← `(trivial), ← `(congrFun), ← `(congrArg)].map elab'
354-
let taggedLemmas := (← use.mapM (tagged ·.raw.getId)).flatten.toList
354+
let labelledLemmas := (← use.mapM (labelled ·.raw.getId)).flatten.toList
355355
|>.map (liftM <| mkConstWithFreshMVarLevels ·)
356356
let lemmas := if noDefaults then
357-
add.map elab' ++ taggedLemmas
357+
add.map elab' ++ labelledLemmas
358358
else
359-
add.map elab' ++ taggedLemmas ++ defaults
359+
add.map elab' ++ labelledLemmas ++ defaults
360360

361361
if !remove.isEmpty && noDefaults && !star then
362362
throwError "It doesn't make sense to remove local hypotheses when using `only` without `*`."
@@ -378,7 +378,7 @@ syntax star := "*"
378378
syntax arg := star <|> erase <|> term
379379
/-- Syntax for adding and removing terms in `solve_by_elim`. -/
380380
syntax args := " [" SolveByElim.arg,* "] "
381-
/-- Syntax for using all lemmas tagged with an attribute in `solve_by_elim`. -/
381+
/-- Syntax for using all lemmas labelled with an attribute in `solve_by_elim`. -/
382382
syntax using_ := " using " ident,*
383383

384384
open Syntax
@@ -431,8 +431,8 @@ The assumptions can be modified with similar syntax as for `simp`:
431431
* `solve_by_elim only [h₁, h₂, ..., hᵣ]` does not include the local context,
432432
`rfl`, `trivial`, `congrFun`, or `congrArg` unless they are explicitly included.
433433
* `solve_by_elim [-h₁, ... -hₙ]` removes the given local hypotheses.
434-
* `solve_by_elim using [a₁, ...]` uses all lemmas which have been tagged
435-
with the attributes `aᵢ` (these attributes must be created using `register_tag_attr`).
434+
* `solve_by_elim using [a₁, ...]` uses all lemmas which have been labelled
435+
with the attributes `aᵢ` (these attributes must be created using `register_label_attr`).
436436
437437
`solve_by_elim*` tries to solve all goals together, using backtracking if a solution for one goal
438438
makes other goals impossible.
@@ -484,8 +484,8 @@ You can specify additional rules to apply using `apply_assumption [...]`.
484484
By default `apply_assumption` will also try `rfl`, `trivial`, `congrFun`, and `congrArg`.
485485
If you don't want these, or don't want to use all hypotheses, use `apply_assumption only [...]`.
486486
You can use `apply_assumption [-h]` to omit a local hypothesis.
487-
You can use `apply_assumption using [a₁, ...]` to use all lemmas which have been tagged
488-
with the attributes `aᵢ` (these attributes must be created using `register_tag_attr`).
487+
You can use `apply_assumption using [a₁, ...]` to use all lemmas which have been labelled
488+
with the attributes `aᵢ` (these attributes must be created using `register_label_attr`).
489489
490490
`apply_assumption` will use consequences of local hypotheses obtained via `symm`.
491491
@@ -518,8 +518,8 @@ You can use `apply_rules [-h]` to omit a local hypothesis.
518518
`apply_rules` will also use `rfl`, `trivial`, `congrFun` and `congrArg`.
519519
These can be disabled, as can local hypotheses, by using `apply_rules only [...]`.
520520
521-
You can use `apply_rules using [a₁, ...]` to use all lemmas which have been tagged
522-
with the attributes `aᵢ` (these attributes must be created using `register_tag_attr`).
521+
You can use `apply_rules using [a₁, ...]` to use all lemmas which have been labelled
522+
with the attributes `aᵢ` (these attributes must be created using `register_label_attr`).
523523
524524
You can pass a further configuration via the syntax `apply_rules (config := {...})`.
525525
The options supported are the same as for `solve_by_elim` (and include all the options for `apply`).

Mathlib/Tactic/TagAttr.lean

Lines changed: 0 additions & 85 deletions
This file was deleted.

test/solve_by_elim/basic.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,22 @@ end apply_assumption
151151

152152
section «using»
153153

154-
@[dummy_tag_attr] axiom foo : 1 = 2
154+
@[dummy_label_attr] axiom foo : 1 = 2
155155

156156
example : 1 = 2 := by
157157
fail_if_success solve_by_elim
158-
solve_by_elim using dummy_tag_attr
158+
solve_by_elim using dummy_label_attr
159159

160160
end «using»
161161

162162
section issue1581
163163

164164
axiom mySorry {α} : α
165165

166-
@[dummy_tag_attr] theorem le_rfl [LE α] {b c : α} (_h : b = c) : b ≤ c := mySorry
166+
@[dummy_label_attr] theorem le_rfl [LE α] {b c : α} (_h : b = c) : b ≤ c := mySorry
167167

168168
example : 57 := by
169-
apply_rules using dummy_tag_attr
169+
apply_rules using dummy_label_attr
170170
guard_target = 5 = 7
171171
exact mySorry
172172

0 commit comments

Comments
 (0)