hinmil 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +2306 -2
- package/dist/cli.d.ts +15 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +338 -0
- package/dist/cli.js.map +1 -0
- package/dist/commentParser.d.ts +384 -0
- package/dist/commentParser.d.ts.map +1 -0
- package/dist/commentParser.js +2487 -0
- package/dist/commentParser.js.map +1 -0
- package/dist/config.d.ts +203 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +375 -0
- package/dist/config.js.map +1 -0
- package/dist/debugLog.d.ts +13 -0
- package/dist/debugLog.d.ts.map +1 -0
- package/dist/debugLog.js +44 -0
- package/dist/debugLog.js.map +1 -0
- package/dist/estree.d.ts +100 -0
- package/dist/estree.d.ts.map +1 -0
- package/dist/estree.js +34 -0
- package/dist/estree.js.map +1 -0
- package/dist/hm/env.d.ts +134 -0
- package/dist/hm/env.d.ts.map +1 -0
- package/dist/hm/env.js +939 -0
- package/dist/hm/env.js.map +1 -0
- package/dist/hm/index.d.ts +7 -0
- package/dist/hm/index.d.ts.map +1 -0
- package/dist/hm/index.js +26 -0
- package/dist/hm/index.js.map +1 -0
- package/dist/hm/substitution.d.ts +57 -0
- package/dist/hm/substitution.d.ts.map +1 -0
- package/dist/hm/substitution.js +546 -0
- package/dist/hm/substitution.js.map +1 -0
- package/dist/hm/typeParser.d.ts +39 -0
- package/dist/hm/typeParser.d.ts.map +1 -0
- package/dist/hm/typeParser.js +1285 -0
- package/dist/hm/typeParser.js.map +1 -0
- package/dist/hm/typeUtils.d.ts +248 -0
- package/dist/hm/typeUtils.d.ts.map +1 -0
- package/dist/hm/typeUtils.js +905 -0
- package/dist/hm/typeUtils.js.map +1 -0
- package/dist/hm/types.d.ts +738 -0
- package/dist/hm/types.d.ts.map +1 -0
- package/dist/hm/types.js +1853 -0
- package/dist/hm/types.js.map +1 -0
- package/dist/hm/unify.d.ts +59 -0
- package/dist/hm/unify.d.ts.map +1 -0
- package/dist/hm/unify.js +1700 -0
- package/dist/hm/unify.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +196 -0
- package/dist/index.js.map +1 -0
- package/dist/infer/annotation-rigid.d.ts +76 -0
- package/dist/infer/annotation-rigid.d.ts.map +1 -0
- package/dist/infer/annotation-rigid.js +542 -0
- package/dist/infer/annotation-rigid.js.map +1 -0
- package/dist/infer/annotation-validation.d.ts +109 -0
- package/dist/infer/annotation-validation.d.ts.map +1 -0
- package/dist/infer/annotation-validation.js +1078 -0
- package/dist/infer/annotation-validation.js.map +1 -0
- package/dist/infer/annotations.d.ts +277 -0
- package/dist/infer/annotations.d.ts.map +1 -0
- package/dist/infer/annotations.js +1343 -0
- package/dist/infer/annotations.js.map +1 -0
- package/dist/infer/call-overloads.d.ts +24 -0
- package/dist/infer/call-overloads.d.ts.map +1 -0
- package/dist/infer/call-overloads.js +900 -0
- package/dist/infer/call-overloads.js.map +1 -0
- package/dist/infer/call-rows.d.ts +154 -0
- package/dist/infer/call-rows.d.ts.map +1 -0
- package/dist/infer/call-rows.js +2162 -0
- package/dist/infer/call-rows.js.map +1 -0
- package/dist/infer/calls.d.ts +20 -0
- package/dist/infer/calls.d.ts.map +1 -0
- package/dist/infer/calls.js +1468 -0
- package/dist/infer/calls.js.map +1 -0
- package/dist/infer/constraints.d.ts +99 -0
- package/dist/infer/constraints.d.ts.map +1 -0
- package/dist/infer/constraints.js +1208 -0
- package/dist/infer/constraints.js.map +1 -0
- package/dist/infer/expr-assignment.d.ts +20 -0
- package/dist/infer/expr-assignment.d.ts.map +1 -0
- package/dist/infer/expr-assignment.js +620 -0
- package/dist/infer/expr-assignment.js.map +1 -0
- package/dist/infer/expr-constraint-lookup.d.ts +44 -0
- package/dist/infer/expr-constraint-lookup.d.ts.map +1 -0
- package/dist/infer/expr-constraint-lookup.js +207 -0
- package/dist/infer/expr-constraint-lookup.js.map +1 -0
- package/dist/infer/expr-functions.d.ts +20 -0
- package/dist/infer/expr-functions.d.ts.map +1 -0
- package/dist/infer/expr-functions.js +1359 -0
- package/dist/infer/expr-functions.js.map +1 -0
- package/dist/infer/expr-jsx.d.ts +19 -0
- package/dist/infer/expr-jsx.d.ts.map +1 -0
- package/dist/infer/expr-jsx.js +310 -0
- package/dist/infer/expr-jsx.js.map +1 -0
- package/dist/infer/expr-member.d.ts +40 -0
- package/dist/infer/expr-member.d.ts.map +1 -0
- package/dist/infer/expr-member.js +1154 -0
- package/dist/infer/expr-member.js.map +1 -0
- package/dist/infer/expr-prototype.d.ts +11 -0
- package/dist/infer/expr-prototype.d.ts.map +1 -0
- package/dist/infer/expr-prototype.js +202 -0
- package/dist/infer/expr-prototype.js.map +1 -0
- package/dist/infer/expressions.d.ts +40 -0
- package/dist/infer/expressions.d.ts.map +1 -0
- package/dist/infer/expressions.js +1497 -0
- package/dist/infer/expressions.js.map +1 -0
- package/dist/infer/functionPatterns.d.ts +229 -0
- package/dist/infer/functionPatterns.d.ts.map +1 -0
- package/dist/infer/functionPatterns.js +1035 -0
- package/dist/infer/functionPatterns.js.map +1 -0
- package/dist/infer/helpers.d.ts +73 -0
- package/dist/infer/helpers.d.ts.map +1 -0
- package/dist/infer/helpers.js +187 -0
- package/dist/infer/helpers.js.map +1 -0
- package/dist/infer/index.d.ts +551 -0
- package/dist/infer/index.d.ts.map +1 -0
- package/dist/infer/index.js +1127 -0
- package/dist/infer/index.js.map +1 -0
- package/dist/infer/jsx-dom.d.ts +26 -0
- package/dist/infer/jsx-dom.d.ts.map +1 -0
- package/dist/infer/jsx-dom.js +303 -0
- package/dist/infer/jsx-dom.js.map +1 -0
- package/dist/infer/moduleReps.d.ts +7 -0
- package/dist/infer/moduleReps.d.ts.map +1 -0
- package/dist/infer/moduleReps.js +48 -0
- package/dist/infer/moduleReps.js.map +1 -0
- package/dist/infer/modules.d.ts +10 -0
- package/dist/infer/modules.d.ts.map +1 -0
- package/dist/infer/modules.js +775 -0
- package/dist/infer/modules.js.map +1 -0
- package/dist/infer/narrowing.d.ts +199 -0
- package/dist/infer/narrowing.d.ts.map +1 -0
- package/dist/infer/narrowing.js +1428 -0
- package/dist/infer/narrowing.js.map +1 -0
- package/dist/infer/results.d.ts +24 -0
- package/dist/infer/results.d.ts.map +1 -0
- package/dist/infer/results.js +611 -0
- package/dist/infer/results.js.map +1 -0
- package/dist/infer/setupPass.d.ts +11 -0
- package/dist/infer/setupPass.d.ts.map +1 -0
- package/dist/infer/setupPass.js +524 -0
- package/dist/infer/setupPass.js.map +1 -0
- package/dist/infer/statements.d.ts +26 -0
- package/dist/infer/statements.d.ts.map +1 -0
- package/dist/infer/statements.js +750 -0
- package/dist/infer/statements.js.map +1 -0
- package/dist/infer/stmt-bindings.d.ts +22 -0
- package/dist/infer/stmt-bindings.d.ts.map +1 -0
- package/dist/infer/stmt-bindings.js +1482 -0
- package/dist/infer/stmt-bindings.js.map +1 -0
- package/dist/infer/stmt-destructure.d.ts +29 -0
- package/dist/infer/stmt-destructure.d.ts.map +1 -0
- package/dist/infer/stmt-destructure.js +490 -0
- package/dist/infer/stmt-destructure.js.map +1 -0
- package/dist/infer/types.d.ts +220 -0
- package/dist/infer/types.d.ts.map +1 -0
- package/dist/infer/types.js +46 -0
- package/dist/infer/types.js.map +1 -0
- package/dist/infer/unifyDiag.d.ts +46 -0
- package/dist/infer/unifyDiag.d.ts.map +1 -0
- package/dist/infer/unifyDiag.js +642 -0
- package/dist/infer/unifyDiag.js.map +1 -0
- package/dist/parsing-babel.d.ts +3 -0
- package/dist/parsing-babel.d.ts.map +1 -0
- package/dist/parsing-babel.js +353 -0
- package/dist/parsing-babel.js.map +1 -0
- package/dist/parsing.d.ts +69 -0
- package/dist/parsing.d.ts.map +1 -0
- package/dist/parsing.js +288 -0
- package/dist/parsing.js.map +1 -0
- package/dist/registries/constraintRegistry.d.ts +233 -0
- package/dist/registries/constraintRegistry.d.ts.map +1 -0
- package/dist/registries/constraintRegistry.js +803 -0
- package/dist/registries/constraintRegistry.js.map +1 -0
- package/dist/registries/dataTypeRegistry.d.ts +63 -0
- package/dist/registries/dataTypeRegistry.d.ts.map +1 -0
- package/dist/registries/dataTypeRegistry.js +180 -0
- package/dist/registries/dataTypeRegistry.js.map +1 -0
- package/dist/registries/domGlobals.d.ts +33 -0
- package/dist/registries/domGlobals.d.ts.map +1 -0
- package/dist/registries/domGlobals.js +309 -0
- package/dist/registries/domGlobals.js.map +1 -0
- package/dist/registries/methodRegistry.d.ts +277 -0
- package/dist/registries/methodRegistry.d.ts.map +1 -0
- package/dist/registries/methodRegistry.js +2074 -0
- package/dist/registries/methodRegistry.js.map +1 -0
- package/dist/registries/session.d.ts +38 -0
- package/dist/registries/session.d.ts.map +1 -0
- package/dist/registries/session.js +75 -0
- package/dist/registries/session.js.map +1 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1896 -0
- package/dist/server.js.map +1 -0
- package/dist/services/codeActions.d.ts +22 -0
- package/dist/services/codeActions.d.ts.map +1 -0
- package/dist/services/codeActions.js +465 -0
- package/dist/services/codeActions.js.map +1 -0
- package/dist/services/completion.d.ts +259 -0
- package/dist/services/completion.d.ts.map +1 -0
- package/dist/services/completion.js +2794 -0
- package/dist/services/completion.js.map +1 -0
- package/dist/services/definitions.d.ts +37 -0
- package/dist/services/definitions.d.ts.map +1 -0
- package/dist/services/definitions.js +605 -0
- package/dist/services/definitions.js.map +1 -0
- package/dist/services/diagnosticCodes.d.ts +70 -0
- package/dist/services/diagnosticCodes.d.ts.map +1 -0
- package/dist/services/diagnosticCodes.js +239 -0
- package/dist/services/diagnosticCodes.js.map +1 -0
- package/dist/services/diagnostics.d.ts +86 -0
- package/dist/services/diagnostics.d.ts.map +1 -0
- package/dist/services/diagnostics.js +665 -0
- package/dist/services/diagnostics.js.map +1 -0
- package/dist/services/documentCache.d.ts +88 -0
- package/dist/services/documentCache.d.ts.map +1 -0
- package/dist/services/documentCache.js +108 -0
- package/dist/services/documentCache.js.map +1 -0
- package/dist/services/documentSymbols.d.ts +20 -0
- package/dist/services/documentSymbols.d.ts.map +1 -0
- package/dist/services/documentSymbols.js +381 -0
- package/dist/services/documentSymbols.js.map +1 -0
- package/dist/services/fileSelection.d.ts +24 -0
- package/dist/services/fileSelection.d.ts.map +1 -0
- package/dist/services/fileSelection.js +154 -0
- package/dist/services/fileSelection.js.map +1 -0
- package/dist/services/fileSelectionRefresh.d.ts +26 -0
- package/dist/services/fileSelectionRefresh.d.ts.map +1 -0
- package/dist/services/fileSelectionRefresh.js +77 -0
- package/dist/services/fileSelectionRefresh.js.map +1 -0
- package/dist/services/files.d.ts +342 -0
- package/dist/services/files.d.ts.map +1 -0
- package/dist/services/files.js +1712 -0
- package/dist/services/files.js.map +1 -0
- package/dist/services/fsProbe.d.ts +14 -0
- package/dist/services/fsProbe.d.ts.map +1 -0
- package/dist/services/fsProbe.js +112 -0
- package/dist/services/fsProbe.js.map +1 -0
- package/dist/services/globalsLoader.d.ts +20 -0
- package/dist/services/globalsLoader.d.ts.map +1 -0
- package/dist/services/globalsLoader.js +122 -0
- package/dist/services/globalsLoader.js.map +1 -0
- package/dist/services/hmFileServices.d.ts +113 -0
- package/dist/services/hmFileServices.d.ts.map +1 -0
- package/dist/services/hmFileServices.js +686 -0
- package/dist/services/hmFileServices.js.map +1 -0
- package/dist/services/hover.d.ts +280 -0
- package/dist/services/hover.d.ts.map +1 -0
- package/dist/services/hover.js +1687 -0
- package/dist/services/hover.js.map +1 -0
- package/dist/services/importContext.d.ts +396 -0
- package/dist/services/importContext.d.ts.map +1 -0
- package/dist/services/importContext.js +1999 -0
- package/dist/services/importContext.js.map +1 -0
- package/dist/services/projectChecker.d.ts +50 -0
- package/dist/services/projectChecker.d.ts.map +1 -0
- package/dist/services/projectChecker.js +228 -0
- package/dist/services/projectChecker.js.map +1 -0
- package/dist/services/references.d.ts +41 -0
- package/dist/services/references.d.ts.map +1 -0
- package/dist/services/references.js +757 -0
- package/dist/services/references.js.map +1 -0
- package/dist/services/rename.d.ts +22 -0
- package/dist/services/rename.d.ts.map +1 -0
- package/dist/services/rename.js +101 -0
- package/dist/services/rename.js.map +1 -0
- package/dist/services/shortCircuit.d.ts +27 -0
- package/dist/services/shortCircuit.d.ts.map +1 -0
- package/dist/services/shortCircuit.js +28 -0
- package/dist/services/shortCircuit.js.map +1 -0
- package/dist/services/sidecarPerf.d.ts +19 -0
- package/dist/services/sidecarPerf.d.ts.map +1 -0
- package/dist/services/sidecarPerf.js +59 -0
- package/dist/services/sidecarPerf.js.map +1 -0
- package/dist/services/signatureHelp.d.ts +27 -0
- package/dist/services/signatureHelp.d.ts.map +1 -0
- package/dist/services/signatureHelp.js +984 -0
- package/dist/services/signatureHelp.js.map +1 -0
- package/dist/walking.d.ts +25 -0
- package/dist/walking.d.ts.map +1 -0
- package/dist/walking.js +107 -0
- package/dist/walking.js.map +1 -0
- package/hm-types-builtin/assert.hm +20 -0
- package/hm-types-builtin/buffer.hm +8 -0
- package/hm-types-builtin/child_process.hm +16 -0
- package/hm-types-builtin/crypto.hm +17 -0
- package/hm-types-builtin/fs.hm +15 -0
- package/hm-types-builtin/http.hm +14 -0
- package/hm-types-builtin/os.hm +19 -0
- package/hm-types-builtin/path.hm +18 -0
- package/hm-types-builtin/react-dom.hm +27 -0
- package/hm-types-builtin/react.hm +113 -0
- package/hm-types-builtin/url.hm +7 -0
- package/hm-types-builtin/zlib.hm +12 -0
- package/package.json +53 -4
- package/schemas/hinmil.config.schema.json +261 -0
- package/schemas/package-hinmil.schema.json +59 -0
package/dist/hm/env.js
ADDED
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Type Environment — maps variable names to type schemes
|
|
4
|
+
//
|
|
5
|
+
// WHAT THIS FILE DOES (in plain language)
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// The environment is the checker's phone book: "which names are currently in
|
|
8
|
+
// scope, and what is the type of each?". Entering a new scope (a function
|
|
9
|
+
// body, a block) adds a new page that can shadow the previous one —
|
|
10
|
+
// implemented cheaply as a child node with a pointer back to its parent.
|
|
11
|
+
//
|
|
12
|
+
// This file also hosts the two operations that make polymorphism work:
|
|
13
|
+
// • generalize — seal a finished definition's leftover placeholders into
|
|
14
|
+
// a reusable template (Scheme): `x => x` becomes `a -> a`.
|
|
15
|
+
// • instantiate — stamp out a fresh copy of such a template for one
|
|
16
|
+
// particular use site, with brand-new placeholders.
|
|
17
|
+
// Plus the seeding of the global scope (built-in globals like console, JSON).
|
|
18
|
+
// ============================================================================
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.TypeEnv = void 0;
|
|
21
|
+
exports.freeTypeVarsInType = freeTypeVarsInType;
|
|
22
|
+
exports.freeTypeVarsInScheme = freeTypeVarsInScheme;
|
|
23
|
+
exports.applySubstToScheme = applySubstToScheme;
|
|
24
|
+
exports.instantiate = instantiate;
|
|
25
|
+
exports.collectLabelVarIds = collectLabelVarIds;
|
|
26
|
+
exports.collectRowVarIds = collectRowVarIds;
|
|
27
|
+
exports.instantiateWithAny = instantiateWithAny;
|
|
28
|
+
exports.instantiateSkolem = instantiateSkolem;
|
|
29
|
+
exports.instantiateRigid = instantiateRigid;
|
|
30
|
+
exports.skolemizeForall = skolemizeForall;
|
|
31
|
+
exports.skolemSubstForScheme = skolemSubstForScheme;
|
|
32
|
+
exports.generalize = generalize;
|
|
33
|
+
exports.mergeOverloadSchemes = mergeOverloadSchemes;
|
|
34
|
+
exports.resetDefaultEnvCache = resetDefaultEnvCache;
|
|
35
|
+
exports.defaultEnv = defaultEnv;
|
|
36
|
+
const types_1 = require("./types");
|
|
37
|
+
const domGlobals_1 = require("../registries/domGlobals");
|
|
38
|
+
const substitution_1 = require("./substitution");
|
|
39
|
+
const typeUtils_1 = require("./typeUtils");
|
|
40
|
+
class TypeEnv {
|
|
41
|
+
constructor(entries) {
|
|
42
|
+
this.bindings = new Map(entries);
|
|
43
|
+
}
|
|
44
|
+
/** Traverse the parent chain, yielding each TypeEnv node. */
|
|
45
|
+
*chain() {
|
|
46
|
+
let current = this;
|
|
47
|
+
while (current) {
|
|
48
|
+
yield current;
|
|
49
|
+
current = current.parent;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Look up a variable. */
|
|
53
|
+
lookup(name) {
|
|
54
|
+
const local = this.bindings.get(name);
|
|
55
|
+
if (local !== undefined)
|
|
56
|
+
return local;
|
|
57
|
+
return this.parent?.lookup(name);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Extend environment with a new binding (returns a new env).
|
|
61
|
+
*
|
|
62
|
+
* O(1) — creates a lightweight child node with a single-entry map and a
|
|
63
|
+
* parent pointer back to `this`. The previous implementation copied the
|
|
64
|
+
* *entire* bindings map (O(n)) on every call, which dominated bind-time
|
|
65
|
+
* costs for environments with 30-50+ entries.
|
|
66
|
+
*/
|
|
67
|
+
extend(name, scheme) {
|
|
68
|
+
const child = new TypeEnv();
|
|
69
|
+
child.parent = this;
|
|
70
|
+
child.bindings.set(name, scheme);
|
|
71
|
+
return child;
|
|
72
|
+
}
|
|
73
|
+
/** Remove a binding (returns a new env). */
|
|
74
|
+
remove(name) {
|
|
75
|
+
const flat = new TypeEnv();
|
|
76
|
+
for (const [n, s] of this.entries()) {
|
|
77
|
+
if (n !== name)
|
|
78
|
+
flat.bindings.set(n, s);
|
|
79
|
+
}
|
|
80
|
+
return flat;
|
|
81
|
+
}
|
|
82
|
+
/** Apply a substitution to every scheme in the environment. */
|
|
83
|
+
applySubst(subst) {
|
|
84
|
+
const newEntries = [];
|
|
85
|
+
for (const [name, scheme] of this.entries()) {
|
|
86
|
+
newEntries.push([name, applySubstToScheme(subst, scheme)]);
|
|
87
|
+
}
|
|
88
|
+
return new TypeEnv(newEntries);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Collect all free type-variable ids across the environment.
|
|
92
|
+
*
|
|
93
|
+
* When `subst` is supplied, each scheme's free variables are resolved
|
|
94
|
+
* THROUGH the current substitution first. This matters for generalization:
|
|
95
|
+
* an environment binding stored as `mono(t_f)` may have had `t_f` unified to
|
|
96
|
+
* `t_arg -> t_r` during body inference; without resolving through `subst`,
|
|
97
|
+
* `t_r` would be invisible here and a `const` sharing it (e.g.
|
|
98
|
+
* `const result = f(arg)`) would be WRONGLY generalized to `∀. t_r`.
|
|
99
|
+
*/
|
|
100
|
+
freeTypeVars(subst) {
|
|
101
|
+
const result = new Set();
|
|
102
|
+
const seen = new Set();
|
|
103
|
+
for (const env of this.chain()) {
|
|
104
|
+
for (const [name, scheme] of env.bindings) {
|
|
105
|
+
if (!seen.has(name)) {
|
|
106
|
+
seen.add(name);
|
|
107
|
+
for (const v of freeTypeVarsInScheme(scheme, subst)) {
|
|
108
|
+
result.add(v);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
/** Get all bindings (respects shadowing — child bindings win). */
|
|
116
|
+
*entries() {
|
|
117
|
+
const seen = new Set();
|
|
118
|
+
for (const env of this.chain()) {
|
|
119
|
+
for (const [name, scheme] of env.bindings) {
|
|
120
|
+
if (!seen.has(name)) {
|
|
121
|
+
seen.add(name);
|
|
122
|
+
yield [name, scheme];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
get size() {
|
|
128
|
+
const seen = new Set();
|
|
129
|
+
for (const env of this.chain()) {
|
|
130
|
+
for (const name of env.bindings.keys()) {
|
|
131
|
+
seen.add(name);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return seen.size;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.TypeEnv = TypeEnv;
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
// Helpers for free variables & substitution on schemes
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
function freeTypeVarsInType(t) {
|
|
142
|
+
const ids = new Set();
|
|
143
|
+
(0, typeUtils_1.collectTVarIdsFromType)(t, ids);
|
|
144
|
+
return ids;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Resolve a set of type-variable ids through a substitution, returning the
|
|
148
|
+
* ids that remain FREE (unbound) once every binding is followed. A bound id
|
|
149
|
+
* is replaced by the free variables of the type it resolves to (transitively).
|
|
150
|
+
* `visited` guards against cyclic (recursive-type) substitutions.
|
|
151
|
+
*/
|
|
152
|
+
function resolveFreeVarsUnderSubst(roots, subst) {
|
|
153
|
+
const out = new Set();
|
|
154
|
+
const visited = new Set();
|
|
155
|
+
const stack = [...roots];
|
|
156
|
+
while (stack.length > 0) {
|
|
157
|
+
const id = stack.pop();
|
|
158
|
+
if (visited.has(id))
|
|
159
|
+
continue;
|
|
160
|
+
visited.add(id);
|
|
161
|
+
const bound = subst.lookup(id);
|
|
162
|
+
if (bound === undefined) {
|
|
163
|
+
out.add(id);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
for (const v of freeTypeVarsInType(bound))
|
|
167
|
+
stack.push(v);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
function freeTypeVarsInScheme(scheme, subst) {
|
|
173
|
+
const free = freeTypeVarsInType(scheme.type);
|
|
174
|
+
for (const v of scheme.vars) {
|
|
175
|
+
free.delete(v);
|
|
176
|
+
}
|
|
177
|
+
return subst ? resolveFreeVarsUnderSubst(free, subst) : free;
|
|
178
|
+
}
|
|
179
|
+
function applySubstToScheme(subst, scheme) {
|
|
180
|
+
// Substitute only free variables — skip bound vars
|
|
181
|
+
const cleanSubst = new substitution_1.Substitution();
|
|
182
|
+
for (const [id, type] of subst.entries()) {
|
|
183
|
+
if (!scheme.vars.includes(id)) {
|
|
184
|
+
cleanSubst.set(id, type);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
...scheme,
|
|
189
|
+
type: cleanSubst.apply(scheme.type),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Instantiate a scheme: replace all bound variables with fresh type variables.
|
|
194
|
+
*/
|
|
195
|
+
function instantiate(scheme) {
|
|
196
|
+
// Collect original var names from the scheme type so fresh vars
|
|
197
|
+
// carry the same names (e.g. 'a', 'c' instead of 't39', 't40').
|
|
198
|
+
const varNames = new Map();
|
|
199
|
+
(0, types_1.collectVarNames)(scheme.type, varNames);
|
|
200
|
+
// Determine which scheme ids appear as TLabel-vars (so they get
|
|
201
|
+
// freshened as labels, not ordinary type variables).
|
|
202
|
+
const labelIds = new Set();
|
|
203
|
+
collectLabelVarIds(scheme.type, labelIds);
|
|
204
|
+
// Determine which scheme ids appear as TRow vars (so they get freshened
|
|
205
|
+
// as rows, preserving the disjoint sort across instantiation).
|
|
206
|
+
const rowIds = new Set();
|
|
207
|
+
collectRowVarIds(scheme.type, rowIds);
|
|
208
|
+
const subst = new substitution_1.Substitution();
|
|
209
|
+
const schemeVarSet = new Set(scheme.vars);
|
|
210
|
+
for (const v of scheme.vars) {
|
|
211
|
+
if (labelIds.has(v)) {
|
|
212
|
+
const name = varNames.get(v);
|
|
213
|
+
// Allocate via tVar() for a fresh id, then wrap as TLabel-var.
|
|
214
|
+
let fresh = (0, types_1.tVar)(name);
|
|
215
|
+
while (schemeVarSet.has(fresh.id))
|
|
216
|
+
fresh = (0, types_1.tVar)(name);
|
|
217
|
+
subst.set(v, { tag: 'TLabel', kind: 'var', id: fresh.id, name: name ?? fresh.name });
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (rowIds.has(v)) {
|
|
221
|
+
const name = varNames.get(v);
|
|
222
|
+
let freshRow = (0, types_1.tRow)(name);
|
|
223
|
+
while (schemeVarSet.has(freshRow.id))
|
|
224
|
+
freshRow = (0, types_1.tRow)(name);
|
|
225
|
+
subst.set(v, freshRow);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
let fresh = (0, types_1.tVar)(varNames.get(v));
|
|
229
|
+
// Avoid allocating a TVar whose id collides with one of the scheme's
|
|
230
|
+
// own bound variables — otherwise the substitution becomes a no-op and
|
|
231
|
+
// the original (shared) TVar leaks into the engine's unification.
|
|
232
|
+
while (schemeVarSet.has(fresh.id)) {
|
|
233
|
+
fresh = (0, types_1.tVar)(varNames.get(v));
|
|
234
|
+
}
|
|
235
|
+
subst.set(v, fresh);
|
|
236
|
+
}
|
|
237
|
+
return subst.apply(scheme.type);
|
|
238
|
+
}
|
|
239
|
+
function collectLabelVarIds(t, out) {
|
|
240
|
+
switch (t.tag) {
|
|
241
|
+
case 'TLabel':
|
|
242
|
+
if (t.kind === 'var')
|
|
243
|
+
out.add(t.id);
|
|
244
|
+
return;
|
|
245
|
+
case 'TFun':
|
|
246
|
+
collectLabelVarIds(t.param, out);
|
|
247
|
+
collectLabelVarIds(t.result, out);
|
|
248
|
+
return;
|
|
249
|
+
case 'TCon':
|
|
250
|
+
t.args.forEach(a => collectLabelVarIds(a, out));
|
|
251
|
+
return;
|
|
252
|
+
case 'TRecord':
|
|
253
|
+
for (const k in t.fields)
|
|
254
|
+
collectLabelVarIds(t.fields[k], out);
|
|
255
|
+
if (t.labelFields)
|
|
256
|
+
for (const lf of t.labelFields) {
|
|
257
|
+
collectLabelVarIds(lf.label, out);
|
|
258
|
+
collectLabelVarIds(lf.type, out);
|
|
259
|
+
}
|
|
260
|
+
return;
|
|
261
|
+
case 'TVar': return;
|
|
262
|
+
case 'TRow': return;
|
|
263
|
+
case 'TForall':
|
|
264
|
+
collectLabelVarIds(t.body, out);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** Walk a type tree collecting ids of any TRow nodes encountered. Used by
|
|
269
|
+
* {@link instantiate} so quantified row-vars get freshened as rows (not
|
|
270
|
+
* TVars) and the disjoint TRow sort survives instantiation. */
|
|
271
|
+
function collectRowVarIds(t, out) {
|
|
272
|
+
switch (t.tag) {
|
|
273
|
+
case 'TRow':
|
|
274
|
+
out.add(t.id);
|
|
275
|
+
return;
|
|
276
|
+
case 'TFun':
|
|
277
|
+
collectRowVarIds(t.param, out);
|
|
278
|
+
collectRowVarIds(t.result, out);
|
|
279
|
+
return;
|
|
280
|
+
case 'TCon':
|
|
281
|
+
t.args.forEach(a => collectRowVarIds(a, out));
|
|
282
|
+
return;
|
|
283
|
+
case 'TRecord':
|
|
284
|
+
for (const k in t.fields)
|
|
285
|
+
collectRowVarIds(t.fields[k], out);
|
|
286
|
+
if (t.labelFields)
|
|
287
|
+
for (const lf of t.labelFields)
|
|
288
|
+
collectRowVarIds(lf.type, out);
|
|
289
|
+
if (t.rest && t.rest.tag === 'TRow')
|
|
290
|
+
out.add(t.rest.id);
|
|
291
|
+
return;
|
|
292
|
+
case 'TVar': return;
|
|
293
|
+
case 'TLabel': return;
|
|
294
|
+
case 'TForall':
|
|
295
|
+
collectRowVarIds(t.body, out);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Instantiate a scheme replacing quantified variables with the wildcard type `*`.
|
|
301
|
+
* Used for expectedType when the annotation has free type variables — concrete
|
|
302
|
+
* parts (like Boolean) are preserved and enforced, while free vars become `*`
|
|
303
|
+
* which unifies with anything without producing bindings.
|
|
304
|
+
*/
|
|
305
|
+
function instantiateWithAny(scheme) {
|
|
306
|
+
// Collect original var names so each free var gets a named wildcard
|
|
307
|
+
// (e.g. '*:e', '*:a') that displays with the annotation name.
|
|
308
|
+
const varNames = new Map();
|
|
309
|
+
(0, types_1.collectVarNames)(scheme.type, varNames);
|
|
310
|
+
// Row- and label-variables must NOT degrade to wildcard `*` TCons:
|
|
311
|
+
// a row variable models "an open record of unknown extra fields", and
|
|
312
|
+
// collapsing it to a wildcard would (a) make an open record `{...a}`
|
|
313
|
+
// render as the empty `{}` and (b) let unrelated types (e.g. `String`)
|
|
314
|
+
// unify permissively with what should be a record, silently swallowing
|
|
315
|
+
// genuine body-vs-annotation mismatches. So we freshen them just like
|
|
316
|
+
// {@link instantiate} does — as TRow / TLabel-var — and only the ordinary
|
|
317
|
+
// type variables become named wildcards.
|
|
318
|
+
const labelIds = new Set();
|
|
319
|
+
collectLabelVarIds(scheme.type, labelIds);
|
|
320
|
+
const rowIds = new Set();
|
|
321
|
+
collectRowVarIds(scheme.type, rowIds);
|
|
322
|
+
const subst = new substitution_1.Substitution();
|
|
323
|
+
const schemeVarSet = new Set(scheme.vars);
|
|
324
|
+
for (const v of scheme.vars) {
|
|
325
|
+
if (labelIds.has(v)) {
|
|
326
|
+
const name = varNames.get(v);
|
|
327
|
+
let fresh = (0, types_1.tVar)(name);
|
|
328
|
+
while (schemeVarSet.has(fresh.id))
|
|
329
|
+
fresh = (0, types_1.tVar)(name);
|
|
330
|
+
subst.set(v, { tag: 'TLabel', kind: 'var', id: fresh.id, name: name ?? fresh.name });
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
if (rowIds.has(v)) {
|
|
334
|
+
const name = varNames.get(v);
|
|
335
|
+
let freshRow = (0, types_1.tRow)(name);
|
|
336
|
+
while (schemeVarSet.has(freshRow.id))
|
|
337
|
+
freshRow = (0, types_1.tRow)(name);
|
|
338
|
+
subst.set(v, freshRow);
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
const name = varNames.get(v);
|
|
342
|
+
subst.set(v, name ? (0, types_1.tNamedWildcard)(name) : types_1.tAny);
|
|
343
|
+
}
|
|
344
|
+
const result = subst.apply(scheme.type);
|
|
345
|
+
return result;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Instantiate a scheme with rigid (skolem) type constants instead of fresh
|
|
349
|
+
* unifiable variables. Two distinct quantified variables become two distinct
|
|
350
|
+
* TCon names (e.g. `§0`, `§1`), so unification will fail if the implementation
|
|
351
|
+
* forces them to be equal.
|
|
352
|
+
*
|
|
353
|
+
* Returns the rigid type, the set of skolem names used, and a map from each
|
|
354
|
+
* skolem name back to the original variable letter (e.g. `§0` → `b`) so the
|
|
355
|
+
* skolems can be scrubbed back to readable names for display / diagnostics.
|
|
356
|
+
*/
|
|
357
|
+
let skolemCounter = 0;
|
|
358
|
+
/**
|
|
359
|
+
* Collect the ids of every quantified variable that appears in a *parameter*
|
|
360
|
+
* position of an arrow chain (recursing into nested function-typed params).
|
|
361
|
+
* Variables that appear ONLY in the final return position are deliberately
|
|
362
|
+
* excluded.
|
|
363
|
+
*
|
|
364
|
+
* Rationale: a variable shared with a parameter (or `this`) is *fixed by the
|
|
365
|
+
* caller* — the body must preserve it, so it is skolemized (rigid + display
|
|
366
|
+
* frozen). A variable that occurs only in the return is *chosen by the body*
|
|
367
|
+
* (e.g. `f :: a -> [b]` with `return [1,2,3]`), so it stays a flexible TVar
|
|
368
|
+
* and a concrete return value is accepted while the annotation still drives
|
|
369
|
+
* the displayed return type.
|
|
370
|
+
*/
|
|
371
|
+
function collectParamPositionVarIds(type, into) {
|
|
372
|
+
let t = type;
|
|
373
|
+
while (t.tag === 'TFun') {
|
|
374
|
+
const m = new Map();
|
|
375
|
+
(0, types_1.collectVarNames)(t.param, m);
|
|
376
|
+
for (const id of m.keys())
|
|
377
|
+
into.add(id);
|
|
378
|
+
t = t.result;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
function instantiateSkolem(scheme) {
|
|
382
|
+
// Original variable names so each skolem can be displayed with the
|
|
383
|
+
// annotation's letter (e.g. `§0` represents `b`).
|
|
384
|
+
const varNames = new Map();
|
|
385
|
+
(0, types_1.collectVarNames)(scheme.type, varNames);
|
|
386
|
+
// Only freeze variables that appear in a parameter position; return-only
|
|
387
|
+
// variables remain flexible so the body may return a concrete instance.
|
|
388
|
+
const paramVarIds = new Set();
|
|
389
|
+
collectParamPositionVarIds(scheme.type, paramVarIds);
|
|
390
|
+
// Row- and label-variables stay rows/labels: skolemizing them to an opaque
|
|
391
|
+
// TCon would make an open record `{...a}` unify-incompatible with the
|
|
392
|
+
// structurally-identical inferred record, producing a spurious self-mismatch.
|
|
393
|
+
const labelIds = new Set();
|
|
394
|
+
collectLabelVarIds(scheme.type, labelIds);
|
|
395
|
+
const rowIds = new Set();
|
|
396
|
+
collectRowVarIds(scheme.type, rowIds);
|
|
397
|
+
const subst = new substitution_1.Substitution();
|
|
398
|
+
const skolems = new Set();
|
|
399
|
+
const skolemToName = new Map();
|
|
400
|
+
for (const v of scheme.vars) {
|
|
401
|
+
if (labelIds.has(v)) {
|
|
402
|
+
const name = varNames.get(v);
|
|
403
|
+
const fresh = (0, types_1.tVar)(name);
|
|
404
|
+
subst.set(v, { tag: 'TLabel', kind: 'var', id: fresh.id, name: name ?? fresh.name });
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
if (rowIds.has(v)) {
|
|
408
|
+
subst.set(v, (0, types_1.tRow)(varNames.get(v)));
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (paramVarIds.has(v)) {
|
|
412
|
+
const name = `${types_1.SKOLEM_PREFIX}${skolemCounter++}`;
|
|
413
|
+
skolems.add(name);
|
|
414
|
+
const orig = varNames.get(v);
|
|
415
|
+
const named = !!orig && !types_1.AUTO_TVAR_NAME_RE.test(orig);
|
|
416
|
+
if (named)
|
|
417
|
+
skolemToName.set(name, orig);
|
|
418
|
+
subst.set(v, named ? { ...(0, types_1.tCon)(name), skolemName: orig } : (0, types_1.tCon)(name));
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
// Return-only variable: fresh flexible TVar, keeping the annotation's
|
|
422
|
+
// display name so hover still shows the original letter.
|
|
423
|
+
subst.set(v, (0, types_1.tVar)(varNames.get(v)));
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return { type: subst.apply(scheme.type), skolems, skolemToName };
|
|
427
|
+
}
|
|
428
|
+
function instantiateRigid(scheme) {
|
|
429
|
+
// Fully rigid: every quantified variable becomes a skolem. Used by the
|
|
430
|
+
// post-hoc annotation rigid check (checkAnnotationRigid), which compares an
|
|
431
|
+
// already-scrubbed inferred type against the annotation and therefore wants
|
|
432
|
+
// maximal rigidity to surface genuine mismatches.
|
|
433
|
+
const varNames = new Map();
|
|
434
|
+
(0, types_1.collectVarNames)(scheme.type, varNames);
|
|
435
|
+
// Row- and label-variables stay rows/labels even under full rigidity, so an
|
|
436
|
+
// open record compares structurally rather than as an opaque skolem TCon.
|
|
437
|
+
const labelIds = new Set();
|
|
438
|
+
collectLabelVarIds(scheme.type, labelIds);
|
|
439
|
+
const rowIds = new Set();
|
|
440
|
+
collectRowVarIds(scheme.type, rowIds);
|
|
441
|
+
const subst = new substitution_1.Substitution();
|
|
442
|
+
const skolems = new Set();
|
|
443
|
+
for (const v of scheme.vars) {
|
|
444
|
+
if (labelIds.has(v)) {
|
|
445
|
+
const name = varNames.get(v);
|
|
446
|
+
const fresh = (0, types_1.tVar)(name);
|
|
447
|
+
subst.set(v, { tag: 'TLabel', kind: 'var', id: fresh.id, name: name ?? fresh.name });
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (rowIds.has(v)) {
|
|
451
|
+
subst.set(v, (0, types_1.tRow)(varNames.get(v)));
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const name = `${types_1.SKOLEM_PREFIX}${skolemCounter++}`;
|
|
455
|
+
skolems.add(name);
|
|
456
|
+
subst.set(v, (0, types_1.tCon)(name));
|
|
457
|
+
}
|
|
458
|
+
return { type: subst.apply(scheme.type), skolems };
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Skolemize a rank-N `forall` type for call-site subsumption: every bound
|
|
462
|
+
* variable becomes a fresh rigid skolem constant. The argument supplied for
|
|
463
|
+
* a `forall` parameter slot must type-check against the skolemized body —
|
|
464
|
+
* only a genuinely polymorphic argument can do so, because the skolems unify
|
|
465
|
+
* with nothing concrete. Returns the skolemized body plus the skolem names
|
|
466
|
+
* (for failure detection and escape checking) and a skolem→letter map for
|
|
467
|
+
* readable diagnostics.
|
|
468
|
+
*/
|
|
469
|
+
function skolemizeForall(f) {
|
|
470
|
+
const subst = new substitution_1.Substitution();
|
|
471
|
+
const skolems = new Set();
|
|
472
|
+
const skolemToName = new Map();
|
|
473
|
+
const varToSkolem = new Map();
|
|
474
|
+
for (let i = 0; i < f.vars.length; i++) {
|
|
475
|
+
const name = `${types_1.SKOLEM_PREFIX}${skolemCounter++}`;
|
|
476
|
+
skolems.add(name);
|
|
477
|
+
skolemToName.set(name, f.varNames[i] ?? `t${f.vars[i]}`);
|
|
478
|
+
varToSkolem.set(f.vars[i], name);
|
|
479
|
+
subst.set(f.vars[i], (0, types_1.tCon)(name));
|
|
480
|
+
}
|
|
481
|
+
return { body: subst.apply(f.body), skolems, skolemToName, varToSkolem };
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Build a skolem substitution for a scheme's quantified variables, collecting
|
|
485
|
+
* display names from one or more type "shapes" (e.g. a method annotation's
|
|
486
|
+
* receiver *and* method type) so the skolems can later be scrubbed back to the
|
|
487
|
+
* annotation's letters. Returns the substitution plus the skolem→name map.
|
|
488
|
+
*
|
|
489
|
+
* Apply the returned substitution to each shape separately to obtain rigid
|
|
490
|
+
* receiver / method types that share the same skolems (mirrors how
|
|
491
|
+
* `coordinateInstantiate` shares fresh TVars across receiver + methodType).
|
|
492
|
+
*
|
|
493
|
+
* `preBindings` (scheme var id → type) pins selected variables to
|
|
494
|
+
* caller-supplied types INSTEAD of minting fresh skolems — used to link a
|
|
495
|
+
* nested method annotation's receiver variables to the enclosing annotated
|
|
496
|
+
* factory's own signature variables (which may themselves be skolems).
|
|
497
|
+
*/
|
|
498
|
+
function skolemSubstForScheme(scheme, receiver, methodType, preBindings) {
|
|
499
|
+
const varNames = new Map();
|
|
500
|
+
(0, types_1.collectVarNames)(receiver, varNames);
|
|
501
|
+
(0, types_1.collectVarNames)(methodType, varNames);
|
|
502
|
+
// Freeze only the variables fixed by the caller: every variable in the
|
|
503
|
+
// receiver (`this`) plus every variable in a parameter position of the
|
|
504
|
+
// method type. Variables that occur only in the method's return position
|
|
505
|
+
// stay flexible so the body may return a concrete instance (matching the
|
|
506
|
+
// plain-`::` behaviour in instantiateSkolem).
|
|
507
|
+
const paramVarIds = new Set();
|
|
508
|
+
const recvVars = new Map();
|
|
509
|
+
(0, types_1.collectVarNames)(receiver, recvVars);
|
|
510
|
+
for (const id of recvVars.keys())
|
|
511
|
+
paramVarIds.add(id);
|
|
512
|
+
collectParamPositionVarIds(methodType, paramVarIds);
|
|
513
|
+
// Row- and label-variables must stay rows/labels (never degrade to a skolem
|
|
514
|
+
// TCon): an open record `{...b}` in `this`/param position must remain open
|
|
515
|
+
// so a spread body `{...this, ...obj}` reconstructs the annotated merge and
|
|
516
|
+
// the rendered parameter is `{ b }` rather than the closed `{}`.
|
|
517
|
+
const labelIds = new Set();
|
|
518
|
+
collectLabelVarIds(receiver, labelIds);
|
|
519
|
+
collectLabelVarIds(methodType, labelIds);
|
|
520
|
+
const rowIds = new Set();
|
|
521
|
+
collectRowVarIds(receiver, rowIds);
|
|
522
|
+
collectRowVarIds(methodType, rowIds);
|
|
523
|
+
const subst = new substitution_1.Substitution();
|
|
524
|
+
const skolems = new Set();
|
|
525
|
+
const skolemToName = new Map();
|
|
526
|
+
for (const v of scheme.vars) {
|
|
527
|
+
const pre = preBindings?.get(v);
|
|
528
|
+
if (pre !== undefined) {
|
|
529
|
+
subst.set(v, pre);
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
if (labelIds.has(v)) {
|
|
533
|
+
const name = varNames.get(v);
|
|
534
|
+
const fresh = (0, types_1.tVar)(name);
|
|
535
|
+
subst.set(v, { tag: 'TLabel', kind: 'var', id: fresh.id, name: name ?? fresh.name });
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
if (rowIds.has(v)) {
|
|
539
|
+
subst.set(v, (0, types_1.tRow)(varNames.get(v)));
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
if (paramVarIds.has(v)) {
|
|
543
|
+
const name = `${types_1.SKOLEM_PREFIX}${skolemCounter++}`;
|
|
544
|
+
skolems.add(name);
|
|
545
|
+
const orig = varNames.get(v);
|
|
546
|
+
const named = !!orig && !types_1.AUTO_TVAR_NAME_RE.test(orig);
|
|
547
|
+
if (named)
|
|
548
|
+
skolemToName.set(name, orig);
|
|
549
|
+
subst.set(v, named ? { ...(0, types_1.tCon)(name), skolemName: orig } : (0, types_1.tCon)(name));
|
|
550
|
+
}
|
|
551
|
+
else {
|
|
552
|
+
subst.set(v, (0, types_1.tVar)(varNames.get(v)));
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return { subst, skolems, skolemToName };
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Generalize a type over all variables not free in the environment.
|
|
559
|
+
*
|
|
560
|
+
* When `subst` is supplied it MUST already have been applied to `type`, and
|
|
561
|
+
* the environment's free variables are resolved through it as well. This
|
|
562
|
+
* prevents over-generalizing a `const` whose type shares a variable with an
|
|
563
|
+
* enclosing binding that has since been unified (see `freeTypeVars`).
|
|
564
|
+
*/
|
|
565
|
+
function generalize(env, type, subst) {
|
|
566
|
+
const rawTypeFree = freeTypeVarsInType(type);
|
|
567
|
+
const typeFree = subst ? resolveFreeVarsUnderSubst(rawTypeFree, subst) : rawTypeFree;
|
|
568
|
+
// A type without free variables quantifies nothing: skip the walk over
|
|
569
|
+
// every binding in scope (the expensive half of generalization).
|
|
570
|
+
if (typeFree.size === 0)
|
|
571
|
+
return { vars: [], type };
|
|
572
|
+
const envFree = env.freeTypeVars(subst);
|
|
573
|
+
const vars = [];
|
|
574
|
+
for (const v of typeFree) {
|
|
575
|
+
if (!envFree.has(v)) {
|
|
576
|
+
vars.push(v);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return { vars, type };
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Combine two schemes for the SAME name into a single overloaded scheme.
|
|
583
|
+
*
|
|
584
|
+
* Used when a `.hm` annotation is repeated (e.g. two `Table :: ...` lines)
|
|
585
|
+
* to model "overloaded" / "intersection" function types. The resulting
|
|
586
|
+
* scheme's type is a `Union` of the input branches; call-site unification
|
|
587
|
+
* already supports `Union` and will pick the first branch whose parameter
|
|
588
|
+
* shape matches the actual arguments.
|
|
589
|
+
*
|
|
590
|
+
* Both inputs are first instantiated to fresh type variables so their
|
|
591
|
+
* bound vars never collide. The new scheme quantifies over the union of
|
|
592
|
+
* the freshly-allocated var IDs.
|
|
593
|
+
*/
|
|
594
|
+
function mergeOverloadSchemes(a, b) {
|
|
595
|
+
const refresh = (s) => {
|
|
596
|
+
const subst = new substitution_1.Substitution();
|
|
597
|
+
const fresh = [];
|
|
598
|
+
const names = new Map();
|
|
599
|
+
(0, types_1.collectVarNames)(s.type, names);
|
|
600
|
+
// Preserve the disjoint sort of each quantified id: label-var ids must be
|
|
601
|
+
// refreshed as fresh TLabel-vars and row-var ids as fresh TRow-vars.
|
|
602
|
+
// Mapping a label/row id to a plain TVar is a no-op in `Substitution.apply`
|
|
603
|
+
// (it defensively keeps the original label/row node), which would leave the
|
|
604
|
+
// original id FREE in the merged union — unquantified and therefore shared
|
|
605
|
+
// across every call site, breaking per-call freshening of label keys.
|
|
606
|
+
const labelIds = new Set();
|
|
607
|
+
collectLabelVarIds(s.type, labelIds);
|
|
608
|
+
const rowIds = new Set();
|
|
609
|
+
collectRowVarIds(s.type, rowIds);
|
|
610
|
+
for (const id of s.vars) {
|
|
611
|
+
if (labelIds.has(id)) {
|
|
612
|
+
const name = names.get(id);
|
|
613
|
+
const seed = (0, types_1.tVar)(name);
|
|
614
|
+
subst.set(id, { tag: 'TLabel', kind: 'var', id: seed.id, name: name ?? seed.name });
|
|
615
|
+
fresh.push(seed.id);
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
if (rowIds.has(id)) {
|
|
619
|
+
const freshRow = (0, types_1.tRow)(names.get(id));
|
|
620
|
+
subst.set(id, freshRow);
|
|
621
|
+
fresh.push(freshRow.id);
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
const v = (0, types_1.tVar)(names.get(id));
|
|
625
|
+
subst.set(id, v);
|
|
626
|
+
fresh.push(v.id);
|
|
627
|
+
}
|
|
628
|
+
// Refresh any types embedded in the branch's constraints so their bound
|
|
629
|
+
// vars track the freshly-allocated ids. Constraints reference their
|
|
630
|
+
// primary var by NAME (`typeVarName`), which is preserved by `collectVarNames`
|
|
631
|
+
// above, so nominal-class constraints survive unchanged; only the embedded
|
|
632
|
+
// alternative/positional types need remapping.
|
|
633
|
+
//
|
|
634
|
+
// Also record the branch-specific fresh var id (`varId`) for the primary
|
|
635
|
+
// constrained var. Two overload branches may independently use the same
|
|
636
|
+
// letter (e.g. `Semigroupoid c` in one branch and `Function [b] c` in
|
|
637
|
+
// another); resolving the constraint by NAME at instantiation time would
|
|
638
|
+
// collapse both `c`s onto a single id and mis-attach the constraint to the
|
|
639
|
+
// wrong branch. Carrying the id makes the attachment branch-precise.
|
|
640
|
+
const nameToId = new Map();
|
|
641
|
+
for (const [id, name] of names) {
|
|
642
|
+
if (!nameToId.has(name))
|
|
643
|
+
nameToId.set(name, id);
|
|
644
|
+
}
|
|
645
|
+
const constraints = s.constraints?.map(c => {
|
|
646
|
+
const originalId = nameToId.get(c.typeVarName);
|
|
647
|
+
const refreshedVar = originalId !== undefined
|
|
648
|
+
? subst.apply({ tag: 'TVar', id: originalId, name: c.typeVarName })
|
|
649
|
+
: undefined;
|
|
650
|
+
return {
|
|
651
|
+
...c,
|
|
652
|
+
varId: refreshedVar && refreshedVar.tag === 'TVar' ? refreshedVar.id : c.varId,
|
|
653
|
+
unionAlternatives: c.unionAlternatives?.map(t => subst.apply(t)),
|
|
654
|
+
positionalArgs: c.positionalArgs?.map(t => subst.apply(t)),
|
|
655
|
+
};
|
|
656
|
+
});
|
|
657
|
+
return { vars: fresh, type: subst.apply(s.type), constraints };
|
|
658
|
+
};
|
|
659
|
+
const ra = refresh(a);
|
|
660
|
+
const rb = refresh(b);
|
|
661
|
+
// If `a` was already a union of overload branches, flatten so we don't
|
|
662
|
+
// build a deeply nested union as more overloads accumulate.
|
|
663
|
+
const aBranches = ra.type.tag === 'TCon' && ra.type.name === 'Union'
|
|
664
|
+
? ra.type.args
|
|
665
|
+
: [ra.type];
|
|
666
|
+
const bBranches = rb.type.tag === 'TCon' && rb.type.name === 'Union'
|
|
667
|
+
? rb.type.args
|
|
668
|
+
: [rb.type];
|
|
669
|
+
// Preserve each branch's type-class constraints so a constrained overload
|
|
670
|
+
// (e.g. `Foldable f => … -> f a -> b`) still registers its pending
|
|
671
|
+
// constraint when the merged binding is instantiated. When the constrained
|
|
672
|
+
// branch is the one selected at a call site, its var binds to the concrete
|
|
673
|
+
// argument type and the constraint is validated; when a different branch is
|
|
674
|
+
// selected, the constrained var stays free and the check is skipped.
|
|
675
|
+
const mergedConstraints = [...(ra.constraints ?? []), ...(rb.constraints ?? [])];
|
|
676
|
+
return {
|
|
677
|
+
vars: [...ra.vars, ...rb.vars],
|
|
678
|
+
type: (0, types_1.tUnion)([...aBranches, ...bBranches], { overloadSet: true }),
|
|
679
|
+
constraints: mergedConstraints.length > 0 ? mergedConstraints : undefined,
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
// ---------------------------------------------------------------------------
|
|
683
|
+
// Default environment with JS built-ins
|
|
684
|
+
// ---------------------------------------------------------------------------
|
|
685
|
+
/**
|
|
686
|
+
* Build a TypeEnv pre-populated with common JS globals (console, Math,
|
|
687
|
+
* parseInt, Promise, Map, Set, …). This is the starting environment for
|
|
688
|
+
* every inference run.
|
|
689
|
+
*
|
|
690
|
+
* The result is cached after the first call. The cached environment is built
|
|
691
|
+
* inside a RESERVED high type-variable ID range ({@link BUILTIN_ENV_VAR_BASE})
|
|
692
|
+
* so its baked-in free vars (e.g. `String :: * -> String`, `Array`) can never
|
|
693
|
+
* collide with the per-run fresh vars allocated from 0 during inference. This
|
|
694
|
+
* keeps inference deterministic regardless of how many type variables the
|
|
695
|
+
* current file consumes — previously the counter was restored *after*
|
|
696
|
+
* annotation processing, which could move it backwards and alias unrelated
|
|
697
|
+
* variables, producing intermittent spurious unification errors.
|
|
698
|
+
*/
|
|
699
|
+
let _cachedDefaultEnv = null;
|
|
700
|
+
/**
|
|
701
|
+
* High base offset for the type-variable IDs baked into the cached builtin
|
|
702
|
+
* environment. Per-run inference allocates IDs starting from 0 and is never
|
|
703
|
+
* expected to reach this magnitude, so builtin free vars stay in their own
|
|
704
|
+
* non-overlapping namespace.
|
|
705
|
+
*/
|
|
706
|
+
const BUILTIN_ENV_VAR_BASE = 1000000000;
|
|
707
|
+
/**
|
|
708
|
+
* Invalidate the cached default environment. Called when settings that
|
|
709
|
+
* shape the global scope change (e.g. `hinmil.dom.globalTypes` toggles the
|
|
710
|
+
* `document`/`window`/… bindings between typed TCons and `Any`).
|
|
711
|
+
*/
|
|
712
|
+
function resetDefaultEnvCache() {
|
|
713
|
+
_cachedDefaultEnv = null;
|
|
714
|
+
}
|
|
715
|
+
function defaultEnv() {
|
|
716
|
+
if (_cachedDefaultEnv) {
|
|
717
|
+
return _cachedDefaultEnv;
|
|
718
|
+
}
|
|
719
|
+
// Build the builtin environment in a reserved type-variable ID range so its
|
|
720
|
+
// free vars never collide with per-run fresh vars. Restore the caller's
|
|
721
|
+
// allocator afterwards so normal inference continues from where it was.
|
|
722
|
+
const prevAllocator = (0, types_1.setActiveAllocator)(new types_1.TypeVarAllocator(BUILTIN_ENV_VAR_BASE));
|
|
723
|
+
try {
|
|
724
|
+
return _cachedDefaultEnv = buildDefaultEnv();
|
|
725
|
+
}
|
|
726
|
+
finally {
|
|
727
|
+
(0, types_1.setActiveAllocator)(prevAllocator);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
function buildDefaultEnv() {
|
|
731
|
+
let env = new TypeEnv();
|
|
732
|
+
// console.log : a -> Undefined
|
|
733
|
+
env = env.extend('console', (0, types_1.mono)((0, types_1.tCon)('Console')));
|
|
734
|
+
// Math.floor etc — simplified
|
|
735
|
+
env = env.extend('Math', (0, types_1.mono)((0, types_1.tCon)('Math')));
|
|
736
|
+
// Built-in functions
|
|
737
|
+
// parseInt (string, radix?) — the radix is optional; a 1-argument call is
|
|
738
|
+
// padded with `Undefined` by the arity check.
|
|
739
|
+
env = env.extend('parseInt', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tTuple)([types_1.tString, (0, types_1.tUnion)([types_1.tNumber, types_1.tUndefined])]), types_1.tNumber)));
|
|
740
|
+
env = env.extend('parseFloat', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, types_1.tNumber)));
|
|
741
|
+
env = env.extend('isNaN', (0, types_1.mono)((0, types_1.tFun)(types_1.tNumber, types_1.tBool)));
|
|
742
|
+
// Object — like `Date`, the VALUE is the constructor: `Object (v)` /
|
|
743
|
+
// `new Object (v)` wraps a value in an object. Statics (`Object.keys`, …)
|
|
744
|
+
// still resolve through the MethodRegistry (objectMethods).
|
|
745
|
+
const objArg = (0, types_1.tVar)('a');
|
|
746
|
+
const objKey = (0, types_1.tVar)('k');
|
|
747
|
+
const objVal = (0, types_1.tVar)('b');
|
|
748
|
+
env = env.extend('Object', (0, types_1.scheme)([objArg.id, objKey.id, objVal.id], (0, types_1.tFun)((0, types_1.tTuple)([objArg, types_1.tSpread]), (0, types_1.tCon)('Object', [objKey, objVal]))));
|
|
749
|
+
// Date constructor: `new Date ()`, `new Date (ms | isoString | date)`,
|
|
750
|
+
// `new Date (year, month, …)`. Previously the VALUE `Date` was typed as
|
|
751
|
+
// the instance type, so `new Date (2020, 0, 1)` was "not callable" and
|
|
752
|
+
// `Date` did not satisfy `TypeRep f` (FP-library `is (Date)`). Statics
|
|
753
|
+
// (`Date.now`) are unaffected: they resolve through the method registry.
|
|
754
|
+
const dateArg = (0, types_1.tVar)('a');
|
|
755
|
+
env = env.extend('Date', (0, types_1.scheme)([dateArg.id], (0, types_1.tFun)((0, types_1.tTuple)([dateArg, types_1.tSpread]), (0, types_1.tCon)('Date'))));
|
|
756
|
+
env = env.extend('TypeError', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('TypeError'))));
|
|
757
|
+
// Promise — constructor TFun; statics (resolve/reject/all/race/…) live
|
|
758
|
+
// in MethodRegistry (promiseMethods). `new Promise(executor)` typed as
|
|
759
|
+
// executor: ((a -> ()), (e -> ())) -> x returning Promise e a, where `e`
|
|
760
|
+
// is the rejection (error) channel and `a` the resolution value. The
|
|
761
|
+
// executor's return value is IGNORED at runtime, so it stays a free
|
|
762
|
+
// variable — `new Promise (res => setTimeout (res, ms))` is idiomatic.
|
|
763
|
+
const promVal = (0, types_1.tVar)('a');
|
|
764
|
+
const promErr = (0, types_1.tVar)('e');
|
|
765
|
+
const promExecRet = (0, types_1.tVar)('x');
|
|
766
|
+
env = env.extend('Promise', (0, types_1.scheme)([promVal.id, promErr.id, promExecRet.id], (0, types_1.tFun)((0, types_1.tFun)((0, types_1.tTuple)([(0, types_1.tFun)(promVal, types_1.tVoid), (0, types_1.tFun)(promErr, types_1.tVoid)]), promExecRet), (0, types_1.tCon)('Promise', [promErr, promVal]))));
|
|
767
|
+
// Error constructors
|
|
768
|
+
env = env.extend('Error', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('Error'))));
|
|
769
|
+
env = env.extend('RangeError', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('RangeError'))));
|
|
770
|
+
env = env.extend('ReferenceError', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('ReferenceError'))));
|
|
771
|
+
env = env.extend('SyntaxError', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('SyntaxError'))));
|
|
772
|
+
env = env.extend('URIError', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('URIError'))));
|
|
773
|
+
env = env.extend('EvalError', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('EvalError'))));
|
|
774
|
+
// JSON — namespace TCon; statics (parse/stringify) live in
|
|
775
|
+
// MethodRegistry (jsonMethods).
|
|
776
|
+
env = env.extend('JSON', (0, types_1.mono)((0, types_1.tCon)('JSON')));
|
|
777
|
+
// Reflect — namespace TCon; statics live in MethodRegistry (reflectMethods).
|
|
778
|
+
env = env.extend('Reflect', (0, types_1.mono)((0, types_1.tCon)('Reflect')));
|
|
779
|
+
// Browser globals. With `dom.globalTypes` on (the default) these are
|
|
780
|
+
// TYPED via the compact DOM catalogue in registries/domGlobals.ts —
|
|
781
|
+
// members complete, hover and get argument checking, while anything not
|
|
782
|
+
// catalogued silently resolves to `Any` (open-world fallback). With the
|
|
783
|
+
// setting off they revert to plain `Any` (kill switch for projects that
|
|
784
|
+
// define their own `Element`/`Document` types). Either way, projects can
|
|
785
|
+
// shadow these names via `globals.hm`.
|
|
786
|
+
if ((0, domGlobals_1.isDomGlobalTypesEnabled)()) {
|
|
787
|
+
env = env.extend('document', (0, types_1.mono)((0, types_1.tCon)('Document')));
|
|
788
|
+
env = env.extend('window', (0, types_1.mono)((0, types_1.tCon)('Window')));
|
|
789
|
+
env = env.extend('navigator', (0, types_1.mono)((0, types_1.tCon)('Navigator')));
|
|
790
|
+
env = env.extend('localStorage', (0, types_1.mono)((0, types_1.tCon)('Storage')));
|
|
791
|
+
env = env.extend('sessionStorage', (0, types_1.mono)((0, types_1.tCon)('Storage')));
|
|
792
|
+
}
|
|
793
|
+
else {
|
|
794
|
+
env = env.extend('document', (0, types_1.mono)(types_1.tAny));
|
|
795
|
+
env = env.extend('window', (0, types_1.mono)(types_1.tAny));
|
|
796
|
+
env = env.extend('navigator', (0, types_1.mono)(types_1.tAny));
|
|
797
|
+
env = env.extend('localStorage', (0, types_1.mono)(types_1.tAny));
|
|
798
|
+
env = env.extend('sessionStorage', (0, types_1.mono)(types_1.tAny));
|
|
799
|
+
}
|
|
800
|
+
// Timers — universal across browsers and Node, independent of
|
|
801
|
+
// `dom.globalTypes`. The returned handle is an opaque TCon consumed only
|
|
802
|
+
// by the matching clear* function (Node returns a Timeout object, browsers
|
|
803
|
+
// a Number — the opaque type hides that split). The callback's return
|
|
804
|
+
// value is ignored at runtime, so it stays a free variable. The delay is
|
|
805
|
+
// optional (defaults to 0): `setTimeout (() => res (value))`.
|
|
806
|
+
const timeoutRet = (0, types_1.tVar)('a');
|
|
807
|
+
env = env.extend('setTimeout', (0, types_1.scheme)([timeoutRet.id], (0, types_1.tFun)((0, types_1.tTuple)([(0, types_1.tFun)(types_1.tVoid, timeoutRet), (0, types_1.tUnion)([types_1.tNumber, types_1.tUndefined])]), (0, types_1.tCon)('TimeoutId'))));
|
|
808
|
+
env = env.extend('clearTimeout', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tCon)('TimeoutId'), types_1.tVoid)));
|
|
809
|
+
const intervalRet = (0, types_1.tVar)('a');
|
|
810
|
+
env = env.extend('setInterval', (0, types_1.scheme)([intervalRet.id], (0, types_1.tFun)((0, types_1.tTuple)([(0, types_1.tFun)(types_1.tVoid, intervalRet), (0, types_1.tUnion)([types_1.tNumber, types_1.tUndefined])]), (0, types_1.tCon)('IntervalId'))));
|
|
811
|
+
env = env.extend('clearInterval', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tCon)('IntervalId'), types_1.tVoid)));
|
|
812
|
+
// Fetch API — always available (browser, node ≥18, workers), independent
|
|
813
|
+
// of `dom.globalTypes`. Loose in its arguments (URL string, Request or
|
|
814
|
+
// URL object; optional init), typed in its return: `Promise e Response`.
|
|
815
|
+
const fetchUrl = (0, types_1.tVar)('u');
|
|
816
|
+
const fetchOpts = (0, types_1.tVar)('o');
|
|
817
|
+
const fetchErr = (0, types_1.tVar)('e');
|
|
818
|
+
env = env.extend('fetch', (0, types_1.scheme)([fetchUrl.id, fetchOpts.id, fetchErr.id], (0, types_1.tFun)((0, types_1.tTuple)([fetchUrl, (0, types_1.tUnion)([fetchOpts, types_1.tUndefined])]), (0, types_1.tCon)('Promise', [fetchErr, (0, types_1.tCon)('Response')]))));
|
|
819
|
+
// `AbortSignal` (the value): its static factories, e.g.
|
|
820
|
+
// `fetch (url, { signal: AbortSignal.timeout (5000) })`. The instance type
|
|
821
|
+
// `AbortSignal` lives in the Fetch catalogue (domGlobals.ts).
|
|
822
|
+
env = env.extend('AbortSignal', (0, types_1.mono)((0, types_1.tCon)('AbortSignalStatics')));
|
|
823
|
+
// `new AbortController ()` (WHATWG DOM, a Node and browser global): its
|
|
824
|
+
// `signal` goes into `fetch`, `abort ()` cancels.
|
|
825
|
+
env = env.extend('AbortController', (0, types_1.mono)((0, types_1.tFun)(types_1.tVoid, (0, types_1.tCon)('AbortController'))));
|
|
826
|
+
// `new URLSearchParams (init)` — init is a query string, a record of
|
|
827
|
+
// strings or a list of pairs; loose in its argument, typed in its result.
|
|
828
|
+
const uspInit = (0, types_1.tVar)('a');
|
|
829
|
+
env = env.extend('URLSearchParams', (0, types_1.scheme)([uspInit.id], (0, types_1.tFun)((0, types_1.tUnion)([uspInit, types_1.tUndefined]), (0, types_1.tCon)('URLSearchParams'))));
|
|
830
|
+
// `new URL (input, base?)` (WHATWG URL, a Node and browser global).
|
|
831
|
+
// Without a binding `URL` was an unbound variable.
|
|
832
|
+
env = env.extend('URL', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tTuple)([types_1.tString, (0, types_1.tUnion)([types_1.tString, (0, types_1.tCon)('URL'), types_1.tUndefined])]), (0, types_1.tCon)('URL'))));
|
|
833
|
+
// Conversion functions — polymorphic in their argument so each call site
|
|
834
|
+
// instantiates a FRESH parameter variable. A `mono` scheme would share one
|
|
835
|
+
// cached type variable across the whole program, so two calls like
|
|
836
|
+
// `String(nullVal)` then `String(undefinedVal)` would pin the param to `Null`
|
|
837
|
+
// and then fail to unify with `Undefined`.
|
|
838
|
+
const strArg = (0, types_1.tVar)();
|
|
839
|
+
env = env.extend('String', (0, types_1.scheme)([strArg.id], (0, types_1.tFun)(strArg, types_1.tString)));
|
|
840
|
+
const numArg = (0, types_1.tVar)();
|
|
841
|
+
env = env.extend('Number', (0, types_1.scheme)([numArg.id], (0, types_1.tFun)(numArg, types_1.tNumber)));
|
|
842
|
+
const boolArg = (0, types_1.tVar)();
|
|
843
|
+
env = env.extend('Boolean', (0, types_1.scheme)([boolArg.id], (0, types_1.tFun)(boolArg, types_1.tBool)));
|
|
844
|
+
// Bare `Object.prototype.toString` exposed as a global identifier — the
|
|
845
|
+
// `toString.call(value)` idiom (used by isObject-style predicates) references
|
|
846
|
+
// it without a receiver. Typed as a function so `.call`/`.apply` (Function
|
|
847
|
+
// methods) resolve on it.
|
|
848
|
+
env = env.extend('toString', (0, types_1.mono)((0, types_1.tFun)(types_1.tVoid, types_1.tString)));
|
|
849
|
+
// Array constructor / TypeRep witness — polymorphic so each reference
|
|
850
|
+
// instantiates FRESH variables. A `mono` scheme (as `String`/`Number`
|
|
851
|
+
// above once were) shares one cached element variable across the whole
|
|
852
|
+
// program, so `const t = of (Array)` could not generalize and a later
|
|
853
|
+
// `of (Array) (1)` would retro-pin `t` to `Number -> [Number]`.
|
|
854
|
+
const arrParam = (0, types_1.tVar)();
|
|
855
|
+
const arrElem = (0, types_1.tVar)();
|
|
856
|
+
env = env.extend('Array', (0, types_1.scheme)([arrParam.id, arrElem.id], (0, types_1.tFun)(arrParam, (0, types_1.tArray)(arrElem))));
|
|
857
|
+
// RegExp constructor: `new RegExp (pattern, flags?)` / `RegExp (pattern)`.
|
|
858
|
+
// Without a binding the identifier fell back to the `TypeRep RegExp`
|
|
859
|
+
// witness, which is not callable. A TFun still satisfies `TypeRep f`
|
|
860
|
+
// (unifyTypeRep reads the constructor off the return type).
|
|
861
|
+
env = env.extend('RegExp', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tTuple)([(0, types_1.tUnion)([types_1.tString, (0, types_1.tCon)('RegExp')]), (0, types_1.tUnion)([types_1.tString, types_1.tUndefined])]), (0, types_1.tCon)('RegExp'))));
|
|
862
|
+
env = env.extend('Symbol', (0, types_1.mono)((0, types_1.tFun)(types_1.tString, (0, types_1.tCon)('Symbol'))));
|
|
863
|
+
const bigIntArg = (0, types_1.tVar)();
|
|
864
|
+
env = env.extend('BigInt', (0, types_1.scheme)([bigIntArg.id], (0, types_1.tFun)(bigIntArg, (0, types_1.tCon)('BigInt'))));
|
|
865
|
+
// `globalThis` is the global object: any property may be read or set
|
|
866
|
+
// (`globalThis.ResizeObserver = globalThis.ResizeObserver || Stub`).
|
|
867
|
+
env = env.extend('globalThis', (0, types_1.mono)(types_1.tAnyType));
|
|
868
|
+
// Map/Set/WeakMap/WeakSet constructors: `new Set ()` and `new Set (xs)`.
|
|
869
|
+
// These were bound to the instance type itself (`Set a`), which only worked
|
|
870
|
+
// without arguments: `new Set ([1, 2])` reported "Type 'Set a' is not
|
|
871
|
+
// callable". Like RegExp above, a TFun still satisfies `TypeRep f`. The
|
|
872
|
+
// optional initial value is an array (or another collection); Map entries
|
|
873
|
+
// stay loose (`[[e]]`), as for Object.fromEntries.
|
|
874
|
+
const mapK = (0, types_1.tVar)('k');
|
|
875
|
+
const mapV = (0, types_1.tVar)('v');
|
|
876
|
+
const mapE = (0, types_1.tVar)('e');
|
|
877
|
+
env = env.extend('Map', (0, types_1.scheme)([mapK.id, mapV.id, mapE.id], (0, types_1.tFun)((0, types_1.tUnion)([(0, types_1.tArray)((0, types_1.tArray)(mapE)), (0, types_1.tCon)('Map', [mapK, mapV]), types_1.tUndefined]), (0, types_1.tCon)('Map', [mapK, mapV]))));
|
|
878
|
+
const setA = (0, types_1.tVar)('a');
|
|
879
|
+
env = env.extend('Set', (0, types_1.scheme)([setA.id], (0, types_1.tFun)((0, types_1.tUnion)([(0, types_1.tArray)(setA), (0, types_1.tCon)('Set', [setA]), types_1.tUndefined]), (0, types_1.tCon)('Set', [setA]))));
|
|
880
|
+
const wmK = (0, types_1.tVar)('k');
|
|
881
|
+
const wmV = (0, types_1.tVar)('v');
|
|
882
|
+
const wmE = (0, types_1.tVar)('e');
|
|
883
|
+
env = env.extend('WeakMap', (0, types_1.scheme)([wmK.id, wmV.id, wmE.id], (0, types_1.tFun)((0, types_1.tUnion)([(0, types_1.tArray)((0, types_1.tArray)(wmE)), types_1.tUndefined]), (0, types_1.tCon)('WeakMap', [wmK, wmV]))));
|
|
884
|
+
const wsA = (0, types_1.tVar)('a');
|
|
885
|
+
env = env.extend('WeakSet', (0, types_1.scheme)([wsA.id], (0, types_1.tFun)((0, types_1.tUnion)([(0, types_1.tArray)(wsA), types_1.tUndefined]), (0, types_1.tCon)('WeakSet', [wsA]))));
|
|
886
|
+
// -------------------------------------------------------------------------
|
|
887
|
+
// Node.js globals (Plan v6.4 Werkstroom 5)
|
|
888
|
+
// -------------------------------------------------------------------------
|
|
889
|
+
// process — singleton TCon('Process'); properties/methods live in
|
|
890
|
+
// MethodRegistry (processMethods).
|
|
891
|
+
env = env.extend('process', (0, types_1.mono)((0, types_1.tCon)('Process')));
|
|
892
|
+
env = env.extend('performance', (0, types_1.mono)((0, types_1.tCon)('Performance')));
|
|
893
|
+
// Intl — ECMA-402 namespace (formatters in methodRegistry `intlMethods`).
|
|
894
|
+
env = env.extend('Intl', (0, types_1.mono)((0, types_1.tCon)('Intl')));
|
|
895
|
+
// Standard (ECMAScript) global functions missing so far.
|
|
896
|
+
// eval :: String -> a — the result is unknowable; a fresh var per use.
|
|
897
|
+
const evalRet = (0, types_1.tVar)('a');
|
|
898
|
+
env = env.extend('eval', (0, types_1.scheme)([evalRet.id], (0, types_1.tFun)(types_1.tString, evalRet)));
|
|
899
|
+
// structuredClone :: a -> a
|
|
900
|
+
const cloneArg = (0, types_1.tVar)('a');
|
|
901
|
+
env = env.extend('structuredClone', (0, types_1.scheme)([cloneArg.id], (0, types_1.tFun)(cloneArg, cloneArg)));
|
|
902
|
+
for (const uriFn of ['encodeURIComponent', 'decodeURIComponent', 'encodeURI', 'decodeURI']) {
|
|
903
|
+
env = env.extend(uriFn, (0, types_1.mono)((0, types_1.tFun)(types_1.tString, types_1.tString)));
|
|
904
|
+
}
|
|
905
|
+
// Global isFinite coerces its argument (unlike Number.isFinite).
|
|
906
|
+
const finiteArg = (0, types_1.tVar)('a');
|
|
907
|
+
env = env.extend('isFinite', (0, types_1.scheme)([finiteArg.id], (0, types_1.tFun)(finiteArg, types_1.tBool)));
|
|
908
|
+
// Buffer — namespace TCon for statics (from/alloc/isBuffer/concat/…);
|
|
909
|
+
// statics live in MethodRegistry (bufferMethods, isStatic: true).
|
|
910
|
+
env = env.extend('Buffer', (0, types_1.mono)((0, types_1.tCon)('Buffer')));
|
|
911
|
+
// Typed-array views + binary buffers — constructors are arity-0; `new
|
|
912
|
+
// Uint8Array(len)` / `new ArrayBuffer(len)` accept a length (or source) and
|
|
913
|
+
// return the view. Statics (of/from/isView) live in MethodRegistry.
|
|
914
|
+
for (const taName of [
|
|
915
|
+
'Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array',
|
|
916
|
+
'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array',
|
|
917
|
+
'BigInt64Array', 'BigUint64Array', 'ArrayBuffer', 'DataView',
|
|
918
|
+
]) {
|
|
919
|
+
const taArg = (0, types_1.tVar)();
|
|
920
|
+
env = env.extend(taName, (0, types_1.scheme)([taArg.id], (0, types_1.tFun)(taArg, (0, types_1.tCon)(taName))));
|
|
921
|
+
}
|
|
922
|
+
// CommonJS file/dir constants
|
|
923
|
+
env = env.extend('__dirname', (0, types_1.mono)(types_1.tString));
|
|
924
|
+
env = env.extend('__filename', (0, types_1.mono)(types_1.tString));
|
|
925
|
+
// `module` and `exports` as values (`Object.defineProperty (exports,
|
|
926
|
+
// "__esModule", …)` in compiled CommonJS). Their content is the module's
|
|
927
|
+
// own exports, so `Any`; `module.exports = …` and `exports.x = …` keep
|
|
928
|
+
// their syntactic handling as export sites.
|
|
929
|
+
env = env.extend('module', (0, types_1.mono)(types_1.tAnyType));
|
|
930
|
+
env = env.extend('exports', (0, types_1.mono)(types_1.tAnyType));
|
|
931
|
+
// Global object — loosely typed for compatibility
|
|
932
|
+
env = env.extend('global', (0, types_1.mono)((0, types_1.tCon)('Object')));
|
|
933
|
+
// Microtask / immediate scheduling
|
|
934
|
+
env = env.extend('setImmediate', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tFun)(types_1.tVoid, types_1.tVoid), (0, types_1.tCon)('Object'))));
|
|
935
|
+
env = env.extend('clearImmediate', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tCon)('Object'), types_1.tVoid)));
|
|
936
|
+
env = env.extend('queueMicrotask', (0, types_1.mono)((0, types_1.tFun)((0, types_1.tFun)(types_1.tVoid, types_1.tVoid), types_1.tVoid)));
|
|
937
|
+
return env;
|
|
938
|
+
}
|
|
939
|
+
//# sourceMappingURL=env.js.map
|