@aufbau/syntax 0.0.5 → 0.0.6

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/dist/parse.js CHANGED
@@ -18,12 +18,14 @@
18
18
  * 4. **Lints** — the closed set of post-parse checks
19
19
  * (`parenthesize-binary-only`, `closed-sentences`).
20
20
  *
21
- * Two survivable deviations, both surface-side supersets: a lexicon name
22
- * (nullary, elided-bare, or applied to parenthesized or juxtaposed
23
- * arguments) counts as `expression(max)`, where MM0 puts constructor
24
- * application at 1024; and a token that is both a notation and a lexicon
25
- * name (Calgary's `A`) is disambiguated by backtracking, notation reading
26
- * first.
21
+ * Constructor application is MM0's own production, `expression(1024) →
22
+ * FUNC expression(max){n}` — `S x`, `pair a b`, `succ (succ x)` — with
23
+ * two surface-side supersets in front of it: a lexicon name applied to
24
+ * bracketed arguments (`S(x)`, `R(a,b)`), or elided-bare, or glued to
25
+ * juxtaposed arguments, counts as `expression(max)`, so a textbook's
26
+ * `F(a)` can sit anywhere an atom can. A token that is both a notation
27
+ * and a lexicon name (Calgary's `A`) is disambiguated by backtracking,
28
+ * notation reading first.
27
29
  *
28
30
  * mm0.md's slot-precedence rules are used exactly: a prefix notation's
29
31
  * intermediate arguments parse at max and its last at the notation's
@@ -32,6 +34,7 @@
32
34
  * when last. Coercions are inserted along the (unique) path in the coercion
33
35
  * graph, per the spec.
34
36
  */
37
+ import { delimiterRules, isReachableChunk } from "./delimiters.js";
35
38
  import { diagnostic } from "./diagnostics.js";
36
39
  import { elaborate, remapSpan } from "./elab.js";
37
40
  import { Scanner } from "./scan.js";
@@ -53,9 +56,45 @@ const TEMPLATES = {
53
56
  needs_parentheses: "“{token}” binds too loosely here; parenthesize it.",
54
57
  spec_notation_incomplete: "The notation for {term} does not cover all of its arguments.",
55
58
  };
56
- function precNum(prec) {
59
+ export function precNum(prec) {
57
60
  return prec === "max" ? Number.POSITIVE_INFINITY : prec;
58
61
  }
62
+ /** mm0.md: bare constructor application is `expression(1024)`. */
63
+ const APPLICATION_PRECEDENCE = 1024;
64
+ /**
65
+ * A general notation after its head, as the parser reads it: constants to
66
+ * expect and slots to parse, each slot at mm0.md's P(lits, q) — max before
67
+ * a variable, p+1 before a constant of precedence p, and q (the head
68
+ * precedence) when it is the trailing slot. The printer reads the same
69
+ * table, so what it decides needs no parentheses is what this parses.
70
+ */
71
+ export function generalParts(notation, headPrec) {
72
+ const parts = [];
73
+ const rest = notation.literals.slice(1);
74
+ for (let i = 0; i < rest.length; i += 1) {
75
+ const literal = rest[i];
76
+ if (literal === undefined) {
77
+ continue;
78
+ }
79
+ if (literal.kind === "constant") {
80
+ parts.push({ kind: "constant", token: literal.token });
81
+ continue;
82
+ }
83
+ const index = notation.binders.findIndex((binder) => binder.name === literal.name);
84
+ const binder = notation.binders[index];
85
+ if (binder === undefined) {
86
+ continue;
87
+ }
88
+ const following = rest[i + 1];
89
+ const prec = following === undefined
90
+ ? precNum(headPrec)
91
+ : following.kind === "constant"
92
+ ? precNum(following.prec) + 1
93
+ : Number.POSITIVE_INFINITY;
94
+ parts.push({ kind: "slot", slot: { binder, index, prec } });
95
+ }
96
+ return parts;
97
+ }
59
98
  const EMPTY_SCOPE = new Map();
60
99
  function binderSort(binder) {
61
100
  if (binder === undefined || !("sort" in binder.type)) {
@@ -129,33 +168,7 @@ export class SurfaceLanguage {
129
168
  if (head === undefined || head.kind !== "constant") {
130
169
  continue;
131
170
  }
132
- // mm0.md's P(lits, q): a slot parses at max before a variable, at
133
- // p+1 before a constant of precedence p, and at q (the head
134
- // precedence) when it is the trailing slot.
135
- const parts = [];
136
- const rest = notation.literals.slice(1);
137
- for (let i = 0; i < rest.length; i += 1) {
138
- const literal = rest[i];
139
- if (literal === undefined) {
140
- continue;
141
- }
142
- if (literal.kind === "constant") {
143
- parts.push({ kind: "constant", token: literal.token });
144
- continue;
145
- }
146
- const index = notation.binders.findIndex((binder) => binder.name === literal.name);
147
- const binder = notation.binders[index];
148
- if (binder === undefined) {
149
- continue;
150
- }
151
- const following = rest[i + 1];
152
- const prec = following === undefined
153
- ? precNum(head.prec)
154
- : following.kind === "constant"
155
- ? precNum(following.prec) + 1
156
- : Number.POSITIVE_INFINITY;
157
- parts.push({ kind: "slot", slot: { binder, index, prec } });
158
- }
171
+ const parts = generalParts(notation, head.prec);
159
172
  this.heads.set(head.token, { kind: "general", info, parts });
160
173
  }
161
174
  for (const [open, close] of spec.groupingPairs) {
@@ -175,8 +188,21 @@ export class SurfaceLanguage {
175
188
  }
176
189
  }
177
190
  }
191
+ // The canonical spelling is the last-declared notation the theory's
192
+ // own delimiters can read back; a token a delimiter cuts through (`/\`
193
+ // under `[x/t]`'s `/`) is dead — mm0.md forbids it — and only stands
194
+ // when nothing else does.
195
+ const rules = delimiterRules(spec.delimiters);
196
+ const readable = (notation) => (notation.form === "simple"
197
+ ? [notation.token]
198
+ : notation.literals.flatMap((literal) => literal.kind === "constant" ? [literal.token] : [])).every((token) => isReachableChunk(token, rules));
178
199
  for (const notation of spec.notations) {
179
- this.canonical.set(notation.term, notation);
200
+ const standing = this.canonical.get(notation.term);
201
+ if (standing === undefined ||
202
+ readable(notation) ||
203
+ !readable(standing)) {
204
+ this.canonical.set(notation.term, notation);
205
+ }
180
206
  }
181
207
  // Later notations overwrite earlier ones, so each sort's arc ends up
182
208
  // borrowing from the combiner's *last-declared* infix spelling — the
@@ -653,7 +679,7 @@ class Parse {
653
679
  }
654
680
  parsePrimaryReading(start, reading, min) {
655
681
  if (reading.kind === "name") {
656
- return this.parseName(start, reading);
682
+ return this.parseName(start, reading, min);
657
683
  }
658
684
  const close = this.lang.closeOf.get(reading.token);
659
685
  if (close !== undefined) {
@@ -836,18 +862,23 @@ class Parse {
836
862
  };
837
863
  }
838
864
  /**
839
- * A lexicon name: a `@vars` token (a variable of its sort), or a
840
- * declared term with no notation of its own — a sentence letter,
841
- * predicate, function symbol, or constant. A term's arguments come
842
- * parenthesized (application syntax, so the argument is not marked
843
- * `grouped` and the binary-only lint does not apply inside; the
844
- * canonical pair is always the first declared one), or juxtaposed when
845
- * the argument sort has an `@syntax juxtaposed` combiner, or not at all
846
- * when it has an `@syntax elided` term. Context settles the `A`-as-∀
847
- * ambiguity exactly as in Carnap: the quantifier reading was tried
848
- * first, and fell through to here only if it failed.
865
+ * A lexicon name: a `@vars` token (a variable of its sort), or any
866
+ * declared term — a sentence letter, predicate, function symbol, or
867
+ * constant, or a notated term written by name. Each of a term's arguments
868
+ * comes one of four ways, tried in this order: parenthesized
869
+ * (application syntax, so the argument is not marked `grouped` and the
870
+ * binary-only lint does not apply inside; the canonical pair is always
871
+ * the first declared one); juxtaposed, when the argument sort has an
872
+ * `@syntax juxtaposed` combiner; not at all, when it has an `@syntax
873
+ * elided` term; or bare — MM0's own `FUNC expression(max){n}`, so
874
+ * `S x` and `pair a b` read exactly as the engine reads them. The
875
+ * declared shapes go first because they are declared: a spec that
876
+ * elides a sort's unit has said what a bare letter means there.
877
+ * Context settles the `A`-as-∀ ambiguity exactly as in Carnap: the
878
+ * quantifier reading was tried first, and fell through to here only if
879
+ * it failed.
849
880
  */
850
- parseName(start, reading) {
881
+ parseName(start, reading, min) {
851
882
  this.position = start + reading.length;
852
883
  if (reading.ref.kind === "var") {
853
884
  return {
@@ -865,8 +896,21 @@ class Parse {
865
896
  }
866
897
  const argBinders = info.binders.filter((binder) => !binder.binds);
867
898
  const args = [];
868
- for (const binder of argBinders) {
899
+ for (const binder of info.binders) {
869
900
  const target = binderSort(binder);
901
+ // A bound binder is filled positionally, by a variable of its sort —
902
+ // `sb x t p`, `all x p` — as in every MM0 application.
903
+ if (binder.binds) {
904
+ if (min > APPLICATION_PRECEDENCE) {
905
+ return this.report("needs_parentheses", { token: reading.name }, { start, end: start + reading.length });
906
+ }
907
+ const variable = this.expectBoundVariable(target);
908
+ if (variable === null) {
909
+ return null;
910
+ }
911
+ args.push(variable);
912
+ continue;
913
+ }
870
914
  const combiner = argBinders.length === 1
871
915
  ? this.lang.juxtaposedOf.get(target)
872
916
  : undefined;
@@ -952,7 +996,22 @@ class Parse {
952
996
  });
953
997
  continue;
954
998
  }
955
- return this.report("expected_formula_found", { token: reading.name }, { start, end: start + reading.length });
999
+ // Bare application. It is `expression(1024)`, so it cannot fill a
1000
+ // max slot — another application's argument, a prefix notation's
1001
+ // intermediate argument: `succ succ x` is refused here just as the
1002
+ // engine refuses it, and `succ (succ x)` is the spelling.
1003
+ if (min > APPLICATION_PRECEDENCE) {
1004
+ return this.report("needs_parentheses", { token: reading.name }, { start, end: start + reading.length });
1005
+ }
1006
+ const arg = this.parseExpr(Number.POSITIVE_INFINITY);
1007
+ if (arg === null) {
1008
+ return null;
1009
+ }
1010
+ const coerced = this.coerceOrReport(arg, target);
1011
+ if (coerced === null) {
1012
+ return null;
1013
+ }
1014
+ args.push(coerced);
956
1015
  }
957
1016
  return {
958
1017
  kind: "app",