Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/sigdb-page/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface FlagInfo {
/** how a flag reads on the page and what it means, from the database property it stands for unless it is the page's own */
export const Flags: Readonly<Record<ExportFlag, FlagInfo>> = Object.fromEntries(Object.entries({
[ExportFlag.CanThrow]: { name: 'can throw' },
[ExportFlag.Warns]: { name: 'may warn', short: 'warns' },
[ExportFlag.S3Method]: { name: 'S3 method', short: 's3' },
[ExportFlag.NonDeterministic]: { name: 'non-deterministic', short: 'nondet' },
[ExportFlag.CallsDeprecated]: { name: 'calls something deprecated', short: 'calls depr' },
Expand Down
4 changes: 3 additions & 1 deletion scripts/sigdb-page/export-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum ExportFlag {
Deprecated = 'x',
NoDoc = 'u',
NoLocation = 'v',
Warns = 'w',
/** the database does not list the name for the package, flowR states a signature for it there */
Stated = '?'
}
Expand All @@ -32,5 +33,6 @@ export const PropFlags: Readonly<Record<string, ExportFlag>> = {
'value': ExportFlag.Value,
'higher-order': ExportFlag.HigherOrder,
'deprecated': ExportFlag.Deprecated,
'no-doc': ExportFlag.NoDoc
'no-doc': ExportFlag.NoDoc,
'warns': ExportFlag.Warns
};
1 change: 1 addition & 0 deletions scripts/sigdb-page/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const Filters: readonly Filter[] = [
{ kind: FilterKind.Only, key: 'package', label: 'packages', about: 'list packages rather than the names they export' },
{ kind: FilterKind.Package, key: 'archive', label: 'archived', about: 'names from a package CRAN no longer carries' },
{ kind: FilterKind.Flag, key: ExportFlag.CanThrow, label: 'throw', about: Flags[ExportFlag.CanThrow].about ?? '' },
{ kind: FilterKind.Flag, key: ExportFlag.Warns, label: 'warns', about: Flags[ExportFlag.Warns].about ?? '' },
{ kind: FilterKind.Flag, key: ExportFlag.NonDeterministic, label: 'non-det.', about: Flags[ExportFlag.NonDeterministic].about ?? '' },
{ kind: FilterKind.Flag, key: ExportFlag.S3Method, label: 'S3', about: Flags[ExportFlag.S3Method].about ?? '' },
{ kind: FilterKind.Flag, key: ExportFlag.Deprecated, label: 'depr.', about: Flags[ExportFlag.Deprecated].about ?? '' },
Expand Down
2 changes: 2 additions & 0 deletions src/dataflow/environments/ambient-state-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export const AmbientStateName = {
unknownOption: '#option:*',
/** written along with the option `warn` when it may turn warnings into errors (`warn >= 2`) */
warnsAsErrors: '#warn-error',
/** written along with the option `warn` when it surely turns warnings into errors, every value it may get being 2 or more */
warnsAsErrorsSurely: '#warn-error:sure',
/** the finalizers registered (`reg.finalizer`) and the removals that may release their objects, which `gc()` may run */
finalizers: '#finalizers',
/** marks `name` as bound to a text connection opened for writing, whose writes change it */
Expand Down
4 changes: 3 additions & 1 deletion src/dataflow/environments/built-in-eval-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
export enum BuiltInEvalName {
/** `c(...)`, see {@link resolveAsVector} */
Vector = 'eval:vector',
/** the sequence operator `:`, see {@link resolveAsSeq} */
/** the sequence operator `:` and `seq_len`, see {@link resolveAsSeq} */
Seq = 'eval:seq',
/** a single random draw (`runif(1)`), folding to the interval of the values it can take, see {@link resolveAsRandomDraw} */
RandomDraw = 'eval:random-draw',
/** every numeric operator and function, from `+` to `sqrt`, see {@link resolveAsNumeric} */
Numeric = 'eval:numeric',
/** the comparison operators, see {@link resolveAsComparison} */
Expand Down
3 changes: 2 additions & 1 deletion src/dataflow/environments/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { resolveAsElement, resolveAsList, resolveAsSeq, resolveAsVector } from '
import { resolveAsStringFn } from '../eval/resolve/resolve-strings';
import { resolveAsComparison, resolveAsGroup, resolveAsLogical } from '../eval/resolve/resolve-operators';
import { resolveAsGet } from '../eval/resolve/resolve-get';
import { resolveAsNumeric } from '../eval/resolve/resolve-numbers';
import { resolveAsNumeric, resolveAsRandomDraw } from '../eval/resolve/resolve-numbers';
import { BuiltInEvalName } from './built-in-eval-name';
import type { VariableResolve } from '../../config';
import type {
Expand Down Expand Up @@ -426,6 +426,7 @@ export const BuiltInEvalHandlerMapper = {
[BuiltInEvalName.Vector]: resolveAsVector,
[BuiltInEvalName.Seq]: resolveAsSeq,
[BuiltInEvalName.Numeric]: resolveAsNumeric,
[BuiltInEvalName.RandomDraw]: resolveAsRandomDraw,
[BuiltInEvalName.Comparison]: resolveAsComparison,
[BuiltInEvalName.Logical]: resolveAsLogical,
[BuiltInEvalName.StringFn]: resolveAsStringFn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export const BaseDefinitions = [

/* indices and index sequences: bounded by the shape of what they are handed, never by its contents */
/* `seq_along(x)` asks `length(x)` and `which.max(x)` asks `xtfrm(x)` */
fn(base('which', 'which.max', 'which.min', 'seq_len', 'seq_along'), BuiltInProcName.DefaultReadAllArgs, { props: CallProp.Pure, tags: [SemanticCallTag.Narrows], dispatches: ['length', 'xtfrm'] }, Primitive),
fn(base('which', 'which.max', 'which.min', 'seq_along'), BuiltInProcName.DefaultReadAllArgs, { props: CallProp.Pure, tags: [SemanticCallTag.Narrows], dispatches: ['length', 'xtfrm'] }, Primitive),
fn(base('seq_len'), BuiltInProcName.DefaultReadAllArgs, { props: CallProp.Pure, tags: [SemanticCallTag.Narrows], dispatches: ['length', 'xtfrm'] }, { ...Primitive, evalHandler: BuiltInEvalName.Seq }),
/* `seq` dispatches, so `...` really is all it declares; the named formals belong to `seq.default`/`seq.int` */
fn(base('seq'), BuiltInProcName.DefaultReadAllArgs, { props: CallProp.Pure, sig: sig({ '...': Forced.Value }) }, Primitive),
fn(base('scan'), BuiltInProcName.DefaultReadAllArgs,
Expand Down Expand Up @@ -207,9 +208,11 @@ export const BaseDefinitions = [
{ props: CallProp.Invisible, tags: [SemanticCallTag.File, SemanticCallTag.Writes], sig: sig({ x: Forced.Value, file: Forced.Resource | ArgProp.Optional }) }),
fn(base('jitter'), BuiltInProcName.DefaultReadAllArgs,
{ ambient: Ambient.rng.adds, tags: [SemanticCallTag.Random], sig: sig({ x: ArgProp.NoDefault, factor: 0, amount: 0 }) }, Primitive),
fn([...base('sample', 'sample.int'), ...stats('runif', 'rnorm', 'rbinom', 'rpois', 'rexp', 'rgamma', 'rbeta', 'rcauchy', 'rchisq', 'rgeom', 'rhyper', 'rlnorm', 'rlogis',
'rmultinom', 'rnbinom', 'rsignrank', 'rt', 'rf', 'rweibull', 'rwilcox', 'arima.sim', 'simulate', 'kmeans')], BuiltInProcName.DefaultReadAllArgs,
{ ambient: Ambient.rng.adds, tags: [SemanticCallTag.Random] }),
fn([...base('sample', 'sample.int'), ...stats('rnorm', 'rcauchy', 'rhyper', 'rlogis', 'rmultinom', 'rt', 'arima.sim', 'simulate', 'kmeans')], BuiltInProcName.DefaultReadAllArgs,
{ ambient: Ambient.rng.adds, tags: [SemanticCallTag.Random] }),
/* a single draw of these folds to the values it can take, see `RandomDraws` of the value solver */
fn(stats('runif', 'rbeta', 'rbinom', 'rpois', 'rexp', 'rgamma', 'rchisq', 'rgeom', 'rnbinom', 'rweibull', 'rlnorm', 'rf', 'rsignrank', 'rwilcox'), BuiltInProcName.DefaultReadAllArgs,
{ ambient: Ambient.rng.adds, tags: [SemanticCallTag.Random] }, { evalHandler: BuiltInEvalName.RandomDraw }),
fn(base('expression'), BuiltInProcName.Default, { props: CallProp.Lang, sig: sig({ '...': ArgProp.Nse }) }, Primitive),
fn(base('reg.finalizer'), BuiltInProcName.Default,
{ ambient: Ambient.finalizers.adds, sig: sig({ e: ArgProp.Forced, f: ArgProp.Forced, onexit: Forced.Flag }) }),
Expand Down
52 changes: 52 additions & 0 deletions src/dataflow/environments/default-builtin-config/warnings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/** When a call of a function R ships surely signals a warning, by the values of its arguments: always (`warning("x")`), without any argument (`max()`), where a name it removes is not bound (`rm(x)`, which only the code running before can tell), or where a value of the formal (one element suffices) is a string R cannot read as a number (`as.numeric("a")`), a string at all (`mean("a")`), a finite number outside of the closed range `[from, to]` (`sqrt(-1)`), or a whole number of at most `wholeAtMost`, a pole (`gamma(0)`). Each entry was checked with R 4.6: a condition only ever holds for values that warn. Under `options(warn = 2)` R turns each of these warnings into an error. */
export type WarnsWhen = { readonly always: true } | { readonly noArguments: true } | { readonly removesUnbound: true }
| { readonly formal: string } & ({ readonly notANumber: true } | { readonly aString: true } | { readonly outside: readonly [from: number, to: number] } | { readonly wholeAtMost: number });

/** what R does not read as a number: everything but decimal and hexadecimal numbers, `Inf`, `Infinity`, `NaN`, and `NA` (which warns, but is left out to keep this list exact), around them only white space */
const RNumberSyntax = /^\s*(?:[+-]?(?:(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?|0[xX](?:[0-9a-fA-F]+\.?[0-9a-fA-F]*|\.[0-9a-fA-F]+)(?:[pP][+-]?\d+)?|inf(?:inity)?|nan|na))?\s*$/i;

/** Whether R's coercion of the string to a number surely warns: it is no number (an empty string or white space gives `NA` silently). */
export function notANumber(str: string): boolean {
return !RNumberSyntax.test(str);
}

/** the coercions that warn about strings they cannot read (`NAs introduced by coercion`) */
const Coerces: readonly WarnsWhen[] = [{ formal: 'x', notANumber: true }];
/** beyond it, `as.integer` gives `NA` (a value between it and the largest integer truncates into the range) */
const IntegerBound = 2147483648;

/** The functions of R that surely warn for some values of their arguments, by name. Functions not listed may warn as well (any closure may call `warning`), they are just never sure to. */
export const RWarnings: Readonly<Record<string, readonly WarnsWhen[]>> = {
'warning': [{ always: true }],
'as.numeric': Coerces,
'as.double': Coerces,
/* `as.complex` is left out: it reads complex numbers (`"1+2i"`), which the syntax above does not cover */ /* `as.integer(3e9)`: NAs introduced by coercion to integer range */
'as.integer': [...Coerces, { formal: 'x', outside: [-IntegerBound, IntegerBound] }],
/* NaNs produced */
...Object.fromEntries([['log', 0, Infinity], ['log2', 0, Infinity], ['log10', 0, Infinity], ['sqrt', 0, Infinity], ['log1p', -1, Infinity],
['asin', -1, 1], ['acos', -1, 1], ['atanh', -1, 1], ['acosh', 1, Infinity]].map(([f, from, to]) => [f, [{ formal: 'x', outside: [from, to] }]])),
'gamma': [{ formal: 'x', wholeAtMost: 0 }],
'factorial': [{ formal: 'x', wholeAtMost: -1 }],
/* argument is not numeric or logical: returning NA */
'mean': [{ formal: 'x', aString: true }],
/* no non-missing arguments to max; returning -Inf */
'max': [{ noArguments: true }], 'min': [{ noArguments: true }],
/* object 'x' not found */
'rm': [{ removesUnbound: true }]
};

/** Whether the scalar `value` of an argument makes `when` hold, `undefined` for a condition on no argument. */
export function warnsFor(when: WarnsWhen, value: string | number): boolean | undefined {
if('always' in when || 'noArguments' in when || 'removesUnbound' in when) {
return undefined;
} else if('notANumber' in when) {
return typeof value === 'string' && notANumber(value);
} else if('aString' in when) {
return typeof value === 'string';
} else if(typeof value !== 'number' || !Number.isFinite(value)) {
return false;
} else if('outside' in when) {
return value < when.outside[0] || value > when.outside[1];
}
return value <= when.wholeAtMost && Number.isInteger(value);
}
Loading
Loading