math / data /lean /OAI /Computability /RandomMean /Control.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
23.1 kB
import OAI.Computability.RandomMean.Orientation
namespace OAI
section
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " β‰«β‚š " => Code.comp
local infixr:70 " β‹β‚š " => Code.pair
namespace Code
variable {Ξ± Ξ² : Type} [Encoding Ξ±] [Encoding Ξ²]
end Code
namespace RawControl
abbrev Orient := RawOrientation.Data
abbrev Tuple := RawBudget.DataΓ—RawBox.DataΓ—CountΓ—List WΓ—List WΓ—List (List W)Γ—OrientΓ—List Orient
structure Pack where
p : RawBudget.Data
b : RawBox.Data
t : Count
q : List W
r : List W
vs : List (List W)
o : Orient
os : List Orient
def Pack.tuple (z : Pack) : Tuple := (z.p,z.b,z.t,z.q,z.r,z.vs,z.o,z.os)
instance : Encoding Pack := ⟨fun z => encode z.tuple⟩
def zeroBudget : RawBudget.Data := ((Array.zero,Array.zero),(Array.zero,Array.zero),[],[])
def zeroBox : RawBox.Data := (Array.zero,[],[])
def Pack.zero : Pack := ⟨zeroBudget,zeroBox,[],[],[],[],(false,[]),[]⟩
noncomputable def proj {Ξ± : Type} [Encoding Ξ±] (f : Code Tuple Ξ±) : Code Pack Ξ± :=
f.recode Pack.tuple (fun z => f z.tuple) (fun _ => rfl) (fun _ => rfl)
noncomputable def p : Code Pack RawBudget.Data := proj fst
noncomputable def b : Code Pack RawBox.Data := proj (snd β‰«β‚š fst)
noncomputable def t : Code Pack Count := proj (snd β‰«β‚š snd β‰«β‚š fst)
noncomputable def q : Code Pack (List W) := proj (snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š fst)
noncomputable def r : Code Pack (List W) := proj (snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š fst)
noncomputable def vs : Code Pack (List (List W)) := proj (snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š fst)
noncomputable def o : Code Pack Orient := proj (snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š fst)
noncomputable def os : Code Pack (List Orient) := proj (snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd)
@[simp] theorem p_eval (z : Pack) : p z=z.p := rfl
@[simp] theorem b_eval (z : Pack) : b z=z.b := rfl
@[simp] theorem t_eval (z : Pack) : t z=z.t := rfl
@[simp] theorem q_eval (z : Pack) : q z=z.q := rfl
@[simp] theorem r_eval (z : Pack) : r z=z.r := rfl
@[simp] theorem vs_eval (z : Pack) : vs z=z.vs := rfl
@[simp] theorem o_eval (z : Pack) : o z=z.o := rfl
@[simp] theorem os_eval (z : Pack) : os z=z.os := rfl
noncomputable def pack {Ξ± : Type} [Encoding Ξ±]
(p : Code Ξ± RawBudget.Data := const zeroBudget)
(b : Code Ξ± RawBox.Data := const zeroBox)
(t : Code Ξ± Count := const [])
(q r : Code Ξ± (List W) := const [])
(vs : Code Ξ± (List (List W)) := const [])
(o : Code Ξ± Orient := const (false,[]))
(os : Code Ξ± (List Orient) := const []) : Code Ξ± Pack :=
(p β‹β‚š b β‹β‚š t β‹β‚š q β‹β‚š r β‹β‚š vs β‹β‚š o β‹β‚š os).recode _root_.id
(fun x => ⟨p x,b x,t x,q x,r x,vs x,o x,os x⟩) (fun _ => rfl) (fun _ => rfl)
@[simp] theorem pack_eval {Ξ± : Type} [Encoding Ξ±]
(p : Code Ξ± RawBudget.Data) (b : Code Ξ± RawBox.Data) (t : Code Ξ± Count)
(q r : Code Ξ± (List W)) (vs : Code Ξ± (List (List W))) (o : Code Ξ± Orient) (os : Code Ξ± (List Orient)) (x : Ξ±) :
pack p b t q r vs o os x=⟨p x,b x,t x,q x,r x,vs x,o x,os x⟩ := rfl
abbrev Cell := CountΓ—Pack
abbrev State := CellΓ—List Cell
abbrev Env := RawBasic.ArenaΓ—CountΓ—CountΓ—CountΓ—Count
noncomputable def arena : Code Env RawBasic.Arena := fst
noncomputable def exponent : Code Env Count := snd β‰«β‚š fst
noncomputable def samples : Code Env Count := snd β‰«β‚š snd β‰«β‚š fst
noncomputable def trials : Code Env Count := snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š fst
noncomputable def basicSteps : Code Env Count := snd β‰«β‚š snd β‰«β‚š snd β‰«β‚š snd
abbrev Input := EnvΓ—CountΓ—State
noncomputable def env : Code Input Env := fst
noncomputable def random : Code Input Count := snd β‰«β‚š fst
noncomputable def state : Code Input State := snd β‰«β‚š snd
noncomputable def current : Code Input Pack := state β‰«β‚š fst β‰«β‚š snd
noncomputable def stack : Code Input (List Cell) := state β‰«β‚š snd
noncomputable def caller : Code Input Pack := stack β‰«β‚š head ([],Pack.zero) β‰«β‚š snd
noncomputable def rest : Code Input (List Cell) := stack β‰«β‚š tail
noncomputable def makeCell {Ξ± : Type} [Encoding Ξ±] (tag : β„•) (c : Code Ξ± Pack) : Code Ξ± Cell :=
const (List.replicate tag ()) β‹β‚š c
noncomputable def call {Ξ± : Type} [Encoding Ξ±] (tag : β„•) (c : Code Ξ± Pack) (k : Code Ξ± (List Cell)) : Code Ξ± State :=
makeCell tag c β‹β‚š k
noncomputable def ret {Ξ± : Type} [Encoding Ξ±] (c : Code Ξ± Pack) (k : Code Ξ± (List Cell)) : Code Ξ± State := call 10 c k
noncomputable def push {Ξ± : Type} [Encoding Ξ±] (tag : β„•) (c : Code Ξ± Pack) (k : Code Ξ± (List Cell)) : Code Ξ± (List Cell) :=
(makeCell tag c β‹β‚š k) β‰«β‚š cons
noncomputable def save (tag : β„•) : Code Input (List Cell) := push tag current stack
noncomputable def smallWidth (x : Code Input RawBox.Data) : Code Input Bool :=
((x β‰«β‚š RawBox.width) β‹β‚š (env β‰«β‚š samples β‰«β‚š Counter.word β‰«β‚š RawBudget.times 128)) β‰«β‚š Word.le
noncomputable def basic (x : Code Input RawBox.Data) : Code Input (List W) :=
(((env β‰«β‚š arena) β‹β‚š x) β‹β‚š (env β‰«β‚š basicSteps)) β‰«β‚š RawBasic.solve
noncomputable def narrow (x : Code Input RawBox.Data) (v : Code Input (List W)) : Code Input RawBox.Data :=
(((x β‹β‚š (((env β‰«β‚š exponent) β‹β‚š const (List.replicate 5 ())) β‰«β‚š Counter.add)) β‰«β‚š RawBox.dividedWidth) β‹β‚š v) β‰«β‚š RawBox.centered
noncomputable def wide (x : Code Input RawBox.Data) (v : Code Input (List W)) : Code Input RawBox.Data :=
(((x β‹β‚š const (List.replicate 3 ())) β‰«β‚š RawBox.dividedWidth) β‹β‚š v) β‰«β‚š RawBox.centered
noncomputable def delta (x : Code Input RawBox.Data) : Code Input W :=
(x β‹β‚š (((env β‰«β‚š exponent) β‹β‚š const (List.replicate 2 ())) β‰«β‚š Counter.add)) β‰«β‚š RawBox.dividedWidth
noncomputable def capWidth (x : Code Input RawBox.Data) : Code Input W :=
((x β‰«β‚š RawBox.width) β‹β‚š delta x) β‰«β‚š Word.sub
noncomputable def solveCase : Code Input State :=
Code.branch (current β‰«β‚š p β‰«β‚š RawBudget.small)
(ret (pack (q:=((current β‰«β‚š p) β‹β‚š (current β‰«β‚š b)) β‰«β‚š RawBox.smallVector)) stack)
(Code.branch (smallWidth (current β‰«β‚š b))
(ret (pack (q:=basic (current β‰«β‚š b))) stack)
(call 8 (pack (current β‰«β‚š p) (current β‰«β‚š b)) (save 15)))
noncomputable def askCase : Code Input State :=
call 2 (pack (current β‰«β‚š p) (current β‰«β‚š b) (env β‰«β‚š trials)) (save 2)
noncomputable def trialsCase : Code Input State :=
Code.branch (current β‰«β‚š t β‰«β‚š empty)
(ret (pack (vs:=const [])) stack)
(call 0 current (push 0 (pack (current β‰«β‚š p) (current β‰«β‚š b) (current β‰«β‚š t β‰«β‚š tail)) stack))
noncomputable def updateCase : Code Input State :=
call 1 (pack (current β‰«β‚š p β‰«β‚š RawBudget.reduceK) (narrow (current β‰«β‚š b) (current β‰«β‚š q))) (save 3)
noncomputable def centersCase : Code Input State :=
Code.branch (current β‰«β‚š t β‰«β‚š empty)
(ret (pack (vs:=((current β‰«β‚š q) β‹β‚š const []) β‰«β‚š cons)) stack)
(call 3 current (push 5 (pack (current β‰«β‚š p) (current β‰«β‚š b) (current β‰«β‚š t β‰«β‚š tail) (current β‰«β‚š q)) stack))
noncomputable def orientCase : Code Input State :=
Code.branch (smallWidth (current β‰«β‚š b))
(ret (pack (o:=((current β‰«β‚š p) β‹β‚š (current β‰«β‚š b) β‹β‚š basic (current β‰«β‚š b)) β‰«β‚š RawOrientation.base)) stack)
(call 4 (pack (current β‰«β‚š p) (current β‰«β‚š b) (env β‰«β‚š samples) (current β‰«β‚š b β‰«β‚š RawBox.midpoint)) (save 6))
noncomputable def orientTrialsCase : Code Input State :=
Code.branch (current β‰«β‚š t β‰«β‚š empty) (ret (pack (os:=const [])) stack)
(call 5 current (push 8 (pack (current β‰«β‚š p) (current β‰«β‚š b) (current β‰«β‚š t β‰«β‚š tail)) stack))
noncomputable def orientPlusCase : Code Input State :=
call 6 (pack (current β‰«β‚š p) (current β‰«β‚š b) (env β‰«β‚š trials)) (save 10)
noncomputable def tightenCase : Code Input State :=
call 1 (pack (current β‰«β‚š p β‰«β‚š RawBudget.reduceK) (current β‰«β‚š b)) (save 11)
noncomputable def reweightCase : Code Input State :=
Code.branch (current β‰«β‚š o β‰«β‚š snd β‰«β‚š empty) (ret (pack (vs:=const [])) stack)
(call 1 (pack
(Code.branch (current β‰«β‚š o β‰«β‚š fst)
(((current β‰«β‚š p) β‹β‚š (current β‰«β‚š o β‰«β‚š snd β‰«β‚š head [])) β‰«β‚š RawBudget.reweightM)
(((current β‰«β‚š p) β‹β‚š (current β‰«β‚š o β‰«β‚š snd β‰«β‚š head [])) β‰«β‚š RawBudget.reweightK))
(current β‰«β‚š b))
(push 17 (pack (current β‰«β‚š p) (current β‰«β‚š b)
(o:=(current β‰«β‚š o β‰«β‚š fst) β‹β‚š (current β‰«β‚š o β‰«β‚š snd β‰«β‚š tail))) stack))
end RawControl
end MeanPayoff.TreeDevice.Certified
end
section
namespace MeanPayoff.TreeDevice.Certified
open BinaryData Code
local infixl:80 " β‰«β‚š " => Code.comp
local infixr:70 " β‹β‚š " => Code.pair
namespace Array
theorem fold_select_bound (f : Code (WΓ—W) W) (hf : βˆ€ z, f z=z.1 ∨ f z=z.2)
{N : β„•} {xs : List W} {xss : List (List W)} (h : Bounded xs N)
(hs : βˆ€ ys∈xss, Bounded ys N) :
Bounded (Fold.fold (zip zero zero f).eval xs xss) N ∧
(Fold.fold (zip zero zero f).eval xs xss).length=xs.length := by
induction xss generalizing xs with
| nil => exact ⟨h,rfl⟩
| cons ys yss ih =>
change Bounded (Fold.fold _ (zip zero zero f (xs,ys)) yss) N ∧ _
have hh := ih (bounded_zip h (hs ys (by simp)) f hf) (fun zs hz => hs zs (by simp [hz]))
exact ⟨hh.1,hh.2.trans (zip_length f.eval zero xs ys)⟩
noncomputable def foldSelect (f : Code (WΓ—W) W) (hf : βˆ€ z, f z=z.1 ∨ f z=z.2) :
Code (List WΓ—List (List W)) (List W) :=
foldPrefix (zip zero zero f) 20 2 (by
intro xs xss k _hk
let S := treeSize (encode (xs,xss))
have hx : Bounded xs S := fun w hw => (digits_le_wordSize w).trans ((Size.mem hw).trans (Size.fst (xs,xss)))
have hxs : βˆ€ ys∈xss.take k, Bounded ys S := by
intro ys hy w hw
exact (digits_le_wordSize w).trans ((Size.mem hw).trans
((Size.mem (List.mem_of_mem_take hy)).trans (Size.snd (xs,xss))))
have hb := fold_select_bound f hf hx hxs
have ht := size_bound hb.1
rw [hb.2] at ht
have hl := (Size.length xs).trans (Size.fst (xs,xss))
change xs.length≀S at hl
have hm := Nat.mul_le_mul_right (4*S+6) hl
calc
treeSize (encode (Fold.fold (zip zero zero f).eval xs (xss.take k))) ≀ xs.length*(4*S+6)+1 := ht
_ ≀ 20*(S+1)^2 := by nlinarith)
noncomputable def foldMin : Code (List WΓ—List (List W)) (List W) := foldSelect Word.min EdgeScan.low_mem
noncomputable def foldMax : Code (List WΓ—List (List W)) (List W) := foldSelect Word.max EdgeScan.high_mem
theorem foldMin_rep {n B : β„•} {I : SolveBox n B} (v : BoxVector I) {xs : List W}
(hv : VectorRep v.val xs) {vs : List (BoxVector I)} {xss : List (List W)}
(hs : VectorsRep (vs.map Subtype.val) xss) :
VectorRep (vs.foldl I.minVector v).val (foldMin (xs,xss)) := by
induction vs generalizing v xs xss with
| nil => cases hs; exact hv
| cons w ws ih =>
cases hs with
| cons hw hws =>
exact ih (I.minVector v w) (Array.zip_rep Word.min Min.min (fun _ _ => Word.min_val _ _) hv hw) hws
theorem foldMax_rep {n B : β„•} {I : SolveBox n B} (v : BoxVector I) {xs : List W}
(hv : VectorRep v.val xs) {vs : List (BoxVector I)} {xss : List (List W)}
(hs : VectorsRep (vs.map Subtype.val) xss) :
VectorRep (vs.foldl I.maxVector v).val (foldMax (xs,xss)) := by
induction vs generalizing v xs xss with
| nil => cases hs; exact hv
| cons w ws ih =>
cases hs with
| cons hw hws =>
exact ih (I.maxVector v w) (Array.zip_rep Word.max Max.max (fun _ _ => Word.max_val _ _) hv hw) hws
end Array
end MeanPayoff.TreeDevice.Certified
end
section
namespace MeanPayoff.TreeDevice.Certified.RawControl
open BinaryData Code
local infixl:80 " β‰«β‚š " => Code.comp
local infixr:70 " β‹β‚š " => Code.pair
noncomputable def trialsHeadCase : Code Input State :=
call 2 (pack (caller β‰«β‚š p) (caller β‰«β‚š b) (caller β‰«β‚š t))
(push 1 (pack (q:=current β‰«β‚š q)) rest)
noncomputable def vectorConsCase : Code Input State :=
ret (pack (vs:=((caller β‰«β‚š q) β‹β‚š (current β‰«β‚š vs)) β‰«β‚š cons)) rest
noncomputable def medianCase : Code Input State :=
ret (pack (q:=((caller β‰«β‚š b) β‹β‚š (current β‰«β‚š vs)) β‰«β‚š RawMedian.boxed)) rest
noncomputable def centerFirstCase : Code Input State :=
call 1 (pack (caller β‰«β‚š p β‰«β‚š RawBudget.reduceM) (narrow (caller β‰«β‚š b) (caller β‰«β‚š q)))
(push 4 (pack (b:=caller β‰«β‚š b) (r:=current β‰«β‚š q)) rest)
noncomputable def centerSecondCase : Code Input State :=
ret (pack (q:=((caller β‰«β‚š b) β‹β‚š (caller β‰«β‚š r) β‹β‚š (current β‰«β‚š q)) β‰«β‚š RawBox.averageClipped)) rest
noncomputable def centersHeadCase : Code Input State :=
call 4 (pack (caller β‰«β‚š p) (caller β‰«β‚š b) (caller β‰«β‚š t) (current β‰«β‚š q))
(push 1 (pack (q:=caller β‰«β‚š q)) rest)
noncomputable def centerAt (index : Code Input Count) : Code Input (List W) :=
(((current β‰«β‚š vs) β‹β‚š index) β‹β‚š (caller β‰«β‚š b β‰«β‚š RawBox.midpoint)) β‰«β‚š getD []
noncomputable def lastPositive : Code Input (List Bool) :=
((caller β‰«β‚š b) β‹β‚š (env β‰«β‚š samples β‰«β‚š Counter.word) β‹β‚š centerAt (env β‰«β‚š samples)) β‰«β‚š RawOrientation.positives
noncomputable def lastNegative : Code Input (List Bool) :=
((caller β‰«β‚š b) β‹β‚š (env β‰«β‚š samples β‰«β‚š Counter.word) β‹β‚š centerAt (env β‰«β‚š samples)) β‰«β‚š RawOrientation.negatives
noncomputable def orientLoopCase : Code Input State :=
Code.branch (((caller β‰«β‚š p) β‹β‚š lastPositive) β‰«β‚š RawBudget.measureLarge)
(ret (pack (o:=lastPositive β‰«β‚š RawOrientation.trueComplement)) rest)
(Code.branch (((caller β‰«β‚š p) β‹β‚š lastNegative) β‰«β‚š RawBudget.measureLarge)
(ret (pack (o:=lastNegative β‰«β‚š RawOrientation.falseComplement)) rest)
(call 5 (pack (caller β‰«β‚š p) (wide (caller β‰«β‚š b) (centerAt random)))
(push 7 (pack (o:=const false β‹β‚š ((lastPositive β‹β‚š ((lastNegative β‹β‚š const []) β‰«β‚š cons)) β‰«β‚š cons))) rest)))
noncomputable def orientTailCase : Code Input State :=
ret (pack (o:=((current β‰«β‚š o) β‹β‚š (caller β‰«β‚š o β‰«β‚š snd β‰«β‚š head []) β‹β‚š
(caller β‰«β‚š o β‰«β‚š snd β‰«β‚š tail β‰«β‚š head [])) β‰«β‚š RawOrientation.extend)) rest
noncomputable def orientHeadCase : Code Input State :=
call 6 (pack (caller β‰«β‚š p) (caller β‰«β‚š b) (caller β‰«β‚š t))
(push 9 (pack (o:=current β‰«β‚š o)) rest)
noncomputable def orientConsCase : Code Input State :=
ret (pack (os:=((caller β‰«β‚š o) β‹β‚š (current β‰«β‚š os)) β‰«β‚š cons)) rest
noncomputable def combineCase : Code Input State :=
ret (pack (o:=current β‰«β‚š os β‰«β‚š RawOrientation.combine)) rest
noncomputable def tightenFirstCase : Code Input State :=
Code.branch (((caller β‰«β‚š b) β‹β‚š (current β‰«β‚š q) β‹β‚š delta (caller β‰«β‚š b)) β‰«β‚š RawBox.movedFloor)
(call 8 (pack (caller β‰«β‚š p) (((caller β‰«β‚š b) β‹β‚š (current β‰«β‚š q)) β‰«β‚š RawBox.withFloor))
(push 13 (pack (b:=caller β‰«β‚š b) (q:=current β‰«β‚š q)) rest))
(call 1 (pack (caller β‰«β‚š p β‰«β‚š RawBudget.reduceM) (caller β‰«β‚š b)) (push 12 caller rest))
noncomputable def tightenSecondCase : Code Input State :=
Code.branch (((caller β‰«β‚š b) β‹β‚š (current β‰«β‚š q) β‹β‚š delta (caller β‰«β‚š b)) β‰«β‚š RawBox.movedCeiling)
(call 8 (pack (caller β‰«β‚š p) (((caller β‰«β‚š b) β‹β‚š (current β‰«β‚š q)) β‰«β‚š RawBox.withCeiling))
(push 14 (pack (b:=caller β‰«β‚š b) (q:=current β‰«β‚š q)) rest))
(ret (pack (b:=caller β‰«β‚š b)) rest)
noncomputable def boxPassCase : Code Input State := ret (pack (b:=current β‰«β‚š b)) rest
noncomputable def solveTightCase : Code Input State :=
call 7 (pack (caller β‰«β‚š p) (current β‰«β‚š b))
(push 16 (pack (caller β‰«β‚š p) (current β‰«β‚š b)) (push 21 (const Pack.zero) rest))
noncomputable def solveOrientedCase : Code Input State :=
call 9 (pack (caller β‰«β‚š p) (caller β‰«β‚š b) (o:=current β‰«β‚š o))
(push 18 (pack (caller β‰«β‚š p) (caller β‰«β‚š b) (o:=(current β‰«β‚š o β‰«β‚š fst) β‹β‚š const [])) rest)
noncomputable def reweightHeadCase : Code Input State :=
call 9 (pack (caller β‰«β‚š p) (caller β‰«β‚š b) (o:=caller β‰«β‚š o))
(push 1 (pack (q:=current β‰«β‚š q)) rest)
noncomputable def solveReweightedCase : Code Input State :=
Code.branch (caller β‰«β‚š o β‰«β‚š fst)
(call 0 (pack (caller β‰«β‚š p) (((caller β‰«β‚š b) β‹β‚š capWidth (caller β‰«β‚š b)) β‰«β‚š RawBox.capHigh))
(push 19 (pack (b:=caller β‰«β‚š b) (vs:=current β‰«β‚š vs)) rest))
(call 0 (pack (caller β‰«β‚š p) (((caller β‰«β‚š b) β‹β‚š capWidth (caller β‰«β‚š b)) β‰«β‚š RawBox.capLow))
(push 20 (pack (b:=caller β‰«β‚š b) (vs:=current β‰«β‚š vs)) rest))
noncomputable def capHighCase : Code Input State :=
ret (pack (q:=((current β‰«β‚š q) β‹β‚š (caller β‰«β‚š vs)) β‰«β‚š Array.foldMin)) rest
noncomputable def capLowCase : Code Input State :=
ret (pack (q:=((current β‰«β‚š q) β‹β‚š (caller β‰«β‚š vs)) β‰«β‚š Array.foldMax)) rest
noncomputable def includeInnerCase : Code Input State := ret (pack (q:=current β‰«β‚š q)) rest
noncomputable def resume : Code Input State :=
Code.branch (stack β‰«β‚š empty) state
(select (stack β‰«β‚š head ([],Pack.zero) β‰«β‚š fst)
[trialsHeadCase,vectorConsCase,medianCase,centerFirstCase,centerSecondCase,centersHeadCase,
orientLoopCase,orientTailCase,orientHeadCase,orientConsCase,combineCase,tightenFirstCase,
tightenSecondCase,boxPassCase,boxPassCase,solveTightCase,solveOrientedCase,reweightHeadCase,
solveReweightedCase,capHighCase,capLowCase,includeInnerCase] state)
noncomputable def dispatch : Code Input State :=
select (state β‰«β‚š fst β‰«β‚š fst)
[solveCase,askCase,trialsCase,updateCase,centersCase,orientCase,orientTrialsCase,orientPlusCase,
tightenCase,reweightCase] resume
end MeanPayoff.TreeDevice.Certified.RawControl
end
section
namespace MeanPayoff.SourceControl
def sampledResume {G : Game} {B : β„•} {A C R : Result G B} (f : Frame G B A C) (v : A.Value)
(k : Kont G B C R) (j : β„•) : State G B R :=
match f with
| .trialsHead P I J => (.call (.trials P I J) (.push (.vectorCons v) k))
| .vectorCons a => (.ret k (a :: v))
| .median I => (.ret k (I.medianBox v))
| .centerFirst P _ I hB q =>
(.call (.ask P.reduceM (I.narrowCentered hB q.val)) (.push (.centerSecond I hB q v) k))
| .centerSecond I _ _ r => (.ret k (I.averageClipped r.val v.val))
| .centersHead P hR I hB t q =>
(.call (.centers P hR I hB t v) (.push (.vectorCons q) k))
| .orientLoop P hR I hB =>
let H := PositiveDisplacement I (v.getD (globalS B) I.midpoint)
let N := NegativeDisplacement I (v.getD (globalS B) I.midpoint)
if hH : 1 / 2 < P.measure H then
(.ret k ⟨true, [⟨Hᢜ, by rw [P.measure_compl G.positive]; linarith⟩]⟩)
else if hN : 1 / 2 < P.measure N then
(.ret k ⟨false, [⟨Nᢜ, by rw [P.measure_compl G.positive]; linarith⟩]⟩)
else
.call (.orient P hR (I.wideCentered hB (v.getD j I.midpoint).val))
(.push (.orientTail ⟨H, le_of_not_gt hH⟩ ⟨N, le_of_not_gt hN⟩) k)
| .orientTail H N => (.ret k ⟨v.chooseY, v.exceptions ++ [H, N]⟩)
| .orientHead P hR I J => (.call (.orientTrials P hR I J) (.push (.orientCons v) k))
| .orientCons O => (.ret k (O :: v))
| .combine _ => (.ret k (combineOrientations v))
| .tightenFirst P hR I hB =>
if _ : βˆƒ i, (I.delta : β„€) ≀ v.val i - I.a i then
(.call (.tighten P hR (I.withFloor v) hB) (.push (.floor I v) k))
else (.call (.ask P.reduceM I) (.push (.tightenSecond P hR I hB) k))
| .tightenSecond P hR I hB =>
if _ : βˆƒ i, (I.delta : β„€) ≀ I.b i - v.val i then
(.call (.tighten P hR (I.withCeiling v) hB) (.push (.ceiling I v) k))
else (.ret k ⟨I, rfl, le_refl _, le_refl _⟩)
| .floor _ q => (.ret k ⟨v.box, v.width_eq, q.property.1.trans v.floor_le, v.ceiling_le⟩)
| .ceiling _ q => (.ret k ⟨v.box, v.width_eq, v.floor_le, v.ceiling_le.trans q.property.2⟩)
| .solveTight P hR _ hB =>
have hK : 128 * globalS B < v.box.width := by rw [v.width_eq]; exact hB
(.call (.orientPlus P hR v.box) (.push (.solveOriented P hR v.box hK) (.push (.includeInner v) k)))
| .solveOriented P hR I hB =>
(.call (.reweight P hR I v.chooseY v.exceptions) (.push (.solveReweighted P hR I hB v.chooseY) k))
| .reweightHead P hR I side Hs => (.call (.reweight P hR I side Hs) (.push (.vectorCons v) k))
| .solveReweighted P _ I hB side =>
if side then (.call (.solve P (I.capHigh I.C I.C_positive (I.C_smaller hB).le)) (.push (.capHigh I hB v) k))
else (.call (.solve P (I.capLow I.C I.C_positive (I.C_smaller hB).le)) (.push (.capLow I hB v) k))
| .capHigh I hB rs => (.ret k (rs.foldl I.minVector (I.includeCapHigh I.C I.C_positive (I.C_smaller hB).le v)))
| .capLow I hB rs => (.ret k (rs.foldl I.maxVector (I.includeCapLow I.C I.C_positive (I.C_smaller hB).le v)))
| .includeInner T => (.ret k (T.include v))
def sampledStep {G : Game} {B : β„•} {R : Result G B} (j : β„•) : State G B R β†’ State G B R
| .ret .done v => (.ret .done v)
| .ret (.push f k) v => sampledResume f v k j
| .call job k => match job with
| .solve P I => if hR : P.parameter < 1 then (.ret k (I.smallVector P))
else if hB : I.width ≀ 128 * globalS B then (.ret k (I.exactVector G))
else (.call (.tighten P (le_of_not_gt hR) I (by omega)) (.push (.solveTight P (le_of_not_gt hR) I (by omega)) k))
| .ask P I => (.call (.trials P I (globalJ G.n B)) (.push (.median I) k))
| .trials P I J => match J with
| 0 => (.ret k [])
| J + 1 => (.call (.solve P I) (.push (.trialsHead P I J) k))
| .update P hR I hB q =>
(.call (.ask P.reduceK (I.narrowCentered hB q.val)) (.push (.centerFirst P hR I hB q) k))
| .centers P hR I hB t q => match t with
| 0 => (.ret k [q])
| t + 1 => (.call (.update P hR I hB q) (.push (.centersHead P hR I hB t q) k))
| .orient P hR I =>
if hB : I.width ≀ 128 * globalS B then (.ret k (orientBase G P I))
else (.call (.centers P hR I (by omega) (globalS B) I.midpoint) (.push (.orientLoop P hR I (by omega)) k))
| .orientTrials P hR I J => match J with
| 0 => (.ret k [])
| J + 1 => (.call (.orient P hR I) (.push (.orientHead P hR I J) k))
| .orientPlus P hR I => (.call (.orientTrials P hR I (globalJ G.n B)) (.push (.combine P) k))
| .tighten P hR I hB => (.call (.ask P.reduceK I) (.push (.tightenFirst P hR I hB) k))
| .reweight P hR I side Hs => match Hs with
| [] => (.ret k [])
| H :: Hs =>
(.call (.ask (if side then P.reweightM H.val else P.reweightK H.val) I)
(.push (.reweightHead P hR I side Hs) k))
end MeanPayoff.SourceControl
end
end OAI