Documentation

VCVio.OracleComp.QueryTracking.WriterCost

Writer Cost Accounting #

This file collects reusable AddWriterT facts for pathwise and expected cost reasoning. It also equips QueryImpl with additive writer-cost instrumentation.

def QueryImpl.withAddCost {ι : Type} {spec : OracleSpec ι} {m : TypeType u_1} [Monad m] {ω : Type} [AddMonoid ω] (impl : QueryImpl spec m) (costFn : spec.Domainω) :
QueryImpl spec (AddWriterT ω m)

Instrument an implementation with additive cost accumulation in an AddWriterT layer.

Instances For
    @[simp]
    theorem QueryImpl.withAddCost_apply {ι : Type} {spec : OracleSpec ι} {m : TypeType u_1} [Monad m] {ω : Type} [AddMonoid ω] (impl : QueryImpl spec m) (costFn : spec.Domainω) (t : spec.Domain) :
    impl.withAddCost costFn t = do AddWriterT.addTell (costFn t) liftM (impl t)
    def QueryImpl.withUnitCost {ι : Type} {spec : OracleSpec ι} {m : TypeType u_1} [Monad m] (impl : QueryImpl spec m) :

    Instrument an implementation with unit additive cost for every query.

    Instances For
      @[simp]
      theorem QueryImpl.withUnitCost_apply {ι : Type} {spec : OracleSpec ι} {m : TypeType u_1} [Monad m] (impl : QueryImpl spec m) (t : spec.Domain) :
      impl.withUnitCost t = do AddWriterT.addTell 1 liftM (impl t)
      def AddWriterT.PathwiseCostAtMost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] (oa : AddWriterT ω m α) (w : ω) :

      Pathwise upper bound for an AddWriterT computation: every reachable execution result carries additive cost at most w.

      Instances For
        def AddWriterT.PathwiseCostAtLeast {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] (oa : AddWriterT ω m α) (w : ω) :

        Pathwise lower bound for an AddWriterT computation: every reachable execution result carries additive cost at least w.

        Instances For
          def AddWriterT.PathwiseCostEqOnSupport {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] (oa : AddWriterT ω m α) (w : ω) :

          Pathwise exactness on support for an AddWriterT computation: every reachable execution result carries exactly the additive cost w.

          This is the weak extensional notion of pathwise exactness. If oa.run has empty support, it holds vacuously for every w. Use [AddWriterT.PathwiseHasCost] when the intended meaning is that oa has an exact pathwise cost and admits at least one reachable execution.

          Instances For
            @[simp]
            theorem AddWriterT.pathwiseCostEqOnSupport_iff {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] (oa : AddWriterT ω m α) (w : ω) :
            def AddWriterT.PathwiseHasCost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] (oa : AddWriterT ω m α) (w : ω) :

            Pathwise exact cost for an AddWriterT computation: oa has at least one reachable execution, and every reachable execution result carries exactly the additive cost w.

            This is the strong semantic notion of exact cost over execution paths. Unlike [AddWriterT.HasCost], it does not require cost to be recoverable from the final output alone. Unlike [AddWriterT.PathwiseCostEqOnSupport], it is not vacuous on computations with empty support.

            Instances For
              @[simp]
              theorem AddWriterT.pathwiseHasCost_iff {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] (oa : AddWriterT ω m α) (w : ω) :
              theorem AddWriterT.PathwiseHasCost.nonempty {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} (h : oa.PathwiseHasCost w) :
              theorem AddWriterT.PathwiseCostEqOnSupport.atMost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} (h : oa.PathwiseCostEqOnSupport w) :
              theorem AddWriterT.PathwiseCostEqOnSupport.atLeast {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} (h : oa.PathwiseCostEqOnSupport w) :
              theorem AddWriterT.PathwiseHasCost.eqOnSupport {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} (h : oa.PathwiseHasCost w) :
              theorem AddWriterT.PathwiseHasCost.atMost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} (h : oa.PathwiseHasCost w) :
              theorem AddWriterT.PathwiseHasCost.atLeast {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} (h : oa.PathwiseHasCost w) :
              theorem AddWriterT.PathwiseHasCost.unique {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [PartialOrder ω] {oa : AddWriterT ω m α} {w₁ w₂ : ω} (h₁ : oa.PathwiseHasCost w₁) (h₂ : oa.PathwiseHasCost w₂) :
              w₁ = w₂
              theorem AddWriterT.pathwiseCostAtMost_of_hasCost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] [LawfulMonad m] {oa : AddWriterT ω m α} {w b : ω} (h : oa.HasCost w) (hwb : w b) :
              theorem AddWriterT.pathwiseCostAtLeast_of_hasCost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddMonoid ω] [Preorder ω] [LawfulMonad m] {oa : AddWriterT ω m α} {w b : ω} (h : oa.HasCost w) (hbw : b w) :
              noncomputable def AddWriterT.expectedCost {m : TypeType u_1} [Monad m] {α ω : Type} [HasEvalSPMF m] (oa : AddWriterT ω m α) (val : ωENNReal) :

              The expected additive cost of an AddWriterT computation, obtained by taking the expectation of its cost marginal.

              This expectation is computed over the base monad's subdistribution semantics on oa.costs. In particular, if the underlying computation can fail, the missing mass contributes 0, exactly as for other wp-style expectations in VCV-io.

              Instances For
                @[reducible, inline]
                noncomputable abbrev AddWriterT.expectedCostNat {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSPMF m] (oa : AddWriterT m α) :

                Convenience specialization of [AddWriterT.expectedCost] to natural-valued additive costs.

                Instances For
                  theorem AddWriterT.expectedCostNat_eq_tsum_tail_probs {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSPMF m] (oa : AddWriterT m α) :
                  oa.expectedCostNat = ∑' (i : ), probEvent oa.costs fun (c : ) => i < c

                  Tail-sum formula for the natural-valued expected cost of an AddWriterT computation:

                  E[cost] = ∑ i, Pr[i < cost].

                  This is the standard discrete expectation identity specialized to the writer-cost marginal.

                  theorem AddWriterT.expectedCostNat_le_tsum_of_tail_probs_le {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSPMF m] (oa : AddWriterT m α) {a : ENNReal} (h : ∀ (i : ), (probEvent oa.costs fun (c : ) => i < c) a i) :

                  Tail domination bounds the expected natural-valued writer cost.

                  If the tail probability Pr[i < cost] is bounded by a i for every i, then E[cost] ≤ ∑ i, a i.

                  theorem AddWriterT.expectedCostNat_eq_sum_tail_probs_of_pathwiseCostAtMost {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSPMF m] [LawfulMonad m] {oa : AddWriterT m α} {n : } (h : oa.PathwiseCostAtMost n) :
                  oa.expectedCostNat = iFinset.range n, probEvent oa.costs fun (c : ) => i < c

                  Finite tail-sum formula for natural-valued writer cost under a pathwise upper bound.

                  If every execution path of oa incurs cost at most n, then the tail probabilities vanish above n, so the infinite tail sum truncates to Finset.range n.

                  theorem AddWriterT.expectedCost_le_of_support_bound {m : TypeType u_1} [Monad m] {α ω : Type} [HasEvalSPMF m] (oa : AddWriterT ω m α) (val : ωENNReal) (c : ENNReal) (h : wsupport oa.costs, val w c) :
                  oa.expectedCost val c
                  theorem AddWriterT.expectedCost_le_of_pathwiseCostAtMost {m : TypeType u_1} [Monad m] {α ω : Type} [AddMonoid ω] [HasEvalSPMF m] [LawfulMonad m] [Preorder ω] {oa : AddWriterT ω m α} {w : ω} {val : ωENNReal} (h : oa.PathwiseCostAtMost w) (hval : Monotone val) :
                  oa.expectedCost val val w
                  theorem AddWriterT.expectedCost_ge_of_pathwiseCostAtLeast {m : TypeType u_1} [Monad m] {α ω : Type} [AddMonoid ω] [LawfulMonad m] [Preorder ω] [HasEvalPMF m] {oa : AddWriterT ω m α} {w : ω} {val : ωENNReal} (h : oa.PathwiseCostAtLeast w) (hval : Monotone val) :
                  val w oa.expectedCost val
                  theorem AddWriterT.expectedCost_eq_tsum_outputs_of_costsAs {m : TypeType u_1} [Monad m] {α ω : Type} [HasEvalSPMF m] [LawfulMonad m] {oa : AddWriterT ω m α} {f : αω} {val : ωENNReal} (h : oa.CostsAs f) :
                  oa.expectedCost val = ∑' (a : α), Pr[= a | oa.outputs] * val (f a)
                  theorem AddWriterT.pathwiseHasCost_pure {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] (x : α) :
                  theorem AddWriterT.pathwiseCostAtMost_mono {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] {oa : AddWriterT ω m α} {w₁ w₂ : ω} (h : oa.PathwiseCostAtMost w₁) (hw : w₁ w₂) :
                  theorem AddWriterT.pathwiseCostAtLeast_mono {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] {oa : AddWriterT ω m α} {w₁ w₂ : ω} (h : oa.PathwiseCostAtLeast w₂) (hw : w₁ w₂) :
                  theorem AddWriterT.pathwiseCostAtMost_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] {oa : AddWriterT ω m α} {w : ω} (f : αβ) (h : oa.PathwiseCostAtMost w) :
                  theorem AddWriterT.pathwiseCostAtLeast_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] {oa : AddWriterT ω m α} {w : ω} (f : αβ) (h : oa.PathwiseCostAtLeast w) :
                  theorem AddWriterT.pathwiseCostEqOnSupport_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] {oa : AddWriterT ω m α} {w : ω} (f : αβ) (h : oa.PathwiseCostEqOnSupport w) :
                  theorem AddWriterT.pathwiseHasCost_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] {oa : AddWriterT ω m α} {w : ω} (f : αβ) (h : oa.PathwiseHasCost w) :
                  theorem AddWriterT.pathwiseCostAtMost_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w₁ w₂ : ω} (h₁ : oa.PathwiseCostAtMost w₁) (h₂ : ∀ (a : α), (f a).PathwiseCostAtMost w₂) :
                  (oa >>= f).PathwiseCostAtMost (w₁ + w₂)
                  theorem AddWriterT.pathwiseCostAtLeast_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w₁ w₂ : ω} (h₁ : oa.PathwiseCostAtLeast w₁) (h₂ : ∀ (a : α), (f a).PathwiseCostAtLeast w₂) :
                  (oa >>= f).PathwiseCostAtLeast (w₁ + w₂)
                  theorem AddWriterT.pathwiseCostEqOnSupport_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w₁ w₂ : ω} (h₁ : oa.PathwiseCostEqOnSupport w₁) (h₂ : ∀ (a : α), (f a).PathwiseCostEqOnSupport w₂) :
                  (oa >>= f).PathwiseCostEqOnSupport (w₁ + w₂)
                  theorem AddWriterT.pathwiseHasCost_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w₁ w₂ : ω} (h₁ : oa.PathwiseHasCost w₁) (h₂ : ∀ (a : α), (f a).PathwiseHasCost w₂) :
                  (oa >>= f).PathwiseHasCost (w₁ + w₂)
                  theorem AddWriterT.pathwiseHasCost_bind_zero_left {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w : ω} (h₁ : oa.PathwiseHasCost 0) (h₂ : ∀ (a : α), (f a).PathwiseHasCost w) :
                  theorem AddWriterT.pathwiseHasCost_bind_zero_right {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w : ω} (h₁ : oa.PathwiseHasCost w) (h₂ : ∀ (a : α), (f a).PathwiseHasCost 0) :
                  theorem AddWriterT.pathwiseCostEqOnSupport_bind_zero_left {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w : ω} (h₁ : oa.PathwiseCostEqOnSupport 0) (h₂ : ∀ (a : α), (f a).PathwiseCostEqOnSupport w) :
                  theorem AddWriterT.pathwiseCostEqOnSupport_bind_zero_right {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {oa : AddWriterT ω m α} {f : αAddWriterT ω m β} {w : ω} (h₁ : oa.PathwiseCostEqOnSupport w) (h₂ : ∀ (a : α), (f a).PathwiseCostEqOnSupport 0) :
                  theorem AddWriterT.pathwiseCostAtMost_fin_mOfFn {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {n : } {k : ω} {f : Fin nAddWriterT ω m α} (h : ∀ (i : Fin n), (f i).PathwiseCostAtMost k) :
                  theorem AddWriterT.pathwiseCostAtLeast_fin_mOfFn {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {n : } {k : ω} {f : Fin nAddWriterT ω m α} (h : ∀ (i : Fin n), (f i).PathwiseCostAtLeast k) :
                  theorem AddWriterT.pathwiseHasCost_fin_mOfFn {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {ω : Type} [AddCommMonoid ω] [PartialOrder ω] [LawfulMonad m] [IsOrderedAddMonoid ω] {n : } {k : ω} {f : Fin nAddWriterT ω m α} (h : ∀ (i : Fin n), (f i).PathwiseHasCost k) :
                  def AddWriterT.QueryBoundedAboveBy {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] (oa : AddWriterT m α) (n : ) :

                  Pathwise upper bound for a unit-cost AddWriterT computation.

                  Instances For
                    def AddWriterT.QueryBoundedBelowBy {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] (oa : AddWriterT m α) (n : ) :

                    Pathwise lower bound for a unit-cost AddWriterT computation.

                    Instances For
                      theorem AddWriterT.queryBoundedAboveBy_mono {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {oa : AddWriterT m α} {n₁ n₂ : } (h : oa.QueryBoundedAboveBy n₁) (hn : n₁ n₂) :
                      theorem AddWriterT.queryBoundedBelowBy_mono {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {oa : AddWriterT m α} {n₁ n₂ : } (h : oa.QueryBoundedBelowBy n₂) (hn : n₁ n₂) :
                      theorem AddWriterT.queryBoundedAboveBy_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] [LawfulMonad m] {oa : AddWriterT m α} {n : } (f : αβ) (h : oa.QueryBoundedAboveBy n) :
                      theorem AddWriterT.queryBoundedBelowBy_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] [LawfulMonad m] {oa : AddWriterT m α} {n : } (f : αβ) (h : oa.QueryBoundedBelowBy n) :
                      theorem AddWriterT.queryBoundedAboveBy_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] [LawfulMonad m] {oa : AddWriterT m α} {f : αAddWriterT m β} {n₁ n₂ : } (h₁ : oa.QueryBoundedAboveBy n₁) (h₂ : ∀ (a : α), (f a).QueryBoundedAboveBy n₂) :
                      (oa >>= f).QueryBoundedAboveBy (n₁ + n₂)
                      theorem AddWriterT.queryBoundedBelowBy_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] [LawfulMonad m] {oa : AddWriterT m α} {f : αAddWriterT m β} {n₁ n₂ : } (h₁ : oa.QueryBoundedBelowBy n₁) (h₂ : ∀ (a : α), (f a).QueryBoundedBelowBy n₂) :
                      (oa >>= f).QueryBoundedBelowBy (n₁ + n₂)
                      theorem AddWriterT.queryBoundedAboveBy_fin_mOfFn {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] [LawfulMonad m] {n k : } {f : Fin nAddWriterT m α} (h : ∀ (i : Fin n), (f i).QueryBoundedAboveBy k) :
                      theorem AddWriterT.queryBoundedBelowBy_fin_mOfFn {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] [LawfulMonad m] {n k : } {f : Fin nAddWriterT m α} (h : ∀ (i : Fin n), (f i).QueryBoundedBelowBy k) :
                      def AddWriterT.QueryCostExactly {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] (oa : AddWriterT m α) (n : ) :

                      Pathwise exact cost for a unit-cost AddWriterT computation: every reachable execution carries exactly n unit queries.

                      Instances For
                        theorem AddWriterT.QueryCostExactly.toAbove {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {oa : AddWriterT m α} {n : } (h : oa.QueryCostExactly n) :
                        theorem AddWriterT.QueryCostExactly.toBelow {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] {oa : AddWriterT m α} {n : } (h : oa.QueryCostExactly n) :
                        theorem AddWriterT.queryCostExactly_pure {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] [LawfulMonad m] (x : α) :
                        theorem AddWriterT.queryCostExactly_map {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] [LawfulMonad m] {oa : AddWriterT m α} {n : } (f : αβ) (h : oa.QueryCostExactly n) :
                        theorem AddWriterT.queryCostExactly_bind {m : TypeType u_1} [Monad m] {α β : Type} [HasEvalSet m] [LawfulMonad m] {oa : AddWriterT m α} {f : αAddWriterT m β} {n₁ n₂ : } (h₁ : oa.QueryCostExactly n₁) (h₂ : ∀ (a : α), (f a).QueryCostExactly n₂) :
                        (oa >>= f).QueryCostExactly (n₁ + n₂)
                        theorem AddWriterT.queryCostExactly_fin_mOfFn {m : TypeType u_1} [Monad m] {α : Type} [HasEvalSet m] [LawfulMonad m] {n k : } {f : Fin nAddWriterT m α} (h : ∀ (i : Fin n), (f i).QueryCostExactly k) :