diff --git a/PolyFun/PFunctor/Free/WP/Charge.lean b/PolyFun/PFunctor/Free/WP/Charge.lean index e75bfa05..05ee9578 100644 --- a/PolyFun/PFunctor/Free/WP/Charge.lean +++ b/PolyFun/PFunctor/Free/WP/Charge.lean @@ -34,6 +34,22 @@ leaf. This module states its `ℕ∞` laws in fold form and connects roll budget * `triple_liftM_of_isChargeBound`: the same over a charge bound in any cost type, with ranks indexed by the amount spent. * `triple_liftM_invariant`: the unranked case, in which every call preserves one assertion. +* `wpFold_chargeUnder_liftBind_le_iff`, `wpFold_chargeUnder_liftBind_const`, + `wpFold_chargeUnder_liftBind_const_add`: the fold of `OpSpec.chargeUnder c allows` at a call, + at a general postcondition; `isRollBound_iff_wpFold_chargeUnder_true` is the case of every + answer allowed. +* `wpFold_chargeUnder_pure_le`, `wpFold_chargeUnder_liftBind_le`, + `wpFold_chargeUnder_bind_le_add`, `wpFold_ite_le_max_enat`, `wpFold_dite_le_max_enat`: the + clauses of a fold rule whose bounds are natural numbers cast to `ℕ∞`. +* `wpFold_demonicUnder_liftBind_const`: a call whose continuation ignores the answer keeps the + leaf invariant of the continuation. +* `wpFold_charge_eq_chargeUnder_true`: the worst-case charge fold is the fold of `chargeUnder` + with every answer allowed. +* `wpFold_chargeUnder_pi_apply`, `wpFold_chargeUnder_pi_le_iff`, + `wpFold_chargeUnder_price_le_sum`, `wpFold_chargeUnder_price_le`: vector charges fold + componentwise, and a price turns a vector bound into a scalar one. +* `OpSpec.chargeUnder_eq`, `OpSpec.demonicUnder_eq`, `wpFold_chargeUnder_eq_restrict`: the + readings over allowed answers are the readings of the restriction `P.restrict allows`. Under the upper-bound reading `(OpSpec.charge c).toUpperWPMonad (OpSpec.charge_mono c)`, a triple `⦃ toDual t ⦄ x ⦃ post ⦄` states that the charge-to-go of `x` is at most `t`. @@ -167,4 +183,252 @@ end Ranked end FreeM +/-! ## The charge reading over allowed answers as a reading of the restriction -/ + +namespace OpSpec + +variable {P : PFunctor.{uA, uB}} + +/-- `chargeUnder c allows` is the charge reading of the restriction, with the continuation read +through the inclusion of directions. -/ +theorem chargeUnder_eq {R : Type w} [CompleteLattice R] [Add R] (c : P.A → R) + (allows : (a : P.A) → P.B a → Prop) : + chargeUnder c allows = + fun a k => charge (P := P.restrict allows) c a (k ∘ Subtype.val) := by + funext a k + rw [chargeUnder_apply] + exact congrArg (c a + ·) iSup_subtype' + +/-- `demonicUnder allows` is the demonic reading of the restriction, with the continuation read +through the inclusion of directions. -/ +theorem demonicUnder_eq (allows : (a : P.A) → P.B a → Prop) : + demonicUnder allows = fun a k => demonic (P.restrict allows) a (k ∘ Subtype.val) := by + funext a k + exact propext ⟨fun h b => h b.1 b.2, fun h b hb => h ⟨b, hb⟩⟩ + +end OpSpec + +namespace FreeM + +variable {P : PFunctor.{uA, uB}} + +/-! ## The charge fold over allowed answers -/ + +section Under + +variable {α : Type v} + +/-- A natural-number budget `n` bounds the charge fold of a call at `a` iff `c a ≤ n` and +`n - c a` bounds the charge fold of the continuation at every allowed answer. -/ +theorem wpFold_chargeUnder_liftBind_le_iff (c : P.A → ℕ) (allows : (a : P.A) → P.B a → Prop) + (a : P.A) (r : P.B a → FreeM P α) (n : ℕ) {post : α → ℕ∞} : + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (FreeM.liftBind a r) post ≤ n ↔ + c a ≤ n ∧ ∀ b, allows a b → + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (r b) post ≤ + ((n - c a : ℕ) : ℕ∞) := by + rw [wpFold_liftBind, OpSpec.chargeUnder_apply] + constructor + · intro h + have hc : c a ≤ n := by exact_mod_cast le_self_add.trans h + refine ⟨hc, fun b hb => ?_⟩ + have h' : (c a : ℕ∞) + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (r b) post ≤ + (c a : ℕ∞) + ((n - c a : ℕ) : ℕ∞) := by + rw [← Nat.cast_add, Nat.add_sub_cancel' hc] + refine le_trans ?_ h + gcongr + exact le_iSup₂_of_le (f := fun b (_ : allows a b) => + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (r b) post) b hb le_rfl + exact (ENat.add_le_add_iff_left (ENat.natCast_ne_top _)).1 h' + · rintro ⟨hc, hr⟩ + calc _ ≤ (c a : ℕ∞) + ((n - c a : ℕ) : ℕ∞) := by gcongr; exact iSup₂_le hr + _ = n := by rw [← Nat.cast_add, Nat.add_sub_cancel' hc] + +/-- A call at `a` whose continuation ignores the answer costs `c a` plus the continuation, when +`a` has an allowed answer. This is `costUnder_liftBind_const` at a general postcondition. -/ +theorem wpFold_chargeUnder_liftBind_const {R : Type w} [CompleteLattice R] [Add R] + (c : P.A → R) (allows : (a : P.A) → P.B a → Prop) (a : P.A) {b : P.B a} (hb : allows a b) + (y : FreeM P α) (post : α → R) : + wpFold (OpSpec.chargeUnder c allows) (FreeM.liftBind a fun _ => y) post = + c a + wpFold (OpSpec.chargeUnder c allows) y post := by + rw [wpFold_liftBind, OpSpec.chargeUnder_apply] + exact congrArg (c a + ·) + (le_antisymm (iSup₂_le fun _ _ => le_rfl) (le_iSup₂_of_le b hb le_rfl)) + +/-- Ticks merge: a call at `a` with charge `c a₁ + c a₂` whose continuation ignores the answer +has the fold of a call at `a₁` followed by a call at `a₂`, when each has an allowed answer. -/ +theorem wpFold_chargeUnder_liftBind_const_add {R : Type w} [CompleteLattice R] [AddSemigroup R] + (c : P.A → R) (allows : (a : P.A) → P.B a → Prop) {a a₁ a₂ : P.A} + (hc : c a = c a₁ + c a₂) {b : P.B a} (hb : allows a b) {b₁ : P.B a₁} (hb₁ : allows a₁ b₁) + {b₂ : P.B a₂} (hb₂ : allows a₂ b₂) (y : FreeM P α) (post : α → R) : + wpFold (OpSpec.chargeUnder c allows) (FreeM.liftBind a fun _ => y) post = + wpFold (OpSpec.chargeUnder c allows) + (FreeM.liftBind a₁ fun _ => FreeM.liftBind a₂ fun _ => y) post := by + rw [wpFold_chargeUnder_liftBind_const c allows a hb, + wpFold_chargeUnder_liftBind_const c allows a₁ hb₁, + wpFold_chargeUnder_liftBind_const c allows a₂ hb₂, hc, add_assoc] + +/-- With every answer allowed, a bound on the charge fold is the subtractive roll bound: the +`chargeUnder` form of `isRollBound_iff_wpFold_charge`. -/ +theorem isRollBound_iff_wpFold_chargeUnder_true {α : Type uB} (c : P.A → ℕ) (x : FreeM P α) + (n : ℕ) : + IsRollBound x n (fun a b => c a ≤ b) (fun a b => b - c a) ↔ + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) fun _ _ => True) x (fun _ => 0) ≤ + n := by + rw [OpSpec.chargeUnder_true, isRollBound_iff_wpFold_charge] + +end Under + +/-! ## The clauses of the fold rule + +Each clause bounds the charge fold of a program by a natural number cast to `ℕ∞` from the +bounds of its parts, so that a bound computed clause by clause is a term of `ℕ`. -/ + +section Rule + +variable {X : Type v} {Y : Type z} + +/-- A return charges nothing. -/ +theorem wpFold_chargeUnder_pure_le (c : P.A → ℕ) (allows : (a : P.A) → P.B a → Prop) (x : X) + (n : ℕ) : + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (FreeM.pure x : FreeM P X) + (fun _ => 0) ≤ (n : ℕ∞) := + zero_le + +/-- A call at `a` is bounded by a bound `m` of its charge plus a bound `n` of its continuation at +every allowed answer. -/ +theorem wpFold_chargeUnder_liftBind_le (c : P.A → ℕ) (allows : (a : P.A) → P.B a → Prop) + (a : P.A) (r : P.B a → FreeM P X) (m n : ℕ) {post : X → ℕ∞} (hc : c a ≤ m) + (hr : ∀ b, allows a b → + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (r b) post ≤ (n : ℕ∞)) : + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (FreeM.liftBind a r) post ≤ + ((m + n : ℕ) : ℕ∞) := by + rw [wpFold_liftBind, OpSpec.chargeUnder_apply, Nat.cast_add] + exact add_le_add (by exact_mod_cast hc) (iSup₂_le hr) + +/-- The bind clause with natural-number bounds: a bound `m` of `x` and a bound `n` of `f a` at +every return `a` satisfying the leaf invariant `I` of `x` add to a bound of `x.bind f`. Only `x` +is bounded at the postcondition `0`. -/ +theorem wpFold_chargeUnder_bind_le_add (c : P.A → ℕ) (allows : (a : P.A) → P.B a → Prop) + (x : FreeM P X) (f : X → FreeM P Y) (I : X → Prop) (m n : ℕ) {post : Y → ℕ∞} + (hx : wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) x (fun _ => 0) ≤ (m : ℕ∞)) + (hf : ∀ a, I a → + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (f a) post ≤ (n : ℕ∞)) + (hI : wpFold (OpSpec.demonicUnder allows) x I) : + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (x.bind f) post ≤ + ((m + n : ℕ) : ℕ∞) := by + rw [wpFold_bind', Nat.cast_add] + exact (wpFold_chargeUnder_le_costUnder_add hI _).trans (add_le_add hx (iSup₂_le hf)) + +/-- A call whose continuation ignores the answer keeps the leaf invariant of the continuation. -/ +theorem wpFold_demonicUnder_liftBind_const (allows : (a : P.A) → P.B a → Prop) (a : P.A) + {y : FreeM P X} {I : X → Prop} (h : wpFold (OpSpec.demonicUnder allows) y I) : + wpFold (OpSpec.demonicUnder allows) (FreeM.liftBind a fun _ => y) I := + fun _ _ => h + +/-- An `if` of programs is bounded in `ℕ∞` by the maximum of the bounds of its branches. -/ +theorem wpFold_ite_le_max_enat (Φ : OpSpec P ℕ∞) (p : Prop) [Decidable p] (x y : FreeM P X) + (post : X → ℕ∞) {m n : ℕ} (hx : wpFold Φ x post ≤ (m : ℕ∞)) (hy : wpFold Φ y post ≤ (n : ℕ∞)) : + wpFold Φ (if p then x else y) post ≤ ((max m n : ℕ) : ℕ∞) := by + split + · exact hx.trans (by exact_mod_cast le_max_left m n) + · exact hy.trans (by exact_mod_cast le_max_right m n) + +/-- A dependent `if` of programs is bounded in `ℕ∞` by the maximum of the bounds of its +branches. -/ +theorem wpFold_dite_le_max_enat (Φ : OpSpec P ℕ∞) (p : Prop) [Decidable p] (x : p → FreeM P X) + (y : ¬p → FreeM P X) (post : X → ℕ∞) {m n : ℕ} (hx : ∀ h, wpFold Φ (x h) post ≤ (m : ℕ∞)) + (hy : ∀ h, wpFold Φ (y h) post ≤ (n : ℕ∞)) : + wpFold Φ (if h : p then x h else y h) post ≤ ((max m n : ℕ) : ℕ∞) := by + split + · exact (hx _).trans (by exact_mod_cast le_max_left m n) + · exact (hy _).trans (by exact_mod_cast le_max_right m n) + +/-- The worst-case charge fold is the charge fold with every answer allowed: the `wpFold` form of +`OpSpec.chargeUnder_true`. -/ +theorem wpFold_charge_eq_chargeUnder_true {R : Type w} [CompleteLattice R] [Add R] + (c : P.A → R) (x : FreeM P X) (post : X → R) : + wpFold (OpSpec.charge c) x post = wpFold (OpSpec.chargeUnder c fun _ _ => True) x post := by + rw [OpSpec.chargeUnder_true] + +end Rule + +/-! ## Vector charges and prices + +A charge `c : P.A → ι → R` with values in the pointwise lattice `ι → R` counts one resource per +index `i`. Suprema and sums in `ι → R` are pointwise, so the component `i` of the charge fold is +the scalar charge fold at `fun a => c a i`. Pricing the components by `p : ι → ℕ∞` turns a vector +bound `u` into the scalar bound `∑ i, p i * u i` on the fold at the priced charge. -/ + +section Vector + +variable {α : Type v} + +/-- The component `i` of the charge fold at a vector charge is the charge fold at the component +`i` of the charge, with the component `i` of the postcondition. -/ +theorem wpFold_chargeUnder_pi_apply {ι : Type*} {R : Type w} [CompleteLattice R] [Add R] + (c : P.A → ι → R) (allows : (a : P.A) → P.B a → Prop) (x : FreeM P α) (post : α → ι → R) + (i : ι) : + wpFold (OpSpec.chargeUnder c allows) x post i = + wpFold (OpSpec.chargeUnder (fun a => c a i) allows) x (fun r => post r i) := by + induction x with + | pure r => rfl + | lift_bind a r ih => + change (c a + ⨆ b, ⨆ (_ : allows a b), wpFold (OpSpec.chargeUnder c allows) (r b) post) i = + c a i + ⨆ b, ⨆ (_ : allows a b), + wpFold (OpSpec.chargeUnder (fun a => c a i) allows) (r b) (fun r => post r i) + simp only [Pi.add_apply, iSup_apply, ih] + +/-- A vector bound on the charge fold is the family of the component bounds, each a bound on the +scalar charge fold at the component of the charge. -/ +theorem wpFold_chargeUnder_pi_le_iff {ι : Type*} {R : Type w} [CompleteLattice R] [Add R] + (c : P.A → ι → R) (allows : (a : P.A) → P.B a → Prop) (x : FreeM P α) (post : α → ι → R) + (u : ι → R) : + wpFold (OpSpec.chargeUnder c allows) x post ≤ u ↔ + ∀ i, wpFold (OpSpec.chargeUnder (fun a => c a i) allows) x (fun r => post r i) ≤ u i := by + simp only [Pi.le_def, wpFold_chargeUnder_pi_apply] + +/-- The fold at a priced charge is at most the priced vector fold: at the price `p`, the charge +`fun a => ∑ i, p i * c a i` folds to at most `∑ i, p i * (fold at c) i`. -/ +theorem wpFold_chargeUnder_price_le_sum {ι : Type*} [Fintype ι] (c : P.A → ι → ℕ∞) + (allows : (a : P.A) → P.B a → Prop) (p : ι → ℕ∞) (x : FreeM P α) (post : α → ι → ℕ∞) : + wpFold (OpSpec.chargeUnder (fun a => ∑ i, p i * c a i) allows) x + (fun r => ∑ i, p i * post r i) ≤ + ∑ i, p i * wpFold (OpSpec.chargeUnder c allows) x post i := by + induction x with + | pure r => exact le_rfl + | lift_bind a r ih => + change (∑ i, p i * c a i) + ⨆ b, ⨆ (_ : allows a b), + wpFold (OpSpec.chargeUnder (fun a => ∑ i, p i * c a i) allows) (r b) + (fun r => ∑ i, p i * post r i) ≤ + ∑ i, p i * (c a + ⨆ b, ⨆ (_ : allows a b), + wpFold (OpSpec.chargeUnder c allows) (r b) post) i + simp only [Pi.add_apply, mul_add, Finset.sum_add_distrib] + refine add_le_add le_rfl (iSup₂_le fun b hb => (ih b).trans ?_) + refine Finset.sum_le_sum fun i _ => mul_le_mul_right ?_ (p i) + exact le_iSup₂_of_le (f := fun b (_ : allows a b) => + wpFold (OpSpec.chargeUnder c allows) (r b) post) b hb le_rfl i + +/-- **Pricing a vector bound.** If the charge fold at the vector charge `c` is at most `u`, then +at every price `p` the charge fold at `fun a => ∑ i, p i * c a i` is at most +`∑ i, p i * u i`. -/ +theorem wpFold_chargeUnder_price_le {ι : Type*} [Fintype ι] (c : P.A → ι → ℕ∞) + (allows : (a : P.A) → P.B a → Prop) (p : ι → ℕ∞) {x : FreeM P α} {u : ι → ℕ∞} + (hu : wpFold (OpSpec.chargeUnder c allows) x (fun _ => 0) ≤ u) : + wpFold (OpSpec.chargeUnder (fun a => ∑ i, p i * c a i) allows) x (fun _ => 0) ≤ + ∑ i, p i * u i := by + simpa using (wpFold_chargeUnder_price_le_sum c allows p x (fun _ => 0)).trans + (Finset.sum_le_sum fun i _ => mul_le_mul_right (hu i) (p i)) + +/-- The charge fold over the allowed answers is the charge fold of the restricted tree. -/ +theorem wpFold_chargeUnder_eq_restrict {R : Type w} [CompleteLattice R] [Add R] (c : P.A → R) + (allows : (a : P.A) → P.B a → Prop) (x : FreeM P α) (post : α → R) : + wpFold (OpSpec.chargeUnder c allows) x post = + wpFold (OpSpec.charge (P := P.restrict allows) c) (x.restrict allows) post := by + rw [FreeM.restrict, wpFold_mapLens, OpSpec.chargeUnder_eq] + rfl + +end Vector + +end FreeM + end PFunctor diff --git a/PolyFunTest/PFunctor/Charge/Under.lean b/PolyFunTest/PFunctor/Charge/Under.lean new file mode 100644 index 00000000..9a1fb176 --- /dev/null +++ b/PolyFunTest/PFunctor/Charge/Under.lean @@ -0,0 +1,142 @@ +/- +Copyright (c) 2026 PolyFun Contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Devon Tuma +-/ + +module + +public import PolyFun.PFunctor.Free.WP.Charge + +/-! +# Charge folds over allowed answers + +Boundary cases for the fold rules of `OpSpec.chargeUnder`: + +* a call whose answers are all excluded charges only itself, so ignoring the answer is not enough + for the continuation to be charged; +* the budget of a call is exact at a nonzero postcondition, and a bind charges its continuation + only at the results the first program can return; +* the priced fold can be strictly below the priced vector fold, when the branches use different + resources. +-/ + +public section + +open PFunctor PFunctor.FreeM + +namespace PolyFunTest.ChargeUnder + +abbrev Coin : PFunctor.{0, 0} := ⟨Unit, fun _ => Bool⟩ + +/-- A second flip after tails only. -/ +@[expose] def again : FreeM Coin Bool := + FreeM.liftBind () fun b => if b then FreeM.pure b else FreeM.liftBind () FreeM.pure + +/-! ## Calls -/ + +/-- With no answer allowed, a call that ignores its answer charges itself and not its +continuation. -/ +example : wpFold (OpSpec.chargeUnder (fun _ => (1 : ℕ∞)) fun _ _ => False) + (FreeM.liftBind (P := Coin) () fun _ => again) (fun _ => 0) = 1 := by + rw [wpFold_liftBind, OpSpec.chargeUnder_apply] + simp + +/-- With heads allowed, the same call charges its continuation too. -/ +example : wpFold (OpSpec.chargeUnder (fun _ => (1 : ℕ∞)) fun _ b => b = true) + (FreeM.liftBind (P := Coin) () fun _ => again) (fun _ => 5) = + 1 + wpFold (OpSpec.chargeUnder (fun _ => (1 : ℕ∞)) fun _ b => b = true) again + (fun _ => 5) := + wpFold_chargeUnder_liftBind_const (P := Coin) (fun _ => (1 : ℕ∞)) (fun _ b => b = true) () + (b := true) rfl again _ + +/-- Over heads, one flip and a postcondition of five cost exactly six. -/ +example (n : ℕ) : wpFold (OpSpec.chargeUnder (fun _ => ((1 : ℕ) : ℕ∞)) fun _ b => b = true) + again (fun _ => 5) ≤ n ↔ 6 ≤ n := by + rw [again, wpFold_chargeUnder_liftBind_le_iff (fun _ => 1)] + simp only [forall_eq] + change 1 ≤ n ∧ (5 : ℕ∞) ≤ ((n - 1 : ℕ) : ℕ∞) ↔ 6 ≤ n + rw [Nat.ofNat_le_cast] + omega + +/-- Over all answers the second flip counts, and the same program costs exactly seven. -/ +example (n : ℕ) : wpFold (OpSpec.chargeUnder (fun _ => ((1 : ℕ) : ℕ∞)) fun _ _ => True) + again (fun _ => 5) ≤ n ↔ 7 ≤ n := by + rw [again, wpFold_chargeUnder_liftBind_le_iff (fun _ => 1)] + simp only [forall_const, Bool.forall_bool, Bool.false_eq_true, reduceIte] + rw [wpFold_chargeUnder_liftBind_le_iff (fun _ => 1)] + change 1 ≤ n ∧ (1 ≤ n - 1 ∧ ∀ b : Bool, True → (5 : ℕ∞) ≤ ((n - 1 - 1 : ℕ) : ℕ∞)) ∧ + (5 : ℕ∞) ≤ ((n - 1 : ℕ) : ℕ∞) ↔ 7 ≤ n + simp only [forall_const, Nat.ofNat_le_cast] + omega + +/-! ## Sequencing -/ + +/-- Over heads, the continuation is charged only at heads: the branch after tails may cost more +than the bound. -/ +example : wpFold (OpSpec.chargeUnder (fun _ => ((1 : ℕ) : ℕ∞)) fun _ b => b = true) + ((FreeM.lift (P := Coin) ()).bind fun b => + if b then FreeM.pure b else (FreeM.lift (P := Coin) ()).bind fun _ => FreeM.lift ()) + (fun _ => 5) ≤ ((1 + 5 : ℕ) : ℕ∞) := by + refine wpFold_chargeUnder_bind_le_add (P := Coin) (fun _ => 1) (fun _ b => b = true) _ _ + (· = true) 1 5 ?_ ?_ fun _ hb => hb + · exact wpFold_chargeUnder_liftBind_le (P := Coin) (fun _ => 1) (fun _ b => b = true) () + FreeM.pure 1 0 le_rfl fun _ _ => le_rfl + · rintro _ rfl + exact le_rfl + +/-! ## Prices -/ + +/-- A free choice of one of two resources, and one call to it. -/ +abbrev Choose : PFunctor.{0, 0} := ⟨Option Bool, fun _ => Bool⟩ + +@[expose] def useOne : FreeM Choose Bool := + FreeM.liftBind none fun b => FreeM.liftBind (some b) FreeM.pure + +@[expose] def count (a : Option Bool) (i : Bool) : ℕ∞ := if a = some i then 1 else 0 + +/-- Each branch uses one resource, so at unit prices the program costs one. -/ +theorem useOne_priced : + wpFold (OpSpec.chargeUnder (fun a => ∑ i, 1 * count a i) fun _ _ => True) useOne + (fun _ => 0) = 1 := by + change (∑ i, 1 * count none i) + ⨆ b, ⨆ (_ : True), ((∑ i, 1 * count (some b) i) + + ⨆ _ : Bool, ⨆ (_ : True), (0 : ℕ∞)) = 1 + simp [count, iSup_bool_eq] + +/-- The worst case over both resources is one of each, and its price is two. -/ +theorem useOne_vector : + ∑ i, 1 * wpFold (OpSpec.chargeUnder count fun _ _ => True) useOne (fun _ _ => 0) i = 2 := by + simp only [wpFold_chargeUnder_pi_apply] + change ∑ i, 1 * (count none i + ⨆ b, ⨆ (_ : True), (count (some b) i + + ⨆ _ : Bool, ⨆ (_ : True), (0 : ℕ∞))) = 2 + simp [count, iSup_bool_eq, one_add_one_eq_two] + +/-- The priced fold is strictly below the priced vector fold. -/ +example : wpFold (OpSpec.chargeUnder (fun a => ∑ i, 1 * count a i) fun _ _ => True) useOne + (fun _ => 0) < + ∑ i, 1 * wpFold (OpSpec.chargeUnder count fun _ _ => True) useOne (fun _ _ => 0) i := by + rw [useOne_priced, useOne_vector] + exact_mod_cast (one_lt_two : (1 : ℕ) < 2) + +/-! ## Universes -/ + +/-- The result universe of a call is independent of the interface's. -/ +example {P : PFunctor.{1, 2}} {α : Type 3} (c : P.A → ℕ) (allows : (a : P.A) → P.B a → Prop) + (a : P.A) (r : P.B a → FreeM P α) (n : ℕ) {post : α → ℕ∞} : + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (FreeM.liftBind a r) post ≤ n ↔ + c a ≤ n ∧ ∀ b, allows a b → + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (r b) post ≤ + ((n - c a : ℕ) : ℕ∞) := + wpFold_chargeUnder_liftBind_le_iff c allows a r n + +/-- The two results of a bind live in independent universes. -/ +example {P : PFunctor.{1, 2}} {X : Type 3} {Y : Type 4} (c : P.A → ℕ) + (allows : (a : P.A) → P.B a → Prop) (x : FreeM P X) (f : X → FreeM P Y) (m n : ℕ) + (hx : wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) x (fun _ => 0) ≤ (m : ℕ∞)) + (hf : ∀ a, wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (f a) (fun _ => 0) ≤ + (n : ℕ∞)) (hI : wpFold (OpSpec.demonicUnder allows) x fun _ => True) : + wpFold (OpSpec.chargeUnder (fun a => (c a : ℕ∞)) allows) (x.bind f) (fun _ => 0) ≤ + ((m + n : ℕ) : ℕ∞) := + wpFold_chargeUnder_bind_le_add c allows x f _ m n hx (fun a _ => hf a) hI + +end PolyFunTest.ChargeUnder