marlowe_lang 0.2.1

experimental parser lib for Cardano Marlowe DSL
Documentation
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
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
# Marlowe Specification


## Version 3


#### Pablo Lamela Seijas Alexander Nemish David Smith


#### Simon Thompson Hernán Rajchert Brian Bush


#### December 31, 1979



## Contents




- 1 Marlowe
   - 1.1 Introduction
   - 1.2 The Marlowe Model.
      - 1.2.1 Data types.
      - 1.2.2 Quiescent
      - 1.2.3 Participants, accounts and state
      - 1.2.4 Core and Extended
   - 1.3 Specification generation and nomenclature
   - 1.4 Blockchain agnostic
- 2 Marlowe Core
   - 2.1 Types
      - 2.1.1 Participants, roles and addresses
      - 2.1.2 Multi-Asset token.
      - 2.1.3 Accounts
      - 2.1.4 Choices
      - 2.1.5 Values and Observations
      - 2.1.6 Actions and inputs
      - 2.1.7 Contracts
      - 2.1.8 State and Environment
   - 2.2 Semantics
      - 2.2.1 Compute Transaction.
      - 2.2.2 Fix Interval
      - 2.2.3 Apply All Inputs
      - 2.2.4 Reduce Contract Until Quiescent
      - 2.2.5 Reduction Loop
      - 2.2.6 Reduce Contract Step
      - 2.2.7 Apply Input
      - 2.2.8 Apply Cases
      - 2.2.9 Utilities
      - 2.2.10 Evaluate Value
      - 2.2.11 Evaluate Observation.
- 3 Marlowe Guarantees
   - 3.1 Money Preservation.
   - 3.2 Contracts Always Close.
   - 3.3 Positive Accounts
   - 3.4 Quiescent Result
   - 3.5 Reducing a Contract until Quiescence Is Idempotent
   - 3.6 Split Transactions Into Single Input Does Not Affect the Result
      - 3.6.1 Termination Proof
      - 3.6.2 All Contracts Have a Maximum Time.
      - 3.6.3 Contract Does Not Hold Funds After it Closes
      - 3.6.4 Transaction Bound
- A Contract examples
   - A.1 Simple Swap.
      - A.1.1 Contract definition
      - A.1.2 Example execution
      - A.1.3 Contract guarantees
- B ByteString
   - B.1 Ordering
- C Code exports
- D Marlowe Core JSON
   - D.1 Party.
   - D.2 Token
   - D.3 Payee
   - D.4 ChoicesId
   - D.5 Bound
   - D.6 Values
   - D.7 Observation
   - D.8 Action
   - D.9 Case
   - D.10 Contract
   - D.11 Input.
   - D.12 Transaction
   - D.13 Payment
   - D.14 State
   - D.15 TransactionWarning.
- D.16 IntervalError
- D.17 TransactionError
- D.18 TransactionOutput
- D.19 Full Contract Example
- D.20 Parse utils


# Chapter 1


# Marlowe


### 1.1 Introduction


Marlowe is a special purpose or domain-specific language (DSL) that is de-
signed to be usable by someone who is expert in the field of financial con-
tracts, somewhat lessening the need for programming skills.

Marlowe is modelled on special-purpose financial contract languages popu-
larised in the last decade or so by academics and enterprises such as LexiFi

(^1) , which provides contract software in the financial sector. In developing
Marlowe, we have adapted these languages to work on any blockchain §1.4.
Where we differ from non-blockchain approaches is in how we make sure
that the contract is followed. In the smart contracts world there is a saying
“Code is law”, which implies that the assets deposited in a contract will
follow its logic, without the ability of a human to change the rules. This
applies for both the intended and not intended behaviour (in the form of
bugs or exploits).
To reduce the probability of not intended behaviour, the Marlowe DSL is
designed with simplicity in mind. Without loops, recursion, or other features
that general purposes smart-contract languages (E.g: Plutus, Solidity) have,
it is easier to make certain claims. Each Marlowe contract can be reasoned
with a static analizer to avoid common pitfalls such as trying to Pay more
money than the available. And the _executable semantics_ that dictates the
logic of **all** Marlowe contracts is formalized with the proof-assistant Isabelle.
Chapter §1 provides an overview of the Marlowe language. Chapter §2 defines
the Core language and semantics in detail. Chapter §3 presents proofs that
(^1) https://www.lexifi.com/


guarantee that Marlowe contracts possess properties desirable for financial
agreements.

### 1.2 The Marlowe Model.


Marlowe _Contract_ s describe a series of steps, typically by describing the first
step, together with another (sub-)contract that describes what to do next.
For example, the contract _Pay a p t v c_ says “make a payment of _v_ number
of tokens _t_ to the party _p_ from the account _a_ , and then follow the contract
_c_ ”. We call _c_ the continuation of the contract. All paths of the contract are
made explicit this way, and each _Contract_ term is executed at most once.

#### 1.2.1 Data types.


The _Value_ s and _Observation_ s § 2.1.5only works with integers and booleans
respectively. There are no custom data types, records, tuples, nor string ma-
nipulation. There are also no floating point numbers, so in order to represent
currencies it is recommended to work with cents. Dates are only used in the
context of _Timeout_ s and they are absolute, but it is likely we’ll add relative
times in a future version.

#### 1.2.2 Quiescent


The blockchain can’t force a participant to make a transaction. To avoid
having a participant blocking the execution of a contract, whenever an _Input_
is expected, there is a _Timeout_ with a contingency continuation. For each
step, we can know in advance how long it can last, and we can extend this to
know the maximum duration and the amount of transactions of a contract.

#### 1.2.3 Participants, accounts and state


Once we define a contract, we can see how many participants it will have.
The number of participants is fixed for the duration of the contract, but there
are mechanisms to trade participation §2.1.1.

Each participant has an internal account that allows the contract to define
default owner for assets §2.1.3. Whenever a _Party_ deposits an asset in the
contract, they need to decide the default owner of that asset. Payments can
be made to transfer the default owner or to take the asset out of the contract.


If the contract is closed, the default owner can redeem the assets available in
their internal accounts.

The accounts, choices, and variables stored in the _State_ §2.1.8are global to
that contract.

#### 1.2.4 Core and Extended


The set of types and functions that conform the semantics executed in the
blockchain is called _Marlowe Core_ , and it’s formalized in chapter §2. To
improve usability, there is another set of types and functions that compile to
core, and it is called _Marlowe Extended_.

In the first version of the extended language, the only modification to the
DSL is the addition of template parameters. These allows an initial form
of contract reutilization, allowing to instantiate the same contract with dif-
ferent _Value_ s and _Timeout_ s. For the moment, the extended language is not
formalized in this specification but it will be added in the future

### 1.3 Specification generation and nomenclature


The Marlowe specification is formalized using the proof assistant Isabelle^2.
The code is written in a literate programming style and this document is
generated from the proofs. This improves code documentation and lowers
the probability of stale information.

As a drawback, the code/doc organization is more rigid. Isabelle require us to
define code in a bottom-up approach, having to define first the dependencies
and later the most complex structures.

The notation is closer to a Mathematical formula than a functional program-
ming language. There are some configurations in the _SpecificationLatexSugar_
theory file that makes the output be closer to code.

### 1.4 Blockchain agnostic


Marlowe is currently implemented on the Cardano Blockchain, but it is de-
signed to be Blockchain agnostic.

(^2) https://isabelle.in.tum.de/


Programs written in languages like Java and Python can be run on differ-
ent architectures, like amd64 or arm64, because they have interpreters and
runtimes for them. In the same way, the Marlowe interpreter could be im-
plemented to run on other blockchains, like Ethereum, Solana for example.

We make the following assumptions on the underlying Blockchain that allow
Marlowe Semantics to serve as a common abstraction:

In order to define the different _Token_ s that are used as currency in the
participants accounts §2.1.3, deposits, and payments, we need to be able to
express a _TokenName_ and _CurrencySymbol_.

**type-synonym** _TokenName_ = _ByteString_
**type-synonym** _CurrencySymbol_ = _ByteString_

To define a fixed participant in the contract §2.1.1and to make payouts to
them, we need to express an _Address_.

**type-synonym** _Address_ = _ByteString_

In the context of this specification, these _ByteString_ types are opaque, and we
don’t enforce a particular encoding or format, only that they can be sorted
§B.

The _Timeout_ s that prevent us from waiting forever for external _Input_ s are
represented by the number of milliseconds from the Unix Epoch^3.

**type-synonym** _POSIXTime_ = _int_

**type-synonym** _Timeout_ = _POSIXTime_

The _TimeInterval_ that defines the validity of a transaction is a tuple of
exclusive start and end time.

**type-synonym** _TimeInterval_ = _POSIXTime_ × _POSIXTime_

(^3) January 1st, 1970 at 00:00:00 UTC


# Chapter 2


# Marlowe Core


### 2.1 Types


This section introduces the data types of _Marlowe Core_ , which are composed
by the Marlowe DSL and also the types required to compute a _Transaction_.

Because of the literate programming nature of Isabelle §1.3, the types are
defined bottom-up. To follow just the DSL, a reader can start by looking at
a _Contract_ definition §2.1.7.

#### 2.1.1 Participants, roles and addresses


We should separate the notions of participant, role, and address in a Marlowe
contract. A participant (or _Party_ ) in the contract can be represented by
either a fixed _Address_ or a _Role_.

**type-synonym** _RoleName_ = _ByteString_

**datatype** _Party_ =
_Address Address_
| _Role RoleName_

An address party is defined by a Blockhain specific _Address_ §1.4 and it cannot
be traded (it is fixed for the lifetime of a contract).

A _Role_ , on the other hand, allows the participation of the contract to be
dynamic. Any user that can prove to have permission to act as _RoleName_
is able to carry out the actions assigned §2.1.6, and redeem the payments
issued to that role. The roles could be implemented as tokens^1 that can be

(^1) In the Cardano implementation roles are represented by native tokens and they are


traded. By minting multiple tokens for a particular role, several people can
be given permission to act on behalf of that role simultaneously, this allows
for more complex use cases.

#### 2.1.2 Multi-Asset token.


Inspired by Cardano’s Multi-Asset tokens^2 , Marlowe also supports to trans-
act with different assets. A _Token_ consists of a _CurrencySymbol_ that repre-
sents the monetary policy of the _Token_ and a _TokenName_ which allows to
have multiple tokens with the same monetary policy.

**datatype** _Token_ = _Token CurrencySymbol TokenName_

The Marlowe semantics treats both types as opaque _ByteString_.

#### 2.1.3 Accounts


The Marlowe model allows for a contract to store assets. All participants of
the contract implicitly own an account identified with an _AccountId_.

**type-synonym** _AccountId_ = _Party_

All assets stored in the contract must be in an internal account for one of
the parties; this way, when the contract is closed, all remaining assets can
be redeemed by their respective owners. These accounts are local: they only
exist (and are accessible) within the contract.

**type-synonym** _Accounts_ =(( _AccountId_ × _Token_ )× _int_ ) _list_

During its execution, the contract can invite parties to deposit assets into an
internal account through the construct “ _When_ [ _Deposit accountId party token
value_ ] _timeout continuation_ ". The contract can transfer assets internally
(between accounts) or externally (from an account to a party) by using the
term " _Pay accountId payee token value continuation_ ”, where _Payee_ is:

**datatype** _Payee_ = _Account AccountId_
| _Party Party_

A _Pay_ always takes money from an internal _AccountId_ , and the _Payee_ defines
if we transfer internally ( _Account p_ ) or externally ( _Party p_ )

distributed to addresses at the time a contract is deployed to the blockchain

(^2) https://docs.cardano.org/native-tokens/learn


#### 2.1.4 Choices


Choices – of integers – are identified by _ChoiceId_ which is defined with a
canonical name and the _Party_ who had made the choice:

**type-synonym** _ChoiceName_ = _ByteString_
**datatype** _ChoiceId_ = _ChoiceId ChoiceName Party_

Choices are _Bound_ ed. As an argument for the _Choice_ action §2.1.6, we pass
a list of _Bound_ s that limit the integer that we can choose. The _Bound_ data
type is a tuple of integers that represents an **inclusive** lower and upper
bound.

