Source file gen_why3.ml
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
open Location
module M = Model
open Tools
open Mlwtree
let gArchetypeDir = "archetype"
let gArchetypeLib = "Lib"
let gArchetypeColl = "AssetCollection"
let gArchetypeSum = "Sum"
let gArchetypeList = "IntListUtils"
let gArchetypeTrace = "Trace"
let mk_id i = "_"^i
let mk_ac_id a = mk_id (a^"_assets")
let mk_ac_added_id a = mk_id (a^"_assets_added")
let mk_ac_rmed_id a = mk_id (a^"_assets_removed")
let gs = "_s"
let mk_ac a = Tdoti (gs, mk_ac_id a)
let mk_ac_old a = Tdot (Told (Tvar gs), Tvar (mk_ac_id a))
let mk_ac_added a = Tdoti (gs, mk_ac_added_id a)
let mk_ac_old_added a = Tdot (Told (Tvar gs), Tvar (mk_ac_added_id a))
let mk_ac_rmed a = Tdoti (gs, mk_ac_rmed_id a)
let mk_ac_old_rmed a = Tdot (Told (Tvar gs), Tvar (mk_ac_rmed_id a))
let mk_ac_sv s a = Tdoti (s, mk_ac_id a)
let mk_use = Duse [gArchetypeDir;gArchetypeLib] |> loc_decl |> deloc
let mk_use_list = Duse ["list";"List"] |> loc_decl |> deloc
let mk_use_module m = Duse [deloc m] |> loc_decl |> deloc
type change =
| CAdd of ident
| CRm of ident
| CUpdate of ident
| CTransfer of ident
| CGet of ident
| CIterate of ident
| CCall of ident
let mk_change_term tr =
match tr with
| CAdd id -> Tapp (Tdoti("Tr",
"TrAdd_"),
[Tvar (String.capitalize_ascii id)])
| CRm id -> Tapp (Tdoti("Tr",
"TrRm_"),
[Tvar (String.capitalize_ascii id)])
| CUpdate id -> Tapp (Tdoti("Tr",
"TrUpdate_"),
[Tvar (String.capitalize_ascii id)])
| CGet id -> Tapp (Tdoti("Tr",
"TrGet_"),
[Tvar (String.capitalize_ascii id)])
| _ -> assert false
let mk_trace tr =
let gstr = Tdoti(gs,
mk_id "tr") in
Tassign (gstr,
Tcons (mk_change_term tr,
gstr)
) |> loc_term
let mk_trace_asset m =
Denum ("_asset",
M.Utils.get_assets m
|> List.map (fun (a : M.info_asset) -> String.capitalize_ascii a.name))
|> loc_decl
let mk_trace_entry m =
Denum ("_entry",
M.Utils.get_entries m
|> List.map (fun (_, (f : M.function_struct)) -> String.capitalize_ascii (unloc f.name)))
|> loc_decl
let mk_trace_field m =
Denum ("_field",
(M.Utils.get_variables m
|> List.map (fun (v : M.storage_item) -> String.capitalize_ascii (Model.Utils.get_storage_id_name v.id))) @
(M.Utils.get_assets m
|> List.map (fun (a : M.info_asset) ->
List.map (fun (n,_,_) -> String.capitalize_ascii n) a.values
)
|> List.flatten))
|> loc_decl
let mk_trace_clone () =
Dclone ([gArchetypeDir;gArchetypeTrace], "Tr",
[Ctype ("_asset","_asset");
Ctype ("_entry","_entry");
Ctype ("_field","_field")])
|> loc_decl
let mk_trace_utils m =
if M.Utils.with_trace m then [
mk_trace_asset m;
mk_trace_entry m;
mk_trace_field m;
mk_trace_clone ()
] else []
let mk_default_init = function
| Drecord (n,fs) ->
Dfun {
name = "mk_default_"^n;
logic = NoMod;
args = [];
returns = Tyasset n;
raises = [];
variants = [];
requires = [];
ensures = [];
body = Trecord (None, List.map (fun (f:field) ->
f.name, f.init
) fs);
}
| _ -> assert false
let mk_collection_field asset to_id = {
name = with_dummy_loc (to_id asset);
typ = loc_type (Tycoll asset);
init = loc_term (Temptycoll asset);
mutable_ = true;
}
let mk_const_fields m = [
{ name = mk_id "ops" ; typ = Tyrecord "transfers" ; init = Tvar "Nil"; mutable_ = true; };
{ name = mk_id "balance" ; typ = Tytez; init = Tint Big_int.zero_big_int; mutable_ = false; };
{ name = mk_id "transferred" ; typ = Tytez; init = Tint Big_int.zero_big_int; mutable_ = false; };
{ name = mk_id "caller" ; typ = Tyaddr; init = Tint Big_int.zero_big_int; mutable_ = false; };
{ name = mk_id "now" ; typ = Tydate; init = Tint Big_int.zero_big_int; mutable_ = false; }
] @
if M.Utils.with_trace m then
[
{ name = mk_id "entry" ; typ = Tyoption (Tyasset "_entry"); init = Tnone; mutable_ = false; };
{ name = mk_id "tr" ; typ = Tyasset ("Tr._traces"); init = Tnil; mutable_ = true; }
]
else []
let mk_sum_clone_id a f = (String.capitalize_ascii a) ^ (String.capitalize_ascii f)
let mk_sum_clone _m asset key field =
let cap_asset = String.capitalize_ascii asset in
Dclone ([gArchetypeDir;gArchetypeSum],
mk_sum_clone_id asset field,
[Ctype ("container",
cap_asset^".collection");
Ctype ("t",
asset);
Cval ("f",
"get_"^field);
Cval ("field",
field);
Cval ("card",
cap_asset^".card");
Cfun ("union",
cap_asset^".union");
Cfun ("inter",
cap_asset^".inter");
Cfun ("diff",
cap_asset^".diff");
Cpred("is_empty",
cap_asset^".is_empty");
Cpred("subset",
cap_asset^".subset");
Cval ("singleton",
cap_asset^".singleton");
Cval ("witness",
cap_asset^".witness");
Cval ("keyf",
key);
])
let mk_keys_eq_axiom n f ktyp : decl =
Dtheorem (Axiom,
"eq_"^n^"_keys",
Tforall ([["s"],Tystorage;["k"],ktyp],
Timpl (Tmem (n,
Tvar "k",
Tdoti ("s",n^"_keys")),
Teq (Tyint,
Tapp (Tvar f,
[Tget (n,
Tdoti ("s",n^"_assets"),
Tvar "k")]),
Tvar "k"))))
let mk_partition_axiom asset f _kt pa kpt : decl =
Dtheorem (Axiom,
asset^"_"^f^"_is_partition",
Tforall ([["s"],Tystorage;["a"],Tyasset asset;["k"],kpt],
Timpl (Tmem (asset,
Tvar("a"),
mk_ac_sv "s" asset),
Timpl (Tlmem (gArchetypeList,
Tvar "k",
Tapp (Tvar f,
[Tvar "a"])),
Tcontains (pa,
Tvar "k",
mk_ac_sv "s" pa)))))
let rec mk_select_test = function
| Tdot (Tvar v,f) when compare v "the" = 0 -> Tdot (Tvar "a",f)
| Tnow _ -> Tvar (mk_id "now")
| _ as t -> map_abstract_term mk_select_test id id t
let mk_select_body asset mlw_test : term =
let capa = String.capitalize_ascii asset in
Tletfun (
{
name = "internal_select";
logic = Rec;
args = ["l",Tylist (Tyasset asset)];
returns = Tylist (Tyasset asset);
raises = [];
variants = [Tvar "l"];
requires = [];
ensures = [];
body = Tmlist (Tnil,"l","a","tl",
Tif(mk_select_test mlw_test,
Tcons (Tvar "a",Tapp (Tvar ("internal_select"),[Tvar "tl"])),
Some (Tapp (Tvar ("internal_select"),[Tvar "tl"])))
);
},
Trecord (
None,
[capa^".content", Tapp (Tvar "internal_select",[Tdoti("c."^capa,"content")])]
)
)
let test =
let rec acc (term : M.mterm) =
match term.M.node with
| M.Mnow -> acc @ [term,mk_id "now", Tydate]
| _ -> M.fold_term internal_extract_args acc term in
internal_extract_args [] test
let get_select_id (m : M.model) asset test =
let rec internal_get_select_id acc = function
| (sc : M.api_item) :: tl ->
begin
match sc.node_item with
| M.APIFunction (Select (a,t)) ->
if compare a asset = 0 then
if M.cmp_mterm t test then
acc + 1
else
internal_get_select_id (acc + 1) tl
else
internal_get_select_id acc tl
| _ -> internal_get_select_id acc tl
end
| [] -> acc in
internal_get_select_id 0 m.api_items
let mk_select_name m asset test = "select_"^asset^"_"^(string_of_int (get_select_id m asset test))
let mk_select m asset test mlw_test only_formula =
let id = mk_select_name m asset test in
let decl = Dfun {
name = id;
logic = if only_formula then Logic else NoMod;
args = (extract_args test |> List.map (fun (_,a,b) -> a,b)) @ ["c",Tycoll asset];
returns = Tycoll asset;
raises = [];
variants = [];
requires = [];
ensures = [
{ id = id^"_post_1";
form = Tforall (
[["a"],Tyasset asset],
Timpl (Tmem (asset,Tvar "a",Tresult),
mk_select_test mlw_test
)
);
};
{ id = id^"_post_2";
form = Tsubset (asset,
Tresult,
Tvar "c");
}
];
body = mk_select_body asset mlw_test;
} in
decl
let wdl (l : 'a list) = List.map with_dummy_loc l
let unloc_decl = List.map unloc_decl
let loc_decl = List.map loc_decl
let loc_field = List.map loc_field
let deloc (l : 'a list) = List.map deloc l
let rec zip l1 l2 l3 l4 =
match l1,l2,l3,l4 with
| e1::tl1,e2::tl2,e3::tl3,e4::tl4 -> e1::e2::e3::e4::(zip tl1 tl2 tl3 tl4)
| _ -> []
let cap s = mk_loc s.loc (String.capitalize_ascii s.obj)
let map_lident (i : M.lident) : loc_ident = {
obj = i.pldesc;
loc = i.plloc;
}
let map_lidents = List.map map_lident
let type_to_init (typ : loc_typ) : loc_term =
mk_loc typ.loc (match typ.obj with
| Typartition i -> Temptycoll i
| Tycoll i -> Temptycoll i
| Tylist _ -> Tnil
| Tymap i -> Tvar (mk_loc typ.loc ("const (mk_default_"^i.obj^" ())"))
| _ -> Tint Big_int.zero_big_int)
let map_btype = function
| M.Bbool -> Tybool
| M.Bint -> Tyint
| M.Brational -> Tyrational
| M.Bdate -> Tydate
| M.Bduration -> Tyduration
| M.Bstring -> Tystring
| M.Baddress -> Tyaddr
| M.Brole -> Tyrole
| M.Bcurrency -> Tytez
| M.Bkey -> Tykey
let rec map_mtype (t : M.type_) : loc_typ =
with_dummy_loc (match t with
| M.Tasset id -> Tyasset (map_lident id)
| M.Tenum id -> Tyenum (map_lident id)
| M.Tbuiltin v -> map_btype v
| M.Tcontainer (Tasset id,M.Partition) -> Typartition (map_lident id)
| M.Tcontainer (Tasset id,M.Collection) -> Tycoll (map_lident id)
| M.Tcontainer (t,M.Collection) -> Tylist (map_mtype t).obj
| M.Toption t -> Tyoption (map_mtype t).obj
| M.Ttuple l -> Tytuple (l |> List.map map_mtype |> deloc)
| M.Tunit -> Tyunit
| _ -> assert false)
let rec map_term (t : M.mterm) : loc_term = mk_loc t.loc (
match t.node with
| M.Maddress v -> Tint (sha v)
| M.Mint i -> Tint i
| M.Mvarlocal i -> Tvar (map_lident i)
| M.Mgt (t1,t2) -> Tgt (with_dummy_loc Tyint,map_term t1,map_term t2)
| _ ->
let str = Format.asprintf "Not translated : %a@." M.pp_mterm t in
print_endline str;
Tnottranslated
)
let map_record_term _ = map_term
let map_record_values (values : M.record_item list) =
List.map (fun (value : M.record_item) ->
let typ_ = map_mtype value.type_ in
let init_value = type_to_init typ_ in {
name = map_lident value.name;
typ = typ_;
init = Option.fold map_record_term init_value value.default;
mutable_ = false;
}
) values
let map_storage_items = List.fold_left (fun acc (item : M.storage_item) ->
acc @
match item.typ with
| M.Tcontainer (Tasset id, Collection) ->
let id = unloc id in [
mk_collection_field id mk_ac_id;
mk_collection_field id mk_ac_added_id;
mk_collection_field id mk_ac_rmed_id
]
| _ ->
let typ_ = map_mtype item.typ in
let init_value = type_to_init typ_ in
[{
name = Model.Utils.get_storage_id_name item.id |> with_dummy_loc;
typ = typ_;
init = map_record_term init_value item.default;
mutable_ = true;
}]
) []
let is_local_invariant _m an t =
let rec internal_is_local acc (term : M.mterm) =
match term.M.node with
| M.Mforall (_i,M.Tasset a,_,_b) -> not (compare (a |> unloc) an = 0)
| M.Msum (a,_,_) -> not (compare a an = 0)
| M.Mmax (a,_,_) -> not (compare a an = 0)
| M.Mmin (a,_,_) -> not (compare a an = 0)
| M.Mselect (a,_,_) -> not (compare a an = 0)
| _ -> M.fold_term internal_is_local acc term in
internal_is_local true t
let adds_asset m an b =
let rec internal_adds acc (term : M.mterm) =
match term.M.node with
| M.Maddasset (a,_) -> compare a an = 0
| M.Maddfield (a,f,_,_) ->
let (pa,_,_) = M.Utils.get_partition_asset_key m (dumloc a) (dumloc f) in
compare pa an = 0
| _ -> M.fold_term internal_adds acc term in
internal_adds false b
let is_only_security (s : M.security_predicate) =
match s.s_node with
| M.SonlyByRole _ -> true
| M.SonlyInAction _ -> true
| M.SonlyByRoleInAction _ -> true
| _ -> false
let map_action_to_change = function
| M.ADadd i -> CAdd i
| M.ADremove i -> CRm i
| M.ADupdate i -> CUpdate i
| M.ADtransfer i -> CTransfer i
| M.ADget i -> CGet i
| M.ADiterate i -> CIterate i
| M.ADcall i -> CCall i
| _ -> assert false
let map_security_pred loc (t : M.security_predicate) =
let vars =
["tr";"caller";"entry"]
|> List.map mk_id
|> List.map (fun v ->
match loc with
| `Storage -> Tvar (v)
| `Loop -> Tdoti(gs,v)
)
in
let tr = List.nth vars 0 in
let caller = List.nth vars 1 in
let entry = List.nth vars 2 in
let mk_eq a b opt = Teq (Tyint,a,
if opt then
Tsome (Tvar b)
else
match loc with
| `Storage -> Tvar b
| `Loop -> Tdoti(gs,b)
) in
let mk_performed_by t l opt =
Tapp (Tvar "Tr.performed_by",
[tr;
List.fold_left (fun acc r ->
Tor (acc,mk_eq t r opt)
) (mk_eq t (List.hd l) opt) (List.tl l) ])
in
let mk_changes_performed_by t a l opt =
Tapp (Tvar "Tr.changes_performed_by",
[tr;
Tcons (map_action_to_change a |> mk_change_term,Tnil);
List.fold_left (fun acc r ->
Tor (acc,mk_eq t r opt)
) (mk_eq t (List.hd l) opt) (List.tl l) ])
in
let mk_performed_by_2 t1 t2 l1 l2 =
Tapp (Tvar "Tr.performed_by",
[tr;
Tand (
List.fold_left (fun acc r ->
Tor (acc,mk_eq t1 r false)
) (mk_eq t1 (List.hd l1) false) (List.tl l2),
List.fold_left (fun acc r ->
Tor (acc,mk_eq t2 r true)
) (mk_eq t2 (List.hd l1) true) (List.tl l2))])
in
let mk_changes_performed_by_2 t1 t2 a l1 l2 =
Tapp (Tvar "Tr.performed_by",
[tr;
Tcons (map_action_to_change a |> mk_change_term,Tnil);
Tand (
List.fold_left (fun acc r ->
Tor (acc,mk_eq t1 r false)
) (mk_eq t1 (List.hd l1) false) (List.tl l2),
List.fold_left (fun acc r ->
Tor (acc,mk_eq t2 r true)
) (mk_eq t2 (List.hd l1) true) (List.tl l2))])
in
match t.M.s_node with
| M.SonlyByRole (ADany,roles) ->
mk_performed_by caller (roles |> List.map unloc) false
| M.SonlyInAction (ADany,Sentry entries) ->
mk_performed_by entry (entries |> List.map unloc |> List.map String.capitalize_ascii) true
| M.SonlyByRole (a,roles) ->
mk_changes_performed_by caller a (roles |> List.map unloc) false
| M.SonlyInAction (a,Sentry entries) ->
mk_changes_performed_by entry
a
(entries |> List.map unloc |> List.map String.capitalize_ascii)
true
| M.SonlyByRoleInAction (ADany,roles,Sentry entries) ->
mk_performed_by_2 caller entry
(roles |> List.map unloc)
(entries |> List.map unloc |> List.map String.capitalize_ascii)
| M.SonlyByRoleInAction (a,roles,Sentry entries) ->
mk_changes_performed_by_2 caller entry a
(roles |> List.map unloc)
(entries |> List.map unloc |> List.map String.capitalize_ascii)
| _ -> Tnottranslated
let mk_spec_invariant loc (sec : M.security_item) =
if is_only_security sec.predicate then
[
{
id = map_lident sec.label;
form = map_security_pred loc sec.predicate |> loc_term;
}
]
else []
let mk_app_field (a : ident) (f : loc_ident) : loc_term * loc_term =
let arg : term = Tvar a in
let loc_f : loc_term = mk_loc f.loc (Tvar f) in
(loc_f,with_dummy_loc (Tapp (loc_f,[loc_term arg])))
let mk_invariant m n src inv : loc_term =
let r = M.Utils.get_info_asset m n in
let fields = r.values |> List.map (fun (i,_,_) -> i) |> wdl in
let asset = map_lident n in
let variable =
match src with
| `Preasset arg -> arg
| _ -> "a" in
let replacements = List.map (fun f -> mk_app_field variable f) fields in
let replacing = List.fold_left (fun acc (t1,t2) -> loc_replace t1 t2 acc) inv replacements in
match src with
| `Preasset _ -> replacing
| _ ->
let mem_pred =
match src with
| `Storage -> Tmem ((unloc_ident asset),
Tvar variable,
Tvar (mk_ac_id asset.obj))
| `Axiom -> Tmem ((unloc_ident asset),
Tvar variable,
Tdoti ("s", mk_ac_id asset.obj))
| `Axiom2 -> Tmem ((unloc_ident asset),
Tvar variable,
Tvar ("c"))
| `Loop -> Tmem ((unloc_ident asset),
Tvar variable,
mk_ac (unloc n))
| `Prelist arg ->
Tapp (Tvar ((String.capitalize_ascii (unloc n))^".internal_mem"),
[Tvar variable; Tvar arg])
| `Precoll arg -> Tmem ((unloc_ident asset),
Tvar variable,
Tvar arg)
| _ -> Tnone
in
let prefix =
match src with
| `Axiom ->
Tforall ([["s"],Tystorage],
Tforall ([[variable],Tyasset (unloc_ident asset)],
Timpl (mem_pred,
Ttobereplaced)))
| `Axiom2 ->
Tforall ([["s"],Tystorage],
Tforall ([["c"],Tycoll (unloc n)],
Timpl (
Tsubset (unloc n,
Tvar "c",
Tdoti ("s", mk_ac_id asset.obj)),
Tforall ([[variable],Tyasset (unloc_ident asset)],
Timpl (mem_pred,
Ttobereplaced)))))
| _ ->
Tforall ([[variable],Tyasset (unloc_ident asset)],
Timpl (mem_pred,
Ttobereplaced)) in
loc_replace (with_dummy_loc Ttobereplaced) replacing (loc_term prefix)
let mk_storage_invariant m n (lt : M.label_term) = {
id = Option.fold (fun _ x -> map_lident x) (with_dummy_loc "") lt.label;
form = mk_invariant m n `Storage (map_term lt.term);
}
let mk_pre_list m n arg inv : loc_term = mk_invariant m (dumloc n) (`Prelist arg) inv
let mk_pre_coll m n arg inv : loc_term = mk_invariant m (dumloc n) (`Precoll arg) inv
let mk_pre_asset m n arg inv : loc_term = mk_invariant m (dumloc n) (`Preasset arg) inv
let mk_loop_invariant m n inv : loc_term = mk_invariant m (dumloc n) `Loop inv
let mk_axiom_invariant m n inv : loc_term = mk_invariant m (dumloc n) `Axiom inv
let mk_axiom2_invariant m n inv : loc_term = mk_invariant m (dumloc n) `Axiom2 inv
let mk_eq_asset _m (r : M.record) =
let cmps = List.map (fun (item : M.record_item) ->
let f1 = Tdoti("a1",unloc item.name) in
let f2 = Tdoti("a2",unloc item.name) in
match item.type_ with
| Tcontainer _ -> Tapp (Tvar "eql",[f1;f2])
| _ -> Teq (Tyint,f1,f2)
) r.values in
Dfun {
name = "eq_"^(unloc r.name) |> with_dummy_loc;
logic = Logic;
args = ["a1" |> with_dummy_loc, Tyasset (map_lident r.name) |> with_dummy_loc;
"a2" |> with_dummy_loc, Tyasset (map_lident r.name) |> with_dummy_loc];
returns = Tybool |> with_dummy_loc;
raises = [];
variants = [];
requires = [];
ensures = [];
body = List.fold_left (fun acc cmp ->
Tpand (acc,cmp)
) (List.hd cmps) (List.tl cmps) |> loc_term;
}
let mk_eq_extensionality _m (r : M.record) : loc_decl =
let asset = unloc r.name in
Dtheorem (Lemma,
asset^"_extensionality",
Tforall ([["a1";"a2"],Tyasset asset],
Timpl (Tapp (Tvar ("eq_"^asset),
[Tvar "a1";Tvar "a2"]),
Teq (Tyasset asset,Tvar "a1",Tvar "a2")
))) |> Mlwtree.loc_decl
let map_record _m (r : M.record) =
Drecord (map_lident r.name, map_record_values r.values)
let record_to_clone m (r : M.info_asset) =
let (key,_) = M.Utils.get_asset_key m (dumloc r.name) in
let sort =
if List.length r.sort > 0 then
List.hd r.sort
else key in
Dclone ([gArchetypeDir;gArchetypeColl] |> wdl,
String.capitalize_ascii r.name |> with_dummy_loc,
[Ctype ("t" |> with_dummy_loc, r.name |> with_dummy_loc);
Cval ("keyf" |> with_dummy_loc, key |> with_dummy_loc);
Cval ("sortf" |> with_dummy_loc, sort |> with_dummy_loc);
Cval ("eqf" |> with_dummy_loc, "eq_"^r.name |> with_dummy_loc)])
let map_storage m (l : M.storage) =
Dstorage {
fields = (map_storage_items l)@ (mk_const_fields m |> loc_field |> deloc);
invariants = List.concat (List.map (fun (item : M.storage_item) ->
List.map (mk_storage_invariant m (match item.id with
| SIname name -> name
| SIstate -> dumloc "state")
) item.invariants) l) @
(List.fold_left (fun acc sec ->
acc @ (mk_spec_invariant `Storage sec)) [] m.security.items)
}
let mk_partition_axioms (m : M.model) =
M.Utils.get_partitions m |> List.map (fun (n,i,_) ->
let kt = M.Utils.get_asset_key m (dumloc n) |> snd |> map_btype in
let pa,_,pkt = M.Utils.get_partition_asset_key m (dumloc n) (dumloc i) in
mk_partition_axiom n i kt pa (pkt |> map_btype)
) |> loc_decl |> deloc
let rec get_record id = function
| Drecord (n,_) as r :: _tl when compare id n = 0 -> r
| _ :: tl -> get_record id tl
| [] -> assert false
let get_record_name = function
| Drecord (n,_) -> n
| _ -> assert false
let mk_var (i : ident) = Tvar i
let get_for_fun = function
| M.Tcontainer (Tasset a,_) -> (fun (t1,t2) -> loc_term (Tnth (unloc a,t1,t2))),
(fun t -> loc_term (Tcard (unloc a,t)))
| _ -> assert false
type logical_mod = Nomod | Added | Removed
type logical_context = {
old : bool;
lmod : logical_mod;
}
let init_ctx = {
old = false;
lmod = Nomod;
}
let mk_trace_seq m t chs =
if M.Utils.with_trace m then
Tseq ([with_dummy_loc t] @ (List.map mk_trace chs))
else t
let rec map_mterm m ctx (mt : M.mterm) : loc_term =
let t =
match mt.node with
| M.Mif (c,t,Some { node=M.Mseq []; type_=_}) ->
Tif (map_mterm m ctx c, map_mterm m ctx t, None)
| M.Mif (c,t,e) -> Tif (map_mterm m ctx c, map_mterm m ctx t, Option.map (map_mterm m ctx) e)
| M.Mnot c -> Tnot (map_mterm m ctx c)
| M.Mfail InvalidCaller -> Traise Einvalidcaller
| M.Mfail NoTransfer -> Traise Enotransfer
| M.Mfail (InvalidCondition _) -> Traise Einvalidcondition
| M.Mfail _ -> Traise Enotfound
| M.Mequal (l,r) -> Teq (with_dummy_loc Tyint,map_mterm m ctx l,map_mterm m ctx r)
| M.Mcaller -> Tcaller (with_dummy_loc gs)
| M.Mtransferred -> Ttransferred (with_dummy_loc gs)
| M.Mvarstorevar v -> Tdoti (with_dummy_loc gs, map_lident v)
| M.Mcurrency (v,_) -> Tint v
| M.Mgt (l, r) -> Tgt (with_dummy_loc Tyint, map_mterm m ctx l, map_mterm m ctx r)
| M.Mge (l, r) -> Tge (with_dummy_loc Tyint, map_mterm m ctx l, map_mterm m ctx r)
| M.Mlt (l, r) -> Tlt (with_dummy_loc Tyint, map_mterm m ctx l, map_mterm m ctx r)
| M.Mle (l, r) -> Tle (with_dummy_loc Tyint, map_mterm m ctx l, map_mterm m ctx r)
| M.Mvarlocal v -> Tvar (map_lident v)
| M.Mvarparam v -> Tvar (map_lident v)
| M.Mint v -> Tint v
| M.Mdotasset (e,i) -> Tdot (map_mterm m ctx e, mk_loc (loc i) (Tvar (map_lident i)))
| M.Munshallow (a,e) ->
Tapp (loc_term (Tvar ("unshallow_"^a)),
[map_mterm m ctx (M.mk_mterm (M.Mvarstorecol (dumloc a))
(M.Tcontainer (Tasset (dumloc a),Collection)));
map_mterm m ctx e])
| M.Mshallow (a,e) -> Tapp (loc_term (Tvar ("shallow_"^a)),[map_mterm m ctx e])
| M.Mcontains (a,_,r) -> Tapp (loc_term (Tvar ("contains_"^a)),[map_mterm m ctx r])
| M.Maddfield (a,f,c,i) ->
let t,_,_ = M.Utils.get_partition_asset_key m (dumloc a) (dumloc f) in
mk_trace_seq m
(Tapp (loc_term (Tvar ("add_"^a^"_"^f)),
[map_mterm m ctx c; map_mterm m ctx i]))
[CUpdate f; CAdd t]
| M.Mget (n,k) -> Tapp (loc_term (Tvar ("get_"^n)),[map_mterm m ctx k])
| M.Maddshallow (n,l) ->
let pa = M.Utils.get_partition_assets m n |> List.map (fun a ->
CAdd (String.capitalize_ascii a)) in
mk_trace_seq m
(Tapp (loc_term (Tvar ("add_shallow_"^n)),List.map (map_mterm m ctx) l))
([CAdd n] @ pa)
| M.Maddasset (n,i) ->
mk_trace_seq m
(Tapp (loc_term (Tvar ("add_"^n)),[map_mterm m ctx i ]))
[CAdd n]
| M.Mrecord l ->
let asset = M.Utils.get_asset_type mt in
let fns = M.Utils.get_field_list m asset |> wdl in
Trecord (None,(List.combine fns (List.map (map_mterm m ctx) l)))
| M.Mlisttocoll (n,l) -> Tapp (loc_term (Tvar ("listtocoll_"^n)),[map_mterm m ctx l])
| M.Marray l ->
begin
match mt.type_ with
| Tcontainer (_,_) -> Tlist (l |> List.map (map_mterm m ctx))
| _ -> assert false
end
| M.Mletin ([id],v,_,b,_o) ->
Tletin (M.Utils.is_local_assigned id b,map_lident id,None,map_mterm m ctx v,map_mterm m ctx b)
| M.Mselect (a,l,r) ->
let args = extract_args r in
let id = mk_select_name m a r in
let argids = args |> List.map (fun (e,_,_) -> e) |> List.map (map_mterm m ctx) in
Tapp (loc_term (Tvar id),argids @ [map_mterm m ctx l])
| M.Mnow -> Tnow (with_dummy_loc gs)
| M.Mseq l -> Tseq (List.map (map_mterm m ctx) l)
| M.Mfor (id,c,b,lbl) ->
let (nth,card) = get_for_fun c.type_ in
Tfor (with_dummy_loc "i",
with_dummy_loc (
Tminus (with_dummy_loc Tyunit,card (map_mterm m ctx c |> unloc_term),
(loc_term (Tint Big_int.unit_big_int)))
),
mk_invariants m ctx lbl b,
with_dummy_loc (
Tletin (false,
map_lident id,
None,
nth (Tvar "i",map_mterm m ctx c |> unloc_term),
map_mterm m ctx b)))
| M.Massign (ValueAssign,id,v) -> Tassign (with_dummy_loc (Tvar (map_lident id)),map_mterm m ctx v)
| M.Massign (MinusAssign,id,v) -> Tassign (with_dummy_loc (Tvar (map_lident id)),
with_dummy_loc (
Tminus (with_dummy_loc Tyint,
with_dummy_loc (Tvar (map_lident id)),
map_mterm m ctx v)))
| M.Massignfield (ValueAssign,{node = M.Mvarstorecol id1},id2,v) ->
let id = with_dummy_loc (Tdoti (map_lident id1,map_lident id2)) in
Tassign (id,map_mterm m ctx v)
| M.Massignfield (MinusAssign,{node = M.Mvarstorecol id1},id2,v) ->
let id = with_dummy_loc (Tdoti (map_lident id1,map_lident id2)) in
Tassign (id,
with_dummy_loc (
Tminus (with_dummy_loc Tyint,
id,
map_mterm m ctx v)))
| M.Mset (a,l,k,v) ->
let asset =
match k.node with
| M.Mdotasset (a,_) -> map_mterm m ctx a
| _ -> with_dummy_loc (Tapp (loc_term (Tvar ("get_"^a)),
[map_mterm m ctx v]))
in
mk_trace_seq m
(Tletin (false,
with_dummy_loc ("_old"^a),
None,
asset,
with_dummy_loc (Tapp (loc_term (Tvar ("set_"^a)),
[
loc_term (Tvar ("_old"^a));
map_mterm m ctx v
]))))
(List.map (fun f -> CUpdate f) l)
| M.Mremovefield (a,f,k,v) ->
let t,_,_ = M.Utils.get_partition_asset_key m (dumloc a) (dumloc f) in
let asset =
match v.node with
| M.Mdotasset (a,_) -> map_mterm m ctx a
| _ -> with_dummy_loc (Tapp (loc_term (Tvar ("get_"^t)),
[map_mterm m ctx v]))
in
mk_trace_seq m
(Tletin (false,
with_dummy_loc ("_rm"^t),
None,
asset,
with_dummy_loc (Tapp (loc_term (Tvar ("remove_"^a^"_"^f)),
[
map_mterm m ctx k;
loc_term (Tvar ("_rm"^t))
]
))))
[CUpdate f; CRm t]
| M.Mremoveasset (n,a) ->
mk_trace_seq m
(Tletin (false,
with_dummy_loc ("_rm"^n),
None,
with_dummy_loc (Tapp (loc_term (Tvar ("get_"^n)),
[map_mterm m ctx a])),
with_dummy_loc (Tapp (loc_term (Tvar ("remove_"^n)),
[loc_term (Tvar ("_rm"^n))]))))
[CRm n]
| M.Msum (a,f,l) ->
Tapp (loc_term (Tvar ((mk_sum_clone_id a (f |> unloc))^".sum")),[map_mterm m ctx l])
| M.Mapp (f,args) ->
Tapp (mk_loc (map_lident f).loc (Tvar (map_lident f)),List.map (map_mterm m ctx) args)
| M.Mminus (l,r) -> Tminus (with_dummy_loc Tyint, map_mterm m ctx l, map_mterm m ctx r)
| M.Mplus (l,r) -> Tplus (with_dummy_loc Tyint, map_mterm m ctx l, map_mterm m ctx r)
| M.Mvarstorecol n ->
let coll =
match ctx.old, ctx.lmod with
| false, Nomod -> mk_ac (n |> unloc)
| false, Added -> mk_ac_added (n |> unloc)
| false, Removed -> mk_ac_rmed (n |> unloc)
| true, Nomod -> mk_ac_old (n |> unloc)
| true, Added -> mk_ac_old_added (n |> unloc)
| true, Removed -> mk_ac_old_rmed (n |> unloc)
in
loc_term coll |> Mlwtree.deloc
| M.Msetbefore c -> map_mterm m { ctx with old = true } c |> Mlwtree.deloc
| M.Msetadded c -> map_mterm m { ctx with lmod = Added } c |> Mlwtree.deloc
| M.Msetremoved c -> map_mterm m { ctx with lmod = Removed } c |> Mlwtree.deloc
| M.Mforall (i,t,None,b) ->
let asset = M.Utils.get_asset_type (M.mk_mterm (M.Mbool false) t) |> unloc in
Tforall (
[[i |> map_lident],loc_type (Tyasset asset)],
map_mterm m ctx b)
| M.Mforall (i,t,Some coll,b) ->
let asset = M.Utils.get_asset_type (M.mk_mterm (M.Mbool false) t) |> unloc in
Tforall (
[[i |> map_lident],loc_type (Tyasset asset)],
with_dummy_loc (Timpl (with_dummy_loc (Tmem (with_dummy_loc asset,
loc_term (Tvar (unloc i)),
map_mterm m ctx coll)),
map_mterm m ctx b)))
| M.Mmem (a,e,c) -> Tmem (with_dummy_loc a, map_mterm m ctx e, map_mterm m ctx c)
| M.Mimply (a,b) -> Timpl (map_mterm m ctx a, map_mterm m ctx b)
| M.Mlabel lbl ->
begin
match M.Utils.get_formula m None (unloc lbl) with
| Some formula -> Tassert (Some (map_lident lbl),map_mterm m ctx formula)
| _ -> assert false
end
| M.Mand (l,r)-> Tand (map_mterm m ctx l,map_mterm m ctx r)
| M.Misempty (l,r) -> Tempty (with_dummy_loc l,map_mterm m ctx r)
| M.Msubsetof (n,l,r) -> Tsubset (with_dummy_loc n,map_mterm m ctx l,map_mterm m ctx r)
| M.Msettoiterate c ->
let n = M.Utils.get_asset_type mt |> map_lident in
Ttoiter (n,with_dummy_loc "i",map_mterm m ctx c)
| _ -> Tnone in
mk_loc mt.loc t
and mk_invariants (m : M.model) ctx (lbl : ident option) lbody =
let loop_invariants =
Option.fold (M.Utils.get_loop_invariants m) [] lbl |>
List.map (fun ((ilbl : M.lident),(i : M.mterm)) ->
let iid =
match lbl,ilbl with
| Some a, b -> (unloc b) ^ "_" ^ a
| None, b -> (unloc b) in
{ id = with_dummy_loc iid; form = map_mterm m ctx i }
) in
let storage_loop_invariants =
M.Utils.get_storage_invariants m None |>
List.fold_left (fun acc (an,inn,t) ->
if is_local_invariant m an t && not (adds_asset m an lbody)
then
let iid =
match lbl with
| Some a -> inn^"_"^a
| _ -> inn in
acc @ [{ id = with_dummy_loc iid;
form = mk_loop_invariant m an (map_mterm m ctx t) }]
else acc
) ([] : (loc_term, loc_ident) abstract_formula list) in
let security_loop_invariants =
m.security.items
|> List.filter (fun i -> is_only_security i.M.predicate)
|> List.map (fun (sec : M.security_item) ->
let id =
match lbl with
| Some a -> (unloc sec.label) ^ "_" ^ a
| _ -> unloc sec.label in
{ id = with_dummy_loc id;
form = map_security_pred `Loop sec.predicate |> loc_term; }
)
in
loop_invariants @ storage_loop_invariants @ security_loop_invariants
let mk_axioms (m : M.model) : (loc_term, loc_typ, loc_ident) abstract_decl list =
List.fold_left (fun acc apiv ->
match apiv with
| M.StorageInvariant (id,asset,formula) ->
acc @ [
Dtheorem (Axiom,
with_dummy_loc (asset^"_"^id^"_axiom"),
mk_axiom_invariant m asset (map_mterm m init_ctx formula));
Dtheorem (Lemma,
with_dummy_loc (asset^"_"^id^"_axiom_2"),
mk_axiom2_invariant m asset (map_mterm m init_ctx formula))]
) [] m.api_verif
let mk_api_precond m apid a src =
M.Utils.get_storage_invariants m (Some a)
|> List.fold_left (fun acc (_,lbl,t) ->
if is_local_invariant m a t then
acc @ [{
id = "require_"^apid^"_"^lbl;
form = unloc_term (mk_invariant m (dumloc a) src (map_mterm m init_ctx t))
}]
else acc
) []
let mk_key_found_cond old asset var =
Tcontains (asset,
var,
match old with
| `Old -> mk_ac_old asset
| `Curr -> mk_ac asset)
let mk_not_found_cond old asset var = Tnot (mk_key_found_cond old asset var)
let mk_get_field_from_pos asset field = Dfun {
name = "get_"^field;
logic = Logic;
args = ["c",Tycoll asset; "i",Tyint];
returns = Tyint;
raises = [];
variants = [];
requires = [];
ensures = [];
body = Tapp (Tvar field,
[
Tnth (asset,
Tvar "i",
Tvar "c")
])
}
let mk_get_asset asset key ktyp = Dfun {
name = "get_"^asset;
logic = NoMod;
args = ["k",ktyp];
returns = Tyasset asset;
raises = [ Timpl (Texn Enotfound,
mk_not_found_cond `Old asset (Tvar "k"))];
variants = [];
requires = [];
ensures = [
{ id = "get_"^asset^"_post_1";
form = Tmem (asset,
Tresult,
mk_ac asset);
};
{ id = "get_"^asset^"_post_2";
form = Teq(Tyint,
Tdot (Tresult,
Tvar key),
Tvar "k")
}
];
body = Tif (mk_not_found_cond `Curr asset (Tvar "k"),
Traise Enotfound,
Some (Tget (asset,
mk_ac asset,
Tvar "k")))
}
let mk_set_sum_ensures m a =
List.fold_left (fun acc f ->
acc @ [{
id = "set_"^a^"_sum_post";
form = Teq (Tyint,
Tapp (Tvar ((mk_sum_clone_id a f)^".sum"),
[mk_ac_old a]),
Tplus (Tyint,
Tminus (Tyint,
Tapp (Tvar ((mk_sum_clone_id a f)^".sum"),
[mk_ac a]),
Tdoti("new_asset",f)),
Tdot(
Tvar "old_asset",
Tvar f)))
}]) [] (M.Utils.get_sum_fields m a)
let mk_set_ensures m n key fields =
snd (List.fold_left (fun (i,acc) (f:field) ->
if compare f.name key = 0 then
(i,acc)
else
(succ i,acc@[{
id = "set_"^n^"_post"^(string_of_int i);
form = Teq (Tyint,
Tapp (Tvar f.name,
[Tget (n,
mk_ac n,
Tdoti ("old_asset",
key))]),
Tapp (Tvar f.name,
[Tvar ("new_asset")]))
}])
) (1,[]) fields) @ (mk_set_sum_ensures m n)
let mk_set_asset_precond m apid a id = mk_api_precond m apid a (`Preasset id)
let mk_set_asset m key = function
| Drecord (asset, fields) -> Dfun {
name = "set_"^asset;
logic = NoMod;
args = ["old_asset", Tyasset asset; "new_asset", Tyasset asset];
returns = Tyunit;
raises = [ Timpl (Texn Enotfound,
mk_not_found_cond `Old asset (Tdoti ("old_asset",key)))];
variants = [];
requires = mk_set_asset_precond m ("set_"^asset) asset "new_asset";
ensures = mk_set_ensures m asset key fields;
body = Tif (mk_not_found_cond `Curr asset (Tdoti ("old_asset",key)),
Traise Enotfound,
Some (
Tassign (mk_ac asset,
Tset (asset,
mk_ac asset,
Tdoti ("old_asset",key),
Tvar ("new_asset")))
))
}
| _ -> assert false
let mk_contains asset keyt = Dfun {
name = "contains_"^asset;
logic = NoMod;
args = ["k",keyt];
returns = Tybool;
raises = [];
variants = [];
requires = [];
ensures = [
{ id = asset^"_contains_1";
form = Teq(Tyint, Tvar "result", Tcontains (asset,
Tvar "k",
mk_ac asset))
}];
body = Tcontains (asset,
Tvar "k",
mk_ac asset)
}
let mk_unshallow asset keyt = Dfun {
name = "unshallow_"^asset;
logic = Logic;
args = ["c",Tycoll asset;"l",Tylist keyt];
returns = Tycoll asset;
raises = [];
variants = [];
requires = [];
ensures = [
{ id = asset^"_unshallow_post_1";
form = Tsubset (asset,
Tresult,
Tvar "c")
}];
body = Tunshallow (asset,
Tvar "c",
Tvar "l")
}
let mk_add_asset_precond m apid a id = mk_api_precond m apid a (`Preasset id)
let mk_listtocoll_precond m apid a id = mk_api_precond m apid a (`Prelist id)
let mk_listtocoll m asset = Dfun {
name = "listtocoll_"^asset;
logic = Logic;
args = ["l",Tylist (Tyasset asset)];
returns = Tycoll asset;
raises = [];
variants = [];
requires = mk_listtocoll_precond m ("listtocoll_"^asset) asset "l";
ensures = [
];
body = Tcoll (asset,
Tvar "l")
}
let gen_field_getter n _field = Dfun {
name = n;
logic = Logic;
args = [];
returns = Tyunit;
raises = [];
variants = [];
requires = [];
ensures = [];
body = Tnone;
}
let gen_field_getters = function
| Drecord (n,fs) ->
List.map (gen_field_getter n) fs
| _ -> assert false
let mk_add_sum_ensures m a e =
List.fold_left (fun acc f ->
acc @ [{
id = "add_"^a^"_sum_post";
form = Teq (Tyint,
Tapp (Tvar ((mk_sum_clone_id a f)^".sum"),
[mk_ac a]),
Tplus (Tyint,
Tapp (Tvar ((mk_sum_clone_id a f)^".sum"),
[mk_ac_old a]),
Tdoti(e,
f)))
}]) [] (M.Utils.get_sum_fields m a)
let mk_add_ensures m p a e =
[
{ id = p^"_post_1";
form = Tmem (a,
Tvar e,
mk_ac a)
};
{ id = p^"_post_2";
form = Teq (Tycoll a,
mk_ac a,
Tunion (a,
mk_ac_old a,
Tsingl (a,
Tvar e)));
};
{ id = p^"_post_3";
form = Teq (Tycoll a,
mk_ac_added a,
Tunion (a,
mk_ac_old_added a,
Tsingl (a,
Tvar e)));
};
{ id = p^"_post_4";
form = Tempty (a,
Tinter (a,
mk_ac_old_added a,
Tsingl (a,
Tvar e)
));
};
] @ (mk_add_sum_ensures m a e)
let mk_add_asset m asset key : decl = Dfun {
name = "add_"^asset;
logic = NoMod;
args = ["new_asset",Tyasset asset];
returns = Tyunit;
raises = [ Timpl (Texn Ekeyexist,
mk_not_found_cond `Old asset (Tdoti ("new_asset", key)))];
variants = [];
requires = mk_add_asset_precond m ("add_"^asset) asset "new_asset";
ensures = mk_add_ensures m ("add_"^asset) asset "new_asset";
body = Tseq [
Tif (mk_not_found_cond `Curr asset (Tdoti ("new_asset", key)),
Traise Ekeyexist,
Some (Tseq [
Tassign (mk_ac asset,
Tadd (asset,
mk_ac asset,
Tvar "new_asset"));
Tassign (mk_ac_added asset,
Tadd (asset,
mk_ac_added asset,
Tvar "new_asset"))
]
))
];
}
let mk_rm_sum_ensures m a e =
List.fold_left (fun acc f ->
acc @ [{
id = "remove_"^a^"_sum_post";
form = Teq (Tyint,
Tapp (Tvar ((mk_sum_clone_id a f)^".sum"),
[mk_ac a]),
Tminus (Tyint,
Tapp (Tvar ((mk_sum_clone_id a f)^".sum"),
[mk_ac_old a]),
Tdoti(e,
f)))
}]) [] (M.Utils.get_sum_fields m a)
let mk_rm_ensures m p a e =
[
{ id = p^"_post1";
form = Tnot (Tmem (a,
Tvar (e),
mk_ac a))
};
{ id = p^"_post2";
form = Teq (Tycoll a,
mk_ac a,
Tdiff (a,
mk_ac_old a,
Tsingl (a,
Tvar e)))
};
{ id = p^"_post3";
form = Teq (Tycoll a,
mk_ac_rmed a,
Tunion (a,
mk_ac_old_rmed a,
Tsingl (a,
Tvar e)))
}
] @ (mk_rm_sum_ensures m a e)
let mk_rm_asset m asset key : decl =
Dfun {
name = "remove_"^asset;
logic = NoMod;
args = ["a", Tyasset asset];
returns = Tyunit;
raises = [Timpl (Texn Enotfound,
mk_not_found_cond `Old asset (Tdoti ("a",key)))];
variants = [];
requires = [];
ensures = mk_rm_ensures m ("remove_"^asset) asset "a";
body = Tif (mk_not_found_cond `Curr asset (Tdoti ("a",key)),
Traise Enotfound,
Some (
Tseq [
Tassign (mk_ac_rmed asset,
Tadd (asset,
mk_ac_rmed asset,
Tget(asset,
mk_ac asset,
Tdoti ("a",
key))));
Tassign (mk_ac asset,
Tremove (asset,
mk_ac asset,
Tdoti ("a",
key)))
]));
}
let mk_add_partition_field m a ak pf adda addak : decl =
let addak_cond = mk_key_found_cond `Old adda (Tdoti ("new_asset", addak)) in
let akey = Tapp (Tvar ak,[Tvar "asset"]) in
let addak = Tapp (Tvar addak,[Tvar "new_asset"]) in
Dfun {
name = "add_"^a^"_"^pf;
logic = NoMod;
args = ["asset",Tyasset a; "new_asset",Tyasset adda];
returns = Tyunit;
raises = [Timpl (Texn Enotfound,
mk_not_found_cond `Old a (Tdoti ("asset",ak)));
Timpl (Texn Ekeyexist,
addak_cond)];
variants = [];
requires = mk_add_asset_precond m ("add_"^a^"_"^pf) adda "new_asset";
ensures = mk_add_ensures m ("add_"^a^"_"^pf) adda "new_asset";
body =
Tif (mk_not_found_cond `Curr a (Tdoti ("asset",ak)),
Traise Enotfound,
Some (Tseq [
Tapp (Tvar ("add_"^adda),
[Tvar "new_asset"]);
Tletin (false, a^"_"^pf,None,
Tapp (Tvar pf,[Tvar "asset"]),
Tletin (false,"new_"^a^"_"^pf,None,
Tcons (addak,Tvar (a^"_"^pf)),
Tletin (false,"new_asset",None,
Trecord (Some (Tvar "asset"),
[pf,Tvar ("new_"^a^"_"^pf)]),
Tassign (mk_ac a,
Tset (a,
mk_ac a,
akey,
Tvar ("new_asset")))
)))]));
}
let mk_rm_partition_field m asset keyf f rmed_asset rmkey : decl = Dfun {
name = "remove_"^asset^"_"^f;
logic = NoMod;
args = ["asset",Tyasset asset; "rm_asset",Tyasset rmed_asset];
returns = Tyunit;
raises = [Timpl (Texn Enotfound,
mk_not_found_cond `Old asset (Tdoti ("asset",keyf))) ];
variants = [];
requires = [];
ensures = mk_rm_ensures m ("remove_"^asset^"_"^f) rmed_asset "rm_asset";
body =
Tif (mk_not_found_cond `Curr asset (Tdoti ("asset",keyf)),
Traise Enotfound,
Some (
Tletin (false,
asset^"_"^f,
None,
Tapp (Tvar f,
[Tvar ("asset")]),
Tletin (false,
"new_"^asset^"_"^f,
None,
Tlistremove (gArchetypeList,
Tvar (asset^"_"^f),
Tdoti ("rm_asset",
rmkey)),
Tletin (false,
"new_"^asset^"_asset",
None,
Trecord (Some (Tvar ("asset")),
[f,Tvar ("new_"^asset^"_"^f)]),
Tseq [
Tassign (mk_ac asset,
Tset (asset,
mk_ac asset,
Tdoti ("asset",
keyf),
Tvar ("new_"^asset^"_asset")));
Tapp (Tvar ("remove_"^rmed_asset),
[Tvar ("rm_asset")])
]
)))));
}
let mk_storage_api (m : M.model) records =
m.api_items |> List.fold_left (fun acc (sc : M.api_item) ->
match sc.node_item with
| M.APIStorage (Get n) ->
let k,kt = M.Utils.get_asset_key m (dumloc n) in
acc @ [mk_get_asset n k (kt |> map_btype)]
| M.APIStorage (Add n) ->
let k = M.Utils.get_asset_key m (dumloc n) |> fst in
acc @ [mk_add_asset m n k]
| M.APIStorage (Remove n) ->
let kt = M.Utils.get_asset_key m (dumloc n) |> fst in
acc @ [mk_rm_asset m n kt]
| M.APIStorage (Set n) ->
let record = get_record n (records |> unloc_decl) in
let k = M.Utils.get_asset_key m (get_record_name record |> dumloc) |> fst in
acc @ [mk_set_asset m k record]
| M.APIStorage (UpdateAdd (a,pf)) ->
let k = M.Utils.get_asset_key m (dumloc a) |> fst in
let (pa,addak,_) = M.Utils.get_partition_asset_key m (dumloc a) (dumloc pf) in
acc @ [
mk_add_partition_field m a k pf pa addak
]
| M.APIStorage (UpdateRemove (n,f)) ->
let t = M.Utils.get_asset_key m (dumloc n) |> fst in
let (pa,pk,_) = M.Utils.get_partition_asset_key m (dumloc n) (dumloc f) in
acc @ [
mk_rm_partition_field m n t f pa pk
]
| M.APIFunction (Contains n) ->
let t = M.Utils.get_asset_key m (dumloc n) |> snd |> map_btype in
acc @ [ mk_contains n t ]
| M.APIFunction (Select (asset,test)) ->
let mlw_test = map_mterm m init_ctx test in
acc @ [ mk_select m asset test (mlw_test |> unloc_term) sc.only_formula ]
| M.APIFunction (Sum (asset,field)) when compare asset "todo" <> 0 ->
let key = M.Utils.get_asset_key m (dumloc asset) |> fst in
acc @ [ mk_get_field_from_pos asset field;
mk_sum_clone m asset key field ]
| M.APIFunction (Unshallow n) ->
let t = M.Utils.get_asset_key m (dumloc n) |> snd |> map_btype in
acc @ [ mk_unshallow n t ]
| M.APIFunction (Listtocoll n) ->
acc @ [ mk_listtocoll m n ]
| _ -> acc
) [] |> loc_decl |> deloc
let fold_exns body : term list =
let rec internal_fold_exn acc (term : M.mterm) =
match term.M.node with
| M.Mget _ -> acc @ [Texn Enotfound]
| M.Maddasset _ -> acc @ [Texn Ekeyexist]
| M.Maddfield _ -> acc @ [Texn Enotfound; Texn Ekeyexist]
| M.Mfail InvalidCaller -> acc @ [Texn Einvalidcaller]
| M.Mfail NoTransfer -> acc @ [Texn Enotransfer]
| M.Mfail (InvalidCondition _) -> acc @ [Texn Einvalidcondition]
| M.Mremoveasset _ -> acc @ [Texn Enotfound]
| _ -> M.fold_term internal_fold_exn acc term in
Tools.List.dedup (internal_fold_exn [] body)
let is_fail (t : M.mterm) =
match t.node with
| M.Mfail _ -> true
| _ -> false
let flatten_if_fail m (t : M.mterm) : loc_term =
let rec rec_flat acc (t : M.mterm) : loc_term list =
match t.node with
| M.Mif (c,th, Some e) when is_fail th ->
rec_flat (acc@[mk_loc t.loc (Tif (map_mterm m init_ctx c, map_mterm m init_ctx th,None))]) e
| _ -> acc @ [map_mterm m init_ctx t] in
mk_loc t.loc (Tseq (rec_flat [] t))
let mk_ensures m acc (v : M.specification) =
acc @ (List.map (fun (spec : M.postcondition) -> {
id = spec.name |> map_lident;
form = map_mterm m init_ctx spec.formula
}) (v.postconditions |> List.filter M.Utils.is_post))
let mk_require n i t = {
id = with_dummy_loc (n^"_require_"^(string_of_int i));
form = t
}
let mk_requires m n v =
M.Utils.get_added_removed_sets m v
|> List.map (fun t ->
match t with
| M.Msetadded e ->
let a = M.Utils.get_asset_type e |> unloc in
loc_term (Tempty (a,mk_ac_added a))
| M.Msetremoved e ->
let a = M.Utils.get_asset_type e |> unloc in
loc_term (Tempty (a,mk_ac_rmed a))
| _ -> assert false
)
|> Tools.List.dedup
|> List.mapi (fun i t -> mk_require n i t)
let mk_preconds m args body : ((loc_term,loc_ident) abstract_formula) list =
List.fold_left (fun acc (id,t,_) ->
match t with
| M.Tasset n ->
let n = unloc n in
M.Utils.get_storage_invariants m (Some n)
|> List.fold_left (fun acc (_,lbl,t) ->
if is_local_invariant m n t && adds_asset m n body then
acc @ [{
id = with_dummy_loc lbl;
form = mk_pre_asset m n (unloc id) (map_mterm m init_ctx t)
}]
else
acc
) []
| M.Tcontainer (Tasset n, Collection)
| M.Tcontainer (Tasset n, Partition) ->
let n = unloc n in
M.Utils.get_storage_invariants m (Some n)
|> List.fold_left (fun acc (_,lbl,t) ->
if is_local_invariant m n t && adds_asset m n body then
acc @ [{
id = with_dummy_loc lbl;
form = mk_pre_coll m n (unloc id) (map_mterm m init_ctx t)
}]
else
acc
) []
| _ -> acc
) [] args
let is_src src (_,f,_) = f.M.src = src
let mk_entry_require m idents =
if M.Utils.with_trace m && List.length idents > 0 then
let mk_entry_eq id = Teq (Tyint,
Tdoti (gs,mk_id "entry"),
Tsome (Tvar (String.capitalize_ascii id))) in
[
{
id = with_dummy_loc "entry_require";
form =
List.fold_left (fun acc id ->
Tor (acc,mk_entry_eq id)
) (mk_entry_eq (List.hd idents)) (List.tl idents)
|> loc_term
};
{
id = with_dummy_loc "empty_trace";
form = Teq (Tyint,
Tdoti (gs,mk_id "tr"),
Tnil)
|> loc_term
}
]
else []
let add_raise_ctx args src m exn =
match src with
| M.Endo ->
begin
match exn with
| Texn Ekeyexist ->
if List.length args > 0 then
let mk_ctx (id,t) =
let id = Mlwtree.deloc id in
match Mlwtree.deloc t with
| Tyasset a ->
let a = Mlwtree.deloc a in
let key,_ = M.Utils.get_asset_key m (dumloc a) in
mk_key_found_cond `Old a (Tdoti (a,key))
| Typartition a | Tycoll a ->
let a = Mlwtree.deloc a in
let key,_ = M.Utils.get_asset_key m (dumloc a) in
Texists ([["a"],Tyasset a],
Tand (mk_key_found_cond `Old a (Tdoti("a",key)),
Tcontains (a,
Tdoti ("a",key),
Tvar (id))))
| _ -> exn
in
let ctx = List.fold_left (fun acc arg ->
Tor (acc,mk_ctx arg)
) (mk_ctx (List.hd args)) (List.tl args) in
Timpl (exn, ctx)
else exn
| _ -> exn
end
| _ -> exn
let mk_functions src m =
M.Utils.get_functions m |> List.filter (is_src src) |> List.map (
fun ((v : M.specification option),
(s : M.function_struct),
(t : M.type_)) ->
let args = (List.map (fun (i,t,_) ->
(map_lident i, map_mtype t)
) s.args) in
Dfun {
name = map_lident s.name;
logic = NoMod;
args = args;
returns = map_mtype t;
raises = fold_exns s.body |> List.map (add_raise_ctx args src m) |> List.map loc_term;
variants = [];
requires =
(mk_entry_require m (M.Utils.get_callers m (unloc s.name))) @
(mk_requires m (s.name |> unloc) v) @
(mk_preconds m s.args s.body);
ensures = Option.fold (mk_ensures m) [] v;
body = flatten_if_fail m s.body;
}
)
let mk_endo_functions = mk_functions M.Endo
let mk_exo_functions = mk_functions M.Exo
let mk_entries m =
M.Utils.get_entries m |> List.map (
fun ((v : M.specification option),
(s : M.function_struct)) ->
Dfun {
name = map_lident s.name;
logic = NoMod;
args = (List.map (fun (i,t,_) ->
(map_lident i, map_mtype t)
) s.args);
returns = with_dummy_loc Tyunit;
raises = fold_exns s.body |> List.map loc_term;
variants = [];
requires = (mk_entry_require m [unloc s.name]) @
(mk_requires m (unloc s.name) v);
ensures = Option.fold (mk_ensures m) [] v;
body = flatten_if_fail m s.body;
}
)
let rm_fail_exn = List.filter (fun e ->
match unloc_term e with
| Texn Enotfound
| Texn Ekeyexist -> false
| _ -> true)
let process_no_fail m (d : (loc_term, loc_typ, loc_ident) abstract_decl) =
match d with
| Dfun f ->
begin
match M.Utils.no_fail m (Mlwtree.deloc f.name) with
| Some id ->
Dfun { f with
raises = rm_fail_exn f.raises;
body = loc_term (Ttry (unloc_term f.body,
[Enotfound,Tassert (Some ("security_"^(unloc id)),Tfalse);
Ekeyexist,Tassert (Some ("security_"^(unloc id)),Tfalse)]));
}
| _ -> d
end
| _ -> d
let to_whyml (m : M.model) : mlw_tree =
let storage_module = with_dummy_loc (String.capitalize_ascii (m.name.pldesc^"_storage")) in
let uselib = mk_use in
let uselist = mk_use_list in
let traceutils = mk_trace_utils m |> deloc in
let records = M.Utils.get_records m |> List.map (map_record m) |> wdl in
let eq_assets = M.Utils.get_records m |> List.map (mk_eq_asset m) |> wdl in
let eq_exten = M.Utils.get_records m |> List.map (mk_eq_extensionality m) |> deloc in
let clones = M.Utils.get_assets m |> List.map (record_to_clone m) |> wdl in
let init_records = records |> unloc_decl |> List.map mk_default_init |> loc_decl in
let records = zip records eq_assets init_records clones |> deloc in
let storage = M.Utils.get_storage m |> map_storage m in
let storageval = Dval (with_dummy_loc gs, with_dummy_loc Tystorage) in
let axioms = mk_axioms m in
let storage_api = mk_storage_api m (records |> wdl) in
let endo = mk_endo_functions m in
let functions = mk_exo_functions m in
let entries = mk_entries m |> List.map (process_no_fail m) in
let usestorage = mk_use_module storage_module in
let loct : loc_mlw_tree = [{
name = storage_module;
decls = [uselib] @
traceutils @
records @
eq_exten @
[storage;storageval] @
axioms @
storage_api @
endo;
};{
name = cap (map_lident m.name);
decls = [uselib;uselist;usestorage] @
functions @
entries;
}] in unloc_tree loct