math / data /lean /OAI /Computability /PerfectCompleteness /Foundations /ExactConditionalOutput.lean
Download data/lean/OAI/Computability/PerfectCompleteness/Foundations/ExactConditionalOutput.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 9.75 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/PerfectCompleteness/Foundations/ExactConditionalOutput.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/PerfectCompleteness/Foundations/ExactConditionalOutput.lean
-
curl -L -o ExactConditionalOutput.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/PerfectCompleteness/Foundations/ExactConditionalOutput.lean
9.75 kB
| import OAI.Computability.PerfectCompleteness.Foundations.DummyEliminationLemmas | |
| import OAI.Computability.PerfectCompleteness.Reduction.TargetGameLemmas | |
| namespace OAI | |
| namespace PerfectCompleteness.ExactConditionalOutput | |
| open scoped BigOperators Classical | |
| noncomputable section | |
| variable {S X : Type*} {E : S β Type*} [β s, Fintype (E s)] | |
| (count : β s, E s β Nat) | |
| def enumeration (s : S) : E s β Fin (Fintype.card (E s)) := Fintype.equivFin _ | |
| def copies (s : S) : List (E s) := | |
| (List.ofFn fun i : Fin (Fintype.card (E s)) => | |
| List.replicate (count s ((enumeration s).symm i)) ((enumeration s).symm i)).flatten | |
| theorem copies_length (s : S) : (copies count s).length = β e, count s e := by | |
| simp only [copies, List.length_flatten, List.map_ofFn, Function.comp_def, | |
| List.length_replicate, List.sum_ofFn] | |
| exact (enumeration s).symm.sum_comp (fun e : E s => count s e) | |
| theorem copies_filter_length (s : S) (keep : E s β Bool) : | |
| ((copies count s).filter keep).length = β e, if keep e then count s e else 0 := by | |
| simp only [copies, List.filter_flatten, List.length_flatten, List.map_ofFn, | |
| List.sum_ofFn] | |
| calc | |
| _ = β i : Fin (Fintype.card (E s)), | |
| if keep ((enumeration s).symm i) then count s ((enumeration s).symm i) else 0 := by | |
| apply Finset.sum_congr rfl | |
| intro i _ | |
| cases h : keep ((enumeration s).symm i) <;> simp [h] | |
| _ = _ := (enumeration s).symm.sum_comp | |
| (fun e : E s => if keep e then count s e else 0) | |
| section Lists | |
| variable {Ξ± : Type*} (shape : X β S) (entries : (x : X) β E (shape x) β Ξ±) | |
| def localOutput (x : X) : List Ξ± := (copies count (shape x)).map (entries x) | |
| def output (xs : List X) : List Ξ± := xs.flatMap (localOutput count shape entries) | |
| theorem localOutput_length (D : Nat) (htotal : β s, β e, count s e = D) (x : X) : | |
| (localOutput count shape entries x).length = D := by | |
| rw [localOutput, List.length_map, copies_length, htotal] | |
| theorem output_length (D : Nat) (htotal : β s, β e, count s e = D) (xs : List X) : | |
| (output count shape entries xs).length = D * xs.length := by | |
| induction xs with | |
| | nil => rfl | |
| | cons x xs ih => | |
| change (localOutput count shape entries x ++ output count shape entries xs).length = _ | |
| rw [List.length_append, localOutput_length count shape entries D htotal, ih, | |
| List.length_cons, Nat.mul_add, Nat.mul_one] | |
| omega | |
| theorem output_nonempty (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) : output count shape entries xs β [] := by | |
| intro hempty | |
| have hlength := output_length count shape entries D htotal xs | |
| have hxpos : 0 < xs.length := by | |
| cases xs with | |
| | nil => exact False.elim (hxs rfl) | |
| | cons x xs => simp | |
| rw [hempty, List.length_nil] at hlength | |
| exact (Nat.ne_of_gt (Nat.mul_pos hD hxpos)) hlength.symm | |
| def localCount (accepts : Ξ± β Bool) (x : X) : Nat := | |
| β e, if accepts (entries x e) then count (shape x) e else 0 | |
| theorem localOutput_filter_length (accepts : Ξ± β Bool) (x : X) : | |
| ((localOutput count shape entries x).filter accepts).length = | |
| localCount count shape entries accepts x := by | |
| simpa only [localOutput, localCount, List.filter_map, List.length_map, Function.comp_def] | |
| using copies_filter_length count (shape x) (fun e => accepts (entries x e)) | |
| theorem output_filter_length (xs : List X) (accepts : Ξ± β Bool) : | |
| ((output count shape entries xs).filter accepts).length = | |
| (xs.map (localCount count shape entries accepts)).sum := by | |
| induction xs with | |
| | nil => rfl | |
| | cons x xs ih => | |
| change ((localOutput count shape entries x ++ output count shape entries xs).filter | |
| accepts).length = _ | |
| rw [List.filter_append, List.length_append, localOutput_filter_length, ih] | |
| rfl | |
| def conditionalRate (D : Nat) (accepts : Ξ± β Bool) (x : X) : β := | |
| β e, if accepts (entries x e) then (count (shape x) e : β) / D else 0 | |
| def average (D : Nat) (xs : List X) (accepts : Ξ± β Bool) : β := | |
| (xs.map (conditionalRate count shape entries D accepts)).sum / xs.length | |
| theorem localCount_div (D : Nat) (accepts : Ξ± β Bool) (x : X) : | |
| (localCount count shape entries accepts x : β) / D = | |
| conditionalRate count shape entries D accepts x := by | |
| rw [localCount, Nat.cast_sum, Finset.sum_div] | |
| apply Finset.sum_congr rfl | |
| intro e _ | |
| cases accepts (entries x e) <;> simp | |
| theorem conditionalRate_sum (D : Nat) (xs : List X) (accepts : Ξ± β Bool) : | |
| (xs.map (conditionalRate count shape entries D accepts)).sum = | |
| ((xs.map (localCount count shape entries accepts)).sum : β) / D := by | |
| induction xs with | |
| | nil => simp | |
| | cons x xs ih => | |
| simp only [List.map_cons, List.sum_cons, Nat.cast_add] | |
| rw [β localCount_div, ih, add_div] | |
| theorem rate_eq (D : Nat) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (accepts : Ξ± β Bool) : | |
| (((output count shape entries xs).filter accepts).length : β) / | |
| (output count shape entries xs).length = | |
| average count shape entries D xs accepts := by | |
| rw [output_filter_length, output_length count shape entries D htotal, | |
| Nat.cast_mul, average, conditionalRate_sum] | |
| exact div_mul_eq_div_div _ _ _ | |
| theorem output_support (xs : List X) (entry : Ξ±) | |
| (hentry : entry β output count shape entries xs) : | |
| β x β xs, β e : E (shape x), entries x e = entry := by | |
| obtain β¨x, hx, hlocalβ© := List.mem_flatMap.mp hentry | |
| obtain β¨e, _, heβ© := List.mem_map.mp hlocal | |
| exact β¨x, hx, e, heβ© | |
| end Lists | |
| section Target | |
| variable {l r q : Nat} (shape : X β S) | |
| (entries : (x : X) β E (shape x) β Edge l r q) | |
| def construct (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) : Instance q where | |
| leftVertices := l | |
| rightVertices := r | |
| edges := output count shape entries xs | |
| nonempty := output_nonempty count shape entries D hD htotal xs hxs | |
| theorem construct_length (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) : | |
| (construct count shape entries D hD htotal xs hxs).edges.length = D * xs.length := | |
| output_length count shape entries D htotal xs | |
| theorem count_eq (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) | |
| (a : Fin l β Fin (2 * q)) (b : Fin r β Fin q) : | |
| countSatisfied a b (construct count shape entries D hD htotal xs hxs).edges = | |
| (xs.map (fun x => β e, if (entries x e).satisfied a b then count (shape x) e else 0)).sum := by | |
| change countSatisfied a b (output count shape entries xs) = _ | |
| rw [RoundedTarget.countSatisfied_eq_filter] | |
| exact output_filter_length count shape entries xs (fun e => e.satisfied a b) | |
| theorem construct_rate_eq (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) | |
| (a : Fin l β Fin (2 * q)) (b : Fin r β Fin q) : | |
| (countSatisfied a b (construct count shape entries D hD htotal xs hxs).edges : β) / | |
| (construct count shape entries D hD htotal xs hxs).edges.length = | |
| average count shape entries D xs (fun e => e.satisfied a b) := by | |
| change (countSatisfied a b (output count shape entries xs) : β) / | |
| (output count shape entries xs).length = _ | |
| rw [RoundedTarget.countSatisfied_eq_filter] | |
| exact rate_eq count shape entries D htotal xs (fun e => e.satisfied a b) | |
| theorem value_le (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) (hq : 0 < q) (bound : β) | |
| (hsound : β a : Fin l β Fin (2 * q), β b : Fin r β Fin q, | |
| average count shape entries D xs (fun e => e.satisfied a b) β€ bound) : | |
| (construct count shape entries D hD htotal xs hxs).value β€ bound := by | |
| obtain β¨s, hsβ© := (construct count shape entries D hD htotal xs hxs).maxSatisfied_attained hq | |
| unfold Instance.value | |
| rw [β hs] | |
| exact (construct_rate_eq count shape entries D hD htotal xs hxs s.1 s.2).le.trans | |
| (hsound s.1 s.2) | |
| theorem perfectlyComplete (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) | |
| (hcomplete : β s : RoundedTarget.EndpointLabeling l r q, | |
| β x β xs, β e : E (shape x), (entries x e).satisfied s.1 s.2 = true) : | |
| PerfectlyComplete (construct count shape entries D hD htotal xs hxs) := by | |
| obtain β¨s, hsβ© := hcomplete | |
| refine β¨s, ?_β© | |
| intro edge hedge | |
| obtain β¨x, hx, e, heβ© := output_support count shape entries xs edge hedge | |
| rw [β he] | |
| exact hs x hx e | |
| theorem complete_value_eq_one (D : Nat) (hD : 0 < D) (htotal : β s, β e, count s e = D) | |
| (xs : List X) (hxs : xs β []) (hq : 0 < q) | |
| (hcomplete : β s : RoundedTarget.EndpointLabeling l r q, | |
| β x β xs, β e : E (shape x), (entries x e).satisfied s.1 s.2 = true) : | |
| (construct count shape entries D hD htotal xs hxs).value = 1 := | |
| ((construct count shape entries D hD htotal xs hxs).perfectlyComplete_iff_value_eq_one hq).1 | |
| (perfectlyComplete count shape entries D hD htotal xs hxs hcomplete) | |
| end Target | |
| theorem conditionalRate_uniformization {Ξ± : Type*} (w : β s, E s β β) | |
| (U : ExactRationalMultiplicity.Uniformization w) (shape : X β S) | |
| (entries : (x : X) β E (shape x) β Ξ±) (accepts : Ξ± β Bool) (x : X) : | |
| conditionalRate U.count shape entries U.denominator accepts x = | |
| β e, if accepts (entries x e) then (w (shape x) e : β) else 0 := by | |
| apply Finset.sum_congr rfl | |
| intro e _ | |
| have h := congrArg (fun z : β => (z : β)) (U.fraction (shape x) e) | |
| simp only [Rat.cast_div, Rat.cast_natCast] at h | |
| cases accepts (entries x e) <;> simp [h] | |
| end | |
| end PerfectCompleteness.ExactConditionalOutput | |
| end OAI | |