# Identity and persistence

The first stage of MUL ended with a compiler that runs a program twice and
compares the results. The second stage gives the compiler memory: a program
has names that survive edits, the verified IR can be written to a file and
read back, and an error at run time points to the line that caused it. This
post describes what works now and what does not yet.

## Every function has an id and a hash

An agent that changes a program needs a way to say which function it means,
and later to check that the function is still the same. The Code Graph gives
each function a `symbol_id` of the form `sym:mul:<module>:<name>`, and a
`content_hash`: a SHA-256 of the function's canonical text, built from the
syntax tree and not from the characters in the file. Reformatting a file, from
the compact layout to the pretty one and back, changes neither value. Editing
the function changes its hash.

`mul inspect` prints the graph of a module. For this program, where `main`
calls `div`:

```mul
module demo

fn div(a: i32, b: i32) -> i32 {
    return a / b
}

pub fn main() -> i32 {
    return div(10, 0)
}
```

the whole module is a list of symbols and one `snapshot_id` for the module:

```console
$ mul inspect demo.mul
module: demo
snapshot_id: snap:c66e718c35f3a60b
symbols: 2
sym:mul:demo:div (a: i32, b: i32) -> i32 sha256:b22e9eff…cf2cc
sym:mul:demo:main () -> i32 sha256:87f357d4…a8a
```

and `--symbol` shows one definition with its effects, source range, calls and
references:

```console
$ mul inspect --symbol sym:mul:demo:div demo.mul
symbol_id: sym:mul:demo:div
kind: function
name: div
visibility: private
signature: (a: i32, b: i32) -> i32
effects: [pure]
origin: {start: 16, end: 63}
content_hash: sha256:b22e9eff…cf2cc
snapshot_id: snap:c66e718c35f3a60b
calls: []
references: [sym:mul:demo:main@99]
```

(Hashes are shortened here.) The graph covers functions only. Parameters and
locals are not symbols yet.

## The `.mulb` file

`mul build --emit mulb` writes the verified IR to a binary `.mulb` file, and
`check`, `ir`, `run` and `build` accept such a file instead of source. The
format is specified in RFC-012: a header with a magic number, a schema
version, feature flags and a content hash over the rest of the file. Reading
it back verifies the file before anything runs it:

- a file that is cut short, has the wrong magic number, or has a hash that
  does not match is rejected with a `MUL-B*` code;
- a file that requires a feature or a schema version this `mul` does not know
  is rejected, instead of being read as something it is not;
- sizes and counts are checked against limits;
- the IR inside goes through the verifier again, so a hand-made file cannot
  skip it.

```console
$ mul check damaged.mulb
damaged.mulb: error[MUL-B006]: content hash mismatch, the file is corrupted
```

The codes `MUL-B001` to `MUL-B009` are listed on the [errors page](/errors/).
A `.mulb` file holds the IR only. The source map and the Code Graph are not
stored in it yet, so a program run from a `.mulb` file reports errors without
a position.

## Errors that point to the source

Every IR function, block, instruction and terminator has a byte range in the
MUL source, kept in a side table next to the IR. The IR itself does not carry
it, so the table does not change what the verifier checks or what the
backends compile. When the program stops at run time, the error shows where:

```console
$ mul run demo.mul
demo.mul:4:12: error[MUL-R003]: division by zero [byte 56]
  |     return a / b
  |            ^^^^^
```

The interpreter and the Cranelift backend print the same position for the
same error. `mul ir --source-map` prints the ranges of all nodes, which is
what a tool needs to go from an IR node back to a line. An executable built
with `mul build` still prints the error without a position; that is not done.

## Fuzzing

The lexer, parser, type checker, formatter and the `.mulb` reader are
exercised with random input. The property tests in `make check` feed them
arbitrary bytes, truncated and mutated programs, and corrupted `.mulb` files,
and require an error, not a crash. They also check that formatting a program
and reading it again gives the same program. For longer runs there are
libFuzzer targets in `fuzz/` (`make fuzz`), which need a nightly toolchain.
So far none of these has found a crash.

The generated programs are mostly rejected by the type checker, so they test
the front end far better than lowering and the backends. A generator of typed
programs is planned.

## What comes next

Near term, from the points above:

- **Complete `.mulb` files.** The source map and the Code Graph stored in the
  file, so that positioned errors and `mul inspect` work on it, and
  `mul inspect` showing the header and sections of a `.mulb` file.
- **Positions in executables.** Errors from a program built with `mul build`
  pointing to the source.
- **A deeper graph.** Parameters and locals as symbols with their own ids.
- **Deeper fuzzing.** Typed program generation and a step limit for runs.

After that come the stages from the [first post](/blog/welcome-to-mul/): Go
patch mode, an operation engine and an API for agents.
