This site is version 0.0.1. Most of the code on this site is drafted by an agent and needs review. I apologize for the AI-speak, we are working on rewriting it!
viv-lang.org

Lambdas & higher-order builtins

fn values, quadrature, and exact differentiation. Run a cell to see its output beside the expected one.

fn is a value

Viv [1]

Quadrature

Integral of x² over [0,1]; exact answer 1/3.

Viv [2]

Exact derivatives

deriv uses forward-mode autodiff, so the slope is exact to rounding: d/dx[x sin x] at 2 against the product rule.

Viv [3]

deriv knows log

d/dx log(x) at 3 is 1/3.

Viv [4]

Two-argument lambdas

Viv [5]

simpson accepts an odd panel count

Simpson's rule needs an even number of panels, so simpson rounds an odd n up to the next even number: 7 panels give exactly the 8-panel answer. The integrand is exp, which Simpson's rule does not integrate exactly, so the two would differ otherwise.

Viv [6]

deriv composes through the chain rule

d/dx exp(sin x) at 1 = exp(sin 1) · cos 1.

Viv [7]

The functions deriv can differentiate

Besides + - * and /, deriv knows exp, sqrt, pow, sin, cos, log and tan. The last line uses the quotient rule.

Viv [8]

deriv wants a constant exponent

pow(x, c) differentiates only for literal c.

Viv [9]

expected error: constant exponent…

Quadrature error shrinks with panels

For the integral of sin on [0, pi], exactly 2, Simpson's error falls from 0.0046 with 4 panels to 6.5e-8 with 64.

Viv [10]

An array of functions

An array holds numbers, or it holds functions. A function array can be written as a literal, indexed, passed to len, and passed to a def. That is enough to keep a list of methods and pick one while the program runs: because the index is not known in advance, the compiled call goes through a function pointer instead of being fixed at compile time.

Viv [11]

Methods take the function they work on

Each method's first argument is the function it works on, so one program can hand the same problem to several methods and compare their answers. Here spread returns the gap between the largest and smallest answer.

Viv [12]

Every function in an array takes the same arguments

Every function in one array must have the same C signature: the same number of arguments, in the same positions. Mixing them is an error at the line that builds the array. The interpreter refuses it too, so a program cannot pass in the browser and then fail when compiled to C.

Viv [13]

expected error: the same number of arguments…