File size: 25,497 Bytes
9b6e014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Probability.Contingency.IntegerDrawTail

namespace OAI

/-! Stack Safety. -/

noncomputable section


namespace Contingency.BitModel.StackCode
noncomputable section

theorem mass_eq_zero_of_safe (P : Program) (xs : List Bool) (Safe : List Bool→Prop)
    (hs : ∀ (t : ℕ) (f : Fin t→Bool),(run P xs f).state=none → Safe (run P xs f).memory.outputRev.reverse)
    (out : List Bool) (ho : ¬Safe out) (t : ℕ) : mass P xs out t=0 := by
  classical
  unfold mass
  have he : (Finset.univ.filter fun f : Fin t→Bool => (run P xs f).state=none ∧
      (run P xs f).memory.outputRev.reverse=out)=∅ := by
    apply Finset.filter_eq_empty_iff.mpr
    intro f _ hh
    apply ho
    rw [← hh.2]
    exact hs t f hh.1
  rw [he]
  simp

theorem safe_compile (P : Program) (xs : List Bool) (Safe : List Bool→Prop)
    (hs : ∀ (t : ℕ) (f : Fin t→Bool),(run P xs f).state=none → Safe (run P xs f).memory.outputRev.reverse)
    (t : ℕ) (f : Fin t→Bool) (hh : ((compile P).run xs f).state=none) :
    Safe ((compile P).run xs f).outputRev.reverse := by
  classical
  by_contra hn
  have hm := mass_compile_all P xs ((compile P).run xs f).outputRev.reverse t
  rw [mass_eq_zero_of_safe P xs Safe hs _ hn] at hm
  have hc : 0<(Finset.univ.filter fun g : Fin t→Bool => ((compile P).run xs g).state=none ∧
      ((compile P).run xs g).outputRev.reverse=((compile P).run xs f).outputRev.reverse).card := by
    apply Finset.card_pos.mpr
    exact ⟨f,Finset.mem_filter.mpr ⟨Finset.mem_univ _,hh,rfl⟩⟩
  have hp : 0<(compile P).mass xs ((compile P).run xs f).outputRev.reverse t := by
    unfold Machine.mass
    apply div_pos _ (by positivity)
    exact_mod_cast hc
  linarith

end
end Contingency.BitModel.StackCode

 

 

 

open Filter
namespace Contingency.BitModel.StackCode.IntegerDraw
open BinaryTrial
noncomputable section

theorem beValue_snoc (xs : List Bool) (b : Bool) :
    beValue (xs++[b])=2*beValue xs+b.toNat := by
  unfold beValue
  rw [List.foldl_append]
  rfl

theorem beValue_bits_reverse (N : ℕ) : beValue N.bits.reverse=N := by
  induction N using Nat.binaryRec' with
  | zero => rfl
  | bit b n h ih =>
    rw [Nat.bits_append_bit _ _ h,List.reverse_cons,beValue_snoc,ih]
    cases b <;> simp [Nat.bit]

theorem minimal_width (N : ℕ) : 2^N.size≤2*(N+1) := by
  by_cases hn : N.size=0
  · rw [hn]
    simp only [pow_zero]
    omega
  have hh := Nat.lt_size.mp (show N.size-1<N.size by omega)
  have he : N.size=(N.size-1)+1 := by omega
  calc
    2^N.size=2^(N.size-1)*2 := by conv_lhs => rw [he]; rw [pow_succ]
    _ ≤ N*2 := Nat.mul_le_mul_right 2 hh
    _ ≤ 2*(N+1) := by omega

def rawMaximum (N : ℕ) : List Bool := N.bits.reverse

@[simp] theorem rawMaximum_length (N : ℕ) : (rawMaximum N).length=N.size := by
  simp [rawMaximum,Nat.size_eq_bits_len]
@[simp] theorem rawMaximum_value (N : ℕ) : beValue (rawMaximum N)=N := beValue_bits_reverse N

def integerWord (N : ℕ) (u : Fin (N+1)) : List Bool :=
  validWord (rawMaximum N) ⟨u,by simpa using u.isLt⟩

@[simp] theorem integerWord_value (N : ℕ) (u : Fin (N+1)) : beValue (integerWord N u)=u.val := by
  unfold integerWord validWord
  rw [word_value]

@[simp] theorem integerWord_length (N : ℕ) (u : Fin (N+1)) : (integerWord N u).length=N.size := by
  simp [integerWord,validWord,word]

