File size: 18,903 Bytes
135052a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import OAI.Probability.SATVariance.BlockKilling

namespace OAI

noncomputable section

open MeasureTheory ProbabilityTheory

namespace RandomKSAT

open scoped Classical ENNReal

def favg.{u_1} {α : Type u_1} [Fintype α] (f : α → ℝ) : ℝ :=
  (∑ a, f a) / Fintype.card α

lemma favg_mono.{u_1} {α : Type u_1} [Fintype α] {f g : α → ℝ} (h : ∀ a, f a ≤ g a) :
    favg f ≤ favg g := by
  exact div_le_div_of_nonneg_right (Finset.sum_le_sum fun a _ => h a) (by positivity)

lemma favg_const.{u_1} {α : Type u_1} [Fintype α] [Nonempty α] (c : ℝ) :
    favg (fun _ : α => c) = c := by
  simp [favg, Fintype.card_ne_zero]

lemma favg_add.{u_1} {α : Type u_1} [Fintype α] (f g : α → ℝ) :
    favg (fun a => f a + g a) = favg f + favg g := by
  simp only [favg, Finset.sum_add_distrib, add_div]

lemma favg_sub.{u_1} {α : Type u_1} [Fintype α] (f g : α → ℝ) :
    favg (fun a => f a - g a) = favg f - favg g := by
  simp only [favg, Finset.sum_sub_distrib, sub_div]

lemma favg_equiv.{u_1, u_2} {α : Type u_1} {β : Type u_2} [Fintype α] [Fintype β] (e : α ≃ β) (f : β → ℝ) :
    favg (fun a => f (e a)) = favg f := by
  rw [favg, favg, e.sum_comp f, Fintype.card_congr e]

lemma favg_comm.{u_1, u_2} {α : Type u_1} {β : Type u_2} [Fintype α] [Fintype β] (f : α → β → ℝ) :
    favg (fun a => favg (f a)) = favg (fun b => favg (fun a => f a b)) := by
  simp only [favg, ← Finset.sum_div]
  rw [Finset.sum_comm]
  ring

lemma favg_prod.{u_1, u_2} {α : Type u_1} {β : Type u_2} [Fintype α] [Fintype β] (f : α × β → ℝ) :
    favg f = favg (fun a => favg fun b => f (a,b)) := by
  simp only [favg, Fintype.sum_prod_type, Fintype.card_prod, Nat.cast_mul,
    ← Finset.sum_div]
  ring

lemma uniformOn_real.{u_1} {α : Type u_1} [Fintype α] [MeasurableSpace α]
    [MeasurableSingletonClass α] (s : Set α) :
    (uniformOn Set.univ s).toReal = favg (fun a => if a ∈ s then 1 else 0) := by
  rw [uniformOn_univ, count_set_fintype]
  simp only [ENNReal.toReal_div, ENNReal.toReal_natCast, favg]
  congr 1
  simp [Fintype.card_subtype]

lemma prefixLaw {u s : ℕ} (hsu : s ≤ u) (m : ℕ) :
    (streamLaw u s).map (fun ω (i : Fin m) => ω i) =
      uniformOn (Set.univ : Set (Fin m → Clause u s)) := by
  let := clauseLaw_probability u s hsu
  rw [streamLaw, Measure.map_infinitePi_infinitePi_of_inj Fin.val_injective,
    Measure.infinitePi_eq_pi]
  simpa only [Set.pi_univ, clauseLaw] using
    (uniformOn_pi (f := fun _ : Fin m => (Set.univ : Set (Clause u s)))).symm

def residual.{u_1} {u s : ℕ} {ι : Type u_1} [Fintype ι]
    (S : Finset (Assignment u)) (cs : ι → Clause u s) : Finset (Assignment u) :=
  S.filter fun a => ∀ i, Satisfies (cs i) a

lemma residual_comm.{u_1, u_2} {u r s : ℕ} {ι : Type u_1} {κ : Type u_2} [Fintype ι] [Fintype κ]
    (S : Finset (Assignment u)) (cs : ι → Clause u r) (ds : κ → Clause u s) :
    residual (residual S cs) ds = residual (residual S ds) cs := by
  ext a
  simp only [residual, Finset.mem_filter]
  tauto

lemma residual_cons {u s m : ℕ} (S : Finset (Assignment u)) (c : Clause u s)
    (cs : Fin m → Clause u s) :
    residual S (Fin.cons c cs) = residual (S.filter (Satisfies c)) cs := by
  ext a
  simp [residual, Fin.forall_fin_succ, and_assoc]

