tla-ts 0.0.3-dev.c8c988d → 0.0.3-dev.ccbff8c
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/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +163 -46
- package/dist/index.d.ts +163 -46
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -22
- package/dist/run-cli.js +1351 -22
- package/dist/transfer.cjs +83 -33
- package/dist/transfer.js +83 -33
- package/package.json +5 -1
- package/src/ast.ts +51 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -17
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1317 -428
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -21
- package/src/config.ts +225 -206
- package/src/environment.ts +417 -129
- package/src/errors.ts +155 -18
- package/src/evaluate.ts +713 -259
- package/src/exit-codes.ts +6 -24
- package/src/fingerprints.ts +1 -2
- package/src/index.ts +1 -0
- package/src/instances.ts +3 -1
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +44 -28
- package/src/lexer.ts +32 -4
- package/src/live-check.ts +204 -148
- package/src/liveness.ts +191 -122
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +6 -1
- package/src/parallel.ts +57 -6
- package/src/parser.ts +405 -167
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1397 -457
- package/src/run-cli.ts +1161 -196
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +25 -5
- package/src/standard-modules.ts +172 -129
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +5 -8
- package/src/successors.ts +141 -173
- package/src/symmetry.ts +168 -50
- package/src/tableau.ts +3 -1
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +49 -21
- package/src/tlc-modules.ts +0 -4
- package/src/tlc-order.ts +55 -9
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/values.ts +664 -317
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
// The semantic nodes of SANY's tla2sany.semantic package that its XMLExporter writes out, with the
|
|
2
|
+
// fields that the export and SANY's level checking read.
|
|
3
|
+
|
|
4
|
+
export interface SanyLocation {
|
|
5
|
+
file: string;
|
|
6
|
+
beginLine: number;
|
|
7
|
+
beginColumn: number;
|
|
8
|
+
endLine: number;
|
|
9
|
+
endColumn: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// An error or warning SANY reports, with the location its message names. A location of undefined
|
|
13
|
+
// is SANY's unknown location, and one that is just a module name stands for the whole module.
|
|
14
|
+
export interface SanyMessage {
|
|
15
|
+
location: SanyLocation | string | undefined;
|
|
16
|
+
text: string;
|
|
17
|
+
isWarning: boolean;
|
|
18
|
+
// The ErrorCode SANY files a message under, where another check files the same text.
|
|
19
|
+
code?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Errors.addMessage: SANY keeps one copy of each message, and after TLC 1.7.4 one for each code.
|
|
23
|
+
export const addSanyMessage = (messages: SanyMessage[], message: SanyMessage) => {
|
|
24
|
+
const key = JSON.stringify(message);
|
|
25
|
+
if (!messages.some((candidate) => JSON.stringify(candidate) === key)) messages.push(message);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Location.toString.
|
|
29
|
+
export const formatSanyLocation = (location: SanyMessage["location"]) => {
|
|
30
|
+
if (location === undefined) return "Unknown location";
|
|
31
|
+
if (typeof location === "string") return `In module ${location}`;
|
|
32
|
+
// Location.toString names only the module of a location at line 0, column 0.
|
|
33
|
+
if (
|
|
34
|
+
location.beginLine === 0 &&
|
|
35
|
+
location.beginColumn === 0 &&
|
|
36
|
+
location.endLine === 0 &&
|
|
37
|
+
location.endColumn === 0
|
|
38
|
+
) {
|
|
39
|
+
return `In module ${location.file}`;
|
|
40
|
+
}
|
|
41
|
+
return `line ${location.beginLine}, col ${location.beginColumn} to line ${location.endLine}, col ${location.endColumn} of module ${location.file}`;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// SANY's ArgLevelParam: the level of argument index of operator op depends on param.
|
|
45
|
+
export interface ArgLevelParam {
|
|
46
|
+
op: SymbolNode;
|
|
47
|
+
index: number;
|
|
48
|
+
param: SymbolNode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// SANY's ParamAndPosition with its level in a SetOfArgLevelConstraints: the argument at position of
|
|
52
|
+
// operator param must be allowed a level of at least level.
|
|
53
|
+
export interface ArgLevelConstraint {
|
|
54
|
+
param: SymbolNode;
|
|
55
|
+
position: number;
|
|
56
|
+
level: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The fields of SANY's LevelNode. levelChecked is 0 until levelCheck runs, and SANY exports a level
|
|
60
|
+
// only for the nodes it has level-checked.
|
|
61
|
+
export interface LevelFields {
|
|
62
|
+
id: number;
|
|
63
|
+
levelChecked: number;
|
|
64
|
+
level: number;
|
|
65
|
+
levelParams: Set<SymbolNode>;
|
|
66
|
+
allParams: Set<SymbolNode>;
|
|
67
|
+
nonLeibnizParams: Set<SymbolNode>;
|
|
68
|
+
argLevelParams: Map<string, ArgLevelParam>;
|
|
69
|
+
levelCorrect: boolean;
|
|
70
|
+
// SetOfLevelConstraints: the highest level each parameter may have.
|
|
71
|
+
levelConstraints: Map<SymbolNode, number>;
|
|
72
|
+
argLevelConstraints: Map<string, ArgLevelConstraint>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// SANY's kinds of declared symbols, from tla2sany.semantic.ASTConstants.
|
|
76
|
+
export const CONSTANT_DECL_KIND = 2;
|
|
77
|
+
export const VARIABLE_DECL_KIND = 3;
|
|
78
|
+
export const NEW_CONSTANT_KIND = 24;
|
|
79
|
+
|
|
80
|
+
export interface FormalParamNode extends LevelFields {
|
|
81
|
+
kind: "FormalParam";
|
|
82
|
+
name: string;
|
|
83
|
+
arity: number;
|
|
84
|
+
location: SanyLocation | undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface OpDeclNode extends LevelFields {
|
|
88
|
+
kind: "OpDecl";
|
|
89
|
+
name: string;
|
|
90
|
+
arity: number;
|
|
91
|
+
declKind: number;
|
|
92
|
+
location: SanyLocation;
|
|
93
|
+
module: ModuleNode;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface OpDefNode extends LevelFields {
|
|
97
|
+
kind: "OpDef";
|
|
98
|
+
defKind: "UserDefined" | "BuiltIn" | "ModuleInstance" | "NumberedProofStep";
|
|
99
|
+
name: string;
|
|
100
|
+
arity: number;
|
|
101
|
+
params: FormalParamNode[] | undefined;
|
|
102
|
+
body: ExprNode | undefined;
|
|
103
|
+
isLocal: boolean;
|
|
104
|
+
location: SanyLocation;
|
|
105
|
+
module: ModuleNode | undefined;
|
|
106
|
+
source: OpDefNode | undefined;
|
|
107
|
+
preComments: string[];
|
|
108
|
+
isDefined: boolean;
|
|
109
|
+
inRecursive: boolean;
|
|
110
|
+
inRecursiveSection: boolean;
|
|
111
|
+
recursiveSection: number;
|
|
112
|
+
letInLevel: number;
|
|
113
|
+
weights: number[];
|
|
114
|
+
isLeibnizArg: boolean[] | undefined;
|
|
115
|
+
isLeibniz: boolean;
|
|
116
|
+
opLevelCond: boolean[][][] | undefined;
|
|
117
|
+
maxLevels: number[];
|
|
118
|
+
minMaxLevel: number[][] | undefined;
|
|
119
|
+
labels: Map<string, LabelNode>;
|
|
120
|
+
// The DEFINE, USE, HIDE or INSTANCE step that a NumberedProofStep definition names.
|
|
121
|
+
step: ProofStepNode | undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface ThmOrAssumpDefNode extends LevelFields {
|
|
125
|
+
kind: "ThmOrAssumpDef";
|
|
126
|
+
name: string;
|
|
127
|
+
isTheorem: boolean;
|
|
128
|
+
body: LevelNode;
|
|
129
|
+
params: FormalParamNode[];
|
|
130
|
+
isLocal: boolean;
|
|
131
|
+
location: SanyLocation;
|
|
132
|
+
module: ModuleNode;
|
|
133
|
+
source: ThmOrAssumpDefNode | undefined;
|
|
134
|
+
labels: Map<string, LabelNode>;
|
|
135
|
+
isLeibnizArg: boolean[];
|
|
136
|
+
isLeibniz: boolean;
|
|
137
|
+
opLevelCond: boolean[][][];
|
|
138
|
+
maxLevels: number[];
|
|
139
|
+
minMaxLevel: number[][];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface ModuleNode {
|
|
143
|
+
kind: "Module";
|
|
144
|
+
id: number;
|
|
145
|
+
name: string;
|
|
146
|
+
location: SanyLocation;
|
|
147
|
+
extendees: ModuleNode[];
|
|
148
|
+
context: Context;
|
|
149
|
+
topLevel: TopLevelNode[];
|
|
150
|
+
definitions: SemanticNode[];
|
|
151
|
+
opDefsInRecursiveSection: OpDefNode[];
|
|
152
|
+
isLevelChecked: boolean;
|
|
153
|
+
levelCorrect: boolean;
|
|
154
|
+
argLevelParams: Map<string, ArgLevelParam>;
|
|
155
|
+
levelConstraints: Map<SymbolNode, number>;
|
|
156
|
+
argLevelConstraints: Map<string, ArgLevelConstraint>;
|
|
157
|
+
// ModuleNode.getRecords: the record constructors and sets of records generated for the module.
|
|
158
|
+
records: OpApplNode[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export type SymbolNode = FormalParamNode | OpDeclNode | OpDefNode | ThmOrAssumpDefNode | ModuleNode;
|
|
162
|
+
|
|
163
|
+
export interface OpApplNode extends LevelFields {
|
|
164
|
+
kind: "OpAppl";
|
|
165
|
+
operator: SymbolNode;
|
|
166
|
+
operands: (ExprOrOpArgNode | undefined)[];
|
|
167
|
+
unbounded: FormalParamNode[] | undefined;
|
|
168
|
+
bounded: FormalParamNode[][] | undefined;
|
|
169
|
+
tupleOrs: boolean[] | undefined;
|
|
170
|
+
ranges: ExprNode[];
|
|
171
|
+
location: SanyLocation;
|
|
172
|
+
// Generator.nullOAN, whose operator, nullODN, has no kind that the XMLExporter can export.
|
|
173
|
+
isNull?: boolean;
|
|
174
|
+
// The null that SANY generates for an operator it cannot resolve.
|
|
175
|
+
isAbsent?: boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const isAbsent = (node: LevelNode | undefined) =>
|
|
179
|
+
node?.kind === "OpAppl" && node.isAbsent === true;
|
|
180
|
+
|
|
181
|
+
export interface NumeralNode extends LevelFields {
|
|
182
|
+
kind: "Numeral";
|
|
183
|
+
value: string;
|
|
184
|
+
location: SanyLocation;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export interface StringNode extends LevelFields {
|
|
188
|
+
kind: "String";
|
|
189
|
+
value: string;
|
|
190
|
+
location: SanyLocation;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface DecimalNode extends LevelFields {
|
|
194
|
+
kind: "Decimal";
|
|
195
|
+
mantissa: string;
|
|
196
|
+
exponent: string;
|
|
197
|
+
integralPart: string;
|
|
198
|
+
fractionalPart: string;
|
|
199
|
+
location: SanyLocation;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface LetInNode extends LevelFields {
|
|
203
|
+
kind: "LetIn";
|
|
204
|
+
opDefs: (OpDefNode | ThmOrAssumpDefNode)[];
|
|
205
|
+
instances: InstanceNode[];
|
|
206
|
+
body: ExprNode;
|
|
207
|
+
context: Context;
|
|
208
|
+
location: SanyLocation;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export interface LabelNode extends LevelFields {
|
|
212
|
+
kind: "Label";
|
|
213
|
+
name: string;
|
|
214
|
+
params: FormalParamNode[];
|
|
215
|
+
body: LevelNode;
|
|
216
|
+
labels: Map<string, LabelNode>;
|
|
217
|
+
isAssumeProve: boolean;
|
|
218
|
+
location: SanyLocation;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export interface OpArgNode extends LevelFields {
|
|
222
|
+
kind: "OpArg";
|
|
223
|
+
op: SymbolNode;
|
|
224
|
+
location: SanyLocation;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export interface AtNode extends LevelFields {
|
|
228
|
+
kind: "At";
|
|
229
|
+
except: OpApplNode;
|
|
230
|
+
component: OpApplNode;
|
|
231
|
+
location: SanyLocation;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export interface Subst {
|
|
235
|
+
op: OpDeclNode;
|
|
236
|
+
expr: ExprOrOpArgNode;
|
|
237
|
+
isImplicit: boolean;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export interface SubstInNode extends LevelFields {
|
|
241
|
+
kind: "SubstIn" | "APSubstIn";
|
|
242
|
+
substs: Subst[];
|
|
243
|
+
body: LevelNode;
|
|
244
|
+
instantiatingModule: ModuleNode;
|
|
245
|
+
instantiatedModule: ModuleNode;
|
|
246
|
+
location: SanyLocation;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export interface InstanceNode extends LevelFields {
|
|
250
|
+
kind: "Instance";
|
|
251
|
+
name: string | undefined;
|
|
252
|
+
isLocal: boolean;
|
|
253
|
+
params: FormalParamNode[];
|
|
254
|
+
module: ModuleNode;
|
|
255
|
+
substs: Subst[];
|
|
256
|
+
location: SanyLocation;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export interface AssumeNode extends LevelFields {
|
|
260
|
+
kind: "Assume";
|
|
261
|
+
definition: ThmOrAssumpDefNode | undefined;
|
|
262
|
+
body: ExprNode;
|
|
263
|
+
location: SanyLocation;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface TheoremNode extends LevelFields {
|
|
267
|
+
kind: "Theorem";
|
|
268
|
+
definition: ThmOrAssumpDefNode | undefined;
|
|
269
|
+
body: LevelNode;
|
|
270
|
+
proof: ProofNode | undefined;
|
|
271
|
+
isSuffices: boolean;
|
|
272
|
+
location: SanyLocation;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// A fact of a BY, USE or HIDE: an expression, an ASSUME/PROVE or NEW clause, a module, or the
|
|
276
|
+
// definition of a module instance.
|
|
277
|
+
export type FactNode = LevelNode | ModuleNode | OpDefNode;
|
|
278
|
+
|
|
279
|
+
export interface LeafProofNode extends LevelFields {
|
|
280
|
+
kind: "LeafProof";
|
|
281
|
+
facts: FactNode[];
|
|
282
|
+
defs: (OpDefNode | ThmOrAssumpDefNode | ModuleNode)[];
|
|
283
|
+
isOmitted: boolean;
|
|
284
|
+
isOnly: boolean;
|
|
285
|
+
location: SanyLocation;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export interface DefStepNode extends LevelFields {
|
|
289
|
+
kind: "DefStep";
|
|
290
|
+
defs: OpDefNode[];
|
|
291
|
+
location: SanyLocation;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export type ProofStepNode = TheoremNode | DefStepNode | UseOrHideNode | InstanceNode;
|
|
295
|
+
|
|
296
|
+
export interface NonLeafProofNode extends LevelFields {
|
|
297
|
+
kind: "NonLeafProof";
|
|
298
|
+
steps: ProofStepNode[];
|
|
299
|
+
instances: InstanceNode[];
|
|
300
|
+
proofLevel: number;
|
|
301
|
+
location: SanyLocation;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export type ProofNode = LeafProofNode | NonLeafProofNode;
|
|
305
|
+
|
|
306
|
+
export interface NewSymbNode extends LevelFields {
|
|
307
|
+
kind: "NewSymb";
|
|
308
|
+
declaration: OpDeclNode;
|
|
309
|
+
set: ExprNode | undefined;
|
|
310
|
+
location: SanyLocation;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export interface AssumeProveNode extends LevelFields {
|
|
314
|
+
kind: "AssumeProve";
|
|
315
|
+
assumptions: LevelNode[];
|
|
316
|
+
goal: ExprNode;
|
|
317
|
+
isSuffices: boolean;
|
|
318
|
+
isBoxed: boolean;
|
|
319
|
+
location: SanyLocation;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface UseOrHideNode extends LevelFields {
|
|
323
|
+
kind: "UseOrHide";
|
|
324
|
+
facts: FactNode[];
|
|
325
|
+
defs: (OpDefNode | ThmOrAssumpDefNode | ModuleNode)[];
|
|
326
|
+
isOnly: boolean;
|
|
327
|
+
isHide: boolean;
|
|
328
|
+
location: SanyLocation;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export type ExprNode =
|
|
332
|
+
| OpApplNode
|
|
333
|
+
| NumeralNode
|
|
334
|
+
| StringNode
|
|
335
|
+
| DecimalNode
|
|
336
|
+
| LetInNode
|
|
337
|
+
| LabelNode
|
|
338
|
+
| AtNode
|
|
339
|
+
| SubstInNode;
|
|
340
|
+
|
|
341
|
+
export type ExprOrOpArgNode = ExprNode | OpArgNode;
|
|
342
|
+
|
|
343
|
+
export type LevelNode = ExprOrOpArgNode | AssumeProveNode | NewSymbNode;
|
|
344
|
+
|
|
345
|
+
export type ProofPartNode = ProofNode | DefStepNode;
|
|
346
|
+
|
|
347
|
+
export type TopLevelNode = InstanceNode | AssumeNode | TheoremNode | UseOrHideNode;
|
|
348
|
+
|
|
349
|
+
export type SemanticNode = SymbolNode | LevelNode | TopLevelNode | ProofPartNode;
|
|
350
|
+
|
|
351
|
+
// SANY's Context: a java.util.Hashtable from names to symbols, whose iteration order SANY's lists
|
|
352
|
+
// of constants, variables and instantiated definitions follow, and the symbols in the order they
|
|
353
|
+
// were added.
|
|
354
|
+
export interface Context {
|
|
355
|
+
table: JavaHashtable;
|
|
356
|
+
symbols: SymbolNode[];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
interface HashtableEntry {
|
|
360
|
+
hash: number;
|
|
361
|
+
key: string;
|
|
362
|
+
value: SymbolNode;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export interface JavaHashtable {
|
|
366
|
+
buckets: HashtableEntry[][];
|
|
367
|
+
count: number;
|
|
368
|
+
threshold: number;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// java.lang.String.hashCode, over UTF-16 code units.
|
|
372
|
+
const getJavaHash = (text: string) => {
|
|
373
|
+
let hash = 0;
|
|
374
|
+
for (let index = 0; index < text.length; index++) {
|
|
375
|
+
hash = (Math.imul(hash, 31) + text.charCodeAt(index)) | 0;
|
|
376
|
+
}
|
|
377
|
+
return hash;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// A java.util.Hashtable with its default capacity of 11 and load factor of 0.75.
|
|
381
|
+
const createJavaHashtable = (): JavaHashtable => ({
|
|
382
|
+
buckets: Array.from({ length: 11 }, () => []),
|
|
383
|
+
count: 0,
|
|
384
|
+
threshold: 8,
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
const getBucketIndex = (hash: number, length: number) => (hash & 0x7fffffff) % length;
|
|
388
|
+
|
|
389
|
+
const rehash = (table: JavaHashtable) => {
|
|
390
|
+
const capacity = table.buckets.length * 2 + 1;
|
|
391
|
+
const buckets: HashtableEntry[][] = Array.from({ length: capacity }, () => []);
|
|
392
|
+
for (let index = table.buckets.length - 1; index >= 0; index--) {
|
|
393
|
+
table.buckets[index].forEach((entry) =>
|
|
394
|
+
buckets[getBucketIndex(entry.hash, capacity)].unshift(entry),
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
table.buckets = buckets;
|
|
398
|
+
table.threshold = Math.floor(capacity * 0.75);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const getHashtableValue = (table: JavaHashtable, key: string, hashKey = key) =>
|
|
402
|
+
table.buckets[getBucketIndex(getJavaHash(hashKey), table.buckets.length)].find(
|
|
403
|
+
(entry) => entry.key === key,
|
|
404
|
+
)?.value;
|
|
405
|
+
|
|
406
|
+
const putHashtableValue = (table: JavaHashtable, key: string, value: SymbolNode, hashKey = key) => {
|
|
407
|
+
const hash = getJavaHash(hashKey);
|
|
408
|
+
const existing = table.buckets[getBucketIndex(hash, table.buckets.length)].find(
|
|
409
|
+
(entry) => entry.key === key,
|
|
410
|
+
);
|
|
411
|
+
if (existing) {
|
|
412
|
+
existing.value = value;
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (table.count >= table.threshold) rehash(table);
|
|
416
|
+
table.buckets[getBucketIndex(hash, table.buckets.length)].unshift({ hash, key, value });
|
|
417
|
+
table.count++;
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
// The values in the order of Hashtable.elements(), from the last bucket to the first.
|
|
421
|
+
export const getHashtableValues = (table: JavaHashtable) =>
|
|
422
|
+
table.buckets.toReversed().flatMap((bucket) => bucket.map((entry) => entry.value));
|
|
423
|
+
|
|
424
|
+
const MODULE_KEY_PREFIX = "\u0000module ";
|
|
425
|
+
|
|
426
|
+
export const createContext = (): Context => ({ table: createJavaHashtable(), symbols: [] });
|
|
427
|
+
|
|
428
|
+
const getSymbolKey = (symbol: SymbolNode) =>
|
|
429
|
+
symbol.kind === "Module" ? MODULE_KEY_PREFIX + symbol.name : symbol.name;
|
|
430
|
+
|
|
431
|
+
export const addToContext = (context: Context, symbol: SymbolNode) => {
|
|
432
|
+
putHashtableValue(context.table, getSymbolKey(symbol), symbol, symbol.name);
|
|
433
|
+
context.symbols.push(symbol);
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
export const getSymbolArity = (node: SymbolNode) =>
|
|
437
|
+
node.kind === "ThmOrAssumpDef" ? node.params.length : node.kind === "Module" ? 0 : node.arity;
|
|
438
|
+
|
|
439
|
+
export const getContextSymbol = (context: Context, name: string) =>
|
|
440
|
+
getHashtableValue(context.table, name);
|
|
441
|
+
|
|
442
|
+
export const getContextModule = (context: Context, name: string) =>
|
|
443
|
+
getHashtableValue(context.table, MODULE_KEY_PREFIX + name, name);
|
|
444
|
+
|
|
445
|
+
// ModuleNode.getOpDefs: the definitions in the order they joined the context, without builtins
|
|
446
|
+
// and module instances.
|
|
447
|
+
export const getOpDefs = (module: ModuleNode) =>
|
|
448
|
+
module.context.symbols.filter(
|
|
449
|
+
(symbol): symbol is OpDefNode => symbol.kind === "OpDef" && symbol.defKind === "UserDefined",
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
// ModuleNode.getConstantDecls and getVariableDecls reverse the Hashtable's order.
|
|
453
|
+
export const getDeclarations = (module: ModuleNode, declKind: number) =>
|
|
454
|
+
getHashtableValues(module.context.table)
|
|
455
|
+
.filter(
|
|
456
|
+
(symbol): symbol is OpDeclNode => symbol.kind === "OpDecl" && symbol.declKind === declKind,
|
|
457
|
+
)
|
|
458
|
+
.toReversed();
|
|
459
|
+
|
|
460
|
+
export const getThmOrAssumpDefs = (module: ModuleNode) =>
|
|
461
|
+
module.context.symbols.filter(
|
|
462
|
+
(symbol): symbol is ThmOrAssumpDefNode => symbol.kind === "ThmOrAssumpDef",
|
|
463
|
+
);
|