File size: 3,445 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import OAI.Combinatorics.LowSpaceSubsetSum.Word.WordSeedMeasured
import OAI.Combinatorics.LowSpaceSubsetSum.Word.WordSeedExecution

namespace OAI

/-! # Sequential composition of measured seed generation and a stored action

The generated seed stays resident while the prepared action runs. Generation
work and action work are both charged, including the exhaustion branch.
-/

noncomputable section
namespace LowSpaceSubsetSum.WordSampledAction

open WordSeedRealization WordSeedMeasured

variable {w N : ℕ} {α : Type} [Fintype α] {S : SamplerCoupling α}

structure Result where
  answer : Bool
  work : ℕ
  cells : ℕ
  cursor : ℕ

def run {X : Realization w N S} (E : Execution X)
    (action : S.Seed → (Bool × ℕ) × ℕ)
    (stream : ℕ → Fin (2^w)) (cursor : ℕ) : Result :=
  let generated := E.run stream cursor
  match generated.accepted with
  | none => ⟨false,generated.work+1,max generated.cells 8,generated.cursor⟩
  | some accepted =>
      let out := action accepted.seed.val
      ⟨out.1.1,generated.work+out.1.2+1,
        max generated.cells (generated.retained+out.2+8),generated.cursor⟩

theorem value {X : Realization w N S} (E : Execution X)
    (action : S.Seed → (Bool × ℕ) × ℕ)
    (stream : ℕ → Fin (2^w)) (cursor : ℕ) :
    (run E action stream cursor).answer =
      match (WordProgram.run stream X.program cursor).1 with
      | none => false | some s => (action s.val).1.1 := by
  have h := congrArg Prod.fst (E.value stream cursor)
  rw [←h]
  cases hg : (E.run stream cursor).accepted <;>
    simp only [run,hg,Option.map_none,Option.map_some]

theorem finite_value {X : Realization w N S} (E : Execution X)
    (action : S.Seed → (Bool × ℕ) × ℕ) (words : Fin N → Fin (2^w)) :
    (run E action (WordProgram.finiteStream words) 0).answer =
      WordSeedRealization.execute X (fun s => (action s).1.1) false words :=
  value E action _ 0

theorem cursor_eq {X : Realization w N S} (E : Execution X)
    (action : S.Seed → (Bool × ℕ) × ℕ)
    (stream : ℕ → Fin (2^w)) (cursor : ℕ) :
    (run E action stream cursor).cursor = (WordProgram.run stream X.program cursor).2 := by
  have h := congrArg Prod.snd (E.value stream cursor)
  rw [←h]
  cases hg : (E.run stream cursor).accepted <;> simp only [run,hg]

theorem cursor_le {X : Realization w N S} (E : Execution X)
    (action : S.Seed → (Bool × ℕ) × ℕ)
    (stream : ℕ → Fin (2^w)) (cursor : ℕ) :
    (run E action stream cursor).cursor ≤ cursor+N := by
  rw [cursor_eq]
  exact (WordProgram.run_bound X.program N X.bounded stream cursor).2

theorem bounded {X : Realization w N S} (E : Execution X)
    (action : S.Seed → (Bool × ℕ) × ℕ) (W C R V D : ℕ)
    (hE : E.Bounded W C R) (hV : ∀ s, (action s).1.2 ≤ V)
    (hD : ∀ s, (action s).2 ≤ D)
    (stream : ℕ → Fin (2^w)) (cursor : ℕ) :
    (run E action stream cursor).work ≤ W+V+1 ∧
      (run E action stream cursor).cells ≤ max C (R+D+8) := by
  have he := hE stream cursor
  cases ha : (E.run stream cursor).accepted with
  | none =>
    simp only [run,ha]
    exact ⟨by omega,max_le (he.2.1.trans (le_max_left _ _))
      ((show 8 ≤ R+D+8 by omega).trans (le_max_right _ _))⟩
  | some a =>
    simp only [run,ha]
    have hv := hV a.seed.val
    have hd := hD a.seed.val
    exact ⟨by omega,max_le_max he.2.1 (by omega)⟩

end LowSpaceSubsetSum.WordSampledAction

end

end OAI