report.tex 159 KB
Newer Older
Michael Hanus 's avatar
Michael Hanus committed
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
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
% $Id$
% $Log$

%\documentstyle[makeidx,11pt,fleqn]{article}
\documentclass[11pt,fleqn]{article}

\usepackage{latexsym}
\usepackage{makeidx}
\usepackage{amssymb}

\setlength{\textwidth}{16.5cm}
\setlength{\textheight}{23cm}
\renewcommand{\baselinestretch}{1.1}
\setlength{\topmargin}{-1cm}
\setlength{\oddsidemargin}{0cm}
\setlength{\evensidemargin}{0cm}
\setlength{\marginparwidth}{0.0cm}
\setlength{\marginparsep}{0.0cm}

\newlength{\figurewidth}
\setlength{\figurewidth}{\textwidth}
\addtolength{\figurewidth}{-0.4cm}

% environment for typing program texts:
\makeatletter
\newenvironment{prog}{\par\vspace{1.5ex}
\setlength{\parindent}{1.0cm}
\setlength{\parskip}{-0.1ex}
\obeylines\@vobeyspaces\tt}{\vspace{1.5ex}\noindent
}
\makeatother
\newcommand{\startprog}{\begin{prog}}
\newcommand{\stopprog}{\end{prog}\noindent}
\newcommand{\pr}[1]{\mbox{\tt #1}}   % program text in normal text

\newcommand{\Ac}{{\cal{A}}}
\newcommand{\Cc}{{\cal{C}}}
\newcommand{\Dc}{{\cal{D}}}
\newcommand{\Fc}{{\cal{F}}}
\newcommand{\Tc}{{\cal{T}}}
\newcommand{\Xc}{{\cal{X}}}
\newcommand{\var}{{\cal V}ar}
\newcommand{\Dom}{{\cal D}om}
\newcommand{\VRan}{{\cal VR}an}
\renewcommand{\emptyset}{\varnothing}

\newcommand{\ans}{\mathbin{\framebox[1mm]{\rule{0cm}{1.2mm}}}}
%\newcommand{\ans}{\;}
\newcommand{\todo}[1]{\fbox{\sc To do: #1}}
\newcommand{\dexp}{D}  % a disjunctive expression
\newcommand{\cconj}{\ensuremath \mathop{\pr{\&}}} % concurrent conj. in math
\newcommand{\ttbs}{\mbox{\tt\char92}} % backslash in tt font
\newcommand{\ttus}{\mbox{\tt\char95}} % underscore in tt font
\newcommand{\sem}[1]{\ensuremath [\![#1]\!]} % double square brackets
\newcommand{\eval}[1]{{\cal E}\!val\sem{#1}}
\newcommand{\To}{\Rightarrow}
\newcommand{\infrule}[2]{\begin{array}{@{}c@{}} #1 \\ \hline #2 \end{array}}

\newcommand{\pindex}[1]{\index{#1@{\tt #1}}}  % program elements in index
\newcommand{\comment}[1]{} % ignore the argument
\catcode`\_=\active
\let_=\sb
\catcode`\_=12

% commands for the syntax:
\newcommand{\term}[1]{{\tt #1}}
\newcommand{\opt}[1]{\rm [\it #1{\rm ]}}
\newcommand{\offside}[3]{\term{\{}\seq{#1}{#2}{#3} \term{\}}}
\newcommand{\seq}[3]{{\it #1$_1$ \term{#2} $\ldots$ \term{#2} #1$_#3$}}
\newcommand{\head}[1]{\makebox[1.3in][r]{\it #1}}
\newcommand{\por}{\\\head{}\makebox[3em][c]{$|$}}
\newcommand{\next}{\\\head{}\makebox[3em]{}}
\newcommand{\marg}[1]{\hspace{\fill}$(#1)$}
\newcommand{\lexicon}[1]{\production #1 {\rm see lexicon} end}
\def\production #1 #2 end {\head{#1}\makebox[3em]{\rm ::=}\it #2}

\makeindex

\begin{document}
\sloppy

\begin{titlepage}
\begin{center}
\fbox{
\begin{minipage}[t]{\figurewidth}
\begin{center}\vspace{10ex}
{\Huge\bf Curry}\\[4ex]
{\LARGE\bf An Integrated Functional Logic Language}\\[5ex]
{\large\bf Version 0.5.1}\\[1ex]
{\large \today}\\[8ex]
\Large
Michael Hanus$^1$ [editor] \\[3ex]
{\large Additional Contributors:}\\[2ex]
Sergio Antoy$^2$ \\
Herbert Kuchen$^3$ \\
Francisco J. L\'opez-Fraguas$^4$ \\
Juan Jos\'e Moreno Navarro$^5$ \\
Frank Steiner$^6$ \\[20ex]
\normalsize
(1) RWTH Aachen, Germany, {\tt hanus@informatik.rwth-aachen.de} \\
(2) Portland State University, USA, {\tt antoy@cs.pdx.edu} \\
(3) University of M\"unster, Germany, {\tt kuchen@uni-muenster.de} \\
(4) Universidad Complutense de Madrid, Spain, {\tt fraguas@dia.ucm.es} \\
(5) Universidad Polit\'ecnica de Madrid, Spain, {\tt jjmoreno@fi.upm.es} \\
(6) RWTH Aachen, Germany, {\tt steiner@i2.informatik.rwth-aachen.de} \\[5ex]~
\end{center}
\end{minipage}}
\end{center}
\end{titlepage}
\tableofcontents
\newpage


\section{Introduction}

Curry is a universal programming language aiming at the amalgamation
of the most important declarative programming paradigms,
namely functional programming and logic programming.  
Curry combines in a seamless way features from functional programming
(nested expressions, lazy evaluation, higher-order functions),
logic programming (logical variables, partial data structures,
built-in search), and concurrent programming (concurrent evaluation
of constraints with synchronization on logical variables).
Moreover, Curry provides additional features in
comparison to the pure languages (compared to functional programming:
search, computing with partial information; compared to logic
programming: more efficient evaluation due to the deterministic
evaluation of functions).
Moreover, it also amalgamates the most
important operational principles developed in the area of integrated
functional logic languages: ``residuation''\index{residuation}
and ``narrowing''\index{narrowing} (see
\cite{Hanus94JLP} for a survey on functional logic programming).

The development of Curry is an international initiative intended to
provide a common platform for the research, teaching\footnote{%
Actually, Curry has been successfully applied to teach functional and
logic programming techniques in a single course without switching
between different programming languages. More details about
this aspect can be found in \cite{Hanus97DPLE}.}
and application
of integrated functional logic languages.
This document describes the features of Curry, its syntax and
operational semantics.


\section{Programs}

A Curry program specifies the semantics of expressions, where
goals, which occur in logic programming, are particular expressions
(of type \pr{Constraint}).
Executing a Curry program means simplifying an expression until
a value (or solution) is computed.
To distinguish between values and reducible expressions,
Curry has a strict distinction between (data)
\emph{constructors}\index{constructor} and
\emph{operations}\index{operation} or
\emph{defined functions}\index{defined function}\index{function!defined}
on these data.
Hence, a Curry program consists of a set of type and function declarations.
The type declarations define the computational domains (constructors)
and the function declarations the operations on these domains.
Predicates in the logic programming sense can be considered
as constraints, i.e., functions with result type \pr{Constraint}.

Modern functional languages (e.g., Haskell \cite{HudakPeytonJonesWadler92},
SML \cite{MilnerTofteHarper90}) allow the detection of many programming
errors at compile time by the use of polymorphic type systems.
Similar type systems are also used in modern logic languages
(e.g., G\"odel \cite{HillLloyd94}, $\lambda$Prolog \cite{NadathurMiller88}).
Therefore, Curry is a strongly typed language with a
Hindley/Milner-like polymorphic type system \cite{DamasMilner82}.\footnote{%
The extension of this type system to Haskell's type classes
is not included in the kernel language but will be considered
in a future version.}
Each object in a program has a unique type, where
the types of variables and operations can be omitted and are
reconstructed by a type inference mechanism.


\subsection{Datatype Declarations}
\label{sec-datatypes}

A datatype declaration\index{type declaration}\index{datatype!declaration}
\index{declaration!datatype} has the form
\startprog
data $T$ $\alpha_1$ \ldots $\alpha_n$ = $C_1$ $\tau_{11}$ \ldots $\tau_{1n_1}$ | $\cdots$ | $C_k$ $\tau_{k1}$ \ldots $\tau_{kn_k}$
\stopprog
and introduces a new $n$-ary \emph{type constructor}\index{type constructor}
$T$ and $k$
new (data) \emph{constructors} $C_1,\ldots,C_k$, where each $C_i$ has the type
\startprog
$\tau_{i1}$ -> $\cdots$ -> $\tau_{in_i}$ -> $T~\alpha_1\ldots\alpha_n$
\stopprog
($i=1,\ldots,k$). Each $\tau_{ij}$ is a
\emph{type expression}\index{type expression} built from the
\emph{type variables}\index{type variable} $\alpha_1,\ldots,\alpha_n$
and some type constructors. Curry has a number of built-in type constructors,
like \pr{Bool}, \pr{Int}, \pr{->} (function space), or, lists and tuples,
which are described in Section~\ref{sec-builtin-types}.
Since Curry is a higher-order language,
the types of functions (i.e., constructors or operations)
are written in their curried form
\pr{$\tau_1$ -> $\tau_2$ -> $\cdots$ -> $\tau_n$ -> $\tau$}
where $\tau$ is not a functional type.
In this case, $n$ is called the \emph{arity}\index{arity}\index{function!arity}
of the function. 
For instance, the datatype declarations
\startprog
data Bool = True | False
data List a = [] | a : List a
data Tree a = Leaf a | Node (Tree a) a (Tree a)
\stopprog
introduces the datatype \pr{Bool} with the 0-ary constructors
(\emph{constants})\index{constant} \pr{True} and \pr{False},
and the polymorphic types \pr{List a} and \pr{Tree a} of lists
and binary trees. Here, ``\pr{:}'' is an infix operator, i.e.,
``\pr{a:List a}'' is another notation for ``\pr{(:) a (List a)}''.
Lists are predefined in Curry, where the notation ``\pr{[a]}''
is used to denote list types (instead of ``\pr{List a}'').
The usual convenient notations for lists are supported,
i.e., \pr{[0,1,2]} is an abbreviation for
\pr{0:(1:(2:[]))} (see also Section~\ref{sec-builtin-types}).

A \emph{data term}\index{data term} is a variable $x$ or a
constructor application
$c~t_1\ldots t_n$ where $c$ is an $n$-ary constructor and
$t_1,\ldots,t_n$ are data terms. An \emph{expression}\index{expression}
is a variable or a (partial) application $\varphi~e_1 \ldots e_m$
where $\varphi$ is an $n$-ary function or
constructor, $m\leq n$, and $e_1,\ldots,e_m$ are expressions.
A data term or expression is called \emph{ground}\index{ground expression}
\index{expression!ground}\index{ground term}\index{data term!ground}
if it does not contain any variable.
Ground data terms correspond to values in the intended domain,
and expressions containing operations
should be evaluated to data terms.
Note that traditional functional languages compute on ground expressions,
whereas logic languages also allow non-ground expressions.


\subsection{Function Declarations}
\label{sec-funcdecl}

A function is defined by a type declaration (which can be omitted)
followed by a list of defining equations.
A \emph{type declaration}\index{type declaration}\index{function declaration}
\index{declaration!function} for an $n$-ary function has the form
\startprog
$f$ :: $\tau_1$ -> $\tau_2$ -> $\cdots$ -> $\tau_n$ -> $\tau$
\stopprog
where $\tau_1,\ldots,\tau_n,\tau$ are type expressions
and $\tau$ is not a functional type.
The simplest form of a
\emph{defining equation}\index{equation}\index{defining equation}
(or \emph{rule})\index{rule}\index{function rule}\pindex{=}
for an $n$-ary function $f$ ($n \geq 0$) is
\startprog
$f~t_1\ldots t_n$ = $e$
\stopprog
where $t_1,\ldots,t_n$ are data terms and the
\emph{right-hand side}\index{right-hand side} $e$ is an expression.
The \emph{left-hand side}\index{left-hand side}
$f~t_1\ldots t_n$ must not contain multiple occurrences of a variable.
Functions can also be defined by
\emph{conditional equations}\index{conditional equation}%
\index{equation!conditional}\index{conditional rule}\index{rule!conditional}
which have the form
\startprog
$f~t_1\ldots t_n$ | $c$ = $e$
\stopprog
where the \emph{condition}\index{condition} $c$ is a constraint
(cf.\ Section~\ref{sec-equality}). In order to apply a conditional equation,
its condition must be solved. Conditional equations with
identical left-hand sides can be also written as
\startprog
$f~t_1\ldots t_n$ | $c_1$ = $e_1$
          \,$\vdots$
          \,| $c_k$ = $e_k$
\stopprog
which is an abbreviation for the $k$ conditional equations
\startprog
$f~t_1\ldots t_n$ | $c_1$ = $e_1$
$\vdots$
$f~t_1\ldots t_n$ | $c_k$ = $e_k$
\stopprog
%
Note that one can define functions with a non-determinate
behavior by providing several rules with overlapping left-hand sides
or \emph{free variables}\index{free variable}\index{variable!free}
(i.e., variables which do not occur in the left-hand
side) in the conditions or right-hand sides of rules.
For instance, the following non-deterministic function inserts an element
at an arbitrary position in a list:
\startprog
insert x []     = [x]
insert x (y:ys) = x : y : ys
insert x (y:ys) = y : insert x ys
\stopprog
Such \emph{non-deterministic functions}\index{non-deterministic function}
\index{function!non-deterministic}
can be given a perfect declarative semantics \cite{GonzalesEtAl96ESOP}
and their implementation causes no overhead in Curry
since techniques for handling non-determinism are already contained in
the logic programming part (see also \cite{Antoy97ALP}).
However, deterministic functions are advantageous since
they provide for more efficient implementations (like the
\emph{dynamic cut} \cite{LoogenWinkler95}).
If one is interested only in defining deterministic functions,
this can be ensured by the following restrictions:
\begin{enumerate}
\item
Each variable occurring in the right-hand side of a rule
must also occur in the corresponding left-hand side.
\item
If \pr{~$f~t_1\ldots t_n$ | $c$ = $e$~} and
\pr{~$f~t'_1\ldots t'_n$ | $c'$ = $e'$~} are rules defining $f$ and
$\sigma$ is a \emph{substitution}\index{substitution}\footnote{%
A \emph{substitution} $\sigma$ is a mapping from variables into expressions
which is extended to a homomorphism on expressions by defining
$\sigma(f~t_1\ldots t_n) = f~\sigma(t_1)\ldots \sigma(t_n)$.
$\{x_1 \mapsto e_1,\ldots,x_k \mapsto e_k\}$ denotes the substitution
$\sigma$ with $\sigma(x_i)=e_i$ ($i=1,\ldots,k$) and
$\sigma(x)=x$ for all variables $x\not\in\{x_1,\ldots,x_k\}$.}
with $\sigma(t_1\ldots t_n)=\sigma(t'_1\ldots t'_n)$, then at least
one of the following conditions holds:
\begin{enumerate}
\item $\sigma(e)=\sigma(e')$ (\emph{compatibility of right-hand sides}).
\item $\sigma(c)$ and $\sigma(c')$ are not simultaneously satisfiable
(\emph{incompatibility of conditions}). A decidable approximation
of this condition can be found in \cite{KuchenEtAl96NGC}.
\end{enumerate}
\end{enumerate}
These conditions ensure the confluence\index{confluence}
of the rules if they are considered as a conditional term rewriting
system \cite{SuzukiMiddeldorpIda95RTA}.
Implementations of Curry may check these conditions and warn the
user if they are not satisfied. There are also more general
conditions to ensure confluence \cite{SuzukiMiddeldorpIda95RTA}
which can be checked instead of the above conditions.
% Note that we do not require
% the termination of the rewrite system so that the computation with
% infinite data structures is supported as in lazy functional languages.
% In contrast to functional languages,
% \emph{extra variables}\index{extra variables} in the conditions
% (i.e., variables which do not occur in the left-hand
% side) are allowed. These extra variables provide the power of
% logic programming since a search for appropriate values is necessary
% in order to apply a conditional rule with extra variables.

Note that defining \emph{equations of higher-type},
\index{equation!higher-order}\index{higher-order equation}
e.g., \pr{f = g} if \pr{f} and \pr{g} are of type \pr{Bool -> Bool},
are formally excluded in order to define a simple operational semantics based
on first-order rewriting.\footnote{Note that this is also necessary
in an extension of Curry which allows higher-order rewrite rules,
since rules with lambda-abstractions in left-hand sides which are
not of base type may cause a gap between
the standard notion of higher-order rewriting and the corresponding equational
theory \cite{Nipkow91LICS}.}
For convenience, a defining equation \pr{f = g}
between functions is allowed but will be interpreted in Curry
as syntactic sugar for the corresponding defining equation \pr{f x = g x}
on base types.


\subsubsection{Functions vs.\ Variables}
\label{sec-variable-sharing}

In lazy functional languages, different occurrences of the same
variable are shared to avoid multiple evaluations of
identical expressions. For instance, if we apply the rule
\startprog
double x = x+x
\stopprog
to an expression \pr{double$\,t$}, we obtain the new expression
\pr{$t$+$t$} but both occurrences of $t$ denote the identical
expression, i.e., these subterms will be simultaneously evaluated.
Thus, \emph{several occurrences of the same variable are
always shared.} This is  necessary not only for efficiency reasons
but it has also an influence on the soundness of the operational semantics
in the presence of non-deterministic functions
(see also \cite{GonzalesEtAl96ESOP}). For instance,
consider the non-deterministic function \pr{coin} defined by the rules
\startprog
coin = 0
coin = 1
\stopprog
Thus, the expression \pr{coin} evaluates to \pr{0} or \pr{1}.
However, the result values of the expression \pr{(double\,\,coin)}
depend on the sharing of the two occurrences of \pr{coin}
after applying the rule for \pr{double}: if both occurrences
are shared (as in Curry), the results are \pr{0} or \pr{2},
otherwise (without sharing) the results are \pr{0}, \pr{1}, or \pr{2}.
The sharing of argument variables corresponds to the so-called
``call time choice'' in the declarative semantics
\cite{GonzalesEtAl96ESOP}: if a rule is applied to a function call,
a unique value must be assigned to each argument
(in the example above: either \pr{0} or \pr{1} must be assigned
to the occurrence of \pr{coin} when the expression
\pr{(double\,\,coin)} is evaluated). This does not mean that
functions have to be evaluated in a strict manner but this
behavior can be easily obtained by sharing the different occurrences
of a variable.

Since different occurrences of the same variable are always shared
but different occurrences of (textually identical) function
calls are not shared, it is important to distinguish
between variables and functions. Usually, all symbols
occurring at the top-level in the left-hand side of some rule
are considered as functions and the non-constructor symbols
in the arguments of the left-hand sides are considered
as variables. But note that there is a small exception
from this general rule in local declarations
(see Section~\ref{sec-localdecls}).


\subsubsection{Conditional Equations with Boolean Guards}
\label{sec-bool-guards}

For convenience and compatibility with Haskell,
one can also write conditional equations with multiple guards
\startprog
$f~t_1\ldots t_n$ | $b_1$ = $e_1$
          \,$\vdots$
          \,| $b_k$ = $e_k$
\stopprog
where $b_1,\ldots,b_k$ ($k>0$) are expressions of type \pr{Bool}
(instead of \pr{Constraint}).
Such a rule is interpreted as in Haskell: the guards are successively evaluated
and the right-hand side of the first guard which is evaluated to \pr{True}
is the result of applying this equation. Thus, this equation
can be considered as an abbreviation for the rule
\startprog
$f~t_1\ldots t_n$ = if $b_1$ then $e_1$ else
            \,$\vdots$
            \,if $b_k$ then $e_k$ else \mbox{\it{}undefined}
\stopprog
(where {\it undefined} is some non-reducible function).
To write rules with several Boolean guards more nicely,
there is a Boolean function \pr{otherwise}\pindex{otherwise}
which is predefined as \pr{True}. For instance, the factorial
function can be declared as follows:
\startprog
fac n | n==0      = 1
      | otherwise = fac(n-1)*n
\stopprog
Since all guards have type Bool, this definition is equivalent to
(after a simple optimization)
\startprog
fac n = if n==0 then 1 else fac(n-1)*n
\stopprog
%
To avoid confusion, it is not allowed to mix the Haskell-like
notation with Boolean guards and the standard notation
with constraints: in a rule with multiple guards,
all guards must be expressions of either type \pr{Constraint}
or type \pr{Bool} but not a simultaneous mixture of both.
The default type of a guard is \pr{Constraint}, i.e., in a rule like
\startprog
f c x | c = x
\stopprog
the type of the variable \pr{c} is \pr{Constraint}
(provided that it is not restricted to \pr{Bool} by the use of \pr{f}).


\subsection{Local Declarations}
\label{sec-localdecls}

\index{local declaration}\index{declaration!local}
Since not all auxiliary functions should be globally
visible, it is possible to restrict the scope of declared entities.
Note that the scope of parameters in function definitions
is already restricted since the variables occurring in parameters
of the left-hand side are only visible in the corresponding
conditions and right-hand sides. The visibility of other
entities can be restricted using \pr{let} in expressions
or \pr{where} in defining equations.

An expression of the form \pr{let {\it{}decls} in {\it{}exp}}
\index{local declaration}\index{declaration!local}\pindex{let}
introduces a set of local names. The list of local declarations
\emph{decls} can contain function definitions as well as
definitions of constants by pattern matching. The names
introduced in these declarations are visible in the expression
\emph{exp} and the right-hand sides of the declarations in \emph{decls},
i.e., the local declarations can be mutually recursive.
For instance, the expression
\startprog
let a=3*b
    b=6
in 4*a
\stopprog
reduces to the value \pr{72}.

Auxiliary functions which are only introduced to define another
function should often not be visible outside. Therefore,
such functions can be declared in a \pr{where}-clause
\index{local declaration}\index{declaration!local}\pindex{where}
added to the right-hand side of the corresponding function definition.
This is done in the following definition of a fast exponentiation
where the auxiliary functions \pr{even} and \pr{square}
are only visible in the right-hand side of the rule for \pr{exp}:
\startprog
exp b n = if n==0
          then 1
          else if even n then square (exp b (n `div` 2))
                         else b * (exp b (n-1))

       where even n = n `mod` 2 == 0
             square n = n*n
\stopprog
Similarly to \pr{let}, \pr{where}-clauses can contain
mutually recursive function definitions as well as
definitions of constants by pattern matching.
The names declared in the \pr{where}-clauses are only visible
in the corresponding conditions and right-hand sides.
As a further example, the following Curry program implements
the quicksort algorithm with a function \pr{split} which
splits a list into two lists containing the smaller and larger elements:
\startprog
split e []            = ([],[])
split e (x:xs) | e>=x = (x:l,r)
               | e<x  = (l,x:r)
               where
                 (l,r) = split e xs
~
qsort []     = []
qsort (x:xs) = qsort l ++ (x:qsort r)
             where
               (l,r) = split x xs
\stopprog
To distinguish between locally introduced functions and variables
(see also Section~\ref{sec-variable-sharing}),
we define a \emph{local pattern}\index{local pattern}\index{pattern!local}
as a (variable) identifier or an application where the top symbol
is a data constructor. If the left-hand side of a local declaration
in a \pr{let} or \pr{where} is a pattern, then all identifiers
in this pattern that are not data constructors are
considered as variables. For instance, the locally introduced
identifiers \pr{a}, \pr{b}, \pr{l}, and \pr{r} in the previous examples
are variables whereas the identifiers \pr{even} and \pr{square}
denote functions. Note that this rule exclude the
definition of 0-ary local functions since a definition of the form
``\pr{where f = \ldots}'' is considered as the definition of
a local variable \pr{f} by this rule which is usually the intended
interpretation (see previous examples).


\subsection{Free Variables}
\label{sec-freevars}

Since Curry is intended to cover functional as well as logic
programming paradigms, expressions (or constraints, see
Section~\ref{sec-equality}) might contain free
(unbound, uninstantiated) variables.
\index{variable!free}\index{free variable}\index{unbound variable}
The idea is to compute values
for these variables such that the expression is reducible
to a data term or that the constraint is solvable.
For instance, consider the definitions
\startprog
mother John    = Christine
mother Alice   = Christine
mother Andrew  = Alice
\stopprog
Then we can compute a child of \pr{Alice} by solving the
equation (see Section~\ref{sec-equality})
\pr{mother x =:= Alice}. Here, \pr{x} is a free variable
which is instantiated to \pr{Andrew} in order to reduce
the equation's left-hand side to \pr{Alice}.
Similarly, we can compute a grandchild of \pr{Chistine}
by solving the equation \pr{mother (mother x) =:= Christine}
which yields the value \pr{Andrew} for \pr{x}.

In logic programming languages like Prolog, all free variables
are considered as existentially quantified at the top-level.
Thus, they are always implicitly declared at the top-level.
In a language with different nested scopes like Curry,
it is not clear to which scope an undeclared variable belongs
(the exact scope of a variable becomes particularly important
in the presence of search operators, see Section~\ref{sec-encapsearch},
where existential quantifiers and lambda abstractions are often
mixed). Therefore, Curry requires that
\emph{each free variable $x$ must be explicitly declared}
using a declaration \index{declaration!free}\index{free declaration}
\index{variable!declaration}
of the form \pr{$x$ free}.\pindex{free} These declarations
can occur in \pr{where}-clauses or in a \pr{let} enclosing
a constraint. The variable is then introduced as unbound
with the same scoping rules as for all other local
entities (see Section~\ref{sec-localdecls}).
For instance, we can define
\startprog
isgrandmother g | let c free in mother (mother c) =:= g  = True
\stopprog
As a further example, consider the definition of the
concatentation of two lists:
\startprog
append []     ys = ys
append (x:xs) ys = x : append xs ys
\stopprog
Then we can define the function \pr{last} which computes
the last element of a list by the rule
\startprog
last l | append xs [x] =:= l  = x  where x,xs free
\stopprog
Since the variable \pr{xs} occurs in the condition but not in the
right-hand side, the following definition is also possible:
\startprog
last l | let xs free in append xs [x] =:= l  = x  where x free
\stopprog
%
Note that the \pr{free} declarations can be freely mixed with
other local declarations after a \pr{let} or \pr{where}.
The only difference is that a declaration like ``\pr{let x free}''
introduces an existentially quantified variable (and, thus,
it can only occur in front of a constraint) whereas
other \pr{let} declarations introduce local functions or parameters.
Since all local declarations can be mutually recursive,
it is also possible to use local variables in the bodies
of the local functions in one \pr{let} declarations.
For instance, the following expression is valid (where the functions
\pr{h} and \pr{k} are defined elsewhere):
\startprog 
let f x = h x y
    y free
    g z = k y z
in c y (f (g 1))
\stopprog
Similarly to the usual interpretation of local definitions
by lambda lifting \cite{Johnsson85}, this expression
can be interpreted by transforming the local definitions
for \pr{f} and \pr{g} into global ones by adding the non-local
variables of the bodies as parameters:
\startprog 
f y x = h x y
g y z = k y z
\ldots
  let y free in c y (f y (g y 1))
\stopprog



\subsection{Constraints and Equality}
\label{sec-equality}

A condition of a rule is a constraint which must be solved
in order to apply the rule. An elementary constraint\index{constraint} is an
\emph{equational constraint}\index{equational constraint}
\index{constraint!equational}\pindex{=:=}\index{equality!in constraints}
\pr{$e_1$=:=$e_2$} between two expressions
(of base type). \pr{$e_1$=:=$e_2$} is satisfied if both sides are reducible
to a same ground data term. This notion of equality, which is the
only sensible notion of equality in the presence of non-terminating functions
\cite{GiovannettiLeviMoisoPalamidessi91,MorenoRodriguez92}
and also used in functional languages, is also called
\emph{strict equality}.\index{strict equality}\index{equality!strict}
As a consequence, if one side is undefined
(nonterminating), then the strict equality does not hold.
Operationally, an equational constraint $e_1\pr{=:=}e_2$ is solved
by evaluating $e_1$ and $e_2$ to unifiable data terms.
The equational constraint could also be solved in an incremental
way by an interleaved lazy evaluation of the expressions
and binding of variables to constructor terms (see
\cite{LoogenLopezFraguasRodriguezArtalejo93PLILP}
or Section~\ref{sec-equationsolving} in the appendix).

Equational constraints should be distinguished from standard Boolean functions
(cf.\ Section~\ref{sec-builtin-types})
since constraints are checked for satisfiability. For instance,
the equational constraint \pr{[x]=:=[0]} is satisfiable if the variable
\pr{x} is bound to \pr{0}. However, the evaluation of \pr{[x]=:=[0]}
does not deliver a Boolean value \pr{True} or \pr{False},
since the latter value would require a binding of \pr{x}
to all values different from \pr{0} (which could be expressed
if a richer constraint system than substitutions, e.g.,
disequality constraints \cite{ArenasGilLopez94PLILP}, is used).
This is sufficient since, similarly to logic programming,
constraints are only activated in conditions of equations
which must be checked for satisfiability.

Note that the basic kernel of Curry only provides
strict equations $e_1\pr{=:=}e_2$ between expressions
as elementary constraints. Since it is conceptually fairly easy to add
other constraint structures \cite{LopezFraguas92},
extensions of Curry may provide richer constraint systems
to support constraint logic programming applications.

Constraints can be combined into a \emph{conjunction} written as
$c_1 \cconj c_2$.
\index{conjunction}\index{conjunction!concurrent}
\index{conjunction!of constraints}\pindex{\&}
The conjunction is interpreted \emph{concurrently}:
if the combined constraint $c_1 \cconj c_2$ should
be solved, $c_1$ and $c_2$ are solved concurrently.
In particular, if the evaluation of $c_1$ suspends,
the evaluation of $c_2$ can proceed which may cause the reactivation
of $c_1$ at some later time (due to the binding of common variables).
In a sequential implementation, the evaluation of $c_1 \cconj c_2$
could be started by an attempt to solve $c_1$.
If the evaluation of $c_1$ suspends,
an evaluation step is applied to $c_2$.

%Syntactically, constraints are always enclosed
%in curly brackets.\pindex{\{\ldots\}}
%Thus, \pr{\{x = f y, y = h 0\}} denotes the concurrent conjunction
%of the equational constraints \pr{x = f y} and \pr{y = h 0}.
%There is also a predefined infix operator \verb+/\+\index{/\@\verb+/\+}
%for the concurrent conjunction of two constraint
%which can be considered as defined by
%\startprog
%c1 \verb+/\+ c2 = \{c1, c2\}
%\stopprog
%
It is interesting to note that parallel functional computation models
\cite{BreitingerLoogenOrtega95,ChakravartyEtAl98Goffin}
are covered by the use of concurrent constraints.
For instance, a constraint of the form
\startprog
x =:= f t1  \&  y =:= g t2  \&  z =:= h x y
\stopprog
specifies
a potentially concurrent computation of the functions \pr{f}, \pr{g} and
\pr{h} where the function \pr{h} can proceed its computation
only if the arguments have been bound by evaluating the expressions
\pr{f t1} and \pr{g t2}. Since constraints
could be passed as arguments or results of functions
(like any other data object or function), it is possible
to specify general operators to create flexible communication
architectures similarly to Goffin \cite{ChakravartyEtAl98Goffin}.
Thus, the same abstraction facilities could be used for sequential
as well as concurrent programming. On the other hand,
the clear separation between sequential and concurrent computations
(e.g., a program without any occurrences of concurrent conjunctions
is purely sequential)
supports the use of efficient and optimal evaluation strategies
for the sequential parts \cite{Antoy97ALP,AntoyEchahedHanus94POPL},
where similar techniques for the concurrent parts are not available.

 

\subsection{Higher-order Features}

Curry is a higher-order language supporting the common
functional programming techniques by partial function applications
and lambda abstractions. \emph{Function application}\index{function application}
is denoted by juxtaposition
the function and its argument. For instance, the well-known \pr{map}
function is defined in Curry by
\startprog
map :: (a -> b) -> [a] -> [b]
map f []     = []
map f (x:xs) = f x : map f xs
\stopprog
However, there is an important difference w.r.t.{} to functional programming.
Since Curry is also a logic language, it allows logical variables
also for functional values, i.e., it is possible to evaluate the
equation \pr{map f [1 2] =:= [2 3]} which has, for instance, a solution
\pr{\{f=inc\}} if \pr{inc} is the increment function on natural numbers.
In principle, such solutions can be computed by extending (first-order)
unification to \emph{higher-order unification}
\cite{HanusPrehofer96RTA,NadathurMiller88,Prehofer95Diss}.
Since higher-order unification is a computationally expensive
operation, Curry delays the application of unknown functions
until the function becomes known \cite{Ait-KaciLincolnNasr87,Smolka95}.
Thus, the application operation can be considered as a function
(``\pr{@}'' is a left-associative infix operator)
\startprog
(@) :: (a -> b) -> a -> b
f@x = f x
\stopprog
which is ``rigid'' in its first argument (cf.\ Section~\ref{sec-opsem}).

In cases where a function is only used a single time, it is tedious
to assign a name to it. For such cases, anonymous functions
(\emph{$\lambda$-abstractions}\index{lambda-abstraction@$\lambda$-abstraction}),
denoted by
\startprog
\ttbs{}$x_1\ldots{}x_n$->$e$
\stopprog
are provided.


\section{Operational Semantics}
\label{sec-opsem}

Curry's operational semantics is based on the lazy evaluation
of expressions combined with a possible instantiation
of free variables occurring in the expression.
If the expression is ground, the operational model
is similar to lazy functional languages,
otherwise (possibly non-deterministic) variable instantiations
are performed as in logic programming.
If an expression contains free variables, it
may be reduced to different values by binding the
free variables to different expressions. In functional programming,
one is interested in the computed \emph{value}, whereas logic programming
emphasizes the different bindings (\emph{answers}).
Thus, we define for the integrated functional logic language Curry an
\emph{answer expression}\index{answer expression}\index{expression!answer}
as a pair ``$\sigma \ans e$''
consisting of a substitution $\sigma$ (the answer computed so far)
and an expression $e$.
An answer expression $\sigma \ans e$ is \emph{solved} if $e$
is a data term.
Usually, the identity substitution in answer expressions is omitted,
i.e., we write $e$ instead of $\{\} \ans e$ if it is clear from
the context.

Since more than one answer may exist for expressions containing
free variables, in general, initial expressions are reduced to disjunctions
of answer expressions. Thus, a
\emph{disjunctive expression}\index{disjunctive expression}\index{expression!disjunctive}
is a (multi-)set of answer expressions
$\{\sigma_1 \ans e_1,\ldots, \sigma_n \ans e_n\}$.
For the sake of readability, we write concrete disjunctive expressions
like
\[
\{\{\pr{x}=\pr{0},\pr{y}=\pr{2}\} \ans \pr{2}~,~\{\pr{x}=\pr{1},\pr{y}=\pr{2}\} \ans \pr{3}\}
\]
in the form \pr{\{x=0,y=2\}\,2\,|\,\{x=1,y=2\}\,3}.
Thus, substitutions are represented by lists of equations enclosed in
curly brackets, and disjunctions are separated by vertical bars.

A single \emph{computation step}\index{computation step}
performs a reduction in exactly one unsolved
expression of a disjunction (e.g., in the leftmost unsolved
answer expression in Prolog-like implementations).
If the computation step is
deterministic, the expression is reduced to a new one.
If the computation step is non-deterministic,
the expression is replaced by a disjunction of new expressions.
The precise behavior depends on the function calls occurring
in the expression.
For instance, consider the following rules:
\startprog
f 0 = 2
f 1 = 3
\stopprog
The result of evaluating the expression \pr{f 1} is \pr{3}, whereas
the expression \pr{f x} should be evaluated to the disjunctive expression
\startprog
\{x=0\} 2 | \{x=1\} 3 .
\stopprog
To avoid superfluous computation steps and to apply
programming techniques of modern functional languages,
nested expressions are evaluated lazily, i.e., the leftmost outermost
function call is primarily selected in a computation step.
Due to the presence of free variables in expressions, this
function call may have a free variable at an argument position
where a value is demanded by the left-hand sides of the function's rules
(a value is \emph{demanded} by an argument position of the left-hand side
of some rule, if the left-hand side has a constructor at this
position, i.e., in order to apply the rule, the actual value at
this position must be the constructor).
In this situation there are two possibilities to proceed:
\begin{enumerate}
\item
Delay the evaluation of this function call until the corresponding
free variable is bound (this corresponds to the
\emph{residuation}\index{residuation} principle
which is the basis of languages like Escher \cite{Lloyd94ILPS,Lloyd95},
Le Fun \cite{Ait-KaciLincolnNasr87}, Life \cite{Ait-Kaci90},
NUE-Prolog \cite{Naish91}, or Oz \cite{Smolka95}).
In this case, the function is called \emph{rigid}\index{rigid}.
\item
(Non-deterministically) instantiate the free variable to the different values
demanded by the left-hand sides and apply reduction steps using
the different rules (this corresponds to \emph{narrowing}\index{narrowing}
principle which is the basis of languages like
ALF \cite{Hanus90PLILP}, Babel \cite{MorenoRodriguez92},
K-Leaf \cite{GiovannettiLeviMoisoPalamidessi91}, LPG \cite{BertEchahed86},
or SLOG \cite{Fribourg85}).
In this case, the function is called \emph{flexible}\index{flexible}.
\end{enumerate}
Since Curry is an attempt to provide a common platform for different
declarative programming styles and the decision about the
``right'' strategy depends on the definition and
intended meaning of the functions, Curry supports both strategies.
The precise strategy is specified by \emph{evaluation annotations}
\index{evaluation annotation}
for each function.\footnote{%
Evaluation annotations are similar to coroutining
declarations \cite{Naish87Diss} in Prolog where the programmer
specifies conditions under which a literal is ready for
a resolution step.}
The precise operational meaning of evaluation annotations
is defined in Appendix~\ref{app-opsem}.
A function can be annotated as
\emph{rigid}\index{rigid}\index{annotation!rigid}\pindex{rigid} or
\emph{flexible}\index{flexible}\index{annotation!flexible}\pindex{flex}.
If an explicit annotation is not provided by the user, a default strategy
is used: functions with the result type \pr{Constraint}
are flexible and all other functions are rigid.
Functions with a polymorphic result type (like the identity)
are considered as rigid, although they can used as a constraint function
in a particular context.
This default strategy can be changed by providing explicit
evaluation annotations for each function or by pragmas
(see Section~\ref{sec-pragmas}).

For instance, consider the function \pr{f} as defined above.
If \pr{f} has the evaluation annotation
\startprog
f eval flex
\stopprog
(i.e., \pr{f} is flexible),
then the expression \pr{f x} is evaluated by instantiating \pr{x} to
\pr{0} or \pr{1} and applying a reduction step in both cases.
This yields the disjunctive expression
\startprog
\{x=0\} 2 | \{x=1\} 3 .
\stopprog
However, if \pr{f} has the evaluation annotation
\startprog
f eval rigid
\stopprog
(i.e., \pr{f} is rigid),
then the expression \pr{f x} cannot be evaluated since the argument
is a free variable. In order to proceed, we need a ``generator''
for values for \pr{x}, which is the case in the following constraint:
\startprog
f x =:= y  \&  x =:= 1
\stopprog
Here, the first constraint \pr{f x =:= y}
cannot be evaluated and thus suspends,
but the second constraint \pr{x=:=1} is evaluated by binding \pr{x} to \pr{1}.
After this binding, the first constraint can be evaluated and the entire
constraint is solved. Thus, the constraint is solved by the following
steps:
\startprog
f x =:= y  \&  x =:= 1
$\leadsto$ \{x=1\} f 1 =:= y
$\leadsto$ \{x=1\} 3 =:= y
$\leadsto$ \{x=1,y=3\}
\stopprog
(The empty constraint is omitted in the final answer.)


\section{Types}

\subsection{Built-in Types}
\label{sec-builtin-types}

The following types are predefined in Curry:
\begin{description}
\item[Boolean values:]
\pindex{Bool}\pindex{True}\pindex{False}
They are predefined by the datatype declaration
\startprog
data Bool = True | False
\stopprog
The (sequential) conjunction\index{conjunction}\index{conjunction!sequential}
is predefined as the left-associative infix operator \pr{\&\&}\pindex{\&\&}:
\startprog
(\&\&) :: Bool -> Bool -> Bool
True  \&\& x = x
False \&\& x = False
\stopprog
Similarly, the (sequential) disjunction\index{disjunction}
\pr{||}\pindex{"|"|} and the negation\index{negation} \pr{not}\pindex{not}
are defined as usual (see Appendix~\ref{app-prelude}).
Furthermore, the function \pr{otherwise}\pindex{otherwise}
is predefined as \pr{True}
to write rules with multiple guards more nicely.

Boolean values are mainly used in conditionals, i.e., the
conditional function \pr{if_then_else} is predefined as
\startprog
if_then_else :: Bool -> a -> a -> a
if True  then x else y = x
if False then x else y = y
\stopprog
where \pr{if b then x else y} is syntactic sugar for the application
\pr{if_then_else b x y}.

A function with result type \pr{Bool} is often called
a \emph{predicate}\index{predicate} (in contrast to constraints
which have result type \pr{Constraint}, see below).\footnote{Predicates
in the logic programming sense should be considered as constraints
since they are only checked for satisfiability and usually
not reduced to \pr{True} or \pr{False} in contrast to Boolean functions.}
There are a number of built-in predicates for comparing objects,
like the predicate ``\pr{<}'' to compare numbers (e.g., \pr{1<2} evaluates
to \pr{True} and \pr{4<3} evaluates to \pr{False}).
There is also a standard predicate ``\pr{==}''\pindex{==}\index{equality!test}
\index{strict equality}\index{equality!strict}
to test the convertibility of expressions to identical data terms
(``strict equality'', cf.\ Section~\ref{sec-equality}):
$e_1 \pr{==} e_2$ reduces to \pr{True} if
$e_1$ and $e_2$ are reducible to identical ground data terms,
and it reduces to \pr{False} if $e_1$ and $e_2$ are reducible to
different ground data terms. The evaluation of $e_1 \pr{==} e_2$
suspends if one of the arguments is a free variable
(i.e., \pr{==} is a ``rigid'' function, cf.\ Section~\ref{sec-opsem}).
If neither $e_1$ nor $e_2$ is a free variable, $e_1 \pr{==} e_2$
is reduced according to the following rules:
\startprog
C         == C        \,\,= True \hfill\%{\rm for all $0$-ary constructors} C
C $x_1\ldots{}x_n$ == C $y_1\ldots{}y_n$ \,= $x_1\pr{==}y_1$ \&\&\ldots\&\& $x_n\pr{==}y_n$\hfill\%{\rm for all $n$-ary constructors} C
C $x_1\ldots{}x_n$ == D $y_1\ldots{}y_m$ = False \hfill\%{\rm for all different constructors} C {\rm{}and} D
\stopprog
This implements a test for
\emph{strict equality}\index{strict equality}\index{equality!strict}
(cf.\ Section~\ref{sec-equality}).
For instance, the test ``\pr{[0]==[0]}'' reduces to \pr{True},
whereas the test ``\pr{1==0}'' reduces to \pr{False}.
An equality test with a free variable in one side is delayed
in order to avoid an infinite set of solutions for insufficiently
instantiated tests like \pr{x==y}.
Usually, strict equality is only defined on data terms, i.e.,
\pr{==} is not really polymorphic but an overloaded
function symbol. This could be more precisely expressed using
type classes which will be considered in a future version.

Note that \pr{$e_1$==$e_2$} only \emph{tests} the identity of $e_1$ and $e_2$
but never binds one side to the other if it is a free variable.
This is in contrast to solving the equational constraint
\pr{$e_1$=:=$e_2$} which is checked for \emph{satisfiability} and propagates
new variable bindings in order to solve this constraint.
Thus, in terms of concurrent constraint programming languages
\cite{Saraswat93}, \pr{==} and \pr{=:=} correspond to
\emph{ask} and \emph{tell} equality constraints, respectively.


\item[Constraints:]
\pindex{Constraint}\index{constraint}
The type \pr{Constraint} is the type of a constraint which
is used in conditions of defining rules.
A function with result type \pr{Constraint} is often called
a \emph{constraint}\index{constraint}.
Constraints are different from Boolean values: a Boolean expression
reduces to \pr{True} or \pr{False} whereas a constraint is checked
for satisfiability. A constraint is applied (i.e., solved)
in a condition of a conditional equation. The equational constraint
\pr{$e_1$=:=$e_2$} is an elementary constraint which is solvable if
the expressions $e_1$ and $e_2$ are evaluable to unifiable data terms.

Constraints can be combined into a
conjunction\index{conjunction}\index{conjunction!concurrent}
of the form
$c_1 \cconj c_2 \cconj\ldots\cconj c_n$
by applying the concurrent conjunction operator \pr{\&}\pindex{\&}.
In this case, all constraints in the conjunction are
evaluated concurrently.
Constraints can also be evaluated in a sequential order
by the sequential
conjunction operator\index{conjunction}\index{conjunction!sequential}
\pr{\&>}\pindex{\&>}, i.e., the combined constraint
\pr{$c_1$ \&> $c_2$} will be evaluated by first completely evaluating
$c_1$ and then $c_2$.
\pr{success}\pindex{success} denotes an always solvable constraint.

Constraints can be passed as parameters or results of functions
like any other data object. For instance, the following function
takes a list of constraints as input and produces a single constraint,
the conjunction of all constraints of the input list:
\startprog
conj :: [Constraint] -> Constraint
conj []     = success
conj (c:cs) = c \& conj cs
\stopprog

The trivial constraint \pr{success} is usually not shown in answers
to a constraint expression. For instance, the constraint
\pr{x*x=:=y \& x=:=2} is evaluated to the answer
\startprog
\{x=2, y=4\}
\stopprog

\item[Functions:] The type \pr{t1 -> t2}\pindex{->}
is the type of a function
which produces a value of type \pr{t2} for each argument of type \pr{t1}.
A function $f$ is applied to an argument $x$ by writing ``$f~x$''.
The type expression
\startprog
$t_1$ -> $t_2$ -> $\cdots$ -> $t_{n+1}$
\stopprog
is an abbreviation for the type
\startprog
$t_1$ -> ($t_2$ -> ($\cdots$ -> $t_{n+1}$))
\stopprog
and denotes the type of a (currified) $n$-ary function,
i.e., \pr{->} associates to the right.
Similarly, the expression
\startprog
$f$ $e_1$ $e_2$ $\ldots$ $e_n$
\stopprog
is an abbreviation for the expression
\startprog
(\ldots(($f$ $e_1$) $e_2$) $\ldots$ $e_n$)
\stopprog
and denotes the application of a function $f$ to $n$ arguments,
i.e., the application associates to the left.

\item[Integers:] \pindex{Int}
The common integer values, like ``42'' or ``-15'',
are considered as constructors (constants) of type \pr{Int}.
The usual operators on integers, like \pr{+} or \pr{*},
are predefined functions with ``rigid'' arguments,
i.e., are evaluated only if both arguments are integer values,
otherwise such function calls are delayed.
Thus, these functions can be used as ``passive constraints''
which become active after binding their arguments.
For instance, if the constraint \pr{digit} is defined by the
equations
\startprog
digit 0 = success
\ldots
digit 9 = success
\stopprog
then the constraint \pr{ x*x=:=y \& x+x=:=y \& digit x }
is solved by delaying
the two equations which will be activated after binding the variable \pr{x}
to a digit by the constraint \pr{digit x}. Thus, the corresponding
computed solution is
\startprog
\{x=0,y=0\} | \{x=2,y=4\}
\stopprog

\item[Floating point numbers:] \pindex{Float}
Similarly to integers, values like ``3.14159'' or ``5.0e-4'' are
considered as constructors of type \pr{Float}.
Since overloading is not included in the kernel version of Curry,
the names of arithmetic functions on floats are different
from the corresponding functions on integers.

% SA I don't think it would be too hard to fix without classes.
% SA E.g., SML does it well

\item[Lists:] \pindex{[a]}\pindex{[]}\pindex{:}\index{lists}
The type \pr{[t]} denotes all lists whose elements are values of
type \pr{t}. The type of lists can be considered as predefined
by the declaration
\startprog
data [a] = [] | a : [a]
\stopprog
where \pr{[]} denotes the empty list and \pr{x:xs} is the non-empty list
consisting of the first element \pr{x} and the remaining list \pr{xs}.
Since it is common to denote lists with square brackets,
the following convenient notation is supported:
\startprog
[$e_1$,$e_2$,\ldots,$e_n$]
\stopprog
denotes the list \pr{$e_1$:$e_2$:$\cdots$:$e_n$:[]} (which is equivalent
to \pr{$e_1$:($e_2$:($\cdots$:($e_n$:[])\ldots))} since ``\pr{:}''
is a right-associative infix operator).
%The notation
%\startprog
%[$e_1$,$e_2$,\ldots,$e_n$|as]
%\stopprog
%is equivalent to \pr{$e_1$:$e_2$:$\cdots$:$e_n$:as}, i.e., ``\pr{|}''
%introduces the tail of a list.
Note that there is an overloading in the notation \pr{[t]}: if \pr{t}
is a type, \pr{[t]} denotes the type of lists containing elements
of type \pr{t}, where \pr{[t]} denotes a single element list
(with element \pr{t}) if \pr{t} is an expression.
Since there is a strong distinction between occurrences of
types and expressions, this overloading can always be resolved.

For instance, the following predefined functions define
the concatenation of two lists and the application of a function to
all elements in a list:\pindex{++}\pindex{map}
\startprog
(++) :: [a] -> [a] -> [a]
[]     ++ ys = ys
(x:xs) ++ ys = x : xs ++ ys
~
map :: (a -> b)  -> [a] -> [b]
map f []     = []
map f (x:xs) = f x : map f xs
\stopprog

\item[Characters:] \pindex{Char}
Values like \pr{'a'} or \pr{'0'} denote constants of type \pr{Char}.
There are two conversion functions between characters and
their corresponding ASCII values:\pindex{ord}\pindex{chr}
\startprog
ord :: Char -> Int
chr :: Int -> Char
\stopprog

\item[Strings:] \pindex{String}
The type \pr{String} is an abbreviation for \pr{[Char]},
i.e., strings are considered as lists of characters.
String constants are enclosed in double quotes.
Thus, the string constant \pr{"Hello"} is identical
to the character list \pr{['H','e','l','l','o']}.
A term can be converted into a string by the function\pindex{show}
\startprog
show :: a -> String
\stopprog
For instance, the result of \pr{show(42)} is the
character list \pr{['4','2']}.

\item[Tuples:] \index{tuple}\pindex{(\ldots)}
If $t_1, t_2,\ldots,t_n$ are types and $n \geq 2$, then
\pr{($t_1$,$t_2$,\ldots,$t_n$)}
denotes the type of all $n$-tuples.
The elements of type \pr{($t_1$,$t_2$,\ldots,$t_n$)}
are \pr{($x_1$,$x_2$,\ldots,$x_n$)} where $x_i$ is an element
of type $t_i$ ($i=1,\ldots,n$). Thus, for each $n$,
the tuple-building operation
\pr{($\dots$)} can be considered as an $n$-ary constructor
introduced by the pseudo-declaration
\startprog
data ($a_1\pr{,}a_2\pr{,}\ldots\pr{,}a_n$) = ($\cdot$) $a_1$ $a_2$\ldots$a_n$
\stopprog
where \pr{($x_1$,$x_2$,\ldots,$x_n$)} is equivalent to the
constructor application \pr{($\cdot$) $x_1$ $x_2$\ldots$x_n$}.

The unit type\index{unit type}\pindex{()}
\pr{()} has only a single element
\pr{()} and can be considered as defined by
\startprog
data () = ()
\stopprog
Thus, the unit type can also be interpreted as the type of 0-tuples.
\end{description}



\subsection{Type System}

Curry is a strongly typed language with a
Hindley/Milner-like polymorphic type system \cite{DamasMilner82}.\footnote{%
The extension of the type system to Haskell's type classes
is not included in the kernel language but may be considered
in a future version.}
Each variable, constructor and operation has a unique type,
where only the types of constructors have to be declared by
datatype declarations (see Section~\ref{sec-datatypes}).
The types of functions can be declared (see Section~\ref{sec-funcdecl})
but can also be omitted. In the latter case they will be reconstructed
by a type inference mechanism.

Note that Curry is an explicitly typed language, i.e., each function
has a type. The type can only be omitted if the type inferencer
is able to reconstruct it and to insert the missing type declaration.
In particular, the type inferencer can reconstruct only those
types which are visible in the module (cf.\ Section~\ref{sec-modules}).
Each type inferencer of a Curry implementation must be able
to insert the types of the parameters and the free variables
(cf.\ Section~\ref{sec-freevars}) for each rule.
The automatic inference of the types of the defined functions
might require further restrictions depending on the type inference method.
Therefore, the following definition of a well-typed Curry program
assumes that the types of all defined functions are given
(either by the programmer or by the type inferencer).
A Curry implementation must accept a well-typed program
if all types are explicitly provided but should also
support the inference of function types according to \cite{DamasMilner82}.

A \emph{type expression}\index{type expression}\index{type}
is a well-formed expression containing type variables,
basic types like \pr{Bool}, \pr{Constraint}, \pr{Int}, \pr{Float},
\pr{Char}, and type constructors like \pr{[$\cdot$]},
\pr{($\cdot$,$\cdots$,$\cdot$)}, \pr{$\cdot$->$\cdot$}, or introduced by
datatype declarations (cf.\ Section~\ref{sec-datatypes}).
For instance, \pr{[(Int,a)]->a} is a type expression
containing the type variable \pr{a}. A \emph{type scheme}\index{type scheme}
is a type expression with a universal quantification for some
type variables, i.e., it has the form $\forall \alpha_1,\ldots,\alpha_n.\tau$
($n \geq 0$; in case of $n=0$, the type scheme is equivalent to a
type expression). A function type declaration \pr{$f$::$\tau$}
is considered as an assignment of the type scheme
$\forall \alpha_1,\ldots,\alpha_n.\tau$ to $f$, where
$\alpha_1,\ldots,\alpha_n$ are all type variables occurring in $\tau$.
The type $\tau$ is called a
\emph{generic instance}\index{generic instance}\index{type instance}
of the type scheme $\forall \alpha_1,\ldots,\alpha_n.\tau'$
if there is a substitution
$\sigma = \{\alpha_1 \mapsto \tau_1,\ldots,\alpha_n \mapsto \tau_n\}$
on the types with $\sigma(\tau') = \tau$.

The types of all defined functions are collected in a
\emph{type environment}\index{type environment}
$\Ac$ which is a mapping from identifiers to type schemes.
It contains at least the type schemes of the defined functions
and an assignment of types for some local variables.
An expression $e$ is \emph{well-typed}\index{well-typed}
and has type $\tau$ w.r.t.\ a type environment $\Ac$
if $\Ac \vdash e::\tau$ is derivable according to the
following inference rules:
\[
\begin{array}{@{}ll}
\mbox{Axiom:} &
\infrule{}
 {\Ac \vdash x::\tau}
 \mbox{~~if $\tau$ is a generic instance of $\Ac(x)$}\\[3ex]

\mbox{Application:} &
\infrule{\Ac \vdash e_1::\tau_1 \to \tau_2 \quad \Ac \vdash e_2::\tau_1}
 {\Ac \vdash e_1\,e_2::\tau_2}\\[3ex]

\mbox{Abstraction:} &
\infrule{\Ac[x/\tau] \vdash e::\tau'}
 {\Ac \vdash \ttbs{}x\pr{->}e::\tau \to \tau'}
 \mbox{~~if $\tau$ is a type expression}\\[3ex]

\mbox{Existential:} &
\infrule{\Ac[x/\tau] \vdash e::\pr{Constraint}}
 {\Ac \vdash \pr{let\,}x\pr{\,free\,in\,}e::\pr{Constraint}}
 \mbox{~~if $\tau$ is a type expression}\\[3ex]

\mbox{Conditional:} &
\infrule{\Ac \vdash e_1::\pr{Bool} \quad \Ac \vdash e_2::\tau \quad \Ac \vdash e_3::\tau}
 {\Ac \vdash \pr{if\,}e_1\pr{\,then\,}e_2\pr{\,else\,}e_3::\tau}\\[3ex]

\end{array}
\]
A defining equation \pr{~$f~t_1\ldots t_n$ = $e$ $[$where $x$ free$]$~}
is well-typed
w.r.t.\ a type environment $\Ac$ if
$\Ac(f) = \forall \alpha_1,\ldots,\alpha_m.\tau$
[and $\Ac(x)$ is a type expression] and
$\Ac \vdash \ttbs{}t_1\pr{->}\cdots \ttbs{}t_n\pr{->}e :: \tau$
is derivable according to the above inference rules.
A conditional equation \pr{~$l$ | $c$ = $r$}
is considered (for the purpose of typing) as syntactic sugar
for \pr{~$l$ = cond $c$ $r$~}
where \pr{cond} is a new function with type scheme
$\Ac(\pr{cond}) = \forall \alpha.\pr{Constraint}\to \alpha \to \alpha$.

A \emph{program is well-typed}\index{well-typed}
if all its rules are well-typed with a unique assignment of
type schemes to defined functions.

Note that the following recursive definition is a well-typed
Curry program according to the definition above (and the type definitions
given in the prelude, cf.\ Appendix~\ref{app-prelude}):
\startprog
f :: [a] -> [a]
f x = if length x == 0 then fst (g x x) else x
~
g :: [a] -> [b] -> ([a],[b])
g x y = (f x, f y)
~
h :: ([Int],[Bool])
h = g [3,4] [True,False]
\stopprog
However, if the type declaration for \pr{g} is omitted,
the usual type inference algorithms are not able to
infer this type.


\section{Modules}
\label{sec-modules}
%The design of a module system for Curry is not influenced
%by the functional logic features of Curry. Therefore, the current
%version of Curry uses a standard module system similar
%to ALF's \cite{Hanus90PLILP} or G\"odel's \cite{HillLloyd94}.
%The extension to a more sophisticated module system
%such as SML's \cite{MilnerTofteHarper90} is a topic for
%future extensions.

A \emph{module}\index{module} defines a collection of datatypes, constructors
and functions which we call \emph{entities}\index{entity} in the following.
A module exports some of its entities which can be imported
and used by other modules. An entity which is not exported
is not accessible from other modules.

A Curry \emph{program}\index{program} is a collection of modules.
There is one main module which is loaded into a Curry system.
The modules imported by the main module are implicitly loaded
but not visible to the user. After loading the main module,
the user can evaluate expressions which contain entities
exported by the main module.

% SA in practice there is the important notion of compilation unit
% Until there are modules, this should be clarified

There is one distinguished module, named \pr{prelude}\index{prelude},
which is implicitly imported into all programs
(see also Appendix~\ref{app-prelude}).
Thus, the entities defined in the prelude (basic functions
for arithmetic, list processing etc.) can be always used.

A module always starts with the head which contains at least
the name of the module followed by the keyword
\pr{where}\pindex{module}\pindex{where}, like
\startprog
module stack where \ldots
\stopprog
If a program does not contain a module head, the \emph{standard module head}
``\pr{module main where}'' is implicitly inserted.
\emph{Module names}\index{module name}\index{name!of a module}
can be given a hierarchical structure by inserting dots
which is useful if larger applications should be structured
into different subprojects, e.g.,
\startprog
company.project1.subproject2.mod4
\stopprog
is a valid module name.
The dots may reflect the hierarchical file structure
where modules are stored. For instance, the module
\pr{compiler.symboltable} could be stored in the file
\pr{symboltable.cur} in the directory \pr{compiler}.

Without any further restrictions in the module head,
all entities which are directly accessible (see below) in the module
(roughly speaking, all entities defined or imported in the module)
are exported.
In order to restrict the set of exported entities of a module,
an \emph{export list}\index{entity!exported}
\index{export declaration}\index{declaration!export}
can be added to the module head.
For instance, a module with the head
\startprog
module stack(stackType, push, pop, newStack) where \ldots
\stopprog
exports the entities \pr{stackType}, \pr{push}, \pr{pop},
and \pr{newStack}. An export list can contain the following entries:
\begin{enumerate}
\item
\emph{Names of datatypes}: This exports only the datatype, whose name
must be directly accessible in this module,
\emph{but not} the constructors of the datatype.
The export of a datatype
without its constructors allows the definition of
\emph{abstract datatypes}\index{abstract datatype}\index{datatype!abstract}.

\item
\emph{Datatypes with constructors}: If the export list contains
the entry \pr{t(c$_1$,\ldots,c$_n$)}, then \pr{t} must be a datatype
directly accessible in the module and \pr{c$_1$},\ldots,\pr{c$_n$}
are directly accessible constructors of this datatype. In this case,
the datatype \pr{t} and the constructors \pr{c$_1$},\ldots,\pr{c$_n$}
are exported by this module.

\item
\emph{Datatypes with all constructors}: If the export list contains
the entry \pr{t(..)}, then \pr{t} must be a datatype
whose name is directly accessible in the module. In this case,
the datatype \pr{t} and all constructors of this datatype,
which must be also directly accessible in this module, are exported.

\item
\emph{Names of functions}: This exports the corresponding functions
whose names must be directly accessible
in this module. The types occurring in the
argument and result type of this function are implicitly exported,
otherwise the function may not be applicable outside this module.

\item
\emph{Modules}: The set of all directly accessible
entities imported from a module $m$
into the current module (see below)
can be exported by a single entry ``\pr{module $m$}''
in the export list. For instance, if the head of the module \pr{stack}
is defined as above, the module head
\startprog
module queue(module stack, enqueue, dequeue) where \ldots
\stopprog
specifies that the module \pr{queue} exports the entities
\pr{stackType}, \pr{push}, \pr{pop}, \pr{newStack}, \pr{enqueue},
and \pr{dequeue}.

If the exported entities from imported modules should be further
restricted, one can also add an export list to the exported module.
This list can contain names of datatypes
and functions imported from this module. If a datatype which is imported
from another module is exported, the datatype is exported in the same way
(i.e., with or without constructors) how it is imported
into the current module. Thus, a further specification
for the exported constructors is not necessary.
For instance, the module head
\startprog
module queue(module stack(stackType,newStack), enqueue, dequeue) \ldots
\stopprog
specifies that the module \pr{queue} exports the entities
\pr{stackType} and \pr{newStack}, which are imported from \pr{stack},
and \pr{enqueue} and \pr{dequeue}, which are defined in \pr{queue}.
\end{enumerate}
%
All entities defined by top-level declarations in a module are
\emph{directly accessible}\index{directly accessible}\index{accessible!directly}
in this module. Additionally, the
entities exported by another module can be also made directly accessible
in the module by an \pr{import}%
\pindex{index}\index{entity!imported}%
\index{import declaration}\index{declaration!import}
declaration. An import declaration
consists of the name of the imported module and (optionally)
a list of entities imported from that module. If the list
of imported entities is omitted, all entities exported by
that module are imported. For instance,
the import declaration
\startprog
import stack
\stopprog
imports all entities exported by the module \pr{stack},
whereas the declaration
\startprog
import family(father, grandfather)
\stopprog
imports only the entities \pr{father} and \pr{grandfather}
from the module \pr{family}, provided that they are exported
by \pr{family}.

The names of all imported entities are directly accessible in the current
module, i.e., they are equivalent to top-level declarations,
provided that their names are not in conflict with other names.
For instance, if a function \pr{f} is imported from module \pr{m}
but the current module contains a top-level declaration for \pr{f}
(which is thus directly accessible in the current module),
the imported function is not directly accessible.
Similarly, if two identical names are imported from different modules,
none of these entities is directly accessible.
It is possible to access imported but not directly accessible
names by prefixing them with the module identifier.
For instance, consider the module \pr{m1} defined by
\startprog
module m1 where
f :: Int -> Int
\ldots
\stopprog
and the module \pr{m2} defined by
\startprog
module m2 where
f :: Int -> Int
\ldots
\stopprog
together with the main module
\startprog
module main where
import m1
import m2
\ldots
\stopprog
Then the names of the imported functions \pr{f} are not directly accessible
in the main module but one can refer by the qualified identifiers
\pr{m1.f} or \pr{m2.f}
to the corresponding imported entities. Note that export
declarations only allow unqualified names to be exported
(which is the reason for the direct accessibility condition
for the exports). This prevents hierarchical qualifications like
\pr{mod1.mod2.f} and supports the view to consider a module
as a single collection of related entities.

Another method to resolve name conflicts between imported
entities is the renaming of imported entities.
For instance, the name conflict between \pr{m1.f} and \pr{m2.f}
can be resolved by
the following imports:
\startprog
module main where
import m1
import m2 renaming f to m2_f
\ldots
\stopprog
Thus, the entity \pr{f} exported by module \pr{m2} is imported
with the name \pr{m2_f}.
In the subsequent body of this module, the (directly accessible)
name \pr{f} refers to the entity exported by module \pr{m1}
and the (directly accessible) name
\pr{m2_f} refers to the entity \pr{f} exported by module \pr{m2}.
Only imported entities can be renamed, i.e., the import declaration
\startprog
import m(f) renaming g to mg
\stopprog
will always cause an error.

Although each name refers to exactly one entity, it is possible
that the same entity is referred by different names.
For instance, consider the module \pr{m} defined by
\startprog
module m(f) where
f :: Int -> Int
\ldots
\stopprog
and the module \pr{use_m} defined by
\startprog
module use_m(f) where
import m
\stopprog
together with the main module
\startprog
module main where
import m renaming f to mf
import use_m
\ldots
\stopprog
Then the names \pr{mf} as well as \pr{f} refers in the main module
to the same function defined in module \pr{m}.

The import dependencies between modules must be \emph{non-circular},
i.e., it is not allowed that module $m_1$ imports module $m_2$
and module $m_2$ also imports (directly or indirectly)
module $m_1$.




\section{Input/Output}

Curry provides a declarative model of I/O by considering
I/O operations as transformations on the outside world.
In order to avoid dealing with different versions of the
outside world, it must be ensured that at each point of
a computation only one version of the world is accessible.
This is guaranteed by using the monadic I/O approach
\cite{PeytonJonesWadler93POPL} of Haskell and by
requiring that I/O operations are not allowed in program
parts where non-deterministic search is possible.

In the monadic I/O\index{monadic I/O} approach,
the outside ``world'' is not directly
accessible but only through actions which change the world.
Thus, the world is encapsulated in an abstract datatype
which provides actions to change the world.
The type of such actions is \pr{IO t}\pindex{IO}
which is an abbreviation for
\startprog
World -> (t,World)
\stopprog
where \pr{World} denotes the type of all states of the outside world.
If an action of type \pr{IO t} is applied to a particular world,
it yields a value of type \pr{t} and a new (changed) world.
For instance, \pr{getChar} is an action which reads a character
from the standard input when it is executed, i.e., applied to a world.
Therefore, \pr{getChar} has the type \pr{IO Char}.
The important point is that values of type \pr{World} are
not accessible for the programmer --- she/he can only create
and compose actions on the world. Thus, a program intended to perform I/O
operations has a sequence of actions as the result. These actions
are computed and executed when the program is connected to the world
by executing it. For instance,
\pindex{getChar}\pindex{getLine}
\startprog
getChar :: IO Char
getLine :: IO String
\stopprog
are actions which read the next character or the next line from
the standard input. The functions
\startprog
putChar  :: Char -> IO ()
putStr   :: String -> IO ()
putStrLn :: String -> IO ()
\stopprog
\pindex{putChar}\pindex{putStr}\pindex{putStrLn}
take a character (string) and produces an action which, when
applied to a world, puts this character (string) to the standard output
(and a line-feed in case of \pr{putStrLn}.


Since an interactive program consists of a sequence of I/O operations,
where the order in the sequence is important, there are two
operations to compose actions in a particular order.
The function \pindex{>>}
\startprog
(>>) :: IO a -> IO b -> IO b
\stopprog
takes two actions as input and yields an action as output.
The output action consists of performing the first action followed
be the second action, where the produced value of the first action is
ignored. If the value of the first action should be taken into account
before performing the second action, the actions can be composed by
the function
\pindex{>>=}
\startprog
(>>=) :: IO a -> (a -> IO b) -> IO b
\stopprog
where the second argument is a function taking the value produced
by the first action as input and performs another action.
For instance, the action
\startprog
getLine >>= putStrLn
\stopprog
is of type \pr{IO ()} and copies, when executed, a line
from standard input to standard output.

The \pr{return}\pindex{return} function
\startprog
return :: a -> IO a
\stopprog
is sometimes useful to terminate a sequence of actions and return
a result of an I/O operation. Thus, \pr{return v} is an action which
does not change the world and returns the value \pr{v}.

To execute an action, it must be the main expression
in a program, i.e., interactive programs have type \pr{IO ()}.
Since the world cannot be copied (note that the world contains at least
the complete file system), non-determinism in relation with
I/O operations must be avoided. Thus, the applied action must always
be known, i.e., \pr{>>} and \pr{>>=} are rigid in their arguments.
Moreover, it is a runtime error if a disjunctive expression
(cf.\ Section~\ref{sec-opsem})
\pr{$\sigma_1 \ans e_1$ |$\cdots$|  $\sigma_n \ans e_n$},
where the $e_i$'s are of type \pr{IO ()}, occurs as the top-level
expression of a program, since it is unclear in this case
which of the disjunctive actions should be applied to the current
world. Thus, all possible search must be encapsulated between I/O operations
(see Section~\ref{sec-encapsearch}).

Using the evaluation annotations, a compiler is able to
detect functions where search is definitely avoided
(e.g., if all evaluated positions are declared as \pr{rigid}).
Thus, the compiler may warn the user about non-deterministic
computations which may occur in I/O actions so that the programmer
can encapsulate them.



\section{Encapsulated Search}
\label{sec-encapsearch}

Global search, possibly implemented by backtracking,
must be avoided in some situations (user-control of
efficiency, concurrent computations, non-backtrackable I/O).
Hence it is sometimes necessary to encapsulate search,
i.e., non-deterministic computations in parts of larger programs.
Non-deterministic computations might occur in Curry
whenever a function must be evaluated with a free variable at
a flexible argument position. In this case, the computation must follow
different branches with different bindings applied to
the current expression which has the effect that the entire
expression is split into (at least) two independent disjunctions.
To give the programmer control
on the actions taken in this situation, Curry provides 
a primitive search operator which is the basis to implement
sophisticated search strategies.
This section sketches the idea of encapsulating search in Curry
and describes some predefined search strategies.


\subsection{Search Goals and Search Operators}

Since search is used to find solutions to some constraint,
search is always initiated by a constraint containing a
\emph{search variable}\index{variable!search}\index{search variable}
for which a solution should be
computed.\footnote{The generalization to more than
one search variable is straightforward by the use of tuples.}
Since the search variable may be bound to different solutions
in different disjunctive branches, it must be abstracted.
Therefore, a \emph{search goal}\index{search goal} has the type
\pr{a->Constraint} where \pr{a} is the type of the values
which we are searching for. In particular, if $c$ is a constraint
containing a variable $x$ and we are interested in solutions
for $x$, i.e., values for $x$ such that $c$ is satisfied,
then the corresponding search goal has the form \pr{\ttbs$x$->$c$}.
However, any other expression of the same type can also be used
as a search goal.

To control the search performed to find solutions to search goals,
Curry has a predefined operator\pindex{try}
\startprog
try :: (a->Constraint) -> [a->Constraint]
\stopprog
which takes a search goal and produces a list of search goals.
The search operator \pr{try} attempts to evaluate
the search goal until the computation finishes or
performs a non-deterministic splitting. In the latter case,
the computation is stopped and the different search goals
caused by this splitting are returned as the result list.
Thus, an expression of the form \pr{try \ttbs$x$->$c$}
can have three possible results:
\begin{enumerate}
\item
An empty list. This indicates that the search goal \pr{\ttbs$x$->$c$}
has no solution. For instance, the expression
\startprog
try \ttbs{}x -> 1=:=2
\stopprog
reduces to \pr{[]}.

\item
A list containing a single element. In this case, the search goal
\pr{\ttbs$x$->$c$} has a single solution represented by the element
of this list. For instance, the expression
\startprog
try \ttbs{}x->[x]=:=[0]
\stopprog
reduces to \pr{[\ttbs{}x->x=:=0]}.
Note that a solution, i.e., a binding for the search variable
like the substitution $\{\pr{x}\mapsto\pr{0}\}$,
can always be presented by an equational constraint
like \pr{x=:=0}.

Generally, a one-element list as a result of \pr{try}
has always the form \pr{[\ttbs$x$->$x$=:=$e$]}
(plus some local variables, see next subsection)
where $e$ is fully evaluated, i.e., $e$ does not contain defined functions.
Otherwise, this goal might not be solvable due to the definition
of equational constraints.

\item
A list containing more than one element. In this case, the evaluation
of the search goal \pr{\ttbs$x$->$c$} requires a non-deterministic
computation step. The different alternatives immediately after
this non-deterministic step are represented as elements of this list.
For instance, if the function \pr{f} is defined by
\startprog
f eval flex
f a = c
f b = d
\stopprog
then the expression
\startprog
try \ttbs{}x -> f x =:= d
\stopprog
reduces to the list
\pr{[\ttbs{}x->x=:=a \& f a =:= d, \ttbs{}x->x=:=b \& f b =:= d]}.
This example also shows why the search variable must be abstracted:
the alternative bindings cannot be actually performed
(since a free variable is only bound to at most one value
in each computation thread) but are represented as
equational constraints in the search goal.
\end{enumerate}
Note that the search goals of the list in the last example
are not further evaluated.
This provides the possibility to determine the behavior
for non-deterministic computations. For instance,
the following function defines a depth-first search operator
which collects all solutions of a search goal in a list:
\startprog
all :: (a->Constraint) -> [a->Constraint]
all g = collect (try g)
      where collect []         = []
            collect [g]        = [g]
            collect (g1:g2:gs) = concat (map all (g1:g2:gs))
\stopprog
(\pr{concat} concatenates a list of lists to a single list).
For instance, if \pr{append} is the list concatenation
(and defined as flexible), then the expression
\startprog
all \ttbs{}l -> append l [1] =:= [0,1]
\stopprog
reduces to \pr{[\ttbs{}l->l=:=[0]]}.

The value computed for the search variable in a search goal
can be easily accessed by applying it to a free variable.
For instance, the evaluation of the applicative expression
\startprog
(all \ttbs{}l->append l [1] =:= [0,1]) =:= [g] \& g x
\stopprog
binds the variable \pr{g} to the search goal
\pr{[\ttbs{}l->l=:=[0]]} and the variable \pr{x} to the value \pr{[0]}
(due to solving the constraint \pr{x=:=[0]}).
Based on this idea, there is a predefined function
\startprog
findall :: (a->Constraint) -> [a]
\stopprog
which takes a search goal and collects all solutions
(computed by a depth-first search like \pr{all}) for the search variable into
a list.

Due to the laziness of the language, search goals with infinitely
many solutions cause no problems if one is interested only in finitely
many solutions. For instance, a function which computes only the
first solution w.r.t.\ a depth-first search strategy can be defined by
\startprog
first g = head (findall g)
\stopprog
Note that \pr{first} is a partial function, i.e., it is undefined
if \pr{g} has no solution.


\subsection{Local Variables}
\label{sec-localvars}

Some care is necessary if free variables occur in the search goal,
like in the goal
\startprog
\ttbs{}l2 -> append l1 l2 =:= [0,1]
\stopprog
Here, only the variable \pr{l2} is abstracted in the search goal
but \pr{l1} is free. Since non-deterministic bindings
cannot be performed during encapsulated search,
\emph{free variables are never bound inside encapsulated search}.
Thus, if it is necessary to bind a free variable in order to
proceed an encapsulated search operation, the computation suspends.
For instance, the expression
\startprog
first \ttbs{}l2 -> append l1 l2 =:= [0,1]
\stopprog
cannot be evaluated and will be suspended until the variable \pr{l1}
is bound by another part of the computation. Thus, the constraint
\startprog
s =:= (first \ttbs{}l2->append l1 l2 =:= [0,1])  \&  l1 =:= [0]
\stopprog
can be evaluated since the free variable \pr{l1} in the search goal
is bound to \pr{[0]}, i.e., this constraint reduces to the answer
\startprog
\{l1=[0], s=[1]\}
\stopprog
In some cases it is reasonable to have unbound variables
in search goals, but these variables should be treated as local,
i.e., they might have different bindings in different branches
of the search. For instance, if we want to compute
the last element of the list \pr{[3,4,5]}
based on \pr{append}, we could try to solve
the search goal
\startprog
\ttbs{}e -> append l [e] =:= [3,4,5]
\stopprog
However, the evaluation of this goal suspends due to the necessary
binding of the free variable \pr{l}.
This can be avoided by declaring the variable \pr{l}
as \emph{local} to the constraint by the use of \pr{let}
(see Section~\ref{sec-freevars}), like in the following expression:
\startprog
first \ttbs{}e -> let l free in append l [e] =:= [3,4,5]
\stopprog
Due to the local declaration of the variable \pr{l}
(which corresponds logically to an
existential quantification), the variable \pr{l} is only visible
inside the constraint and, therefore, it can be bound to
different values in different branches. Hence this expression
evaluates to the result \pr{5}.

In order to ensure that an encapsulated search will not be
suspended due to necessary bindings of free variables,
the search goal should be a closed expression when a search operator is
applied to it, i.e., the search variable is bound by the
lambda abstraction and all other free variables are existentially
quantified by local declarations.


\subsection{Predefined Search Operators}
\label{sec-predef-search}

There are a number of search operators which are predefined
in the prelude. All these operators are based on the primitive
\pr{try} as described above.
It is also possible to define other search strategies
in a similar way. Thus, the \pr{try} operator is a
a powerful primitive to define appropriate search strategies.
In the following, we list the predefined search operators.

\begin{description}
\item[\pr{all :: (a->Constraint) -> [a->Constraint]}]~\\
Compute all solutions for a search goal via a depth-first search
strategy. If there is no solution and the search space is finite,
the result is the empty list, otherwise the list contains solved
search goals (i.e., without defined operations).

\item[\pr{once :: (a->Constraint) -> (a->Constraint)}]~\\
Compute the first solution for a search goal via a depth-first search
strategy. Note that \pr{once} is partially defined, i.e.,
if there is no solution and the search space is finite,
the result is undefined.

\item[\pr{findall :: (a->Constraint) -> [a]}]~\\
Compute all solutions for a search goal via a depth-first search
strategy and unpack the solution's values for the search variable
into a list.

\item[\pr{best :: (a->Constraint) -> (a->a->Bool) -> [a->Constraint]}]~\\
Compute the best solution via a depth-first search strategy, according to
a specified relation (the second argument)
that can always take a decision which of two solutions is better
(the relation should deliver \pr{True} if the first argument
is a better solution than the second argument).
This operator is only used for a test, i.e.,
it should be a rigid function.

As a trivial example, consider the relation \pr{shorter} defined by
\startprog
shorter l1 l2 = length l1 <= length l2
\stopprog
Then the expression
\startprog
best (\ttbs{}x -> let y free in append x y =:= [1,2,3]) shorter
\stopprog
computes the shortest list which can be obtained by splitting the
list \pr{[1,2,3]} into this list and some other list, i.e.,
it reduces to \pr{[\ttbs{}x->x=:=[]]}. Similarly, the expression
\startprog
best (\ttbs{}x -> let y free in append x y =:= [1,2,3])
     (\ttbs{}l1 l2 -> length l1 > length l2)
\stopprog
reduces to \pr{[\ttbs{}x->x=:=[1,2,3]]}.

\item[\pr{one :: (a->Constraint) -> [a->Constraint]}]~\\
Compute one solution via a fair strategy.
If there is no solution and the search space is finite,
the result is the empty list, otherwise the list contains one element
(the first solution represented as a search goal).

\item[\pr{condSearch :: (a->Constraint) -> (a->Bool) -> [a->Constraint]}]~\\
Compute the first solution (via a fair strategy)
that meets a specified condition.
The condition (second argument) must be a unary Boolean function.

\item[\pr{browse :: (a->Constraint) -> IO ()}]~\\
Show the solution of a \emph{solved} constraint on the standard output,
i.e., a call \pr{browse g}, where \pr{g} is a solved search goal,
is evaluated to an I/O action which prints the solution.
If \pr{g} is not an abstraction of a solved constraint,
a call \pr{browse g} produces a runtime error.

\item[\pr{browseList :: [a->Constraint] -> IO ()}]~\\
Similar to \pr{browse} but shows a list of solutions on the  standard output.
The \pr{browse} operators are useful for testing search
strategies. For instance, the evaluation of the expression
\startprog
browseList (all \ttbs{}x -> let y free in append x y =:= [0,1,2])
\stopprog
produces the following result on the standard output:
\startprog
[]
[0]
[0,1]
[0,1,2]
\stopprog
Due to the laziness of the evaluation strategy, one can also
browse the solutions of a goal with infinitely many solutions
which are printed on the standard output until the process is stopped.
\end{description}


\subsection{Choice}

In order to provide a ``don't care'' selection of an element
out of different alternatives, which is necessary in concurrent
computation structures to support many-to-one communication,
Curry provides the special evaluation annotation\index{evaluation annotation}
\pr{choice}\pindex{choice}\index{committed choice}:
\startprog
$f$ eval choice
\stopprog
Intuitively, a function $f$ declared as a \pr{choice} function
behaves as follows. A call to $f$ is evaluated as usual
(with a fair evaluation of disjunctive alternatives, which is
important here!) but with the following differences:
\begin{enumerate}
\item No free variable of this function call is bound
(i.e., this function call and all its subcalls are considered as rigid).
\item If one rule for $f$ matches and its guard is entailed
(i.e., satisfiable without binding goal variables), all other
alternatives for evaluating this call are ignored.
\end{enumerate}
Th