Tula
Turing Language
Install / Use
/learn @tsoding/TulaREADME
Tula
Tula (Turing Language) is an Esoteric Programming Language based on Turing Machine and extended with Set Theory.
Install
- Install Rust Compiler
- Clone this repo:
$ git clone https://github.com/tsoding/tula && cd tula
- Install the compiler to
~/.cargo/bin/(it should be already in your$PATHafter rustup installation)
$ cargo install --path .
- Run some examples:
$ tula run ./examples/05-rule110.tula
- Expand the example into a form without Universal Quantifiers and Sets:
$ tula expand ./examples/05-rule110.tula
Test
The project is using rere.py for testing the behavior of the compiler:
- To run the tests:
$ ./rere.py replay ./tests.list
- To record the new behavior:
$ ./rere.py record ./tests.list
Base Syntax
The program consist of sequence of rules:
case <State> <Read> <Write> <Step> <Next>
case <State> <Read> <Write> <Step> <Next>
case <State> <Read> <Write> <Step> <Next>
...
Each rule starts with the keyword case and 5 expressions:
<State>- The current state of the Machine,<Read>- What the Machine reads on the Tape,<Write>- What the Machine should write on the Tape,<Step>- Where the Head of the Machine must step (<-left,->right or.stand),<Next>- What is the next state of the Machine.
Example
Simple program that increments a binary number (least significant bits come first):
// When in the state `Inc` and read `0`, replace it with `1` move the head
// to the right and switch to the state `Halt` which halts the program.
case Inc 0 1 -> Halt
// When in the state `Inc` and read `1`, replace it with `0` move the head
// to the right and keep staying in the state `Inc` effectively looping over
// the tape
case Inc 1 0 -> Inc
// Start the program from the state `Inc` with the tape `{ 1 1 0 1 }` and print
// the tape on each state change
trace Inc { 1 1 0 1 }
The trace of the execution of the above program:
Inc: 1 1 0 1
^
Inc: 0 1 0 1
^
Inc: 0 0 0 1
^
Halt: 0 0 1 1
^
You can actually have several trace statements within a single file
that start at different states with different tapes. All of them are
going to be executed sequentually.
If you need to start the head from a different position provide two tape sequences. First one is going to be treated as everything to the left of the head, the second one is everything to the right:
trace Loop { a b c } { 1 1 1 0 }
// ^
// The head starts here
case Loop 1 0 -> Loop
The trace of the above program
Loop: a b c 1 1 1 0
^
Loop: a b c 0 1 1 0
^
Loop: a b c 0 0 1 0
^
Loop: a b c 0 0 0 0
^
Compound Expressions
Instead of using just Symbols you can actually use Compound Expressions. The syntax of a Compond Expression is similar to S-expressions but without the pair syntax. Just Symbols and Lists:
<expr> ::= <symbol>
<expr> ::= "(" *<expr> ")"
Here is a simple example that iterates the Tape of Pairs of Numbers and swaps each pair until it reaches the delimiter &:
case Swap (1 2) (2 1) -> Swap
case Swap (2 3) (3 2) -> Swap
case Swap (3 4) (4 3) -> Swap
trace Swap { (1 2) (2 3) (3 4) & }
Trace of the above program:
Swap: (1 2) (2 3) (3 4) &
^~~~~
Swap: (2 1) (2 3) (3 4) &
^~~~~
Swap: (2 1) (3 2) (3 4) &
^~~~~
Swap: (2 1) (3 2) (4 3) &
^
The Compound Expressions don't really add that much to the Language by themselves. We could've written the above program like this and end up with basically this same result:
case Swap 1_2 2_1 -> Swap
case Swap 2_3 3_2 -> Swap
case Swap 3_4 4_3 -> Swap
trace Swap { 1_2 2_3 3_4 & }
What they actually do is emphasize that the Symbols may contain additional information and enable use with extrating this information by using Sets and Universal Quantification.
Sets and Universal Quantification
Tula supports defining Sets (which are collections of Compound Expressions) and using Universal Quantification on those Sets to generate Rules automatically.
let Set { a b c }
for n in Set case S n 0 -> S
The above program will expand to
case S a 0 -> S
case S b 0 -> S
case S c 0 -> S
Note that for n in Set quantifier is applied only to a single statement that follows it. To apply the quantifier to a block of statements use curly braces:
let Set { a b c }
for n in Set {
case S n 0 -> S
case I n 1 -> I
}
The above expands to:
case S a 0 -> S
case I a 1 -> I
case S b 0 -> S
case I b 1 -> I
case S c 0 -> S
case I c 1 -> I
You can also nest the Quantifiers:
let Set { a b c }
for n in Set
for m in Set
case (S n) m 0 -> S
The above expands to this:
case (S a) a 0 -> S
case (S a) b 0 -> S
case (S a) c 0 -> S
case (S b) a 0 -> S
case (S b) b 0 -> S
case (S b) c 0 -> S
case (S c) a 0 -> S
case (S c) b 0 -> S
case (S c) c 0 -> S
Nested Quantifiers that iterate over the same set can be collapsed like so:
let Set { a b c }
for n m in Set
case (S n) m 0 -> S
Example
Here is the example that iterates the Tape of Pairs of Numbers again but using Sets, Universal Quantifiers:
let Numbers { 1 2 3 4 }
// For each `a` and `b` from the set of Numbers when in the state `Swap` and read `(a b)`
// replace it with `(b a)` move the head to the right and stay in the `Swap` state loop
// over the entire tape until you encounter something else
for a b in Numbers
case Swap (a b) (b a) -> Swap
// When in the state `Swap` and read `&`, keep it as `&` move the head to the right and `Halt`
case Swap & & -> Halt
// Execute and trace the program starting from state `Swap` with the tape that contains a
// bunch of pairs of numbers.
trace Swap { (1 2) (2 3) (3 4) & }
The trace of the above program:
Swap: (1 2) (2 3) (3 4) &
^~~~~
Swap: (2 1) (2 3) (3 4) &
^~~~~
Swap: (2 1) (3 2) (3 4) &
^~~~~
Swap: (2 1) (3 2) (4 3) &
^
Halt: (2 1) (3 2) (4 3) & &
^
The tape is infinite to the left and right filled with the first and last symbols correspondingly. In the example above the tape is filled with & to the right, which is clearly indicated by the last trace output.
Anonymous Sets
It is not necessary to define the Sets upfront with the let keyword. You can use them directly in Universal Quantifiers:
for n in { a b c } {
case S n 0 -> S
}
Set operations
You can combine the Sets with Union and Difference operations (+ and - infix operators correspondingly)
let Numbers { 69 420 }
let Emoji { 😳 🍆 🔥 💯 }
// For any Emoji or Numbers except 🍆 replace it with 🦀.
// This effectively makes the program stop at 🍆 'cause there is no case for it.
for e in Numbers + Emoji - { 🍆 } {
case Crab e 🦀 -> Crab
}
trace Crab { 🔥 😳 69 420 🍆 }
The trace of the above program
Crab: 🔥 😳 69 420 🍆
^~
Crab: 🦀 😳 69 420 🍆
^~
Crab: 🦀 🦀 69 420 🍆
^~
Crab: 🦀 🦀 🦀 420 🍆
^~~
Crab: 🦀 🦀 🦀 🦀 🍆
^~
This kind of Set Expressions are also allowed in the Set Definitions:
let Numbers { 69 420 }
let Emoji { 😳 🍆 🔥 💯 }
let Anything_But_Eggplant ( Numbers + Emoji - { 🍆 } ) // Parenthesis for clarity
let Anything_But_Eggplant Numbers + Emoji - { 🍆 } // Also works without parenthesis
Cartesian Products
One special operation on the Sets allows you to create Cartesian Products of them. Here is how you can Skip all Pairs of Numbers without using nested Universal Quantifiers:
let Number { 1 2 3 4 }
let Pair Number * Number
for _ in Pair
case Skip _ _ -> Skip
trace Skip { (1 2) (2 3) (3 4) & }
The trace of the above program:
Skip: (1 2) (2 3) (3 4) &
^~~~~
Skip: (1 2) (2 3) (3 4) &
^~~~~
Skip: (1 2) (2 3) (3 4) &
^~~~~
Skip: (1 2) (2 3) (3 4) &
^
"Magical" Sets
Tula supports a special "magical" set Integer that is infinite (actually not, it's i32, but you get the point):
for a b in Integer
case Swap (a b) (b a) -> Swap
case Swap & & -> Halt
trace Swap { (69 420) (1337 7331) (42 37) & }
The trace of the above program:
Swap: (69 420) (1337 7331) (42 37) &
^~~~~~~~
Swap: (420 69) (1337 7331) (42 37) &
^~~~~~~~~~~
Swap: (420 69) (7331 1337) (42 37) &
^~~~~~~
Swap: (420 69) (7331 1337) (37 42) &
^
Halt: (420 69) (7331 1337) (37 42) & &
^
It is actually impossible to expand the example because Integer is
just too big. But the Interpreter still prints the trace
instantaneously because internally it does not actually generate any
cases. It treats the Sets as Types and performs an efficient Type
Checking and Pattern Matching to infer the <Write>, <Step> and
<Next> based on the current state of the Machine.
You can use Integer in Set Expressions:
// For any Integer except 5 specifically keep moving to the right
for n in Integer - { 5 }
case Until_Five n n -> Until_Five
trace Until_Five { 1 2 3 4 5 6 7 8 }
Trace of the above program
Until_Five: 1 2 3 4 5 6 7 8
^
Until_Five: 1 2 3 4 5 6 7 8
^
Until_Five: 1 2 3 4 5 6 7 8
^
Unt
Related Skills
node-connect
346.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
