Source lean/README.md
Review these entry points and the definitions they import:- `Determinize/Theorems.lean` states every theorem in full and proves it by one term from `Proof`. The paper's theorems come first, under the paper's names and with the paper's hypotheses and conclusions: `inference_correctness`, `trace_preservation`, `tracewise_soundness`, `output_mass_preservation`, `expectation_preservation`, `variance_non_increase`, `convex_function_inequality`, `probabilistic_type_safety`, `trace_variance_decomposition`, `determinization_preserves_typing`, `equality_of_total_masses` and `returned_expectation_along_traces`. Expectation and variance are those of the output conditioned on returning. `tracewise_soundness` and `trace_variance_decomposition` use Mathlib's `Measure.condKernel`. Further results follow: `return_or_diverge` (the paper states it at affinity E only), `finite_expectation_preservation`, `conditional_expectation_preservation` (equal expectations conditioned on acceptance, the statement behind `observe`), `determinized_output_law` (the target's output law is the law of the source's conditional mean given the trace, under typing and domain safety alone; the output marginal of the appendix's big-step symbolic soundness lemma). `trace_erasure`, among the paper's theorems, proves that the output law is the output marginal of the joint law, which Section 4 states without a theorem. The file's last command fails the build unless each theorem depends only on `propext`, `Classical.choice`, and `Quot.sound`. It also fails if a theorem's statement relies on a declaration from a `Proof` module that is not a proof, following the statements through every definition they use.- `Determinize/Spec/` contains the definitions the theorems are about: ordinary syntax, typing, primitive distributions and means, determinization, and semantics. `Spec/Expectation.lean` defines expectations in the extended reals (`HasExpectation`, `extendedExpectation`), the return probability (`returnProbability`), the output law conditioned on returning (`returnedLaw`), and the expectation and variance conditioned on returning (`returnedExpectation`, `returnedVariance`). `Spec/Traces/Semantics.lean` defines the operational traces, the joint law `traceAndOutputLaw` of a program's trace and output, and its trace marginal `traceLaw`; operational replay lives in `Proof/Traces/ReplaySemantics.lean`.- `Determinize/Spec/Inference.lean` defines what affinity inference is measured against. `Frontend.infer` fills the omitted sample affinities of a resolved program `input : Input`. A *completion* of `input` fills exactly those affinities (`Input.Matches`) and is closed and typed at some type; `AffinityLE` compares programs site by site in the order G ≤ E. `Theorems.inference_correctness`: if `infer` fails, no completion exists; if it succeeds, its program is a completion, typed at the returned type, and every completion lies below it, so it is the greatest completion. `Theorems.lean` imports `Frontend/Infer.lean` because `infer` is the subject of that statement; its body need not be read. `Spec/Inference.lean` reuses the program types and `Input.Matches` from `Spec/Frontend.lean`: `Input` and `Annotated` are `Expr` with rational literals, whose sites carry a requested affinity or `none` (a placeholder), respectively an affinity. `Input.Matches` and `AffinityLE` are both `Expr.Sitewise`, which relates two programs with the same constructors, literals and indices site by site. An annotated program is coerced to a `Core` program (`Annotated.toCore`) where `interpret` expects one.For normalized output laws, `finite_expectation_preservation` states positive target return probability, probability-law and integrability facts for both `returnedLaw`s, and equality of their finite means. `expectation_preservation`, `variance_non_increase`, and `trace_variance_decomposition` cover extended means and normalized variance results.For the executable backend, also review `Spec/Frontend.lean` (the rational core `Core`, with sample and mean sites, and its embedding `interpret` into real-literal expressions), `Spec/FiniteModel/Model.lean`, `Spec/FiniteModel/Statistics.lean`, `Spec/RewardModel/Model.lean`, and `Spec/RewardModel/Results.lean`. The `MomentCertificate` and reward `Solution` certificates handle closed divergent regions. `finite_reward_integrability` derives finite moments for every finite additive model without user-supplied integrability bounds. Applying determinization to infer source expectations still requires the source hypotheses; finite target exploration does not establish source integrability.Run `lake build --wfail` from this directory; the build is warning-free and contains no `sorry`, and it checks the public theorems' axioms. With Lean's kernel and these standard axioms trusted, reviewers can omit the proof bodies in `Proof`. `Theorems.lean` and `Spec` are the specification. A theorem cannot be stated in `Theorems.lean` without being proved, so no separate check that every statement has a proof is needed. No `Spec` file imports a `Proof` module or the front end. `Theorems.lean` imports `Proof`, but its statements may use it only for proofs: Lean treats any two proofs of a proposition as equal, so they cannot change what a statement means, and the build fails if a statement relies on anything else declared in `Proof`. The one proof the statements themselves use is finiteness evidence from `Proof/Traces/Mass.lean`: Mathlib defines `condKernel` only for finite measures, and the joint law of trace and output is one. The check sees what the statements use, not how an import changes the way `Theorems.lean` elaborates: a coercion, a notation or a changed instance priority from `Proof` could do that without leaving anything from `Proof` in a statement. `Theorems.lean` also imports the front end: `Frontend/Infer.lean`, and through it `Frontend/Syntax.lean`, the shape unifier `Frontend/Unify.lean` and the affinity solver `Frontend/Affinity.lean`.The entry points can also be read as [API documentation](../website/docs/Determinize/Theorems.html), in which every name in a statement links to its definition and every declaration to its source. It covers the modules `Determinize.lean` imports, and CI publishes it from `main`, next to the simulator. To build and browse it locally:```shcd docbuildlake build Determinize:docspython3 -m http.server -d .lake/build/doc```The first build also documents Lean and the imported part of Mathlib, which takes about two CPU-hours. `docbuild` pins doc-gen4 and shares `.lake/packages` with this directory. After changing `lean-toolchain` or a dependency, set doc-gen4's `rev` in `docbuild/lakefile.toml` to the new Lean version and run `MATHLIB_NO_CACHE_ON_UPDATE=1 lake update` in `docbuild`; CI fails while the two directories pin different revisions.The typed determinization theorems assume `DomainSafe`: every reached operation has valid arguments almost surely at every finite execution depth. For typed programs, this requires valid distribution parameters and nonzero divisors. Rejection and divergence remain possible. `return_or_diverge` proves that returned mass plus divergence probability is one for domain-safe programs of real type. Divergence is defined as the infimum of finite-depth running probabilities and includes rejection. `Proof.Paper.domainSafe_iff_return_or_diverge` proves the converse under real typing, so the mass-balance equation characterizes domain safety. `trace_preservation` and `probabilistic_type_safety` also establish target domain safety. Finite replay certificates use the same predicate, defined in `Spec/Semantics.lean`.Source expressions need not be in ANF. Each primitive distribution is its own constructor with the paper's operands (`uniform action lower upper`, `gaussian action mean variance`, and so on). `DistributionAction` is either `sample affinity` or `mean`, where `Affinity` is E or G. Mean expressions carry no affinity annotation; their result affinity follows the operand typing rules. Operands may contain nested sampling and are evaluated left to right; a mean site also evaluates every operand exactly once, including a Gaussian's variance. The soundness theorems also cover source expressions containing mean sites. `Spec/Primitives.lean` gives each primitive one fiber: its law at a stochastic site, the Dirac mass at its mean at a mean site, and the zero measure outside the parameter domain. Expressions have no type annotations; `Typed` assigns types separately. Expressions carry E/G labels only on sample sites; literals and arithmetic are unannotated and a literal types at either affinity, as in the paper's rule Real; subtyping is silent; variables are de Bruijn indices. `Typed` enforces the affinity restrictions: E multiplication requires a G left operand, division a G denominator, and comparisons G operands. Arithmetic uses real numbers; division by zero gets stuck. The theorems quantify over closed float programs of either affinity; the proof uses subsumption to assign an E result type to the same program.`DistributionAction.determinize` changes `sample E` to `mean` and retains `sample G`. A mean site still evaluates its operands and checks its parameter domain; G draws nested in those operands therefore still execute and appear in the trace.Output laws are defined directly by recursion over reduction depth. Deterministic actions continue evaluation; sampling actions integrate the continuation over the primitive measure on reals. Expressions of every type may occur during evaluation, but only terminal reals contribute output. Neither evaluator requires a measurable structure on expressions. `Proof` introduces one internally to establish measurability of the evaluators.A trace is a list of `(primitive, value)` pairs recording only stochastic G draws. Deterministic steps and E draws add no entry. Trace soundness factors the actual joint trace/output measures over `traceLaw source`, the trace marginal of the source joint law: the source output is a Markov kernel indexed by the trace, and the target output is a measurable function of the trace. For almost every trace, the source fiber is an integrable probability measure and the target output equals its mean. The target therefore has the same trace law and the same termination probability as the source. No global integrability is required. Detailed traces with one entry per reduction step live in `Proof/Traces/Detailed.lean`. The proof uses them to connect symbolic execution to compact traces; `Proof/Traces/CompactFiberSoundness.lean` and `Proof/Traces/CompactSoundness.lean` establish the trace factorization directly using compact replay.The output-mass theorem preserves the probability of returning; dividing the preserved integral by it gives `conditional_expectation_preservation`. `variance_non_increase` bounds the target variance, and `trace_variance_decomposition` decomposes the source variance along traces. `finite_expectation_preservation` adds source integrability and proves target integrability and equal means. `expectation_preservation` only assumes that one of `∫ v⁺` and `∫ v⁻` under the source output law is finite, and concludes the same for the target and equal extended-real expectations. Jensen's inequality (`convex_function_inequality`) bounds `∫ φ` under the target output law by `∫ φ` under the source output law for every nonnegative convex `φ : ℝ → ℝ`. The results are derived from trace soundness in `Proof/Corollaries.lean` and `Proof/Normalization.lean`. Output measures are unnormalized: divergence contributes no output mass. The theorems' expectations and variances are conditioned on returning, except in `convex_function_inequality`.Symbolic mean steps evaluate affine formulas over the existing E samples, preserving parameter checks and reduction depth.The default build checks mean sites with affine-dependent operands, invalid mean parameters, nested sampling, the `x + 1/y` example, a G draw scaling an E draw from the left, and a sampled value captured by a function. `Proof/InterfaceChecks.lean` checks the direct evaluator using only public imports and verifies that these imports provide no measurable structure on expressions.## StyleThe Lean sources follow Mathlib's conventions for naming and layout. The deliberate exceptions:- **Paper theorems.** The theorems in `Theorems.lean` are named after the paper's theorems, in  `snake_case`, and not after the shape of their statements. Each states the conclusions of  its paper theorem as one conjunction.- **Hypotheses and variables.** A hypothesis is named after what it says (`typed`, `safe`,  `notValue`) and a variable is written out (`program`, `expression`), where Mathlib writes  `h`, `ht` and `e`.- **Explicit arguments.** A theorem takes the program it is about as an explicit argument,  also when a later hypothesis determines it.- **Affinities.** The constructors `Affinity.E` and `Affinity.G` are capitalised as in the  paper.- **Headers.** Files carry no copyright or author header.Three things do not follow Mathlib and are not deliberate: definitions outside `Spec` arenot all documented; the `reduce…` families in `Proof/Semantics/Measurability.lean` and theequality deciders in `Proof/FiniteModel/Equality.lean` are built by tactics; and someproofs and files are much longer than Mathlib allows.## Lean command-line implementationBuild with `lake build --wfail`, then run from `lean/`:```sh.lake/build/bin/determinize ../tests/execution/foldr.det.lake/build/bin/determinize --samples 1000 --seed 42 ../tests/execution/foldr.det.lake/build/bin/determinize --check ../tests/execution/foldr.det./test.sh```The CLI reads the existing `.det` grammar, with optional `[E]` or `[G]` after asampling primitive. Unannotated sites are inferred; explicit affinities are constraints.It prints the annotated source and the result of the existing `Expr.determinize`.`mean_uniform`, `mean_gauss`, etc. in the output denote atomic mean operations:they evaluate every operand exactly once and check the primitive domain. They areoutput notation, not additional source primitives. `--fuel` bounds each numericalrun; `--samples` defaults to zero, so compilation does not execute the program.## Source layoutUnder `Determinize/`:- `Spec/`: mathematical definitions and assumptions; `Traces/` and `FiniteModel/` contain  their respective specifications.- `Proof/Primitives/`: distribution laws, kernels, masses, and moments.- `Proof/Semantics/`: expression measurability, evaluator kernels, and type safety.- `Proof/Symbolic/`: affine expressions, symbolic reduction, and its invariants.- `Proof/Traces/`: detailed and compact traces, replay, and conditional laws.- `Proof/FiniteModel/`: finite-model correspondence and certificate soundness.- `Proof/Frontend/`: soundness, optimality and completeness of affinity inference.  - `Unify.lean`: the shape unifier returns a most general unifier (`unify_mgu`).  - `Affinity.lean`: the affinity solver returns the greatest solution (`solveAffinities_spec`).  - `Typing.lean`: inversion of `Typed` for each constructor, up to subsumption.  - `Decompose.lean`: erasure of types to shapes; decomposing subtyping between decorated    types into affinity constraints is sound and complete.  - `Ground.lean`: ground substitutions, the relations they solve, monotone read-back, and    tactics shared by the two proofs below.  - `Soundness.lean`: every solution of the generated constraints reads back to a typed    program (`generate_sound`).  - `Completeness.lean`: every typed completion comes from a solution of the generated    constraints (`generate_complete`).  - `Inference.lean`: the proof of `Theorems.inference_correctness`.- `Proof/Soundness.lean`, `Proof/Corollaries.lean`: global results derived from traces.- `Theorems.lean`: the theorems, each stated in full and proved by a term from `Proof/`.The executable and its checkers are separated as follows:- `Frontend/`: affinity inference (`Infer.lean`) with the shape unifier (`Unify.lean`)  and the affinity solver (`Affinity.lean`), verified in `Proof/Frontend/`; unverified  parsing, desugaring/name resolution, and pretty printing; and `compile`  (`Compile.lean`), which runs them in order.- `Checking/`: executable checkers for finite distributions and for finite-model result,  moment, and termination certificates, with their soundness theorems.- `Finite/`: verified graph construction and expected-reward solving, with model export.- `Proof/LinearAlgebra/`: executable Gaussian elimination with a proof of the original equations.- `Runtime/`: an unverified floating-point interpreter and seeded numerical samplers.- `Tests/`: parsing, inference and input preservation, completions, mean-site typing,  runtime, finite-model, and kernel proof tests.- `Main.lean`: the CLI.`Spec` and the semantic soundness proofs do not import the front end or runtime. The CLI uses the formalization's syntax and determinization,generalized over literal and site types. Decimal input is parsed exactly as `Rat`; the mathematicalinterpretation `interpret` embeds each rational into `ℝ`. A proved commuting equation(`interpret_determinize` in `Proof/FiniteModel/Initial.lean`) connects rationaldeterminization to the existing real-literal theorem.### What is verified`Frontend.Surface` represents parsed syntax with named binders and dedicatedconstructors. Elaboration resolves names to de Bruijn indices and removes syntaxsugar, producing an `Input`: an `Expr` with rational literals whose sample sites carryan optional E/G affinity, `none` being a placeholder for an affinity to infer. `infer`fills the placeholders and returns an `Annotated` program, whose sites all carry anaffinity, so that it has no mean sites, and a type. There is no positional annotationlist. `compile` converts the annotated program once into a `Core` program(`Program.source`) for the runtime and the finite models, which also run determinizedprograms with mean sites.`Theorems.inference_correctness` holds for every `Input`. The returned programkeeps every constructor, payload, and requested sampling affinity of the input at thesame AST node (`Input.Matches`), so inference cannot silently change literals,operators, binders, or distribution kinds. It is typed at the returned type, it hasthe most E sites among all completions, and `infer` rejects an input only if nocompletion exists. `compile` takes the typing and input-preservation proofs of its`Program` from `Theorems.inference_correctness`; no checker runs afterwards. Theguarantees start at the **resolved input expression**: parsing, name resolution, anddesugaring from source bytes are outside the verified boundary, and so is the Leancompiler that runs `infer` in the executable.If the inferred type is a float, the program also has type `float E` by subsumption.The soundness theorems of `Theorems.lean` then apply to thecompiled program under their own hypotheses: `DomainSafe` and, for expectations,integrability. Typing alone proves neither hypothesis.### Surface extensions and inference limitsSubtraction lowers to addition and negation. `<=` evaluates both operands once,in source order, and negates the reversed strict comparison. Multiplication by aright-hand numeric literal is rearranged to use the core's left-G multiplicationrule. These transformations belong to the unverified desugaring stage.`bernoulli[E](p)` and `bernoulli[G](p)` are core numeric draws with outcomes 0 and 1.The probability is evaluated once and must be in `[0,1]`; an invalid probabilityhas zero mass in the formal semantics and raises a runtime error. E-affinityprobabilities may depend affinely on E values; G-affinity probabilities must have Gtype. Typing does not establish domain safety. Determinization changes an E drawto a mean site with the same probability expression. `flip(p)` lowers to a G-affinityBernoulli comparison and therefore returns a Boolean; `flip[E]` is rejected.`discrete[E](p0,...,pn,*)` supplies probabilities for outcomes `0,...,n`;the final outcome `n+1` has the remaining probability `1 - (p0 + ... + pn)`.The supplied probabilities may be computed expressions. They must be nonnegativeand sum to at most one. `discrete[E](*)` always returns zero.`discrete_list[E](ps)` takes an ordinary list expression with the same implied-final-probabilityconvention. G sampling requires G elements; E sampling accepts E elements.The mean is affine in the supplied probabilities, and determinization recursivelytransforms the operand before computing this mean.The formal semantics uses exact reals and returns zero mass outside the domain.Exact execution completes the rational list and checks it with `FiniteDistribution`;certificates retain exact arithmetic. Float execution permits accumulation roundingat the upper boundary (eight machine epsilons per supplied probability plus one),clamps a slightly negative remainder to zero for its mean, and samples with cumulativethresholds, falling through to the final outcome. This numerical approximation isoutside the theorem. Probabilities are not normalized by their sum.The existing `discrete[E](p0,...,pn)` form still requires literal rational probabilitiessumming exactly to one. It lowers to the new representation without changing itsoutcomes. Pretty printing uses `discrete_list` to preserve arbitrary list operands.`observe(c)` lowers to `if c then () else reject`. The explicit core rejection termhas zero output mass: formally it is an absorbing non-value, as proved in`Proof/Semantics/Rejection.lean`. The numerical runtime returns a distinct rejection outcomeimmediately; ordinary divergence still exhausts fuel. Conditions are evaluated once,and rejected executions do not evaluate their continuation. The CLI reports rejectedobservations separately from execution failures. `conditional_expectation_preservation`proves equality of the normalized expectations under its stated premises;empirical means use returned values and differ from unnormalized expectations.Inference is monomorphic, uses an occurs check and structural subtyping constraints,and defaults unconstrained affinities to E and unused type variables to `unit`.Products, sums, and lists are covariant; function arguments are contravariant andresults covariant. `Float[G]` is a subtype of `Float[E]`; the reverse is not allowed.Using a G draw at type E leaves its sample annotation G, so determinization stillretains the draw. Subsumption appears in typing derivations, never in the expression.Inference is sound, optimal, and complete (`Theorems.inference_correctness`): it rejects aprogram only if no assignment of its unannotated affinities is typable, and otherwisereturns the typable assignment with the most E sites.The shared corpus and analytical expectations live in [`../tests/`](../tests/README.md).Run `./test.sh` for unit tests, all corpus compilation/typing checks, and exact executionchecks. Run `./test.sh --statistical` for sampledsource/target moment checks, or `./test.sh --all` for both. Python 3.11+ reads TOML;the compiled Lean test runner performs all language evaluation and assertions.### Numerical runtimeThe runtime evaluates the checked core, with closures, recursive functions, sums,lists, and left-to-right evaluation. It uses SplitMix64 with separate E/G streams,Box–Muller Gaussian draws, Marsaglia–Tsang gamma draws, gamma-ratio beta draws,inverse-CDF exponential draws, and sums of bounded-rate Poisson draws. Identical seedsreplay identical runs. These algorithms, Lean's compiler, floating-point rounding,and the PRNG are not covered by the measure-theoretic soundness proof.Primitive domains follow the formalization: reversed uniform bounds fail instead ofbeing swapped; zero-variance Gaussians and point uniforms are allowed; division byzero fails. Nonfinite arithmetic and sampling results fail explicitly. Gammarejection and Poisson iterations are bounded, and Poisson rates above 1,000,000 arerejected by this numerical runtime. Failed and exhausted runs are reported.Statistical tests cover representative parameters, not the entire numerical range.Overflow and underflow in intermediate calculations can distort finite outputs:for example, `beta[E](1e308, 1e308)` currently produces a numerical mean of zeroinstead of one half. The exact rational evaluator and real-measure theorems donot use these Float calculations.### Discrete-distribution migration`Checking/FiniteDistribution.lean` defines checked rational probabilities andweighted expectation, and `Proof/Primitives/FiniteDistributionMeasure.lean` their realfinite law; no theorem statement uses either. `Spec/Primitives.lean` defines the Bernoullifiber and the mean fibers. Their probability, integrability, expectation, and Bernoulli variance andmeasurability proofs are in the corresponding `Proof/` modules. The shared primitive kernels,affine-mean laws, moment bounds, and domain-convexity proofs cover both distributions.Both primitives pass through core typing, symbolic and trace semantics, affinityinference, and the numerical runtime. The runtime remains unverified.### Finite-model contract[finite-model-contract.md](finite-model-contract.md) specifies exact rationalmodels, one-time terminal rewards, rejection, the initial primitive policy, andcertificates with value equations, a closed divergent boundary and descending paths. Definitionslive in `Spec/FiniteModel/`; proofs and theorems composing checkercorrectness guarantees live in `Proof/FiniteModel/`. The verified explorer is implemented in `Finite/`, with builder invariants in `Proof/FiniteModel/`. The verifiedmodel checker is in `Checking/FiniteModel.lean`. `replay_matches` supplies the correspondence proof carried by each extracted `CheckedModel`.`Checking/Statistics.lean` proves moment- and termination-checker soundness.`Tests/FiniteModel.lean` checks signed rewards and a nonterminating model whose valueequations have spurious solutions.### Exact finite-state exportFrom `lean/`:```shlake exe determinize --check --export /tmp/model --subject source ../tests/statistical/discrete.det```The default subject is `determinized`. Exploration uses exact rational arithmeticand structural machine states, including closures and continuation stacks.Stochastic Bernoulli and discrete draws have finite successors; mean draws ofall supported primitives are rational. Residual stochastic continuous andPoisson draws are rejected. Terminal results must be numeric.Successful exploration carries a proof of correspondence with the executable machine before writing`.candidate.lean`, `.replay.lean`, `.tra`, `.lab`, `.positive.state.rew`, and`.negative.state.rew`. The replay checker takes the requested source and subjectseparately from the candidate and checks their alignment, exact transitions,complete positive successor coverage, rewards, and absorbing terminal states.Acceptance constructs a `CheckedModel` with a `Spec.FiniteModel.Model` anda proof of paper safety and complete output-law equality. Stored states must be unique.The internal export path uses the builder’s proof without replaying the graph.The `.candidate.lean` file contains raw data. The `.replay.lean` file additionallycontains `machineReplay`, proved by `decide +kernel`, the resulting `model`, and`modelMatches`, which certifies the selected paper program.Check it independently with:```shlake env lean /tmp/model.replay.lean```These are program-level model certificates: they cover unbounded execution,rejection, and divergence. Moment certificates also cover finite graphs withdivergence. Portable replay checks sparse rows and successor-index witnesses;checking large graphs can cost substantially more than exploration.Limits default to `--max-states 10000 --max-edges 100000--max-state-bytes 1000000`. A limit or execution failure exits unsuccessfullywithout writing exports; existing files at the same path are left untouched.The byte limit measures each serialized machine state after construction, notprocess memory. Structural exploration can find finite cycles, but does notabstract infinite state spaces. Full captured environments may distinguishstates with equivalent future behavior.`Tests/Explorer.lean` checks exact rewards, recursion, cycles, limits, and errors.`tests/test_export.py` independently solves the emitted rational transitionequations for known answers, recompiles generated data, and kernel-checksreplay certificates (including rejection of changed initial states).Both run through `./test.sh`.The semantic bridge in `Proof/FiniteModel/` now reifies closures and continuationstacks into paper expressions. It proves the binding equations, deterministicroot reductions, bookkeeping equalities, and contextual rejection laws.The replay checker also requires a closed source (all variables bound), so`replay_initial_reification` identifies the initial paper program directly froman accepted replay certificate. These results are exercised in`Tests/SemanticBridge.lean`. Sampling correspondence, reachable continuationinvariants, and a bookkeeping bound feed into the unbounded execution proof.`Graph.lean` connects the graph to machine output, and `Soundness.lean` establishes`Model.Matches`, which is carried by the extracting checker’s result.### Certified output moments and StormFrom `lean/`:```shlake exe determinize --result /tmp/model --subject source ../tests/statistical/discrete.detlake env lean /tmp/model.result.lean````--result` exports the model, `.result.json`, and a standalone `.result.lean`certificate. The JSON reports return, rejection, and divergence probabilities,the first two unnormalized output moments, and conditional mean and variance.Conditional quantities are `null` when return probability is zero.For example, equal chances of diverging and returning 2 give return mass 1/2,first moment 1, second moment 2, conditional mean 2, and conditional variance 0.The verified builder constructs the graph correspondence proof. Terminalreachability identifies a closed divergent region, which contributes no output.One positive-probability edge of decreasing rank per remaining transient stateproves uniqueness of the equations. `Finite.solveStatistics` and`Finite.solveTermination` use verified rational Gaussian elimination and returnproofs directly. No graph replay or result checker runs on this internal path.The dense solver defaults to `--max-result-states 256`.Portable exports check saved graph data, path witnesses, and value equations inLean's kernel, using per-state proofs and sparse equations. `outputStatistics` certifies the selected core program's outputlaw; `expectedReward` and `conditionalVariance` give its first moment andconditional variance. `terminationProbabilities` certifies return, rejection,and divergence probabilities for the finite model. These probabilities sum toone by `Proof.FiniteModel.mass_balance`, with divergence defined as the limit ofsurvival probabilities. The paper semantics counts rejection among executionswith no output; the finite model distinguishes rejection from divergence.Every result certificate, built-in or Storm, additive or not, ends with the reported numbersas literals:```leantheorem printedStatistics :    (⟨1, 5/4, 9/4⟩ : Determinize.Spec.FiniteModel.OutputStatistics).Matches      (Determinize.Spec.Paper.bigStepMeasure (checkedSubject.program checkedSource)) :=  (by decide +kernel : statistics = ⟨1, 5/4, 9/4⟩) ▸ outputStatistics```The three literals are the return mass and the first and second moments. The statement namesonly `Spec/` definitions and `checkedSource` and `checkedSubject`, whose bodies are literals inthe file. When the return mass is positive, `printedConditionalMoments` states the program's`returnedExpectation` and `returnedVariance` as literals, proved from `printedStatistics` by`Proof.FiniteModel.statistics_returned_moments`. A changed literal fails the kernel check.For an independent Storm certificate, from the repository root:```shpython3 -m venv /tmp/determinize-storm/tmp/determinize-storm/bin/pip install -r tools/storm-requirements.txt/tmp/determinize-storm/bin/python tools/storm.py tests/statistical/discrete.det --prefix /tmp/model --subject source```The adapter invokes `--export`, obtains full exact rational state-value vectorsfrom Storm, and writes `.storm.lean`. Lean checks the vectors against the originalmodel, including boundary conditions. `reportedStatistics` additionally binds every reported rational statistic (including optional conditional quantities) to the certified initial-state statistics; changing the exported initial label cannot silently select a different answer. The certificate then ends with `printedStatistics`. This route never calls our solver unless`--compare` is supplied and has no 256-state solver limit. Both routes certifythe same quantities. Storm, rational decoding, and serialization are outside theproof; incorrect vectors fail checking.`.storm.json` records the certified values, versions, commands, stage durations,and completion or failure. A kernel-checked report also requires axiom reports usingonly `propext`, `Classical.choice`, and `Quot.sound`. Each subprocess has a 120-second timeout, adjustablewith `--timeout`; large portable kernel checks can require more time.When overriding `--binary`, use an absolute path; relative paths resolve againstthe repository root.`lean/test.sh` includes exact ground truth, independent kernel replay, andcertificates with tampered values, boundaries, ranks, and dimensions.`tests/test_printed_statistics.py` checks the closing theorems on every route, including thata changed literal is rejected; it finds the routes by the files that generate`outputStatistics`.Real Storm integration tests run when `STORM_PYTHON` names an interpreter with thepinned `stormpy` dependency, which the `.#lean` shell sets on Linux. The precisecontract and proof boundaries are described in[finite-model-contract.md](finite-model-contract.md).### Storm tacticImport `Determinize.Tactic.Storm` and use `by storm` to prove exact output statistics,returned expectation, or returned variance for a closed rational core program:```leanimport Determinize.Tactic.Stormopen Determinize.Spec.Paper Determinize.Spec.FiniteModeldef coin : Core := .bernoulli (.sample .G) (.real (1/3))theorem coin_statistics : (⟨1, 1/3, 1/3⟩ : OutputStatistics).Matches    (bigStepMeasure (Subject.source.program coin)) := by stormtheorem coin_mean : Determinize.Spec.returnedExpectation (Subject.source.program coin) =    ((1/3 : Rat) : ℝ) := by storm```Set `STORM_PYTHON` to a Python with `tools/storm-requirements.txt` installed. The tacticfinds `tools/storm.py` above its working directory; `set_option determinize.storm.worker"/absolute/path/to/tools/storm.py"` overrides that search. It supports `Subject.source.program`,`Subject.determinized.program`, `interpret`, and the determinization of `interpret`. Otherprogram representations must first be reduced to one of these forms. Conditional momentsrequire positive return mass. The certified statistics are unnormalized.Each invocation checks a fresh model; prove output statistics once when several resultsare needed, then derive conditional moments with `Proof.FiniteModel.statistics_returned_moments`.Storm supplies exact rational value vectors, a divergent boundary, and descending-pathwitnesses. The tactic encodes the vectors as one string literal, decodes them into arrays,and reconstructs the graph from the program inside one compiled verifier. It makes one`native_decide` check of sparse equations and the witnesses, then appliesthe existing correspondence and moment theorems. This trusts Lean's compiler and runtime;the proof's axiom report includes the native check. Storm and JSON decoding supply candidatedata that the checker verifies. Reopening a compiled proof requires no Storm installation.Set `determinize.storm.native false` for kernel-only checking with no additional axioms;this builds separate per-state proofs and is substantially slower on large graphs.The checked-in `dreckon` benchmark (`n = 2`, three iterations) has 43,218 states and43,498 edges. The native tactic completed its statistics theorem in 67.9 seconds with3.60 GiB peak process RSS on the development Mac, including imports, exploration, andStorm. The kernel-only attempt had not finished after 15 minutes.From `lean/`, run standalone examples with `lake lean /absolute/path/to/example.lean`.Lake loads the precompiled tactic modules with this command; `lake env lean` aloneuses interpreted tactic code and can be slower.The native check uses array lookup and iterates over sparse edges. Kernel-only checkingshares syntax, environments, values, and continuation stacks through auxiliary Leandefinitions and uses balanced lookup tables. The tactic does not writeLean certificate source files or invoke the dense internal solver. `determinize.storm.maxStates`defaults to 100000; `determinize.storm.timeout` bounds the Storm subprocess and defaults to120 seconds. Lean's heartbeat limit bounds proof construction; increase `maxHeartbeats` forlarge models. `set_option trace.Determinize.storm true` reports the checking stages.Portable exports also share repeated structures. Additive result certificates check sparseedge equations and assemble the existing solution theorem. `--storm-export PREFIX` writesonly the transition, label, and reward files. `tools/storm.py --skip-certificate` uses thismode and skips Lean certificate generation as well as compilation.