lawspec 0.24.0 → 0.26.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 (148) hide show
  1. package/CHANGELOG.md +109 -1
  2. package/README.md +34 -26
  3. package/UNICODE-LICENSE.txt +39 -0
  4. package/api.mjs +6 -23
  5. package/bin/lawspec.mjs +148 -65
  6. package/build.json +245 -177
  7. package/builtin-data-names.mjs +73 -0
  8. package/core.wasm +2 -2
  9. package/core_jsffi.js +5 -0
  10. package/examples/native-payments/clojure/lawspec.json +13 -2
  11. package/examples/native-payments/csharp/lawspec.json +13 -2
  12. package/examples/native-payments/elixir/lawspec.json +14 -3
  13. package/examples/native-payments/erlang/lawspec.json +14 -3
  14. package/examples/native-payments/fsharp/lawspec.json +14 -3
  15. package/examples/native-payments/gleam/lawspec.json +14 -3
  16. package/examples/native-payments/go/lawspec.json +14 -3
  17. package/examples/native-payments/groovy/lawspec.json +14 -3
  18. package/examples/native-payments/haskell/lawspec.json +14 -3
  19. package/examples/native-payments/java/lawspec.json +14 -3
  20. package/examples/native-payments/javascript/lawspec.json +14 -3
  21. package/examples/native-payments/kotlin/lawspec.json +14 -3
  22. package/examples/native-payments/python/lawspec.json +14 -3
  23. package/examples/native-payments/rust/lawspec.json +14 -3
  24. package/examples/native-payments/scala/lawspec.json +14 -3
  25. package/examples/native-payments/swift/lawspec.json +14 -3
  26. package/examples/native-payments/typescript/lawspec.json +14 -3
  27. package/examples/packages/orders/lawspec.json +25 -5
  28. package/examples/packages/orders/orders.lawspec +57 -53
  29. package/examples/packages/shop-domain/src/domain.lawspec +36 -33
  30. package/examples/packages/shop-tax-1/src/api.lawspec +1 -0
  31. package/examples/packages/shop-tax-1/src/rates.lawspec +5 -4
  32. package/examples/packages/shop-tax-2/src/api.lawspec +1 -0
  33. package/examples/packages/shop-tax-2/src/rates.lawspec +6 -4
  34. package/examples/rc1/authorization-snapshots.lawspec +113 -0
  35. package/examples/rc1/banner-filenames.lawspec +64 -0
  36. package/examples/rc1/bit-codec.lawspec +57 -0
  37. package/examples/rc1/bounded-witness-search.lawspec +151 -0
  38. package/examples/rc1/conflicting-requirements.lawspec +31 -0
  39. package/examples/rc1/constructive-witnesses.lawspec +120 -0
  40. package/examples/rc1/documentary-review.lawspec +43 -0
  41. package/examples/rc1/empty-domains.lawspec +47 -0
  42. package/examples/rc1/finite-integer-domains.lawspec +76 -0
  43. package/examples/rc1/law-metadata.lawspec +30 -0
  44. package/examples/rc1/native-payments.lawspec +108 -0
  45. package/examples/rc1/nesting/rrr.lawspec +34 -0
  46. package/examples/rc1/nesting/rru.lawspec +57 -0
  47. package/examples/rc1/nesting/rur.lawspec +58 -0
  48. package/examples/rc1/nesting/ruu.lawspec +78 -0
  49. package/examples/rc1/nesting/urr.lawspec +53 -0
  50. package/examples/rc1/nesting/uru.lawspec +73 -0
  51. package/examples/rc1/nesting/uur.lawspec +74 -0
  52. package/examples/rc1/nesting/uuu.lawspec +96 -0
  53. package/examples/rc1/order-workflow.lawspec +127 -0
  54. package/examples/rc1/parcel-measurements.lawspec +93 -0
  55. package/examples/rc1/picture-records.lawspec +52 -0
  56. package/examples/rc1/picture-tuples.lawspec +48 -0
  57. package/examples/rc1/record-payloads.lawspec +57 -0
  58. package/examples/rc1/recursive-records.lawspec +65 -0
  59. package/examples/rc1/refund-conformance.lawspec +45 -0
  60. package/examples/rc1/refund-policy.lawspec +89 -0
  61. package/examples/rc1/requirement-composition.lawspec +90 -0
  62. package/examples/rc1/structural-records.lawspec +54 -0
  63. package/examples/rc1-authoring.lawspec +57 -0
  64. package/examples/specs/abilities.lawspec +67 -65
  65. package/examples/specs/ability_imports.lawspec +58 -57
  66. package/examples/specs/actors.lawspec +23 -22
  67. package/examples/specs/algebra.lawspec +142 -141
  68. package/examples/specs/approvals.lawspec +16 -15
  69. package/examples/specs/async_bindings.lawspec +10 -9
  70. package/examples/specs/async_fetch.lawspec +12 -11
  71. package/examples/specs/atoi_codec.lawspec +16 -15
  72. package/examples/specs/boolean_flags.lawspec +10 -9
  73. package/examples/specs/builtins.lawspec +73 -72
  74. package/examples/specs/canonical_url.lawspec +14 -13
  75. package/examples/specs/collections.lawspec +153 -154
  76. package/examples/specs/composition.lawspec +70 -66
  77. package/examples/specs/concurrent_collections.lawspec +8 -7
  78. package/examples/specs/consistency.lawspec +4 -3
  79. package/examples/specs/contracts.lawspec +80 -79
  80. package/examples/specs/crypto.lawspec +26 -25
  81. package/examples/specs/ctc.lawspec +148 -0
  82. package/examples/specs/currying.lawspec +32 -31
  83. package/examples/specs/data_types.lawspec +50 -49
  84. package/examples/specs/distribution.lawspec +57 -56
  85. package/examples/specs/domain_modeling.lawspec +48 -47
  86. package/examples/specs/durations.lawspec +39 -38
  87. package/examples/specs/equivalent.lawspec +25 -24
  88. package/examples/specs/failures.lawspec +64 -63
  89. package/examples/specs/finite_data.lawspec +32 -31
  90. package/examples/specs/flow_stack.lawspec +64 -63
  91. package/examples/specs/gadt_expressions.lawspec +50 -49
  92. package/examples/specs/generation.lawspec +115 -114
  93. package/examples/specs/handler_bindings.lawspec +42 -40
  94. package/examples/specs/handles.lawspec +2 -1
  95. package/examples/specs/harness.lawspec +45 -43
  96. package/examples/specs/harness_testing.lawspec +18 -13
  97. package/examples/specs/indexed_arithmetic.lawspec +401 -126
  98. package/examples/specs/indexed_families.lawspec +182 -66
  99. package/examples/specs/keywords.lawspec +22 -21
  100. package/examples/specs/limits.lawspec +6 -5
  101. package/examples/specs/list_contracts.lawspec +121 -120
  102. package/examples/specs/list_refinements.lawspec +43 -42
  103. package/examples/specs/lists.lawspec +45 -44
  104. package/examples/specs/matchers.lawspec +79 -78
  105. package/examples/specs/matching.lawspec +43 -42
  106. package/examples/specs/mixed_inputs.lawspec +25 -24
  107. package/examples/specs/models.lawspec +44 -43
  108. package/examples/specs/parse_port.lawspec +28 -27
  109. package/examples/specs/payments.lawspec +55 -54
  110. package/examples/specs/railway.lawspec +59 -58
  111. package/examples/specs/records.lawspec +149 -148
  112. package/examples/specs/records_inner.lawspec +5 -4
  113. package/examples/specs/recursive_refinements.lawspec +30 -29
  114. package/examples/specs/refined_definitions.lawspec +24 -23
  115. package/examples/specs/refinements.lawspec +58 -57
  116. package/examples/specs/resilience.lawspec +159 -158
  117. package/examples/specs/resources.lawspec +61 -60
  118. package/examples/specs/scalar_adapters.lawspec +60 -59
  119. package/examples/specs/scalar_catalog.lawspec +165 -164
  120. package/examples/specs/scalars.lawspec +78 -77
  121. package/examples/specs/scheduling_order.lawspec +8 -7
  122. package/examples/specs/scheduling_overlap.lawspec +5 -4
  123. package/examples/specs/sequences.lawspec +66 -65
  124. package/examples/specs/sessions.lawspec +13 -12
  125. package/examples/specs/slug.lawspec +16 -15
  126. package/examples/specs/sum_refinements.lawspec +42 -41
  127. package/examples/specs/tables.lawspec +29 -28
  128. package/examples/specs/total_functions.lawspec +71 -70
  129. package/examples/specs/workflows.lawspec +101 -100
  130. package/examples-command.mjs +8 -4
  131. package/explain.mjs +114 -0
  132. package/expression-presentation.mjs +333 -0
  133. package/files.mjs +16 -0
  134. package/index.d.ts +406 -11
  135. package/launcher.mjs +9 -2
  136. package/lsp.mjs +235 -0
  137. package/native-evidence.mjs +187 -0
  138. package/native-examples.mjs +8 -1
  139. package/node-events-reporter.mjs +23 -0
  140. package/package.json +8 -1
  141. package/positional-fields.mjs +13 -0
  142. package/proof.d.ts +35 -0
  143. package/proof.mjs +174 -0
  144. package/starter-rc1.lawspec +26 -0
  145. package/starter.lawspec +16 -15
  146. package/templates.mjs +6 -4
  147. package/test-command.mjs +107 -35
  148. package/typed-presentation.mjs +165 -0
