Skip to content

Worked examples

These examples are drawn from Octant's reference library. Each reference is a pair: a LaTeX file and a hand-written Chelis implementation that the translation of that LaTeX is expected to match. The pairs are both the onboarding material and the correctness oracle for the translator. The LaTeX and the Chelis shown below are the actual reference files, quoted verbatim.

Each example below shows the LaTeX source, the hand-written Chelis the reference pairs it with, and what the translation exercises. To translate any of them yourself:

Terminal window
octant translate references/<name>.tex --output /tmp/<name>.dp

The hero example. The call price as a single equation.

LaTeX (references/black_scholes_call.tex):

% chelis: s : f32
% chelis: k : f32
% chelis: r : f32
% chelis: t : f32
% chelis: d_1 : f32
% chelis: d_2 : f32
% chelis: c : f32
c = s \Phi(d_1) - k e^{-r t} \Phi(d_2)

Hand-written Chelis (references/black_scholes_call.ch):

import Nautilus.Special (normal_cdf)
def c(s: f32, k: f32, r: f32, t: f32, d_1: f32, d_2: f32, e: f32) -> f32 =
sub(mul(s, normal_cdf(d_1)),
mul(mul(k, exp(mul(log(e), mul(sub(0, r), t)))), normal_cdf(d_2)))

This exercises the \Phi mapping, implicit multiplication by juxtaposition (s \Phi(d_1)), subtraction at the top level, and the discount factor e^{-r t}. Note how e^{-r t} lowers: the exponent is not a recognized special-case shape, so it routes through the general power identity (exp (mul (log e) (mul (sub 0 r) t))). The unary minus on r becomes (sub 0 r).

The d_1 input to the call price. A larger nested expression.

LaTeX (references/black_scholes_d1.tex):

% chelis: s : f32
% chelis: k : f32
% chelis: r : f32
% chelis: t : f32
% chelis: sigma : f32
% chelis: d : f32
d = \frac{\log(s / k) + \left(r + \frac{\sigma^2}{2}\right) t}{\sigma \sqrt{t}}

Hand-written Chelis (references/black_scholes_d1.ch):

def d(s: f32, k: f32, r: f32, t: f32, sigma: f32) -> f32 =
div(add(log(div(s, k)), mul(add(r, div(mul(sigma, sigma), 2)), t)),
mul(sigma, sqrt(t)))

This exercises nested fractions, \log with a parenthesized argument, the \sigma^2 squaring (which lowers to mul(sigma, sigma)), and the sized \left( ... \right) grouping.

A small formula that shows the general power lowering on its own.

LaTeX (references/discount_factor.tex):

% chelis: r : f32
% chelis: t : f32
% chelis: d : f32
d = e^{-r t}

Hand-written Chelis (references/discount_factor.ch):

def d(r: f32, t: f32, e: f32) -> f32 = exp(mul(log(e), mul(sub(0, r), t)))

e^{-r t} has an exponent (-r t) that is not one of the special-case shapes, so it lowers through the general power identity. The result uses only exp, log, mul, and sub.

A present-value sum over a stream of cashflows. This exercises the \sum reduction.

LaTeX (references/present_value.tex):

% chelis: n : i32
% chelis: r : f32
% chelis: c : tensor[n, f32]
% chelis: t : tensor[n, f32]
% chelis: v : f32
v = \sum_{i=1}^{n} c_i e^{-r t_i}

Hand-written Chelis (references/present_value.ch):

def v(n: i32, r: f32, c_i: f32, t_i: f32, e: f32, i: i32) -> f32 =
reduce_sum(i, 1, n, mul(c_i, exp(mul(log(e), mul(sub(0, r), t_i)))))

The bounded sum \sum_{i=1}^{n} lowers to reduce_sum(index, lower, upper, body). The subscripted identifiers c_i and t_i flatten to single names. The tensor annotations declare c and t with a named dim n.

The i-th component of a softmax, a \sum inside a \frac.

LaTeX (references/softmax.tex):

% chelis: n : i32
% chelis: x_i : f32
% chelis: x_j : f32
% chelis: y : f32
y = \frac{\exp(x_i)}{\sum_{j=1}^{n} \exp(x_j)}

