File size: 11,700 Bytes
f61e547
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import Mathlib

namespace OAI

noncomputable section
open Set Filter MeasureTheory ProbabilityTheory
open scoped Topology BigOperators ENNReal NNReal
namespace FKCRT.Inventory

/-- `none` is the flexible order; a typed order is `some type`. -/
abbrev Order := Option Bool
inductive Letter where
  | burger : Bool → Letter
  | order : Order → Letter
  deriving DecidableEq, Fintype

/-- Internal stacks are freshest-first. The source's surviving-burger list
is `Evaluation.burgers.reverse`, so source chronology is unchanged. -/
def eligible (a : Order) (b : Bool) : Prop := a=none ∨ a=some b

instance (a : Order) (b : Bool) : Decidable (eligible a b) :=
  inferInstanceAs (Decidable (a=none ∨ a=some b))

def pop (a : Order) : List Bool → Option (List Bool)
  | [] => none
  | b::bs => if eligible a b then some bs else (pop a bs).map (b::·)

lemma pop_append (a : Order) (s t : List Bool) :
    pop a (s++t) = match pop a s with
      | some s' => some (s'++t)
      | none => (pop a t).map (s++·) := by
  induction s with
  | nil => simp [pop]
  | cons b bs ih =>
    simp only [List.cons_append,pop]
    split_ifs with h
    · rfl
    · rw [ih]
      cases pop a bs <;> cases pop a t <;> rfl

lemma pop_eq_none_iff (a : Order) (s : List Bool) :
    pop a s=none ↔ ∀ b∈s,¬eligible a b := by
  induction s with
  | nil => simp [pop]
  | cons b bs ih =>
    by_cases h : eligible a b <;> simp [pop,h,ih]

lemma pop_length {a : Order} {s t : List Bool} (h : pop a s=some t) :
    t.length+1=s.length := by
  induction s generalizing t with
  | nil => simp [pop] at h
  | cons b bs ih =>
    simp only [pop] at h
    split_ifs at h with he
    · cases h; rfl
    · cases hp : pop a bs with
      | none => simp [hp] at h
      | some s' =>
        simp only [hp,Option.map_some,Option.some.injEq] at h
        subst t
        simp only [List.length_cons]
        have := ih hp
        omega

structure Evaluation where
  orders : List Order
  burgers : List Bool
  deriving DecidableEq

def step (e : Evaluation) : Letter → Evaluation
  | .burger b => ⟨e.orders,b::e.burgers⟩
  | .order a => match pop a e.burgers with
    | some bs => ⟨e.orders,bs⟩
    | none => ⟨e.orders++[a],e.burgers⟩

def run : List Letter → Evaluation → Evaluation
  | [],e => e
  | a::w,e => run w (step e a)

def reduce (w : List Letter) : Evaluation := run w ⟨[],[]⟩

def evalOrders (as : List Order) (s : List Bool) : Evaluation :=
  run (as.map Letter.order) ⟨[],s⟩

