@eagleoutice/flowr 2.17.7 → 2.17.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/abstract-interpretation/branch-paths.d.ts +10 -0
  2. package/abstract-interpretation/branch-paths.js +104 -10
  3. package/config.js +3 -1
  4. package/control-flow/control-flow-graph.js +18 -2
  5. package/data/r-base-guards.generated.js +2 -2
  6. package/dataflow/environments/ambient-effect.js +4 -1
  7. package/dataflow/environments/built-in-props.d.ts +8 -2
  8. package/dataflow/environments/built-in-props.js +16 -5
  9. package/dataflow/environments/default-builtin-config/base-functions.js +1 -1
  10. package/dataflow/environments/default-builtin-config/guards.d.ts +1 -1
  11. package/dataflow/environments/default-builtin-config/guards.js +28 -0
  12. package/dataflow/environments/default-builtin-config/index.d.ts +8 -4
  13. package/dataflow/environments/default-builtin-config/index.js +9 -5
  14. package/dataflow/environments/default-builtin-config/parallel-execution.d.ts +1 -1
  15. package/dataflow/environments/default-builtin-config/parallel-execution.js +25 -0
  16. package/dataflow/environments/guard-facts.d.ts +28 -1
  17. package/dataflow/environments/pkg-name.d.ts +2 -0
  18. package/dataflow/environments/query-fn-props.d.ts +19 -15
  19. package/dataflow/environments/query-fn-props.js +46 -36
  20. package/dataflow/environments/removal-marker.d.ts +1 -1
  21. package/dataflow/environments/removal-marker.js +18 -3
  22. package/dataflow/environments/reserved-names.d.ts +6 -0
  23. package/dataflow/environments/reserved-names.js +14 -0
  24. package/dataflow/environments/resolve-by-name.js +5 -10
  25. package/dataflow/eval/resolve/alias-tracking.js +23 -6
  26. package/dataflow/extractor.js +103 -28
  27. package/dataflow/fn/argument-roles.d.ts +3 -0
  28. package/dataflow/fn/argument-roles.js +5 -5
  29. package/dataflow/fn/exceptions-of-function.d.ts +7 -0
  30. package/dataflow/fn/exceptions-of-function.js +15 -2
  31. package/dataflow/fn/function-props.d.ts +1 -1
  32. package/dataflow/fn/function-props.js +40 -18
  33. package/dataflow/fn/function-semantics.d.ts +3 -1
  34. package/dataflow/fn/function-semantics.js +2 -0
  35. package/dataflow/fn/guards/built-in-guards.js +2 -1
  36. package/dataflow/fn/guards/condition-grouping.d.ts +11 -0
  37. package/dataflow/fn/guards/condition-grouping.js +50 -3
  38. package/dataflow/fn/guards/function-guards.d.ts +10 -0
  39. package/dataflow/fn/guards/function-guards.js +130 -14
  40. package/dataflow/fn/guards/guard-claims.d.ts +2 -0
  41. package/dataflow/fn/guards/guard-claims.js +42 -2
  42. package/dataflow/fn/guards/guard-knowledge.d.ts +7 -1
  43. package/dataflow/fn/guards/guard-knowledge.js +50 -4
  44. package/dataflow/fn/guards/guard-term.js +10 -1
  45. package/dataflow/fn/higher-order-function.d.ts +15 -1
  46. package/dataflow/fn/higher-order-function.js +54 -10
  47. package/dataflow/graph/call-graph.d.ts +5 -0
  48. package/dataflow/graph/call-graph.js +51 -15
  49. package/dataflow/graph/df-helper.d.ts +2 -0
  50. package/dataflow/graph/df-helper.js +4 -2
  51. package/dataflow/graph/graph.d.ts +21 -0
  52. package/dataflow/graph/graph.js +63 -2
  53. package/dataflow/internal/linker.d.ts +20 -0
  54. package/dataflow/internal/linker.js +190 -24
  55. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +20 -12
  56. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +5 -3
  57. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +2 -1
  58. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +2 -0
  59. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +5 -0
  60. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +7 -3
  61. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +12 -5
  62. package/dataflow/internal/process/functions/call/deferred.d.ts +7 -2
  63. package/dataflow/internal/process/functions/call/deferred.js +63 -28
  64. package/dataflow/internal/process/functions/call/quoted.js +33 -7
  65. package/documentation/data/dfg/doc-ambient.js +3 -1
  66. package/documentation/wiki-normalized-ast.js +1 -1
  67. package/documentation/wiki-query.js +9 -3
  68. package/package.json +5 -3
  69. package/project/context/flowr-analyzer-context.d.ts +7 -4
  70. package/project/context/flowr-analyzer-context.js +8 -3
  71. package/project/context/flowr-analyzer-files-context.js +1 -0
  72. package/project/context/flowr-analyzer-functions-context.d.ts +2 -1
  73. package/project/context/flowr-analyzer-functions-context.js +9 -8
  74. package/project/flowr-analyzer.d.ts +7 -4
  75. package/project/flowr-analyzer.js +37 -8
  76. package/project/plugins/file-plugins/files/flowr-namespace-file.d.ts +22 -0
  77. package/project/plugins/file-plugins/files/flowr-namespace-file.js +34 -0
  78. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +4 -1
  79. package/project/sigdb/build.js +41 -2
  80. package/project/sigdb/decode.d.ts +3 -1
  81. package/project/sigdb/decode.js +45 -1
  82. package/project/sigdb/decompress.d.ts +5 -0
  83. package/project/sigdb/decompress.js +17 -1
  84. package/project/sigdb/manifest.d.ts +2 -1
  85. package/project/sigdb/manifest.js +12 -5
  86. package/project/sigdb/native-routines.d.ts +82 -0
  87. package/project/sigdb/native-routines.js +178 -0
  88. package/project/sigdb/reader.d.ts +21 -3
  89. package/project/sigdb/reader.js +35 -3
  90. package/project/sigdb/schema.d.ts +107 -3
  91. package/project/sigdb/schema.js +8 -2
  92. package/project/sigdb/sigdb-download.js +1 -1
  93. package/project/sigdb/sigdb-version.d.ts +7 -1
  94. package/project/sigdb/sigdb-version.js +12 -9
  95. package/project/sigdb/spec-query.d.ts +9 -4
  96. package/project/sigdb/spec-query.js +71 -45
  97. package/queries/catalog/call-context-query/call-context-query-executor.js +76 -29
  98. package/queries/catalog/inspect-exceptions-query/inspect-exception-query-format.js +1 -12
  99. package/queries/catalog/inspect-fn-guards-query/inspect-fn-guards-query-format.d.ts +3 -0
  100. package/queries/catalog/inspect-fn-guards-query/inspect-fn-guards-query-format.js +3 -2
  101. package/queries/catalog/inspect-fn-props-query/inspect-fn-props-query-format.d.ts +3 -0
  102. package/queries/catalog/inspect-fn-props-query/inspect-fn-props-query-format.js +3 -2
  103. package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-format.js +3 -1
  104. package/queries/catalog/inspect-recursion-query/inspect-recursion-query-format.js +4 -2
  105. package/queries/catalog/signature-query/signature-query-executor.js +6 -2
  106. package/queries/catalog/signature-query/signature-query-format.d.ts +4 -0
  107. package/util/collections/bounded-map.d.ts +9 -0
  108. package/util/collections/bounded-map.js +22 -0
  109. package/util/collections/defaultmap.d.ts +5 -0
  110. package/util/collections/defaultmap.js +10 -0
  111. package/util/fixpoint.d.ts +6 -1
  112. package/util/fixpoint.js +45 -3
  113. package/util/flowr-version.d.ts +1 -1
  114. package/util/flowr-version.js +2 -2
  115. package/util/global-caches.d.ts +26 -0
  116. package/util/global-caches.js +43 -0
  117. package/util/monoid.js +2 -2
  118. package/util/quads.js +11 -3
  119. package/util/r-package-urls.d.ts +19 -0
  120. package/util/r-package-urls.js +41 -0
