Bthreads: A Simple and Easy Paradigm for Clojure

submited by
Style Pass
2024-11-06 11:30:03

Asynchronous programs are hard to reason about. But is this intrinsic to asynchrony? Or might we be using the wrong paradigms?

Behavioral programming is a programming paradigm that aims to make asynchronous, event-driven systems both simple and easy by using a system centered on behavioral threads (bthreads). In my previous article, I introduced the idea of behavioral programming in Clojure.

Bthreads are both simple and easy. The simplicity is evident. Bthreads define isolated stateful application behaviors that communicate by requesting or blocking events.

To see why bthreads are easy, we will consider an example: programming a tic tac toe game. Programming games involves rules that can come into conflict (and hence need prioritization). In addition to rules (what must occur), there are strategies (what a rational player should do). Some events in tic tac toe originate from a human player. Other events are generated from the program, such as wins, draws, and computer moves.

Behavioral programming is performed by “teaching” the computer in a way similar to how we would teach a human. We might start teaching a person by saying “tic tac toe is a turn-based game, first player x goes; then, player y.” We teach our program in a similar fashion.

Leave a Comment