diff --git a/RandomDo.lean b/RandomDo.lean index 44782b1..7bb313c 100644 --- a/RandomDo.lean +++ b/RandomDo.lean @@ -4,6 +4,7 @@ public import RandomDo.ForMathlib.MeasureTheory.MeasurableSpace.Embedding public import RandomDo.Measurable public import RandomDo.MeasurableSpaceMonad.Defs public import RandomDo.MeasurableSpaceMonad.ForIn +public import RandomDo.MeasurableSpaceMonad.Lift public import RandomDo.Monad.ForInInstances public import RandomDo.Monad.Instances public import RandomDo.NumLean.Binomial diff --git a/RandomDo/MeasurableSpaceMonad/Defs.lean b/RandomDo/MeasurableSpaceMonad/Defs.lean index 74d5fc8..c2bf08f 100644 --- a/RandomDo/MeasurableSpaceMonad/Defs.lean +++ b/RandomDo/MeasurableSpaceMonad/Defs.lean @@ -26,6 +26,9 @@ instance. `MeasurableSpaceFunctor` satisfies the laws of a functor on measurable spaces. * `LawfulMeasurableSpaceMonad`: A `Prop`-valued typeclass which states that a given `MeasurableSpaceMonad` satisfies the laws of a monad on measurable spaces. +* `Monad.toMeasurableSpaceMonad`: transforms a core monad into a measurable space monad by +forgetting the measurable space argument. +* `MId`: the identity measurable space monad. ## Notation @@ -149,6 +152,22 @@ class MeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Type extends MeasurableSpaceFunctor m, MeasurableSpacePure m, MeasurableSpaceBind m where mMap f μ := mBind μ (Function.comp mPure f) +/-- A (core) monad automatically defines a (not necessarily lawful) measurable space monad by +forgetting the measurable space argument. -/ +def _root_.Monad.toMeasurableSpaceMonad (m : Type u → Type v) (α : Type u) + [_mα : MeasurableSpace α] : Type v := m α + +instance {m : Type u → Type v} [Monad m] : + MeasurableSpaceMonad (Monad.toMeasurableSpaceMonad m) where + mPure := pure + mBind := bind + +/-- The identity measurable space monad. -/ +abbrev MId := Monad.toMeasurableSpaceMonad Id + +instance {α : Type*} [MeasurableSpace α] : MeasurableSpace (MId α) := + inferInstanceAs (MeasurableSpace α) + variable {m : (α : Type u) → [MeasurableSpace α] → Type v} {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] @@ -236,6 +255,15 @@ attribute [fun_prop] measurable_mPure measurable_mBind attribute [simp] mPure_mBind mBind_assoc mBind_mPure_comp attribute [grind <=] mPure_mBind +instance : LawfulMeasurableSpaceMonad MId where + mMap_const := rfl + id_mMap _ := rfl + measurable_mPure {_} _ := measurable_id + measurable_mBind hf := hf + mBind_mPure_comp _ _ := rfl + mPure_mBind _ _ _ := rfl + mBind_assoc _ _ _ _ _ := rfl + variable {m : (α : Type u) → [MeasurableSpace α] → Type v} [MeasurableSpaceMonad m] [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] [LawfulMeasurableSpaceMonad m] {α β γ : Type u} [MeasurableSpace α] [MeasurableSpace β] [MeasurableSpace γ] diff --git a/RandomDo/MeasurableSpaceMonad/Lift.lean b/RandomDo/MeasurableSpaceMonad/Lift.lean new file mode 100644 index 0000000..abdb682 --- /dev/null +++ b/RandomDo/MeasurableSpaceMonad/Lift.lean @@ -0,0 +1,168 @@ +/- +Copyright (c) 2026 David Ledvinka. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: David Ledvinka +-/ +module + +public import RandomDo.MeasurableSpaceMonad.Defs + +/-! +# Lifting between measurable space monads + +This file defines classes `MeasurableSpaceMonadLift` and `MeasurableSpaceMonadLiftT` for lifting +computations from one measurable space monad to another. They play the role of Lean core's +`MonadLift` and `MonadLiftT`. + +## Main definitions + +* `MeasurableSpaceMonadLift`: a lifting operation between two measurable space monads. +* `MeasurableSpaceMonadLiftT`: the reflexive, transitive lifting interface. +* `LawfulMeasurableSpaceMonadLift`: A `Prop`-valued typeclass which asserts that the `mMonadLift` +of a `MeasurableSpaceMonadLift` is measurable and preserves the monad operations. +* `LawfulMeasurableSpaceMonadLiftT`: A `Prop`-valued typeclass which asserts that the `mMonadLift` +of a `MeasurableSpaceMonadLiftT` is measurable and preserves the monad operations. +-/ + +@[expose] public section + +namespace MeasureTheory + +universe u v w + +/-- Computations in the measurable space monad `m` can be run in the measurable space monad `n`. -/ +class MeasurableSpaceMonadLift + (m : semiOutParam ((α : Type u) → [MeasurableSpace α] → Type v)) + (n : (α : Type u) → [MeasurableSpace α] → Type w) where + /-- Translates an action from `m` into `n`. -/ + mMonadLift {α : Type u} [MeasurableSpace α] : m α → n α + +/-- Computations in the measurable space monad `m` can be run in the measurable monad `n`. +Unlike `do` notation, `rdo` notation can't currently automatically insert these translations. + +This is the reflexive, transitive closure of `MeasurableSpaceMonadLift`. Clients that require one +measurable space monad to be liftable into another should request an instance of +`MeasurableSpaceMonadLiftT`. New instances should instead be defined for `MeasurableSpaceMonadLift` +itself. -/ +class MeasurableSpaceMonadLiftT + (m : (α : Type u) → [MeasurableSpace α] → Type v) + (n : (α : Type u) → [MeasurableSpace α] → Type w) where + /-- Translates an action from `m` into `n`. -/ + mMonadLift {α : Type u} [MeasurableSpace α] : m α → n α + +export MeasurableSpaceMonadLiftT (mMonadLift) + +@[inherit_doc mMonadLift] +abbrev mLiftM := @mMonadLift + +@[always_inline] +instance (m n o) [MeasurableSpaceMonadLift n o] [MeasurableSpaceMonadLiftT m n] : + MeasurableSpaceMonadLiftT m o where + mMonadLift x := MeasurableSpaceMonadLift.mMonadLift (m := n) (mMonadLift x) + +@[always_inline] +instance (m) : MeasurableSpaceMonadLiftT m m where + mMonadLift x := x + +-- This instance is set to low priority so that the composition and identity instances +-- are preferred. +instance (priority := low) {m : Type u → Type v} {n : Type u → Type w} [MonadLiftT m n] : + MeasurableSpaceMonadLiftT (Monad.toMeasurableSpaceMonad m) + (Monad.toMeasurableSpaceMonad n) where + mMonadLift := monadLift + +instance {m : (α : Type u) → [MeasurableSpace α] → Type v} [MeasurableSpacePure m] : + MeasurableSpaceMonadLiftT MId m where + mMonadLift x := MeasurableSpacePure.mPure x.run + +section Lawful + +/-- Asserts that the `mMonadLift` of a `MeasurableSpaceMonadLift` is measurable and preserves +`mPure` and `mBind` of measurable functions. -/ +class LawfulMeasurableSpaceMonadLift + (m : semiOutParam ((α : Type u) → [MeasurableSpace α] → Type v)) + (n : (α : Type u) → [MeasurableSpace α] → Type w) + [MeasurableSpaceMonad m] [MeasurableSpaceMonad n] + [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + [∀ α, [MeasurableSpace α] → MeasurableSpace (n α)] + [inst : MeasurableSpaceMonadLift m n] : Prop where + /-- `mMonadLift` is a measurable function. -/ + measurable_mMonadLift {α : Type u} [MeasurableSpace α] : + Measurable (inst.mMonadLift : m α → n α) + /-- Lifting preserves `mPure`. -/ + mMonadLift_mPure {α : Type u} [MeasurableSpace α] (a : α) : + inst.mMonadLift (mPure a) = mPure a + /-- Lifting preserves `mBind` of measurable functions. -/ + mMonadLift_mBind {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] + (ma : m α) (f : α → m β) (hf : Measurable f) : + inst.mMonadLift (ma >>=ₘ f) = inst.mMonadLift ma >>=ₘ (fun a => inst.mMonadLift (f a)) + +/-- Asserts that the `mMonadLift` of a `MeasurableSpaceMonadLiftT` is measurable and preserves +`mPure` and `mBind` of measurable functions. -/ +class LawfulMeasurableSpaceMonadLiftT + (m : (α : Type u) → [MeasurableSpace α] → Type v) + (n : (α : Type u) → [MeasurableSpace α] → Type w) + [MeasurableSpaceMonad m] [MeasurableSpaceMonad n] + [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + [∀ α, [MeasurableSpace α] → MeasurableSpace (n α)] + [inst : MeasurableSpaceMonadLiftT m n] : Prop where + /-- Lifting is a measurable function. -/ + measurable_mMonadLift {α : Type u} [MeasurableSpace α] : + Measurable (mMonadLift : m α → n α) + /-- Lifting preserves `mPure`. -/ + mMonadLift_mPure {α : Type u} [MeasurableSpace α] (a : α) : + inst.mMonadLift (mPure a) = mPure a + /-- Lifting preserves `mBind` of measurable functions. -/ + mMonadLift_mBind {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] + (ma : m α) (f : α → m β) (hf : Measurable f) : + inst.mMonadLift (ma >>=ₘ f) = mMonadLift ma >>=ₘ (fun a => mMonadLift (f a)) + +export LawfulMeasurableSpaceMonadLiftT (measurable_mMonadLift mMonadLift_mPure mMonadLift_mBind) + +attribute [fun_prop] LawfulMeasurableSpaceMonadLift.measurable_mMonadLift measurable_mMonadLift + +section + +variable {m : (α : Type u) → [MeasurableSpace α] → Type v} + {n : (α : Type u) → [MeasurableSpace α] → Type w} + [MeasurableSpaceMonad m] [MeasurableSpaceMonad n] + [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + [∀ α, [MeasurableSpace α] → MeasurableSpace (n α)] + [MeasurableSpaceMonadLiftT m n] [LawfulMeasurableSpaceMonadLiftT m n] + {α β : Type u} [MeasurableSpace α] [MeasurableSpace β] + +-- As in core, duplicate the laws for the alias so that `rw` matches either spelling. +@[simp] theorem mLiftM_mPure (a : α) : mLiftM (mPure a : m α) = mPure (f := n) a := + mMonadLift_mPure _ + +@[simp] theorem mLiftM_mBind (ma : m α) (f : α → m β) (hf : Measurable f) : + mLiftM (n := n) (ma >>=ₘ f) = mLiftM ma >>=ₘ fun a => mLiftM (f a) := + mMonadLift_mBind _ _ hf + +end + +instance (m n o) [MeasurableSpaceMonad m] [MeasurableSpaceMonad n] [MeasurableSpaceMonad o] + [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] + [∀ α, [MeasurableSpace α] → MeasurableSpace (n α)] + [∀ α, [MeasurableSpace α] → MeasurableSpace (o α)] + [MeasurableSpaceMonadLift n o] [MeasurableSpaceMonadLiftT m n] + [LawfulMeasurableSpaceMonadLift n o] [LawfulMeasurableSpaceMonadLiftT m n] : + LawfulMeasurableSpaceMonadLiftT m o where + measurable_mMonadLift {_} _ := + LawfulMeasurableSpaceMonadLift.measurable_mMonadLift.comp measurable_mMonadLift + mMonadLift_mPure a := by + simp [mMonadLift, LawfulMeasurableSpaceMonadLift.mMonadLift_mPure] + mMonadLift_mBind ma f hf := by + simp only [mMonadLift, mLiftM_mBind ma f hf] + exact LawfulMeasurableSpaceMonadLift.mMonadLift_mBind _ _ (by fun_prop) + +instance (m) [MeasurableSpaceMonad m] + [∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] : + LawfulMeasurableSpaceMonadLiftT m m where + measurable_mMonadLift {_} _ := measurable_id + mMonadLift_mPure _ := rfl + mMonadLift_mBind _ _ _ := rfl + +end Lawful + +end MeasureTheory diff --git a/RandomDo/Monad/Instances.lean b/RandomDo/Monad/Instances.lean index 04bf7c3..b92e24c 100644 --- a/RandomDo/Monad/Instances.lean +++ b/RandomDo/Monad/Instances.lean @@ -23,16 +23,6 @@ open MeasureTheory universe u v w -/-- A (core) monad automatically defines a (not necessarily lawful) measurable space monad by -forgetting the measurable space argument. -/ -def Monad.toMeasurableSpaceMonad (m : Type u → Type v) (α : Type u) [_mα : MeasurableSpace α] : - Type v := m α - -instance {m : Type u → Type v} [Monad m] : - MeasurableSpaceMonad (Monad.toMeasurableSpaceMonad m) where - mPure := pure - mBind := bind - /-- A measurable space monad for pseudo random number generation. -/ abbrev PseudoRandomM := Monad.toMeasurableSpaceMonad Rand diff --git a/Test/Bind.lean b/Test/Bind.lean index 25ca20c..822836c 100644 --- a/Test/Bind.lean +++ b/Test/Bind.lean @@ -18,60 +18,60 @@ open MeasureTheory ProbabilityTheory namespace Test.Bind /-- A single bind. -/ -def one (xs : List ℕ) : IdM ℕ := rdo - let x ← (xs.headD 0 : IdM ℕ) +def one (xs : List ℕ) : MId ℕ := rdo + let x ← (xs.headD 0 : MId ℕ) return x + 1 -example : IdM.run (one [4, 5]) = 5 := rfl +example : Id.run (one [4, 5]) = 5 := rfl /-- A chain of binds. -/ -def chain (a b : ℕ) : IdM ℕ := rdo - let x ← (a : IdM ℕ) - let y ← (b : IdM ℕ) +def chain (a b : ℕ) : MId ℕ := rdo + let x ← (a : MId ℕ) + let y ← (b : MId ℕ) return x * y -example : IdM.run (chain 6 7) = 42 := rfl +example : Id.run (chain 6 7) = 42 := rfl /-- `←` nested inside a larger expression is lifted out of it. -/ -def nested (a b : ℕ) : IdM ℕ := rdo - return (← (a : IdM ℕ)) + (← (b : IdM ℕ)) * 2 +def nested (a b : ℕ) : MId ℕ := rdo + return (← (a : MId ℕ)) + (← (b : MId ℕ)) * 2 -example : IdM.run (nested 3 4) = 11 := rfl +example : Id.run (nested 3 4) = 11 := rfl /-- A pure `let`, and a `have`, alongside the monadic ones. -/ -def mixedLets (a : ℕ) : IdM ℕ := rdo - let x ← (a : IdM ℕ) +def mixedLets (a : ℕ) : MId ℕ := rdo + let x ← (a : MId ℕ) let y := x + 1 have : 0 < y + 1 := Nat.succ_pos y return y * 2 -example : IdM.run (mixedLets 5) = 12 := rfl +example : Id.run (mixedLets 5) = 12 := rfl /-- Destructuring a bound pair. -/ -def destructure (p : ℕ × ℕ) : IdM ℕ := rdo - let (a, b) ← (p : IdM (ℕ × ℕ)) +def destructure (p : ℕ × ℕ) : MId ℕ := rdo + let (a, b) ← (p : MId (ℕ × ℕ)) return a + b -example : IdM.run (destructure (3, 4)) = 7 := rfl +example : Id.run (destructure (3, 4)) = 7 := rfl /-- A statement in the middle of a block is sequenced, not dropped. -/ -def sequenced (a : ℕ) : IdM ℕ := rdo +def sequenced (a : ℕ) : MId ℕ := rdo let mut s := 0 - (pure () : IdM PUnit) + (pure () : MId PUnit) s := s + a return s -example : IdM.run (sequenced 9) = 9 := rfl +example : Id.run (sequenced 9) = 9 := rfl /-- `return` in straight-line code drops the rest of the block. -/ -def earlyReturn (a : ℕ) : IdM ℕ := rdo +def earlyReturn (a : ℕ) : MId ℕ := rdo if a = 0 then return 100 return a -example : IdM.run (earlyReturn 0) = 100 := rfl +example : Id.run (earlyReturn 0) = 100 := rfl -example : IdM.run (earlyReturn 7) = 7 := rfl +example : Id.run (earlyReturn 7) = 7 := rfl /-- The same shapes at `Measure`, where the binds are genuine integrals. -/ noncomputable def twoCoins : Measure Bool := rdo diff --git a/Test/Common.lean b/Test/Common.lean index 83934c5..bce1ce6 100644 --- a/Test/Common.lean +++ b/Test/Common.lean @@ -21,16 +21,6 @@ open MeasureTheory ProbabilityTheory @[expose] public section -universe u - -/-- A deterministic `MeasurableSpaceMonad`. An `rdo` program written at `IdM` denotes a value, so -the tests can state what a program computes and not merely that it typechecks. -/ -abbrev IdM := Monad.toMeasurableSpaceMonad Id - -/-- Read the value out of a deterministic `rdo` program. `IdM α` is definitionally `α` but not -reducibly so, which is what otherwise stops numerals and `rfl` from seeing through it. -/ -def IdM.run {α : Type u} [MeasurableSpace α] (x : IdM α) : α := x - /-- The fair coin, as a probability measure on `Bool`. -/ noncomputable def fairCoin : Measure Bool := bernoulliMeasure true false ⟨(1 : ℝ) / 2, by norm_num⟩ diff --git a/Test/Control.lean b/Test/Control.lean index a6b3069..0c8189f 100644 --- a/Test/Control.lean +++ b/Test/Control.lean @@ -19,29 +19,29 @@ open MeasureTheory ProbabilityTheory namespace Test.Control /-- `if … then … else`, branching the whole rest of the block. -/ -def branch (n : ℕ) : IdM ℕ := rdo +def branch (n : ℕ) : MId ℕ := rdo if n = 0 then return 100 else return n -example : IdM.run (branch 0) = 100 := rfl +example : Id.run (branch 0) = 100 := rfl -example : IdM.run (branch 5) = 5 := rfl +example : Id.run (branch 5) = 5 := rfl /-- `if` with no `else`: the block carries on afterwards. -/ -def clampZero (n : ℕ) : IdM ℕ := rdo +def clampZero (n : ℕ) : MId ℕ := rdo let mut s := n if n = 0 then s := 100 return s -example : IdM.run (clampZero 0) = 100 := rfl +example : Id.run (clampZero 0) = 100 := rfl -example : IdM.run (clampZero 5) = 5 := rfl +example : Id.run (clampZero 5) = 5 := rfl /-- Nested branches. -/ -def nestedIf (a b : ℕ) : IdM ℕ := rdo +def nestedIf (a b : ℕ) : MId ℕ := rdo if a = 0 then if b = 0 then return 0 @@ -50,64 +50,64 @@ def nestedIf (a b : ℕ) : IdM ℕ := rdo else return 2 -example : IdM.run (nestedIf 0 0) = 0 := rfl +example : Id.run (nestedIf 0 0) = 0 := rfl -example : IdM.run (nestedIf 0 1) = 1 := rfl +example : Id.run (nestedIf 0 1) = 1 := rfl -example : IdM.run (nestedIf 1 0) = 2 := rfl +example : Id.run (nestedIf 1 0) = 2 := rfl /-- A dependent `if`, whose branch uses the proof it introduces. -/ -def headOr (xs : List ℕ) : IdM ℕ := rdo +def headOr (xs : List ℕ) : MId ℕ := rdo if h : 0 < xs.length then return xs[0]'h else return 0 -example : IdM.run (headOr [7, 8]) = 7 := rfl +example : Id.run (headOr [7, 8]) = 7 := rfl -example : IdM.run (headOr []) = 0 := rfl +example : Id.run (headOr []) = 0 := rfl /-- A `match` on a value bound by `←`. -/ -def matchArrow (o : Option ℕ) : IdM ℕ := rdo - match ← (o : IdM (Option ℕ)) with +def matchArrow (o : Option ℕ) : MId ℕ := rdo + match ← (o : MId (Option ℕ)) with | none => return 0 | some n => return n + 1 -example : IdM.run (matchArrow (some 4)) = 5 := rfl +example : Id.run (matchArrow (some 4)) = 5 := rfl -example : IdM.run (matchArrow none) = 0 := rfl +example : Id.run (matchArrow none) = 0 := rfl /-- A pure `match` inside the block. -/ -def matchPure (o : Option ℕ) : IdM ℕ := rdo - let v ← (o : IdM (Option ℕ)) +def matchPure (o : Option ℕ) : MId ℕ := rdo + let v ← (o : MId (Option ℕ)) match v with | none => return 0 | some n => return n + 1 -example : IdM.run (matchPure (some 4)) = 5 := rfl +example : Id.run (matchPure (some 4)) = 5 := rfl /-- `if let`. -/ -def ifLet (o : Option ℕ) : IdM ℕ := rdo - let v ← (o : IdM (Option ℕ)) +def ifLet (o : Option ℕ) : MId ℕ := rdo + let v ← (o : MId (Option ℕ)) if let some n := v then return n + 1 else return 0 -example : IdM.run (ifLet (some 4)) = 5 := rfl +example : Id.run (ifLet (some 4)) = 5 := rfl -example : IdM.run (ifLet none) = 0 := rfl +example : Id.run (ifLet none) = 0 := rfl /-- `unless`. -/ -def unlessFlag (b : Bool) : IdM ℕ := rdo +def unlessFlag (b : Bool) : MId ℕ := rdo let mut s := 0 unless b do s := 1 return s -example : IdM.run (unlessFlag true) = 0 := rfl +example : Id.run (unlessFlag true) = 0 := rfl -example : IdM.run (unlessFlag false) = 1 := rfl +example : Id.run (unlessFlag false) = 1 := rfl /-- Branching on a value drawn from a genuine distribution. -/ noncomputable def fairCoinBranch : Measure ℕ := rdo diff --git a/Test/Gaps.lean b/Test/Gaps.lean index 39aeb1c..40a8da5 100644 --- a/Test/Gaps.lean +++ b/Test/Gaps.lean @@ -31,7 +31,7 @@ error: No `ControlInfo` inference handler found for `RDo.rdoFor` in syntax Register a handler with `@[doElem_control_info RDo.rdoFor]`. -/ #guard_msgs (whitespace := lax) in -def nestedLoops (xs ys : List ℕ) : IdM ℕ := rdo +def nestedLoops (xs ys : List ℕ) : MId ℕ := rdo let mut s := 0 for x in xs rdo for y in ys rdo @@ -47,12 +47,12 @@ core's `doFor` and so asks for a `ForIn` instance. Supporting them needs the mac /-- error: failed to synthesize instance of type class - ForIn IdM Lean.Loop ?α + ForIn MId Lean.Loop ?α Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command. -/ #guard_msgs in -def whileLoop : IdM ℕ := rdo +def whileLoop : MId ℕ := rdo let mut i := 0 while i < 3 do i := i + 1 @@ -85,12 +85,12 @@ TODO: instances exist for `List`, `Array` and `Vector` only. A range is the one /-- error: failed to synthesize instance of type class - MeasurableSpaceForIn IdM Std.Legacy.Range ?α + MeasurableSpaceForIn MId Std.Legacy.Range ?α Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command. -/ #guard_msgs in -def overRange : IdM ℕ := rdo +def overRange : MId ℕ := rdo let mut s := 0 for _ in [0:3] rdo s := s + 1 @@ -98,12 +98,12 @@ def overRange : IdM ℕ := rdo /-- error: failed to synthesize instance of type class - MeasurableSpaceForIn IdM (Finset ℕ) ?α + MeasurableSpaceForIn MId (Finset ℕ) ?α Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command. -/ #guard_msgs in -def overFinset : IdM ℕ := rdo +def overFinset : MId ℕ := rdo let mut s := 0 for _ in Finset.range 3 rdo s := s + 1 diff --git a/Test/Instances.lean b/Test/Instances.lean index 1105bdb..85dd037 100644 --- a/Test/Instances.lean +++ b/Test/Instances.lean @@ -45,8 +45,8 @@ def twice (x : m ℕ) : m ℕ := rdo let b ← x return a + b -/-- At `IdM` it computes. -/ -example : IdM.run (twice ((3 : ℕ) : IdM ℕ)) = 6 := rfl +/-- At `MId` it computes. -/ +example : Id.run (twice (m := MId) (3 : ℕ)) = 6 := rfl /-- At `Measure` it denotes a distribution. -/ noncomputable def twiceCoin : Measure ℕ := twice (m := Measure) (rdo @@ -66,7 +66,7 @@ def sumOver (xs : List ℕ) (f : ℕ → m ℕ) : m ℕ := rdo s := s + y return s -example : IdM.run (sumOver [1, 2, 3] (fun x ↦ ((x * 2 : ℕ) : IdM ℕ))) = 12 := rfl +example : Id.run (sumOver (m := MId) [1, 2, 3] (fun x ↦ x * 2)) = 12 := rfl noncomputable def sumOverMeasure : Measure ℕ := sumOver (m := Measure) [1, 2, 3] (fun x ↦ rdo diff --git a/Test/Loops.lean b/Test/Loops.lean index ff77c85..efa14e8 100644 --- a/Test/Loops.lean +++ b/Test/Loops.lean @@ -22,46 +22,46 @@ open MeasureTheory ProbabilityTheory namespace Test.Loops /-- A loop over a `List`. -/ -def sumList (xs : List ℕ) : IdM ℕ := rdo +def sumList (xs : List ℕ) : MId ℕ := rdo let mut s := 0 for x in xs rdo s := s + x return s -example : IdM.run (sumList [1, 2, 3]) = 6 := rfl +example : Id.run (sumList [1, 2, 3]) = 6 := rfl -example : IdM.run (sumList []) = 0 := rfl +example : Id.run (sumList []) = 0 := rfl /-- A loop over an `Array`. -/ -def sumArray (xs : Array ℕ) : IdM ℕ := rdo +def sumArray (xs : Array ℕ) : MId ℕ := rdo let mut s := 0 for x in xs rdo s := s + x return s -example : IdM.run (sumArray #[1, 2, 3]) = 6 := rfl +example : Id.run (sumArray #[1, 2, 3]) = 6 := rfl /-- A loop over a `Vector`. -/ -def sumVector (xs : Vector ℕ 3) : IdM ℕ := rdo +def sumVector (xs : Vector ℕ 3) : MId ℕ := rdo let mut s := 0 for x in xs rdo s := s + x return s -example : IdM.run (sumVector #v[1, 2, 3]) = 6 := rfl +example : Id.run (sumVector #v[1, 2, 3]) = 6 := rfl /-- `for h : x in xs`, which hands the body a proof that `x` is in the collection. -/ -def sumWithProof (xs : List ℕ) : IdM ℕ := rdo +def sumWithProof (xs : List ℕ) : MId ℕ := rdo let mut s := 0 for h : x in xs rdo have : x ∈ xs := h s := s + x return s -example : IdM.run (sumWithProof [1, 2, 3]) = 6 := rfl +example : Id.run (sumWithProof [1, 2, 3]) = 6 := rfl /-- Several mutable variables carried through one loop. -/ -def sumAndCount (xs : List ℕ) : IdM (ℕ × ℕ) := rdo +def sumAndCount (xs : List ℕ) : MId (ℕ × ℕ) := rdo let mut s := 0 let mut n := 0 for x in xs rdo @@ -69,10 +69,10 @@ def sumAndCount (xs : List ℕ) : IdM (ℕ × ℕ) := rdo n := n + 1 return (s, n) -example : IdM.run (sumAndCount [1, 2, 3]) = (6, 3) := rfl +example : Id.run (sumAndCount [1, 2, 3]) = (6, 3) := rfl /-- `break`. -/ -def sumUntilZero (xs : List ℕ) : IdM ℕ := rdo +def sumUntilZero (xs : List ℕ) : MId ℕ := rdo let mut s := 0 for x in xs rdo if x = 0 then @@ -80,12 +80,12 @@ def sumUntilZero (xs : List ℕ) : IdM ℕ := rdo s := s + x return s -example : IdM.run (sumUntilZero [1, 2, 0, 4]) = 3 := rfl +example : Id.run (sumUntilZero [1, 2, 0, 4]) = 3 := rfl -example : IdM.run (sumUntilZero [1, 2, 3]) = 6 := rfl +example : Id.run (sumUntilZero [1, 2, 3]) = 6 := rfl /-- `continue`. -/ -def sumSkippingZero (xs : List ℕ) : IdM ℕ := rdo +def sumSkippingZero (xs : List ℕ) : MId ℕ := rdo let mut s := 0 for x in xs rdo if x = 0 then @@ -93,21 +93,21 @@ def sumSkippingZero (xs : List ℕ) : IdM ℕ := rdo s := s + x return s -example : IdM.run (sumSkippingZero [1, 0, 3]) = 4 := rfl +example : Id.run (sumSkippingZero [1, 0, 3]) = 4 := rfl /-- An early `return` out of a loop, which elaborates through `Break.runK`. -/ -def firstNonzero (xs : List ℕ) : IdM (Option ℕ) := rdo +def firstNonzero (xs : List ℕ) : MId (Option ℕ) := rdo for x in xs rdo if x ≠ 0 then return some x return none -example : IdM.run (firstNonzero [0, 0, 3, 4]) = some 3 := rfl +example : Id.run (firstNonzero [0, 0, 3, 4]) = some 3 := rfl -example : IdM.run (firstNonzero [0, 0]) = none := rfl +example : Id.run (firstNonzero [0, 0]) = none := rfl /-- An early `return` from a loop that also carries mutable state. -/ -def runningSumOver (xs : List ℕ) (limit : ℕ) : IdM ℕ := rdo +def runningSumOver (xs : List ℕ) (limit : ℕ) : MId ℕ := rdo let mut s := 0 for x in xs rdo s := s + x @@ -115,20 +115,20 @@ def runningSumOver (xs : List ℕ) (limit : ℕ) : IdM ℕ := rdo return s return 0 -example : IdM.run (runningSumOver [1, 2, 3, 4] 4) = 6 := rfl +example : Id.run (runningSumOver [1, 2, 3, 4] 4) = 6 := rfl -example : IdM.run (runningSumOver [1, 2] 100) = 0 := rfl +example : Id.run (runningSumOver [1, 2] 100) = 0 := rfl /-- A loop with no mutable state, whose only effect is an early return. -/ -def containsZero (xs : List ℕ) : IdM Bool := rdo +def containsZero (xs : List ℕ) : MId Bool := rdo for x in xs rdo if x = 0 then return true return false -example : IdM.run (containsZero [1, 0]) = true := rfl +example : Id.run (containsZero [1, 0]) = true := rfl -example : IdM.run (containsZero [1, 2]) = false := rfl +example : Id.run (containsZero [1, 2]) = false := rfl /-- A loop whose body binds monadically, at `Measure`. -/ noncomputable def countHeads (n : ℕ) : Measure ℕ := rdo