@eagleoutice/flowr 2.17.12 → 2.17.13

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 (29) hide show
  1. package/config.d.ts +1 -0
  2. package/config.js +4 -2
  3. package/dataflow/environments/default-builtin-config/base-functions.d.ts +1 -1
  4. package/dataflow/environments/default-builtin-config/base-functions.js +2 -0
  5. package/dataflow/environments/default-builtin-config/guards.js +3 -1
  6. package/dataflow/environments/default-builtin-config/index.d.ts +1 -1
  7. package/dataflow/extractor.js +58 -0
  8. package/dataflow/fn/guards/built-in-guards.js +4 -4
  9. package/dataflow/fn/guards/call-contracts.js +8 -8
  10. package/dataflow/fn/guards/condition-grouping.js +4 -3
  11. package/dataflow/fn/guards/function-guards.js +103 -97
  12. package/dataflow/fn/guards/group-method-guards.js +10 -8
  13. package/dataflow/fn/guards/guard-claims.js +152 -128
  14. package/dataflow/fn/guards/guard-knowledge.js +19 -18
  15. package/dataflow/fn/guards/guard-term-syntax.js +13 -12
  16. package/dataflow/fn/guards/guard-term.d.ts +71 -0
  17. package/dataflow/fn/guards/guard-term.js +183 -75
  18. package/dataflow/graph/graph.d.ts +9 -0
  19. package/dataflow/graph/graph.js +19 -0
  20. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.d.ts +6 -2
  21. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.js +9 -2
  22. package/dataflow/internal/process/functions/call/built-in/built-in-eval.js +1 -1
  23. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +1 -1
  24. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +19 -9
  25. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +35 -3
  26. package/documentation/wiki-interface.js +1 -0
  27. package/package.json +1 -1
  28. package/util/flowr-version.d.ts +1 -1
  29. package/util/flowr-version.js +2 -2
@@ -1,13 +1,80 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GuardTerm = exports.RConstantNames = exports.AtomPrecedence = exports.NegationPrecedence = exports.Precedence = void 0;
3
+ exports.GuardTerm = exports.RConstantNames = exports.AtomPrecedence = exports.NegationPrecedence = exports.Precedence = exports.GuardFn = void 0;
4
4
  const identifier_1 = require("../../environments/identifier");
5
5
  const r_base_packages_1 = require("../../../util/r-base-packages");
6
6
  const guard_facts_1 = require("../../environments/guard-facts");
7
+ /** The operators and functions of R the guards speak of by name, with flowR's own `succeeds` and `...names` (see {@link GuardTerm}). */
8
+ var GuardFn;
9
+ (function (GuardFn) {
10
+ GuardFn["And"] = "&&";
11
+ GuardFn["Or"] = "||";
12
+ GuardFn["Not"] = "!";
13
+ GuardFn["ElementAnd"] = "&";
14
+ GuardFn["ElementOr"] = "|";
15
+ GuardFn["Equal"] = "==";
16
+ GuardFn["NotEqual"] = "!=";
17
+ GuardFn["Less"] = "<";
18
+ GuardFn["LessEqual"] = "<=";
19
+ GuardFn["Greater"] = ">";
20
+ GuardFn["GreaterEqual"] = ">=";
21
+ GuardFn["Plus"] = "+";
22
+ GuardFn["Minus"] = "-";
23
+ GuardFn["Times"] = "*";
24
+ GuardFn["Divide"] = "/";
25
+ GuardFn["Power"] = "^";
26
+ GuardFn["Modulo"] = "%%";
27
+ GuardFn["IntegerDivide"] = "%/%";
28
+ GuardFn["In"] = "%in%";
29
+ GuardFn["MatrixProduct"] = "%*%";
30
+ GuardFn["Colon"] = ":";
31
+ GuardFn["Dollar"] = "$";
32
+ GuardFn["At"] = "@";
33
+ GuardFn["Index"] = "[";
34
+ GuardFn["Element"] = "[[";
35
+ GuardFn["Succeeds"] = "succeeds";
36
+ GuardFn["DotsNames"] = "...names";
37
+ GuardFn["Missing"] = "missing";
38
+ GuardFn["All"] = "all";
39
+ GuardFn["Any"] = "any";
40
+ GuardFn["Combine"] = "c";
41
+ GuardFn["Length"] = "length";
42
+ GuardFn["IsTrue"] = "isTRUE";
43
+ GuardFn["IsFalse"] = "isFALSE";
44
+ GuardFn["IsNull"] = "is.null";
45
+ GuardFn["IsNa"] = "is.na";
46
+ GuardFn["IsObject"] = "is.object";
47
+ GuardFn["IsCharacter"] = "is.character";
48
+ GuardFn["IsFunction"] = "is.function";
49
+ GuardFn["AsLogical"] = "as.logical";
50
+ GuardFn["Unclass"] = "unclass";
51
+ GuardFn["OldClass"] = "oldClass";
52
+ GuardFn["Class"] = "class";
53
+ GuardFn["Inherits"] = "inherits";
54
+ GuardFn["Identical"] = "identical";
55
+ GuardFn["Exists"] = "exists";
56
+ GuardFn["Get"] = "get";
57
+ GuardFn["MatchArg"] = "match.arg";
58
+ GuardFn["Paste"] = "paste";
59
+ GuardFn["Paste0"] = "paste0";
60
+ GuardFn["Sprintf"] = "sprintf";
61
+ GuardFn["If"] = "if";
62
+ GuardFn["For"] = "for";
63
+ GuardFn["While"] = "while";
64
+ GuardFn["Switch"] = "switch";
65
+ GuardFn["Return"] = "return";
66
+ GuardFn["OnExit"] = "on.exit";
67
+ GuardFn["UseMethod"] = "UseMethod";
68
+ GuardFn["NextMethod"] = "NextMethod";
69
+ })(GuardFn || (exports.GuardFn = GuardFn = {}));
7
70
  const True = { kind: 'constant', lexeme: 'TRUE', value: true };
