import OAI.Combinatorics.ContingencyTables.Sampling.ExactIntegerChoice import OAI.Combinatorics.ContingencyTables.Machines.ListWordProgram import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialSums namespace OAI /-! # Binary execution of an exact integer-weighted rejection trial -/ namespace ContingencyTables.ExactIntegerPrograms open MatchingFPRAS MatchingFPRAS.TreeTyped ProfilePrograms /-- Fractions share the total integer weight before canonical reduction. -/ def weightsCode (xs : List ℕ) : List TreeFraction.Fraction := xs.map (fun w => TreeFraction.normalize (w,xs.sum)) def weightsCodeRealizer : Realizer weightsCode := composition (f := fun xs : List ℕ => (xs.sum,xs)) (g := fun z : ℕ×List ℕ => z.2.map (fun w => TreeFraction.normalize (w,z.1))) (pair listSum identity) (mapWith (composition (pair second first) TreeFraction.normalizeRealizer)) lemma polynomial_weightsCode : PolynomialTime weightsCodeRealizer := polynomial_composition (polynomial_pair polynomial_listSum polynomial_identity) (polynomial_mapWith (polynomial_composition (polynomial_pair polynomial_second polynomial_first) TreeFraction.polynomial_normalize)) abbrev Input := List ℕ×List Bool def choiceInput (z : Input) : List TreeFraction.Fraction×TreeFraction.Fraction := (weightsCode z.1,(StackAddition.value z.2,z.1.sum)) def choiceInputRealizer : Realizer choiceInput := pair (composition first weightsCodeRealizer) (pair (composition second bitValue) (composition first listSum)) lemma polynomial_choiceInput : PolynomialTime choiceInputRealizer := polynomial_pair (polynomial_composition polynomial_first polynomial_weightsCode) (polynomial_pair (polynomial_composition polynomial_second polynomial_bitValue) (polynomial_composition polynomial_first polynomial_listSum)) /-- One trial returns an index or requests another independent word. All integers are processed in binary; no weight is expanded into copies. -/ def weightedIndex (z : Input) : Option ℕ := if StackAddition.value z.2 TreeFraction.repr (normalized w i)) := by simp only [weightsCode,List.sum_ofFn,List.map_ofFn,TreeFraction.normalize_repr] rfl /-- The compiled binary trial is exactly the integer-grid experiment. -/ lemma weightedIndex_ofFn {r k : ℕ} (w : Fin r → ℕ) (h : 0