@@ -49,6 +49,10 @@ macro "to_additive!?" rest:to_additiveRest : attr => `(attr| to_additive ! ? $re
49
49
/-- The `to_additive` attribute. -/
50
50
macro "to_additive?!" rest:to_additiveRest : attr => `(attr| to_additive ! ? $rest)
51
51
52
+ /-- A set of strings of names that are written in all-caps. -/
53
+ def allCapitalNames : RBTree String compare :=
54
+ .ofList ["LE" , "LT" , "WF" ]
55
+
52
56
/--
53
57
This function takes a String and splits it into separate parts based on the following
54
58
(naming conventions)[ https://github.com/leanprover-community/mathlib4/wiki#naming-convention ] .
@@ -59,31 +63,17 @@ E.g. `#eval "InvHMulLEConjugate₂Smul_ne_top".splitCase` yields
59
63
partial def String.splitCase (s : String) (i₀ : Pos := 0 ) (r : List String := []) : List String :=
60
64
-- We test if we need to split between `i₀` and `i₁`.
61
65
let i₁ := s.next i₀
62
- let i₂ := s.next i₁
63
66
if s.atEnd i₁ then
64
67
-- If `i₀` is the last position, return the list.
65
68
let r := s::r
66
69
r.reverse
67
- -- First, we split on both sides of `_` to keep them there when rejoining the string.
68
- else if (s.get i₀ = '_' ) || (s.get i₁ = '_' ) then
69
- let r := (s.extract 0 i₁)::r
70
- splitCase (s.extract i₁ s.endPos) 0 r
71
- -- Otherwise, we only ever split when there is an upper case at `i₁`.
72
- else if (s.get i₁).isUpper then
73
- -- There are two cases we need to split:
74
- if (s.get i₀).isUpper then
75
- -- 1) If `i₀` and `i₁` are upper, `i₂` is not upper, and `i₀ > 0`.
76
- -- This prevents single capital letters being split.
77
- -- Example: Splits `LEOne`to `LE`, `One` but leaves `HMul` together.
78
- if (i₀ ≠ 0 ) && ¬((s.get i₂).isUpper) then
79
- let r := (s.extract 0 i₁)::r
80
- splitCase (s.extract i₁ s.endPos) 0 r
81
- else
82
- splitCase s i₁ r
83
- -- 2) Upper `i₁` is preceeded by non-upper `i₀`.
84
- else
85
- let r := (s.extract 0 i₁)::r
86
- splitCase (s.extract i₁ s.endPos) 0 r
70
+ /- We split the string in three cases
71
+ * We split on both sides of `_` to keep them there when rejoining the string;
72
+ * We split after a sequence of capital letters in `allCapitalNames`;
73
+ * We split after a lower-case letter that is followed by an upper-case letter. -/
74
+ else if s.get i₀ == '_' || s.get i₁ == '_' ||
75
+ ((s.get i₁).isUpper && (!(s.get i₀).isUpper || allCapitalNames.contains (s.extract 0 i₁))) then
76
+ splitCase (s.extract i₁ s.endPos) 0 <| (s.extract 0 i₁)::r
87
77
else
88
78
splitCase s i₁ r
89
79
@@ -107,8 +97,7 @@ initialize ignoreArgsAttr : NameMapExtension (List Nat) ←
107
97
let ids ← match stx with
108
98
| `(attr| to_additive_ignore_args $[$ids:num]*) => pure <| ids.map (·.1 .isNatLit?.get!)
109
99
| _ => throwUnsupportedSyntax
110
- return ids.toList
111
- }
100
+ return ids.toList }
112
101
113
102
/-- Gets the set of arguments that should be ignored for the given name
114
103
(according to `@[to_additive_ignore_args ...]`).
@@ -407,7 +396,7 @@ partial def transformDeclAux
407
396
return
408
397
let srcDecl ← getConstInfo src
409
398
let prefixes : NameSet := .ofList [pre, env.mainModule ++ `_auxLemma]
410
- -- we first transform all auxilliary declarations generated when elaborating `pre`
399
+ -- we first transform all auxiliary declarations generated when elaborating `pre`
411
400
for n in srcDecl.type.listNamesWithPrefixes prefixes do
412
401
transformDeclAux none pre tgt_pre n
413
402
if let some value := srcDecl.value? then
@@ -541,8 +530,7 @@ structure ValueType : Type where
541
530
deriving Repr
542
531
543
532
/-- Helper for `capitalizeLike`. -/
544
- partial def capitalizeLikeAux (s : String) (i : String.Pos := 0 ) : String → String :=
545
- fun p =>
533
+ partial def capitalizeLikeAux (s : String) (i : String.Pos := 0 ) (p : String) : String :=
546
534
if p.atEnd i || s.atEnd i then
547
535
p
548
536
else
@@ -558,7 +546,9 @@ partial def capitalizeLikeAux (s : String) (i : String.Pos := 0) : String → S
558
546
def capitalizeLike (r : String) (s : String) :=
559
547
capitalizeLikeAux r 0 s
560
548
561
- /-- Capitalize First element of a list like `s`. -/
549
+ /-- Capitalize First element of a list like `s`.
550
+ Note that we need to capitalize multiple characters in some cases,
551
+ in examples like `HMul` or `hAdd`. -/
562
552
def capitalizeFirstLike (s : String) : List String → List String
563
553
| x :: r => capitalizeLike s x :: r
564
554
| [] => []
@@ -578,10 +568,11 @@ private def nameDict : String → List String
578
568
| "div" => ["sub" ]
579
569
| "prod" => ["sum" ]
580
570
| "hmul" => ["hadd" ]
571
+ | "hsmul" => ["hvadd" ]
581
572
| "hdiv" => ["hsub" ]
582
- | "hpow" => ["hmul " ]
573
+ | "hpow" => ["hsmul " ]
583
574
| "finprod" => ["finsum" ]
584
- | "pow" => ["nsmul " ]
575
+ | "pow" => ["smul " ]
585
576
| "npow" => ["nsmul" ]
586
577
| "zpow" => ["zsmul" ]
587
578
| "monoid" => ["add" , "Monoid" ]
@@ -634,19 +625,16 @@ def fixAbbreviation : List String → List String
634
625
| "Add" :: "Support" :: s => "Support" :: fixAbbreviation s
635
626
| "add" :: "Support" :: s => "support" :: fixAbbreviation s
636
627
| "add" :: "_" :: "support" :: s => "support" :: fixAbbreviation s
637
- -- TODO: Is it `TSupport` or `Tsupport`?
638
628
| "Add" :: "TSupport" :: s => "TSupport" :: fixAbbreviation s
639
629
| "add" :: "TSupport" :: s => "tsupport" :: fixAbbreviation s
640
630
| "add" :: "_" :: "tsupport" :: s => "tsupport" :: fixAbbreviation s
641
631
| "Add" :: "Indicator" :: s => "Indicator" :: fixAbbreviation s
642
632
| "add" :: "Indicator" :: s => "indicator" :: fixAbbreviation s
643
633
| "add" :: "_" :: "indicator" :: s => "indicator" :: fixAbbreviation s
644
- -- TODO: Bug in `splitCase` splits like ["LEH", "Pow"] instead of ["LE", "HPow"].
645
- -- Currently we just fix these cases manually.
646
- | "HNsmul" :: s => "HMul" :: fixAbbreviation s
647
- | "hnsmul" :: s => "hmul" :: fixAbbreviation s
648
- | "Zero" :: "LEH" :: s => "NonnegH" :: fixAbbreviation s
649
- | "Zero" :: "LTH" :: s => "PosH" :: fixAbbreviation s
634
+ -- the capitalization heuristic of `applyNameDict` doesn't work in the following cases
635
+ | "Smul" :: s => "SMul" :: fixAbbreviation s
636
+ | "HSmul" :: s => "HSMul" :: fixAbbreviation s
637
+ | "hSmul" :: s => "hSMul" :: fixAbbreviation s
650
638
| x :: s => x :: fixAbbreviation s
651
639
| [] => []
652
640
0 commit comments