Documentation

CslibFrontier.Foundations.Data.List.Weighted

Weighted lists #

A weighted list is a list of pairs (weight, value) with weights in a semiring A: the list of the outcomes of a computation that branches, each with the product of the weights along its branch. This file has the three operations of such lists:

Weighted lists with these operations are the writer monad of the multiplicative monoid of A over lists; the file keeps the type List (A × α) of the pairs, which is what the statements about programs use.

Main definitions #

Main results #

References #

From VP ≠ VNP over every field [Ant26], Appendix J.3 (the semantics of the evaluator in the proof of Theorem J.3); modules Nc.Grp.Lang and Nc.Grp.Trans.

def List.weightedSum {A : Type u_1} {α : Type u_2} [Mul A] [Add A] [Zero A] (l : List (A × α)) (G : α → A) :
A

The weighted sum ∑ (w, a) ∈ l, w * G a of a function of the value.

def List.weightedBind {A : Type u_1} {α : Type u_2} {β : Type u_3} [Mul A] (l : List (A × α)) (f : α → List (A × β)) :
List (A × β)

First l, then f at every value of l; the weights multiply.

def List.weightedFoldl {A : Type u_1} {β : Type u_3} {ε : Type u_5} [Mul A] [One A] (f : ε → β → List (A × β)) :
List ε → β → List (A × β)

The weighted list of a fold: the step f x at every element x of a list, in order, from the state s.

