@@ -32,8 +32,6 @@ already appears in the input.
3232
3333This 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ₙ}]
7068end toExpr
7169
7270section 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
10098macro_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+
117136end Parser
118137
119138variable (a b : ℕ)
0 commit comments