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
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
open Location
module M = Model
open Tools
open Mlwtree
type error_desc =
| NotSupported of string
| TODONotTranslated of string
let pp_error_desc fmt = function
| NotSupported msg ->
Format.fprintf fmt
"Not supported: %s" msg
| TODONotTranslated msg ->
Format.fprintf fmt
"Not translated: %s" msg
type error = Location.t * error_desc
let emit_error (lc, error) =
let str : string = Format.asprintf "%a@." pp_error_desc error in
let pos : Position.t list = [location_to_position lc] in
Error.error_alert pos str (fun _ -> ())
let dl = with_dummy_loc
let gArchetypeDir = "archetype"
let gArchetypeLib = "Lib"
let gArchetypeField = "Field"
let gArchetypeView = "View"
let gArchetypeColl = "Collection"
let gArchetypeAgg = "Aggregate"
let gArchetypeSum = "Sum"
let gArchetypeSort = "Sort"
let gArchetypeTrace = "Trace"
let gArchetypeSet = "Set"
let gArchetypeList = "List"
let gOperations = "ops"
let gListAs = "L"
let gFieldAs = "F"
let gViewAs = "V"
let mk_module_name id =
if compare (String.get id 0) '_' = 0 then
String.capitalize_ascii (String.sub id 1 ((String.length id) - 1))
else String.capitalize_ascii id
let mk_id i = "_" ^ i
let mk_param_value i = "_param_value_" ^ 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 mk_ac_unmvd_id a = mk_id (a ^ "_assets_unmoved")
let mk_aggregate_id aggid = gArchetypeAgg ^ "_" ^ aggid
let gs = "_s"
let gsinit = "_s_init"
let gsarg = "_s_arg"
let mk_ac_st s a = Tdoti (s, mk_ac_id a)
let mk_ac_old_st s a = Tdot (Told (Tvar s), Tvar (mk_ac_id a))
let mk_ac a = mk_ac_st gs a
let mk_ac_old a = mk_ac_old_st gs a
let mk_ac_added_st s a = Tdoti (s, mk_ac_added_id a)
let mk_ac_old_added_st s a = Tdot (Told (Tvar s), Tvar (mk_ac_added_id a))
let mk_ac_added a = mk_ac_added_st gs a
let mk_ac_old_added a = mk_ac_old_added_st gs a
let mk_ac_rmed_st s a = Tdoti (s, mk_ac_rmed_id a)
let mk_ac_old_rmed_st s a = Tdot (Told (Tvar s), Tvar (mk_ac_rmed_id a))
let mk_ac_rmed a = mk_ac_rmed_st gs a
let mk_ac_old_rmed a = mk_ac_old_rmed_st gs a
let mk_field_id a = gArchetypeField^ "_" ^ a
let mk_view_id a = gArchetypeView ^ "_" ^ a
let mk_use_list = Duse (false,["list";"List"],Some gListAs) |> loc_decl |> deloc
let mk_use = Duse (false,[gArchetypeDir;gArchetypeLib],None) |> loc_decl |> deloc
let mk_use_field = Duse (false,[gArchetypeDir;gArchetypeField],Some gFieldAs) |> loc_decl |> deloc
let mk_use_view = Duse (false,[gArchetypeDir;gArchetypeView],Some gViewAs) |> loc_decl |> deloc
let mk_use_module m = Duse (false,[deloc m],None) |> loc_decl |> deloc
let mk_use_euclidean_div m =
if M.Utils.with_division m then
[Duse (true,["int";"EuclideanDivision"],None) |> loc_decl |> deloc]
else []
let mk_use_min_max m =
if M.Utils.with_min_max m then
[Duse (true,["int";"MinMax"],None) |> loc_decl |> deloc]
else []
let map_lident (i : M.lident) : loc_ident = {
obj = i.pldesc;
loc = i.plloc;
}
let map_btype = function
| M.Bunit -> Tyunit
| M.Bbool -> Tybool
| M.Bint -> Tyint
| M.Brational -> Tyrational
| M.Bdate -> Tydate
| M.Bduration -> Tyduration
| M.Btimestamp -> Tyint
| M.Bstring -> Tystring
| M.Baddress -> Tyaddr
| M.Bcurrency -> Tytez
| M.Bsignature -> Tysignature
| M.Bkey -> Tykey
| M.Bkeyhash -> Tykeyhash
| M.Bbytes -> Tybytes
| M.Bnat -> Tyuint
| M.Bchainid -> Tychainid
| M.Bbls12_381_fr -> Tybls12_381_fr
| M.Bbls12_381_g1 -> Tybls12_381_g1
| M.Bbls12_381_g2 -> Tybls12_381_g2
| M.Bnever -> Tynever
let get_type_idx t = List.index_of (M.cmp_type t)
let mk_map_name m (t : M.type_) = "map"^(string_of_int (get_type_idx t (M.Utils.get_all_map_types m)))
let mk_set_name m (t : M.type_) = "set"^(string_of_int (get_type_idx t (M.Utils.get_all_set_types m)))
let mk_list_name m (t : M.type_) = "list"^(string_of_int (get_type_idx t (M.Utils.get_all_list_types m)))
let rec map_mtype m (t : M.type_) : loc_typ =
dl (match M.get_ntype 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 (dl (mk_field_id (unloc id)))
| M.Tcontainer ((Tasset id, _),M.Aggregate) -> Tyaggregate (dl (mk_field_id (unloc id)))
| M.Tcontainer ((Tasset id, _),M.View) -> Tyview (dl (mk_view_id (unloc id)))
| M.Tcontainer ((Tasset id, _),M.Collection) -> Tycoll (map_lident id)
| M.Toption t -> Tyoption (map_mtype m t)
| M.Ttuple l -> Tytuple (l |> List.map (map_mtype m))
| M.Tunit -> Tyunit
| M.Tstate -> Tystate
| M.Tmap (_, _, _) -> Tycoll (dl (mk_map_name m t))
| M.Tstorage -> Tystorage
| M.Toperation -> Tyoperation
| M.Tprog _ -> Tyunit
| M.Tvset _ -> Tyunit
| M.Ttrace _ -> Tyunit
| M.Tset t -> Tyset (dl (mk_set_name m (M.tset t)))
| M.Tlist t -> Tylist (map_mtype m t)
| M.Tcontract _ -> Tycontract
| M.Trecord id -> Tyrecord (map_lident id)
| M.Tor (a, b) -> Tyor (map_mtype m a, map_mtype m b)
| M.Tlambda (a, b) -> Tylambda (map_mtype m a, map_mtype m b)
| M.Tcontainer (_, _)
| M.Tticket _
| M.Tsapling_state _
| M.Tsapling_transaction _
-> print_endline (Format.asprintf "%a@." M.pp_type_ t); assert false)
let mk_list_name_from_mlwtype m t =
let idx =
M.Utils.get_all_list_types m
|> List.map (map_mtype m)
|> List.map unloc_type
|> List.index_of (cmp_type (Tylist t)) in
"List" ^ (string_of_int idx)
let rec mk_eq_type m e1 e2 = function
| Tyunit -> Ttrue
| Tybool -> Tor (Tpand (Tvar e1,Tvar e2),Tpand(Tnot (Tvar e1), Tnot (Tvar e2)))
| Tyrational -> Tapp (Tvar "rat_eq",[Tvar e1; Tvar e2])
| Tykeyhash -> Teq (Tykeyhash, Tvar e1, Tvar e2)
| Tystring -> Teq (Tystring, Tvar e1, Tvar e2)
| Tybytes -> Teq (Tybytes, Tvar e1, Tvar e2)
| Tyaddr -> Teq (Tyaddr, Tvar e1, Tvar e2)
| Tyasset a -> Tapp (Tvar ("eq_"^a),[Tvar e1; Tvar e2])
| Typartition a -> Teqfield(a, Tvar e1, Tvar e2)
| Tyaggregate a -> Teqfield(a, Tvar e1, Tvar e2)
| Tyenum i -> Tapp (Tvar ("eq_"^i),[Tvar e1; Tvar e2])
| Tyoperation -> Tapp (Tvar "_eq_operation",[Tvar e1; Tvar e2])
| Tylist t -> Tapp (Tdoti (mk_list_name_from_mlwtype m t,"eq_list"),[Tvar e1; Tvar e2])
| Tyoption t -> Tmatch (
Ttuple [Tvar e1; Tvar e2], [
Tpatt_tuple [Tpsome ("_v1"); Tpsome ("_v2")], mk_eq_type m ("_v1") ("_v2") t;
Tpatt_tuple [Twild;Twild], Tfalse
])
| Tytuple l ->
let cmps = List.mapi (fun i t ->
let e1i = e1^(string_of_int i) in
let e2i = e2^(string_of_int i) in
mk_eq_type m e1i e2i t
) l in
let cmp = List.fold_left (fun acc cmp -> Tpand (acc,cmp)) (List.hd cmps) (List.tl cmps) in
Tmatch (
Ttuple [Tvar e1; Tvar e2], [
Tpatt_tuple [
Tpatt_tuple (List.mapi (fun i _ -> Tconst (e1^(string_of_int i)))l);
Tpatt_tuple (List.mapi (fun i _ -> Tconst (e2^(string_of_int i)))l)
], Tif (cmp, Ttrue, Some Tfalse);
Tpatt_tuple [Twild;Twild], Tfalse
])
| Tyor (a, b) -> Tmatch (
Ttuple [Tvar e1; Tvar e2], [
Tpatt_tuple [Tpleft ("_v1"); Tpleft ("_v2")], mk_eq_type m ("_v1") ("_v2") a;
Tpatt_tuple [Tpright ("_v1"); Tpright ("_v2")], mk_eq_type m ("_v1") ("_v2") b;
Tpatt_tuple [Twild; Twild], Tfalse
])
| Tyrecord id -> begin
let r = Model.Utils.get_record m id in
let cmps = List.map (fun (f : M.record_field) ->
let fn = unloc f.name in
let e1i = e1 ^ "." ^ fn in
let e2i = e2 ^ "." ^ fn in
let t : typ = unloc_type (map_mtype m f.type_) in
mk_eq_type m e1i e2i t
) r.fields in
List.fold_left (fun acc cmp -> Tpand (acc,cmp)) (List.hd cmps) (List.tl cmps)
end
| Tyset idx -> begin
Tapp (Tdoti (String.capitalize_ascii idx, "eq_set"),[Tvar e1; Tvar e2])
end
| Tymap idx -> begin
Tapp (Tdoti ("Map" ^ idx, "eq_collection"),[Tvar e1; Tvar e2])
end
| Tycoll idx -> Tapp (Tdoti (String.capitalize_ascii idx, "eq_collection"),[Tvar e1; Tvar e2])
| Tyint
| Tyuint
| Tykey
| Tydate
| Tyduration
| Tytez
| Tysignature
| Tychainid
| Tystorage
| Tycontract
| Tystate
| Tybls12_381_fr
| Tybls12_381_g1
| Tybls12_381_g2
| Tynever
| Tyview _
| Tylambda (_, _)
-> Teq (Tyint, Tvar e1, Tvar e2)
let rec mk_le_type m e1 e2 = function
| Tyunit -> Ttrue
| Tybool -> Tor ((Tnot (Tvar e1), (Tvar e2)))
| Tyrational -> Tapp (Tvar "rat_cmp",[Tvar "OpCmpLe"; Tvar e1; Tvar e2])
| Tystring -> Tle (Tystring, Tvar e1, Tvar e2)
| Tyaddr -> Tle (Tyaddr, Tvar e1, Tvar e2)
| Tytuple l ->
let cmps = List.mapi (fun i t ->
let e1i = e1^(string_of_int i) in
let e2i = e2^(string_of_int i) in
mk_le_type m e1i e2i t
) l in
let cmp = List.fold_left (fun acc cmp -> Tpand(cmp,acc)) (List.hd cmps) (List.tl cmps) in
Tmatch (
Ttuple [Tvar e1; Tvar e2], [
Tpatt_tuple [
Tpatt_tuple (List.mapi (fun i _ -> Tconst (e1^(string_of_int i)))l);
Tpatt_tuple (List.mapi (fun i _ -> Tconst (e2^(string_of_int i)))l)
], Tif (cmp, Ttrue, Some Tfalse);
Tpatt_tuple [Twild;Twild], Tfalse
])
| Tyset idx -> Tapp (Tdoti (String.capitalize_ascii idx, "eq_set"),[Tvar e1; Tvar e2])
| Tylist t -> Tapp (Tdoti (mk_list_name_from_mlwtype m t,"eq_list"),[Tvar e1; Tvar e2])
| Tymap idx -> Tapp (Tdoti ("Map" ^ idx, "eq_collection"),[Tvar e1; Tvar e2])
| Tycoll idx -> Tapp (Tdoti (String.capitalize_ascii idx, "eq_collection"),[Tvar e1; Tvar e2])
| Tyrecord id -> begin
let r = Model.Utils.get_record m id in
let mk g (f : M.record_field) =
let fn = unloc f.name in
let e1i = e1 ^ "." ^ fn in
let e2i = e2 ^ "." ^ fn in
let t : typ = unloc_type (map_mtype m f.type_) in
g m e1i e2i t
in
let mk_le = mk mk_le_type in
let mk_eq = mk mk_eq_type in
let mk_if x accu =
Tif (mk_eq x, accu, Some (mk_le x))
in
match List.rev r.fields with
| [] -> Ttrue
| [x] -> mk_le x
| x::tl -> List.fold_right mk_if (List.rev tl) (mk_le x)
end
| _ -> Tle (Tyint, Tvar e1, Tvar e2)
type change =
| CAdd of ident
| CRm of ident
| CUpdate of ident
| CTransfer of ident
| CGet of ident
| CIterate of ident
| CCall of ident
type trace_id_type =
| Asset
| Entry
| Field
let trace_value_type_to_string = function Asset -> "A" | Entry -> "E" | Field -> "F"
let mk_trace_id trtyp s = (trace_value_type_to_string trtyp) ^ (String.capitalize_ascii s)
let mk_change_term tr =
match tr with
| CAdd id -> Tapp (Tdoti("Tr",
"TrAdd_"),
[Tvar (mk_trace_id Asset id)])
| CRm id -> Tapp (Tdoti("Tr",
"TrRm_"),
[Tvar (mk_trace_id Asset id)])
| CUpdate id -> Tapp (Tdoti("Tr",
"TrUpdate_"),
[Tvar (mk_trace_id Field id)])
| CGet id -> Tapp (Tdoti("Tr",
"TrGet_"),
[Tvar (mk_trace_id Asset id)])
| _ -> assert false
let mk_trace tr =
let gstr = Tdoti(gs,
mk_id "tr") in
Tassign (gstr,
Tcons (gListAs, mk_change_term tr,
gstr)
) |> loc_term
let mk_trace_asset m =
let assets = M.Utils.get_assets m in
if List.length assets > 0 then
[ Denum ("_asset",
assets
|> List.map (fun (a : M.asset) -> mk_trace_id Asset (unloc a.name)))
|> loc_decl]
else []
let mk_trace_entry m =
Denum ("_entry",
M.Utils.get_entries m
|> List.map (fun (_, (f : M.function_struct)) -> mk_trace_id Entry (unloc f.name)))
|> loc_decl
let mk_trace_field m =
Denum ("_field",
(M.Utils.get_vars m
|> List.map (fun (v : M.var) -> mk_trace_id Field (unloc v.name))) @
(M.Utils.get_assets m
|> List.map (fun (a : M.asset) ->
List.map (fun (x : M.asset_item) -> mk_trace_id Field (unloc x.name)) a.values
)
|> List.flatten))
|> loc_decl
let mk_trace_clone () =
Dclone (
[gArchetypeDir;gArchetypeTrace], "Tr", [
Ctype ("_asset", Tyasset "_asset");
Ctype ("_entry", Tyasset "_entry");
Ctype ("_field", Tyasset "_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 = [];
fails = [];
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 init = {
name = dl (to_id asset);
typ = loc_type (Tycoll asset);
init = begin match init with
| Some l -> dl (Tmkcoll (dl asset, l))
| None -> loc_term (Temptycoll asset);
end;
mutable_ = true;
}
let mk_const_fields m = [
{ name = mk_id gOperations ; typ = Tylist Tyoperation ; init = Tnil gListAs; mutable_ = true; };
{ name = mk_id "balance" ; typ = Tytez; init = Tint Big_int.zero_big_int; mutable_ = true; };
{ name = mk_id "transferred" ; typ = Tytez; init = Tint Big_int.zero_big_int; mutable_ = false; };
{ name = mk_id "caller" ; typ = Tyaddr; init = Tdefaultaddr; mutable_ = false; };
{ name = mk_id "source" ; typ = Tyaddr; init = Tdefaultaddr; mutable_ = false; };
{ name = mk_id "now" ; typ = Tydate; init = Tint Big_int.zero_big_int; mutable_ = false; };
{ name = mk_id "chainid" ; typ = Tychainid; init = Tint Big_int.zero_big_int; mutable_ = false; };
{ name = mk_id "selfaddress" ; typ = Tyaddr; init = Tdefaultaddr; 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 gListAs; mutable_ = true; }
]
else []
let mk_sum_clone_id m a f = (String.capitalize_ascii a) ^"Sum" ^ (string_of_int (M.Utils.get_sum_idx m a f))
let mk_sum_clone_from_id asset id = (String.capitalize_ascii asset) ^"Sum" ^ (string_of_int id)
let mk_get_sum_value_id asset id = "get_" ^ asset ^ "_sum" ^ (string_of_int id)
let mk_get_sum_value_from_pos_id asset id = (mk_get_sum_value_id asset id)^"_from_pos"
let mk_sum a i v c = Tvsum ( mk_sum_clone_from_id a i, v, c)
let mk_sum_from_col a i c = Tcsum (mk_sum_clone_from_id a i, c)
let mk_sum_clone m asset key tkey formula =
let cap_asset = String.capitalize_ascii asset in
let id = M.Utils.get_sum_idx m asset formula in
Dclone (
[gArchetypeDir;gArchetypeSum],
mk_sum_clone_from_id asset id, [
Ctype ("collection", Tyasset (cap_asset ^ ".collection"));
Ctype ("view", Tyasset ((String.capitalize_ascii (mk_view_id asset))^ ".view"));
Ctype ("t", Tyasset asset);
Ctype ("tk", tkey |> map_mtype m |> unloc_type);
Cval ("field", mk_get_sum_value_id asset id);
Cval ("view_to_list", cap_asset ^ ".view_to_list");
Cval ("to_view", cap_asset ^ ".to_view");
Cval ("empty", (String.capitalize_ascii (mk_view_id asset)) ^ ".empty");
Cval ("contains", (String.capitalize_ascii (mk_view_id asset)) ^ ".contains");
Cval ("nth", (String.capitalize_ascii (mk_view_id asset)) ^ ".nth");
Cval ("head", (String.capitalize_ascii (mk_view_id asset)) ^ ".head");
Cval ("tail", (String.capitalize_ascii (mk_view_id asset)) ^ ".tail");
Cval ("card", (String.capitalize_ascii (mk_view_id asset)) ^ ".card");
Cval ("add", cap_asset ^ ".add");
Cval ("remove", cap_asset ^ ".remove");
Cval ("set", cap_asset ^ ".set");
Cval ("get", cap_asset ^ ".get");
Cval ("keyt", key);
]
)
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_st "s" asset),
Timpl (Tlmem (gListAs,
Tvar "k",
Tapp (Tvar f,
[Tvar "a"])),
Tccontains (pa,
Tvar "k",
mk_ac_st "s" pa)))))
let sort_kind_to_string = function
| M.SKasc -> "asc"
| M.SKdesc -> "desc"
let mk_cmp_function_id asset fields = "cmp_" ^ asset ^ "_" ^
(String.concat "_" (List.map (fun (f,k) ->
f ^ "_" ^ (sort_kind_to_string k)) fields))
let rec mk_cmp_function_body m asset fields =
match fields with
| [field,kind] ->
let a, b =
begin match kind with
| M.SKasc -> Tdoti("a", field),Tdoti("b", field)
| M.SKdesc -> Tdoti("b", field),Tdoti("a", field)
end in
let (_,typ,_) = M.Utils.get_asset_field m (asset,field) in
let t = map_mtype m typ |> unloc_type in
Tle (t, a, b)
| (field,kind)::tl ->
let a, b =
begin match kind with
| M.SKasc -> Tdoti("a", field),Tdoti("b", field)
| M.SKdesc -> Tdoti("b", field),Tdoti("a", field)
end in
Tif (
Tlt (Tyint,a,b),
Ttrue,
Some (Tif (
Teq (Tyint,a,b),
mk_cmp_function_body m asset tl,
Some (Tfalse)
))
)
| [] -> Ttrue
let mk_cmp_function m asset fields =
let decl : (term, typ, ident) abstract_decl = Dfun {
name = mk_cmp_function_id asset fields;
logic = Logic;
args = ["a", Tyasset asset; "b", Tyasset asset];
returns = Tybool;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = mk_cmp_function_body m asset fields
} in
decl
let mk_sort_clone_id asset fields =
(String.capitalize_ascii asset) ^ "Sort" ^
(String.concat "" (List.map (fun (f,k) ->
(String.capitalize_ascii f) ^ (String.capitalize_ascii (sort_kind_to_string k))) fields))
let mk_sort_clone _m asset fields =
let cap_asset = String.capitalize_ascii asset in
Dclone (
[gArchetypeDir;gArchetypeSort],
mk_sort_clone_id asset fields, [
Ctype ("t", Tyasset asset);
Ctype ("view", Tyasset ((mk_view_id asset)^".view"));
Ctype ("collection", Tyasset (cap_asset ^ ".collection"));
Cval ("cmp", mk_cmp_function_id asset fields);
Cval ("view_to_list", cap_asset ^ ".view_to_list");
Cval ("list_to_view", cap_asset ^ ".list_to_view")
])
type filter = Select | Removeif
let rec mk_afun_test = function
| Tdot (Tvar v,f) when compare v "the" = 0 -> Tdot (Tvar "a",f)
| Tnow _ -> Tvar (mk_id "now")
| Tcaller _ -> Tvar (mk_id "caller")
| Tsender _ -> Tvar (mk_id "source")
| _ as t -> map_abstract_term mk_afun_test id id t
let rec acc_has_id id = function
| [] -> false
| (_,i,_)::_ when compare id i = 0 -> true
| (_,_,_)::tl -> acc_has_id id tl
let test =
let rec acc (term : M.mterm) =
match term.M.node with
| M.Mnow -> if acc_has_id "now" acc then acc else acc @ [term,mk_id "now", Tydate]
| M.Mcaller -> if acc_has_id "caller" acc then acc else acc @ [term,mk_id "caller", Tyaddr]
| M.Msource -> if acc_has_id "source" acc then acc else acc @ [term,mk_id "source", Tyaddr]
| _ -> M.fold_term internal_extract_args acc term in
internal_extract_args [] test
let mk_filter_name m asset test = function
| Select -> "select_" ^ asset ^ "_" ^ (string_of_int (M.Utils.get_select_idx m asset test))
| Removeif -> "removeif_" ^ asset ^ "_" ^ (string_of_int (M.Utils.get_removeif_idx m asset test))
let mk_select_name m asset test = mk_filter_name m asset test Select
let mk_removeif_name m asset test = mk_filter_name m asset test Removeif
let mk_filter_predicate ftyp m asset test filter args =
let args : (string * typ) list = List.map (fun (i,t) -> (i, (map_mtype m t |> unloc_type))) args in
let name = mk_filter_name m asset test ftyp in
let body = mk_afun_test filter in
Dfun {
name = name;
logic = Logic;
args = args @ (extract_args test |> List.map (fun (_,a,b) -> a,b)) @ ["a", Tyasset asset];
returns = Tybool;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [{
id = name ^ "_post";
form = (Teq(Tyint,Tresult,body));
}];
body = body;
}
let mk_select_predicate = mk_filter_predicate Select
let mk_removeif_predicate = mk_filter_predicate Removeif
let get_definition_body m id =
let rec get_body = function
| [] -> None
| (def : M.definition) :: _ when compare (unloc def.name) id = 0 -> Some def.body
| _ :: tl -> get_body tl in
let rec get_spec = function
| [] -> None
| spec :: tl ->
begin match get_body spec.M.definitions with
| Some b -> Some b
| None -> get_spec tl
end in
get_spec (M.Utils.get_specifications m)
let get_predicate_body m id =
let rec get_body = function
| [] -> None
| (def : M.predicate) :: _ when compare (unloc def.name) id = 0 -> Some def.body
| _ :: tl -> get_body tl in
let rec get_spec = function
| [] -> None
| spec :: tl ->
begin match get_body spec.M.predicates with
| Some b -> Some b
| None -> get_spec tl
end in
get_spec (M.Utils.get_specifications m)
let is_predicate m id =
let rec search_pred = function
| [] -> false
| (def : M.predicate) :: _ when compare (unloc def.name) id = 0 -> true
| _ :: tl -> search_pred tl in
let rec search_spec = function
| [] -> false
| spec :: tl ->
if search_pred spec.M.predicates then
true
else search_spec tl
in
search_spec (M.Utils.get_specifications m)
let m body =
let rec acc (term : M.mterm) =
match term.M.node with
| M.Mvar (id ,Vparam, _, _) ->
if acc_has_id (unloc id) acc
then acc
else acc @ [Tvar (unloc id), unloc id, map_mtype m term.type_]
| _ -> M.fold_term internal_extract_def_args acc term in
internal_extract_def_args [] body
let get_def_params m id =
match get_definition_body m id with
| Some b -> extract_def_args m b |> List.map (fun (p,_,_) -> p)
| None -> assert false
let get_pred_params m id =
match get_predicate_body m id with
| Some b -> extract_def_args m b |> List.map (fun (p,_,_) -> p)
| None -> assert false
let wdl (l : 'a list) = List.map dl 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 l5 l6 l7 l8 =
match l1,l2,l3,l4,l5,l6,l7,l8 with
| e1::tl1,e2::tl2,e3::tl3,e4::tl4,e5::tl5,e6::tl6,e7::tl7,e8::tl8 ->
e1::e2::e3::e4::e5::e6::e7::e8::(zip tl1 tl2 tl3 tl4 tl5 tl6 tl7 tl8)
| _ -> []
let cap s = mk_loc s.loc (String.capitalize_ascii s.obj)
let mk_eq_type_fun m id t = Dfun {
name = "eq_" ^ id |> dl;
logic = Logic;
args = [
dl "e1", t;
dl "e2", t
];
returns = Tybool |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = loc_term (mk_eq_type m "e1" "e2" (unloc_type t));
}
let mk_le_type_fun m id t = Dfun {
name = "le_" ^ id |> dl;
logic = Logic;
args = [
dl "e1", t;
dl "e2", t
];
returns = Tybool |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = loc_term (mk_le_type m "e1" "e2" (unloc_type t));
}
let mk_map_clone id k t =
Dclone ([gArchetypeDir;gArchetypeColl] |> wdl,
String.capitalize_ascii id |> dl, [
Ctype (dl "t", t);
Ctype (dl "tk", k);
Cval ("keyt" |> dl, "fst" |> dl);
Cval ("eqt" |> dl, "eq_" ^ id |> dl);
Cval ("lek" |> dl, "le_" ^ id |> dl);
]
)
let mk_map_type m (t : M.type_) =
match M.get_ntype t with
| Tmap (_, k, v) ->
let map_name = mk_map_name m t in
let t = M.ttuple [k; v] in
let typ = map_mtype m t in
let key = map_mtype m k in [
mk_eq_type_fun m map_name typ;
mk_le_type_fun m map_name key;
mk_map_clone map_name key typ
]
| _ -> assert false
let mk_set_clone id t =
Dclone ([gArchetypeDir;gArchetypeSet] |> wdl,
String.capitalize_ascii id |> dl, [
Ctype (dl "t", t);
Cval ("eqt" |> dl, "eq_" ^ id |> dl);
Cval ("le_t" |> dl, "le_" ^ id |> dl);
]
)
let mk_set_type m (t : M.type_) =
match M.get_ntype t with
| Tset et ->
let set_name = mk_set_name m t in
let et = map_mtype m et in [
mk_eq_type_fun m set_name et;
mk_le_type_fun m set_name et;
mk_set_clone set_name et;
]
| _ -> assert false
let mk_list_clone id t =
Dclone ([gArchetypeDir;gArchetypeList] |> wdl,
String.capitalize_ascii id |> dl, [
Ctype (dl "t", t);
Cval ("eqt" |> dl, "eq_" ^ id |> dl)
]
)
let mk_list_type m (t : M.type_) =
match M.get_ntype t with
| Tlist et ->
let list_name = mk_list_name m t in
let et = map_mtype m et in [
mk_eq_type_fun m list_name et;
mk_list_clone list_name et;
]
| _ -> assert false
let map_record_fields m =
List.map (fun (f : M.record_field) -> {
name = map_lident f.name;
typ = map_mtype m f.type_;
init = loc_term Tnone;
mutable_ = false;
})
let mk_record m (r : M.record) : (loc_term, loc_typ, loc_ident) abstract_decl =
Drecord (map_lident r.name, map_record_fields m r.fields)
let map_lidents = List.map map_lident
let rec type_to_init m (typ : loc_typ) : loc_term =
mk_loc typ.loc (match typ.obj with
| Tyasset i -> Tapp (loc_term (Tvar ("mk_default_"^i.obj)),[])
| Typartition i -> Temptyfield i
| Tyaggregate i -> Temptyfield i
| Tycoll i -> Temptycoll i
| Tylist _ -> Tnil (dl gListAs)
| Tyview i -> Temptyview i
| Tymap i -> Tvar (mk_loc typ.loc ("const (mk_default_" ^ i.obj ^ " ())"))
| Tyenum i -> Tvar (mk_loc typ.loc (unloc (M.Utils.get_enum m i.obj).initial))
| Tytuple l -> Ttuple (List.map (type_to_init m) l)
| Tybool -> Ttrue
| Tystring -> Temptystr
| Tyaddr -> Tdefaultaddr
| Tyoption _ -> Tnone
| Tyunit -> Tunit
| Tyor (l, r) -> Tleft (r, type_to_init m l)
| Tyset i -> Temptycoll i
| Tyrecord _
| Tylambda (_, _)
| Tyint
| Tyuint
| Tyrational
| Tykey
| Tykeyhash
| Tydate
| Tyduration
| Tytez
| Tysignature
| Tybytes
| Tychainid
| Tystorage
| Tyoperation
| Tycontract
| Tystate
| Tybls12_381_fr
| Tybls12_381_g1
| Tybls12_381_g2
| Tynever
-> Tint Big_int.zero_big_int)
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.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_container_asset_key m a 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.SonlyInEntry _ -> true
| M.SonlyByRoleInEntry _ -> 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 (gListAs, map_action_to_change a |> mk_change_term,Tnil gListAs);
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 l2) true) (List.tl l2))])
in
let mk_changes_performed_by_2 t1 t2 a l1 l2 =
Tapp (Tvar "Tr.performed_by",
[tr;
Tcons (gListAs, map_action_to_change a |> mk_change_term,Tnil gListAs);
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.SonlyInEntry (ADany,Sentry entries) ->
mk_performed_by entry (entries |> List.map unloc |> List.map (mk_trace_id Entry)) true
| M.SonlyByRole (a,roles) ->
mk_changes_performed_by caller a (roles |> List.map unloc) false
| M.SonlyInEntry (a,Sentry entries) ->
mk_changes_performed_by entry
a
(entries |> List.map unloc |> List.map (mk_trace_id Entry))
true
| M.SonlyByRoleInEntry (ADany,roles,Sentry entries) ->
mk_performed_by_2 caller entry
(roles |> List.map unloc)
(entries |> List.map unloc |> List.map (mk_trace_id Entry))
| M.SonlyByRoleInEntry (a,roles,Sentry entries) ->
mk_changes_performed_by_2 caller entry a
(roles |> List.map unloc)
(entries |> List.map unloc |> List.map (mk_trace_id Entry))
| _ -> 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,dl (Tapp (loc_f,[loc_term arg])))
let mk_invariant m n src inv : loc_term =
let r = M.Utils.get_asset m (unloc n) in
let fields = r.values |> List.map (fun (x : M.asset_item) -> (unloc x.name)) |> 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 (dl Ttobereplaced) replacing (loc_term prefix)
let mk_storage_invariant m n (lbl : M.lident) (t : loc_term) = {
id = map_lident lbl;
form = mk_invariant m n `Storage t;
}
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_state_invariant _m _v (lbl : M.lident) (t : loc_term) = {
id = map_lident lbl;
form = Timpl (
loc_term (Teq(Tyint,Tvar "state", Tvar (unloc _v))),
t) |> dl
}
let mk_eq_enums m (r : M.asset) =
List.fold_left (fun acc (item : M.asset_item) ->
match M.get_ntype item.type_ with
| Tenum lid ->
let id = unloc lid in
if List.mem id acc then acc else acc @ [id]
| _ -> acc
) [] r.values |>
List.map (fun id ->
Dfun {
name = "eq_" ^ id |> dl;
logic = Logic;
args = ["e1" |> dl, loc_type (Tyenum id);
"e2" |> dl, loc_type (Tyenum id)];
returns = Tybool |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = loc_term (Tmatch (
Ttuple [Tvar "e1"; Tvar "e2"],
List.fold_left (fun acc eval ->
[ Tpatt_tuple [Tconst eval; Tconst eval], Ttrue ] @ acc
) [ Tpatt_tuple [Twild;Twild], Tfalse ] (M.Utils.get_enum_values m id)
));
})
let mk_eq_key m (r : M.asset) =
let asset = unloc r.name in
let (_key, tkey) = M.Utils.get_asset_key m asset in
let tkey = map_mtype m tkey in
Dfun {
name = "eq_"^asset^"_key" |> dl;
logic = Logic;
args = [
"k1" |> dl, tkey;
"k2" |> dl, tkey;
];
returns = Tybool |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = loc_term (mk_eq_type m "k1" "k2" (unloc_type tkey));
}
let mk_le_key m (r : M.asset) =
let asset = unloc r.name in
let (_key, tkey) = M.Utils.get_asset_key m asset in
let tkey = map_mtype m tkey in
Dfun {
name = "le_"^asset^"_key" |> dl;
logic = Logic;
args = [
"k1" |> dl, tkey;
"k2" |> dl, tkey;
];
returns = Tybool |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = loc_term (mk_le_type m "k1" "k2" (unloc_type tkey));
}
let mk_eq_asset m (r : M.asset) =
let cmps = List.map (fun (item : M.asset_item) ->
let id1 = "a1_"^(unloc item.name) in
let id2 = "a2_"^(unloc item.name) in
Tletin (false, id1, None, Tdoti("a1",unloc item.name),
Tletin (false, id2, None, Tdoti ("a2",unloc item.name),
mk_eq_type m id1 id2 (unloc_type (map_mtype m item.type_))
)
)
) r.values in
Dfun {
name = "eq_" ^ (unloc r.name) |> dl;
logic = Logic;
args = ["a1" |> dl, Tyasset (map_lident r.name) |> dl;
"a2" |> dl, Tyasset (map_lident r.name) |> dl];
returns = Tybool |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = List.fold_left (fun acc cmp ->
Tpand (acc,cmp)
) (List.hd cmps) (List.tl cmps) |> loc_term;
}
let mk_enum _m (e : M.enum) : (loc_term,loc_typ,loc_ident) abstract_decl =
Denum (map_lident e.name, List.map (fun (item : M.enum_item) -> map_lident item.name) e.values)
let get_fail_idx m t = succ (List.index_of (M.cmp_type t) (M.Utils.get_all_fail_types m))
let mk_exn m i t : (loc_term, loc_typ, ident with_loc) abstract_decl =
let id = string_of_int (succ i) in
Dexn (dl id,map_mtype m t)
let mk_field m (r : M.asset) =
let asset = unloc r.name in
let (_key, tkey) = M.Utils.get_asset_key m asset in
let tkey = map_mtype m tkey in
Dclone ([gArchetypeDir; gArchetypeField] |> wdl,
String.capitalize_ascii (mk_field_id asset) |> dl,
[Ctype ("tk" |> dl, tkey);
Cval ("eqk" |> dl, "eq_" ^ asset ^ "_key" |> dl);
Cval ("lek" |> dl, "le_" ^ asset ^ "_key" |> dl);
Ctype ("view" |> dl, loc_type (Tyview (mk_view_id asset)));
Cval ("vmk" |> dl, (String.capitalize_ascii (mk_view_id asset))^".mk" |> dl);
Cval ("velts" |> dl, (String.capitalize_ascii (mk_view_id asset))^".elts" |> dl);
Cval ("vcontains" |> dl, (String.capitalize_ascii (mk_view_id asset))^".contains" |> dl)])
let mk_view m (r : M.asset) =
let asset = unloc r.name in
let (_key, tkey) = M.Utils.get_asset_key m asset in
let tkey = map_mtype m tkey in
Dclone ([gArchetypeDir; gArchetypeView] |> wdl,
String.capitalize_ascii (mk_view_id asset) |> dl,
[Ctype ("tk" |> dl, tkey);
Cval ("eqk" |> dl, "eq_" ^ asset ^ "_key" |> dl)])
let mk_coll m (r : M.asset) =
let asset = unloc r.name in
let (key, tkey) = M.Utils.get_asset_key m asset in
let tkey = map_mtype m tkey in
Dclone ([gArchetypeDir;gArchetypeColl] |> wdl,
String.capitalize_ascii asset |> dl,
[Ctype ("tk" |> dl, tkey);
Cval ("eqk" |> dl, "eq_" ^ asset ^ "_key" |> dl);
Cval ("lek" |> dl, "le_" ^ asset ^ "_key" |> dl);
Ctype ("t" |> dl, Tyasset (dl asset) |> dl);
Cval ("keyt" |> dl, key |> dl);
Cval ("eqt" |> dl, "eq_" ^ asset |> dl);
Ctype ("view" |> dl, loc_type (Tyview (mk_view_id asset)));
Cval ("vmk" |> dl, (String.capitalize_ascii (mk_view_id asset))^".mk" |> dl);
Cval ("velts" |> dl, (String.capitalize_ascii (mk_view_id asset))^".elts" |> dl);
Cval ("vcontains" |> dl, (String.capitalize_ascii (mk_view_id asset))^".contains" |> dl);
Cval ("vcard" |> dl, (String.capitalize_ascii (mk_view_id asset))^".card" |> dl);
Ctype ("field" |> dl, loc_type (Tyasset ((mk_field_id asset)^".field")));
Cval ("felts" |> dl, (String.capitalize_ascii (mk_field_id asset))^".elts" |> dl);
Cval ("fcontains" |> dl, (String.capitalize_ascii (mk_field_id asset))^".contains" |> dl)
])
let mk_set_field_id fieldid = "set_" ^ fieldid
let mk_set_field _m asset fieldid oasset =
let name = mk_set_field_id fieldid in
Dfun {
name = name |> dl;
logic = Logic;
args = [
dl "f", loc_type (Tyaggregate (mk_field_id oasset));
dl "a", loc_type (Tyasset asset)
];
returns = loc_type (Tyasset asset);
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body = dl (Trecord(Some (loc_term (Tvar "a")), [
dl fieldid, loc_term (Tvar "f")
]))
}
let mk_aggregates m (r : M.asset) =
let asset = unloc r.name in
let capasset = String.capitalize_ascii asset in
let (_, tkey) = M.Utils.get_asset_key m asset in
let tkey = map_mtype m tkey in
let aggregates = M.Utils.get_asset_containers m asset in
List.fold_left (fun acc (agg_id, field_type, _) ->
let oasset = M.Utils.type_to_asset field_type in
let (_,oasset_key_type) = M.Utils.get_asset_key m oasset in
let agg_key_type = map_mtype m oasset_key_type in
let clone = Dclone (
[gArchetypeDir; gArchetypeAgg] |> wdl,
String.capitalize_ascii (mk_aggregate_id agg_id) |> dl, [
Ctype (dl "t", loc_type (Tyasset asset));
Ctype (dl "tk", tkey);
Ctype (dl "collection", loc_type (Tycoll capasset));
Cval (dl "elts", dl (capasset ^ "." ^ "elts"));
Cval (dl "get", dl (capasset ^ "." ^ "get"));
Cval (dl "set", dl (capasset ^ "." ^ "set"));
Ctype (dl "field", loc_type (Tyaggregate (mk_field_id oasset)));
Cval (dl "setF", dl (mk_set_field_id agg_id));
Cval (dl "aggregate", dl agg_id);
Ctype (dl "tkF", agg_key_type);
Cval (dl "containsF", dl ((mk_field_id oasset) ^ "." ^ "contains"));
Cval (dl "mkF", dl ((mk_field_id oasset) ^ "." ^ "mk"));
Cval (dl "eltsF", dl ((mk_field_id oasset) ^ "." ^ "elts"));
Cval (dl "addF", dl ((mk_field_id oasset) ^ "." ^ "add"));
Cval (dl "removeF", dl ((mk_field_id oasset) ^ "." ^ "remove"));
Cval (dl "emptyF", dl ((mk_field_id oasset) ^ "." ^ "empty"));
Ctype (dl "tO", loc_type (Tyasset oasset));
Ctype (dl "collectionO", loc_type (Tycoll oasset));
Cval (dl "getO", dl ((String.capitalize_ascii oasset) ^ "." ^ "get"));
]) in
acc @ [mk_set_field m asset agg_id oasset; clone]
) [] aggregates
let mk_partition_axioms (m : M.model) =
M.Utils.get_containers m |> List.map (fun (n,i,_) ->
let kt = M.Utils.get_asset_key m n |> snd in
let pa,_,pkt = M.Utils.get_container_asset_key m n i in
mk_partition_axiom n i kt pa (pkt |> map_mtype m |> unloc_type)
) |> 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_lbl_before lbl =
match lbl with
| Some a -> "Before_" ^ a
| None -> "Before_loop"
let mk_inv_lbl lbl id =
match lbl with
| Some a-> id ^ "_invariant_" ^ a
| None -> id ^ "_invariant"
let mk_storage_loop_inv lbl lblbef id =
let iid = mk_inv_lbl lbl id in {
id = dl iid;
form = loc_term (Teq (Tyint, Tapp (Tvar id,[Tvar gs]), Tapp (Tvar id, [Tat (lblbef,Tvar gs)])))
}
let rec is_identical id = function
| (M.Eadded i)::_ when String.compare i id = 0 -> false
| (M.Eremoved i)::_ when String.compare i id = 0 -> false
| (M.Eupdated i)::_ when String.compare i id = 0 -> false
| _::tl -> is_identical id tl
| [] -> true
let mk_vars_loop_invariants m entry lbl lblbef body =
let assigned_vars = M.Utils.extract_assign_kind body |>
List.fold_left (fun acc ak ->
match ak with
| M.Avar id -> acc @ [unloc id]
| M.Avarstore id -> acc @ [unloc id]
| _ -> acc
) []
in
let assigned_assets = M.Utils.extract_asset_effect m body in
let get_specifications acc name = begin
match M.Utils.get_specification m name with
| Some s -> acc @ List.map (fun (p : M.postcondition) -> p.formula) s.postconditions
| None -> acc
end in
let invariant_vars =
Option.fold get_specifications [] entry |>
List.fold_left (fun acc t ->
let l = M.Utils.extract_var_idents m t in
acc @ l) [] |> Tools.List.dedup in
let storage_invs = List.fold_left (fun acc (item : M.storage_item) ->
match item.model_type with
| M.MTasset id when (List.mem id invariant_vars) ->
let acc = if is_identical id assigned_assets then
acc @ [mk_storage_loop_inv lbl lblbef (mk_ac_id id)]
else acc in
let acc = if not (List.mem (M.Eadded id) assigned_assets) then
acc @ [mk_storage_loop_inv lbl lblbef (mk_ac_added_id id)]
else acc in
let acc = if not (List.mem (M.Eremoved id) assigned_assets) then
acc @ [mk_storage_loop_inv lbl lblbef (mk_ac_rmed_id id)]
else acc in
acc
| _ when (List.mem (unloc item.id) invariant_vars) && not (List.mem (unloc item.id) assigned_vars) ->
acc @ [mk_storage_loop_inv lbl lblbef (unloc (item.id))]
| _ -> acc
) [] (M.Utils.get_storage m) in
let const_storage_invs = List.fold_left (fun acc id ->
if List.mem id invariant_vars && not (List.mem id assigned_vars) then
acc @ [mk_storage_loop_inv lbl lblbef ("_"^id)]
else acc
) [] ["now"; "caller"; "balance"; "source"; "selfaddress"] in
storage_invs @ const_storage_invs
type mode = Inv | Logic | Exec | Def
type logical_context = {
lctx : mode;
entry_id : ident option;
locals : ident list;
loop_id : ident option;
fun_ : bool;
fails : bool;
}
let init_ctx = {
lctx = Exec;
entry_id = None;
locals = [];
loop_id = None;
fun_ = false;
fails = false;
}
let mk_sid ctx =
match ctx.fun_ with
| true -> gsarg
| false -> gsinit
let add_local id ctx = { ctx with locals = id::ctx.locals }
let mk_trace_seq m t chs =
if M.Utils.with_trace m then
Tseq ([dl t] @ (List.map mk_trace chs))
else t
let map_mpattern (p : M.lident M.pattern_node) =
match p with
| M.Pwild -> Twild
| M.Pconst (i, _) -> Tconst (map_lident i)
let is_coll_field m f : bool =
M.Utils.get_containers m |> List.map (fun (_,v,_) -> v) |> List.mem f
let is_exec_divergent = function
| M.Mget _
| M.Mnth _
-> true
| _ -> false
let get_tuple_size = function
| M.Ttuple l -> List.length l
| _ -> assert false
let cp_storage id = Tapp (Tvar "_cp_storage",[Tvar id])
let fail_if_neg_nat_value (t : M.type_) left right op =
match M.get_ntype t with
| M.Tbuiltin Bnat -> dl (
Tif (dl (Tge(dl Tyint, left, right)), op,
Some (loc_term (Tseq [Tassign (Tvar gs, cp_storage gsinit); Traise ENatAssign])))
)
| _ -> op
let get_assign_value (t : M.type_) left right = function
| M.ValueAssign -> right
| M.MinusAssign ->
let op = dl (Tminus (dl Tyint, left, right)) in
fail_if_neg_nat_value t left right op
| M.PlusAssign -> dl (Tplus (dl Tyint, left, right))
| M.MultAssign -> dl (Tmult (dl Tyint, left, right))
| M.DivAssign -> dl (Tdiv (dl Tyint, left, right))
| M.AndAssign -> dl (Tand (left, right))
| M.OrAssign -> dl (Tor (left, right))
let is_partition m n f =
match M.Utils.get_field_container m n f with
| _,Partition -> true
| _ -> false
let mk_get_force ctx n k c =
let sid = mk_sid ctx in
Tmatch (dl (Tget(n,k,c)),[
Tpsome (dl "v"), loc_term (Tvar "v");
Twild, loc_term (Tseq [Tassign(Tvar gs, cp_storage sid); Traise ENotFound])
])
let mk_match_get_some ctx a k instr excn =
let sid = mk_sid ctx in
Tmatch (dl (Tget (dl a, k, loc_term (mk_ac a))), [
Tpignore, instr;
Twild, loc_term (Tseq [Tassign (Tvar gs, cp_storage sid); Traise excn])
])
let mk_match_get_some_id ctx id a k instr excn =
let sid = mk_sid ctx in
Tmatch (dl (Tget (dl a, k, loc_term (mk_ac a))), [
Tpsome id, instr;
Twild, loc_term (Tseq [Tassign (Tvar gs, cp_storage sid); Traise excn])
])
let mk_match_get_some_id_nil id a k instr =
Tmatch (dl (Tget (dl a, k, loc_term (mk_ac a))), [
Tpsome id, instr;
Twild, dl Tunit
])
let mk_match_get_none ctx a k instr excn =
let sid = mk_sid ctx in
Tmatch (dl (Tget (dl a, k, loc_term (mk_ac a))), [
Tpignore, loc_term (Tseq [Tassign (Tvar gs, cp_storage sid); Traise excn]);
Twild, instr
])
let mk_match ctx matched id instr excn =
let sid = mk_sid ctx in
Tmatch (matched, [
Tpsome (dl id), instr;
Twild, loc_term (Tseq [Tassign (Tvar gs, cp_storage sid); Traise excn])
])
let mk_storage_id ctx =
match ctx.lctx with
| Def -> gsarg
| _ -> gs
let mk_coll_term n ctx (t,d) =
let s = mk_storage_id ctx in
match ctx.lctx, t, d with
| Inv, _, M.Dnone -> Tvar (mk_ac_id n)
| Inv, _, M.Dadded -> Tvar (mk_ac_added_id n)
| Inv, _, M.Dremoved -> Tvar (mk_ac_rmed_id n)
| Inv, _, M.Dunmoved -> Tvar (mk_ac_unmvd_id n)
| _, M.Tnone, M.Dnone -> mk_ac_st s n
| _, M.Tnone, M.Dadded -> mk_ac_added_st s n
| _, M.Tnone, M.Dremoved -> mk_ac_rmed_st s n
| _, M.Tnone, M.Dunmoved -> mk_ac_st s n
| _, M.Tbefore, M.Dnone -> mk_ac_old_st s n
| _, M.Tbefore, M.Dadded -> mk_ac_old_added_st s n
| _, M.Tbefore, M.Dremoved -> mk_ac_old_rmed_st s n
| _, M.Tbefore, M.Dunmoved -> mk_ac_st s n
| _, M.Tat lbl, M.Dnone -> Tat (lbl, mk_ac_st s n)
| _, M.Tat lbl, M.Dadded -> Tat (lbl, mk_ac_added_st s n)
| _, M.Tat lbl, M.Dremoved -> Tat (lbl, mk_ac_rmed_st s n)
| _, M.Tat _lbl, M.Dunmoved -> mk_ac_st s n
let mk_loc_coll_term n ctx (t,d) = loc_term (mk_coll_term n ctx (t,d))
let mk_lc_term n ctx = mk_loc_coll_term n ctx (M.Tnone, M.Dnone)
let mk_temp_delta = function
| M.CKcoll (t,d) -> (t,d)
| _ -> M.Tnone,M.Dnone
let assign_operation a e l =
Tassign (
loc_term (Tdoti(gs,"_ops")),
dl (Tcons (dl gListAs,
dl (Tapp( loc_term (Tvar "_mk_operation"),[a; e; l])),
loc_term (Tdoti(gs,"_ops"))
)))
let rec map_mterm m ctx (mt : M.mterm) : loc_term =
let error_internal desc = emit_error (mt.loc, desc); Tnottranslated in
let error_not_translated (msg : string) = error_internal (TODONotTranslated msg) in
let error_not_supported (msg : string) = error_internal (NotSupported msg) in
let to_collection (an : ident) (mt : M.mterm) : loc_term =
let a = map_mterm m ctx mt in
match M.get_ntype mt.type_ with
| Tcontainer (_, View) -> begin
let b : loc_term = loc_term (mk_ac (an)) in
mk_loc mt.loc (Tfromview (with_dummy_loc an, a, b))
end
| _ -> a
in
let t =
match mt.node with
| Mletin ([id], v, _, b, None) ->
let ctx = add_local (unloc id) ctx in
Tletin (M.Utils.is_local_assigned (unloc id) b, map_lident id, None, map_mterm m ctx v, map_mterm m ctx b)
| Mletin ([id], { node = M.Mget (a, CKcoll (t,d), k); type_ = _ }, _, b, Some e) ->
let ctx = ctx in
Tmatch (Tget (loc_ident a,
map_mterm m ctx k,
mk_loc_coll_term a ctx (t,d)) |> dl,[
Tpsome (map_lident id), map_mterm m ctx b;
Twild, map_mterm m ctx e
])
| Mletin ([id], { node = M.Mmapget (_kty, _vty, container, k); type_ = _ }, _, b, Some e) ->
let ctx = ctx in
let map_id = mk_map_name m container.type_ in
Tmatch (Tsndopt (Tget (loc_ident map_id,
map_mterm m ctx k,
map_mterm m ctx container) |> dl) |> dl,[
Tpsome (map_lident id), map_mterm m ctx b;
Twild, map_mterm m ctx e
])
| Mletin ([id], { node = M.Mnth (n, CKview c,k); type_ = _ }, _, b, Some e) ->
Tmatch (Tnth (dl (mk_view_id n),
map_mterm m ctx k,
map_mterm m ctx c) |> dl,[
Tpsome (map_lident id), map_mterm m ctx b;
Twild, map_mterm m ctx e
])
| Mletin ([id], { node = M.Mnth (n, CKcoll (t,d),k); type_ = _ }, _, b, Some e) ->
Tmatch (
Tnth (
dl (mk_view_id n),
map_mterm m ctx k,
dl(Ttoview (dl n,mk_loc_coll_term n ctx (t,d))) ) |> dl,[
Tpsome (map_lident id), map_mterm m ctx b;
Twild, map_mterm m ctx e
])
| Mletin ([id], { node = M.Moptget v; type_ = _ }, _, b, Some e) ->
Tmatch (map_mterm m ctx v,[
Tpsome (map_lident id), map_mterm m ctx b;
Twild, map_mterm m ctx e
])
| Mletin ([id], v, _, b, Some o) ->
let ctx = ctx in
Tmatch (map_mterm m ctx v,[
Tpsome (map_lident id), map_mterm m ctx b;
Twild, map_mterm m ctx o
])
| Mletin (l, v, _, b, None) ->
let ctx = List.fold_left (fun acc id -> add_local (unloc id) acc) ctx l in
let id = "("^(l |> List.map unloc |> String.concat ",")^")" in
Tletin (false, dl id , None, map_mterm m ctx v, map_mterm m ctx b)
| Mletin _ -> Tvar (dl "TODO letin")
| Mdeclvar _ -> error_not_supported "Mdeclvar"
| Mapp (f, args) ->
let args = args |> List.map (map_mterm m ctx) in
if is_predicate m (unloc f) then
let storage = loc_term (Tvar (mk_storage_id ctx)) in
let params = get_pred_params m (unloc f) |> List.map loc_term in
Tapp (mk_loc (map_lident f).loc (Tvar (map_lident f)), [storage] @ params @ args)
else
let sid =
match ctx.fails, ctx.fun_ with
| true, _ -> gs
| _, true -> gsarg
| _ -> gsinit
in
Tapp (mk_loc (map_lident f).loc (Tvar (map_lident f)), [loc_term (Tvar sid)] @ args)
| Massign (ValueAssign, _, Avar id, v) ->
Tassign (dl (Tvar (map_lident id)),map_mterm m ctx v)
| Massign (MinusAssign, _, Avar id, v) ->
Tassign (dl (Tvar (map_lident id)),
dl (
Tminus (dl Tyint,
dl (Tvar (map_lident id)),
map_mterm m ctx v)))
| Massign (ValueAssign, _, Aoperations, v) -> Tassign (loc_term (Tdoti(gs,mk_id gOperations)), map_mterm m ctx v)
| Massign (_, _, Avar _, _) -> error_not_translated "Massign (_, _, Avar _, _)"
| Massign (assignop, t, Avarstore id, v) ->
let left = dl (Tdoti (dl gs,map_lident id)) in
let right = map_mterm m ctx v in
Tassign (left,get_assign_value t left right assignop)
| Massign (assignop, t, Aasset (_id1, id2, k), v) ->
let left = dl (Tdot (map_mterm m ctx k,
dl (Tvar (map_lident id2)))) in
let right = map_mterm m ctx v in
Tassign (left,get_assign_value t left right assignop)
| Massign (assignop, t, Arecord (_id1, id2, k), v) ->
let left = map_mterm m ctx k in
let right : loc_term = with_dummy_loc (Trecord (Some left, [map_lident id2, map_mterm m ctx v])) in
Tassign (left, get_assign_value t left right assignop)
| Massign (_, _, Astate, v) -> Tassign (loc_term (Tdoti (gs, "state")), map_mterm m ctx v)
| Massign (_, _, Aassetstate _, _) -> error_not_translated "Massign (_, _, Aassetstate _, _)"
| Massign (_, _, Aoperations, _) -> error_not_translated "Massign (_, _, Aoperations, _)"
| Mif (c, t, Some { node=M.Mseq []; type_=_}) ->
Tif (map_mterm m ctx c, map_mterm m ctx t, None)
| Mif (c, t, e) ->
Tif (map_mterm m ctx c, map_mterm m ctx t, Option.map (map_mterm m ctx) e)
| Mmatchwith (t, l) ->
Tmatch (map_mterm m ctx t, List.map (fun ((p : M.lident M.pattern_gen), e) ->
(map_mpattern p.node, map_mterm m ctx e)
) l)
| Minstrmatchoption (x, i, ve, ne) -> Tmatchoption (map_mterm m ctx x, map_lident i, map_mterm m ctx ve, map_mterm m ctx ne)
| Minstrmatchor (x, lid, le, rid, re) -> Tmatchor (map_mterm m ctx x, map_lident lid, map_mterm m ctx le, map_lident rid, map_mterm m ctx re)
| Minstrmatchlist (x, hd, tl, a, b) -> Tmatchlist (map_mterm m ctx x, map_lident hd, map_lident tl, map_mterm m ctx a, map_mterm m ctx b)
| Mfor (_id, _c, _b, _lbl) -> error_not_supported "Mfor"
| Miter (id, from, to_, body, lbl) ->
let inv_ctx = { ctx with lctx = Logic } in
Tmark (dl (mk_lbl_before lbl),
dl (Tfor (map_lident id,
map_mterm m ctx from,
map_mterm m ctx to_,
mk_invariants m inv_ctx (Some id) lbl body,
map_mterm m ctx body
)))
| Mwhile (test, body, lbl) ->
let inv_ctx = { ctx with lctx = Logic } in
Tmark (dl (mk_lbl_before lbl),
dl (Twhile (map_mterm m ctx test,
mk_invariants m inv_ctx None lbl body,
map_mterm m ctx body
)))
| Mseq [] -> Tunit
| Mseq l -> Tseq (List.map (map_mterm m ctx) l)
| Mreturn v -> map_mterm m ctx v |> Mlwtree.deloc
| 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
| Mmark (lbl, x) -> Tmark (map_lident lbl, map_mterm m ctx x)
| Mfail x -> begin
let sid = mk_sid ctx in
Tseq
[
loc_term (Tassign (Tvar gs, cp_storage sid));
dl (Traise
(match x with
| Invalid v ->
let idx = get_fail_idx m v.type_ in
Efail (idx, Some (map_mterm m ctx v))
| InvalidCaller -> EInvalidCaller
| InvalidCondition lbl -> (EInvalidCondition lbl)
| NotFound -> ENotFound
| OutOfBound -> EOutOfBound
| KeyExists -> EKeyExists
| KeyExistsOrNotFound -> EKeyExistsOrNotFound
| DivByZero -> EDivByZero
| NatAssign -> ENatAssign
| NoTransfer -> ENoTransfer
| InvalidState -> EInvalidState))
]
end
| Mtransfer tr ->
begin
match tr with
| TKsimple (v, d) ->
let a = map_mterm m ctx v in
let t = map_mterm m ctx d in
Tseq[
dl (Tassign (
loc_term (Tdoti(gs,"_ops")),
dl (Tcons (dl gListAs,
dl (Tapp(loc_term (Tvar "_mk_transfer"),[t;a])),
loc_term (Tdoti(gs,"_ops"))
))));
dl (Tassign (
loc_term (Tdoti (gs,"_balance")),
dl (Tminus (dl Tyint,
loc_term (Tdoti (gs,"_balance")),
a
))
))
]
| TKcall (v, id, _, d, _a) ->
let t = map_mterm m ctx v in
let l = loc_term (Tnil gListAs) in
let a = map_mterm m ctx d in
let n = loc_term (Tstring id) in
Tassign (
loc_term (Tdoti(gs,"_ops")),
dl (Tcons (dl gListAs,
dl (Tapp(loc_term (Tvar "_mk_call"),[a; t; n; l])),
loc_term (Tdoti(gs,"_ops"))
)))
| TKentry (v, e, _a) ->
assign_operation (map_mterm m ctx v) (map_mterm m ctx e) (loc_term (Tnil gListAs))
| TKself (v, id, _a)->
assign_operation
(map_mterm m ctx v)
(dl (Tapp (loc_term (Tvar "getopt"), [loc_term (Tentrypoint (id, Tselfaddress gs))])))
(loc_term (Tnil gListAs))
| TKoperation op ->
Tassign (
loc_term (Tdoti(gs,"_ops")),
dl (Tcons (dl gListAs,
map_mterm m ctx op,
loc_term (Tdoti(gs,"_ops"))
)))
end
| Mentrypoint (_t, a, s) -> Tentrypoint (map_lident a, map_mterm m ctx s)
| Mself id -> Tapp (loc_term (Tvar "getopt"), [loc_term (Tentrypoint (unloc id, Tdefaultaddr))])
| Moperations ->
begin match ctx.lctx with
| Inv -> Tvar (dl (mk_id gOperations))
| _ -> Tdoti (dl gs, dl (mk_id gOperations))
end
| Mmkoperation (v, d, _a) ->
let a = map_mterm m ctx v in
let e = map_mterm m ctx d in
let l = loc_term (Tnil gListAs) in
Tapp( loc_term (Tvar "_mk_operation"),[a; e; l])
| Mint v -> Tint v
| Mnat v -> Tint v
| Mbool false -> Tfalse
| Mbool true -> Ttrue
| Mrational (l,r) -> Ttuple([ loc_term (Tint l); loc_term (Tint r)])
| Mcurrency (i, Utz) -> Tint i
| Mstring v -> Tstring v
| Maddress v -> Tstring v
| Mbytes v -> Tstring v
| Mdate s -> Tint (Core.date_to_timestamp s)
| Mduration v -> Tint (Core.duration_to_timestamp v)
| Mtimestamp v -> Tint v
| Munit -> Tunit
| Mexprif (c, t, e) ->
Tif (map_mterm m ctx c, map_mterm m ctx t, Some (map_mterm m ctx e))
| Mexprmatchwith (t, l) ->
Tmatch (map_mterm m ctx t, List.map (fun ((p : M.lident M.pattern_gen), e) ->
(map_mpattern p.node, map_mterm m ctx e)
) l)
| Mmatchoption (x, i, ve, ne) ->
Tmatchoption (map_mterm m ctx x, map_lident i, map_mterm m ctx ve, map_mterm m ctx ne)
| Mmatchor (x, lid, le, rid, re) ->
Tmatchor (map_mterm m ctx x, map_lident lid, map_mterm m ctx le, map_lident rid, map_mterm m ctx re)
| Mmatchlist (x, hid, tid, hte, ee) ->
Tmatchlist (map_mterm m ctx x, map_lident hid, map_lident tid, map_mterm m ctx hte, map_mterm m ctx ee)
| Mfold (x, i, e) -> Tfold (map_mterm m ctx x, map_lident i, map_mterm m ctx e)
| Mmap (x, i, e) ->
let st, _dt =
match M.get_ntype mt.type_, M.get_ntype x.type_ with
| Tlist st, Tlist dt -> st, dt
| _ -> assert false
in
Tlistmap (dl (mk_list_name m (M.tlist st)), map_mterm m ctx x, map_lident i, map_mterm m ctx e)
| Mexeclambda (a, b) -> Texeclambda (map_mterm m ctx a, map_mterm m ctx b)
| Mapplylambda (a, b) -> Tapplylambda (map_mterm m ctx a, map_mterm m ctx b)
| Mleft (t, x) -> Tleft (map_mtype m t, map_mterm m ctx x)
| Mright (t, x) -> Tright (map_mtype m t, map_mterm m ctx x)
| Mnone -> Tnone
| Msome v -> Tsome (map_mterm m ctx v)
| Mtuple l -> Ttuple (List.map (map_mterm m ctx) l)
| Mtupleaccess (x, k) ->
let card = begin match M.get_ntype x.type_ with
| Ttuple l -> List.length l
| _ -> assert false
end in Ttupleaccess (map_mterm m ctx x, (Big_int.int_of_big_int k)+1, card)
| Mrecupdate (id, l) ->
Trecord (Some (map_mterm m ctx id), List.map (fun (i,t) -> (dl i, map_mterm m ctx t)) l)
| Masset 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)))
| Massets l ->
begin
match M.get_ntype mt.type_ with
| Tcontainer ((Tasset a, _),Collection) ->
Tmkcoll (map_lident a, List.map (map_mterm m ctx) l)
| Tcontainer ((Tasset a, _),_) -> Temptyfield (dl (mk_field_id (unloc a)))
| _ -> assert false
end
| Mlitset l ->
let set = mk_set_name m mt.type_ in
if List.length l > 0 then
Tmkcoll (dl set,
List.fold_left (fun acc v ->
acc @ [ map_mterm m ctx v]
) ([] : loc_term list) l)
else Temptycoll (dl set)
| Mlitlist l ->
List.fold_left(fun acc e ->
dl (Tcons(dl gListAs, map_mterm m ctx e, acc))
) (loc_term (Tnil gListAs)) l |> Mlwtree.deloc
| Mlitmap (_, l) ->
let map = mk_map_name m mt.type_ in
if List.length l > 0 then
Tmkcoll (dl map,
List.fold_left (fun acc (k,v) ->
acc @ [dl (Ttuple [map_mterm m ctx k; map_mterm m ctx v])]
) ([] : loc_term list) l)
else Temptycoll (dl map)
| Mlitrecord l -> Trecord (None, List.map (fun (n,v) -> (dl n, map_mterm m ctx v)) l)
| Mlambda (rt, id, at, e) -> Tvlambda (map_mtype m rt, map_lident id, map_mtype m at, map_mterm m ctx e)
| Mdot (e, i) -> Tdot (map_mterm m ctx e, mk_loc (loc i) (Tvar (map_lident i)))
| Mdotassetfield (an, k, fn) ->
Tdot(
dl (Tapp (loc_term (Tvar ("get_"^(unloc an))),[map_mterm m ctx k])),
loc_term (Tvar (unloc fn)))
| Mequal (t, l, r) ->
begin match M.get_ntype t, ctx.lctx with
| M.Tcontainer ((Tasset id, _), Collection), (Logic | Inv) -> Teq (dl (Tycoll (map_lident id)), map_mterm m ctx l, map_mterm m ctx r)
| _, (Logic | Inv) -> Teq (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| _ -> Teq (map_mtype m t, map_mterm m ctx l, map_mterm m ctx r)
end
| Mnequal (t, l, r) ->
begin match M.get_ntype t, ctx.lctx with
| M.Tcontainer ((Tasset id, _), Collection), (Logic | Inv) -> Tneq (dl (Tycoll (map_lident id)), map_mterm m ctx l, map_mterm m ctx r)
| _, (Logic | Inv) -> Tneq (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| _ -> Tneq (map_mtype m t, map_mterm m ctx l, map_mterm m ctx r)
end
| Mgt (l, r) -> Tgt (map_mtype m l.type_, map_mterm m ctx l, map_mterm m ctx r)
| Mge (l, r) -> Tge (map_mtype m l.type_, map_mterm m ctx l, map_mterm m ctx r)
| Mlt (l, r) -> Tlt (map_mtype m l.type_, map_mterm m ctx l, map_mterm m ctx r)
| Mle (l, r) -> Tle (map_mtype m l.type_, map_mterm m ctx l, map_mterm m ctx r)
| Mmulticomp _ -> error_not_translated "Mmulticomp"
| Mand (l, r) -> Tpand (map_mterm m ctx l, map_mterm m ctx r)
| Mor (a, b) -> Tor (map_mterm m ctx a, map_mterm m ctx b)
| Mxor (a, b) ->
let t = map_mtype m (mt.type_) in
Txor (t, map_mterm m ctx a, map_mterm m ctx b)
| Mnot c -> Tnot (map_mterm m ctx c)
| Mplus (l, r) -> Tplus (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Mminus (l, r) -> Tminus (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Mmult (l, r) -> Tmult (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Mdivrat _ -> error_not_translated "Mdivrat"
| Mdiveuc (l, r) -> Tdiv (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Mmodulo (l, r) -> Tmod (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Mdivmod (l, r) -> Tdivmod (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Muminus v -> Tuminus (dl Tyint, map_mterm m ctx v)
| MthreeWayCmp (l, r) -> Tthreewaycmp (dl Tyint, map_mterm m ctx l, map_mterm m ctx r)
| Mshiftleft (l, r) -> Tshiftleft (map_mterm m ctx l, map_mterm m ctx r)
| Mshiftright (l, r) -> Tshiftright (map_mterm m ctx l, map_mterm m ctx r)
| Maddasset (n, i) ->
let key_value = mk_asset_key_value m ctx n i in
let mk_add_assign coll =
let add = dl (Tadd (dl n, map_mterm m ctx i, loc_term coll)) in
dl (Tassign (loc_term coll, add)) in
let assign = mk_add_assign (mk_ac n) in
let assign_added = mk_add_assign (mk_ac_added n) in
let assigns = dl (Tseq [assign; assign_added]) in
mk_trace_seq m
(mk_match_get_none ctx n key_value assigns EKeyExists)
[CAdd n]
| Maddfield (a, f, k, kb) ->
let oasset, _, _ = M.Utils.get_container_asset_key m a f in
let mk_add_id = loc_term (Tdoti (mk_aggregate_id f, "add")) in
let v =
if is_partition m a f then mk_asset_key_value m ctx a kb
else map_mterm m ctx kb in
let assign = dl (Tassign (loc_term (mk_ac a), dl (Tapp(mk_add_id,[
map_mterm m ctx k;
v;
loc_term (mk_ac a)
])))) in
let instr =
if is_partition m a f then
let mk_add_assign coll =
let add = dl (Tadd (dl oasset, map_mterm m ctx kb, loc_term coll)) in
dl (Tassign (loc_term coll, add)) in
let assign = mk_add_assign (mk_ac oasset) in
let assign_added = mk_add_assign (mk_ac_added oasset) in
let assigns = dl (Tseq [assign; assign_added]) in
dl (Tseq [assign; dl (mk_match_get_none ctx oasset v assigns EKeyExists)])
else dl (mk_match_get_some ctx oasset v assign ENotFound) in
mk_trace_seq m
(mk_match_get_some ctx a (map_mterm m ctx k) instr ENotFound)
([CUpdate f] @ if is_partition m a f then [CAdd oasset] else [])
| Mremoveasset (n, i) ->
let partitions = M.Utils.get_asset_partitions m n in
let remove = List.map (fun (f, oasset) ->
let capoasset = String.capitalize_ascii oasset in
let field = loc_term (Tdoti("_a", f)) in
let remove = dl (Tapp (loc_term (Tdoti(capoasset,"removeif_in_field")), [field; loc_term (mk_ac oasset)])) in
dl (Tassign (loc_term (mk_ac oasset), remove))
) partitions in
let tr_rm_oassets = List.map (fun (f,_) ->
let oasset, _, _ = M.Utils.get_container_asset_key m n f in
CRm oasset) partitions in
let remove =
if List.length remove > 1 then
dl (Tseq remove)
else if compare (List.length remove) 1 = 0 then
(List.hd remove)
else dl Tnone in
let remove_instr = dl (mk_match_get_some_id_nil (dl "_a") n (map_mterm m ctx i) remove) in
let mk_assign coll = dl (Tassign (loc_term coll,dl (Tremove(dl n,map_mterm m ctx i,loc_term coll)))) in
let mk_assign_add coll asset = dl (Tassign (loc_term coll,dl (Tadd(dl n,asset,loc_term coll)))) in
let assign = mk_assign (mk_ac n) in
let rm_instr = mk_assign_add (mk_ac_rmed n) (loc_term (Tvar "_a")) in
let assign_rmed = dl (mk_match_get_some_id_nil (dl "_a") n (map_mterm m ctx i) rm_instr) in
if List.length partitions > 0 then
mk_trace_seq m
(Tseq [remove_instr; assign; assign_rmed])
([CRm n] @ tr_rm_oassets)
else
mk_trace_seq m
(Tseq [assign; assign_rmed])
[CRm n]
| Mremovefield (a, f, k, kb) ->
let oasset, _, _ = M.Utils.get_container_asset_key m a f in
let t, _, _ = M.Utils.get_container_asset_key m a f in
let mk_rm_id = loc_term (Tdoti (mk_aggregate_id f, "remove")) in
let assign = dl (Tassign (loc_term (mk_ac a), dl (Tapp(mk_rm_id,[
map_mterm m ctx k;
map_mterm m ctx kb;
loc_term (mk_ac a)
])))) in
let instr =
if is_partition m a f then
let mk_assign_rm coll =
let rm = dl (Tremove (dl oasset, map_mterm m ctx kb, loc_term coll)) in
dl (Tassign (loc_term coll, rm)) in
let mk_assign_add coll asset =
let rm = dl (Tadd (dl oasset, asset, loc_term coll)) in
dl (Tassign (loc_term coll, rm)) in
let rm_assign = mk_assign_rm (mk_ac oasset) in
let rm_instr = mk_assign_add (mk_ac_rmed oasset) (loc_term (Tvar "_a")) in
let rm_assign_rmed = dl (mk_match_get_some_id_nil (dl "_a") oasset (map_mterm m ctx kb) rm_instr) in
dl (Tseq [rm_assign; rm_assign_rmed; assign])
else assign in
mk_trace_seq m
(mk_match_get_some ctx a (map_mterm m ctx k) instr ENotFound)
([CUpdate f] @ if is_partition m a f then [CRm t] else [])
| Mremoveall (a, f, v) ->
let rm_field = dl (Tapp (loc_term (Tdoti (mk_aggregate_id f,"removeall")),[map_mterm m ctx v; loc_term (mk_ac a)])) in
let assign_rm_field = dl (Tassign (loc_term (mk_ac a), rm_field)) in
let oasset , _, _ = M.Utils.get_container_asset_key m a f in
let instr =
if is_partition m a f then
let field = loc_term (Tdoti("_a", f)) in
let capoasset = String.capitalize_ascii oasset in
let rmif = dl (Tapp (loc_term (Tdoti(capoasset, "removeif_in_field")), [field; loc_term (mk_ac oasset)])) in
let assign_rmif = dl (Tassign(loc_term (mk_ac oasset), rmif)) in
mk_match_get_some_id ctx (dl "_a") a (map_mterm m ctx v) (dl (Tseq [assign_rmif; assign_rm_field])) ENotFound
else mk_match_get_some ctx a (map_mterm m ctx v) assign_rm_field ENotFound in
mk_trace_seq m instr ([CUpdate f] @ if is_partition m a f then [CRm oasset] else [])
| Mremoveif (_a, (CKview _l), _la, _lb, _) -> assert false
| Mremoveif (_a, CKdef _, _la, _lb, _) -> assert false
| Mremoveif (a, CKfield (_, field, k, _, _), args, tbody, _a) ->
let args = mk_filter_args m ctx args tbody in
let oasset, _ = M.Utils.get_field_container m a field in
let removeif_name = mk_removeif_name m oasset tbody in
let removeif = dl (Tfremoveif (dl (
mk_aggregate_id field),
dl removeif_name, args,
map_mterm m ctx k, mk_lc_term oasset ctx, mk_lc_term a ctx)) in
let assign = dl (Tassign(mk_lc_term a ctx, removeif)) in
if is_partition m a field then
let removecoll = loc_term (Tpremoveif(oasset, removeif_name, args |> List.map unloc_term, Tdoti("_a",field), mk_ac oasset)) in
let assign_rmcoll = dl (Tassign (loc_term (mk_ac oasset),removecoll)) in
let instr = dl (Tseq[assign_rmcoll; assign]) in
mk_trace_seq m (mk_match_get_some_id ctx (dl "_a") a (map_mterm m ctx k) instr ENotFound) [CUpdate field; CRm oasset]
else
mk_trace_seq m (mk_match_get_some ctx a (map_mterm m ctx k) assign ENotFound) [CUpdate field]
| Mremoveif (a, CKcoll _, args, tbody, _a) ->
let args = mk_filter_args m ctx args tbody in
let partitions = M.Utils.get_asset_partitions m a in
let remove = List.map (fun (f, oasset) ->
let capoasset = String.capitalize_ascii oasset in
let coll = Tselect(a, Tapp (Tvar (mk_removeif_name m a tbody), args |> List.map unloc_term), mk_ac a) in
let field = loc_term (Tapp (Tdoti(mk_aggregate_id f,"union"),[coll])) in
let remove = dl (Tapp (loc_term (Tdoti(capoasset,"removeif_in_field")), [field; loc_term (mk_ac oasset)])) in
dl (Tassign (loc_term (mk_ac oasset), remove))
) partitions in
let tr_rm_oassets = List.map (fun (f,_) ->
let oasset, _, _ = M.Utils.get_container_asset_key m a f in
CRm oasset) partitions in
let removeif =
dl (Tremoveif (dl a,
dl (mk_removeif_name m a tbody), args,
mk_lc_term a ctx)) in
if List.length partitions > 0 then
let assign = dl (Tassign (mk_lc_term a ctx, removeif)) in
mk_trace_seq m (Tseq (remove @ [assign])) ([CRm a] @ tr_rm_oassets)
else
mk_trace_seq m (Tassign (mk_lc_term a ctx, removeif)) [CRm a]
| Mclear (n, CKcoll _) ->
let partitions = M.Utils.get_asset_partitions m n in
let remove = List.map (fun (f, oasset) ->
let capoasset = String.capitalize_ascii oasset in
let field = loc_term (Tapp (Tdoti(mk_aggregate_id f,"union"),[mk_ac n])) in
let remove = dl (Tapp (loc_term (Tdoti(capoasset,"removeif_in_field")), [field; loc_term (mk_ac oasset)])) in
dl (Tassign (loc_term (mk_ac oasset), remove))
) partitions in
let tr_rm_oassets = List.map (fun (f,_) ->
let oasset, _, _ = M.Utils.get_container_asset_key m n f in
CRm oasset) partitions in
if List.length partitions > 0 then
let assign = dl (Tassign(loc_term (Tdoti(gs,mk_ac_id n)), loc_term (Temptycoll n))) in
mk_trace_seq m (Tseq (remove @ [assign])) ([CRm n] @ tr_rm_oassets)
else
mk_trace_seq m (Tassign(loc_term (Tdoti(gs,mk_ac_id n)), loc_term (Temptycoll n))) [CRm n]
| Mclear (n, CKview v) ->
let partitions = M.Utils.get_asset_partitions m n in
let remove = List.map (fun (f, oasset) ->
let capn = String.capitalize_ascii n in
let capoasset = String.capitalize_ascii oasset in
let viewvar = loc_term (Tvar "_view") in
let field = dl (Tunionpred (dl (mk_aggregate_id f), dl (capn^".is_in_view"), [viewvar], loc_term (mk_ac n))) in
let remove = dl (Tapp (loc_term (Tdoti(capoasset,"removeif_in_field")), [field; loc_term (mk_ac oasset)])) in
dl (Tletin(false, dl "_view",None,map_mterm m ctx v, dl (Tassign (loc_term (mk_ac oasset), remove))))
) partitions in
let tr_rm_oassets = List.map (fun (f,_) ->
let oasset, _, _ = M.Utils.get_container_asset_key m n f in
CRm oasset) partitions in
let field = map_mterm m ctx v in
let capasset = String.capitalize_ascii n in
let clear = dl (Tapp (loc_term (Tdoti(capasset,"removeif_in_view")),[field; loc_term (mk_ac n)])) in
let assign = Tassign (loc_term (mk_ac n), clear) in
if List.length partitions > 0 then
let instr = Tseq (remove @ [dl (assign)]) in
mk_trace_seq m instr ([CRm n] @ tr_rm_oassets)
else
mk_trace_seq m assign [CRm n]
| Mclear (_, CKdef _) -> assert false
| Mclear (_n, CKfield (n, f, v, _, _)) ->
let oasset,_ = M.Utils.get_field_container m n f in
let asset = dl (mk_match_get_some_id ctx (dl "_a") n (map_mterm m ctx v) (loc_term (Tvar "_a")) ENotFound) in
let field = dl (Tdot(asset, loc_term (Tvar f))) in
let capoasset = String.capitalize_ascii oasset in
let clear = dl (Tapp (loc_term (Tdoti(capoasset,"removeif_in_field")),[field; loc_term (mk_ac oasset)])) in
let assign = dl (Tassign (loc_term (mk_ac oasset), clear)) in
let rm_field = dl (Tapp (loc_term (Tdoti (mk_aggregate_id f,"removeall")),[map_mterm m ctx v; loc_term (mk_ac n)])) in
let assign_rm_field = dl (Tassign (loc_term (mk_ac n), rm_field)) in
mk_trace_seq m (Tseq [assign; assign_rm_field]) [CRm oasset]
| Mset (n, l, k, v) ->
mk_trace_seq m
(Tassign (loc_term (Tdoti(gs,mk_ac_id n)),dl (Tset(dl n,map_mterm m ctx k, map_mterm m ctx v,loc_term (mk_ac n)))))
(List.map (fun f -> CUpdate f) l)
| Mupdate _ -> error_not_translated "Mupdate"
| Maddupdate _ -> error_not_translated "Maddupdate"
| Maddforce _ -> error_not_translated "Maddforce"
| Mget (an, _c, k) ->
begin match ctx.lctx with
| Inv | Logic | Def -> Tget(dl an, map_mterm m ctx k,mk_lc_term an ctx)
| _ -> mk_get_force ctx (dl an) (map_mterm m ctx k) (mk_lc_term an ctx)
end
| Mselect (n, c, args, tbody, _a) -> begin
let args = mk_filter_args m ctx args tbody in
match c with
| CKcoll _ -> Tcselect (dl n, dl (mk_select_name m n tbody), args, mk_lc_term n ctx)
| _ ->
Tvselect (dl n, dl (mk_select_name m n tbody), args, mk_container_term m n ctx c, mk_lc_term n ctx)
end
| Msort (n, c,l) -> Tvsort (dl (mk_sort_clone_id n l),mk_container_term m n ctx c,mk_lc_term n ctx)
| Mcontains (n, c, r) -> begin
match c with
| CKcoll (t,d) -> Tcontains (dl n, map_mterm m ctx r, mk_loc_coll_term n ctx (t,d))
| _ -> Tvcontains (dl (mk_view_id n), map_mterm m ctx r, mk_container_term m n ctx c)
end
| Mnth (n, c, k) ->
let nth = Tnth (dl (mk_view_id n), map_mterm m ctx k, mk_container_term m n ctx c) in
begin match ctx.lctx with
| Logic | Inv | Def -> nth
| _ -> mk_match ctx (dl nth) "_a" (loc_term (Tvar "_a")) ENotFound
end
| Mcount (n, c) -> Tcard (dl (mk_view_id n), mk_container_term m n ctx c)
| Msum (n, c, f) ->
let cloneid = mk_sum_clone_id m n f in
let col = mk_loc_coll_term n ctx (mk_temp_delta c) in
Tvsum(dl cloneid , mk_container_term m n ctx c, col)
| Mhead (n, c, v) ->
Tvhead(dl (mk_view_id n), map_mterm m ctx v, mk_container_term m n ctx c)
| Mtail (n, c, v) ->
Tvtail(dl (mk_view_id n), map_mterm m ctx v, mk_container_term m n ctx c)
| Mcast ((Tcontainer ((Tasset a, _),Collection), _) , (Tcontainer ((Tasset _, _), View), _), v) ->
begin match v.node, ctx.lctx with
| Mapp(f,_), _ when is_coll_field m (unloc f) ->
map_mterm m ctx v |> Mlwtree.deloc
| Mvar (f, Vlocal, _, _), _ when is_coll_field m (unloc f) ->
map_mterm m ctx v |> Mlwtree.deloc
| Mdotassetfield (_, _, f), _ when is_coll_field m (unloc f) ->
map_mterm m ctx v |> Mlwtree.deloc
| _ -> Ttoview (map_lident a,map_mterm m ctx v)
end
| Mcast ((Tcontainer ((Tasset a, _), View), _) , (Tlist _, _), v) -> Telts(dl (mk_view_id (unloc a)), map_mterm m ctx v)
| Mcast ((Tbuiltin Baddress, _), (Tcontract _, _), v) -> Tapp (loc_term (Tvar "getopt"), [(dl (Tentrypoint (dl "", map_mterm m ctx v)))])
| Mcast ((Tmap _, _) as t, (Tlist _, _), c) -> Telts (dl (mk_map_name m t), map_mterm m ctx c)
| Mcast ((Tset _, _) as t, (Tlist _, _), c) -> Telts (dl (mk_set_name m t), map_mterm m ctx c)
| Mcast (_, _, v) -> map_mterm m ctx v |> Mlwtree.deloc
| Msetadd (t, s, e) -> Tadd (dl (mk_set_name m (M.tset t)), map_mterm m ctx e, map_mterm m ctx s)
| Msetremove (t, s, e) -> Tremove (dl (mk_set_name m (M.tset t)), map_mterm m ctx e, map_mterm m ctx s)
| Msetcontains (t, s, e) -> Tcontains (dl (mk_set_name m (M.tset t)), map_mterm m ctx e, map_mterm m ctx s)
| Msetlength (t, s) -> Tcard (dl (mk_set_name m (M.tset t)), map_mterm m ctx s)
| Msetfold _ -> error_not_translated "Mmapfold"
| Msetinstradd _ -> error_not_translated "Msetinstradd"
| Msetinstrremove _ -> error_not_translated "Msetinstrremove"
| Mlistprepend (t, l, e) -> Tprepend (dl (mk_list_name m (M.tlist t)), map_mterm m ctx e, map_mterm m ctx l)
| Mlistlength (t, l) -> Tcard (dl (mk_list_name m (M.tlist t)), map_mterm m ctx l)
| Mlistcontains (t, l, e) -> Tcontains (dl (mk_list_name m (M.tlist t)), map_mterm m ctx e, map_mterm m ctx l)
| Mlistnth (t, n, l) ->
let nth = Tnth (dl (mk_list_name m (M.tlist t)), map_mterm m ctx n, map_mterm m ctx l) in
begin match ctx.lctx with
| Logic | Inv | Def -> nth
| _ -> mk_match ctx (dl nth) "_a" (loc_term (Tvar "_a")) ENotFound
end
| Mlistreverse (t, l) -> Tlistreverse (dl (mk_list_name m (M.tlist t)), map_mterm m ctx l)
| Mlistconcat (t, l1, l2) -> Tlistconcat (dl (mk_list_name m (M.tlist t)), map_mterm m ctx l1, map_mterm m ctx l2)
| Mlistfold _ -> error_not_translated "Mlistfold"
| Mlistinstrprepend _ -> error_not_translated "Mlistinstrprepend"
| Mlistinstrconcat _ -> error_not_translated "Mlistinstrconcat"
| Mmapput (_, _, c, k, v) ->
Tadd (dl (mk_map_name m c.type_), dl (Ttuple [ map_mterm m ctx k; map_mterm m ctx v]), map_mterm m ctx c)
| Mmapremove (_, _, c, k) ->
Tremove (dl (mk_map_name m c.type_),map_mterm m ctx k, map_mterm m ctx c)
| Mmapupdate (_, _, c, k, v) ->
Tupdate (dl (mk_map_name m c.type_), map_mterm m ctx k, map_mterm m ctx v, map_mterm m ctx c)
| Mmapget (_, _, c, k) -> Tsnd(
dl (mk_get_force ctx (dl (mk_map_name m c.type_)) (map_mterm m ctx k) (map_mterm m ctx c)))
| Mmapgetopt (_, _, c, k) -> Tsndopt(
dl (Tget (dl (mk_map_name m c.type_),map_mterm m ctx k, map_mterm m ctx c)))
| Mmapcontains (_, _, c, k) ->
Tcontains (dl (mk_map_name m c.type_),map_mterm m ctx k, map_mterm m ctx c)
| Mmaplength (_, _, c) ->
let tmap = mk_map_name m c.type_ in Tcard (dl tmap,map_mterm m ctx c)
| Mmapfold _ -> error_not_translated "Mmapfold"
| Mmapinstrput _ -> error_not_translated "Mmapinstrput"
| Mmapinstrremove _ -> error_not_translated "Mmapinstrremove"
| Mmapinstrupdate _ -> error_not_translated "Mmapinstrupdate"
| Mmax (l,r) ->
begin match M.get_ntype mt.type_ with
| Ttuple _ -> Tapp (loc_term (Tvar "rat_max"),[map_mterm m ctx l; map_mterm m ctx r])
| _ -> Tapp (loc_term (Tvar "max"),[map_mterm m ctx l; map_mterm m ctx r])
end
| Mmin (l,r) ->
begin match M.get_ntype mt.type_ with
| Ttuple _ -> Tapp (loc_term (Tvar "rat_min"),[map_mterm m ctx l; map_mterm m ctx r])
| _ -> Tapp (loc_term (Tvar "min"),[map_mterm m ctx l; map_mterm m ctx r])
end
| Mabs v ->
begin match M.get_ntype v.type_ with
| M.Tbuiltin (M.Bint) -> Tapp (loc_term (Tvar "abs"),[map_mterm m ctx v])
| M.Ttuple [(M.Tbuiltin (M.Bint), _); (M.Tbuiltin M.Bnat, _)] ->
Tapp (loc_term (Tvar "abs_rat"),[map_mterm m ctx v])
| _ -> error_not_translated "Mfunabs"
end
| Mconcat (x, y) ->
begin
match M.get_ntype mt.type_ with
| Tbuiltin Bstring -> Tapp (loc_term (Tvar "str_concat"),[map_mterm m ctx x; map_mterm m ctx y])
| Tbuiltin Bbytes -> Tapp (loc_term (Tvar "str_concat"),[map_mterm m ctx x; map_mterm m ctx y])
| _ -> error_not_translated "Mconcat"
end
| Mconcatlist x ->
begin
match M.get_ntype mt.type_ with
| Tbuiltin Bstring -> Tapp (loc_term (Tvar "str_concat_list"),[map_mterm m ctx x])
| Tbuiltin Bbytes -> Tapp (loc_term (Tvar "str_concat_list"),[map_mterm m ctx x])
| _ -> error_not_translated "Mconcatlist"
end
| Mslice (s, i1, i2) ->
begin match M.get_ntype s.type_ with
| Toption (Tbuiltin Bbytes, _) -> Tapp (loc_term (Tvar "slice"),[map_mterm m ctx s; map_mterm m ctx i1; map_mterm m ctx i2])
| _ -> Tapp (loc_term (Tvar "slice"),[map_mterm m ctx s; map_mterm m ctx i1; map_mterm m ctx i2])
end
| Mlength s ->
begin match M.get_ntype s.type_ with
| Tbuiltin Bbytes -> Tapp (loc_term (Tvar "str_length"),[map_mterm m ctx s])
| _ -> Tapp (loc_term (Tvar "str_length"),[map_mterm m ctx s])
end
| Misnone s -> Tapp (loc_term (Tvar "isnone"),[map_mterm m ctx s])
| Missome s -> Tapp (loc_term (Tvar "issome"),[map_mterm m ctx s])
| Moptget s -> Tapp (loc_term (Tvar "getopt"),[map_mterm m ctx s])
| Mfloor s -> Tapp (loc_term (Tvar "floor"),[map_mterm m ctx s])
| Mceil s -> Tapp (loc_term (Tvar "ceil"),[map_mterm m ctx s])
| Mtostring (_, s) -> Tapp (loc_term (Tvar "from_int"),[map_mterm m ctx s])
| Mpack s -> Tapp (loc_term (Tvar "pack"),[map_mterm m ctx s])
| Munpack (_, s) -> Tapp (loc_term (Tvar "unpack"),[map_mterm m ctx s])
| Msetdelegate s -> Tapp (loc_term (Tvar "set_delegate"),[map_mterm m ctx s])
| Mimplicitaccount s -> Tapp (loc_term (Tvar "implicit_account"),[map_mterm m ctx s])
| Mblake2b x -> Tapp (loc_term (Tvar "blake2b"),[map_mterm m ctx x])
| Msha256 x -> Tapp (loc_term (Tvar "sha256"),[map_mterm m ctx x])
| Msha512 x -> Tapp (loc_term (Tvar "sha512"),[map_mterm m ctx x])
| Msha3 x -> Tapp (loc_term (Tvar "sha3"),[map_mterm m ctx x])
| Mkeccak x -> Tapp (loc_term (Tvar "keccak"),[map_mterm m ctx x])
| Mhashkey x -> Tapp (loc_term (Tvar "hash_key"),[map_mterm m ctx x])
| Mchecksignature (k,s,b) -> Tapp (loc_term (Tvar "check_signature"),[map_mterm m ctx k;map_mterm m ctx s;map_mterm m ctx b])
| Mtotalvotingpower -> Tvar (loc_ident "total_voting_power")
| Mvotingpower x -> Tapp (loc_term (Tvar "voting_power"), [map_mterm m ctx x])
| Mcreateticket (_x, _a) -> assert false
| Mreadticket _x -> assert false
| Msplitticket (_x, _a, _b) -> assert false
| Mjointickets (_x, _y) -> assert false
| Msapling_empty_state _n -> assert false
| Msapling_verify_update (_s, _t) -> assert false
| Mpairing_check _x -> assert false
| Mnow -> Tnow (dl gs)
| Mtransferred -> Ttransferred (dl gs)
| Mcaller -> Tcaller (dl gs)
| Mbalance ->
begin
match ctx.lctx with
| Inv -> loc_term (Tvar "_balance") |> Mlwtree.deloc
| _ -> loc_term (Tdoti (gs, "_balance")) |> Mlwtree.deloc
end
| Msource -> Tsender (dl gs)
| Mselfaddress -> Tdoti(dl gs, dl (mk_id "selfaddress"))
| Mchainid -> Tchainid (dl gs)
| Mmetadata -> assert false
| Mlevel -> assert false
| Mvar(_, Vassetstate _, _, _) -> error_not_translated "Mvar(_, Vassetstate _)"
| Mvar (v, Vstorevar, t, _) ->
begin
match ctx.fun_, ctx.lctx, t with
| true, _, _ -> Tdoti (dl gsarg, map_lident v)
| _, Inv, _ -> Tvar (map_lident v)
| _, Def, _ -> Tdoti (dl gsarg, map_lident v)
| _, _, M.Tnone -> Tdoti (dl gs, map_lident v)
| _, _, M.Tbefore -> Tdot (Told (Tvar gs), Tvar (unloc v)) |> loc_term |> Mlwtree.deloc
| _, _, M.Tat lbl -> Tat (dl lbl, Tdoti (gs, unloc v) |> loc_term)
end
| Mvar (n, Vstorecol, t, d) ->
let coll = mk_loc_coll_term (unloc n) ctx (t,d) in
coll |> Mlwtree.deloc
| Mvar (v, Vdefinition, _, _) ->
let params = get_def_params m (unloc v) |> List.map loc_term in
Tapp (loc_term (Tvar (unloc v)), [loc_term (Tvar (mk_storage_id ctx))] @ params)
| Mvar (v, Vlocal, _, _) -> Tvar (map_lident v)
| Mvar (v, Vparam, _, _) -> Tvar (map_lident v)
| Mvar (_, Vfield, _, _) -> error_not_translated "Mvar (_, Vfield)"
| Mvar (_, Vthe, _, _) -> error_not_translated "Mvar (_, Vthe)"
| Mvar (_, Vstate, _, _) ->
begin
match ctx.lctx with
| Inv -> loc_term (Tvar "state") |> Mlwtree.deloc
| _ -> loc_term (Tdoti (gs, "state")) |> Mlwtree.deloc
end
| Mvar (v, Vparameter, _, _) -> Tvar (v |> unloc |> mk_param_value |> loc_ident)
| Menumval _ -> error_not_translated "Menumval"
| Mrateq (r,t) -> Tapp (loc_term (Tvar "rat_eq"),[map_mterm m ctx r; map_mterm m ctx t])
| Mratcmp (cop,r,t) ->
let cop_to_mterm = function
| M.Ge -> Tvar "OpCmpGe"
| M.Le -> Tvar "OpCmpLe"
| M.Gt -> Tvar "OpCmpGt"
| M.Lt -> Tvar "OpCmpLt" in
Tapp (loc_term (Tvar "rat_cmp"),[loc_term (cop_to_mterm cop); map_mterm m ctx r; map_mterm m ctx t])
| Mratarith (aop,r,t) ->
let aop_to_mterm = function
| M.Rplus -> Tvar "OpArithPlus"
| M.Rminus -> Tvar "OpArithMinus"
| M.Rmult -> Tvar "OpArithMult"
| M.Rdiv -> Tvar "OpArithDiv" in
Tapp (loc_term (Tvar "rat_arith"),[loc_term (aop_to_mterm aop); map_mterm m ctx r; map_mterm m ctx t])
| Mratuminus v -> Tapp (loc_term (Tvar "rat_uminus"),[map_mterm m ctx v])
| Mrattez (r,t) -> Tapp (loc_term (Tvar "rat_tez"),[map_mterm m ctx r; map_mterm m ctx t])
| Mnattoint v -> map_mterm m ctx v |> Mlwtree.deloc
| Mnattorat v -> Ttuple ([map_mterm m ctx v; loc_term (Tint (Big_int.big_int_of_int 1))])
| Minttorat v -> Ttuple ([map_mterm m ctx v; loc_term (Tint (Big_int.big_int_of_int 1))])
| Mratdur (r,t) -> Tapp (loc_term (Tvar "rat_dur"),[map_mterm m ctx r; map_mterm m ctx t])
| Mdatefromtimestamp _ -> assert false
| Mforall (i, t, None, b) ->
let typ = map_mtype m t in
Tforall (
[[i |> map_lident],typ],
map_mterm m ctx b)
| Mforall (i, t, Some coll, b) ->
let asset = M.Utils.get_asset_type (M.mk_mterm (M.Mbool false) t) in
Tforall (
[[i |> map_lident],loc_type (Tyasset asset)],
dl (Timpl (dl (Tmem (dl asset,
loc_term (Tvar (unloc i)),
map_mterm m ctx coll)),
map_mterm m ctx b)))
| Mexists (i, t, None, b) ->
let typ = map_mtype m t in
Texists (
[[i |> map_lident],typ],
map_mterm m ctx b)
| Mexists (i, t, Some coll, b) ->
let asset = M.Utils.get_asset_type (M.mk_mterm (M.Mbool false) t) in
Texists (
[[i |> map_lident],loc_type (Tyasset asset)],
dl (Timpl (dl (Tmem (dl asset,
loc_term (Tvar (unloc i)),
map_mterm m ctx coll)),
map_mterm m ctx b)))
| Mimply (a, b) -> Timpl (map_mterm m ctx a, map_mterm m ctx b)
| Mequiv (a, b) -> Tequiv (map_mterm m ctx a, map_mterm m ctx b)
| Msetiterated container ->
let n = M.Utils.get_asset_type mt in
let iter_id = Option.get (ctx.loop_id) in
let arg = begin match container with
| ICKview v -> map_mterm m ctx v
| ICKcoll n -> dl (Ttoview (dl n, mk_lc_term n ctx))
| ICKfield (_, _, c) -> dl (Ttoview (dl (mk_field_id n), map_mterm m ctx c))
| ICKset _ -> assert false
| ICKlist _ -> assert false
| ICKmap _ -> assert false
end in
Tvhead (dl (mk_view_id n), loc_term (Tvar iter_id), arg)
| Msettoiterate container ->
let n = M.Utils.get_asset_type mt in
let iter_id = Option.get (ctx.loop_id) in
let arg = begin match container with
| ICKview v -> map_mterm m ctx v
| ICKcoll n -> dl (Ttoview (dl n, mk_lc_term n ctx))
| ICKfield (_, _, c) -> dl (Ttoview (dl (mk_field_id n), map_mterm m ctx c))
| ICKset _ -> assert false
| ICKlist _ -> assert false
| ICKmap _ -> assert false
end in
Tvtail (dl (mk_view_id n), loc_term (Tvar iter_id), arg)
| Mempty an -> Temptycoll(dl (mk_view_id an))
| Msingleton (an, k) -> Tsingl(dl an, map_mterm m ctx k)
| Msubsetof (n, c, x) ->
let arg = mk_container_term m n ctx c in
Tsubset(dl (mk_view_id n), arg, map_mterm m ctx x)
| Misempty (n, r) ->
begin match M.get_ntype r.type_ with
| M.Tcontainer (_,View) -> Tvempty (dl (mk_view_id n), map_mterm m ctx r)
| _ -> Tempty (dl n, map_mterm m ctx r)
end
| Munion (an, l, r) -> begin
let l = to_collection an l in
let r = to_collection an r in
Tunion(dl an, l, r)
end
| Minter (an, l, r) -> begin
let l = to_collection an l in
let r = to_collection an r in
Tinter(dl an, l, r)
end
| Mdiff (an, l, r) -> begin
let l = to_collection an l in
let r = to_collection an r in
Tdiff(dl an, l, r)
end
in
mk_loc mt.loc t
and mk_invariants (m : M.model) ctx id (lbl : ident option) lbody =
let loop_invariants =
Option.fold (M.Utils.get_loop_invariants m) [] lbl |>
List.map (fun ((ilbl : ident),(i : M.mterm)) ->
let iid =
match lbl,ilbl with
| Some a, b -> b ^ "_" ^ a
| None, b -> b in
let ctx = { ctx with loop_id = Option.map unloc id } in
{ id = dl 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 = dl 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 = dl id;
form = map_security_pred `Loop sec.predicate |> loc_term; }
)
in
let vars_loop_invariants = mk_vars_loop_invariants m ctx.entry_id lbl (mk_lbl_before lbl) lbody in
loop_invariants @
storage_loop_invariants @
security_loop_invariants @
vars_loop_invariants
and mk_filter_args m ctx args tbody =
let globals =
extract_args tbody |>
List.map (fun (e, _, _) -> e) |>
List.map (map_mterm m ctx) in
let args = List.map (fun (i,_) -> loc_term (Tvar i)) args in
args @ globals
and mk_asset_key_value m ctx a r = begin
match r with
| { M.node = (Masset _); type_ = _ } ->
map_mterm m ctx (M.Utils.extract_key_value_from_masset m r)
| _ ->
let (k, _) = M.Utils.get_asset_key m a in
dl (Tapp(loc_term (Tvar k),[map_mterm m ctx r]))
end
and mk_container_term m n ctx = function
| M.CKview c -> map_mterm m ctx c
| M.CKfield (_, _, c, _, _) -> dl (Ttoview (dl (mk_field_id n), map_mterm m ctx c))
| M.CKcoll (t,d) -> dl (Ttoview (dl n, mk_loc_coll_term n ctx (t,d)))
| M.CKdef d ->
let params = get_def_params m d in
loc_term (Tapp(Tvar d, [Tvar (mk_storage_id ctx)] @ params))
let map_asset_values m (values : M.asset_item list) =
let ctx = { init_ctx with lctx = Inv } in
List.map (fun (value : M.asset_item) ->
let typ_ = map_mtype m value.type_ in
let init_value = type_to_init m typ_ in {
name = map_lident value.name;
typ = typ_;
init = Option.fold (fun _ -> map_mterm m ctx) init_value value.default;
mutable_ = false;
}
) values
let mk_asset m (r : M.asset) =
Drecord (map_lident r.name, map_asset_values m r.values)
let map_init_mterm m ctx (t : M.mterm) =
match t.node with
| M.Mnow -> loc_term (Tint Big_int.zero_big_int)
| _ -> map_mterm m ctx t
let mk_storage_items m =
let ctx = { init_ctx with lctx = Inv } in
List.fold_left (fun acc (item : M.storage_item) ->
acc @
match M.get_ntype item.typ with
| M.Tcontainer ((Tasset id, _), Collection) ->
let id = unloc id in [
mk_collection_field id mk_ac_id (Some (match item.default.node with | Massets l -> List.map (map_mterm m ctx) l | _ -> assert false));
mk_collection_field id mk_ac_added_id None;
mk_collection_field id mk_ac_rmed_id None
]
| M.Tcontainer ((Tasset id, _), View) ->
let id = unloc id in [
mk_collection_field id mk_ac_id None;
mk_collection_field id mk_ac_added_id None;
mk_collection_field id mk_ac_rmed_id None
]
| _ ->
let typ_ = map_mtype m item.typ in [{
name = unloc item.id |> dl;
typ = typ_;
init = map_init_mterm m ctx item.default;
mutable_ = true;
}]
) []
let mk_asset_invariants m ctx =
List.concat (List.map (fun (item : M.storage_item) ->
let storage_id = item.id in
let invs : M.label_term list =
match item.model_type with
| MTasset asset_name ->
begin
try
let assets = M.Utils.get_assets m in
let asset = List.find (fun (x : M.asset) -> cmp_ident (unloc x.name) asset_name) assets in
asset.invariants
with
| Not_found -> assert false
end
| _ -> [] in
List.map (fun (inv : M.label_term) -> mk_storage_invariant m storage_id inv.label (map_mterm m ctx inv.term)) invs
) m.storage)
let mk_contract_invariants m ctx =
List.fold_left (fun acc (post : M.postcondition) ->
acc @ [{
id = map_lident post.name;
form = map_mterm m ctx post.formula;
}]
) [] m.specification.postconditions
let mk_variable_invariants m ctx =
List.fold_left (fun acc decl ->
match decl with
| M.Dvar var ->
acc @ (List.map (fun (inv : M.label_term) ->
{ id = map_lident inv.label; form = map_mterm m ctx inv.term}
) var.invariants)
| _ -> acc
) [] m.decls
let mk_security_invariants (m : M.model) _ctx =
List.fold_left (fun acc sec ->
acc @ (mk_spec_invariant `Storage sec)
) [] m.security.items
let mk_state_invariants m ctx =
List.fold_left (fun acc decl ->
match decl with
| M.Denum e ->
List.fold_left (fun acc (value : M.enum_item) ->
acc @ List.map (fun (inv : M.label_term) ->
mk_state_invariant m value.name inv.label (map_mterm m ctx inv.term)
) value.invariants
) acc e.values
| _ -> acc
) [] m.decls
let mk_storage m (l : M.storage) =
let ctx = { init_ctx with lctx = Inv } in
Dstorage {
fields = (mk_storage_items m l) @ (mk_const_fields m |> loc_field |> deloc);
invariants =
mk_asset_invariants m ctx @
mk_security_invariants m ctx @
mk_state_invariants m ctx @
mk_contract_invariants m ctx @
mk_variable_invariants m ctx
}
let mk_cp_storage m (l : M.storage) =
let arg = "_s_storage" in
Dfun {
name = "_cp_storage" |> dl;
logic = Logic;
args = [arg |> dl, Tystorage |> dl];
returns = Tystorage |> dl;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [{
id = dl "cp_1";
form = loc_term (Teq(Tyint,Tresult,Tvar arg));
}];
body = dl (Trecord (None, (List.map (fun (f : ('a, loc_typ, ident with_loc) abstract_field) ->
f.name, dl (Tdoti(dl arg,f.name))
) (mk_storage_items m l)) @ (List.map (fun (f : (('a, 'b, ident) abstract_term, (ident, (ident, 'c) abstract_type) abstract_type, ident) abstract_field) ->
dl f.name, dl (Tdoti (dl arg, dl f.name))
) (mk_const_fields m))))
}
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,
dl (asset ^ "_" ^ id ^ "_axiom"),
mk_axiom_invariant m asset (map_mterm m init_ctx formula));
Dtheorem (Lemma,
dl (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 =
let coll = match old with
| `Old -> mk_ac_old asset
| `Curr -> mk_ac asset in
Tneq(Tyint, Tget (asset, var, coll), Tnone)
let mk_not_found_cond old asset var =
let coll =
match old with
| `Curr -> mk_ac asset
| `Old -> mk_ac_old asset in
Teq(Tyint, Tget(asset, var, coll), Tnone)
let mk_get_sum_value_from_pos asset id formula =
Dfun {
name = mk_get_sum_value_from_pos_id asset id;
logic = Logic;
args = ["v",Tyview asset; "c",Tycoll asset; "i",Tyint];
returns = Tyint;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body =
let rec mk_body = function
| Tdot (Tvar v,f) when compare v "the" = 0 ->
Tmatch (
Tcnth (asset,
Tvar "i",
Tvar "v"), [
Tpsome "k",(Tmatch (Tget(asset,
Tvar "k",
Tvar "c"),[
Tpsome "e", Tapp (f,[Tvar "e"]);
Twild, Tint (Big_int.zero_big_int)
]));
Twild,Tint (Big_int.zero_big_int)
]
)
| _ as t -> map_abstract_term mk_body Tools.id Tools.id t in
mk_body formula
}
let mk_get_sum_value asset id formula =
Dfun {
name = mk_get_sum_value_id asset id;
logic = Logic;
args = ["a",Tyasset asset];
returns = Tyint;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body =
let rec mk_body = function
| Tdot (Tvar v,f) when compare v "the" = 0 ->
Tdot (Tvar "a",f)
| _ as t -> map_abstract_term mk_body Tools.id Tools.id t in
mk_body formula
}
let parameter_to_val model (p : M.parameter) : (loc_term, loc_typ, loc_ident) abstract_decl =
let id = p.name |> unloc |> mk_param_value |> loc_ident in
Dval (false, id, map_mtype model p.typ)
let mk_storage_api_before_storage (m : M.model) _records =
m.api_items |> List.fold_left (fun acc (sc : M.api_storage) ->
match sc.node_item, sc.api_loc with
| M.APIAsset (Sum (asset,_,_,formula)), _ when compare asset "todo" <> 0 ->
let key, tkey = M.Utils.get_asset_key m asset in
let mlw_formula = map_mterm m init_ctx formula |> unloc_term in
let id = M.Utils.get_sum_idx m asset formula in
acc @ [
mk_get_sum_value asset id mlw_formula;
mk_sum_clone m asset key tkey formula ]
| M.APIAsset (Select (asset, _, args, test)), _ ->
let mlw_test = map_mterm m init_ctx test in
acc @ [ mk_select_predicate m asset test (mlw_test |> unloc_term) args ]
| M.APIAsset (RemoveIf (asset, Field (_,field), args, test)), _ ->
let mlw_test = map_mterm m init_ctx test in
let oasset,_ = M.Utils.get_field_container m asset field in
acc @ [ mk_removeif_predicate m oasset test (mlw_test |> unloc_term) args ]
| M.APIAsset (RemoveIf (asset, Coll, args, test)), _ ->
let mlw_test = map_mterm m init_ctx test in
acc @ [ mk_removeif_predicate m asset test (mlw_test |> unloc_term) args ]
| _ -> acc
) [] |> loc_decl |> deloc
let mk_storage_api (m : M.model) _records =
m.api_items |> List.fold_left (fun acc (sc : M.api_storage) ->
match sc.node_item, sc.api_loc with
| M.APIAsset (Sort (asset, _, field)), _ ->
acc @ [ mk_cmp_function m asset field; mk_sort_clone m asset field]
| M.APIBuiltin(Babs ((M.Tbuiltin M.Bint, _))), _ ->
acc @ [Duse (true,["int";"Abs"],None)]
| _ -> acc
) [] |> loc_decl |> deloc
let fold_fails m ctx body : (loc_ident option * loc_term) list =
let rec internal_fold_fails acc (term : M.mterm) =
match term.M.node with
| M.Mfail (Invalid v) ->
let idx = get_fail_idx m v.type_ in
let fails =
Option.fold (fun acc (spec : M.specification) -> acc @ spec.fails) []
(Option.fold (fun _ id -> M.Utils.get_specification m id) None ctx.entry_id) in
let formulas = List.fold_left (fun acc (fail : M.fail) ->
let fidx = get_fail_idx m fail.atype in
if compare idx fidx = 0 then
acc @ [
Some (map_lident fail.label),
dl (Timpl (loc_term (Texn (Efail (idx, Some (Tvar (unloc fail.arg))))), map_mterm m {ctx with fails = true} (fail.formula)))
]
else acc
) [] fails in
if compare (List.length formulas) 0 = 0 then
acc @ [None, loc_term (Texn (Efail (idx, None)))]
else acc @ formulas
| _ -> M.fold_term internal_fold_fails acc term in
internal_fold_fails [] body
let fold_exns m body : term list =
let rec internal_fold_exn acc (term : M.mterm) =
match term.M.node with
| M.Mget (_, _, k) -> internal_fold_exn (acc @ [Texn ENotFound]) k
| M.Mmapget (_ , _, c, k) -> internal_fold_exn (internal_fold_exn (acc @ [Texn ENotFound]) k) c
| M.Mnth (_, CKview c, k) -> internal_fold_exn (internal_fold_exn (acc @ [Texn ENotFound]) c) k
| M.Mnth (_, CKcoll _, k) -> internal_fold_exn ((acc @ [Texn ENotFound])) k
| M.Mset (_, _, k, v) -> internal_fold_exn (internal_fold_exn (acc @ [Texn ENotFound]) k) v
| M.Maddasset (_, i) -> internal_fold_exn (acc @ [Texn EKeyExists]) i
| M.Maddfield (a, f, c, i) ->
internal_fold_exn
(internal_fold_exn (acc @ if (is_partition m a f) then [Texn EKeyExists; Texn ENotFound]
else [Texn ENotFound ]) c) i
| M.Mremovefield (_,_,k,v) -> internal_fold_exn
(internal_fold_exn (acc @ [Texn ENotFound]) k) v
| M.Mremoveall (_a,_f,v) -> internal_fold_exn (acc @ [Texn ENotFound]) v
| M.Mremoveif (_, CKfield (_,_,k,_,_), _, _ ,_ ) -> internal_fold_exn (acc @ [Texn ENotFound]) k
| M.Mclear (_a,CKfield (_,_,k,_,_)) -> internal_fold_exn (acc @ [Texn ENotFound]) k
| M.Moptget _ -> acc @ [Texn ENotFound]
| M.Mfail InvalidCaller -> acc @ [Texn EInvalidCaller]
| M.Mfail NoTransfer -> acc @ [Texn ENoTransfer]
| M.Mfail (InvalidCondition lbl) -> acc @ [Texn (EInvalidCondition lbl)]
| M.Mfail InvalidState -> acc @ [Texn EInvalidState]
| M.Mfail NatAssign -> acc @ [Texn ENatAssign]
| M.Mfail Invalid v ->
let idx = get_fail_idx m v.type_ in
acc @ [Texn (Efail (idx, None))]
| M.Mlistnth _ -> acc @ [Texn ENotFound]
| M.Mself _ -> acc @ [Texn ENotFound]
| M.Mcast ((Tbuiltin Baddress, _), (Tcontract _, _), v) -> internal_fold_exn (acc @ [Texn ENotFound]) v
| M.Mtransfer (TKself (v, _, _)) -> internal_fold_exn (acc @ [Texn ENotFound]) v
| M.Mtransfer (TKsimple (v, _))
| M.Mtransfer (TKentry (v, _, _))
| M.Mtransfer (TKcall (v, _, _, _, _))
| M.Mtransfer (TKoperation v) -> internal_fold_exn acc v
| M.Mapp (id, args) ->
let fun_struct = M.Utils.get_function m (unloc id) in
List.fold_left (fun acc arg ->
internal_fold_exn acc arg) (internal_fold_exn acc fun_struct.body) args
| _ -> M.fold_term internal_fold_exn acc term in
Tools.List.dedup (internal_fold_exn [] body)
let mk_theory m =
List.fold_left (fun acc (spec : M.specification) ->
let ctx = { init_ctx with lctx = Def } in
let defs = List.map (fun (def : M.definition) ->
let t = map_mtype m (M.mktype (Tcontainer (def.typ, View))) in
let asset = M.Utils.type_to_asset def.typ in
let params = extract_def_args m def.body |> List.map (fun (_,id,typ) -> (dl id,typ)) in
Dfun {
name = map_lident def.name;
logic = LogicOnly;
args = [ dl gsarg, dl Tystorage ] @ params;
returns = t;
raises = [];
fails = [];
variants = [];
requires = [];
ensures = [];
body =
let coll = loc_term (mk_ac_st gsarg asset) in
let select = dl (Tselect(
dl (M.Utils.type_to_asset def.typ),
dl (Tlambda ([map_lident def.var],map_mterm m ctx def.body)),
coll)) in
dl (Ttoview (dl asset,select))
}
) spec.definitions in
let preds = List.map (fun (pred : M.predicate) ->
let args = pred.args |> List.map (fun (i,t) -> map_lident i, map_mtype m t) in
let params = extract_def_args m pred.body |> List.map (fun (_,id,typ) -> (dl id,typ)) in
Dpred (map_lident pred.name, [dl gsarg, dl Tystorage] @ params @ args, map_mterm m ctx pred.body)
) spec.predicates in
acc @ defs @ preds
) [] (M.Utils.get_specifications m)
let is_fail (t : M.mterm) =
match t.node with
| M.Mfail _ -> true
| _ -> false
let flatten_if_fail m ctx (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 ctx c, map_mterm m ctx th,None))]) e
| _ -> acc @ [map_mterm m ctx t] in
mk_loc t.loc (Tseq (rec_flat [] t))
let mk_ensures m ctx acc (v : M.specification) =
acc @ (List.map (fun (spec : M.postcondition) -> {
id = spec.name |> map_lident;
form = map_mterm m { ctx with lctx = Logic } spec.formula
}) (v.postconditions |> List.filter M.Utils.is_post))
let mk_delta_requires m =
M.Utils.get_assets m |>
List.map (fun (a : M.asset) ->
let name = unloc a.name in
[{
id = dl ("require_" ^ name ^ "_added_isempty");
form =
Tempty (name, mk_ac_added name)
|> loc_term
}; {
id = dl (name ^ "_removed_isempty");
form =
Tempty (name, mk_ac_rmed name)
|> loc_term
}]
) |> List.flatten
let mk_preconds m (args : M.argument list) body : ((loc_term,loc_ident) abstract_formula) list =
List.fold_left (fun acc (id, t, _ : M.argument) ->
match M.get_ntype 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 = dl 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, _), Aggregate)
| 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 = dl lbl;
form = mk_pre_coll m n (unloc id) (map_mterm m init_ctx t)
}]
else
acc
) []
| _ -> acc
) [] args
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 (mk_trace_id Entry id))) in
[
{
id = dl "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 = dl "empty_ops";
form = Teq (Tyint,
Tdoti (gs, mk_id "ops"),
Tnil gListAs)
|> loc_term
};
{
id = dl "empty_trace";
form = Teq (Tyint,
Tdoti (gs, mk_id "tr"),
Tnil gListAs)
|> loc_term
}
]
else []
let mk_functions m =
M.Utils.get_functions m |> 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 m t)
) s.args) in
let ctx = { init_ctx with entry_id = Some (unloc s.name); fun_ = true } in
let with_side_effect =
let rec aux (accu : bool) (mt : M.mterm) : bool =
match mt.node with
| Mfail _ -> Format.eprintf "Mfail"; true
| Moperations -> Format.eprintf "Moperations"; true
| Moptget _ -> Format.eprintf "Moptget "; true
| Mmap _ -> Format.eprintf "Mmap "; true
| Mlistnth _ -> Format.eprintf "Mlistnth "; true
| Mmapget _ -> Format.eprintf "Mmapget "; true
| Maddasset _ -> Format.eprintf "Maddasset "; true
| Maddfield _ -> Format.eprintf "Maddfield "; true
| Mremoveasset _ -> Format.eprintf "Mremoveasset"; true
| Mremovefield _ -> Format.eprintf "Mremovefield"; true
| Mremoveall _ -> Format.eprintf "Mremoveall "; true
| Mremoveif _ -> Format.eprintf "Mremoveif "; true
| Mclear _ -> Format.eprintf "Mclear "; true
| Mset _ -> Format.eprintf "Mset "; true
| Mupdate _ -> Format.eprintf "Mupdate "; true
| Maddupdate _ -> Format.eprintf "Maddupdate "; true
| Maddforce _ -> Format.eprintf "Maddforce "; true
| Mget _ -> Format.eprintf "Mget "; true
| Mselect _ -> Format.eprintf "Mselect "; true
| Msort _ -> Format.eprintf "Msort "; true
| Mcontains _ -> Format.eprintf "Mcontains "; true
| Mnth _ -> Format.eprintf "Mnth "; true
| Mcount _ -> Format.eprintf "Mcount "; true
| Msum _ -> Format.eprintf "Msum "; true
| Mhead _ -> Format.eprintf "Mhead "; true
| Mtail _ -> Format.eprintf "Mtail "; true
| _ -> accu || M.fold_term aux accu mt
in
aux false s.body
in
Dfun {
name = map_lident s.name;
logic = if with_side_effect then NoMod else Logic;
args = [dl gsarg, loc_type Tystorage] @ args;
returns = map_mtype m t;
raises = fold_exns m s.body |> List.map loc_term;
fails = fold_fails m { ctx with lctx = Logic } s.body;
variants = [];
requires =
(mk_entry_require m (M.Utils.get_callers m (unloc s.name))) @
(mk_preconds m s.args s.body);
ensures = Option.fold (mk_ensures m ctx) [] v;
body = flatten_if_fail m { ctx with fun_ = true } s.body;
}
)
let mk_entries m =
M.Utils.get_entries m |> List.map (
fun ((v : M.specification option),
(s : M.function_struct)) ->
let ctx = { init_ctx with entry_id = Some (unloc s.name) } in
Dfun {
name = map_lident s.name;
logic = NoMod;
args = (List.map (fun (i,t,_) ->
(map_lident i, map_mtype m t)
) s.args);
returns = dl Tyunit;
raises = fold_exns m s.body |> List.map loc_term;
fails = fold_fails m { ctx with lctx = Logic; fails = true } s.body;
variants = [];
requires =
(mk_entry_require m [unloc s.name]) @
(mk_delta_requires m);
ensures = Option.fold (mk_ensures m ctx) [] v;
body = flatten_if_fail m ctx s.body;
}
)
let rm_fail_exn = List.filter (fun e ->
match unloc_term e with
| Texn ENotFound
| Texn EKeyExists -> 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 =
let body =
loc_term (Ttry (unloc_term f.body, [
ENotFound,Tassert (Some ("security_" ^ id),Tfalse);
EKeyExists,Tassert (Some ("security_" ^ id),Tfalse)
])) in
loc_term (
Tletin (false, gsinit, None, cp_storage gs, unloc_term body));
}
| _ ->
Dfun { f with
body = loc_term (
Tletin (false, gsinit, None, cp_storage gs, unloc_term f.body));
}
end
| _ -> d
type desc_container =
| Dset of M.type_
| Dlist of M.type_
| Dmap of M.type_
| Dasset of M.asset
| Denum of M.enum
| Drecord of M.record
[@@deriving show {with_path = false}]
let pp_desc_container fmt dc =
let pp = Format.fprintf fmt in
match dc with
| Dset v -> pp "%a" Printer_model.pp_type v
| Dlist v -> pp "%a" Printer_model.pp_type v
| Dmap v -> pp "%a" Printer_model.pp_type v
| Dasset a -> Format.fprintf fmt "%s" (unloc a.name)
| Denum e -> Format.fprintf fmt "%s" (unloc e.name)
| Drecord r -> Format.fprintf fmt "%s" (unloc r.name)
let cmp_desc_container (d1 : desc_container) (d2 : desc_container) : bool =
match d1, d2 with
| Dset t1, Dset t2 -> M.cmp_type t1 t2
| Dlist t1, Dlist t2 -> M.cmp_type t1 t2
| Dmap t1, Dmap t2 -> M.cmp_type t1 t2
| Dasset a1, Dasset a2 -> M.cmp_ident (unloc a1.name) (unloc a2.name)
| Denum e1, Denum e2 -> M.cmp_ident (unloc e1.name) (unloc e2.name)
| Drecord r1, Drecord r2 -> M.cmp_ident (unloc r1.name) (unloc r2.name)
| _ -> false
let mk_decls (model : M.model) =
let push x l =
if List.exists (cmp_desc_container x) l
then l
else l @ [x]
in
let rec for_type (accu : desc_container list) (t : M.type_) : desc_container list =
match M.get_ntype t with
| Tlist ty -> for_type accu ty |> push (Dlist t)
| Tset ty -> for_type accu ty |> push (Dset t)
| Tmap (_, kty, vty) -> for_type (for_type accu kty) vty |> push (Dmap t)
| Toption t -> for_type accu t
| Ttuple ts -> List.fold_left (for_type) accu ts
| Tor (a, b) -> for_type (for_type accu a) b
| Tlambda (a, b) -> for_type (for_type accu a) b
| Tcontract t -> for_type accu t
| Tticket t -> for_type accu t
| Tprog t -> for_type accu t
| Tvset (_, t) -> for_type accu t
| _ -> accu
in
let for_decl (d : M.decl_node) (accu : desc_container list) : desc_container list =
match d with
| Dvar _v -> accu
| Denum e -> push (Denum e) accu
| Dasset a -> push (Dasset a) accu
| Drecord r -> push (Drecord r) accu
in
let desc_containers = M.Utils.get_all_gen_mterm_type (M.Utils.get_all_type_for_mterm for_type) for_type for_decl model in
let for_decl accu d =
let ds =
match d with
| Dset t -> mk_set_type model t
| Dlist t -> mk_list_type model t
| Dmap t -> mk_map_type model t
| Dasset _a -> []
| Denum _e -> []
| Drecord r -> [mk_record model r]
in
accu @ ds
in
List.fold_left for_decl [] desc_containers
let to_whyml (m : M.model) : mlw_tree =
let decls = mk_decls m in
let assets = M.Utils.get_assets m in
let storage_module = dl ((mk_module_name m.name.pldesc) ^ "_storage") in
let uselib = mk_use in
let uselist = mk_use_list in
let uses = [mk_use; mk_use_list; mk_use_field; mk_use_view] in
let useEuclDiv = mk_use_euclidean_div m in
let useMinMax = mk_use_min_max m in
let traceutils = mk_trace_utils m |> deloc in
let enums = M.Utils.get_enums m |> List.map (mk_enum m) in
let exns = M.Utils.get_all_fail_types m |> List.mapi (mk_exn m) in
let records = M.Utils.get_records m |> List.map (mk_record m) in
let mlwassets = assets |> List.map (mk_asset m) |> wdl in
let eq_enums = assets |> List.map (mk_eq_enums m) |> List.flatten in
let eq_keys = assets |> List.map (mk_eq_key m) |> wdl in
let le_keys = assets |> List.map (mk_le_key m) |> wdl in
let eq_assets = assets |> List.map (mk_eq_asset m) |> wdl in
let colls = assets |> List.map (mk_coll m) |> wdl in
let fields = assets |> List.map (mk_field m) |> wdl in
let views = assets |> List.map (mk_view m) |> wdl in
let aggregates = assets |> List.map (mk_aggregates m) |> List.flatten in
let init_records = mlwassets |> unloc_decl |> List.map mk_default_init |> loc_decl in
let mlwassets = zip mlwassets eq_keys le_keys eq_assets init_records views fields colls |> deloc in
let parameters = List.map (parameter_to_val m) m.parameters in
let storage_api_bs = mk_storage_api_before_storage m (records |> wdl) in
let storage = M.Utils.get_storage m |> mk_storage m in
let cp_storage = M.Utils.get_storage m |> mk_cp_storage m in
let storageval = Dval (true, dl gs, dl Tystorage) in
let axioms = mk_axioms m in
let theory = mk_theory m in
let storage_api = mk_storage_api m (mlwassets |> wdl) in
let functions = mk_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 = uses @
useEuclDiv @
useMinMax @
traceutils @
decls @
exns @
enums @
eq_enums @
mlwassets @
aggregates @
storage_api_bs @
parameters @
[storage;cp_storage;storageval] @
axioms @
storage_api @
theory;
};{
name = dl (mk_module_name (unloc m.name));
decls = [uselib;uselist;usestorage] @
functions @
entries;
}] in unloc_tree loct