Skip to content

Commit 10fdfc5

Browse files
authored
chore: upstream HSMul notation typeclass (#8401)
Upstreaming the `HSMul` notation typeclass, to enable `grind` to process goals using it.
1 parent 943a9c6 commit 10fdfc5

File tree

10 files changed

+66
-88
lines changed

10 files changed

+66
-88
lines changed

src/Init/Notation.lean

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ recommended_spelling "PProd" for "×'" in [PProd, «term_×'_»]
295295
@[inherit_doc] prefix:75 "-" => Neg.neg
296296
@[inherit_doc] prefix:100 "~~~" => Complement.complement
297297
@[inherit_doc] postfix:max "⁻¹" => Inv.inv
298+
@[inherit_doc] infixr:73 " • " => HSMul.hSMul
298299

299300
/-!
300301
Remark: the infix commands above ensure a delaborator is generated for each relations.
@@ -312,6 +313,40 @@ macro_rules | `($x % $y) => `(binop% HMod.hMod $x $y)
312313
macro_rules | `($x ^ $y) => `(rightact% HPow.hPow $x $y)
313314
macro_rules | `($x ++ $y) => `(binop% HAppend.hAppend $x $y)
314315
macro_rules | `(- $x) => `(unop% Neg.neg $x)
316+
/-!
317+
We have a macro to make `x • y` notation participate in the expression tree elaborator,
318+
like other arithmetic expressions such as `+`, `*`, `/`, `^`, `=`, inequalities, etc.
319+
The macro is using the `leftact%` elaborator introduced in
320+
[this RFC](https://github.com/leanprover/lean4/issues/2854).
321+
322+
As a concrete example of the effect of this macro, consider
323+
```lean
324+
variable [Ring R] [AddCommMonoid M] [Module R M] (r : R) (N : Submodule R M) (m : M) (n : N)
325+
#check m + r • n
326+
```
327+
Without the macro, the expression would elaborate as `m + ↑(r • n : ↑N) : M`.
328+
With the macro, the expression elaborates as `m + r • (↑n : M) : M`.
329+
To get the first interpretation, one can write `m + (r • n :)`.
330+
331+
Here is a quick review of the expression tree elaborator:
332+
1. It builds up an expression tree of all the immediately accessible operations
333+
that are marked with `binop%`, `unop%`, `leftact%`, `rightact%`, `binrel%`, etc.
334+
2. It elaborates every leaf term of this tree
335+
(without an expected type, so as if it were temporarily wrapped in `(... :)`).
336+
3. Using the types of each elaborated leaf, it computes a supremum type they can all be
337+
coerced to, if such a supremum exists.
338+
4. It inserts coercions around leaf terms wherever needed.
339+
340+
The hypothesis is that individual expression trees tend to be calculations with respect
341+
to a single algebraic structure.
342+
343+
Note(kmill): If we were to remove `HSMul` and switch to using `SMul` directly,
344+
then the expression tree elaborator would not be able to insert coercions within the right operand;
345+
they would likely appear as `↑(x • y)` rather than `x • ↑y`, unlike other arithmetic operations.
346+
-/
347+
348+
@[inherit_doc HSMul.hSMul]
349+
macro_rules | `($x • $y) => `(leftact% HSMul.hSMul $x $y)
315350

316351
recommended_spelling "or" for "|||" in [HOr.hOr, «term_|||_»]
317352
recommended_spelling "xor" for "^^^" in [HXor.hXor, «term_^^^_»]
@@ -323,6 +358,7 @@ recommended_spelling "mul" for "*" in [HMul.hMul, «term_*_»]
323358
recommended_spelling "div" for "/" in [HDiv.hDiv, «term_/_»]
324359
recommended_spelling "mod" for "%" in [HMod.hMod, «term_%_»]
325360
recommended_spelling "pow" for "^" in [HPow.hPow, «term_^_»]
361+
recommended_spelling "smul" for "•" in [HSMul.hSMul, «term_•_»]
326362
recommended_spelling "append" for "++" in [HAppend.hAppend, «term_++_»]
327363
/-- when used as a unary operator -/
328364
recommended_spelling "neg" for "-" in [Neg.neg, «term-_»]

src/Init/Prelude.lean

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,23 @@ class HPow (α : Type u) (β : Type v) (γ : outParam (Type w)) where
13481348
The meaning of this notation is type-dependent. -/
13491349
hPow : α → β → γ
13501350

1351+
/--
1352+
The notation typeclass for heterogeneous scalar multiplication.
1353+
This enables the notation `a • b : γ` where `a : α`, `b : β`.
1354+
1355+
It is assumed to represent a left action in some sense.
1356+
The notation `a • b` is augmented with a macro (below) to have it elaborate as a left action.
1357+
Only the `b` argument participates in the elaboration algorithm: the algorithm uses the type of `b`
1358+
when calculating the type of the surrounding arithmetic expression
1359+
and it tries to insert coercions into `b` to get some `b'`
1360+
such that `a • b'` has the same type as `b'`.
1361+
See the module documentation near the macro for more details.
1362+
-/
1363+
class HSMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
1364+
/-- `a • b` computes the product of `a` and `b`.
1365+
The meaning of this notation is type-dependent, but it is intended to be used for left actions. -/
1366+
hSMul : α → β → γ
1367+
13511368
/--
13521369
The notation typeclass for heterogeneous append.
13531370
This enables the notation `a ++ b : γ` where `a : α`, `b : β`.
@@ -1510,6 +1527,12 @@ class HomogeneousPow (α : Type u) where
15101527
/-- `a ^ b` computes `a` to the power of `b` where `a` and `b` both have the same type. -/
15111528
protected pow : α → α → α
15121529

1530+
/-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/
1531+
class SMul (M : Type u) (α : Type v) where
1532+
/-- `a • b` computes the product of `a` and `b`. The meaning of this notation is type-dependent,
1533+
but it is intended to be used for left actions. -/
1534+
smul : M → α → α
1535+
15131536
/-- The homogeneous version of `HAppend`: `a ++ b : α` where `a b : α`. -/
15141537
class Append (α : Type u) where
15151538
/-- `a ++ b` is the result of concatenation of `a` and `b`. See `HAppend`. -/
@@ -1601,6 +1624,13 @@ instance instPowNat [NatPow α] : Pow α Nat where
16011624
instance [HomogeneousPow α] : Pow α α where
16021625
pow a b := HomogeneousPow.pow a b
16031626

1627+
@[default_instance]
1628+
instance instHSMul {α β} [SMul α β] : HSMul α β β where
1629+
hSMul := SMul.smul
1630+
1631+
instance (priority := 910) {α : Type u} [Mul α] : SMul α α where
1632+
smul x y := Mul.mul x y
1633+
16041634
@[default_instance]
16051635
instance [Append α] : HAppend α α α where
16061636
hAppend a b := Append.append a b

tests/lean/run/2461.lean

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,6 @@ end algebra_hierarchy_morphisms
9393

9494
section HSMul_stuff
9595

96-
class HSMul (α : Type) (β : Type) (γ : outParam Type) where
97-
hSMul : α → β → γ
98-
99-
class SMul (M : Type) (α : Type) where
100-
smul : M → α → α
101-
102-
infixr:73 "" => HSMul.hSMul
103-
104-
instance instHSMul {α β : Type} [SMul α β] : HSMul α β β where
105-
hSMul := SMul.smul
106-
10796
-- note that the function `SMulZeroClass.toSMul` is what disrupts `simp` later
10897
class SMulZeroClass (M A : Type) extends SMul M A where
10998

tests/lean/run/3807.lean

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -751,19 +751,6 @@ universe u v w
751751

752752
open Function
753753

754-
class HSMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
755-
hSMul : α → β → γ
756-
757-
class SMul (M : Type u) (α : Type v) where
758-
smul : M → α → α
759-
760-
infixr:73 " • " => HSMul.hSMul
761-
762-
macro_rules | `($x • $y) => `(leftact% HSMul.hSMul $x $y)
763-
764-
instance instHSMul {α β} [SMul α β] : HSMul α β β where
765-
hSMul := SMul.smul
766-
767754
variable {G : Type _}
768755

769756
class Semigroup (G : Type u) extends Mul G where

tests/lean/run/binop_binrel_perf_issue.lean

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,10 @@ universe u v w
153153
class HVAdd (α : Type u) (β : Type v) (γ : outParam (Type w)) where
154154
hVAdd : α → β → γ
155155

156-
class HSMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
157-
hSMul : α → β → γ
158-
159156
class VAdd (G : Type u) (P : Type v) where
160157
vadd : G → P → P
161158

162-
class SMul (M : Type u) (α : Type v) where
163-
smul : M → α → α
164-
165159
infixl:65 " +ᵥ " => HVAdd.hVAdd
166-
infixr:73 "" => HSMul.hSMul
167-
168-
macro_rules | `($x • $y) => `(leftact% HSMul.hSMul $x $y)
169-
170-
instance instHSMul {α β} [SMul α β] : HSMul α β β where
171-
hSMul := SMul.smul
172160

173161
instance instHVAdd {α β} [VAdd α β] : HVAdd α β β where
174162
hVAdd := VAdd.vadd

tests/lean/run/dsimpNatLitIssue.lean

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ class Bot (α : Type) where
88
/-- The bot (`⊥`, `\bot`) element -/
99
notation "⊥" => Bot.bot
1010

11-
/-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/
12-
class SMul (M α : Type) where
13-
smul : M → α → α
14-
15-
infixr:73 " • " => SMul.smul
16-
1711
structure Submodule (R : Type) (M : Type) [Zero M] [SMul R M] where
1812
carrier : M → Prop
1913
zero_mem : carrier (0 : M)

tests/lean/run/linearCategory_perf_issue.lean

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@ universe u v w v₁ v₂ v₃ u₁ u₂ u₃
22

33
section Mathlib.Algebra.Group.Defs
44

5-
class HSMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
6-
hSMul : α → β → γ
7-
8-
class SMul (M : Type u) (α : Type v) where
9-
smul : M → α → α
10-
11-
infixr:73 "" => SMul.smul
12-
13-
macro_rules | `($x • $y) => `(leftact% HSMul.hSMul $x $y)
14-
15-
instance instHSMul {α β} [SMul α β] : HSMul α β β where
16-
hSMul := SMul.smul
17-
185
class AddMonoid (M : Type u) extends Add M, Zero M where
196
protected add_assoc : ∀ a b c : M, a + b + c = a + (b + c)
207
protected zero_add : ∀ a : M, 0 + a = a

tests/lean/run/nestedtc.lean

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ variable {R : Type}
33

44
class Zip (α : Type) -- represents `Zero`
55

6-
class SMul (R : Type) (α : Type) where
7-
smul : R → α → α
8-
9-
infixr:73 "" => SMul.smul
10-
116
class MulAction (R : Type) (β : Type) extends SMul R β
127

138
class SMulZeroClass (R α : Type) extends SMul R α where

tests/lean/run/structWithAlgTCSynth.lean

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,6 @@ end Mathlib.Logic.Function.Basic
101101

102102
section Mathlib.Algebra.Group.Defs
103103

104-
class HSMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
105-
hSMul : α → β → γ
106-
107-
class SMul (M : Type _) (α : Type _) where
108-
smul : M → α → α
109-
110-
infixr:73 "" => HSMul.hSMul
111-
112-
instance instHSMul [SMul α β] : HSMul α β β where
113-
hSMul := SMul.smul
114-
115104
class Semigroup (G : Type u) extends Mul G where
116105
mul_assoc : ∀ a b c : G, a * b * c = a * (b * c)
117106

tests/lean/run/synthInstsIssue.lean

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,6 @@ https://github.com/leanprover/lean4/pull/2793.
44
We find that we need to either specify a named argument or use `..` in certain rewrites.
55
-/
66

7-
section Mathlib.Algebra.Group.Defs
8-
9-
universe u v w
10-
11-
class HSMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
12-
hSMul : α → β → γ
13-
14-
class SMul (M : Type u) (α : Type v) where
15-
smul : M → α → α
16-
17-
infixr:73 " • " => HSMul.hSMul
18-
19-
instance instHSMul {α β} [SMul α β] : HSMul α β β where
20-
hSMul := SMul.smul
21-
22-
end Mathlib.Algebra.Group.Defs
23-
247
section Mathlib.Data.FunLike.Basic
258

269
class DFunLike (F : Sort _) (α : outParam (Sort _)) (β : outParam <| α → Sort _) where

0 commit comments

Comments
 (0)