-
Notifications
You must be signed in to change notification settings - Fork 1
/
Notation.lean
352 lines (292 loc) · 14.1 KB
/
Notation.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
import Lean.Parser import Lean.Parser.Command
import Lean.Elab.Command import Lean.PrettyPrinter
import Lean.Elab.DeclUtil
import Lean.Elab.PreDefinition
open Lean.Parser Lean.Parser.Term Lean.Elab.Command
open Lean.PrettyPrinter.Delaborator
namespace GroundZero.Meta.Notation
open Lean in def delabCustomSort (t₀ : Delab) (t : Syntax.Level → Delab) : Delab :=
whenPPOption Lean.getPPNotation do {
let ε ← SubExpr.getExpr; let n := ε.constLevels!.get! 0;
if n.isZero then t₀ else t (n.quote max_prec)
}
@[app_unexpander Nat.succ]
def natSuccUnexpander : Lean.PrettyPrinter.Unexpander
| `($_ $n) => `($n + 1)
| _ => throw ()
@[app_unexpander Eq]
def revertDefaultEq : Lean.PrettyPrinter.Unexpander
| `($t $a $b) => `($t $a $b)
| _ => throw ()
@[delab sort]
def revertDefaultProp : Delab := do {
match (← delabSort) with
| `(Prop) => `(Sort 0)
| stx => return stx
}
macro "variables " vs:(ppSpace bracketedBinder)* : command => `(variable $vs*)
syntax "Π " many1(ppSpace (binderIdent <|> bracketedBinder)) ", " term : term
macro_rules | `(Π $xs*, $y) => `(∀ $xs*, $y)
-- https://github.com/leanprover-community/mathlib4/blob/f8593acbc38a592e6eeda6ce0d26383798de1b32/Mathlib/Util/Delaborators.lean#L79-L90
@[delab forallE]
def delabPi : Delab := whenPPOption Lean.getPPNotation do {
match (← delabForall) with
| `($group:bracketedBinder → Π $groups*, $body) => `(Π $group $groups*, $body)
| `($group:bracketedBinder → $body) => `(Π $group, $body)
| stx => pure stx
}
macro "λ " xs:(ppSpace funBinder)+ ", " f:term : term => `(fun $xs* => $f)
@[delab lam]
def delabLambda : Delab := whenPPOption Lean.getPPNotation do {
match (← delabLam) with
| `(fun $group:funBinder => λ $groups:funBinder*, $body) => `(λ $group $groups*, $body)
| `(fun $group:funBinder => $body) => `(λ $group, $body)
| stx => pure stx;
}
section
open Lean.TSyntax.Compat
@[delab app.Sigma]
def delabSig : Delab := whenPPOption Lean.getPPNotation do {
match (← delabSigma) with
| `($group:bracketedExplicitBinders × Σ $groups*, $body) => `(Σ $group:bracketedExplicitBinders $groups*, $body)
| `($group:bracketedExplicitBinders × $body) => `(Σ $group:bracketedExplicitBinders, $body)
| stx => pure stx
}
end
macro "begin " ts:sepBy(tactic, ";", "; ", allowTrailingSep) i:"end" : term =>
`(by { $[($ts:tactic)]* }%$i)
declare_syntax_cat superscriptNumeral
syntax "⁰" : superscriptNumeral
syntax "¹" : superscriptNumeral
syntax "²" : superscriptNumeral
syntax "³" : superscriptNumeral
syntax "⁴" : superscriptNumeral
syntax "⁵" : superscriptNumeral
syntax "⁶" : superscriptNumeral
syntax "⁷" : superscriptNumeral
syntax "⁸" : superscriptNumeral
syntax "⁹" : superscriptNumeral
def parseSupNumeral : Lean.Syntax → Nat
| `(superscriptNumeral| ⁰) => 0
| `(superscriptNumeral| ¹) => 1
| `(superscriptNumeral| ²) => 2
| `(superscriptNumeral| ³) => 3
| `(superscriptNumeral| ⁴) => 4
| `(superscriptNumeral| ⁵) => 5
| `(superscriptNumeral| ⁶) => 6
| `(superscriptNumeral| ⁷) => 7
| `(superscriptNumeral| ⁸) => 8
| `(superscriptNumeral| ⁹) => 9
| _ => 0
declare_syntax_cat subscriptNumeral
syntax "₀" : subscriptNumeral
syntax "₁" : subscriptNumeral
syntax "₂" : subscriptNumeral
syntax "₃" : subscriptNumeral
syntax "₄" : subscriptNumeral
syntax "₅" : subscriptNumeral
syntax "₆" : subscriptNumeral
syntax "₇" : subscriptNumeral
syntax "₈" : subscriptNumeral
syntax "₉" : subscriptNumeral
def parseSubNumeral : Lean.Syntax → Nat
| `(subscriptNumeral| ₀) => 0
| `(subscriptNumeral| ₁) => 1
| `(subscriptNumeral| ₂) => 2
| `(subscriptNumeral| ₃) => 3
| `(subscriptNumeral| ₄) => 4
| `(subscriptNumeral| ₅) => 5
| `(subscriptNumeral| ₆) => 6
| `(subscriptNumeral| ₇) => 7
| `(subscriptNumeral| ₈) => 8
| `(subscriptNumeral| ₉) => 9
| _ => 0
def parseNumber (φ : Lean.Syntax → Nat) (stx : Array Lean.Syntax) :=
let ns := Array.mapIdx stx.reverse (fun i s => φ s * 10 ^ i.val)
let n := Array.foldl (· + ·) 0 ns
Lean.Syntax.mkNumLit (toString n)
def parseSupNumber := parseNumber parseSupNumeral
def parseSubNumber := parseNumber parseSubNumeral
declare_syntax_cat superscriptChar
syntax "ᵃ" : superscriptChar syntax "ᵇ" : superscriptChar
syntax "ᶜ" : superscriptChar syntax "ᵈ" : superscriptChar
syntax "ᵉ" : superscriptChar syntax "ᶠ" : superscriptChar
syntax "ᵍ" : superscriptChar syntax "ʰ" : superscriptChar
syntax "ⁱ" : superscriptChar syntax "ʲ" : superscriptChar
syntax "ᵏ" : superscriptChar syntax "ˡ" : superscriptChar
syntax "ᵐ" : superscriptChar syntax "ⁿ" : superscriptChar
syntax "ᵒ" : superscriptChar syntax "ᵖ" : superscriptChar
syntax "ʳ" : superscriptChar syntax "ˢ" : superscriptChar
syntax "ᵗ" : superscriptChar syntax "ᵘ" : superscriptChar
syntax "ᵛ" : superscriptChar syntax "ʷ" : superscriptChar
syntax "ˣ" : superscriptChar syntax "ʸ" : superscriptChar
syntax "ᴬ" : superscriptChar syntax "ᴮ" : superscriptChar
syntax "ᴰ" : superscriptChar syntax "ᴱ" : superscriptChar
syntax "ᴳ" : superscriptChar syntax "ᴴ" : superscriptChar
syntax "ᴵ" : superscriptChar syntax "ᴶ" : superscriptChar
syntax "ᴷ" : superscriptChar syntax "ᴸ" : superscriptChar
syntax "ᴹ" : superscriptChar syntax "ᴺ" : superscriptChar
syntax "ᴼ" : superscriptChar syntax "ᴾ" : superscriptChar
syntax "ᴿ" : superscriptChar syntax "ᵀ" : superscriptChar
syntax "ᵁ" : superscriptChar syntax "ⱽ" : superscriptChar
syntax "ᵂ" : superscriptChar
def parseSupChar : Lean.Syntax → Char
| `(superscriptChar| ᵃ) => 'a' | `(superscriptChar| ᵇ) => 'b'
| `(superscriptChar| ᶜ) => 'c' | `(superscriptChar| ᵈ) => 'd'
| `(superscriptChar| ᵉ) => 'e' | `(superscriptChar| ᶠ) => 'f'
| `(superscriptChar| ᵍ) => 'g' | `(superscriptChar| ʰ) => 'h'
| `(superscriptChar| ⁱ) => 'i' | `(superscriptChar| ʲ) => 'j'
| `(superscriptChar| ᵏ) => 'k' | `(superscriptChar| ˡ) => 'l'
| `(superscriptChar| ᵐ) => 'm' | `(superscriptChar| ⁿ) => 'n'
| `(superscriptChar| ᵒ) => 'o' | `(superscriptChar| ᵖ) => 'p'
| `(superscriptChar| ʳ) => 'r' | `(superscriptChar| ˢ) => 's'
| `(superscriptChar| ᵗ) => 't' | `(superscriptChar| ᵘ) => 'u'
| `(superscriptChar| ᵛ) => 'v' | `(superscriptChar| ʷ) => 'w'
| `(superscriptChar| ˣ) => 'x' | `(superscriptChar| ʸ) => 'y'
| `(superscriptChar| ᴬ) => 'A' | `(superscriptChar| ᴮ) => 'B'
| `(superscriptChar| ᴰ) => 'D' | `(superscriptChar| ᴱ) => 'E'
| `(superscriptChar| ᴳ) => 'G' | `(superscriptChar| ᴴ) => 'H'
| `(superscriptChar| ᴵ) => 'I' | `(superscriptChar| ᴶ) => 'J'
| `(superscriptChar| ᴷ) => 'K' | `(superscriptChar| ᴸ) => 'L'
| `(superscriptChar| ᴹ) => 'M' | `(superscriptChar| ᴺ) => 'N'
| `(superscriptChar| ᴼ) => 'O' | `(superscriptChar| ᴾ) => 'P'
| `(superscriptChar| ᴿ) => 'R' | `(superscriptChar| ᵀ) => 'T'
| `(superscriptChar| ᵁ) => 'U' | `(superscriptChar| ⱽ) => 'V'
| `(superscriptChar| ᵂ) => 'W' | _ => 'A'
declare_syntax_cat subscriptChar
syntax "ₐ" : subscriptChar syntax "ₑ" : subscriptChar
syntax "ₕ" : subscriptChar syntax "ᵢ" : subscriptChar
syntax "ⱼ" : subscriptChar syntax "ₖ" : subscriptChar
syntax "ₗ" : subscriptChar syntax "ₘ" : subscriptChar
syntax "ₙ" : subscriptChar syntax "ₒ" : subscriptChar
syntax "ₚ" : subscriptChar syntax "ᵣ" : subscriptChar
syntax "ₛ" : subscriptChar syntax "ₜ" : subscriptChar
syntax "ᵤ" : subscriptChar syntax "ᵥ" : subscriptChar
syntax "ₓ" : subscriptChar
def parseSubChar : Lean.Syntax → Char
| `(subscriptChar| ₐ) => 'a' | `(subscriptChar| ₑ) => 'e'
| `(subscriptChar| ₕ) => 'h' | `(subscriptChar| ᵢ) => 'i'
| `(subscriptChar| ⱼ) => 'j' | `(subscriptChar| ₖ) => 'k'
| `(subscriptChar| ₗ) => 'l' | `(subscriptChar| ₘ) => 'm'
| `(subscriptChar| ₙ) => 'n' | `(subscriptChar| ₒ) => 'o'
| `(subscriptChar| ₚ) => 'p' | `(subscriptChar| ᵣ) => 'r'
| `(subscriptChar| ₛ) => 's' | `(subscriptChar| ₜ) => 't'
| `(subscriptChar| ᵤ) => 'u' | `(subscriptChar| ᵥ) => 'v'
| `(subscriptChar| ₓ) => 'x' | _ => 'a'
def parseIdent (φ : Lean.Syntax → Char) (stx : Array Lean.Syntax) :=
String.mk (Array.map φ stx).toList |> Lean.Name.str Lean.Name.anonymous
def parseSubIdent := parseIdent parseSubChar
def parseSupIdent := parseIdent parseSupChar
declare_syntax_cat superscript
syntax (name := superscriptNumber) many1(superscriptNumeral) : superscript
syntax (name := superscriptIdent) many1(superscriptChar) : superscript
syntax:65 superscript:65 "⁺" superscript:66 : superscript
syntax:65 superscript:65 "⁻" superscript:66 : superscript
syntax:max "⁽" superscript "⁾" : superscript
partial def parseSuperscript : Lean.Syntax → Lean.MacroM Lean.Term
| `(superscriptNumber| $stx*) => return parseSupNumber stx
| `(superscriptIdent| $stx*) => return Lean.mkIdent (parseSupIdent stx)
| `(superscript| $stx₁ ⁺ $stx₂) => do `(HAdd.hAdd $(← parseSuperscript stx₁) $(← parseSuperscript stx₂))
| `(superscript| $stx₁ ⁻ $stx₂) => do `(HSub.hSub $(← parseSuperscript stx₁) $(← parseSuperscript stx₂))
| `(superscript| ⁽$stx⁾) => parseSuperscript stx
| stx => Lean.Macro.throwError "invalid superscript"
declare_syntax_cat subscript
syntax (name := subscriptNumber) many1(subscriptNumeral) : subscript
syntax (name := subscriptIdent) many1(subscriptChar) : subscript
syntax:65 subscript:65 "₊" subscript:66 : subscript
syntax:65 subscript:65 "₋" subscript:66 : subscript
syntax:max "₍" subscript "₎" : subscript
partial def parseSubscript : Lean.Syntax → Lean.MacroM Lean.Term
| `(subscriptNumber| $stx*) => return parseSubNumber stx
| `(subscriptIdent| $stx*) => return Lean.mkIdent (parseSubIdent stx)
| `(subscript| $stx₁ ₊ $stx₂) => do `(HAdd.hAdd $(← parseSubscript stx₁) $(← parseSubscript stx₂))
| `(subscript| $stx₁ ₋ $stx₂) => do `(HSub.hSub $(← parseSubscript stx₁) $(← parseSubscript stx₂))
| `(subscript| ₍$stx₎) => parseSubscript stx
| stx => Lean.Macro.throwError "invalid subscript"
namespace Record
open Lean
def expandBinderIdent : Syntax → CommandElabM Name
| `(Lean.binderIdent| $i:ident) => return i.getId
| stx => throwErrorAt stx "expected ident"
def expandBEBinder : Syntax → TSyntax `term × TSyntaxArray `Lean.binderIdent
| `(bracketedExplicitBinders| ($[$xs]* : $t)) => (t, xs)
| _ => default
def withImplicits {α : Type} (xs : Array Expr) (k : MetaM α) : MetaM α :=
do {
let mut lctx ← getLCtx;
for x in xs do {
if (lctx.get! x.fvarId!).binderInfo.isExplicit then {
lctx := lctx.setBinderInfo x.fvarId! BinderInfo.implicit;
}
}
withReader (λ ctx => {ctx with lctx := lctx}) k
}
def declAccessor (tname fname : Name) (t e : Expr) (lparams : List Name) : Elab.Command.CommandElabM Unit :=
do {
let bvar ← Elab.Term.mkFreshBinderName;
let lam ← liftTermElabM <|
Meta.forallTelescope t λ xs _ =>
do mkAppN (mkConst tname (lparams.map Level.param)) xs
|> (mkLambda bvar BinderInfo.default · e)
|> Meta.mkLambdaFVars xs
|> withImplicits xs;
let typ ← liftTermElabM (Meta.inferType lam);
Elab.Command.liftCoreM <| addAndCompile <| Declaration.defnDecl {
name := tname ++ fname,
levelParams := lparams,
type := typ,
value := lam,
hints := ReducibilityHints.abbrev,
safety := DefinitionSafety.safe
}
}
abbrev sigfst := mkProj ``Sigma 0
abbrev sigsnd := mkProj ``Sigma 1
elab "record " id:declId sig:optDeclSig " :=" ppSpace fields:(ppSpace bracketedExplicitBinders)+ : command =>
do {
unless (fields.size > 0) do throwErrorAt id "empty record is disallowed";
let (e, is) := expandBEBinder fields.back;
let term ← if is.size > 1 then `(Σ $(fields.pop)* ($is.pop* : $e), $e)
else `(Σ $(fields.pop)*, $e);
elabCommand (← `(def $id $sig := $term));
if (← get).messages.hasErrors then return;
let ns ← getCurrNamespace;
let tname := ns ++ (id.raw.getArg 0).getId;
let ci ← getConstInfo tname;
let ids ← fields.concatMapM λ stx =>
Array.mapM expandBinderIdent
(expandBEBinder stx).2;
let mut acc := mkBVar 0;
for ident in ids.pop do {
declAccessor tname ident ci.type (sigfst acc) ci.levelParams;
acc := sigsnd acc;
}
declAccessor tname ids.back ci.type acc ci.levelParams
}
end Record
namespace Defeq
open Lean
def elabTypeSpec {ks : SyntaxNodeKinds} :=
Option.mapM (λ (stx : TSyntax ks), Elab.Term.elabType stx.raw[1])
elab "#success " σ₁:term tag:" ≡ " σ₂:term τ:(typeSpec)? : command =>
Elab.Command.runTermElabM fun _ => do {
let T ← elabTypeSpec τ;
let τ₁ ← Elab.Term.elabTermEnsuringType σ₁ T;
let τ₂ ← Elab.Term.elabTermEnsuringType σ₂ T;
Elab.Term.synthesizeSyntheticMVarsNoPostponing;
let τ₁ ← Elab.Term.levelMVarToParam (← instantiateMVars τ₁);
let τ₂ ← Elab.Term.levelMVarToParam (← instantiateMVars τ₂);
unless (← Meta.isDefEqGuarded τ₁ τ₂) do throwErrorAt tag s!"{← Meta.ppExpr τ₁} ≠ {← Meta.ppExpr τ₂}"
}
elab "#failure " σ₁:term tag:" ≡ " σ₂:term τ:(typeSpec)? : command =>
Elab.Command.runTermElabM fun _ => do {
let T ← elabTypeSpec τ;
let τ₁ ← Elab.Term.elabTermEnsuringType σ₁ T;
let τ₂ ← Elab.Term.elabTermEnsuringType σ₂ T;
Elab.Term.synthesizeSyntheticMVarsNoPostponing;
let τ₁ ← Elab.Term.levelMVarToParam (← instantiateMVars τ₁);
let τ₂ ← Elab.Term.levelMVarToParam (← instantiateMVars τ₂);
if (← Meta.isDefEqGuarded τ₁ τ₂) then throwErrorAt tag s!"{← Meta.ppExpr τ₁} ≡ {← Meta.ppExpr τ₂}"
}
end Defeq
end GroundZero.Meta.Notation