Skip to content

Commit d718815

Browse files
committed
feat(Mathlib/Data/Matrix/Notation): delaborator for !![a, b; c, d] notation (#14376)
1 parent 9389bd0 commit d718815

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Mathlib/Data/Matrix/Notation.lean

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ already appears in the input.
3232
3333
This file provide notation `!![a, b; c, d]` for matrices, which corresponds to
3434
`Matrix.of ![![a, b], ![c, d]]`.
35-
TODO: until we implement a `Lean.PrettyPrinter.Unexpander` for `Matrix.of`, the pretty-printer will
36-
not show `!!` notation, instead showing the version with `of ![![...]]`.
3735
3836
## Examples
3937
@@ -70,7 +68,7 @@ protected instance toExpr [ToLevel.{u}] [ToLevel.{uₘ}] [ToLevel.{uₙ}]
7068
end toExpr
7169

7270
section Parser
73-
open Lean Elab Term Macro TSyntax
71+
open Lean Meta Elab Term Macro TSyntax PrettyPrinter.Delaborator SubExpr
7472

7573
/-- Notation for m×n matrices, aka `Matrix (Fin m) (Fin n) α`.
7674
@@ -93,9 +91,9 @@ syntax (name := matrixNotation)
9391
"!![" ppRealGroup(sepBy1(ppGroup(term,+,?), ";", "; ", allowTrailingSep)) "]" : term
9492

9593
@[inherit_doc matrixNotation]
96-
syntax (name := matrixNotationRx0) "!![" ";"* "]" : term
94+
syntax (name := matrixNotationRx0) "!![" ";"+ "]" : term
9795
@[inherit_doc matrixNotation]
98-
syntax (name := matrixNotation0xC) "!![" ","+ "]" : term
96+
syntax (name := matrixNotation0xC) "!![" ","* "]" : term
9997

10098
macro_rules
10199
| `(!![$[$[$rows],*];*]) => do
@@ -114,6 +112,27 @@ macro_rules
114112
`(@Matrix.of (Fin $(quote semicolons.size)) (Fin 0) _ ![$emptyVecs,*])
115113
| `(!![$[,%$commas]*]) => `(@Matrix.of (Fin 0) (Fin $(quote commas.size)) _ ![])
116114

115+
/-- Delaborator for the `!![]` notation. -/
116+
@[delab app.DFunLike.coe]
117+
def delabMatrixNotation : Delab := whenNotPPOption getPPExplicit <| whenPPOption getPPNotation <|
118+
withOverApp 6 do
119+
let mkApp3 (.const ``Matrix.of _) (.app (.const ``Fin _) em) (.app (.const ``Fin _) en) _ :=
120+
(← getExpr).appFn!.appArg! | failure
121+
let some m ← withNatValue em (pure ∘ some) | failure
122+
let some n ← withNatValue en (pure ∘ some) | failure
123+
withAppArg do
124+
if m = 0 then
125+
guard <| (← getExpr).isAppOfArity ``vecEmpty 1
126+
let commas := mkArray n (mkAtom ",")
127+
`(!![$[,%$commas]*])
128+
else
129+
if n = 0 then
130+
let `(![$[![]%$evecs],*]) ← delab | failure
131+
`(!![$[;%$evecs]*])
132+
else
133+
let `(![$[![$[$melems],*]],*]) ← delab | failure
134+
`(!![$[$[$melems],*];*])
135+
117136
end Parser
118137

119138
variable (a b : ℕ)

test/matrix.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ end safety
7878
#guard !![1,2;3,4;] = of ![![1,2], ![3,4]]
7979
#guard !![1,2,;3,4,] = of ![![1,2], ![3,4]]
8080

81+
/-- info: !![0, 1, 2; 3, 4, 5] : Matrix (Fin 2) (Fin 3) ℕ -/
82+
#guard_msgs in #check (!![0, 1, 2; 3, 4, 5] : Matrix (Fin 2) (Fin 3) ℕ)
83+
84+
/-- info: !![0, 1, 2; 3, 4, 5] 1 1 : ℕ -/
85+
#guard_msgs in #check (!![0, 1, 2; 3, 4, 5] : Matrix (Fin 2) (Fin 3) ℕ) 1 1
86+
87+
/-- info: !![,,,] : Matrix (Fin 0) (Fin 3) ℕ -/
88+
#guard_msgs in #check (!![,,,] : Matrix (Fin 0) (Fin 3) ℕ)
89+
90+
/-- info: !![;;;] : Matrix (Fin 3) (Fin 0) ℕ -/
91+
#guard_msgs in #check (!![;;;] : Matrix (Fin 3) (Fin 0) ℕ)
92+
93+
/-- info: !![] : Matrix (Fin 0) (Fin 0) ℕ -/
94+
#guard_msgs in #check (!![] : Matrix (Fin 0) (Fin 0) ℕ)
95+
8196
example {a a' b b' c c' d d' : α} :
8297
!![a, b; c, d] + !![a', b'; c', d'] = !![a + a', b + b'; c + c', d + d'] := by
8398
simp

0 commit comments

Comments
 (0)