**datatype** _Bound_ = _Bound int int_

#### 2.1.5 Values and Observations


We can store a _Value_ in the Marlowe State §2.1.8using the _Let_ construct
§2.1.7, and we use a _ValueId_ to referrence it

**datatype** _ValueId_ = _ValueId ByteString_

_Value_ s and _Observation_ s are language terms that interact with most of the
other constructs. _Value_ evaluates to an integer and _Observation_ evaluates to
a boolean using _evalValue_ §2.2.10and _evalObservation_ §2.2.11respectively.

They are defined in a mutually recursive way as follows:

**datatype** _Value_ = _AvailableMoney AccountId Token_
| _Constant int_
| _NegValue Value_
| _AddValue Value Value_
| _SubValue Value Value_
| _MulValue Value Value_
| _DivValue Value Value_
| _ChoiceValue ChoiceId_
| _TimeIntervalStart_
| _TimeIntervalEnd_
| _UseValue ValueId_
| _Cond Observation Value Value_
**and** _Observation_ = _AndObs Observation Observation_
| _OrObs Observation Observation_
| _NotObs Observation_
| _ChoseSomething ChoiceId_
| _ValueGE Value Value_
| _ValueGT Value Value_


```
| ValueLT Value Value
| ValueLE Value Value
| ValueEQ Value Value
| TrueObs
| FalseObs
```
Three of the _Value_ terms look up information in the Marlowe state: _Avail-
ableMoney p t_ reports the amount of token _t_ in the internal account of party
_p_ ; _ChoiceValue i_ reports the most recent value chosen for choice _i_ , or zero if
no such choice has been made; and _UseValue i_ reports the most recent value
of the variable _i_ , or zero if that variable has not yet been set to a value.

_Constant v_ evaluates to the integer _v_ , while _NegValue x_ , _AddValue x y_ , _Sub-
Value x y_ , _MulValue x y_ , and _DivValue x y_ provide the common arithmetic
operations− _x_ , _x_ + _y_ , _x_ − _y_ , _x_ ∗ _y_ , and _x_ / _y_ , where division always rounds
(truncates) its result towards zero.

_Cond b x y_ represents a condition expression that evaluates to _x_ if _b_ is true
and to _y_ otherwise.

The last _Value_ s, _TimeIntervalStart_ and _TimeIntervalEnd_ , evaluate respec-
tively to the start or end of the validity interval for the Marlowe transaction.

For the observations, the _ChoseSomething i_ term reports whether a choice _i_
has been made thus far in the contract.

The terms _TrueObs_ and _FalseObs_ provide the logical constants _true_ and _false_.
The logical operators¬ _x_ , _x_ ∧ _y_ , and _x_ ∨ _y_ are represented by the terms
_NotObs x_ , _AndObs x y_ , and _OrObs x y_ , respectively.

Value comparisons _x_ < _y_ , _x_ ≤ _y_ , _x_ > _y_ , _x_ ≥ _y_ , and _x_ = _y_ are represented
by _ValueLT x y_ , _ValueLE x y_ , _ValueGT x y_ , _ValueGE x y_ , and _ValueEQ x y_.

#### 2.1.6 Actions and inputs


_Action_ s and _Input_ s are closely related. An _Action_ can be added in a list of
_Case_ s § 2.1.7as a way to declare the possible external _Input_ s a _Party_ can
include in a _Transaction_ at a certain time.

The different types of actions are:

**datatype** _Action_ = _Deposit AccountId Party Token Value_
| _Choice ChoiceId Bound list_
| _Notify Observation_


A _Deposit a p t v_ makes a deposit of # _v Token_ s _t_ from _Party p_ into account
_a_.

A choice _Choice i bs_ is made for a particular choice identified by the ChoiceId
§2.1.4 _i_ with a list of inclusive bounds _bs_ on the values that are acceptable.
For example,[ _Bound 0 0_ , _Bound 3 5_ ]offers the choice of one of 0, 3, 4 and
5.

A notification can be triggered by anyone as long as the _Observation_ evaluates
to _true_. If multiple _Notify_ are present in the _Case_ list, the first one with a
_true_ observation is matched.

For each _Action_ , there is a corresponding _Input_ that can be included inside
a _Transaction_

**type-synonym** _ChosenNum_ = _int_

**datatype** _Input_ = _IDeposit AccountId Party Token int_
| _IChoice ChoiceId ChosenNum_
| _INotify_

The differences between them are:

- _Deposit_ uses a _Value_ while _IDeposit_ has the _int_ it was evaluated to
    with _evalValue_ §2.2.10.
- _Choice_ defines a list of valid _Bound_ s while _IChoice_ has the actual _Cho-_
    _senNum_.
- _Notify_ has an _Observation_ while _INotify_ does not have arguments, the
    _Observation_ must evaluate to true inside the _Transaction_

#### 2.1.7 Contracts


Marlowe is a continuation-based language, this means that a _Contract_ can
either be a _Close_ or another construct that recursively has a _Contract_. Even-
tually, **all** contracts end up with a _Close_ construct.

_Case_ and _Contract_ are defined in a mutually recursive way as follows:

**datatype** _Case_ = _Case Action Contract_
**and** _Contract_ = _Close_
| _Pay AccountId Payee Token Value Contract_
| _If Observation Contract Contract_


```
| When Case list Timeout Contract
| Let ValueId Value Contract
| Assert Observation Contract
```
_Close_ is the simplest contract, when we evaluate it, the execution is completed
and we generate _Payment_ s § **??** for the assets in the internal accounts to their
default owners^3.

The contract _Pay a p t v c_ , generates a _Payment_ from the internal account _a_
to a payee §2.1.3 _p_ of # _v Token_ s and then continues to contract _c_. Warnings
will be generated if the value _v_ is not positive, or if there is not enough in the
account to make the payment in full. In the latter case, a partial payment
(of the available amount) is made

The contract _If obs x y_ allows branching. We continue to branch _x_ if the
_Observation obs_ evaluates to _true_ , or to branch _y_ otherwise.

_When_ is the most complex constructor for contracts, with the form _When cs
t c_. The list _cs_ contains zero or more pairs of _Action_ s and _Contract_ continu-
ations. When we do a _computeTransaction_ §2.2.1, we follow the continuation
associated to the first _Action_ that matches the _Input_. If no action is matched
it returns a _ApplyAllNoMatchError_. If a valid _Transaction_ is computed with
a _TimeInterval_ with a start time bigger than the _Timeout t_ , the contingency
continuation _c_ is evaluated. The explicit timeout mechanism is what allows
Marlowe to avoid waiting forever for external inputs.

A _Let_ contract _Let i v c_ allows a contract to record a value using an identifier
_i_. In this case, the expression _v_ is evaluated, and the result is stored with
the name _i_. The contract then continues as _c_. As well as allowing us to
use abbreviations, this mechanism also means that we can capture and save
volatile values that might be changing with time, e.g. the current price of oil,
or the current time, at a particular point in the execution of the contract, to
be used later on in contract execution.

An assertion contract _Assert b c_ does not have any effect on the state of
the contract, it immediately continues as _c_ , but it issues a warning if the
observation _b_ evaluates to false. It can be used to ensure that a property
holds in a given point of the contract, since static analysis will fail if any
execution causes a warning. The _Assert_ term might be removed from future
on-chain versions of Marlowe.

(^3) Even if the payments are generated one at a time (per account and per Token), the
cardano implementation generates a single transaction


#### 2.1.8 State and Environment


The internal state of a Marlowe contract consists of the current balances in
each party’s account, a record of the most recent value of each type of choice,
a record of the most recent value of each variable, and the lower bound for the
current time that is used to refine time intervals and ensure _TimeIntervalStart_
never decreases. The data for accounts, choices, and bound values are stored
as association lists.

**record** _State_ = _accounts_ :: _Accounts
choices_ ::( _ChoiceId_ × _ChosenNum_ ) _list
boundValues_ ::( _ValueId_ × _int_ ) _list
minTime_ :: _POSIXTime_

The execution environment of a Marlowe contract simply consists of the
(inclusive) time interval within which the transaction is occurring.

**record** _Environment_ = _timeInterval_ :: _TimeInterval_

— TODO: see if we want to add data types of Semantic here (Transaction, etc)
or if we want to move this types to Semantic

**datatype** _IntervalError_ = _InvalidInterval TimeInterval_
| _IntervalInPastError POSIXTime TimeInterval_

**datatype** _IntervalResult_ = _IntervalTrimmed Environment State_
| _IntervalError IntervalError_

### 2.2 Semantics


Marlowe’s behavior is defined via the _operational semantics_ (or _executable
semantics_ ) of the Isabelle implementation of its _computeTransaction_ func-
tion. That function calls several auxiliary functions to apply inputs and find
a quiescent state of the contract. These, in turn, call evaluators for _Value_
and _Observation_.


#### 2.2.1 Compute Transaction.


The entry point into Marlowe semantics is the function _computeTransaction_
that applies input to a prior state to transition to a posterior state, perhaps
reporting warnings or throwing an error, all in the context of an environment
for the transaction.

_computeTransaction_ :: _Transaction_ ⇒ _State_ ⇒ _Contract_ ⇒ _TransactionOut-
put_

FIXME: Print record: _Transaction_

**datatype** _TransactionOutput_ =
_TransactionOutput
TransactionOutputRecord_
| _TransactionError TransactionError_

FIXME: Print record: _TransactionOutputRecord_

This function adjusts the time interval for the transaction using _fixInter-
val_ and then applies all of the transaction inputs to the contract using _ap-
plyAllInputs_. It reports relevant warnings and throws relevant errors.

