File size: 3,791 Bytes
e63eb7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Combinatorics.EditApproximation.Queries.QueryActionInputs
import OAI.Combinatorics.EditApproximation.Queries.QueryCodeBounds
import OAI.Combinatorics.EditApproximation.Queries.QueryHistoryBounds

namespace OAI

/-! Bounds for the final adaptive child gather.  The temporary list and
its sorting work depend on emitted actions, rather than the full table. -/

namespace EditApproximation

theorem queryActionInputsWithWork_bounds {Parent Child : Type*} {M : ℕ}
    (actions : List (BinaryBellmanAction Parent Child M)) :
    (queryActionInputsWithWork actions).1.length = actions.length * M ∧
    (queryActionInputsWithWork actions).2 ≤ actions.length * (5 * M + 3) + M + 1 := by
  let run := fun action : BinaryBellmanAction Parent Child M =>
    arithmeticMapWithWork (fun i : Fin M => (action.childState i, 1)) (List.finRange M)
  have hlen (action : BinaryBellmanAction Parent Child M) : (run action).1.length = M := by
    simp only [run, arithmeticMapWithWork_value, List.length_map, List.length_finRange]
  have hc (action : BinaryBellmanAction Parent Child M) : (run action).2 ≤ 4 * M := by
    have h := arithmeticMapWithWork_cost (fun i : Fin M => (action.childState i, 1))
      (List.finRange M) 1 (fun _ _ => le_rfl)
    simpa only [List.length_finRange, Nat.reduceAdd, Nat.mul_comm] using h
  constructor
  · simp only [queryActionInputsWithWork, arithmeticFlatMapWithWork_value, List.length_flatMap,
      arithmeticMapWithWork_value, List.length_map, List.length_finRange, List.map_const',
      List.sum_replicate, smul_eq_mul]
  · have h := arithmeticFlatMapWithWork_cost run actions (4 * M) M
      (fun action _ => hc action) (fun action _ => (hlen action).le)
    change (arithmeticFlatMapWithWork run actions).2 + M + 1 ≤ _
    have heq : 4 * M + M + 3 = 5 * M + 3 := by omega
    rw [heq] at h
    omega

theorem queryOrderedChildInputsWithWork_length {M n : ℕ} (inputs : List (Fin M × TargetInterval n)) :
    (queryOrderedChildInputsWithWork inputs).1.length ≤ inputs.length := by
  rw [queryOrderedChildInputsWithWork_value, orderedInputs_length]
  exact List.toFinset_card_le inputs

def queryNeededInputsBitBudget (actions M B : ℕ) : ℕ :=
  actions * (5 * M + 3) + 2 * M + actions * M +
    (actions * M + 1) ^ 2 * (202000 * (B + 1) ^ 2) + 4

theorem queryNeededInputsWithWork_bounds {n M : ℕ}
    (selected online : List (BinaryBellmanAction (TargetInterval n) (Fin M × TargetInterval n) M))
    (B : ℕ) (hM : Nat.size M ≤ B) (hn : Nat.size n ≤ B) :
    (queryNeededInputsWithWork selected online).1.length ≤ (selected.length + online.length) * M ∧
    (queryNeededInputsWithWork selected online).2 ≤
      queryNeededInputsBitBudget (selected.length + online.length) M B := by
  let first := queryActionInputsWithWork selected
  let second := queryActionInputsWithWork online
  have hf := queryActionInputsWithWork_bounds selected
  have hs := queryActionInputsWithWork_bounds online
  have hl : (first.1 ++ second.1).length = (selected.length + online.length) * M := by
    rw [List.length_append, hf.1, hs.1, Nat.add_mul]
  have hsort := queryOrderedChildInputsWithWork_cost (first.1 ++ second.1) B hM hn
  rw [hl] at hsort
  constructor
  · exact (queryOrderedChildInputsWithWork_length (first.1 ++ second.1)).trans_eq hl
  · change first.2 + second.2 + first.1.length +
      (queryOrderedChildInputsWithWork (first.1 ++ second.1)).2 + 2 ≤ _
    have hfc : first.2 ≤ selected.length * (5 * M + 3) + M + 1 := hf.2
    have hsc : second.2 ≤ online.length * (5 * M + 3) + M + 1 := hs.2
    unfold queryNeededInputsBitBudget
    rw [Nat.add_mul]
    have hfl : first.1.length ≤ (selected.length + online.length) * M := by
      rw [hf.1, Nat.add_mul]
      omega
    omega

end EditApproximation

end OAI