@@ -0,0 +1,165 @@
1
+ // Generated from templates/npm/typed-presentation.mjs by lawspec-dev generate. Do not edit.
2
+ // Presentation only: identities, constraints and proof evidence remain in Core.
3
+ // In particular, a nominal record is never expanded into a structural product.
4
+ import { createExpressionPresentation } from './expression-presentation.mjs';
5
+ import { builtinDataNames } from './builtin-data-names.mjs';
6
+ import { showScalar } from './scalars.mjs';
7
+ import { positionalFields } from './positional-fields.mjs';
8
+
9
+ export function createTypePresentation(result) {
10
+ const declarations = new Map([...builtinDataNames, ...(result.dataTypes ?? [])].map(d => [d.id, d]));
11
+ const constructors = new Map([...declarations.values()].flatMap(d =>
12
+ d.constructors.map(c => [c.id, { declaration: d, constructor: c }])));
13
+ const shortName = name => ({ BigUInt: 'Whole', BigInt: 'Integer', Natural: 'Whole' })[name]
14
+ ?? declarations.get(name)?.name ?? name.split('::type::').at(-1);
15
+ const names = new Map();
16
+ for (const d of declarations.values()) {
17
+ const name = shortName(d.id);
18
+ names.set(name, (names.get(name) ?? 0) + 1);
19
+ }
20
+ const nominalName = name => {
21
+ const display = shortName(name), separator = name.indexOf('::type::');
22
+ return names.get(display) > 1 && separator >= 0
23
+ ? `${name.slice(0, separator)}.${display}` : display;
24
+ };
25
+
26
+ // The compiler marks ordinary checked recursive representation definitions.
27
+ // Names alone do not authorize hiding a call, and its full signature must
28
+ // agree with the typed application. The wire assertion is never rewritten.
29
+ const recursiveViews = new Map((result.units ?? []).flatMap(unit => unit.declarations
30
+ .filter(d => d.representation === 'structural-record-storage' && d.source === undefined
31
+ && d.origin?.kind === 'generated' && d.origin.declaration === d.id && d.type.kind === 'function')
32
+ .map(d => [d.id, d.type])));
33
+ function sameType(left, right) {
34
+ if (!left || !right || left.kind !== right.kind) return false;
35
+ if (left.kind === 'variable') return left.id === right.id;
36
+ if (left.kind === 'function') return sameType(left.parameter, right.parameter) && sameType(left.result, right.result);
37
+ if (left.kind !== 'constructor' || left.name !== right.name || left.arguments.length !== right.arguments.length) return false;
38
+ return left.arguments.every((a, i) => {
39
+ const b = right.arguments[i];
40
+ return a.kind === b.kind && (a.kind === 'type' ? sameType(a.type, b.type)
41
+ : a.kind === 'natural' ? a.value === b.value : a.kind === 'indexVariable' && a.id === b.id);
42
+ });
43
+ }
44
+
45
+ // Recognize only compiler-generated storage views which copy every field in
46
+ // order. Do not simplify user matches, erase conversions or discard unused
47
+ // constructor arguments: those may contain checks or undefined computations.
48
+ function storageView(expression) {
49
+ const node = expression.node;
50
+ if (node?.kind === 'call') {
51
+ const signature = recursiveViews.get(node.declaration);
52
+ return signature && node.arguments.length === 1
53
+ && sameType(signature.parameter, node.arguments[0].type) && sameType(signature.result, expression.type)
54
+ ? node.arguments[0] : undefined;
55
+ }
56
+ if (node?.kind !== 'match' || node.cases.length !== 1) return undefined;
57
+ const branch = node.cases[0], body = branch.body.node;
58
+ const source = constructors.get(branch.constructor), target = constructors.get(body?.constructor);
59
+ if (body?.kind !== 'construct' || !source || !target
60
+ || source.declaration.structure?.kind !== 'record' || target.declaration.structure?.kind !== 'record'
61
+ || expression.origin?.kind !== 'generated'
62
+ || !branch.binders.length || !branch.binders.every(b => b.id.startsWith('lawspec::record-view::'))
63
+ || branch.binders.length !== source.constructor.fields.length
64
+ || body.arguments.length !== branch.binders.length
65
+ || target.constructor.fields.length !== branch.binders.length) return undefined;
66
+ for (let i = 0; i < branch.binders.length; i++) {
67
+ if (source.constructor.fields[i].name !== target.constructor.fields[i].name) return undefined;
68
+ let argument = body.arguments[i], unwrapped;
69
+ while ((unwrapped = storageView(argument)) !== undefined) argument = unwrapped;
70
+ if (argument.node?.kind !== 'local' || argument.node.id !== branch.binders[i].id) return undefined;
71
+ }
72
+ return node.value;
73
+ }
74
+
75
+ function expressionText(expression, locals) {
76
+ const view = storageView(expression);
77
+ if (view) return expressionText(view, locals);
78
+ const node = expression.node;
79
+ switch (node?.kind) {
80
+ case 'local': return locals.get(node.id) ?? expression.text;
81
+ case 'field': {
82
+ const field = constructors.get(node.constructor)?.constructor.fields[node.index];
83
+ if (!field) return expression.text;
84
+ let value = node.value, unwrapped;
85
+ while ((unwrapped = storageView(value)) !== undefined) value = unwrapped;
86
+ const text = expressionText(value, locals);
87
+ return `${['local', 'field'].includes(value.node?.kind) ? text : `(${text})`}.${field.name}`;
88
+ }
89
+ case 'binary':
90
+ case 'shortCircuit': return `(${expressionText(node.left, locals)} ${{ '==': '=', '&&': 'and', '||': 'or' }[node.operator] ?? node.operator} ${expressionText(node.right, locals)})`;
91
+ case 'unary': return `${node.operator === '!' ? 'not' : node.operator} (${expressionText(node.argument, locals)})`;
92
+ // Keep unfamiliar expressions verbatim; never omit a restriction because
93
+ // a presentation does not yet know its typed node.
94
+ default: return expression.text;
95
+ }
96
+ }
97
+
98
+ function typeName(type, substitutions = new Map(), active = new Set()) {
99
+ if (type.kind === 'variable') {
100
+ const replacement = substitutions.get(type.id);
101
+ return replacement ? typeName(replacement) : type.id;
102
+ }
103
+ if (type.kind === 'function') return `(${typeName(type.parameter, substitutions, active)} -> ${typeName(type.result, substitutions, active)})`;
104
+ if (type.kind !== 'constructor') return type.id;
105
+ const declaration = declarations.get(type.name);
106
+ const argumentsText = () => type.arguments.map(a => {
107
+ if (a.kind !== 'type') return a.value ?? a.id;
108
+ const text = typeName(a.type, substitutions, active);
109
+ return a.type.kind === 'constructor' && a.type.arguments.length ? `(${text})` : text;
110
+ }).join(' ');
111
+ const named = () => nominalName(type.name) + (type.arguments.length ? ` ${argumentsText()}` : '');
112
+ if (declaration?.structure?.kind !== 'record' || active.has(type.name)) return named();
113
+ const constructor = declaration.constructors[0];
114
+ const bindings = new Map(substitutions);
115
+ declaration.parameters.forEach((parameter, i) => {
116
+ const argument = type.arguments[i];
117
+ if (argument?.kind === 'type') bindings.set(parameter, substitute(argument.type, substitutions));
118
+ });
119
+ const nested = new Set([...active, type.name]);
120
+ const positions = positionalFields(declaration);
121
+ const ordered = positions ?? constructor.fields;
122
+ const locals = new Map(ordered.map((f, index) => [f.id, positions ? `component${index + 1}` : f.name]));
123
+ const fields = ordered.map(field => {
124
+ const base = typeName(field.type, bindings, nested);
125
+ const predicates = declaration.structure.fields.find(f => f.name === field.name)?.predicates ?? [];
126
+ const name = locals.get(field.id);
127
+ const refined = predicates.length
128
+ ? `(${name} of type ${base} where ${predicates.map(p => expressionText(p, locals)).join(' and ')})`
129
+ : base;
130
+ return positions ? refined : `${field.name} of type ${refined}`;
131
+ });
132
+ if (positions) return `(${fields.join(', ')})`;
133
+ return `record{${fields.join(', ')}}`;
134
+ }
135
+ function substitute(type, bindings) {
136
+ if (type.kind === 'variable') return bindings.get(type.id) ?? type;
137
+ if (type.kind === 'function') return { ...type, parameter: substitute(type.parameter, bindings), result: substitute(type.result, bindings) };
138
+ if (type.kind === 'constructor') return { ...type, arguments: type.arguments.map(a => a.kind === 'type'
139
+ ? { ...a, type: substitute(a.type, bindings) } : a) };
140
+ return type;
141
+ }
142
+ // Only concrete bindings have already passed the compiler's stored-value
143
+ // validation. Keep that fact outside the wire expression objects.
144
+ const storedValues = new WeakSet();
145
+ const checkedExpressionText = createExpressionPresentation(result, { constructors, typeName, storageView,
146
+ isStoredValue: expression => storedValues.has(expression) });
147
+ // Example bindings are already checked concrete values. Render their stored
148
+ // structure through the same constructor metadata; do not execute anything.
149
+ function valueExpression(value) {
150
+ const text = JSON.stringify(value);
151
+ if (value?.kind === 'data') {
152
+ const expression = {type: value.type, text,
153
+ node: {kind: 'construct', constructor: value.constructor,
154
+ arguments: value.fields.map(valueExpression)}};
155
+ storedValues.add(expression);
156
+ return expression;
157
+ }
158
+ return {type: {kind: 'constructor', name: value.type, arguments: []},
159
+ text: showScalar(value), node: {kind: 'constant', value}};
160
+ }
161
+ function valueText(value, scope = new Map()) {
162
+ return checkedExpressionText(valueExpression(value), scope);
163
+ }
164
+ return { typeName, expressionText: checkedExpressionText, valueText };
165
+ }