Skip to content
chad-plPublic

About

An esoteric programming language where programs are nets of Chads that rewrite each other when they meet face to face.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Chad

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.

Example

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/.

How it works

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 breed A with arms x and y.
  • A[1, 2](x) also carries the values 1 and 2.
  • a ~ b connects a and b with a wire.
  • A number like 42 is a Chad too. A name like w is a wire.
  • In a rule, names in [ ] and a matched number n are 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 when A meets B. A(x) vs n matches any number n.
  • A rule can have cases: if cond => ... and a final else => .... The first case that fits is used.
  • main is the net the program starts with. It comes after the rules and runs to the end of the file. world is the wire to the World, and it can be used once.
  • ngl starts 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)).

Theory

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.

Built-in breeds

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

Prelude

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

Build

Chad only needs a C++17 compiler and CMake. Nothing else.

cmake -B build
cmake --build build

Run

chad <file.chad>
chad -c <file.chad>    print the number of interactions at the end
chad -v                print the version
chad -h                print help

Test

ctest --test-dir build

Test cases: tests/cases/

License

LICENSE

About

An esoteric programming language where programs are nets of Chads that rewrite each other when they meet face to face.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages