Package {ast2ast}


Type: Package
Title: Translates an R Function to a C++ Function
Version: 1.0
Date: 2026-09-01
Maintainer: Krämer Konrad <konrad_kraemer@yahoo.de>
BugReports: https://github.com/Konrad1991/ast2ast/issues
URL: https://github.com/Konrad1991/ast2ast
Description: Enable translation of a tiny subset of R to C++. The user has to define a R function which gets translated. For a full list of possible functions check the documentation. After translation an R function is returned which is a shallow wrapper around the C++ code. Alternatively an external pointer to the C++ function is returned to the user. The intention of the package is to generate fast functions which can be used as ode-system or during optimization.
License: GPL-3
Imports: Rcpp (≥ 1.0.4), R6, methods
LinkingTo: Rcpp
Depends: R (≥ 4.1.0)
SystemRequirements: GNU make, C++20, Rtools (>= 4.5) on Windows
NeedsCompilation: yes
VignetteBuilder: knitr
Suggests: knitr, kableExtra, rmarkdown, tinytest, microbenchmark, ggplot2, RcppXPtrUtils, pkgbuild, nnls
Encoding: UTF-8
Packaged: 2026-09-04 05:30:21 UTC; konrad
Author: Krämer Konrad [aut, cre]
Repository: CRAN
Date/Publication: 2026-09-14 15:00:10 UTC

Translate an R function into C++

Description

translate() compiles an R function into C++ code using the ast2ast expression template library. The result can be called directly from R (default) or returned as an external pointer. The C++ source code can also be retrieved without compilation.

Usage

translate(
  f,
  types_f = NULL,
  output = "R",
  derivative = NULL,
  verbose = FALSE,
  getsource = FALSE,
  debug = TRUE
)

Arguments

f

An R function to be translated into C++. Argument types are declared with an argtypes(...) block as the first statement of its body; see the Details section.

types_f

A helper function that declares custom struct types via new_type(), for use as argument types, variable types, or slot types inside f. Optional; defaults to NULL. See the Details section.

output

Controls what is returned:

  • "R" (default): an R function wrapping the compiled C++.

  • "XPtr": an external pointer to the compiled C++ function.

derivative

When derivatives are required one can set here the mode of the automatic differentiation:

  • NULL (default): You don't require any derivatives.

  • "forward" Determine the derivatives via forward automatic differentiation.

  • "reverse" Determine the derivatives via reverse automatic differentiation.

verbose

Logical. If TRUE, prints output from the compilation process.

getsource

Logical. If TRUE, returns the generated C++ source code as a string instead of compiling it.

debug

Logical. Defaults to TRUE. When TRUE, a runtime error is reported as "In '<statement>': <message>", naming the specific line of translated code that failed. Set to FALSE to get the plain, unattributed message instead.

Details

Supported functions

The following R constructs are currently supported:

  1. Assignment: =, <-, and $ (struct field access/assignment)

  2. Allocation: vector, matrix, array, rep, logical, integer, numeric

  3. Object info: length, dim, nrow, ncol

  4. Arithmetic: +, -, *, /, %%, %/%, %*%, ^

  5. Indexing: [], [[]], and at() (an alias for [[]])

  6. Math: sin, asin, sinh, cos, acos, cosh, tan, atan, tanh, sqrt, abs, sign, log, exp, floor, ceiling, trunc, round

  7. Reductions: sum, prod, mean, max, min, which.max, which.min, which, all, any

  8. Vector transforms: rev, sort, cumsum

  9. Matrix margins: colSums, rowSums, colMeans, rowMeans

  10. Selection: ifelse

  11. Linear algebra: t, chol, crossprod, tcrossprod, diag, get_diag, solve, backsolve, forwardsolve, rbind, cbind

  12. Root finding / least squares: uniroot, nnls

  13. Functionals (take an fn()): map, Reduce, Filter, apply

  14. Optimizers / derivatives of an fn(): jacobian, lbfgsb, pso

  15. Casts: as.numeric, as.integer, as.logical

  16. Sequences: seq_len, seq_along

  17. Concatenation: c

  18. Control flow: for, while, repeat, next, break, if, else if, else

  19. Comparison: ==, !=, >, <, >=, <=

  20. Logical ops: &&, ||, &, |, !

  21. Printing: print

  22. Errors: stop

  23. Return: return

  24. Catmull–Rom spline: cmr

  25. Sequence operator: :

  26. Helpers: is.na, is.nan, is.finite, is.infinite

  27. Explicit typing: type()

  28. Derivative functions in forward mode: seed, unseed, and get_dot

  29. Derivative functions in reverse mode: deriv

  30. Inner functions: fn(), see below

  31. Custom types: new_type(), see below

