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
|
@@ -0,0 +1,1035 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Functional pattern recognition — central home for the LSP's understanding of
|
|
4
|
+
// curried, variadic (`...`) and container-shaped combinators.
|
|
5
|
+
// ============================================================================
|
|
6
|
+
//
|
|
7
|
+
// The type inference for call expressions (`inferCallExpression` in calls.ts)
|
|
8
|
+
// dispatches over a family of *functional patterns* — curry wrappers, curried
|
|
9
|
+
// results, variadic spread chains, spread-array appliers, and the fan-out
|
|
10
|
+
// combinators (converge / combine). This module is the shared home for the
|
|
11
|
+
// pattern classification and for the self-contained pattern handlers, so the
|
|
12
|
+
// recognition logic lives in one place rather than being scattered through the
|
|
13
|
+
// dispatch tree.
|
|
14
|
+
//
|
|
15
|
+
// All pattern handlers now live here in full (curry wrapper, variadic wrapper,
|
|
16
|
+
// curried application, variadic items, spread-array applier, container spread
|
|
17
|
+
// chain, applyArgs peel capture, and fan-out); calls.ts retains only the
|
|
18
|
+
// dispatch that selects among them (see CallPatternKind for the catalogue).
|
|
19
|
+
//
|
|
20
|
+
// ----------------------------------------------------------------------------
|
|
21
|
+
// The chain-combinator subsystem — two DUAL special rules
|
|
22
|
+
// ----------------------------------------------------------------------------
|
|
23
|
+
// Two heterogeneous-fold rules cooperate to type `reduce`-based combinators
|
|
24
|
+
// whose accumulator type varies across the fold (the homogeneous
|
|
25
|
+
// `reduce :: (b -> a -> b) -> b -> f a -> b` cannot express them). Both are
|
|
26
|
+
// detected PURELY STRUCTURALLY — no `reduce`/`Foldable` name is ever matched.
|
|
27
|
+
//
|
|
28
|
+
// R. reduce-over-chain (pipe/compose): a container of ARROWS
|
|
29
|
+
// `f (a->b, ..., y->z)`. FINITE (endomorphic collapse `b = a`).
|
|
30
|
+
// R1a collapse — unify.ts (isArrowChainTuple/collapseArrowChainTuple)
|
|
31
|
+
// R1b trust — annotations.ts (annotationHasArrowChainTuple + skip)
|
|
32
|
+
// R2 call routing — this module (tryContainerSpreadChainCall,
|
|
33
|
+
// inferSpreadArrayCall)
|
|
34
|
+
//
|
|
35
|
+
// A. applyArgs peeling: a single curried fn + a `[a, b, ...]` applier
|
|
36
|
+
// `(a->b->...->z) -> [a, b, ...] -> z = reduce(apply)`. INFINITE
|
|
37
|
+
// (occurs-check `d = c -> d`).
|
|
38
|
+
// A-unify — unify.ts (unifySpreadArrowChain / hasTopLevelSpreadChain)
|
|
39
|
+
// A-infinite — index.ts/statements.ts (suppressInfiniteType, occursCheck)
|
|
40
|
+
// A-peel — this module (captureSpreadChainPeel + inferSpreadArrayCall);
|
|
41
|
+
// the `spreadChainPeel` Map is engine state (index.ts)
|
|
42
|
+
//
|
|
43
|
+
// The pure Type predicates both rules share live in hm/typeUtils.ts (the lowest
|
|
44
|
+
// layer); they are re-exported below so this module is the single, documented
|
|
45
|
+
// reference surface for the subsystem. See sample/demo9.js for a worked
|
|
46
|
+
// demonstration of both rules.
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.CallPatternKind = exports.hasTopLevelSpreadChain = exports.collapseArrowChainTuple = exports.isArrowChainTuple = exports.detectSpreadChainLink = exports.collapseMultiParamLevel = exports.isSpreadTupleOrArray = exports.hasSpreadInArrowChain = void 0;
|
|
49
|
+
exports.matchFanOutCallee = matchFanOutCallee;
|
|
50
|
+
exports.inferFanOutCall = inferFanOutCall;
|
|
51
|
+
exports.applySpreadSlotConstraints = applySpreadSlotConstraints;
|
|
52
|
+
exports.inferSpreadArrayCall = inferSpreadArrayCall;
|
|
53
|
+
exports.inferVariadicItems = inferVariadicItems;
|
|
54
|
+
exports.inferCurryWrapperCall = inferCurryWrapperCall;
|
|
55
|
+
exports.inferVariadicWrapperCall = inferVariadicWrapperCall;
|
|
56
|
+
exports.shouldProtectPolymorphicParamArg = shouldProtectPolymorphicParamArg;
|
|
57
|
+
exports.inferCurriedFunctionCall = inferCurriedFunctionCall;
|
|
58
|
+
exports.tryContainerSpreadChainCall = tryContainerSpreadChainCall;
|
|
59
|
+
exports.captureSpreadChainPeel = captureSpreadChainPeel;
|
|
60
|
+
exports.trySpreadChainCalleePeel = trySpreadChainCalleePeel;
|
|
61
|
+
exports.adaptCallbackArity = adaptCallbackArity;
|
|
62
|
+
exports.classifyCallPattern = classifyCallPattern;
|
|
63
|
+
const types_1 = require("../hm/types");
|
|
64
|
+
const typeUtils_1 = require("../hm/typeUtils");
|
|
65
|
+
Object.defineProperty(exports, "hasSpreadInArrowChain", { enumerable: true, get: function () { return typeUtils_1.hasSpreadInArrowChain; } });
|
|
66
|
+
Object.defineProperty(exports, "isSpreadTupleOrArray", { enumerable: true, get: function () { return typeUtils_1.isSpreadTupleOrArray; } });
|
|
67
|
+
Object.defineProperty(exports, "collapseMultiParamLevel", { enumerable: true, get: function () { return typeUtils_1.collapseMultiParamLevel; } });
|
|
68
|
+
Object.defineProperty(exports, "detectSpreadChainLink", { enumerable: true, get: function () { return typeUtils_1.detectSpreadChainLink; } });
|
|
69
|
+
Object.defineProperty(exports, "isArrowChainTuple", { enumerable: true, get: function () { return typeUtils_1.isArrowChainTuple; } });
|
|
70
|
+
Object.defineProperty(exports, "collapseArrowChainTuple", { enumerable: true, get: function () { return typeUtils_1.collapseArrowChainTuple; } });
|
|
71
|
+
Object.defineProperty(exports, "hasTopLevelSpreadChain", { enumerable: true, get: function () { return typeUtils_1.hasTopLevelSpreadChain; } });
|
|
72
|
+
const narrowing_1 = require("./narrowing");
|
|
73
|
+
const annotations_1 = require("./annotations");
|
|
74
|
+
const helpers_1 = require("./helpers");
|
|
75
|
+
/**
|
|
76
|
+
* The functional call-patterns the inference engine recognises. This enum is
|
|
77
|
+
* the documented catalogue of the dispatch order used by `inferCallExpression`
|
|
78
|
+
* (calls.ts); every handler it names now lives in this module, while the
|
|
79
|
+
* dispatch that selects among them stays inline in the engine's hot path.
|
|
80
|
+
*/
|
|
81
|
+
var CallPatternKind;
|
|
82
|
+
(function (CallPatternKind) {
|
|
83
|
+
/** `curry(f)` — wraps a multi-param function into a curried chain. */
|
|
84
|
+
CallPatternKind["CurryWrapper"] = "CurryWrapper";
|
|
85
|
+
/** The result of a curried application: `a -> b -> ... -> z`. */
|
|
86
|
+
CallPatternKind["CurriedResult"] = "CurriedResult";
|
|
87
|
+
/** A wrapper over a variadic callee: `(a, ..., z) -> r`. */
|
|
88
|
+
CallPatternKind["VariadicWrapper"] = "VariadicWrapper";
|
|
89
|
+
/** A variadic callee applied to positional items: `pipe(f, g, h)`. */
|
|
90
|
+
CallPatternKind["VariadicItems"] = "VariadicItems";
|
|
91
|
+
/** A callee applied to a spread array argument: `f(...xs)`. */
|
|
92
|
+
CallPatternKind["SpreadArrayApplier"] = "SpreadArrayApplier";
|
|
93
|
+
/** A container-wrapped linear spread chain: `f (a->b, b->c, ..., y->z)`. */
|
|
94
|
+
CallPatternKind["ContainerSpreadChain"] = "ContainerSpreadChain";
|
|
95
|
+
/** A curried arrow chain carrying a `...` marker: `a -> b -> ... -> z`. */
|
|
96
|
+
CallPatternKind["SpreadArrowChain"] = "SpreadArrowChain";
|
|
97
|
+
/** A fan-out combinator: `converge([f, g, h])` / `combine([...])`. */
|
|
98
|
+
CallPatternKind["FanOut"] = "FanOut";
|
|
99
|
+
/** An uppercase constructor value used in call position. */
|
|
100
|
+
CallPatternKind["ConstructorCallable"] = "ConstructorCallable";
|
|
101
|
+
/** A callback argument whose arity is adapted to the expected slot. */
|
|
102
|
+
CallPatternKind["CallbackArity"] = "CallbackArity";
|
|
103
|
+
/** The default application path. */
|
|
104
|
+
CallPatternKind["Regular"] = "Regular";
|
|
105
|
+
})(CallPatternKind || (exports.CallPatternKind = CallPatternKind = {}));
|
|
106
|
+
/**
|
|
107
|
+
* Recognise a fan-out combinator callee. Returns the template when the callee
|
|
108
|
+
* is `f (Tuple(HEAD, a -> x1, ..., ...)) -> a -> RESULT` with a Foldable-style
|
|
109
|
+
* container (a constrained type variable applied to a single spread tuple whose
|
|
110
|
+
* first element is a curried arrow chain), otherwise `null`.
|
|
111
|
+
*/
|
|
112
|
+
function matchFanOutCallee(resolvedCallee) {
|
|
113
|
+
if (resolvedCallee.tag !== 'TFun')
|
|
114
|
+
return null;
|
|
115
|
+
const param = resolvedCallee.param;
|
|
116
|
+
// Container: a constrained type var (`$kv:f:N`) applied to exactly one arg.
|
|
117
|
+
const containerKv = param.tag === 'TCon' ? (0, types_1.parseKvName)(param.name) : null;
|
|
118
|
+
if (param.tag !== 'TCon' || !containerKv || param.args.length !== 1)
|
|
119
|
+
return null;
|
|
120
|
+
const inner = param.args[0];
|
|
121
|
+
if (inner.tag !== 'TCon' || inner.name !== types_1.T_TUPLE)
|
|
122
|
+
return null;
|
|
123
|
+
if (!inner.args.some(a => (0, types_1.isSpread)(a)))
|
|
124
|
+
return null;
|
|
125
|
+
// First tuple element must be the curried combiner (a function).
|
|
126
|
+
if (inner.args.length < 2 || inner.args[0].tag !== 'TFun')
|
|
127
|
+
return null;
|
|
128
|
+
// At least one tail element must be a branch `a -> xk`.
|
|
129
|
+
const hasBranch = inner.args.slice(1).some(a => a.tag === 'TFun');
|
|
130
|
+
if (!hasBranch)
|
|
131
|
+
return null;
|
|
132
|
+
// Determine the branch/result wrapper from the call's final result template
|
|
133
|
+
// `a -> RESULT`: if RESULT is `m z` (a constrained var applied to one arg),
|
|
134
|
+
// this is the Applicative-lifting variant (combine); otherwise plain.
|
|
135
|
+
let wrapper = null;
|
|
136
|
+
const resTail = resolvedCallee.result;
|
|
137
|
+
if (resTail.tag === 'TFun' && resTail.result.tag === 'TCon'
|
|
138
|
+
&& (0, types_1.parseKvName)(resTail.result.name) && resTail.result.args.length === 1) {
|
|
139
|
+
wrapper = resTail.result.name;
|
|
140
|
+
}
|
|
141
|
+
return { wrapper, containerVarId: containerKv.varId };
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Infer a fan-out combinator call `converge([combiner, b1, b2, ...])` /
|
|
145
|
+
* `combine([...])`. Positional linking: the combiner is flattened to params
|
|
146
|
+
* `P`; branch `k` is unified with `a -> P[k]` (or `a -> m P[k]` for combine)
|
|
147
|
+
* against a single shared input variable `a`. A branch-count vs combiner-arity
|
|
148
|
+
* mismatch is reported. The call yields `a -> z` (or `a -> m z`).
|
|
149
|
+
*/
|
|
150
|
+
function inferFanOutCall(engine, node, env, template, arrayNode) {
|
|
151
|
+
const elements = arrayNode.elements;
|
|
152
|
+
const aShared = (0, types_1.tVar)();
|
|
153
|
+
const wrap = (t) => template.wrapper ? (0, types_1.tCon)(template.wrapper, [t]) : t;
|
|
154
|
+
// The fan-out argument is an array literal, so the Foldable container `f`
|
|
155
|
+
// is `Array`. Bind its kind-variable id (still unresolved here because
|
|
156
|
+
// this path bypasses the ordinary parameter/argument unification) so the
|
|
157
|
+
// container's declared constraints — `Foldable f`, `Applicative f`,
|
|
158
|
+
// `Monoid (f a)` — resolve to `Array` and are validated exactly as they
|
|
159
|
+
// are for a simple `f a` parameter. Only bind when still unresolved to
|
|
160
|
+
// avoid clobbering a concrete binding from an earlier step.
|
|
161
|
+
const containerVar = { tag: 'TVar', id: template.containerVarId, name: '' };
|
|
162
|
+
if (engine.subst.apply(containerVar).tag === 'TVar') {
|
|
163
|
+
engine.subst.set(template.containerVarId, (0, types_1.tCon)('Array', []));
|
|
164
|
+
}
|
|
165
|
+
if (elements.length === 0) {
|
|
166
|
+
// Empty container: nothing to converge — result is `a -> a`.
|
|
167
|
+
return (0, types_1.tFun)(aShared, aShared);
|
|
168
|
+
}
|
|
169
|
+
// HEAD combiner.
|
|
170
|
+
const combinerType = engine.subst.apply(engine.inferExpression(elements[0], env));
|
|
171
|
+
const flat = (0, types_1.flattenCurriedType)(combinerType);
|
|
172
|
+
const params = flat.params;
|
|
173
|
+
const branches = elements.slice(1);
|
|
174
|
+
// Arity: one branch per combiner parameter.
|
|
175
|
+
if (branches.length !== params.length && params.length > 0) {
|
|
176
|
+
engine.addDiag(`The fan-out combiner takes ${params.length} argument${params.length === 1 ? '' : 's'}, ` +
|
|
177
|
+
`but ${branches.length} branch${branches.length === 1 ? ' was' : 'es were'} supplied`, arrayNode);
|
|
178
|
+
}
|
|
179
|
+
const linked = Math.min(branches.length, params.length);
|
|
180
|
+
for (let k = 0; k < linked; k++) {
|
|
181
|
+
const branchType = engine.subst.apply(engine.inferExpression(branches[k], env));
|
|
182
|
+
const expected = (0, types_1.tFun)(aShared, wrap(engine.subst.apply(params[k])));
|
|
183
|
+
engine.unifyWithDiagnostic(branchType, expected, branches[k]);
|
|
184
|
+
}
|
|
185
|
+
// Branches beyond the combiner's arity (over-supply) still get inferred so
|
|
186
|
+
// their own bodies are checked, but they have no combiner slot to link to.
|
|
187
|
+
for (let k = linked; k < branches.length; k++) {
|
|
188
|
+
engine.inferExpression(branches[k], env);
|
|
189
|
+
}
|
|
190
|
+
const zc = engine.subst.apply(flat.result);
|
|
191
|
+
return (0, types_1.tFun)(engine.subst.apply(aShared), wrap(zc));
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Assign before/after slot constraints and set up chain constraints for a
|
|
195
|
+
* spread-annotated call (both function-arg and array-element variants).
|
|
196
|
+
*
|
|
197
|
+
* - `before` annotation types are unified with the first N slots.
|
|
198
|
+
* - `after` annotation types are unified with the last M slots.
|
|
199
|
+
* - Middle slots get fresh chain-compatible type variables.
|
|
200
|
+
* - Pairwise chain constraints are added across the spread boundary.
|
|
201
|
+
*/
|
|
202
|
+
function applySpreadSlotConstraints(engine, slots, before, after, numItems, reportNode) {
|
|
203
|
+
// Assign before types to first slots
|
|
204
|
+
for (let i = 0; i < before.length && i < numItems; i++) {
|
|
205
|
+
engine.unifyWithDiagnostic(slots[i], before[i], reportNode);
|
|
206
|
+
}
|
|
207
|
+
// Assign after types to last slots (may overlap with before)
|
|
208
|
+
for (let i = 0; i < after.length; i++) {
|
|
209
|
+
const idx = numItems - after.length + i;
|
|
210
|
+
if (idx >= 0) {
|
|
211
|
+
engine.unifyWithDiagnostic(slots[idx], after[i], reportNode);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Add chain constraints across the ... boundary and for middle slots.
|
|
215
|
+
const chain = (0, typeUtils_1.detectSpreadChainLink)(before, after);
|
|
216
|
+
if (chain && (before.length > 0 || after.length > 0) && numItems >= before.length + after.length) {
|
|
217
|
+
const middleStart = before.length;
|
|
218
|
+
const middleEnd = numItems - after.length;
|
|
219
|
+
// Create chain-compatible type slots for middle items
|
|
220
|
+
if (middleStart < middleEnd) {
|
|
221
|
+
const prototype = before[0] ?? after[0];
|
|
222
|
+
for (let i = middleStart; i < middleEnd; i++) {
|
|
223
|
+
const freshSlot = (0, typeUtils_1.freshenType)(prototype);
|
|
224
|
+
engine.unifyWithDiagnostic(slots[i], freshSlot, reportNode);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// Add pairwise chain constraints from lastBefore through all middle to firstAfter.
|
|
228
|
+
const chainStart = before.length > 0 ? before.length - 1 : 0;
|
|
229
|
+
const chainEnd = numItems - after.length;
|
|
230
|
+
for (let i = chainStart; i < chainEnd && i < numItems - 1; i++) {
|
|
231
|
+
const slotI = engine.subst.apply(slots[i]);
|
|
232
|
+
const slotJ = engine.subst.apply(slots[i + 1]);
|
|
233
|
+
if (slotI.tag === 'TFun' && slotJ.tag === 'TFun') {
|
|
234
|
+
if (chain.direction === 'ltr') {
|
|
235
|
+
const outI = (0, typeUtils_1.extractCorresponding)(chain.resultTemplate, slotI.result, chain.linkId);
|
|
236
|
+
const inJ = (0, typeUtils_1.extractCorresponding)(chain.paramTemplate, slotJ.param, chain.linkId);
|
|
237
|
+
if (outI && inJ)
|
|
238
|
+
engine.unifyWithDiagnostic(outI, inJ, reportNode);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
const outJ = (0, typeUtils_1.extractCorresponding)(chain.resultTemplate, slotJ.result, chain.linkId);
|
|
242
|
+
const inI = (0, typeUtils_1.extractCorresponding)(chain.paramTemplate, slotI.param, chain.linkId);
|
|
243
|
+
if (outJ && inI)
|
|
244
|
+
engine.unifyWithDiagnostic(outJ, inI, reportNode);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Infer a spread-array call `[a, ..., z] -> r` (or the container-wrapped
|
|
252
|
+
* `f (a, ..., z)` normalised to the same shape) against a single array-literal
|
|
253
|
+
* argument. Splits the spread tuple into before/after, chain-links the array
|
|
254
|
+
* elements via `inferVariadicItems`, records the array node's hover type, and
|
|
255
|
+
* applies applyArgs-style peeling when the callee captured a curried arity.
|
|
256
|
+
*/
|
|
257
|
+
function inferSpreadArrayCall(engine, node, env, resolvedCallee, arrayNode, expectedResultType) {
|
|
258
|
+
// Arity check: for an arrow *chain* spread `[a->b, b->c, ..., y->z]` the
|
|
259
|
+
// `...` denotes "zero or more links", and the shared boundary types let any
|
|
260
|
+
// length collapse onto the declared endpoints. All degenerate lengths are
|
|
261
|
+
// therefore legal and inferable:
|
|
262
|
+
// 0 links -> the chain is the identity `a -> a` (an empty fold returns
|
|
263
|
+
// the threaded seed unchanged). This mirrors the fan-out
|
|
264
|
+
// empty-container rule in `inferFanOutCall`.
|
|
265
|
+
// 1 link -> the single element's arrow type (the first `before` and last
|
|
266
|
+
// `after` templates unify onto that one slot).
|
|
267
|
+
// N links -> the collapsed chain `first.param -> last.result`.
|
|
268
|
+
// For a plain heterogeneous spread tuple (`[String, ..., Number]`) the
|
|
269
|
+
// before/after positions are independent, so every non-spread element is
|
|
270
|
+
// mandatory.
|
|
271
|
+
const tArgs = resolvedCallee.param.args;
|
|
272
|
+
const spreadIdxAr = tArgs.findIndex(a => (0, types_1.isSpread)(a));
|
|
273
|
+
const nonSpreadAr = tArgs.filter(a => !(0, types_1.isSpread)(a));
|
|
274
|
+
const isArrowChain = nonSpreadAr.length > 0 && nonSpreadAr.every(a => a.tag === 'TFun');
|
|
275
|
+
const elemCount = arrayNode.elements.length;
|
|
276
|
+
if (isArrowChain && elemCount === 0) {
|
|
277
|
+
// Empty arrow chain: nothing to compose — the result is the identity
|
|
278
|
+
// `a -> a`. Record an empty-array hover type and short-circuit before
|
|
279
|
+
// the slot machinery (which has no elements to link).
|
|
280
|
+
engine.recordNodeType(arrayNode, (0, types_1.tArray)((0, types_1.tVar)()));
|
|
281
|
+
const identityInput = (0, types_1.tVar)();
|
|
282
|
+
return (0, types_1.tFun)(identityInput, identityInput);
|
|
283
|
+
}
|
|
284
|
+
const minRequired = spreadIdxAr >= 0
|
|
285
|
+
? (isArrowChain
|
|
286
|
+
? 1
|
|
287
|
+
: tArgs.length - 1)
|
|
288
|
+
: tArgs.length;
|
|
289
|
+
if (elemCount < minRequired) {
|
|
290
|
+
engine.addDiag(`Array literal has ${elemCount} element${elemCount === 1 ? '' : 's'}, expected at least ${minRequired} for type '${(0, types_1.typeToString)(resolvedCallee.param)}'`, arrayNode);
|
|
291
|
+
}
|
|
292
|
+
const result = inferVariadicItems(engine, node, env, resolvedCallee, arrayNode.elements, expectedResultType);
|
|
293
|
+
// Record a standard Array type on the array node for hover display
|
|
294
|
+
const firstNonSpread = tArgs.find(a => !(0, types_1.isSpread)(a));
|
|
295
|
+
const arrayElemType = firstNonSpread ? (0, typeUtils_1.freshenType)(firstNonSpread) : (0, types_1.tVar)();
|
|
296
|
+
engine.recordNodeType(arrayNode, (0, types_1.tArray)(engine.subst.apply(arrayElemType)));
|
|
297
|
+
// applyArgs-style peeling. If the callee of THIS array application is
|
|
298
|
+
// a combinator partial whose function argument's arity we captured,
|
|
299
|
+
// derive the result by peeling that function by the array length:
|
|
300
|
+
// m > n -> over-application (runtime crash) => diagnostic
|
|
301
|
+
// m < n -> partial application => result is the curried remainder
|
|
302
|
+
// m === n -> full application => keep the chain terminal result
|
|
303
|
+
// (n counts curried levels; the array literal supplies m arguments.)
|
|
304
|
+
const peel = engine.spreadChainPeel.get(node.callee);
|
|
305
|
+
if (peel) {
|
|
306
|
+
const n = peel.params.length;
|
|
307
|
+
const m = elemCount;
|
|
308
|
+
if (m > n) {
|
|
309
|
+
engine.addDiag(`Array literal has ${m} element${m === 1 ? '' : 's'}, but the function takes at most ${n} argument${n === 1 ? '' : 's'}`, arrayNode);
|
|
310
|
+
}
|
|
311
|
+
else if (m < n) {
|
|
312
|
+
let remainder = engine.subst.apply(peel.result);
|
|
313
|
+
for (let i = n - 1; i >= m; i--) {
|
|
314
|
+
remainder = (0, types_1.tFun)(engine.subst.apply(peel.params[i]), remainder);
|
|
315
|
+
}
|
|
316
|
+
return remainder;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Shared logic for inferring variadic tuple/array calls (with `...` spread).
|
|
323
|
+
*
|
|
324
|
+
* The callee has a function type whose param is a Tuple or SpreadArray that
|
|
325
|
+
* contains a spread marker. We split the tuple into "before" (types before
|
|
326
|
+
* `...`) and "after" (types after `...`). Before types are assigned to the
|
|
327
|
+
* first N items, after types to the last M items. Overlapping positions
|
|
328
|
+
* (when actualItems < before+after) unify with both — naturally connecting
|
|
329
|
+
* the chain pattern (e.g. pipe, compose). Middle items get fresh type vars.
|
|
330
|
+
*
|
|
331
|
+
* `items` is either function arguments or array literal elements.
|
|
332
|
+
*/
|
|
333
|
+
function inferVariadicItems(engine, node, env, resolvedCallee, items, expectedResultType) {
|
|
334
|
+
const numItems = items.length;
|
|
335
|
+
const calleeFun = resolvedCallee;
|
|
336
|
+
const tupleArgs = calleeFun.param.args;
|
|
337
|
+
const spreadIdx = tupleArgs.findIndex(a => (0, types_1.isSpread)(a));
|
|
338
|
+
const before = tupleArgs.slice(0, spreadIdx);
|
|
339
|
+
const after = tupleArgs.slice(spreadIdx + 1);
|
|
340
|
+
const resultType = (0, types_1.tVar)();
|
|
341
|
+
engine.unifyWithDiagnostic(resultType, calleeFun.result, node);
|
|
342
|
+
if (expectedResultType) {
|
|
343
|
+
engine.unifyWithDiagnostic(resultType, expectedResultType, node);
|
|
344
|
+
}
|
|
345
|
+
const slots = (0, helpers_1.freshVars)(numItems);
|
|
346
|
+
applySpreadSlotConstraints(engine, slots, before, after, numItems, node);
|
|
347
|
+
for (let i = 0; i < numItems; i++) {
|
|
348
|
+
if (items[i]) {
|
|
349
|
+
const expectedItemType = engine.subst.apply(slots[i]);
|
|
350
|
+
const itemType = engine.inferExpressionWithExpected(items[i], env, expectedItemType);
|
|
351
|
+
// Use the already-applied expected type rather than the raw slot TVar so
|
|
352
|
+
// that auto-union does not bind the slot to a Union when an annotation
|
|
353
|
+
// gave it a concrete type — we want a clear mismatch diagnostic.
|
|
354
|
+
engine.unifyWithDiagnostic(itemType, expectedItemType, items[i]);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return engine.subst.apply(resultType);
|
|
358
|
+
}
|
|
359
|
+
function inferCurryWrapperCall(engine, node, env, expectedResultType) {
|
|
360
|
+
// Infer the callee (the curry function itself) to record its type
|
|
361
|
+
engine.inferExpression(node.callee, env);
|
|
362
|
+
// An OVERLOADED binding annotation (union of function branches, e.g.
|
|
363
|
+
// an FP library's `ap :: Apply f => f (a -> b) -> f a -> f b` +
|
|
364
|
+
// `ap :: (a -> b -> c) -> (a -> b) -> a -> c`) is not a single expected
|
|
365
|
+
// type for the curried result: unifying against the union always fails.
|
|
366
|
+
// The binding-level overload check (`maybeRigidCheck`) verifies the body
|
|
367
|
+
// branch-wise, exactly as for a plain nested-arrow implementation.
|
|
368
|
+
if (expectedResultType) {
|
|
369
|
+
const exp = engine.subst.apply(expectedResultType);
|
|
370
|
+
if (exp.tag === 'TCon' && exp.name === types_1.T_UNION && exp.args.length >= 2
|
|
371
|
+
&& exp.args.every(b => b.tag === 'TFun')) {
|
|
372
|
+
expectedResultType = undefined;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (node.arguments.length !== 1) {
|
|
376
|
+
engine.addDiag(`Curry wrapper expects exactly 1 argument (the function to curry), got ${node.arguments.length}`, (0, annotations_1.narrowToIdentifier)(node));
|
|
377
|
+
return (0, types_1.tVar)();
|
|
378
|
+
}
|
|
379
|
+
// If the call site has an annotation in curried form
|
|
380
|
+
// (e.g. `// foo :: String -> a -> Boolean`), build a tuple-form
|
|
381
|
+
// expected type for the lambda argument so its parameters pick up
|
|
382
|
+
// the annotated types BEFORE body inference, instead of being left
|
|
383
|
+
// as fresh tvars that drift to default numerics.
|
|
384
|
+
let expectedArgType;
|
|
385
|
+
const fnArg = node.arguments[0];
|
|
386
|
+
const fnArity = (fnArg && (fnArg.type === 'ArrowFunctionExpression' || fnArg.type === 'FunctionExpression'))
|
|
387
|
+
? (fnArg.params?.length ?? 0)
|
|
388
|
+
: 0;
|
|
389
|
+
if (expectedResultType && fnArity > 1) {
|
|
390
|
+
const flat = (0, types_1.flattenCurriedType)(engine.subst.apply(expectedResultType));
|
|
391
|
+
if (flat.params.length >= fnArity) {
|
|
392
|
+
const tupleParams = flat.params.slice(0, fnArity);
|
|
393
|
+
// Result is whatever remains after consuming `fnArity` params
|
|
394
|
+
// (re-curry the remaining params with the original tail).
|
|
395
|
+
let remainingResult = flat.result;
|
|
396
|
+
for (let i = flat.params.length - 1; i >= fnArity; i--) {
|
|
397
|
+
remainingResult = (0, types_1.tFun)(flat.params[i], remainingResult);
|
|
398
|
+
}
|
|
399
|
+
expectedArgType = (0, types_1.tFun)((0, types_1.tTuple)(tupleParams), remainingResult);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
// Infer the argument (the function being curried)
|
|
403
|
+
const argType = expectedArgType
|
|
404
|
+
? engine.inferExpressionWithExpected(node.arguments[0], env, expectedArgType)
|
|
405
|
+
: engine.inferExpression(node.arguments[0], env);
|
|
406
|
+
const resolvedArgType = engine.subst.apply(argType);
|
|
407
|
+
// Helper: build a fully curried type from one TFun branch. Returns
|
|
408
|
+
// the original type when it's not a multi-param tuple function (curry
|
|
409
|
+
// is then a no-op).
|
|
410
|
+
const curryBranch = (branch) => {
|
|
411
|
+
if (branch.tag !== 'TFun')
|
|
412
|
+
return null;
|
|
413
|
+
if (!(0, types_1.isTuple)(branch.param))
|
|
414
|
+
return branch;
|
|
415
|
+
const ps = branch.param.args;
|
|
416
|
+
if (ps.length <= 1)
|
|
417
|
+
return branch;
|
|
418
|
+
let cur = branch.result;
|
|
419
|
+
for (let i = ps.length - 1; i >= 0; i--)
|
|
420
|
+
cur = (0, types_1.tFun)(ps[i], cur);
|
|
421
|
+
return cur;
|
|
422
|
+
};
|
|
423
|
+
// Overloaded function: curry each branch and re-union the results so
|
|
424
|
+
// call sites continue to pick the matching overload.
|
|
425
|
+
if (resolvedArgType.tag === 'TCon' && resolvedArgType.name === types_1.T_UNION
|
|
426
|
+
&& resolvedArgType.args.every(a => a.tag === 'TFun')) {
|
|
427
|
+
const curried = [];
|
|
428
|
+
for (const branch of resolvedArgType.args) {
|
|
429
|
+
const c = curryBranch(branch);
|
|
430
|
+
if (c)
|
|
431
|
+
curried.push(c);
|
|
432
|
+
}
|
|
433
|
+
if (curried.length === 0) {
|
|
434
|
+
engine.addDiag(`Curry wrapper expects a function argument`, node.arguments[0]);
|
|
435
|
+
return (0, types_1.tVar)();
|
|
436
|
+
}
|
|
437
|
+
engine.pendingIsCurriedResult = true;
|
|
438
|
+
const result = curried.length === 1 ? curried[0] : (0, types_1.tCon)(types_1.T_UNION, curried);
|
|
439
|
+
if (expectedResultType)
|
|
440
|
+
engine.unifyWithDiagnostic(result, expectedResultType, node);
|
|
441
|
+
return engine.subst.apply(result);
|
|
442
|
+
}
|
|
443
|
+
if (resolvedArgType.tag !== 'TFun') {
|
|
444
|
+
engine.addDiag(`Curry wrapper expects a function argument`, node.arguments[0]);
|
|
445
|
+
return (0, types_1.tVar)();
|
|
446
|
+
}
|
|
447
|
+
let params;
|
|
448
|
+
let result;
|
|
449
|
+
if ((0, types_1.isTuple)(resolvedArgType.param)) {
|
|
450
|
+
// Multi-param function: (A, B, C) -> R
|
|
451
|
+
params = resolvedArgType.param.args;
|
|
452
|
+
result = resolvedArgType.result;
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
// Single-param function: not useful to curry. Return as-is.
|
|
456
|
+
engine.pendingIsCurriedResult = false;
|
|
457
|
+
return resolvedArgType;
|
|
458
|
+
}
|
|
459
|
+
if (params.length <= 1) {
|
|
460
|
+
// 0 or 1 param — curry is a no-op
|
|
461
|
+
engine.pendingIsCurriedResult = false;
|
|
462
|
+
return resolvedArgType;
|
|
463
|
+
}
|
|
464
|
+
// Build fully curried type: A -> B -> C -> R
|
|
465
|
+
let curriedType = result;
|
|
466
|
+
for (let i = params.length - 1; i >= 0; i--) {
|
|
467
|
+
curriedType = (0, types_1.tFun)(params[i], curriedType);
|
|
468
|
+
}
|
|
469
|
+
engine.pendingIsCurriedResult = true;
|
|
470
|
+
if (expectedResultType) {
|
|
471
|
+
engine.unifyWithDiagnostic(curriedType, expectedResultType, node);
|
|
472
|
+
}
|
|
473
|
+
return engine.subst.apply(curriedType);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Arity-tolerant unification for an aligned callback parameter pair. When
|
|
477
|
+
* both sides are function types and the ACTUAL declares fewer parameters than
|
|
478
|
+
* the EXPECTED callback supplies, JS simply leaves the surplus parameters
|
|
479
|
+
* undefined — align the declared prefix and the results (e.g. a node-style
|
|
480
|
+
* `cb` used as `cb ("err")` against a slot's `(e, a) -> ()`). Falls back to
|
|
481
|
+
* plain unification in every other case.
|
|
482
|
+
*/
|
|
483
|
+
function unifyCallbackParamTolerant(engine, actual, expected, argNode) {
|
|
484
|
+
const a = engine.subst.apply(actual);
|
|
485
|
+
const e = engine.subst.apply(expected);
|
|
486
|
+
if (a.tag === 'TFun' && e.tag === 'TFun') {
|
|
487
|
+
const apl = (0, types_1.isTuple)(a.param) ? a.param.args : [a.param];
|
|
488
|
+
const epl = (0, types_1.isTuple)(e.param) ? e.param.args : [e.param];
|
|
489
|
+
if (apl.length < epl.length && !apl.some(types_1.isSpread) && !epl.some(types_1.isSpread)) {
|
|
490
|
+
for (let j = 0; j < apl.length; j++) {
|
|
491
|
+
engine.unifyWithDiagnostic(apl[j], epl[j], argNode);
|
|
492
|
+
}
|
|
493
|
+
engine.unifyWithDiagnostic(a.result, e.result, argNode);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
engine.unifyWithDiagnostic(actual, expected, argNode);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Handle a call to a spread-function wrapper like memoize/once/tryCatch.
|
|
501
|
+
*
|
|
502
|
+
* Pattern: ((a, b, ...) -> z) -> (a, b, ...) -> z
|
|
503
|
+
* The callee takes a function with a spread tuple param and returns a
|
|
504
|
+
* function with the same (or modified) shape. We unify the argument's
|
|
505
|
+
* concrete param list against the spread pattern and reconstruct the
|
|
506
|
+
* result type with the concrete param list (stripping spread markers).
|
|
507
|
+
*/
|
|
508
|
+
function inferVariadicWrapperCall(engine, node, env, calleeType, resolvedCallee, expectedResultType) {
|
|
509
|
+
const calleeFun = resolvedCallee;
|
|
510
|
+
const paramFun = calleeFun.param;
|
|
511
|
+
const paramTuple = paramFun.param;
|
|
512
|
+
// Infer the argument (the function being wrapped)
|
|
513
|
+
const argType = engine.inferExpression(node.arguments[0], env);
|
|
514
|
+
const resolvedArg = engine.subst.apply(argType);
|
|
515
|
+
// Extract concrete param types from the argument
|
|
516
|
+
let concreteParams;
|
|
517
|
+
let concreteResult;
|
|
518
|
+
if (resolvedArg.tag === 'TFun' && (0, types_1.isTuple)(resolvedArg.param)) {
|
|
519
|
+
concreteParams = resolvedArg.param.args;
|
|
520
|
+
concreteResult = resolvedArg.result;
|
|
521
|
+
}
|
|
522
|
+
else if (resolvedArg.tag === 'TFun') {
|
|
523
|
+
// Single-param function
|
|
524
|
+
concreteParams = [resolvedArg.param];
|
|
525
|
+
concreteResult = resolvedArg.result;
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
// Can't determine — fall back to standard inference
|
|
529
|
+
const paramType = (0, types_1.tVar)();
|
|
530
|
+
const resultType = (0, types_1.tVar)();
|
|
531
|
+
const calleeFunType = (0, types_1.tFun)(paramType, resultType);
|
|
532
|
+
engine.unifyWithDiagnostic(calleeType, calleeFunType, node.callee);
|
|
533
|
+
engine.unifyArgWithDiagnostic(argType, engine.subst.apply(paramType), node.arguments[0]);
|
|
534
|
+
if (expectedResultType) {
|
|
535
|
+
engine.unifyWithDiagnostic(resultType, expectedResultType, node);
|
|
536
|
+
}
|
|
537
|
+
return engine.subst.apply(resultType);
|
|
538
|
+
}
|
|
539
|
+
// Bind the spread pattern's type vars to concrete types
|
|
540
|
+
const spreadIdx = paramTuple.args.findIndex(a => (0, types_1.isSpread)(a));
|
|
541
|
+
const beforePattern = paramTuple.args.slice(0, spreadIdx);
|
|
542
|
+
const afterPattern = paramTuple.args.slice(spreadIdx + 1);
|
|
543
|
+
// Unify before elements
|
|
544
|
+
for (let i = 0; i < beforePattern.length && i < concreteParams.length; i++) {
|
|
545
|
+
unifyCallbackParamTolerant(engine, concreteParams[i], beforePattern[i], node.arguments[0]);
|
|
546
|
+
}
|
|
547
|
+
// Unify after elements (from the end)
|
|
548
|
+
for (let i = 0; i < afterPattern.length; i++) {
|
|
549
|
+
const concreteIdx = concreteParams.length - afterPattern.length + i;
|
|
550
|
+
if (concreteIdx >= 0) {
|
|
551
|
+
unifyCallbackParamTolerant(engine, concreteParams[concreteIdx], afterPattern[i], node.arguments[0]);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
// Unify the result type var
|
|
555
|
+
engine.unifyWithDiagnostic(paramFun.result, concreteResult, node.arguments[0]);
|
|
556
|
+
// Now build the return type by applying substitutions to the annotation's
|
|
557
|
+
// result type, but replacing any spread Tuples with the concrete param list.
|
|
558
|
+
const resultType = engine.subst.apply(calleeFun.result);
|
|
559
|
+
const finalResult = (0, typeUtils_1.replaceSpreadTupleWithConcrete)(resultType, concreteParams);
|
|
560
|
+
if (expectedResultType) {
|
|
561
|
+
engine.unifyWithDiagnostic(finalResult, expectedResultType, node);
|
|
562
|
+
}
|
|
563
|
+
return engine.subst.apply(finalResult);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Handle a call to a curried function (result of curry wrapper) with any number of arguments.
|
|
567
|
+
*
|
|
568
|
+
* For a curried type A -> B -> C -> R:
|
|
569
|
+
* - Called with 1 arg: unify arg with A, return B -> C -> R
|
|
570
|
+
* - Called with 2 args: unify args with [A, B], return C -> R
|
|
571
|
+
* - Called with 3 args: unify args with [A, B, C], return R
|
|
572
|
+
*/
|
|
573
|
+
/**
|
|
574
|
+
* Decide whether a call argument that references an annotated, still-
|
|
575
|
+
* polymorphic parameter should be protected from being pinned by the callee's
|
|
576
|
+
* concrete parameter type. Returns true only when:
|
|
577
|
+
* • the argument is a bare Identifier,
|
|
578
|
+
* • it resolves to a parameter whose binding carries a `frozenDisplay`
|
|
579
|
+
* (i.e. an annotated parameter),
|
|
580
|
+
* • that declared type is an *overload union* of unrelated branches
|
|
581
|
+
* (e.g. `Foldable f => f a | String`) — NOT a single polymorphic type
|
|
582
|
+
* such as `a -> b`, which must still unify so genuine mismatches surface,
|
|
583
|
+
* • the current argument type is still polymorphic (unresolved), and
|
|
584
|
+
* • the callee slot it would unify against is MORE concrete than a bare
|
|
585
|
+
* variable (so the unification would actually specialise the parameter).
|
|
586
|
+
*
|
|
587
|
+
* In that situation the parameter is being used purely as a value passed to an
|
|
588
|
+
* unrelated helper (such as a `hasProp` membership test); pinning its variable
|
|
589
|
+
* would leak the helper's parameter type back onto the parameter everywhere.
|
|
590
|
+
*/
|
|
591
|
+
function shouldProtectPolymorphicParamArg(engine, argNode, env, argSlot) {
|
|
592
|
+
if (argNode.type !== 'Identifier')
|
|
593
|
+
return false;
|
|
594
|
+
const scheme = env.lookup(argNode.name);
|
|
595
|
+
if (!scheme || !scheme.frozenDisplay)
|
|
596
|
+
return false;
|
|
597
|
+
// The declared type must be an overload union of unrelated branches. A
|
|
598
|
+
// single polymorphic type (e.g. `a -> b`) is intentionally left unprotected
|
|
599
|
+
// so that passing it to a callee whose slot has more structure (e.g. a
|
|
600
|
+
// constraint method expecting `a -> m b`) still reports a real mismatch.
|
|
601
|
+
const declared = scheme.frozenDisplay;
|
|
602
|
+
if (declared.tag !== 'TCon' || declared.name !== types_1.T_UNION)
|
|
603
|
+
return false;
|
|
604
|
+
// The live argument type must still be polymorphic (unresolved); if the body
|
|
605
|
+
// already specialised it to something concrete, respect that.
|
|
606
|
+
const resolvedArg = engine.subst.apply(scheme.type);
|
|
607
|
+
const argVars = new Set();
|
|
608
|
+
(0, typeUtils_1.collectTVarIdsFromType)(resolvedArg, argVars);
|
|
609
|
+
if (argVars.size === 0)
|
|
610
|
+
return false;
|
|
611
|
+
// The callee slot must be more concrete than a bare variable — otherwise no
|
|
612
|
+
// specialisation would occur and protection is unnecessary.
|
|
613
|
+
const resolvedSlot = engine.subst.apply(argSlot);
|
|
614
|
+
if (resolvedSlot.tag === 'TVar')
|
|
615
|
+
return false;
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* `__` or `ns.__` whose type is `Placeholder`: Ramda's placeholder, which
|
|
620
|
+
* leaves a parameter of a curried function open. The name is checked first,
|
|
621
|
+
* so ordinary arguments are inferred once, with their expected type.
|
|
622
|
+
*/
|
|
623
|
+
function isPlaceholderArgument(engine, arg, env) {
|
|
624
|
+
const name = arg.type === 'Identifier'
|
|
625
|
+
? arg.name
|
|
626
|
+
: arg.type === 'MemberExpression' && !arg.computed
|
|
627
|
+
&& arg.property?.type === 'Identifier'
|
|
628
|
+
? arg.property.name
|
|
629
|
+
: null;
|
|
630
|
+
if (name !== '__')
|
|
631
|
+
return false;
|
|
632
|
+
const t = engine.subst.apply(engine.inferExpression(arg, env));
|
|
633
|
+
return t.tag === 'TCon' && t.name === 'Placeholder' && t.args.length === 0;
|
|
634
|
+
}
|
|
635
|
+
function inferCurriedFunctionCall(engine, node, env, calleeType, curryInfo, expectedResultType) {
|
|
636
|
+
const numArgs = node.arguments.length;
|
|
637
|
+
const { params, result } = curryInfo;
|
|
638
|
+
// Zero-arg call: treat as passing `undefined` for the first parameter.
|
|
639
|
+
// Consume the parameter so that subsequent chained calls shift to the
|
|
640
|
+
// next param (e.g. takeWhile()([2]) checks [2] against Array a, not
|
|
641
|
+
// against a -> Boolean). Only the first `()` gets the error.
|
|
642
|
+
if (numArgs === 0) {
|
|
643
|
+
if (params.length > 0) {
|
|
644
|
+
// Highlight only the "()" part, not the whole call expression
|
|
645
|
+
const parensLoc = { start: node.callee.end, end: node.end };
|
|
646
|
+
// A genuine nullary step (`() -> R`, param is the Void marker) is
|
|
647
|
+
// satisfied by the empty call itself — exactly as on the regular call
|
|
648
|
+
// path (calls.ts). Only a real parameter receives `undefined`.
|
|
649
|
+
const first = engine.subst.apply(params[0]);
|
|
650
|
+
if (!(first.tag === 'TCon' && first.name === 'Void' && first.args.length === 0)) {
|
|
651
|
+
engine.unifyWithDiagnostic(types_1.tUndefined, params[0], parensLoc);
|
|
652
|
+
}
|
|
653
|
+
// Build the remaining curried type after consuming the first param
|
|
654
|
+
const remainingParams = params.slice(1);
|
|
655
|
+
let restType = result;
|
|
656
|
+
for (let i = remainingParams.length - 1; i >= 0; i--) {
|
|
657
|
+
restType = (0, types_1.tFun)(remainingParams[i], restType);
|
|
658
|
+
}
|
|
659
|
+
if (remainingParams.length > 0) {
|
|
660
|
+
engine.pendingIsCurriedResult = true;
|
|
661
|
+
}
|
|
662
|
+
return engine.subst.apply(restType);
|
|
663
|
+
}
|
|
664
|
+
return engine.subst.apply(calleeType);
|
|
665
|
+
}
|
|
666
|
+
if (numArgs === 1) {
|
|
667
|
+
// Single arg: use normal inference (works naturally with curried types)
|
|
668
|
+
// But still set pendingIsCurriedResult if there are remaining params
|
|
669
|
+
const resultType = (0, types_1.tVar)();
|
|
670
|
+
const argSlot = (0, types_1.tVar)();
|
|
671
|
+
const calleeFunType = (0, types_1.tFun)(argSlot, resultType);
|
|
672
|
+
engine.unifyWithDiagnostic(calleeType, calleeFunType, node.callee);
|
|
673
|
+
// Check if conditional return narrowing may apply. When it does,
|
|
674
|
+
// we must defer the expectedResultType unification until AFTER
|
|
675
|
+
// narrowing so that the narrowed (not the original union) type
|
|
676
|
+
// propagates into the caller's type variables.
|
|
677
|
+
const calleeName = node.callee.type === 'Identifier' ? node.callee.name : null;
|
|
678
|
+
const calleeScheme = calleeName ? env.lookup(calleeName) : null;
|
|
679
|
+
const deferExpected = calleeScheme?.conditionalReturn != null;
|
|
680
|
+
if (expectedResultType && !deferExpected) {
|
|
681
|
+
engine.unifyWithDiagnostic(resultType, expectedResultType, node);
|
|
682
|
+
}
|
|
683
|
+
const expectedArgType = engine.subst.apply(argSlot);
|
|
684
|
+
const argType = engine.inferExpressionWithExpected(node.arguments[0], env, expectedArgType);
|
|
685
|
+
// Same protection as the regular call path: an overload-annotated
|
|
686
|
+
// parameter passed to an unrelated helper (`hasProp ("length")
|
|
687
|
+
// (foldable)` in an FP library's `size`) must not be pinned to the helper's
|
|
688
|
+
// parameter type — also when the helper is a `curry (…)` result.
|
|
689
|
+
const effectiveArgType = shouldProtectPolymorphicParamArg(engine, node.arguments[0], env, argSlot)
|
|
690
|
+
? (0, typeUtils_1.freshenType)(engine.subst.apply(argType))
|
|
691
|
+
: argType;
|
|
692
|
+
// Directional, like the multi-argument path below. Against the
|
|
693
|
+
// RESOLVED slot: the fresh `argSlot` is bound to the callee's parameter
|
|
694
|
+
// by now, and a still-variable slot would let auto-union widen it
|
|
695
|
+
// instead of reporting a wrong argument (`curry (f) ("2")`).
|
|
696
|
+
engine.unifyArgWithDiagnostic(effectiveArgType, engine.subst.apply(argSlot), node.arguments[0]);
|
|
697
|
+
// If remaining params > 1, result is still curried
|
|
698
|
+
if (params.length > 1) {
|
|
699
|
+
engine.pendingIsCurriedResult = true;
|
|
700
|
+
}
|
|
701
|
+
if (deferExpected) {
|
|
702
|
+
// Apply narrowing first, then unify with expected type
|
|
703
|
+
let narrowed = (0, narrowing_1.applyConditionalReturnNarrowing)(engine.subst, node, env, engine.subst.apply(resultType));
|
|
704
|
+
if (expectedResultType) {
|
|
705
|
+
engine.unifyWithDiagnostic(narrowed, expectedResultType, node);
|
|
706
|
+
}
|
|
707
|
+
return engine.subst.apply(narrowed);
|
|
708
|
+
}
|
|
709
|
+
return engine.subst.apply(resultType);
|
|
710
|
+
}
|
|
711
|
+
if (numArgs > params.length) {
|
|
712
|
+
engine.addDiag(`Curried function called with ${numArgs} arguments, but only accepts up to ${params.length}`, (0, annotations_1.narrowToIdentifier)(node));
|
|
713
|
+
return (0, types_1.tVar)();
|
|
714
|
+
}
|
|
715
|
+
// Multi-arg call: consume numArgs parameters from the curried chain
|
|
716
|
+
const consumedParams = params.slice(0, numArgs);
|
|
717
|
+
// A placeholder argument (`subtract (__, 10)`) leaves its parameter open:
|
|
718
|
+
// the result takes the skipped parameters first, then the rest.
|
|
719
|
+
const skippedParams = [];
|
|
720
|
+
// Infer each argument and unify with the corresponding consumed param
|
|
721
|
+
for (let i = 0; i < numArgs; i++) {
|
|
722
|
+
if (isPlaceholderArgument(engine, node.arguments[i], env)) {
|
|
723
|
+
skippedParams.push(consumedParams[i]);
|
|
724
|
+
continue;
|
|
725
|
+
}
|
|
726
|
+
const argType = engine.inferExpressionWithExpected(node.arguments[i], env, consumedParams[i]);
|
|
727
|
+
const effectiveArgType = shouldProtectPolymorphicParamArg(engine, node.arguments[i], env, consumedParams[i])
|
|
728
|
+
? (0, typeUtils_1.freshenType)(engine.subst.apply(argType))
|
|
729
|
+
: argType;
|
|
730
|
+
engine.unifyArgWithDiagnostic(effectiveArgType, consumedParams[i], node.arguments[i]);
|
|
731
|
+
}
|
|
732
|
+
const remainingParams = [...skippedParams, ...params.slice(numArgs)];
|
|
733
|
+
let restType = result;
|
|
734
|
+
for (let i = remainingParams.length - 1; i >= 0; i--) {
|
|
735
|
+
restType = (0, types_1.tFun)(remainingParams[i], restType);
|
|
736
|
+
}
|
|
737
|
+
if (expectedResultType) {
|
|
738
|
+
engine.unifyWithDiagnostic(restType, expectedResultType, node);
|
|
739
|
+
}
|
|
740
|
+
// If there are remaining params, result is still curried
|
|
741
|
+
if (remainingParams.length > 0) {
|
|
742
|
+
engine.pendingIsCurriedResult = true;
|
|
743
|
+
}
|
|
744
|
+
return engine.subst.apply(restType);
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Route a container-wrapped spread chain called with an array literal.
|
|
748
|
+
*
|
|
749
|
+
* `f (a -> b, b -> c, ..., y -> z)` behaves exactly like the bare spread-array
|
|
750
|
+
* form `[a -> b, ..., y -> z]`. Detected up front (before the curried-call
|
|
751
|
+
* path swallows the single array argument as a whole): the kind-variable `f` is
|
|
752
|
+
* bound to the literal's concrete container (`Array`) so the Foldable `::rep`
|
|
753
|
+
* discharge still resolves, then the literal's elements are routed through the
|
|
754
|
+
* shared chain-collapsing machinery (`inferSpreadArrayCall`).
|
|
755
|
+
*
|
|
756
|
+
* Only a *linear* pipe/compose chain (every non-spread element a single-arg
|
|
757
|
+
* arrow) is routed here; a fan-out combinator whose HEAD is a multi-arity
|
|
758
|
+
* combiner is left for `matchFanOutCallee`. Returns the inferred result type
|
|
759
|
+
* when it routes, otherwise `null`.
|
|
760
|
+
*/
|
|
761
|
+
function tryContainerSpreadChainCall(engine, node, env, calleeType, expectedResultType) {
|
|
762
|
+
const rc = engine.subst.apply(calleeType);
|
|
763
|
+
if (rc.tag === 'TFun' &&
|
|
764
|
+
rc.param.tag === 'TCon' &&
|
|
765
|
+
node.arguments.length === 1 &&
|
|
766
|
+
node.arguments[0].type === 'ArrayExpression') {
|
|
767
|
+
const kvInfo = (0, types_1.parseKvName)(rc.param.name);
|
|
768
|
+
if (kvInfo && rc.param.args.length === 1) {
|
|
769
|
+
const inner = engine.subst.apply(rc.param.args[0]);
|
|
770
|
+
if (inner.tag === 'TCon' &&
|
|
771
|
+
(inner.name === types_1.T_TUPLE || inner.name === types_1.T_SPREAD_ARRAY) &&
|
|
772
|
+
inner.args.some(a => (0, types_1.isSpread)(a))) {
|
|
773
|
+
// Distinguish a *linear* pipe/compose chain
|
|
774
|
+
// `(a -> b, b -> c, ..., y -> z)` — every element a single-arg
|
|
775
|
+
// arrow — from a fan-out combinator `(x1 -> x2 -> ... -> z,
|
|
776
|
+
// a -> x1, ...)` whose HEAD is a multi-arity combiner. Only the
|
|
777
|
+
// former is routed through the spread-array chain machinery; the
|
|
778
|
+
// latter is left for `matchFanOutCallee` further down.
|
|
779
|
+
const nonSpread = inner.args.filter(a => !(0, types_1.isSpread)(a));
|
|
780
|
+
const allSingleArgArrows = nonSpread.length > 0 &&
|
|
781
|
+
nonSpread.every(a => a.tag === 'TFun' && (0, types_1.flattenCurriedType)(a).params.length === 1);
|
|
782
|
+
if (allSingleArgArrows) {
|
|
783
|
+
// Bind the container kind-variable to Array (0 args absorbed) —
|
|
784
|
+
// mirrors `unifyKvConcrete`'s binding for `f a` vs `Array x`.
|
|
785
|
+
engine.subst.set(kvInfo.varId, (0, types_1.tCon)('Array', []));
|
|
786
|
+
const effCallee = {
|
|
787
|
+
tag: 'TFun',
|
|
788
|
+
param: (0, types_1.tCon)(types_1.T_SPREAD_ARRAY, inner.args),
|
|
789
|
+
result: rc.result,
|
|
790
|
+
};
|
|
791
|
+
return inferSpreadArrayCall(engine, node, env, effCallee, node.arguments[0], expectedResultType);
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* applyArgs-style peeling CAPTURE. When a combinator's parameter is a spread
|
|
800
|
+
* arrow chain `(a -> b -> ... -> z)` and applying the concrete curried function
|
|
801
|
+
* argument yields a spread-array applier `[a, b, ...] -> z`, record the
|
|
802
|
+
* function's flattened arity + terminal keyed by THIS call node. The follow-up
|
|
803
|
+
* array application (`partial(array)`) looks it up via `node.callee` to peel the
|
|
804
|
+
* result (partial application) and flag over-application. Cheap to compute;
|
|
805
|
+
* only fires for the SpreadArray form.
|
|
806
|
+
*/
|
|
807
|
+
function captureSpreadChainPeel(engine, node, numArgs, actualArgTypes, resolvedCallee, result) {
|
|
808
|
+
if (numArgs === 1 &&
|
|
809
|
+
actualArgTypes.length === 1 &&
|
|
810
|
+
resolvedCallee.tag === 'TFun' &&
|
|
811
|
+
(0, typeUtils_1.hasSpreadInArrowChain)(resolvedCallee.param) &&
|
|
812
|
+
result.tag === 'TFun' &&
|
|
813
|
+
result.param.tag === 'TCon' &&
|
|
814
|
+
result.param.name === types_1.T_SPREAD_ARRAY &&
|
|
815
|
+
result.param.args.some(a => (0, types_1.isSpread)(a))) {
|
|
816
|
+
// A spread arrow chain `a -> b -> ... -> z` consumes ONE argument per
|
|
817
|
+
// curried level (the applier feeds one array element per level). A
|
|
818
|
+
// function level that takes multiple parameters (e.g. `(x, dummy) => …`)
|
|
819
|
+
// still consumes exactly one array element — the surplus parameters read
|
|
820
|
+
// as `undefined` at runtime (that is the whole point of the `dummy`
|
|
821
|
+
// slot). Rather than rejecting such a level we SOFT-COLLAPSE it to its
|
|
822
|
+
// first parameter, so `(x, dummy) -> b` behaves like `x -> b`. This is
|
|
823
|
+
// reflected in the recorded peel params so partial-application remainders
|
|
824
|
+
// display as clean single-parameter chains.
|
|
825
|
+
const fnFlat = (0, types_1.flattenCurriedType)(engine.subst.apply(actualArgTypes[0]));
|
|
826
|
+
engine.spreadChainPeel.set(node, {
|
|
827
|
+
params: fnFlat.params.map(p => (0, typeUtils_1.collapseMultiParamLevel)(engine.subst.apply(p))),
|
|
828
|
+
result: fnFlat.result,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* PARTIAL APPLICATION of a top-level spread-chain CALLEE. When a value typed
|
|
834
|
+
* as a variadic arrow chain `p1 -> p2 -> ... -> z` is CALLED with fewer
|
|
835
|
+
* arguments than its fixed leading params, the honest result is the PEELED
|
|
836
|
+
* REMAINDER chain (`p2 -> ... -> z`), not the terminal `z` that the lenient
|
|
837
|
+
* `unifySpreadArrowChain` bottom-out rule would produce. This is the rule
|
|
838
|
+
* that types the heterogeneous fold accumulator in FP-library `combine`:
|
|
839
|
+
*
|
|
840
|
+
* // reducer :: Applicative m => (m x -> m y -> ... -> m z) -> (a -> m x) -> m y -> ... -> m z
|
|
841
|
+
* const reducer = acc => fn => acc (fn (value));
|
|
842
|
+
*
|
|
843
|
+
* `acc (…)` peels ONE argument off the chain per step. Context-aware by
|
|
844
|
+
* design: only a CALL site knows the chain is being applied — at the raw
|
|
845
|
+
* unification level a short concrete function is indistinguishable from a
|
|
846
|
+
* short function ARGUMENT, which must keep bottoming out on the terminal so
|
|
847
|
+
* over-application diagnostics stay intact.
|
|
848
|
+
*
|
|
849
|
+
* Returns the remainder type, or `null` when the pattern does not apply
|
|
850
|
+
* (no top-level spread, zero args, or enough args to reach the spread —
|
|
851
|
+
* the generic path handles those).
|
|
852
|
+
*/
|
|
853
|
+
function trySpreadChainCalleePeel(engine, node, env, resolvedCallee, expectedResultType) {
|
|
854
|
+
if (resolvedCallee.tag !== 'TFun' || !(0, typeUtils_1.hasTopLevelSpreadChain)(resolvedCallee))
|
|
855
|
+
return null;
|
|
856
|
+
const numArgs = node.arguments.length;
|
|
857
|
+
if (numArgs === 0)
|
|
858
|
+
return null;
|
|
859
|
+
if (node.arguments.some(a => a.type === 'SpreadElement'))
|
|
860
|
+
return null;
|
|
861
|
+
const flat = (0, types_1.flattenCurriedType)(resolvedCallee);
|
|
862
|
+
const spreadIdx = flat.params.findIndex(types_1.isSpread);
|
|
863
|
+
// Only intercept when the args stop BEFORE the spread — once the spread
|
|
864
|
+
// starts absorbing, the chain length is unknown and the generic terminal
|
|
865
|
+
// reconciliation is the right (existing) semantics.
|
|
866
|
+
if (spreadIdx < 0 || numArgs >= spreadIdx)
|
|
867
|
+
return null;
|
|
868
|
+
for (let i = 0; i < numArgs; i++) {
|
|
869
|
+
const slot = engine.subst.apply(flat.params[i]);
|
|
870
|
+
const argType = engine.inferExpressionWithExpected(node.arguments[i], env, slot);
|
|
871
|
+
engine.unifyArgWithDiagnostic(argType, flat.params[i], node.arguments[i]);
|
|
872
|
+
}
|
|
873
|
+
const remainder = flat.params
|
|
874
|
+
.slice(numArgs)
|
|
875
|
+
.reduceRight((acc, p) => (0, types_1.tFun)(p, acc), flat.result);
|
|
876
|
+
if (expectedResultType) {
|
|
877
|
+
engine.unifyWithDiagnostic(remainder, expectedResultType, node);
|
|
878
|
+
}
|
|
879
|
+
return engine.subst.apply(remainder);
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* JS allows ignoring extra callback arguments (e.g. `[1,2].map(String)` works
|
|
883
|
+
* even though `map` passes `(element, index, array)`). When the expected slot
|
|
884
|
+
* is a multi-arg Tuple function and the actual arg is a function with fewer
|
|
885
|
+
* params, pad the arg type to match the expected shape instead of erroring.
|
|
886
|
+
*/
|
|
887
|
+
function adaptCallbackArity(engine, argType, argSlot, argNode) {
|
|
888
|
+
const resolvedArg = engine.subst.apply(argType);
|
|
889
|
+
const resolvedExpected = engine.subst.apply(argSlot);
|
|
890
|
+
if (resolvedArg.tag === 'TFun' && resolvedExpected.tag === 'TFun') {
|
|
891
|
+
const paramList = (p) => (0, types_1.isTuple)(p) ? p.args.slice() : [p];
|
|
892
|
+
const actualParams = paramList(resolvedArg.param);
|
|
893
|
+
const expectedParams = paramList(resolvedExpected.param);
|
|
894
|
+
// Arity-tolerant unification for an aligned parameter pair — see
|
|
895
|
+
// `unifyCallbackParamTolerant` (module level).
|
|
896
|
+
const unifyParamPairTolerant = (actual, expected) => unifyCallbackParamTolerant(engine, actual, expected, argNode);
|
|
897
|
+
// (0) Expected callback is VARIADIC — a tuple containing a `...` spread
|
|
898
|
+
// (e.g. a node-style wrapper `fromNode :: ((..., (e, a) -> ()) -> *)
|
|
899
|
+
// -> ...`). The spread absorbs any number of the actual function's
|
|
900
|
+
// params; align the fixed params before the spread from the START
|
|
901
|
+
// and the fixed params after the spread from the END, each with the
|
|
902
|
+
// arity-tolerant pair unification above.
|
|
903
|
+
const spreadIdx = expectedParams.findIndex(p => (0, types_1.isSpread)(p));
|
|
904
|
+
if (spreadIdx >= 0 && (0, types_1.isTuple)(resolvedExpected.param)
|
|
905
|
+
&& !actualParams.some(p => (0, types_1.isSpread)(p))) {
|
|
906
|
+
const before = expectedParams.slice(0, spreadIdx);
|
|
907
|
+
const after = expectedParams.slice(spreadIdx + 1).filter(p => !(0, types_1.isSpread)(p));
|
|
908
|
+
if (actualParams.length >= before.length
|
|
909
|
+
&& actualParams.length >= after.length) {
|
|
910
|
+
for (let j = 0; j < before.length; j++) {
|
|
911
|
+
unifyParamPairTolerant(actualParams[j], before[j]);
|
|
912
|
+
}
|
|
913
|
+
for (let j = 0; j < after.length; j++) {
|
|
914
|
+
unifyParamPairTolerant(actualParams[actualParams.length - after.length + j], after[j]);
|
|
915
|
+
}
|
|
916
|
+
engine.unifyWithDiagnostic(resolvedArg.result, resolvedExpected.result, argNode);
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
// (1) Actual takes FEWER params than the expected multi-arg callback:
|
|
921
|
+
// JS ignores the extra arguments the caller passes (e.g.
|
|
922
|
+
// `[1, 2].map(String)` works even though `map` passes
|
|
923
|
+
// `(element, index, array)`). Pad the arg to the expected shape so
|
|
924
|
+
// the slot variable still binds to a full function type.
|
|
925
|
+
if (actualParams.length < expectedParams.length && (0, types_1.isTuple)(resolvedExpected.param)) {
|
|
926
|
+
for (let j = 0; j < actualParams.length; j++) {
|
|
927
|
+
engine.unifyWithDiagnostic(actualParams[j], expectedParams[j], argNode);
|
|
928
|
+
}
|
|
929
|
+
const paddedType = (0, types_1.tFun)((0, types_1.tTuple)(expectedParams), resolvedArg.result);
|
|
930
|
+
engine.unifyWithDiagnostic(paddedType, argSlot, argNode);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
// (2) Actual takes MORE params than the expected callback: the surplus
|
|
934
|
+
// trailing params are simply never supplied at call time, so accept
|
|
935
|
+
// the function by aligning only the leading params and the results
|
|
936
|
+
// (e.g. a binary `(a, b) => a` passed where a unary `a -> b` is
|
|
937
|
+
// expected, or a native `Array#reduce` 4-param callback used where a
|
|
938
|
+
// 2-param `reduce` is required).
|
|
939
|
+
if (actualParams.length > expectedParams.length) {
|
|
940
|
+
for (let j = 0; j < expectedParams.length; j++) {
|
|
941
|
+
engine.unifyWithDiagnostic(actualParams[j], expectedParams[j], argNode);
|
|
942
|
+
}
|
|
943
|
+
engine.unifyWithDiagnostic(resolvedArg.result, resolvedExpected.result, argNode);
|
|
944
|
+
return;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
// (3) An as-yet-unknown function (`[…].map (done)` with `done` an
|
|
948
|
+
// unannotated parameter) passed to a builtin callback whose trailing
|
|
949
|
+
// params are optional (`(x, i, xs)`): bind it to the short form
|
|
950
|
+
// `x -> b`, as almost every such callback is written. Binding the
|
|
951
|
+
// full tuple would pin `done` to three parameters.
|
|
952
|
+
if (resolvedArg.tag === 'TVar' && resolvedExpected.tag === 'TFun'
|
|
953
|
+
&& (0, types_1.isTuple)(resolvedExpected.param)
|
|
954
|
+
&& resolvedExpected.param.tag === 'TCon'
|
|
955
|
+
&& resolvedExpected.param.optionalFrom !== undefined) {
|
|
956
|
+
const n = resolvedExpected.param.optionalFrom;
|
|
957
|
+
const kept = resolvedExpected.param.args.slice(0, n);
|
|
958
|
+
const shortParam = kept.length === 1 ? kept[0] : (0, types_1.tTuple)(kept);
|
|
959
|
+
engine.unifyWithDiagnostic(argType, (0, types_1.tFun)(shortParam, resolvedExpected.result), argNode);
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
// Pass the raw argSlot (TVar) so auto-union can trace the chain
|
|
963
|
+
engine.unifyArgWithDiagnostic(argType, argSlot, argNode);
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Classify a resolved callee + call node into the functional CALL PATTERN it
|
|
967
|
+
* matches, returning the FIRST match in the subsystem's dispatch priority order
|
|
968
|
+
* (or `Regular` when none applies). This is the single, testable CATALOGUE of
|
|
969
|
+
* the *structural* dispatch decisions that `inferCallExpression` (calls.ts)
|
|
970
|
+
* makes once the callee type has been resolved.
|
|
971
|
+
*
|
|
972
|
+
* NOTE: the engine's hot path keeps its own inline structural checks (kept for
|
|
973
|
+
* clarity and to avoid a second pass over the callee); this classifier MIRRORS
|
|
974
|
+
* that order rather than driving it, and serves as the documented,
|
|
975
|
+
* independently-tested reference for the dispatch shapes. The state-dependent
|
|
976
|
+
* early paths (curry-wrapper-by-name, spread-arg wildcard, curried-result) are
|
|
977
|
+
* handled by the engine's flow before this classification would apply.
|
|
978
|
+
*
|
|
979
|
+
* Priority (load-bearing — mirrors the historical inline order):
|
|
980
|
+
* 1. VariadicWrapper — `((a, .., z) -> r) -> (a, .., z) -> r`, 1 arg.
|
|
981
|
+
* 2. ContainerSpreadChain — `f (a->b, .., y->z)` linear pipe/compose chain
|
|
982
|
+
* (every element a single-arg arrow), 1 array arg.
|
|
983
|
+
* 3. FanOut — `f (combiner, a->x1, ..)` multi-arity head, 1 array arg.
|
|
984
|
+
* 4. VariadicItems — `(a, .., z) -> r` spread tuple applied positionally.
|
|
985
|
+
* 5. SpreadArrayApplier — `[a, .., z] -> r` applied to 1 array literal.
|
|
986
|
+
* 6. Regular — the default application path.
|
|
987
|
+
*/
|
|
988
|
+
function classifyCallPattern(resolvedCallee, node) {
|
|
989
|
+
if (resolvedCallee.tag !== 'TFun')
|
|
990
|
+
return CallPatternKind.Regular;
|
|
991
|
+
const param = resolvedCallee.param;
|
|
992
|
+
const numArgs = node.arguments.length;
|
|
993
|
+
const singleArrayArg = numArgs === 1 && node.arguments[0].type === 'ArrayExpression';
|
|
994
|
+
// 1. Spread-function wrapper: `((a, b, ...) -> z) -> (a, b, ...) -> z`.
|
|
995
|
+
if (param.tag === 'TFun' &&
|
|
996
|
+
(0, typeUtils_1.isSpreadTupleOrArray)(param.param) &&
|
|
997
|
+
!(0, typeUtils_1.hasSpreadInArrowChain)(resolvedCallee.result) &&
|
|
998
|
+
numArgs === 1) {
|
|
999
|
+
return CallPatternKind.VariadicWrapper;
|
|
1000
|
+
}
|
|
1001
|
+
if (param.tag === 'TCon') {
|
|
1002
|
+
// A container kind-variable applied to one heterogeneous tuple/spread.
|
|
1003
|
+
const kvInfo = (0, types_1.parseKvName)(param.name);
|
|
1004
|
+
if (kvInfo && param.args.length === 1 && singleArrayArg) {
|
|
1005
|
+
const inner = param.args[0];
|
|
1006
|
+
if (inner.tag === 'TCon' &&
|
|
1007
|
+
(inner.name === types_1.T_TUPLE || inner.name === types_1.T_SPREAD_ARRAY) &&
|
|
1008
|
+
inner.args.some(a => (0, types_1.isSpread)(a))) {
|
|
1009
|
+
// 2. Linear pipe/compose chain — every non-spread element a single-arg
|
|
1010
|
+
// arrow — takes priority over the multi-arity fan-out combiner.
|
|
1011
|
+
const nonSpread = inner.args.filter(a => !(0, types_1.isSpread)(a));
|
|
1012
|
+
const allSingleArgArrows = nonSpread.length > 0 &&
|
|
1013
|
+
nonSpread.every(a => a.tag === 'TFun' && (0, types_1.flattenCurriedType)(a).params.length === 1);
|
|
1014
|
+
if (allSingleArgArrows)
|
|
1015
|
+
return CallPatternKind.ContainerSpreadChain;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
// 3. Fan-out combinator (converge/combine): multi-arity combiner head.
|
|
1019
|
+
if (singleArrayArg && matchFanOutCallee(resolvedCallee)) {
|
|
1020
|
+
return CallPatternKind.FanOut;
|
|
1021
|
+
}
|
|
1022
|
+
// 4. Variadic spread tuple applied positionally.
|
|
1023
|
+
if (param.name === types_1.T_TUPLE && param.args.some(a => (0, types_1.isSpread)(a))) {
|
|
1024
|
+
return CallPatternKind.VariadicItems;
|
|
1025
|
+
}
|
|
1026
|
+
// 5. Spread-array applier applied to a single array literal.
|
|
1027
|
+
if (param.name === types_1.T_SPREAD_ARRAY &&
|
|
1028
|
+
param.args.some(a => (0, types_1.isSpread)(a)) &&
|
|
1029
|
+
singleArrayArg) {
|
|
1030
|
+
return CallPatternKind.SpreadArrayApplier;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return CallPatternKind.Regular;
|
|
1034
|
+
}
|
|
1035
|
+
//# sourceMappingURL=functionPatterns.js.map
|