Skip to main content

20  Calls from documents

A typed function whose parameters all have types can be described completely: its parameters, their types, defaults, and descriptions, and its rules. An agent or another program reads that description as a JSON Schema, writes a document of a call, and R8 checks the document and runs the call.

20.1 Descriptions

description, after default, describes a parameter in the declaration, so that the description is known where the function’s code is not: in its schema, and to a program that writes a call. arg(description = ) alone describes a parameter without a type:

fit_line <- new_function(
  "fit_line",
  function(
    x = arg_vec(type_double(), description = "The predictor."),
    y = arg_vec(type_double(), description = "The outcome."),
    intercept = arg_logical(default = TRUE, description = "Fit an intercept.")
  ) {
    stats::coef(if (intercept) stats::lm(y ~ x) else stats::lm(y ~ x - 1))
  },
  description = "Fits a line by least squares."
)
fit_line
<r8 function demo::fit_line>
fit_line(x, y, intercept)
  x: vector of double
    The predictor.
  y: vector of double
    The outcome.
  intercept: logical = TRUE
    Fit an intercept.

In a package, the roclet r8rd() writes the @param of a described parameter from its declaration (Documentation with roxygen2); a block that also documents that parameter in @param is an error, so that each parameter has one text.

20.2 The schema of the arguments

r8schema(f) writes the JSON Schema of the arguments of a function whose parameters all have types, which a tool’s input schema takes: one property per parameter, with its type, default, and description. A parameter without a default is required; a default written in R, such as n = length(x), makes its parameter optional, and the function evaluates it when it runs:

cat(r8schema(fit_line))
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "demo::fit_line",
  "description": "Fits a line by least squares.",
  "type": "object",
  "properties": {
    "x": {
      "type": "array",
      "items": {
        "type": "number"
      },
      "description": "The predictor."
    },
    "y": {
      "type": "array",
      "items": {
        "type": "number"
      },
      "description": "The outcome."
    },
    "intercept": {
      "type": "boolean",
      "description": "Fit an intercept.",
      "default": true
    }
  },
  "required": [
    "x",
    "y"
  ],
  "additionalProperties": false,
  "x-r8": {
    "function": "demo::fit_line"
  }
}

A function with a parameter without a type, or with ..., has no schema, and the error names those parameters.

20.3 Documents of a call

A document of a call names the function and holds its arguments: {"$function": "ns::f", "arguments": {...}}. r8check_call(f, json) checks a document without running the function, and gives every problem, each at its path in the document; the rules are checked once every argument satisfies its type. r8call_from_json(f, json) runs a call that holds, and raises every problem of one that does not. The functions that a document may call are those given as f, one or a list, and a document never selects other code:

call <- '{"$function": "demo::fit_line", "arguments": {"x": [1, 2, 3, 4], "y": [2.1, 3.9, 6.2, 7.8]}}'
r8check_call(fit_line, call)
[1] code       path       rule       rule_index message    where      suggestion
[8] hint      
<0 rows> (or 0-length row.names)
r8call_from_json(fit_line, call)
(Intercept)           x 
       0.15        1.94 
r8check_call(fit_line, '{"$function": "demo::fit_line", "arguments": {"x": [1, "b"], "slope": 2}}')[, c("code", "path")]
              code             path
1 unknown_argument /arguments/slope
2    type_mismatch   /arguments/x/1
3 missing_argument     /arguments/y
r8call_from_json(fit_line, '{"$function": "demo::fit_line", "arguments": {"x": [1, "b"]}}')
Error:
! Invalid arguments of `fit_line()`: 2 problems
- `x[[2]]` must be a number; found a string. [type_mismatch]
- `y` is required. [missing_argument]

An argument that the document omits takes its default, and null is NULL. r8call_to_json(f, ...) writes the document of a call, with the versions of the types its arguments hold in "$types":

r8call_to_json(fit_line, c(1, 2, 3), c(2, 4, 6), intercept = FALSE)
{"$function":"demo::fit_line","$types":{},"arguments":{"intercept":false,"x":[1,2,3],"y":[2,4,6]}} 

20.4 Cost

A call from a document of three arguments and a rule takes 13 µs, about 11 µs more than the direct call: r8call_from_json() reads and checks the document in one pass in Rust, and the function receives the arguments in their stored form.

© 2026 E.D. Gennatas