MLISP calculator
| Human-friendly | Raw | Tree | Tree image | |
|---|---|---|---|---|
| i | ||||
| j | ||||
| k |
| Human-friendly | Raw | Tree | Tree image | |
|---|---|---|---|---|
| out |
Explanations
MLISP is a minimal yet Turing-complete programming language helpful for defining mathematical structures. A program is a string of single-character symbols, each acting in turn on a stack of strings representing binary trees, as when parsing Reverse Polish Notation. MLISP has only the 9 basic 1-symbol operations a b c d f 0 i j k defined above, and unused symbols are ignored, but you can define new programs: for example, +iib0jcfjd# defines + to mean iib0jcfjd. For example, the definitions below let you do math with natural numbers ℕ represented as nested pairs:0, 1={0,0}, 2={0,{0,0}}, 3={0,{0,{0,0}}}; inputs may be typed this way, the usual way, or as a bare bracket string read as a Dyck word ({}{}{} is 3: the atom is the empty string and the pair {x,y} is {x}y), or in the raw form, a string over 0 and c read in reverse Polish (0000ccc is 3: 0 pushes the atom and c pairs the two most recent entries). The raw form is the MLISP program that builds the value: MLISP data are bit strings with 1 written as c, and the Human-friendly, Tree and Tree-image boxes are three ways of reading the same string.
| Symbol | Interpretation | Args | Input types | Output type | Definition |
|---|---|---|---|---|---|
1 | one, true | 0 | 00c | ||
2 | two | 0 | 01c | ||
e | equal ({0,0} if i = j, else 0) | 2 | ijiajafibjbf0d0dj01dd | ||
↑ | successor | 1 | 0ic | ||
− | minus (undefined for i < j) | 2 | jibjbfid | ||
+ | plus | 2 | iib0jcfjd | ||
* | times | 2 | iibjfj+0d | ||
^ | power | 2 | jijbfi*1d | ||
! | factorial | 1 | iiibf*1d | ||
¬ | not | 1 | i01d | ||
& | and | 2 | ij10d0d | ||
∨ | or | 2 | i1j10dd | ||
< | less than (i<j) | 2 | jiibjbf1d0d | ||
% | remainder (i mod j) | 2 | ij<iij-jfd | ||
Δ | divisible (i by j) | 2 | ij%¬ | ||
P | i indivisible by all of 2…j | 2 | jbijΔ¬ijbf&1d | ||
Π | prime (i ≥ 2) | 1 | iibP |
Worked examples
The symbols are read left to right. A symbol with no arguments pushes a value on the stack; a symbol with n arguments takes the top n values off the stack and pushes its result. Below, the stack is shown after each symbol, with the inputs i = 3 and j = 1 (that is, i = {0,{0,{0,0}}} and j = {0,0}).
0ic (the successor ↑, one argument) | ||
|---|---|---|
| Symbol | Stack afterwards | Comment |
| 0 | 0 | push the atom |
| i | 0, 3 | push the first input |
| c | {0,3} | combine the top two into the pair {0,3}, which is the number 4 |
jibjbfid (minus, two arguments: i − j) | ||
|---|---|---|
| Symbol | Stack afterwards | Comment |
| j | 1 | push the second input |
| i | 1, 3 | push the first input |
| b | 1, 2 | b-part of 3 = {0,2} is 2: the predecessor of i |
| j | 1, 2, 1 | |
| b | 1, 2, 0 | the predecessor of j |
| f | 1, f(2,0) | the function itself applied to the top two values (a recursive call, not yet evaluated) |
| i | 1, f(2,0), 3 | |
| d | d(1, f(2,0), 3) | decide: the first of the three is 1, a list, so the result is the second, f(2,0); the third is never evaluated |
The recursive call f(2,0) runs the same program with i = 2 and j = 0: its stack ends as d(0, f(1,b(0)), 2), and since the first value is the atom 0, the result is the third, 2; the second, which would need b of 0, is never evaluated. So 3 − 1 = 2. Each call strips one from both arguments until j is 0, which is why the program is undefined when i runs out first: b of 0 has no value.
Examples you can try (click a row to load it into the calculator):
| Program | i | j | What happens |
|---|---|---|---|
| a | 1 | the first part of the input i (with i = 1 = {0,0} it gives 0) | |
| 0ic | 4 | the successor of i: 5 | |
| +iib0jcfjd# | defines the symbol + (nothing is run) | ||
| + | 2 | 3 | adds i and j, once + is defined: 5 |
| +iib0jcfjd#+ | 2 | 3 | the definition and the sum in one go: 5 |
| eijiajafibjbf0d0dj01dd#ije | 2 | 2 | defines e, equality: two values are equal if both are 0, or both are pairs whose parts are equal; then tests 2 = 2: 1 (true) |
| eijiajafibjbf0d0dj01dd#ije | 2 | 3 | the same test on 2 and 3: 0 (false); e is also loaded by the natural-number definitions, since several programs use it |
| * | 3 | 4 | 3 times 4 = 12 (after loading the natural number definitions) |
| ^ | 2 | 10 | 2 to the power 10 = 1024 |
| ! | 5 | 5 factorial = 120 | |
| Π | 7 | is 7 prime? 1 (true); try 9 | |
| - | 1 | 2 | 1 minus 2: undefined, since b of 0 has no value |
| ⊞ | 3 | -5 | integer addition: 3 + (-5) = -2 (after loading the integer & rational definitions; the pull-down switches to Integers) |
| ⊠ | 3 | -5 | integer multiplication: 3 × (-5) = -15 |
| ⊟ | 2 | 5 | integer subtraction: 2 - 5 = -3 |
| ⊕ | 1/2 | 1/3 | rational addition: 1/2 + 1/3 = 5/6 |
| ⊘ | 1/2 | 3/4 | rational division: (1/2) / (3/4) = 2/3 |
| ⊗ | -2/3 | 3/4 | rational multiplication: (-2/3) × (3/4) = -1/2 |
| ≼ | 1/2 | 1/3 | is 1/2 ≤ 1/3? 0 (false) |
Integers and rationals
The additional definitions below let you do math also with integers ℤ, i=m-n (represented as pairs of natural numbers {m,n}, one of which equals zero), and rational numbers ℚ, q=i/n (represented as the pair {i,n}):
| Symbol | Interpretation | Args | Input types | Output type | Definition |
|---|
MLISP is not a typed language: it simply operates on strings representing binary trees, which you get to interpret however you want. However, to make life easier for you, the calculator above converts between these trees and familiar human notation such as 5, -2 and 2/3 by guessing whether you are dealing with natural numbers, integers or rational numbers. You will need to ensure that you feed programs inputs of the correct type.
Turing completeness
MLISP is Turing-complete, as proven here in Lean. In fact, only six of the 9 operations (a c d f 0 i) are needed for Turing completeness: with them one can run a two-counter machine on a single number, for example 2 = iiaf0c0c0d doubles it and H = iiaiaaf0c0d0d halves it. b, j and k are included as an added convenience to make programs shorter. 0 is also redundant except for programs outputting constants.
More programs
Lists. The language has lists already: a list [x1, …, xn] is the nested pair {x1,{x2,…{xn,0}…}}, ending in the atom. So 0 is the empty list, c prepends an element, a is the head, b the tail, and d tests whether a list is nonempty. A natural number n is the list of n zeros, so numbers and lists are one notion, and the list [x,y,z] is written xyz0ccc. The applier u = iibjbfjad reads element i of a list, in the same postfix spirit as every other symbol: iUu applies the list U to the stack.
Finite structures. A finite structure presentation consists of sets S1 = {0, …, n1−1}, S2 = {0, …, n2−1}, … with functions defined by lookup tables, and a lookup table is a (potentially nested) list: a unary table U is the list of its values, applied as iUu; a binary table B is the list of its rows, applied as ijBo with o = jikuu (row i, then entry j); a constant is its value. The digits in the tables are the numerals of Table 1 (1 = 00c, 2 = 01c). So the presentation tableaus of the finite structures on the tree pages are human-friendly pictures of such programs, and a program that takes a table as an argument works for every structure of that kind at once, like the identity and inverse finders and the axiom checks below. Programs that need one element to start from take the element 0: every structure has one, and the user is free to number the elements as they please.
Integers. The last rows use the integer library (loaded automatically): N is negation, ⊞ addition and ⊠ multiplication, and each row first defines ↑ as the integer successor i00c0c⊞ (x + 1; this replaces the successor of Table 1 for the rest of the session, since a symbol has one definition at a time). The programs use these functions only as black boxes.
| Program | i | j | What happens |
|---|---|---|---|
| B0120ccc1200ccc2010ccc0ccc# | defines B, the multiplication table of the group C3 (addition mod 3) as a list of three rows [0,1,2], [1,2,0], [2,0,1]; nothing is run | ||
| B0120ccc1200ccc2010ccc0ccc# uiibjbfjad# ojikuu# ijBo | 1 | 2 | defines the appliers u (iUu is element i of the list U) and o (ijBo is row i, column j of the table B), then computes 1∘2 in C3: 0 |
| B1110ccc0110ccc0010ccc0ccc# uiibjbfjad# ojikuu# ijBo | 1 | 2 | a relation as a table: the total order ≤ on {0,1,2} is the {0,1}-valued table with rows [1,1,1], [0,1,1], [0,0,1] (row i, column j holds 1 iff i ≤ j); ijBo looks up 1 ≤ 2: 1 (and 2 1 gives 0). The C3 table two rows up is the group operation, not an order |
| U0210ccc# | defines U, a unary table: the inverses [0,2,1] of C3; nothing is run | ||
| U0210ccc# uiibjbfjad# iUu | 2 | applies the unary table U to i: the inverse of 2 is 1 | |
| uiibjbfjad#iju | 2 | {5,{7,{9,0}}} | lists in general: element number i (from 0) of the list j = [5,7,9]: 9 |
| uiibjbfjad# ojikuu# Kkijoiekijikjofd# 0j0K | {{0,{1,{2,0}}},{{1,{2,… | the identity of a finite group, found from its product table alone: the table is the input j (here C3), and the program runs through the powers of the element 0 until they return to 0; no element needs to be chosen by the user, and the same program works for every group table: 0 | |
| uiibjbfjad# ojikuu# Kkijoiekijikjofd# Jkijo0j0Kekijikjofd# ijiJ | 2 | {{0,{1,{2,0}}},{{1,{2,… | the inverse of i in a finite group given by its table j: the power of i whose product with i is the identity: 1 |
| ℓiibf↑0d# uiibjbfjad# ojikuu# ³kiℓe1jajbiokiojajbkioioeijk↑f0dd# ²kiℓe1ijkc0³ijk↑f0dd# αjiℓe1ij0²ij↑f0dd# j0α | {{0,{1,{0,0}}},{{1,{1,… | associative? Three nested loops over the indices of the table j compare (i∘j)∘k with i∘(j∘k); here j is the rock–paper–scissors table: 0 (no; the C3 table gives 1) | |
| ℓiibf↑0d# uiibjbfjad# ojikuu# χkiℓe1jkiokjioeijk↑f0dd# κjiℓe1ij0χij↑f0dd# j0κ | {{0,{1,{0,0}}},{{1,{1,… | commutative? Two loops compare i∘j with j∘i over the table j; rock–paper–scissors is commutative: 1 | |
| ↑i00c0c⊞#iN↑N | -3 | predecessor of the integer i as a term, −((−i)′), using only ↑ (integer successor) and N (negation): −4 | |
| ↑i00c0c⊞# Zjie1jNie0ij↑fdd# i00cZ00c↑00cd | -2 | is the integer i ≥ 0? Searches 0, 1, 2, … until j = i (true) or −j = i (false), and returns the integer 1 or 0: 0 | |
| ↑i00c0c⊞# ukj↑e0ikk⊠N⊞00ce1ijk↑fdd# vkj↑e0ikk⊠N⊞j00cu1ijk↑fdd# wkj↑e0ikk⊠N⊞j00cv1ijk↑fdd# xkj↑e0ikk⊠N⊞j00cw1ijk↑fdd# Fij00cx1iNj00cx0ij↑fdd# i00cF00c↑00cd | 5 | is i ≥ 0, decided as Lagrange's theorem says: is i or −i a sum of four squares? Uses only ↑, N, ⊞, ⊠, and returns the integer 1 or 0: 1 | |
| ↑i00c0c⊞# Zjie1jNie0ij↑fdd# Hj00ceij00cZijN↑Nf↑ij↑fN↑Ndd# ijH | -2 | 3 | integer addition computed from 0, ↑ and N alone, by recursion on the predecessor N↑N: 1 |
| uiibjbfjad# λi00c0iaibf0cccc00cd# εiaiabiabbiabbbiabbbbiabbbbbibakuibbλjkfkfibbajkfibbbajkfibbbbajkfddibbajkfbdibbajkfadibajudibbajkfibbbajkfcd0d# ijkε | {{0,{0,{0,{0,{0,0}}}}}… | {{0,{0,0}},{{0,{0,{0,0}}},0}} | an MLISP interpreter written in MLISP: ε evaluates a program given as a tree. i is the tree of + (iib0jcfjd; a node is {opcode, {extra, args}}: 0 = zero, 1 = c, 2 = argument, 3 = a, 4 = b, 5 = d, 6 = call of definition extra; there is no e, equality is a definition like any other), j the argument list [2,3], k the list of definition trees (just +): 5 |
| uiibjbfjad# τiibjbjakcfkjcd# ωibaibbjaccjbc# ψikuikuaj0τω# πiibiajkψkfjad# i0kπji0kπ0cε | {{0,0},{{0,0},{{0,{0,{… | {{0,{0,0}},{{0,{0,{0,0}}},0}} | a parser: π turns a program written as the list of its symbol codes (i = the codes of iib0jcfjd: 0 i j k a b c d f are 0–8, defined symbols 9, 10, …) into a tree, using a symbol table k of {arity, {opcode, extra}} entries; then ε runs the parsed + on j = [2,3]: 5 |
| ∀ijib0ckεibjkf&1d# ∃ijib0ckε1ibjkfd0d# ijk∀ | 3 | {{0,{0,{0,{0,{0,{0,0}}… | bounded quantifier. ijk∀ computes the truth value of (∀x < i) φ(x), where the predicate φ(x) ≡ x < 3 is given as the tree j (the tree of the program i0000ccc<, "x < 3") and k holds the trees of the definitions it calls (1 and <); the interpreter ε, loaded from its own row above, runs them. With i = 3 the proposition (∀x < 3)(x < 3) is true: 1. With i = 4, (∀x < 4)(x < 3) is false: 0. ijk∃ computes (∃x < i) φ(x) in the same way. |
| μji0ckεii↑jkfd# Ǝ0ijμ# ijƎ | {{0,{0,{0,{0,{0,{0,0}}… | {{{0,0},{0,{{0,{0,0}},… | ijƎ computes the truth value of ∃x ψ(x), where the predicate ψ(x) ≡ 3 < x is given as the tree i and j holds the trees of the definitions it calls, by searching for a witness: μ tries x = 0, 1, 2, … in turn and returns the least x with ψ(x). It halts with that witness exactly when the proposition is true (here x = 4, so ∃x (3 < x) holds) and runs forever when it is false; no program can do better for an unbounded ∃, which is why the answer is a witness rather than 0 or 1. |
| ≡ijiajafibjbf&0dj01dd# ↦jjaai≡jab0cijbfd0d# ⊔ik↦ik↦aj≡k0c0dijckc0cd# ≈iaiabjajabibajbakfibbjbbibajbakfaf0d0d0djajab0ibjbkfd0ddibjk⊔d# ⊑jjai0≈1ijbfd0d# ∈jjai≡1ijbfd0d# ⇒j01c00cjacicck∈1ijbkfd0d# ✓jjak⊑jai⊑∨jaii⇒∨jaicjbkf0d1d# Ωibibfiad# ⊢0jk✓jΩi≡&# ijk⊢ | {{0,{0,0}},{{{0,0},{0,… | {{{0,{0,0}},{{{0,0},{0… | a proof checker for the Propositional Calculus of the tree: WFFs are trees (variable n = {0,n}, [¬S] = {1,S}, [S∨T] = {2,{S,T}}, S⇒T abbreviates [[¬S]∨T]); k is the formal system, the list of its four axioms; j a proof, the five-line Principia proof of x⇒x (x a variable); ijk⊢ checks that every line is a substitution instance of an axiom or of an earlier line or follows by modus ponens, and that the last line is i: 1 (change i, drop a line, or end the proof with x⇒y and it says 0) |
Reference implementations
The whole language fits in a short file, in each of three languages. None of them contains any of the code for reading and writing human-friendly numbers: values go in and out as raw strings over 0 and c. All three give the same results as this page on every example above, including the interpreter and the parser.
- mlisp.py — Python, 84 lines: the reference interpreter. Run it from a terminal as
python3 mlisp.py -t 'ij+' 0000ccc 00000cccc(the-tloads Table 1), or import it and callrun(program, [value(s) for s in inputs]). - mlisp.c — the same algorithm in C, about 80 times faster:
cc -O2 -o mlisp mlisp.c, then./mlisp '+iib0jcfjd#ij+' 0000ccc 00000ccccprintsRESULT 00000000ccccccc;-n 1000repeats the run for timing, and-7switches to the seven-symbol mLISP of the other calculator. - mlisp.lean — the Lean 4 formalization (the file MathTree/MLISP.lean of the MathTree library, which builds on Mathlib): values, the parser, the big-step semantics proved deterministic, an executable evaluator proved sound, and theorems that every program of Table 1 and of the integer and rational library computes what it claims, for all inputs.
How i j k interact with the stack. There is no stack at run time. The program is parsed once, left to right, with a stack of parse trees (each symbol pops as many trees as its arity and pushes one), and the resulting tree is evaluated with i, j, k bound to the arguments of the function being evaluated; reading i twice costs nothing, and a call of a defined symbol or of f rebinds them to its own arguments. The equivalent stack picture: a program consumes as many entries as its arity (3, 2, 1 or 0 according to whether it mentions k, j or i), with i the deepest and k the top, and leaves one result in their place. A plain reverse-Polish evaluation on a value stack would not do, because d evaluates only the chosen branch and f must not be expanded before d decides.