theorem machine_safe (B : List Bool) (t : ℕ) (f : Fin t→Bool) (hh : (machine.run B f).state=none) :
    ∃ u : Fin (beValue B+1),(machine.run B f).outputRev.reverse=validWord B u :=
  safe_compile program B (fun out => ∃ u : Fin (beValue B+1),out=validWord B u)
    (fun _ f hh => stack_safe B f hh) t f hh

 

theorem integer_sampler (N : ℕ) :
    (∀ (t : ℕ) (coins : Fin t→Bool),(machine.run (rawMaximum N) coins).state=none →
      ∃ u : Fin (N+1),(machine.run (rawMaximum N) coins).outputRev.reverse=integerWord N u) ∧
    Tendsto (machine.tail (rawMaximum N)) atTop (nhds 0) ∧
    (∀ u : Fin (N+1),Tendsto (machine.mass (rawMaximum N) (integerWord N u)) atTop (nhds (1/(N+1:ℝ)))) ∧
    Summable (machine.tail (rawMaximum N)) ∧
    (∑' t,machine.tail (rawMaximum N) t)≤52*(N.size:ℝ)+30 := by
  refine ⟨?_,machine_halts_ae _,?_,machine_summable _,?_⟩
  · intro t coins hh
    obtain ⟨u,hu⟩ := machine_safe (rawMaximum N) t coins hh
    refine ⟨⟨u,by simpa using u.isLt⟩,?_⟩
    exact hu
  · intro u
    simpa only [rawMaximum_value,integerWord] using machine_uniform (rawMaximum N) ⟨u,by simpa using u.isLt⟩
  · have hw : 2^(rawMaximum N).length≤2*(beValue (rawMaximum N)+1) := by
      simpa using minimal_width N
    simpa using machine_linear_expected (rawMaximum N) hw

end
end Contingency.BitModel.StackCode.IntegerDraw

 

 

 

namespace Contingency.BitModel.StackCode.BinarySubtract
noncomputable section

def outBit (a b carry : Bool) : Bool := xor (xor a b) carry
def nextBorrow (a b carry : Bool) : Bool := (!a && (b || carry)) || (b && carry)

theorem digit_identity (a b carry : Bool) :
    a.toNat+2*(nextBorrow a b carry).toNat = b.toNat+carry.toNat+(outBit a b carry).toNat := by
  cases a <;> cases b <;> cases carry <;> decide

def value : List Bool→ℕ
  | [] => 0
  | b::bs => b.toNat+2*value bs

structure Digit where
  a : Bool
  b : Bool

def output : List Digit → Bool → List Bool
  | [],_ => []
  | d::ds,c => outBit d.a d.b c :: output ds (nextBorrow d.a d.b c)

def borrow : List Digit → Bool → Bool
  | [],c => c
  | d::ds,c => borrow ds (nextBorrow d.a d.b c)

@[simp] theorem output_length (ds : List Digit) (c : Bool) : (output ds c).length=ds.length := by
  induction ds generalizing c with
  | nil => rfl
  | cons d ds ih => simp [output,ih]

theorem value_lt (bs : List Bool) : value bs<2^bs.length := by
  induction bs with
  | nil => decide
  | cons b bs ih =>
    change b.toNat+2*value bs<2^(bs.length+1)
    rw [pow_succ]
    cases b <;> simp only [Bool.toNat_false,Bool.toNat_true] <;> omega

theorem word_identity (ds : List Digit) (c : Bool) :
    value (ds.map Digit.a)+2^ds.length*(borrow ds c).toNat =
      value (ds.map Digit.b)+c.toNat+value (output ds c) := by
  induction ds generalizing c with
  | nil => simp [value,borrow,output]
  | cons d ds ih =>
    have hd := digit_identity d.a d.b c
    have hh := ih (nextBorrow d.a d.b c)
    simp only [List.map_cons,value,List.length_cons,pow_succ,borrow,output]
    nlinarith

theorem subtract_correct (ds : List Digit) (h : value (ds.map Digit.b)≤value (ds.map Digit.a)) :
    borrow ds false=false ∧ value (output ds false)=value (ds.map Digit.a)-value (ds.map Digit.b) := by
  have hh := word_identity ds false
  have ho := value_lt (output ds false)
  rw [output_length] at ho
  cases hb : borrow ds false <;> simp only [hb,Bool.toNat_false,Bool.toNat_true,Nat.mul_zero,Nat.mul_one,Nat.add_zero] at hh
  · exact ⟨rfl,by omega⟩
  · omega

inductive Q
  | start (carry : Bool)
  | second (carry a : Bool)
  | place (carry a b : Bool)
  | done (carry : Bool)
  deriving DecidableEq, Fintype

def label (q : Q) : Fin (Fintype.card Q) := Fintype.equivFin Q q

def code : Q→Op (Fintype.card Q) 4
  | .start c => .pop 0 (label (.done c)) (label (.second c false)) (label (.second c true))
  | .second c a => .pop 1 (label (.place c a false)) (label (.place c a false)) (label (.place c a true))
  | .place c a b => .push 2 (outBit a b c) (label (.start (nextBorrow a b c)))
  | .done _ => .halt

def program : Program := ⟨Fintype.card Q,4,label (.start false),fun pc => code ((Fintype.equivFin Q).symm pc)⟩
def config (q : Q) (m : Memory 4) : Config program := ⟨some (label q),m⟩

def digitMemory (d : Digit) (c : Bool) (m : Memory 4) : Memory 4 :=
  ((m.pop 0).pop 1).push 2 (outBit d.a d.b c)

def wordMemory : List Digit→Bool→Memory 4→Memory 4
  | [],_,m => m
  | d::ds,c,m => wordMemory ds (nextBorrow d.a d.b c) (digitMemory d c m)

theorem one_step (q : Q) (m : Memory 4) (coin : Bool) :
    step program coin (config q m)=
      ⟨(effect (code q) coin m).1,(effect (code q) coin m).2⟩ := by
  simp [step,config,program,label]

theorem first_step (m : Memory 4) (c a x : Bool) (as : List Bool) (ha : m.stack 0=a::as) :
    step program x (config (.start c) m)=config (.second c a) (m.pop 0) := by
  rw [one_step]
  cases a <;> simp [code,effect,branch,ha,config] <;> rfl

theorem second_step (m : Memory 4) (c a b x : Bool) (bs : List Bool) (hb : m.stack 1=b::bs) :
    step program x (config (.second c a) m)=config (.place c a b) (m.pop 1) := by
  rw [one_step]
  cases b <;> simp [code,effect,branch,hb,config] <;> rfl

theorem third_step (m : Memory 4) (c a b x : Bool) :
    step program x (config (.place c a b) m)=
      config (.start (nextBorrow a b c)) (m.push 2 (outBit a b c)) := by
  rw [one_step]
  rfl

theorem three_steps (m : Memory 4) (d : Digit) (c x y z : Bool) (as bs : List Bool)
    (ha : m.stack 0=d.a::as) (hb : m.stack 1=d.b::bs) :
    [x,y,z].foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      config (.start (nextBorrow d.a d.b c)) (digitMemory d c m) := by
  simp only [List.foldl_cons,List.foldl_nil]
  rw [first_step m c d.a x as ha,second_step _ c d.a d.b y bs,third_step]
  · rfl
  · simpa using hb

theorem pass (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b) :
    (List.replicate (3*ds.length) false).foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      config (.start (borrow ds c)) (wordMemory ds c m) := by
  induction ds generalizing c m with
  | nil => rfl
  | cons d ds ih =>
    have he : 3*(d::ds).length=3+3*ds.length := by simp; omega
    rw [he,List.replicate_add,List.foldl_append]
    change (List.replicate (3*ds.length) false).foldl _
      ([false,false,false].foldl _ (config (.start c) m))=_
    rw [three_steps m d c false false false _ _ ha hb,ih]
    · rfl
    · simp [digitMemory,Memory.push,Memory.pop,ha]
    · simp [digitMemory,Memory.push,Memory.pop,hb]

end
end Contingency.BitModel.StackCode.BinarySubtract

 

 

 

namespace Contingency.BitModel.StackCode.BinarySubtract
noncomputable section

theorem step_independent (cfg : Config program) (b b' : Bool) : step program b cfg=step program b' cfg := by
  rcases cfg with ⟨s,m⟩
  cases s with
  | none => rfl
  | some pc =>
    unfold step
    dsimp only [program]
    cases (Fintype.equivFin Q).symm pc <;> rfl

theorem fold_independent (xs ys : List Bool) (hl : xs.length=ys.length) (cfg : Config program) :
    xs.foldl (fun cfg b => step program b cfg) cfg=ys.foldl (fun cfg b => step program b cfg) cfg := by
  induction xs generalizing ys cfg with
  | nil => cases ys <;> simp_all
  | cons x xs ih =>
    cases ys with
    | nil => simp at hl
    | cons y ys =>
      simp only [List.length_cons,Nat.add_right_cancel_iff] at hl
      simp only [List.foldl_cons]
      rw [step_independent _ x y]
      exact ih _ hl _

theorem pass_any (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b)
    (coins : List Bool) (hl : coins.length=3*ds.length) :
    coins.foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      config (.start (borrow ds c)) (wordMemory ds c m) := by
  rw [fold_independent coins (List.replicate (3*ds.length) false) (by simpa using hl)]
  exact pass ds c m ha hb

theorem wordMemory_stacks (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b) :
    (wordMemory ds c m).stack 0=[] ∧ (wordMemory ds c m).stack 1=[] ∧
    (wordMemory ds c m).stack 2=(output ds c).reverse++m.stack 2 ∧
    (wordMemory ds c m).stack 3=m.stack 3 ∧
    (wordMemory ds c m).input=m.input ∧ (wordMemory ds c m).outputRev=m.outputRev := by
  induction ds generalizing c m with
  | nil => exact ⟨ha,hb,rfl,rfl,rfl,rfl⟩
  | cons d ds ih =>
    have h0 : (digitMemory d c m).stack 0=ds.map Digit.a := by
      simp [digitMemory,Memory.push,Memory.pop,ha]
    have h1 : (digitMemory d c m).stack 1=ds.map Digit.b := by
      simp [digitMemory,Memory.push,Memory.pop,hb]
    have hh := ih (nextBorrow d.a d.b c) (digitMemory d c m) h0 h1
    refine ⟨hh.1,hh.2.1,?_,?_,hh.2.2.2.2.1,hh.2.2.2.2.2⟩
    · simpa [wordMemory,output,List.reverse_cons,List.append_assoc,digitMemory,Memory.push,Memory.pop,Function.update_apply] using hh.2.2.1
    · simpa [wordMemory,digitMemory,Memory.push,Memory.pop,Function.update_apply] using hh.2.2.2.1

theorem finish (m : Memory 4) (c b : Bool) (hm : m.stack 0=[]) :
    step program b (config (.start c) m)=config (.done c) (m.pop 0) := by
  simp [step,program,config,label,code,effect,branch,hm]

theorem halt (m : Memory 4) (c b : Bool) :
    step program b (config (.done c) m)=⟨none,m⟩ := by
  simp [step,program,config,label,code,effect]

theorem finish_run (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b)
    (coins : List Bool) (hl : coins.length=3*ds.length) (b b' : Bool) :
    (coins++[b,b']).foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      ⟨none,(wordMemory ds c m).pop 0⟩ := by
  rw [List.foldl_append,pass_any ds c m ha hb coins hl]
  simp only [List.foldl_cons,List.foldl_nil]
  rw [finish _ _ _ (wordMemory_stacks ds c m ha hb).1,halt]

theorem fold_compiled (P : Program) (cfg : Config P) {t : ℕ} (coins : Fin (t*2)→Bool) :
    (List.ofFn coins).foldl (fun cfg b => (compile P).step b cfg) (encode P cfg)=
      encode P ((List.ofFn (fun i : Fin t => coins (second i))).foldl (fun cfg b => step P b cfg) cfg) := by
  rw [coins_as_pairs,fold_pairs]
  congr 1
  have hh := List.foldl_map (f:=Prod.snd) (g:=fun cfg b => step P b cfg)
    (l:=List.ofFn fun i : Fin t => (coins (first i),coins (second i))) (init:=cfg)
  simpa only [List.map_ofFn,Function.comp_def] using hh.symm

 

theorem tape_pass (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b)
    (coins : Fin ((3*ds.length)*2)→Bool) :
    (List.ofFn coins).foldl (fun cfg b => (compile program).step b cfg) (encode program (config (.start c) m))=
      encode program (config (.start (borrow ds c)) (wordMemory ds c m)) := by
  rw [fold_compiled,pass_any ds c m ha hb _ (by simp)]

end
end Contingency.BitModel.StackCode.BinarySubtract

 

 

 

namespace Contingency.BitModel.StackCode.BinaryAdd
open BinarySubtract
noncomputable section

def nextCarry (a b c : Bool) : Bool := (a && b) || (a && c) || (b && c)

theorem digit_identity (a b c : Bool) :
    a.toNat+b.toNat+c.toNat = (outBit a b c).toNat+2*(nextCarry a b c).toNat := by
  cases a <;> cases b <;> cases c <;> decide

def output : List Digit → Bool → List Bool
  | [],_ => []
  | d::ds,c => outBit d.a d.b c :: output ds (nextCarry d.a d.b c)

def carry : List Digit → Bool → Bool
  | [],c => c
  | d::ds,c => carry ds (nextCarry d.a d.b c)

@[simp] theorem output_length (ds : List Digit) (c : Bool) : (output ds c).length=ds.length := by
  induction ds generalizing c with
  | nil => rfl
  | cons d ds ih => simp [output,ih]

theorem word_identity (ds : List Digit) (c : Bool) :
    value (ds.map Digit.a)+value (ds.map Digit.b)+c.toNat =
      value (output ds c)+2^ds.length*(carry ds c).toNat := by
  induction ds generalizing c with
  | nil => simp [value,carry,output]
  | cons d ds ih =>
    have hd := digit_identity d.a d.b c
    have hh := ih (nextCarry d.a d.b c)
    simp only [List.map_cons,value,List.length_cons,pow_succ,carry,output]
    nlinarith

inductive Q
  | start (carry : Bool)
  | second (carry a : Bool)
  | place (carry a b : Bool)
  | done (carry : Bool)
  | halt
  deriving DecidableEq, Fintype

def label (q : Q) : Fin (Fintype.card Q) := Fintype.equivFin Q q

def code : Q→Op (Fintype.card Q) 4
  | .start c => .pop 0 (label (.done c)) (label (.second c false)) (label (.second c true))
  | .second c a => .pop 1 (label (.place c a false)) (label (.place c a false)) (label (.place c a true))
  | .place c a b => .push 2 (outBit a b c) (label (.start (nextCarry a b c)))
  | .done c => .push 2 c (label .halt)
  | .halt => .halt

def program : Program := ⟨Fintype.card Q,4,label (.start false),fun pc => code ((Fintype.equivFin Q).symm pc)⟩
def config (q : Q) (m : Memory 4) : Config program := ⟨some (label q),m⟩

def digitMemory (d : Digit) (c : Bool) (m : Memory 4) : Memory 4 :=
  ((m.pop 0).pop 1).push 2 (outBit d.a d.b c)

def wordMemory : List Digit→Bool→Memory 4→Memory 4
  | [],_,m => m
  | d::ds,c,m => wordMemory ds (nextCarry d.a d.b c) (digitMemory d c m)

theorem one_step (q : Q) (m : Memory 4) (coin : Bool) :
    step program coin (config q m)=
      ⟨(effect (code q) coin m).1,(effect (code q) coin m).2⟩ := by
  simp [step,config,program,label]

theorem first_step (m : Memory 4) (c a x : Bool) (as : List Bool) (ha : m.stack 0=a::as) :
    step program x (config (.start c) m)=config (.second c a) (m.pop 0) := by
  rw [one_step]
  cases a <;> simp [code,effect,branch,ha,config] <;> rfl

theorem second_step (m : Memory 4) (c a b x : Bool) (bs : List Bool) (hb : m.stack 1=b::bs) :
    step program x (config (.second c a) m)=config (.place c a b) (m.pop 1) := by
  rw [one_step]
  cases b <;> simp [code,effect,branch,hb,config] <;> rfl

theorem third_step (m : Memory 4) (c a b x : Bool) :
    step program x (config (.place c a b) m)=
      config (.start (nextCarry a b c)) (m.push 2 (outBit a b c)) := by
  rw [one_step]
  rfl

theorem three_steps (m : Memory 4) (d : Digit) (c x y z : Bool) (as bs : List Bool)
    (ha : m.stack 0=d.a::as) (hb : m.stack 1=d.b::bs) :
    [x,y,z].foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      config (.start (nextCarry d.a d.b c)) (digitMemory d c m) := by
  simp only [List.foldl_cons,List.foldl_nil]
  rw [first_step m c d.a x as ha,second_step _ c d.a d.b y bs,third_step]
  · rfl
  · simpa using hb

theorem pass (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b) :
    (List.replicate (3*ds.length) false).foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      config (.start (carry ds c)) (wordMemory ds c m) := by
  induction ds generalizing c m with
  | nil => rfl
  | cons d ds ih =>
    have he : 3*(d::ds).length=3+3*ds.length := by simp; omega
    rw [he,List.replicate_add,List.foldl_append]
    change (List.replicate (3*ds.length) false).foldl _
      ([false,false,false].foldl _ (config (.start c) m))=_
    rw [three_steps m d c false false false _ _ ha hb,ih]
    · rfl
    · simp [digitMemory,Memory.push,Memory.pop,ha]
    · simp [digitMemory,Memory.push,Memory.pop,hb]

end
end Contingency.BitModel.StackCode.BinaryAdd

 

 

 

namespace Contingency.BitModel.StackCode.BinaryAdd
open BinarySubtract
noncomputable section

theorem step_independent (cfg : Config program) (b b' : Bool) : step program b cfg=step program b' cfg := by
  rcases cfg with ⟨s,m⟩
  cases s with
  | none => rfl
  | some pc =>
    unfold step
    dsimp only [program]
    cases (Fintype.equivFin Q).symm pc <;> rfl

theorem fold_independent (xs ys : List Bool) (hl : xs.length=ys.length) (cfg : Config program) :
    xs.foldl (fun cfg b => step program b cfg) cfg=ys.foldl (fun cfg b => step program b cfg) cfg := by
  induction xs generalizing ys cfg with
  | nil => cases ys <;> simp_all
  | cons x xs ih =>
    cases ys with
    | nil => simp at hl
    | cons y ys =>
      simp only [List.length_cons,Nat.add_right_cancel_iff] at hl
      simp only [List.foldl_cons]
      rw [step_independent _ x y]
      exact ih _ hl _

theorem pass_any (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b)
    (coins : List Bool) (hl : coins.length=3*ds.length) :
    coins.foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      config (.start (carry ds c)) (wordMemory ds c m) := by
  rw [fold_independent coins (List.replicate (3*ds.length) false) (by simpa using hl)]
  exact pass ds c m ha hb

theorem wordMemory_stacks (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b) :
    (wordMemory ds c m).stack 0=[] ∧ (wordMemory ds c m).stack 1=[] ∧
    (wordMemory ds c m).stack 2=(output ds c).reverse++m.stack 2 ∧
    (wordMemory ds c m).stack 3=m.stack 3 ∧
    (wordMemory ds c m).input=m.input ∧ (wordMemory ds c m).outputRev=m.outputRev := by
  induction ds generalizing c m with
  | nil => exact ⟨ha,hb,rfl,rfl,rfl,rfl⟩
  | cons d ds ih =>
    have h0 : (digitMemory d c m).stack 0=ds.map Digit.a := by
      simp [digitMemory,Memory.push,Memory.pop,ha]
    have h1 : (digitMemory d c m).stack 1=ds.map Digit.b := by
      simp [digitMemory,Memory.push,Memory.pop,hb]
    have hh := ih (nextCarry d.a d.b c) (digitMemory d c m) h0 h1
    refine ⟨hh.1,hh.2.1,?_,?_,hh.2.2.2.2.1,hh.2.2.2.2.2⟩
    · simpa [wordMemory,output,List.reverse_cons,List.append_assoc,digitMemory,Memory.push,Memory.pop,Function.update_apply] using hh.2.2.1
    · simpa [wordMemory,digitMemory,Memory.push,Memory.pop,Function.update_apply] using hh.2.2.2.1

theorem finish (m : Memory 4) (c b : Bool) (hm : m.stack 0=[]) :
    step program b (config (.start c) m)=config (.done c) (m.pop 0) := by
  simp [step,program,config,label,code,effect,branch,hm]

theorem final_carry (m : Memory 4) (c b : Bool) :
    step program b (config (.done c) m)=config .halt (m.push 2 c) := by
  rw [one_step]; rfl

theorem halt (m : Memory 4) (b : Bool) :
    step program b (config .halt m)=⟨none,m⟩ := by
  rw [one_step]; rfl

theorem finish_run (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b)
    (coins : List Bool) (hl : coins.length=3*ds.length) (b b' b'' : Bool) :
    (coins++[b,b',b'']).foldl (fun cfg b => step program b cfg) (config (.start c) m)=
      ⟨none,((wordMemory ds c m).pop 0).push 2 (carry ds c)⟩ := by
  rw [List.foldl_append,pass_any ds c m ha hb coins hl]
  simp only [List.foldl_cons,List.foldl_nil]
  rw [finish _ _ _ (wordMemory_stacks ds c m ha hb).1,final_carry,halt]

 

theorem tape_pass (ds : List Digit) (c : Bool) (m : Memory 4)
    (ha : m.stack 0=ds.map Digit.a) (hb : m.stack 1=ds.map Digit.b)
    (coins : Fin ((3*ds.length)*2)→Bool) :
    (List.ofFn coins).foldl (fun cfg b => (compile program).step b cfg) (encode program (config (.start c) m))=
      encode program (config (.start (carry ds c)) (wordMemory ds c m)) := by
  rw [BinarySubtract.fold_compiled,pass_any ds c m ha hb _ (by simp)]

end
end Contingency.BitModel.StackCode.BinaryAdd

 

 

 

namespace Contingency.BitModel.StackCode.BinarySubtract
noncomputable section

theorem value_append_false (xs : List Bool) (n : ℕ) :
    value (xs++List.replicate n false)=value xs := by
  induction xs with
  | nil =>
    simp only [List.nil_append,value]
    induction n with
    | zero => rfl
    | succ n ih => simp [List.replicate_succ,value,ih]
  | cons b xs ih => simp [value,ih]

theorem value_bits (x : ℕ) : value x.bits=x := by
  induction x using Nat.binaryRec' with
  | zero => rfl
  | bit b n h ih =>
    rw [Nat.bits_append_bit _ _ h]
    change b.toNat+2*value n.bits=Nat.bit b n
    rw [ih]
    cases b <;> simp [Nat.bit]
    omega

def bits (n x : ℕ) : List Bool := x.bits++List.replicate (n-x.size) false

@[simp] theorem bits_length (n x : ℕ) (h : x<2^n) : (bits n x).length=n := by
  have hh := Nat.size_le.mpr h
  simp only [bits,List.length_append,List.length_replicate]
  have he := Nat.size_eq_bits_len x
  omega

@[simp] theorem bits_value (n x : ℕ) : value (bits n x)=x := by
  rw [bits,value_append_false,value_bits]

def digits (as bs : List Bool) : List Digit := List.zipWith Digit.mk as bs

theorem digits_maps (as bs : List Bool) (h : as.length=bs.length) :
    (digits as bs).map Digit.a=as ∧ (digits as bs).map Digit.b=bs ∧
    (digits as bs).length=as.length := by
  induction as generalizing bs with
  | nil =>
    have he : bs=[] := List.length_eq_zero_iff.mp (by simpa using h.symm)
    subst bs
    exact ⟨rfl,rfl,rfl⟩
  | cons a as ih =>
    cases bs with
    | nil => simp at h
    | cons b bs =>
      have hh := ih bs (by simpa using h)
      simpa [digits,List.zipWith_cons_cons] using hh

theorem borrow_iff (ds : List Digit) :
    borrow ds false=true ↔ value (ds.map Digit.a)<value (ds.map Digit.b) := by
  have hi := word_identity ds false
  have ha := value_lt (ds.map Digit.a)
  have hd := value_lt (output ds false)
  simp only [List.length_map,output_length] at ha hd
  cases hb : borrow ds false <;>
    simp only [hb,Bool.toNat_false,Bool.toNat_true,Nat.mul_zero,Nat.mul_one,Nat.add_zero] at hi
  · constructor
    · intro h; cases h
    · intro h; omega
  · constructor
    · intro h; omega
    · intro h; rfl

 

theorem padded_subtraction (n x y : ℕ) (hx : x<2^n) (hy : y<2^n) :
    let ds := digits (bits n x) (bits n y)
    ds.length=n ∧ (borrow ds false=true ↔ x<y) ∧
      (y≤x → value (output ds false)=x-y) := by
  dsimp only
  obtain ⟨ha,hb,hl⟩ := digits_maps (bits n x) (bits n y) (by rw [bits_length _ _ hx,bits_length _ _ hy])
  refine ⟨hl.trans (bits_length _ _ hx),?_,?_⟩
  · simpa [ha,hb] using borrow_iff (digits (bits n x) (bits n y))
  · intro h
    have hh := subtract_correct (digits (bits n x) (bits n y)) (by simpa [ha,hb] using h)
    simpa [ha,hb] using hh.2

end
end Contingency.BitModel.StackCode.BinarySubtract

end

end OAI