Skip to content

Commit 34f1f7f

Browse files
committed
Adapt eqns
1 parent c819cdc commit 34f1f7f

File tree

4 files changed

+24
-97
lines changed

4 files changed

+24
-97
lines changed

Mathlib/Tactic/Eqns.lean

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +0,0 @@
1-
/-
2-
Copyright (c) 2023 Eric Wieser. All rights reserved.
3-
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: Eric Wieser
5-
-/
6-
import Mathlib.Init
7-
import Lean.Meta.Eqns
8-
import Batteries.Lean.NameMapAttribute
9-
import Lean.Elab.Exception
10-
import Lean.Elab.InfoTree.Main
11-
12-
/-! # The `@[eqns]` attribute
13-
14-
This file provides the `eqns` attribute as a way of overriding the default equation lemmas. For
15-
example
16-
17-
```lean4
18-
def transpose {m n} (A : m → n → ℕ) : n → m → ℕ
19-
| i, j => A j i
20-
21-
theorem transpose_apply {m n} (A : m → n → ℕ) (i j) :
22-
transpose A i j = A j i := rfl
23-
24-
attribute [eqns transpose_apply] transpose
25-
26-
theorem transpose_const {m n} (c : ℕ) :
27-
transpose (fun (i : m) (j : n) => c) = fun j i => c := by
28-
funext i j -- the rw below does not work without this line
29-
rw [transpose]
30-
```
31-
-/
32-
open Lean Elab
33-
34-
syntax (name := eqns) "eqns" (ppSpace ident)* : attr
35-
36-
initialize eqnsAttribute : NameMapExtension (Array Name) ←
37-
registerNameMapAttribute {
38-
name := `eqns
39-
descr := "Overrides the equation lemmas for a declaration to the provided list"
40-
add := fun
41-
| declName, `(attr| eqns $[$names]*) => do
42-
if let some _ := Meta.eqnsExt.getState (← getEnv) |>.map.find? declName then
43-
throwError "There already exist stored eqns for '{declName}'; registering new equations \
44-
will not have the desired effect."
45-
names.mapM realizeGlobalConstNoOverloadWithInfo
46-
| _, _ => Lean.Elab.throwUnsupportedSyntax }
47-
48-
initialize Lean.Meta.registerGetEqnsFn (fun name => do
49-
pure (eqnsAttribute.find? (← getEnv) name))

Mathlib/Tactic/IrreducibleDef.lean

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ open Command in elab_rules : command
6161

6262
syntax irredDefLemma := atomic(" (" &"lemma" " := ") ident ")"
6363

