A riff on Cook-Levin Chapters

Chapter 41

What is Shale?

Shale is a small scripting language for working on code, Lua-like on the surface, and the language of every script in this repository and of the port in the next chapter. This chapter describes the language itself, from its own documentation, and leaves Cook-Levin aside; a reader here for the machine can skip ahead to “The VM in Shale”.

What it is for §

Shale lives in a public repository of its own, showell/shale, under the same license as this one, the Universal Permissive License 1.0. Its README.md opens: "Shale is a small scripting language for working on code. One line greps a whole repo. A short script refactors one. Every run is budgeted, confined, readable. Agents write it in place of bash, in place of Python." The same README says its code was written by AI coding agents (Claude), with human supervision, as this repository's says of its own.

Its builtins cover the common Linux work, by the README's list: reading, editing and searching files, walking folders, running programs and background jobs, git, text and JSON. A task that would be a pipeline of grep, sed and sort in a shell is a short script, launched as a heredoc (shale <<'EOF') or as a file with name=value arguments, which the script reads as arg("name", default). In this repository, build.shale runs the C's tiers, roc/check.shale and js/oracle.shale hold the Roc and JavaScript ports to the goldens, sat/oracle.shale drives rocsat, and book/book.shale builds this book.

How a script runs §

A C runtime reads each script, compiles it to bytecode and runs it on a small virtual machine. The README says the whole runtime "builds as one C file, links only the C library, and weighs under a megabyte", and runs on Linux x86-64. The first implementation was in Zig; it is frozen in the repository's legacy_zig/, and the C was written against it as its oracle, with golden tests, each input frozen with its output, as the judge of behaviour.

Every run has budgets: steps, reads, bytes read, bytes printed, and the arena, the memory the run's values live in. docs/SHALE.md gives the defaults as 5,000,000 steps, 10,000 reads, 64 MiB read and a 16 MiB arena, and the README gives 64 KiB of output. A step is each statement, expression and loop iteration, and every byte or item a builtin touches. A run that reaches a budget stops and says which one. --budget NAME=N raises one on the command line, and a script that always needs more says so in a -- budget: line among its opening comments, as the port's first line does.

A stop is one line that says where and why, with a class (type, index, refused, budget reads) and the calls that led there. An argument is read by the script's arg calls, and one whose name they leave unread, a misspelled one, stops the run before it starts. Past half of any budget, or on a stop, the runner prints what the run used; --verbose prints it always, and ends with the step count that the port's check reads.

What it refuses §

The README's guard rails come down to four rules.

The style is an allow-list: Shale and its runner name what they permit, and a refusal names what was asked for and what to write instead.

The scripting language §

docs/SHALE.md says that Shale "looks like Lua, with smaller semantics of its own". Its integers are 64-bit and checked, so an overflow stops the script. Lists count from 1. A function is declared at the top level, above the code that calls it, and const names a top-level value made once. Reading a map's missing key stops the run, and get(m, k, default) asks for a default instead; a list that would hold nil is a type error.

A long list is refused, each item by name: floats and /; ^, hex literals and the bitwise operators; function values, closures, nested functions and local function; : method calls, ..., goto and repeat; multiple assignment and multiple returns; and Lua's library, string., pairs and the rest. What remains is integers, strings, booleans, lists and maps, loops, and functions by name, with a large set of builtins beside them. “The VM in Shale” is the machine and its mathematics in that much.