computeTransaction ::
Transaction_ext () -> State_ext () -> Contract -> TransactionOutput;
computeTransaction tx state contract =
let {
inps = inputs tx;
} in (case fixInterval (interval tx) state of {
IntervalTrimmed env fixSta ->
(case applyAllInputs env fixSta contract inps of {
ApplyAllSuccess reduced warnings payments newState cont
->
(if not reduced &&
(not (equal_Contract contract Close) ||
null (accounts state))
then TransactionError TEUselessTransaction
else TransactionOutput
(TransactionOutputRecord_ext warnings payments
newState
cont ()));
ApplyAllNoMatchError -> TransactionError TEApplyNoMatchError;
ApplyAllAmbiguousTimeIntervalError ->


```
TransactionError TEAmbiguousTimeIntervalError;
});
IntervalError errora -> TransactionError (TEIntervalError errora);
});
```
#### 2.2.2 Fix Interval


The _fixInterval_ functions combines the minimum-time constraint of _State_
with the time interval of _Environment_ to yield a “trimmed” validity interval
and a minimum time for the new state that will result from applying the
transaction. It throws an error if the interval is nonsensical or in the past.

FIXME: print type synonym: _IntervalResult_

fixInterval :: (Int, Int) -> State_ext () -> IntervalResult;
fixInterval (low, high) state =
let {
curMinTime = minTime state;
newLow = max low curMinTime;
curInterval = (newLow, high);
env = Environment_ext curInterval ();
newState = minTime_update (\ _ -> newLow) state;
} in (if less_int high low then IntervalError (InvalidInterval (low,
high))
else (if less_int high curMinTime
then IntervalError (IntervalInPastError curMinTime (low,
high))
else IntervalTrimmed env newState));

#### 2.2.3 Apply All Inputs


The _applyAllInputs_ function iteratively progresses the contract and applies
the transaction inputs to the state, checking for errors along the way and con-
tinuing until all the inputs are consumed and the contract reaches a quiescent
state.

applyAllInputs ::
Environment_ext () -> State_ext () -> Contract -> [Input] -> ApplyAllResult;
applyAllInputs env state contract inputs =
applyAllLoop False env state contract inputs [] [];


applyAllLoop ::
Bool ->
Environment_ext () ->
State_ext () ->
Contract ->
[Input] -> [TransactionWarning] -> [Payment] -> ApplyAllResult;
applyAllLoop contractChanged env state contract inputs warnings payments
=
(case reduceContractUntilQuiescent env state contract of {
ContractQuiescent reduced reduceWarns pays curState cont ->
(case inputs of {
[] -> ApplyAllSuccess (contractChanged || reduced)
(warnings ++ convertReduceWarnings reduceWarns)
(payments ++ pays) curState cont;
input : rest ->
(case applyInput env curState input cont of {
Applied applyWarn newState conta ->
applyAllLoop True env newState conta rest
(warnings ++
convertReduceWarnings reduceWarns ++
convertApplyWarning applyWarn)
(payments ++ pays);
ApplyNoMatchError -> ApplyAllNoMatchError;
});
});
RRAmbiguousTimeIntervalError -> ApplyAllAmbiguousTimeIntervalError;
});

#### 2.2.4 Reduce Contract Until Quiescent


The _reduceContractUntilQuiescent_ executes as many non-input steps of the
contract as is possible. Marlowe semantics do not allow partial execution of
a series of non-input steps.

reduceContractUntilQuiescent ::
Environment_ext () -> State_ext () -> Contract -> ReduceResult;
reduceContractUntilQuiescent env state contract =
reductionLoop False env state contract [] [];


#### 2.2.5 Reduction Loop


The _reductionLoop_ function attempts to apply the next, non-input step to
the contract. It emits warnings along the way and it will through an error if
it encounters an ambiguous time interval.

reductionLoop ::
Bool ->
Environment_ext () ->
State_ext () -> Contract -> [ReduceWarning] -> [Payment] -> ReduceResult;
reductionLoop reduced env state contract warnings payments =
(case reduceContractStep env state contract of {
Reduced warning effect newState ncontract ->
let {
newWarnings =
(if equal_ReduceWarning warning ReduceNoWarning then warnings
else warning : warnings);
a = (case effect of {
ReduceNoPayment -> payments;
ReduceWithPayment payment -> payment : payments;
});
} in reductionLoop True env newState ncontract newWarnings a;
NotReduced ->
ContractQuiescent reduced (reverse warnings) (reverse payments)
state
contract;
AmbiguousTimeIntervalReductionError -> RRAmbiguousTimeIntervalError;
});

#### 2.2.6 Reduce Contract Step


The _reduceContractStep_ function handles the progression of the _Contract_
in the absence of inputs: it performs the relevant action (payments, state-
change, etc.), reports warnings, and throws errors if needed. It stops reducing
the contract at the point when the contract requires external input.

Note that this function should report an implicit payment of zero (due to
lack of funds) as a partial payment of zero, not as a non-positive payment.
An explicit payment of zero (due to the contract actually specifying a zero
payment) should be reported as a non-positive payment.


reduceContractStep ::
Environment_ext () -> State_ext () -> Contract -> ReduceStepResult;
reduceContractStep uu state Close =
(case refundOne (accounts state) of {
Nothing -> NotReduced;
Just ((party, (token, money)), newAccount) ->
let {
newState = accounts_update (\ _ -> newAccount) state;
} in Reduced ReduceNoWarning
(ReduceWithPayment (Payment party (Party party) token money))
newState Close;
});
reduceContractStep env state (Pay accId payee token val cont) =
let {
moneyToPay = evalValue env state val;
} in (if less_eq_int moneyToPay Zero_int
then let {
warning = ReduceNonPositivePay accId payee token moneyToPay;
} in Reduced warning ReduceNoPayment state cont
else let {
balance = moneyInAccount accId token (accounts state);
paidMoney = min balance moneyToPay;
newBalance = minus_int balance paidMoney;
newAccs =
updateMoneyInAccount accId token newBalance (accounts
state);
warning =
(if less_int paidMoney moneyToPay
then ReducePartialPay accId payee token paidMoney
moneyToPay
else ReduceNoWarning);
} in (case giveMoney accId payee token paidMoney newAccs
of {
(payment, finalAccs) ->
Reduced warning payment
(accounts_update (\ _ -> finalAccs) state) cont;
}));
reduceContractStep env state (If obs cont1 cont2) =
let {
a = (if evalObservation env state obs then cont1 else cont2);
} in Reduced ReduceNoWarning ReduceNoPayment state a;
reduceContractStep env state (When uv timeout cont) =
(case timeInterval env of {


(startTime, endTime) ->
(if less_int endTime timeout then NotReduced
else (if less_eq_int timeout startTime
then Reduced ReduceNoWarning ReduceNoPayment state cont
else AmbiguousTimeIntervalReductionError));
});
reduceContractStep env state (Let valId val cont) =
let {
evaluatedValue = evalValue env state val;
boundVals = boundValues state;
newState =
boundValues_update (\ _ -> insert valId evaluatedValue boundVals)
state;
warn = (case lookup valId boundVals of {
Nothing -> ReduceNoWarning;
Just oldVal -> ReduceShadowing valId oldVal evaluatedValue;
});
} in Reduced warn ReduceNoPayment newState cont;
reduceContractStep env state (Assert obs cont) =
let {
warning =
(if evalObservation env state obs then ReduceNoWarning
else ReduceAssertionFailed);
} in Reduced warning ReduceNoPayment state cont;

#### 2.2.7 Apply Input


The _applyInput_ function attempts to apply the next input to each _Case_ in
the _When_ , in sequence.

applyInput ::
Environment_ext () -> State_ext () -> Input -> Contract -> ApplyResult;
applyInput env state input (When cases t cont) =
applyCases env state input cases;
applyInput env state input Close = ApplyNoMatchError;
applyInput env state input (Pay v va vb vc vd) = ApplyNoMatchError;
applyInput env state input (If v va vb) = ApplyNoMatchError;
applyInput env state input (Let v va vb) = ApplyNoMatchError;
applyInput env state input (Assert v va) = ApplyNoMatchError;


#### 2.2.8 Apply Cases


The _applyCases_ function attempts to match an _Input_ to an _Action_ , compute
the new contract state, emit warnings, throw errors if needed, and determine
the appropriate continuation of the contract.

applyCases ::
Environment_ext () -> State_ext () -> Input -> [Case] -> ApplyResult;
applyCases env state (IDeposit accId1 party1 tok1 amount)
(Case (Deposit accId2 party2 tok2 val) cont : rest) =
(if equal_Party accId1 accId2 &&
equal_Party party1 party2 &&
equal_Token tok1 tok2 && equal_int amount (evalValue env state
val)
then let {
warning =
(if less_int Zero_int amount then ApplyNoWarning
else ApplyNonPositiveDeposit party1 accId2 tok2 amount);
newState =
accounts_update
(\ _ -> addMoneyToAccount accId1 tok1 amount (accounts
state))
state;
} in Applied warning newState cont
else applyCases env state (IDeposit accId1 party1 tok1 amount) rest);
applyCases env state (IChoice choId1 choice)
(Case (Choice choId2 bounds) cont : rest) =
(if equal_ChoiceId choId1 choId2 && inBounds choice bounds
then let {
newState =
choices_update (\ _ -> insert choId1 choice (choices state))
state;
} in Applied ApplyNoWarning newState cont
else applyCases env state (IChoice choId1 choice) rest);
applyCases env state INotify (Case (Notify obs) cont : rest) =
(if evalObservation env state obs then Applied ApplyNoWarning state
cont
else applyCases env state INotify rest);
applyCases env state (IDeposit accId1 party1 tok1 amount)
(Case (Choice vb vc) va : rest) =
applyCases env state (IDeposit accId1 party1 tok1 amount) rest;
applyCases env state (IDeposit accId1 party1 tok1 amount)
(Case (Notify vb) va : rest) =


applyCases env state (IDeposit accId1 party1 tok1 amount) rest;
applyCases env state (IChoice choId1 choice)
(Case (Deposit vb vc vd ve) va : rest) =
applyCases env state (IChoice choId1 choice) rest;
applyCases env state (IChoice choId1 choice) (Case (Notify vb) va : rest)
=
applyCases env state (IChoice choId1 choice) rest;
applyCases env state INotify (Case (Deposit vb vc vd ve) va : rest) =
applyCases env state INotify rest;
applyCases env state INotify (Case (Choice vb vc) va : rest) =
applyCases env state INotify rest;
applyCases env state acc [] = ApplyNoMatchError;

#### 2.2.9 Utilities


The _moneyInAccount_ , _updateMoneyInAccount_ , and _addMoneyToAccount_ func-
tions read, write, and increment the funds in a particular account of the _State_ ,
respectively. The _giveMoney_ function transfer funds internally between ac-
counts. The _refundOne_ function finds the first account with funds in it.

moneyInAccount :: Party -> Token -> [((Party, Token), Int)] -> Int;
moneyInAccount accId token accountsV =
findWithDefault Zero_int (accId, token) accountsV;

updateMoneyInAccount ::
Party -> Token -> Int -> [((Party, Token), Int)] -> [((Party, Token),
Int)];
updateMoneyInAccount accId token money accountsV =
(if less_eq_int money Zero_int then delete (accId, token) accountsV
else insert (accId, token) money accountsV);

addMoneyToAccount ::
Party -> Token -> Int -> [((Party, Token), Int)] -> [((Party, Token),
Int)];
addMoneyToAccount accId token money accountsV =
let {
balance = moneyInAccount accId token accountsV;
newBalance = plus_int balance money;
} in (if less_eq_int money Zero_int then accountsV
else updateMoneyInAccount accId token newBalance accountsV);


giveMoney ::
Party ->
Payee ->
Token ->
Int ->
[((Party, Token), Int)] -> (ReduceEffect, [((Party, Token),
Int)]);
giveMoney accountId payee token money accountsV =
let {
a = (case payee of {
Account accId -> addMoneyToAccount accId token money accountsV;
Party _ -> accountsV;
});
} in (ReduceWithPayment (Payment accountId payee token money), a);

refundOne ::
[((Party, Token), Int)] ->
Maybe ((Party, (Token, Int)), [((Party, Token), Int)]);
refundOne (((accId, tok), money) : rest) =
(if less_int Zero_int money then Just ((accId, (tok, money)), rest)
else refundOne rest);
refundOne [] = Nothing;

#### 2.2.10 Evaluate Value


Given the _Environment_ and the current _State_ , the _evalValue_ function evalu-
ates a _Value_ into a number

_evalValue_ :: _Environment_ ⇒ _State_ ⇒ _Value_ ⇒ _int_

**Available Money**

For the _AvailableMoney_ case, _evalValue_ will give us the amount of _Token_ s
that a _Party_ has in their internal account.

_evalValue env state_ ( _AvailableMoney accId token_ ) = _findWithDefault 0_ ( _accId_ ,
_token_ ) ( _accounts state_ )


**Constant**

For the _Constant_ case, _evalValue_ will always evaluate to the same value

_evalValue env state_ ( _Constant integer_ ) = _integer_

**Addition**

For the _AddValue_ case, _evalValue_ will evaluate both sides and add them
together.

_evalValue env state_ ( _AddValue lhs rhs_ ) = _evalValue env state lhs_ + _evalValue
env state rhs_

Addition is associative and commutative:

_evalValue env sta_ ( _AddValue x_ ( _AddValue y z_ )) = _evalValue env sta_ ( _AddValue_
( _AddValue x y_ ) _z_ )

_evalValue env sta_ ( _AddValue x y_ ) = _evalValue env sta_ ( _AddValue y x_ )

**Subtraction**

For the _SubValue_ case, _evalValue_ will evaluate both sides and subtract the
second value from the first.

_evalValue env state_ ( _SubValue lhs rhs_ ) = _evalValue env state lhs_ − _evalValue
env state rhs_

**Negation**

For every value _x_ there is the complement _NegValue x_ so that

_evalValue env sta_ ( _AddValue x_ ( _NegValue x_ )) = _0_

**Multiplication**

For the _MulValue_ case, _evalValue_ will evaluate both sides and multiply them.

_evalValue env state_ ( _MulValue lhs rhs_ ) = _evalValue env state lhs_ ∗ _evalValue
env state rhs_


**Division**

Division is a special case because we only evaluate to natural numbers:

- If the denominator is 0, the result is also 0. Other languages uses NaN
    or Infinity to represent this case
- The result will be rounded towards zero.

_evalValue env state_ ( _DivValue lhs rhs_ )=
( _letn_ = _evalValue env state lhs_ ;
_d_ = _evalValue env state rhs
in ifd_ = _0 then 0 elsen quot d_ )

TODO: lemmas around division? maybe extend the following to proof eval-
Value and not just div

_c_ 6 = _0_ =⇒ _c_ ∗ _a div_ ( _c_ ∗ _b_ ) = _a div b_

_c_ 6 = _0_ =⇒ | _c__a_ | _div_ | _c__b_ |=| _a_ | _div_ | _b_ |

COMMENT(BWB): I suggest that the lemmas be (i) exact multiples di-
vide with no remainder, (ii) the remainder equals the excess above an exact
multiple, and (iii) negation commutues with division.

**Choice Value**

For the _ChoiceValue_ case, _evalValue_ will look in its state if a _Party_ has made
a choice for the _ChoiceName_. It will default to zero if it doesn’t find it.

_evalValue env state_ ( _ChoiceValue choId_ ) = _findWithDefault 0 choId_ ( _choices
state_ )

**Time Interval Start**

All transactions are executed in the context of a valid time interval. For the
_TimeIntervalStart_ case, _evalValue_ will return the beginning of that interval.

_evalValue env state TimeIntervalStart_ = _fst_ ( _timeInterval env_ )


**Time Interval End**

All transactions are executed in the context of a valid time interval. For the
_TimeIntervalEnd_ case, _evalValue_ will return the end of that interval.

_evalValue env state TimeIntervalEnd_ = _snd_ ( _timeInterval env_ )

**Use Value**

For the _TimeIntervalEnd_ case, _evalValue_ will look in its state for a bound
_ValueId_. It will default to zero if it doesn’t find it.

_evalValue env state_ ( _UseValue valId_ ) = _findWithDefault 0 valId_ ( _boundValues
state_ )

**Conditional Value**

For the _Cond_ case, _evalValue_ will first call _evalObservation_ on the condition,
and it will evaluate the the true or false value depending on the result.

_evalValue env state_ ( _Cond cond thn els_ ) = ( _ifevalObservation env state cond
thenevalValue env state thnelseevalValue env state els_ )

#### 2.2.11 Evaluate Observation.


Given the _Environment_ and the current _State_ , the _evalObservation_ function
evaluates an _Observation_ into a number

_evalObservation_ :: _Environment_ ⇒ _State_ ⇒ _Observation_ ⇒ _bool_

**True and False**

The logical constants _true_ and _false_ are trivially evaluated.

_evalObservation env state TrueObs_ = _True_

_evalObservation env state FalseObs_ = _False_

**Not, And, Or**

The standard logical operators¬,∧, and∨are evaluated in a straightforward
manner.


_evalObservation env state_ ( _NotObs subObs_ ) = (¬ _evalObservation env state
subObs_ )

_evalObservation env state_ ( _AndObs lhs rhs_ ) = ( _evalObservation env state lhs_
∧ _evalObservation env state rhs_ )

_evalObservation env state_ ( _OrObs lhs rhs_ ) = ( _evalObservation env state lhs_
∨ _evalObservation env state rhs_ )

**Comparison of Values**

Five functions are provided for the comparison (equality and ordering of
integer values) have traditional evaluations:=,<,≤,>, and≥.

_evalObservation env state_ ( _ValueEQ lhs rhs_ ) = ( _evalValue env state lhs_ =
_evalValue env state rhs_ )

_evalObservation env state_ ( _ValueLT lhs rhs_ ) = ( _evalValue env state lhs_ <
_evalValue env state rhs_ )

_evalObservation env state_ ( _ValueLE lhs rhs_ ) = ( _evalValue env state lhs_ ≤
_evalValue env state rhs_ )

_evalObservation env state_ ( _ValueGT lhs rhs_ ) = ( _evalValue env state rhs_ <
_evalValue env state lhs_ )

_evalObservation env state_ ( _ValueGE lhs rhs_ ) = ( _evalValue env state rhs_ ≤
_evalValue env state lhs_ )

**Chose Something**

The _ChoseSometing i_ term evaluates to true if the a choice _i_ was previously
made in the history of the contract.

_evalObservation env state_ ( _ChoseSomething choId_ ) = _member choId_ ( _choices
state_ )


# Chapter 3


# Marlowe Guarantees


We can also use proof assistants to demonstrate that the Marlowe semantics
presents certain desirable properties, such as that money is preserved and
anything unspent is returned to users by the end of the execution of any
contract.

**Auxillary Functions**

Many of the proofs in this chapter rely on function _playTrace_ and _play-
TraceAux_ that execute a sequence of transactions using the Marlowe seman-
tics defined in _computeTransaction_. They also rely on starting from a valid
and positive contract state, _validAndPositive-state_ and a function _maxTime-
Contract_ that extracts the latest timeout from the contract.

_playTrace_ :: _int_ ⇒ _Contract_ ⇒ _Transaction list_ ⇒ _TransactionOutput_

_playTraceAux_ :: _TransactionOutputRecord_ ⇒ _Transaction list_ ⇒ _Transac-
tionOutput_

_validAndPositive-state_ :: _State_ ⇒ _bool_

_maxTimeContract_ :: _Contract_ ⇒ _int_

### 3.1 Money Preservation.


One of the dangers of using smart contracts is that a badly written one can
potentially lock its funds forever. By the end of the contract, all the money
paid to the contract must be distributed back, in some way, to a subset of
the participants of the contract. To ensure this is the case we proved two
properties: “Money Preservation” and “Contracts Always Close”.


Regarding money preservation, money is not created or destroyed by the
semantics. More specifically, the money that comes in plus the money in the
contract before the transaction must be equal to the money that comes out
plus the contract after the transaction, except in the case of an error.

_moneyInTransactions tra_ = _moneyInPlayTraceResult tra_ ( _playTrace sl con-
tract tra_ )

where _moneyInTransactions_ and _moneyInPlayTraceResult_ measure the funds
in the transactions applied to a contract versus the funds in the contract state
and the payments that it has made while executing.

### 3.2 Contracts Always Close.


For every Marlowe Contract there is a time after which an empty transaction
can be issued that will close the contract and refund all the money in its
accounts.

FIXME: This theorem doesn’t actually prove the narrative. Are we missing
a theorem?

[[ _validAndPositive-state sta_ ; _accounts sta_ 6 = []∨ _cont_ 6 = _Close_ ]] =⇒ ∃ _inp_.
_isClosedAndEmpty_ ( _computeTransaction inp sta cont_ )

### 3.3 Positive Accounts


There are some values for State that are allowed by its type but make no
sense, especially in the case of Isabelle semantics where we use lists instead
of maps:

1. The lists represent maps, so they should have no repeated keys.
2. We want two maps that are equal to be represented the same, so we
    force keys to be in ascending order.
3. We only want to record those accounts that contain a positive amount.

We call a value for State valid if the first two properties are true. And we
say it has positive accounts if the third property is true.

FIXME: Address the review comment "Is this a note for us or the explanation
to the user of what _playTraceAux-preserves-validAndPositive-state_ proves?".


[[ _validAndPositive-state_ ( _txOutState txIn_ ); _playTraceAux txIn transList_ = _Trans-
actionOutput txOut_ ]] =⇒ _validAndPositive-state_ ( _txOutState txOut_ )

### 3.4 Quiescent Result


A contract is quiescent if and only if the root construct is _When_ , or if the
contract is _Close_ and all accounts are empty. If an input _State_ is valid and
accounts are positive, then the output will be quiescent, _isQuiescent_.

The following always produce quiescent contracts:

- reductionLoop §2.2.5
- reduceContractUntilQuiescent §2.2.4
- applyAllInputs §2.2.3
- computeTransaction §2.2.1
- playTrace § 3

_playTrace sl cont_ ( _h_ : _t_ ) = _TransactionOutput traOut_ =⇒ _isQuiescent_ ( _txOutContract
traOut_ ) ( _txOutState traOut_ )

### 3.5 Reducing a Contract until Quiescence Is Idempotent


Once a contract is quiescent, further reduction will not change the contract
or state, and it will not produce any payments or warnings.

_reduceContractUntilQuiescent env state contract_ = _ContractQuiescent re-
ducedAfter wa pa nsta ncont_ =⇒ _reduceContractUntilQuiescent env nsta
ncont_ = _ContractQuiescent False_ [] [] _nsta ncont_

### 3.6 Split Transactions Into Single Input Does Not Affect the Result


Applying a list of inputs to a contract produces the same result as applying
each input singly.

_playTraceAux acc tral_ = _playTraceAux acc_ ( _traceListToSingleInput tral_ )


#### 3.6.1 Termination Proof


Isabelle automatically proves termination for most function. However, this is
not the case for _reductionLoop_ , but it is manually proved that the reduction
loop monotonically reduces the size of the contract (except for _Close_ , which
reduces the number of accounts), this is sufficient to prove termination.

_reduceContractStep env sta c_ = _Reduced twa tef nsta nc_ =⇒ _evalBound nsta
nc_ < _evalBound sta c_

#### 3.6.2 All Contracts Have a Maximum Time.


If one sends an empty transaction with time equal to _maxTimeContract_ , then
the contract will close.

```
validAndPositive-state sta
minTime sta ≤ iniTime maxTimeContract cont ≤ iniTime
iniTime ≤ endTime accounts sta 6 = []∨ cont 6 = Close
isClosedAndEmpty ( computeTransaction (| interval = ( iniTime , endTime ), inputs = []|) sta cont )
```
#### 3.6.3 Contract Does Not Hold Funds After it Closes


Funds are not held in a contract after it closes.

_computeTransaction tra sta Close_ = _TransactionOutput trec_ =⇒ _txOutWarn-
ings trec_ = []

#### 3.6.4 Transaction Bound


There is a maximum number of transaction that can be accepted by a con-
tract.

_playTrace sl c l_ = _TransactionOutput txOut_ =⇒ | _l_ |_maxTransactionsIni-
tialState c_


# Appendix A


# Contract examples


This appendix includes some example contracts embedded inside isabelle
with their corresponding guarantees:

### A.1 Simple Swap.


A simple swap contract consists on two parties exchanging some _amount_ of
_Token_ s atomically. Each participant needs to deposit their tokens into the
contract by a certain _depositDeadline_. If they do, the contract makes the
swap and pays the participants, if one of the participant fails to make the
deposit, the funds held by the contract can be redeemed by the owner.

#### A.1.1 Contract definition


To reduce the number of parameters we bundle the information required by
each participant into a record.

**record** _SwapParty_ =
— A participant of the contract,
_party_ :: _Party_
— wants to swap an _amount_ of _Token
amount_ :: _Value
currency_ :: _Token_
— before a deadline
_depositDeadline_ :: _Timeout_

The following helper function allows participants to deposit their tokens into
the contract.

**fun** _makeDeposit_ :: _SwapParty_ ⇒ _Contract_ ⇒ _Contract_ **where**


```
makeDeposit sp continue =
— The contract waits for a deposit
When
[
Case
( Deposit
— into the internal account of the party
( party sp )
— from the party wallet
( party sp )
— Amount of tokens
( currency sp )
( amount sp )
)
— Once the deposit has been made, execute the continuation
continue
]
— If the tokens haven’t been deposited by the deadline, close the contract.
— This will return all current funds to their owners.
( depositDeadline sp ) Close
```
The following helper function makes a _Payment_ from one party to the other

**fun** _makePayment_ :: _SwapParty_ ⇒ _SwapParty_ ⇒ _Contract_ ⇒ _Contract_ **where**
_makePayment src dest_ =
— The contract makes a Payment
_Pay_
— from the party internal account
( _party src_ )
— to the destination wallet
( _Party_ ( _party dest_ ))
— of the number of tokens from the source
( _currency src_ )( _amount src_ )

The actual swap contract waits for both parties to make their deposits, then
makes the payout and closes.

**fun** _swap_ :: _SwapParty_ ⇒ _SwapParty_ ⇒ _Contract_ **where**
_swap p1 p2_ = _makeDeposit p1_
( _makeDeposit p2_
( _makePayment p1 p2_
( _makePayment p2 p1 Close_
)))


#### A.1.2 Example execution


Let’s define two participants that want to trade USD and ADA in the cardano
blockchain.

**definition** _adaProvider_ = _Role_ ( _BS_ ′′ _Ada Provider_ ′′)
**definition** _dollarProvider_ = _Role_ ( _BS_ ′′ _Dollar Provider_ ′′)

In cardano, the ADA symbol is represented by the empty string

**definition** _adaToken_ = _Token_ ( _BS_ ′′′′)( _BS_ ′′′′)
**definition** _dollarToken_ = _Token_ ( _BS_ ′′ _85bb65_ ′′)( _BS_ ′′ _dollar_ ′′)

The contract can be created as follow.

**definition**
_swapExample_ =
_swap_
— Party A trades 10 lovelaces
— deposited before Monday, October 3, 2022 4:00:00 PM GMT
(| _party_ = _adaProvider_
, _amount_ = _Constant 10_
, _currency_ = _adaToken_
, _depositDeadline_ = _1664812800000_
|)
— Party B trades 20 cents
— deposited before Monday, October 3, 2022 5:00:00 PM GMT
(| _party_ = _dollarProvider_
, _amount_ = _Constant 20_
, _currency_ = _dollarToken_
, _depositDeadline_ = _1664816400000_
|)

**Happy path**

If both parties deposit before their deadline,

**definition**
_happyPathTransactions_ =
[
— First party deposit
(| _interval_ =( _1664812600000_ , _1664812700000_ )
, _inputs_ =[


```
IDeposit
adaProvider
adaProvider
adaToken
10
]
|)
— Second party deposit
,(| interval =( 1664812900000 , 1664813100000 )
, inputs =[
IDeposit
dollarProvider
dollarProvider
dollarToken
20
]
|)
]
```
payments are made to swap the tokens

**definition**
_happyPathPayments_ =
[ _Payment adaProvider_ ( _Party dollarProvider_ ) _adaToken 10_
, _Payment dollarProvider_ ( _Party adaProvider_ ) _dollarToken 20_
]

and the contract is closed without emitting a warning

**proposition**
_playTrace 0 swapExample happyPathTransactions_ = _TransactionOutput txOut_
=⇒
_txOutContract txOut_ = _Close_
∧ _txOutPayments txOut_ = _happyPathPayments_
∧ _txOutWarnings txOut_ =[]

#### A.1.3 Contract guarantees


**Number of transactions**

Counting the amount of When’s, it is easy to notice that there can be at
most two transactions

**proposition** _maxTransactionsInitialState_ ( _swap a b_ )= _2_


Expressed in a different way, if we use the lemma defined in §3.6.4we can state
that, if the execution of the contract yields a succesful _TransactionOutput_ ,
then the number of transactions must be lower or equal than _2_

**lemma**
_playTrace
initialTime_
( _swap a b_ )
_transactions_ = _TransactionOutput txOut_
=⇒ _length transactions_ ≤ _2_

**Maximum time**

If the deadline of the second party is bigger than the first, then that deadline
is the maximum time of the contract.

**proposition**
_sp1_ =
(| _party_ = _p1_
, _amount_ = _a1_
, _currency_ = _t1_
, _depositDeadline_ = _d1_
|)
=⇒ _sp2_ =
(| _party_ = _p2_
, _amount_ = _a2_
, _currency_ = _t2_
, _depositDeadline_ = _d2_
|)
=⇒ _d2_ > _d1_
=⇒ _d1_ > _0_
=⇒ _contract_ = _swap sp1 sp2_
=⇒ _maxTimeContract_ ( _contract_ )= _d2_


# Appendix B


# ByteString


Conceptually, a _ByteString_ is defined as a list of 8-bit words.

**datatype** ( _plugins del_ : _size_ ) _ByteString_ = _ByteString_ ( _8 word_ ) _list_

**definition** _emptyByteString_ :: _ByteString_ **where**
_emptyByteString_ = _ByteString_ []

**fun** _singletonByteString_ :: _8 word_ ⇒ _ByteString_ **where**
_singletonByteString w_ = _ByteString_ [ _w_ ]

**fun** _consByteString_ :: _8 word_ ⇒ _ByteString_ ⇒ _ByteString_ **where**
_consByteString w_ ( _ByteString t_ )= _ByteString_ ( _w_ # _t_ )

**fun** _appendByteStrings_ :: _ByteString_ ⇒ _ByteString_ ⇒ _ByteString_ **where**
_appendByteStrings_ ( _ByteString a_ )( _ByteString b_ )= _ByteString_ ( _a_ @ _b_ )

**fun** _innerListByteString_ :: _ByteString_ ⇒ _8 word list_ **where**
_innerListByteString_ ( _ByteString x_ )= _x_

**lemma** _lazyConsByteString_ : _consByteString w t_ = _ByteString_ ( _w_ # _innerList-
ByteString t_ )
**by** ( _metis consByteString_. _simps innerListByteString_. _elims_ )

**lemma** _intToWordToUint_ : _x_ ≥ _0_ =⇒ _x_ < _256_ =⇒ _uint_ ( _word-of-int x_ :: _8 word_ )
=( _x_ :: _int_ )
**apply** ( _simp only_ : _uint-word-of-int_ )
**by** _auto_

**lemma** _appendByteStringsAssoc_ : _appendByteStrings_ ( _appendByteStrings x y_ ) _z_


= _appendByteStrings x_ ( _appendByteStrings y z_ )
**by** ( _metis append_. _assoc appendByteStrings_. _simps innerListByteString_. _elims_ )

**fun** _lengthByteString_ :: _ByteString_ ⇒ _nat_ **where**
_lengthByteString_ ( _ByteString x_ )= _length x_

**fun** _takeByteString_ :: _nat_ ⇒ _ByteString_ ⇒ _ByteString_ **where**
_takeByteString n_ ( _ByteString x_ )= _ByteString_ ( _take n x_ )

**fun** _dropByteString_ :: _nat_ ⇒ _ByteString_ ⇒ _ByteString_ **where**
_dropByteString n_ ( _ByteString x_ )= _ByteString_ ( _drop n x_ )

**lemma** _appendTakeDropByteString_ : _appendByteStrings_ ( _takeByteString n x_ )( _dropByteString
n x_ )= _x_
**by** ( _metis appendByteStrings_. _simps append-take-drop-id dropByteString_. _simps
innerListByteString_. _cases takeByteString_. _simps_ )

The _BS_ helper allows to create a _ByteString_ out of a regular _string_.

**fun** _BS_ :: _string_ ⇒ _ByteString_ **where**
_BS str_ = _ByteString_ ( _map of-char str_ )

For example _BS_ ′′ _abc_ ′′is evaluated to _ByteString_ [ _97_ , _98_ , _99_ ]

**Size**

**instantiation** _ByteString_ :: _size_
**begin**

**definition** _size-ByteString_ **where**
_size-ByteString-overloaded-def_ : _size-ByteString_ = _lengthByteString_
**instance ..**

**end**

### B.1 Ordering


We define the(<)and(≤)functions that provide _ord_ ering.

**instantiation** _ByteString_ :: _ord_
**begin**

**fun** _less-eq-BS_ ′::( _8 word_ ) _list_ ⇒( _8 word_ ) _list_ ⇒ _bool_ **where**
_less-eq-BS_ ′ _Nil Nil_ = _True_ |


_less-eq-BS_ ′( _Cons - -_ ) _Nil_ = _False_ |
_less-eq-BS_ ′ _Nil_ ( _Cons - -_ )= _True_ |
_less-eq-BS_ ′( _Cons h1 t1_ )( _Cons h2 t2_ )=
( _if h2_ < _h1 then False
else_ ( _if h1_ = _h2 then less-eq-BS_ ′ _t1 t2 else True_ ))

**fun** _less-eq-BS_ :: _ByteString_ ⇒ _ByteString_ ⇒ _bool_ **where**
_less-eq-BS_ ( _ByteString xs_ )( _ByteString ys_ )= _less-eq-BS_ ′ _xs ys_

**definition** _a_ ≤ _b_ = _less-eq-BS a b_

**fun** _less-BS_ :: _ByteString_ ⇒ _ByteString_ ⇒ _bool_ **where**
_less-BS a b_ =(¬( _less-eq-BS b a_ ))

**definition** _a_ < _b_ = _less-BS a b_
**end**

And we also define some lemmas useful for total order.

**lemma** _oneLessEqBS_ ′:¬ _less-eq-BS_ ′ _bs2 bs1_ =⇒ _less-eq-BS_ ′ _bs1 bs2_
**lemma** _oneLessEqBS_ :¬ _less-eq-BS bs2 bs1_ =⇒ _less-eq-BS bs1 bs2_
**lemma** _less-eq-BS-trans_ ′: _less-eq-BS_ ′ _x y_ =⇒ _less-eq-BS_ ′ _y z_ =⇒ _less-eq-BS_ ′ _x z_
**lemma** _less-eq-BS-trans_ : _less-eq-BS x y_ =⇒ _less-eq-BS y z_ =⇒ _less-eq-BS x z_
**lemma** _byteStringLessEqTwiceEq_ ′: _less-eq-BS_ ′ _x y_ =⇒ _less-eq-BS_ ′ _y x_ =⇒ _x_ =
_y_
**lemma** _byteStringLessEqTwiceEq_ : _less-eq-BS x y_ =⇒ _less-eq-BS y x_ =⇒ _x_ = _y_
**lemma** _lineaBS_ : _less-eq-BS x y_ ∨ _less-eq-BS y x_


# Appendix C


# Code exports


This theory contains the necessary code to export a version of the Marlowe
Semantics in Haskell.

We start by importing the theories we want to export and a translation the-
ory. The theory _Code-Target-Numeral_ translates the default representation
of numbers (which is suitable for logic reasoning) into a more performant
representation.

**theory** _CodeExports_

**imports**
_Core_. _Semantics
Examples_. _Swap
HOL_ − _Library_. _Code-Target-Numeral
HOL_. _String_

**begin**

We provide some Serialization options to use Haskell native _String_ instead
of our logical represenation of _ByteString_

**code-printing**
— The first command tells the serializer to use Haskell
— native _String_ instead of our logical ByteString
**type-constructor** _ByteString_
⇀( _Haskell_ ) _String_
— The next three commands tells the serializer to use the operators provided by
— the Ord instance instead of the ones that work with the logical representation
| **constant** _less-eq-BS_
⇀( _Haskell_ ) **infix** _4_ <=
| **constant** _less-BS_


```
⇀( Haskell ) infix 4 <
| constant HOL. equal :: ByteString ⇒ ByteString ⇒ bool
⇀( Haskell ) infix 4 ==
— The next command tells the serializer to implode the logical Isabelle string
— into Haskell string. Because this is a textual rewrite, we need to force the
— generation of String.implode
| constant BS :: string ⇒ ByteString
⇀( Haskell ) Stringa. implode
```
With a **code_identifier** we hint what the name of the module should be.

**code-identifier
code-module** _Swap_ ⇀( _Haskell_ ) _Examples_. _Swap_

We export all the constants in one statement, because they don’t add up, if
you export two times, the second export will overwrite the first one.

**export-code**
— With the following exports, we declare that we want to have all the important
semantic functions. Ideally, just with this we would have everything we need, but
we need to force some exports.
_evalValue
evalObservation
reductionLoop
reduceContractUntilQuiescent
applyAllInputs
playTrace
computeTransaction_

```
— Export examples to be used as oracle specificaiton tests
swapExample
happyPathTransactions
happyPathPayments
```
— Force the export of string implode (works together with the BS code_printing
hint)
_String_. _implode_

```
— Force export of State record selectors
txOutContract
txOutWarnings
txOutPayments
txOutState
```
```
— Force export of Arith.Int constructor
```

```
int-of-integer
```
```
— Force export of TransactionOutput constructors
TransactionOutput
```
```
— Force export of TransactionWarning constructors
TransactionNonPositiveDeposit
```
```
— Force export of TransactionError constructors
TEAmbiguousTimeIntervalError
```
```
— Force export of Payment constructor
Payment
```
```
— Force the export of the transaction record
Transaction-ext
```
```
— Force the export of the transaction output record
TransactionOutputRecord-ext
```
— Force the export on some equality functions (sadly it does not force the Eq
instance)
_equal-TransactionWarning-inst_. _equal-TransactionWarning
equal-Payment-inst_. _equal-Payment
equal-Value-inst_. _equal-Value
equal-Observation-inst_. _equal-Observation
equal-Action-inst_. _equal-Action
equal-Input-inst_. _equal-Input
equal-Transaction-ext-inst_. _equal-Transaction-ext
equal-State-ext-inst_. _equal-State-ext
equal-IntervalError-inst_. _equal-IntervalError
equal-TransactionError-inst_. _equal-TransactionError
equal-TransactionOutput-inst_. _equal-TransactionOutput_

```
in Haskell ( string-classes )
```

# Appendix D


# Marlowe Core JSON


The Json specification for Marlowe Core is defined in Literate Haskell us-
ing the Aeson library. In order to fully understand the specification, some
knowledge of Haskell and the library is recommended but not necessary.

For each Marlowe datatype we define a way to parse the JSON into a value
(FromJSON instances) and a way to serialize a value to JSON (ToJSON
instances).

### D.1 Party.


Parties are serialized as a simple object with an _address_ or _role_token_ key,
depending on the _Party_ type.

```
instanceToJSON Partywhere
toJSON (Address address) =
object["address".=address]
toJSON (Role name) =
object["role_token".=name]
instanceFromJSON Partywhere
parseJSON =withObject"Party"$
λv→asAddress v<|>asRole v
where
asAddress v=Address<$>v.:"address"
asRole v=Role<$>v.:"role_token"
```
for example, the following _Party_

```
addressExample::Party
addressExample=Address"example address"
```

is serialized as{"address":"example address"}, and

```
roleExample::Party
roleExample=Role"example role"
```
is serialized as{"role_token":"example role"}

### D.2 Token


The _Token_ type is serialized as an object with two properties, _currency_sym-
bol_ and _token_name_

```
instanceToJSON Tokenwhere
toJSON (Token currSym tokName) =object
["currency_symbol".=currSym
,"token_name".=tokName
]
instanceFromJSON Tokenwhere
parseJSON =withObject"Token"
(λv→
Token<$>(v.:"currency_symbol")
<∗>(v.:"token_name")
)
```
for example, the following _Token_

```
dolarToken::Token
dolarToken=Token"85bb65" "dolar"
```
is serialized as{"currency_symbol":"85bb65","token_name":"dolar"}

### D.3 Payee


Payees are serialized as a simple object with an _account_ or _party_ key, de-
pending on the _Payee_ type.

```
instanceToJSON Payeewhere
toJSON (Account account) =
object["account".=account]
toJSON (Party party) =
```

```
object["party".=party]
instanceFromJSON Payeewhere
parseJSON =withObject"Payee"$
λv→asAccount v<|>asParty v
where
asAccount v=Account<$>v.:"account"
asParty v=Party<$>v.:"party"
```
for example, the following _Payee_

```
internalPayeeExample::Payee
internalPayeeExample=Account addressExample
```
is serialized as{"account":{"address":"example address"}}, and

```
externalPayeeExample::Payee
externalPayeeExample=Party roleExample
```
is serialized as{"party":{"role_token":"example role"}}

### D.4 ChoicesId


The _ChoiceId_ type is serialized as an object with two properties, _choice_name_
and _choice_owner_

```
instanceToJSON ChoiceIdwhere
toJSON (ChoiceId name party) =object
["choice_name".=name
,"choice_owner".=party
]
instanceFromJSON ChoiceIdwhere
parseJSON =withObject"ChoiceId"
(λv→
ChoiceId<$>(v.:"choice_name")
<∗>(v.:"choice_owner")
)
```
for example, the following _ChoiceId_

```
choiceIdExample::ChoiceId
choiceIdExample=ChoiceId"ada price"addressExample
```
is serialized as


##### {


```
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
}
```
### D.5 Bound


The _Bound_ type is serialized as an object with two properties, _from_ and _to_

```
instanceToJSON Boundwhere
toJSON (Bound from to) =object
["from".=from
,"to".=to
]
instanceFromJSON Boundwhere
parseJSON =withObject"Bound"(λv→
Bound<$>(getInteger"lower bound"=<<(v.:"from"))
<∗>(getInteger"higher bound"=<<(v.:"to"))
)
```
for example, the following _Bound_

```
exampleBound::Bound
exampleBound=Bound2 10
```
is serialized as{"from": 2,"to": 10}

### D.6 Values


The _ValueId_ type is serialized as a literal string.

```
instanceToJSON ValueIdwhere
toJSON (ValueId x) =toJSON x
instanceFromJSON ValueIdwhere
parseJSON =withText"ValueId"$return◦ValueId◦T.unpack
```
The _Value_ serialization depends on the constructor. A _Constant_ is serialized
as a _number_ , _TimeIntervalStart_ and _TimeIntervalEnd_ are serialized as literal


strings, and the rest are serialized as a single object (with keys depending on
the constructor).

```
instanceToJSON Valuewhere
toJSON (AvailableMoney accountId token) =object
["amount_of_token".=token
,"in_account".=accountId
]
toJSON (Constant(Int_of_integer x)) =toJSON x
toJSON (NegValue x) =object
["negate".=x]
toJSON (AddValue lhs rhs) =object
["add".=lhs
,"and".=rhs
]
toJSON (SubValue lhs rhs) =object
["value".=lhs
,"minus".=rhs
]
toJSON (MulValue lhs rhs) =object
["multiply".=lhs
,"times".=rhs
]
toJSON (DivValue lhs rhs) =object
["divide".=lhs
,"by".=rhs
]
toJSON (ChoiceValue choiceId) =object
["value_of_choice".=choiceId]
toJSON TimeIntervalStart=JSON.String$T.pack"time_interval_start"
toJSON TimeIntervalEnd=JSON.String$T.pack"time_interval_end"
toJSON (UseValue valueId) =object
["use_value".=valueId]
toJSON (Cond obs tv ev) =object
["if".=obs
,"then".=tv
,"else".=ev
]
instanceFromJSON Valuewhere
parseJSON (JSON.Object v) =
(AvailableMoney<$>(v.:"in_account")
```

```
<∗>(v.:"amount_of_token"))
<|>(NegValue<$>(v.:"negate"))
<|>(AddValue<$>(v.:"add")
<∗>(v.:"and"))
<|>(SubValue<$>(v.:"value")
<∗>(v.:"minus"))
<|>(MulValue<$>(v.:"multiply")
<∗>(v.:"times"))
<|>(DivValue<$>(v.:"divide")<∗>(v.:"by"))
<|>(ChoiceValue<$>(v.:"value_of_choice"))
<|>(UseValue<$>(v.:"use_value"))
<|>(Cond<$>(v.:"if")
<∗>(v.:"then")
<∗>(v.:"else"))
parseJSON (JSON.String"time_interval_start") =return TimeIntervalStart
parseJSON (JSON.String"time_interval_end") =return TimeIntervalEnd
parseJSON (JSON.Number n) =Constant<$>getInteger"constant value"n
parseJSON =fail"Value must be either a string, object or an integer"
```
Some examples for each _Value_ s type

**Constant**

```
constantExample::Value
constantExample=Constant 1
```
is serialized as 1

**TimeIntervalStart**

```
intervalStartExample::Value
intervalStartExample=TimeIntervalStart
```
is serialized as"time_interval_start"

**TimeIntervalEnd**

```
intervalEndExample::Value
intervalEndExample=TimeIntervalEnd
```
is serialized as"time_interval_end"


**AddValue**

```
addExample::Value
addExample=AddValue(Constant1) (Constant2)
```
is serialized as{"add": 1,"and": 2}

**SubValue**

```
subExample::Value
subExample=SubValue(Constant4) (Constant2)
```
is serialized as{"minus": 2,"value": 4}

**MulValue**

```
mulExample::Value
mulExample=MulValue(Constant3) (Constant6)
```
is serialized as{"multiply": 3,"times": 6}

**DivValue**

```
divExample::Value
divExample=DivValue(Constant8) (Constant4)
```
is serialized as{"by": 4,"divide": 8}

**NegValue**

```
negateExample::Value
negateExample=NegValue(Constant3)
```
is serialized as{"negate": 3}


**ChoiceValue**

```
choiceValueExample::Value
choiceValueExample=ChoiceValue choiceIdExample
```
is serialized as

```
{
"value_of_choice": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
}
}
```
**UseValue**

```
useValueExample::Value
useValueExample=UseValue(ValueId"variable name")
```
is serialized as{"use_value":"variable name"}

**Cond**

```
condExample::Value
condExample=Cond TrueObs addExample mulExample
```
is serialized as

```
{
"else": {
"multiply": 3,
"times": 6
},
"if": true,
"then": {
"add": 1,
"and": 2
}
}
```

**AvailableMoney**

```
availableMoneyExample::Value
availableMoneyExample=AvailableMoney addressExample dolarToken
```
is serialized as

```
{
"amount_of_token": {
"currency_symbol": "85bb65",
"token_name": "dolar"
},
"in_account": {
"address": "example address"
}
}
```
### D.7 Observation


The _Observation_ type is serialized as native boolean (for _TrueObs_ and _FalseObs_ )
or as an object with different properties, depending on the constructor.

```
instanceToJSON Observationwhere
toJSON (AndObs lhs rhs) =object
["both".=lhs
,"and".=rhs
]
toJSON (OrObs lhs rhs) =object
["either".=lhs
,"or".=rhs
]
toJSON (NotObs v) =object
["not".=v]
toJSON (ChoseSomething choiceId) =object
["chose_something_for".=choiceId]
toJSON (ValueGE lhs rhs) =object
["value".=lhs
,"ge_than".=rhs
]
toJSON (ValueGT lhs rhs) =object
```

```
["value".=lhs
,"gt".=rhs
]
toJSON (ValueLT lhs rhs) =object
["value".=lhs
,"lt".=rhs
]
toJSON (ValueLE lhs rhs) =object
["value".=lhs
,"le_than".=rhs
]
toJSON (ValueEQ lhs rhs) =object
["value".=lhs
,"equal_to".=rhs
]
toJSON TrueObs=toJSON True
toJSON FalseObs=toJSON False
instanceFromJSON Observationwhere
parseJSON (JSON.Bool True) =return TrueObs
parseJSON (JSON.Bool False) =return FalseObs
parseJSON (JSON.Object v) =
(AndObs<$>(v.:"both")
<∗>(v.:"and"))
<|>(OrObs<$>(v.:"either")
<∗>(v.:"or"))
<|>(NotObs<$>(v.:"not"))
<|>(ChoseSomething<$>(v.:"chose_something_for"))
<|>(ValueGE<$>(v.:"value")
<∗>(v.:"ge_than"))
<|>(ValueGT<$>(v.:"value")
<∗>(v.:"gt"))
<|>(ValueLT<$>(v.:"value")
<∗>(v.:"lt"))
<|>(ValueLE<$>(v.:"value")
<∗>(v.:"le_than"))
<|>(ValueEQ<$>(v.:"value")
<∗>(v.:"equal_to"))
parseJSON =fail"Observation must be either an object or a boolean"
```
Some examples for each _Observation_ type


**TrueObs**

```
trueExample::Observation
trueExample=TrueObs
```
is serialized astrue

**FalseObs**

```
falseExample::Observation
falseExample=FalseObs
```
is serialized asfalse

**AndObs**

```
andExample::Observation
andExample=AndObs TrueObs FalseObs
```
is serialized as{"and":false,"both":true}

**OrObs**

```
orExample::Observation
orExample=OrObs TrueObs FalseObs
```
is serialized as{"either":true,"or":false}

**NotObs**

```
notExample::Observation
notExample=NotObs TrueObs
```
is serialized as{"not":true}


**ChoseSomething**

```
choseExample::Observation
choseExample=ChoseSomething choiceIdExample
```
is serialized as

```
{
"chose_something_for": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
}
}
```
**ValueGE**

```
valueGEExample::Observation
valueGEExample=ValueGE(Constant1) (Constant2)
```
is serialized as{"ge_than": 2,"value": 1}

**ValueGT**

```
valueGTExample::Observation
valueGTExample=ValueGT(Constant1) (Constant2)
```
is serialized as{"gt": 2,"value": 1}

**ValueLT**

```
valueLTExample::Observation
valueLTExample=ValueLT (Constant1) (Constant2)
```
is serialized as{"lt": 2,"value": 1}


**ValueLE**

```
valueLEExample::Observation
valueLEExample=ValueLE(Constant1) (Constant2)
```
is serialized as{"le_than": 2,"value": 1}

**ValueEQ**

```
valueEQExample::Observation
valueEQExample=ValueEQ(Constant1) (Constant2)
```
is serialized as{"equal_to": 2,"value": 1}

### D.8 Action


The _Action_ type is serialized as an object with different properties, depending
the constructor.

```
instanceToJSON Actionwhere
toJSON (Deposit accountId party token val) =object
["into_account".=accountId
,"party".=party
,"of_token".=token
,"deposits".=val
]
toJSON (Choice choiceId bounds) =object
["for_choice".=choiceId
,"choose_between".=toJSONList(map toJSON bounds)
]
toJSON (Notify obs) =object
["notify_if".=obs]
instanceFromJSON Actionwhere
parseJSON =withObject"Action"(λv→
(Deposit<$>(v.:"into_account")
<∗>(v.:"party")
<∗>(v.:"of_token")
<∗>(v.:"deposits"))
<|>(Choice<$>(v.:"for_choice")
```

```
<∗>((v.:"choose_between")>>=
withArray"Bound list"(λbl→
mapM parseJSON (F.toList bl)
)))
<|>(Notify<$>(v.:"notify_if"))
)
```
Some examples for each _Action_ type

**Deposit**

```
depositExample::Action
depositExample=Deposit
addressExample
roleExample
dolarToken
constantExample
```
is serialized as

```
{
"deposits": 1,
"into_account": {
"address": "example address"
},
"of_token": {
"currency_symbol": "85bb65",
"token_name": "dolar"
},
"party": {
"role_token": "example role"
}
}
```
**Choice**

```
choiceExample::Action
choiceExample=Choice
choiceIdExample
[Bound0 1,Bound4 8]
```
is serialized as


##### {


```
"choose_between": [
{
"from": 0,
"to": 1
},
{
"from": 4,
"to": 8
}
],
"for_choice": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
}
}
```
**Notify**

```
notifyExample::Action
notifyExample=Notify(ChoseSomething choiceIdExample)
```
is serialized as

```
{
"notify_if": {
"chose_something_for": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
}
}
}
```
### D.9 Case


The _Case_ type is serialized as an object with two properties ( _case_ and _then_ ).


```
instanceToJSON Casewhere
toJSON (Case act cont) =object
["case".=act
,"then".=cont
]
instanceFromJSON Casewhere
parseJSON =withObject"Case"
(λv→
Case<$>(v.:"case")<∗>(v.:"then")
)
```
For example, the following _Case_

```
caseExample::Case
caseExample=Case notifyExample Close
```
is serialized as

```
{
"case": {
"notify_if": {
"chose_something_for": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
}
}
},
"then": "close"
}
```
### D.10 Contract


The _Contract_ type is serialized as the literal string "close" or as an object,
depending on the constructor

```
instanceToJSON Contractwhere
toJSON Close=JSON.String$T.pack"close"
toJSON (Pay accountId payee token value contract) =object
```

```
["from_account".=accountId
,"to".=payee
,"token".=token
,"pay".=value
,"then".=contract
]
toJSON (If obs cont1 cont2) =object
["if".=obs
,"then".=cont1
,"else".=cont2
]
toJSON (When caseList timeout cont) =object
["when".=toJSONList(map toJSON caseList)
,"timeout".=timeout
,"timeout_continuation".=cont
]
toJSON (Let valId value cont) =object
["let".=valId
,"be".=value
,"then".=cont
]
toJSON (Assert obs cont) =object
["assert".=obs
,"then".=cont
]
```
instanceFromJSON Contractwhere
parseJSON (JSON.String"close") =return Close
parseJSON (JSON.Object v) =
(Pay<$>(v.:"from_account")
<∗>(v.:"to")
<∗>(v.:"token")
<∗>(v.:"pay")
<∗>(v.:"then"))
<|>(If<$>(v.:"if")
<∗>(v.:"then")
<∗>(v.:"else"))
<|>(When<$>((v.:"when")>>=
withArray"Case list"(λcl→
mapM parseJSON(F.toList cl)
))


```
<∗>(withInteger"when timeout"=<<(v.:"timeout"))
<∗>(v.:"timeout_continuation"))
<|>(Let<$>(v.:"let")
<∗>(v.:"be")
<∗>(v.:"then"))
<|>(Assert<$>(v.:"assert")
<∗>(v.:"then"))
parseJSON =
fail"Contract must be either an object or a the string \"close\""
```
Some examples for each _Contract_ type

**Close**

```
closeExample::Contract
closeExample=Close
```
is serialized as"close"

**Pay**

```
payExample::Contract
payExample=Pay
roleExample
internalPayeeExample
dolarToken
(Constant10)
Close
```
is serialized as

```
{
"from_account": {
"role_token": "example role"
},
"pay": 10,
"then": "close",
"to": {
"account": {
"address": "example address"
```

##### }


##### },


```
"token": {
"currency_symbol": "85bb65",
"token_name": "dolar"
}
}
```
**If**

```
ifExample::Contract
ifExample=If
TrueObs
Close
Close
```
is serialized as

```
{
"else": "close",
"if": true,
"then": "close"
}
```
**When**

```
whenExample::Contract
whenExample=When
[Case(Notify TrueObs)Close
,Case(Notify FalseObs)Close
]
20
Close
```
is serialized as

```
{
"timeout": 20,
"timeout_continuation": "close",
"when": [
```

##### {


```
"case": {
"notify_if": true
},
"then": "close"
},
{
"case": {
"notify_if": false
},
"then": "close"
}
]
}
```
**Let**

```
letExample::Contract
letExample=Let(ValueId"var") (Constant10)Close
```
is serialized as

```
{
"be": 10,
"let": "var",
"then": "close"
}
```
**Assert**

```
assertExample::Contract
assertExample=Assert choseExample Close
```
is serialized as

```
{
"assert": {
"chose_something_for": {
"choice_name": "ada price",
"choice_owner": {
```

```
"address": "example address"
}
}
},
"then": "close"
}
```
### D.11 Input.


The _Input_ type is serialized as the literal string "input_notify" or as an object,
depending on the constructor.

```
instanceToJSON Inputwhere
toJSON (IDeposit accId party tok amount) =object
["input_from_party".=party
,"that_deposits".=amount
,"of_token".=tok
,"into_account".=accId
]
toJSON (IChoice choiceId chosenNum) =object
["input_that_chooses_num".=chosenNum
,"for_choice_id".=choiceId
]
toJSON INotify=JSON.String$T.pack"input_notify"
instanceFromJSON Inputwhere
parseJSON (JSON.String"input_notify") =return INotify
parseJSON (JSON.Object v) =
IChoice<$>v.:"for_choice_id"
<∗>v.:"input_that_chooses_num"
<|>IDeposit<$>v.:"into_account"
<∗>v.:"input_from_party"
<∗>v.:"of_token"
<∗>v.:"that_deposits"
parseJSON =
fail"Input must be either an object or the string \"input_notify\""
```
Some examples for each _Input_ type


**INotify**

```
iNotifyExample::Input
iNotifyExample=INotify
```
is serialized as"input_notify"

**IChoice**

```
iChoiceExample::Input
iChoiceExample=IChoice choiceIdExample 3
```
is serialized as

```
{
"for_choice_id": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
},
"input_that_chooses_num": 3
}
```
**IDeposit**

```
iDepositExample::Input
iDepositExample=IDeposit addressExample roleExample dolarToken 5
```
is serialized as

```
{
"input_from_party": {
"role_token": "example role"
},
"into_account": {
"address": "example address"
},
"of_token": {
"currency_symbol": "85bb65",
```

```
"token_name": "dolar"
},
"that_deposits": 5
}
```
### D.12 Transaction


The _Transaction_ type is serialized as an object with two properties, _tx_in-
terval_ and _tx_inputs_.

```
instanceToJSON(Transaction_ext a)where
toJSON (Transaction_ext(from,to)txInps ) =object
["tx_interval".=timeIntervalJSON
,"tx_inputs".=toJSONList(map toJSON txInps)
]
wheretimeIntervalJSON =object["from".=from
,"to".=to
]
instanceFromJSON(Transaction_ext())where
parseJSON (JSON.Object v) =
Transaction_ext<$>(parseTimeInterval=<<(v.:"tx_interval"))
<∗>((v.:"tx_inputs")>>=
withArray"Transaction input list"(λcl→
mapM parseJSON(F.toList cl)
))
<∗>pure()
whereparseTimeInterval=withObject"TimeInterval"(λv→
dofrom←withInteger"TimeInterval from"=<<(v.:"from")
to←withInteger"TimeInterval to"=<<(v.:"to")
return(from,to)
)
parseJSON =fail"Transaction must be an object"
```
for example, the following _Transaction_

```
transactionExample::Transaction_ext()
transactionExample=Transaction_ext
(10,100)
[iChoiceExample
,iNotifyExample
```

##### ]


##### ()


is serialized as

```
{
"tx_inputs": [
{
"for_choice_id": {
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
},
"input_that_chooses_num": 3
},
"input_notify"
],
"tx_interval": {
"from": 10,
"to": 100
}
}
```
### D.13 Payment


The _Payment_ type is serialized as a single object with three properties

```
instanceToJSON Paymentwhere
toJSON (Payment from to token amount) =object
["payment_from".=from
,"to".=to
,"token".=token
,"amount".=amount
]
instanceFromJSON Paymentwhere
parseJSON =withObject"Payment"
(λv→
Payment<$>(v.:"payment_from")
<∗>(v.:"to")
```

```
<∗>(v.:"token")
<∗>(v.:"amount")
)
```
for example, the following _Payment_

```
paymentExample::Payment
paymentExample=Payment
addressExample
externalPayeeExample
dolarToken
10
```
is serialized as

```
{
"amount": 10,
"payment_from": {
"address": "example address"
},
"to": {
"party": {
"role_token": "example role"
}
},
"token": {
"currency_symbol": "85bb65",
"token_name": "dolar"
}
}
```
### D.14 State


The _State_ type is serialized as a single object with four properties. Each Map
is represented by a list of key value tuples.

```
instanceToJSON(State_ext())where
toJSON (State_ext accounts choices boundValues minTime ) =object
["accounts".=toJSON accounts
,"choices".=toJSON choices
,"boundValues".=toJSON boundValues
```

```
,"minTime".=minTime
]
instanceFromJSON(State_ext())where
parseJSON =withObject"State"
(λv→
State_ext<$>(v.:"accounts")
<∗>(v.:"choices")
<∗>(v.:"boundValues")
<∗>(v.:"minTime")
<∗>pure()
)
```
for example, the following state

```
stateExample::State_ext()
stateExample=State_ext
[((roleExample,dolarToken),20)]
[(choiceIdExample,10)]
[(ValueId"example",30)]
90
()
```
is serialized as

```json
{
"accounts": [
[
[
{
"role_token": "example role"
},
{
"currency_symbol": "85bb65",
"token_name": "dolar"
}
],
20
]
],
"boundValues": [
[
```

```json
"example",
30
]
],
"choices": [
[
{
"choice_name": "ada price",
"choice_owner": {
"address": "example address"
}
},
10
]
],
"minTime": 90
}
```
### D.15 TransactionWarning.


The _TransactionWarning_ type is serialized as a literal string (in case of _Trans-
actionAssertionFailed_ ) or as an object with different properties, depending
the constructor.

```
instanceToJSON TransactionWarningwhere
toJSON (TransactionNonPositiveDeposit party accId tok amount) =object
["party".=party
,"asked_to_deposit".=amount
,"of_token".=tok
,"in_account".=accId
]
toJSON (TransactionNonPositivePay accId payee tok amount) =object
["account".=accId
,"asked_to_pay".=amount
,"of_token".=tok
,"to_payee".=payee
]
toJSON (TransactionPartialPay accId payee tok paid expected) =object
["account".=accId
,"asked_to_pay".=expected
```

```
,"of_token".=tok
,"to_payee".=payee
,"but_only_paid".=paid
]
toJSON (TransactionShadowing valId oldVal newVal) =object
["value_id".=valId
,"had_value".=oldVal
,"is_now_assigned".=newVal
]
toJSON TransactionAssertionFailed=JSON.String$T.pack"assertion_failed"
instanceFromJSON TransactionWarningwhere
parseJSON (JSON.String"assertion_failed") =
return TransactionAssertionFailed
parseJSON (JSON.Object v) =
(TransactionNonPositiveDeposit<$>(v.:"party")
<∗>(v.:"in_account")
<∗>(v.:"of_token")
<∗>(v.:"asked_to_deposit"))
<|>(domaybeButOnlyPaid←v.:?"but_only_paid"
casemaybeButOnlyPaid::Maybe Scientificof
Nothing→TransactionNonPositivePay<$>(v.:"account")
<∗>(v.:"to_payee")
<∗>(v.:"of_token")
<∗>(v.:"asked_to_pay")
Just butOnlyPaid→TransactionPartialPay<$>(v.:"account")
<∗>(v.:"to_payee")
<∗>(v.:"of_token")
<∗>getInteger"but only paid"butOnlyPaid
<∗>(v.:"asked_to_pay"))
<|>(TransactionShadowing<$>(v.:"value_id")
<∗>(v.:"had_value")
<∗>(v.:"is_now_assigned"))
parseJSON =
fail"Contract must be either an object or a the string \"close\""
```
Some examples for each _TransactionWarning_ type

**TransactionNonPositiveDeposit**

```
transactionNonPositiveDepositExample::TransactionWarning
transactionNonPositiveDepositExample=TransactionNonPositiveDeposit
```

```
addressExample
roleExample
dolarToken
20
```
is serialized as

```json
{
  "asked_to_deposit": 20,
  "in_account": {
    "role_token": "example role"
  },
  "of_token": {
    "currency_symbol": "85bb65",
    "token_name": "dolar"
  },
  "party": {
    "address": "example address"
  }
}
```
**TransactionNonPositivePay**

```haskell
transactionNonPositivePayExample::TransactionWarning
transactionNonPositivePayExample=TransactionNonPositivePay
addressExample
internalPayeeExample
dolarToken
20
```
is serialized as

```json
{
  "account": {
  "address": "example address"
  },
  "asked_to_pay": 20,
  "of_token": {
  "currency_symbol": "85bb65",
  "token_name": "dolar"
  },
  "to_payee": {
    "account": {
      "address": "example address"
    }
  }
}
```
**TransactionPartialPay**

```
transactionPartialPayExample::TransactionWarning
transactionPartialPayExample=TransactionPartialPay
addressExample
internalPayeeExample
dolarToken
20
30
```
is serialized as

```json
{
  "account": {
    "address": "example address"
  },
  "asked_to_pay": 30,
  "but_only_paid": 20,
  "of_token": {
  "currency_symbol": "85bb65",
  "token_name": "dolar"
  },
  "to_payee": {
    "account": {
      "address": "example address"
    }
  }
}
```
**TransactionShadowing**

```
transactionShadowingExample::TransactionWarning
transactionShadowingExample=TransactionShadowing
```

```haskell
(ValueId"example")
4
5
```
is serialized as

```json
{
  "had_value": 4,
  "is_now_assigned": 5,
  "value_id": "example"
}
```
**TransactionAssertionFailed**

```
transactionAssertionFailedExample::TransactionWarning
transactionAssertionFailedExample=TransactionAssertionFailed
```
is serialized as"assertion_failed"

## D.16 IntervalError


The _IntervalError_ type is serialized as an object with a single property (de-
pending on the constructor) and in a tuple, the values.

```haskell
instanceToJSON IntervalErrorwhere
toJSON (InvalidInterval(s,e)) =object
[("invalidInterval".=toJSON(s,e))]
toJSON (IntervalInPastError t(s,e)) =object
[("intervalInPastError".=toJSON (t,s,e))]
instanceFromJSON IntervalErrorwhere
parseJSON (JSON.Object v) =
let
parseInvalidInterval=do
(s,e)←v.:"invalidInterval"
pure$InvalidInterval(s,e)
parseIntervalInPastError=do
(t,s,e)←v.:"intervalInPastError"
pure$IntervalInPastError t(s,e)
in
parseIntervalInPastError<|>parseInvalidInterval
```

```
parseJSON invalid=
JSON.prependFailure"parsing IntervalError failed, "(JSON.typeMismatch"Object"invalid)
```
Some examples for each _IntervalError_ type

**InvalidInterval**

```
invalidIntervalExample::IntervalError
invalidIntervalExample=InvalidInterval(10,20)
```
is serialized as{"invalidInterval": [10,20]}

**IntervalInPastError**

```
intervalInPastErrorExample::IntervalError
intervalInPastErrorExample=IntervalInPastError30 (10,20)
```
is serialized as{"intervalInPastError": [30, 10 ,20]}

## D.17 TransactionError


The _TransactionError_ type is serialized as an object with a _tag_ property that
differentiates the type, and a _contents_ property that includes the parameter
if any.

```haskel
instanceToJSON TransactionErrorwhere
toJSON TEAmbiguousTimeIntervalError=object
["tag".=JSON.String"TEAmbiguousTimeIntervalError"
,"contents".=JSON.Null
]
toJSON TEApplyNoMatchError=object
["tag".=JSON.String"TEApplyNoMatchError"
,"contents".=JSON.Null
]
toJSON (TEIntervalError e) =object
["tag".=JSON.String"TEIntervalError"
,"contents".=toJSON e
]
toJSON TEUselessTransaction=object
```

```haskell
["tag".=JSON.String"TEUselessTransaction"
,"contents".=JSON.Null
]
instanceFromJSON TransactionErrorwhere
parseJSON=withObject"TransactionError"
(λv→
do
tag::String←v.:"tag"
casetagof
"TEAmbiguousTimeIntervalError"→
pure TEAmbiguousTimeIntervalError
"TEApplyNoMatchError"→
pure TEApplyNoMatchError
"TEIntervalError"→
TEIntervalError<$>v.:"contents"
"TEUselessTransaction"→
pure TEUselessTransaction
)
```
Some examples for each _TransactionError_ type

**TEAmbiguousTimeIntervalError**

```
teAmbiguousTimeIntervalErrorExample::TransactionError
teAmbiguousTimeIntervalErrorExample=TEAmbiguousTimeIntervalError
```
is serialized as

```json
{
  "contents": null,
  "tag": "TEAmbiguousTimeIntervalError"
}
```
**TEApplyNoMatchError**

```
teApplyNoMatchErrorExample::TransactionError
teApplyNoMatchErrorExample=TEApplyNoMatchError
```
is serialized as


##### {


```json
  "contents": null,
  "tag": "TEApplyNoMatchError"
}
```
**TEIntervalError**

```
teIntervalErrorExample::TransactionError
teIntervalErrorExample=TEIntervalError intervalInPastErrorExample
```
is serialized as

```json
{
  "contents": {
    "intervalInPastError": [
      30,
      10,
      20
    ]
  },
  "tag": "TEIntervalError"
  }
```
**TEUselessTransaction**

```haskell
teUselessTransactionExample::TransactionError
teUselessTransactionExample=TEUselessTransaction
```
is serialized as

```json
{
  "contents": null,
  "tag": "TEUselessTransaction"
}
```
## D.18 TransactionOutput


The _TransactionOutput_ is serialized as a single object with one property
( _transaction_error_ ) in case of an error, or 4 properties in case of success.


```haskell
instanceToJSON TransactionOutputwhere
toJSON (TransactionError err) =object
["transaction_error".=toJSON err]
toJSON (TransactionOutput out)
=object
["warnings".=toJSON(txOutWarnings out)
,"payments".=toJSON(txOutPayments out)
,"state".=toJSON (txOutState out)
,"contract".=toJSON(txOutContract out)
]
instanceFromJSON TransactionOutputwhere
parseJSON =withObject"TransactionOutput"
(λv→
(TransactionError<$>(v.:"transaction_error"))
<|>(TransactionOutput<$>
(TransactionOutputRecord_ext
<$>(v.:"warnings")
<∗>(v.:"payments")
<∗>(v.:"state")
<∗>(v.:"contract")
<∗>pure()
)
)
)
```
Some examples for each _TransactionOutput_ type

**TransactionError**

```
transactionOutputErrorExample::TransactionOutput
transactionOutputErrorExample=TransactionError teUselessTransactionExample
```
is serialized as

```json
{
"transaction_error": {
  "contents": null,
  "tag": "TEUselessTransaction"
  }
}
```

**TransactionOutput**

```
transactionOutputSuccessExample::TransactionOutput
transactionOutputSuccessExample=playTrace
0
Examples.Swap.swapExample
Examples.Swap.happyPathTransactions
```
is serialized as

```json
{
"contract": "close",
"payments": [
{
"amount": 10,
"payment_from": {
"role_token": "Ada Provider"
},
"to": {
"party": {
"role_token": "Dollar Provider"
}
},
"token": {
"currency_symbol": "",
"token_name": ""
}
},
{
"amount": 20,
"payment_from": {
"role_token": "Dollar Provider"
},
"to": {
"party": {
"role_token": "Ada Provider"
}
},
"token": {
"currency_symbol": "85bb65",
"token_name": "dollar"
 }
 }
],
"state": {
"accounts": [],
"boundValues": [],
"choices": [],
"minTime": 1664812900000
},
"warnings": []
}
```

## D.19 Full Contract Example


The Swap Example, defined in section §A.1.2is serialized as

```
{
"timeout": 1664812800000,
"timeout_continuation": "close",
"when": [
{
"case": {
"deposits": 10,
"into_account": {
"role_token": "Ada Provider"
},
"of_token": {
"currency_symbol": "",
"token_name": ""
},
"party": {
"role_token": "Ada Provider"
}
},
"then": {
"timeout": 1664816400000,
"timeout_continuation": "close",
"when": [
{
"case": {
"deposits": 20,
"into_account": {
"role_token": "Dollar Provider"
},
"of_token": {
"currency_symbol": "85bb65",
"token_name": "dollar"
},
"party": {
"role_token": "Dollar Provider"
}
},
"then": {
"from_account": {
"role_token": "Ada Provider"
},
"pay": 10,
"then": {
"from_account": {
"role_token": "Dollar Provider"
},
"pay": 20,
"then": "close",
"to": {
"party": {
"role_token": "Ada Provider"
}
},
"token": {
"currency_symbol": "85bb65",
"token_name": "dollar"
}
},
"to": {
"party": {
"role_token": "Dollar Provider"
}
},
"token": {
"currency_symbol": "",
"token_name": ""
 } } } ] } } ] }
```

## D.20 Parse utils


These are some Aeson utils to help parse a number to the Isabelle exported
_Arithİnt_

```
getInteger::String→Scientific→Parser Arith.Int
getInteger ctx x=case(floatingOrInteger x::Either Double Integer)of
Right a→return$Int_of_integer a
Left →fail$"parsing "++ctx++" failed, expected integer, but encountered floating point"
withInteger::String→JSON.Value→Parser Arith.Int
withInteger ctx=withScientific ctx$getInteger ctx
instanceToJSON Arith.Intwhere
toJSON (Int_of_integer x) =toJSON x
instanceFromJSON Arith.Intwhere
parseJSON (JSON.Number x) =getInteger"Int"x
parseJSON =fail"expecting integer"
```