gdp-wired 0.25.0 → 0.27.0

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.
Files changed (52) hide show
  1. package/README.md +14 -9
  2. package/dist/core/accel.d.ts +20 -1
  3. package/dist/core/accel.js +797 -220
  4. package/dist/core/bmc.d.ts +12 -1
  5. package/dist/core/bmc.js +275 -106
  6. package/dist/core/cycles.d.ts +2 -3
  7. package/dist/core/cycles.js +35 -131
  8. package/dist/core/cyclic.d.ts +4 -1
  9. package/dist/core/cyclic.js +103 -74
  10. package/dist/core/data.d.ts +1 -1
  11. package/dist/core/data.js +89 -118
  12. package/dist/core/discover.js +65 -27
  13. package/dist/core/dsl.d.ts +3 -2
  14. package/dist/core/dsl.js +29 -25
  15. package/dist/core/exists.js +7 -14
  16. package/dist/core/extract-check.d.ts +55 -4
  17. package/dist/core/extract-check.js +248 -122
  18. package/dist/core/extract-print.d.ts +1 -0
  19. package/dist/core/extract-print.js +26 -3
  20. package/dist/core/extract-read.d.ts +98 -4
  21. package/dist/core/extract-read.js +306 -43
  22. package/dist/core/extract-shapes.d.ts +14 -0
  23. package/dist/core/extract-shapes.js +609 -79
  24. package/dist/core/extract-xstate.d.ts +5 -1
  25. package/dist/core/extract-xstate.js +618 -184
  26. package/dist/core/extract.d.ts +1 -0
  27. package/dist/core/extract.js +81 -30
  28. package/dist/core/infer.d.ts +1 -0
  29. package/dist/core/infer.js +126 -64
  30. package/dist/core/machine.d.ts +6 -0
  31. package/dist/core/machine.js +35 -4
  32. package/dist/core/nonterm.d.ts +5 -1
  33. package/dist/core/nonterm.js +133 -45
  34. package/dist/core/q.d.ts +1 -0
  35. package/dist/core/q.js +2 -2
  36. package/dist/core/relation.d.ts +1 -0
  37. package/dist/core/relation.js +20 -36
  38. package/dist/core/solver.d.ts +33 -0
  39. package/dist/core/solver.js +84 -30
  40. package/dist/core/transfer.js +22 -13
  41. package/dist/core/translate.d.ts +1 -1
  42. package/dist/core/translate.js +88 -65
  43. package/dist/core/xstate.d.ts +6 -1
  44. package/dist/core/xstate.js +336 -55
  45. package/dist/kernel/cdcl.js +212 -79
  46. package/dist/kernel/prove.d.ts +36 -5
  47. package/dist/kernel/prove.js +212 -104
  48. package/dist/kernel/reader.d.ts +75 -5
  49. package/dist/kernel/reader.js +533 -76
  50. package/dist/kernel/simplex.d.ts +27 -2
  51. package/dist/kernel/simplex.js +160 -14
  52. package/package.json +3 -2
@@ -93,9 +93,19 @@ export function shapeOf(t, checker, at) {
93
93
  return refuse(at, 'a Map without its key and value types');
94
94
  return { k: 'map', key: keySort(shapeOf(kt, checker, at), at), val: shapeOf(vt, checker, at), js: 'Map' };
95
95
  }
96
- if (checker.isArrayType(t) || checker.isTupleType(t))
97
- return refuse(at, `${checker.typeToString(t)}: an array (lists are not in the subset yet)`);
98
- if (name === 'Set' || name === 'Date' || name === 'Promise')
96
+ if (checker.isTupleType(t))
97
+ return refuse(at, `${checker.typeToString(t)}: a tuple`);
98
+ if (checker.isArrayType(t)) {
99
+ const el = shapeOf(checker.getTypeArguments(t)[0], checker, at);
100
+ if (el.k !== 'num' && el.k !== 'bool' && el.k !== 'choice')
101
+ return refuse(at, `${checker.typeToString(t)}: an array of ${el.k === 'str' ? 'strings' : el.k === 'rec' ? 'records' : 'compound values'} (arrays of numbers, booleans and string unions are lists in the subset)`);
102
+ if (el.date)
103
+ return refuse(at, `${checker.typeToString(t)}: an array of Dates`);
104
+ return { k: 'list', of: el };
105
+ }
106
+ if (name === 'Date')
107
+ return { k: 'num', date: true };
108
+ if (name === 'Set' || name === 'Promise')
99
109
  return refuse(at, `a ${name}`);
100
110
  const sidx = checker.getIndexInfoOfType(t, ts.IndexKind.String), nidx = checker.getIndexInfoOfType(t, ts.IndexKind.Number);
