Functoid
Befunge's grid with lambda calculus inside. There is no stack; the whole state is one lambda term, and every command applies the term to something.
BMO, 2018 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 6 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
A Functoid program is a grid that an instruction pointer walks over, turning at arrows and wrapping at the edges. What it carries is a single lambda term, at first the identity. A digit, a combinator letter or an arithmetic sign is a term too, and stepping on one applies the current term to it. So +23 builds plus 2 3, and . prints it as the number 5.
Read the story
There are no numbers underneath. 2 is the Church numeral that applies a function twice, T and F are the two Church booleans, and +, * and = are the usual lambda terms for adding, multiplying and comparing them. Nothing is worked out until the program prints or tests the term; only then is it reduced to its normal form.
Printing resets the term to the identity, which is how a program prints one value after another. Hello World is thirteen numbers, each written between quotes as a character code and printed with ,.
The machine
| Code | Does |
|---|---|
0…9 "…" | Church numerals; between quotes, digits count as themselves and other characters as their codes |
B C I K O S U W Y | Combinators |
T F i n A V X | True, false, if, not, and, or, xor |
] [ + - * + "`" + `` | Successor, predecessor, plus, minus, times, power | |
= L l G g Z | Equal, at most, less, at least, greater, is zero |
x y z | Helpers that turn a base case and a step into a recursive function, for use with Y |
( … ) ) … ( | Build a term separately and apply the current term to it, or it to the current term |
. , ; : p | Print a numeral, a character, a boolean, the term itself, a newline |
< > ^ v ? # | Turn, turn at random, skip a cell |
_ \| | Go right or down if the term is false, else left or up |
f r ~ @ | Reduce the term, reset it, read a term from input, stop |
% | Write a character into the grid, so programs can change themselves |
This implementation follows bforte's Haskell interpreter, the one Try It Online runs. It reduces terms by evaluating them on demand and reading the result back, which reaches the same normal form as the interpreter's own repeated passes. ? follows a fixed sequence instead of a random one, so that every run replays.
Example
The fourth line prints the numeral 1 as a term, in de Bruijn notation, where x1 is the variable bound by…
The fourth line prints the numeral 1 as a term, in de Bruijn notation, where x1 is the variable bound by the nearest λ. The fifth builds a function that tests whether a number is even, with Y for the recursion, and applies it to 23. The last reads the successor function applied to 1 and prints its normal form, the numeral 2.
Patterns it teaches
- Lambda encoding: numbers and booleans are Church encodings, and the printers only recognise the shapes.
- Combinators:
S,K,B,C,WandYare commands, and the recursion helpers are combinators too. - Two-dimensional control flow: the pointer turns on whether the term is false, as in Befunge.
Try this
- Print the numbers 0 to 9.
- Print whether 7 is less than 5.
- Hard: print the factorial of 4 using
Yandx.
Further
- esolangs.org/wiki/Functoid
- The Haskell interpreter, with examples
See also
Functional paradigm
Self-modifying
Write your own in Run Convert a program from Functoid
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: functoid.
Command line
shib run functoid -e '1:@'shib manual functoidSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/functoid.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org