Repository navigation
[Query] Inspect Function Guards
Generated from 'wiki-query.ts' on 2026-11-25, 20:20:36 UTC (v2.17.13), do not edit directly.
Inspect Function Guards Query [overview]
Infer what functions ask of the calls they return from
This query is requested with the type inspect-fn-guards.
Run in the REPL: :query @inspect-fn-guards [(<crit>;...)] <code | file://path>
This query works out the contract of every function of a program: what a call has to fulfill to return normally, read off the checks in the body.
In the example below, f stops unless x is a non-negative number at least n long, and match.arg only lets type be "a" or "b".
The query states exactly that: per formal what a call has to give and what holds of it, and what relates several formals, like length(x) >= n:
[ { "type": "inspect-fn-guards" } ](This can be shortened to @inspect-fn-guards when used with the REPL command :query).
Results (prettified and summarized):
Query: inspect-fn-guards
- Function 53 (1.6-6.1) requires succeeds(match.arg(type, c("a", "b"))) && (!is.function(x) && !is.environment(x) && !is.symbol(x) && !is.call(x) || is.object(x)) && is.numeric(x) && x >= 0 && (!is.function(n) && !is.environment(n) && !is.symbol(n) && !is.call(n) || is.object(n) || is.object(length(x))) && length(x) >= n
x (required): integer or double, unless classed, value >= 0
checked by: is.numeric(x), x >= 0, length(x) >= n
n (required): (anything but function, environment, symbol, or language, unless classed) unless is.object(length(x))
checked by: length(x) >= n
between x and n: length(x) >= n
type: NULL or character, one of a, b (or a prefix)
checked by: succeeds(match.arg(type, c("a", "b")))
Show Detailed Results as Json
The analysis ran (including parsing and normalization and the query) within the generation environment.
The JSON contains the ids of the nodes in question (see the Interface wiki page).
As the code is pretty long, we inhibit pretty printing and syntax highlighting (JSON, hiding built-in):
{"inspect-fn-guards":{".meta":{},"guards":{"53":{"requires":"succeeds(match.arg(type, c(\"a\", \"b\"))) && (!is.function(x) && !is.environment(x) && !is.symbol(x) && !is.call(x) || is.object(x)) && is.numeric(x) && x >= 0 && (!is.function(n) && !is.environment(n) && !is.symbol(n) && !is.call(n) || is.object(n) || is.object(length(x))) && length(x) >= n","pure":"succeeds(match.arg(type, c(\"a\", \"b\"))) && (!is.function(x) && !is.environment(x) && !is.symbol(x) && !is.call(x) || is.object(x)) && is.numeric(x) && x >= 0 && (!is.function(n) && !is.environment(n) && !is.symbol(n) && !is.call(n) || is.object(n) || is.object(length(x))) && length(x) >= n","clauses":[{"requires":{"kind":"call","fn":"succeeds","known":true,"args":[{"value":{"kind":"call","fn":"match.arg","args":[{"value":{"kind":"formal","name":"type"}},{"value":{"kind":"call","fn":"c","args":[{"value":{"kind":"constant","lexeme":"\"a\"","value":"a"}},{"value":{"kind":"constant","lexeme":"\"b\"","value":"b"}}],"pkg":"base","known":true}}],"pkg":"base","known":true}}]},"rendered":"succeeds(match.arg(type, c(\"a\", \"b\")))","at":19,"kind":"explicit","pure":true},{"requires":{"kind":"call","fn":"||","known":true,"args":[{"value":{"kind":"call","fn":"&&","known":true,"args":[{"value":{"kind":"call","fn":"&&","known":true,"args":[{"value":{"kind":"call","fn":"&&","known":true,"args":[{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.function","known":true,"args":[{"value":{"kind":"formal","name":"x"}}]}}]}},{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.environment","known":true,"args":[{"value":{"kind":"formal","name":"x"}}]}}]}}]}},{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.symbol","known":true,"args":[{"value":{"kind":"formal","name":"x"}}]}}]}}]}},{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.call","known":true,"args":[{"value":{"kind":"formal","name":"x"}}]}}]}}]}},{"value":{"kind":"call","fn":"is.object","known":true,"args":[{"value":{"kind":"formal","name":"x"}}]}}]},"rendered":"!is.function(x) && !is.environment(x) && !is.symbol(x) && !is.call(x) || is.object(x)","at":28,"kind":"implicit","message":"comparison (<) is possible only for atomic and list types","pure":true,"under":[{"kind":"call","fn":"is.numeric","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true}]},{"requires":{"kind":"call","fn":"is.numeric","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true},"rendered":"is.numeric(x)","at":33,"kind":"explicit","message":"invalid x","pure":true,"under":[{"kind":"call","fn":"||","known":true,"args":[{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.numeric","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true}}]}},{"value":{"kind":"call","fn":"<","args":[{"value":{"kind":"formal","name":"x"}},{"value":{"kind":"constant","lexeme":"0","value":0}}],"pkg":"base","known":true}}]}]},{"requires":{"kind":"call","fn":">=","args":[{"value":{"kind":"formal","name":"x"}},{"value":{"kind":"constant","lexeme":"0","value":0}}],"pkg":"base","known":true},"rendered":"x >= 0","at":33,"kind":"explicit","message":"invalid x","pure":true,"under":[{"kind":"call","fn":"||","known":true,"args":[{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.numeric","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true}}]}},{"value":{"kind":"call","fn":"<","args":[{"value":{"kind":"formal","name":"x"}},{"value":{"kind":"constant","lexeme":"0","value":0}}],"pkg":"base","known":true}}]}]},{"requires":{"kind":"call","fn":"||","known":true,"args":[{"value":{"kind":"call","fn":"||","known":true,"args":[{"value":{"kind":"call","fn":"&&","known":true,"args":[{"value":{"kind":"call","fn":"&&","known":true,"args":[{"value":{"kind":"call","fn":"&&","known":true,"args":[{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.function","known":true,"args":[{"value":{"kind":"formal","name":"n"}}]}}]}},{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.environment","known":true,"args":[{"value":{"kind":"formal","name":"n"}}]}}]}}]}},{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.symbol","known":true,"args":[{"value":{"kind":"formal","name":"n"}}]}}]}}]}},{"value":{"kind":"call","fn":"!","known":true,"args":[{"value":{"kind":"call","fn":"is.call","known":true,"args":[{"value":{"kind":"formal","name":"n"}}]}}]}}]}},{"value":{"kind":"call","fn":"is.object","known":true,"args":[{"value":{"kind":"formal","name":"n"}}]}}]}},{"value":{"kind":"call","fn":"is.object","known":true,"args":[{"value":{"kind":"call","fn":"length","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true}}]}}]},"rendered":"!is.function(n) && !is.environment(n) && !is.symbol(n) && !is.call(n) || is.object(n) || is.object(length(x))","at":41,"kind":"implicit","message":"comparison (<) is possible only for atomic and list types","pure":true},{"requires":{"kind":"call","fn":">=","args":[{"value":{"kind":"call","fn":"length","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true}},{"value":{"kind":"formal","name":"n"}}],"pkg":"base","known":true},"rendered":"length(x) >= n","at":45,"kind":"explicit","message":"x is too short","pure":true,"under":[{"kind":"call","fn":"<","args":[{"value":{"kind":"call","fn":"length","args":[{"value":{"kind":"formal","name":"x"}}],"pkg":"base","known":true}},{"value":{"kind":"formal","name":"n"}}],"pkg":"base","known":true}]}],"conditions":{"type":["succeeds(match.arg(type, c(\"a\", \"b\")))"],"x":["is.numeric(x)","x >= 0","length(x) >= n"],"n":["length(x) >= n"]},"formals":{"x":{"unclassedTypes":["integer","double"],"bounds":{"value":[0,null]}},"type":{"types":["NULL","character"],"values":["a","b"],"partial":true}},"relations":[{"holds":"length(x) >= n","formals":["x","n"]}],"conditional":[{"formal":"n","claim":{"unclassedTypes":["NULL","logical","integer","double","complex","character","raw","list","pairlist","expression","other"]},"unless":"is.object(length(x))"}],"required":["x","n"]}}},".meta":{}}
Original Code
f <- function(x, n, type = c("a", "b")) {
type <- match.arg(type)
if(!is.numeric(x) || x < 0) stop("invalid x")
if(length(x) < n) stop("x is too short")
sum(x)
}- the
call-contractslinting rule reports a call that cannot fulfill the contract of what it calls, likef(-1)orf("a") - the Inspect Argument Roles Query reports the formals a call has to supply (
ArgProp::Required) and those a condition reads (ArgProp::Guarded) - the Signature Query and the signature browser show the contracts of the functions of R and of packages
- the playground shows the contract of the function under the cursor
Each function the program defines gets FunctionGuards:
-
FunctionGuards::clauses: oneGuardClauseper check, with the condition as R code and as aGuardTermtree (succeeds(e)stands forenot raising an error), whether R checks it itself (implicit, likex + 1for a value that is no number) or the code does (explicit, like thestopabove), and the message of the failure -
FunctionGuards::formals: what the conditions together claim of each formal, as aFormalClaim:
| field | a normal return makes sure that |
|---|---|
types |
typeof(x) is one of these |
unclassedTypes |
the same, but only for an x without a class (a classed one may bring its own method) |
values, partial
|
x is one of these strings, or with partial abbreviates one (match.arg) |
classes |
x inherits from one of these |
bounds |
x (value), length(x), nchar(x), nrow(x), ... lie in this range |
whole, notNA, callable
|
trunc(x) == x, !anyNA(x), x is or names a function |
resolves |
a name built from x, like paste0("f_", x), exists |
cases |
what holds in each case a branch on x tells apart, like if(is.logical(center)) ... else ...
|
-
FunctionGuards::relations: what relates several formals, likelength(x) >= n, each with the formals it speaks of and what lets a call through anyway (unless) -
FunctionGuards::conditional: what holds of a formal unless something about the others lets the call through, likemhaving to be a number unlessxis no count -
FunctionGuards::returns: what holds of the value a call returns normally -
FunctionGuards::conditions: per formal the checks as they read in R, also those no claim has a word for, likestopifnot(x %% 2 == 0) -
FunctionGuards::required: the formals a call has to supply -
FunctionGuards::pure: the checks that fail before the function does anything but compute, so a call failing on them leaves nothing behind
The query takes kind (keep only implicit or explicit conditions), onlyPure, formals (only these formals), and builtIns (also state the contracts of the functions of R and of the packages flowR knows).
-
stop,rlang::abort, and every other function that always fails, reached underif,&&,||, andswitch, taking an earlierreturninto account:if(a) return(1); if(!is.numeric(x)) stop()asksa || is.numeric(x) -
stopifnot,assertthat::assert_that,match.arg,rlang::arg_match, and theassert_*functions ofcheckmate - what R fails on by itself, like
strsplitfor anything but a character vector - names that have to resolve, like
do.call(paste0("f_", x), list()), and aUseMethodwithout a default method - the methods a dispatch may run: a generic asks what its
defaultmethod (or that of an implicit class likematrix) asks, unlessis.object(x) - the contracts of the functions a body surely calls, in terms of what it hands them, and predicates of the program like
is_count <- function(v) is.numeric(v) && v >= 0
A contract can only be as good as what flowR knows about the functions a body calls. There are three places to add that knowledge:
-
A predicate or check of your own code base, without changing flowR. Describe it in the configuration of the analyzer (the same
overwriteBuiltInsworks in a configuration file). With this,f <- function(x) { stopifnot(is_text(x)); x }claims thatxis a character vector:
new FlowrAnalyzerBuilder().amendConfig(c => {
c.semantics.environment.overwriteBuiltIns.definitions = [{
type: 'function', names: ['is_text'], processor: BuiltInProcName.DefaultReadAllArgs,
config: { guards: { is_text: { predicate: { yes: RValueType.Character, exact: true } } } }
}];
})Besides predicate, the guard facts can state asks (what the function fails on per formal), required (formals a call has to give), check (the function fails unless its argument passes, like checkmate::assert_numeric), asserts (it fails unless every argument is TRUE, like stopifnot), and returns. All of them are typed in src/dataflow/environments/guard-facts.ts.
2. A function of R or a package that flowR should know for everyone. Add its guard facts to the built-in configuration: src/dataflow/environments/default-builtin-config/guards.ts for functions of base R and packages, guards-primitive-ops.ts for the operators and math primitives. For example 'which': { asks: { x: fails(is('is.logical'), 'argument to \'which\' is not logical') } } makes every which(x) ask for a logical x.
3. The closures of the base packages. Their contracts are read off R itself with npx ts-node --transpile-only scripts/gen-base-guards.ts, which writes src/data/r-base-guards.generated.ts (currently from R 4.6.1). Rerun it after changing what flowR knows, as the base contracts are built on top of that knowledge.
Every condition the query reports is necessary: a call that breaks one fails in R. What flowR cannot state exactly (a loop body, a variable with several definitions, a function it does not know) weakens a condition or drops it, it never makes one stronger than what the code asks for.
- A condition speaks of the value at the check, so a formal overwritten before it is read as what it was overwritten with.
- A default value claims nothing:
function(n = 5L)still takesn = 1.5. Onlymatch.argandrlang::arg_matchread the default, as the values they allow. - What R fails on depends on the R version flowR assumes (
solver.sigdb.assumedRVersion, else R 4.6.1): a condition ofiflonger than one only fails since R 4.2.0, an operand of&&longer than one since R 4.3.0. - A missing argument handed on to another function written in R is only required if that function needs it, as R hands it on as a promise:
matpoints(x)passes its missingytomatplot, which checksmissing(y).
Implementation Details
The Inspect Function Guards Query is executed by executeFnGuardsQuery.
Currently maintained by Florian Sihler and Oliver Gerstl at Ulm University
Email | GitHub | Penguins | Portfolio
- 🧑💻 Developer Onboarding
- 💻 Setup
- 👓 Overview
- 🪟 Interfacing with flowR
- 🌋 Core
- 🧹 Testing & Linting (Benchmark Page)
⁉️ FAQ- ℹ️ Extra Information