← Projects · Code

Code, keyed
by its structure.

A file system knows code as text at a path. Sophia stores every definition under the hash of its normalised structure: rename it, reformat it, print it in another syntax, and it keeps its id; change what it computes, and it gets a new one. Names become labels, tests and proofs become facts attached to the code, and every question about the code is a query.

sourcewritten
add2(x, y) = x + y * 2
content id#············same id
one definition, rewritten · the id changes only when the computation does
Try it

Same code, same id.

Two definitions, each normalised and hashed in your browser. Pick an example or edit either side, and watch what reaches the id and what does not. Swapping the operands is the instructive one.

as the store sees it
content id
as the store sees it
content id
…

definition A, printed back by the store
julia profile
rust profile

A toy, written for this page: one-line arithmetic definitions, normalised the way Sophia normalises (names out, parameters as positions, layout and comments gone, structure and operand order kept) and hashed with SHA-256. Sophia's normalisers read full languages, refer to other definitions by their ids rather than their names, and hash with BLAKE3.

How it works

Four ideas, one store.

The first two are borrowed from Unison, which content-addresses code by its syntax tree, and are known to work. The last is new, and is the part most likely to fail.

  1. 01

    A definition's id is the hash of its structure

    Every definition is parsed and normalised: bound names become positions, layout disappears. What remains is hashed from the leaves up, each node folding in its children's hashes. Equal structure, equal id: on any machine, at any time.

  2. 02

    Names are labels on it

    A name points at an id; code refers to other code by id. Renaming moves a label and touches no code. Editing a definition makes a new id and moves the name; the old version stays, and everything that used it still means what it meant.

  3. 03

    Everything else is attached to it

    Tests, test results, proofs, documentation and comments are not compiled; they are annotations and derived facts on an id. A test result belongs to the id it ran on, so it holds for exactly as long as that code exists, and never for a changed version.

  4. 04

    Sameness is a claim, with evidence

    Two definitions that do the same thing but differ in structure keep their two ids. The store relates them by an equivalence edge that says how strong the claim is, from “a test agreed” to “a proof was checked”, and what it ignores. That is meant to replace an FFI.

Why it matters

No boundaries, one library.

Code today is cut up by namespaces, repositories and crates. Those borders say where code was written and how it is shipped, not what it computes. When a definition is found by what it is, they stop being walls.

No arbitrary boundaries. Namespaces, repositories, crates and packages are somewhat arbitrary lines: they decide what can see what and what has to ship together. In Sophia every definition sits in one store under its own id. A namespace is only a set of names laid over it, so code is reused across what used to be package borders, without wrapping or vendoring.

Versions and dependencies, done differently. A dependency points at an id, not at a package and a version range, so it cannot drift or conflict: two versions of a function are two ids, side by side. A release is a named set of bindings, and whether a change is a patch, minor or major release is derived from the evidence instead of declared.

Against AI code bloat. Coding agents tend to write a new helper instead of finding the one that exists, and a library fills up with copies of the same function under different names. In a store keyed by structure, an exact duplicate gets the original's id and folds into it; near-duplicates need equivalence edges, below. And an agent can ask the store whether something exists before writing it (the MCP server, M3b, planned).

Reuse, not rewrite. AI makes rewriting other people's code cheap, but a rewrite throws away the tests, proofs and trust the old code had gathered. The intention is the opposite: find the existing library, with the facts attached to it, and use it.

The novel part

Same, but how sure?

Code from different languages is meant to work together not through a foreign-function interface, but through stored claims that two definitions are equivalent. A claim is only as good as its evidence, so every equivalence edge carries a level, and only the top four let the compiler substitute one definition for the other.

levelmeansdecided bysubstitutable
alpha identical after normalisation comparing hashes yes
defeq equal by definition, as a type checker decides the kernel, by normalisation yes
rewrite connected by a chain of trusted rewrite rules proof extraction from an e-graph yes
observational no context can tell them apart, for a stated observation a proof term yes
tested agree on a test suite or on property fuzzing running the tests only by explicit policy
asserted a person said so a signature only by explicit policy

Most equivalence is free. Two definitions that normalise to the same term get the same hash: one node, no edge, nothing to prove.

No claim is absolute. Each edge names what it ignores: allocations, timing, floating-point reassociation, which exception is thrown. Chaining claims unions these, so chains are kept short.

Designed, not built. This is the riskiest of Sophia's claims, and the vault says why: few people will write proofs, so most edges in practice would be tested or asserted.

Where it is going

Many languages, one core.

Source languages are to be elaborated into one small core calculus, and the core lowered through MLIR to LLVM IR. Every arrow in the picture is a stored relation, not a step inside a compiler process: run it twice on the same input and nothing new is written. A compiler becomes a set of queries over the store.