Types are static in C++ and cannot be changed within the function. Each type consists of a base data type and a data structure:

Types are usually inferred automatically. Users may annotate explicitly, for example:

      a |> type(logical)      # scalar logical
      b |> type(vec(int))     # integer vector
      c |> type(mat(double))  # double matrix
    

Scalars in ast2ast differ from R: in R, scalars are length-1 vectors, but in C++ they are true scalars and cannot be subset.

Argument types

Arguments default to matrix(double) if f's body has no argtypes(...) block. To override, make argtypes(...) the first statement of the body, one type() annotation per argument:

      f <- function(a, b, c) {
        argtypes(
          a |> type(borrow_vec(double)) |> ref(),
          b |> type(borrow_mat(double)) |> ref() |> const(),
          c |> type(double) |> ref()
        )
        # ... body ...
      }
    

Supported extensions for arguments:

Inner functions

Functions can be defined inside f with fn(), which takes three positional parts: argtypes(...) (argument types, same form as the outer argtypes(...) block), return(...) (the return type), and a { } block (the function body; the braces are optional for a single statement). Inner functions may call each other, including recursively, and can be passed as values to functions expecting one, such as uniroot(). A non-const() parameter only binds to a bare variable at the call site, not to an arbitrary expression (e.g. x + 1); declare the parameter const() to accept expressions.

      factorial <- fn(
        argtypes(a |> type(int) |> const()),
        return(int),
        {
          if (a == 1L) return(a) else return(a * factorial(a - 1L))
        }
      )
    

uniroot(f, interval, tol, maxiter) takes a function of a single double returning a double (or two arguments if a fifth, extra-data argument is supplied to uniroot), and returns a struct with fields root, f_root, iter, and estim_prec. nnls(A, b) solves the non-negative least squares problem and returns the solution vector.

Functionals

These take an fn() as their first argument:

Optimizers and Jacobians

For lbfgsb and pso the optional trailing data argument (any non-function, non-character value) is passed to f unchanged as a second argument.

Custom types

User-defined struct types are declared in a types_f helper function (passed to translate() via types_f =) and created with new_type() and slots():

      types_f <- function() {
        new_type(Point, slots(x |> type(double), y |> type(double)))
      }
    

On the R side, a value of a custom type is a named list with a matching class attribute, e.g. structure(list(x = 1, y = 2), class = "Point"). Fields are read and written with $, including chained access such as s$circles[[1L]]$center$x. A slot may itself be a custom type (nested structs) or collection(TypeName), a vector of a custom type; allocate one locally with vector(mode = "TypeName", n).

Derivatives

ast2ast supports automatic differentiation (AD) in two modes: forward and reverse. The mode is selected via the derivative argument of translate().

The AD system is intentionally low-level and explicit: derivative computations are assembled from a small set of primitive building blocks (seed, unseed, get_dot, deriv), which keeps the interface transparent and close to the generated C++ code. A built-in jacobian(f, x) is also provided for the common case (see Optimizers and Jacobians above); it is itself implemented on top of those primitives.

Forward mode

In forward mode, derivatives are propagated alongside values. The following functions are available:

A typical pattern is to loop over input dimensions, seed one component at a time, evaluate the function, extract derivatives using get_dot(), and assemble the Jacobian manually.

Reverse mode

In reverse mode, derivatives are accumulated by backpropagation from outputs to inputs. The function

computes the Jacobian of y with respect to x. This call must appear explicitly in the translated function. Reverse mode is particularly efficient when the number of outputs is small relative to the number of inputs.

