File size: 28,396 Bytes
ea76aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import Mathlib

namespace OAI

/-!
Generalized-circuit smoothing, perturbation stability and rational meshes.

Gate satisfaction is conditional on a real fixed assignment, and mesh existence
approximates a given cube point. Applying Brouwer and combining these lemmas to
establish simultaneous totality remain unproved. `QuasilinearPCPStatement` is a
goal proposition: the uniform quasilinear reduction, soundness, deterministic
algorithm realization and complexity bounds also remain unproved.
-/

namespace PCPforPPAD

abbrev BitString := List Bool
abbrev UnitRat := {a : ℚ // 0 ≤ a ∧ a ≤ 1}

def encodeNat (n : ℕ) : BitString :=
  List.replicate n.bits.length true ++ [false] ++ n.bits

def encodeRat (q : ℚ) : BitString :=
  [decide (q.num < 0)] ++ encodeNat q.num.natAbs ++ encodeNat q.den

def encodeList {α : Type*} (f : α → BitString) (l : List α) : BitString :=
  encodeNat l.length ++ l.flatMap f

inductive BooleanGate (V : Type)
  | and : V → V → BooleanGate V
  | or : V → V → BooleanGate V
  | not : V → BooleanGate V
  deriving DecidableEq

def BooleanGate.eval {V : Type} (v : V → Bool) : BooleanGate V → Bool
  | .and a b => v a && v b
  | .or a b => v a || v b
  | .not a => !v a

def BooleanGate.encode {n : ℕ} : BooleanGate (Fin n) → BitString
  | .and a b => [false, false] ++ encodeNat a.val ++ encodeNat b.val
  | .or a b => [false, true] ++ encodeNat a.val ++ encodeNat b.val
  | .not a => [true, false] ++ encodeNat a.val

structure BooleanCircuit (n : ℕ) where
  gateCount : ℕ
  gate : (i : Fin gateCount) → BooleanGate (Fin (n + i.val))
  output : Fin n → Fin (n + gateCount)

def BooleanCircuit.wireValue {n : ℕ} (C : BooleanCircuit n) (x : Fin n → Bool)
    (k : ℕ) : Bool :=
  if hi : k < n then x ⟨k, hi⟩
  else if hg : k - n < C.gateCount then
    (C.gate ⟨k - n, hg⟩).eval (fun i => C.wireValue x i.val)
  else false
termination_by k
decreasing_by
  have hi := i.isLt
  simp only at hi
  omega

def BooleanCircuit.eval {n : ℕ} (C : BooleanCircuit n) (x : Fin n → Bool) :
    Fin n → Bool :=
  fun i => C.wireValue x (C.output i).val

def BooleanCircuit.encode {n : ℕ} (C : BooleanCircuit n) : BitString :=
  encodeNat n ++ encodeList encodeNat (List.range n) ++
    encodeList (fun i : Fin C.gateCount =>
      encodeNat (n + i.val) ++ (C.gate i).encode) (List.finRange C.gateCount) ++
    encodeList (fun i : Fin n => encodeNat (C.output i).val) (List.finRange n)

structure EndOfLine where
  n : ℕ
  n_pos : 0 < n
  S : BooleanCircuit n
  P : BooleanCircuit n
  pred_zero : P.eval (fun _ => false) = (fun _ => false)
  succ_zero : S.eval (fun _ => false) ≠ (fun _ => false)
  pred_succ_zero : P.eval (S.eval (fun _ => false)) = (fun _ => false)

def EndOfLine.IsSolution (I : EndOfLine) (v : Fin I.n → Bool) : Prop :=
  v ≠ (fun _ => false) ∧
    (I.P.eval (I.S.eval v) ≠ v ∨ I.S.eval (I.P.eval v) ≠ v)

def EndOfLine.encode (I : EndOfLine) : BitString := I.S.encode ++ I.P.encode

def EndOfLine.length (I : EndOfLine) : ℕ := I.encode.length

inductive Gate (V : Type)
  | constant : UnitRat → V → Gate V
  | scale : UnitRat → V → V → Gate V
  | copy : V → V → Gate V
  | add : V → V → V → Gate V
  | subtract : V → V → V → Gate V
  | less : V → V → V → Gate V
  | or : V → V → V → Gate V
  | and : V → V → V → Gate V
  | not : V → V → Gate V
  deriving DecidableEq

def Gate.output {V : Type} : Gate V → V
  | .constant _ z | .scale _ _ z | .copy _ z | .add _ _ z |
    .subtract _ _ z | .less _ _ z | .or _ _ z | .and _ _ z | .not _ z => z

def Gate.Satisfied {V : Type} (ε : ℝ) (x : V → ℝ) : Gate V → Prop
  | .constant α z => |x z - (α.val : ℝ)| ≤ ε
  | .scale α a z => |x z - (α.val : ℝ) * x a| ≤ ε
  | .copy a z => |x z - x a| ≤ ε
  | .add a b z => |x z - min (x a + x b) 1| ≤ ε
  | .subtract a b z => |x z - max (x a - x b) 0| ≤ ε
  | .less a b z =>
      (x a < x b - ε → 1 - ε ≤ x z) ∧ (x b + ε < x a → x z ≤ ε)
  | .or a b z =>
      (1 - ε ≤ x a ∨ 1 - ε ≤ x b → 1 - ε ≤ x z) ∧
        (x a ≤ ε ∧ x b ≤ ε → x z ≤ ε)
  | .and a b z =>
      (1 - ε ≤ x a ∧ 1 - ε ≤ x b → 1 - ε ≤ x z) ∧
        (x a ≤ ε ∨ x b ≤ ε → x z ≤ ε)
  | .not a z => (x a ≤ ε → 1 - ε ≤ x z) ∧ (1 - ε ≤ x a → x z ≤ ε)

structure GeneralizedCircuit where
  nodeCount : ℕ
  gateCount : ℕ
  gates_nonempty : 0 < gateCount
  gate : Fin gateCount → Gate (Fin nodeCount)
  unique_output : Function.Injective (fun i => (gate i).output)

def Gate.encode {n : ℕ} : Gate (Fin n) → BitString
  | .constant α z => encodeNat 0 ++ encodeNat z.val ++ encodeRat α.val
  | .scale α a z => encodeNat 1 ++ encodeNat z.val ++ encodeNat a.val ++ encodeRat α.val
  | .copy a z => encodeNat 2 ++ encodeNat z.val ++ encodeNat a.val
  | .add a b z => encodeNat 3 ++ encodeNat z.val ++ encodeNat a.val ++ encodeNat b.val
  | .subtract a b z => encodeNat 4 ++ encodeNat z.val ++ encodeNat a.val ++ encodeNat b.val
  | .less a b z => encodeNat 5 ++ encodeNat z.val ++ encodeNat a.val ++ encodeNat b.val
  | .or a b z => encodeNat 6 ++ encodeNat z.val ++ encodeNat a.val ++ encodeNat b.val
  | .and a b z => encodeNat 7 ++ encodeNat z.val ++ encodeNat a.val ++ encodeNat b.val
  | .not a z => encodeNat 8 ++ encodeNat z.val ++ encodeNat a.val

def GeneralizedCircuit.encode (G : GeneralizedCircuit) : BitString :=
  encodeList encodeNat (List.range G.nodeCount) ++
    encodeList Gate.encode (List.ofFn G.gate)

def GeneralizedCircuit.length (G : GeneralizedCircuit) : ℕ := G.encode.length

abbrev RationalAssignment (G : GeneralizedCircuit) := Fin G.nodeCount → UnitRat

def RationalAssignment.toReal {G : GeneralizedCircuit} (x : RationalAssignment G) :
    Fin G.nodeCount → ℝ := fun v => ((x v).val : ℝ)

def RationalAssignment.encode {G : GeneralizedCircuit} (x : RationalAssignment G) : BitString :=
  encodeList (fun r : UnitRat => encodeRat r.val) (List.ofFn x)

def RationalAssignment.length {G : GeneralizedCircuit} (x : RationalAssignment G) : ℕ :=
  x.encode.length

noncomputable def failedGates (ε : ℚ) (G : GeneralizedCircuit) (x : RationalAssignment G) :
    Finset (Fin G.gateCount) := by
  classical
  exact Finset.univ.filter (fun i => ¬ (G.gate i).Satisfied (ε : ℝ) x.toReal)

def Acceptable (ε δ : ℚ) (p : Polynomial ℕ) (G : GeneralizedCircuit)
    (x : RationalAssignment G) : Prop :=
  x.length ≤ p.eval G.length ∧
    ((failedGates ε G x).card : ℚ) ≤ δ * (G.gateCount : ℚ)

structure Algorithm where
  states : ℕ
  transition : Turing.TM0.Machine (Option Bool) (Fin (states + 1))

abbrev Algorithm.Config (A : Algorithm) :=
  Turing.TM0.Cfg (Option Bool) (Fin (A.states + 1))

def Algorithm.runSteps (A : Algorithm) : ℕ → A.Config → Option A.Config
  | 0, c => some c
  | t + 1, c => (Turing.TM0.step A.transition c).bind (A.runSteps t)

def Algorithm.RunsWithin (A : Algorithm) (input output : BitString) (bound : ℕ) : Prop :=
  ∃ t ≤ bound, ∃ c : A.Config,
    A.runSteps t (Turing.TM0.init (input.map some)) = some c ∧
    Turing.TM0.step A.transition c = none ∧
    c.Tape.right₀ = Turing.ListBlank.mk (output.map some)

def logFactor (N : ℕ) : ℕ := Nat.clog 2 (N + 2)

def QuasilinearPCPStatement : Prop :=
  ∃ (ε δ : ℚ) (A b : ℕ) (p timeR timeD : Polynomial ℕ) (R D : Algorithm),
    (0 < ε ∧ ε < 1 / 10) ∧ (0 < δ ∧ δ < 1) ∧ 0 < A ∧
    (∀ G : GeneralizedCircuit, ∃ x : RationalAssignment G, Acceptable ε δ p G x) ∧
    (∀ I : EndOfLine, ∃ G : GeneralizedCircuit,
      G.length ≤ A * I.length * (logFactor I.length) ^ b ∧
      R.RunsWithin I.encode G.encode (timeR.eval I.length) ∧
      ∀ x : RationalAssignment G, Acceptable ε δ p G x →
        ∃ v : Fin I.n → Bool, I.IsSolution v ∧
          D.RunsWithin (I.encode ++ x.encode) (List.ofFn v)
            (timeD.eval (I.length + x.length)))

end PCPforPPAD

namespace PCPforPPAD

@[simp] theorem encodeNat_length (n : ℕ) :
    (encodeNat n).length = 2 * n.bits.length + 1 := by
  simp [encodeNat]
  omega

theorem encodeNat_length_pos (n : ℕ) : 0 < (encodeNat n).length := by
  rw [encodeNat_length]
  omega

theorem BooleanCircuit.encode_length_pos {n : ℕ} (C : BooleanCircuit n) :
    0 < C.encode.length := by
  have h := encodeNat_length_pos n
  simp only [BooleanCircuit.encode, List.length_append]
  omega

theorem EndOfLine.length_ge_two (I : EndOfLine) : 2 ≤ I.length := by
  have hS := I.S.encode_length_pos
  have hP := I.P.encode_length_pos
  simp only [EndOfLine.length, EndOfLine.encode, List.length_append]
  omega

theorem exists_other_endpoint {α : Type*} [Finite α] (S P : α → α) (z : α)
    (hP : P z = z) (hS : S z ≠ z) (hPS : P (S z) = z) :
    ∃ v, v ≠ z ∧ P (S v) ≠ v := by
  classical
  by_contra h
  have hinv : Function.LeftInverse P S := by
    intro v
    by_cases hv : v = z
    · simpa [hv] using hPS
    · by_contra hne
      exact h ⟨v, hv, hne⟩
  obtain ⟨v, hv⟩ := (Finite.surjective_of_injective hinv.injective) z
  have hvz : v = z := by
    calc
      v = P (S v) := (hinv v).symm
      _ = z := by rw [hv, hP]
  exact hS (hvz ▸ hv)

theorem EndOfLine.exists_solution (I : EndOfLine) : ∃ v, I.IsSolution v := by
  obtain ⟨v, hv, hps⟩ := exists_other_endpoint I.S.eval I.P.eval (fun _ => false)
    I.pred_zero I.succ_zero I.pred_succ_zero
  exact ⟨v, hv, Or.inl hps⟩

theorem acceptable_iff_floor_budget (ε δ : ℚ) (hδ : 0 ≤ δ) (p : Polynomial ℕ)
    (G : GeneralizedCircuit) (x : RationalAssignment G) :
    Acceptable ε δ p G x ↔
      x.length ≤ p.eval G.length ∧ (failedGates ε G x).card ≤ ⌊δ * G.gateCount⌋₊ := by
  rw [Nat.le_floor_iff (mul_nonneg hδ (Nat.cast_nonneg _))]
  rfl

def clip01 (t : ℝ) : ℝ := max 0 (min t 1)

lemma clip01_nonneg (t : ℝ) : 0 ≤ clip01 t := le_max_left _ _

lemma clip01_le_one (t : ℝ) : clip01 t ≤ 1 := by
  exact max_le (by norm_num) (min_le_right _ _)

lemma clip01_eq_zero {t : ℝ} (h : t ≤ 0) : clip01 t = 0 := by
  exact max_eq_left ((min_le_left _ _).trans h)

lemma clip01_eq_one {t : ℝ} (h : 1 ≤ t) : clip01 t = 1 := by
  simp [clip01, min_eq_right h]

noncomputable def booleanSignal (ε t : ℝ) : ℝ := clip01 ((t - 2 * ε) / (1 - 4 * ε))

lemma booleanSignal_low {ε t : ℝ} (hε : ε < 1 / 10) (ht : t ≤ 2 * ε) :
    booleanSignal ε t = 0 := by
  apply clip01_eq_zero
  apply div_nonpos_of_nonpos_of_nonneg <;> linarith

lemma booleanSignal_high {ε t : ℝ} (hε : ε < 1 / 10) (ht : 1 - 2 * ε ≤ t) :
    booleanSignal ε t = 1 := by
  apply clip01_eq_one
  rw [le_div_iff₀ (show 0 < 1 - 4 * ε by linarith)]
  linarith

lemma booleanSignal_nonneg (ε t : ℝ) : 0 ≤ booleanSignal ε t := clip01_nonneg _

lemma booleanSignal_le_one (ε t : ℝ) : booleanSignal ε t ≤ 1 := clip01_le_one _

noncomputable def lessSignal (ε a b : ℝ) : ℝ := clip01 ((b - a) / ε + 1 / 2)

lemma lessSignal_low {ε a b : ℝ} (hε : 0 < ε) (hgap : b - a ≤ -ε / 2) :
    lessSignal ε a b = 0 := by
  apply clip01_eq_zero
  have h : (b - a) / ε ≤ -(1 / 2 : ℝ) := by
    rw [div_le_iff₀ hε]
    linarith
  linarith

lemma lessSignal_high {ε a b : ℝ} (hε : 0 < ε) (hgap : ε / 2 ≤ b - a) :
    lessSignal ε a b = 1 := by
  apply clip01_eq_one
  have h : (1 / 2 : ℝ) ≤ (b - a) / ε := by
    rw [le_div_iff₀ hε]
    linarith
  linarith

noncomputable def Gate.smoothedValue {V : Type} (ε : ℝ) (x : V → ℝ) : Gate V → ℝ
  | .constant α _ => (α.val : ℝ)
  | .scale α a _ => (α.val : ℝ) * x a
  | .copy a _ => x a
  | .add a b _ => min (x a + x b) 1
  | .subtract a b _ => max (x a - x b) 0
  | .less a b _ => lessSignal ε (x a) (x b)
  | .or a b _ => max (booleanSignal ε (x a)) (booleanSignal ε (x b))
  | .and a b _ => min (booleanSignal ε (x a)) (booleanSignal ε (x b))
  | .not a _ => 1 - booleanSignal ε (x a)

lemma low_input_before_perturbation {ε a a' : ℝ} (hε : 0 < ε)
    (hclose : |a' - a| ≤ ε / 40) (ha' : a' ≤ ε) : a ≤ 2 * ε := by
  have := (abs_le.mp hclose).1
  linarith

lemma high_input_before_perturbation {ε a a' : ℝ} (hε : 0 < ε)
    (hclose : |a' - a| ≤ ε / 40) (ha' : 1 - ε ≤ a') : 1 - 2 * ε ≤ a := by
  have := (abs_le.mp hclose).2
  linarith

lemma low_output_after_perturbation {ε z z' : ℝ} (hε : 0 < ε)
    (hclose : |z' - z| ≤ ε / 40) (hz : z = 0) : z' ≤ ε := by
  have := (abs_le.mp hclose).2
  linarith

lemma high_output_after_perturbation {ε z z' : ℝ} (hε : 0 < ε)
    (hclose : |z' - z| ≤ ε / 40) (hz : z = 1) : 1 - ε ≤ z' := by
  have := (abs_le.mp hclose).1
  linarith

lemma abs_sum_perturbation {a b a' b' η : ℝ}
    (ha : |a' - a| ≤ η) (hb : |b' - b| ≤ η) :
    |(a + b) - (a' + b')| ≤ η + η := by
  calc
    |(a + b) - (a' + b')| = |(a - a') + (b - b')| := by ring_nf
    _ ≤ |a - a'| + |b - b'| := abs_add_le _ _
    _ ≤ η + η := add_le_add (by simpa [abs_sub_comm] using ha)
      (by simpa [abs_sub_comm] using hb)

lemma abs_difference_perturbation {a b a' b' η : ℝ}
    (ha : |a' - a| ≤ η) (hb : |b' - b| ≤ η) :
    |(a - b) - (a' - b')| ≤ η + η := by
  calc
    |(a - b) - (a' - b')| = |(a - a') - (b - b')| := by ring_nf
    _ ≤ |a - a'| + |b - b'| := abs_sub _ _
    _ ≤ η + η := add_le_add (by simpa [abs_sub_comm] using ha)
      (by simpa [abs_sub_comm] using hb)

theorem Gate.satisfied_of_perturbation {V : Type} (g : Gate V) {ε : ℝ}
    (hε : 0 < ε) (hε' : ε < 1 / 10) (x y : V → ℝ)
    (hclose : ∀ v, |y v - x v| ≤ ε / 40)
    (hfixed : x g.output = g.smoothedValue ε x) : g.Satisfied ε y := by
  cases g with
  | constant α z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    change |y z - (α.val : ℝ)| ≤ ε
    calc
      _ = |y z - x z| := by rw [hfixed]
      _ ≤ ε / 40 := hclose z
      _ ≤ ε := by linarith
  | scale α a z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    change |y z - (α.val : ℝ) * y a| ≤ ε
    have hα0 : (0 : ℝ) ≤ (α.val : ℝ) := by exact_mod_cast α.property.1
    have hα1 : (α.val : ℝ) ≤ (1 : ℝ) := by exact_mod_cast α.property.2
    have hop : |(α.val : ℝ) * x a - (α.val : ℝ) * y a| ≤ ε / 40 := by
      rw [← mul_sub, abs_mul, abs_of_nonneg hα0, abs_sub_comm]
      calc
        _ ≤ 1 * (ε / 40) :=
          mul_le_mul hα1 (hclose a) (abs_nonneg _) (by norm_num)
        _ = ε / 40 := one_mul _
    calc
      _ ≤ |y z - (α.val : ℝ) * x a| +
          |(α.val : ℝ) * x a - (α.val : ℝ) * y a| := abs_sub_le _ _ _
      _ ≤ ε / 40 + ε / 40 := add_le_add (by rw [← hfixed]; exact hclose z) hop
      _ ≤ ε := by linarith
  | copy a z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    change |y z - y a| ≤ ε
    calc
      _ ≤ |y z - x a| + |x a - y a| := abs_sub_le _ _ _
      _ ≤ ε / 40 + ε / 40 := add_le_add
        (by rw [← hfixed]; exact hclose z) (by simpa [abs_sub_comm] using hclose a)
      _ ≤ ε := by linarith
  | add a b z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    change |y z - min (y a + y b) 1| ≤ ε
    have hop : |min (x a + x b) 1 - min (y a + y b) 1| ≤ ε / 40 + ε / 40 := by
      apply (abs_min_sub_min_le_max (x a + x b) 1 (y a + y b) 1).trans
      exact max_le (abs_sum_perturbation (hclose a) (hclose b)) (by simp; linarith)
    calc
      _ ≤ |y z - min (x a + x b) 1| +
          |min (x a + x b) 1 - min (y a + y b) 1| := abs_sub_le _ _ _
      _ ≤ ε / 40 + (ε / 40 + ε / 40) := add_le_add
        (by rw [← hfixed]; exact hclose z) hop
      _ ≤ ε := by linarith
  | subtract a b z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    change |y z - max (y a - y b) 0| ≤ ε
    have hop : |max (x a - x b) 0 - max (y a - y b) 0| ≤ ε / 40 + ε / 40 := by
      exact (abs_max_sub_max_le_abs _ _ _).trans
        (abs_difference_perturbation (hclose a) (hclose b))
    calc
      _ ≤ |y z - max (x a - x b) 0| +
          |max (x a - x b) 0 - max (y a - y b) 0| := abs_sub_le _ _ _
      _ ≤ ε / 40 + (ε / 40 + ε / 40) := add_le_add
        (by rw [← hfixed]; exact hclose z) hop
      _ ≤ ε := by linarith
  | less a b z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    have ha := abs_le.mp (hclose a)
    have hb := abs_le.mp (hclose b)
    constructor
    · intro h
      apply high_output_after_perturbation hε (hclose z)
      rw [hfixed]
      apply lessSignal_high hε
      linarith
    · intro h
      apply low_output_after_perturbation hε (hclose z)
      rw [hfixed]
      apply lessSignal_low hε
      linarith
  | or a b z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    constructor
    · intro h
      apply high_output_after_perturbation hε (hclose z)
      rw [hfixed]
      rcases h with h | h
      · rw [booleanSignal_high hε' (high_input_before_perturbation hε (hclose a) h)]
        exact max_eq_left (booleanSignal_le_one _ _)
      · rw [booleanSignal_high hε' (high_input_before_perturbation hε (hclose b) h)]
        exact max_eq_right (booleanSignal_le_one _ _)
    · rintro ⟨ha, hb⟩
      apply low_output_after_perturbation hε (hclose z)
      rw [hfixed,
        booleanSignal_low hε' (low_input_before_perturbation hε (hclose a) ha),
        booleanSignal_low hε' (low_input_before_perturbation hε (hclose b) hb), max_self]
  | and a b z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    constructor
    · rintro ⟨ha, hb⟩
      apply high_output_after_perturbation hε (hclose z)
      rw [hfixed,
        booleanSignal_high hε' (high_input_before_perturbation hε (hclose a) ha),
        booleanSignal_high hε' (high_input_before_perturbation hε (hclose b) hb), min_self]
    · intro h
      apply low_output_after_perturbation hε (hclose z)
      rw [hfixed]
      rcases h with h | h
      · rw [booleanSignal_low hε' (low_input_before_perturbation hε (hclose a) h)]
        exact min_eq_left (booleanSignal_nonneg _ _)
      · rw [booleanSignal_low hε' (low_input_before_perturbation hε (hclose b) h)]
        exact min_eq_right (booleanSignal_nonneg _ _)
  | not a z =>
    dsimp [Gate.output, Gate.smoothedValue] at hfixed
    constructor
    · intro h
      apply high_output_after_perturbation hε (hclose z)
      rw [hfixed, booleanSignal_low hε' (low_input_before_perturbation hε (hclose a) h)]
      norm_num
    · intro h
      apply low_output_after_perturbation hε (hclose z)
      rw [hfixed, booleanSignal_high hε' (high_input_before_perturbation hε (hclose a) h)]
      norm_num

theorem all_gates_satisfied_of_perturbation (G : GeneralizedCircuit) {ε : ℝ}
    (hε : 0 < ε) (hε' : ε < 1 / 10) (x y : Fin G.nodeCount → ℝ)
    (hclose : ∀ v, |y v - x v| ≤ ε / 40)
    (hfixed : ∀ i, x (G.gate i).output = (G.gate i).smoothedValue ε x) :
    ∀ i, (G.gate i).Satisfied ε y := by
  intro i
  exact (G.gate i).satisfied_of_perturbation hε hε' x y hclose (hfixed i)

theorem Gate.continuous_smoothedValue {V : Type} (g : Gate V) (ε : ℝ) :
    Continuous (fun x : V → ℝ => g.smoothedValue ε x) := by
  cases g <;> dsimp [Gate.smoothedValue, booleanSignal, lessSignal, clip01] <;> fun_prop

theorem Gate.smoothedValue_in_unit {V : Type} (g : Gate V) (ε : ℝ) (x : V → ℝ)
    (hx : ∀ v, 0 ≤ x v ∧ x v ≤ 1) :
    0 ≤ g.smoothedValue ε x ∧ g.smoothedValue ε x ≤ 1 := by
  cases g with
  | constant α z =>
    change (0 : ℝ) ≤ (α.val : ℝ) ∧ (α.val : ℝ) ≤ 1
    exact ⟨by exact_mod_cast α.property.1, by exact_mod_cast α.property.2⟩
  | scale α a z =>
    have hα : (0 : ℝ) ≤ (α.val : ℝ) ∧ (α.val : ℝ) ≤ 1 :=
      ⟨by exact_mod_cast α.property.1, by exact_mod_cast α.property.2⟩
    exact ⟨mul_nonneg hα.1 (hx a).1,
      (mul_le_mul hα.2 (hx a).2 (hx a).1 (by norm_num)).trans (by norm_num)⟩
  | copy a z => exact hx a
  | add a b z =>
    exact ⟨le_min (add_nonneg (hx a).1 (hx b).1) (by norm_num), min_le_right _ _⟩
  | subtract a b z =>
    exact ⟨le_max_right _ _, max_le (by have := hx a; have := hx b; linarith)
      (by norm_num)⟩
  | less a b z => exact ⟨clip01_nonneg _, clip01_le_one _⟩
  | or a b z =>
    exact ⟨(booleanSignal_nonneg ε (x a)).trans (le_max_left _ _),
      max_le (booleanSignal_le_one _ _) (booleanSignal_le_one _ _)⟩
  | and a b z =>
    exact ⟨le_min (booleanSignal_nonneg _ _) (booleanSignal_nonneg _ _),
      (min_le_left _ _).trans (booleanSignal_le_one _ _)⟩
  | not a z =>
    have := booleanSignal_nonneg ε (x a)
    have := booleanSignal_le_one ε (x a)
    dsimp [Gate.smoothedValue]
    constructor <;> linarith

noncomputable def GeneralizedCircuit.cubeMap (G : GeneralizedCircuit) (ε : ℝ)
    (x : Fin G.nodeCount → ℝ) (v : Fin G.nodeCount) : ℝ := by
  classical
  exact if h : ∃ i, (G.gate i).output = v then
    (G.gate h.choose).smoothedValue ε x else 0

theorem GeneralizedCircuit.cubeMap_at_output (G : GeneralizedCircuit) (ε : ℝ)
    (x : Fin G.nodeCount → ℝ) (i : Fin G.gateCount) :
    G.cubeMap ε x (G.gate i).output = (G.gate i).smoothedValue ε x := by
  classical
  have h : ∃ j, (G.gate j).output = (G.gate i).output := ⟨i, rfl⟩
  simp only [GeneralizedCircuit.cubeMap, dite_eq_left h]
  rw [G.unique_output h.choose_spec]

theorem GeneralizedCircuit.continuous_cubeMap (G : GeneralizedCircuit) (ε : ℝ) :
    Continuous (G.cubeMap ε) := by
  classical
  apply continuous_pi
  intro v
  by_cases h : ∃ i, (G.gate i).output = v
  · simpa only [GeneralizedCircuit.cubeMap, dite_eq_left h] using
      (G.gate h.choose).continuous_smoothedValue ε
  · simpa only [GeneralizedCircuit.cubeMap, dite_eq_right h] using
      (continuous_const : Continuous (fun _ : Fin G.nodeCount → ℝ => (0 : ℝ)))

theorem GeneralizedCircuit.cubeMap_in_unit (G : GeneralizedCircuit) (ε : ℝ)
    (x : Fin G.nodeCount → ℝ) (hx : ∀ v, 0 ≤ x v ∧ x v ≤ 1) :
    ∀ v, 0 ≤ G.cubeMap ε x v ∧ G.cubeMap ε x v ≤ 1 := by
  classical
  intro v
  by_cases h : ∃ i, (G.gate i).output = v
  · simpa only [GeneralizedCircuit.cubeMap, dite_eq_left h] using
      (G.gate h.choose).smoothedValue_in_unit ε x hx
  · simp [GeneralizedCircuit.cubeMap, h]

theorem encodeNat_length_le (n : ℕ) : (encodeNat n).length ≤ 2 * n + 1 := by
  have hn : n.bits.length ≤ n := by
    rw [Nat.size_eq_bits_len]
    exact Nat.size_le.mpr Nat.lt_two_pow_self
  rw [encodeNat_length]
  omega

theorem encodeList_length_le {α : Type*} (f : α → BitString) (l : List α) (K : ℕ)
    (h : ∀ a ∈ l, (f a).length ≤ K) :
    (encodeList f l).length ≤ (K + 2) * l.length + 1 := by
  have hflat : (l.flatMap f).length ≤ K * l.length := by
    induction l with
    | nil => simp
    | cons a l ih =>
      have ha : (f a).length ≤ K := h a (by simp)
      have hl := ih (fun b hb => h b (by simp [hb]))
      simp only [List.flatMap_cons, List.length_append, List.length_cons]
      nlinarith
  have hhead := encodeNat_length_le l.length
  simp only [encodeList, List.length_append]
  nlinarith

theorem encodeList_length_ge {α : Type*} (f : α → BitString) (l : List α)
    (h : ∀ a ∈ l, 0 < (f a).length) : l.length ≤ (encodeList f l).length := by
  have hflat : l.length ≤ (l.flatMap f).length := by
    induction l with
    | nil => simp
    | cons a l ih =>
      have ha : 0 < (f a).length := h a (by simp)
      have hl := ih (fun b hb => h b (by simp [hb]))
      simp only [List.flatMap_cons, List.length_append, List.length_cons]
      omega
  simp only [encodeList, List.length_append]
  omega

theorem GeneralizedCircuit.nodeCount_le_length (G : GeneralizedCircuit) :
    G.nodeCount ≤ G.length := by
  have h := encodeList_length_ge encodeNat (List.range G.nodeCount)
    (fun n _ => encodeNat_length_pos n)
  simp only [List.length_range] at h
  simp only [GeneralizedCircuit.length, GeneralizedCircuit.encode, List.length_append]
  omega

def meshEntryBound (q : ℕ) : ℕ :=
  (Finset.range (q + 1)).sup (fun k => (encodeRat ((k : ℚ) / (q : ℚ))).length)

noncomputable def meshWitnessPolynomial (q : ℕ) : Polynomial ℕ :=
  Polynomial.C (meshEntryBound q + 2) * Polynomial.X + 1

theorem mesh_entry_length_le (q k : ℕ) (hk : k ≤ q) :
    (encodeRat ((k : ℚ) / (q : ℚ))).length ≤ meshEntryBound q := by
  exact Finset.le_sup (s := Finset.range (q + 1)) (b := k)
    (f := fun k => (encodeRat ((k : ℚ) / (q : ℚ))).length)
    (Finset.mem_range.mpr (by omega))

theorem RationalAssignment.mesh_length_le (G : GeneralizedCircuit) (q : ℕ)
    (x : RationalAssignment G)
    (hx : ∀ v, ∃ k : ℕ, k ≤ q ∧ (x v).val = (k : ℚ) / (q : ℚ)) :
    x.length ≤ (meshWitnessPolynomial q).eval G.length := by
  have hclen := encodeList_length_le (fun r : UnitRat => encodeRat r.val)
    (List.ofFn x) (meshEntryBound q) (by
      intro r hr
      obtain ⟨v, rfl⟩ := List.mem_ofFn.mp hr
      obtain ⟨k, hk, hval⟩ := hx v
      simpa only [hval] using mesh_entry_length_le q k hk)
  change (encodeList (fun r : UnitRat => encodeRat r.val) (List.ofFn x)).length ≤ _
  simp only [List.length_ofFn] at hclen
  simp only [meshWitnessPolynomial, Polynomial.eval_add, Polynomial.eval_mul,
    Polynomial.eval_C, Polynomial.eval_X, Polynomial.eval_one]
  exact hclen.trans (Nat.add_le_add_right
    (Nat.mul_le_mul_left _ G.nodeCount_le_length) 1)

theorem exists_mesh_numerator (q : ℕ) (hq : 0 < q) (t : ℝ)
    (ht : 0 ≤ t ∧ t ≤ 1) :
    ∃ k : ℕ, k ≤ q ∧ |(k : ℝ) / (q : ℝ) - t| ≤ 1 / (2 * (q : ℝ)) := by
  let m : ℤ := round ((q : ℝ) * t)
  have hqR : 0 < (q : ℝ) := by exact_mod_cast hq
  have hm0 : 0 ≤ m := by
    have h : round (0 : ℝ) ≤ round ((q : ℝ) * t) := by
      rw [round_eq, round_eq]
      apply Int.floor_mono
      nlinarith
    simpa only [round_zero] using h
  have hmq : m ≤ (q : ℤ) := by
    have h : round ((q : ℝ) * t) ≤ round (q : ℝ) := by
      rw [round_eq, round_eq]
      apply Int.floor_mono
      nlinarith
    simpa only [round_natCast] using h
  refine ⟨m.toNat, Int.toNat_le.mpr hmq, ?_⟩
  have hmR : (m.toNat : ℝ) = (m : ℝ) := by
    exact_mod_cast Int.toNat_of_nonneg hm0
  have hd : (m.toNat : ℝ) / (q : ℝ) - t =
      ((m : ℝ) - (q : ℝ) * t) / (q : ℝ) := by
    rw [hmR]
    field_simp
  rw [hd, abs_div, abs_of_pos hqR, abs_sub_comm]
  calc
    _ ≤ (1 / 2) / (q : ℝ) := div_le_div_of_nonneg_right
      (abs_sub_round ((q : ℝ) * t)) hqR.le
    _ = 1 / (2 * (q : ℝ)) := by ring

def meshDenominator (ε : ℚ) : ℕ := ⌈20 / ε⌉₊

theorem meshDenominator_pos {ε : ℚ} (hε : 0 < ε) : 0 < meshDenominator ε := by
  exact Nat.one_le_ceil_iff.mpr (div_pos (by norm_num) hε)

theorem mesh_error_le {ε : ℚ} (hε : 0 < ε) :
    1 / (2 * (meshDenominator ε : ℝ)) ≤ (ε : ℝ) / 40 := by
  have hεR : (0 : ℝ) < (ε : ℝ) := by exact_mod_cast hε
  have hqR : (0 : ℝ) < (meshDenominator ε : ℝ) := by
    exact_mod_cast meshDenominator_pos hε
  have hceil : (20 : ℝ) / (ε : ℝ) ≤ (meshDenominator ε : ℝ) := by
    exact_mod_cast (Nat.le_ceil (20 / ε))
  have hprod := (div_le_iff₀ hεR).mp hceil
  rw [div_le_iff₀ (mul_pos (by norm_num : (0 : ℝ) < 2) hqR)]
  nlinarith

theorem exists_mesh_assignment (G : GeneralizedCircuit) (q : ℕ) (hq : 0 < q)
    (x : Fin G.nodeCount → ℝ) (hx : ∀ v, 0 ≤ x v ∧ x v ≤ 1) :
    ∃ y : RationalAssignment G,
      (∀ v, ∃ k : ℕ, k ≤ q ∧ (y v).val = (k : ℚ) / (q : ℚ)) ∧
      (∀ v, |y.toReal v - x v| ≤ 1 / (2 * (q : ℝ))) ∧
      y.length ≤ (meshWitnessPolynomial q).eval G.length := by
  classical
  choose k hk hclose using fun v => exists_mesh_numerator q hq (x v) (hx v)
  have hqQ : (0 : ℚ) < (q : ℚ) := by exact_mod_cast hq
  let y : RationalAssignment G := fun v =>
    ⟨(k v : ℚ) / (q : ℚ), div_nonneg (Nat.cast_nonneg _) hqQ.le,
      (div_le_one hqQ).mpr (by exact_mod_cast hk v)⟩
  have hy : ∀ v, ∃ j : ℕ, j ≤ q ∧ (y v).val = (j : ℚ) / (q : ℚ) :=
    fun v => ⟨k v, hk v, rfl⟩
  refine ⟨y, hy, ?_, RationalAssignment.mesh_length_le G q y hy⟩
  intro v
  simpa only [RationalAssignment.toReal, y, Rat.cast_div, Rat.cast_natCast] using hclose v

end PCPforPPAD

end OAI