In the Prologs I've used, a running query lives in exactly one place: the process running it. Kill the process and the search is gone. If you want the rest of the answers, you start again from the top.
cocolog started from one question: what would a Prolog look like if the state of a proof in progress were plain data? Something you could write into a table, read back in another process, and continue from exactly where it stopped.
This is the first time I'm showing it publicly. Here's the trick it was built for:
$ cocolog --embed KB consult family.pl
consulted 12 clause(s) into 'main'
$ cocolog --embed KB start job "ancestor(tom,X)"
started 'job'
$ cocolog --embed KB --steps 12 step job
- ancestor(tom,bob)
- ancestor(tom,liz)
- ancestor(tom,ann)
- ancestor(tom,pat)
job: suspended at 12 inference(s), 4 answer(s) this turn
$ cocolog --embed KB --steps 12 step job
- ancestor(tom,jim)
- ancestor(tom,zoe)
job: suspended at 24 inference(s), 2 answer(s) this turn
Each of those commands is a separate process. When the third one exits, the proof isn't in memory anywhere. Its pending goals, choice points and bindings are in the store, as a few dozen kilobytes of plain text in a table. The fourth command reads them back, lands in the middle of the search, and finds the next two answers. Swap the embedded store for a database server, and that next step can run on another machine, a day later.
The one decision
The usual way to write an interpreter is to allocate terms as nodes linked by pointers, with a recursive \`solve()\` that uses the C stack as its continuation. It's simple, and it takes about half the code. But pointers and stack frames aren't data: you can't write them to a table and read them back somewhere else.
So cocolog makes one decision and builds everything else on it. A term is an index into a flat array of 64-bit cells, never a pointer, and the engine never recurses in C. The continuation is itself a term on the heap, and a choice point is a frame of integers. An array of indices means the same thing at any address, so freezing a machine is writing out six arrays, and thawing it is reading them back.
Every feature added since then had to keep that property, or say in the code why it doesn't. That includes compiled clauses, the garbage collector, the tracer and the green-thread scheduler.
The stack under it
cocolog is written in Cicili, a C with Lisp syntax that I also wrote. It keeps its knowledge base in ZiguratIP, my MVCC database. In cocolog, "knowledge base" is meant literally: a clause is a row, a keyed lookup is an index probe, and each turn of a suspended proof commits as one transaction.
It all ships as one binary: the interpreter, an embedded store, the module loader and a scheduler. Where the knowledge base lives is a command-line flag, not a different build: in memory, embedded in the process, or on a server over TCP, TLS or HTTP.
The long part: becoming boring on purpose
Making proofs stop and resume was the fun part. Making cocolog boring took far longer.
By boring I mean answering exactly as SWI-Prolog 9.0.4 does: the same output text, the same errors, the same exit codes. A float prints as the shortest text that reads back as the same double. An error's context names the predicate that raised it. A syntax error in a file is reported in SWI's words, at SWI's line and column, and the load goes on. \`--trace\` prints the ports SWI prints, at the depths it prints them.
What else is in there
\- Green threads, Go-style. \`go\`, \`yield\`, \`await\` and channels, over one shared store, with one worker per core. A task is preempted every 10,000 steps, so one that never yields can't hog its worker. On a four-vCPU Linux box, eight tasks run CPU-bound work about three times as fast as one, the same as eight OS threads. A green task costs about 10 µs to start, and an OS thread about 500 µs.
\- 21 loadable modules over C and C++ libraries: sockets, TLS and cryptography, curl, PCRE2, OpenCV, raylib, libtorch and TensorFlow, with HTTP and WebSocket libraries on top. Each module is declared in a Cicili binding and called as Prolog. No C is pasted into Prolog strings.
\- library(flow/chart) and library(flow/debug).
\- flow/chart draws a program as a flowchart without running it. It flags what looks wrong the way a compiler would: a call nothing answers (with the name you probably meant), left recursion, a clause that is never tried, a determinism promise that isn't kept.
\- flow/debug runs a query untraced in a child process, records only the window you ask for, and draws it over the chart: the path taken, failures in red, exceptions in purple. A journal answers every call that reaches the outside world, so the replay is exactly the run it explains.
\- cocolint, a linter for this dialect's traps.
\- 132 tutorials that are also tests. Every claim in a lesson is a check, so a lesson that stops being true fails.
Machine learning, written as expressions
library(tensor_expr) lets you write a network the way you'd write it on a whiteboard. relu(X matmul W1 + B1) matmul W2 + B2 is a two-layer net. loss(X, Y, W, B) ::= mse(X matmul W + B, Y) defines a loss. L := loss(X, Y, W, B) runs it and frees every intermediate tensor, and Gs := grad(L, [W, B]) returns the gradients. The same file runs on libtorch or TensorFlow, eagerly or as a compiled graph, on a CPU or a CUDA GPU. The program never names its backend: you pick it from outside, with tensor_execution/3. And because this is cocolog, a trained model doesn't stay in memory. params_save/2 writes its parameters into the knowledge base as terms, so training, testing and predicting run as three separate processes over one store, and a model trained on TensorFlow can be tested on torch from that same store. There are 42 tutorial networks, one file each, from linear regression through LSTMs, ResNet, U-Net, a small GPT, a VAE, a GAN, a graph convolutional network, RealNVP and a diffusion model, up to object detection on real photographs. One of them is a character-level language model trained on cocolog's own source.
Honest numbers
cocolog is not faster than SWI-Prolog, and I won't pretend it is. The language benchmark runs five small programs, each from the same \`.pl\` file in every system, on a four-vCPU Linux box. cocolog takes 0.86 to 2.86 times CPython's time, and 0.95 to 2.03 times SWI-Prolog \`-O\`'s.
It wins when the data lives in the store as a state machine and inference engine to capable of reasoning. Against Python with sqlite3:
\- Keyed lookups: a thousand of them over 200 facts in the embedded store are about 15 times faster.
\- A counting loop whose every addend is a row: 5.7 times faster.
\- Queens: 1.5 times slower.
The benchmark page keeps the losses in.
Try it
The release has two Docker images: a plain one, and one that adds raylib, PyTorch and NumPy.
docker load -i cocolog-3.78.22-docker-amd64.tar.gz
docker run --rm cocolog query 'X is 6\*7, write(X), nl'
docker run --rm -it cocolog # the interactive toplevel
docker run --rm -v "$PWD":/work cocolog --embed KB -s program.pl
Or build it from source with clang. It's BSD 2-Clause.
\- Repo: https://github.com/saman-pasha/cocolog
\- Release 3.78.22: https://github.com/saman-pasha/cocolog/releases/tag/v3.78.22