Today languages are read up to their content ids; support is stated by level, per language, each level with the tests behind it, never as a percentage of code that got an id.

Julia Rust Python sophialang sophialang core MLIR LLVM IR machine code works planned
language L1 parsedL2 normalisedL3 resolvedL4 loweredL5 runsL6 verified
sophialang the store's own language; it is the core
Sophia queries runs on four backends; checked by differential tests
Metamath dependency analysis checked against metamath.exe
Julia module scope; 95.1 % agreement with Julia's own method table
Python names resolved within one file
Rust for, match, closures, while let and let-else are refused

yes · partly · no. From Sophia's README and Languages note, 2026-11-21.

Ask the store

Questions as queries.

Sophia has its own query language: Datalog, with a Julia-flavoured syntax. A query is itself stored as a definition with a content id, and a result is identified by the query, a snapshot of the store and its parameters. It runs natively on every backend, and as translated SQL on SQLite.

Sophia's query language · examples from its specification
query transitive_dependants(target::Id)
    # direct and transitive users of `target`
    dep(a, b) = refs(a, b)
    dep(a, c) = refs(a, b) && dep(b, c)
    select a where dep(a, target)
end

select k, count(d) where def(d, k)

# names under core. that nothing refers to
select p where name(p, d) && startswith(p, "core.") && !refs(_, d)
On a real corpus

What does 2 + 2 = 4 rest on?

Metamath's set.mm proves arithmetic from axioms. Read into Sophia, the question is a closure over the proof's references. The answer: 15 axioms and 29 definitions.

  • ax-mp
  • ax-1
  • ax-2
  • ax-3
  • ax-4
  • ax-5
  • ax-6
  • ax-7
  • ax-8
  • ax-9
  • ax-gen
  • ax-ext
  • ax-1cn
  • ax-addcl
  • ax-addass

Asked for every theorem of set.mm and iset.mm, 64 566 of them, Sophia's answer agrees with metamath.exe's on every one.

Metamath in Sophia
Where it stands

An experiment, said so.

The store runs, with tests and benchmarks, and an editor depends on it. Everything past the query language is design. Whether sophialang can meet all of its specifications is still to be found out.

  1. M0 Identity done
  2. M1 The store done
  3. M2 Moonkale interface done
  4. M3a Query language first form
  5. M3b Tools for agents (MCP) open
  6. M4 Running code open
  7. M5 Compiling to LLVM open
  8. M6 Self-hosting starts open
  9. M7 More languages open

Works today

  • Content ids on four database backends (SQLite, redb, LMDB, DuckDB) behind one interface, with one conformance suite and benchmarks
  • sophialang printed and parsed back Julia-flavoured or Rust-flavoured, round-trip tested on 20 000 random definitions
  • A Datalog query language with its own content ids, run natively on every backend and translated to SQL on SQLite
  • The code store of the Moonkale editor: replaying 130 commits of Lenticulum.jl and 471 of Moonkale gives 0 differences from a fresh build
  • Julia, Rust and Python read through the normaliser shared with Moonkale, so a definition has the same id in both tools

Known limits

  • Nothing runs or compiles yet: no interpreter, no lowering through MLIR or LLVM
  • Equivalence edges are designed, not built
  • Sophia stores proofs and walks their references; it does not check them
  • A store is about ten times the size of its source, most of it derivable
  • Open risks, written down: reading a compiler's state back from a database may cost more than recomputing it; a normaliser that is too coarse would merge different programs silently; Julia has no formal semantics for cross-language claims to rest on
Get started

Read first, then build.

The repository is an Obsidian vault: every Rust file has a note of the same name explaining it, next to the requirements, specifications, decisions and benchmarks. The whole vault is online.

Build and try the CLI · tools from flake.nix
git clone https://github.com/MathStruct/Sophia && cd Sophia
nix develop                                     # Rust, sqlite3, metamath, Julia
cargo test --workspace
cargo run -p sophia-cli -- sample               # a sample program into .sophia/store.sqlite
cargo run -p sophia-cli -- text sample.add      # printed Julia-flavoured
cargo run -p sophia-cli -- profile rust         # the same code, Rust-flavoured

Where to start, by background

Feedback welcome

Disagree, and migrate.

The store is content-addressed, so it does not have to be trusted: any node can be checked again by hashing it again. And if you dislike the schema, write a different normaliser and migrate the store to it: a supported operation, not a fork. If you have thoughts on an undertaking like this one, send a message, or read CONTRIBUTING.md.

Please don't train any AI on this project's material.