def clauseStep (u s : ℕ) (f : Finset (Assignment u) → ℝ)
    (S : Finset (Assignment u)) : ℝ :=
  favg fun c : Clause u s => f (S.filter (Satisfies c))

def alive (u : ℕ) (S : Finset (Assignment u)) : ℝ := if S.Nonempty then 1 else 0

lemma clauseStep_mono (u s : ℕ) : Monotone (clauseStep u s) := by
  intro f g h S
  exact favg_mono fun c => h _

lemma clauseStep_const {u s : ℕ} (hsu : s ≤ u) (c : ℝ) :
    clauseStep u s (fun _ => c) = fun _ => c := by
  let := clause_nonempty u s hsu
  funext S
  exact favg_const c

lemma clauseStep_add (u s : ℕ) (f g : Finset (Assignment u) → ℝ) :
    clauseStep u s (f+g) = clauseStep u s f + clauseStep u s g := by
  funext S
  exact favg_add _ _

lemma clauseStep_comm (u r s : ℕ) : Function.Commute (clauseStep u r) (clauseStep u s) := by
  intro f
  funext S
  unfold clauseStep
  rw [favg_comm]
  congr 1
  funext d
  congr 1
  funext c
  congr 1
  ext a
  simp only [Finset.mem_filter]
  tauto

