@aufbau/syntax 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/LICENSE +202 -0
- package/README.md +118 -0
- package/dist/diagnostics.d.ts +26 -0
- package/dist/diagnostics.js +23 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/elab.d.ts +47 -0
- package/dist/elab.js +234 -0
- package/dist/elab.js.map +1 -0
- package/dist/engine.d.ts +20 -0
- package/dist/engine.js +40 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/parse.d.ts +113 -0
- package/dist/parse.js +896 -0
- package/dist/parse.js.map +1 -0
- package/dist/print.d.ts +28 -0
- package/dist/print.js +183 -0
- package/dist/print.js.map +1 -0
- package/dist/reader/annotations.d.ts +92 -0
- package/dist/reader/annotations.js +198 -0
- package/dist/reader/annotations.js.map +1 -0
- package/dist/reader/lex.d.ts +37 -0
- package/dist/reader/lex.js +123 -0
- package/dist/reader/lex.js.map +1 -0
- package/dist/reader/spec.d.ts +88 -0
- package/dist/reader/spec.js +423 -0
- package/dist/reader/spec.js.map +1 -0
- package/dist/reader/statements.d.ts +115 -0
- package/dist/reader/statements.js +577 -0
- package/dist/reader/statements.js.map +1 -0
- package/dist/scan.d.ts +61 -0
- package/dist/scan.js +96 -0
- package/dist/scan.js.map +1 -0
- package/dist/term.d.ts +48 -0
- package/dist/term.js +28 -0
- package/dist/term.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* From parsed statements to a `Spec` — the library's central object: the
|
|
3
|
+
* signature, notations, coercions, and `@syntax` surface metadata of one
|
|
4
|
+
* logical system, validated.
|
|
5
|
+
*
|
|
6
|
+
* Validation here is about the spec being *coherent* (token conflicts,
|
|
7
|
+
* dangling names, non-invertible elab rules); whether the theory proves
|
|
8
|
+
* anything is the engine's business, and axiom bodies pass through opaque.
|
|
9
|
+
*/
|
|
10
|
+
import { diagnostic } from "../diagnostics.js";
|
|
11
|
+
import { parseSyntaxAnnotation, } from "./annotations.js";
|
|
12
|
+
import { parseStatements, } from "./statements.js";
|
|
13
|
+
const TEMPLATES = {
|
|
14
|
+
duplicate_declaration: "{name} is already declared",
|
|
15
|
+
unknown_sort: "sort {name} is not declared",
|
|
16
|
+
unknown_term: "term {name} is not declared",
|
|
17
|
+
infix_arity: "an infix operator needs exactly two regular arguments",
|
|
18
|
+
unknown_notation_variable: "notation variable {name} is not one of the declaration's binders",
|
|
19
|
+
token_conflict: "token {token} already has a meaning; a token may carry one parse rule",
|
|
20
|
+
precedence_mixed_associativity: "precedence {prec} mixes infixl and infixr operators",
|
|
21
|
+
grouping_token_conflict: "{token} is a grouping bracket and cannot also be a notation token",
|
|
22
|
+
elab_duplicate_capture: "capture {name} appears twice in the pattern",
|
|
23
|
+
elab_unknown_reference: "the template references {name}, which the pattern does not capture",
|
|
24
|
+
elab_not_invertible: "capture {name} is not used exactly once in the template; mark the rule input-only if that is intended",
|
|
25
|
+
juxtaposed_target: "@syntax juxtaposed must sit on a binary term whose arguments and result share one sort",
|
|
26
|
+
juxtaposed_needs_notation: "a juxtaposed term needs a declared notation, so the engine can read it",
|
|
27
|
+
juxtaposed_duplicate: "sort {sort} already has a juxtaposed combiner",
|
|
28
|
+
elided_target: "@syntax elided must sit on a term with no arguments",
|
|
29
|
+
elided_duplicate: "sort {sort} already has an elided term",
|
|
30
|
+
vars_term_conflict: "@vars token {token} is also a declared term; a name can be only one",
|
|
31
|
+
role_target: "@syntax role must sit on a term",
|
|
32
|
+
};
|
|
33
|
+
function report(diagnostics, id, params, span) {
|
|
34
|
+
diagnostics.push(diagnostic(id, TEMPLATES[id] ?? id, params, span));
|
|
35
|
+
}
|
|
36
|
+
/** The binder count an application must fill (bound and regular alike). */
|
|
37
|
+
function regularBinders(binders) {
|
|
38
|
+
return binders.filter((binder) => !binder.binds);
|
|
39
|
+
}
|
|
40
|
+
function binderSortOf(binder) {
|
|
41
|
+
return "sort" in binder.type ? binder.type.sort : "";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* One line per parse rule a token can carry. Two identical lines are a
|
|
45
|
+
* pointless duplicate but not an ambiguity; anything else sharing a token
|
|
46
|
+
* is a conflict.
|
|
47
|
+
*/
|
|
48
|
+
function meaningOf(notation) {
|
|
49
|
+
if (notation.form === "simple") {
|
|
50
|
+
return {
|
|
51
|
+
key: `${notation.fixity}:${String(notation.prec)}`,
|
|
52
|
+
term: notation.term,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const first = notation.literals[0];
|
|
56
|
+
if (first === undefined || first.kind !== "constant") {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return { key: "general", term: notation.term };
|
|
60
|
+
}
|
|
61
|
+
export function parseSpec(source) {
|
|
62
|
+
const { statements, diagnostics: parseDiagnostics } = parseStatements(source);
|
|
63
|
+
const diagnostics = [...parseDiagnostics];
|
|
64
|
+
const sorts = new Map();
|
|
65
|
+
const terms = new Map();
|
|
66
|
+
const coercions = [];
|
|
67
|
+
const notations = [];
|
|
68
|
+
const elabRules = [];
|
|
69
|
+
const lints = [];
|
|
70
|
+
const assocNone = new Set();
|
|
71
|
+
const delimitersLeft = new Set();
|
|
72
|
+
const delimitersRight = new Set();
|
|
73
|
+
const groupingPairs = [["(", ")"]];
|
|
74
|
+
let dropOuterParens = false;
|
|
75
|
+
let rotateBrackets = null;
|
|
76
|
+
const attached = [];
|
|
77
|
+
for (const statement of statements) {
|
|
78
|
+
const foreign = [];
|
|
79
|
+
for (const annotation of statement.annotations) {
|
|
80
|
+
const parsed = parseSyntaxAnnotation(annotation.text, annotation.span);
|
|
81
|
+
if (parsed === null) {
|
|
82
|
+
foreign.push(annotation);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (parsed.diagnostic !== null) {
|
|
86
|
+
diagnostics.push(parsed.diagnostic);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
attached.push({
|
|
90
|
+
annotation: parsed.annotation,
|
|
91
|
+
span: annotation.span,
|
|
92
|
+
statement,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
switch (statement.kind) {
|
|
96
|
+
case "sort": {
|
|
97
|
+
if (sorts.has(statement.name) || terms.has(statement.name)) {
|
|
98
|
+
report(diagnostics, "duplicate_declaration", { name: statement.name }, statement.span);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
// The engine's `@vars` pools are this library's variable lexicon
|
|
102
|
+
// too; the lines stay foreign (they are the engine's to keep).
|
|
103
|
+
const vars = [];
|
|
104
|
+
for (const annotation of foreign) {
|
|
105
|
+
const match = /^@vars\s+(.+)$/.exec(annotation.text.trim());
|
|
106
|
+
if (match !== null) {
|
|
107
|
+
vars.push(...(match[1] ?? "").split(/\s+/).filter((w) => w.length > 0));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
sorts.set(statement.name, {
|
|
111
|
+
foreignAnnotations: foreign,
|
|
112
|
+
modifiers: statement.modifiers,
|
|
113
|
+
name: statement.name,
|
|
114
|
+
span: statement.span,
|
|
115
|
+
vars,
|
|
116
|
+
});
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "term":
|
|
120
|
+
case "def": {
|
|
121
|
+
if (sorts.has(statement.name) || terms.has(statement.name)) {
|
|
122
|
+
report(diagnostics, "duplicate_declaration", { name: statement.name }, statement.span);
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
const returnSort = statement.kind === "term"
|
|
126
|
+
? (statement.returnChain[statement.returnChain.length - 1]
|
|
127
|
+
?.sort ?? "")
|
|
128
|
+
: statement.returnType.sort;
|
|
129
|
+
// Arrow sugar on a term (`a > b > c`) desugars to extra anonymous
|
|
130
|
+
// regular binders so arity means one thing everywhere downstream.
|
|
131
|
+
const extra = statement.kind === "term"
|
|
132
|
+
? statement.returnChain.slice(0, -1).map((type) => ({
|
|
133
|
+
binds: false,
|
|
134
|
+
dummy: false,
|
|
135
|
+
name: "_",
|
|
136
|
+
span: type.span,
|
|
137
|
+
type,
|
|
138
|
+
}))
|
|
139
|
+
: [];
|
|
140
|
+
terms.set(statement.name, {
|
|
141
|
+
binders: [...statement.binders, ...extra],
|
|
142
|
+
elided: false,
|
|
143
|
+
foreignAnnotations: foreign,
|
|
144
|
+
isDef: statement.kind === "def",
|
|
145
|
+
juxtaposed: false,
|
|
146
|
+
name: statement.name,
|
|
147
|
+
returnSort,
|
|
148
|
+
roles: [],
|
|
149
|
+
span: statement.span,
|
|
150
|
+
});
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "coercion": {
|
|
154
|
+
coercions.push({
|
|
155
|
+
from: statement.from,
|
|
156
|
+
name: statement.name,
|
|
157
|
+
span: statement.span,
|
|
158
|
+
to: statement.to,
|
|
159
|
+
});
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case "delimiter": {
|
|
163
|
+
for (const ch of statement.left) {
|
|
164
|
+
delimitersLeft.add(ch);
|
|
165
|
+
}
|
|
166
|
+
for (const ch of statement.right) {
|
|
167
|
+
delimitersRight.add(ch);
|
|
168
|
+
}
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case "simple-notation": {
|
|
172
|
+
notations.push({
|
|
173
|
+
form: "simple",
|
|
174
|
+
fixity: statement.fixity,
|
|
175
|
+
prec: statement.prec,
|
|
176
|
+
span: statement.span,
|
|
177
|
+
term: statement.term,
|
|
178
|
+
token: statement.token,
|
|
179
|
+
});
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case "notation": {
|
|
183
|
+
notations.push({
|
|
184
|
+
form: "general",
|
|
185
|
+
binders: statement.binders,
|
|
186
|
+
literals: statement.literals,
|
|
187
|
+
span: statement.span,
|
|
188
|
+
term: statement.term,
|
|
189
|
+
});
|
|
190
|
+
for (const literal of statement.literals) {
|
|
191
|
+
if (literal.kind === "variable" &&
|
|
192
|
+
!statement.binders.some((b) => b.name === literal.name)) {
|
|
193
|
+
report(diagnostics, "unknown_notation_variable", { name: literal.name }, literal.span);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case "axiom":
|
|
199
|
+
case "theorem":
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// ── Second pass: interpret @syntax annotations now that names resolve.
|
|
204
|
+
const juxtaposedBySort = new Map();
|
|
205
|
+
const elidedBySort = new Map();
|
|
206
|
+
for (const { annotation, span, statement } of attached) {
|
|
207
|
+
switch (annotation.kind) {
|
|
208
|
+
case "juxtaposed": {
|
|
209
|
+
const info = statement.kind === "term" || statement.kind === "def"
|
|
210
|
+
? terms.get(statement.name)
|
|
211
|
+
: undefined;
|
|
212
|
+
const regular = info === undefined ? [] : regularBinders(info.binders);
|
|
213
|
+
const sort = info?.returnSort ?? "";
|
|
214
|
+
if (info === undefined ||
|
|
215
|
+
info.binders.length !== 2 ||
|
|
216
|
+
regular.length !== 2 ||
|
|
217
|
+
regular.some((binder) => binderSortOf(binder) !== sort)) {
|
|
218
|
+
report(diagnostics, "juxtaposed_target", {}, span);
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
if (!notations.some((notation) => notation.term === info.name)) {
|
|
222
|
+
report(diagnostics, "juxtaposed_needs_notation", {}, span);
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
if (juxtaposedBySort.has(sort)) {
|
|
226
|
+
report(diagnostics, "juxtaposed_duplicate", { sort }, span);
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
juxtaposedBySort.set(sort, info.name);
|
|
230
|
+
terms.set(info.name, { ...info, juxtaposed: true });
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case "elided": {
|
|
234
|
+
const info = statement.kind === "term" || statement.kind === "def"
|
|
235
|
+
? terms.get(statement.name)
|
|
236
|
+
: undefined;
|
|
237
|
+
if (info === undefined || regularBinders(info.binders).length !== 0) {
|
|
238
|
+
report(diagnostics, "elided_target", {}, span);
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
if (elidedBySort.has(info.returnSort)) {
|
|
242
|
+
report(diagnostics, "elided_duplicate", { sort: info.returnSort }, span);
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
elidedBySort.set(info.returnSort, info.name);
|
|
246
|
+
terms.set(info.name, { ...info, elided: true });
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "role": {
|
|
250
|
+
if (statement.kind !== "term" && statement.kind !== "def") {
|
|
251
|
+
report(diagnostics, "role_target", {}, span);
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
const info = terms.get(statement.name);
|
|
255
|
+
if (info !== undefined) {
|
|
256
|
+
terms.set(statement.name, {
|
|
257
|
+
...info,
|
|
258
|
+
roles: [...info.roles, annotation.role],
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
case "brackets": {
|
|
264
|
+
for (const pair of annotation.pairs) {
|
|
265
|
+
const exists = groupingPairs.some(([open, close]) => open === pair[0] && close === pair[1]);
|
|
266
|
+
if (!exists) {
|
|
267
|
+
groupingPairs.push(pair);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case "assoc-none": {
|
|
273
|
+
assocNone.add(annotation.prec);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case "lint": {
|
|
277
|
+
if (!lints.includes(annotation.name)) {
|
|
278
|
+
lints.push(annotation.name);
|
|
279
|
+
}
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
case "display-drop-outer-parens": {
|
|
283
|
+
dropOuterParens = true;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case "display-rotate-brackets": {
|
|
287
|
+
rotateBrackets = annotation.pairs;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
case "elab": {
|
|
291
|
+
elabRules.push(annotation.rule);
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
// ── Validation over the collected spec.
|
|
297
|
+
for (const info of terms.values()) {
|
|
298
|
+
for (const binder of info.binders) {
|
|
299
|
+
if ("sort" in binder.type && !sorts.has(binder.type.sort)) {
|
|
300
|
+
report(diagnostics, "unknown_sort", { name: binder.type.sort }, binder.type.span);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (info.returnSort !== "" && !sorts.has(info.returnSort)) {
|
|
304
|
+
report(diagnostics, "unknown_sort", { name: info.returnSort }, info.span);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
for (const sort of sorts.values()) {
|
|
308
|
+
for (const token of sort.vars) {
|
|
309
|
+
if (terms.has(token)) {
|
|
310
|
+
report(diagnostics, "vars_term_conflict", { token }, sort.span);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
for (const coercion of coercions) {
|
|
315
|
+
if (!terms.has(coercion.name)) {
|
|
316
|
+
report(diagnostics, "unknown_term", { name: coercion.name }, coercion.span);
|
|
317
|
+
}
|
|
318
|
+
for (const sort of [coercion.from, coercion.to]) {
|
|
319
|
+
if (!sorts.has(sort)) {
|
|
320
|
+
report(diagnostics, "unknown_sort", { name: sort }, coercion.span);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const tokenMeanings = new Map();
|
|
325
|
+
for (const notation of notations) {
|
|
326
|
+
const info = terms.get(notation.term);
|
|
327
|
+
if (info === undefined) {
|
|
328
|
+
report(diagnostics, "unknown_term", { name: notation.term }, notation.span);
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (notation.form === "simple" &&
|
|
332
|
+
notation.fixity !== "prefix" &&
|
|
333
|
+
(regularBinders(info.binders).length !== 2 ||
|
|
334
|
+
info.binders.some((b) => b.binds))) {
|
|
335
|
+
report(diagnostics, "infix_arity", {}, notation.span);
|
|
336
|
+
}
|
|
337
|
+
const token = notation.form === "simple"
|
|
338
|
+
? notation.token
|
|
339
|
+
: notation.literals[0]?.kind === "constant"
|
|
340
|
+
? notation.literals[0].token
|
|
341
|
+
: null;
|
|
342
|
+
const meaning = meaningOf(notation);
|
|
343
|
+
if (token === null || meaning === null) {
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const existing = tokenMeanings.get(token);
|
|
347
|
+
if (existing !== undefined) {
|
|
348
|
+
if (existing.key !== meaning.key || existing.term !== meaning.term) {
|
|
349
|
+
report(diagnostics, "token_conflict", { token }, notation.span);
|
|
350
|
+
}
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
tokenMeanings.set(token, meaning);
|
|
354
|
+
}
|
|
355
|
+
const precFixities = new Map();
|
|
356
|
+
for (const notation of notations) {
|
|
357
|
+
if (notation.form === "simple" &&
|
|
358
|
+
notation.fixity !== "prefix" &&
|
|
359
|
+
typeof notation.prec === "number") {
|
|
360
|
+
const set = precFixities.get(notation.prec) ?? new Set();
|
|
361
|
+
set.add(notation.fixity);
|
|
362
|
+
precFixities.set(notation.prec, set);
|
|
363
|
+
if (set.size > 1) {
|
|
364
|
+
report(diagnostics, "precedence_mixed_associativity", { prec: String(notation.prec) }, notation.span);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const [open, close] of groupingPairs) {
|
|
369
|
+
for (const token of [open, close]) {
|
|
370
|
+
if (tokenMeanings.has(token)) {
|
|
371
|
+
report(diagnostics, "grouping_token_conflict", { token }, {
|
|
372
|
+
start: 0,
|
|
373
|
+
end: 0,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
for (const rule of elabRules) {
|
|
379
|
+
const captures = new Map();
|
|
380
|
+
for (const element of rule.pattern) {
|
|
381
|
+
if (element.kind === "capture") {
|
|
382
|
+
if (captures.has(element.name)) {
|
|
383
|
+
report(diagnostics, "elab_duplicate_capture", { name: element.name }, rule.span);
|
|
384
|
+
}
|
|
385
|
+
captures.set(element.name, 0);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
for (const element of rule.template) {
|
|
389
|
+
if (element.kind === "reference") {
|
|
390
|
+
const count = captures.get(element.name);
|
|
391
|
+
if (count === undefined) {
|
|
392
|
+
report(diagnostics, "elab_unknown_reference", { name: element.name }, rule.span);
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
captures.set(element.name, count + 1);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
if (!rule.inputOnly) {
|
|
399
|
+
for (const [name, count] of captures) {
|
|
400
|
+
if (count !== 1) {
|
|
401
|
+
report(diagnostics, "elab_not_invertible", { name }, rule.span);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return {
|
|
407
|
+
diagnostics,
|
|
408
|
+
spec: {
|
|
409
|
+
assocNone,
|
|
410
|
+
coercions,
|
|
411
|
+
delimiters: { left: delimitersLeft, right: delimitersRight },
|
|
412
|
+
display: { dropOuterParens, rotateBrackets },
|
|
413
|
+
groupingPairs,
|
|
414
|
+
lints,
|
|
415
|
+
notations,
|
|
416
|
+
elabRules,
|
|
417
|
+
sorts,
|
|
418
|
+
statements,
|
|
419
|
+
terms,
|
|
420
|
+
},
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
//# sourceMappingURL=spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spec.js","sourceRoot":"","sources":["../../src/reader/spec.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAmB,UAAU,EAAa,MAAM,mBAAmB,CAAC;AAC3E,OAAO,EAGL,qBAAqB,GAEtB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAKL,eAAe,G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,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;QAED,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpC,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;gBACjC,MAAM,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;gBAEzC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACxB,MAAM,CACJ,WAAW,EACX,wBAAwB,EACxB,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,EACtB,IAAI,CAAC,IAAI,CACV,CAAC;oBACF,SAAS;gBACX,CAAC;gBAED,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,QAAQ,EAAE,CAAC;gBACrC,IAAI,KAAK,KAAK,CAAC,EAAE,CAAC;oBAChB,MAAM,CAAC,WAAW,EAAE,qBAAqB,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;gBAClE,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,WAAW;QACX,IAAI,EAAE;YACJ,SAAS;YACT,SAAS;YACT,UAAU,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,eAAe,EAAE;YAC5D,OAAO,EAAE,EAAE,eAAe,EAAE,cAAc,EAAE;YAC5C,aAAa;YACb,KAAK;YACL,SAAS;YACT,SAAS;YACT,KAAK;YACL,UAAU;YACV,KAAK;SACN;KACF,CAAC;AACJ,CAAC","sourcesContent":["/**\n * From parsed statements to a `Spec` — the library's central object: the\n * signature, notations, coercions, and `@syntax` surface metadata of one\n * logical system, validated.\n *\n * Validation here is about the spec being *coherent* (token conflicts,\n * dangling names, non-invertible elab rules); whether the theory proves\n * anything is the engine's business, and axiom bodies pass through opaque.\n */\n\nimport { type Diagnostic, diagnostic, type Span } from \"../diagnostics.js\";\nimport {\n type ElabRule,\n type LintName,\n parseSyntaxAnnotation,\n type SyntaxAnnotation,\n} from \"./annotations.js\";\nimport {\n type Annotation,\n type Binder,\n type NotationLiteral,\n type Precedence,\n parseStatements,\n type Statement,\n} from \"./statements.js\";\n\nexport interface SortInfo {\n readonly foreignAnnotations: readonly Annotation[];\n readonly modifiers: readonly string[];\n readonly name: string;\n readonly span: Span;\n /**\n * The sort's variable tokens, read from the engine's own `@vars`\n * annotation (which stays in `foreignAnnotations` untouched — it is the\n * engine's, we only listen in). These are the surface lexicon's\n * identifiers of this sort; whether a quantifier can bind them falls\n * out of the quantifiers' binder sorts, not from here.\n */\n readonly vars: readonly string[];\n}\n\nexport interface TermInfo {\n readonly binders: readonly Binder[];\n /** `@syntax elided`: supplied when an argument of its sort is missing. */\n readonly elided: boolean;\n readonly foreignAnnotations: readonly Annotation[];\n /** True when this came from a `def` rather than a `term`. */\n readonly isDef: boolean;\n /** `@syntax juxtaposed`: adjacency of its sort's leaves denotes it. */\n readonly juxtaposed: boolean;\n readonly name: string;\n readonly returnSort: string;\n readonly roles: readonly string[];\n readonly span: Span;\n}\n\nexport interface CoercionInfo {\n readonly from: string;\n readonly name: string;\n readonly span: Span;\n readonly to: string;\n}\n\nexport type NotationInfo =\n | {\n readonly form: \"general\";\n /** The notation statement's own binders, which its variables name. */\n readonly binders: readonly Binder[];\n readonly literals: readonly NotationLiteral[];\n readonly span: Span;\n readonly term: string;\n }\n | {\n readonly form: \"simple\";\n readonly fixity: \"infixl\" | \"infixr\" | \"prefix\";\n readonly prec: Precedence;\n readonly span: Span;\n readonly term: string;\n readonly token: string;\n };\n\nexport interface Spec {\n readonly assocNone: ReadonlySet<number>;\n readonly coercions: readonly CoercionInfo[];\n readonly delimiters: {\n readonly left: ReadonlySet<string>;\n readonly right: ReadonlySet<string>;\n };\n readonly display: {\n readonly dropOuterParens: boolean;\n readonly rotateBrackets: readonly (readonly [string, string])[] | null;\n };\n /** Always starts with the built-in `(\"(\", \")\")` pair. */\n readonly groupingPairs: readonly (readonly [string, string])[];\n readonly lints: readonly LintName[];\n /** In declaration order; the last notation for a term is canonical. */\n readonly notations: readonly NotationInfo[];\n readonly elabRules: readonly ElabRule[];\n readonly sorts: ReadonlyMap<string, SortInfo>;\n /** Every statement, in order, annotations intact — full fidelity. */\n readonly statements: readonly Statement[];\n readonly terms: ReadonlyMap<string, TermInfo>;\n}\n\nexport interface SpecParse {\n readonly diagnostics: readonly Diagnostic[];\n readonly spec: Spec;\n}\n\nconst TEMPLATES: Record<string, string> = {\n duplicate_declaration: \"{name} is already declared\",\n unknown_sort: \"sort {name} is not declared\",\n unknown_term: \"term {name} is not declared\",\n infix_arity: \"an infix operator needs exactly two regular arguments\",\n unknown_notation_variable:\n \"notation variable {name} is not one of the declaration's binders\",\n token_conflict:\n \"token {token} already has a meaning; a token may carry one parse rule\",\n precedence_mixed_associativity:\n \"precedence {prec} mixes infixl and infixr operators\",\n grouping_token_conflict:\n \"{token} is a grouping bracket and cannot also be a notation token\",\n elab_duplicate_capture: \"capture {name} appears twice in the pattern\",\n elab_unknown_reference:\n \"the template references {name}, which the pattern does not capture\",\n elab_not_invertible:\n \"capture {name} is not used exactly once in the template; mark the rule input-only if that is intended\",\n juxtaposed_target:\n \"@syntax juxtaposed must sit on a binary term whose arguments and result share one sort\",\n juxtaposed_needs_notation:\n \"a juxtaposed term needs a declared notation, so the engine can read it\",\n juxtaposed_duplicate: \"sort {sort} already has a juxtaposed combiner\",\n elided_target: \"@syntax elided must sit on a term with no arguments\",\n elided_duplicate: \"sort {sort} already has an elided term\",\n vars_term_conflict:\n \"@vars token {token} is also a declared term; a name can be only one\",\n role_target: \"@syntax role must sit on a term\",\n};\n\nfunction report(\n diagnostics: Diagnostic[],\n id: string,\n params: Record<string, string>,\n span: Span,\n): void {\n diagnostics.push(diagnostic(id, TEMPLATES[id] ?? id, params, span));\n}\n\n/** The binder count an application must fill (bound and regular alike). */\nfunction regularBinders(binders: readonly Binder[]): readonly Binder[] {\n return binders.filter((binder) => !binder.binds);\n}\n\nfunction binderSortOf(binder: Binder): string {\n return \"sort\" in binder.type ? binder.type.sort : \"\";\n}\n\ninterface TokenMeaning {\n readonly key: string;\n readonly term: string;\n}\n\n/**\n * One line per parse rule a token can carry. Two identical lines are a\n * pointless duplicate but not an ambiguity; anything else sharing a token\n * is a conflict.\n */\nfunction meaningOf(notation: NotationInfo): TokenMeaning | null {\n if (notation.form === \"simple\") {\n return {\n key: `${notation.fixity}:${String(notation.prec)}`,\n term: notation.term,\n };\n }\n\n const first = notation.literals[0];\n\n if (first === undefined || first.kind !== \"constant\") {\n return null;\n }\n\n return { key: \"general\", term: notation.term };\n}\n\nexport function parseSpec(source: string): SpecParse {\n const { statements, diagnostics: parseDiagnostics } =\n parseStatements(source);\n const diagnostics: Diagnostic[] = [...parseDiagnostics];\n\n const sorts = new Map<string, SortInfo>();\n const terms = new Map<string, TermInfo>();\n const coercions: CoercionInfo[] = [];\n const notations: NotationInfo[] = [];\n const elabRules: ElabRule[] = [];\n const lints: LintName[] = [];\n const assocNone = new Set<number>();\n const delimitersLeft = new Set<string>();\n const delimitersRight = new Set<string>();\n const groupingPairs: (readonly [string, string])[] = [[\"(\", \")\"]];\n let dropOuterParens = false;\n let rotateBrackets: readonly (readonly [string, string])[] | null = null;\n\n // ── First pass: split each statement's annotations, collect declarations.\n\n interface Attached {\n readonly annotation: SyntaxAnnotation;\n readonly span: Span;\n readonly statement: Statement;\n }\n\n const attached: Attached[] = [];\n\n for (const statement of statements) {\n const foreign: Annotation[] = [];\n\n for (const annotation of statement.annotations) {\n const parsed = parseSyntaxAnnotation(annotation.text, annotation.span);\n\n if (parsed === null) {\n foreign.push(annotation);\n continue;\n }\n\n if (parsed.diagnostic !== null) {\n diagnostics.push(parsed.diagnostic);\n continue;\n }\n\n attached.push({\n annotation: parsed.annotation,\n span: annotation.span,\n statement,\n });\n }\n\n switch (statement.kind) {\n case \"sort\": {\n if (sorts.has(statement.name) || terms.has(statement.name)) {\n report(\n diagnostics,\n \"duplicate_declaration\",\n { name: statement.name },\n statement.span,\n );\n break;\n }\n\n // The engine's `@vars` pools are this library's variable lexicon\n // too; the lines stay foreign (they are the engine's to keep).\n const vars: string[] = [];\n\n for (const annotation of foreign) {\n const match = /^@vars\\s+(.+)$/.exec(annotation.text.trim());\n\n if (match !== null) {\n vars.push(\n ...(match[1] ?? \"\").split(/\\s+/).filter((w) => w.length > 0),\n );\n }\n }\n\n sorts.set(statement.name, {\n foreignAnnotations: foreign,\n modifiers: statement.modifiers,\n name: statement.name,\n span: statement.span,\n vars,\n });\n break;\n }\n\n case \"term\":\n case \"def\": {\n if (sorts.has(statement.name) || terms.has(statement.name)) {\n report(\n diagnostics,\n \"duplicate_declaration\",\n { name: statement.name },\n statement.span,\n );\n break;\n }\n\n const returnSort =\n statement.kind === \"term\"\n ? (statement.returnChain[statement.returnChain.length - 1]\n ?.sort ?? \"\")\n : statement.returnType.sort;\n\n // Arrow sugar on a term (`a > b > c`) desugars to extra anonymous\n // regular binders so arity means one thing everywhere downstream.\n const extra: Binder[] =\n statement.kind === \"term\"\n ? statement.returnChain.slice(0, -1).map((type) => ({\n binds: false,\n dummy: false,\n name: \"_\",\n span: type.span,\n type,\n }))\n : [];\n\n terms.set(statement.name, {\n binders: [...statement.binders, ...extra],\n elided: false,\n foreignAnnotations: foreign,\n isDef: statement.kind === \"def\",\n juxtaposed: false,\n name: statement.name,\n returnSort,\n roles: [],\n span: statement.span,\n });\n break;\n }\n\n case \"coercion\": {\n coercions.push({\n from: statement.from,\n name: statement.name,\n span: statement.span,\n to: statement.to,\n });\n break;\n }\n\n case \"delimiter\": {\n for (const ch of statement.left) {\n delimitersLeft.add(ch);\n }\n for (const ch of statement.right) {\n delimitersRight.add(ch);\n }\n break;\n }\n\n case \"simple-notation\": {\n notations.push({\n form: \"simple\",\n fixity: statement.fixity,\n prec: statement.prec,\n span: statement.span,\n term: statement.term,\n token: statement.token,\n });\n break;\n }\n\n case \"notation\": {\n notations.push({\n form: \"general\",\n binders: statement.binders,\n literals: statement.literals,\n span: statement.span,\n term: statement.term,\n });\n\n for (const literal of statement.literals) {\n if (\n literal.kind === \"variable\" &&\n !statement.binders.some((b) => b.name === literal.name)\n ) {\n report(\n diagnostics,\n \"unknown_notation_variable\",\n { name: literal.name },\n literal.span,\n );\n }\n }\n break;\n }\n\n case \"axiom\":\n case \"theorem\":\n break;\n }\n }\n\n // ── Second pass: interpret @syntax annotations now that names resolve.\n\n const juxtaposedBySort = new Map<string, string>();\n const elidedBySort = new Map<string, string>();\n\n for (const { annotation, span, statement } of attached) {\n switch (annotation.kind) {\n case \"juxtaposed\": {\n const info =\n statement.kind === \"term\" || statement.kind === \"def\"\n ? terms.get(statement.name)\n : undefined;\n const regular =\n info === undefined ? [] : regularBinders(info.binders);\n const sort = info?.returnSort ?? \"\";\n\n if (\n info === undefined ||\n info.binders.length !== 2 ||\n regular.length !== 2 ||\n regular.some((binder) => binderSortOf(binder) !== sort)\n ) {\n report(diagnostics, \"juxtaposed_target\", {}, span);\n break;\n }\n\n if (!notations.some((notation) => notation.term === info.name)) {\n report(diagnostics, \"juxtaposed_needs_notation\", {}, span);\n break;\n }\n\n if (juxtaposedBySort.has(sort)) {\n report(diagnostics, \"juxtaposed_duplicate\", { sort }, span);\n break;\n }\n\n juxtaposedBySort.set(sort, info.name);\n terms.set(info.name, { ...info, juxtaposed: true });\n break;\n }\n\n case \"elided\": {\n const info =\n statement.kind === \"term\" || statement.kind === \"def\"\n ? terms.get(statement.name)\n : undefined;\n\n if (info === undefined || regularBinders(info.binders).length !== 0) {\n report(diagnostics, \"elided_target\", {}, span);\n break;\n }\n\n if (elidedBySort.has(info.returnSort)) {\n report(\n diagnostics,\n \"elided_duplicate\",\n { sort: info.returnSort },\n span,\n );\n break;\n }\n\n elidedBySort.set(info.returnSort, info.name);\n terms.set(info.name, { ...info, elided: true });\n break;\n }\n\n case \"role\": {\n if (statement.kind !== \"term\" && statement.kind !== \"def\") {\n report(diagnostics, \"role_target\", {}, span);\n break;\n }\n\n const info = terms.get(statement.name);\n\n if (info !== undefined) {\n terms.set(statement.name, {\n ...info,\n roles: [...info.roles, annotation.role],\n });\n }\n break;\n }\n\n case \"brackets\": {\n for (const pair of annotation.pairs) {\n const exists = groupingPairs.some(\n ([open, close]) => open === pair[0] && close === pair[1],\n );\n\n if (!exists) {\n groupingPairs.push(pair);\n }\n }\n break;\n }\n\n case \"assoc-none\": {\n assocNone.add(annotation.prec);\n break;\n }\n\n case \"lint\": {\n if (!lints.includes(annotation.name)) {\n lints.push(annotation.name);\n }\n break;\n }\n\n case \"display-drop-outer-parens\": {\n dropOuterParens = true;\n break;\n }\n\n case \"display-rotate-brackets\": {\n rotateBrackets = annotation.pairs;\n break;\n }\n\n case \"elab\": {\n elabRules.push(annotation.rule);\n break;\n }\n }\n }\n\n // ── Validation over the collected spec.\n\n for (const info of terms.values()) {\n for (const binder of info.binders) {\n if (\"sort\" in binder.type && !sorts.has(binder.type.sort)) {\n report(\n diagnostics,\n \"unknown_sort\",\n { name: binder.type.sort },\n binder.type.span,\n );\n }\n }\n\n if (info.returnSort !== \"\" && !sorts.has(info.returnSort)) {\n report(\n diagnostics,\n \"unknown_sort\",\n { name: info.returnSort },\n info.span,\n );\n }\n }\n\n for (const sort of sorts.values()) {\n for (const token of sort.vars) {\n if (terms.has(token)) {\n report(diagnostics, \"vars_term_conflict\", { token }, sort.span);\n }\n }\n }\n\n for (const coercion of coercions) {\n if (!terms.has(coercion.name)) {\n report(\n diagnostics,\n \"unknown_term\",\n { name: coercion.name },\n coercion.span,\n );\n }\n for (const sort of [coercion.from, coercion.to]) {\n if (!sorts.has(sort)) {\n report(diagnostics, \"unknown_sort\", { name: sort }, coercion.span);\n }\n }\n }\n\n const tokenMeanings = new Map<string, { key: string; term: string }>();\n\n for (const notation of notations) {\n const info = terms.get(notation.term);\n\n if (info === undefined) {\n report(\n diagnostics,\n \"unknown_term\",\n { name: notation.term },\n notation.span,\n );\n continue;\n }\n\n if (\n notation.form === \"simple\" &&\n notation.fixity !== \"prefix\" &&\n (regularBinders(info.binders).length !== 2 ||\n info.binders.some((b) => b.binds))\n ) {\n report(diagnostics, \"infix_arity\", {}, notation.span);\n }\n\n const token =\n notation.form === \"simple\"\n ? notation.token\n : notation.literals[0]?.kind === \"constant\"\n ? notation.literals[0].token\n : null;\n const meaning = meaningOf(notation);\n\n if (token === null || meaning === null) {\n continue;\n }\n\n const existing = tokenMeanings.get(token);\n\n if (existing !== undefined) {\n if (existing.key !== meaning.key || existing.term !== meaning.term) {\n report(diagnostics, \"token_conflict\", { token }, notation.span);\n }\n continue;\n }\n\n tokenMeanings.set(token, meaning);\n }\n\n const precFixities = new Map<number, Set<\"infixl\" | \"infixr\">>();\n\n for (const notation of notations) {\n if (\n notation.form === \"simple\" &&\n notation.fixity !== \"prefix\" &&\n typeof notation.prec === \"number\"\n ) {\n const set = precFixities.get(notation.prec) ?? new Set();\n set.add(notation.fixity);\n precFixities.set(notation.prec, set);\n\n if (set.size > 1) {\n report(\n diagnostics,\n \"precedence_mixed_associativity\",\n { prec: String(notation.prec) },\n notation.span,\n );\n }\n }\n }\n\n for (const [open, close] of groupingPairs) {\n for (const token of [open, close]) {\n if (tokenMeanings.has(token)) {\n report(\n diagnostics,\n \"grouping_token_conflict\",\n { token },\n {\n start: 0,\n end: 0,\n },\n );\n }\n }\n }\n\n for (const rule of elabRules) {\n const captures = new Map<string, number>();\n\n for (const element of rule.pattern) {\n if (element.kind === \"capture\") {\n if (captures.has(element.name)) {\n report(\n diagnostics,\n \"elab_duplicate_capture\",\n { name: element.name },\n rule.span,\n );\n }\n captures.set(element.name, 0);\n }\n }\n\n for (const element of rule.template) {\n if (element.kind === \"reference\") {\n const count = captures.get(element.name);\n\n if (count === undefined) {\n report(\n diagnostics,\n \"elab_unknown_reference\",\n { name: element.name },\n rule.span,\n );\n continue;\n }\n\n captures.set(element.name, count + 1);\n }\n }\n\n if (!rule.inputOnly) {\n for (const [name, count] of captures) {\n if (count !== 1) {\n report(diagnostics, \"elab_not_invertible\", { name }, rule.span);\n }\n }\n }\n }\n\n return {\n diagnostics,\n spec: {\n assocNone,\n coercions,\n delimiters: { left: delimitersLeft, right: delimitersRight },\n display: { dropOuterParens, rotateBrackets },\n groupingPairs,\n lints,\n notations,\n elabRules,\n sorts,\n statements,\n terms,\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The MM0 statement parser: file tokens in, statements out.
|
|
3
|
+
*
|
|
4
|
+
* The grammar is the upstream one (mm0.md), in the subset Aufbau reads —
|
|
5
|
+
* summarized in the manual's appendix-grammars.md. Math strings are carried
|
|
6
|
+
* through opaque: scanning their contents is the surface layer's job, and an
|
|
7
|
+
* axiom's body never needs it at all.
|
|
8
|
+
*
|
|
9
|
+
* On a malformed statement the parser records one diagnostic and skips to
|
|
10
|
+
* the next `;`, so a spec author sees every broken statement at once.
|
|
11
|
+
*/
|
|
12
|
+
import { type Diagnostic, type Span } from "../diagnostics.js";
|
|
13
|
+
/** A math string, with the span of the whole `$ … $` lexeme. */
|
|
14
|
+
export interface MathString {
|
|
15
|
+
readonly span: Span;
|
|
16
|
+
readonly text: string;
|
|
17
|
+
}
|
|
18
|
+
/** An annotation line's payload, e.g. `@acui seq_assoc _ snil _`. */
|
|
19
|
+
export interface Annotation {
|
|
20
|
+
readonly span: Span;
|
|
21
|
+
readonly text: string;
|
|
22
|
+
}
|
|
23
|
+
/** A sort name applied to the bound variables the value may depend on. */
|
|
24
|
+
export interface TypeRef {
|
|
25
|
+
readonly dependencies: readonly string[];
|
|
26
|
+
readonly sort: string;
|
|
27
|
+
readonly span: Span;
|
|
28
|
+
}
|
|
29
|
+
export interface Binder {
|
|
30
|
+
/** `{x: s}` binders bind; `(a: s)` binders are regular. */
|
|
31
|
+
readonly binds: boolean;
|
|
32
|
+
/** True for a `.`-prefixed dummy on a `def`. */
|
|
33
|
+
readonly dummy: boolean;
|
|
34
|
+
readonly name: string;
|
|
35
|
+
readonly span: Span;
|
|
36
|
+
/** A hypothesis binder `(h: $ … $)` carries a formula instead. */
|
|
37
|
+
readonly type: TypeRef | MathString;
|
|
38
|
+
}
|
|
39
|
+
export type Precedence = number | "max";
|
|
40
|
+
/** One slot of a general notation: a constant literal or a variable. */
|
|
41
|
+
export type NotationLiteral = {
|
|
42
|
+
readonly kind: "constant";
|
|
43
|
+
readonly prec: Precedence;
|
|
44
|
+
readonly token: string;
|
|
45
|
+
readonly span: Span;
|
|
46
|
+
} | {
|
|
47
|
+
readonly kind: "variable";
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly span: Span;
|
|
50
|
+
};
|
|
51
|
+
interface StatementBase {
|
|
52
|
+
readonly annotations: readonly Annotation[];
|
|
53
|
+
readonly span: Span;
|
|
54
|
+
}
|
|
55
|
+
export interface SortStatement extends StatementBase {
|
|
56
|
+
readonly kind: "sort";
|
|
57
|
+
readonly modifiers: readonly string[];
|
|
58
|
+
readonly name: string;
|
|
59
|
+
}
|
|
60
|
+
export interface TermStatement extends StatementBase {
|
|
61
|
+
readonly kind: "term";
|
|
62
|
+
readonly name: string;
|
|
63
|
+
readonly binders: readonly Binder[];
|
|
64
|
+
/** The `:`-side arrow chain; the last entry is the return type. */
|
|
65
|
+
readonly returnChain: readonly TypeRef[];
|
|
66
|
+
}
|
|
67
|
+
export interface DefStatement extends StatementBase {
|
|
68
|
+
readonly kind: "def";
|
|
69
|
+
readonly name: string;
|
|
70
|
+
readonly binders: readonly Binder[];
|
|
71
|
+
readonly returnType: TypeRef;
|
|
72
|
+
/** Absent on a bodyless def (definiens supplied by the proof file). */
|
|
73
|
+
readonly definiens: MathString | null;
|
|
74
|
+
}
|
|
75
|
+
export interface AssertStatement extends StatementBase {
|
|
76
|
+
readonly kind: "axiom" | "theorem";
|
|
77
|
+
readonly name: string;
|
|
78
|
+
readonly binders: readonly Binder[];
|
|
79
|
+
/** The `>`-chain: hypotheses (types or formulas), then the conclusion. */
|
|
80
|
+
readonly hypotheses: readonly (TypeRef | MathString)[];
|
|
81
|
+
readonly conclusion: MathString;
|
|
82
|
+
}
|
|
83
|
+
export interface DelimiterStatement extends StatementBase {
|
|
84
|
+
readonly kind: "delimiter";
|
|
85
|
+
/** The one-list form fills both sides with the same characters. */
|
|
86
|
+
readonly left: readonly string[];
|
|
87
|
+
readonly right: readonly string[];
|
|
88
|
+
}
|
|
89
|
+
export interface SimpleNotationStatement extends StatementBase {
|
|
90
|
+
readonly kind: "simple-notation";
|
|
91
|
+
readonly fixity: "infixl" | "infixr" | "prefix";
|
|
92
|
+
readonly term: string;
|
|
93
|
+
readonly token: string;
|
|
94
|
+
readonly prec: Precedence;
|
|
95
|
+
}
|
|
96
|
+
export interface CoercionStatement extends StatementBase {
|
|
97
|
+
readonly kind: "coercion";
|
|
98
|
+
readonly name: string;
|
|
99
|
+
readonly from: string;
|
|
100
|
+
readonly to: string;
|
|
101
|
+
}
|
|
102
|
+
export interface GenNotationStatement extends StatementBase {
|
|
103
|
+
readonly kind: "notation";
|
|
104
|
+
readonly term: string;
|
|
105
|
+
readonly binders: readonly Binder[];
|
|
106
|
+
readonly returnType: TypeRef;
|
|
107
|
+
readonly literals: readonly NotationLiteral[];
|
|
108
|
+
}
|
|
109
|
+
export type Statement = AssertStatement | CoercionStatement | DefStatement | DelimiterStatement | GenNotationStatement | SimpleNotationStatement | SortStatement | TermStatement;
|
|
110
|
+
export interface StatementParse {
|
|
111
|
+
readonly statements: readonly Statement[];
|
|
112
|
+
readonly diagnostics: readonly Diagnostic[];
|
|
113
|
+
}
|
|
114
|
+
export declare function parseStatements(source: string): StatementParse;
|
|
115
|
+
export {};
|