-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathContract.lean
More file actions
3250 lines (3176 loc) · 160 KB
/
Contract.lean
File metadata and controls
3250 lines (3176 loc) · 160 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 Compiler.Proofs.IRGeneration.Dispatch
namespace Compiler.Proofs.IRGeneration
open Compiler
open Compiler.CompilationModel
open Compiler.Yul
namespace Contract
private theorem pickUniqueFunctionByName_eq_ok_none_of_absent
(name : String) (funcs : List FunctionSpec)
(habsent : ∀ fn ∈ funcs, fn.name != name) :
pickUniqueFunctionByName name funcs = Except.ok none := by
induction funcs with
| nil =>
rfl
| cons fn rest ih =>
have hfn : (fn.name == name) = false := by
by_cases heq : fn.name = name
· have habs := habsent fn (by simp)
simp [heq] at habs
· simp [heq]
have hrest : ∀ fn' ∈ rest, fn'.name != name := by
intro fn' hmem
exact habsent fn' (by simp [hmem])
have ih' := ih hrest
simpa [pickUniqueFunctionByName, hfn] using ih'
private theorem compiled_functions_forall₂_of_mapM_ok
(fields : List Field)
(events : List EventDef)
(errors : List ErrorDef) :
∀ (entries : List (FunctionSpec × Nat)) irFns,
(entries.mapM fun (entry : FunctionSpec × Nat) =>
compileFunctionSpec fields events errors entry.2 entry.1) = Except.ok irFns →
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec fields events errors entry.2 entry.1 = Except.ok irFn)
entries irFns := by
intro entries
induction entries with
| nil =>
intro irFns hmap
cases hmap
simp
| cons entry entries ih =>
intro irFns hmap
rcases hstep : compileFunctionSpec fields events errors entry.2 entry.1 with _ | irFn
· simp only [List.mapM_cons, hstep, bind, Except.bind] at hmap
cases hmap
· rcases htail : List.mapM
(fun (entry : FunctionSpec × Nat) =>
compileFunctionSpec fields events errors entry.2 entry.1) entries with _ | irFnsTail
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
exact List.Forall₂.cons hstep (ih _ htail)
private theorem compiled_internal_functions_forall₂_of_mapM_ok
(fields : List Field)
(events : List EventDef)
(errors : List ErrorDef) :
∀ (entries : List FunctionSpec) internalDefs,
(entries.mapM (compileInternalFunction fields events errors)) =
Except.ok internalDefs →
List.Forall₂
(fun fn internalDef =>
compileInternalFunction fields events errors fn = Except.ok internalDef)
entries internalDefs := by
intro entries
induction entries with
| nil =>
intro internalDefs hmap
cases hmap
simp
| cons entry entries ih =>
intro internalDefs hmap
rcases hstep : compileInternalFunction fields events errors entry with _ | internalDef
· simp only [List.mapM_cons, hstep, bind, Except.bind] at hmap
cases hmap
· rcases htail :
List.mapM (compileInternalFunction fields events errors) entries with _ | internalDefsTail
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
exact List.Forall₂.cons hstep (ih _ htail)
private theorem exists_right_of_forall₂_mem_left
{α β : Type}
{R : α → β → Prop}
{xs : List α}
{ys : List β}
(hrel : List.Forall₂ R xs ys)
{x : α}
(hmem : x ∈ xs) :
∃ y, y ∈ ys ∧ R x y := by
induction hrel with
| nil =>
cases hmem
| @cons headX headY tailX tailY hhead htail ih =>
simp only [List.mem_cons] at hmem
rcases hmem with rfl | hmemTail
· exact ⟨headY, by simp, hhead⟩
· rcases ih hmemTail with ⟨y, hy, hRy⟩
exact ⟨y, by simp [hy], hRy⟩
-- SORRY'D: induction fields with
-- SORRY'D: | nil =>
-- SORRY'D: simp [findFieldWithResolvedSlot] at hfind
-- SORRY'D: | cons field rest ih =>
-- SORRY'D: simp [findFieldWithResolvedSlot] at hfind ⊢
-- SORRY'D: by_cases hname : field.name == fieldName
-- SORRY'D: · injection hfind with hfield _
-- SORRY'D: subst hfield
-- SORRY'D: simp [hname]
-- SORRY'D: · simp [hname] at hfind
-- SORRY'D: exact Or.inr (ih hfind)
private theorem legacyCompatibleExternalStmtList_append
(before : List YulStmt)
(after : List YulStmt)
(hbefore : LegacyCompatibleExternalStmtList before)
(hafter : LegacyCompatibleExternalStmtList after) :
LegacyCompatibleExternalStmtList (before ++ after) := by
revert after hafter
induction hbefore with
| nil => intro after hafter; simpa using hafter
| comment msg rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.comment msg (rest ++ after) (ih after hafter)
| let_ name value rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.let_ name value (rest ++ after) (ih after hafter)
| assign name value rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.assign name value (rest ++ after) (ih after hafter)
| expr value rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.expr value (rest ++ after) (ih after hafter)
| if_ cond body rest hbody hrest ihBody ihRest =>
intro after hafter
simpa using LegacyCompatibleExternalStmtList.if_ cond body (rest ++ after) hbody (ihRest after hafter)
| block body rest hbody hrest ihBody ihRest =>
intro after hafter
simpa using LegacyCompatibleExternalStmtList.block body (rest ++ after) hbody (ihRest after hafter)
| funcDef name params rets body rest hbody hrest ihBody ihRest =>
intro after hafter
simpa using LegacyCompatibleExternalStmtList.funcDef name params rets body (rest ++ after) hbody (ihRest after hafter)
-- SORRY'D: induction hprefix generalizing suffix with
-- SORRY'D: | nil =>
-- SORRY'D: simpa using hsuffix
-- SORRY'D: | comment msg rest hrest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.comment msg (rest ++ suffix) (ih hsuffix)
-- SORRY'D: | let_ name value rest hrest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.let_ name value (rest ++ suffix) (ih hsuffix)
-- SORRY'D: | assign name value rest hrest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.assign name value (rest ++ suffix) (ih hsuffix)
-- SORRY'D: | expr value rest hrest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.expr value (rest ++ suffix) (ih hsuffix)
-- SORRY'D: | if_ cond body rest hbody hrest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.if_ cond body (rest ++ suffix) hbody (ih hsuffix)
-- SORRY'D: | block body rest hbody hrest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.block body (rest ++ suffix) hbody (ih hsuffix)
-- SORRY'D: | funcDef name params rets body rest hbody hrest ih =>
-- SORRY'D: simpa using
-- SORRY'D: LegacyCompatibleExternalStmtList.funcDef name params rets body (rest ++ suffix) hbody (ih hsuffix)
private theorem legacyCompatibleExternalStmtList_of_exprStmtExprs
(exprs : List YulExpr) :
LegacyCompatibleExternalStmtList (exprs.map YulStmt.expr) := by
induction exprs with
| nil =>
exact LegacyCompatibleExternalStmtList.nil
| cons expr rest ih =>
simpa using LegacyCompatibleExternalStmtList.expr expr (rest.map YulStmt.expr) ih
-- SORRY'D: induction exprs with
-- SORRY'D: | nil =>
-- SORRY'D: exact LegacyCompatibleExternalStmtList.nil
-- SORRY'D: | cons expr rest ih =>
-- SORRY'D: simpa using LegacyCompatibleExternalStmtList.expr expr (rest.map YulStmt.expr) ih
private theorem legacyCompatibleExternalStmtList_revertWithMessage
(message : String) :
LegacyCompatibleExternalStmtList (CompilationModel.revertWithMessage message) := by
unfold CompilationModel.revertWithMessage
let headerExprs :=
[ YulExpr.call "mstore" [YulExpr.lit 0, YulExpr.hex errorStringSelectorWord]
, YulExpr.call "mstore" [YulExpr.lit 4, YulExpr.lit 32]
, YulExpr.call "mstore"
[YulExpr.lit 36, YulExpr.lit (CompilationModel.bytesFromString message).length]
]
let dataExprs :=
(((CompilationModel.chunkBytes32 (CompilationModel.bytesFromString message)).zipIdx).map
(fun (chunk, idx) =>
let offset := 68 + idx * 32
let word := CompilationModel.wordFromBytes chunk
YulExpr.call "mstore" [YulExpr.lit offset, YulExpr.hex word]))
let revertStmt :=
YulStmt.expr
(YulExpr.call "revert"
[ YulExpr.lit 0
, YulExpr.lit
(68 + (((CompilationModel.bytesFromString message).length + 31) / 32) * 32)
])
simpa [headerExprs, dataExprs, revertStmt, List.append_assoc] using
legacyCompatibleExternalStmtList_append
(before := headerExprs.map YulStmt.expr)
(after := dataExprs.map YulStmt.expr ++ [revertStmt])
(legacyCompatibleExternalStmtList_of_exprStmtExprs headerExprs)
(legacyCompatibleExternalStmtList_append
(before := dataExprs.map YulStmt.expr)
(after := [revertStmt])
(legacyCompatibleExternalStmtList_of_exprStmtExprs dataExprs)
(LegacyCompatibleExternalStmtList.expr
(YulExpr.call "revert"
[ YulExpr.lit 0
, YulExpr.lit
(68 + (((CompilationModel.bytesFromString message).length + 31) / 32) * 32)
])
[]
LegacyCompatibleExternalStmtList.nil))
-- SORRY'D: unfold CompilationModel.revertWithMessage
-- SORRY'D: apply legacyCompatibleExternalStmtList_append
-- SORRY'D: · exact legacyCompatibleExternalStmtList_of_exprStmtExprs
-- SORRY'D: [ YulExpr.call "mstore" [YulExpr.lit 0, YulExpr.hex errorStringSelectorWord]
-- SORRY'D: , YulExpr.call "mstore" [YulExpr.lit 4, YulExpr.lit 32]
-- SORRY'D: , YulExpr.call "mstore" [YulExpr.lit 36, YulExpr.lit (CompilationModel.bytesFromString message).length]
-- SORRY'D: ]
-- SORRY'D: · apply legacyCompatibleExternalStmtList_append
-- SORRY'D: · exact legacyCompatibleExternalStmtList_of_exprStmtExprs
-- SORRY'D: (((CompilationModel.chunkBytes32 (CompilationModel.bytesFromString message)).zipIdx).map
-- SORRY'D: (fun (chunk, idx) =>
-- SORRY'D: let offset := 68 + idx * 32
-- SORRY'D: let word := CompilationModel.wordFromBytes chunk
-- SORRY'D: YulExpr.call "mstore" [YulExpr.lit offset, YulExpr.hex word]))
-- SORRY'D: · exact LegacyCompatibleExternalStmtList.expr
-- SORRY'D: (YulExpr.call "revert"
-- SORRY'D: [ YulExpr.lit 0
-- SORRY'D: , YulExpr.lit
-- SORRY'D: (68 + (((CompilationModel.bytesFromString message).length + 31) / 32) * 32)
-- SORRY'D: ])
-- SORRY'D: []
-- SORRY'D: LegacyCompatibleExternalStmtList.nil
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileSetStorage_ok_of_noPackedFields
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {fieldName : String}
-- TYPESIG_SORRY: {value : Expr}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hnoPacked : ∀ field ∈ fields, field.packedBits = none)
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileSetStorage fields .calldata fieldName value =
-- TYPESIG_SORRY: Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: unfold CompilationModel.compileSetStorage at hcompile
-- SORRY'D: by_cases hmapping : isMapping fields fieldName
-- SORRY'D: · simp [hmapping] at hcompile
-- SORRY'D: · simp [hmapping] at hcompile
-- SORRY'D: rcases hfind : findFieldWithResolvedSlot fields fieldName with _ | ⟨f, slot⟩
-- SORRY'D: · simp [hfind] at hcompile
-- SORRY'D: · simp [hfind] at hcompile
-- SORRY'D: rcases hvalue : CompilationModel.compileExpr fields .calldata value with _ | valueExpr
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: have hunpacked : f.packedBits = none :=
-- SORRY'D: hnoPacked f (field_mem_of_findFieldWithResolvedSlot_some hfind)
-- SORRY'D: rw [hunpacked] at hcompile
-- SORRY'D: cases halias : f.aliasSlots with
-- SORRY'D: | nil =>
-- SORRY'D: simp [halias] at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: exact LegacyCompatibleExternalStmtList.expr
-- SORRY'D: (YulExpr.call "sstore" [YulExpr.lit slot, valueExpr])
-- SORRY'D: []
-- SORRY'D: LegacyCompatibleExternalStmtList.nil
-- SORRY'D: | cons alias rest =>
-- SORRY'D: simp [halias] at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: apply LegacyCompatibleExternalStmtList.block
-- SORRY'D: · apply LegacyCompatibleExternalStmtList.let_
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_exprStmtExprs
-- SORRY'D: (((slot :: alias :: rest).map
-- SORRY'D: (fun writeSlot =>
-- SORRY'D: YulExpr.call "sstore"
-- SORRY'D: [YulExpr.lit writeSlot, YulExpr.ident "__compat_value"])))
-- SORRY'D: · exact LegacyCompatibleExternalStmtList.nil
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmt_ok_letVar
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {name : String}
-- TYPESIG_SORRY: {value : Expr}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmt
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames (.letVar name value) =
-- TYPESIG_SORRY: Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: unfold CompilationModel.compileStmt at hcompile
-- SORRY'D: rcases hvalue : CompilationModel.compileExpr fields .calldata value with _ | valueIR
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: exact LegacyCompatibleExternalStmtList.let_ name valueIR [] LegacyCompatibleExternalStmtList.nil
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmt_ok_assignVar
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {name : String}
-- TYPESIG_SORRY: {value : Expr}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmt
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames (.assignVar name value) =
-- TYPESIG_SORRY: Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: unfold CompilationModel.compileStmt at hcompile
-- SORRY'D: rcases hvalue : CompilationModel.compileExpr fields .calldata value with _ | valueIR
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: exact LegacyCompatibleExternalStmtList.assign name valueIR [] LegacyCompatibleExternalStmtList.nil
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmt_ok_require
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {cond : Expr}
-- TYPESIG_SORRY: {message : String}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmt
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames (.require cond message) =
-- TYPESIG_SORRY: Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: unfold CompilationModel.compileStmt at hcompile
-- SORRY'D: rcases hfail : CompilationModel.compileRequireFailCond fields .calldata cond with _ | failCond
-- SORRY'D: · simp [hfail] at hcompile
-- SORRY'D: · simp [hfail] at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: exact LegacyCompatibleExternalStmtList.if_
-- SORRY'D: failCond
-- SORRY'D: (CompilationModel.revertWithMessage message)
-- SORRY'D: []
-- SORRY'D: (legacyCompatibleExternalStmtList_revertWithMessage message)
-- SORRY'D: LegacyCompatibleExternalStmtList.nil
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmt_ok_return
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {value : Expr}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmt
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames (.return value) =
-- TYPESIG_SORRY: Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: unfold CompilationModel.compileStmt at hcompile
-- SORRY'D: rcases hvalue : CompilationModel.compileExpr fields .calldata value with _ | valueIR
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: · simp [hvalue] at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: exact LegacyCompatibleExternalStmtList.expr
-- SORRY'D: (YulExpr.call "mstore" [YulExpr.lit 0, valueIR])
-- SORRY'D: [YulStmt.expr (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32])]
-- SORRY'D: (LegacyCompatibleExternalStmtList.expr
-- SORRY'D: (YulExpr.call "return" [YulExpr.lit 0, YulExpr.lit 32])
-- SORRY'D: []
-- SORRY'D: LegacyCompatibleExternalStmtList.nil)
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmt_ok_stop
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmt
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames .stop =
-- TYPESIG_SORRY: Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: unfold CompilationModel.compileStmt at hcompile
-- SORRY'D: injection hcompile with hbody
-- SORRY'D: subst hbody
-- SORRY'D: exact LegacyCompatibleExternalStmtList.expr
-- SORRY'D: (YulExpr.call "stop" [])
-- SORRY'D: []
-- SORRY'D: LegacyCompatibleExternalStmtList.nil
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmt_ok_on_supportedContractSurface
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {stmt : Stmt}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hnoPacked : ∀ field ∈ fields, field.packedBits = none)
-- TYPESIG_SORRY: (hsurface : stmtTouchesUnsupportedContractSurface stmt = false)
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmt
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames stmt = Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: cases stmt <;> simp [stmtTouchesUnsupportedContractSurface] at hsurface
-- SORRY'D: case letVar name value =>
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_compileStmt_ok_letVar hcompile
-- SORRY'D: case assignVar name value =>
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_compileStmt_ok_assignVar hcompile
-- SORRY'D: case setStorage fieldName value =>
-- SORRY'D: unfold CompilationModel.compileStmt at hcompile
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_compileSetStorage_ok_of_noPackedFields hnoPacked hcompile
-- SORRY'D: case require cond message =>
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_compileStmt_ok_require hcompile
-- SORRY'D: case return value =>
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_compileStmt_ok_return hcompile
-- SORRY'D: case stop =>
-- SORRY'D: exact legacyCompatibleExternalStmtList_of_compileStmt_ok_stop hcompile
-- SORRY'D: all_goals
-- SORRY'D: cases hsurface
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_scalar
-- TYPESIG_SORRY: (loadWord : YulExpr → YulExpr)
-- TYPESIG_SORRY: (sizeExpr : YulExpr)
-- TYPESIG_SORRY: (headSize baseOffset : Nat)
-- TYPESIG_SORRY: (param : Param)
-- TYPESIG_SORRY: (rest : List Param)
-- TYPESIG_SORRY: (headOffset : Nat)
-- TYPESIG_SORRY: (hparam : SupportedExternalParamType param.ty)
-- TYPESIG_SORRY: (hrest :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList
-- TYPESIG_SORRY: (CompilationModel.genParamLoadBodyFrom
-- TYPESIG_SORRY: loadWord sizeExpr headSize baseOffset rest
-- TYPESIG_SORRY: (headOffset + paramHeadSize param.ty))) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList
-- TYPESIG_SORRY: (CompilationModel.genParamLoadBodyFrom
-- TYPESIG_SORRY: loadWord sizeExpr headSize baseOffset (param :: rest) headOffset) := by sorry
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint256
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.uint256))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.uint256 } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(loadWord (YulExpr.lit headOffset))
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.uint256))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint8
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.uint8))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.uint8 } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(YulExpr.call "and" [loadWord (YulExpr.lit headOffset), YulExpr.lit 255])
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.uint8))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_address
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.address))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.address } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(YulExpr.call "and" [loadWord (YulExpr.lit headOffset), YulExpr.hex addressMask])
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.address))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_bytes32
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.bytes32))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.bytes32 } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(loadWord (YulExpr.lit headOffset))
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.bytes32))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_scalar
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(param : Param)
(rest : List Param)
(headOffset : Nat)
(hparam : SupportedExternalParamType param.ty)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize param.ty))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset (param :: rest) headOffset) := by
cases param with
| mk name ty =>
cases ty <;> simp [SupportedExternalParamType] at hparam
case uint256 =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint256
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case int256 =>
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(loadWord (YulExpr.lit headOffset))
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.int256))
hrest
case uint8 =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint8
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case address =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_address
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case bytes32 =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_bytes32
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_of_supported
-- TYPESIG_SORRY: (loadWord : YulExpr → YulExpr)
-- TYPESIG_SORRY: (sizeExpr : YulExpr)
-- TYPESIG_SORRY: (headSize baseOffset : Nat)
-- TYPESIG_SORRY: (params : List Param)
-- TYPESIG_SORRY: (headOffset : Nat)
-- TYPESIG_SORRY: (hparams : ∀ param ∈ params, SupportedExternalParamType param.ty) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList
-- TYPESIG_SORRY: (CompilationModel.genParamLoadBodyFrom
-- TYPESIG_SORRY: loadWord sizeExpr headSize baseOffset params headOffset) := by sorry
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_of_supported
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(params : List Param)
(headOffset : Nat)
(hparams : ∀ param ∈ params, SupportedExternalParamType param.ty) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset params headOffset) := by
induction params generalizing headOffset with
| nil =>
exact LegacyCompatibleExternalStmtList.nil
| cons param rest ih =>
have hparam : SupportedExternalParamType param.ty := hparams param (by simp)
have hrest : ∀ other ∈ rest, SupportedExternalParamType other.ty := by
intro other hmem
exact hparams other (by simp [hmem])
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_scalar
loadWord sizeExpr headSize baseOffset param rest headOffset hparam (ih _ hrest)
-- SORRY'D: induction params generalizing headOffset with
-- SORRY'D: | nil =>
-- SORRY'D: simp [CompilationModel.genParamLoadBodyFrom]
-- SORRY'D: | cons param rest ih =>
-- SORRY'D: have hparam : SupportedExternalParamType param.ty := hparams param (by simp)
-- SORRY'D: have hrest :
-- SORRY'D: ∀ other ∈ rest, SupportedExternalParamType other.ty := by
-- SORRY'D: intro other hmem
-- SORRY'D: exact hparams other (by simp [hmem])
-- SORRY'D: exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_scalar
-- SORRY'D: loadWord sizeExpr headSize baseOffset param rest headOffset hparam (ih _ hrest)
private theorem legacyCompatibleExternalStmtList_genParamLoads_of_supported
(params : List Param)
(hparams : ∀ param ∈ params, SupportedExternalParamType param.ty) :
LegacyCompatibleExternalStmtList (CompilationModel.genParamLoads params) := by
unfold CompilationModel.genParamLoads CompilationModel.genParamLoadsFrom
apply LegacyCompatibleExternalStmtList.if_
· exact LegacyCompatibleExternalStmtList.expr
(YulExpr.call "revert" [YulExpr.lit 0, YulExpr.lit 0])
[]
LegacyCompatibleExternalStmtList.nil
· exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_of_supported
(loadWord := fun pos => YulExpr.call "calldataload" [pos])
(sizeExpr := YulExpr.call "calldatasize" [])
(headSize := (params.map (fun p => paramHeadSize p.ty)).foldl (· + ·) 0)
(baseOffset := 4)
(params := params)
(headOffset := 4)
hparams
-- SORRY'D: unfold CompilationModel.genParamLoads CompilationModel.genParamLoadsFrom
-- SORRY'D: apply LegacyCompatibleExternalStmtList.if_
-- SORRY'D: · exact LegacyCompatibleExternalStmtList.expr
-- SORRY'D: (YulExpr.call "revert" [YulExpr.lit 0, YulExpr.lit 0])
-- SORRY'D: []
-- SORRY'D: LegacyCompatibleExternalStmtList.nil
-- SORRY'D: · exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_of_supported
-- SORRY'D: (loadWord := fun pos => YulExpr.call "calldataload" [pos])
-- SORRY'D: (sizeExpr := YulExpr.call "calldatasize" [])
-- SORRY'D: (headSize := (params.map (fun p => paramHeadSize p.ty)).foldl (· + ·) 0)
-- SORRY'D: (baseOffset := 4)
-- SORRY'D: (params := params)
-- SORRY'D: (headOffset := 4)
-- SORRY'D: hparams
-- TYPESIG_SORRY: private theorem legacyCompatibleExternalStmtList_of_compileStmtList_ok_on_supportedContractSurface
-- TYPESIG_SORRY: {fields : List Field}
-- TYPESIG_SORRY: {inScopeNames : List String}
-- TYPESIG_SORRY: {stmts : List Stmt}
-- TYPESIG_SORRY: {bodyIR : List YulStmt}
-- TYPESIG_SORRY: (hnoPacked : ∀ field ∈ fields, field.packedBits = none)
-- TYPESIG_SORRY: (hsurface : stmtListTouchesUnsupportedContractSurface stmts = false)
-- TYPESIG_SORRY: (hcompile :
-- TYPESIG_SORRY: CompilationModel.compileStmtList
-- TYPESIG_SORRY: fields [] [] .calldata [] false inScopeNames stmts = Except.ok bodyIR) :
-- TYPESIG_SORRY: LegacyCompatibleExternalStmtList bodyIR := by sorry
-- SORRY'D: induction stmts generalizing inScopeNames bodyIR with
-- SORRY'D: | nil =>
-- SORRY'D: simpa [CompilationModel.compileStmtList] using hcompile
-- SORRY'D: | cons stmt rest ih =>
-- SORRY'D: have hstmtSurface : stmtTouchesUnsupportedContractSurface stmt = false := by
-- SORRY'D: simpa [stmtListTouchesUnsupportedContractSurface] using (Bool.or_eq_false.mp hsurface).1
-- SORRY'D: have hrestSurface : stmtListTouchesUnsupportedContractSurface rest = false := by
-- SORRY'D: simpa [stmtListTouchesUnsupportedContractSurface] using (Bool.or_eq_false.mp hsurface).2
-- SORRY'D: rcases FunctionBody.compileStmtList_cons_ok_inv
-- SORRY'D: (fields := fields)
-- SORRY'D: (inScopeNames := inScopeNames)
-- SORRY'D: (stmt := stmt)
-- SORRY'D: (rest := rest)
-- SORRY'D: hcompile with
-- SORRY'D: ⟨headIR, tailIR, hhead, htail, hbody⟩
-- SORRY'D: subst hbody
-- SORRY'D: exact legacyCompatibleExternalStmtList_append
-- SORRY'D: (legacyCompatibleExternalStmtList_of_compileStmt_ok_on_supportedContractSurface
-- SORRY'D: hnoPacked hstmtSurface hhead)
-- SORRY'D: (ih hnoPacked hrestSurface htail)
private theorem compileValidatedCore_ok_yields_compiled_functions
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
unfold compileValidatedCore at hcore
rw [hSupported.normalizedFields, hSupported.noEvents, hSupported.noErrors,
hSupported.noConstructor, hfallback, hreceive] at hcore
simp only [bind, Except.bind, pure, Except.pure] at hcore
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields [] [] x.2 x.1) with _ | irFns
· simp [hmap] at hcore
· simp [hmap] at hcore
rcases hinternal :
(model.functions.filter (·.isInternal)).mapM
(compileInternalFunction model.fields [] []) with _ | internalFuncDefs
· simp [hinternal] at hcore
· simp [hinternal, compileConstructor] at hcore
have hfunctions : ir.functions = irFns := by
injection hcore with hir
cases hir
rfl
have hcompiled :
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec model.fields [] [] entry.2 entry.1 = Except.ok irFn)
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors)
irFns :=
compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap
simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions,
hSupported.noEvents, hSupported.noErrors, hfunctions] using hcompiled
private theorem compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
unfold compileValidatedCore at hcore
rw [hSupported.normalizedFields, hSupported.noEvents, hSupported.noErrors,
hSupported.noConstructor, hfallback, hreceive] at hcore
simp only [bind, Except.bind, pure, Except.pure] at hcore
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields [] [] x.2 x.1) with _ | irFns
· simp [hmap] at hcore
· simp [hmap] at hcore
rcases hinternal :
(model.functions.filter (·.isInternal)).mapM
(compileInternalFunction model.fields [] []) with _ | internalFuncDefs
· simp [hinternal] at hcore
· simp [hinternal, compileConstructor] at hcore
have hfunctions : ir.functions = irFns := by
injection hcore with hir
cases hir
rfl
have hcompiled :
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec model.fields [] [] entry.2 entry.1 = Except.ok irFn)
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors)
irFns :=
compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap
simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions,
hSupported.noEvents, hSupported.noErrors, hfunctions] using hcompiled
private theorem filterInternalFunctions_eq_nil_of_all_nonInternal :
∀ (fns : List FunctionSpec),
(∀ fn ∈ fns, fn.isInternal = false) →
fns.filter (·.isInternal) = []
| [], _ => rfl
| fn :: rest, hall => by
have hfn : fn.isInternal = false := hall fn (by simp)
have hrest : ∀ fn' ∈ rest, fn'.isInternal = false := by
intro fn' hmem
exact hall fn' (by simp [hmem])
simp [hfn, filterInternalFunctions_eq_nil_of_all_nonInternal rest hrest]
private theorem filterInternalFunctions_eq_nil_of_supported
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors) :
model.functions.filter (·.isInternal) = [] := by
exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions
(hSupported.noInternalFunctions)
private theorem filterInternalFunctions_eq_nil_of_supported_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors) :
model.functions.filter (·.isInternal) = [] := by
exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions
(hSupported.noInternalFunctions)
private theorem compileValidatedCore_ok_yields_internalFunctions_nil
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
ir.internalFunctions = [] := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
have hnoInternalFns :
model.functions.filter (·.isInternal) = [] :=
filterInternalFunctions_eq_nil_of_supported model selectors hSupported
have harray : contractUsesArrayElement model = false :=
hSupported.contractUsesArrayElement_eq_false
have hstorageArray : contractUsesStorageArrayElement model = false :=
hSupported.contractUsesStorageArrayElement_eq_false
have hdynamicBytesEq : contractUsesDynamicBytesEq model = false :=
hSupported.contractUsesDynamicBytesEq_eq_false
unfold compileValidatedCore at hcore
rw [hSupported.normalizedFields, hfallback, hreceive, harray, hstorageArray,
hdynamicBytesEq, hnoInternalFns, hSupported.noConstructor] at hcore
simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcore
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields model.events model.errors x.2 x.1) with _ | irFns
· simp [hmap] at hcore
· simp [hmap, compileConstructor] at hcore
injection hcore with hir
cases hir
rfl
theorem supported_params_of_supportedSpec
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors) :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty := by
intro fn hfn param hparam
have hfnModel : fn ∈ model.functions := by
exact List.mem_of_mem_filter hfn
exact (hSupported.functions fn hfnModel).paramsSupported param hparam
theorem supported_params_of_supportedSpec_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors) :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty := by
intro fn hfn param hparam
have hfnModel : fn ∈ model.functions := by
exact List.mem_of_mem_filter hfn
exact (hSupported.functions fn hfnModel).paramsSupported param hparam
theorem interpretIR_eq_runtimeContractOfFunctions
(ir : IRContract)
(runtimeName : String)
(irFns : List IRFunction)
(tx : IRTransaction)
(initialState : IRState)
(hfunctions : ir.functions = irFns) :
interpretIR ir tx initialState =
interpretIR (Dispatch.runtimeContractOfFunctions runtimeName irFns) tx initialState := by
cases ir
subst hfunctions
simp [interpretIR, Dispatch.runtimeContractOfFunctions]
theorem interpretContract_correct_of_ir_functions
(model : CompilationModel)
(selectors : List Nat)
(ir : IRContract)
(irFns : List IRFunction)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(hfunctions : ir.functions = irFns)
(hcompiled :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
irFns)
(hparamsSupported :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty)
(hfunction :
∀ fn sel irFn bindings,
fn ∈ selectorDispatchedFunctions model →
compileFunctionSpec model.fields model.events model.errors sel fn = Except.ok irFn →
SourceSemantics.bindSupportedParams fn.params tx.args = some bindings →
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretFunction model fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld))) :
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretContract model selectors tx initialWorld)
(interpretIR ir tx (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
rw [interpretIR_eq_runtimeContractOfFunctions
(ir := ir)
(runtimeName := model.name)
(irFns := irFns)
(tx := tx)
(initialState := FunctionBody.initialIRStateForTx model tx initialWorld)
(hfunctions := hfunctions)]
exact Dispatch.interpretContract_correct_of_compiled_functions
(model := model) (selectors := selectors) (irFns := irFns)
(tx := tx) (initialWorld := initialWorld)
hcompiled hparamsSupported hfunction
theorem compile_preserves_semantics_of_compiled_functions
(model : CompilationModel)
(selectors : List Nat)
(ir : IRContract)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(_hcompile : CompilationModel.compile model selectors = Except.ok ir)
(hcompiled :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions)
(hparamsSupported :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty)
(hfunction :
∀ fn sel irFn bindings,
fn ∈ selectorDispatchedFunctions model →
compileFunctionSpec model.fields model.events model.errors sel fn = Except.ok irFn →
SourceSemantics.bindSupportedParams fn.params tx.args = some bindings →
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretFunction model fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld))) :
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretContract model selectors tx initialWorld)
(interpretIR ir tx (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
exact interpretContract_correct_of_ir_functions
(model := model)
(selectors := selectors)
(ir := ir)
(irFns := ir.functions)
(tx := tx)
(initialWorld := initialWorld)
(hfunctions := rfl)
(hcompiled := hcompiled)
(hparamsSupported := hparamsSupported)
(hfunction := hfunction)
/-- Derive the compiled runtime function table directly from
`CompilationModel.compile = Except.ok ir` and `SupportedSpec`, without any
intermediate `List.Forall₂` hypothesis supplied by callers. -/
theorem compile_ok_yields_compiled_functions
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
unfold CompilationModel.compile at hcompile
simp only [bind, Except.bind] at hcompile
rcases hvalidate : validateCompileInputs model selectors with _ | validated
· simp [hvalidate] at hcompile
· simp [hvalidate] at hcompile
exact compileValidatedCore_ok_yields_compiled_functions
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcore := hcompile)
theorem compile_ok_yields_compiled_functions_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
unfold CompilationModel.compile at hcompile
simp only [bind, Except.bind] at hcompile
rcases hvalidate : validateCompileInputs model selectors with _ | validated
· simp [hvalidate] at hcompile
· simp [hvalidate] at hcompile
exact compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcore := hcompile)