Skip to content

Commit 4bfac1c

Browse files
committed
Program logic cutover phase 2 baseline
1 parent 9e41ef3 commit 4bfac1c

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

ToMathlib/Control/Monad/Algebra.lean

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,83 @@ theorem triple_bind [LawfulMonad m] {pre : l} {x : m α} {cut : α → l}
114114
exact le_trans hx (wp_mono x hf)
115115

116116
end MAlgOrdered
117+
118+
/-! ## Transformer lifting instances -/
119+
120+
namespace MAlgOrdered
121+
122+
variable {m : Type u → Type v} {l : Type u}
123+
variable [Monad m] [LawfulMonad m] [CompleteLattice l] [MAlgOrdered m l]
124+
125+
/-- Lift an ordered monad algebra through `StateT`. -/
126+
noncomputable def instStateT (σ : Type u) :
127+
MAlgOrdered (StateT σ m) (σ → l) where
128+
μ x := fun s => MAlgOrdered.μ (x.run s >>= fun y => pure (y.1 y.2))
129+
μ_pure x := by
130+
funext s
131+
simp [StateT.run_pure, MAlgOrdered.μ_pure]
132+
μ_bind_mono f g hfg x := by
133+
intro s
134+
simp only [StateT.run_bind, bind_assoc]
135+
exact MAlgOrdered.μ_bind_mono
136+
(f := fun y => (f y.1).run y.2 >>= fun z => pure (z.1 z.2))
137+
(g := fun y => (g y.1).run y.2 >>= fun z => pure (z.1 z.2))
138+
(fun y => (hfg y.1) y.2)
139+
(x.run s)
140+
141+
attribute [instance] instStateT
142+
143+
/-- Lift an ordered monad algebra through `ReaderT`. -/
144+
noncomputable def instReaderT (ρ : Type u) :
145+
MAlgOrdered (ReaderT ρ m) (ρ → l) where
146+
μ x := fun r => MAlgOrdered.μ (x.run r >>= fun q => pure (q r))
147+
μ_pure x := by
148+
funext r
149+
simp [ReaderT.run_pure, MAlgOrdered.μ_pure]
150+
μ_bind_mono f g hfg x := by
151+
intro r
152+
simp only [ReaderT.run_bind, bind_assoc]
153+
exact MAlgOrdered.μ_bind_mono
154+
(f := fun a => (f a).run r >>= fun q => pure (q r))
155+
(g := fun a => (g a).run r >>= fun q => pure (q r))
156+
(fun a => (hfg a) r)
157+
(x.run r)
158+
159+
attribute [instance] instReaderT
160+
161+
/-- Lift an ordered monad algebra through `ExceptT` by interpreting exceptions as `⊥`. -/
162+
noncomputable def instExceptT (ε : Type u) :
163+
MAlgOrdered (ExceptT ε m) l where
164+
μ x := MAlgOrdered.μ <| (fun y : Except ε l =>
165+
match y with
166+
| Except.ok z => z
167+
| Except.error _ => ⊥) <$> x.run
168+
μ_pure x := by
169+
simp [MAlgOrdered.μ_pure]
170+
μ_bind_mono f g hfg x := by
171+
let collapse : Except ε l → l := fun y =>
172+
match y with
173+
| Except.ok z => z
174+
| Except.error _ => ⊥
175+
simpa [ExceptT.run_bind, collapse] using
176+
(MAlgOrdered.μ_bind_mono
177+
(f := fun y => collapse <$>
178+
(match y with
179+
| Except.ok a => (f a).run
180+
| Except.error e => pure (Except.error e)))
181+
(g := fun y => collapse <$>
182+
(match y with
183+
| Except.ok a => (g a).run
184+
| Except.error e => pure (Except.error e)))
185+
(by
186+
intro y
187+
cases y with
188+
| error e =>
189+
simp [collapse, MAlgOrdered.μ_pure]
190+
| ok a =>
191+
simpa [collapse] using hfg a)
192+
x.run)
193+
194+
attribute [instance] instExceptT
195+
196+
end MAlgOrdered

VCVio/ProgramLogic/Relational/Basic.lean

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,36 @@ def PointwiseDom (oa : OracleComp spec₁ α) (ob : OracleComp spec₂ β)
2929
/-- Equality relation helper for same-type outputs. -/
3030
def EqRel (α : Type) : RelPost α α := fun x y => x = y
3131

