# Mu

With apologies to Robert Pirsig:

Is it a language, or an operating system, or a virtual machine?

Mu.

Read these first: problem statement, trying out Mu. (Mu requires minimal dependencies.)

Mu's code looks quite alien, requiring editors to be specially configured to colorize it in a sane manner. So this page provides links to the source files showing how it currently looks in my custom setup.

Whetting your appetite, some example programs:

- x.mu: a simple program to add two numbers together. Shows that at bottom Mu is a simple VM bytecode designed to convert directly to machine code.
- factorial.mu: everyone's favorite example, showing how Mu supports conditionals and loops without any special syntax, using the special labels '{' and '}'.
- tangle.mu: another (contrived) version of factorial showing Mu's ability to 'tangle' code from multiple places into a single function or recipe.
- simple examples showing off support for concurrency: fork.mu, channel.mu
- simple examples showing off hardware control: display.mu, console.mu.
- screen.mu: example program showing print primitives that inject a 'screen' dependency which can be faked for testing.
- filesystem.mu: example program showing file primitives that inject a 'filesystem' dependency which can be faked for testing.
- http-client.mu and http-server.mu, examples of Mu's testable high-level interfaces to the network.
- static-dispatch.mu: example program showing Mu's ability to define recipes with headers, and thereby to allow functions with the same name but arguments of different types to coexist.
- counters.mu: lexical scope
- chessboard.mu: a little program for 2 people to play chess, with thorough tests of its behavior including both screen and keyboard handling.
- nqueens.mu: a solution to the N queens problem.
- Example programs using delimited continuations: 1, 2, 3, 4, 5.
- A code essay: “Coroutines in Mu”
- same-fringe.mu: a solution to the ‘same fringe’ problem, demonstrating Mu's coroutines and generic functions.
- Two klunky ways to build exceptions out of continuations in Mu: 1, 2. Since Mu is statically typed, there's no way to implement a `try` primitive that can wrap arbitrary functions.

Now a listing of every layer in Mu. Recall that you can stop loading at any layer and get a valid program to run with a subset of features, that passes all its tests.

Part I: basic infrastructure

000organization.cc: the basic skeleton program. Compiles and runs but doesn't do much. Later layers hook into this skeleton to add functionality. Mu's guarantee: you can load features up until any layer, and it will compile and pass all tests until that point. More details →
001help.cc: just a simple test layer to show how to hook into the skeleton. Also summarizes how to invoke Mu, behaviors that later layers will be providing.
002test.cc: Mu's minimalist test harness, relying on a couple of one-liners in the `build` script to auto-generate lists of tests to run.
003trace.cc: support for logging facts about our program, and for checking the facts logged in tests. (tests for the tracing system)

Part II: the core Mu virtual machine, designed to compile easily to machine language.

010vm.cc: core data structures: recipes, instructions and reagents (operands).
011load.cc: the textual representation of recipes and how it's turned into the data structures.
012transform.cc: after Mu programs are loaded but before they are run they can be transformed in an extensible manner akin to lisp macros. Think of this as the core of Mu's ‘compiler’ for providing high-level features atop the core.
013update_operation.cc: our first transform: check for unknown recipes before the program runs.
014literal_string.cc: extend the loader to support literal strings in various instructions.
015literal_noninteger.cc: extend the loader to support non-integer numbers.
016dilated_reagent.cc: allowing whitespace in reagents.
017parse_tree.cc: a new syntax for representing complex types as trees using whitespace and parentheses (s-expressions).
019type_abbreviations.cc: the core types of Mu are designed to be fully explicit and familiar to non-programmers at the cost of some verbosity: `number`, `character`, `boolean`, etc. Once learners get acclimatized, we can teach them abbreviated forms that are familiar to veteran programmers: `num`, `char`, `bool`. Mu's facility for type abbreviations is extensible: learners can abbreviate `number` to `n` if they so choose, thereby exploring such trade-offs. You can also create abbreviations suitable for a specific program, like abbreviating `address:array:address:array:character` to `board` for say a tic-tac-toe or chess program. Think C's `typedef` statement.
020run.cc: executing Mu recipes by executing the list of instructions they contain. Future layers will define more primitive operations that can be used in instructions.
021check_instruction.cc: harness for adding per-primitive checks to run before running a program.