101
111
  if (sidx || nidx) {
@@ -217,6 +227,10 @@ export class Exec {
217
227
  mergeV(c, va, vb, a, b, heap, pairs, at) {
218
228
  va = this.deref(a, va);
219
229
  vb = this.deref(b, vb);
230
+ if (va.t === 'outside' && vb.t === 'outside' && va.path === vb.path)
231
+ return va;
232
+ if (va.t === 'opaque' && vb.t === 'opaque')
233
+ return va;
220
234
  if (va.t === 'num' && vb.t === 'num')
221
235
  return { t: 'num', e: I.nite(c, va.e, vb.e) };
222
236
  if (va.t === 'bool' && vb.t === 'bool')
@@ -228,12 +242,12 @@ export class Exec {
228
242
  return { t: 'choice', e: I.lite(c, this.asChoice(va, values, at), this.asChoice(vb, values, at)), values };
229
243
  }
230
244
  if (va.t === 'undef' && vb.t === 'undef')
231
- return va;
245
+ return va.null === vb.null ? va : refuse(at, 'null on one path and undefined on another');
232
246
  if (va.t === 'opt' || vb.t === 'opt' || va.t === 'undef' || vb.t === 'undef') {
233
247
  const [ha, xa] = va.t === 'opt' ? [va.has, va.v] : va.t === 'undef' ? [I.F, undefined] : [I.T, va];
234
248
  const [hb, xb] = vb.t === 'opt' ? [vb.has, vb.v] : vb.t === 'undef' ? [I.F, undefined] : [I.T, vb];
235
- const v = !xa ? xb : !xb ? xa : this.mergeV(c, xa, xb, a, b, heap, pairs, at);
236
- return { t: 'opt', has: I.bite(c, ha, hb), v };
249
+ const v = !xa && !xb ? refuse(at, 'a value missing on both paths') : !xa ? xb : !xb ? xa : this.mergeV(c, xa, xb, a, b, heap, pairs, at);
250
+ return { t: 'opt', has: I.bite(c, ha, hb), v, ...nullOf(at, va, vb) };
237
251
  }
238
252
  if (va.t === 'cell' && vb.t === 'cell' && va.map === vb.map) {
239
253
  // Current on both paths (the map not set since it was read on either): current in the map merged.
@@ -254,11 +268,19 @@ export class Exec {
254
268
  if (oa.k === 'alias' && ob.k === 'alias')
255
269
  return { k: 'alias', to: this.mergeV(c, oa.to, ob.to, a, b, heap, pairs, at) };
256
270
  if (oa.k === 'rec' && ob.k === 'rec') {
257
- const fa = [...oa.fields.keys()].sort().join(), fb = [...ob.fields.keys()].sort().join();
271
+ // An untyped record grows a field as it is read (an input): read on one path, it is there on the other too.
272
+ const fA = oa.lazy && ob.lazy ? new Map([...ob.fields, ...oa.fields]) : oa.fields, fB = oa.lazy && ob.lazy ? new Map([...oa.fields, ...ob.fields]) : ob.fields;
273
+ const fa = [...fA.keys()].sort().join(), fb = [...fB.keys()].sort().join();
258
274
  if (fa !== fb)
259
275
  return refuse(at, 'an object with other fields by the path taken');
260
276
  const origin = oa.origin === 'input' || ob.origin === 'input' ? 'input' : oa.origin === 'shared' || ob.origin === 'shared' ? 'shared' : oa.origin;
261
- return { k: 'rec', origin, fields: new Map([...oa.fields].map(([f, v]) => [f, this.mergeV(c, v, ob.fields.get(f), a, b, heap, pairs, at)])), ...(oa.lazy ? { lazy: oa.lazy } : {}) };
277
+ return { k: 'rec', origin, fields: new Map([...fA].map(([f, v]) => [f, this.mergeV(c, v, fB.get(f), a, b, heap, pairs, at)])), ...(oa.lazy ? { lazy: oa.lazy } : {}) };
278
+ }
279
+ if (oa.k === 'list' && ob.k === 'list') {
280
+ if (oa.base !== ob.base)
281
+ return refuse(at, 'one of two lists by the path taken');
282
+ const origin = oa.origin === 'input' || ob.origin === 'input' ? 'input' : oa.origin;
283
+ return { ...oa, origin, ops: mergeOps(c, oa.ops, ob.ops) };
262
284
  }
263
285
  if (oa.k === 'map' && ob.k === 'map') {
264
286
  if (oa.base !== ob.base)
@@ -270,15 +292,7 @@ export class Exec {
270
292
  return refuse(at, 'an object that is of another kind by the path taken');
271
293
  }
272
294
  mergeW(c, oa, ob, at) { return this.mergeWrites(c, oa.decl, oa.w, ob.w, at); }
273
- mergeWrites(c, decl, wa, wb, at) {
274
- if (!wa && !wb)
275
- return undefined;
276
- const base = (key) => this.baseCell(decl, key);
277
- const fill = (w, key) => w ?? { key, ...base(key) };
278
- const xa = fill(wa, (wa ?? wb).key), xb = fill(wb, (wb ?? wa).key);
279
- const vals = new Map([...xa.vals].map(([f, v]) => [f, this.mergeScalar(c, v, xb.vals.get(f), at)]));
280
- return { key: I.kite(c, xa.key, xb.key), has: I.bite(c, xa.has, xb.has), vals };
281
- }
295
+ mergeWrites(c, decl, wa, wb, at) { return mergeWrites(c, wa, wb, key => this.baseCell(decl, key), (c2, x, y) => this.mergeScalar(c2, x, y, at)); }
282
296
  mergeScalar(c, a, b, at) {
283
297
  if (a.t === 'num' && b.t === 'num')
284
298
  return { t: 'num', e: I.nite(c, a.e, b.e) };
@@ -347,6 +361,8 @@ export class Exec {
347
361
  refuse(st, 'var (let or const are in the subset)');
348
362
  let q = p;
349
363
  for (const d of st.declarationList.declarations) {
364
+ if (isDate(this.s.checker.getTypeAtLocation(d.name)) && !freshDate(d.initializer))
365
+ refuse(d, `${d.name.getText()}: a second name for a Date (its time is read where it is held: getTime())`);
350
366
  let v = { t: 'undef' };
351
367
  if (d.initializer)
352
368
  [v, q] = this.ev(d.initializer, q);
@@ -478,12 +494,15 @@ export class Exec {
478
494
  case 'bool': return v.e;
479
495
  case 'num': return I.not(I.cmp('=', v.e, I.num(0)));
480
496
  case 'choice': return v.values.includes('') ? I.not(I.is(v.e, '')) : I.T;
481
- case 'str': return v.e.op === 'slit' ? (v.e.value ? I.T : I.F) : refuse(at, 'a string as a condition');
497
+ // A string is truthy where it isn't empty.
498
+ case 'str': return v.e.op === 'slit' ? (v.e.value ? I.T : I.F) : I.not(I.keq(v.e, { op: 'slit', value: '' }));
482
499
  case 'ref':
483
500
  case 'cell':
484
- case 'phi': return I.T;
501
+ case 'phi':
502
+ case 'outside': return I.T;
485
503
  case 'opt': return I.and(v.has, this.truthy(v.v, at));
486
504
  case 'undef': return I.F;
505
+ case 'opaque': return refuse(at, `${text(at)} read as a condition: ${v.what}`);
487
506
  }
488
507
  }
489
508
  num(v, at, p) {
@@ -493,10 +512,22 @@ export class Exec {
493
512
  return this.num(v.v, at, p);
494
513
  if (v.t === 'opt')
495
514
  return refuse(at, `${text(at)} may be missing (undefined): give a default (?? 0) or check for it first`);
496
- return refuse(at, `${text(at)} is a ${v.t === 'bool' ? 'boolean' : v.t === 'choice' || v.t === 'str' ? 'string' : v.t === 'undef' ? 'undefined' : 'object'} where a number is read`);
515
+ if (v.t === 'opaque')
516
+ return refuse(at, `${text(at)} read as a number: ${v.what}`);
517
+ return refuse(at, `${text(at)} is a ${v.t === 'bool' ? 'boolean' : v.t === 'choice' || v.t === 'str' ? 'string' : v.t === 'undef' ? (v.null ? 'null' : 'undefined') : 'object'} where a number is read`);
497
518
  }
498
519
  bool(v, at) { return v.t === 'bool' ? v.e : refuse(at, `${text(at)} where a boolean is read`); }
499
- equal(a, b, at) {
520
+ equal(a, b, at, loose = false) {
521
+ if (a.t === 'opaque' || b.t === 'opaque')
522
+ return refuse(at, `${text(at)} compares ${(a.t === 'opaque' ? a : b).what}`);
523
+ // Missing values: null and undefined are == to each other, === each to itself; one that may be missing is equal to a
524
+ // missing one where it is missing, to a value where it is there and equal.
525
+ if (a.t === 'opt' || b.t === 'opt' || (a.t === 'undef' && b.t === 'undef')) {
526
+ const parts = (x) => (x.t === 'opt' ? [x.has, x.v, !!x.null] : x.t === 'undef' ? [I.F, undefined, !!x.null] : [I.T, x, false]);
527
+ const [ha, va, na] = parts(a), [hb, vb, nb] = parts(b);
528
+ const bothMissing = loose || na === nb ? I.and(I.not(ha), I.not(hb)) : I.F;
529
+ return I.or(bothMissing, va && vb ? I.and(ha, hb, this.equal(va, vb, at, loose)) : I.F);
530
+ }
500
531
  if (a.t === 'num' && b.t === 'num')
501
532
  return I.cmp('=', a.e, b.e);
502
533
  if (a.t === 'bool' && b.t === 'bool')
@@ -507,10 +538,9 @@ export class Exec {
507
538
  return this.equal(b, a, at);
508
539
  if (a.t === 'str' && b.t === 'str')
509
540
  return I.keq(a.e, b.e);
510
- if (a.t === 'undef' || b.t === 'undef') {
511
- const o = a.t === 'undef' ? b : a;
512
- return o.t === 'undef' ? I.T : o.t === 'opt' ? I.not(o.has) : I.F;
513
- }
541
+ // A missing value and one that is there: never equal.
542
+ if (a.t === 'undef' || b.t === 'undef')
543
+ return I.F;
514
544
  return refuse(at, `a comparison of a ${a.t} with a ${b.t}`);
515
545
  }
516
546
  ev(e, p) {
@@ -537,10 +567,10 @@ export class Exec {
537
567
  if (e.kind === ts.SyntaxKind.FalseKeyword)
538
568
  return [{ t: 'bool', e: I.F }, p];
539
569
  if (e.kind === ts.SyntaxKind.NullKeyword)
540
- return refuse(e, 'null');
570
+ return [{ t: 'undef', null: true }, p];
541
571
  if (e.kind === ts.SyntaxKind.ThisKeyword) {
542
- if (!p.heap.has('this'))
543
- refuse(e, 'this outside a class');
572
+ if (!p.heap.has('this') || this.s.closure)
573
+ refuse(e, this.s.closure ? 'this in an object a factory makes (its functions close over the factory\'s variables: this is the object, called as it is called)' : 'this outside a class');
544
574
  return [{ t: 'ref', id: 'this' }, p];
545
575
  }
546
576
  if (ts.isIdentifier(e))
@@ -573,7 +603,7 @@ export class Exec {
573
603
  if (ts.isNewExpression(e))
574
604
  return this.newExpr(e, p);
575
605
  if (ts.isArrayLiteralExpression(e))
576
- return refuse(e, 'an array (lists are not in the subset yet)');
606
+ return this.array(e, p);
577
607
  if (ts.isTemplateExpression(e))
578
608
  return refuse(e, 'a template string');
579
609
  if (ts.isArrowFunction(e) || ts.isFunctionExpression(e))
@@ -603,6 +633,9 @@ export class Exec {
603
633
  return refuse(e, `${e.text} (the subset's numbers are integers)`);
604
634
  const sym = this.resolved(e);
605
635
  const decl = sym?.valueDeclaration ?? sym?.declarations?.[0];
636
+ // A factory's own variable (or argument), closed over by the object it returns: a field of that object's state.
637
+ if (decl && this.s.closure?.has(decl))
638
+ return this.field({ t: 'ref', id: 'this' }, e.text, e, p);
606
639
  if (decl && ts.isVariableDeclaration(decl) && decl.initializer && this.s.ours(decl.getSourceFile()) && ts.isVariableStatement(decl.parent.parent) && ts.isSourceFile(decl.parent.parent.parent)) {
607
640
  if (!(decl.parent.flags & ts.NodeFlags.Const))
608
641
  return refuse(e, `${e.text}: a module-level let (state outside the step)`);
@@ -692,14 +725,18 @@ export class Exec {
692
725
  return [{ t: 'bool', e: I.cmp('<', n(r, e.right), n(l, e.left)) }, q2];
693
726
  if (op === S.GreaterThanEqualsToken)
694
727
  return [{ t: 'bool', e: I.cmp('<=', n(r, e.right), n(l, e.left)) }, q2];
728
+ if ((op === S.EqualsEqualsEqualsToken || op === S.EqualsEqualsToken || op === S.ExclamationEqualsEqualsToken || op === S.ExclamationEqualsToken) && (isDate(this.s.checker.getTypeAtLocation(e.left)) || isDate(this.s.checker.getTypeAtLocation(e.right))))
729
+ return refuse(e, 'two Dates compared by === (that is whether they are one object; their times are compared by getTime())');
695
730
  if (op === S.EqualsEqualsEqualsToken || op === S.EqualsEqualsToken)
696
- return [{ t: 'bool', e: this.equal(l, r, e) }, q2];
731
+ return [{ t: 'bool', e: this.equal(l, r, e, op === S.EqualsEqualsToken) }, q2];
697
732
  if (op === S.ExclamationEqualsEqualsToken || op === S.ExclamationEqualsToken)
698
- return [{ t: 'bool', e: I.not(this.equal(l, r, e)) }, q2];
733
+ return [{ t: 'bool', e: I.not(this.equal(l, r, e, op === S.ExclamationEqualsToken)) }, q2];
699
734
  if (op === S.PlusToken || op === S.MinusToken || op === S.AsteriskToken || op === S.PercentToken)
700
735
  return [this.arith(op, l, r, e, q2), q2];
736
+ // A fraction is a binary float in the code: each operation on it rounds (0.1 + 0.2 is not 0.3), so a rule over ℚ
737
+ // (gdp-wired/rationals) is the code only where nothing rounds, which no check shows; refused, with what is read.
701
738
  if (op === S.SlashToken)
702
- return refuse(e, 'a division (its result may be a fraction): Math.floor(x / k) for a constant k is in the subset');
739
+ return refuse(e, 'a division whose result may be a fraction (a binary float in the code: each operation on it rounds, where a rule over the rationals would not; Math.floor or Math.ceil of x / k for a constant k is in the subset)');
703
740
  if (op === S.AsteriskAsteriskToken)
704
741
  return refuse(e, 'a power');
705
742
  if (op === S.CommaToken)
@@ -769,6 +806,9 @@ export class Exec {
769
806
  const fields = new Map();
770
807
  for (const prop of e.properties) {
771
808
  if (ts.isSpreadAssignment(prop)) {
809
+ const st = this.s.checker.getTypeAtLocation(prop.expression);
810
+ if (this.s.checker.getPropertiesOfType(st).some(x => isDate(this.s.checker.getTypeOfSymbolAtLocation(x, prop))))
811
+ refuse(prop, 'a spread of an object holding a Date (the copy and the original share it)');
772
812
  const [v, q2] = this.ev(prop.expression, q);
773
813
  q = q2;
774
814
  const src = v.t === 'ref' ? this.obj(q, v.id) : undefined;
@@ -781,6 +821,8 @@ export class Exec {
781
821
  }
782
822
  else if (ts.isPropertyAssignment(prop)) {
783
823
  const name = ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name) ? prop.name.text : refuse(prop, 'a computed field name on an object');
824
+ if (isDate(this.s.checker.getTypeAtLocation(prop.initializer)) && !freshDate(prop.initializer))
825
+ refuse(prop, `${name}: a Date held elsewhere (one object in two places)`);
784
826
  const [v, q2] = this.ev(prop.initializer, q);
785
827
  q = q2;
786
828
  fields.set(name, v);
@@ -823,6 +865,10 @@ export class Exec {
823
865
  }
824
866
  // Two values within one path, by a condition.
825
867
  phiV(c, x, y, at) {
868
+ if (x.t === 'outside' && y.t === 'outside' && x.path === y.path)
869
+ return x;
870
+ if (x.t === 'opaque' && y.t === 'opaque')
871
+ return x;
826
872
  if (x.t === 'num' && y.t === 'num')
827
873
  return { t: 'num', e: I.nite(c, x.e, y.e) };
828
874
  if (x.t === 'bool' && y.t === 'bool')
@@ -836,11 +882,11 @@ export class Exec {
836
882
  return { t: 'choice', e: I.lite(c, this.asChoice(x, values, at), this.asChoice(y, values, at)), values };
837
883
  }
838
884
  if (x.t === 'undef' && y.t === 'undef')
839
- return x;
885
+ return x.null === y.null ? x : refuse(at, 'null or undefined by a condition');
840
886
  if (x.t === 'opt' || y.t === 'opt' || x.t === 'undef' || y.t === 'undef') {
841
887
  const [hx, vx] = x.t === 'opt' ? [x.has, x.v] : x.t === 'undef' ? [I.F, undefined] : [I.T, x];
842
888
  const [hy, vy] = y.t === 'opt' ? [y.has, y.v] : y.t === 'undef' ? [I.F, undefined] : [I.T, y];
843
- return { t: 'opt', has: I.bite(c, hx, hy), v: !vx ? vy : !vy ? vx : this.phiV(c, vx, vy, at) };
889
+ return { t: 'opt', has: I.bite(c, hx, hy), v: !vx ? vy : !vy ? vx : this.phiV(c, vx, vy, at), ...nullOf(at, x, y) };
844
890
  }
845
891
  if (x.t === 'ref' && y.t === 'ref' && x.id === y.id)
846
892
  return x;
@@ -869,6 +915,8 @@ export class Exec {
869
915
  const x = cell.vals.get(f);
870
916
  return x ? [x, p] : refuse(at, `no field ${f}`);
871
917
  }
918
+ if (v.t === 'outside')
919
+ return refuse(at, `${text(at)}: a function from outside used as a value`);
872
920
  if (v.t !== 'ref')
873
921
  return refuse(at, `${text(at)}: a field of a ${v.t}`);
874
922
  const o = this.obj(p, v.id);
@@ -880,8 +928,33 @@ export class Exec {
880
928
  if (o.lazy) {
881
929
  // Its sort: compared with literals, a choice among them; else its type where it has one (an XState event's
882
930
  // payload, typed by setup's types); else a number.
883
- const typed = (() => { const t = this.s.checker.getTypeAtLocation(at); if (t.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown))
884
- return undefined; const sh = shapeOf(t, this.s.checker, at); return sh.k === 'num' || sh.k === 'bool' || sh.k === 'choice' || sh.k === 'str' ? sh : undefined; })();
931
+ // In XState's guards and actions, a payload field of a type outside the subset (an object, a value that may be
932
+ // missing) is opaque: refused where it is read, kept where it is only passed on.
933
+ let opaque;
934
+ const typed = (() => {
935
+ const t = this.s.checker.getTypeAtLocation(at);
936
+ if (t.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
937
+ if (this.s.xstate && t.flags & ts.TypeFlags.Unknown)
938
+ opaque = `${text(at)} is of type unknown`;
939
+ return undefined;
940
+ }
941
+ let sh;
942
+ try {
943
+ sh = shapeOf(t, this.s.checker, at);
944
+ }
945
+ catch (x) {
946
+ if (this.s.xstate && x instanceof Refusal) {
947
+ opaque = x.what;
948
+ return undefined;
949
+ }
950
+ throw x;
951
+ }
952
+ if (this.s.xstate && sh.k !== 'num' && sh.k !== 'bool' && sh.k !== 'choice' && sh.k !== 'str')
953
+ opaque = `${text(at)} is a ${sh.k === 'rec' ? 'record' : 'map'} (not a value the subset reads)`;
954
+ return sh.k === 'num' || sh.k === 'bool' || sh.k === 'choice' || sh.k === 'str' ? sh : undefined;
955
+ })();
956
+ if (opaque)
957
+ return [{ t: 'opaque', what: opaque }, p];
885
958
  const path = `${o.lazy.prefix}${f}`, sort = this.s.lazySorts.get(path) ?? typed ?? { k: 'num' };
886
959
  // The same field read again (in another guard or action, say) is the same input.
887
960
  const seen = [...this.s.lazy].find(([, l]) => l.prefix === o.lazy.prefix && l.field === f)?.[0];
@@ -891,7 +964,7 @@ export class Exec {
891
964
  return [nv, { ...p, heap: new Map(p.heap).set(v.id, { ...o, fields }) }];
892
965
  }
893
966
  if (v.id === 'this' && this.s.cls) {
894
- const member = this.s.cls.members.find(m => m.name && memberName(m.name) === f);
967
+ const member = this.member(f);
895
968
  if (member && ts.isGetAccessorDeclaration(member))
896
969
  return this.inline({ decl: member, self: 'this' }, [], at, p);
897
970
  if (member && ts.isMethodDeclaration(member))
@@ -917,14 +990,27 @@ export class Exec {
917
990
  if (o?.k === 'map')
918
991
  return refuse(e, `${f} of a map (get, set, has and delete are in the subset)`);
919
992
  }
993
+ if (base.t === 'ref') {
994
+ const o = this.obj(q, base.id);
995
+ if (o?.k === 'list') {
996
+ if (f !== 'length')
997
+ return refuse(e, `${f} of an array (length, [i], push, pop and [i] = x are in the subset)`);
998
+ return [{ t: 'num', e: this.listLen(o, e) }, q];
999
+ }
1000
+ }
920
1001
  if (f === 'length')
921
- return refuse(e, 'length (lists and strings are not in the subset yet)');
1002
+ return refuse(e, 'length of a string (strings are compared and set in the subset)');
922
1003
  return this.field(base, f, e, q);
923
1004
  }
924
1005
  const arg = e.argumentExpression;
925
1006
  const b = this.deref(q, base);
926
1007
  if (b.t === 'ref') {
927
1008
  const o = this.obj(q, b.id);
1009
+ if (o?.k === 'list') {
1010
+ // xs[i]: the item there, or undefined past either end.
1011
+ const [k, q2] = this.ev(arg, q), i = this.num(k, arg, q2), len = this.listLen(o, e);
1012
+ return [{ t: 'opt', has: I.and(I.cmp('<=', I.num(0), i), I.cmp('<', i, len)), v: Exec.cellOf(o.decl, { op: 'num', e: i }, o.decl.fields[0]) }, q2];
1013
+ }
928
1014
  if (o?.k === 'map') {
929
1015
  if (o.decl.js !== 'record')
930
1016
  return refuse(e, 'a Map read with [ ] (get is how a Map is read)');
@@ -1002,10 +1088,24 @@ export class Exec {
1002
1088
  }
1003
1089
  }
1004
1090
  // Assignment: to a local, a field (of this, or of an object the step made), a map's key, or through a cell.
1005
- assign(target, v, p) {
1091
+ assign(target, v, p, setTime = false) {
1006
1092
  if (ts.isParenthesizedExpression(target))
1007
- return this.assign(target.expression, v, p);
1093
+ return this.assign(target.expression, v, p, setTime);
1094
+ // A Date is an object: one written where it is held is a new one (new Date(...)), or its time set (setTime); one taken
1095
+ // from another place would be shared by both (a setTime through one moves the other).
1096
+ if (!setTime && isDate(this.s.checker.getTypeAtLocation(target))) {
1097
+ const rhs = ts.isBinaryExpression(target.parent) && target.parent.left === target ? target.parent.right : undefined;
1098
+ const fresh = (x) => !!x && (ts.isParenthesizedExpression(x) ? fresh(x.expression) : ts.isNewExpression(x) && ts.isIdentifier(x.expression) && x.expression.text === 'Date');
1099
+ if (!fresh(rhs))
1100
+ return refuse(target, `${text(target)} set to a Date held elsewhere (one object in two places: a setTime through one moves the other)`);
1101
+ }
1008
1102
  if (ts.isIdentifier(target)) {
1103
+ const cd = !p.locals.has(target.text) ? this.s.checker.getSymbolAtLocation(target)?.valueDeclaration : undefined;
1104
+ if (cd && this.s.closure?.has(cd)) {
1105
+ if (ts.isVariableDeclaration(cd) && cd.parent.flags & ts.NodeFlags.Const)
1106
+ return refuse(target, `${target.text} is a const`);
1107
+ return this.writeField({ t: 'ref', id: 'this' }, target.text, v, p, target);
1108
+ }
1009
1109
  if (!p.locals.has(target.text))
1010
1110
  return refuse(target, `${target.text}: an assignment to a name outside the step`);
1011
1111
  const sym = this.s.checker.getSymbolAtLocation(target), decl = sym?.valueDeclaration;
@@ -1019,6 +1119,13 @@ export class Exec {
1019
1119
  const b = this.deref(q, base);
1020
1120
  if (ts.isElementAccessExpression(target) && b.t === 'ref') {
1021
1121
  const o = this.obj(q, b.id);
1122
+ if (o?.k === 'list') {
1123
+ // xs[i] = x: within the list only (past its end the array grows, with holes where nothing was set).
1124
+ const [k, q2] = this.ev(target.argumentExpression, q), i = this.num(k, target.argumentExpression, q2), len = this.listLen(o, target);
1125
+ if (!this.known(q2, I.cmp('<=', I.num(0), i)) || !this.known(q2, I.cmp('<', i, len)))
1126
+ return refuse(target, `${text(target)} = ...: an index the code doesn't keep within the array (past its end the array grows, with holes)`);
1127
+ return this.listOp(b.id, { op: 'set', i, x: v, when: I.T }, target, q2);
1128
+ }
1022
1129
  if (o?.k === 'map') {
1023
1130
  const [k, q2] = this.ev(target.argumentExpression, q);
1024
1131
  return this.mapSet(b.id, this.key(k, target.argumentExpression, o.decl), v, target, q2);
@@ -1073,6 +1180,9 @@ export class Exec {
1073
1180
  const om = this.obj(q, old.id);
1074
1181
  if (om?.k === 'map')
1075
1182
  return refuse(target, `${f}: a map replaced in a step (clearing every key is not in the subset yet)`);
1183
+ const nl = this.obj(q, v.id);
1184
+ if (om?.k === 'list' && (nl?.k !== 'list' || nl.base !== om.base || nl.origin === 'this'))
1185
+ return refuse(target, `${f} set to ${nl?.k === 'list' && nl.base !== om.base ? 'another array (one array in two places, or another list)' : nl?.k === 'list' ? 'itself' : 'other than a copy of itself'}`);
1076
1186
  }
1077
1187
  const heap = new Map(q.heap).set(id, { ...o, fields: new Map(o.fields).set(f, v) });
1078
1188
  return { ...q, heap, wrote: o.origin === 'this' ? [...new Set([...q.wrote, f])] : q.wrote };
@@ -1121,15 +1231,60 @@ export class Exec {
1121
1231
  }
1122
1232
  if (ts.isIdentifier(obj) && ['Number', 'Object', 'JSON', 'Array', 'String', 'Promise', 'performance'].includes(obj.text))
1123
1233
  return refuse(e, `${obj.text}.${m}`);
1234
+ // A function the factory is given: its answer is an input of the step (any value of its type), read once.
1235
+ if (ts.isIdentifier(obj) && p.locals.get(obj.text)?.t === 'outside')
1236
+ return this.outside(e, `${p.locals.get(obj.text).path}.${m}`, p);
1124
1237
  if (obj.kind === ts.SyntaxKind.ThisKeyword && this.s.cls) {
1125
- const member = this.s.cls.members.find(x => x.name && memberName(x.name) === m);
1238
+ const member = this.member(m);
1126
1239
  if (member && ts.isMethodDeclaration(member)) {
1127
1240
  const [args, q] = this.args(e.arguments, p);
1128
1241
  return this.inline({ decl: member, self: 'this' }, args, e, q);
1129
1242
  }
1130
1243
  }
1244
+ // super.m(): the nearest m above the class the call is written in (resolved where it is written, not by the object).
1245
+ if (obj.kind === ts.SyntaxKind.SuperKeyword && this.s.chain) {
1246
+ let c = e;
1247
+ while (c && !ts.isClassLike(c))
1248
+ c = c.parent;
1249
+ const k = this.s.chain.indexOf(c), member = k >= 0 ? this.member(m, k + 1) : undefined;
1250
+ if (!member || !ts.isMethodDeclaration(member))
1251
+ return refuse(e, `super.${m}: no method ${m} above ${c?.name?.text ?? 'this class'}`);
1252
+ const [args, q] = this.args(e.arguments, p);
1253
+ return this.inline({ decl: member, self: 'this' }, args, e, q);
1254
+ }
1255
+ // A Date's time: getTime() (or valueOf()) reads it; setTime(t) sets it where the Date is (a field, not a second
1256
+ // name for it).
1257
+ if (isDate(this.s.checker.getTypeAtLocation(obj)) && (m === 'getTime' || m === 'valueOf') && !e.arguments.length) {
1258
+ const [v, q] = this.ev(obj, p);
1259
+ return [{ t: 'num', e: this.num(v, obj, q) }, q];
1260
+ }
1261
+ if (isDate(this.s.checker.getTypeAtLocation(obj)) && m === 'setTime' && e.arguments.length === 1) {
1262
+ const target = ts.isParenthesizedExpression(obj) ? obj.expression : obj;
1263
+ if (!ts.isPropertyAccessExpression(target) && !ts.isElementAccessExpression(target))
1264
+ return refuse(e, `setTime on ${text(target)}, a second name for a Date (its time is set where it is held)`);
1265
+ const [v, q] = this.ev(e.arguments[0], p), t = { t: 'num', e: this.num(v, e.arguments[0], q) };
1266
+ return [t, this.assign(target, t, q, true)];
1267
+ }
1268
+ if (isDate(this.s.checker.getTypeAtLocation(obj)))
1269
+ return refuse(e, `Date.prototype.${m} (a Date's time is read by getTime and set by setTime)`);
1131
1270
  const [base, q] = this.ev(obj, p), b = this.deref(q, base);
1132
1271
  const o = b.t === 'ref' ? this.obj(q, b.id) : undefined;
1272
+ if (o?.k === 'list' && b.t === 'ref') {
1273
+ const [args, q2] = this.args(e.arguments, q);
1274
+ if (m === 'push') {
1275
+ if (args.length !== 1)
1276
+ return refuse(e, 'push of other than one item');
1277
+ const len = this.listLen(o, e);
1278
+ return [{ t: 'num', e: I.add(len, I.num(1)) }, this.listOp(b.id, { op: 'push', x: args[0], when: I.T }, e, q2)];
1279
+ }
1280
+ if (m === 'pop') {
1281
+ if (args.length)
1282
+ return refuse(e, 'pop() takes no argument');
1283
+ const len = this.listLen(o, e), last = { t: 'opt', has: I.cmp('<', I.num(0), len), v: Exec.cellOf(o.decl, { op: 'num', e: I.sub(len, I.num(1)) }, o.decl.fields[0]) };
1284
+ return [last, this.listOp(b.id, { op: 'pop', when: I.T }, e, q2)];
1285
+ }
1286
+ return refuse(e, `the array method ${m} (length, [i], push, pop and [i] = x are in the subset)`);
1287
+ }
1133
1288
  if (o?.k === 'map' && b.t === 'ref') {
1134
1289
  const [args, q2] = this.args(e.arguments, q);
1135
1290
  const k = () => this.key(args[0] ?? refuse(e, `${m} with no key`), e.arguments[0], o.decl);
@@ -1147,6 +1302,13 @@ export class Exec {
1147
1302
  return refuse(e, `${text(callee)}: a method the subset doesn't read`);
1148
1303
  }
1149
1304
  if (ts.isIdentifier(callee)) {
1305
+ // Boolean(x): whether x is truthy.
1306
+ if (callee.text === 'Boolean' && !p.locals.has('Boolean') && e.arguments.length === 1) {
1307
+ const [v, q] = this.ev(e.arguments[0], p);
1308
+ return [{ t: 'bool', e: this.truthy(v, e.arguments[0]) }, q];
1309
+ }
1310
+ if (p.locals.get(callee.text)?.t === 'outside')
1311
+ return this.outside(e, p.locals.get(callee.text).path, p);
1150
1312
  const sym = this.resolved(callee), decl = sym?.valueDeclaration;
1151
1313
  const fn = decl && ts.isFunctionDeclaration(decl) ? decl : decl && ts.isVariableDeclaration(decl) && decl.initializer && (ts.isArrowFunction(decl.initializer) || ts.isFunctionExpression(decl.initializer)) ? decl.initializer : undefined;
1152
1314
  if (fn && this.s.ours(fn.getSourceFile())) {
@@ -1159,6 +1321,74 @@ export class Exec {
1159
1321
  }
1160
1322
  return refuse(e, 'a call the subset doesn\'t read');
1161
1323
  }
1324
+ outside(e, path, p) {
1325
+ const x = this.s.xstate ?? refuse(e, `${path}: a function from outside`);
1326
+ if (e.arguments.some(impure))
1327
+ refuse(e, `${path}(...) with arguments that have effects`);
1328
+ const [, q] = this.args(e.arguments, p);
1329
+ const t = this.s.checker.getTypeAtLocation(e);
1330
+ if (t.flags & (ts.TypeFlags.Void | ts.TypeFlags.Undefined)) {
1331
+ this.s.notes.add(`${path}() is an effect outside the machine (it returns nothing; left out)`);
1332
+ return [{ t: 'undef' }, q];
1333
+ }
1334
+ if (t.getSymbol()?.getName() === 'Promise')
1335
+ return refuse(e, `${path}() returns a promise (an answer that comes later is an actor's, invoked)`);
1336
+ const sh = shapeOf(t, this.s.checker, e);
1337
+ if (sh.k !== 'num' && sh.k !== 'bool' && sh.k !== 'choice' && sh.k !== 'str')
1338
+ return refuse(e, `${path}() returns a compound value`);
1339
+ if ([...x.outside.values()].some(o => o.path === path))
1340
+ return refuse(e, `${path}() read in two places (each call may answer differently)`);
1341
+ const v = this.newVar(path.split('.').pop(), sh, 'input', `${path}(): the answer of a function the machine is given (any value of its type)`);
1342
+ x.outside.set(v.name, { path, sort: sh });
1343
+ return [Exec.valueOf(v), q];
1344
+ }
1345
+ // --- arrays, as lists -------------------------------------------------------------------------------------------
1346
+ // Its length, read before it is written in the step (a read after would need the write's place: refused).
1347
+ listLen(o, at) {
1348
+ if (o.ops.length)
1349
+ return refuse(at, `${o.decl.name} read after it is written in one step`);
1350
+ return I.nvar(`${o.decl.name}_len`);
1351
+ }
1352
+ // A list's write: one a step (the list's apply takes the first of its operations whose condition holds).
1353
+ listOp(id, op, at, p) {
1354
+ id = this.mapId(p, id);
1355
+ const o = p.heap.get(id);
1356
+ if (o.origin === 'input')
1357
+ return refuse(at, `writes to ${o.decl.name} in the state it was given (React and Redux keep the old state; copy it first)`);
1358
+ if (o.origin === 'shared')
1359
+ return refuse(at, 'writes to a module-level array (every call shares it)');
1360
+ if (o.ops.length)
1361
+ return refuse(at, `a second write to ${o.decl.name} in one step (a rule's list takes one write a step)`);
1362
+ const x = op.op === 'pop' ? op : { ...op, x: this.cellValue(op.x, o.decl.fields[0], at, p) };
1363
+ return { ...p, heap: new Map(p.heap).set(id, { ...o, ops: [x] }), wrote: o.origin === 'this' ? [...new Set([...p.wrote, o.decl.name])] : p.wrote };
1364
+ }
1365
+ // [...xs] (a copy), [...xs, x] (a copy with x pushed); [] only as a state's start.
1366
+ array(e, p) {
1367
+ const [first, ...rest] = e.elements;
1368
+ if (!first || !ts.isSpreadElement(first) || rest.length > 1 || rest.some(ts.isSpreadElement))
1369
+ return refuse(e, e.elements.length ? 'an array made other than as [...xs] or [...xs, x]' : 'an empty array in a step (a list emptied whole is not in the subset)');
1370
+ const [v, q] = this.ev(first.expression, p), src = v.t === 'ref' ? this.obj(q, v.id) : undefined;
1371
+ if (src?.k !== 'list')
1372
+ return refuse(first, 'a spread of other than an array');
1373
+ if (src.ops.length)
1374
+ return refuse(first, `${src.decl.name} copied after it is written in one step`);
1375
+ const id = this.id('list');
1376
+ let r = { ...q, heap: new Map(q.heap).set(id, { ...src, origin: 'fresh', ops: [] }) };
1377
+ if (rest[0]) {
1378
+ const [x, r2] = this.ev(rest[0], r);
1379
+ r = this.listOp(id, { op: 'push', x, when: I.T }, rest[0], r2);
1380
+ }
1381
+ return [{ t: 'ref', id }, r];
1382
+ }
1383
+ // A member of the class by name, from the most derived (or from the k-th class up).
1384
+ member(name, from = 0) {
1385
+ for (const c of (this.s.chain ?? (this.s.cls ? [this.s.cls] : [])).slice(from)) {
1386
+ const m = c.members.find(x => x.name && memberName(x.name) === name && !(ts.isMethodDeclaration(x) && !x.body));
1387
+ if (m)
1388
+ return m;
1389
+ }
1390
+ return undefined;
1391
+ }
1162
1392
  math(m, e, p) {
1163
1393
  if (m === 'floor' || m === 'ceil') {
1164
1394
  const a = e.arguments[0];
@@ -1213,8 +1443,18 @@ export class Exec {
1213
1443
  const id = this.id('map');
1214
1444
  return [{ t: 'ref', id }, { ...q, heap: new Map(q.heap).set(id, { ...src, origin: 'fresh' }) }];
1215
1445
  }
1216
- if (ts.isIdentifier(c) && c.text === 'Date')
1217
- return refuse(e, 'new Date() (Date.now() is how the subset reads the time)');
1446
+ // new Date(): the time now (a read of Date.now()); new Date(t): the time t.
1447
+ if (ts.isIdentifier(c) && c.text === 'Date' && !p.locals.has('Date')) {
1448
+ const a = e.arguments ?? [];
1449
+ if (!a.length)
1450
+ return this.now(e, p);
1451
+ if (a.length !== 1)
1452
+ return refuse(e, 'new Date(y, m, ...) (a calendar date)');
1453
+ const [v, q] = this.ev(a[0], p);
1454
+ if (v.t !== 'num')
1455
+ return refuse(e, 'new Date of other than a number of milliseconds');
1456
+ return [v, q];
1457
+ }
1218
1458
  return refuse(e, `new ${text(c)}`);
1219
1459
  }
1220
1460
  // A function of the same module, or a method or getter of the class, run where it is called.
@@ -1234,6 +1474,8 @@ export class Exec {
1234
1474
  d.parameters.forEach((param, i) => {
1235
1475
  if (param.dotDotDotToken)
1236
1476
  refuse(param, 'a rest parameter');
1477
+ if (isDate(this.s.checker.getTypeAtLocation(param)))
1478
+ refuse(param, `${param.name.getText()}: a Date passed as an argument (a second name for it)`);
1237
1479
  let v = args[i];
1238
1480
  if (v === undefined) {
1239
1481
  if (!param.initializer)
@@ -1264,6 +1506,27 @@ export class Exec {
1264
1506
  }
1265
1507
  }
1266
1508
  // A map set on one path and not the other: a generation of its own, so a reference read before either is stale.
1509
+ // Missing as null or as undefined, the same on both sides (a value that is null on one and undefined on the other has
1510
+ // three cases a rule's maybe doesn't hold).
1511
+ const freshDate = (x) => !!x && (ts.isParenthesizedExpression(x) ? freshDate(x.expression) : ts.isNewExpression(x) && ts.isIdentifier(x.expression) && x.expression.text === 'Date');
1512
+ export const isDate = (t) => t.getSymbol()?.getName() === 'Date' || t.aliasSymbol?.getName() === 'Date';
1513
+ const nullOf = (at, a, b) => {
1514
+ const kind = (x) => (x.t === 'opt' || x.t === 'undef' ? (x.null ? 'null' : 'undefined') : undefined);
1515
+ const ka = kind(a), kb = kind(b);
1516
+ if (ka && kb && ka !== kb)
1517
+ refuse(at, 'a value that may be null on one side and undefined on the other');
1518
+ return ka === 'null' || kb === 'null' ? { null: true } : {};
1519
+ };
1520
+ // A map's write and a list's operations by a condition (the Go reader's too).
1521
+ export function mergeWrites(c, wa, wb, base, scalar) {
1522
+ if (!wa && !wb)
1523
+ return undefined;
1524
+ const fill = (w, key) => w ?? { key, ...base(key) };
1525
+ const xa = fill(wa, (wa ?? wb).key), xb = fill(wb, (wb ?? wa).key);
1526
+ const vals = new Map([...xa.vals].map(([f, v]) => [f, scalar(c, v, xb.vals.get(f))]));
1527
+ return { key: I.kite(c, xa.key, xb.key), has: I.bite(c, xa.has, xb.has), vals };
1528
+ }
1529
+ export const mergeOps = (c, a, b) => [...a.map(o => ({ ...o, when: I.and(c, o.when) })), ...b.map(o => ({ ...o, when: I.and(I.not(c), o.when) }))];
1267
1530
  const mergedGen = (a, b) => (a.gen === b.gen ? a.gen : Math.max(a.gen, b.gen) + 1);
1268
1531
  // The module an import names, for a refusal.
1269
1532
  const moduleOf = (d) => { let n = d; while (n && !ts.isImportDeclaration(n))
@@ -9,5 +9,19 @@ export type ReadOptions = {
9
9
  };
10
10
  export declare function readFunctions(code: string, file: string, opts: ReadOptions): Model;
11
11
  export declare function readClass(code: string, file: string, opts: ReadOptions): Model;
12
+ export type Result = {
13
+ k: 'scalar';
14
+ name: string;
15
+ } | {
16
+ k: 'rec';
17
+ fields: [string, string][];
18
+ } | {
19
+ k: 'maybe';
20
+ name: string;
21
+ missing: string;
22
+ null: boolean;
23
+ };
24
+ export declare const isFactory: (code: string, file: string, entry: string) => boolean;
25
+ export declare function readFactory(code: string, file: string, opts: ReadOptions): Model;
12
26
  export declare const isSlice: (code: string, entry: string) => boolean;
13
27
  export declare function readSlice(code: string, file: string, opts: ReadOptions): Model;