lemma clauseStep_iterate_avg (u s m : ℕ) (f : Finset (Assignment u) → ℝ)
    (S : Finset (Assignment u)) :
    (clauseStep u s)^[m] f S = favg (fun cs : Fin m → Clause u s => f (residual S cs)) := by
  induction m generalizing S with
  | zero => simp [residual, favg]
  | succ m ih =>
    rw [Function.iterate_succ_apply', clauseStep]
    simp_rw [ih]
    rw [← favg_prod (fun p : Clause u s × (Fin m → Clause u s) =>
      f (residual (S.filter (Satisfies p.1)) p.2)),
      ← favg_equiv (Fin.consEquiv (fun _ : Fin (m+1) => Clause u s))]
    congr 1
    funext p
    simp only [Fin.consEquiv, Equiv.coe_fn_mk, residual_cons]

lemma blockKill_iterate {u s : ℕ} (hsu : s ≤ u) (S : Finset (Assignment u)) (m : ℕ) :
    blockKill u s S m = 1 - (clauseStep u s)^[m] (alive u) S := by
  rw [blockKill_compl hsu, clauseStep_iterate_avg]
  congr 1
  have he : {ω : Stream u s | SetSAT S ω m} =
      (fun ω (i : Fin m) => ω i) ⁻¹' {cs | (residual S cs).Nonempty} := by
    ext ω
    simp only [Set.mem_ofPred_eq, Set.mem_preimage, SetSAT, residual,
      Finset.Nonempty, Finset.mem_filter]
    exact exists_congr fun a => and_congr_right fun _ => (Fin.forall_iff (p := fun i : Fin m => Satisfies (ω i) a)).symm
  rw [he, ← Measure.map_apply (by fun_prop) (Set.toFinite _).measurableSet, prefixLaw hsu,
    uniformOn_real]
  simp only [alive, Set.mem_ofPred_eq]
  congr 1
  funext cs
  split_ifs <;> rfl

lemma iterate_add_const.{u_1} {α : Type u_1} (A : (α → ℝ) → (α → ℝ))
    (hA : ∀ f c, A (f + fun _ => c) = A f + fun _ => c)
    (m : ℕ) (f : α → ℝ) (c : ℝ) :
    A^[m] (f + fun _ => c) = A^[m] f + fun _ => c := by
  induction m with
  | zero => rfl
  | succ m ih => rw [Function.iterate_succ_apply', ih, hA, Function.iterate_succ_apply']

lemma markov_telescoping.{u_1} {α : Type u_1} (A B : (α → ℝ) → (α → ℝ))
    (hA : Monotone A) (hB : Monotone B)
    (hAc : ∀ f c, A (f + fun _ => c) = A f + fun _ => c)
    (hBc : ∀ f c, B (f + fun _ => c) = B f + fun _ => c)
    (hcomm : Function.Commute A B) (f : α → ℝ) (δ : ℝ)
    (hone : A f ≤ B f + fun _ => δ) (d : ℕ) :
    A^[d] f ≤ B^[d] f + fun _ => (d : ℝ)*δ := by
  induction d with
  | zero => intro x; simp
  | succ d ih =>
    have h₁ := hA.iterate d hone
    rw [iterate_add_const A hAc, hcomm.iterate_left d f] at h₁
    have h₂ := hB ih
    rw [hBc, ← Function.iterate_succ_apply' B d f] at h₂
    intro x
    have h₁x := h₁ x
    have h₂x := h₂ x
    simp only [Pi.add_apply] at h₁x h₂x
    rw [Function.iterate_succ_apply]
    change A^[d] (A f) x ≤ B^[d+1] f x + ((d+1 : ℕ) : ℝ)*δ
    push_cast
    linarith

lemma clauseStep_add_const {u s : ℕ} (hsu : s ≤ u)
    (f : Finset (Assignment u) → ℝ) (c : ℝ) :
    clauseStep u s (f + fun _ => c) = clauseStep u s f + fun _ => c := by
  rw [clauseStep_add, clauseStep_const hsu]

lemma sequential_replacement {u r g : ℕ} (hr : 2 ≤ r) (hru : r+1 ≤ u) (hg : 1 ≤ g)
    (S : Finset (Assignment u)) (d : ℕ) :
    blockKill u r S d - (d : ℝ)*replacementK (r+1)/(g : ℝ)^r ≤
      blockKill u (r+1) S (d*g) := by
  have hru' : r ≤ u := by omega
  have hone : (clauseStep u (r+1))^[g] (alive u) ≤
      clauseStep u r (alive u) + fun _ => replacementK (r+1)/(g : ℝ)^r := by
    intro S'
    have h := one_replacement hr hru hg S'
    rw [blockKill_iterate hru', blockKill_iterate hru, Function.iterate_one] at h
    change _ ≤ clauseStep u r (alive u) S' + replacementK (r+1)/(g : ℝ)^r
    linarith
  have hh := markov_telescoping (clauseStep u (r+1))^[g] (clauseStep u r)
    ((clauseStep_mono u (r+1)).iterate g) (clauseStep_mono u r)
    (iterate_add_const _ (clauseStep_add_const hru) g)
    (clauseStep_add_const hru') ((clauseStep_comm u (r+1) r).iterate_left g)
    (alive u) (replacementK (r+1)/(g : ℝ)^r) hone d S
  rw [← Function.iterate_mul, Nat.mul_comm g d] at hh
  rw [blockKill_iterate hru', blockKill_iterate hru]
  change _ ≤ _
  simp only [Pi.add_apply, ← mul_div_assoc] at hh
  linarith

lemma favg_mul.{u_1} {α : Type u_1} [Fintype α] (c : ℝ) (f : α → ℝ) :
    favg (fun a => c*f a) = c*favg f := by
  simp only [favg, ← Finset.mul_sum]
  ring

lemma favg_nonneg.{u_1} {α : Type u_1} [Fintype α] {f : α → ℝ} (h : ∀ a, 0 ≤ f a) :
    0 ≤ favg f := by
  exact div_nonneg (Finset.sum_nonneg fun a _ => h a) (by positivity)

lemma residual_subset.{u_1} {u s : ℕ} {ι : Type u_1} [Fintype ι]
    (S : Finset (Assignment u)) (cs : ι → Clause u s) : residual S cs ⊆ S :=
  Finset.filter_subset _ _

lemma residual_mono.{u_1} {u s : ℕ} {ι : Type u_1} [Fintype ι]
    {S T : Finset (Assignment u)} (h : S ⊆ T) (cs : ι → Clause u s) :
    residual S cs ⊆ residual T cs := Finset.filter_subset_filter _ h

lemma alive_nonneg (u : ℕ) (S : Finset (Assignment u)) : 0 ≤ alive u S := by
  unfold alive
  split_ifs <;> norm_num

lemma alive_le_one (u : ℕ) (S : Finset (Assignment u)) : alive u S ≤ 1 := by
  unfold alive
  split_ifs <;> norm_num

lemma alive_mono {u : ℕ} {S T : Finset (Assignment u)} (h : S ⊆ T) :
    alive u S ≤ alive u T := by
  unfold alive
  by_cases hS : S.Nonempty
  · simp [hS, hS.mono h]
  · simp only [hS, ite_false]
    split_ifs <;> norm_num

def survival (u s : ℕ) (S : Finset (Assignment u)) (m : ℕ) : ℝ :=
  (clauseStep u s)^[m] (alive u) S

lemma survival_avg (u s : ℕ) (S : Finset (Assignment u)) (m : ℕ) :
    survival u s S m = favg fun cs : Fin m → Clause u s => alive u (residual S cs) :=
  clauseStep_iterate_avg _ _ _ _ _

lemma survival_zero (u s : ℕ) (S : Finset (Assignment u)) : survival u s S 0 = alive u S := rfl

lemma survival_empty (u s m : ℕ) : survival u s ∅ m = 0 := by
  simp [survival_avg, residual, alive, favg]

lemma survival_nonneg (u s : ℕ) (S : Finset (Assignment u)) (m : ℕ) :
    0 ≤ survival u s S m := by
  rw [survival_avg]
  exact favg_nonneg fun _ => alive_nonneg _ _

lemma survival_le_one {u s : ℕ} (hsu : s ≤ u) (S : Finset (Assignment u)) (m : ℕ) :
    survival u s S m ≤ 1 := by
  let := clause_nonempty u s hsu
  rw [survival_avg, ← favg_const (α := Fin m → Clause u s) (1 : ℝ)]
  exact favg_mono fun _ => alive_le_one _ _

lemma survival_mono {u s : ℕ} {S T : Finset (Assignment u)} (h : S ⊆ T) (m : ℕ) :
    survival u s S m ≤ survival u s T m := by
  rw [survival_avg, survival_avg]
  exact favg_mono fun _ => alive_mono (residual_mono h _)

lemma survival_eq_prob {u s : ℕ} (hsu : s ≤ u) (S : Finset (Assignment u)) (m : ℕ) :
    survival u s S m = (streamLaw u s {ω | SetSAT S ω m}).toReal := by
  have h := blockKill_iterate hsu S m
  rw [blockKill_compl hsu] at h
  change _ = 1 - survival u s S m at h
  linarith

lemma survival_summable {u s : ℕ} (hsu : s ≤ u) (S : Finset (Assignment u)) :
    Summable (survival u s S) := by
  have hr : 1-((2 : ℝ)^s)⁻¹ < 1 := by
    have h : 0 < ((2 : ℝ)^s)⁻¹ := by positivity
    linarith
  apply Summable.of_nonneg_of_le (survival_nonneg u s S) _
    ((summable_geometric_of_lt_one (real_rate_nonneg s) hr).mul_left (S.card : ℝ))
  intro m
  rw [survival_eq_prob hsu]
  have h := ENNReal.toReal_mono (by finiteness) (set_survival_le hsu S m)
  simpa only [ENNReal.toReal_mul, ENNReal.toReal_natCast, ENNReal.toReal_pow, rate_toReal] using h

lemma survival_add (u s : ℕ) (S : Finset (Assignment u)) (m j : ℕ) :
    survival u s S (m+j) =
      favg fun cs : Fin m → Clause u s => survival u s (residual S cs) j := by
  unfold survival
  rw [Function.iterate_add_apply, clauseStep_iterate_avg]

lemma survival_submultiplicative (u s : ℕ) (S : Finset (Assignment u)) (m j : ℕ) :
    survival u s S (m+j) ≤ survival u s S m * survival u s S j := by
  rw [survival_add]
  calc
    _ ≤ favg (fun cs : Fin m → Clause u s =>
        survival u s S j * alive u (residual S cs)) := by
      apply favg_mono
      intro cs
      by_cases he : (residual S cs).Nonempty
      · simpa [alive, he] using survival_mono (residual_subset S cs) j
      · have he' := Finset.not_nonempty_iff_eq_empty.mp he
        simp [he', alive, survival_empty]
    _ = _ := by rw [favg_mul, ← survival_avg]; ring

def lifetime (u s : ℕ) (S : Finset (Assignment u)) : ℝ := ∑' m, survival u s S m

lemma lifetime_nonneg (u s : ℕ) (S : Finset (Assignment u)) : 0 ≤ lifetime u s S :=
  tsum_nonneg (survival_nonneg u s S)

lemma lifetime_empty (u s : ℕ) : lifetime u s ∅ = 0 := by simp [lifetime, survival_empty]

lemma lifetime_block {u s : ℕ} (hsu : s ≤ u) (S : Finset (Assignment u)) (m : ℕ)
    (hR : 0 < blockKill u s S m) :
    lifetime u s S ≤ (m : ℝ) / blockKill u s S m := by
  have hsum := survival_summable hsu S
  have hsplit := hsum.sum_add_tsum_nat_add m
  have hhead : ∑ i ∈ Finset.range m, survival u s S i ≤ (m : ℝ) := by
    calc
      _ ≤ ∑ _i ∈ Finset.range m, (1 : ℝ) := Finset.sum_le_sum fun i _ => survival_le_one hsu S i
      _ = _ := by simp
  have htail : (∑' j, survival u s S (j+m)) ≤ survival u s S m * lifetime u s S := by
    rw [lifetime, ← tsum_mul_left]
    apply Summable.tsum_le_tsum _ (hsum.comp_injective (fun _ _ h => Nat.add_right_cancel h))
      (hsum.mul_left _)
    intro j
    change survival u s S (j+m) ≤ _
    rw [Nat.add_comm j m]
    exact survival_submultiplicative u s S m j
  apply (le_div_iff₀ hR).2
  rw [blockKill_iterate hsu]
  change lifetime u s S * (1 - survival u s S m) ≤ (m : ℝ)
  change _ + _ = lifetime u s S at hsplit
  nlinarith

def lifetimeBeta (k : ℕ) : ℝ := (k : ℝ) / ((k-1 : ℕ) : ℝ)

def lifetimeC (k : ℕ) : ℝ := 4 * (2*replacementK k)^(((k-1 : ℕ) : ℝ)⁻¹)

lemma replacementK_one_le {r : ℕ} (hr : 1 ≤ r) : 1 ≤ replacementK (r+1) := by
  rw [replacementK_succ]
  apply one_le_pow₀
  exact one_le_mul_of_one_le_of_one_le (one_le_pow₀ (by norm_num)) (by exact_mod_cast hr)

lemma lifetimeBeta_succ {r : ℕ} (hr : 0 < r) : lifetimeBeta (r+1) = 1+(r : ℝ)⁻¹ := by
  simp only [lifetimeBeta, Nat.add_sub_cancel, Nat.cast_add, Nat.cast_one]
  have hr₀ : (r : ℝ) ≠ 0 := by exact_mod_cast (Nat.ne_of_gt hr)
  field_simp

lemma lifetimeC_pos {r : ℕ} (hr : 1 ≤ r) : 0 < lifetimeC (r+1) := by
  have hK := replacementK_one_le hr
  unfold lifetimeC
  exact mul_pos (by norm_num) (Real.rpow_pos_of_pos (by linarith) _)

lemma replacement_block_choice {r d : ℕ} (hr : 1 ≤ r) (hd : 1 ≤ d)
    {q : ℝ} (hq : 0 < q) (hq₁ : q ≤ 1) :
    ∃ g : ℕ, 1 ≤ g ∧
      (d : ℝ)*replacementK (r+1)/(g : ℝ)^r ≤ q/2 ∧
      (g : ℝ) ≤ 2*(2*replacementK (r+1)*d/q)^((r : ℝ)⁻¹) := by
  let x : ℝ := (2*replacementK (r+1)*d/q)^((r : ℝ)⁻¹)
  have hK := replacementK_one_le hr
  have hd₀ : (1 : ℝ) ≤ d := by exact_mod_cast hd
  have hz : (1 : ℝ) ≤ 2*replacementK (r+1)*d/q := by
    apply (le_div_iff₀ hq).2
    nlinarith
  have hx : 1 ≤ x := Real.one_le_rpow hz (by positivity)
  have hg : 1 ≤ ⌈x⌉₊ := Nat.one_le_ceil_iff.mpr (by linarith)
  have hg₀ : (0 : ℝ) < ⌈x⌉₊ := by exact_mod_cast (by omega : 0 < ⌈x⌉₊)
  refine ⟨⌈x⌉₊, hg, ?_, ?_⟩
  · have hp : 2*replacementK (r+1)*d/q ≤ (⌈x⌉₊ : ℝ)^r := by
      calc
        _ = x^r := (Real.rpow_inv_natCast_pow (by positivity) (by omega : r ≠ 0)).symm
        _ ≤ _ := pow_le_pow_left₀ (by linarith : 0 ≤ x) (Nat.le_ceil x) r
    apply (div_le_iff₀ (pow_pos hg₀ r)).2
    have hh := (div_le_iff₀ hq).mp hp
    nlinarith
  · exact (Nat.ceil_lt_add_one (by linarith : 0 ≤ x)).le.trans (by linarith)

lemma lifetime_product {u r d : ℕ} (hr : 2 ≤ r) (hru : r+1 ≤ u) (hd : 1 ≤ d)
    (S : Finset (Assignment u)) :
    lifetime u (r+1) S * (blockKill u r S d)^(lifetimeBeta (r+1)) ≤
      lifetimeC (r+1) * (d : ℝ)^(lifetimeBeta (r+1)) := by
  let q := blockKill u r S d
  have hq₀ : 0 ≤ q := blockKill_nonneg _ _ _ _
  have hq₁ : q ≤ 1 := blockKill_le_one (by omega) _ _
  have hβ : 0 < lifetimeBeta (r+1) := by
    rw [lifetimeBeta_succ (by omega)]
    positivity
  have hK := replacementK_one_le (by omega : 1 ≤ r)
  have hC := lifetimeC_pos (by omega : 1 ≤ r)
  have hd₀ : (0 : ℝ) < d := by exact_mod_cast hd
  by_cases hq : q = 0
  · change lifetime u (r+1) S * q^_ ≤ _
    rw [hq, Real.zero_rpow (ne_of_gt hβ), mul_zero]
    exact mul_nonneg hC.le (Real.rpow_nonneg hd₀.le _)
  · have hq : 0 < q := lt_of_le_of_ne hq₀ (Ne.symm hq)
    obtain ⟨g, hg, herr, hgub⟩ := replacement_block_choice (by omega : 1 ≤ r) hd hq hq₁
    have hR : q/2 ≤ blockKill u (r+1) S (d*g) := by
      have hh := sequential_replacement hr hru hg S d
      change q - _ ≤ _ at hh
      linarith
    have hR₀ : 0 < blockKill u (r+1) S (d*g) := lt_of_lt_of_le (by linarith) hR
    have hτ : lifetime u (r+1) S ≤ 4 * (2*replacementK (r+1))^((r : ℝ)⁻¹) *
        ((d : ℝ)/q)^(1+(r : ℝ)⁻¹) := by
      calc
        _ ≤ (d*g : ℕ)/blockKill u (r+1) S (d*g) := lifetime_block hru S (d*g) hR₀
        _ ≤ (d*g : ℕ)/(q/2) := div_le_div_of_nonneg_left (by positivity) (by linarith) hR
        _ ≤ (d : ℝ)*(2*(2*replacementK (r+1)*d/q)^((r : ℝ)⁻¹))/(q/2) := by
          push_cast
          exact div_le_div_of_nonneg_right (mul_le_mul_of_nonneg_left hgub hd₀.le) (by positivity)
        _ = _ := by
          rw [show 2*replacementK (r+1)*(d : ℝ)/q =
            (2*replacementK (r+1))*((d : ℝ)/q) by ring,
            Real.mul_rpow (by linarith) (by positivity), Real.rpow_add (by positivity),
            Real.rpow_one]
          ring
    have hCeq : lifetimeC (r+1) = 4 * (2*replacementK (r+1))^((r : ℝ)⁻¹) := by
      simp [lifetimeC]
    rw [← lifetimeBeta_succ (by omega), ← hCeq] at hτ
    rw [Real.div_rpow hd₀.le hq.le] at hτ
    change lifetime u (r+1) S * q^_ ≤ _
    apply (le_div_iff₀ (Real.rpow_pos_of_pos hq _)).mp
    simpa only [mul_div_assoc] using hτ

lemma lifetime_level {u r d : ℕ} (hr : 2 ≤ r) (hru : r+1 ≤ u) (hd : 1 ≤ d)
    (S : Finset (Assignment u)) {a : ℝ} (ha : 0 < a) (haq : a ≤ blockKill u r S d) :
    lifetime u (r+1) S ≤ lifetimeC (r+1) * (d : ℝ)^(lifetimeBeta (r+1)) *
      a^(-lifetimeBeta (r+1)) := by
  have hβ : 0 ≤ lifetimeBeta (r+1) := by
    rw [lifetimeBeta_succ (by omega)]
    positivity
  have hh := lifetime_product hr hru hd S
  have hh' := mul_le_mul_of_nonneg_left (Real.rpow_le_rpow ha.le haq hβ)
    (lifetime_nonneg u (r+1) S)
  rw [Real.rpow_neg ha.le, ← div_eq_mul_inv]
  apply (le_div_iff₀ (Real.rpow_pos_of_pos ha _)).2
  exact hh'.trans hh

end RandomKSAT

end

end OAI