Various primitive operations: on numbers, booleans, for control flow, and comparing values.

Support for defining new recipes. In Mu calls to functions look just like primitive operations, with the ability to pass in ingredients, and to return products. In particular, Mu supports returning multiple values, and uses this ability far more pervasively than high-level languages can.

Support for various data structures: heterogeneous compound types called containers, akin to records or structs, homogeneous arrays of a single type of value (type `array` conventionally abbreviated as `@`), and exclusive containers, akin to C unions but with a tag so each value knows its ‘kind’. Out of these primitive types, Mu builds the usual and growing menagerie of data structures: linked lists permitting fast insertion and deletion and unidirectional scanning but slow search; duplex lists that permit bidirectional scanning; associative arrays or tables for fast insertion, deletion and search using hash functions; streams for scanning through strings incrementally; and buffers for gradually constructing long strings in a piecemeal fashion.

Dynamic memory management: Mu supports allocating space at run-time as pointers or addresses. All Mu instructions can dereference or lookup addresses of values in addition to operating on regular values. These addresses are manually managed like C, and can be reclaimed using the `abandon` instruction. To ensure that stale addresses aren't used after being abandoned/reused, each allocation gets a unique alloc id that is also stored in the address returned. The lookup operation ensures that the alloc id of an address matches that of its payload. This eliminates a whole class of undefined behavior and security vulnerabilities that plague C. Compared to Rust, Mu pays some additional runtime cost in exchange for C-like flexibility (you can copy addresses around all you like, and write from any copy of an address) and simpler implementation (no static analysis). Mu by convention abbreviates type `address` to `&`.

Support for higher-order recipes that can pass recipes around like any other value.

Support for running multiple functions concurrently using routines, for communicating between routines using channels, and for synchronizing between routines. Channels are Mu's only synchronization primitive, queues that can cause the routine reading or writing from them to stall without taking up CPU resources. Mu provides safe concurrency by forbidding routines from sharing addresses; writing to a channel always performs a deep copy that preserves all internal aliasing.

Support for delimited continuations that let one pause and resume sub-computations.

Part III: transforms to make Mu a little more expressive, and give it some of the benefits of a high-level language.

040brace.cc and 041jump_target.cc: how Mu provides structured goto-less programming without introducing the syntax of conditionals and loops other languages require.
042name.cc: how Mu transforms variable names to raw memory addresses.
043space.cc: how variables in different routines are isolated from each other using spaces. Mu ‘local variables’ are allocated on the heap.
044space_surround.cc: Chaining spaces together to accomodate variables with varying lifetimes and ownership properties.
045closure_name.cc: how spaces can implement lexical scope.
046check_type_by_name.cc: a transform to deduce missing types in instructions on the basis of previous instructions in the same function.
050scenario.cc: Mu's first syntax — not for code but for tests. (example)
052tangle.cc: support for layers in Mu programs. They've been so good to us.
053recipe_header.cc: a new syntax for summarizing the number and types of ingredients and products a function expects at the top next to its name, in a header.
054static_dispatch.cc: allowing multiple variants of a function to coexist as long as each has a unique header.
Support for generic or shape-shifting data structures and recipes, containing wildcard type ingredients that start with an ‘_’. Everytime you use a shape-shifting recipe with a new set of ‘concrete’ types for its type ingredients, it creates a new variant of the recipe for you matching those types.
057immutable.cc, a static analysis to ensure that functions never modify anything but their products.

Part IV: Miscellaneous.