32+
/-! A minimal relational triple skeleton (coupling-ready API surface). -/
33+
34+
/-- Minimal relational triple skeleton for two computations and a relational postcondition. -/
35+
def RelTriple (oa : OracleComp spec₁ α) (ob : OracleComp spec₂ β)
36+
(post : RelPost α β) : Prop :=
37+
PointwiseDom oa ob post
38+
3239
@[simp]
3340
lemma pointwiseDom_refl (oa : OracleComp spec₁ α) :
3441
PointwiseDom (spec₁ := spec₁) (spec₂ := spec₁) oa oa (EqRel α) := by
3542
intro x y hxy
3643
subst hxy
3744
exact le_rfl
3845

46+
@[simp]
47+
lemma relTriple_refl (oa : OracleComp spec₁ α) :
48+
RelTriple (spec₁ := spec₁) (spec₂ := spec₁) oa oa (EqRel α) :=
49+
pointwiseDom_refl (spec₁ := spec₁) oa
50+
51+
lemma pointwiseDom_post_mono {oa : OracleComp spec₁ α} {ob : OracleComp spec₂ β}
52+
{post post' : RelPost α β}
53+
(h : PointwiseDom oa ob post) (hpost : ∀ ⦃x y⦄, post' x y → post x y) :
54+
PointwiseDom oa ob post' := by
55+
intro x y hxy
56+
exact h (hpost hxy)
57+
58+
lemma relTriple_post_mono {oa : OracleComp spec₁ α} {ob : OracleComp spec₂ β}
59+
{post post' : RelPost α β}
60+
(h : RelTriple oa ob post) (hpost : ∀ ⦃x y⦄, post' x y → post x y) :
61+
RelTriple oa ob post' :=
62+
pointwiseDom_post_mono h hpost
63+
3964
end OracleComp.ProgramLogic.Relational

VCVio/ProgramLogic/Unary/Examples.lean

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,33 @@ example (oa : OracleComp spec α) (p : α → Prop) [DecidablePred p] :
3636
probEvent_eq_wp_indicator (spec := spec) oa p
3737

3838
end OracleComp.ProgramLogic
39+
40+
namespace MAlgOrdered
41+
42+
open MAlgOrdered
43+
44+
universe v
45+
46+
variable {m : Type u → Type v} {L : Type u}
47+
variable [Monad m] [LawfulMonad m] [CompleteLattice L] [MAlgOrdered m L]
48+
variable {α β σ ρ ε : Type u}
49+
50+
example (x : StateT σ m α) (f : α → StateT σ m β) (post : β → σ → L) :
51+
MAlgOrdered.wp (m := StateT σ m) (l := σ → L) (x >>= f) post =
52+
MAlgOrdered.wp (m := StateT σ m) (l := σ → L) x
53+
(fun a => MAlgOrdered.wp (m := StateT σ m) (l := σ → L) (f a) post) := by
54+
simpa using (MAlgOrdered.wp_bind (m := StateT σ m) (l := σ → L) x f post)
55+
56+
example (x : ReaderT ρ m α) (f : α → ReaderT ρ m β) (post : β → ρ → L) :
57+
MAlgOrdered.wp (m := ReaderT ρ m) (l := ρ → L) (x >>= f) post =
58+
MAlgOrdered.wp (m := ReaderT ρ m) (l := ρ → L) x
59+
(fun a => MAlgOrdered.wp (m := ReaderT ρ m) (l := ρ → L) (f a) post) := by
60+
simpa using (MAlgOrdered.wp_bind (m := ReaderT ρ m) (l := ρ → L) x f post)
61+
62+
example (x : ExceptT ε m α) (f : α → ExceptT ε m β) (post : β → L) :
63+
MAlgOrdered.wp (m := ExceptT ε m) (l := L) (x >>= f) post =
64+
MAlgOrdered.wp (m := ExceptT ε m) (l := L) x
65+
(fun a => MAlgOrdered.wp (m := ExceptT ε m) (l := L) (f a) post) := by
66+
simpa using (MAlgOrdered.wp_bind (m := ExceptT ε m) (l := L) x f post)
67+
68+
end MAlgOrdered

VCVio/ProgramLogic/Unary/HoareTriple.lean

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ variable {ι : Type u} {spec : OracleSpec ι}
2525
variable [spec.Fintype] [spec.Inhabited]
2626
variable {α β : Type}
2727

28+
/-! ## API contract
29+
30+
- This unary quantitative interface is instantiated for `OracleComp spec`.
31+
- Probability/evaluation assumptions are `[spec.Fintype]` and `[spec.Inhabited]`.
32+
- The quantitative codomain is fixed to `ℝ≥0∞`.
33+
-/
34+
2835
/-- Expectation-style algebra for oracle computations returning `ℝ≥0∞`. -/
2936
noncomputable def μ (oa : OracleComp spec ℝ≥0∞) : ℝ≥0∞ :=
3037
∑' x, Pr[= x | oa] * x

0 commit comments

Comments
 (0)