64+
/-- Like `theorem`, but allows defining reserved names. -/
65+
local elab "reserved_theorem_alias" declIdent:ident suffix:str " := " srcIdent:ident : command => do
66+
let declName ← resolveGlobalConstNoOverload declIdent
67+
let src ← resolveGlobalConstNoOverload srcIdent
68+
let info ← getConstVal src
69+
let tgt := .str declName suffix.getString
70+
liftTermElabM do
71+
realizeConst declName tgt do
72+
addDecl <| Declaration.thmDecl {
73+
name := tgt,
74+
type := info.type,
75+
value := .const src (info.levelParams.map mkLevelParam),
76+
levelParams := info.levelParams
77+
}
78+
pure ()
79+
80+
6481
/--
6582
Introduces an irreducible definition.
6683
`irreducible_def foo := 42` generates
@@ -100,8 +117,9 @@ elab mods:declModifiers "irreducible_def" n_id:declId n_def:(irredDefLemma)?
100117
delta $n:ident
101118
rw [show wrapped = ⟨@definition.{$us',*}, rfl⟩ from Subtype.ext wrapped.2.symm]
102119
rfl
120+
reserved_theorem_alias $n "eq_def" := $n_def:ident
121+
reserved_theorem_alias $n "eq_1" := $n_def:ident
103122
attribute [irreducible] $n definition
104-
attribute [eqns $n_def] $n
105123
attribute [$attrs:attrInstance,*] $n)
106124
if prot.isSome then
107125
modifyEnv (addProtected · ((← getCurrNamespace) ++ n.getId))

Mathlib/Tactic/ToAdditive/Frontend.lean

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import Lean.Meta.AbstractNestedProofs
1818
import Batteries.Lean.NameMapAttribute
1919
import Batteries.Tactic.Lint -- useful to lint this file and for DiscrTree.elements
2020
import Batteries.Tactic.Trans
21-
import Mathlib.Tactic.Eqns -- just to copy the attribute
2221
import Mathlib.Tactic.Simps.Basic
2322

2423
/-!
@@ -1287,16 +1286,11 @@ partial def applyAttributes (stx : Syntax) (rawAttrs : Array Syntax) (thisAttr s
12871286
Copies equation lemmas and attributes from `src` to `tgt`
12881287
-/
12891288
partial def copyMetaData (cfg : Config) (src tgt : Name) : CoreM (Array Name) := do
1290-
if let some eqns := eqnsAttribute.find? (← getEnv) src then
1291-
unless (eqnsAttribute.find? (← getEnv) tgt).isSome do
1292-
for eqn in eqns do _ ← addToAdditiveAttr eqn cfg
1293-
eqnsAttribute.add tgt (eqns.map (findTranslation? (← getEnv) · |>.get!))
1294-
else
1295-
/- We need to generate all equation lemmas for `src` and `tgt`, even for non-recursive
1296-
definitions. If we don't do that, the equation lemma for `src` might be generated later
1297-
when doing a `rw`, but it won't be generated for `tgt`. -/
1298-
additivizeLemmas #[src, tgt] "equation lemmas" fun nm ↦
1299-
(·.getD #[]) <$> MetaM.run' (getEqnsFor? nm)
1289+
/- We need to generate all equation lemmas for `src` and `tgt`, even for non-recursive
1290+
definitions. If we don't do that, the equation lemma for `src` might be generated later
1291+
when doing a `rw`, but it won't be generated for `tgt`. -/
1292+
additivizeLemmas #[src, tgt] "equation lemmas" fun nm ↦
1293+
(·.getD #[]) <$> MetaM.run' (getEqnsFor? nm)
13001294
MetaM.run' <| Elab.Term.TermElabM.run' <|
13011295
applyAttributes cfg.ref cfg.attrs `to_additive src tgt
13021296

MathlibTest/eqns.lean

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +0,0 @@
1-
import Mathlib.Tactic.Eqns
2-
def transpose {m n} (A : m → n → Nat) : n → m → Nat
3-
| i, j => A j i
4-
5-
theorem transpose_apply {m n} (A : m → n → Nat) (i j) : transpose A i j = A j i := rfl
6-
7-
attribute [eqns transpose_apply] transpose
8-
9-
theorem transpose_const {m n} (c : Nat) :
10-
transpose (fun (_i : m) (_j : n) => c) = fun _j _i => c := by
11-
fail_if_success {rw [transpose]}
12-
fail_if_success {simp [transpose]}
13-
funext i j -- the rw below does not work without this line
14-
rw [transpose]
15-
16-
def t : Nat := 0 + 1
17-
18-
theorem t_def : t = 1 := rfl
19-
-- this rw causes lean to generate equations itself for t before the user can register them
20-
theorem t_def' : t = 1 := by rw [t]
21-
22-
-- We used to test that this generated an error,
23-
-- but with asynchronous elaboration, it now longer does!
24-
-- `eqns` still seems to work (via `irreducible_def`) as expected,
25-
-- so for now we just comment out the test...
26-
-- #guard_msgs(error) in
27-
-- attribute [eqns t_def] t
28-
29-
/--
30-
warning: declaration uses 'sorry'
31-
-/
32-
#guard_msgs in
33-
-- the above should error as the above equation would not have changed the output of the below
34-
example (n : Nat) : t = n := by
35-
rw [t]
36-
admit

0 commit comments

Comments
 (0)