# Mental Model: How Plumb Thinks

Plumb is a **formal verification language and strict referee for empirical and theoretical physics**.

In real physics, major errors rarely come from basic arithmetic. They happen in the **hidden plumbing** around a calculation:

- Smuggling a theory's ontology (such as cold dark matter or a fiducial cosmological template) into a "raw" measurement, then testing a competing theory against it.
- Adding a deterministic worst-case systematic bound ($|\delta| \le 0.02\text{ mag}$) in quadrature with random Gaussian noise.
- Forgetting that two measurements share a calibration zero-point — or double-counting a nuisance already baked into a covariance matrix.
- Testing a model on data that was already used to calibrate it (**circular calibration**), or combining two published marginals from the same posterior as if they were independent.
- Quietly dropping rows based on the observed outcome (Malmquist / selection bias) under a plain Gaussian likelihood, or permuting data table rows out of order.
- Missing an **indirect contradiction** where every pair of measurements agrees, yet no single shared set of parameters satisfies the whole network at once.

Plumb makes assumptions, error models, theories, predictions, and comparisons explicit in **`.plumb` files** that can build on the standard library (`std/`).

## The Three-File Separation

Plumb keeps **scientific meaning**, **raw numbers**, and **integrity hashes** strictly separate:

| Layer | File type | What belongs here |
| --- | --- | --- |
| **Scientific meaning** | `*.plumb`, `*.lean` | Units, physical constants, assumptions, shared nuisances, bounded systematics, declared roles, measurements, model-conditioned inferences, theories, predictions, rules, claims, comparisons, scopes, and optional Lean 4 proof sources (including inside `std/`). |
| **Raw numeric data** | `*.json` | Strictly raw bulk data: ordered axis manifests (`Axes`), numeric data tables (`Table`), and covariance matrices (`Matrix`). No prose or hidden physics assumptions. |
| **Integrity lockfile** | `plumb.lock` / `std/lock.json` | Exact-byte SHA-256 digests and byte lengths for every `.plumb` module, referenced Lean proof source, and data artifact, generated and checked with `plumb lock`. |

Your `.plumb` files stay readable and clean (`artifact desi_values : Table from "data/desi.table.json" ...`), while `plumb lock` freezes the exact bytes so nothing can drift silently.

## Four Non-Negotiable Rules Plumb Enforces

1. **State the ontology explicitly.** Declare model-dependent summaries with `inference ... conditioned on ... requires ...`. A comparison must satisfy those premises through the theory's `assume`, a prediction's `assuming`, or an evaluated rule's `grants`. Raw `measurement` declarations exclude `PhysicalOntology` premises.
2. **Random noise $\ne$ deterministic bounds.** Statistical uncertainties (`noise`, `nuisance`) combine via exact covariance algebra ($LDL^T$). Deterministic worst-case bounds (`bounded`) are never added in quadrature to a covariance; they are evaluated across their full interval box.
3. **Track lineage, cancel shared biases, refuse loops.** Shared biases pool coherently when measurements are combined and cancel cleanly when measurements are differenced. Structural cycles, circular calibration, and aliased/shared-posterior double counting are caught and refused automatically.
4. **One verdict per check.** The receipt records whether each check is `Compared`, `ConditionalResult`, `Conflict`, `NonIdentifiable`, `SystematicsLimited`, `Blocked`, `Unassessed`, or `Unresolved`.

## Built-In Checkers, Lean 4, and External Fitters

Plumb stays fast and lightweight by focusing its own core on physics verification while connecting cleanly to established tools:

- **Built-in Plumb checkers** handle empirical comparisons, shared-bias algebra, epistemic/cycle auditing, certified intervals, quadrature, symbolic derivatives, piecewise/trig/hyperbolic expressions, and targeted polynomial/radical/scaling/SOS claims.
- **Lean 4 (`by lean`)** checks proofs of closed comparisons that Plumb can translate to Lean. A proof can use locally vendored libraries such as **Mathlib** or **Physlib**. Write `by lean "proofs/c.lean"` or reference a local Lake theorem (`by lean theorem Mod.thm from lake "formal" module Mod`; local Lake projects build offline in the sandbox from pinned local sources), and optionally discharge a `MathematicalPremise` with `states ... discharged by <claim>`. Plumb generates the authoritative Lean target, checks statement agreement and transitive axioms (isolated `lake comparator` export verification when available, or kernel-environment replay), pins `.lean` sources in `plumb.lock`, and caches verified proofs locally so warm runs take milliseconds (`plumb doctor --lean` reports backend and cache status; see `plumb doc lean`).
- **External MCMC & optimizers** (Cobaya, emcee, NumPyro, Stan, SciPy, etc.) stay outside Plumb's kernel: use `plumb parity` before sampling to confirm exact per-block $\chi^2$ and log-likelihood agreement at fixed parameter points, run your sampler as usual, and pass joint draws back through `plumb audit --witness` for independent certification.

## Built-In Documentation for Humans and AI Agents

Everything in this guide is embedded directly into the `plumb` CLI:

```sh
plumb doc                  # list all topics
plumb doc language         # print the complete .plumb syntax reference
plumb doc --search "mus"   # search all documentation sections
plumb doc --all            # export the entire guide as one Markdown stream
plumb doc --json           # structured JSON export for tools and agents
```
