|
1 | | -# Random-do notation |
| 1 | +# Random-do notation |
| 2 | + |
| 3 | +Write a probability program once using `rdo`, then interpret it in different measurable-space |
| 4 | +monads. `Measure` gives its distribution; `RandomM Ω P` samples from a probability source while |
| 5 | +preserving fresh source state. `SampleM Ω P` uses an infinite stream of independent `P` draws. |
| 6 | + |
| 7 | +To relate the two interpretations, add `rdo_program` to a polymorphic definition: |
| 8 | + |
| 9 | +```lean |
| 10 | +import RandomDo |
| 11 | +
|
| 12 | +open MeasureTheory |
| 13 | +universe v |
| 14 | +
|
| 15 | +def sumDraws {m : (α : Type) → [MeasurableSpace α] → Type v} |
| 16 | + [MeasurableSpaceMonad m] (coin : m Bool) : ℕ → m ℕ |
| 17 | + | 0 => rdo return 0 |
| 18 | + | n + 1 => rdo |
| 19 | + let b ← coin |
| 20 | + let s ← sumDraws coin n |
| 21 | + return b.toNat + s |
| 22 | +
|
| 23 | +attribute [rdo_program] sumDraws |
| 24 | +
|
| 25 | +example {Ω : Type*} [MeasurableSpace Ω] {P : Measure Ω} |
| 26 | + [IsProbabilityMeasure P] (coin : RandomM Ω P Bool) (n : ℕ) : |
| 27 | + (sumDraws coin n).law = sumDraws (m := Measure) coin.law n := |
| 28 | + sumDraws.law coin n |
| 29 | +``` |
| 30 | + |
| 31 | +The attribute leaves the original definition unchanged. It generates a recorded program |
| 32 | +(`.program`), a certificate (`.valid` and `.certified`), bridges to the original interpretations |
| 33 | +(`.sample_bridge` and `.measure_bridge`), and the resulting `.law` theorem. The proof uses |
| 34 | +`RDo.Program.Certified.law`, which holds for every certified program. The `rdo_valid` tactic can |
| 35 | +also construct certificates directly, leaving any unresolved measurability conditions as goals. |
| 36 | + |
| 37 | +The current automation supports returns, binds, measurable conditionals, sampler arguments, |
| 38 | +and one-argument sampler families. A family argument adds a joint-measurability hypothesis to |
| 39 | +the generated certificate and law theorem. `SampleM.ofKernel` provides this property for Markov |
| 40 | +kernels; `SampleM.ofMeasure` supplies independent draws from probability measures on standard |
| 41 | +Borel spaces. Both constructors use a stream of uniform draws from the unit interval. |
| 42 | + |
| 43 | +The attribute currently requires leading `{m} [MeasurableSpaceMonad m]` parameters and an |
| 44 | +independent universe parameter for the monad's output, as above. It attempts induction on the |
| 45 | +last explicit `Nat` argument. General recursion and loops need further support. Certification |
| 46 | +fails if a proof obligation remains; no global measurable-evaluation assumption is required. |
| 47 | + |
| 48 | +See `Test/Program.lean` for continuous and kernel examples, and `Test/SampleM.lean` for independent |
| 49 | +draws and the proof that a sum of `n` Bernoulli samples has the binomial distribution. |
0 commit comments