File size: 5,604 Bytes
760f90e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import OAI.Computability.Superstring.Tables

namespace OAI

namespace Superstring.PrefixGreedy
open BitCode
variable {α : Type} [Coded α]

@[fun_prop] theorem poly_prefer : Poly (fun q : Token α × Token α => prefer q.1 q.2) := by
  have hp : Poly (fun q : Token α × Token α => if less q.1.2.length q.2.2.length then q.2 else q.1) := by
    fun_prop
  simpa only [prefer, less, decide_eq_true_eq] using hp

@[fun_prop] theorem poly_largest : Poly (largest : List (Token α) → Token α) := by
  have hs : ∀ a b : Token α, bitSize (prefer a b) ≤ bitSize a +
      (Polynomial.X : Polynomial ℕ).eval (bitSize b) := by
    intro a b
    unfold prefer
    split <;> simp only [Polynomial.eval_X] <;> omega
  have hf := BitCode.Poly.foldl_additive poly_prefer Polynomial.X hs
  exact hf.comp (BitCode.Poly.id.pair (BitCode.Poly.headD (0,[])))

variable [DecidableEq α]

@[fun_prop] theorem poly_prefixB : Poly (fun q : List α × List α => prefixB q.1 q.2) := by
  have hp : Poly (fun q : List α × List α => equal q.1 (q.2.take q.1.length)) := by fun_prop
  convert hp using 1
  funext q
  apply Bool.eq_iff_iff.mpr
  simp only [prefixB, equal, decide_eq_true_eq, List.prefix_iff_eq_take]

@[fun_prop] theorem poly_remove : Poly (fun q : Token α × List (Token α) => remove q.1 q.2) := by
  have hp := BitCode.Poly.filterFn (p := fun q : (Token α × List (Token α)) × Token α =>
    !(equal q.2 q.1.1)) (by fun_prop) (BitCode.Poly.snd : Poly (Prod.snd : Token α × List (Token α) → _))
  convert hp using 1
  funext q
  simp only [remove, equal, decide_not]

@[fun_prop] theorem poly_extension : Poly (fun q : Token α × List (Token α) => extension q.1 q.2) := by
  unfold extension
  exact (BitCode.Poly.headD (0,[])).comp (BitCode.Poly.filterFn
    (p := fun q : (Token α × List (Token α)) × Token α => prefixB q.1.1.2 q.2.2)
    (by fun_prop) BitCode.Poly.snd)

@[fun_prop] theorem poly_step : Poly (step : State α → State α) := by
  unfold step
  fun_prop

omit [DecidableEq α] in
theorem largest_size {R : List (Token α)} (hR : R ≠ []) : bitSize (largest R) ≤ bitSize R :=
  bitSize_mem (largest_spec R hR).1

theorem extension_size (a : Token α) (S : List (Token α)) :
    bitSize (extension a S) ≤ bitSize S + 2 := by
  have hb : ∀ xs : List (Token α), bitSize (xs.headD (0,[])) ≤ bitSize xs + 2 := by
    intro xs
    cases xs with
    | nil => simp
    | cons x xs => simp only [List.headD_cons, bitSize_cons]; omega
  have hh := hb (S.filter (fun b => prefixB a.2 b.2))
  have hm := bitSize_sublist (List.filter_sublist (p := fun b => prefixB a.2 b.2) (l := S))
  exact le_trans hh (Nat.add_le_add_right hm 2)

theorem step_bounds (q : State α) :
    bitSize (step q).1 + bitSize (step q).2.1 ≤ bitSize q.1 + bitSize q.2.1 ∧
    bitSize (step q).2.2 ≤ bitSize q.2.2 + (bitSize q.1 + bitSize q.2.1 + 3) := by
  by_cases he : q.1.isEmpty = true
  · simp only [step, he, ite_true]
    omega
  · have hne : q.1 ≠ [] := by simpa only [List.isEmpty_iff] using he
    have ha := largest_size hne
    have hb := extension_size (largest q.1) q.2.1
    have hr := bitSize_sublist (List.filter_sublist (p := fun b => decide (b ≠ largest q.1)) (l := q.1))
    have hs := bitSize_sublist (List.filter_sublist (p := fun b => decide (b ≠ extension (largest q.1) q.2.1)) (l := q.2.1))
    simp only [step, he, Bool.false_eq_true, ite_false, bitSize_cons, bitSize_prod, remove]
    constructor <;> omega

theorem iterate_size (q : State α) (n : ℕ) :
    bitSize (step^[n] q).1 + bitSize (step^[n] q).2.1 ≤ bitSize q.1 + bitSize q.2.1 ∧
    bitSize (step^[n] q).2.2 ≤ bitSize q.2.2 + n * (bitSize q.1 + bitSize q.2.1 + 3) := by
  induction n with
  | zero => simp
  | succ n ih =>
    rw [Function.iterate_succ_apply']
    have hh := step_bounds (step^[n] q)
    constructor
    · exact hh.1.trans ih.1
    · rw [Nat.succ_mul]
      omega

@[fun_prop] theorem poly_iterate : Poly (fun q : ℕ × State α => step^[q.1] q.2) := by
  apply BitCode.Poly.iterate poly_step (Polynomial.X * Polynomial.X + 4 * Polynomial.X + 4)
  intro n q j hj
  have h := iterate_size q j
  rcases q with ⟨R,S,P⟩
  have ht : bitSize (step^[j] (R,S,P)) ≤
      bitSize R + bitSize S + bitSize P + j * (bitSize R + bitSize S + 3) := by
    generalize he : step^[j] (R,S,P) = z at *
    rcases z with ⟨R',S',P'⟩
    simp only [bitSize_prod]
    dsimp only at h
    omega
  simp only [bitSize_prod, bitSize_nat, Polynomial.eval_add, Polynomial.eval_mul,
    Polynomial.eval_X, Polynomial.eval_ofNat]
  have hjn : j ≤ n := by omega
  have hm := Nat.mul_le_mul_right (bitSize R + bitSize S + 3) hjn
  nlinarith

def compute (R S : List (Token α)) : List (Token α × Token α) :=
  (step^[R.length] (R,S,[])).2.2

@[fun_prop] theorem poly_compute : Poly (fun q : List (Token α) × List (Token α) => compute q.1 q.2) := by
  unfold compute
  exact (BitCode.Poly.snd.comp BitCode.Poly.snd).comp
    (poly_iterate.comp (BitCode.Poly.length.comp BitCode.Poly.fst |>.pair
      (BitCode.Poly.fst.pair (BitCode.Poly.snd.pair (BitCode.Poly.const [])))))

omit [Coded α] in
theorem compute_correct (R S : List (Token α)) (hR : R.Nodup) (hS : S.Nodup)
    (hc : Capacity Prod.snd Prod.snd R.toFinset S.toFinset) (hl : R.length = S.length) :
    ((compute R S).map Prod.fst).Perm R ∧ ((compute R S).map Prod.snd).Perm S ∧
      ∀ p ∈ compute R S, p.1.2 <+: p.2.2 := by
  have h := iterate_correct R.length R S [] hR hS hc hl le_rfl (by simp)
  simpa only [compute, List.map_nil, List.append_nil] using h.2.2

end Superstring.PrefixGreedy

end OAI