Odrzywołek (2026), §4.1, observes that EML expressions map naturally onto a single-instruction stack machine: every node is the same operation. emlR implements that machine literally — three opcodes, a constants pool, a preallocated complex stack — for vectorised evaluation in the inner loop of symbolic regression.
The instruction set
Three opcodes (SPEC §4):
| Opcode | Mnemonic | Stack effect | Argument |
|---|---|---|---|
| 0 | LIT |
… → …, v |
constants-pool index |
| 1 | VAR |
… → …, x_j |
variable-table index |
| 2 | EML |
…, a, b → …, eml(a, b) |
none |
The compiler compile_eml() does a post-order tree walk,
emitting one opcode per node: LIT for a numeric/complex
literal, VAR for a name, EML for an interior
eml(l, r) call.
The K = 7 example
The paper’s headline expression is
log(x) = eml(1, eml(eml(1, x), 1)). Compiling it:
log_expr <- quote(eml(1, eml(eml(1, x), 1)))
bc <- compile_eml(log_expr)
str(bc, max.level = 1)
#> List of 6
#> $ ops : int [1:7] 0 0 1 2 0 2 2
#> $ args : int [1:7] 1 1 1 0 1 0 0
#> $ consts : cplx 1+0i
#> $ vars : chr "x"
#> $ max_stack: int 3
#> $ source : language eml(1, eml(eml(1, x), 1))Seven instructions — matching K = 7 from the paper’s Table 4 — with opcodes:
bc$ops
#> [1] 0 0 1 2 0 2 2
# 0=LIT, 1=VAR, 2=EML → LIT, LIT, VAR, EML, LIT, EML, EML
bc$args
#> [1] 1 1 1 0 1 0 0
# constant-pool / variable-table indices, 0 for EMLReading the stream
LIT(1) LIT(1) VAR(x) EML LIT(1) EML EML:
| Step | Op | Stack after |
|---|---|---|
| 1 | LIT(1) |
[1] |
| 2 | LIT(1) |
[1, 1] |
| 3 | VAR(x) |
[1, 1, x] |
| 4 | EML |
[1, eml(1, x)] |
| 5 | LIT(1) |
[1, eml(1, x), 1] |
| 6 | EML |
[1, eml(eml(1, x), 1)] |
| 7 | EML |
[eml(1, eml(eml(1, x), 1))] |
The constants pool is deduplicated: a tree with five 1s
holds a single 1+0i and points every LIT to
index 1.
bc$consts
#> [1] 1+0i
bc$vars
#> [1] "x"
bc$max_stack # = depth + 1 for right-deep trees
#> [1] 3This is the exact RPN serialisation the paper writes as
1 1 x E 1 E E (with E denoting the
eml operator after its operands) — emlR’s
eml_rpn() reproduces the human-readable form:
eml_rpn(log_expr)
#> [1] "1 1 x E 1 E E"The evaluator
run_bytecode() is one tight loop over the opcode stream,
with a preallocated complex stack:
xs <- seq(0.5, 5, length.out = 50)
out <- run_bytecode(bc, list(x = xs))
max(abs(Re(out) - log(xs)))
#> [1] 2.220446e-16The stack slots are list cells of complex vectors, not
single complex scalars — when vars$x is length N, every
slot carries a length-N vector and every EML step is one
vectorised exp(left) - log(right). R’s BLAS-friendly
exp and log do the heavy lifting; the loop is
lean.
Equivalence with eml_eval()
The bytecode evaluator is by design semantically identical to
eml_eval(). The package’s test suite asserts equivalence on
100 random EML trees of depths 1–5 over random complex input
vectors:
random_eml_tree <- function(max_depth, var_names = c("x", "y")) {
if (max_depth == 0 || runif(1) < 0.4) {
if (runif(1) < 0.5) {
sample(c(1, 1, 1, 0.5, 2), 1)
} else {
as.name(sample(var_names, 1))
}
} else {
call(
"eml",
random_eml_tree(max_depth - 1, var_names),
random_eml_tree(max_depth - 1, var_names)
)
}
}
set.seed(2026)
agree <- TRUE
for (i in seq_len(20)) {
expr <- random_eml_tree(sample.int(5, 1))
vars <- list(x = runif(50, 0.1, 5), y = runif(50, 0.1, 5))
v1 <- eml_eval(expr, vars)
v2 <- run_bytecode(compile_eml(expr), vars)
if (!isTRUE(all.equal(v1, v2, tolerance = 1e-10))) {
agree <- FALSE
break
}
}
agree
#> [1] TRUEWhen to use which evaluator
-
eml_eval()— for one-off evaluation, debugging, or working with expressions that may not be pure-EML (simplify_native()output, for example, contains base-R primitives). -
compile_eml()+run_bytecode()— for the inner loop of an optimisation, batch evaluation over many input points, or any setting where the same EML expression is evaluated many times. Compile once, evaluate many times.
eml_fit() uses bytecode internally for its forward-pass
evaluation; gradients come from Deriv::Deriv on the
eml-expanded master expression.
References
Odrzywołek, A. (2026). All elementary functions from a single binary operator. arXiv:2603.21852, §4.1 (“EML compiler”).