8
71
  const False = { kind: 'constant', lexeme: 'FALSE', value: false };
9
72
  /** A call of what R ships with, its arguments unnamed. */
10
73
  function known(fn, ...args) {
74
+ return knownOf(fn, args);
75
+ }
76
+ /** {@link known} with the arguments as one list. */
77
+ function knownOf(fn, args) {
11
78
  return { kind: 'call', fn, known: true, args: args.map(value => ({ value })) };
12
79
  }
13
80
  function isCall(term, fn) {
@@ -27,8 +94,7 @@ function connect(fn, absorbing, neutral, terms) {
27
94
  seen.set(render(term), term);
28
95
  }
29
96
  }
30
- const [first, ...rest] = seen.values();
31
- return first === undefined ? neutral : rest.reduce((acc, term) => known(fn, acc, term), first);
97
+ return seen.size === 0 ? neutral : seen.values().reduce((acc, term) => known(fn, acc, term));
32
98
  }
33
99
  /** The name of the function without the namespace it was written with. */
34
100
  function bareName(fn) {
@@ -37,12 +103,16 @@ function bareName(fn) {
37
103
  }
38
104
  /** All of the terms; dropping unknowable ones turns into {@link True}, which leaves what is left necessary. */
39
105
  function all(...terms) {
40
- const joined = connect('&&', False, True, terms);
41
- return isCall(joined, '&&') && joined.known ? connect('&&', False, True, resolved(flatten('&&', [joined]), t => t, (_a, _b, t) => t)) : joined;
106
+ return allOf(terms);
107
+ }
108
+ /** {@link all} with the terms as one list. */
109
+ function allOf(terms) {
110
+ const joined = connect(GuardFn.And, False, True, terms);
111
+ return isCall(joined, GuardFn.And) && joined.known ? connect(GuardFn.And, False, True, resolved(flatten(GuardFn.And, [joined]), t => t, (_a, _b, t) => t)) : joined;
42
112
  }
43
113
  /** Whether `a` holds exactly when `b` does not, as far as what they are written as tells. */
44
114
  function complementary(a, b) {
45
- const negates = (x, y) => isCall(x, '!') && x.known === true && x.args.length === 1 && render(x.args[0].value) === render(y);
115
+ const negates = (x, y) => isCall(x, GuardFn.Not) && x.known === true && x.args.length === 1 && render(x.args[0].value) === render(y);
46
116
  const flips = isCall(a) && isCall(b) && a.known === true && b.known === true && NegatedComparison[a.fn] === b.fn && a.args.length === 2 && b.args.length === 2
47
117
  && a.args.every((arg, at) => render(arg.value) === render(b.args[at].value));
48
118
  return flips || negates(a, b) || negates(b, a);
@@ -52,10 +122,10 @@ function complementary(a, b) {
52
122
  * and `a || p` with `!a || p` leaves `p`, `merge` telling what the item for that is.
53
123
  */
54
124
  function resolved(items, termOf, merge) {
55
- let current = [...items];
125
+ let current = items.slice();
56
126
  for (let changed = true; changed;) {
57
127
  changed = false;
58
- const alternatives = current.map(item => flatten('||', [termOf(item)]));
128
+ const alternatives = current.map(item => flatten(GuardFn.Or, [termOf(item)]));
59
129
  const keys = alternatives.map(of => new Set(of.map(render)));
60
130
  const kept = current.filter((_, j) => !keys.some((key, i) => i !== j && key.isSubsetOf(keys[j]) && (key.size < keys[j].size || i < j)));
61
131
  if (kept.length < current.length) {
@@ -67,8 +137,8 @@ function resolved(items, termOf, merge) {
67
137
  for (let j = i + 1; j < current.length && !changed; j++) {
68
138
  const [mine, theirs] = [alternatives[i].filter(a => !keys[j].has(render(a))), alternatives[j].filter(a => !keys[i].has(render(a)))];
69
139
  if (mine.length === 1 && theirs.length === 1 && complementary(mine[0], theirs[0])) {
70
- const rest = any(...alternatives[i].filter(a => a !== mine[0]));
71
- current = [...current.slice(0, i), merge(current[i], current[j], rest), ...current.slice(i + 1, j), ...current.slice(j + 1)];
140
+ const rest = anyOf(alternatives[i].filter(a => a !== mine[0]));
141
+ current = current.toSpliced(j, 1).with(i, merge(current[i], current[j], rest));
72
142
  changed = true;
73
143
  }
74
144
  }
@@ -78,12 +148,21 @@ function resolved(items, termOf, merge) {
78
148
  }
79
149
  /** Any of the terms. */
80
150
  function any(...terms) {
151
+ return anyOf(terms);
152
+ }
153
+ /** {@link any} with the terms as one list. */
154
+ function anyOf(terms) {
81
155
  /* 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);
156
+ return connect(GuardFn.Or, True, False, terms.some(t => isCall(t, GuardFn.And)) ? flatten(GuardFn.Or, terms).filter(t => !unsatisfiable(t)) : terms);
83
157
  }
84
158
  /** What a comparison holds as when it does not hold (only ever asked of what R has already seen to be non-`NA`). */
85
159
  const NegatedComparison = {
86
- '==': '!=', '!=': '==', '<': '>=', '>=': '<', '>': '<=', '<=': '>'
160
+ [GuardFn.Equal]: GuardFn.NotEqual,
161
+ [GuardFn.NotEqual]: GuardFn.Equal,
162
+ [GuardFn.Less]: GuardFn.GreaterEqual,
163
+ [GuardFn.GreaterEqual]: GuardFn.Less,
164
+ [GuardFn.Greater]: GuardFn.LessEqual,
165
+ [GuardFn.LessEqual]: GuardFn.Greater
87
166
  };
88
167
  /** The predicates yielding one logical, no matter how long their argument is, so `stopifnot` asks nothing more of them. */
89
168
  function isScalar(term, facts) {
@@ -108,11 +187,11 @@ function firstOnly(facts, fn, operand) {
108
187
  */
109
188
  function truth(term, holds, facts, vector = false, leading = false) {
110
189
  function leaf(of, as, whole) {
111
- const negated = as ? of : known('!', of);
190
+ const negated = as ? of : known(GuardFn.Not, of);
112
191
  if (isCall(of) && isScalar(of, facts)) {
113
192
  return negated;
114
193
  }
115
- return whole ? known('all', negated) : leading ? known('[', negated, numberConstant(1, false)) : negated;
194
+ return whole ? known(GuardFn.All, negated) : leading ? known(GuardFn.Index, negated, numberConstant(1, false)) : negated;
116
195
  }
117
196
  const operand = (fn, of) => truth(of, holds, facts, false, firstOnly(facts, fn, 'x'));
118
197
  if (term.kind === 'constant' && typeof term.value === 'boolean') {
@@ -121,10 +200,10 @@ function truth(term, holds, facts, vector = false, leading = false) {
121
200
  else if (term.kind === 'formal' || term.kind === 'free') {
122
201
  if (vector) {
123
202
  /* `!x` of an empty vector of any type is `logical(0)`, so `all(!x)` holds where `!all(x)` does not */
124
- return known('all', holds ? term : known('!', term));
203
+ return known(GuardFn.All, holds ? term : known(GuardFn.Not, term));
125
204
  }
126
205
  /* a factor is its codes to `if`, while `as.logical` goes by the labels */
127
- return leaf(known('as.logical', known('unclass', term)), holds, false);
206
+ return leaf(known(GuardFn.AsLogical, known(GuardFn.Unclass, term)), holds, false);
128
207
  }
129
208
  else if (!isCall(term) || !term.known) {
130
209
  return leaf(term, holds, vector);
@@ -132,23 +211,23 @@ function truth(term, holds, facts, vector = false, leading = false) {
132
211
  const operands = term.args.map(a => a.value);
133
212
  const [first, second] = operands;
134
213
  switch (term.fn) {
135
- case '!':
214
+ case GuardFn.Not:
136
215
  return truth(first, !holds, facts, vector, leading);
137
- case 'isTRUE':
138
- case 'isFALSE':
216
+ case GuardFn.IsTrue:
217
+ case GuardFn.IsFalse:
139
218
  /* on a formal it says more than the truth of the formal: a single logical */
140
- return holds && isCall(first) && isScalar(first, facts) ? truth(first, term.fn === 'isTRUE', facts) : leaf(term, holds, false);
141
- case '&&':
142
- return (holds ? all : any)(operand('&&', first), operand('&&', second));
143
- case '||':
144
- return (holds ? any : all)(operand('||', first), operand('||', second));
145
- case '&':
219
+ return holds && isCall(first) && isScalar(first, facts) ? truth(first, term.fn === GuardFn.IsTrue, facts) : leaf(term, holds, false);
220
+ case GuardFn.And:
221
+ return (holds ? all : any)(operand(GuardFn.And, first), operand(GuardFn.And, second));
222
+ case GuardFn.Or:
223
+ return (holds ? any : all)(operand(GuardFn.Or, first), operand(GuardFn.Or, second));
224
+ case GuardFn.ElementAnd:
146
225
  return vector && !holds ? leaf(term, holds, vector) : (holds ? all : any)(truth(first, holds, facts, vector, leading), truth(second, holds, facts, vector, leading));
147
- case '|':
226
+ case GuardFn.ElementOr:
148
227
  return vector && holds ? leaf(term, holds, vector) : (holds ? any : all)(truth(first, holds, facts, vector, leading), truth(second, holds, facts, vector, leading));
149
- case 'all':
228
+ case GuardFn.All:
150
229
  return operands.length === 1 && holds ? truth(first, true, facts, true) : leaf(term, holds, false);
151
- case 'any':
230
+ case GuardFn.Any:
152
231
  /* `any(NULL)` is false while `!NULL` fails, so a formal keeps the `any` */
153
232
  return operands.length === 1 && !holds && first.kind !== 'formal' ? truth(first, false, facts, true) : leaf(term, holds, false);
154
233
  default:
@@ -160,8 +239,26 @@ function truth(term, holds, facts, vector = false, leading = false) {
160
239
  }
161
240
  /** How tightly the operators bind, for putting parentheses exactly where R needs them. */
162
241
  exports.Precedence = {
163
- '||': 1, '|': 1, '&&': 2, '&': 2, '==': 4, '!=': 4, '<': 4, '<=': 4, '>': 4, '>=': 4,
164
- '+': 5, '-': 5, '*': 6, '/': 6, '%%': 7, '%/%': 7, '%in%': 7, '%*%': 7, ':': 8, '^': 9
242
+ [GuardFn.Or]: 1,
243
+ [GuardFn.ElementOr]: 1,
244
+ [GuardFn.And]: 2,
245
+ [GuardFn.ElementAnd]: 2,
246
+ [GuardFn.Equal]: 4,
247
+ [GuardFn.NotEqual]: 4,
248
+ [GuardFn.Less]: 4,
249
+ [GuardFn.LessEqual]: 4,
250
+ [GuardFn.Greater]: 4,
251
+ [GuardFn.GreaterEqual]: 4,
252
+ [GuardFn.Plus]: 5,
253
+ [GuardFn.Minus]: 5,
254
+ [GuardFn.Times]: 6,
255
+ [GuardFn.Divide]: 6,
256
+ [GuardFn.Modulo]: 7,
257
+ [GuardFn.IntegerDivide]: 7,
258
+ [GuardFn.In]: 7,
259
+ [GuardFn.MatrixProduct]: 7,
260
+ [GuardFn.Colon]: 8,
261
+ [GuardFn.Power]: 9
165
262
  };
166
263
  exports.NegationPrecedence = 3;
167
264
  exports.AtomPrecedence = 10;
@@ -169,10 +266,10 @@ function precedenceOf(term) {
169
266
  if (!isCall(term)) {
170
267
  return exports.AtomPrecedence;
171
268
  }
172
- else if (term.fn === '!') {
269
+ else if (term.fn === GuardFn.Not) {
173
270
  return exports.NegationPrecedence;
174
271
  }
175
- else if (term.args.length === 1 && (term.fn === '-' || term.fn === '+')) {
272
+ else if (term.args.length === 1 && (term.fn === GuardFn.Minus || term.fn === GuardFn.Plus)) {
176
273
  return exports.AtomPrecedence - 1;
177
274
  }
178
275
  return exports.Precedence[term.fn] !== undefined && term.args.length === 2 ? exports.Precedence[term.fn] : exports.AtomPrecedence;
@@ -197,19 +294,19 @@ function render(term) {
197
294
  }
198
295
  const operands = term.args.map(a => a.value);
199
296
  const [first, second] = operands;
200
- if (term.fn === '!' || (term.args.length === 1 && (term.fn === '-' || term.fn === '+'))) {
297
+ if (term.fn === GuardFn.Not || (term.args.length === 1 && (term.fn === GuardFn.Minus || term.fn === GuardFn.Plus))) {
201
298
  return `${String(term.fn)}${renderBelow(first, precedenceOf(term))}`;
202
299
  }
203
- else if (term.fn === '$' || term.fn === '@') {
300
+ else if (term.fn === GuardFn.Dollar || term.fn === GuardFn.At) {
204
301
  return `${renderBelow(first, exports.AtomPrecedence)}${term.fn}${render(second)}`;
205
302
  }
206
- else if (term.fn === '[' || term.fn === '[[') {
207
- const close = term.fn === '[' ? ']' : ']]';
303
+ else if (term.fn === GuardFn.Index || term.fn === GuardFn.Element) {
304
+ const close = term.fn === GuardFn.Index ? ']' : ']]';
208
305
  return `${renderBelow(first, exports.AtomPrecedence)}${term.fn}${renderArgs(term.args.slice(1))}${close}`;
209
306
  }
210
307
  else if (exports.Precedence[term.fn] !== undefined && term.args.length === 2) {
211
308
  const own = exports.Precedence[term.fn];
212
- const spaced = term.fn === ':' ? ':' : ` ${term.fn} `;
309
+ const spaced = term.fn === GuardFn.Colon ? ':' : ` ${term.fn} `;
213
310
  return `${renderBelow(first, own)}${spaced}${renderBelow(second, own + 1)}`;
214
311
  }
215
312
  return `${calledName(term)}(${renderArgs(term.args)})`;
@@ -225,7 +322,7 @@ function calledName(term) {
225
322
  }
226
323
  /** Every function the term calls, with the package it comes from where that is known, R's own for one flowR knows, leaving out the `&&` and `||` a term is built with. */
227
324
  function callsOf(term, found = new Map()) {
228
- if (isCall(term) && term.known && (term.fn === '&&' || term.fn === '||')) {
325
+ if (isCall(term) && term.known && (term.fn === GuardFn.And || term.fn === GuardFn.Or)) {
229
326
  term.args.forEach(arg => callsOf(arg.value, found));
230
327
  }
231
328
  else if (isCall(term)) {
@@ -270,11 +367,11 @@ function substitute(term, binding, outer, absent) {
270
367
  case 'constant':
271
368
  return term;
272
369
  }
273
- if (term.fn === '...names' && term.known) {
370
+ if (term.fn === GuardFn.DotsNames && term.known) {
274
371
  /* it speaks of the dots of the callee, which the caller fills */
275
372
  return undefined;
276
373
  }
277
- else if (term.fn === 'missing' && term.known) {
374
+ else if (term.fn === GuardFn.Missing && term.known) {
278
375
  const [{ value: formal }] = term.args;
279
376
  if (absent !== undefined) {
280
377
  return formal.kind === 'formal' ? absent.get(formal.name) : undefined;
@@ -316,7 +413,7 @@ function shapeOfLiterals(term) {
316
413
  const type = typeOfLiteral(term);
317
414
  return type === undefined ? undefined : { type, length: type === guard_facts_1.RValueType.Null ? 0 : 1 };
318
415
  }
319
- else if (!isCall(term, 'c') || !term.known || term.args.some(a => a.name !== undefined || a.value.kind !== 'constant')) {
416
+ else if (!isCall(term, GuardFn.Combine) || !term.known || term.args.some(a => a.name !== undefined || a.value.kind !== 'constant')) {
320
417
  return undefined;
321
418
  }
322
419
  const types = term.args.map(a => typeOfLiteral(a.value));
@@ -324,14 +421,14 @@ function shapeOfLiterals(term) {
324
421
  return undefined;
325
422
  }
326
423
  const length = types.filter(t => t !== guard_facts_1.RValueType.Null).length;
327
- return { type: CombinedRanks[Math.max(0, ...types.map(t => CombinedRanks.indexOf(t)))], length };
424
+ return { type: CombinedRanks[types.reduce((rank, t) => Math.max(rank, CombinedRanks.indexOf(t)), 0)], length };
328
425
  }
329
426
  /** The constants of a literal vector like `c("a", "b")` or of a single constant, `undefined` for anything else. */
330
427
  function constantsOf(term) {
331
428
  if (term?.kind === 'constant') {
332
429
  return [term];
333
430
  }
334
- else if (!isCall(term, 'c') || !term.known) {
431
+ else if (!isCall(term, GuardFn.Combine) || !term.known) {
335
432
  return undefined;
336
433
  }
337
434
  const elements = term.args.map(a => a.value);
@@ -360,10 +457,18 @@ function numberConstant(value, integer) {
360
457
  return Number.isFinite(value) ? { kind: 'constant', lexeme: `${value}${integer ? 'L' : ''}`, value } : { kind: 'constant', lexeme: value > 0 ? 'Inf' : '-Inf', value };
361
458
  }
362
459
  const Comparisons = {
363
- '==': (a, b) => a === b, '!=': (a, b) => a !== b, '<': (a, b) => a < b, '<=': (a, b) => a <= b, '>': (a, b) => a > b, '>=': (a, b) => a >= b
460
+ [GuardFn.Equal]: (a, b) => a === b,
461
+ [GuardFn.NotEqual]: (a, b) => a !== b,
462
+ [GuardFn.Less]: (a, b) => a < b,
463
+ [GuardFn.LessEqual]: (a, b) => a <= b,
464
+ [GuardFn.Greater]: (a, b) => a > b,
465
+ [GuardFn.GreaterEqual]: (a, b) => a >= b
364
466
  };
365
467
  const Arithmetic = {
366
- '+': (a, b) => a + b, '-': (a, b) => a - b, '*': (a, b) => a * b, '/': (a, b) => a / b
468
+ [GuardFn.Plus]: (a, b) => a + b,
469
+ [GuardFn.Minus]: (a, b) => a - b,
470
+ [GuardFn.Times]: (a, b) => a * b,
471
+ [GuardFn.Divide]: (a, b) => a / b
367
472
  };
368
473
  const Rounding = { trunc: Math.trunc, floor: Math.floor, ceiling: Math.ceil };
369
474
  /** Whether `match.arg(arg, choices)` returns, for a single string and literal choices, `undefined` for anything else. */
@@ -389,39 +494,39 @@ function evaluated(call) {
389
494
  const operands = call.args.map(a => a.value);
390
495
  const [first, second] = operands;
391
496
  const [a, b] = [first === undefined ? undefined : scalarOf(first), second === undefined ? undefined : scalarOf(second)];
392
- if (call.args.some(arg => arg.name !== undefined) && fn !== 'succeeds') {
497
+ if (call.args.some(arg => arg.name !== undefined) && fn !== GuardFn.Succeeds) {
393
498
  return undefined;
394
499
  }
395
500
  switch (fn) {
396
- case 'length': {
501
+ case GuardFn.Length: {
397
502
  const constants = operands.length === 1 ? constantsOf(first) : undefined;
398
503
  return constants === undefined ? undefined : numberConstant(constants.filter(c => c.lexeme !== 'NULL').length, true);
399
504
  }
400
- case 'is.na':
505
+ case GuardFn.IsNa:
401
506
  return operands.length === 1 && a !== undefined ? logical(a === null) : undefined;
402
- case 'unclass':
507
+ case GuardFn.Unclass:
403
508
  return operands.length === 1 && first.kind === 'constant' ? first : undefined;
404
- case 'as.logical':
405
- case 'all':
406
- case 'any':
407
- if (operands.length !== 1 || (typeof a !== 'boolean' && (fn !== 'as.logical' || typeof a !== 'number'))) {
408
- return a === null && fn === 'as.logical' ? logical(null) : undefined;
509
+ case GuardFn.AsLogical:
510
+ case GuardFn.All:
511
+ case GuardFn.Any:
512
+ if (operands.length !== 1 || (typeof a !== 'boolean' && (fn !== GuardFn.AsLogical || typeof a !== 'number'))) {
513
+ return a === null && fn === GuardFn.AsLogical ? logical(null) : undefined;
409
514
  }
410
515
  return logical(typeof a === 'number' ? (Number.isNaN(a) ? null : a !== 0) : a);
411
- case 'isTRUE':
412
- case 'isFALSE':
413
- return operands.length === 1 && first.kind === 'constant' && first.lexeme !== 'NULL' ? logical(a === (fn === 'isTRUE')) : undefined;
414
- case '%in%': {
516
+ case GuardFn.IsTrue:
517
+ case GuardFn.IsFalse:
518
+ return operands.length === 1 && first.kind === 'constant' && first.lexeme !== 'NULL' ? logical(a === (fn === GuardFn.IsTrue)) : undefined;
519
+ case GuardFn.In: {
415
520
  const among = constantsOf(second)?.map(scalarOf);
416
521
  return a === undefined || a === null || among === undefined || !among.every(v => typeof v === typeof a) ? undefined : logical(among.includes(a));
417
522
  }
418
- case 'succeeds':
419
- return isCall(first, 'match.arg') && first.known ? (r => r === undefined ? undefined : logical(r))(matchesArg(first)) : undefined;
523
+ case GuardFn.Succeeds:
524
+ return isCall(first, GuardFn.MatchArg) && first.known ? (r => r === undefined ? undefined : logical(r))(matchesArg(first)) : undefined;
420
525
  }
421
526
  if (Rounding[fn] !== undefined && operands.length === 1 && typeof a === 'number') {
422
527
  return numberConstant(Rounding[fn](a), false);
423
528
  }
424
- else if (fn === '-' && operands.length === 1 && typeof a === 'number') {
529
+ else if (fn === GuardFn.Minus && operands.length === 1 && typeof a === 'number') {
425
530
  return numberConstant(-a, isInteger(first));
426
531
  }
427
532
  else if (operands.length !== 2 || a === undefined || b === undefined) {
@@ -433,12 +538,12 @@ function evaluated(call) {
433
538
  }
434
539
  else if (typeof a === 'string' || typeof b === 'string') {
435
540
  /* strings compare by the collation of the locale, and a number turns into a string first */
436
- return typeof a === 'string' && typeof b === 'string' && (fn === '==' || fn === '!=') ? logical((a === b) === (fn === '==')) : undefined;
541
+ return typeof a === 'string' && typeof b === 'string' && (fn === GuardFn.Equal || fn === GuardFn.NotEqual) ? logical((a === b) === (fn === GuardFn.Equal)) : undefined;
437
542
  }
438
543
  return logical(Comparisons[fn](Number(a), Number(b)));
439
544
  }
440
545
  else if (Arithmetic[fn] !== undefined && typeof a === 'number' && typeof b === 'number') {
441
- return numberConstant(Arithmetic[fn](a, b), fn !== '/' && isInteger(first) && isInteger(second));
546
+ return numberConstant(Arithmetic[fn](a, b), fn !== GuardFn.Divide && isInteger(first) && isInteger(second));
442
547
  }
443
548
  return undefined;
444
549
  }
@@ -451,13 +556,13 @@ function decided(term, facts) {
451
556
  if (!isCall(term) || !term.known) {
452
557
  return term;
453
558
  }
454
- else if (term.fn === '&&' || term.fn === '||') {
559
+ else if (term.fn === GuardFn.And || term.fn === GuardFn.Or) {
455
560
  const operands = term.args.map(a => decided(a.value, facts));
456
- return term.fn === '&&' ? all(...operands) : any(...operands);
561
+ return term.fn === GuardFn.And ? allOf(operands) : anyOf(operands);
457
562
  }
458
- else if (term.fn === '!') {
563
+ else if (term.fn === GuardFn.Not) {
459
564
  const of = decided(term.args[0].value, facts);
460
- return of.kind === 'constant' && typeof of.value === 'boolean' ? (of.value ? False : True) : known('!', of);
565
+ return of.kind === 'constant' && typeof of.value === 'boolean' ? (of.value ? False : True) : known(GuardFn.Not, of);
461
566
  }
462
567
  const call = { ...term, args: term.args.map(a => ({ ...a, value: decided(a.value, facts) })) };
463
568
  return evaluated(call) ?? predicateDecided(call, facts);
@@ -477,18 +582,18 @@ function predicateDecided(term, facts) {
477
582
  }
478
583
  /** {@link substitute} for a condition in negation normal form, where anything not statable can weaken into {@link True}, while the rest still holds. */
479
584
  function substituteCondition(term, binding, outer, facts, absent) {
480
- if (isCall(term) && term.known && (term.fn === '&&' || term.fn === '||')) {
585
+ if (isCall(term) && term.known && (term.fn === GuardFn.And || term.fn === GuardFn.Or)) {
481
586
  const operands = term.args.map(a => substituteCondition(a.value, binding, outer, facts, absent));
482
- return term.fn === '&&' ? all(...operands) : any(...operands);
587
+ return term.fn === GuardFn.And ? allOf(operands) : anyOf(operands);
483
588
  }
484
589
  const substituted = substitute(term, binding, outer, absent) ?? True;
485
590
  return facts === undefined ? substituted : decided(substituted, facts);
486
591
  }
487
592
  /** 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)`. */
488
593
  function unsatisfiable(term) {
489
- const parts = flatten('&&', [term]);
594
+ const parts = flatten(GuardFn.And, [term]);
490
595
  return parts.some(part => {
491
- const alternatives = flatten('||', [part]);
596
+ const alternatives = flatten(GuardFn.Or, [part]);
492
597
  return alternatives.length > 1 && alternatives.every(a => parts.some(other => other !== part && complementary(a, other)));
493
598
  });
494
599
  }
@@ -501,16 +606,19 @@ exports.GuardTerm = {
501
606
  True,
502
607
  False,
503
608
  known,
609
+ knownOf,
504
610
  isCall,
505
611
  all,
612
+ allOf,
506
613
  any,
614
+ anyOf,
507
615
  truth,
508
616
  firstOnly,
509
617
  render,
510
618
  formalsOf,
511
619
  /** Every function the term calls, see {@link callsOf}. */
512
620
  callsOf(term) {
513
- return [...callsOf(term).values()];
621
+ return callsOf(term).values().toArray();
514
622
  },
515
623
  readsFree,
516
624
  substitute,
@@ -529,7 +637,7 @@ exports.GuardTerm = {
529
637
  if (parts.includes(undefined)) {
530
638
  return undefined;
531
639
  }
532
- return term.fn === 'paste0' ? parts.join('') : term.fn === 'sprintf' && parts.length === 2 && parts[0] === '%s' ? parts[1] : undefined;
640
+ return term.fn === GuardFn.Paste0 ? parts.join('') : term.fn === GuardFn.Sprintf && parts.length === 2 && parts[0] === '%s' ? parts[1] : undefined;
533
641
  },
534
642
  /** Whether the term is a literal `FALSE`. */
535
643
  isFalse(term) {
@@ -550,11 +658,11 @@ exports.GuardTerm = {
550
658
  bareName,
551
659
  /** What all has to hold for a condition to hold. */
552
660
  conjuncts(term) {
553
- return flatten('&&', [term]);
661
+ return flatten(GuardFn.And, [term]);
554
662
  },
555
663
  /** The alternatives of a condition. */
556
664
  disjuncts(term) {
557
- return flatten('||', [term]);
665
+ return flatten(GuardFn.Or, [term]);
558
666
  },
559
667
  /** Whether the term is a literal `TRUE`. */
560
668
  isTrue(term) {
@@ -104,6 +104,7 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
104
104
  fileWritesPerCall: ReadonlySet<NodeId> | undefined;
105
105
  private readonly _unknownSideEffects;
106
106
  private readonly _ambientAccess;
107
+ private readonly _unlessNoEnvironment;
107
108
  constructor(idMap: AstIdMap | undefined);
108
109
  /** Contains the vertices of the root level graph (i.e., included those vertices from the complete graph, that are nested within function definitions) */
109
110
  protected rootVertices: Set<NodeId>;
@@ -245,6 +246,14 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
245
246
  addControlDependency(from: NodeId, to: NodeId, when?: boolean): this;
246
247
  /** Marks the given node as having unknown side effects */
247
248
  markIdForUnknownSideEffects(id: NodeId, target?: LinkTo<RegExp | string>): this;
249
+ /**
250
+ * Marks `id` as having unknown side effects that it only has if the value at `envir` may be an environment:
251
+ * `eval(expr, envir = v)` writes into `v` if it is one, which may be any environment of the program, but into a frame of its own if `v` is a list or a data frame.
252
+ * The dataflow extractor drops the effect once the complete graph shows `v` to be none, see {@link unknownSideEffectsUnlessNoEnvironment}.
253
+ */
254
+ markUnknownUnlessNoEnvironment(id: NodeId, envir: NodeId): this;
255
+ /** The calls marked by {@link markUnknownUnlessNoEnvironment}, with the node whose value they take as their environment. */
256
+ get unknownSideEffectsUnlessNoEnvironment(): ReadonlyMap<NodeId, NodeId>;
248
257
  /** Constructs a dataflow graph instance from the given JSON data, e.g. as sent by the flowR server for further analysis. */
249
258
  static fromJson(data: DataflowGraphJson): DataflowGraph;
250
259
  }
@@ -93,6 +93,8 @@ class DataflowGraph {
93
93
  _unknownSideEffects = new Set();
94
94
  /* sparse: only the calls touching the global state (options, devices, files, ...) have an entry */
95
95
  _ambientAccess = new Map();
96
+ /* the unknown side effects a call only has if the value at the node it maps to may be an environment, see {@link markUnknownUnlessNoEnvironment} */
97
+ _unlessNoEnvironment = new Map();
96
98
  constructor(idMap) {
97
99
  this._idMap = idMap;
98
100
  }
@@ -554,6 +556,9 @@ class DataflowGraph {
554
556
  for (const unknown of otherGraph.unknownSideEffects) {
555
557
  this._unknownSideEffects.add(unknown);
556
558
  }
559
+ for (const [id, value] of otherGraph._unlessNoEnvironment) {
560
+ this._unlessNoEnvironment.set(id, value);
561
+ }
557
562
  for (const [id, access] of otherGraph._ambientAccess) {
558
563
  const known = this._ambientAccess.get(id);
559
564
  this._ambientAccess.set(id, known === undefined ? access : ambient_access_1.AmbientAccess.ofCall(id, ambient_access_1.AmbientAccess.names(known), ambient_access_1.AmbientAccess.names(access)));
@@ -654,6 +659,20 @@ class DataflowGraph {
654
659
  } : node_id_1.NodeId.normalize(id));
655
660
  return this;
656
661
  }
662
+ /**
663
+ * Marks `id` as having unknown side effects that it only has if the value at `envir` may be an environment:
664
+ * `eval(expr, envir = v)` writes into `v` if it is one, which may be any environment of the program, but into a frame of its own if `v` is a list or a data frame.
665
+ * The dataflow extractor drops the effect once the complete graph shows `v` to be none, see {@link unknownSideEffectsUnlessNoEnvironment}.
666
+ */
667
+ markUnknownUnlessNoEnvironment(id, envir) {
668
+ this.markIdForUnknownSideEffects(id);
669
+ this._unlessNoEnvironment.set(node_id_1.NodeId.normalize(id), node_id_1.NodeId.normalize(envir));
670
+ return this;
671
+ }
672
+ /** The calls marked by {@link markUnknownUnlessNoEnvironment}, with the node whose value they take as their environment. */
673
+ get unknownSideEffectsUnlessNoEnvironment() {
674
+ return this._unlessNoEnvironment;
675
+ }
657
676
  /** Constructs a dataflow graph instance from the given JSON data, e.g. as sent by the flowR server for further analysis. */
658
677
  static fromJson(data) {
659
678
  const graph = new DataflowGraph(undefined);
@@ -54,8 +54,12 @@ export declare function suppliesArg<OtherInfo>(args: readonly PotentiallyEmptyRA
54
54
  export declare function resolveEnvirOrPosition<OtherInfo>(arg: PotentiallyEmptyRArgument<OtherInfo & ParentInformation>, formal: BrandedIdentifier, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): EnvirResolution<OtherInfo> | undefined;
55
55
  /** The first of `formals` whose argument resolves to a tracked environment; ambiguity is left to the caller. */
56
56
  export declare function resolveFirstEnvirArg<OtherInfo>(args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], data: DataflowProcessorInformation<OtherInfo & ParentInformation>, sig: FnSig | undefined, formals: readonly BrandedIdentifier[]): EnvirResolution<OtherInfo> | undefined;
57
- /** Marks `result` as an unknown side effect when the envir argument names a value that cannot be pinned down (e.g. a parameter), so routing the writes anywhere would be a guess (and what they bind may be any method); reports whether it did. */
58
- export declare function unknownIfAmbiguous<OtherInfo>(routing: EnvirArgRouting<OtherInfo>, result: DataflowInformation, rootId: NodeId): boolean;
57
+ /**
58
+ * Marks `result` as an unknown side effect when the envir argument names a value that cannot be pinned down (e.g. a parameter), so routing the writes anywhere would be a guess (and what they bind may be any method); reports whether it did.
59
+ * With the node `envir` of the argument's value, the effect only holds while that value may be an environment, which the dataflow extractor decides on the complete graph
60
+ * (see {@link DataflowGraph#markUnknownUnlessNoEnvironment}): a list or a data frame gives the evaluation a frame of its own, which no other binding sees.
61
+ */
62
+ export declare function unknownIfAmbiguous<OtherInfo>(routing: EnvirArgRouting<OtherInfo>, result: DataflowInformation, rootId: NodeId, envir?: NodeId): boolean;
59
63
  /** Resolves a symbol by name to an {@link EnvirResolution} when it holds a tracked environment. */
60
64
  export declare function resolveSymbolToEnvir<OtherInfo>(symbolName: Identifier, nodeId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): EnvirResolution<OtherInfo> | undefined;
61
65
  /** The calls that look up bindings in an environment they are given, or write into it, see {@link readEnvironmentContents}. */
@@ -225,12 +225,19 @@ function resolveFirstEnvirArg(args, data, sig, formals) {
225
225
  }
226
226
  return undefined;
227
227
  }
228
- /** Marks `result` as an unknown side effect when the envir argument names a value that cannot be pinned down (e.g. a parameter), so routing the writes anywhere would be a guess (and what they bind may be any method); reports whether it did. */
229
- function unknownIfAmbiguous(routing, result, rootId) {
228
+ /**
229
+ * Marks `result` as an unknown side effect when the envir argument names a value that cannot be pinned down (e.g. a parameter), so routing the writes anywhere would be a guess (and what they bind may be any method); reports whether it did.
230
+ * With the node `envir` of the argument's value, the effect only holds while that value may be an environment, which the dataflow extractor decides on the complete graph
231
+ * (see {@link DataflowGraph#markUnknownUnlessNoEnvironment}): a list or a data frame gives the evaluation a frame of its own, which no other binding sees.
232
+ */
233
+ function unknownIfAmbiguous(routing, result, rootId, envir) {
230
234
  if (routing !== 'ambiguous') {
231
235
  return false;
232
236
  }
233
237
  (0, unknown_side_effect_1.handleUnknownSideEffect)(result.graph, result.environment, rootId);
238
+ if (envir !== undefined) {
239
+ result.graph.markUnknownUnlessNoEnvironment(rootId, envir);
240
+ }
234
241
  (0, built_in_expression_list_1.noteUnknownMethods)(result.environment, rootId);
235
242
  return true;
236
243
  }
@@ -173,7 +173,7 @@ function evaluateQuoted(code, envir, rootId, information, data) {
173
173
  for (const r of results) {
174
174
  result.graph.addEdge(rootId, r.entryPoint, edge_1.EdgeType.Returns);
175
175
  }
176
- (0, built_in_envir_utils_1.unknownIfAmbiguous)(routing, result, rootId);
176
+ (0, built_in_envir_utils_1.unknownIfAmbiguous)(routing, result, rootId, envir === undefined ? undefined : r_argument_1.RArgument.valueOf(envir)?.info.id);
177
177
  return result;
178
178
  }
179
179
  function namesParentFrame(envir, data) {
@@ -78,7 +78,7 @@ function processLocal(name, args, rootId, data, config) {
78
78
  out: dfExpr.out.concat(dfEnv.out),
79
79
  unknownReferences: []
80
80
  };
81
- (0, built_in_envir_utils_1.unknownIfAmbiguous)(envirRouting, baseResult, rootId);
81
+ (0, built_in_envir_utils_1.unknownIfAmbiguous)(envirRouting, baseResult, rootId, env?.info.id);
82
82
  return baseResult;
83
83
  }
84
84
  /** Evaluates `expr` in the environment `envir` pins down, or in a fresh frame on top of the current one, as `local` and `eval(expr, envir)` do. */