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

Examples of every construct

The language construct by construct. You can run and edit every snippet on these pages, and a test checks that each one still prints the output shown: the docs and the tests are the same 157 examples.

Numbers & arithmetic

Numbers are 64-bit floats, Int is a 64-bit integer, and Bool holds truth values.

16 examples

Booleans

true and false are their own type: what a comparison yields, what if and while take, and a value you can store, pass and return.

8 examples

Control flow

if/else, while, the counted for loop, and the loop-control statements.

11 examples

Functions

def, return, docstrings, recursion, and optional type annotations.

10 examples

Arrays

Literals, zeros, indexing, elementwise arithmetic, and passing arrays through functions.

17 examples

Complex numbers

A complex number type: the `i` suffix on a number makes it imaginary, arithmetic mixes complex and real values, and every engine (the interpreter, C and wasm) computes the complex functions with the same formulas, so they print the same digits.

4 examples

Complex arrays & the FFT

Packed complex arrays, and a Fourier transform that works at every length and gives identical output in every engine.

8 examples

Linear algebra

The matrix product, transpose, and the symmetric eigensolver.

9 examples

Lambdas & higher-order builtins

fn values, quadrature, and exact differentiation.

13 examples

Scope & mutation

Where names live, what functions can see, and which values are shared.

6 examples

Prose in programs

Comments, docstrings, narration, and annotation markers.

5 examples

Plotting

plot prints a structured record, and the page draws the chart from it.

7 examples

Animation

An animation is a Viv program: it creates named objects, and transitions reveal, morph and move them. The program prints records; the page plays them back with a scrubber.

4 examples

Testing

test blocks and the assert family: a program checks its own results and prints which tests passed and which failed.

3 examples

Errors, exactly as the compiler says them

The error messages are part of the language. Each one below is what the compiler prints, and the site's tests fail if a message changes.

16 examples

Gotchas

Float facts and Viv-specific surprises, each shown by a program that runs.

6 examples

Stencils and PDE steps

A finite-difference stencil is arithmetic on shifted copies of an array, and an implicit time step is a tridiagonal solve. These are the pieces the PDE solvers are built from.

7 examples

Small algorithms

Classic numerical algorithms, each a few lines long.

7 examples

Two topics to read first

Errors — every error message on that page is the compiler's actual message, checked by the doctest suite, so a change to any of them fails a test.

Scope & mutation — where names live and which values are shared. Assignment and parameter passing alias arrays, so b = a shares storage and a function that writes into an array parameter writes into the caller's array. It is the rule most likely to surprise you if you guess.

Python and Viv, side by side — small programs written in both languages, with the same output, lined up region by region, for a reader who knows Python.