Design philosophy

Derivative computation in ast2ast is explicit by design. The full control flow (including loops, seeding, unseeding, and accumulation) is visible in the user code and translated directly into C++. This makes the generated code easy to inspect, reason about, and modify, and avoids hidden performance costs. Important: The derivative argument only enables the AD infrastructure. It does not automatically differentiate your function. You must explicitly call seed(), unseed(), get_dot(), or deriv() inside your function, depending on the chosen mode.

Note: The generated C++ mimics R semantics closely but not exactly. Always validate compiled functions against the original R implementation before using in production. See the vignette Detailed Documentation for a full comparison, and InformationForPackageAuthors for internals.

Value

Depending on output:

Examples

## Not run: 
# Hello World
# ----------------------------------------------------------------
f <- function() {
  print("Hello World!")
}
f_cpp <- ast2ast::translate(f)
f_cpp()

# Derivatives
# ----------------------------------------------------------------
f <- function(y, x) {
  y[[1L]] <- x[[1L]] * x[[2L]]
  y[[2L]] <- x[[1L]] + x[[2L]]*x[[2L]]
  jac <- deriv(y, x)
  return(jac)
}
fcpp_reverse <- ast2ast::translate(f, derivative = "reverse")
y <- c(0, 0)
x <- c(2, 3)
fcpp_reverse(y, x)

f <- function(y, x) {
  jac <- matrix(0.0, length(y), length(x))
  for (i in 1L:length(x)) {
    seed(x, i)
    y[[1L]] <- x[[1L]] * x[[2L]]
    y[[2L]] <- x[[1L]] + x[[2L]]*x[[2L]]
    d <- get_dot(y)
    jac[TRUE, i] <- d
    unseed(x, i)
  }
  return(jac)
}
fcpp_forward <- ast2ast::translate(f, derivative = "forward")
fcpp_forward(y, x)

# Bubble sort (using [[ for scalars)
# ----------------------------------------------------------------
bubble <- function(a) {
  size <- length(a)
  for (i in 1:size) {
    for (j in 1:(size - 1)) {
      if (a[[j]] > a[[j + 1]]) {
        temp <- a[[j]]
        a[[j]] <- a[[j + 1]]
        a[[j + 1]] <- temp
      }
    }
  }
  return(a)
}
bubble_cpp <- ast2ast::translate(bubble)
bubble_cpp(runif(10))

# Fibonacci sequence
# ----------------------------------------------------------------
fib <- function(n = 10) {
  f <- integer(n)
  f[[1L]] <- 1L
  f[[2L]] <- 1L
  for (i in 3L:n) {
    f[i] <- f[i-1L] + f[i-2L]
  }
  return(f)
}
fib_cpp <- ast2ast::translate(fib)
fib_cpp(10)

# Custom types
# ----------------------------------------------------------------
types_f <- function() {
  new_type(Point, slots(x |> type(double), y |> type(double)))
}
f <- function(p) {
  argtypes(p |> type(Point))
  p$x <- p$x + 1
  return(p)
}
fcpp <- ast2ast::translate(f, types_f = types_f)
p <- structure(list(x = 1, y = 2), class = "Point")
fcpp(p)

# Inner functions and uniroot
# ----------------------------------------------------------------
f <- function(interval) {
  argtypes(interval |> type(vec(double)))
  g <- fn(
    argtypes(x |> type(double)),
    return(double),
    {
      return(x^2 - 4)
    }
  )
  res <- uniroot(g, interval, 1e-10, 1000)
  return(res$root)
}
fcpp <- ast2ast::translate(f)
fcpp(c(0, 10))

# External pointer interface
# ----------------------------------------------------------------
f <- function() {
  print("Hello World from C++")
}
ptr <- ast2ast::translate(f, output = "XPtr")

# Call from C++ side
Rcpp::sourceCpp(code = "
  #include <Rcpp.h>
  typedef void (*fp)();
  // [[Rcpp::export]]
  void call_fct(Rcpp::XPtr<fp> inp) {
    fp f = *inp;
    f();
  }")
call_fct(ptr)


## End(Not run)