@toolproof-core/genesis 1.0.50 → 1.0.52

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.
@@ -1,214 +1,214 @@
1
- import type {
2
- Boolean,
3
- Natural,
4
- ResourceType,
5
- Tool,
6
- } from '../../generated-src/types/types.js';
7
-
8
- type GenericAssignmentByRoleName<GenericRoleName extends string> = Record<GenericRoleName, ResourceType>;
9
-
10
- type InstantiationToolHandleBySpecializationKey = Record<string, Tool>;
11
-
12
- type TemplateInputs<GenericRoleName extends string> = {
13
- GenericAssignmentByRoleName: GenericAssignmentByRoleName<GenericRoleName>;
14
- InstantiationToolHandleBySpecializationKey: InstantiationToolHandleBySpecializationKey;
15
- };
16
-
17
- type TemplateOutputs = {
18
- InstantiationTool: Tool;
19
- };
20
-
21
- type OperandTemplateInputs = TemplateInputs<'OperandType'>;
22
-
23
- const OPERAND_TYPE_ROLE_NAMES = ['OperandType'] as const;
24
-
25
- function assertExactKeys(actualKeys: string[], expectedKeys: readonly string[], label: string): void {
26
- const sortedActualKeys = [...actualKeys].sort();
27
- const sortedExpectedKeys = [...expectedKeys].sort();
28
-
29
- if (sortedActualKeys.length !== sortedExpectedKeys.length) {
30
- throw new Error(
31
- `${label} must contain exactly [${sortedExpectedKeys.join(', ')}], got [${sortedActualKeys.join(', ')}]`
32
- );
33
- }
34
-
35
- for (let index = 0; index < sortedExpectedKeys.length; index += 1) {
36
- if (sortedActualKeys[index] !== sortedExpectedKeys[index]) {
37
- throw new Error(
38
- `${label} must contain exactly [${sortedExpectedKeys.join(', ')}], got [${sortedActualKeys.join(', ')}]`
39
- );
40
- }
41
- }
42
- }
43
-
44
- function asNatural(value: number, label: string): Natural {
45
- if (!Number.isInteger(value) || value < 0) {
46
- throw new Error(`${label} must be a non-negative integer, got ${value}`);
47
- }
48
-
49
- return value as Natural;
50
- }
51
-
52
- export function createSpecializationKey(assignments: Record<string, ResourceType>): string {
53
- return JSON.stringify(
54
- Object.entries(assignments)
55
- .sort(([leftRoleName], [rightRoleName]) => leftRoleName.localeCompare(rightRoleName))
56
- .map(([roleName, resourceType]) => [roleName, resourceType.handle])
57
- );
58
- }
59
-
60
- function resolveTemplateInstantiation<GenericRoleName extends string>(
61
- inputs: TemplateInputs<GenericRoleName>,
62
- expectedGenericRoleNames: readonly GenericRoleName[]
63
- ): TemplateOutputs {
64
- const { GenericAssignmentByRoleName, InstantiationToolHandleBySpecializationKey } = inputs;
65
-
66
- assertExactKeys(
67
- Object.keys(GenericAssignmentByRoleName),
68
- expectedGenericRoleNames,
69
- 'GenericAssignmentByRoleName'
70
- );
71
-
72
- const specializationKey = createSpecializationKey(GenericAssignmentByRoleName);
73
- const instantiationTool = InstantiationToolHandleBySpecializationKey[specializationKey];
74
-
75
- if (!instantiationTool) {
76
- throw new Error(`No instantiation tool registered for specializationKey ${specializationKey}`);
77
- }
78
-
79
- return {
80
- InstantiationTool: instantiationTool,
81
- };
82
- }
83
-
84
- export function Identity(inputs: OperandTemplateInputs): TemplateOutputs {
85
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
86
- }
87
-
88
- export function Add(inputs: OperandTemplateInputs): TemplateOutputs {
89
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
90
- }
91
-
92
- export function Subtract(inputs: OperandTemplateInputs): TemplateOutputs {
93
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
94
- }
95
-
96
- export function Multiply(inputs: OperandTemplateInputs): TemplateOutputs {
97
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
98
- }
99
-
100
- export function Divide(inputs: OperandTemplateInputs): TemplateOutputs {
101
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
102
- }
103
-
104
- export function Double(inputs: OperandTemplateInputs): TemplateOutputs {
105
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
106
- }
107
-
108
- export function LessThan(inputs: OperandTemplateInputs): TemplateOutputs {
109
- return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
110
- }
111
-
112
- export function BooleanIdentity(inputs: { In: Boolean }): { Out: Boolean } {
113
- return {
114
- Out: inputs.In,
115
- };
116
- }
117
-
118
- export function NaturalIdentity(inputs: { In: Natural }): { Out: Natural } {
119
- return {
120
- Out: inputs.In,
121
- };
122
- }
123
-
124
- export function NaturalLessThan(inputs: {
125
- LessThanSource: Natural;
126
- LessThanTarget: Natural;
127
- }): { LessThanDecision: Boolean } {
128
- return {
129
- LessThanDecision: inputs.LessThanSource < inputs.LessThanTarget,
130
- };
131
- }
132
-
133
- export function NaturalAdd(inputs: {
134
- AddendOne: Natural;
135
- AddendTwo: Natural;
136
- }): { Sum: Natural } {
137
- return {
138
- Sum: asNatural(inputs.AddendOne + inputs.AddendTwo, 'Sum'),
139
- };
140
- }
141
-
142
- export function NaturalSubtract(inputs: {
143
- Minuend: Natural;
144
- Subtrahend: Natural;
145
- }): { Difference: Natural } {
146
- return {
147
- Difference: asNatural(inputs.Minuend - inputs.Subtrahend, 'Difference'),
148
- };
149
- }
150
-
151
- export function NaturalMultiply(inputs: {
152
- Multiplicand: Natural;
153
- Multiplier: Natural;
154
- }): { Product: Natural } {
155
- return {
156
- Product: asNatural(inputs.Multiplicand * inputs.Multiplier, 'Product'),
157
- };
158
- }
159
-
160
- export function NaturalDivide(inputs: {
161
- Dividend: Natural;
162
- Divisor: Natural;
163
- }): { Quotient: Natural; Remainder: Natural } {
164
- if (inputs.Divisor === 0) {
165
- throw new Error('Divisor must be non-zero');
166
- }
167
-
168
- return {
169
- Quotient: asNatural(Math.floor(inputs.Dividend / inputs.Divisor), 'Quotient'),
170
- Remainder: asNatural(inputs.Dividend % inputs.Divisor, 'Remainder'),
171
- };
172
- }
173
-
174
- export function NaturalDouble(inputs: { N: Natural }): { Doubled: Natural } {
175
- return {
176
- Doubled: asNatural(inputs.N * 2, 'Doubled'),
177
- };
178
- }
179
-
180
- export const TOOL_IMPLEMENTATION_BY_NAME = {
181
- Identity,
182
- Add,
183
- Subtract,
184
- Multiply,
185
- Divide,
186
- Double,
187
- LessThan,
188
- BooleanIdentity,
189
- NaturalIdentity,
190
- NaturalLessThan,
191
- NaturalAdd,
192
- NaturalSubtract,
193
- NaturalMultiply,
194
- NaturalDivide,
195
- NaturalDouble,
196
- } as const;
197
-
198
- export const TOOL_IMPLEMENTATION_BY_HANDLE = {
199
- 'TOOL-Identity': Identity,
200
- 'TOOL-Add': Add,
201
- 'TOOL-Subtract': Subtract,
202
- 'TOOL-Multiply': Multiply,
203
- 'TOOL-Divide': Divide,
204
- 'TOOL-Double': Double,
205
- 'TOOL-LessThan': LessThan,
206
- 'TOOL-BooleanIdentity': BooleanIdentity,
207
- 'TOOL-NaturalIdentity': NaturalIdentity,
208
- 'TOOL-NaturalLessThan': NaturalLessThan,
209
- 'TOOL-NaturalAdd': NaturalAdd,
210
- 'TOOL-NaturalSubtract': NaturalSubtract,
211
- 'TOOL-NaturalMultiply': NaturalMultiply,
212
- 'TOOL-NaturalDivide': NaturalDivide,
213
- 'TOOL-NaturalDouble': NaturalDouble,
214
- } as const;
1
+ import type {
2
+ Boolean,
3
+ Natural,
4
+ ResourceType,
5
+ Tool,
6
+ } from '../../generated-src/types/types.js';
7
+
8
+ type GenericAssignmentByRoleName<GenericRoleName extends string> = Record<GenericRoleName, ResourceType>;
9
+
10
+ type InstantiationToolHandleBySpecializationKey = Record<string, Tool>;
11
+
12
+ type TemplateInputs<GenericRoleName extends string> = {
13
+ GenericAssignmentByRoleName: GenericAssignmentByRoleName<GenericRoleName>;
14
+ InstantiationToolHandleBySpecializationKey: InstantiationToolHandleBySpecializationKey;
15
+ };
16
+
17
+ type TemplateOutputs = {
18
+ InstantiationTool: Tool;
19
+ };
20
+
21
+ type OperandTemplateInputs = TemplateInputs<'OperandType'>;
22
+
23
+ const OPERAND_TYPE_ROLE_NAMES = ['OperandType'] as const;
24
+
25
+ function assertExactKeys(actualKeys: string[], expectedKeys: readonly string[], label: string): void {
26
+ const sortedActualKeys = [...actualKeys].sort();
27
+ const sortedExpectedKeys = [...expectedKeys].sort();
28
+
29
+ if (sortedActualKeys.length !== sortedExpectedKeys.length) {
30
+ throw new Error(
31
+ `${label} must contain exactly [${sortedExpectedKeys.join(', ')}], got [${sortedActualKeys.join(', ')}]`
32
+ );
33
+ }
34
+
35
+ for (let index = 0; index < sortedExpectedKeys.length; index += 1) {
36
+ if (sortedActualKeys[index] !== sortedExpectedKeys[index]) {
37
+ throw new Error(
38
+ `${label} must contain exactly [${sortedExpectedKeys.join(', ')}], got [${sortedActualKeys.join(', ')}]`
39
+ );
40
+ }
41
+ }
42
+ }
43
+
44
+ function asNatural(value: number, label: string): Natural {
45
+ if (!Number.isInteger(value) || value < 0) {
46
+ throw new Error(`${label} must be a non-negative integer, got ${value}`);
47
+ }
48
+
49
+ return value as Natural;
50
+ }
51
+
52
+ export function createSpecializationKey(assignments: Record<string, ResourceType>): string {
53
+ return JSON.stringify(
54
+ Object.entries(assignments)
55
+ .sort(([leftRoleName], [rightRoleName]) => leftRoleName.localeCompare(rightRoleName))
56
+ .map(([roleName, resourceType]) => [roleName, resourceType.handle])
57
+ );
58
+ }
59
+
60
+ function resolveTemplateInstantiation<GenericRoleName extends string>(
61
+ inputs: TemplateInputs<GenericRoleName>,
62
+ expectedGenericRoleNames: readonly GenericRoleName[]
63
+ ): TemplateOutputs {
64
+ const { GenericAssignmentByRoleName, InstantiationToolHandleBySpecializationKey } = inputs;
65
+
66
+ assertExactKeys(
67
+ Object.keys(GenericAssignmentByRoleName),
68
+ expectedGenericRoleNames,
69
+ 'GenericAssignmentByRoleName'
70
+ );
71
+
72
+ const specializationKey = createSpecializationKey(GenericAssignmentByRoleName);
73
+ const instantiationTool = InstantiationToolHandleBySpecializationKey[specializationKey];
74
+
75
+ if (!instantiationTool) {
76
+ throw new Error(`No instantiation tool registered for specializationKey ${specializationKey}`);
77
+ }
78
+
79
+ return {
80
+ InstantiationTool: instantiationTool,
81
+ };
82
+ }
83
+
84
+ export function Identity(inputs: OperandTemplateInputs): TemplateOutputs {
85
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
86
+ }
87
+
88
+ export function Add(inputs: OperandTemplateInputs): TemplateOutputs {
89
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
90
+ }
91
+
92
+ export function Subtract(inputs: OperandTemplateInputs): TemplateOutputs {
93
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
94
+ }
95
+
96
+ export function Multiply(inputs: OperandTemplateInputs): TemplateOutputs {
97
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
98
+ }
99
+
100
+ export function Divide(inputs: OperandTemplateInputs): TemplateOutputs {
101
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
102
+ }
103
+
104
+ export function Double(inputs: OperandTemplateInputs): TemplateOutputs {
105
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
106
+ }
107
+
108
+ export function LessThan(inputs: OperandTemplateInputs): TemplateOutputs {
109
+ return resolveTemplateInstantiation(inputs, OPERAND_TYPE_ROLE_NAMES);
110
+ }
111
+
112
+ export function BooleanIdentity(inputs: { In: Boolean }): { Out: Boolean } {
113
+ return {
114
+ Out: inputs.In,
115
+ };
116
+ }
117
+
118
+ export function NaturalIdentity(inputs: { In: Natural }): { Out: Natural } {
119
+ return {
120
+ Out: inputs.In,
121
+ };
122
+ }
123
+
124
+ export function NaturalLessThan(inputs: {
125
+ LessThanSource: Natural;
126
+ LessThanTarget: Natural;
127
+ }): { LessThanDecision: Boolean } {
128
+ return {
129
+ LessThanDecision: inputs.LessThanSource < inputs.LessThanTarget,
130
+ };
131
+ }
132
+
133
+ export function NaturalAdd(inputs: {
134
+ AddendOne: Natural;
135
+ AddendTwo: Natural;
136
+ }): { Sum: Natural } {
137
+ return {
138
+ Sum: asNatural(inputs.AddendOne + inputs.AddendTwo, 'Sum'),
139
+ };
140
+ }
141
+
142
+ export function NaturalSubtract(inputs: {
143
+ Minuend: Natural;
144
+ Subtrahend: Natural;
145
+ }): { Difference: Natural } {
146
+ return {
147
+ Difference: asNatural(inputs.Minuend - inputs.Subtrahend, 'Difference'),
148
+ };
149
+ }
150
+
151
+ export function NaturalMultiply(inputs: {
152
+ Multiplicand: Natural;
153
+ Multiplier: Natural;
154
+ }): { Product: Natural } {
155
+ return {
156
+ Product: asNatural(inputs.Multiplicand * inputs.Multiplier, 'Product'),
157
+ };
158
+ }
159
+
160
+ export function NaturalDivide(inputs: {
161
+ Dividend: Natural;
162
+ Divisor: Natural;
163
+ }): { Quotient: Natural; Remainder: Natural } {
164
+ if (inputs.Divisor === 0) {
165
+ throw new Error('Divisor must be non-zero');
166
+ }
167
+
168
+ return {
169
+ Quotient: asNatural(Math.floor(inputs.Dividend / inputs.Divisor), 'Quotient'),
170
+ Remainder: asNatural(inputs.Dividend % inputs.Divisor, 'Remainder'),
171
+ };
172
+ }
173
+
174
+ export function NaturalDouble(inputs: { N: Natural }): { Doubled: Natural } {
175
+ return {
176
+ Doubled: asNatural(inputs.N * 2, 'Doubled'),
177
+ };
178
+ }
179
+
180
+ export const TOOL_IMPLEMENTATION_BY_NAME = {
181
+ Identity,
182
+ Add,
183
+ Subtract,
184
+ Multiply,
185
+ Divide,
186
+ Double,
187
+ LessThan,
188
+ BooleanIdentity,
189
+ NaturalIdentity,
190
+ NaturalLessThan,
191
+ NaturalAdd,
192
+ NaturalSubtract,
193
+ NaturalMultiply,
194
+ NaturalDivide,
195
+ NaturalDouble,
196
+ } as const;
197
+
198
+ export const TOOL_IMPLEMENTATION_BY_HANDLE = {
199
+ 'TOOL-Identity': Identity,
200
+ 'TOOL-Add': Add,
201
+ 'TOOL-Subtract': Subtract,
202
+ 'TOOL-Multiply': Multiply,
203
+ 'TOOL-Divide': Divide,
204
+ 'TOOL-Double': Double,
205
+ 'TOOL-LessThan': LessThan,
206
+ 'TOOL-BooleanIdentity': BooleanIdentity,
207
+ 'TOOL-NaturalIdentity': NaturalIdentity,
208
+ 'TOOL-NaturalLessThan': NaturalLessThan,
209
+ 'TOOL-NaturalAdd': NaturalAdd,
210
+ 'TOOL-NaturalSubtract': NaturalSubtract,
211
+ 'TOOL-NaturalMultiply': NaturalMultiply,
212
+ 'TOOL-NaturalDivide': NaturalDivide,
213
+ 'TOOL-NaturalDouble': NaturalDouble,
214
+ } as const;