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.
- counters.mu: lexical scope
- 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.
- static_dispatch.mu: example
program showing mu's ability to define recipes with headers, and allow
recipes with the same name but different headers to coexist.
- 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 for the N queens problem.
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 makefile to autogenerate
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 test harness)
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).
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, 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
tables; and streams 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. However, all allocations are transparently
reference-counted or refcounted,
with every copy of a pointer updating refcounts appropriately. When the
refcount of an allocation drops to zero it is transparently reclaimed
and made available to future allocations. By construction it is impossible to
reclaim memory prematurely, while some other part of a program is still
pointing to it. 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).
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.
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.
046global.cc: experimental support
for 'global' variables that are always available inside a single routine. Mu
has no variables that are available transparently across routines, and this
might go away as well. Global variables are currently not checked as
rigorously as the rest of the type system.
047check_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: beginnings of a standard library
070text.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.
071rewrite_stash.cc: a
convenience when debugging is the ability to add variables to the trace using
the stash command. By default
stash just prints out the
locations in memory, one by one. To extend its display format specific types,
define a function called to-text
with the appropriate ingredient type, returning a Mu string.
072channel.mu: 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.
073array.mu: utilities for arrays.
074list.mu: linked lists where each
node points to the next, permitting fast insertion/deletion but slow for
search.
075random.cc: a random-number
generator for introducing non-determinism into programs.
076duplex_list.mu: doubly
linked lists that can be traversed both forwards and back.
077stream.mu: data structure to
efficiently append strings.
Part V: Primitives for interfacing with hardware.
080display.cc: primitives for using
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)
029tools.cc: various primitive
operations to help with testing and debugging.
090trace_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 anytime 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.)