Hand-written Chelis (references/softmax.ch):

def y(n: i32, x_i: f32, x_j: f32, j: i32) -> f32 =
div(exp(x_i), reduce_sum(j, 1, n, exp(x_j)))

The fraction lowers to div; the denominator is a reduce_sum whose body is exp(x_j).

The positive branch of the quadratic formula. Shows unary minus, a nested square root, and integer-literal coefficients.

LaTeX (references/quadratic_root.tex):

% chelis: a : f32
% chelis: b : f32
% chelis: c : f32
% chelis: x : f32
x = \frac{-b + \sqrt{b^2 - 4 a c}}{2 a}

Hand-written Chelis (references/quadratic_root.ch):

def x(a: f32, b: f32, c: f32) -> f32 =
div(add(sub(0, b), sqrt(sub(mul(b, b), mul(mul(4, a), c)))), mul(2, a))

The leading -b lowers to sub(0, b). b^2 lowers to mul(b, b). 4 a c is implicit multiplication, left-associated as mul(mul(4, a), c).

A small geometry formula with a literal fraction, \pi, and a cube.

LaTeX (references/sphere_volume.tex):

% chelis: r : f32
% chelis: v : f32
v = \frac{4}{3} \pi r^3

Hand-written Chelis (references/sphere_volume.ch):

def v(r: f32, pi: f32) -> f32 = mul(mul(div(4, 3), pi), mul(r, mul(r, r)))

\pi parses as an identifier and is declared like any other free variable. r^3 lowers to mul(r, mul(r, r)) per the power table.

Compound interest with a non-trivial exponent.

LaTeX (references/compound_interest.tex):

% chelis: p : f32
% chelis: r : f32
% chelis: n : f32
% chelis: t : f32
% chelis: a : f32
a = p \left(1 + \frac{r}{n}\right)^{n t}

Hand-written Chelis (references/compound_interest.ch):

def a(p: f32, r: f32, n: f32, t: f32) -> f32 =
mul(p, exp(mul(log(add(1, div(r, n))), mul(n, t))))

The base is the grouped expression 1 + r/n; the exponent n t is not a special-case shape, so the power lowers through the general identity exp(mul(log(base), exponent)).

A path-end value under geometric Brownian motion. A larger composite expression.

LaTeX (references/gbm_path.tex):

% chelis: s_0 : f32
% chelis: r : f32
% chelis: sigma : f32
% chelis: t : f32
% chelis: z : f32
% chelis: s : f32
s = s_0 \exp\left(\left(r - \frac{\sigma^2}{2}\right) t + \sigma \sqrt{t} z\right)

Hand-written Chelis (references/gbm_path.ch):

def s(s_0: f32, r: f32, sigma: f32, t: f32, z: f32) -> f32 =
mul(s_0, exp(add(mul(sub(r, div(mul(sigma, sigma), 2)), t),
mul(mul(sigma, sqrt(t)), z))))

This composes nested grouping, \exp with a parenthesized argument, \sigma^2 squaring, a square root, and implicit multiplication.

The single-equation hero example also has a multi-equation companion that factors out the d_1 and d_2 helpers as their own equations. Pairing it with --wrap-as-function produces one function definition whose preceding equations become let bindings.

LaTeX (references/black_scholes_call_function.tex, the equation block):

\begin{equation}\label{eq:d1}
d_1 = \frac{\log(s / k) + \left(r + \frac{\sigma^2}{2}\right) t}{\sigma \sqrt{t}}
\end{equation}
\begin{equation}\label{eq:d2}
d_2 = d_1 - \sigma \sqrt{t}
\end{equation}
\begin{equation}\label{eq:c}
c = s \Phi(d_1) - k e^{-r t} \Phi(d_2)
\end{equation}

Translate it as a single function:

Terminal window
octant translate references/black_scholes_call_function.tex \
--wrap-as-function call_price \
--output call_price.dp

The \label{...} on each equation flows into the span identifiers (eq:d1_..., eq:d2_..., eq:c_...), so a node anywhere in the block can be traced to the equation it came from.