mLISP calculator
The seven-symbol language 0 a b c d e f: a sublanguage of the nine-symbol MLISP of the MLISP calculator, with a single argument e in place of i j k.
| Human-friendly | Raw | Tree | Tree image | |
|---|---|---|---|---|
| i | ||||
| j | ||||
| k | ||||
| e |
| Human-friendly | Raw | Tree | Tree image | |
|---|---|---|---|---|
| out |
Explanations
mLISP is a minimal yet Turing-complete programming language with only seven symbols, 0 a b c d e f. Its values are binary trees whose leaves are all the same atom 0: a value is either 0 or a pair {x,y} of values. A program is a string of these symbols (plus the names of earlier definitions) read as Reverse Polish Notation: each symbol pops as many values as it needs from a stack and pushes its result, and a program computes one value from one input value, its argument e.
| Operation | What it returns | Args |
|---|---|---|
| a | a-part: the first part of the pair x (undefined if x is the atom) | 1 |
| b | b-part: the second part of the pair x (undefined if x is the atom) | 1 |
| c | combine: the pair {x,y} | 2 |
| d | decide: y if x is a pair, otherwise z (only the chosen branch is evaluated) | 3 |
| e | extract: the input of the function being evaluated | 0 |
| f | function being defined: for recursion, called with x as its input | 1 |
| 0 | the atom 0 | 0 |
In the middle column, x, y and z stand for the operation's own first, second and third argument, which it takes from the stack.
You can define new symbols: X⟨program⟩# gives the name X (any character other than the seven symbols and the three macro letters below) to the program; for example ↑0ec# defines the successor. A defined symbol pops one value, which becomes the e of its body, if its body mentions e or f, and is a closed constant otherwise. Inside its own body a definition calls itself with f; it can call any earlier definition by name, but no later one. Unused symbols are ignored (with a warning).
Several arguments. A function of two arguments takes the single pair {x,y} (so a call looks like xyc+), a function of three the tuple {x,{y,z}}. To keep programs readable, the accessor macros i, j, k stand for the parts of that tuple: in a program that mentions k, i = ea, j = eba, k = ebb; in one that mentions i or j but not k, i = ea and j = eb. They are notation, not language: the calculator replaces them textually before it parses anything, and the “Definitions so far” box shows each definition both as written and expanded to the seven symbols. With them, a two-argument MLISP program survives almost verbatim, except that the packing c’s appear before every call: MLISP’s + = iib0jcfjd becomes iib0jccfjd.
How the inputs become e. The rows i, j, k above are the components of the argument, and the calculator packs them into e, shown in the last row: a program written with the macros fixes the shape (k mentioned: e = {i,{j,k}}; only i or j: e = {i,j}); a program written with e itself gets the filled rows as they are (i, {i,j} or {i,{j,k}}); a bare defined symbol such as + or Π gets the shape its definition expects, and is run as e+ or eΠ. 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 | Expanded |
|---|---|---|---|---|---|---|
1 | one, true | 0 | 00c | 00c | ||
= | equal ({0,0} if i = j, else 0) | 2 | ijiajacfibjbcf0d0dj01dd | eaebeaaebacfeabebbcf0d0deb01dd | ||
2 | two | 0 | 01c | 01c | ||
↑ | successor | 1 | 0ec | 0ec | ||
- | minus (undefined for i < j) | 2 | jibjbcfid | ebeabebbcfead | ||
+ | plus | 2 | iib0jccfjd | eaeab0ebccfebd | ||
* | times | 2 | iibjcfjc+0d | eaeabebcfebc+0d | ||
^ | power | 2 | jijbcfic*1d | ebeaebbcfeac*1d | ||
! | factorial | 1 | eeebfc*1d | eeebfc*1d | ||
¬ | not | 1 | e01d | e01d | ||
& | and | 2 | ij10d0d | eaeb10d0d | ||
∨ | or | 2 | i1j10dd | ea1eb10dd | ||
< | less than (i<j) | 2 | jiibjbcf1d0d | ebeaeabebbcf1d0d | ||
% | remainder (i mod j) | 2 | ijc<iijc-jcfd | eaebc<eaeaebc-ebcfd | ||
Δ | divisible (i by j) | 2 | ijc%¬ | eaebc%¬ | ||
P | i indivisible by all of 2…j | 2 | jbijcΔ¬ijbcfc&1d | ebbeaebcΔ¬eaebbcfc&1d | ||
Π | prime (i ≥ 2) | 1 | eebcP | eebcP |
Worked examples
The symbols are read left to right. A symbol with no arguments pushes a value on the stack; a symbol that pops n values takes the top n off the stack and pushes its result. Below, the stack is shown after each symbol, with the argument e = {3,1}, that is, the inputs i = 3 and j = 1 packed into one pair (e = {{0,{0,{0,0}}},{0,0}}).
0ec (the successor ↑, one argument: here run with e = i = 3) | ||
|---|---|---|
| Symbol | Stack afterwards | Comment |
| 0 | 0 | push the atom |
| e | 0, 3 | push the argument |
| c | {0,3} | combine the top two into the pair {0,3}, which is the number 4 |
jibjbcfid (minus, i − j on the pair e = {i,j}; expanded: ebeabebbcfead) | ||
|---|---|---|
| Symbol | Stack afterwards | Comment |
| j | 1 | push the second part of the argument (j stands for eb) |
| i | 1, 3 | push the first part (i stands for ea) |
| 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 |
| c | 1, {2,0} | pack the two values into the pair the recursive call will take as its argument |
| f | 1, f({2,0}) | the function itself applied to the top value (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 pair, 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 e = {2,0}, that is 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. The only difference from the MLISP version jibjbfid is the c that packs the two values before f, since f takes one argument.
Examples you can try (click a row to load it into the calculator; the needed definitions are loaded with it):
| Program | i | j | What happens |
|---|---|---|---|
| a | 1 | the first part of the argument (the program runs as ea on e = i) (with i = 1 = {0,0} it gives 0) | |
| 0ec | 4 | the successor of i: 5 | |
| +iib0jccfjd# | defines the symbol + (nothing is run) | ||
| + | 2 | 3 | adds i and j (the program runs as e+ on the pair e = {2,3}), once + is defined: 5 |
| +iib0jccfjd# + | 2 | 3 | the definition and the sum in one go: 5 |
| =ijiajacfibjbcf0d0dj01dd# ijc= | 2 | 2 | defines =, equality (the MLISP program e, renamed because e is the argument symbol of mLISP): two values are equal if both are 0, or both are pairs whose parts are equal; then tests 2 = 2: 1 (true) |
| =ijiajacfibjbcf0d0dj01dd# ijc= | 2 | 3 | the same test on 2 and 3: 0 (false); = 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}). They are the MLISP library of the other calculator translated to mLISP, each definition verified against its original; the Definition column is in macro notation:
| 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; note in particular that a pair of naturals {3,4} is also what an integer looks like, so the e row is displayed component by component.
Relation to MLISP, and Turing completeness
mLISP is exactly the fragment of the nine-symbol MLISP whose programs take at most one argument, with that argument spelled e instead of i: renaming e to i turns any mLISP program into an MLISP program with identical evaluation, so the Lean formalization of MLISP (its big-step semantics, proved deterministic, and its evaluator, proved sound) applies verbatim, and MLISP's Turing completeness, proven here in Lean, carries over. In fact only six of the seven symbols (a c d f 0 e) are needed for Turing completeness: with them one can run a two-counter machine on a single number, for example 2 = eeaf0c0c0d doubles it and H = eeaeaaf0c0d0d halves it. b is included as an added convenience to make programs shorter, and 0 is also redundant except for programs outputting constants. Conversely, every MLISP program becomes an mLISP program by packing its arguments into a tuple, which is how the tables of this page were produced.
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 = iibjbcfjad reads element i of a list j, in the same postfix spirit as every other symbol: iUcu applies the list U to i.
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 iUcu; a binary table B is the list of its rows, applied as ijBcco with o = jikcucu (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 e00c0cc⊞ (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.
The MLISP tree programs. The interpreter ε, the parser π and the quantifiers below are the MLISP programs of the other calculator translated to mLISP; the programs they interpret are still given as trees of MLISP programs (with an argument list), exactly as there, so the inputs are unchanged.
| 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# uiibjbcfjad# ojikcucu# ijBcco | 1 | 2 | defines the appliers u (iUcu is element i of the list U) and o (ijBcco is row i, column j of the table B), then computes 1∘2 in C3: 0 |
| B1110ccc0110ccc0010ccc0ccc# uiibjbcfjad# ojikcucu# ijBcco | 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); ijBcco 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# uiibjbcfjad# eUcu | 2 | applies the unary table U to i: the inverse of 2 is 1 | |
| uiibjbcfjad# ijcu | 2 | {5,{7,{9,0}}} | lists in general: element number i (from 0) of the list j = [5,7,9]: 9 |
| uiibjbcfjad# ojikcucu# Kkijccoic=kijikjccoccfd# 0j0ccK | {{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 | |
| uiibjbcfjad# ojikcucu# Kkijccoic=kijikjccoccfd# Jkijcco0j0ccKc=kijikjccoccfd# ijiccJ | 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 |
| ℓeebf↑0d# uiibjbcfjad# ojikcucu# ³kiℓc=1jajbiccokiccojajbkiccoiccoc=ijk↑ccf0dd# ²kiℓc=1ijkc0cc³ijk↑ccf0dd# αjiℓc=1ij0cc²ij↑cf0dd# j0cα | {{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) | |
| ℓeebf↑0d# uiibjbcfjad# ojikcucu# χkiℓc=1jkiccokjiccoc=ijk↑ccf0dd# κjiℓc=1ij0ccχij↑cf0dd# j0cκ | {{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 | |
| ↑e00c0cc⊞# eN↑N | -3 | predecessor of the integer i as a term, −((−i)′), using only ↑ (integer successor) and N (negation): −4 | |
| ↑e00c0cc⊞# Zjic=1jNic=0ij↑cfdd# e00ccZ00c↑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 | |
| ↑e00c0cc⊞# ukj↑c=0ikkc⊠Nc⊞00cc=1ijk↑ccfdd# vkj↑c=0ikkc⊠Nc⊞j00cccu1ijk↑ccfdd# wkj↑c=0ikkc⊠Nc⊞j00cccv1ijk↑ccfdd# xkj↑c=0ikkc⊠Nc⊞j00cccw1ijk↑ccfdd# Fij00cccx1iNj00cccx0ij↑cfdd# e00ccF00c↑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 | |
| ↑e00c0cc⊞# Zjic=1jNic=0ij↑cfdd# Hj00cc=ij00ccZijN↑Ncf↑ij↑cfN↑Ndd# ijcH | -2 | 3 | integer addition computed from 0, ↑ and N alone, by recursion on the predecessor N↑N: 1 |
| uiibjbcfjad# λe00c0eaebf0cccc00cd# εiaiabiabbiabbbiabbbbiabbbbbibakcuibbλjkccfkccfibbajkccfibbbajkccfibbbbajkccfddibbajkccfbdibbajkccfadibajcudibbajkccfibbbajkccfcd0d# ijkccε | {{0,{0,{0,{0,{0,0}}}}}… | {{0,{0,0}},{{0,{0,{0,0}}},0}} | an interpreter for MLISP program trees, written in mLISP: ε evaluates a program given as a tree. i is the tree of the MLISP program + = iib0jcfjd (the nine-symbol original, with the arguments i, j as a list; 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 equality opcode, equality is a definition like any other), j the argument list [2,3], k the list of definition trees (just +): 5 |
| uiibjbcfjad# τiibjbjakcccfkjcd# ωibaibbjaccjbc# ψikcuikcuaj0ccτcω# πiibiajkccψkccfjad# i0kccπji0kccπ0cccε | {{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 the MLISP program 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 |
| ∀ijib0ckccεibjkccfc&1d# ∃ijib0ckccε1ibjkccfd0d# ijkcc∀ | 3 | {{0,{0,{0,{0,{0,{0,0}}… | bounded quantifier. ijkcc∀ computes the truth value of (∀x < i) φ(x), where the predicate φ(x) ≡ x < 3 is given as the tree j (the tree of the MLISP 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. ijkcc∃ computes (∃x < i) φ(x) in the same way. |
| μji0ckccεii↑jkccfd# Ǝ0ijccμ# ijcƎ | {{0,{0,{0,{0,{0,{0,0}}… | {{{0,0},{0,{{0,{0,0}},… | ijcƎ 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. |
| ≡ijiajacfibjbcfc&0dj01dd# ↦jjaaic≡jab0cijbcfd0d# ⊔ikc↦ikc↦ajc≡k0c0dijckc0cd# ≈iaiabjajabibajbakccfibbjbbibajbakccfaccf0d0d0djajab0ibjbkccfd0ddibjkcc⊔d# ⊑jjai0cc≈1ijbcfd0d# ∈jjaic≡1ijbcfd0d# ⇒j01c00cjacicckc∈1ijbkccfd0d# ✓jjakc⊑jaic⊑c∨jaiicc⇒c∨jaicjbkccf0d1d# Ωebebfead# ⊢0jkcc✓jΩic≡c&# ijkcc⊢ | {{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); ijkcc⊢ 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; the macro expansion is four lines. Values go in and out as raw strings over 0 and c, and the inputs are packed into the one argument e as on this page. Both interpreters give the same results as this page on every example above.
- mlisp7.py — Python:
python3 mlisp7.py -t 'ijc+' 0000ccc 00000cccc(the-tloads Table 1), or import it and callrun(program, [value(s) for s in inputs]). - mlisp7.c — C, about 80 times faster:
cc -O2 -o mlisp7 mlisp7.c, then./mlisp7 '+iib0jccfjd#+' 0000ccc 00000ccccprintsRESULT 00000000ccccccc. It is mlisp.c, the MLISP interpreter, with its mLISP mode switched on by default. - mlisp.lean — the Lean 4 formalization of MLISP (the file MathTree/MLISP.lean of the MathTree library). mLISP is the one-argument fragment of MLISP with the argument spelled
einstead ofi, so after that renaming its programs are MLISP programs with the same evaluation, and the file's semantics, determinism and soundness theorems apply to them verbatim.
How e interacts 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 it needs and pushes one), and the resulting tree is evaluated with e bound to the argument of the function being evaluated; reading e twice costs nothing, and a call of a defined symbol or of f rebinds it to the one value that call popped. The equivalent stack picture: a program consumes one entry, its argument, and leaves one result in its place; several arguments travel as one tuple, which is what the macros i, j, k unpack. 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.