Repository navigation
Expand file tree
/
Copy pathExprCorrect.lean
More file actions
5717 lines (5623 loc) · 335 KB
/
Copy pathExprCorrect.lean
File metadata and controls
5717 lines (5623 loc) · 335 KB
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Verified.ExprSpec
import Verified.ArgsCorrect
import Verified.VarCorrect
import Verified.Float32Correct
import Verified.TreeNodeCorrect
import Verified.TreeCaseCorrect
import Verified.ListFoldCorrect
import Verified.Bytes.Get
import Verified.BytesBuildCorrect
import Verified.BytesLiteralCorrect
import Verified.LiteralCoverage
import Verified.LiteralCoverageFacts
/-! Correctness of expression code and ordinary function templates. -/
namespace Verified
variable {pool : Bytes.LiteralPool}
open Wasm LeanExe.Pipeline LeanExe.Runtime LeanExe.ProofKit
section Cases
variable {S : List Sig} {m : Module} {funs : Funs S} {flags : Flags S} {bounds : Bounds S}
{fits : Fits S} {host : HostEnv Unit}
{pv : List Value}
/-- A constant word. -/
theorem spec_word {Γ : List Ty} (value : UInt64) :
∀ env slots paid live,
CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.word (S := S) (Γ := Γ) value) env slots
paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
have hNext0 := fun store' s' ws a =>
hNext heap store' s' ws a (fun _ => Nat.le_add_right _ _)
simpa [Expr.code, Expr.usesFast_eq, Expr.denote] using
hNext0 store s [.i64 value] (After.refl hAt hVars (by intro i h; simp [h]) rfl
(Mode.fresh_scalar rfl _ _) fun _ _ _ _ _ _ _ b hb => by
rw [Ty.regions_scalar .word rfl] at hb; exact nomatch hb)
theorem spec_treeNil {Γ : List Ty} (shape : TreeShape) (hn : shape.hasNull = true) :
∀ env slots paid live,
CodeSpec (pool := pool) m funs flags bounds fits host pv
(Expr.treeNil (S := S) (Γ := Γ) shape hn) env slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
have hRep : (Ty.tree shape).Rep .owned heap store [.i64 0] (Tree.nil hn) :=
⟨0, rfl, rfl, List.Pairwise.nil⟩
have hAfter : After env slots (fun i => live i || (Expr.treeNil shape hn).uses i) live base
heap store s (.tree shape) .owned (.nil hn) heap store s [.i64 0] :=
After.refl hAt hVars (by intro i hi; simp [hi]) hRep rfl
(fun _ _ _ _ _ _ _ _ hb => nomatch hb)
have hRun := hNext heap store s [.i64 0] hAfter (fun _ => Nat.le_add_right _ _)
simpa [Expr.code, Expr.denote] using hRun
theorem spec_bytesEmpty {Γ : List Ty} :
∀ env slots paid live,
CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.bytesEmpty (S := S) (Γ := Γ))
env slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
have hRep : Ty.bytes.Rep .owned heap store [.i64 0, .i64 0] LeanExe.Bytes.empty :=
⟨0, 0, rfl, Bytes.Rep.empty heap store⟩
have hAfter : After env slots (fun i => live i || Expr.bytesEmpty.uses i) live base
heap store s .bytes .owned LeanExe.Bytes.empty heap store s [.i64 0, .i64 0] :=
After.refl hAt hVars (by intro i hi; simp [hi]) hRep rfl
(fun _ _ _ _ _ _ _ _ hb => nomatch hb)
have hRun := hNext heap store s [.i64 0, .i64 0] hAfter (fun _ => Nat.le_add_right _ _)
simpa [Expr.code, Expr.denote] using hRun
/-- A constant `Bool`. -/
theorem spec_bool {Γ : List Ty} (value : Bool) :
∀ env slots paid live,
CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.bool (S := S) (Γ := Γ) value) env slots
paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
have hNext0 := fun store' s' ws a =>
hNext heap store' s' ws a (fun _ => Nat.le_add_right _ _)
simpa [Expr.code, Expr.usesFast_eq, Expr.denote] using
hNext0 store s [.i64 (boolWord value)] (After.refl hAt hVars (by intro i h; simp [h]) rfl
(Mode.fresh_scalar rfl _ _) fun _ _ _ _ _ _ _ b hb => by
rw [Ty.regions_scalar .bool rfl] at hb; exact nomatch hb)
/-- A float constant, from its bit pattern. -/
theorem spec_float {Γ : List Ty} (bits : UInt64) :
∀ env slots paid live,
CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.float (S := S) (Γ := Γ) bits) env slots
paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
have hNext0 := fun store' s' ws a =>
hNext heap store' s' ws a (fun _ => Nat.le_add_right _ _)
simpa [Expr.code, Expr.usesFast_eq, Expr.denote, F64Bits.toBits_ofBits] using
hNext0 store s [.f64 (Float.ofBits bits).toBits] (After.refl hAt hVars
(by intro i h; simp [h]) rfl (Mode.fresh_scalar rfl _ _) fun _ _ _ _ _ _ _ b hb => by
rw [Ty.regions_scalar .float rfl] at hb; exact nomatch hb)
/-- An operation on words; division and remainder save their operands to test the divisor. -/
theorem spec_bin {Γ : List Ty} (op : BinOp) {left right : Expr S Γ .word}
(leftSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv left env slots paid live)
(rightSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv right env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.bin op left right) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : left.width ≤ max op.scratch (max left.width right.width) :=
(Nat.le_max_left ..).trans (Nat.le_max_right ..)
have hR : right.width ≤ max op.scratch (max left.width right.width) :=
(Nat.le_max_right ..).trans (Nat.le_max_right ..)
have hS : op.scratch ≤ max op.scratch (max left.width right.width) := Nat.le_max_left ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec leftSpec rightSpec env slots paid live h base heap store s hh hpv
hVars hAt hSafe hCap hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
simp only [Ty.rep_word] at hR1 hR2
subst hR1 hR2
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append]
cases op
case div | rem =>
have hW2 : base + 2 ≤ h := by
have := hRoom
simp only [Expr.width, BinOp.scratch] at this
omega
have hp2 : s2.params = s.params := hF.params
have hh2 : s2.positions = h := hF.positions.trans hh
simp only [BinOp.code, List.cons_append, List.nil_append]
refine wp_localSet_local (by rw [hp2]; omega)
(Locals.lt_total (show base + 1 < ({ s2 with values := _ } : Locals).positions by
rw [Locals.positions_values, hh2]; omega)) ?_
refine wp_localSet_local (vs := s.values)
(s := setLocal { s2 with values := .i64 (left.denote funs env) :: s.values } (base + 1)
(.i64 (right.denote funs env)))
(by show s2.params.length ≤ base; rw [hp2]; omega)
(Locals.lt_total (by simp only [Locals.positions_setLocal, Locals.positions_values]; omega)) ?_
let s2a := setLocal { s2 with values := .i64 (left.denote funs env) :: s.values } (base + 1)
(.i64 (right.denote funs env))
let s2b := setLocal { s2a with values := s.values } base (.i64 (left.denote funs env))
show wp m _ Q store2 s2b host
have hLowA : ({ s2 with values := .i64 (left.denote funs env) :: s.values } :
Locals).params.length ≤ base + 1 := by
show s2.params.length ≤ base + 1; rw [hp2]; omega
have hLowB : ({ s2a with values := s.values } : Locals).params.length ≤ base := by
show s2.params.length ≤ base; rw [hp2]; omega
have hRightSlot : s2b.get (base + 1) = some (.i64 (right.denote funs env)) := by
rw [Locals.get_setLocal_ne hLowB (by omega), Locals.get_values]
exact Locals.get_setLocal_same hLowA
(Locals.lt_total (by simp only [Locals.positions_values]; omega))
have hLeftSlot : s2b.get base = some (.i64 (left.denote funs env)) :=
Locals.get_setLocal_same hLowB
(Locals.lt_total (by simp only [s2a, Locals.positions_setLocal, Locals.positions_values]; omega))
have hF2 : Frame base s2 s2b :=
(Frame.setValues (s := s2) (vs := .i64 (left.denote funs env) :: s.values)
(v := .i64 (right.denote funs env)) (by rw [hp2]; omega) (by omega) (by omega)).trans
(Frame.setValues (s := s2a) (vs := s.values) (v := .i64 (left.denote funs env))
(by show s2.params.length ≤ base; rw [hp2]; omega) le_rfl
(by simp only [s2a, Locals.positions_setLocal, Locals.positions_values]; omega))
have hFrame : Frame base s s2b := hF.trans hF2
have hHolds : Holds env slots live base heap2 store2 s2b := hH.agree hF2
have hs2b : s2b.values = s.values := rfl
simp only [wp_localGet_cons, hRightSlot, wp_eqzI64_cons]
rw [wp_iff_control_types]
refine wp_iff_cons rfl ?_
by_cases hZero : right.denote funs env = 0
· simp only [hZero, ite_true, ne_eq]
simpa [-Locals.get, hLeftSlot, hZero, Expr.denote, BinOp.apply, hs2b] using
hNext2 store2 s2b _ (After.ofScalar rfl hStep' rfl hFrame hHolds rfl)
· simp only [hZero, ite_false, ne_eq, not_true_eq_false]
simpa [-Locals.get, hLeftSlot, hRightSlot, hZero, Expr.denote, BinOp.apply, hs2b] using
hNext2 store2 s2b _ (After.ofScalar rfl hStep' rfl hFrame hHolds rfl)
all_goals
simpa [BinOp.code, Expr.denote, BinOp.apply, ← UInt64.shiftLeft_eq_shiftLeft_mod,
← UInt64.shiftRight_eq_shiftRight_mod] using
hNext2 store2 s2 _ (After.ofScalar rfl hStep' rfl hF hH rfl)
/-- A comparison of words. -/
theorem spec_cmp {Γ : List Ty} (op : CmpOp) {left right : Expr S Γ .word}
(leftSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv left env slots paid live)
(rightSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv right env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.cmp op left right) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : left.width ≤ max left.width right.width := Nat.le_max_left ..
have hR : right.width ≤ max left.width right.width := Nat.le_max_right ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec leftSpec rightSpec env slots paid live h base heap store s hh hpv
hVars hAt hSafe hCap hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
simp only [Ty.rep_word] at hR1 hR2
subst hR1 hR2
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
have hv := hNext2 store2 s2 _ (After.ofScalar rfl hStep' rfl hF hH rfl)
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append]
cases op <;>
simp only [CmpOp.instr, wp_eqI64_cons, wp_neI64_cons, wp_ltUI64_cons, wp_leUI64_cons,
wp_extendUI32_cons]
· by_cases hab : left.denote funs env = right.denote funs env <;>
simpa [hab, Expr.denote, CmpOp.apply, boolWord] using hv
· by_cases hab : left.denote funs env = right.denote funs env <;>
simpa [hab, Expr.denote, CmpOp.apply, boolWord] using hv
· by_cases hab : left.denote funs env < right.denote funs env <;>
simpa [hab, Expr.denote, CmpOp.apply, boolWord] using hv
· by_cases hab : left.denote funs env ≤ right.denote funs env <;>
simpa [hab, Expr.denote, CmpOp.apply, boolWord] using hv
/-- An operation on floats. -/
theorem spec_fbin {Γ : List Ty} (op : FBinOp) {left right : Expr S Γ .float}
(leftSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv left env slots paid live)
(rightSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv right env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.fbin op left right) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : left.width ≤ max left.width right.width := Nat.le_max_left ..
have hR : right.width ≤ max left.width right.width := Nat.le_max_right ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec leftSpec rightSpec env slots paid live h base heap store s hh hpv
hVars hAt hSafe hCap hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
have hR1' : ws1 = [.f64 (left.denote funs env).toBits] := hR1
have hR2' : ws2 = [.f64 (right.denote funs env).toBits] := hR2
subst hR1' hR2'
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
have hv := hNext2 store2 s2 [.f64 ((Expr.fbin op left right).denote funs env).toBits]
(After.ofScalar rfl hStep' rfl hF hH rfl)
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append] at hv ⊢
cases op <;>
simpa [FBinOp.instr, Expr.denote, FBinOp.apply, f64Add, f64Sub, f64Mul, f64Div,
F64Bits.toBits_add, F64Bits.toBits_sub, F64Bits.toBits_mul, F64Bits.toBits_div] using hv
/-- A comparison of floats. -/
theorem spec_fcmp {Γ : List Ty} (op : FCmpOp) {left right : Expr S Γ .float}
(leftSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv left env slots paid live)
(rightSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv right env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.fcmp op left right) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : left.width ≤ max left.width right.width := Nat.le_max_left ..
have hR : right.width ≤ max left.width right.width := Nat.le_max_right ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec leftSpec rightSpec env slots paid live h base heap store s hh hpv
hVars hAt hSafe hCap hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
have hR1' : ws1 = [.f64 (left.denote funs env).toBits] := hR1
have hR2' : ws2 = [.f64 (right.denote funs env).toBits] := hR2
subst hR1' hR2'
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
have hv := hNext2 store2 s2 _ (After.ofScalar rfl hStep' rfl hF hH rfl)
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append] at hv ⊢
cases op <;>
simp only [FCmpOp.instr, wp_f64Lt_cons, wp_f64Le_cons, wp_f64Eq_cons, wp_extendUI32_cons]
· cases hc : IEEE64.lt (left.denote funs env).toBits (right.denote funs env).toBits <;>
simpa [hc, Expr.denote, FCmpOp.apply, F64Bits.decide_lt, f64Lt, boolWord] using hv
· cases hc : IEEE64.le (left.denote funs env).toBits (right.denote funs env).toBits <;>
simpa [hc, Expr.denote, FCmpOp.apply, F64Bits.decide_le, f64Le, boolWord] using hv
· cases hc : IEEE64.eq (left.denote funs env).toBits (right.denote funs env).toBits <;>
simpa [hc, Expr.denote, FCmpOp.apply, F64Bits.beq_eq, f64Eq, boolWord] using hv
/-- An operation on one float. The negation subtracts from negative zero, pushed first. -/
theorem spec_funary {Γ : List Ty} (op : FUnOp) {e : Expr S Γ .float}
(eSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv e env slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.funary op e) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
simp only [Expr.code, Expr.usesFast_eq]
cases op with
| neg =>
simp only [FUnOp.code, List.cons_append, List.append_assoc, wp_f64Const_cons]
refine eSpec env slots paid live h base heap store { s with values := .f64 0x8000000000000000 ::
s.values } hh hpv (hVars.agree Frame.ofValues) hAt (hSafe.values _) hCap hBase hRoom hPlace _ _
(by rw [dyingPot_values]; exact hTrap)
fun heap1 store1 s1 ws a1 hTop => ?_
have hNext1 := fun store' s' ws a =>
hNext heap1 store' s' ws a fun hf => top_part rfl (dyingPot_values _ _ _).le (hTop hf)
have hR : ws = [.f64 (e.denote funs env).toBits] := a1.rep
subst hR
have hStep := a1.step
rw [Mode.fresh_scalar rfl] at hStep
have hv := hNext1 store1 s1 [.f64 (-(e.denote funs env)).toBits]
(After.ofValues (After.ofScalar rfl hStep rfl a1.frame a1.holds rfl))
simpa [Expr.denote, FUnOp.apply, f64Sub, F64Bits.toBits_neg] using hv
| sqrt | abs =>
simp only [FUnOp.code, List.append_assoc, List.cons_append, List.nil_append]
refine eSpec env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase
hRoom hPlace _ _
hTrap
fun heap1 store1 s1 ws a1 hTop => ?_
have hNext1 := fun store' s' ws a => hNext heap1 store' s' ws a fun hf => top_part rfl
le_rfl (hTop hf)
have hR : ws = [.f64 (e.denote funs env).toBits] := a1.rep
subst hR
have hStep := a1.step
rw [Mode.fresh_scalar rfl] at hStep
have hv := hNext1 store1 s1 [.f64 ((Expr.funary _ e).denote funs env).toBits]
(After.ofScalar rfl hStep rfl a1.frame a1.holds rfl)
simpa [Expr.denote, FUnOp.apply, f64Sqrt, f64Abs, F64Bits.toBits_sqrt, F64Bits.toBits_abs]
using hv
/-- A conversion of a word to a float. -/
theorem spec_toFloat {Γ : List Ty} (op : ToFloat) {e : Expr S Γ .word}
(eSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv e env slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.toFloat op e) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine eSpec env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom hPlace _ _
hTrap
fun heap1 store1 s1 ws a1 hTop => ?_
have hNext1 := fun store' s' ws a => hNext heap1 store' s' ws a fun hf => top_part
rfl le_rfl (hTop hf)
have hR : ws = [.i64 (e.denote funs env)] := a1.rep
subst hR
have hStep := a1.step
rw [Mode.fresh_scalar rfl] at hStep
have hv := hNext1 store1 s1 [.f64 ((Expr.toFloat op e).denote funs env).toBits]
(After.ofScalar rfl hStep rfl a1.frame a1.holds rfl)
cases op <;>
simpa [ToFloat.code, Expr.denote, ToFloat.apply, f64ConvertI64U, f64Add,
F64Convert.toBits_toFloat, F64Bits.toBits_ofBits, add_negZero] using hv
/-- A conversion of a float to a word. -/
theorem spec_toWord {Γ : List Ty} (op : ToWord) {e : Expr S Γ .float}
(eSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv e env slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.toWord op e) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine eSpec env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom hPlace _ _
hTrap
fun heap1 store1 s1 ws a1 hTop => ?_
have hNext1 := fun store' s' ws a => hNext heap1 store' s' ws a fun hf => top_part
rfl le_rfl (hTop hf)
have hR : ws = [.f64 (e.denote funs env).toBits] := a1.rep
subst hR
have hStep := a1.step
rw [Mode.fresh_scalar rfl] at hStep
have hv := hNext1 store1 s1 [.i64 ((Expr.toWord op e).denote funs env)]
(After.ofScalar rfl hStep rfl a1.frame a1.holds rfl)
cases op <;>
simpa [ToWord.instr, Expr.denote, ToWord.apply, i64TruncSatF64U, F64Convert.toUInt64_eq]
using hv
/-- The negation of a `Bool`. -/
theorem spec_not {Γ : List Ty} {e : Expr S Γ .bool}
(eSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv e env slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.not e) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc, List.cons_append, List.nil_append]
refine eSpec env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom hPlace _ _
hTrap
fun heap1 store1 s1 ws a1 hTop => ?_
have hNext1 := fun store' s' ws a => hNext heap1 store' s' ws a fun hf => top_part
rfl le_rfl (hTop hf)
have hR := a1.rep
simp only [Ty.rep_bool] at hR
subst hR
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append,
wp_eqzI64_cons, wp_extendUI32_cons, boolWord_not]
have hStep := a1.step
rw [Mode.fresh_scalar rfl] at hStep
simpa [Expr.denote] using
hNext1 store1 s1 _ (After.ofScalar rfl hStep rfl a1.frame a1.holds rfl)
/-- The conjunction of `Bool`s. -/
theorem spec_and {Γ : List Ty} {left right : Expr S Γ .bool}
(leftSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv left env slots paid live)
(rightSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv right env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.and left right) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : left.width ≤ max left.width right.width := Nat.le_max_left ..
have hR : right.width ≤ max left.width right.width := Nat.le_max_right ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec leftSpec rightSpec env slots paid live h base heap store s hh hpv
hVars hAt hSafe hCap hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
simp only [Ty.rep_bool] at hR1 hR2
subst hR1 hR2
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append,
wp_andI64_cons, boolWord_and]
simpa [Expr.denote] using hNext2 store2 s2 _ (After.ofScalar rfl hStep' rfl hF hH rfl)
/-- The disjunction of `Bool`s. -/
theorem spec_or {Γ : List Ty} {left right : Expr S Γ .bool}
(leftSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv left env slots paid live)
(rightSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv right env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.or left right) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : left.width ≤ max left.width right.width := Nat.le_max_left ..
have hR : right.width ≤ max left.width right.width := Nat.le_max_right ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec leftSpec rightSpec env slots paid live h base heap store s hh hpv
hVars hAt hSafe hCap hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
simp only [Ty.rep_bool] at hR1 hR2
subst hR1 hR2
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append,
wp_orI64_cons, boolWord_or]
simpa [Expr.denote] using hNext2 store2 s2 _ (After.ofScalar rfl hStep' rfl hF hH rfl)
/-- A tuple of two elements: the code of each, in order. -/
theorem spec_mk {Γ : List Ty} {a b : Elem} {first : Expr S Γ (.elem a)}
{second : Expr S Γ (.elem b)}
(firstSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv first env
slots paid live)
(secondSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv second env
slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.mk first second) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hL : first.width ≤ max first.width second.width := Nat.le_max_left ..
have hR : second.width ≤ max first.width second.width := Nat.le_max_right ..
simp only [Expr.placeArgs, Bool.and_eq_true] at hPlace
simp only [Expr.code, Expr.usesFast_eq, List.append_assoc]
refine seq_spec firstSpec secondSpec env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap
hBase
(by omega) (by omega) hPlace.1 hPlace.2 _ _
hTrap (by flag_tac) (by flag_tac) (by flag_tac) (by flag_tac)
fun heap2 store2 s2 ws1 ws2 hStep hF hH hR1 hR2 _ _ _ hTop => ?_
have hNext2 := fun store' s' ws a => hNext heap2 store' s' ws a (fitsOf hTop)
have hR1' : ws1 = a.values (first.denote funs env) := hR1
have hR2' : ws2 = b.values (second.denote funs env) := hR2
subst hR1' hR2'
have hStep' := hStep
rw [fresh_scalar_append rfl rfl] at hStep'
have hv := hNext2 store2 s2
(a.values (first.denote funs env) ++ b.values (second.denote funs env))
(After.ofScalar rfl hStep' rfl hF hH rfl)
simpa [List.reverse_append, List.append_assoc] using hv
/-- The release of a variable that a reader reads, when it is owned and dies there,
which leaves `top`. -/
theorem read_release (hm : Runtime m) {Γ : List Ty} {env : Env Γ} {slots : List Slot}
{live : Nat → Bool} {base : Nat} {heap : Heap} {store : Store Unit} {s : Locals}
{vals : List Value} {rest : Program} {Q : Assertion Unit} {t : Ty} (x : Var Γ t)
(hVars : Holds env slots (fun k => live k || k == x.index) base heap store s)
(hAt : heap.At store)
(hNext : ∀ heap' store', Evolves env slots (fun k => live k || k == x.index) live base heap
store s heap' store' s → heap'.top = heap.top →
wp m rest Q store' { s with values := vals } host) :
wp m ((if (slots.getD x.index default).mode = .owned ∧ live x.index = false then
releaseCode t (slots.getD x.index default).loc else []) ++ rest) Q store
{ s with values := vals } host := by
have hCode : (if (slots.getD x.index default).mode = .owned ∧ live x.index = false then
releaseCode t (slots.getD x.index default).loc else []) =
(if live x.index = false then [x.index] else []).flatMap (releaseVar Γ slots) := by
cases live x.index <;> simp [releaseVar, x.getElem?_index]
rw [hCode]
refine wp_releaseVars hm _ (by split <;> simp) (hVars.agree Frame.ofValues) hAt
(by split <;> simp) fun heap' store' e hTop => hNext heap' store' ?_ hTop
have e' := e.mono (live' := live) fun k h => by
split
· next hx =>
have hne : k ≠ x.index := fun he => by rw [he, hx] at h; exact nomatch h
simp [h, hne]
· simp [h]
exact ⟨e'.step, e'.frame.values.trans Frame.ofValues,
e'.holds.agree Frame.ofValues⟩
theorem spec_bytesSize (hm : Runtime m) {Γ' : List Ty} (x : Var Γ' .bytes) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.bytesSize (S := S) x)
env slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
obtain ⟨_, _, hold, root, length, rfl, hBytes⟩ := hVars.1 _ x (by simp [Expr.uses])
have hLength : s.get ((slots.getD x.index default).loc + 1) = some (.i64 length) := by
simpa [Ty.types, slotIndex] using hold 1 (by simp)
simp only [Expr.code, List.append_assoc, List.cons_append, List.nil_append,
wp_localGet_cons, hLength]
refine read_release hm x hVars hAt fun heap' store' ev hTop => ?_
exact hNext heap' store' s [.i64 length]
(After.ofScalar rfl ev.step rfl ev.frame ev.holds (by
simp only [Ty.rep_word, Expr.denote, hBytes.size_eq]))
fun _ => top_unpaid_le rfl (by rw [hTop]; omega)
theorem spec_bytesGet (hm : Runtime m) {Γ' : List Ty} (x : Var Γ' .bytes)
{index : Expr S Γ' .word}
(indexSpec : ∀ env slots paid live,
CodeSpec (pool := pool) m funs flags bounds fits host pv index env slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.bytesGet x index)
env slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hIndexWidth : index.width ≤ max index.width 1 := Nat.le_max_left ..
have hScratch : 1 ≤ max index.width 1 := Nat.le_max_right ..
simp only [Expr.placeArgs] at hPlace
simp only [Expr.code, List.append_assoc, List.cons_append, List.nil_append]
have hIn : ∀ k, ((live k || k == x.index) || index.uses k) = true →
(live k || (Expr.bytesGet x index).uses k) = true := fun k hk => by
simp only [Expr.uses]; exact live_assoc _ _ _ hk
have hD : dyingPot store s env slots paid (fun k => (live k || k == x.index) || index.uses k)
(fun k => live k || k == x.index) ≤
dyingPot store s env slots paid (fun k => live k || (Expr.bytesGet x index).uses k) live :=
dyingPot_mono hIn fun k hk => by simp [hk]
refine indexSpec env slots paid (fun k => live k || k == x.index) h base heap store s hh hpv
(hVars.live_mono hIn) hAt (hSafe.mono hIn) hCap hBase (by omega) hPlace _ _
(hTrap.part id id fun _ hw => hw.mono (by dsimp only; simp only [Expr.allocs]; omega))
fun heap1 store1 s1 ws1 a1 hTop1 => ?_
have hValue := a1.rep
simp only [Ty.rep_word] at hValue
subst hValue
have e1 := a1.toEvolves rfl
obtain ⟨hBelow, _, hold, root, length, rfl, hBytes⟩ := e1.holds.1 _ x (by simp)
have hRoot : s1.get (slots.getD x.index default).loc = some (.i64 root) := by
simpa [Ty.types, slotIndex] using hold 0 (by simp)
have hLength : s1.get ((slots.getD x.index default).loc + 1) = some (.i64 length) := by
simpa [Ty.types, slotIndex] using hold 1 (by simp)
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append]
have hLow : ({ s1 with values := s.values } : Locals).params.length ≤ base := by
rw [e1.frame.params]; exact hBase
have hHigh : base < s1.positions := by rw [e1.frame.positions, hh]; omega
refine wp_localSet_local hLow (Locals.lt_total hHigh) ?_
let s1a := setLocal { s1 with values := s.values } base (.i64 (index.denote funs env))
have hIndex : s1a.get base = some (.i64 (index.denote funs env)) :=
Locals.get_setLocal_same hLow (Locals.lt_total hHigh)
have hRoot' : s1a.get (slots.getD x.index default).loc = some (.i64 root) := by
rw [Locals.get_setLocal_ne hLow (by change _ + 2 ≤ base at hBelow; omega), Locals.get_values]
exact hRoot
have hLength' : s1a.get ((slots.getD x.index default).loc + 1) = some (.i64 length) := by
rw [Locals.get_setLocal_ne hLow (by change _ + 2 ≤ base at hBelow; omega), Locals.get_values]
exact hLength
have f1 : Frame base s1 s1a := Frame.setValues hLow le_rfl hHigh
have hFrame : Frame base s s1a := e1.frame.trans f1
have hVars1 := e1.holds.agree f1
show wp m _ Q store1 s1a host
refine Bytes.wp_get e1.step.at_ hBytes hRoot' hLength' hIndex ?_
refine read_release hm x hVars1 e1.step.at_ fun heap2 store2 e2 hTop2 => ?_
have e12 := Evolves.trans (hVars.live_mono hIn) ⟨e1.step, hFrame, hVars1⟩ e2
(fun k hk => by simp [hk]) fun k hk => by simp [hk]
exact hNext heap2 store2 s1a [.i64 ((env.get x).get (index.denote funs env))]
((After.ofScalar (t := .word) (mode := .borrowed) rfl e12.step rfl e12.frame e12.holds
rfl).liveIn hIn)
fun hd => by rw [hTop2]; exact top_part rfl hD (hTop1 hd)
/-- The size of an array variable: the load of its length word, then the release of the variable
when it is owned and dies there. -/
theorem spec_size (hm : Runtime m) {Γ' : List Ty} {e : Elem} (x : Var Γ' (.array e)) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.size (S := S) x) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
obtain ⟨-, ws, hold, hRep⟩ := hVars.1 _ x (by simp [Expr.uses])
obtain ⟨ptr, rfl, hB⟩ := hRep
have hA := hB.borrow.values
have hLength := hA.lengthBound
have hPtr : s.get (slots.getD x.index default).loc = some (.i64 ptr) := by
exact LocalsHold.word hold
have hnk : (env.get x).size * e.width < 536870912 := by
have := hA.1; rw [Elem.words_size] at this; omega
simp only [Expr.code, Var.sizeCode, Expr.usesFast_eq, List.append_assoc, List.cons_append,
List.nil_append]
simp only [wp_localGet_cons, hPtr, wp_wrapI64_cons, wp_load64_cons, wrap_toUInt32,
UInt32.toNat_zero, Nat.add_zero, UInt32.add_zero]
rw [ite_eq_right (by omega), hA.lengthRead, Elem.words_size, wp_divCode e.width_pos,
words_div e.width_pos hnk]
refine read_release hm x hVars hAt fun heap' store' ev hTop => ?_
exact hNext heap' store' s [.i64 (env.get x).size.toUInt64]
(After.ofScalar rfl ev.step rfl ev.frame ev.holds rfl)
fun _ => top_unpaid_le rfl (by rw [hTop]; omega)
theorem spec_leSize (hm : Runtime m) {Γ' : List Ty} {e : Elem}
(x : Var Γ' (.array e)) (i : Var Γ' .word) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv
(Expr.leSize (S := S) x i) env slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap hNext
obtain ⟨_, _, _, hRep⟩ := hVars.1 _ x (by simp [Expr.uses])
have hValue := Expr.leSizeExpand_denote x i funs env hRep.array_size_lt
have hCmp : CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.leSizeExpand x i)
env slots paid live := spec_cmp .le (spec_var hm i) (spec_size hm x) env slots paid live
simp only [CodeSpec, trapFlag, Expr.leSizeExpand_uses, Expr.leSizeExpand_width,
Expr.leSizeExpand_placeArgs, Expr.leSizeExpand_aborts, Expr.leSizeExpand_allocs,
Expr.leSizeExpand_fits, Expr.leSizeExpand_mode, Expr.leSizeExpand_paid,
Expr.leSizeExpand_code, hValue] at hCmp
exact hCmp h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom hPlace rest Q hTrap hNext
/-- A read of an array variable: the position in local `base`, a comparison of it with the
array's size as a flag in local `base + 1`, the position of the element's first word in local
`base`, the loads of its words under the flag, and the release of the variable when it is owned and
dies there. -/
theorem spec_get (hm : Runtime m) {Γ' : List Ty} {e : Elem} (x : Var Γ' (.array e))
{i : Expr S Γ' .word}
(iSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv i env slots paid live) :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.get x i) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hi : i.width ≤ max i.width 2 := Nat.le_max_left ..
have h2 : 2 ≤ max i.width 2 := Nat.le_max_right ..
simp only [Expr.placeArgs] at hPlace
simp only [Expr.code, getCode, Expr.usesFast_eq, List.append_assoc, List.cons_append,
List.nil_append]
have hIn : ∀ k, ((live k || k == x.index) || i.uses k) = true →
(live k || (Expr.get x i).uses k) = true := fun k h => by
simp only [Expr.uses]; exact live_assoc _ _ _ h
have hD : dyingPot store s env slots paid (fun k => (live k || k == x.index) || i.uses k)
(fun k => live k || k == x.index) ≤
dyingPot store s env slots paid (fun k => live k || (Expr.get x i).uses k) live :=
dyingPot_mono hIn fun k h => by simp [h]
refine iSpec env slots paid (fun k => live k || k == x.index) h base heap store s hh hpv
(hVars.live_mono hIn) hAt (hSafe.mono hIn) hCap hBase (by omega) hPlace _ _
(hTrap.part id id fun _ hw => hw.mono (by dsimp only; simp only [Expr.allocs]; omega))
fun heap1 store1 s1 ws1 a1 hTop1 => ?_
have hR1 := a1.rep
simp only [Ty.rep_word] at hR1
subst hR1
have e1 := a1.toEvolves rfl
have hp1 : s1.params = s.params := e1.frame.params
have hh1 : s1.positions = h := e1.frame.positions.trans hh
obtain ⟨hBelowX, wx, holdx, hRepx⟩ := e1.holds.1 _ x (by simp)
obtain ⟨ptr, rfl, hBx⟩ := hRepx
have hA := hBx.borrow.values
have hLength := hA.lengthBound
have hnk : (env.get x).size * e.width < 536870912 := by
have := hA.1; rw [Elem.words_size] at this; omega
have hk := e.width_pos
have hn64 : (env.get x).size < UInt64.size := by
have := Nat.le_mul_of_pos_right (env.get x).size hk
rw [show UInt64.size = 18446744073709551616 from rfl]; omega
simp only [Ty.width] at hBelowX
have hPtr1 : s1.get (slots.getD x.index default).loc = some (.i64 ptr) := by
exact LocalsHold.word holdx
simp only [List.reverse_cons, List.reverse_nil, List.nil_append, List.cons_append]
-- The position, in local `base`.
have hLow1 : ({ s1 with values := s.values } : Locals).params.length ≤ base := by
show s1.params.length ≤ base; rw [hp1]; omega
have hHigh1 : base < s1.positions := by rw [hh1]; omega
refine wp_localSet_local hLow1 (Locals.lt_total hHigh1) ?_
let s1a := setLocal { s1 with values := s.values } base (.i64 (i.denote funs env))
have hK : s1a.get base = some (.i64 (i.denote funs env)) :=
Locals.get_setLocal_same hLow1 (Locals.lt_total hHigh1)
have hP : s1a.get (slots.getD x.index default).loc = some (.i64 ptr) := by
rw [Locals.get_setLocal_ne hLow1 (by omega)]
exact hPtr1
show wp m _ Q store1 s1a host
simp only [wp_localGet_cons, Locals.get_values, hK, hP, wp_wrapI64_cons, wp_load64_cons,
wrap_toUInt32, UInt32.toNat_zero, Nat.add_zero, UInt32.add_zero]
rw [ite_eq_right (by omega), hA.lengthRead, Elem.words_size, wp_divCode hk,
words_div hk hnk]
simp only [wp_ltUI64_cons, wp_extendUI32_cons]
-- The flag, in local `base + 1`.
have hLess : i.denote funs env < UInt64.ofNat (env.get x).size ↔
(i.denote funs env).toNat < (env.get x).size := by
rw [UInt64.lt_iff_toNat_lt, UInt64.toNat_ofNat_of_lt' hn64]
set b := decide ((i.denote funs env).toNat < (env.get x).size) with hb
have hFlag : UInt64.ofNat (if i.denote funs env < UInt64.ofNat (env.get x).size then (1 : UInt32)
else 0).toNat = boolWord b := by
by_cases hc : (i.denote funs env).toNat < (env.get x).size
· simp [hb, hc, hLess.mpr hc, boolWord]
· simp [hb, hc, (not_congr hLess).mpr hc, boolWord]
rw [hFlag]
have hLowB : ({ s1a with values := s.values } : Locals).params.length ≤ base + 1 := by
show s1.params.length ≤ base + 1; rw [hp1]; omega
have hHighB : base + 1 < ({ s1a with values := s.values } : Locals).positions := by
simp only [s1a, Locals.positions_setLocal, Locals.positions_values, hh1]; omega
refine wp_localSet_local hLowB (Locals.lt_total hHighB) ?_
let s1b := setLocal { s1a with values := s.values } (base + 1) (.i64 (boolWord b))
have hFb : s1b.get (base + 1) = some (.i64 (boolWord b)) :=
Locals.get_setLocal_same hLowB (Locals.lt_total hHighB)
have hKb : s1b.get base = some (.i64 (i.denote funs env)) := by
rw [Locals.get_setLocal_ne hLowB (by omega), Locals.get_values]; exact hK
have hPb : s1b.get (slots.getD x.index default).loc = some (.i64 ptr) := by
rw [Locals.get_setLocal_ne hLowB (by omega), Locals.get_values]; exact hP
-- The position of the element's first word, in local `base`.
show wp m _ Q store1 s1b host
simp only [wp_localGet_cons, hKb]
rw [wp_scaleCode]
have hLowC : ({ s1b with values := s.values } : Locals).params.length ≤ base := by
show s1.params.length ≤ base; rw [hp1]; omega
have hHighC : base < ({ s1b with values := s.values } : Locals).positions := by
simp only [s1b, s1a, Locals.positions_setLocal, Locals.positions_values, hh1]; omega
refine wp_localSet_local hLowC (Locals.lt_total hHighC) ?_
let s1c := setLocal { s1b with values := s.values } base
(.i64 (i.denote funs env * wordCount e.width))
have hWc : s1c.get base = some (.i64 (i.denote funs env * wordCount e.width)) :=
Locals.get_setLocal_same hLowC (Locals.lt_total hHighC)
have hFc : s1c.get (base + 1) = some (.i64 (boolWord b)) := by
rw [Locals.get_setLocal_ne hLowC (by omega), Locals.get_values]; exact hFb
have hPc : s1c.get (slots.getD x.index default).loc = some (.i64 ptr) := by
rw [Locals.get_setLocal_ne hLowC (by omega), Locals.get_values]; exact hPb
have f1 : Frame base s1 s1c :=
((Frame.setValues (s := s1) (vs := s.values) hLow1 le_rfl hHigh1).trans
(Frame.setValues (s := s1a) (vs := s.values) hLowB (by omega)
(by simpa [s1a, Locals.positions_setLocal, Locals.positions_values] using hHighB))).trans
(Frame.setValues (s := s1b) (vs := s.values) hLowC le_rfl
(by simpa [s1b, s1a, Locals.positions_setLocal, Locals.positions_values] using hHighC))
have hFrame : Frame base s s1c := e1.frame.trans f1
have hVars1 : Holds env slots (fun k => live k || k == x.index) base heap1 store1 s1c :=
e1.holds.agree f1
have hsc : s1c.values = s.values := rfl
-- The words of the element, or zeros past the end.
show wp m _ Q store1 s1c host
refine wp_loadWordsCode (ws := e.words (env.get x)) b (fun _ => hA) 0 e.types s1c
(e.toWords (env.get x)[(i.denote funs env).toNat]!) hPc hWc hFc
(by rw [e.toWords_length, e.types_length]) (fun hbt => ?_) (fun hbf => ?_) ?_
· have hc : (i.denote funs env).toNat < (env.get x).size := by simpa [hb] using hbt
have hw : (i.denote funs env * wordCount e.width).toNat =
(i.denote funs env).toNat * e.width := by
have hs := words_scale (i := (i.denote funs env).toNat) hk
(lt_of_le_of_lt (Nat.mul_le_mul_right e.width (Nat.le_of_lt hc)) hnk)
rw [UInt64.ofNat_toNat] at hs
rw [hs, UInt64.toNat_ofNat_of_lt' (by
rw [show UInt64.size = 18446744073709551616 from rfl]
have := Nat.mul_le_mul_right e.width (Nat.le_of_lt hc); omega)]
rw [hw, e.types_length, Elem.words_size]
refine ⟨by
have := Nat.mul_le_mul_right e.width (Nat.succ_le_of_lt hc)
rw [Nat.succ_mul] at this; omega,
fun j hj => ?_⟩
rw [e.toWords_length] at hj
rw [Nat.add_zero, Elem.words_getElem! e _ hc hj, getElem!_pos (env.get x) _ hc]
· have hc : ¬(i.denote funs env).toNat < (env.get x).size := by simpa [hb] using hbf
rw [getElem!_neg (env.get x) _ hc, e.toWords_default, e.types_length]
· rw [e.values_typed, hsc]
refine read_release hm x hVars1 e1.step.at_ fun heap2 store2 e2 hTop2 => ?_
have e12 := Evolves.trans (hVars.live_mono hIn) ⟨e1.step, hFrame, hVars1⟩ e2
(fun k h => by simp [h]) fun k h => by simp [h]
exact hNext heap2 store2 s1c (e.values (env.get x)[(i.denote funs env).toNat]!)
((After.ofScalar (t := .elem e) (mode := .borrowed) rfl e12.step rfl e12.frame e12.holds
rfl).liveIn hIn) fun hd => by rw [hTop2]; exact top_part rfl hD (hTop1 hd)
/-- A component of a tuple variable: the loads of the words that hold it. -/
theorem spec_proj {Γ' : List Ty} {e e' : Elem} (x : Var Γ' (.elem e)) (p : Path e e') :
∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv (Expr.proj (S := S) x p) env
slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt _ _ _ _ _ rest Q _ hNext
obtain ⟨-, ws, hold, hRep⟩ := hVars.1 _ x (by simp [Expr.uses])
have hRep' : ws = e.values (env.get x) := hRep
subst hRep'
simp only [Expr.code, Expr.usesFast_eq]
exact wp_loadCode _ (by rw [e'.values_length, e'.types_length]) hh (p.holds hold)
(hNext heap store s _ (After.refl hAt hVars (by intro i h; simp [Expr.uses, h]) rfl rfl
(Holds.Apart.ofScalar rfl)) fun _ => Nat.le_add_right _ _)
/-- One branch of an `if`, after the condition: the releases of the owned variables that die at
its entry, its code, the coercion of its value to the `if`'s mode, and the store of its words,
which the code loads after the `if`. -/
theorem ite_branch (hm : Runtime m) {Γ' : List Ty} {tTy : Ty} {c : Expr S Γ' .bool}
{thenE elseE b : Expr S Γ' tTy} {buy : Bool}
(bSpec : ∀ env slots paid live,
CodeSpecIn (pool := pool) buy m funs flags bounds fits host pv b env slots paid live)
{env : Env Γ'} {slots : List Slot} {paid : List Bool} {live : Nat → Bool} {h base : Nat}
{heap : Heap} {store : Store Unit} {s : Locals} {rest : Program} {Q : Assertion Unit}
(hh : s.positions = h) (hpv : s.params = pv)
(hVars : Holds env slots (fun i => live i || (Expr.ite c thenE elseE).uses i) base heap
store s)
(hSafe : pool.Safe env slots (fun i => live i || (Expr.ite c thenE elseE).uses i)
heap store s)
(hCap : store.memoryCap m 0 ≤ 65535) (hBase : s.params.length ≤ base)
(hRoomB : base + tTy.width + max b.width (copyWidth tTy) ≤ h)
{B cA Db K : Nat} {wf P : Bool}
(hTrap : TrapOK (trapFlag m (Expr.ite c thenE elseE) slots heap store B wf) Q)
(hNext : ∀ heap' store' s' ws,
After env slots (fun i => live i || (Expr.ite c thenE elseE).uses i) live base heap store s
tTy ((Expr.ite c thenE elseE).mode (slots.map Slot.mode))
((Expr.ite c thenE elseE).denote funs env) heap' store' s' ws →
(wf = true → heap'.top.toNat +
(if P = true then tTy.pot store' ws ((Expr.ite c thenE elseE).denote funs env) else 0) ≤
heap.top.toNat + B) →
wp m rest Q store' { s' with values := ws.reverse ++ s.values } host)
{heap1 : Heap} {store1 : Store Unit} {s1 : Locals}
(e1 : Evolves env slots (fun i => live i || (Expr.ite c thenE elseE).uses i)
(fun i => live i || thenE.uses i || elseE.uses i) base heap store s heap1 store1
{ s1 with values := s.values })
(hTop1 : wf = true → heap1.top.toNat ≤ heap.top.toNat + cA)
(hB : B = cA + (Db + (b.allocs funs flags bounds (slots.map Slot.mode) paid live env +
coerceCost tTy (b.mode (slots.map Slot.mode))
((Expr.ite c thenE elseE).mode (slots.map Slot.mode)) (b.denote funs env) +
(b.buysIn buy funs flags (slots.map Slot.mode) paid live env + K))))
(hDb : dyingPot store s env slots paid (fun i => live i || b.uses i) live ≤ Db)
(hbd : wf = true → b.fits funs fits env = true)
(hbPlace : b.placeArgs = true)
(hbu : ∀ i, b.uses i = true → (thenE.uses i || elseE.uses i) = true)
(hba : b.aborts = true → (Expr.ite c thenE elseE).aborts = true)
(hval : b.denote funs env = (Expr.ite c thenE elseE).denote funs env)
(hbm : b.mode (slots.map Slot.mode) = .owned →
(Expr.ite c thenE elseE).mode (slots.map Slot.mode) = .owned)
(hPm : P = true → (Expr.ite c thenE elseE).mode (slots.map Slot.mode) = .owned)
(hK : P = true → b.paidIn buy flags (slots.map Slot.mode) paid live = false →
tTy.credit (b.denote funs env) ≤ K)
(Qb : Assertion Unit) (hQbTrap : ∀ st, Q (.Trap st "unreachable") → Qb (.Trap st "unreachable"))
(hQb : ∀ st' s', wp m (loadCode h base tTy.types ++ rest) Q st'
{ s' with values := s.values } host → Qb (.Fallthrough st' s')) :
wp m (releaseWhere Γ' slots (fun i => (live i || thenE.uses i || elseE.uses i) &&
!(live i || b.uses i)) ++
(b.code (pool := pool) h slots (base + tTy.width) live ++
(coerceCode h tTy (b.mode (slots.map Slot.mode))
((Expr.ite c thenE elseE).mode (slots.map Slot.mode)) (base + tTy.width) ++
storeCode h base tTy.types))) Qb store1 { s1 with values := s.values } host := by
have hp1 : s1.params = s.params := e1.frame.params
have hh1 : s1.positions = h := e1.frame.positions.trans hh
have hTrapB : TrapOK (trapFlag m (Expr.ite c thenE elseE) slots heap store B wf) Qb :=
hTrap.imp hQbTrap
have hwf : (!wf) = false → wf = true := fun hd => by simpa using hd
have hbd' : (!wf) = false → b.fits funs fits env = true := fun hd => hbd (hwf hd)
have hSubMid : ∀ i, (live i || b.uses i) = true →
(live i || thenE.uses i || elseE.uses i) = true := fun i =>
ite_live_branch _ _ _ _ (hbu i)
refine wp_releaseWhere hm e1.holds e1.step.at_
(fun i h => by simp only [Bool.and_eq_true] at h; exact h.1) fun heap2 store2 e2 hTop2 => ?_
have hEq : (fun i => (live i || thenE.uses i || elseE.uses i) &&
!((live i || thenE.uses i || elseE.uses i) && !(live i || b.uses i))) =
(fun i => live i || b.uses i) := by
funext i
exact live_released _ _ (hSubMid i)
rw [hEq] at e2
have hEntry : ∀ i, (live i || thenE.uses i || elseE.uses i) = true →
(live i || (Expr.ite c thenE elseE).uses i) = true := fun i h => by
simp only [Expr.uses]; exact ite_live_entry _ _ _ _ h
-- The variables that die in the branch keep their potential across the condition and the
-- releases.
have hDb2 : dyingPot store2 { s1 with values := s.values } env slots paid
(fun i => live i || b.uses i) live =
dyingPot store s env slots paid (fun i => live i || b.uses i) live := by
rw [dyingPot_evolves e1.holds e2 hSubMid _ _ fun _ h => h,
dyingPot_evolves hVars e1 hEntry _ _ hSubMid]
rw [← List.append_nil (storeCode h base tTy.types)]
have hbw := Nat.le_max_left b.width (copyWidth tTy)
have hcw := Nat.le_max_right b.width (copyWidth tTy)
refine bSpec env slots paid live h (base + tTy.width) heap2 store2 { s1 with values := s.values }
hh1 (hp1.trans hpv) (e2.holds.mono (by omega)) e2.step.at_
((hSafe.evolves hVars e1 hEntry).evolves e1.holds e2 hSubMid)
(by rw [e2.step.cap m, e1.step.cap m]; exact hCap)
(by show s1.params.length ≤ base + tTy.width; rw [hp1]; omega)
(by omega) hbPlace _ _ (hTrapB.part (fun h => by
cases hw : wf
· rfl
· simp [hbd hw] at h) (fun h => by
simp only [Bool.or_eq_true] at h ⊢; exact h.imp_left hba) fun hd hw => by
rw [hB] at hw
exact (hw.after (heap1 := heap2) (store1 := store2)
(by have := hTop1 (hwf hd); have := congrArg UInt64.toNat hTop2; omega)
(by rw [e2.step.cap m, e1.step.cap m])).mono (by rw [hDb2]; omega))
fun heap3 store3 s3 ws3 a3 hTop3 => ?_
have hp3 : s3.params = s.params := a3.frame.params.trans hp1
have hh3 : s3.positions = h := a3.frame.positions.trans hh1
refine After.coerce hm a3 hbm le_rfl (by rw [hp3]; omega) hh3 (by omega)
(by rw [a3.step.cap m, e2.step.cap m, e1.step.cap m]; exact hCap)
(fun hs ht _ => hTrapB.alloc (by
have := (Expr.ite c thenE elseE).mode_owned (slots.map Slot.mode) ht
rw [Slot.any_map] at this; exact this) fun hd hw => by
have h3 := hw.shift (heap1 := heap3) (store1 := store3)
(c1 := cA + Db + b.allocs funs flags bounds (slots.map Slot.mode) paid live env +
b.buysIn buy funs flags (slots.map Slot.mode) paid live env)
(c2 := coerceCost tTy (b.mode (slots.map Slot.mode))
((Expr.ite c thenE elseE).mode (slots.map Slot.mode)) (b.denote funs env))
(by have := hTop3 (hbd' hd); have := hTop1 (hwf hd); have := congrArg UInt64.toNat hTop2
omega)
(by rw [hB]; omega)
(by rw [a3.step.cap m, e2.step.cap m, e1.step.cap m])
simpa [coerceCost, hs, ht] using h3)
fun heap4 store4 s4 ws4 a4 hTop4 hSame4 => ?_
have hp4 : s4.params = s.params := a4.frame.params.trans hp1
have hh4 : s4.positions = h := a4.frame.positions.trans hh1
refine wp_storeCode ws4 h base tTy.types s4 s.values a4.rep.typed hh4 (by rw [hp4]; omega)
(by rw [Ty.types_length, hh4]; omega) fun s5 hF5 hold _ => ?_
rw [wp_nil]
refine hQb _ _ (wp_loadCode ws4 a4.rep.typed (hF5.positions.trans hh4) hold ?_)
-- The `if`'s facts: the evolutions of the condition and the releases, then the branch.
have hLower := a4.lower e2.holds (Nat.le_add_right _ _) (fun i h => by simp [h])
have e12 := Evolves.trans hVars e1 e2 hSubMid hEntry
have aAll := After.prepend hVars e12 hLower (fun i h => hEntry i (hSubMid i h))
(fun i h => by simp [h])
have hF45 : Frame base s4 { s5 with values := s.values } := hF5.trans Frame.ofValues
have hFrame : Frame base s { s5 with values := s.values } := aAll.frame.trans hF45
rw [hval] at aAll
refine hNext heap4 store4 _ ws4 ⟨aAll.step, hFrame, aAll.holds.agree hF45, aAll.rep, ?_⟩
fun hd => by
have := hTop1 hd; have := hTop3 (hbd hd); have := congrArg UInt64.toNat hTop2
-- A paid branch keeps its owned value through the coercion. The charge `K` of an unpaid
-- branch of a paid `if` bounds the potential of the owned result.
have hPot : (if P = true then
tTy.pot store4 ws4 ((Expr.ite c thenE elseE).denote funs env) else 0) ≤
(if b.paidIn buy flags (slots.map Slot.mode) paid live = true then
tTy.pot store3 ws3 (b.denote funs env) else 0) + K := by
by_cases hP : P = true
· by_cases hbP : b.paidIn buy flags (slots.map Slot.mode) paid live = true
· obtain ⟨rfl, rfl⟩ := hSame4 (Expr.mode_of_paidIn b hbP)
rw [ite_eq_left hP, ite_eq_left hbP, ← hval]
omega
· have hRep4 := a4.rep
rw [hPm hP] at hRep4
have hle := Ty.pot_le_credit tTy ws4 _ hRep4
have hk := hK hP (by simpa using hbP)
rw [ite_eq_left hP, ite_eq_right hbP, ← hval]
omega
· rw [ite_eq_right hP]
omega
rw [hB]; omega
exact aAll.apart.agree aAll.holds hF45
/-- An `if`: the condition, then each branch as `ite_branch` describes. -/
theorem spec_ite (hm : Runtime m) {Γ' : List Ty} {tTy : Ty} {c : Expr S Γ' .bool}
{thenE elseE : Expr S Γ' tTy} {buy : Bool}
(cSpec : ∀ env slots paid live, CodeSpec (pool := pool) m funs flags bounds fits host pv c env slots paid live)
(thenSpec : ∀ env slots paid live, CodeSpecIn (pool := pool) buy m funs flags bounds fits host pv
thenE env slots paid live)
(elseSpec : ∀ env slots paid live, CodeSpecIn (pool := pool) buy m funs flags bounds fits host pv
elseE env slots paid live) :
∀ env slots paid live, CodeSpecIn (pool := pool) buy m funs flags bounds fits host pv
(Expr.ite c thenE elseE) env slots paid live := by
intro env slots paid live h base heap store s hh hpv hVars hAt hSafe hCap hBase hRoom
hPlace rest Q hTrap
hNext
have hWidth := hRoom
simp only [Expr.width] at hWidth
have hc : c.width ≤ max c.width (tTy.width + max (max thenE.width elseE.width)
(copyWidth tTy)) := Nat.le_max_left ..
have hb : tTy.width + max (max thenE.width elseE.width) (copyWidth tTy) ≤
max c.width (tTy.width + max (max thenE.width elseE.width) (copyWidth tTy)) :=
Nat.le_max_right ..
have ht : thenE.width ≤ max thenE.width elseE.width := Nat.le_max_left ..