Chad is an esoteric programming language based on interaction networks aimed to be chad.
Chad is cool. Chad is great. Chad needs no dependencies. Chad works alone. All is good.
ngl show n then pass the World to the next Count
Count(w, next) vs n
if n < 0 => next ~ w
else => n ~ Show(w, Say['\n'](w2)), n - 1 ~ Count(w2, next)
main
9 ~ Count(world, Ghost)
More in examples/.
A program is a net of Chads connected by wires. Each Chad has one face and some arms. When two Chads meet face to face, the rule for their pair replaces both of them. The program runs until no two faces meet.
A(x, y)is a Chad of breedAwith armsxandy.A[1, 2](x)also carries the values1and2.a ~ bconnectsaandbwith a wire.- A number like
42is a Chad too. A name likewis a wire. - In a rule, names in
[ ]and a matched numbernare values and can be used any number of times. Names in( )are wires and have two ends. A(x) vs B(y) => ...is the rule for whenAmeetsB.A(x) vs nmatches any numbern.- A rule can have cases:
if cond => ...and a finalelse => .... The first case that fits is used. mainis the net the program starts with. It comes after the rules and runs to the end of the file.worldis the wire to the World, and it can be used once.nglstarts a comment until the end of the line.
Values are 64-bit integers. They support + - * / %, == != < <= > >= and and, or, not. 'a' is the character's number. "hi" is Cons('h', Cons('i', Nil)).
Chad is based on Lafont's interaction nets. Here's a comparison:
| Lafont | Chad |
|---|---|
| agent | Chad |
| symbol | breed |
| principal port | face |
| auxiliary ports | arms |
| active pair | two faces that meet |
| interaction rule | A(x) vs B(y) => ... |
| interface | the arms in a rule head |
| initial net | main |
| constructor γ | Fn |
| duplicator δ | Rep |
| eraser ε | Ghost |
Chad keeps Lafont's rules:
- Linearity: every wire has two ends and every arm in a rule head is used once.
- Binary interaction: rules only fire when two faces meet.
- No ambiguity: each pair of breeds has at most one rule.
So the order Chads meet in never changes the result. Fn, Rep and Ghost are Lafont's core interaction combinators.
| Breed | Does |
|---|---|
Say[c](next) |
when it meets the World, prints c as a UTF-8 character and gives the World to next |
Hear(next, out) |
when it meets the World, reads one UTF-8 character into out (Silence at the end) and gives the World to next |
Ghost |
erases whatever it meets |
Rep(a, b) |
copies whatever it meets into a and b |
Cons(head, rest), Nil |
lists. The head can be any Chad |
Silence |
end of input |
Every program includes these integrated Chad functionalities (directly written in Chad):
| Breed | Does |
|---|---|
Fn(in, out) |
a function. Two Fns meeting join their arms |
Print(w, next) |
prints a string |
Show(w, next) |
prints a number |
HearNum(w, next, out) |
reads a number into out (Silence at the end) |
Ask(w, next, out) |
prints a string, then reads a number into out |
Add(b, out), Sub, Mul, Div, Mod |
a ~ Add(b, out) puts a + b on out |
Eq(b, out), Lt, Gt |
same, but compares |
Chad only needs a C++17 compiler and CMake. Nothing else.
cmake -B build
cmake --build build
chad <file.chad>
chad -c <file.chad> print the number of interactions at the end
chad -v print the version
chad -h print help
ctest --test-dir build
Test cases: tests/cases/