@@ -10,6 +10,7 @@ exports.returnedClaimOf = returnedClaimOf;
10
10
  exports.returnsOf = returnsOf;
11
11
  exports.returnedFormalOf = returnedFormalOf;
12
12
  exports.readingOf = readingOf;
13
+ exports.concreteClaim = concreteClaim;
13
14
  exports.claimConditions = claimConditions;
14
15
  exports.claimWords = claimWords;
15
16
  exports.caseConditions = caseConditions;
@@ -369,14 +370,44 @@ function relationsOf(conditions, facts) {
369
370
  }];
370
371
  });
371
372
  }
373
+ /** A condition split into what it says of `formal` alone and the rest, `undefined` if some alternative mixes both. */
374
+ function splitAbout(condition, formal) {
375
+ const alternatives = guard_term_1.GuardTerm.disjuncts(condition);
376
+ const about = alternatives.filter(a => (0, set_1.setEquals)(guard_term_1.GuardTerm.formalsOf(a), new Set([formal])));
377
+ const others = alternatives.filter(a => !guard_term_1.GuardTerm.formalsOf(a).has(formal));
378
+ return about.length + others.length === alternatives.length ? { about, others } : undefined;
379
+ }
380
+ /**
381
+ * The conditions with what two of them say of a formal together: `a || p` and `a || q` where `p` and `q` cannot both fail say `a`
382
+ * outright (`p && !p` is never true), which neither says alone. The two stay, the claim they give together joins them.
383
+ */
384
+ function withResolvents(conditions, formals) {
385
+ const out = [...conditions];
386
+ for (const formal of formals) {
387
+ const split = conditions.map(c => splitAbout(c, formal)).filter(s => s !== undefined && s.about.length > 0 && s.others.length > 0);
388
+ for (let i = 0; i < split.length; i++) {
389
+ for (let j = i + 1; j < split.length; j++) {
390
+ const [a, b] = [split[i], split[j]];
391
+ const same = guard_term_1.GuardTerm.render(guard_term_1.GuardTerm.any(...a.about)) === guard_term_1.GuardTerm.render(guard_term_1.GuardTerm.any(...b.about));
392
+ /* neither way through can hold when the other one does not */
393
+ if (same && guard_term_1.GuardTerm.isFalse(guard_term_1.GuardTerm.any(guard_term_1.GuardTerm.all(guard_term_1.GuardTerm.any(...a.others), guard_term_1.GuardTerm.any(...b.others))))) {
394
+ out.push(guard_term_1.GuardTerm.any(...a.about));
395
+ }
396
+ }
397
+ }
398
+ }
399
+ return out;
400
+ }
372
401
  /** The claims the given conditions make on a formal unless something not about that formal holds, see {@link ConditionalClaim}. */