061text.mu: strings in Mu are bounds-checked rather than null-terminated. They're also unicode-aware (code points only; no control characters, no combining characters, no normalization). Mu recipes that take strings can take literal strings thanks to a transform that allocates them on the heap.
062convert_ingredients_to_text.cc: a convenience transform primarily intended to provide the illusion of dynamic typing when adding to the trace. The stash command can print any number of ingredients of any type into the trace. Its default output format is fairly simplistic, but it can be overridden for arbitrary types by defining a variant of the to-text function with an ingredient of the appropriate type. For example, see 064list.mu which defines how we trace lists.
067random.cc: a random-number generator with a testable interface.

Part V: Primitives for interfacing with hardware.

080display.cc: primitives for accessing the keyboard and screen.
081print.mu: helpers that can swap the real screen with fake ones for testing.
082scenario_screen.cc: writing tests that check what is printed to screen. (examples)
084console.mu: helpers that can swap the real keyboard and mouse with fake ones for testing.
085scenario_console.cc: writing tests for keyboard and mouse using the fakes. (examples)
087file.cc: primitives for accessing the file system.
088file.mu: helpers that permit swapping a fake filesystem or `resources` object for testing.
089scenario_filesystem.cc: writing tests for filesystem using the fakes. (examples)
091socket.cc: primitives for accessing the network.
092socket.mu: helpers for the network. In Mu you create a fake network ‘neighborhood’ the same way you create a fake local file system.

029tools.cc: various primitive operations to help with testing and debugging.
100trace_browser.cc: a zoomable UI for inspecting traces generated by Mu programs. Allows both scanning a high-level view and drilling down into selective details.

Part VI: An environment that watches programmers as they manually test their code, and turns these interactive sessions into reproducible test scenarios. The readme for the app contains instructions for running it. Stop loading after each of these layers to get a working version with just fewer features.

edit/001-editor.mu: data structures for a text editor widget. Load just this layer to see just the rendering and line-wrapping at work.
edit/002-typing.mu: support for moving the cursor anywhere with the mouse and typing text in there.
edit/003-shortcuts.mu: support for various keyboard shortcuts for manipulating text you've typed in.
edit/004-programming-environment.mu: combining two text editor widgets, one on the left, one on the right.
edit/005-sandbox.mu: support for running Mu code in the right-hand widget using code from the left, and displaying results in a sandbox below on the right. You can have multiple sandboxes, and hit F4 to rerun them all at any time with the latest version of the code on the left side.
edit/006-sandbox-edit.mu: click on the 'copy' button in each sandbox to duplicate its contents.
edit/007-sandbox-delete.mu: support for the 'delete' button in each sandbox.
edit/008-sandbox-edit.mu: click on the 'edit' button of each sandbox to pop its back into the sandbox editor. Basically like copying and then deleting a sandbox.
edit/009-sandbox-test.mu: click on the results of a sandbox to turn them green and save the output as golden/expected. Any future changes to the output will then be flagged in red.
edit/010-sandbox-trace.mu: click on code in a sandbox to open up a drawer containing its trace. The trace can be added to using the stash command, which can be extended to render arbitrary data structures by creating new variants of the to-text recipe for the appropriate types.
edit/011-errors.mu: support for rendering errors on both the left and in each sandbox.
edit/012-editor-undo.mu: support for undo in the editor widget.

The zen of Mu:

- traces, not interfaces
- be rewrite-friendly, not backwards-compatible
- be easy to port rather than portable
- global structure matters more than local hygiene

Mu's vision of utopia:

- Run your devices in 1/1000th the code.
- 1000x more forks for open source projects.
- Make simple changes to any project in an afternoon, no matter how large it is.
- Projects don't slow down with age, they continue to evolve just as fast as when they were first started.
- All software rewards curiosity, allowing anyone to query its design decisions, gradually learn how to tweak it, try out increasingly radical redesign ideas in a sandbox. People learn programming as an imperceptible side effect of tinkering with the projects they care about.
- Habitable digital environments.
- A literate digital society with widespread skills for comprehending large-scale software structure and comparing-and-contrasting similar solutions. (I don't think anybody is literate by this definition today. All we can do easily is read our own programs that we wrote recently.)