lemma run_append (w w' : List Letter) (e : Evaluation) :
    run (w++w') e=run w' (run w e) := by
  induction w generalizing e with
  | nil => rfl
  | cons a w ih => exact ih _

lemma run_orders_prefix (w : List Letter) (os : List Order) (e : Evaluation) :
    run w ⟨os++e.orders,e.burgers⟩ =
      ⟨os++(run w e).orders,(run w e).burgers⟩ := by
  induction w generalizing e with
  | nil => rfl
  | cons a w ih =>
    cases a with
    | burger b => simpa only [run,step] using ih ⟨e.orders,b::e.burgers⟩
    | order a =>
      simp only [run,step]
      cases pop a e.burgers with
      | none => simpa only [List.append_assoc] using ih ⟨e.orders++[a],e.burgers⟩
      | some bs => exact ih ⟨e.orders,bs⟩

lemma evalOrders_nil (s : List Bool) : evalOrders [] s=⟨[],s⟩ := rfl

lemma evalOrders_append (as as' : List Order) (s : List Bool) :
    evalOrders (as++as') s =
      ⟨(evalOrders as s).orders++(evalOrders as' (evalOrders as s).burgers).orders,
       (evalOrders as' (evalOrders as s).burgers).burgers⟩ := by
  unfold evalOrders
  rw [List.map_append,run_append]
  simpa only [List.append_nil] using
    run_orders_prefix (as'.map Letter.order) (run (as.map Letter.order) ⟨[],s⟩).orders
      (⟨[],(run (as.map Letter.order) ⟨[],s⟩).burgers⟩ : Evaluation)

/-- Exact older-supply reduction. Unfilled orders retain chronological order;
all surviving new burgers are fresher than all surviving old burgers. -/
theorem run_older_supply (w : List Letter) (s : List Bool) :
    run w ⟨[],s⟩ =
      ⟨(evalOrders (reduce w).orders s).orders,
        (reduce w).burgers++(evalOrders (reduce w).orders s).burgers⟩ := by
  induction w using List.reverseRecOn with
  | nil => simp [run,reduce,evalOrders]
  | append_singleton w a ih =>
    rw [run_append,ih]
    have hr : reduce (w++[a])=step (reduce w) a := by
      simp [reduce,run_append,run]
    rw [hr]
    cases a with
    | burger b => simp only [run,step,List.cons_append]
    | order a =>
      simp only [run,step]
      rw [pop_append]
      cases hp : pop a (reduce w).burgers with
      | some bs => rfl
      | none =>
        dsimp only
        rw [evalOrders_append]
        simp only [evalOrders,List.map_singleton,run,step]
        cases pop a (run (List.map Letter.order (reduce w).orders) ⟨[],s⟩).burgers <;>
          simp only [Option.map_none,Option.map_some,List.append_nil,List.nil_append]

lemma run_burgers (s : List Bool) (e : Evaluation) :
    run (s.map Letter.burger) e=⟨e.orders,s.reverse++e.burgers⟩ := by
  induction s generalizing e with
  | nil => simp [run]
  | cons b bs ih => simp [run,step,ih,List.reverse_cons,List.append_assoc]

def normalWord (e : Evaluation) : List Letter :=
  e.orders.map Letter.order++e.burgers.reverse.map Letter.burger

/-- A finite word may be replaced by its source reduction in any supply. -/
theorem run_normalWord (w : List Letter) (e : Evaluation) :
    run (normalWord (reduce w)) e=run w e := by
  obtain ⟨os,s⟩ := e
  have hz : run (normalWord (reduce w)) ⟨[],s⟩=run w ⟨[],s⟩ := by
    rw [run_older_supply w s]
    simp only [normalWord,run_append,run_burgers,List.reverse_reverse,evalOrders]
  have h₁ := run_orders_prefix (normalWord (reduce w)) os (⟨[],s⟩ : Evaluation)
  have h₂ := run_orders_prefix w os (⟨[],s⟩ : Evaluation)
  simp only [List.append_nil] at h₁ h₂
  rw [h₁,h₂,hz]

/-- Once a suffix has a surviving burger, its freshest survivor is unchanged
by prepending arbitrary further letters. -/
theorem freshest_survivor_prepend (w w' : List Letter) {b : Bool} {bs : List Bool}
    (h : (reduce w).burgers=b::bs) : (reduce (w'++w)).burgers.head?=some b := by
  unfold reduce
  rw [run_append]
  have hh := run_orders_prefix w (reduce w').orders (⟨[],(reduce w').burgers⟩ : Evaluation)
  simp only [List.append_nil] at hh
  rw [show run w' ⟨[],[]⟩=⟨(reduce w').orders,(reduce w').burgers⟩ from rfl,hh,run_older_supply,h]
  rfl

def Letter.increment : Letter → ℤ
  | .burger _ => 1
  | .order _ => -1

def imbalance (e : Evaluation) : ℤ := (e.burgers.length:ℤ)-e.orders.length

lemma step_imbalance (e : Evaluation) (a : Letter) :
    imbalance (step e a)=imbalance e+a.increment := by
  cases a with
  | burger b => simp [imbalance,step,Letter.increment]; ring
  | order a =>
    simp only [step,Letter.increment]
    cases hp : pop a e.burgers with
    | none => simp [imbalance]; ring
    | some bs =>
      have hlen := pop_length hp
      simp only [imbalance]
      omega

lemma run_imbalance (w : List Letter) (e : Evaluation) :
    imbalance (run w e)=(w.map Letter.increment).sum+imbalance e := by
  induction w generalizing e with
  | nil => simp [run]
  | cons a w ih =>
    simp only [run,ih,step_imbalance,List.map_cons,List.sum_cons]
    ring

lemma reduce_imbalance (w : List Letter) :
    imbalance (reduce w)=(w.map Letter.increment).sum := by
  rw [reduce,run_imbalance]
  simp [imbalance]

lemma survivor_of_sum_pos (w : List Letter) (h : 0<(w.map Letter.increment).sum) :
    (reduce w).burgers≠[] := by
  intro hn
  rw [← reduce_imbalance,imbalance,hn] at h
  simp only [List.length_nil,Nat.cast_zero,zero_sub] at h
  omega

lemma orders_length_le_step (e : Evaluation) (a : Letter) :
    e.orders.length≤(step e a).orders.length := by
  cases a with
  | burger b => rfl
  | order a => simp only [step]; split <;> simp

lemma orders_length_le_run (w : List Letter) (e : Evaluation) :
    e.orders.length≤(run w e).orders.length := by
  induction w generalizing e with
  | nil => rfl
  | cons a w ih => exact (orders_length_le_step e a).trans (ih (step e a))

def EmptyReducing (w : List Letter) : Prop := reduce w=⟨[],[]⟩

/-- Exact necessary Dyck-path event, without invoking a map encoding. -/
theorem emptyReducing_dyck (w : List Letter) (h : EmptyReducing w) :
    (w.map Letter.increment).sum=0 ∧
      ∀ j,0≤((w.take j).map Letter.increment).sum := by
  have he : reduce w=⟨[],[]⟩ := h
  constructor
  · rw [← reduce_imbalance,he,imbalance]; simp
  · intro j
    have hle := orders_length_le_run (w.drop j) (reduce (w.take j))
    have hr : run (w.drop j) (reduce (w.take j))=reduce w := by
      rw [reduce,← run_append,List.take_append_drop]
      rfl
    rw [hr,he] at hle
    have ho : (reduce (w.take j)).orders=[] := List.length_eq_zero_iff.mp (by simpa using hle)
    rw [← reduce_imbalance,imbalance,ho]
    simp

lemma evalOrders_cons (a : Order) (as : List Order) (s : List Bool) :
    evalOrders (a::as) s = match pop a s with
      | some s' => evalOrders as s'
      | none => ⟨a::(evalOrders as s).orders,(evalOrders as s).burgers⟩ := by
  unfold evalOrders
  simp only [List.map_cons,run,step]
  cases pop a s with
  | some s' => rfl
  | none =>
    simpa only [List.nil_append,List.append_nil,List.singleton_append] using
      run_orders_prefix (as.map Letter.order) [a] (⟨[],s⟩ : Evaluation)

lemma evalOrders_empty (as : List Order) : evalOrders as []=⟨as,[]⟩ := by
  induction as with
  | nil => rfl
  | cons a as ih => simp [evalOrders_cons,pop,ih]

lemma evalOrders_singleton (as : List Order) (b : Bool) :
    (evalOrders as [b]).burgers= if ∀ a∈as,¬eligible a b then [b] else [] := by
  induction as with
  | nil => simp [evalOrders,run]
  | cons a as ih =>
    rw [evalOrders_cons]
    by_cases h : eligible a b
    · simp [pop,h,evalOrders_empty]
    · simp [pop,h,ih]

lemma evalOrders_no_match (as : List Order) (s : List Bool)
    (h : ∀ a∈as,pop a s=none) : evalOrders as s=⟨as,s⟩ := by
  induction as with
  | nil => rfl
  | cons a as ih =>
    rw [evalOrders_cons,h a (by simp),ih (by intro a ha; exact h a (by simp [ha]))]

lemma prepend_order_burgers (a : Order) (w : List Letter) :
    (reduce (.order a::w)).burgers=(reduce w).burgers := by
  change (run w (step ⟨[],[]⟩ (.order a))).burgers=_
  simp only [step,pop,List.nil_append]
  have h := run_orders_prefix w [a] (⟨[],[]⟩ : Evaluation)
  simpa only [List.append_nil,reduce] using congrArg Evaluation.burgers h

/-- The first backwards survivor has exactly one burger and only fixed
orders of the opposite type, precisely as in source word:stopped-sum. -/
theorem first_survivor_shape (a : Letter) (w : List Letter)
    (h₀ : (reduce w).burgers=[]) (h₁ : (reduce (a::w)).burgers≠[]) :
    ∃ b, a=.burger b ∧ reduce (a::w)=⟨(reduce w).orders,[b]⟩ ∧
      ∀ o∈(reduce w).orders,o=some (!b) := by
  cases a with
  | order a => exact (h₁ (by rw [prepend_order_burgers,h₀])).elim
  | burger b =>
    have he : reduce (.burger b::w)=evalOrders (reduce w).orders [b] := by
      change run w ⟨[],[b]⟩=_
      rw [run_older_supply,h₀,List.nil_append]
    rw [he,evalOrders_singleton] at h₁
    have hn : ∀ o∈(reduce w).orders,¬eligible o b := by
      by_contra hn
      exact h₁ (ite_eq_right hn)
    refine ⟨b,rfl,?_,?_⟩
    · rw [he,evalOrders_no_match]
      intro o ho
      exact (pop_eq_none_iff o [b]).2 (by simpa using hn o ho)
    · intro o ho
      have hh := hn o ho
      cases o with
      | none => exact (hh (Or.inl rfl)).elim
      | some c => cases b <;> cases c <;> simp_all [eligible]

end FKCRT.Inventory
end

end OAI