373
402
  function conditionalClaimsOf(conditions, formals, facts) {
403
+ /* what holds outright needs no way through */
404
+ const outright = new Set(withResolvents(conditions, formals).slice(conditions.length).map(t => guard_term_1.GuardTerm.render(t)));
374
405
  return conditions.flatMap(condition => {
375
406
  const alternatives = guard_term_1.GuardTerm.disjuncts(condition);
376
407
  return formals.flatMap(formal => {
377
408
  const about = alternatives.filter(a => (0, set_1.setEquals)(guard_term_1.GuardTerm.formalsOf(a), new Set([formal])));
378
409
  const others = alternatives.filter(a => !guard_term_1.GuardTerm.formalsOf(a).has(formal));
379
- if (about.length === 0 || others.length === 0 || about.length + others.length !== alternatives.length) {
410
+ if (about.length === 0 || others.length === 0 || about.length + others.length !== alternatives.length || outright.has(guard_term_1.GuardTerm.render(guard_term_1.GuardTerm.any(...about)))) {
380
411
  return [];
381
412
  }
382
413
  const claim = toClaim(about.map(a => abstractOf(a, formal, facts)).reduce(either));
@@ -449,8 +480,9 @@ function abstractOfAll(conditions, formal, facts) {
449
480
  /** What the given conditions, all of which hold on a normal return, claim about each of the given formals; a formal without any claim is left out. */
450
481
  function claimsOf(conditions, formals, facts) {
451
482
  const claims = {};
483
+ const all = withResolvents(conditions, formals);
452
484
  for (const formal of formals) {
453
- const claim = toClaim(abstractOfAll(conditions, formal, facts));
485
+ const claim = toClaim(abstractOfAll(all, formal, facts));
454
486
  if (Object.keys(claim).length > 0) {
455
487
  claims[formal] = claim;
456
488
  }
@@ -608,6 +640,14 @@ function typesInWords(types) {
608
640
  function setOf(words) {
609
641
  return words.length === 1 ? String(words[0]) : `{${words.join(', ')}}`;
610
642
  }
643
+ /** the types that are no data: what a comparison or arithmetic rejects (unless a class answers it), and what nearly every call asks of what it is handed */
644
+ const NoData = ['function', 'environment', 'symbol', 'language', 'expression'];
645
+ /** The claim without a type that only rules out what is no data (`typeof(x) not in {function, environment, ...}`), which says nothing concrete of the formal. */
646
+ function concreteClaim(claim) {
647
+ const onlyData = (types) => types !== undefined && (0, guard_knowledge_1.typeNames)(guard_facts_1.AnyType).filter(t => !types.includes(t)).every(t => NoData.includes(t)) && types.length < (0, guard_knowledge_1.typeNames)(guard_facts_1.AnyType).length;
648
+ const { types, unclassedTypes, ...rest } = claim;
649
+ return { ...rest, ...types === undefined || onlyData(types) ? {} : { types }, ...unclassedTypes === undefined || onlyData(unclassedTypes) ? {} : { unclassedTypes } };
650
+ }
611
651
  /**
612
652
  * Reads a claim on `formal` as conditions written the way R would test them, all of which hold, like
613
653
  * `typeof(m) in {integer, double}`, `length(m) == 1`, and `m >= 0`.
@@ -1,6 +1,6 @@
1
1
  import { type BrandedIdentifier } from '../../environments/identifier';
2
2
  import type { BuiltInDefinitions } from '../../environments/built-in-config';
3
- import { type GuardFacts, type GuardFactsLookup, type RFailure, type RValueTypes, type RVersions } from '../../environments/guard-facts';
3
+ import { type GuardFacts, type GuardFactsLookup, type RFailure, type RRecycling, type RValueTypes, type RVersions } from '../../environments/guard-facts';
4
4
  import { GuardTerm, type GuardCall } from './guard-term';
5
5
  import type { ReadOnlyFlowrAnalyzerContext } from '../../../project/context/flowr-analyzer-context';
6
6
  /** The names of the given types, see {@link TypeNames}. */
@@ -27,6 +27,12 @@ export declare function composesName(this: void, term: GuardTerm): boolean;
27
27
  export declare function callable(this: void, of: GuardTerm, resolves: (name: BrandedIdentifier) => boolean, byName?: boolean): GuardTerm;
28
28
  /** The term of the value bound to a formal of the same call, `undefined` if flowR cannot state it. */
29
29
  export type FormalTerms = (formal: BrandedIdentifier) => GuardTerm | undefined;
30
+ /**
31
+ * What it takes for the values a call recycles by `recycling` not to make R fail, one condition per pair or per value, see {@link RRecycling}.
32
+ * `values` are all the values the call hands to {@link RRecycling.among} for `divides`, which has to know the largest size, and any of them for the others;
33
+ * `to` is what the call hands to {@link RRecycling.to}, if anything.
34
+ */
35
+ export declare function recyclingConditions(this: void, recycling: RRecycling, values: readonly GuardTerm[], to: GuardTerm | undefined, resolves: (name: BrandedIdentifier) => boolean): GuardTerm[];
30
36
  /** Whether the failure is about what can be applied, which also speaks of what is no formal, like a name spelled out at run time. */
31
37
  export declare function asksCallable(this: void, { requires }: RFailure): boolean;
32
38
  /** Whether the function applies what one of its formals holds. */
@@ -9,6 +9,7 @@ exports.guardFactsOf = guardFactsOf;
9
9
  exports.onlyComputesBy = onlyComputesBy;
10
10
  exports.composesName = composesName;
11
11
  exports.callable = callable;
12
+ exports.recyclingConditions = recyclingConditions;
12
13
  exports.asksCallable = asksCallable;
13
14
  exports.appliesFormal = appliesFormal;
14
15
  exports.passes = passes;
@@ -21,6 +22,7 @@ const guard_facts_1 = require("../../environments/guard-facts");
21
22
  const r_version_order_1 = require("../../../util/r-version-order");
22
23
  const r_base_guards_generated_1 = require("../../../data/r-base-guards.generated");
23
24
  const guard_term_1 = require("./guard-term");
25
+ const global_caches_1 = require("../../../util/global-caches");
24
26
  const { Null, Logical, Integer, Double, Complex, Character, Raw, List, Pairlist, Function: Fn, Environment, Symbol, Language, Expression, Other } = guard_facts_1.RValueType;
25
27
  /** The names of the types, in the order of their bits, as `typeof` spells them. */
26
28
  const TypeNames = [
@@ -40,7 +42,8 @@ function holdsIn({ since, until }, version) {
40
42
  function factsIn({ changed, ...facts }, version) {
41
43
  const merged = (changed ?? []).filter(c => holdsIn(c, version)).reduce((acc, c) => ({ ...acc, ...c.facts }), facts);
42
44
  const asks = merged.asks === undefined ? undefined : Object.fromEntries(Object.entries(merged.asks).map(([formal, failures]) => [formal, failures.filter(f => holdsIn(f, version))]));
43
- return asks === undefined ? merged : { ...merged, asks };
45
+ const recycles = merged.recycles?.filter(r => holdsIn(r, version));
46
+ return { ...merged, ...(asks === undefined ? {} : { asks }), ...(recycles === undefined ? {} : { recycles }) };
44
47
  }
45
48
  /** The classes S3 dispatch sees for a value without a class attribute, as `.class2` names them, beyond `default`. */
46
49
  exports.ImplicitClasses = ['matrix', 'array', 'integer', 'double', 'numeric', 'complex', 'character', 'logical', 'raw', 'list', 'function', 'NULL', 'environment', 'name', 'call', '(', '{', 'if', 'for', 'while', '<-', '=', 'expression', 'pairlist', 'externalptr', 'weakref', 'bytecode'];
@@ -56,7 +59,8 @@ function guardFactsIn(definitions, version = r_base_guards_generated_1.RBaseGuar
56
59
  }
57
60
  return name => known.get(name);
58
61
  }
59
- const defaultFacts = new Map();
62
+ /** per R version asked for */
63
+ const defaultFacts = global_caches_1.GlobalCaches.map('default-guard-facts', global_caches_1.CacheScope.Process, 32);
60
64
  /** What the default built-in definitions state of guards, by default in the R version the guards of the base packages were read off. */
61
65
  function defaultGuardFacts(version = r_base_guards_generated_1.RBaseGuardsVersion) {
62
66
  let facts = defaultFacts.get(version);
@@ -102,9 +106,51 @@ function callable(of, resolves, byName = true) {
102
106
  return byName ? guard_term_1.GuardTerm.any(is('is.function', of), guard_term_1.GuardTerm.all(is('is.character', of), exists(of)), is('is.symbol', of), own)
103
107
  : guard_term_1.GuardTerm.any(is('is.function', of), own);
104
108
  }
109
+ /** `reader(of)`, for a reader written `pkg::name` a call of the function of that package. */
110
+ function readerOf(reader, of) {
111
+ const at = reader.indexOf('::');
112
+ return at < 0 ? is(reader, of) : { kind: 'call', fn: reader.slice(at + 2), pkg: reader.slice(0, at), known: true, args: [{ value: of }] };
113
+ }
105
114
  /** `of` with each of the readers applied, the outermost first. */
106
115
  function readOff(readers, of) {
107
- return (readers ?? []).reduceRight((inner, reader) => is(reader, inner), of);
116
+ return (readers ?? []).reduceRight((inner, reader) => readerOf(reader, inner), of);
117
+ }
118
+ const number = (value) => ({ kind: 'constant', lexeme: String(value), value });
119
+ /**
120
+ * What it takes for the values a call recycles by `recycling` not to make R fail, one condition per pair or per value, see {@link RRecycling}.
121
+ * `values` are all the values the call hands to {@link RRecycling.among} for `divides`, which has to know the largest size, and any of them for the others;
122
+ * `to` is what the call hands to {@link RRecycling.to}, if anything.
123
+ */
124
+ function recyclingConditions(recycling, values, to, resolves) {
125
+ const { rule, skips } = recycling;
126
+ const size = (of) => readerOf(recycling.size, of);
127
+ const skipped = (of) => skips === undefined ? guard_term_1.GuardTerm.False : conditionOn(skips, of, resolves, () => undefined);
128
+ const eq = (a, b) => guard_term_1.GuardTerm.known('==', a, b);
129
+ const one = (of) => eq(size(of), number(1));
130
+ if (rule === 'single') {
131
+ return values.map(v => guard_term_1.GuardTerm.any(skipped(v), one(v)));
132
+ }
133
+ else if (rule === 'one' && to !== undefined) {
134
+ /* R takes the first element of the size it is told, and recycles to no size for `NULL` */
135
+ const [target, unsized] = to.kind === 'constant' ? [to, guard_term_1.GuardTerm.False] : [guard_term_1.GuardTerm.known('[', to, number(1)), guard_term_1.GuardTerm.known('is.null', to)];
136
+ return values.map(v => guard_term_1.GuardTerm.any(skipped(v), unsized, eq(size(v), target), one(v)));
137
+ }
138
+ else if (rule === 'one') {
139
+ return values.flatMap((a, at) => values.slice(at + 1).map(b => guard_term_1.GuardTerm.any(skipped(a), skipped(b), eq(size(a), size(b)), one(a), one(b))));
140
+ }
141
+ else if (values.length < 2) {
142
+ return [];
143
+ }
144
+ /* `divides`: a value of a size flowR cannot read leaves the largest size open */
145
+ const open = values.map(skipped);
146
+ const positive = (of) => guard_term_1.GuardTerm.known('>', size(of), number(0));
147
+ const divides = (a, b) => eq(guard_term_1.GuardTerm.known('%%', b, a), number(0));
148
+ if (values.length === 2) {
149
+ const [a, b] = values;
150
+ return [guard_term_1.GuardTerm.any(...open, eq(size(a), size(b)), guard_term_1.GuardTerm.all(positive(a), positive(b), guard_term_1.GuardTerm.any(divides(size(a), size(b)), divides(size(b), size(a)))))];
151
+ }
152
+ const largest = guard_term_1.GuardTerm.known('max', ...values.map(size));
153
+ return values.map(v => guard_term_1.GuardTerm.any(...open, eq(size(v), largest), guard_term_1.GuardTerm.all(positive(v), divides(size(v), largest))));
108
154
  }
109
155
  /**
110
156
  * The condition in terms of the value `of`, with `resolves` telling whether a function of the name is around, see {@link callable}, and `formals` the values of the other formals.
@@ -113,7 +159,7 @@ function readOff(readers, of) {
113
159
  function conditionOn(condition, of, resolves, formals, positive = true) {
114
160
  const unknown = positive ? guard_term_1.GuardTerm.True : guard_term_1.GuardTerm.False;
115
161
  if ('is' in condition) {
116
- return is(condition.is, condition.of === undefined ? of : is(condition.of, of));
162
+ return readerOf(condition.is, condition.of === undefined ? of : readerOf(condition.of, of));
117
163
  }
118
164
  else if ('not' in condition) {
119
165
  const inner = conditionOn(condition.not, of, resolves, formals, !positive);
@@ -78,7 +78,8 @@ function resolved(items, termOf, merge) {
78
78
  }
79
79
  /** Any of the terms. */
80
80
  function any(...terms) {
81
- return connect('||', True, False, terms);
81
+ /* an alternative that cannot hold as written is none, so `a || (p && !p)` is `a` */
82
+ return connect('||', True, False, terms.some(t => isCall(t, '&&')) ? flatten('||', terms).filter(t => !unsatisfiable(t)) : terms);
82
83
  }
83
84
  /** What a comparison holds as when it does not hold (only ever asked of what R has already seen to be non-`NA`). */
84
85
  const NegatedComparison = {
@@ -477,6 +478,14 @@ function substituteCondition(term, binding, outer, facts) {
477
478
  const substituted = substitute(term, binding, outer) ?? True;
478
479
  return facts === undefined ? substituted : decided(substituted, facts);
479
480
  }
481
+ /** Whether the conjunction cannot hold as written: one of its parts is a disjunction of what its other parts rule out, `p && q && (!p || !q)`. */
482
+ function unsatisfiable(term) {
483
+ const parts = flatten('&&', [term]);
484
+ return parts.some(part => {
485
+ const alternatives = flatten('||', [part]);
486
+ return alternatives.length > 1 && alternatives.every(a => parts.some(other => other !== part && complementary(a, other)));
487
+ });
488
+ }
480
489
  /**
481
490
  * Terms state what a guard asks for, see {@link GuardTerm}.
482
491
  * @helper dataflow
@@ -8,6 +8,18 @@ import type { DataflowGraph } from '../graph/graph';
8
8
  import type { ReadOnlyFlowrAnalyzerContext } from '../../project/context/flowr-analyzer-context';
9
9
  /** The definitions the given node can hand over, to tell an argument being the definition under inspection from one holding another. */
10
10
  export declare function definitionsBehind(id: NodeId, graph: DataflowGraph): ReadonlySet<NodeId>;
11
+ /**
12
+ * {@link definitionsBehind} for many nodes of one graph that does not change in between: every node's answer
13
+ * is worked out once, a strongly connected component at a time, so arguments that lead into the same chain of
14
+ * reads share the walk instead of repeating it. An answer holds at most `cap` definitions, which is enough to
15
+ * tell none, one and several apart.
16
+ */
17
+ export declare class DefinitionsBehind {
18
+ private readonly summaries;
19
+ constructor(graph: DataflowGraph, cap?: number);
20
+ /** the definitions `id` can hand over, at most `cap` of them */
21
+ of(id: NodeId): ReadonlySet<NodeId>;
22
+ }
11
23
  /** What an argument is, which no definition it is looked at for changes. */
12
24
  export interface ArgumentFacts {
13
25
  readonly behind: ReadonlySet<NodeId>;
@@ -22,10 +34,12 @@ export interface HigherOrderFunctionsOptions {
22
34
  readonly invertedGraph?: DataflowGraph;
23
35
  /** what is known about the arguments, to share between the definitions asked about in one go; a call site reaching several of them resolves its arguments once */
24
36
  readonly argumentFacts?: Map<NodeId, ArgumentFacts>;
37
+ /** the definitions behind each argument, worked out once for the whole graph (see {@link DefinitionsBehind}) */
38
+ readonly definitionsBehind?: DefinitionsBehind;
25
39
  }
26
40
  /**
27
41
  * Whether the function is higher-order: it takes a function argument, may return one, or calls one of its own
28
42
  * formals as a function. `function(x) x` alone is not higher-order.
29
43
  * @useInstead {@link FunctionSemantics.isHigherOrder}
30
44
  */
31
- export declare function isHigherOrder(this: void, id: NodeId, graph: DataflowGraph, { ctx, invertedGraph, argumentFacts }: HigherOrderFunctionsOptions): boolean;
45
+ export declare function isHigherOrder(this: void, id: NodeId, graph: DataflowGraph, { ctx, invertedGraph, argumentFacts, definitionsBehind: behind }: HigherOrderFunctionsOptions): boolean;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefinitionsBehind = void 0;
3
4
  exports.definitionsBehind = definitionsBehind;
4
5
  exports.isHigherOrder = isHigherOrder;
5
6
  /**
@@ -17,6 +18,7 @@ const config_1 = require("../../config");
17
18
  const r_function_call_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
18
19
  const resolve_helper_1 = require("../environments/resolve-helper");
19
20
  const argument_roles_1 = require("./argument-roles");
21
+ const fixpoint_1 = require("../../util/fixpoint");
20
22
  const built_in_props_1 = require("../environments/built-in-props");
21
23
  function isAnyReturnAFunction(def, graph) {
22
24
  const workingQueue = def.exitPoints.map(d => graph.getVertex(d.nodeId)).filter(assert_1.isNotUndefined);
@@ -69,16 +71,58 @@ function definitionsBehind(id, graph) {
69
71
  continue;
70
72
  }
71
73
  /* what a call reads beyond its arguments (state it depends on, writes it follows) is no value it hands back */
72
- const isCall = vertex_1.DfgVertex.isFunctionCall(vertex);
73
- for (const [t, e] of graph.edgesFrom(current)) {
74
- if (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Returns | edge_1.EdgeType.DefinedBy) || (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Reads) && (!isCall || edge_1.DfEdge.includesType(e, edge_1.EdgeType.Argument)))) {
75
- workingQueue.push(t);
76
- }
77
- }
74
+ workingQueue.push(...behindSuccessors(current, graph));
78
75
  }
79
76
  return found;
80
77
  }
81
- function inspectCallSitesArgumentsFns(def, graph, ctx, invertedGraph, facts) {
78
+ /** The edges {@link definitionsBehind} follows out of `current`, a vertex that is no function definition. */
79
+ function behindSuccessors(current, graph) {
80
+ const isCall = vertex_1.DfgVertex.isFunctionCall(graph.getVertex(current));
81
+ const out = [];
82
+ for (const [t, e] of graph.edgesFrom(current)) {
83
+ if (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Returns | edge_1.EdgeType.DefinedBy) || (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Reads) && (!isCall || edge_1.DfEdge.includesType(e, edge_1.EdgeType.Argument)))) {
84
+ out.push(t);
85
+ }
86
+ }
87
+ return out;
88
+ }
89
+ /**
90
+ * {@link definitionsBehind} for many nodes of one graph that does not change in between: every node's answer
91
+ * is worked out once, a strongly connected component at a time, so arguments that lead into the same chain of
92
+ * reads share the walk instead of repeating it. An answer holds at most `cap` definitions, which is enough to
93
+ * tell none, one and several apart.
94
+ */
95
+ class DefinitionsBehind {
96
+ summaries;
97
+ constructor(graph, cap = 2) {
98
+ const none = new Set();
99
+ const leaf = (id) => node_id_1.NodeId.isBuiltIn(id) || vertex_1.DfgVertex.isFunctionDefinition(graph.getVertex(id));
100
+ this.summaries = new fixpoint_1.ReachSummaries(id => leaf(id) ? [] : behindSuccessors(id, graph), id => !node_id_1.NodeId.isBuiltIn(id) && vertex_1.DfgVertex.isFunctionDefinition(graph.getVertex(id)) ? new Set([id]) : undefined, { empty: none, concat: (a, b) => cappedUnion(a, b, cap) });
101
+ }
102
+ /** the definitions `id` can hand over, at most `cap` of them */
103
+ of(id) {
104
+ return this.summaries.of(id);
105
+ }
106
+ }
107
+ exports.DefinitionsBehind = DefinitionsBehind;
108
+ /** The union of both sets, but no more than `cap` elements of it; one of them where it already is the answer. */
109
+ function cappedUnion(a, b, cap) {
110
+ if (a.size >= cap || b.size === 0 || b.isSubsetOf(a)) {
111
+ return a;
112
+ }
113
+ else if (b.size >= cap || a.size === 0 || a.isSubsetOf(b)) {
114
+ return b;
115
+ }
116
+ const union = new Set(a);
117
+ for (const x of b) {
118
+ if (union.size >= cap) {
119
+ break;
120
+ }
121
+ union.add(x);
122
+ }
123
+ return union;
124
+ }
125
+ function inspectCallSitesArgumentsFns(def, graph, ctx, invertedGraph, facts, behind) {
82
126
  const callSites = invertedGraph?.outgoingEdges(def.id) ?? graph.ingoingEdges(def.id);
83
127
  for (const [callerId, e] of callSites ?? []) {
84
128
  if (!edge_1.DfEdge.includesType(e, edge_1.EdgeType.Calls)) {
@@ -95,7 +139,7 @@ function inspectCallSitesArgumentsFns(def, graph, ctx, invertedGraph, facts) {
95
139
  /* an apply-family call carries the callback among its arguments, which says nothing about the callback itself */
96
140
  let known = facts?.get(arg.nodeId);
97
141
  if (known === undefined) {
98
- known = { behind: definitionsBehind(arg.nodeId, graph) };
142
+ known = { behind: behind?.of(arg.nodeId) ?? definitionsBehind(arg.nodeId, graph) };
99
143
  facts?.set(arg.nodeId, known);
100
144
  }
101
145
  if (known.behind.size === 1 && known.behind.has(def.id)) {
@@ -122,11 +166,11 @@ function callsAFormal(id, graph, ctx) {
122
166
  * formals as a function. `function(x) x` alone is not higher-order.
123
167
  * @useInstead {@link FunctionSemantics.isHigherOrder}
124
168
  */
125
- function isHigherOrder(id, graph, { ctx, invertedGraph, argumentFacts }) {
169
+ function isHigherOrder(id, graph, { ctx, invertedGraph, argumentFacts, definitionsBehind: behind }) {
126
170
  const vert = graph.getVertex(id);
127
171
  if (!vertex_1.DfgVertex.isFunctionDefinition(vert)) {
128
172
  return false;
129
173
  }
130
- return isAnyReturnAFunction(vert, graph) || callsAFormal(id, graph, ctx) || inspectCallSitesArgumentsFns(vert, graph, ctx, invertedGraph, argumentFacts);
174
+ return isAnyReturnAFunction(vert, graph) || callsAFormal(id, graph, ctx) || inspectCallSitesArgumentsFns(vert, graph, ctx, invertedGraph, argumentFacts, behind);
131
175
  }
132
176
  //# sourceMappingURL=higher-order-function.js.map
@@ -21,6 +21,11 @@ export declare const CallGraph: {
21
21
  readonly name: "CallGraph";
22
22
  /** Extracts the sub call graph from the given call graph, starting from the given entry points. */
23
23
  readonly computeSubCallGraph: (this: void, graph: CallGraph, entryPoints: Set<NodeId>) => CallGraph;
24
+ /**
25
+ * The definitions among `ids` that are recursive (see {@link CallGraph.isRecursive}), for all of them in one walk: a definition is
26
+ * recursive exactly if it lies on a cycle, so its strongly connected component has another member or it calls itself.
27
+ */
28
+ readonly recursiveAmong: (this: void, ids: Iterable<NodeId>, graph: CallGraph) => Set<NodeId>;
24
29
  /** Whether the function definition with the given id (transitively) calls itself; `false` for ids that are no function definition. */
25
30
  readonly isRecursive: (this: void, id: NodeId, graph: CallGraph) => boolean;
26
31
  /** The calls the program makes on its own: those outside of any function definition. */
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CallGraph = void 0;
4
4
  exports.fallbackUntargetedCall = fallbackUntargetedCall;
5
+ const fixpoint_1 = require("../../util/fixpoint");
5
6
  const graph_1 = require("./graph");
6
7
  const vertex_1 = require("./vertex");
7
8
  const node_id_1 = require("../../r-bridge/lang-4.x/ast/model/processing/node-id");
@@ -16,11 +17,24 @@ const identifier_1 = require("../environments/identifier");
16
17
  const r_argument_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
17
18
  const df_helper_1 = require("./df-helper");
18
19
  const linker_1 = require("../internal/linker");
19
- function processCds(vtx, graph, result, state) {
20
+ /** Runs a walk of {@link Visit}s depth first, in the order the recursive walk would take: each visit completes before its caller goes on. */
21
+ function walk(first, graph, result, state) {
22
+ const pending = [first];
23
+ while (pending.length > 0) {
24
+ const step = (pending[pending.length - 1]).next();
25
+ if (step.done) {
26
+ pending.pop();
27
+ continue;
28
+ }
29
+ const [kind, vtx, from] = step.value;
30
+ pending.push(kind === 'unknown' ? processUnknown(vtx, from, graph, result, state) : processFunctionDefinition(vtx, from, graph, result, state));
31
+ }
32
+ }
33
+ function* processCds(vtx, graph) {
20
34
  for (const tar of vtx.cds ?? []) {
21
35
  const targetVtx = graph.getVertex(tar.id);
22
36
  if (targetVtx) {
23
- processUnknown(targetVtx, undefined, graph, result, state);
37
+ yield ['unknown', targetVtx, undefined];
24
38
  }
25
39
  }
26
40
  }
@@ -59,7 +73,7 @@ function fallbackUntargetedCall(vtx, graph) {
59
73
  }
60
74
  return collected;
61
75
  }
62
- function processCall(vtx, from, graph, result, state) {
76
+ function* processCall(vtx, from, graph, result, state) {
63
77
  const vid = vtx.id;
64
78
  if (from) {
65
79
  result.addEdge(from, vid, edge_1.EdgeType.Calls);
@@ -69,7 +83,7 @@ function processCall(vtx, from, graph, result, state) {
69
83
  }
70
84
  state.visited.add(vid);
71
85
  result.addVertex(vtx, undefined, true);
72
- processCds(vtx, graph, result, state);
86
+ yield* processCds(vtx, graph);
73
87
  let addedTarget = false;
74
88
  let addedBiTarget = false;
75
89
  for (const tar of df_helper_1.Dataflow.callTargetsOf(vid, graph, vtx.environment, state.resolver)) {
@@ -81,7 +95,7 @@ function processCall(vtx, from, graph, result, state) {
81
95
  }
82
96
  else if (vertex_1.DfgVertex.isFunctionDefinition(targetVtx)) {
83
97
  addedTarget = true;
84
- processFunctionDefinition(targetVtx, vid, graph, result, state);
98
+ yield ['definition', targetVtx, vid];
85
99
  }
86
100
  }
87
101
  if (!addedBiTarget && vtx.origin !== 'unnamed') {
@@ -119,19 +133,19 @@ function processCall(vtx, from, graph, result, state) {
119
133
  }
120
134
  const tVtx = graph.getVertex(tar);
121
135
  if (tVtx) {
122
- processUnknown(tVtx, vid, graph, result, state);
136
+ yield ['unknown', tVtx, vid];
123
137
  }
124
138
  }
125
139
  }
126
- function processUnknown(vtx, from, graph, result, state) {
140
+ function* processUnknown(vtx, from, graph, result, state) {
127
141
  if (vertex_1.DfgVertex.isFunctionCall(vtx)) {
128
- processCall(vtx, from, graph, result, state);
142
+ yield* processCall(vtx, from, graph, result, state);
129
143
  }
130
144
  else if (vertex_1.DfgVertex.isFunctionDefinition(vtx) && from) {
131
145
  result.addEdge(from, node_id_1.NodeId.toBuiltIn('function'), edge_1.EdgeType.Calls);
132
146
  }
133
147
  }
134
- function processFunctionDefinition(vtx, from, graph, result, state) {
148
+ function* processFunctionDefinition(vtx, from, graph, result, state) {
135
149
  if (from) {
136
150
  result.addEdge(from, vtx.id, edge_1.EdgeType.Calls);
137
151
  }
@@ -140,13 +154,13 @@ function processFunctionDefinition(vtx, from, graph, result, state) {
140
154
  }
141
155
  state.visited.add(vtx.id);
142
156
  result.addVertex(vtx, undefined, true);
143
- processCds(vtx, graph, result, state);
157
+ yield* processCds(vtx, graph);
144
158
  const exits = new Set(vtx.exitPoints);
145
159
  state.potentials.push([vtx.id, vtx.subflow.graph.difference(exits)]);
146
160
  for (const { nodeId } of exits) {
147
161
  const v = graph.getVertex(nodeId);
148
162
  if (v) {
149
- processUnknown(v, vtx.id, graph, result, state);
163
+ yield ['unknown', v, vtx.id];
150
164
  }
151
165
  }
152
166
  }
@@ -238,6 +252,28 @@ exports.CallGraph = {
238
252
  }
239
253
  return result;
240
254
  },
255
+ /**
256
+ * The definitions among `ids` that are recursive (see {@link CallGraph.isRecursive}), for all of them in one walk: a definition is
257
+ * recursive exactly if it lies on a cycle, so its strongly connected component has another member or it calls itself.
258
+ */
259
+ recursiveAmong(ids, graph) {
260
+ const recursive = new Set();
261
+ const components = new fixpoint_1.StronglyConnected(id => graph.edgesFrom(id).keys(), (members, successorsOf) => {
262
+ for (const m of members) {
263
+ if (members.length > 1 || successorsOf(m).includes(m)) {
264
+ recursive.add(m);
265
+ }
266
+ }
267
+ });
268
+ const asked = new Set();
269
+ for (const id of ids) {
270
+ if (vertex_1.DfgVertex.isFunctionDefinition(graph.getVertex(id))) {
271
+ asked.add(id);
272
+ components.visit(id);
273
+ }
274
+ }
275
+ return recursive.intersection(asked);
276
+ },
241
277
  /** Whether the function definition with the given id (transitively) calls itself; `false` for ids that are no function definition. */
242
278
  isRecursive(id, graph) {
243
279
  if (!vertex_1.DfgVertex.isFunctionDefinition(graph.getVertex(id))) {
@@ -308,10 +344,10 @@ exports.CallGraph = {
308
344
  const state = { visited: new Set(), potentials: [], resolver: new linker_1.LinkedFunctionResolver(graph) };
309
345
  for (const [, vert] of graph.vertices(false)) {
310
346
  if (vertex_1.DfgVertex.isFunctionCall(vert)) {
311
- processCall(vert, undefined, graph, result, state);
347
+ walk(processCall(vert, undefined, graph, result, state), graph, result, state);
312
348
  }
313
349
  else if (vertex_1.DfgVertex.isFunctionDefinition(vert)) {
314
- processFunctionDefinition(vert, undefined, graph, result, state);
350
+ walk(processFunctionDefinition(vert, undefined, graph, result, state), graph, result, state);
315
351
  }
316
352
  }
317
353
  for (const [from, tos] of state.potentials) {
@@ -319,9 +355,9 @@ exports.CallGraph = {
319
355
  if (!result.hasVertex(to)) {
320
356
  const v = graph.getVertex(to);
321
357
  if (v) {
322
- processUnknown(v, from, graph, result, state);
358
+ walk(processUnknown(v, from, graph, result, state), graph, result, state);
323
359
  if (vertex_1.DfgVertex.isFunctionDefinition(v)) {
324
- processFunctionDefinition(v, callerOfBodyDefinition(from, to), graph, result, state);
360
+ walk(processFunctionDefinition(v, callerOfBodyDefinition(from, to), graph, result, state), graph, result, state);
325
361
  }
326
362
  }
327
363
  }
@@ -13,6 +13,8 @@ import { type AttachSpec } from '../internal/process/functions/call/built-in/bui
13
13
  import { type Environment, type REnvironmentInformation } from '../environments/environment';
14
14
  import { RFunctionDefinition } from '../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition';
15
15
  import type { ParentInformation } from '../../r-bridge/lang-4.x/ast/model/processing/decorate';
16
+ /** The edges a provenance follows by default, see {@link Dataflow.provenance}. */
17
+ export declare const ProvenanceEdges: number;
16
18
  /**
17
19
  * This is the root helper object to work with the {@link DataflowGraph}.
18
20
  * @helper dataflow This is the root helper object to work with the DataflowGraph.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Dataflow = void 0;
3
+ exports.Dataflow = exports.ProvenanceEdges = void 0;
4
4
  const graph_1 = require("./graph");
5
5
  const ambient_access_1 = require("./ambient-access");
6
6
  const edge_1 = require("./edge");
@@ -40,6 +40,8 @@ function copySelectedMarks(graph, df, select) {
40
40
  }
41
41
  return df;
42
42
  }
43
+ /** The edges a provenance follows by default, see {@link Dataflow.provenance}. */
44
+ exports.ProvenanceEdges = edge_1.EdgeType.Calls | edge_1.EdgeType.Reads | edge_1.EdgeType.Returns | edge_1.EdgeType.Argument | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.DefinedByOnCall;
43
45
  /**
44
46
  * This is the root helper object to work with the {@link DataflowGraph}.
45
47
  * @helper dataflow This is the root helper object to work with the DataflowGraph.
@@ -292,7 +294,7 @@ exports.Dataflow = {
292
294
  return false;
293
295
  },
294
296
  /** Given the id of a vertex (usually a variable use), this returns a reachable provenance set by calculating a non-interprocedural and non-context sensitive backward slice, but stopping at the given ids! */
295
- provenance(id, graph, consider, followEdges = edge_1.EdgeType.Calls | edge_1.EdgeType.Reads | edge_1.EdgeType.Returns | edge_1.EdgeType.Argument | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.DefinedByOnCall) {
297
+ provenance(id, graph, consider, followEdges = exports.ProvenanceEdges) {
296
298
  return exports.Dataflow.reachable(graph, id, { follow: followEdges, consider, cds: true });
297
299
  },
298
300
  /** A simple visitor akin to {@link RNode.visitAst} to traverse the dataflow graph starting from the start id and only respecting edge direction. */
@@ -112,6 +112,7 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
112
112
  /** All edges in the complete graph (including those nested in function definition) */
113
113
  private edgeInformation;
114
114
  private incomingIndex?;
115
+ private beyondSideEffects;
115
116
  private types;
116
117
  /** The id list of `tag`, created on first use, so a caller adding many ids looks it up once. */
117
118
  private typeList;
@@ -119,6 +120,9 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
119
120
  /** Every write above keeps an id listed under one tag at most once, so a lookup never hands the same one out twice. */
120
121
  private unindexType;
121
122
  private qualifiedNames?;
123
+ private derived?;
124
+ /** What `create` derives from the graph, made once and kept under `key` until the graph changes. */
125
+ derivedUntilChanged<T>(key: symbol, create: () => T): T;
122
126
  toJSON(): DataflowGraphJson;
123
127
  /** A view of this graph without the environments of its vertices and subflows, sharing everything else. */
124
128
  withoutEnvironments(): DataflowGraph<Vertex, Edge>;
@@ -135,6 +139,11 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
135
139
  outgoingEdges(id: NodeId): OutgoingEdges | undefined;
136
140
  /** The edges leaving `id`, i.e. what it depends on, as a map from target to edge. */
137
141
  edgesFrom(id: NodeId): ReadonlyOutgoingEdges;
142
+ /**
143
+ * The edges leaving `id` but those that only state a {@link EdgeType.SideEffectOnCall}: a write escaping into thousands of calls
144
+ * has an edge to each, which a walk along the flow of values steps over every time it passes the write.
145
+ */
146
+ edgesBeyondSideEffects(id: NodeId): Iterable<[NodeId, Edge]>;
138
147
  /** The edges arriving at `id`, i.e. what depends on it, as a map from source to edge. */
139
148
  edgesTo(id: NodeId): ReadonlyIngoingEdges;
140
149
  /**
@@ -246,3 +255,15 @@ export interface IEnvironmentJson {
246
255
  t?: EnvType;
247
256
  globalEnv?: true;
248
257
  }
258
+ /**
259
+ * Edges a pass collects while it still asks questions of the graph, added in one go at its end: every change to the graph drops
260
+ * what was resolved over it (the qualified call names, the shared target walks), so adding them as found would resolve the same
261
+ * calls again after each one. The same edge found twice is kept once.
262
+ */
263
+ export declare class PendingEdges {
264
+ private readonly edges;
265
+ /** Notes the edge `from -> to` of the given type. */
266
+ add(from: NodeId, to: NodeId, type: EdgeType | number): void;
267
+ /** Adds every noted edge to `graph`. */
268
+ applyTo(graph: DataflowGraph): void;
269
+ }