-
Notifications
You must be signed in to change notification settings - Fork 259
/
ToAdditive.lean
1274 lines (1161 loc) · 61.9 KB
/
ToAdditive.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Yury Kudryashov, Floris van Doorn, Jon Eugster
-/
import Mathlib.Init.Data.Nat.Notation
import Mathlib.Data.String.Defs
import Mathlib.Data.Array.Defs
import Mathlib.Lean.Expr.ReplaceRec
import Mathlib.Lean.EnvExtension
import Mathlib.Lean.Meta.Simp
import Std.Lean.NameMapAttribute
import Std.Data.Option.Basic
import Std.Tactic.CoeExt -- just to copy the attribute
import Std.Tactic.Ext.Attr -- just to copy the attribute
import Std.Tactic.Lint -- useful to lint this file and for for DiscrTree.elements
import Std.Tactic.Relation.Rfl -- just to copy the attribute
import Std.Tactic.Relation.Symm -- just to copy the attribute
import Mathlib.Tactic.Relation.Trans -- just to copy the attribute
import Mathlib.Tactic.Eqns -- just to copy the attribute
import Mathlib.Tactic.Simps.Basic
/-!
# The `@[to_additive]` attribute.
The attribute `to_additive` can be used to automatically transport theorems
and definitions (but not inductive types and structures) from a multiplicative
theory to an additive theory.
-/
set_option autoImplicit true
open Lean Meta Elab Command Std
/-- The `to_additive_ignore_args` attribute. -/
syntax (name := to_additive_ignore_args) "to_additive_ignore_args" (ppSpace num)* : attr
/-- The `to_additive_relevant_arg` attribute. -/
syntax (name := to_additive_relevant_arg) "to_additive_relevant_arg " num : attr
/-- The `to_additive_reorder` attribute. -/
syntax (name := to_additive_reorder) "to_additive_reorder " (num+),+ : attr
/-- The `to_additive_change_numeral` attribute. -/
syntax (name := to_additive_change_numeral) "to_additive_change_numeral" (ppSpace num)* : attr
/-- An `attr := ...` option for `to_additive`. -/
syntax toAdditiveAttrOption := &"attr" " := " Parser.Term.attrInstance,*
/-- A `reorder := ...` option for `to_additive`. -/
syntax toAdditiveReorderOption := &"reorder" " := " (num+),+
/-- Options to `to_additive`. -/
syntax toAdditiveParenthesizedOption := "(" toAdditiveAttrOption <|> toAdditiveReorderOption ")"
/-- Options to `to_additive`. -/
syntax toAdditiveOption := toAdditiveParenthesizedOption <|> &"existing"
/-- Remaining arguments of `to_additive`. -/
syntax toAdditiveRest := (ppSpace toAdditiveOption)* (ppSpace ident)? (ppSpace str)?
/-- The `to_additive` attribute. -/
syntax (name := to_additive) "to_additive" "?"? toAdditiveRest : attr
/-- The `to_additive` attribute. -/
macro "to_additive?" rest:toAdditiveRest : attr => `(attr| to_additive ? $rest)
/-- A set of strings of names that end in a capital letter.
* If the string contains a lowercase letter, the string should be split between the first occurrence
of a lower-case letter followed by an upper-case letter.
* If multiple strings have the same prefix, they should be grouped by prefix
* In this case, the second list should be prefix-free
(no element can be a prefix of a later element)
Todo: automate the translation from `String` to an element in this `RBMap`
(but this would require having something similar to the `rb_lmap` from Lean 3). -/
def endCapitalNames : Lean.RBMap String (List String) compare :=
-- todo: we want something like
-- endCapitalNamesOfList ["LE", "LT", "WF", "CoeTC", "CoeT", "CoeHTCT"]
.ofList [("LE", [""]), ("LT", [""]), ("WF", [""]), ("Coe", ["TC", "T", "HTCT"])]
/--
This function takes a String and splits it into separate parts based on the following
(naming conventions)[https://github.com/leanprover-community/mathlib4/wiki#naming-convention].
E.g. `#eval "InvHMulLEConjugate₂SMul_ne_top".splitCase` yields
`["Inv", "HMul", "LE", "Conjugate₂", "SMul", "_", "ne", "_", "top"]`.
-/
partial def String.splitCase (s : String) (i₀ : Pos := 0) (r : List String := []) : List String :=
Id.run do
-- We test if we need to split between `i₀` and `i₁`.
let i₁ := s.next i₀
if s.atEnd i₁ then
-- If `i₀` is the last position, return the list.
let r := s::r
return r.reverse
/- We split the string in three cases
* We split on both sides of `_` to keep them there when rejoining the string;
* We split after a name in `endCapitalNames`;
* We split after a lower-case letter that is followed by an upper-case letter
(unless it is part of a name in `endCapitalNames`). -/
if s.get i₀ == '_' || s.get i₁ == '_' then
return splitCase (s.extract i₁ s.endPos) 0 <| (s.extract 0 i₁)::r
if (s.get i₁).isUpper then
if let some strs := endCapitalNames.find? (s.extract 0 i₁) then
if let some (pref, newS) := strs.findSome?
fun x : String ↦ (s.extract i₁ s.endPos).dropPrefix? x |>.map (x, ·.toString) then
return splitCase newS 0 <| (s.extract 0 i₁ ++ pref)::r
if !(s.get i₀).isUpper then
return splitCase (s.extract i₁ s.endPos) 0 <| (s.extract 0 i₁)::r
return splitCase s i₁ r
namespace ToAdditive
initialize registerTraceClass `to_additive
initialize registerTraceClass `to_additive_detail
/-- Linter to check that the `reorder` attribute is not given manually -/
register_option linter.toAdditiveReorder : Bool := {
defValue := true
descr := "Linter to check that the reorder attribute is not given manually." }
/-- Linter, mostly used by `@[to_additive]`, that checks that the source declaration doesn't have
certain attributes -/
register_option linter.existingAttributeWarning : Bool := {
defValue := true
descr := "Linter, mostly used by `@[to_additive]`, that checks that the source declaration " ++
"doesn't have certain attributes" }
/-- Linter to check that the `to_additive` attribute is not given manually -/
register_option linter.toAdditiveGenerateName : Bool := {
defValue := true
descr := "Linter used by `@[to_additive]` that checks if `@[to_additive]` automatically " ++
"generates the user-given name" }
/-- Linter to check whether the user correctly specified that the additive declaration already
exists -/
register_option linter.toAdditiveExisting : Bool := {
defValue := true
descr := "Linter used by `@[to_additive]` that checks whether the user correctly specified that
the additive declaration already exists" }
/--
An attribute that tells `@[to_additive]` that certain arguments of this definition are not
involved when using `@[to_additive]`.
This helps the heuristic of `@[to_additive]` by also transforming definitions if `ℕ` or another
fixed type occurs as one of these arguments.
-/
initialize ignoreArgsAttr : NameMapExtension (List Nat) ←
registerNameMapAttribute {
name := `to_additive_ignore_args
descr :=
"Auxiliary attribute for `to_additive` stating that certain arguments are not additivized."
add := fun _ stx ↦ do
let ids ← match stx with
| `(attr| to_additive_ignore_args $[$ids:num]*) => pure <| ids.map (·.1.isNatLit?.get!)
| _ => throwUnsupportedSyntax
return ids.toList }
/--
An attribute that stores all the declarations that needs their arguments reordered when
applying `@[to_additive]`. It is applied automatically by the `(reorder := ...)` syntax of
`to_additive`, and should not usually be added manually.
-/
initialize reorderAttr : NameMapExtension (List $ List Nat) ←
registerNameMapAttribute {
name := `to_additive_reorder
descr :=
"Auxiliary attribute for `to_additive` that stores arguments that need to be reordered.
This should not appear in any file.
We keep it as an attribute for now so that mathport can still use it, and it can generate a
warning."
add := fun
| _, stx@`(attr| to_additive_reorder $[$[$reorders:num]*],*) => do
Linter.logLintIf linter.toAdditiveReorder stx
m!"Using this attribute is deprecated. Use `@[to_additive (reorder := <num>)]` {""
}instead.\nThat will also generate the additive version with the arguments swapped, {""
}so you are probably able to remove the manually written additive declaration."
pure <| reorders.toList.map (·.toList.map (·.raw.isNatLit?.get! - 1))
| _, _ => throwUnsupportedSyntax }
/--
An attribute that is automatically added to declarations tagged with `@[to_additive]`, if needed.
This attribute tells which argument is the type where this declaration uses the multiplicative
structure. If there are multiple argument, we typically tag the first one.
If this argument contains a fixed type, this declaration will note be additivized.
See the Heuristics section of `to_additive.attr` for more details.
If a declaration is not tagged, it is presumed that the first argument is relevant.
`@[to_additive]` uses the function `to_additive.first_multiplicative_arg` to automatically tag
declarations. It is ok to update it manually if the automatic tagging made an error.
Implementation note: we only allow exactly 1 relevant argument, even though some declarations
(like `prod.group`) have multiple arguments with a multiplicative structure on it.
The reason is that whether we additivize a declaration is an all-or-nothing decision, and if
we will not be able to additivize declarations that (e.g.) talk about multiplication on `ℕ × α`
anyway.
Warning: interactions between this and the `(reorder := ...)` argument are not well-tested.
-/
initialize relevantArgAttr : NameMapExtension Nat ←
registerNameMapAttribute {
name := `to_additive_relevant_arg
descr := "Auxiliary attribute for `to_additive` stating" ++
" which arguments are the types with a multiplicative structure."
add := fun
| _, `(attr| to_additive_relevant_arg $id) => pure <| id.1.isNatLit?.get!.pred
| _, _ => throwUnsupportedSyntax }
/--
An attribute that stores all the declarations that deal with numeric literals on variable types.
Numeral literals occur in expressions without type information, so in order to decide whether `1`
needs to be changed to `0`, the context around the numeral is relevant.
Most numerals will be in an `OfNat.ofNat` application, though tactics can add numeral literals
inside arbitrary functions. By default we assume that we do not change numerals, unless it is
in a function application with the `to_additive_change_numeral` attribute.
`@[to_additive_change_numeral n₁ ...]` should be added to all functions that take one or more
numerals as argument that should be changed if `additiveTest` succeeds on the first argument,
i.e. when the numeral is only translated if the first argument is a variable
(or consists of variables).
The arguments `n₁ ...` are the positions of the numeral arguments (starting counting from 1).
-/
initialize changeNumeralAttr : NameMapExtension (List Nat) ←
registerNameMapAttribute {
name := `to_additive_change_numeral
descr :=
"Auxiliary attribute for `to_additive` that stores functions that have numerals as argument."
add := fun
| _, `(attr| to_additive_change_numeral $[$arg]*) =>
pure <| arg.map (·.1.isNatLit?.get!.pred) |>.toList
| _, _ => throwUnsupportedSyntax }
/-- Maps multiplicative names to their additive counterparts. -/
initialize translations : NameMapExtension Name ← registerNameMapExtension _
/-- Get the multiplicative → additive translation for the given name. -/
def findTranslation? (env : Environment) : Name → Option Name :=
(ToAdditive.translations.getState env).find?
/-- Add a (multiplicative → additive) name translation to the translations map. -/
def insertTranslation (src tgt : Name) (failIfExists := true) : CoreM Unit := do
if let some tgt' := findTranslation? (← getEnv) src then
if failIfExists then
throwError "The translation {src} ↦ {tgt'} already exists"
else
trace[to_additive] "The translation {src} ↦ {tgt'} already exists"
return
modifyEnv (ToAdditive.translations.addEntry · (src, tgt))
trace[to_additive] "Added translation {src} ↦ {tgt}"
/-- `Config` is the type of the arguments that can be provided to `to_additive`. -/
structure Config : Type where
/-- View the trace of the to_additive procedure.
Equivalent to `set_option trace.to_additive true`. -/
trace : Bool := false
/-- The name of the target (the additive declaration).-/
tgt : Name := Name.anonymous
/-- An optional doc string.-/
doc : Option String := none
/-- If `allowAutoName` is `false` (default) then
`@[to_additive]` will check whether the given name can be auto-generated. -/
allowAutoName : Bool := false
/-- The arguments that should be reordered by `to_additive`, using cycle notation. -/
reorder : List (List Nat) := []
/-- The attributes which we want to give to both the multiplicative and additive versions.
For certain attributes (such as `simp` and `simps`) this will also add generated lemmas to the
translation dictionary. -/
attrs : Array Syntax := #[]
/-- The `Syntax` element corresponding to the original multiplicative declaration
(or the `to_additive` attribute if it is added later),
which we need for adding definition ranges. -/
ref : Syntax
/-- An optional flag stating whether the additive declaration already exists.
If this flag is set but wrong about whether the additive declaration exists, `to_additive` will
raise a linter error.
Note: the linter will never raise an error for inductive types and structures. -/
existing : Option Bool := none
deriving Repr
variable [Monad M] [MonadOptions M] [MonadEnv M]
open Lean.Expr.FindImpl in
/-- Implementation function for `additiveTest`.
We cache previous applications of the function, using the same method that `Expr.find?` uses,
to avoid visiting the same subexpression many times. Note that we only need to cache the
expressions without taking the value of `inApp` into account, since `inApp` only matters when
the expression is a constant. However, for this reason we have to make sure that we never
cache constant expressions, so that's why the `if`s in the implementation are in this order.
Note that this function is still called many times by `applyReplacementFun`
and we're not remembering the cache between these calls. -/
unsafe def additiveTestUnsafe (findTranslation? : Name → Option Name)
(ignore : Name → Option (List ℕ)) (e : Expr) : Option Name :=
let rec visit (e : Expr) (inApp := false) : OptionT FindM Name := do
if e.isConst then
if inApp || (findTranslation? e.constName).isSome then
failure
else
return e.constName
checkVisited e
match e with
| x@(.app e a) =>
visit e true <|> do
-- make sure that we don't treat `(fun x => α) (n + 1)` as a type that depends on `Nat`
guard !x.isConstantApplication
if let some n := e.getAppFn.constName? then
if let some l := ignore n then
if e.getAppNumArgs + 1 ∈ l then
failure
visit a
| .lam _ _ t _ => visit t
| .forallE _ _ t _ => visit t
| .letE _ _ e body _ => visit e <|> visit body
| .mdata _ b => visit b
| .proj _ _ b => visit b
| _ => failure
Id.run <| (visit e).run' mkPtrSet
/--
`additiveTest e` tests whether the expression `e` contains a constant
`nm` that is not applied to any arguments, and such that `translations.find?[nm] = none`.
This is used in `@[to_additive]` for deciding which subexpressions to transform: we only transform
constants if `additiveTest` applied to their first argument returns `true`.
This means we will replace expression applied to e.g. `α` or `α × β`, but not when applied to
e.g. `ℕ` or `ℝ × α`.
We ignore all arguments specified by the `ignore` `NameMap`.
-/
def additiveTest (findTranslation? : Name → Option Name)
(ignore : Name → Option (List ℕ)) (e : Expr) : Option Name :=
unsafe additiveTestUnsafe findTranslation? ignore e
/-- Swap the first two elements of a list -/
def _root_.List.swapFirstTwo {α : Type _} : List α → List α
| [] => []
| [x] => [x]
| x::y::l => y::x::l
/-- Change the numeral `nat_lit 1` to the numeral `nat_lit 0`.
Leave all other expressions unchanged. -/
def changeNumeral : Expr → Expr
| .lit (.natVal 1) => mkRawNatLit 0
| e => e
/--
`applyReplacementFun e` replaces the expression `e` with its additive counterpart.
It translates each identifier (inductive type, defined function etc) in an expression, unless
* The identifier occurs in an application with first argument `arg`; and
* `test arg` is false.
However, if `f` is in the dictionary `relevant`, then the argument `relevant.find f`
is tested, instead of the first argument.
It will also reorder arguments of certain functions, using `reorderFn`:
e.g. `g x₁ x₂ x₃ ... xₙ` becomes `g x₂ x₁ x₃ ... xₙ` if `reorderFn g = some [1]`.
-/
def applyReplacementFun (e : Expr) : MetaM Expr := do
let env ← getEnv
let reorderFn : Name → List (List ℕ) := fun nm ↦ (reorderAttr.find? env nm |>.getD [])
let relevantArg : Name → ℕ := fun nm ↦ (relevantArgAttr.find? env nm).getD 0
return aux
(findTranslation? <| ← getEnv) reorderFn (ignoreArgsAttr.find? env)
(changeNumeralAttr.find? env) relevantArg (← getBoolOption `trace.to_additive_detail) e
where /-- Implementation of `applyReplacementFun`. -/
aux (findTranslation? : Name → Option Name)
(reorderFn : Name → List (List ℕ)) (ignore : Name → Option (List ℕ))
(changeNumeral? : Name → Option (List Nat)) (relevantArg : Name → ℕ) (trace : Bool) :
Expr → Expr :=
Lean.Expr.replaceRec fun r e ↦ Id.run do
if trace then
dbg_trace s!"replacing at {e}"
match e with
| .const n₀ ls₀ => do
let n₁ := n₀.mapPrefix findTranslation?
let ls₁ : List Level := if 0 ∈ (reorderFn n₀).join then ls₀.swapFirstTwo else ls₀
if trace then
if n₀ != n₁ then
dbg_trace s!"changing {n₀} to {n₁}"
if 0 ∈ (reorderFn n₀).join then
dbg_trace s!"reordering the universe variables from {ls₀} to {ls₁}"
return some <| Lean.mkConst n₁ ls₁
| .app g x => do
let gf := g.getAppFn
if gf.isBVar && x.isLit then
if trace then
dbg_trace s!"applyReplacementFun: Variables applied to numerals are not changed {g.app x}"
return some <| g.app x
let gArgs := g.getAppArgs
let mut gAllArgs := gArgs.push x
let (gfAdditive, gAllArgsAdditive) ←
if let some nm := gf.constName? then
-- e = `(nm y₁ .. yₙ x)
/- Test if the head should not be replaced. -/
let relevantArgId := relevantArg nm
let gfAdditive :=
if relevantArgId < gAllArgs.size && gf.isConst then
if let some fxd := additiveTest findTranslation? ignore gAllArgs[relevantArgId]! then
Id.run <| do
if trace then
dbg_trace s!"The application of {nm} contains the fixed type {fxd
}, so it is not changed"
gf
else
r gf
else
r gf
/- Test if arguments should be reordered. -/
let reorder := reorderFn nm
if !reorder.isEmpty && relevantArgId < gAllArgs.size &&
(additiveTest findTranslation? ignore gAllArgs[relevantArgId]!).isNone then
gAllArgs := gAllArgs.permute! reorder
if trace then
dbg_trace s!"reordering the arguments of {nm} using the cyclic permutations {reorder}"
/- Do not replace numerals in specific types. -/
let firstArg := gAllArgs[0]!
if let some changedArgNrs := changeNumeral? nm then
if additiveTest findTranslation? ignore firstArg |>.isNone then
if trace then
dbg_trace s!"applyReplacementFun: We change the numerals in this expression. {
""}However, we will still recurse into all the non-numeral arguments."
-- In this case, we still update all arguments of `g` that are not numerals,
-- since all other arguments can contain subexpressions like
-- `(fun x ↦ ℕ) (1 : G)`, and we have to update the `(1 : G)` to `(0 : G)`
gAllArgs := gAllArgs.mapIdx fun argNr arg ↦
if changedArgNrs.contains argNr then
changeNumeral arg
else
arg
pure <| (gfAdditive, ← gAllArgs.mapM r)
else
pure (← r gf, ← gAllArgs.mapM r)
return some <| mkAppN gfAdditive gAllArgsAdditive
| .proj n₀ idx e => do
let n₁ := n₀.mapPrefix findTranslation?
if trace then
dbg_trace s!"applyReplacementFun: in projection {e}.{idx} of type {n₀}, {""
}replace type with {n₁}"
return some <| .proj n₁ idx <| ← r e
| _ => return none
/-- Eta expands `e` at most `n` times.-/
def etaExpandN (n : Nat) (e : Expr) : MetaM Expr := do
forallBoundedTelescope (← inferType e) (some n) fun xs _ ↦ mkLambdaFVars xs (mkAppN e xs)
/-- `e.expand` eta-expands all expressions that have as head a constant `n` in
`reorder`. They are expanded until they are applied to one more argument than the maximum in
`reorder.find n`. -/
def expand (e : Expr) : MetaM Expr := do
let env ← getEnv
let reorderFn : Name → List (List ℕ) := fun nm ↦ (reorderAttr.find? env nm |>.getD [])
let e₂ ← Lean.Meta.transform (input := e) (post := fun e => return .done e) <| fun e ↦ do
let e0 := e.getAppFn
let es := e.getAppArgs
let some e0n := e0.constName? | return .continue
let reorder := reorderFn e0n
if reorder.isEmpty then
-- no need to expand if nothing needs reordering
return .continue
let needed_n := reorder.join.foldr Nat.max 0 + 1
-- the second disjunct is a temporary fix to avoid infinite loops.
-- We may need to use `replaceRec` or something similar to not change the head of an application
if needed_n ≤ es.size || es.size == 0 then
return .continue
else
-- in this case, we need to reorder arguments that are not yet
-- applied, so first η-expand the function.
let e' ← etaExpandN (needed_n - es.size) e
trace[to_additive_detail] "expanded {e} to {e'}"
return .continue e'
if e != e₂ then
trace[to_additive_detail] "expand:\nBefore: {e}\nAfter: {e₂}"
return e₂
/-- Reorder pi-binders. See doc of `reorderAttr` for the interpretation of the argument -/
def reorderForall (src : Expr) (reorder : List (List Nat) := []) : MetaM Expr := do
if reorder == [] then
return src
forallTelescope src fun xs e => do
mkForallFVars (xs.permute! reorder) e
/-- Reorder lambda-binders. See doc of `reorderAttr` for the interpretation of the argument -/
def reorderLambda (src : Expr) (reorder : List (List Nat) := []) : MetaM Expr := do
if reorder == [] then
return src
lambdaTelescope src fun xs e => do
mkLambdaFVars (xs.permute! reorder) e
/-- Run applyReplacementFun on the given `srcDecl` to make a new declaration with name `tgt` -/
def updateDecl (tgt : Name) (srcDecl : ConstantInfo) (reorder : List (List Nat) := []) :
MetaM ConstantInfo := do
let mut decl := srcDecl.updateName tgt
if 0 ∈ reorder.join then
decl := decl.updateLevelParams decl.levelParams.swapFirstTwo
decl := decl.updateType <| ← applyReplacementFun <| ← reorderForall (← expand decl.type) reorder
if let some v := decl.value? then
decl := decl.updateValue <| ← applyReplacementFun <| ← reorderLambda (← expand v) reorder
else if let .opaqueInfo info := decl then -- not covered by `value?`
decl := .opaqueInfo { info with
value := ← applyReplacementFun <| ← reorderLambda (← expand info.value) reorder }
return decl
/-- Find the target name of `pre` and all created auxiliary declarations. -/
def findTargetName (env : Environment) (src pre tgt_pre : Name) : CoreM Name :=
/- This covers auxiliary declarations like `match_i` and `proof_i`. -/
if let some post := pre.isPrefixOf? src then
return tgt_pre ++ post
/- This covers equation lemmas (for other declarations). -/
else if let some post := privateToUserName? src then
match findTranslation? env post.getPrefix with
-- this is an equation lemma for a declaration without `to_additive`. We will skip this.
| none => return src
-- this is an equation lemma for a declaration with `to_additive`. We will additivize this.
-- Note: if this errors we could do this instead by calling `getEqnsFor?`
| some addName => return src.updatePrefix <| mkPrivateName env addName
-- Note: this additivizes lemmas generated by `simp`.
-- Todo: we do not currently check whether such lemmas actually should be additivized.
else if let some post := env.mainModule ++ `_auxLemma |>.isPrefixOf? src then
return env.mainModule ++ `_auxAddLemma ++ post
else if src.hasMacroScopes then
mkFreshUserName src.eraseMacroScopes
else
throwError "internal @[to_additive] error."
/-- Returns a `NameSet` of all auxiliary constants in `e` that might have been generated
when adding `pre` to the environment.
Examples include `pre.match_5`, `Mathlib.MyFile._auxLemma.3` and
`_private.Mathlib.MyFile.someOtherNamespace.someOtherDeclaration._eq_2`.
The last two examples may or may not have been generated by this declaration.
The last example may or may not be the equation lemma of a declaration with the `@[to_additive]`
attribute. We will only translate it has the `@[to_additive]` attribute.
-/
def findAuxDecls (e : Expr) (pre mainModule : Name) : NameSet :=
let auxLemma := mainModule ++ `_auxLemma
e.foldConsts ∅ fun n l ↦
if n.getPrefix == pre || n.getPrefix == auxLemma || isPrivateName n || n.hasMacroScopes then
l.insert n
else
l
/-- transform the declaration `src` and all declarations `pre._proof_i` occurring in `src`
using the transforms dictionary.
`replace_all`, `trace`, `ignore` and `reorder` are configuration options.
`pre` is the declaration that got the `@[to_additive]` attribute and `tgt_pre` is the target of this
declaration. -/
partial def transformDeclAux
(cfg : Config) (pre tgt_pre : Name) : Name → CoreM Unit := fun src ↦ do
let env ← getEnv
trace[to_additive_detail] "visiting {src}"
-- if we have already translated this declaration, we do nothing.
if (findTranslation? env src).isSome && src != pre then
return
-- if this declaration is not `pre` and not an internal declaration, we return an error,
-- since we should have already translated this declaration.
if src != pre && !src.isInternal' then
throwError "The declaration {pre} depends on the declaration {src} which is in the namespace {
pre}, but does not have the `@[to_additive]` attribute. This is not supported.\n{""
}Workaround: move {src} to a different namespace."
-- we find the additive name of `src`
let tgt ← findTargetName env src pre tgt_pre
-- we skip if we already transformed this declaration before.
if env.contains tgt then
if tgt == src then
-- Note: this can happen for equation lemmas of declarations without `@[to_additive]`.
trace[to_additive_detail] "Auxiliary declaration {src} will be translated to itself."
else
trace[to_additive_detail] "Already visited {tgt} as translation of {src}."
return
let srcDecl ← getConstInfo src
-- we first transform all auxiliary declarations generated when elaborating `pre`
for n in findAuxDecls srcDecl.type pre env.mainModule do
transformDeclAux cfg pre tgt_pre n
if let some value := srcDecl.value? then
for n in findAuxDecls value pre env.mainModule do
transformDeclAux cfg pre tgt_pre n
if let .opaqueInfo {value, ..} := srcDecl then
for n in findAuxDecls value pre env.mainModule do
transformDeclAux cfg pre tgt_pre n
-- if the auxiliary declaration doesn't have prefix `pre`, then we have to add this declaration
-- to the translation dictionary, since otherwise we cannot find the additive name.
if !pre.isPrefixOf src then
insertTranslation src tgt
-- now transform the source declaration
let trgDecl : ConstantInfo ←
MetaM.run' <| updateDecl tgt srcDecl <| if src == pre then cfg.reorder else []
let value ← match trgDecl with
| .thmInfo { value, .. } | .defnInfo { value, .. } | .opaqueInfo { value, .. } => pure value
| _ => throwError "Expected {tgt} to have a value."
trace[to_additive] "generating\n{tgt} : {trgDecl.type} :=\n {value}"
try
-- make sure that the type is correct,
-- and emit a more helpful error message if it fails
discard <| MetaM.run' <| inferType value
catch
| Exception.error _ msg => throwError "@[to_additive] failed.
Type mismatch in additive declaration. For help, see the docstring
of `to_additive.attr`, section `Troubleshooting`.
Failed to add declaration\n{tgt}:\n{msg}"
| _ => panic! "unreachable"
if isNoncomputable env src then
addDecl trgDecl.toDeclaration!
setEnv $ addNoncomputable (← getEnv) tgt
else
addAndCompile trgDecl.toDeclaration!
-- now add declaration ranges so jump-to-definition works
-- note: we currently also do this for auxiliary declarations, while they are not normally
-- generated for those. We could change that.
addDeclarationRanges tgt {
range := ← getDeclarationRange (← getRef)
selectionRange := ← getDeclarationRange cfg.ref }
if isProtected (← getEnv) src then
setEnv $ addProtected (← getEnv) tgt
/-- Copy the instance attribute in a `to_additive`
[todo] it seems not to work when the `to_additive` is added as an attribute later. -/
def copyInstanceAttribute (src tgt : Name) : CoreM Unit := do
if (← isInstance src) then
let prio := (← getInstancePriority? src).getD 100
let attr_kind := (← getInstanceAttrKind? src).getD .global
trace[to_additive_detail] "Making {tgt} an instance with priority {prio}."
addInstance tgt attr_kind prio |>.run'
/-- Warn the user when the multiplicative declaration has an attribute. -/
def warnExt [Inhabited σ] (stx : Syntax) (ext : PersistentEnvExtension α β σ) (f : σ → Name → Bool)
(thisAttr attrName src tgt : Name) : CoreM Unit := do
if f (ext.getState (← getEnv)) src then
Linter.logLintIf linter.existingAttributeWarning stx <|
m!"The source declaration {src} was given attribute {attrName} before calling @[{thisAttr}]. {
""}The preferred method is to use `@[{thisAttr} (attr := {attrName})]` to apply the {
""}attribute to both {src} and the target declaration {tgt}." ++
if thisAttr == `to_additive then
m!"\nSpecial case: If this declaration was generated by @[to_additive] {
""}itself, you can use @[to_additive (attr := to_additive, {attrName})] on the original {
""}declaration." else ""
/-- Warn the user when the multiplicative declaration has a simple scoped attribute. -/
def warnAttr [Inhabited β] (stx : Syntax) (attr : SimpleScopedEnvExtension α β)
(f : β → Name → Bool) (thisAttr attrName src tgt : Name) : CoreM Unit :=
warnExt stx attr.ext (f ·.stateStack.head!.state ·) thisAttr attrName src tgt
/-- Warn the user when the multiplicative declaration has a parametric attribute. -/
def warnParametricAttr (stx : Syntax) (attr : ParametricAttribute β)
(thisAttr attrName src tgt : Name) : CoreM Unit :=
warnExt stx attr.ext (·.contains ·) thisAttr attrName src tgt
/-- `runAndAdditivize names desc t` runs `t` on all elements of `names`
and adds translations between the generated lemmas (the output of `t`).
`names` must be non-empty. -/
def additivizeLemmas [Monad m] [MonadError m] [MonadLiftT CoreM m]
(names : Array Name) (desc : String) (t : Name → m (Array Name)) : m Unit := do
let auxLemmas ← names.mapM t
let nLemmas := auxLemmas[0]!.size
for (nm, lemmas) in names.zip auxLemmas do
unless lemmas.size == nLemmas do
throwError "{names[0]!} and {nm} do not generate the same number of {desc}."
for (srcLemmas, tgtLemmas) in auxLemmas.zip <| auxLemmas.eraseIdx 0 do
for (srcLemma, tgtLemma) in srcLemmas.zip tgtLemmas do
insertTranslation srcLemma tgtLemma
/--
Find the first argument of `nm` that has a multiplicative type-class on it.
Returns 1 if there are no types with a multiplicative class as arguments.
E.g. `Prod.Group` returns 1, and `Pi.One` returns 2.
Note: we only consider the first argument of each type-class.
E.g. `[Pow A N]` is a multiplicative type-class on `A`, not on `N`.
-/
def firstMultiplicativeArg (nm : Name) : MetaM Nat := do
forallTelescopeReducing (← getConstInfo nm).type fun xs _ ↦ do
-- xs are the arguments to the constant
let xs := xs.toList
let l ← xs.filterMapM fun x ↦ do
-- x is an argument and i is the index
-- write `x : (y₀ : α₀) → ... → (yₙ : αₙ) → tgt_fn tgt_args₀ ... tgt_argsₘ`
forallTelescopeReducing (← inferType x) fun _ys tgt ↦ do
let (_tgt_fn, tgt_args) := tgt.getAppFnArgs
if let some c := tgt.getAppFn.constName? then
if findTranslation? (← getEnv) c |>.isNone then
return none
return tgt_args[0]?.bind fun tgtArg ↦
xs.findIdx? fun x ↦ Expr.containsFVar tgtArg x.fvarId!
trace[to_additive_detail] "firstMultiplicativeArg: {l}"
match l with
| [] => return 0
| (head :: tail) => return tail.foldr Nat.min head
/-- Helper for `capitalizeLike`. -/
partial def capitalizeLikeAux (s : String) (i : String.Pos := 0) (p : String) : String :=
if p.atEnd i || s.atEnd i then
p
else
let j := p.next i
if (s.get i).isLower then
capitalizeLikeAux s j <| p.set i (p.get i |>.toLower)
else if (s.get i).isUpper then
capitalizeLikeAux s j <| p.set i (p.get i |>.toUpper)
else
capitalizeLikeAux s j p
/-- Capitalizes `s` char-by-char like `r`. If `s` is longer, it leaves the tail untouched. -/
def capitalizeLike (r : String) (s : String) :=
capitalizeLikeAux r 0 s
/-- Capitalize First element of a list like `s`.
Note that we need to capitalize multiple characters in some cases,
in examples like `HMul` or `hAdd`. -/
def capitalizeFirstLike (s : String) : List String → List String
| x :: r => capitalizeLike s x :: r
| [] => []
/--
Dictionary used by `guessName` to autogenerate names.
Note: `guessName` capitalizes first element of the output according to
capitalization of the input. Input and first element should therefore be lower-case,
2nd element should be capitalized properly.
-/
def nameDict : String → List String
| "one" => ["zero"]
| "mul" => ["add"]
| "smul" => ["vadd"]
| "inv" => ["neg"]
| "div" => ["sub"]
| "prod" => ["sum"]
| "hmul" => ["hadd"]
| "hsmul" => ["hvadd"]
| "hdiv" => ["hsub"]
| "hpow" => ["hsmul"]
| "finprod" => ["finsum"]
| "pow" => ["nsmul"]
| "npow" => ["nsmul"]
| "zpow" => ["zsmul"]
| "monoid" => ["add", "Monoid"]
| "submonoid" => ["add", "Submonoid"]
| "group" => ["add", "Group"]
| "subgroup" => ["add", "Subgroup"]
| "semigroup" => ["add", "Semigroup"]
| "magma" => ["add", "Magma"]
| "haar" => ["add", "Haar"]
| "prehaar" => ["add", "Prehaar"]
| "unit" => ["add", "Unit"]
| "units" => ["add", "Units"]
| "cyclic" => ["add", "Cyclic"]
| "rootable" => ["divisible"]
| "commute" => ["add", "Commute"]
| "semiconj" => ["add", "Semiconj"]
| x => [x]
/--
Turn each element to lower-case, apply the `nameDict` and
capitalize the output like the input.
-/
def applyNameDict : List String → List String
| x :: s => (capitalizeFirstLike x (nameDict x.toLower)) ++ applyNameDict s
| [] => []
/--
There are a few abbreviations we use. For example "Nonneg" instead of "ZeroLE"
or "addComm" instead of "commAdd".
Note: The input to this function is case sensitive!
Todo: A lot of abbreviations here are manual fixes and there might be room to
improve the naming logic to reduce the size of `fixAbbreviation`.
-/
def fixAbbreviation : List String → List String
| "cancel" :: "Add" :: s => "addCancel" :: fixAbbreviation s
| "Cancel" :: "Add" :: s => "AddCancel" :: fixAbbreviation s
| "left" :: "Cancel" :: "Add" :: s => "addLeftCancel" :: fixAbbreviation s
| "Left" :: "Cancel" :: "Add" :: s => "AddLeftCancel" :: fixAbbreviation s
| "right" :: "Cancel" :: "Add" :: s => "addRightCancel" :: fixAbbreviation s
| "Right" :: "Cancel" :: "Add" :: s => "AddRightCancel" :: fixAbbreviation s
| "cancel" :: "Comm" :: "Add" :: s => "addCancelComm" :: fixAbbreviation s
| "Cancel" :: "Comm" :: "Add" :: s => "AddCancelComm" :: fixAbbreviation s
| "comm" :: "Add" :: s => "addComm" :: fixAbbreviation s
| "Comm" :: "Add" :: s => "AddComm" :: fixAbbreviation s
| "Zero" :: "LE" :: s => "Nonneg" :: fixAbbreviation s
| "zero" :: "_" :: "le" :: s => "nonneg" :: fixAbbreviation s
| "Zero" :: "LT" :: s => "Pos" :: fixAbbreviation s
| "zero" :: "_" :: "lt" :: s => "pos" :: fixAbbreviation s
| "LE" :: "Zero" :: s => "Nonpos" :: fixAbbreviation s
| "le" :: "_" :: "zero" :: s => "nonpos" :: fixAbbreviation s
| "LT" :: "Zero" :: s => "Neg" :: fixAbbreviation s
| "lt" :: "_" :: "zero" :: s => "neg" :: fixAbbreviation s
| "Add" :: "Single" :: s => "Single" :: fixAbbreviation s
| "add" :: "Single" :: s => "single" :: fixAbbreviation s
| "add" :: "_" :: "single" :: s => "single" :: fixAbbreviation s
| "Add" :: "Support" :: s => "Support" :: fixAbbreviation s
| "add" :: "Support" :: s => "support" :: fixAbbreviation s
| "add" :: "_" :: "support" :: s => "support" :: fixAbbreviation s
| "Add" :: "TSupport" :: s => "TSupport" :: fixAbbreviation s
| "add" :: "TSupport" :: s => "tsupport" :: fixAbbreviation s
| "add" :: "_" :: "tsupport" :: s => "tsupport" :: fixAbbreviation s
| "Add" :: "Indicator" :: s => "Indicator" :: fixAbbreviation s
| "add" :: "Indicator" :: s => "indicator" :: fixAbbreviation s
| "add" :: "_" :: "indicator" :: s => "indicator" :: fixAbbreviation s
| "is" :: "Square" :: s => "even" :: fixAbbreviation s
| "Is" :: "Square" :: s => "Even" :: fixAbbreviation s
-- "Regular" is well-used in mathlib3 with various meanings (e.g. in
-- measure theory) and a direct translation
-- "regular" --> ["add", "Regular"] in `nameDict` above seems error-prone.
| "is" :: "Regular" :: s => "isAddRegular" :: fixAbbreviation s
| "Is" :: "Regular" :: s => "IsAddRegular" :: fixAbbreviation s
| "is" :: "Left" :: "Regular" :: s => "isAddLeftRegular" :: fixAbbreviation s
| "Is" :: "Left" :: "Regular" :: s => "IsAddLeftRegular" :: fixAbbreviation s
| "is" :: "Right" :: "Regular" :: s => "isAddRightRegular" :: fixAbbreviation s
| "Is" :: "Right" :: "Regular" :: s => "IsAddRightRegular" :: fixAbbreviation s
-- the capitalization heuristic of `applyNameDict` doesn't work in the following cases
| "HSmul" :: s => "HSMul" :: fixAbbreviation s -- from `HPow`
| "NSmul" :: s => "NSMul" :: fixAbbreviation s -- from `NPow`
| "Nsmul" :: s => "NSMul" :: fixAbbreviation s -- from `Pow`
| "ZSmul" :: s => "ZSMul" :: fixAbbreviation s -- from `ZPow`
| "neg" :: "Fun" :: s => "invFun" :: fixAbbreviation s
| "Neg" :: "Fun" :: s => "InvFun" :: fixAbbreviation s
| "unique" :: "Prods" :: s => "uniqueSums" :: fixAbbreviation s
| "Unique" :: "Prods" :: s => "UniqueSums" :: fixAbbreviation s
| "order" :: "Of" :: s => "addOrderOf" :: fixAbbreviation s
| "Order" :: "Of" :: s => "AddOrderOf" :: fixAbbreviation s
| "is"::"Of"::"Fin"::"Order"::s => "isOfFinAddOrder" :: fixAbbreviation s
| "Is"::"Of"::"Fin"::"Order"::s => "IsOfFinAddOrder" :: fixAbbreviation s
| "is" :: "Central" :: "Scalar" :: s => "isCentralVAdd" :: fixAbbreviation s
| "Is" :: "Central" :: "Scalar" :: s => "IsCentralVAdd" :: fixAbbreviation s
| "function" :: "_" :: "add" :: "Semiconj" :: s
=> "function" :: "_" :: "semiconj" :: fixAbbreviation s
| "function" :: "_" :: "add" :: "Commute" :: s
=> "function" :: "_" :: "commute" :: fixAbbreviation s
| x :: s => x :: fixAbbreviation s
| [] => []
/--
Autogenerate additive name.
This runs in several steps:
1) Split according to capitalisation rule and at `_`.
2) Apply word-by-word translation rules.
3) Fix up abbreviations that are not word-by-word translations, like "addComm" or "Nonneg".
-/
def guessName : String → String :=
String.mapTokens '\'' <|
fun s =>
String.join <|
fixAbbreviation <|
applyNameDict <|
s.splitCase
/-- Return the provided target name or autogenerate one if one was not provided. -/
def targetName (cfg : Config) (src : Name) : CoreM Name := do
let .str pre s := src | throwError "to_additive: can't transport {src}"
trace[to_additive_detail] "The name {s} splits as {s.splitCase}"
let tgt_auto := guessName s
let depth := cfg.tgt.getNumParts
let pre := pre.mapPrefix <| findTranslation? (← getEnv)
let (pre1, pre2) := pre.splitAt (depth - 1)
if cfg.tgt == pre2.str tgt_auto && !cfg.allowAutoName && cfg.tgt != src then
Linter.logLintIf linter.toAdditiveGenerateName cfg.ref
m!"to_additive correctly autogenerated target name for {src}. {"\n"
}You may remove the explicit argument {cfg.tgt}."
let res := if cfg.tgt == .anonymous then pre.str tgt_auto else pre1 ++ cfg.tgt
-- we allow translating to itself if `tgt == src`, which is occasionally useful for `additiveTest`
if res == src && cfg.tgt != src then
throwError "to_additive: can't transport {src} to itself."
if cfg.tgt != .anonymous then
trace[to_additive_detail] "The automatically generated name would be {pre.str tgt_auto}"
return res
/-- if `f src = #[a_1, ..., a_n]` and `f tgt = #[b_1, ... b_n]` then `proceedFieldsAux src tgt f`
will insert translations from `src.a_i` to `tgt.b_i`. -/
def proceedFieldsAux (src tgt : Name) (f : Name → CoreM (Array Name)) : CoreM Unit := do
let srcFields ← f src
let tgtFields ← f tgt
if srcFields.size != tgtFields.size then
throwError "Failed to map fields of {src}, {tgt} with {srcFields} ↦ {tgtFields}"
for (srcField, tgtField) in srcFields.zip tgtFields do
if srcField != tgtField then
insertTranslation (src ++ srcField) (tgt ++ tgtField)
else
trace[to_additive] "Translation {src ++ srcField} ↦ {tgt ++ tgtField} is automatic."
/-- Add the structure fields of `src` to the translations dictionary
so that future uses of `to_additive` will map them to the corresponding `tgt` fields. -/
def proceedFields (src tgt : Name) : CoreM Unit := do
let aux := proceedFieldsAux src tgt
aux fun declName ↦ do
if isStructure (← getEnv) declName then
return getStructureFields (← getEnv) declName
else
return #[]
aux fun declName ↦ do match (← getEnv).find? declName with
| some (ConstantInfo.inductInfo {ctors := ctors, ..}) => return ctors.toArray.map (·.getString)
| _ => pure #[]
/-- Elaboration of the configuration options for `to_additive`. -/
def elabToAdditive : Syntax → CoreM Config
| `(attr| to_additive%$tk $[?%$trace]? $[$opts:toAdditiveOption]* $[$tgt]? $[$doc]?) => do
let mut attrs := #[]
let mut reorder := []
let mut existing := some false
for stx in opts do
match stx with
| `(toAdditiveOption| (attr := $[$stxs],*)) =>
attrs := attrs ++ stxs
| `(toAdditiveOption| (reorder := $[$[$reorders:num]*],*)) =>
reorder := reorder ++ reorders.toList.map (·.toList.map (·.raw.isNatLit?.get! - 1))
| `(toAdditiveOption| existing) =>
existing := some true
| _ => throwUnsupportedSyntax
reorder := reorder.reverse
trace[to_additive_detail] "attributes: {attrs}; reorder arguments: {reorder}"
return { trace := trace.isSome
tgt := match tgt with | some tgt => tgt.getId | none => Name.anonymous
doc := doc.bind (·.raw.isStrLit?)
allowAutoName := false
attrs
reorder
existing
ref := (tgt.map (·.raw)).getD tk }
| _ => throwUnsupportedSyntax
mutual
/-- Apply attributes to the multiplicative and additive declarations. -/
partial def applyAttributes (stx : Syntax) (rawAttrs : Array Syntax) (thisAttr src tgt : Name) :
TermElabM (Array Name) := do
-- we only copy the `instance` attribute, since `@[to_additive] instance` is nice to allow
copyInstanceAttribute src tgt
-- Warn users if the multiplicative version has an attribute
if linter.existingAttributeWarning.get (← getOptions) then
let appliedAttrs ← getAllSimpAttrs src
if appliedAttrs.size > 0 then
Linter.logLintIf linter.existingAttributeWarning stx <|
m!"The source declaration {src} was given the simp-attribute(s) {appliedAttrs} before {
""}calling @[{thisAttr}]. The preferred method is to use {
""}`@[{thisAttr} (attr := {appliedAttrs})]` to apply the attribute to both {
src} and the target declaration {tgt}."
warnAttr stx Std.Tactic.Ext.extExtension
(fun b n => (b.tree.values.any fun t => t.declName = n)) thisAttr `ext src tgt
warnAttr stx Std.Tactic.reflExt (·.values.contains ·) thisAttr `refl src tgt
warnAttr stx Std.Tactic.symmExt (·.values.contains ·) thisAttr `symm src tgt
warnAttr stx Mathlib.Tactic.transExt (·.values.contains ·) thisAttr `trans src tgt
warnAttr stx Std.Tactic.Coe.coeExt (·.contains ·) thisAttr `coe src tgt
warnParametricAttr stx Lean.Linter.deprecatedAttr thisAttr `deprecated src tgt
-- the next line also warns for `@[to_additive, simps]`, because of the application times
warnParametricAttr stx simpsAttr thisAttr `simps src tgt
warnExt stx Term.elabAsElim.ext (·.contains ·) thisAttr `elab_as_elim src tgt
-- add attributes
-- the following is similar to `Term.ApplyAttributesCore`, but we hijack the implementation of
-- `simp`, `simps` and `to_additive`.
let attrs ← elabAttrs rawAttrs
let (additiveAttrs, attrs) := attrs.partition (·.name == `to_additive)
let nestedDecls ←
match additiveAttrs.size with
| 0 => pure #[]
| 1 => addToAdditiveAttr tgt (← elabToAdditive additiveAttrs[0]!.stx) additiveAttrs[0]!.kind
| _ => throwError "cannot apply {thisAttr} multiple times."
let allDecls := #[src, tgt] ++ nestedDecls
if attrs.size > 0 then
trace[to_additive_detail] "Applying attributes {attrs.map (·.stx)} to {allDecls}"
for attr in attrs do
withRef attr.stx do withLogging do
-- todo: also support other simp-attributes,
-- and attributes that generate simp-attributes, like `norm_cast`
if attr.name == `simp then
additivizeLemmas allDecls "simp lemmas"
(Meta.Simp.addSimpAttrFromSyntax · simpExtension attr.kind attr.stx)
return
if attr.name == `simps then
additivizeLemmas allDecls "simps lemmas" (simpsTacFromSyntax · attr.stx)
return
let env ← getEnv
match getAttributeImpl env attr.name with
| Except.error errMsg => throwError errMsg
| Except.ok attrImpl =>
let runAttr := do
attrImpl.add src attr.stx attr.kind
attrImpl.add tgt attr.stx attr.kind
-- not truly an elaborator, but a sensible target for go-to-definition
let elaborator := attrImpl.ref
if (← getInfoState).enabled && (← getEnv).contains elaborator then
withInfoContext (mkInfo := return .ofCommandInfo { elaborator, stx := attr.stx }) do
try runAttr
finally if attr.stx[0].isIdent || attr.stx[0].isAtom then
-- Add an additional node over the leading identifier if there is one
-- to make it look more function-like.
-- Do this last because we want user-created infos to take precedence
pushInfoLeaf <| .ofCommandInfo { elaborator, stx := attr.stx[0] }
else
runAttr
return nestedDecls
/--
Copies equation lemmas and attributes from `src` to `tgt`
-/
partial def copyMetaData (cfg : Config) (src tgt : Name) : CoreM (Array Name) := do
if let some eqns := eqnsAttribute.find? (← getEnv) src then
unless (eqnsAttribute.find? (← getEnv) tgt).isSome do
for eqn in eqns do _ ← addToAdditiveAttr eqn cfg
eqnsAttribute.add tgt (eqns.map (findTranslation? (← getEnv) · |>.get!))
else
/- We need to generate all equation lemmas for `src` and `tgt`, even for non-recursive
definitions. If we don't do that, the equation lemma for `src` might be generated later
when doing a `rw`, but it won't be generated for `tgt`. -/
additivizeLemmas #[src, tgt] "equation lemmas" fun nm ↦
(·.getD #[]) <$> MetaM.run' (getEqnsFor? nm true)
MetaM.run' <| Elab.Term.TermElabM.run' <|
applyAttributes cfg.ref cfg.attrs `to_additive src tgt
/--
Make a new copy of a declaration, replacing fragments of the names of identifiers in the type and
the body using the `translations` dictionary.
This is used to implement `@[to_additive]`.
-/
partial def transformDecl (cfg : Config) (src tgt : Name) : CoreM (Array Name) := do