@[simp]
theorem List.weightedSum_nil {A : Type u_1} {α : Type u_2} [Mul A] [AddMonoid A] (G : α → A) :
@[simp]
theorem List.weightedSum_cons {A : Type u_1} {α : Type u_2} [Mul A] [AddMonoid A] (p : A × α) (l : List (A × α)) (G : α → A) :
(p :: l).weightedSum G = p.1 * G p.2 + l.weightedSum G
@[simp]
theorem List.weightedSum_append {A : Type u_1} {α : Type u_2} [Mul A] [AddMonoid A] (l l' : List (A × α)) (G : α → A) :
theorem List.weightedSum_congr {A : Type u_1} {α : Type u_2} [Mul A] [AddMonoid A] {l : List (A × α)} {G G' : α → A} (h : ∀ p ∈ l, G p.2 = G' p.2) :
theorem List.weightedSum_map {A : Type u_1} {α : Type u_2} {β : Type u_3} [Mul A] [AddMonoid A] (l : List (A × α)) (e : α → β) (G : β → A) :
(map (Prod.map id e) l).weightedSum G = l.weightedSum fun (a : α) => G (e a)

The weighted sum over the images of the values.

theorem List.weightedSum_singleton_one {A : Type u_1} {α : Type u_2} [MulOneClass A] [AddMonoid A] (a : α) (G : α → A) :
theorem List.weightedSum_of_fst_eq_one {A : Type u_1} {α : Type u_2} [MulOneClass A] [AddMonoid A] {l : List (A × α)} (h : ∀ p ∈ l, p.1 = 1) (G : α → A) :

With all weights 1, the weighted sum is the sum over the values.

theorem List.weightedSum_map_one {A : Type u_1} {α : Type u_2} [MulOneClass A] [AddMonoid A] (l : List α) (G : α → A) :
(map (fun (a : α) => (1, a)) l).weightedSum G = (map G l).sum

The weighted sum of values, each with the weight 1.

theorem List.weightedSum_map_mul_left {A : Type u_1} {α : Type u_2} [NonUnitalSemiring A] (c : A) (l : List (A × α)) (G : α → A) :
(map (fun (r : A × α) => (c * r.1, r.2)) l).weightedSum G = c * l.weightedSum G

A common factor of the weights.

theorem List.weightedSum_weightedBind {A : Type u_1} {α : Type u_2} {β : Type u_3} [NonUnitalSemiring A] (l : List (A × α)) (f : α → List (A × β)) (G : β → A) :
(l.weightedBind f).weightedSum G = l.weightedSum fun (a : α) => (f a).weightedSum G

The weighted sum of a sequence of two computations.

theorem List.mem_weightedBind_iff {A : Type u_1} {α : Type u_2} {β : Type u_3} [Mul A] {l : List (A × α)} {f : α → List (A × β)} {p : A × β} :
p ∈ l.weightedBind f ↔ ∃ a ∈ l, ∃ r ∈ f a.2, p = (a.1 * r.1, r.2)
theorem List.map_weightedBind {A : Type u_1} {α : Type u_2} {β : Type u_3} {γ : Type u_4} [Mul A] (l : List (A × α)) (f : α → List (A × β)) (e : β → γ) :
map (Prod.map id e) (l.weightedBind f) = l.weightedBind fun (a : α) => map (Prod.map id e) (f a)

Mapping the values of a sequence maps the values of its second part.

theorem List.map_snd_weightedBind {A : Type u_1} {α : Type u_2} {β : Type u_3} [Mul A] (l : List (A × α)) (f : α → List (A × β)) :
map Prod.snd (l.weightedBind f) = flatMap (fun (a : α) => map Prod.snd (f a)) (map Prod.snd l)

The values of a sequence are the values of the second part at the values of the first.

theorem List.weightedBind_assoc {A : Type u_1} {α : Type u_2} {β : Type u_3} {γ : Type u_4} [Semigroup A] (l : List (A × α)) (f : α → List (A × β)) (g : β → List (A × γ)) :
(l.weightedBind f).weightedBind g = l.weightedBind fun (a : α) => (f a).weightedBind g
@[simp]
theorem List.weightedBind_singleton_one {A : Type u_1} {α : Type u_2} {β : Type u_3} [MulOneClass A] (a : α) (f : α → List (A × β)) :
theorem List.fst_eq_one_of_mem_weightedBind {A : Type u_1} {α : Type u_2} {β : Type u_3} [MulOneClass A] {l : List (A × α)} {f : α → List (A × β)} (hl : ∀ p ∈ l, p.1 = 1) (hf : ∀ (a : α), ∀ p ∈ f a, p.1 = 1) (p : A × β) :
p ∈ l.weightedBind f → p.1 = 1

A sequence of two parts whose weights are all 1 has only weights 1.

theorem List.weightedBind_map_one {A : Type u_1} {α : Type u_2} {β : Type u_3} {γ : Type u_4} [MulOneClass A] (l : List α) (g : α → β) (f : β → List (A × γ)) :
(map (fun (t : α) => (1, g t)) l).weightedBind f = flatMap (fun (t : α) => f (g t)) l

A sequence after a list of values of weight 1.

@[simp]
theorem List.weightedFoldl_nil {A : Type u_1} {β : Type u_3} {ε : Type u_5} [MulOneClass A] (f : ε → β → List (A × β)) (s : β) :
@[simp]
theorem List.weightedFoldl_cons {A : Type u_1} {β : Type u_3} {ε : Type u_5} [MulOneClass A] (f : ε → β → List (A × β)) (x : ε) (xs : List ε) (s : β) :
weightedFoldl f (x :: xs) s = (f x s).weightedBind (weightedFoldl f xs)
theorem List.weightedFoldl_singleton_one {A : Type u_1} {β : Type u_3} {ε : Type u_5} [MulOneClass A] (g : ε → β → β) (xs : List ε) (s : β) :
weightedFoldl (fun (x : ε) (s : β) => [(1, g x s)]) xs s = [(1, foldl (fun (s : β) (x : ε) => g x s) s xs)]

A fold of deterministic steps.

theorem List.weightedFoldl_ite {A : Type u_1} {β : Type u_3} {ε : Type u_5} [MulOneClass A] (p : ε → Prop) [DecidablePred p] (g : ε → β → β) (xs : List ε) (s : β) :
weightedFoldl (fun (x : ε) (s : β) => if p x then [(1, g x s)] else []) xs s = if ∀ x ∈ xs, p x then [(1, foldl (fun (s : β) (x : ε) => g x s) s xs)] else []

A fold of steps that reject unless their element satisfies p, and otherwise go on deterministically.

theorem List.mem_weightedFoldl_induction {A : Type u_1} {β : Type u_3} {ε : Type u_5} [MulOneClass A] {f : ε → β → List (A × β)} {Inv : ℕ → β → Prop} {xs : List ε} {s : β} {m : ℕ} (h : ∀ x ∈ xs, ∀ (s : β) (m : ℕ), Inv m s → ∀ q ∈ f x s, Inv (m + 1) q.2) (hs : Inv m s) (q : A × β) :
q ∈ weightedFoldl f xs s → Inv (m + xs.length) q.2

An invariant of the steps, with a counter of the steps, holds at every outcome of a fold.