aboutsummaryrefslogtreecommitdiff
path: root/backendC/CleanCompilerSources/statesgen.c
blob: 9b28178d857cf3858b68f977724282779aa5c216 (plain) (blame)
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
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
/*
	 (Concurrent) Clean Compiler:  Generation of states
	 ==================================================

	This module generates the (internal) states that are used during the code generation.

	Authors: Sjaak Smetsers & John van Groningen
	At:		 University of Nijmegen, department of computing science
	Version: 1.1
*/

#pragma segment statesgen

#include "compiledefines.h"
#include "types.t"
#include "system.h"
#include "settings.h"
#include "syntaxtr.t"
#include "comsupport.h"
#include "buildtree.h"
#include "codegen_types.h"
#include "codegen1.h"
#include "codegen2.h"
#include "statesgen.h"
#include "checker.h"
#include "instructions.h"
#include "optimisations.h"
#include "sa.h"

/* #include "dbprint.h" */

#define UNBOX_STRICT_CONSTRUCTOR_RECORD
#define UNBOX_STRICT_RECORD_WITH_ONE_FIELD
#define FASTER_STRICT_IF /* also in codegen2.c */
#define FASTER_STRICT_AND_OR
#define UNTUPLE_STRICT_TUPLES /* also in optimisations.c */

#define for_l(v,l,n) for(v=(l);v!=NULL;v=v->n)
#define for_la(v1,v2,l1,l2,n1) for(v1=(l1),v2=(l2);v1!=NULL;v1=v1->n1,++v2)
#define for_li(v,i,l,n) for(v=(l),i=0;v!=NULL;v=v->n,++i)
#define for_ll(v1,v2,l1,l2,n1,n2) for(v1=(l1),v2=(l2);v1!=NULL;v1=v1->n1,v2=v2->n2)
#define for_lla(v1,v2,v3,l1,l2,a3,n1,n2) for(v1=(l1),v2=(l2),v3=(a3);v1!=NULL;v1=v1->n1,v2=v2->n2,++v3)

static void error_in_function (char *m)
{
	ErrorInCompiler ("statesgen.c",m,"");
}

static void error_in_function_s (char *m,char *s)
{
	ErrorInCompiler ("statesgen.c",m,s);
}

static char *Elhsannots	  = "annotations are not allowed at this position";
static char *ECodeBlock	  = "missing type specification";
static char *Wtypeannot	  = "only strict annotations in a type rule will be regarded";
static char *Wparannot	  = "parallel annotations ignored in sequential mode";
static char *Wrootannot	  = "root annotations are ignored";
/*
static char *Econflict     = "conflicting parallel annotations";
static char *WEorIoverrule   = "strict annotation overruled by parallel annotation";
static char *WStrictoverrule = "parallel annotation overruled by strict annotation";
static char *Wapplannot = "parallel annotations on implicitly defined nodeids are ignored";
static char *Wnonterm	  = "non-terminating rule specified";
static char *Wunkannot       = "unknown annotation";
*/

/* some routines for setting the state fields of an object */

StateS BasicSymbolStates [Nr_Of_Predef_Types];

int FirstStateIsStricter (StateS offered_state,StateS demanded_state)
{
	if (offered_state.state_type==SimpleState){
		if (IsSimpleState (demanded_state)){
			switch (offered_state.state_kind){
				case OnB:
				case StrictOnA:
				case StrictRedirection:
					return 1;
				case OnA:
				case SemiStrict:
				case LazyRedirection:
				case Parallel:
				case UnderEval:
					return demanded_state.state_kind==OnA;
				default:
					error_in_function ("FirstStateIsStricter");
					return 0;
			}
		} else {
			return 0;
		}
	} else {
		if (IsSimpleState (demanded_state))
			return 1;
		else if (offered_state.state_type==ArrayState && demanded_state.state_type==ArrayState)
			return 1;
		else if (offered_state.state_type==RecordState && demanded_state.state_type==RecordState)
			return 1;
		else
			return 0;
	}
}

#define BETWEEN(l,h,v) ((unsigned)((v)-(l)) <= (unsigned)((h)-(l)))

int FieldArgumentNodeStatesAreStricter (ArgS *offered_args,ArgS *field_args,States record_states)
{
	ArgS *offered_arg,*field_arg;
	
	for_ll (offered_arg,field_arg,offered_args,field_args,arg_next,arg_next){
		int node_kind;
		Node arg_node;
		int field_number;
		
		field_number=field_arg->arg_node->node_symbol->symb_def->sdef_sel_field_number;

		arg_node=offered_arg->arg_node;
		
		node_kind=arg_node->node_kind;
		if (node_kind!=NodeIdNode){
			if (node_kind==NormalNode && (BETWEEN (int_denot,real_denot,arg_node->node_symbol->symb_kind) || arg_node->node_symbol->symb_kind==string_denot))
				;
			else
				if (!FirstStateIsStricter (arg_node->node_state,record_states[field_number]))
					return 0;		
		} else
			if (!FirstStateIsStricter (arg_node->node_node_id->nid_state,record_states[field_number]))
				return 0;
	}

	return 1;
}

void SetUnaryState (StateS *state_p,StateKind kind,ObjectKind object)
{
	state_p->state_arity = 1;
	state_p->state_kind = kind;
	state_p->state_object = object;
	state_p->state_type = SimpleState;
	state_p->state_mark = 0;
}

static void SetTupleState (StateS *state_p,States args,int arity)
{
	state_p->state_arity = arity;
	state_p->state_tuple_arguments = args;
	state_p->state_type = TupleState;
	state_p->state_mark = 0;
}

static void SetUpdateableTupleState (StateS *state_p,States args,int arity)
{
	state_p->state_arity = arity;
	state_p->state_tuple_arguments = args;
	state_p->state_type = TupleState;
	state_p->state_mark = STATE_ELEMENTS_UPDATEABLE_MASK;
}

static void SetRecordState (StateS *state_p,SymbDef record_sdef,int arity)
{
	RecordStateDescr recdesc;
	
	recdesc = (RecordStateDescr) CompAlloc (sizeof (struct record_state_descr) + (arity-1)*sizeof (struct state));
	recdesc->rs_symb = record_sdef;

	state_p->state_arity = arity;
	state_p->state_record_desc = recdesc;
	state_p->state_type = RecordState;
	state_p->state_mark = 0;
}

static void SetUnboxedArrayState (StateS *state_p,States arg)
{
	state_p->state_arity = 0;
	state_p->state_array_arguments = arg;
	state_p->state_type = ArrayState ;
	state_p->state_mark = 0;
}

StateS LazyState,StrictState; 

#define NewArrayOfStates(n)	(States) CompAlloc (sizeof (StateS)*(n))

static States NewArrayOfUnaryStates (int arity, StateKind init)
{
	int	i;
	States argstates;

	argstates = NewArrayOfStates (arity);

	for (i=0; i<arity; i++)
		SetUnaryState (&argstates [i], init, UnknownObj);

	return argstates;
}

void ConvertAnnotationToState (Annotation annot,StateS *state_p)
{
	if (annot==NoAnnot)
		return;
	else if (annot==StrictAnnot){
		*state_p=StrictState;
		return;
	} else {
		SetUnaryState (state_p, DoParallel ? Parallel : OnA, UnknownObj);
		if (DoParallel)
			state_p->state_mark |= STATE_PARALLEL_MASK;
		return;
	}
}

#if 0
# include "dbprint.h"
extern File rules_file;
#endif

static void GenRecordState (SymbDef sdef);

void ConvertTypeToState (TypeNode type,StateS *state_p,StateKind kind)
{
	Symbol symbol;

	symbol=type->type_node_symbol;

	if (symbol->symb_kind < Nr_Of_Predef_Types){
		*state_p = BasicSymbolStates [symbol->symb_kind];
		if (kind!=StrictOnA)
			state_p->state_kind=kind;
	} else if (symbol->symb_kind==definition && symbol->symb_def->sdef_kind==RECORDTYPE){
		if (kind==StrictOnA){
			GenRecordState (symbol->symb_def);
#if BOXED_RECORDS
			if (symbol->symb_def->sdef_boxed_record)
				SetUnaryState (state_p,StrictOnA,RecordObj);
			else
#endif
				*state_p=symbol->symb_def->sdef_record_state;
		} else
			SetUnaryState (state_p,kind,RecordObj);
	} else
#if ABSTRACT_OBJECT
	if (symbol->symb_kind==definition && symbol->symb_def->sdef_kind==ABSTYPE)
		SetUnaryState (state_p,kind,AbstractObj);
	else
#endif
		SetUnaryState (state_p,kind,UnknownObj);

#ifdef REUSE_UNIQUE_NODES
	if (type->type_node_attribute==UniqueAttr || (symbol->symb_kind==definition && 
			(symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE) &&
			symbol->symb_def->sdef_type->type_lhs->ft_attribute==UniqueAttr))
	{
		state_p->state_mark |= STATE_UNIQUE_MASK;
	}

	if ((state_p->state_mark & STATE_UNIQUE_MASK) && state_p->state_type==SimpleState){
		if (symbol->symb_kind==list_type || symbol->symb_kind==tuple_type || 
			(symbol->symb_kind==definition && (symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE)))
		{
			unsigned long unq_type_args;
			TypeArgs type_arg;
			int i;
			
			unq_type_args=0;
			for_li (type_arg,i,type->type_node_arguments,type_arg_next)
				if (type_arg->type_arg_node->type_node_attribute==UniqueAttr)
					unq_type_args |= 1<<i;
			
			if (unq_type_args!=0){
				state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
				state_p->state_unq_type_args = unq_type_args;
			}
		}
	}
#endif

	if (kind==StrictOnA && IsSimpleState (*state_p)){
		ObjectKind obj_kind;
		
		obj_kind=state_p->state_object;
		if (obj_kind==TupleObj){
			int i; 
			TypeArgs arg;
			
			SetTupleState (state_p, NewArrayOfStates (type->type_node_arity), type->type_node_arity);

			for_li (arg,i,type->type_node_arguments,type_arg_next){
				TypeNode arg_type_node;
				
				arg_type_node=arg->type_arg_node;

				if (!arg_type_node->type_node_is_var){
					ConvertTypeToState (arg_type_node,&state_p->state_tuple_arguments[i],arg_type_node->type_node_annotation==NoAnnot ? OnA : StrictOnA);
#ifdef UNTUPLE_STRICT_TUPLES_
					arg_type_node->type_node_state=state_p->state_tuple_arguments[i];
#endif
				} else
					state_p->state_tuple_arguments[i] = arg_type_node->type_node_annotation==NoAnnot ? LazyState : StrictState;
			}
		} else if (obj_kind==UnboxedArrayObj || obj_kind==StrictArrayObj || obj_kind==ArrayObj){
			TypeNode element_node;
				
			element_node=type->type_node_arguments->type_arg_node;
			
			state_p->state_arity			= 1;
			state_p->state_array_arguments	= NewArrayOfStates (1);
			state_p->state_type				= ArrayState;
			state_p->state_mark=0;

			switch (obj_kind){
				case ArrayObj:
					SetUnaryState (& state_p->state_array_arguments [0], OnA, UnknownObj);
					break;
				case StrictArrayObj:
					state_p->state_array_arguments [0] = StrictState;
					break;
				case UnboxedArrayObj:
					if (element_node->type_node_is_var)
						state_p->state_array_arguments [0] = StrictState;
					else
						ConvertTypeToState (element_node,&state_p->state_array_arguments [0],StrictOnA);
					state_p->state_mark |= STATE_UNBOXED_ARRAY_MASK;
					break;
			}			
		}
	}
}

static void GenRecordState (SymbDef sdef)
{
	if (sdef->sdef_checkstatus < ConvertingToState){
		Types rectype;
		FieldList fields;
		States fieldstates;
		int i,oldline;
		Symbol oldsymbol;
		FlatType lhs;
		int strict_record;

		rectype = sdef->sdef_type;
		lhs = rectype->type_lhs;
		
		oldline		= CurrentLine;
		oldsymbol	= CurrentSymbol;
		
		CurrentSymbol	= lhs->ft_symbol;
		CurrentLine		= rectype->type_line;

		sdef->sdef_checkstatus = ConvertingToState; /* to detect cyclic strict field dependencies */
		SetRecordState (&sdef->sdef_record_state, sdef, sdef->sdef_cons_arity);
		fieldstates=sdef->sdef_record_state.state_record_arguments;

		strict_record=0;
		
		for_li (fields,i,rectype->type_fields,fl_next){
			TypeNode field_type_node;

			field_type_node = fields->fl_type;

			if (field_type_node->type_node_annotation==StrictAnnot){
				strict_record=1;
				if (!field_type_node->type_node_is_var){
					ConvertTypeToState (field_type_node,&fields->fl_state,StrictOnA);
					
					if (fields->fl_state.state_type==RecordState
#ifdef UNBOX_STRICT_RECORD_WITH_ONE_FIELD
						&& !(fields->fl_state.state_arity==1)
#endif
						)
					{
						SetUnaryState (&fieldstates[i], StrictOnA, RecordObj);
#ifdef REUSE_UNIQUE_NODES
						if (field_type_node->type_node_attribute==UniqueAttr)
							fieldstates[i].state_mark |= STATE_UNIQUE_MASK;
#endif	
					} else
						fieldstates[i]=fields->fl_state;
				} else
					fieldstates[i]=fields->fl_state=field_type_node->type_node_annotation==NoAnnot ? LazyState : StrictState;
#ifdef REUSE_UNIQUE_NODES
				if (field_type_node->type_node_attribute==UniqueAttr){
					fieldstates[i].state_mark |= STATE_UNIQUE_MASK;
					fields->fl_state.state_mark |= STATE_UNIQUE_MASK;
				}
#endif	
			} else {
				fieldstates[i] = LazyState;
#ifdef REUSE_UNIQUE_NODES
				if (field_type_node->type_node_attribute==UniqueAttr)
					fieldstates[i].state_mark |= STATE_UNIQUE_MASK;
#endif	
			}

			fields->fl_symbol->symb_def->sdef_sel_field = fields;
		}

		sdef->sdef_strict_constructor=strict_record;
		sdef->sdef_checkstatus = ConvertedToState; /* to detect cyclic strict field dependencies */
				
		CurrentSymbol = oldsymbol;
		CurrentLine = oldline;
	} else if (sdef->sdef_checkstatus == ConvertedToState)
		return;
	else
		StaticMessage (True, "%S", "%s cyclic strict field dependencies are not allowed", CurrentSymbol, sdef->sdef_ident->ident_name);
}

static void GenResultStatesOfLazyFields (SymbDef sdef)
{
	FieldList fields;
	Types rectype;
	int i;

	rectype = sdef->sdef_type;
	
	CurrentSymbol = rectype->type_lhs->ft_symbol;
	CurrentLine = rectype->type_line;
	
	for (i=0, fields = rectype->type_fields; fields; i++, fields = fields->fl_next){
		TypeNode field_type_node = fields->fl_type;

		if (field_type_node->type_node_annotation!=StrictAnnot){
			if (field_type_node->type_node_is_var || field_type_node->type_node_symbol->symb_kind==apply_symb)
				SetUnaryState (&fields->fl_state, LazyRedirection, UnknownObj);
			else
				ConvertTypeToState (field_type_node,&fields->fl_state,StrictOnA);
		}
	}
}

static void ChangeFieldRecordStateForStrictAbsTypeFields (SymbDef icl_sdef,SymbDef dcl_sdef)
{
	Types icl_type;
	FieldList icl_field;
	StateP icl_fieldstate_p,dcl_fieldstate_p;

	icl_type = icl_sdef->sdef_type;

	CurrentSymbol = icl_type->type_lhs->ft_symbol;
	CurrentLine = icl_type->type_line;

	icl_fieldstate_p=icl_sdef->sdef_record_state.state_record_arguments;
	dcl_fieldstate_p=dcl_sdef->sdef_record_state.state_record_arguments;

	for_l (icl_field,icl_type->type_fields,fl_next){
		if (dcl_fieldstate_p->state_type==SimpleState &&
			(icl_fieldstate_p->state_type!=SimpleState ||
			icl_fieldstate_p->state_kind!=dcl_fieldstate_p->state_kind))
		{
			StaticMessage (False, "%S", "%S strict field is boxed because the field type is an abstract type",
				CurrentSymbol, icl_field->fl_symbol);
			
			*icl_fieldstate_p=*dcl_fieldstate_p;
		}
		
		++icl_fieldstate_p;
		++dcl_fieldstate_p;
	}
}

static void GenerateStatesForConstructors (SymbDef sdef)
{
	ConstructorList constructor;

	CurrentLine	 = sdef->sdef_type->type_line;

	for_l (constructor,sdef->sdef_type->type_constructors,cl_next){
		int strict_constructor;
		SymbDef constructor_sdef;
		TypeNode type_node;
		TypeArgs arg;
		StateP state_p;

		type_node=constructor->cl_constructor;
		CurrentSymbol=type_node->type_node_symbol;

		constructor_sdef=CurrentSymbol->symb_def;

		state_p = NewArrayOfStates (constructor_sdef->sdef_arity);
		constructor->cl_state_p = state_p;		

		constructor_sdef->sdef_constructor=constructor;

		strict_constructor=0;
	
		for_l (arg,type_node->type_node_arguments,type_arg_next){
			TypeNode type_arg_node;
			
			type_arg_node=arg->type_arg_node;
			
			if (type_arg_node->type_node_annotation==StrictAnnot){	
				strict_constructor=1;
				
				if (!type_arg_node->type_node_is_var){
					ConvertTypeToState (type_arg_node,state_p,StrictOnA);
					
					if (state_p->state_type==RecordState)
#ifdef UNBOX_STRICT_CONSTRUCTOR_RECORD
					if (type_node->type_node_arguments->type_arg_next!=NULL)
#endif
#ifdef UNBOX_STRICT_RECORD_WITH_ONE_FIELD
					if (!(state_p->state_arity==1))
#endif
						SetUnaryState (state_p, StrictOnA, RecordObj);
				} else {
					*state_p=StrictState;
				}
			} else
				*state_p=LazyState;

			++state_p;
		}
		
		constructor_sdef->sdef_strict_constructor=strict_constructor;
	}
}

static void ChangeElementStateForStrictAbsTypeFields (SymbDef icl_sdef,SymbDef dcl_sdef)
{
	Types icl_type = icl_sdef->sdef_type, dcl_type = dcl_sdef->sdef_type;
	ConstructorList icl_cons, dcl_cons;

	CurrentLine = icl_type->type_line;

	for (icl_cons = icl_type->type_constructors, dcl_cons = dcl_type->type_constructors; dcl_cons;
		 icl_cons = icl_cons->cl_next, dcl_cons = dcl_cons->cl_next)
	{
		TypeNode icl_node,dcl_node;
		SymbDef icl_constructor,dcl_constructor;
		
		icl_node=icl_cons->cl_constructor;
		icl_constructor=icl_node->type_node_symbol->symb_def;
		
		if (icl_constructor->sdef_strict_constructor){
			TypeArgs icl_arg,dcl_arg;
			StateP icl_arg_state_p,dcl_arg_state_p;
			
			dcl_node=dcl_cons->cl_constructor;
			CurrentSymbol=dcl_node->type_node_symbol;
			dcl_constructor=CurrentSymbol->symb_def;
					
			icl_arg=icl_node->type_node_arguments;
			dcl_arg=dcl_node->type_node_arguments;
			icl_arg_state_p=icl_cons->cl_state_p;
			dcl_arg_state_p=dcl_cons->cl_state_p;
			
			while (icl_arg!=NULL){
				TypeNode icl_element_node,dcl_element_node;
				
				icl_element_node=icl_arg->type_arg_node;
				dcl_element_node=dcl_arg->type_arg_node;

				if (dcl_arg_state_p->state_type==SimpleState &&
					(icl_arg_state_p->state_type!=SimpleState || icl_arg_state_p->state_kind!=dcl_arg_state_p->state_kind))
				{
					StaticMessage (False, "%S", "%S element is boxed because its type is an abstract type",
						CurrentSymbol, icl_element_node->type_node_symbol);
					
					*icl_arg_state_p=*dcl_arg_state_p;
				}
				
				icl_arg=icl_arg->type_arg_next;
				dcl_arg=dcl_arg->type_arg_next;
				++icl_arg_state_p;
				++dcl_arg_state_p;
			}
		}				
	}
}

#ifdef CLEAN2
SymbDefP special_types[2];
#endif

void GenerateStatesForRecords (Symbol symbols)
{
	Symbol symb;
	
	for_l (symb,symbols,symb_next)
		if (symb->symb_kind==definition){
			SymbDef def;

			def=symb->symb_def;
			if (def->sdef_kind==RECORDTYPE){
				GenRecordState (def);
				GenResultStatesOfLazyFields (def);

				if (def->sdef_exported){
					SymbDef dcl_sdef;
					
					dcl_sdef=def->sdef_dcl_icl;

					if (dcl_sdef!=NULL && dcl_sdef->sdef_kind==RECORDTYPE){
						GenRecordState (dcl_sdef);
						GenResultStatesOfLazyFields (dcl_sdef);
						ChangeFieldRecordStateForStrictAbsTypeFields (def,dcl_sdef);
					}
				}
			} else if (def->sdef_kind==TYPE){
				GenerateStatesForConstructors (def);
				
				if (def->sdef_exported){
					SymbDef dcl_sdef;
					
					dcl_sdef=def->sdef_dcl_icl;
					
					if (dcl_sdef->sdef_kind==TYPE){
						GenerateStatesForConstructors (dcl_sdef);
						ChangeElementStateForStrictAbsTypeFields (def,dcl_sdef);
					}
				}
			}
		}

#ifdef CLEAN2
	if (special_types[0]!=NULL)
		BasicSymbolStates[integer_denot] = special_types[0]->sdef_record_state;
#endif
}

/*
static Bool AnnotHasDeferAttr (Annotation annotkind)
{
	switch (annotkind){
		case InterleavedAnnot:
		case LazyInterleavedAnnot:
		case ContinueAnnot:
		case DeferAnnot:
		case WaitAnnot:
		case ContInterleavedAnnot:
		case InterleavedNFAnnot:
			return True;
		default:
			return False;
	}
}
*/	

static StateS DetermineStatesOfRuleType (TypeAlts ruletype,StateS *const function_state_p)
{
	TypeNode lhsroot;
	TypeArgs type_arg;
	StateP arg_state_p;

	lhsroot = ruletype->type_alt_lhs;
	
	CurrentSymbol	= lhsroot ->type_node_symbol;
	CurrentLine = ruletype->type_alt_line;
	
	if (lhsroot->type_node_annotation!=NoAnnot)
		StaticMessage (False, "%S", Wrootannot, CurrentSymbol);

	arg_state_p=function_state_p;
	for_l (type_arg,lhsroot->type_node_arguments,type_arg_next){
		if (!(type_arg->type_arg_node->type_node_annotation==NoAnnot || type_arg->type_arg_node->type_node_annotation==StrictAnnot))
			StaticMessage (False, "%S", Wtypeannot, CurrentSymbol);
	
		if (!type_arg->type_arg_node->type_node_is_var)
			ConvertTypeToState (type_arg->type_arg_node,arg_state_p,type_arg->type_arg_node->type_node_annotation==NoAnnot ? OnA : StrictOnA);
		else
			*arg_state_p = type_arg->type_arg_node->type_node_annotation==NoAnnot ? LazyState : StrictState;
		
		++arg_state_p;
	}

	if (ruletype->type_alt_rhs->type_node_is_var || ruletype->type_alt_rhs->type_node_symbol->symb_kind==apply_symb){
		function_state_p[-1] = StrictState;
		function_state_p[-1].state_kind = StrictRedirection;
	} else
		ConvertTypeToState (ruletype->type_alt_rhs,&function_state_p[-1],StrictOnA);

	return function_state_p[-1];
}

typedef struct type_node *TypeNodeP;

#ifdef REUSE_UNIQUE_NODES
# ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static void determine_unique_state_of_constructor_argument (StateP result_state_p,StateP type_state_p,TypeNodeP type_arg_node,int lhs_type_attribute)
{
	if (type_arg_node->type_node_is_var){
		if ((type_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
			type_arg_node->type_node_tv->tv_argument_nr>=0 &&
			(type_state_p->state_unq_type_args & (1<<(type_arg_node->type_node_tv->tv_argument_nr))))
		{
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
		}
	} else {
		AttributeKind arg_type_attribute;
		
		arg_type_attribute=type_arg_node->type_node_attribute;
		
		if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute))
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
		
		if ((result_state_p->state_mark & STATE_UNIQUE_MASK) && result_state_p->state_type==SimpleState){
			Symbol symbol;
			
			symbol=type_arg_node->type_node_symbol;

			if (symbol->symb_kind==list_type || symbol->symb_kind==tuple_type || (symbol->symb_kind==definition && 
				(symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE)))
			{
				unsigned long unq_type_args;
				TypeArgs type_arg;
				int i;
				
				unq_type_args=0;
				for_li (type_arg,i,type_arg_node->type_node_arguments,type_arg_next){
					TypeNodeP type_arg_node_p;
					
					type_arg_node_p=type_arg->type_arg_node;
					if (type_arg_node_p->type_node_is_var){
						if ((type_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
							type_arg_node_p->type_node_tv->tv_argument_nr>=0 &&
							(type_state_p->state_unq_type_args & (1<<(type_arg_node_p->type_node_tv->tv_argument_nr))))
						{
							unq_type_args |= 1<<i;
						}
					} else {
						AttributeKind arg_type_attribute;

						arg_type_attribute=type_arg_node_p->type_node_attribute;
						if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute))
							unq_type_args |= 1<<i;
					}
				}
				
				if (unq_type_args!=0){
					result_state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
					result_state_p->state_unq_type_args = unq_type_args;
				}
			}
		}
	}
}
# else
static StateP determine_unique_state_of_constructor_argument (StateP type_state_p,TypeNodeP type_arg_node,int lhs_type_attribute)
{	
	if (type_arg_node->type_node_is_var){
		if ((type_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
			type_arg_node->type_node_tv->tv_argument_nr>=0 &&
			(type_state_p->state_unq_type_args & (1<<(type_arg_node->type_node_tv->tv_argument_nr))) &&
			(type_state_p->state_mark & STATE_UNIQUE_MASK)==0)
		{
			StateP result_state_p;
			
			result_state_p=CompAllocType (StateS);
			*result_state_p=*type_state_p;
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
			
			return result_state_p;
		} else
			return type_state_p;
	} else {
		AttributeKind arg_type_attribute;
		StateP result_state_p;
		
		arg_type_attribute=type_arg_node->type_node_attribute;
		
		if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute)){
			result_state_p=CompAllocType (StateS);
			*result_state_p=*type_state_p;
			result_state_p->state_mark |= STATE_UNIQUE_MASK;
			
			type_state_p=result_state_p;
		} else
			result_state_p=NULL;
		
		if ((type_state_p->state_mark & STATE_UNIQUE_MASK) && type_state_p->state_type==SimpleState){
			Symbol symbol;
			
			symbol=type_arg_node->type_node_symbol;

			if (symbol->symb_kind==list_type || symbol->symb_kind==tuple_type || (symbol->symb_kind==definition && 
				(symbol->symb_def->sdef_kind==TYPE || symbol->symb_def->sdef_kind==RECORDTYPE)))
			{
				unsigned long unq_type_args;
				TypeArgs type_arg;
				int i;
				
				unq_type_args=0;
				for_li (type_arg,i,type_arg_node->type_node_arguments,type_arg_next){
					TypeNodeP type_arg_node_p;
					
					type_arg_node_p=type_arg->type_arg_node;
					if (type_arg_node_p->type_node_is_var){
						if ((type_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) &&
							type_arg_node_p->type_node_tv->tv_argument_nr>=0 &&
							(type_state_p->state_unq_type_args & (1<<(type_arg_node_p->type_node_tv->tv_argument_nr))))
						{
							unq_type_args |= 1<<i;
						}
					} else {
						AttributeKind arg_type_attribute;

						arg_type_attribute=type_arg_node_p->type_node_attribute;
						if (arg_type_attribute==UniqueAttr || (arg_type_attribute>=FirstUniVarNumber && arg_type_attribute==lhs_type_attribute))
							unq_type_args |= 1<<i;
					}
				}
				
				if (unq_type_args!=0){
					if (result_state_p==NULL){
						result_state_p=CompAllocType (StateS);
						*result_state_p=*type_state_p;
					}
					result_state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
					result_state_p->state_unq_type_args = unq_type_args;
					
					return result_state_p;
				}
			}
		}
		
		return type_state_p;
	}
}
# endif
#endif

#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static void GenStatesInLhsSubArguments (Node argnode,StateS states[]);
static void GenStatesInLhsSubArg (Node argnode,StateP arg_state_p);

static void GenStatesInLhsNode (Node node,StateP arg_state_p)
{
	Args arg;

	if (node->node_annotation!=NoAnnot)
		StaticMessage (True, "%S", "%S %s",CurrentSymbol,node->node_symbol, Elhsannots);

	if (node->node_kind==NormalNode){
		Symbol symbol;
		
		symbol=node->node_symbol;
		if (symbol->symb_kind==definition){
			SymbDef sdef;

			sdef=symbol->symb_def;

			if (sdef->sdef_kind==CONSTRUCTOR){
# ifdef REUSE_UNIQUE_NODES
				AttributeKind lhs_type_attribute;
				
				lhs_type_attribute=sdef->sdef_type->type_lhs->ft_attribute;

				if (lhs_type_attribute==UniqueAttr)
					arg_state_p->state_mark |= STATE_UNIQUE_MASK;
				
				if (sdef->sdef_arity==node->node_arity && (arg_state_p->state_mark & STATE_UNIQUE_MASK)){
					if (sdef->sdef_strict_constructor){
						struct type_arg *type_arg_p;
						StateP constructor_arg_state_p;
						ArgS *arg;
						
						for_lla (arg,type_arg_p,constructor_arg_state_p,
								node->node_arguments,sdef->sdef_constructor->cl_constructor->type_node_arguments,symbol->symb_def->sdef_constructor->cl_state_p,
								arg_next,type_arg_next)
						{
							Node arg_node;
							
							arg->arg_state = *constructor_arg_state_p;
							
							determine_unique_state_of_constructor_argument (&arg->arg_state,arg_state_p,type_arg_p->type_arg_node,lhs_type_attribute);
														
							arg_node=arg->arg_node;
							
							if (arg_node->node_kind==NodeIdNode){
								arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
								arg_node=arg_node->node_node_id->nid_node;
								if (!arg_node)
									continue;
							}

							GenStatesInLhsSubArg (arg_node,&arg->arg_state);
						}							
					} else {
						struct type_arg *type_arg_p;
						ArgS *arg;
						
						for_ll (arg,type_arg_p,node->node_arguments,sdef->sdef_constructor->cl_constructor->type_node_arguments,arg_next,type_arg_next){
							Node arg_node;
							
							arg->arg_state=LazyState;							
							
							determine_unique_state_of_constructor_argument (&arg->arg_state,arg_state_p,type_arg_p->type_arg_node,lhs_type_attribute);

							arg_node=arg->arg_node;

							if (arg_node->node_kind==NodeIdNode){
								arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
								arg_node=arg_node->node_node_id->nid_node;
								if (!arg_node)
									continue;
							}
							
							GenStatesInLhsNode (arg_node,&arg->arg_state);
						}						
					}
					return;
				}
# endif				
				if (sdef->sdef_strict_constructor && sdef->sdef_arity==node->node_arity){
					GenStatesInLhsSubArguments (node,symbol->symb_def->sdef_constructor->cl_state_p);
					return;
				}
			} else if (sdef->sdef_kind==RECORDTYPE){
# ifdef REUSE_UNIQUE_NODES
				AttributeKind lhs_type_attribute;
				
				lhs_type_attribute=sdef->sdef_type->type_lhs->ft_attribute;

				if (lhs_type_attribute==UniqueAttr)
					arg_state_p->state_mark |= STATE_UNIQUE_MASK;
				
				if (arg_state_p->state_mark & STATE_UNIQUE_MASK){
					StateP arg_state_p;
					ArgS *arg;
					FieldList field;

					for_lla (arg,field,arg_state_p,node->node_arguments,
							 sdef->sdef_type->type_fields,sdef->sdef_record_state.state_record_arguments,arg_next,fl_next)
					{
						Node arg_node;
						
						arg->arg_state = *arg_state_p;

						determine_unique_state_of_constructor_argument (&arg->arg_state,arg_state_p,field->fl_type,lhs_type_attribute);
						
						arg_node=arg->arg_node;

						if (arg_node->node_kind==NodeIdNode){
							arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
							arg_node=arg_node->node_node_id->nid_node;
							if (!arg_node)
								continue;
						}

						GenStatesInLhsSubArg (arg_node,&arg->arg_state);
					}							
				} else				
# endif
				GenStatesInLhsSubArguments (node,sdef->sdef_record_state.state_record_arguments);
				return;
			}
		}
# ifdef REUSE_UNIQUE_NODES
		else if (symbol->symb_kind==cons_symb && (arg_state_p->state_mark & STATE_UNIQUE_MASK) && node->node_arity==2){
			Node arg_node;

			arg=node->node_arguments;
			
			arg->arg_state=LazyState;
			if ((arg_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (arg_state_p->state_unq_type_args & 1)){
				arg->arg_state.state_mark |= STATE_UNIQUE_MASK;
			}

			arg_node=arg->arg_node;
			if (arg_node->node_kind==NodeIdNode){
				arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
				arg_node=arg_node->node_node_id->nid_node;
			}
			if (arg_node!=NULL)
				GenStatesInLhsNode (arg_node,&arg->arg_state);

			arg=arg->arg_next;
			
			arg->arg_state=LazyState;
			arg->arg_state.state_mark |= STATE_UNIQUE_MASK;
			if ((arg_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (arg_state_p->state_unq_type_args & 1)){
				arg->arg_state.state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
				arg->arg_state.state_unq_type_args = 1;
			}
			
			arg_node=arg->arg_node;
			if (arg_node->node_kind==NodeIdNode){
				arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
				arg_node=arg_node->node_node_id->nid_node;
			}
			if (arg_node!=NULL)
				GenStatesInLhsNode (arg_node,&arg->arg_state);
		
			return;
		} else if (symbol->symb_kind==tuple_symb && (arg_state_p->state_mark & STATE_UNIQUE_MASK)){
			int i;

			for_li (arg,i,node->node_arguments,arg_next){
				Node arg_node;
				
				arg->arg_state=LazyState;
				if ((arg_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (arg_state_p->state_unq_type_args & (1<<i))){
					arg->arg_state.state_mark |= STATE_UNIQUE_MASK;
				}

				arg_node=arg->arg_node;
				if (arg_node->node_kind==NodeIdNode){
					arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
					arg_node=arg_node->node_node_id->nid_node;
				}
				if (arg_node!=NULL)
					GenStatesInLhsNode (arg_node,&arg->arg_state);
			}
			
			return;
		}
# endif
	}

	for_l (arg,node->node_arguments,arg_next){
		Node arg_node;
		
		arg->arg_state=LazyState;
		
		arg_node=arg->arg_node;

		if (arg_node->node_kind==NodeIdNode){
			arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
			arg_node=arg_node->node_node_id->nid_node;
			if (!arg_node)
				continue;
		}
		
		GenStatesInLhsNode (arg_node,&arg->arg_state);
	}
}

static void GenStatesInLhsSubArg (Node arg_node,StateP arg_state_p)
{
	if (arg_node->node_annotation!=NoAnnot)
		StaticMessage (True, "%S", Elhsannots, CurrentSymbol);

	switch (arg_state_p->state_type){
		case RecordState:
			GenStatesInLhsSubArguments (arg_node,arg_node->node_symbol->symb_def->sdef_record_state.state_record_arguments);
			return;
		case TupleState:
			GenStatesInLhsSubArguments (arg_node,arg_state_p->state_tuple_arguments);
			return;
		default:
			GenStatesInLhsNode (arg_node,arg_state_p);
			return;
	}
}

static void GenStatesInLhsSubArguments (Node node,StateS states[])
{
	StateP arg_state_p;
	ArgS *arg;

	for (arg=node->node_arguments,arg_state_p=states; arg!=NULL; arg=arg->arg_next,++arg_state_p){
		Node arg_node;
		
		arg->arg_state = *arg_state_p;
			
		arg_node=arg->arg_node;

		if (arg_node->node_kind==NodeIdNode){
			arg_node->node_node_id->nid_lhs_state_p_=&arg->arg_state;
			arg_node=arg_node->node_node_id->nid_node;
			if (!arg_node)
				continue;
		}

		GenStatesInLhsSubArg (arg_node,&arg->arg_state);
	}	
}

static void DetermineLhsStatesOfRule (ImpRules rule)
{
	RuleAlts alt;
	StateP function_state_p;
	
	function_state_p=rule->rule_state_p;
	
	CurrentSymbol = rule->rule_root->node_symbol;
	
	for_l (alt,rule->rule_alts,alt_next){
		CurrentLine = alt->alt_line;
		
		GenStatesInLhsSubArguments (alt->alt_lhs_root,function_state_p);

		alt->alt_lhs_root->node_state = function_state_p[-1]; /* i.e. the result kind */
	}
}
#endif

unsigned next_def_number;

void ExamineTypesAndLhsOfImpRuleSymbolDefinitionAgain (SymbDef def)
{
	StateS rootstate;

	rootstate = DetermineStatesOfRuleType (def->sdef_rule->rule_type,def->sdef_rule->rule_state_p);

#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
	DetermineLhsStatesOfRule (def->sdef_rule);
#endif

	if (def->sdef_exported && def->sdef_dcl_icl!=NULL)
		rootstate=def->sdef_dcl_icl->sdef_rule_type->rule_type_state_p[-1];

	if (IsSimpleState (rootstate)){
		if (rootstate.state_kind == OnA || rootstate.state_kind == StrictOnA){
			def->sdef_calledwithrootnode = True;
			def->sdef_returnsnode = True;
		} else if (rootstate.state_kind == StrictRedirection){
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = True;
		} else {
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = False;
		}
	} else {
		def->sdef_calledwithrootnode = False;
		def->sdef_returnsnode = False;
	}
}

#define allocate_function_state(arity) (((StateP)(CompAlloc (sizeof(StateS)*((arity)+1))))+1)

void ExamineTypesAndLhsOfSymbolDefinition (SymbDef def)
{
	StateS rootstate;

	def->sdef_number = 0;

	if (def->sdef_exported && def->sdef_dcl_icl!=def)
		def->sdef_mark |= SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK;

	switch (def->sdef_kind){
		case SYSRULE:
			def->sdef_ident->ident_symbol = NULL;
			def->sdef_ident->ident_environ = CurrentModule;
		case DEFRULE:
			if (def->sdef_isused){
				def->sdef_rule_type->rule_type_state_p = allocate_function_state (def->sdef_arity);
				rootstate = DetermineStatesOfRuleType (def->sdef_rule_type->rule_type_rule,def->sdef_rule_type->rule_type_state_p);
			} else
				return;
			break;
		case IMPRULE:
			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;

			def->sdef_rule->rule_state_p = allocate_function_state (def->sdef_arity);
			rootstate = DetermineStatesOfRuleType (def->sdef_rule->rule_type,def->sdef_rule->rule_state_p);
			
			if (def->sdef_exported && def->sdef_dcl_icl!=NULL){
				def->sdef_dcl_icl->sdef_rule_type->rule_type_state_p = allocate_function_state (def->sdef_arity);
				rootstate = DetermineStatesOfRuleType (def->sdef_dcl_icl->sdef_rule_type->rule_type_rule,def->sdef_dcl_icl->sdef_rule_type->rule_type_state_p);
			}
#ifndef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
			DetermineLhsStatesOfRule (def->sdef_rule);
#endif
			break;
		case RECORDTYPE:
		{
			FieldList fields;

			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			for_l (fields,def->sdef_type->type_fields,fl_next)
				ExamineTypesAndLhsOfSymbolDefinition (fields->fl_symbol->symb_def);

			if (def->sdef_boxed_record){
				def->sdef_calledwithrootnode = True;
				def->sdef_returnsnode = True;
				return;
			}

			rootstate = def->sdef_record_state;
			break;
		}
		case FIELDSELECTOR:
			rootstate = def->sdef_sel_field->fl_state;

			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			if (def->sdef_exported && def->sdef_dcl_icl!=NULL && def->sdef_dcl_icl->sdef_sel_field)
				rootstate = def->sdef_dcl_icl->sdef_sel_field->fl_state;
			break;
		case TYPE:
			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			rootstate = LazyState;
			break;
		case CONSTRUCTOR:
			if (def->sdef_module==CurrentModule)
				def->sdef_number = next_def_number++;
			rootstate = OnAState;
			break;
		default:
			rootstate = OnAState;
			break;
	}

	if (IsSimpleState (rootstate)){
		if (rootstate.state_kind == OnA || rootstate.state_kind == StrictOnA){
			def->sdef_calledwithrootnode = True;
			def->sdef_returnsnode = True;
		} else if (rootstate.state_kind == StrictRedirection){
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = True;
		} else {
			def->sdef_calledwithrootnode = False;
			def->sdef_returnsnode = False;
		}
	} else {
		def->sdef_calledwithrootnode = False;
		def->sdef_returnsnode = False;
	}
}

#if STRICT_LISTS
extern PolyList unboxed_record_cons_list,unboxed_record_decons_list;
#endif

void ExamineTypesAndLhsOfSymbols (Symbol symbs)
{
	next_def_number = 1;

	while (symbs!=NULL){
		if (symbs->symb_kind==definition)
			ExamineTypesAndLhsOfSymbolDefinition (symbs->symb_def);

		symbs=symbs->symb_next;
	}
#if STRICT_LISTS
	{
		PolyList unboxed_record_cons_elem,unboxed_record_decons_elem;
		
		for_l (unboxed_record_cons_elem,unboxed_record_cons_list,pl_next)
			ExamineTypesAndLhsOfSymbolDefinition (unboxed_record_cons_elem->pl_elem);
		for_l (unboxed_record_decons_elem,unboxed_record_decons_list,pl_next)
			ExamineTypesAndLhsOfSymbolDefinition (unboxed_record_decons_elem->pl_elem);
	}
#endif
}

PolyList UserDefinedArrayFunctions;

char *current_imported_module; /* also used by instructions.c */

void ImportSymbols (Symbol symbols)
{
	Symbol symbol;	
	PolyList array_fun;

	current_imported_module = NULL;
	
	for_l (array_fun,UserDefinedArrayFunctions,pl_next){
		SymbDef fun_def;
		
		fun_def = ((Symbol) array_fun->pl_elem)->symb_def;
		
		if (fun_def ->sdef_mark & (SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK))
			fun_def -> sdef_module = CurrentModule;
	}

	for_l (symbol,symbols,symb_next){
		SymbDef sdef;

		if (symbol->symb_kind!=definition)
			continue;

		sdef=symbol->symb_def;
		if (sdef->sdef_module!=CurrentModule){
			if (sdef->sdef_isused
				&& sdef->sdef_mark & (SDEF_USED_STRICTLY_MASK | SDEF_USED_LAZILY_MASK | SDEF_USED_CURRIED_MASK)
			){
				if (sdef->sdef_module!=current_imported_module){
					current_imported_module=sdef->sdef_module;
					GenImpMod (current_imported_module);
				}
				GenImport (sdef);
			}

			if (sdef->sdef_kind==RECORDTYPE){
				FieldList fields;
	
				for_l (fields,sdef->sdef_type->type_fields,fl_next){
					SymbDef field_sdef;

					field_sdef=fields->fl_symbol->symb_def;

					if (field_sdef->sdef_isused && field_sdef->sdef_mark & SDEF_USED_LAZILY_MASK){
						if (sdef->sdef_module!=current_imported_module){
							current_imported_module=sdef->sdef_module;
							GenImpMod (current_imported_module);
						}
						GenImport (field_sdef);
					}
				}
			}
		}
	}
}

void import_not_yet_imported_record_r_labels (Symbol symbols)
{
	Symbol symbol;	
	
	for_l (symbol,symbols,symb_next){
		if (symbol->symb_kind==definition){
			SymbDef sdef;

			sdef=symbol->symb_def;
			if ((sdef->sdef_mark & (SDEF_USED_STRICTLY_MASK | SDEF_RECORD_R_LABEL_IMPORTED_MASK))==SDEF_USED_STRICTLY_MASK
				&& sdef->sdef_kind==RECORDTYPE && sdef->sdef_module!=CurrentModule)
			{
				if (sdef->sdef_module!=current_imported_module){
					current_imported_module=sdef->sdef_module;
					GenImpMod (current_imported_module);
				}
				GenImport (sdef);
			}
		}
	}
}

static Bool ShouldDecrRefCount;

#if OPTIMIZE_LAZY_TUPLE_RECURSION
# define IF_OPTIMIZE_LAZY_TUPLE_RECURSION(a) ,a
#else
# define IF_OPTIMIZE_LAZY_TUPLE_RECURSION(a)
#endif

#if OPTIMIZE_LAZY_TUPLE_RECURSION
static int roots_are_tuples_or_calls_to_this_function (NodeP node_p,NodeDefP node_defs,SymbDef function_sdef_p)
{
	switch (node_p->node_kind){
		case SwitchNode:
		{
			ArgP arg_p;
			
			for_l (arg_p,node_p->node_arguments,arg_next)
				if (!roots_are_tuples_or_calls_to_this_function (arg_p->arg_node->node_arguments->arg_node,arg_p->arg_node->node_node_defs,function_sdef_p))
					return 0;
			
			return 1;
		}
		case PushNode:
			return roots_are_tuples_or_calls_to_this_function (node_p->node_arguments->arg_next->arg_node,node_defs,function_sdef_p);
		case GuardNode:
		{			
			while (node_p->node_kind==GuardNode){
				if (!roots_are_tuples_or_calls_to_this_function (node_p->node_arguments->arg_node,node_defs,function_sdef_p))
					return 0;
		
				node_defs=node_p->node_node_defs;
				node_p=node_p->node_arguments->arg_next->arg_node;
			}

			return roots_are_tuples_or_calls_to_this_function (node_p,node_defs,function_sdef_p);
		}
		case IfNode:
		{
			ArgP then_arg_p;
			NodeP else_node_p;
			
			then_arg_p=node_p->node_arguments->arg_next;
			
			if (!roots_are_tuples_or_calls_to_this_function (then_arg_p->arg_node,node_p->node_then_node_defs,function_sdef_p))
				return 0;
			
			else_node_p=then_arg_p->arg_next->arg_node;

			if (else_node_p->node_kind==NormalNode && else_node_p->node_symbol->symb_kind==fail_symb)
				return 1;

			return roots_are_tuples_or_calls_to_this_function (else_node_p,node_p->node_else_node_defs,function_sdef_p);
		}
		default:
		{
			if (node_p->node_kind==NormalNode){
				SymbolP node_symbol_p;
				
				node_symbol_p=node_p->node_symbol;
				if	(node_symbol_p->symb_kind==tuple_symb)
					return 1;
				else if (node_symbol_p->symb_kind==definition && node_symbol_p->symb_def==function_sdef_p
						&& node_p->node_arity==node_symbol_p->symb_def->sdef_arity)
					return 1;
			}
		}
	}
		
	return 0;
}
#endif

static void DecrRefCountCopiesOfArgs (Args args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(int local_scope));

static void DecrRefCountCopiesOfArg (Args arg IF_OPTIMIZE_LAZY_TUPLE_RECURSION(int local_scope))
{
	Node node;
	
	node=arg->arg_node;
	
	if (node->node_kind!=NodeIdNode){
#if OPTIMIZE_LAZY_TUPLE_RECURSION
		if (OptimizeLazyTupleRecursion && node->node_kind==NormalNode && node->node_symbol->symb_kind==select_symb && node->node_arguments->arg_node->node_kind==NodeIdNode
			&& node->node_arguments->arg_node->node_node_id->nid_scope==local_scope
		){
			NodeId node_id;
		
			node_id=node->node_arguments->arg_node->node_node_id;
			
			if (node_id->nid_mark2 & NID_HAS_LAZY_SELECTOR_COUNTER){				
				++node_id->nid_lazy_selector_ref_count;

				if (node_id->nid_lazy_selector_ref_count==node_id->nid_refcount){
					NodeP node_id_def_node;
					
					node_id_def_node=node_id->nid_node_def->def_node;
					if (node_id_def_node->node_kind==NormalNode && node_id_def_node->node_symbol->symb_kind==definition
						&& node_id_def_node->node_symbol->symb_def->sdef_kind==IMPRULE && IsLazyState (node_id_def_node->node_state)
						&& node_id_def_node->node_symbol==CurrentSymbol
					){
						SymbDef function_sdef_p;
						RuleAltP rule_alt_p;
						
						function_sdef_p=node_id_def_node->node_symbol->symb_def;
						rule_alt_p=function_sdef_p->sdef_rule->rule_alts;
						
						if (roots_are_tuples_or_calls_to_this_function (rule_alt_p->alt_rhs_root,rule_alt_p->alt_rhs_defs,function_sdef_p)){
							node_id->nid_node_def->def_id->nid_mark2 |= NID_CALL_VIA_LAZY_SELECTIONS_ONLY;
							node_id_def_node->node_symbol->symb_def->sdef_rule->rule_mark |= RULE_CALL_VIA_LAZY_SELECTIONS_ONLY;
							if (ListOptimizations)
								printf ("Optimize lazy tuple recursion of %s\n",node_id_def_node->node_symbol->symb_def->sdef_ident->ident_name);
						}
					}
				}
			} else {
				node_id->nid_mark2 |= NID_HAS_LAZY_SELECTOR_COUNTER;
				node_id->nid_lazy_selector_ref_count=1;
			}
			
			if (node_id->nid_ref_count_copy>0 && node_id->nid_node_def)
				--node_id->nid_ref_count_copy__;
		} else
#endif
		DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
	} else {
		NodeId node_id;
		
		node_id=node->node_node_id;
		if (node_id->nid_ref_count_copy>0 && node_id->nid_node_def)
			--node_id->nid_ref_count_copy__;
	}
}

static void DecrRefCountCopiesOfArgs (Args args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(int local_scope))
{
	for (; args; args = args->arg_next)
		DecrRefCountCopiesOfArg (args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
}

static StateS *RemoveUndefinedsFromTupleState (StateS *state_p,int arity)
{
	int n;
	StateS *element_state_p,*new_element_states;

	element_state_p=state_p;
		
	new_element_states=NULL;
		
	for (n=0; n<arity; ++n, ++element_state_p){
		if (IsSimpleState (*element_state_p)){
			if (element_state_p->state_kind==Undefined){
				if (new_element_states==NULL){
					StateS *new_element_state_p,*old_element_state_p;
					int i;
					
					new_element_states=NewArrayOfStates (arity);
					element_state_p=&new_element_states[n];
					
					new_element_state_p=new_element_states;
					old_element_state_p=state_p;
					for (i=0; i<arity; ++i)
						*new_element_state_p++ = *old_element_state_p++;
				}
				element_state_p->state_kind=OnA;
			}
		} else if (element_state_p->state_type==TupleState){
			StateS *new_element_states_2;
			
			new_element_states_2=RemoveUndefinedsFromTupleState
				(element_state_p->state_tuple_arguments,element_state_p->state_arity);
			if (new_element_states_2){
				if (new_element_states==NULL){
					StateS *new_element_state_p,*old_element_state_p;
					int i;
					
					new_element_states=NewArrayOfStates (arity);
					element_state_p=&new_element_states[n];
					
					new_element_state_p=new_element_states;
					old_element_state_p=state_p;
					for (i=0; i<arity; ++i)
						*new_element_state_p++ = *old_element_state_p++;
				}
				element_state_p->state_tuple_arguments=new_element_states_2;				
			}
		}
	}

	return new_element_states;
}

static Bool ChangeState (StateS *old_state_p,StateS newstate)
{
	switch (old_state_p->state_kind){
		case OnA:
		case StrictOnA:		
			*old_state_p = newstate;
			return True;
		case Undefined:
			*old_state_p = newstate;
			return False;
		default:
			return False;
	}
}

static Bool AdjustState (StateS *old_state_p, StateS newstate)
{
	if (IsSimpleState (newstate)){
		if (IsSimpleState (*old_state_p)){
			switch (newstate.state_kind){
				case StrictOnA:
				case OnB:
					return ChangeState (old_state_p, newstate);
				default:
					return False;
			}
		} else
			return False;
	} else if (IsSimpleState (*old_state_p)){
		if (newstate.state_type==TupleState && 
			(old_state_p->state_kind==OnA || old_state_p->state_kind==StrictOnA || old_state_p->state_kind==Undefined))
		{
			StateS *element_states;
		
			element_states=RemoveUndefinedsFromTupleState (newstate.state_tuple_arguments,newstate.state_arity);
			
			*old_state_p=newstate;
			
			if (element_states){
/*				CheckWarning ("undefined state in tuple state removed",NULL); */
				old_state_p->state_tuple_arguments=element_states;
			}
			
			return old_state_p->state_kind!=Undefined;
		}

		return ChangeState (old_state_p,newstate);
	} else if (newstate.state_type==TupleState){
		int i, arity;
		StateS argstate;
		Bool new_arg_states;

		arity = old_state_p->state_arity;
		new_arg_states = False;

		Assume (newstate.state_arity == arity, "statesgen", "AdjustState");

		for (i=0; i<arity; i++){
			argstate = old_state_p->state_tuple_arguments[i];
			if (AdjustState (& argstate, newstate.state_tuple_arguments[i]) && ! new_arg_states){
				int j;
				States argstates;
			
				new_arg_states = True;
				argstates = NewArrayOfStates (arity);
				for (j=0; j<arity; j++)
					argstates[j] = old_state_p->state_tuple_arguments[j];
				old_state_p->state_tuple_arguments = argstates;
			}
			old_state_p->state_tuple_arguments[i] = argstate;
		}
		return new_arg_states;
	} else
		return False;
}

static void DetermineStateOfThenOrElse (Args t_or_e_args, NodeDefs *t_or_e_defs, StateS demstate,int local_scope)
{
	Node node;
	
	node=t_or_e_args->arg_node;
	
	if (node->node_kind==NodeIdNode && *t_or_e_defs==NULL){
		NodeId node_id;
		
		node_id=node->node_node_id;
		if (node_id->nid_ref_count_copy>=0)
			--node_id->nid_ref_count_copy__;	
	} else {
#if 0
		FPrintF (rules_file,"DetermineStateOfThenOrElse %d\n",local_scope);
#endif
		DetermineStatesOfRootNodeAndDefs (node,t_or_e_defs,demstate, local_scope);
	}

	AdjustState (&t_or_e_args->arg_state,demstate);
}

static void DecrementRefCountCopy (NodeId nid)
{ 
	if (nid->nid_ref_count_copy>0)
		--nid->nid_ref_count_copy__;
}

static Bool AdjustStateOfSharedNode (NodeId nid, StateS demstate, int local_scope)
{
	/*
		Note that if the reference count of a node identifier smaller than one means that the corresponding node 
		has already been treated. In that case it would be dangerous to adjust the node state
	*/
   	
	if (nid->nid_ref_count_copy>=0){
		NodeDefs nodedef;

		nodedef = nid->nid_node_def;

		if (nodedef!=NULL){
			int node_id_scope;
			
			if (ShouldDecrRefCount)
				DecrementRefCountCopy (nid);

			node_id_scope=nid->nid_scope;
			if (node_id_scope<0)
				node_id_scope=-node_id_scope;

#if 0
			FPrintF (rules_file,"AdjustStateOfSharedNode ");
			DPrintNodeId (nid,rules_file);
			FPrintF (rules_file," %d %d\n",node_id_scope,local_scope);
#endif

			if (node_id_scope>=local_scope){
				Node argnode;
				
				argnode = nodedef->def_node;
				if (nid->nid_mark & ON_A_CYCLE_MASK)
					AdjustState (&argnode->node_state, StrictState);
				else
					AdjustState (&argnode->node_state, demstate);
			}

			if (nodedef->def_node)
				return (nodedef->def_node->node_state.state_mark & STATE_PARALLEL_MASK)!=0;
		}
#if 0
		else {
			printf ("AdjustStateOfSharedNode nid_node_def=NULL ");
			DPrintNodeId (nid,StdOut);			
			printf (" %d %d\n",nid->nid_scope,local_scope);
		}
#endif
	}

	return False;
}

static Bool ArgInAStrictContext (Args arg, StateS demstate, Bool semistrict, int local_scope);

static Bool DetermineStrictArgContext (Args arg, StateS demstate, int local_scope)
{
	AdjustState (&arg->arg_state, demstate);
	
	return ArgInAStrictContext (arg, arg->arg_state, False, local_scope);
}

static StateP GetStateOfArguments (SymbDef sdef,int actual_arity)
{
	int symbol_arity;
	StateP state_p;
	
	switch (sdef->sdef_kind){
		case DEFRULE:
		case SYSRULE:
			state_p=sdef->sdef_rule_type->rule_type_state_p;
			symbol_arity = sdef->sdef_arity;
			break;
		case IMPRULE:
			state_p=sdef->sdef_rule->rule_state_p;
			symbol_arity = sdef->sdef_arity;
			break;
		case RECORDTYPE:
			state_p=sdef->sdef_record_state.state_record_arguments;
			symbol_arity = sdef->sdef_cons_arity;
			break;
		default:
			return NULL;
	}
			
	if (symbol_arity==actual_arity)
		return state_p;
	else
		return NULL;
}

static Bool ArgsInAStrictContext (StateP arg_state_p,Args argn, int local_scope)
{
	Bool parallel;
	
	parallel = False;

	for (; argn!=NULL; argn=argn->arg_next){	
		if (! IsLazyState (*arg_state_p)){
			if (DetermineStrictArgContext (argn,*arg_state_p,local_scope))
				parallel = True;
		} else if (ShouldDecrRefCount)
			DecrRefCountCopiesOfArg (argn IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
		
		++arg_state_p;
	}

	return parallel;
}

#if DESTRUCTIVE_RECORD_UPDATES
	static Bool UpdateNodeInAStrictOrSemiStrictContext (Node node,StateP demanded_state_p,int local_scope,Bool semi_strict)
#else
	static Bool UpdateNodeInAStrictOrSemiStrictContext (Node node,int local_scope,Bool semi_strict)
#endif
{
	Bool parallel;
	SymbDef record_sdef;
	int type_arg_number;
	ArgS *arg;
	StateP record_arg_states;

	parallel=False;
	
	record_sdef=node->node_symbol->symb_def;
	
	if (!semi_strict){
#if DESTRUCTIVE_RECORD_UPDATES
		if (demanded_state_p->state_type==SimpleState && 
			demanded_state_p->state_kind==StrictOnA
/*			 && demanded_state_p->state_object==RecordObj */
			)
		{
			node->node_state = *demanded_state_p;
			node->node_state.state_object = RecordObj;
		} else
#endif	
		node->node_state = record_sdef->sdef_record_state;
	}

	arg=node->node_arguments;
	
	if (semi_strict){
		if (ArgInAStrictContext (arg,StrictState,True,local_scope))
			parallel = True;
	} else {

#if BOXED_RECORDS
		if (record_sdef->sdef_boxed_record){
			StateS boxed_record_state;
			
			SetUnaryState (&boxed_record_state,StrictOnA,RecordObj);
			if (DetermineStrictArgContext (arg, boxed_record_state,local_scope))
				parallel = True;			
		} else
#endif

		if (DetermineStrictArgContext (arg, record_sdef->sdef_record_state,local_scope))
			parallel = True;
	}
	
	type_arg_number=0;

	record_arg_states=record_sdef->sdef_record_state.state_record_arguments;
	
	while ((arg=arg->arg_next)!=NULL){
		int selector_number;
		Node selector_node;
			
		selector_node=arg->arg_node;
		selector_number=selector_node->node_symbol->symb_def->sdef_sel_field_number;

#if 1
		type_arg_number=selector_number;
#else
		/* Codewarrior 6 optimizer bug */
		while (type_arg_number!=selector_number){
			++type_arg_number;
		}
#endif

		if (!IsLazyState (record_arg_states[type_arg_number])){
			if (semi_strict
				? ArgInAStrictContext (selector_node->node_arguments,StrictState,True,local_scope)
				: DetermineStrictArgContext (selector_node->node_arguments,record_arg_states[type_arg_number],local_scope))
				parallel = True;
		} else if (ShouldDecrRefCount)
			DecrRefCountCopiesOfArg (selector_node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
	}
	
	if (parallel)
		node->node_state.state_mark |= STATE_PARALLEL_MASK;
	
	return parallel;
}

int optimise_strict_tuple_result_functions;

static Bool NodeInAStrictContext (Node node,StateS demanded_state,int local_scope)
{
	Bool parallel;
	
	parallel=False;

	if (node->node_kind==NormalNode){
		Symbol rootsymb;
		
		rootsymb = node->node_symbol;
		switch (rootsymb->symb_kind){
			case cons_symb:
#if STRICT_LISTS
				if (node->node_arity==2){
					if (rootsymb->symb_head_strictness>1)
						if (rootsymb->symb_head_strictness==4)
							parallel = DetermineStrictArgContext (node->node_arguments,*rootsymb->symb_unboxed_cons_state_p,local_scope);
						else
							parallel = DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);
					if (rootsymb->symb_tail_strictness)
						parallel = DetermineStrictArgContext (node->node_arguments->arg_next,StrictState,local_scope);
				}
#endif
				if (ShouldDecrRefCount)
					DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
			case nil_symb:
#if STRICT_LISTS
				if (rootsymb->symb_head_strictness & 1)
					parallel = DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);
#endif

				SetUnaryState (&node->node_state, StrictOnA, ListObj);
				break;
			case apply_symb:
				node->node_state = StrictState;
				node->node_state.state_kind = StrictRedirection;
				parallel = DetermineStrictArgContext (node->node_arguments, StrictState, local_scope);
				if (ShouldDecrRefCount)
					DecrRefCountCopiesOfArg (node->node_arguments->arg_next IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
				break;
			case if_symb:
			{
				Args args;
				
				args = node->node_arguments;
				node->node_state = StrictState;

				if (node->node_arity==3){
					if (DetermineStrictArgContext (args, BasicSymbolStates[bool_type], local_scope))
						parallel = True;

					args = args->arg_next;
#ifdef FASTER_STRICT_IF
					node->node_state=demanded_state;

					if (DetermineStrictArgContext (args,demanded_state,20000/*local_scope+1*/))
						parallel = True;

					args=args->arg_next;
					
					if (DetermineStrictArgContext (args,demanded_state,20000/*local_scope+1*/))
						parallel = True;

					break;
#else
					node->node_state.state_kind = StrictRedirection;
#endif
				}
				if (ShouldDecrRefCount)
					for (; args; args = args->arg_next)
						DecrRefCountCopiesOfArg (args IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
				break;
			}
			case select_symb:
			{
				Args arg;
				Node argnode;
				
				arg=node->node_arguments;
				
				SetUpdateableTupleState (&arg->arg_state, NewArrayOfUnaryStates (rootsymb->symb_arity, Undefined), rootsymb->symb_arity);
	
				arg->arg_state.state_tuple_arguments [node->node_arity - 1] = demanded_state;
				
				argnode=arg->arg_node;
				if (argnode->node_kind!=NodeIdNode)
					parallel = NodeInAStrictContext (argnode, arg->arg_state, local_scope);
				else {
					NodeId node_id;
					
					node_id=argnode->node_node_id;

					if (node_id->nid_ref_count_copy>=0 && node_id->nid_node_def){
						int node_id_scope;
					
						argnode = node_id->nid_node_def->def_node;
						
						if (ShouldDecrRefCount)
							DecrementRefCountCopy (node_id);
	
						node_id_scope=node_id->nid_scope;
						if (node_id_scope<0)
							node_id_scope=-node_id_scope;

#if 0
						FPrintF (rules_file,"NodeInAStrictContext select_symb %d ",node->node_arity);
						DPrintNodeId (node_id,rules_file);
						FPrintF (rules_file," %d %d\n",node_id_scope,local_scope);
#endif
						if (node_id_scope>=local_scope){
							if (IsSimpleState (argnode->node_state)){
								if (argnode->node_state.state_kind!=Parallel){
									SetUpdateableTupleState (&argnode->node_state, NewArrayOfUnaryStates (rootsymb->symb_arity, OnA), rootsymb->symb_arity);
									AdjustState (&argnode->node_state.state_tuple_arguments[node->node_arity-1],demanded_state);
								}
							} else {
								if ((argnode->node_state.state_mark & STATE_ELEMENTS_UPDATEABLE_MASK)==0){
									int i,arity;
									States arg_states;

									arity = argnode->node_state.state_arity;
									arg_states = NewArrayOfStates (arity);

									for (i=0; i<arity; ++i)
										arg_states[i] = argnode->node_state.state_tuple_arguments[i];
									
									argnode->node_state.state_tuple_arguments = arg_states;
									argnode->node_state.state_mark |= STATE_ELEMENTS_UPDATEABLE_MASK;
								}
								
								AdjustState (&argnode->node_state.state_tuple_arguments[node->node_arity-1],demanded_state);
							}
						}
						
#if 0
						PrintState (argnode->node_state,rules_file);
						FPrintF (rules_file,"\n");
#endif
					}
				}
				node->node_state = demanded_state;
				break;
			}
			case tuple_symb:
				if (IsSimpleState (demanded_state)){
					SetUnaryState (&node->node_state, StrictOnA, TupleObj);
					if (ShouldDecrRefCount)
						DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
				} else {
					Args arg; int i;
					
					for (i=0, arg=node->node_arguments; arg!=NULL; arg=arg->arg_next, i++){
						Bool par;
					
						par = False;
						if (!IsLazyState (demanded_state.state_tuple_arguments[i])){
							if (DetermineStrictArgContext (arg,demanded_state.state_tuple_arguments[i],local_scope))
								par = True;
						} else if (ShouldDecrRefCount)
							DecrRefCountCopiesOfArg (arg IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
						
						arg->arg_state = demanded_state.state_tuple_arguments[i];
						
						if (par){
							arg->arg_state.state_mark |= STATE_PARALLEL_MASK;
							parallel = True;
						}
					}
					node->node_state = demanded_state;
				}
				break;
			case definition:
			{
				StateP definition_state_p;
				SymbDef sdef;
				
				sdef = rootsymb->symb_def;
	
				if (sdef->sdef_arfun!=NoArrayFun
					&& (   (sdef->sdef_arfun==_UnqArraySelectLastFun || sdef->sdef_arfun==_UnqArraySelectNextFun) && node->node_arity==2)
						|| (sdef->sdef_arfun==_ArrayUpdateFun && node->node_arity==3))
				{
					StateP function_state_p;

					function_state_p=sdef->sdef_rule_type->rule_type_state_p;
				
					if (!(function_state_p[0].state_type==SimpleState && function_state_p[0].state_object==UnknownObj)){
						StateP arg_node_state_p;
						NodeP arg_node_p;
						ArgP arg_p;
						
						arg_p=node->node_arguments;

						if (sdef->sdef_arfun!=_ArrayUpdateFun){
							parallel = ArgsInAStrictContext (function_state_p,arg_p,local_scope);
							node->node_state = function_state_p[-1];

							arg_node_p=arg_p->arg_node;
							if (arg_node_p->node_kind!=NodeIdNode){
								arg_node_state_p=&arg_node_p->node_state;
													
								if (arg_node_state_p->state_type==TupleState && arg_node_state_p->state_tuple_arguments[1].state_type!=SimpleState){
									StateP tuple_state_args_p,tuple_result_state_args_p,result_state_p;
									
									result_state_p=&arg_node_state_p->state_tuple_arguments[1];
									
									tuple_state_args_p=CompAllocArray (2,StateS);
									tuple_state_args_p[0]=arg_p->arg_state.state_tuple_arguments[0];							
									tuple_state_args_p[1]=*result_state_p;	
									arg_p->arg_state.state_tuple_arguments=tuple_state_args_p;
									
									tuple_result_state_args_p=CompAllocArray (2,StateS);
									tuple_result_state_args_p[0]=node->node_state.state_tuple_arguments[0];
									tuple_result_state_args_p[1]=*result_state_p;
									node->node_state.state_tuple_arguments=tuple_result_state_args_p;
								}
							}
						} else {
							NodeP arg_node_p;

							if (demanded_state.state_type!=SimpleState){
								StateS arg_states[3],*update_arg_tuple_arg_states;
							
								arg_states[0]=function_state_p[0];
								arg_states[1]=function_state_p[1];
								arg_states[2]=function_state_p[2];
							
								update_arg_tuple_arg_states=NewArrayOfStates (2);
								update_arg_tuple_arg_states[0]=arg_states[0].state_tuple_arguments[0];
								update_arg_tuple_arg_states[1]=demanded_state;
								
								arg_states[0].state_tuple_arguments=update_arg_tuple_arg_states;
						
								parallel = ArgsInAStrictContext (arg_states,arg_p,local_scope);
								node->node_state = demanded_state;
							} else {
								parallel = ArgsInAStrictContext (function_state_p,arg_p,local_scope);
								node->node_state = function_state_p[-1];

								arg_node_p=arg_p->arg_node;
								if (arg_node_p->node_kind!=NodeIdNode){
									arg_node_state_p=&arg_node_p->node_state;
														
									if (arg_node_state_p->state_type==TupleState && arg_node_state_p->state_tuple_arguments[1].state_type!=SimpleState){
										StateP tuple_state_args_p,result_state_p;
										
										result_state_p=&arg_node_state_p->state_tuple_arguments[1];
										
										tuple_state_args_p=CompAllocArray (2,StateS);
										tuple_state_args_p[0]=arg_p->arg_state.state_tuple_arguments[0];							
										tuple_state_args_p[1]=*result_state_p;	
										arg_p->arg_state.state_tuple_arguments=tuple_state_args_p;
										
										node->node_state=*result_state_p;
									}
								}
							}
						}
						break;
					}
				}

				definition_state_p = GetStateOfArguments (sdef,node->node_arity);

				if (definition_state_p!=NULL){
#ifdef FASTER_STRICT_AND_OR
					if (sdef->sdef_module==StdBoolId->ident_name && node->node_arity==2){
						if (sdef->sdef_ident==AndId){
							ArgP arg1,arg2,false_arg;
							NodeP false_node;
							
							arg1=node->node_arguments;
							arg2=arg1->arg_next;
							
							/* if arg2 is a node_id, incorrect code if and on root (redirection with jmpevalupd) */
							
							if (arg2->arg_node->node_kind!=NodeIdNode){	
								node->node_symbol=IfSymbol;
								node->node_arity=3;
								
								false_node=NewNode (FalseSymbol,NULL,0);
								false_node->node_state=LazyState;
								
								false_arg=NewArgument (false_node);
								false_arg->arg_state=LazyState;
								
								arg2->arg_next=false_arg;		

								return NodeInAStrictContext (node,demanded_state,local_scope);
							}
						} else if (sdef->sdef_ident==OrId){
							ArgP arg1,arg2,true_arg;
							NodeP true_node;
							
							arg1=node->node_arguments;
							arg2=arg1->arg_next;

							/* if arg2 is a node_id, incorrect code if or on root (redirection with jmpevalupd) */
							
							if (arg2->arg_node->node_kind!=NodeIdNode){									
								node->node_symbol=IfSymbol;
								node->node_arity=3;

								true_node=NewNode (TrueSymbol,NULL,0);
								true_node->node_state=LazyState;
								
								true_arg=NewArgument (true_node);
								true_arg->arg_state=LazyState;
			
								arg1->arg_next=true_arg;
								true_arg->arg_next=arg2;
								
								return NodeInAStrictContext (node,demanded_state,local_scope);
							}
						}
					}
#endif

					if (sdef->sdef_kind==IMPRULE && demanded_state.state_type==TupleState && definition_state_p[-1].state_type==TupleState
						&& !(sdef->sdef_rule->rule_mark & RULE_CAF_MASK) && sdef->sdef_rule->rule_alts->alt_kind==Contractum && optimise_strict_tuple_result_functions)
							optimise_tuple_result_function (node,demanded_state);

					if (sdef->sdef_kind==RECORDTYPE)
#if BOXED_RECORDS
						if (sdef->sdef_boxed_record)
							SetUnaryState (&node->node_state,StrictOnA,RecordObj);
						else
#endif
						node->node_state = sdef->sdef_record_state;
					else
						node->node_state = definition_state_p[-1];
					parallel = ArgsInAStrictContext (definition_state_p,node->node_arguments,local_scope);
				} else {
					if (sdef->sdef_kind==CONSTRUCTOR && sdef->sdef_strict_constructor && sdef->sdef_arity==node->node_arity){
						SetUnaryState (&node->node_state,StrictOnA,UnknownObj);
						parallel = ArgsInAStrictContext (sdef->sdef_constructor->cl_state_p,node->node_arguments,local_scope);
					} else {
						if (ShouldDecrRefCount)
							DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
						node->node_state = StrictState;
					}
				}
				break;
			}
			case seq_symb:
				node->node_state=demanded_state;
				if (node->node_arity==2){
					parallel = DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);
					parallel = DetermineStrictArgContext (node->node_arguments->arg_next,demanded_state,local_scope);
				} else {
					if (ShouldDecrRefCount)
						DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
					node->node_state = StrictState;
				}
				break;
			default:
				if (rootsymb->symb_kind < Nr_Of_Predef_Types){
					node->node_state = BasicSymbolStates [rootsymb->symb_kind];
					node->node_state.state_kind = demanded_state.state_kind;
				}
				break;
		}
	} else if (node->node_kind==SelectorNode){
		SymbDef ssymb;
		StateP record_state_p,unboxed_record_state_p;
#if BOXED_RECORDS
		static StateS boxed_record_state;
#endif		
		ssymb = node->node_symbol->symb_def;

		unboxed_record_state_p=&ssymb->sdef_type->type_lhs->ft_symbol->symb_def->sdef_record_state;
#if BOXED_RECORDS		
		if (ssymb->sdef_type->type_lhs->ft_symbol->symb_def->sdef_boxed_record){
			SetUnaryState (&boxed_record_state,StrictOnA,RecordObj);
			record_state_p = &boxed_record_state;
		} else
#endif
		record_state_p=unboxed_record_state_p;
		
		if (node->node_arity>=SELECTOR_U){
			StateP tuple_arg_states;
			
			tuple_arg_states=NewArrayOfStates (2);

			if (node->node_arity>=SELECTOR_L){
				StateS sel_arg_state,*sel_arg_tuple_arg_states;
				NodeP arg_node_p;

				sel_arg_tuple_arg_states=NewArrayOfStates (2);
				sel_arg_tuple_arg_states[0]=*record_state_p;
				
				if (demanded_state.state_type==TupleState && !IsLazyState (demanded_state.state_tuple_arguments[1])){
					sel_arg_tuple_arg_states[1]=demanded_state.state_tuple_arguments[1];
					tuple_arg_states[1]=demanded_state.state_tuple_arguments[1];
				} else {
					sel_arg_tuple_arg_states[1]=StrictState;
					tuple_arg_states[1]=StrictState;
				}
				SetTupleState (&sel_arg_state,sel_arg_tuple_arg_states,2);
				
				parallel = DetermineStrictArgContext (node->node_arguments,sel_arg_state,local_scope);

				arg_node_p=node->node_arguments->arg_node;
				if (arg_node_p->node_kind!=NodeIdNode && arg_node_p->node_state.state_type==TupleState
					&& arg_node_p->node_state.state_tuple_arguments[1].state_type!=SimpleState)
				{
					StateP result_state_p;
				
					result_state_p=&arg_node_p->node_state.state_tuple_arguments[1];
					
					tuple_arg_states[1]=*result_state_p;
					sel_arg_tuple_arg_states[1]=*result_state_p;	
				}
			} else {
				parallel = DetermineStrictArgContext (node->node_arguments,*record_state_p,local_scope);
				tuple_arg_states[1]=*record_state_p;
			}			

			tuple_arg_states[0]=unboxed_record_state_p->state_record_arguments[ssymb->sdef_sel_field_number];
			SetTupleState (&node->node_state,tuple_arg_states,2);
		} else {
			parallel = DetermineStrictArgContext (node->node_arguments,*record_state_p,local_scope);
			node->node_state=demanded_state;
		}
	} else if (node->node_kind==UpdateNode)
#if DESTRUCTIVE_RECORD_UPDATES
		return UpdateNodeInAStrictOrSemiStrictContext (node,&demanded_state,local_scope,False);
#else
		return UpdateNodeInAStrictOrSemiStrictContext (node,local_scope,False);
#endif
	else if (node->node_kind==MatchNode){
		parallel = DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);

		node->node_state=demanded_state;		
	} else
		error_in_function ("NodeInAStrictContext");

	if (parallel)
		node->node_state.state_mark |= STATE_PARALLEL_MASK;

	return parallel;	
}

static Bool NodeInASemiStrictContext (Node node, int local_scope);

static Bool ArgInAStrictContext (Args arg, StateS demstate, Bool semistrict, int local_scope)
{
	Bool parallel;
	Node argnode;

	parallel = False;

	argnode=arg->arg_node;
	
	if (argnode->node_kind!=NodeIdNode){
		if (semistrict && argnode->node_number<0){
			parallel = NodeInASemiStrictContext (argnode,local_scope);
			argnode->node_state.state_kind = SemiStrict;
		} else
			parallel = NodeInAStrictContext (argnode, demstate, local_scope);
	} else
		parallel =  AdjustStateOfSharedNode (argnode->node_node_id, demstate, local_scope);

	if (parallel)
		arg->arg_state.state_mark |= STATE_PARALLEL_MASK;

	return parallel;
}

static Bool NodeInASemiStrictContext (Node node,int local_scope)
{
	switch (node->node_kind){
		case NormalNode:
		{
			Bool parallel;
			Symbol symb;

			symb = node->node_symbol;

			parallel = False;
			
			if (symb->symb_kind==definition){
				SymbDef sdef;
				ArgP arg_p;
				StateP arg_state_p;
				
				sdef=symb->symb_def;
				arg_state_p = GetStateOfArguments (sdef,node->node_arity);

				if (arg_state_p==NULL && sdef->sdef_kind==CONSTRUCTOR &&
					sdef->sdef_strict_constructor && sdef->sdef_arity==node->node_arity)
				{
					arg_state_p=sdef->sdef_constructor->cl_state_p;
				}
					
				if (arg_state_p!=NULL){
					for_l (arg_p,node->node_arguments,arg_next){
						if (IsLazyState (*arg_state_p)){
							if (ShouldDecrRefCount)
								DecrRefCountCopiesOfArg (arg_p IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
						} else 
							if (ArgInAStrictContext (arg_p,StrictState,True,local_scope))
								parallel = True;
						
						++arg_state_p;
					}
				} else
					DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
			}
#if STRICT_LISTS
			else if (symb->symb_kind==cons_symb && node->node_arity==2){
				ArgP arg_p;
				
				arg_p=node->node_arguments;				
				if (symb->symb_head_strictness>1){
					if (ArgInAStrictContext (arg_p,StrictState,True,local_scope))
						parallel = True;
				} else
					if (ShouldDecrRefCount)
						DecrRefCountCopiesOfArg (arg_p IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));

				arg_p=arg_p->arg_next;
				if (symb->symb_tail_strictness){
					if (ArgInAStrictContext (arg_p,StrictState,True,local_scope))
						parallel = True;
				} else
					if (ShouldDecrRefCount)
						DecrRefCountCopiesOfArg (arg_p IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
			}
#endif
			else
				DecrRefCountCopiesOfArgs (node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));

			if (parallel)
				node->node_state.state_mark |= STATE_PARALLEL_MASK;
		
			return parallel;
		}
		case SelectorNode:
		case MatchNode:
			if (ArgInAStrictContext (node->node_arguments,StrictState,True,local_scope)){
				node->node_state.state_mark |= STATE_PARALLEL_MASK;
				return True;
			} else
				return False;
		case UpdateNode:
#if DESTRUCTIVE_RECORD_UPDATES
			return UpdateNodeInAStrictOrSemiStrictContext (node,&StrictState,local_scope,True);
#else		
			return UpdateNodeInAStrictOrSemiStrictContext (node,local_scope,True);
#endif
		default:
			error_in_function ("NodeInASemiStrictContext");
			return False;
	}
}

static void DetermineStatesOfNonIfRootNode (Node root,NodeId root_id,StateS demstate,int local_scope)
{	
	if (root->node_state.state_kind != OnA){
		StaticMessage (False, "%S", Wrootannot, CurrentSymbol);
		root->node_state.state_kind = OnA;
	}
	
	if (root_id)
		root_id->nid_ref_count_copy_=-1; /* to indicate that this node has been examined */
	
	if (root_id && (root_id->nid_mark & ON_A_CYCLE_MASK)){
		(void) NodeInASemiStrictContext (root, local_scope);
		root->node_state.state_kind = SemiStrict;
	} else
		NodeInAStrictContext (root, demstate, local_scope);
}

static int scope;
static void DetermineStatesRootNode (Node node, NodeId nid, StateS demstate,int local_scope);

static void DetermineStatesIfRootNode (Node node, StateS demstate,int local_scope)
{
	Args condpart;
	int new_local_scope;

	new_local_scope=scope+2;
	scope+=3;

	condpart = node->node_arguments;
			
	AdjustState (&condpart->arg_state, BasicSymbolStates [bool_type]);

	if (condpart->arg_node->node_kind!=NodeIdNode)
		DetermineStatesRootNode (condpart->arg_node, NULL,condpart->arg_state, local_scope);
	else 
		/* the parallel state of the condition is not needed */
		AdjustStateOfSharedNode (condpart->arg_node->node_node_id,condpart->arg_state,local_scope);
	
	AdjustState (&node->node_state, demstate);

	++scope;
	DetermineStateOfThenOrElse (condpart->arg_next,&node->node_then_node_defs,demstate,new_local_scope);

	++scope;
	DetermineStateOfThenOrElse (condpart->arg_next->arg_next,&node->node_else_node_defs,demstate,new_local_scope);
}

static void DetermineStatesSwitchRootNode (Node root_node, StateS demstate, int local_scope)
{
	ArgP arg_p;

	AdjustState (&root_node->node_state, demstate);

	for_l (arg_p,root_node->node_arguments,arg_next){
		NodeP node;

		node=arg_p->arg_node;
		if (node->node_kind==CaseNode){
			NodeP case_alt_node_p;

			case_alt_node_p=node->node_arguments->arg_node;
/*	Codewarrior bug			if (case_alt_node_p->node_kind==PushNode){  */
			if (node->node_arguments->arg_node->node_kind==PushNode)
				DetermineStatesOfRootNodeAndDefs (case_alt_node_p->node_arguments->arg_next->arg_node,&node->node_node_defs,demstate,local_scope);
			else
				DetermineStatesOfRootNodeAndDefs (node->node_arguments->arg_node,&node->node_node_defs,demstate,local_scope);
		} else if (node->node_kind==DefaultNode){
			DetermineStatesOfRootNodeAndDefs (node->node_arguments->arg_node,&node->node_node_defs,demstate,local_scope);
		} else if (node->node_kind==OverloadedCaseNode){
			NodeP case_node_p;

			DetermineStrictArgContext (node->node_arguments,StrictState,local_scope);
			if (ShouldDecrRefCount)
				DecrRefCountCopiesOfArg (node->node_arguments->arg_next IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));

			case_node_p=node->node_node;
			DetermineStatesOfRootNodeAndDefs (case_node_p->node_arguments->arg_node,&case_node_p->node_node_defs,demstate,local_scope);
		} else
			error_in_function ("DetermineStatesSwitchRootNode");
	}
}

static void DetermineStatesRootNode (Node node, NodeId nid, StateS demstate,int local_scope)
{
	switch (node->node_kind)
	{
		case IfNode:
			DetermineStatesIfRootNode (node, demstate, local_scope);
			break;
		case SwitchNode:
			DetermineStatesSwitchRootNode (node, demstate, local_scope);
			break;
		default:
			DetermineStatesOfNonIfRootNode (node, nid, demstate, local_scope);
			break;
	}
}

static void ParAnnotInAStrictContext (Node node,Annotation annot, int local_scope)
{
	if (annot==ParallelAtAnnot){
		Node at_node;
		
		at_node=get_p_at_node (node);
		
		if (at_node->node_kind!=NodeIdNode)
			NodeInAStrictContext (at_node,BasicSymbolStates[procid_type],local_scope);
	}
}

static void DetermineStatesOfNodeDefs (NodeDefs firstdef, int local_scope)
{
	NodeDefs next;
	Bool ready;

	for_l (next,firstdef,def_next)
		if ((next->def_id->nid_mark & ON_A_CYCLE_MASK) && next->def_node!=NULL)
			DecrRefCountCopiesOfArgs (next->def_node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));

	/* examine all parallel annotated nodes */
	
	for_l (next,firstdef,def_next){
		Node node;
	
		node=next->def_node;
		if (node && node->node_annotation && node->node_state.state_kind==Parallel)
			ParAnnotInAStrictContext (node,node->node_annotation, local_scope);
	}
	
	/* determine states */
	
	do {
		ready = True;

		/* First we examine all the nodes that are not lazy anymore */

		for_l (next,firstdef,def_next){
			Node node;
			
			node = next->def_node;
			if (node && ! IsLazyState (node->node_state) &&
				(next->def_id->nid_ref_count_copy==0 ||
				(next->def_id->nid_ref_count_copy>=0 && (next->def_id->nid_mark & ON_A_CYCLE_MASK))))
			{
				
				/* to indicate that this node has already been examined: */
				next->def_id->nid_ref_count_copy_ = -1;
				
				ready =  False;

				/*
				JVG: hack to remove undefined's in tuple state of tuples which are selected
				and for which there are selectors and may be other references in a lazy context
				(can lead to less efficient code)
				*/
				
				if (next->def_id->nid_refcount>1 && node->node_kind==NormalNode
					&& node->node_symbol->symb_kind==select_symb
					&& node->node_state.state_type==TupleState)
				{
					StateS *element_states;
		
					element_states=RemoveUndefinedsFromTupleState
						(node->node_state.state_tuple_arguments,node->node_state.state_arity);
					if (element_states)
						node->node_state.state_tuple_arguments=element_states;
				}

				if (next->def_id->nid_mark & ON_A_CYCLE_MASK){
					ShouldDecrRefCount = False;
					NodeInASemiStrictContext (node,local_scope);
					SetUnaryState (&node->node_state, SemiStrict, node->node_state.state_object);
				} else {
					ShouldDecrRefCount = True;
					NodeInAStrictContext (node, node->node_state, local_scope);
				}
			}
		}
		
		if (ready){
			for_l (next,firstdef,def_next){
				if (next->def_node && IsLazyState (next->def_node->node_state) && 
					! (next->def_id->nid_mark & ON_A_CYCLE_MASK) && next->def_id->nid_ref_count_copy==0)
				{
					next->def_id->nid_ref_count_copy_ = -1;
					ready =  False;
					DecrRefCountCopiesOfArgs (next->def_node->node_arguments IF_OPTIMIZE_LAZY_TUPLE_RECURSION(local_scope));
					break;
				}
			}
		}
	} while (! ready);

	for_l (next,firstdef,def_next)
		if (next->def_node)
			if (! (next->def_id->nid_ref_count_copy<0 ||
					(next->def_id->nid_ref_count_copy==0 && (next->def_id->nid_mark & ON_A_CYCLE_MASK))))
#if 1
				error_in_function_s ("DetermineStatesOfNodeDefs",CurrentSymbol->symb_def->sdef_ident->ident_name);
#else
			{
				char s[20];
				
				sprintf (s,"%x %d %d",(int)next->def_id,next->def_id->nid_ref_count_copy,next->def_id->nid_refcount);
				error_in_function_s ("DetermineStatesOfNodeDefs",/*CurrentSymbol->symb_def->sdef_ident->ident_name*/s/*next->def_id->nid_ident->ident_name*/);
			}
#endif
}

#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS

static void set_push_node_id_states (NodeIdListElementP node_ids,StateS states[])
{
	StateP arg_state_p;

	for (arg_state_p=states; node_ids!=NULL; node_ids=node_ids->nidl_next,++arg_state_p){
		NodeIdP node_id_p;
		
		node_id_p=node_ids->nidl_node_id;
		node_id_p->nid_lhs_state_p_=arg_state_p;
		node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
	}
}

static void set_lazy_push_node_id_states (NodeIdListElementP node_ids)
{
	for (; node_ids!=NULL; node_ids=node_ids->nidl_next){
		NodeIdP node_id_p;
		
		node_id_p=node_ids->nidl_node_id;
		node_id_p->nid_lhs_state_p_=&LazyState;
		node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
	}
}

static void DetermineStatesOfNodeAndDefs (Node root_node,NodeDefs node_defs,StateS demstate,int local_scope)
{
	ShouldDecrRefCount = True;

	if (root_node->node_kind==SwitchNode){
		ArgP arg_p;
		int old_scope;
		
		old_scope=scope;
		
		if (node_defs!=NULL)
			error_in_function ("DetermineStatesOfNodeAndDefs");
		
		root_node->node_state = *root_node->node_node_id->nid_lhs_state_p;
		root_node->node_node_id->nid_ref_count_copy=root_node->node_node_id->nid_refcount;
		
		for_l (arg_p,root_node->node_arguments,arg_next){
			NodeP arg_node_p;

			arg_node_p=arg_p->arg_node;
			scope=old_scope;
			
			if (arg_node_p->node_kind==CaseNode){
				NodeP case_alt_node_p;
				
				case_alt_node_p=arg_node_p->node_arguments->arg_node;
				if (case_alt_node_p->node_kind==PushNode){
					NodeIdP node_id_p;
					StateP node_id_state_p;
					NodeIdListElementP node_ids;
					
					node_id_p=case_alt_node_p->node_arguments->arg_node->node_node_id;
					node_id_state_p=node_id_p->nid_lhs_state_p;
					node_ids=case_alt_node_p->node_node_ids;
					
					switch (node_id_state_p->state_type){
						case RecordState:
							set_push_node_id_states (node_ids,case_alt_node_p->node_record_symbol->symb_def->sdef_record_state.state_record_arguments);
							break;
						case TupleState:
							set_push_node_id_states (node_ids,node_id_state_p->state_tuple_arguments);
							break;
						default:
						{
							Symbol symbol;
							
							symbol=case_alt_node_p->node_record_symbol;
							
							if (symbol->symb_kind==definition){
								SymbDef sdef;

								sdef=symbol->symb_def;

								if (sdef->sdef_kind==CONSTRUCTOR){																			
# ifdef REUSE_UNIQUE_NODES
									AttributeKind lhs_type_attribute;
									
									lhs_type_attribute=sdef->sdef_type->type_lhs->ft_attribute;

									if (lhs_type_attribute==UniqueAttr && (node_id_state_p->state_mark & STATE_UNIQUE_MASK)==0){
										StateP unique_state_p;
										
										unique_state_p=CompAllocType (StateS);
										*unique_state_p=*node_id_state_p;
										unique_state_p->state_mark |= STATE_UNIQUE_MASK;

										node_id_state_p=unique_state_p;
										node_id_p->nid_lhs_state_p=unique_state_p;
									}
									
									if (sdef->sdef_arity==case_alt_node_p->node_arity && (node_id_state_p->state_mark & STATE_UNIQUE_MASK)){
										NodeIdListElementP node_ids_elem;

										if (sdef->sdef_strict_constructor){
											struct type_arg *type_arg_p;
											StateP constructor_arg_state_p;
											
											for_lla (node_ids_elem,type_arg_p,constructor_arg_state_p,
													node_ids,sdef->sdef_constructor->cl_constructor->type_node_arguments,symbol->symb_def->sdef_constructor->cl_state_p,
													nidl_next,type_arg_next)
											{
												NodeIdP node_id_p;
												
												node_id_p=node_ids_elem->nidl_node_id;
												node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
												
												node_id_p->nid_lhs_state_p_=determine_unique_state_of_constructor_argument (constructor_arg_state_p,type_arg_p->type_arg_node,lhs_type_attribute);
											}							
										} else {
											struct type_arg *type_arg_p;
											
											for_ll (node_ids_elem,type_arg_p,node_ids,sdef->sdef_constructor->cl_constructor->type_node_arguments,nidl_next,type_arg_next){
												NodeIdP node_id_p;
												
												node_id_p=node_ids_elem->nidl_node_id;
												node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
												
												node_id_p->nid_lhs_state_p_=determine_unique_state_of_constructor_argument (&LazyState,type_arg_p->type_arg_node,lhs_type_attribute);
											}						
										}
									} else
# endif				
									if (sdef->sdef_strict_constructor && sdef->sdef_arity==case_alt_node_p->node_arity)
										set_push_node_id_states (node_ids,sdef->sdef_constructor->cl_state_p);
									else
										set_lazy_push_node_id_states (node_ids);
								} else if (sdef->sdef_kind==RECORDTYPE){
# ifdef REUSE_UNIQUE_NODES
									AttributeKind lhs_type_attribute;
									
									lhs_type_attribute=sdef->sdef_type->type_lhs->ft_attribute;

									if (lhs_type_attribute==UniqueAttr && (node_id_state_p->state_mark & STATE_UNIQUE_MASK)==0){
										StateP unique_state_p;
										
										unique_state_p=CompAllocType (StateS);
										*unique_state_p=*node_id_state_p;
										unique_state_p->state_mark |= STATE_UNIQUE_MASK;

										node_id_state_p=unique_state_p;
										node_id_p->nid_lhs_state_p=unique_state_p;
									}
									
									if (node_id_state_p->state_mark & STATE_UNIQUE_MASK){
										NodeIdListElementP node_ids_elem;
										StateP arg_state_p;
										FieldList field;

										for_lla (node_ids_elem,field,arg_state_p,node_ids,
												 sdef->sdef_type->type_fields,sdef->sdef_record_state.state_record_arguments,nidl_next,fl_next)
										{
											NodeIdP node_id_p;
											
											node_id_p=node_ids_elem->nidl_node_id;
											node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
											
											node_id_p->nid_lhs_state_p_=determine_unique_state_of_constructor_argument (arg_state_p,field->fl_type,lhs_type_attribute);
										}
									} else				
# endif

									set_push_node_id_states (node_ids,sdef->sdef_record_state.state_record_arguments);
								} else
									set_lazy_push_node_id_states (node_ids);
							} else
# ifdef REUSE_UNIQUE_NODES
							if (symbol->symb_kind==cons_symb && (node_id_state_p->state_mark & STATE_UNIQUE_MASK) && case_alt_node_p->node_arity==2){
								NodeIdP node_id_p;
								StateP element_state_p;
								
								node_id_p=node_ids->nidl_node_id;
								node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
#  if STRICT_LISTS
								if (symbol->symb_head_strictness>1)
									if (symbol->symb_head_strictness==4)
										element_state_p=symbol->symb_state_p;
									else
										element_state_p=&StrictState;
								else
#  endif
								element_state_p=&LazyState;

								if ((node_id_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (node_id_state_p->state_unq_type_args & 1)){
									StateP unique_state_p;
									
									unique_state_p=CompAllocType (StateS);
									*unique_state_p=*element_state_p;
									unique_state_p->state_mark |= STATE_UNIQUE_MASK;

									node_id_p->nid_lhs_state_p_=unique_state_p;
								} else
									node_id_p->nid_lhs_state_p_=element_state_p;
																
								node_ids=node_ids->nidl_next;
								
								node_id_p=node_ids->nidl_node_id;
								node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
								
								{
									StateP unique_state_p;
									
									unique_state_p=CompAllocType (StateS);
#  if STRICT_LISTS
									if (symbol->symb_tail_strictness)
										*unique_state_p=StrictState;
									else
#  endif
									*unique_state_p=LazyState;
									
									unique_state_p->state_mark |= STATE_UNIQUE_MASK;
									if ((node_id_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (node_id_state_p->state_unq_type_args & 1)){
										unique_state_p->state_mark |= STATE_UNIQUE_TYPE_ARGUMENTS_MASK;
										unique_state_p->state_unq_type_args = 1;
									}

									node_id_p->nid_lhs_state_p_=unique_state_p;
								}
							} else if (symbol->symb_kind==tuple_symb && (node_id_state_p->state_mark & STATE_UNIQUE_MASK)){
								NodeIdListElementP node_ids_elem;
								int i;

								for_li (node_ids_elem,i,node_ids,nidl_next){
									NodeIdP node_id_p;
									
									node_id_p=node_ids_elem->nidl_node_id;
									node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
									
									if ((node_id_state_p->state_mark & STATE_UNIQUE_TYPE_ARGUMENTS_MASK) && (node_id_state_p->state_unq_type_args & (1<<i))){
										StateP unique_state_p;
										
										unique_state_p=CompAllocType (StateS);
										*unique_state_p=LazyState;
										unique_state_p->state_mark |= STATE_UNIQUE_MASK;

										node_id_p->nid_lhs_state_p_=unique_state_p;
									} else
										node_id_p->nid_lhs_state_p_=&LazyState;
								}
							} else
# endif
# if STRICT_LISTS
							if (symbol->symb_kind==cons_symb && (symbol->symb_head_strictness>1 || symbol->symb_tail_strictness) && case_alt_node_p->node_arity==2){
								NodeIdP node_id_p;
							
								node_id_p=node_ids->nidl_node_id;
								node_id_p->nid_lhs_state_p_= symbol->symb_head_strictness>1 ? (symbol->symb_head_strictness==4 ? symbol->symb_state_p : &StrictState) : &LazyState;
								node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;

								node_id_p=node_ids->nidl_next->nidl_node_id;
								node_id_p->nid_lhs_state_p_= symbol->symb_tail_strictness ? &StrictState : &LazyState;
								node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
							} else
# endif
							set_lazy_push_node_id_states (node_ids);
						}
					}
					
					DetermineStatesOfNodeAndDefs (case_alt_node_p->node_arguments->arg_next->arg_node,arg_node_p->node_node_defs,demstate,local_scope);
				} else
					DetermineStatesOfNodeAndDefs (case_alt_node_p,arg_node_p->node_node_defs,demstate,local_scope);
			} else if (arg_node_p->node_kind==DefaultNode){
				DetermineStatesOfNodeAndDefs (arg_node_p->node_arguments->arg_node,arg_node_p->node_node_defs,demstate,local_scope);
			} else if (arg_node_p->node_kind==OverloadedCaseNode){
				DetermineStatesOfNodeAndDefs (arg_node_p->node_node->node_arguments->arg_node,arg_node_p->node_node->node_node_defs,demstate,local_scope);
			} else
				error_in_function ("DetermineStatesOfNodeAndDefs");
		}
	} else if (root_node->node_kind==GuardNode){
		int old_scope;
		
 		old_scope=scope;
		DetermineStatesOfNodeAndDefs (root_node->node_arguments->arg_node,node_defs,demstate,local_scope);
		scope=old_scope;
		DetermineStatesOfNodeAndDefs (root_node->node_arguments->arg_next->arg_node,root_node->node_node_defs,demstate,local_scope);
	} else {
		if (root_node->node_kind==NodeIdNode){
			NodeId node_id;
			
			node_id=root_node->node_node_id;
			if (node_id->nid_node==NULL || node_id->nid_ref_count_copy<0)
				return;
			
			DetermineStatesRootNode (node_id->nid_node,node_id,demstate,local_scope);
		} else
			DetermineStatesRootNode (root_node,NULL,demstate,local_scope);

		if (node_defs)
			DetermineStatesOfNodeDefs (node_defs,local_scope);
	}
}
#endif

void DetermineStatesOfRootNodeAndDefs (Node root_node,NodeDefs *rootdef,StateS demstate,int local_scope)
{
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
	DetermineStatesOfNodeAndDefs (root_node,*rootdef,demstate,local_scope);
#else
	ShouldDecrRefCount = True;

	if (root_node->node_kind==NodeIdNode){
		NodeId node_id;
		
		node_id=root_node->node_node_id;
		if (node_id->nid_node==NULL || node_id->nid_ref_count_copy<0)
			return;
		
		DetermineStatesRootNode (node_id->nid_node,node_id,demstate,local_scope);
	} else
		DetermineStatesRootNode (root_node,NULL,demstate,local_scope);

	if (*rootdef)
		DetermineStatesOfNodeDefs (*rootdef,local_scope);
#endif
}

#ifdef OBSERVE_ARRAY_SELECTS_IN_PATTERN
	static void set_states_of_array_selects_in_pattern (RuleAlts alt)
	{
		StrictNodeIdP strict_node_id;

		for_l (strict_node_id,alt->alt_strict_node_ids,snid_next){
			if (strict_node_id->snid_array_select_in_pattern){
				NodeP select_node,array_uselect_node;
				NodeId node_id;

				node_id=strict_node_id->snid_node_id;
				if (node_id->nid_node->node_symbol->symb_kind==select_symb){							
					select_node=node_id->nid_node;
					array_uselect_node=select_node->node_arguments->arg_node;
				} else {
					select_node=NULL;
					array_uselect_node=node_id->nid_node;
				}
				
				if (array_uselect_node->node_state.state_type==TupleState){
					StateP tuple_arg_states;

					tuple_arg_states=NewArrayOfStates (2);
					SetUnaryState (&tuple_arg_states[1],Undefined,UnknownObj);
					tuple_arg_states[0]=array_uselect_node->node_state.state_tuple_arguments[0];
					SetTupleState (&array_uselect_node->node_state,tuple_arg_states,2);

					if (select_node!=NULL && select_node->node_state.state_type==SimpleState
						&& select_node->node_state.state_kind==StrictOnA
						&& select_node->node_arguments->arg_state.state_type==TupleState
						&& select_node->node_arguments->arg_state.state_tuple_arguments[0].state_type==SimpleState
						&& select_node->node_arguments->arg_state.state_tuple_arguments[0].state_kind==StrictOnA
					){
						select_node->node_state=array_uselect_node->node_state.state_tuple_arguments[0];
						SetTupleState (&select_node->node_arguments->arg_state,tuple_arg_states,2);
					}
				}
			}
		}
	}
#endif				

#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static void set_states_in_lhs (ArgP arguments,StateP states)
{
	StateP arg_state_p;
	ArgP arg_p;

	for_la (arg_p,arg_state_p,arguments,states,arg_next){
		Node arg_node;
		
		arg_p->arg_state=*arg_state_p;
		
		arg_node=arg_p->arg_node;

		if (arg_node->node_kind==NodeIdNode){
			NodeId node_id_p;
			
			node_id_p=arg_node->node_node_id;
			node_id_p->nid_lhs_state_p_=&arg_p->arg_state;
			node_id_p->nid_ref_count_copy=node_id_p->nid_refcount;
			
			arg_node=node_id_p->nid_node;
			if (arg_node!=NULL){
				if (arg_state_p->state_type==TupleState)
					set_states_in_lhs (arg_node->node_arguments,arg_state_p->state_tuple_arguments);
				else if (arg_state_p->state_type==RecordState)
					set_states_in_lhs (arg_node->node_arguments,arg_state_p->state_record_arguments);
			}
		} else {
			if (arg_state_p->state_type==TupleState)
				set_states_in_lhs (arg_node->node_arguments,arg_state_p->state_tuple_arguments);
			else if (arg_state_p->state_type==RecordState)
				set_states_in_lhs (arg_node->node_arguments,arg_state_p->state_record_arguments);
			else
				error_in_function ("set_states_in_lhs");
		}
	}
}
#endif

static void mark_is_constructor_function (ImpRuleP rule)
{
	RuleAltP alt;

	alt=rule->rule_alts;
	if (alt->alt_kind==Contractum && alt->alt_next==NULL && alt->alt_rhs_defs==NULL && alt->alt_lhs_defs==NULL){
		NodeP node_p;
		ArgP arg_p;
		
		arg_p=alt->alt_lhs_root->node_arguments;
		if (arg_p!=NULL && arg_p->arg_next==NULL && arg_p->arg_node->node_kind==NodeIdNode){
			node_p=alt->alt_rhs_root;
			if (node_p->node_kind==SwitchNode){
				NodeP case_node;

				arg_p=node_p->node_arguments;
				case_node=arg_p->arg_node;
				if (case_node->node_kind==CaseNode){
					struct symbol *symbol;
					NodeP case_rhs_node;

					case_rhs_node = case_node->node_arguments->arg_node;
					if (case_rhs_node->node_kind==PushNode)
						case_rhs_node = case_rhs_node->node_arguments->arg_next->arg_node;

					symbol=case_node->node_symbol;
					if (((symbol->symb_kind==definition && symbol->symb_def->sdef_kind==CONSTRUCTOR) ||
						 symbol->symb_kind==nil_symb || symbol->symb_kind==cons_symb) &&
						case_node->node_node_defs==NULL &&
						case_rhs_node->node_kind==NormalNode && case_rhs_node->node_symbol->symb_kind==bool_denot &&
						case_rhs_node->node_symbol->symb_bool==True)
					{
						arg_p=arg_p->arg_next;
						if (arg_p!=NULL){
							NodeP default_node;

							default_node=arg_p->arg_node;
							if (arg_p->arg_node->node_kind==DefaultNode){
								NodeP default_rhs_node;
								
								default_rhs_node=default_node->node_arguments->arg_node;
								if (default_node->node_node_defs==NULL &&
									default_rhs_node->node_kind==NormalNode && default_rhs_node->node_symbol->symb_kind==bool_denot &&
									default_rhs_node->node_symbol->symb_bool==False)
								{
									rule->rule_root->node_symbol->symb_def->sdef_mark |= SDEF_INLINE_IS_CONSTRUCTOR;
								}
							}
						}
					}
				}
			}
		}
	}
}

void GenerateStatesForRule (ImpRuleS *rule)
{
	SymbDef rule_sdef;
	RuleAlts alt;
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
	StateP function_state_p;
#endif

	CurrentSymbol=rule->rule_root->node_symbol;
	rule_sdef=CurrentSymbol->symb_def;

#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
	function_state_p=rule->rule_state_p;
#endif

	for_l (alt,rule->rule_alts,alt_next){
		CurrentLine = alt->alt_line;
	
#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
		set_states_in_lhs (alt->alt_lhs_root->node_arguments,function_state_p);
		alt->alt_lhs_root->node_state = function_state_p[-1]; /* i.e. the result state */
#endif

		scope=1;

		if (alt->alt_kind==Contractum){
			DetermineStatesOfRootNodeAndDefs (alt->alt_rhs_root,&alt->alt_rhs_defs,alt->alt_lhs_root->node_state,0);

#ifdef OBSERVE_ARRAY_SELECTS_IN_PATTERN
			set_states_of_array_selects_in_pattern (alt);
#endif
		} else if (rule->rule_type==NULL)
			StaticMessage (True, "%S", ECodeBlock, CurrentSymbol);
	}

	if (rule_sdef->sdef_arity==1 &&
			function_state_p[-1].state_type==SimpleState && function_state_p[-1].state_kind==OnB && function_state_p[-1].state_object==BoolObj &&
			function_state_p[0].state_type==SimpleState && function_state_p[0].state_kind==StrictOnA)
		mark_is_constructor_function (rule);
}

void GenerateStates (ImpRules rules)
{
	ImpRuleS *rule;
	
	for_l (rule,rules,rule_next)
		GenerateStatesForRule (rule);
}

static NodeDefS **RemoveLocallySharedNodeDefs (NodeDefS **start,NodeDefS **end,NodeDefS **loclist,int scope)
{
	NodeDefS **newend,**current;
	
	newend = start;
	current = start;

	while (current!=end){
		int node_id_scope;
		
		node_id_scope=(*current)->def_id->nid_scope;
		if (node_id_scope<0)
			node_id_scope=-node_id_scope;
		
		if (node_id_scope>scope){
			NodeDefP remove;
			
			remove = *current;
			*start = remove->def_next;
			current = &remove->def_next;
			*loclist = remove;
			loclist = current;
		} else {
			current = start = &(*start)->def_next;
			newend = start;
		} 
	}
	
	*loclist = NULL;
	
	return newend;
}

void DetermineNodeState (Node node)
{
	if (node->node_annotation==NoAnnot)
		node->node_state=LazyState;
	else if (node->node_annotation==StrictAnnot)
		node->node_state=StrictState;
	else {
		SetUnaryState (&node->node_state, DoParallel ? Parallel : OnA, UnknownObj);
		if (DoParallel)
			node->node_state.state_mark |= STATE_PARALLEL_MASK;

		if (node->node_state.state_kind==Parallel){
			if (DoParallel)
				/* node->node_attribute = AnnotHasDeferAttr (node->node_annotation->annot_kind) */;
			else {
				StaticMessage (False, "%S", Wparannot, CurrentSymbol);
				node->node_state.state_kind = OnA;
			}
		}
	}
}

static int NodeIdCount;
static NodeId NodeIdStackTop;

static Bool MarkComponentNodesOnACycle (Node node,int group_number)
{
	if (node->node_number!=0)
		return node->node_number<0;
	
	switch (node->node_kind){
		case NodeIdNode:
		{
			NodeId node_id;
			
			node_id=node->node_node_id;
			
			if (node_id->nid_mark & ON_A_CYCLE_MASK && node_id->nid_number==group_number){
				node->node_number=-1;
				MarkComponentNodesOnACycle (node_id->nid_node,group_number);
				return True;
			} else {
				node->node_number=1;
				return False;
			}
		}
		case NormalNode:
		case UpdateNode:
		case SelectorNode:
		case MatchNode:
		{
			ArgS *arg;
			Bool on_a_cycle;
		
			on_a_cycle=False;
			
			node->node_number=1;
			for_l (arg,node->node_arguments,arg_next)
				if (MarkComponentNodesOnACycle (arg->arg_node,group_number))
					on_a_cycle=True;
			
			if (on_a_cycle)
				node->node_number=-1;
			
			return on_a_cycle;
		}
		case IfNode:
		default:
			error_in_function ("MarkComponentNodesOnACycle");
			return False;
	}
}

static void AddStrictLhsNodeIdsToNodeDefs (StrictNodeIdP strict_node_id,NodeDefs *defs_p)
{
	while (strict_node_id){
		NodeId node_id;

		node_id=strict_node_id->snid_node_id;

		if (node_id->nid_refcount<0){
			NodeDefS *new_def;
			
			new_def = NewNodeDef (node_id,NULL);
/*				node_id->nid_node_def = new_def; */
			
			new_def->def_next=*defs_p;
			*defs_p=new_def;
			defs_p=&new_def->def_next;
		} else if ((strict_node_id->snid_mark & STRICT_NODE_ID_OBSERVE_MASK) && node_id->nid_refcount>0 && node_id->nid_node!=NULL){
			if (node_id->nid_node_def!=NULL)
				node_id->nid_node_def->def_mark |= NODE_DEF_OBSERVE_MASK;
		}

		strict_node_id=strict_node_id->snid_next;
	}
}

#ifdef ADD_ARGUMENTS_TO_HIGHER_ORDER_FUNCTIONS
static ImpRuleP new_rules_with_more_arguments,*last_new_rule_with_more_arguments_h;

static int get_symbol_arity_or_zero (SymbolP symbol_p)
{
	switch (symbol_p->symb_kind){
		case definition:
		{
			SymbDef sdef;
			
			sdef=symbol_p->symb_def;
			
			switch (sdef->sdef_kind){
				case DEFRULE:
				case SYSRULE:
				case IMPRULE:
				case CONSTRUCTOR:
					return sdef->sdef_arity;
				case RECORDTYPE:
					return sdef->sdef_cons_arity;
			}
			break;
		}
		case cons_symb:
			return 2;
		case if_symb:
			return 3;
	}
	
	return 0;
}

static NodeP add_argument_to_node (NodeP rhs_root_p,NodeIdP new_node_id_p);

static NodeP add_argument_to_if_node (NodeP rhs_root_p,NodeIdP new_node_id_p)
{
	ArgP then_arg_p,else_arg_p;
	NodeP else_node_p;
	
	then_arg_p=rhs_root_p->node_arguments->arg_next;
	else_arg_p=then_arg_p->arg_next;
	else_node_p=else_arg_p->arg_node;

	then_arg_p->arg_node=add_argument_to_node (then_arg_p->arg_node,new_node_id_p);
	
	if (else_node_p->node_kind!=NormalNode || else_node_p->node_symbol->symb_kind!=fail_symb){
		--new_node_id_p->nid_refcount;
		else_arg_p->arg_node=add_argument_to_node (else_node_p,new_node_id_p);
	}
				
	return rhs_root_p;
}

static NodeP add_argument_to_switch_node (NodeP rhs_root_p,NodeIdP new_node_id_p)
{
	ArgP arg_p;

	for_l (arg_p,rhs_root_p->node_arguments,arg_next){
		NodeP node_p,*node_h;

		node_p=arg_p->arg_node;
		if (node_p->node_kind==CaseNode){
			node_h=&node_p->node_arguments->arg_node;
			if ((*node_h)->node_kind==PushNode)
				node_h=&(*node_h)->node_arguments->arg_next->arg_node;
		} else if (node_p->node_kind==DefaultNode){
			node_h=&node_p->node_arguments->arg_node;
		} else if (node_p->node_kind==OverloadedCaseNode){
			node_h=&node_p->node_node->node_arguments->arg_node;
		} else
			error_in_function ("add_argument_to_switch_node");

		*node_h=add_argument_to_node (*node_h, new_node_id_p);

		--new_node_id_p->nid_refcount;
	}

	++new_node_id_p->nid_refcount;

	return rhs_root_p;
}

static NodeP add_argument_to_guard_node (NodeP rhs_root_p,NodeIdP new_node_id_p)
{
	add_argument_to_node (rhs_root_p->node_arguments->arg_node, new_node_id_p);
	--new_node_id_p->nid_refcount;
	add_argument_to_node (rhs_root_p->node_arguments->arg_next->arg_node, new_node_id_p);

	return rhs_root_p;
}

static NodeP add_argument_to_node (NodeP rhs_root_p,NodeIdP new_node_id_p)
{
	ArgP new_arg1,new_arg2;

	if (rhs_root_p->node_kind==NormalNode){
		SymbolP root_symbol_p;
		
		root_symbol_p=rhs_root_p->node_symbol;
		if (root_symbol_p->symb_kind==if_symb && rhs_root_p->node_arity==3)
			return add_argument_to_if_node (rhs_root_p,new_node_id_p);
		else if (root_symbol_p->symb_kind == fail_symb)
			return rhs_root_p;
		else if (get_symbol_arity_or_zero (root_symbol_p) > rhs_root_p->node_arity){
			ArgP *last_rhs_arg_h;
			
			new_arg2=NewArgument (NewNodeIdNode (new_node_id_p));
		
			last_rhs_arg_h=&rhs_root_p->node_arguments;
			while (*last_rhs_arg_h)
				last_rhs_arg_h=&(*last_rhs_arg_h)->arg_next;
			
			*last_rhs_arg_h=new_arg2;
			++rhs_root_p->node_arity;
			
			return rhs_root_p;
		}
	} else if (rhs_root_p->node_kind==IfNode)
		return add_argument_to_if_node (rhs_root_p,new_node_id_p);
	else if (rhs_root_p->node_kind==SwitchNode)
		return add_argument_to_switch_node (rhs_root_p,new_node_id_p);
	else if (rhs_root_p->node_kind==GuardNode)
		return add_argument_to_guard_node (rhs_root_p,new_node_id_p);

	new_arg2=NewArgument (NewNodeIdNode (new_node_id_p));

	new_arg1=NewArgument (rhs_root_p);
	new_arg1->arg_next=new_arg2;
	rhs_root_p=NewNode (ApplySymbol,new_arg1,2);
	
	return rhs_root_p;
}

static SymbolP copy_imp_rule_and_add_arguments (SymbDef rule_sdef,int n_extra_arguments)
{
	SymbolP new_symbol_p;
	SymbDef new_sdef_p;
	ImpRuleP old_rule_p,new_rule_p,last_rule_version_p;
	int n_wanted_arguments;
	
	n_wanted_arguments=rule_sdef->sdef_arity + n_extra_arguments;
	
	old_rule_p=rule_sdef->sdef_rule;
	
	while (old_rule_p->rule_mark & RULE_HAS_VERSION_WITH_MORE_ARGUMENTS){
		old_rule_p=old_rule_p->rule_next_function_with_more_arguments;
		
		if (old_rule_p->rule_root->node_symbol->symb_def->sdef_arity==n_wanted_arguments)
			return old_rule_p->rule_root->node_symbol;
	}
	
	new_symbol_p=copy_imp_rule_and_type (rule_sdef);

	new_sdef_p=new_symbol_p->symb_def;
	new_rule_p=new_sdef_p->sdef_rule;
	old_rule_p=rule_sdef->sdef_rule;

	last_rule_version_p=old_rule_p;
	while (last_rule_version_p->rule_mark & RULE_HAS_VERSION_WITH_MORE_ARGUMENTS)
		last_rule_version_p=last_rule_version_p->rule_next_function_with_more_arguments;

	last_rule_version_p->rule_mark |= RULE_HAS_VERSION_WITH_MORE_ARGUMENTS;	
	last_rule_version_p->rule_next_function_with_more_arguments=new_rule_p;

	new_sdef_p->sdef_next_scc=rule_sdef->sdef_next_scc;
	rule_sdef->sdef_next_scc=new_sdef_p;

	copy_imp_rule_nodes (old_rule_p,new_rule_p);

	{
		struct type_alt *rule_type;
		struct type_node *rhs_type_node_p;
		struct type_arg **last_lhs_type_arg_p;
		int n;
		
		rule_type=new_rule_p->rule_type;
		rhs_type_node_p=rule_type->type_alt_rhs;
		last_lhs_type_arg_p=&rule_type->type_alt_lhs->type_node_arguments;
		while (*last_lhs_type_arg_p)
			last_lhs_type_arg_p=&(*last_lhs_type_arg_p)->type_arg_next;
		
		for (n=0; n<n_extra_arguments; ++n){
#if 0
			if (rhs_type_node_p->type_node_is_var){
				struct type_arg *new_type_arg_p;
				
				new_type_arg_p=NewTypeArgument (NewTypeVarNode (NewTypeVar (NULL),NoAnnot,NoAttr));

				*last_lhs_type_arg_p=new_type_arg_p;
				last_lhs_type_arg_p=&new_type_arg_p->type_arg_next;
			} else
#else				
			if (rhs_type_node_p->type_node_is_var || rhs_type_node_p->type_node_symbol->symb_kind!=fun_type)
				error_in_function ("copy_imp_rule_and_add_arguments");
#endif
			{
				struct type_arg *first_arg_p;
				
				if (rhs_type_node_p->type_node_symbol->symb_kind!=fun_type)
					error_in_function ("copy_imp_rule_and_add_arguments");
							
				first_arg_p=rhs_type_node_p->type_node_arguments;
				*last_lhs_type_arg_p=first_arg_p;
				
				first_arg_p->type_arg_node->type_node_annotation=NoAnnot;
				
				last_lhs_type_arg_p=&first_arg_p->type_arg_next;

				rhs_type_node_p=first_arg_p->type_arg_next->type_arg_node;
			}
		}
		
		*last_lhs_type_arg_p=NULL;
		rule_type->type_alt_rhs=rhs_type_node_p;
		
		rule_type->type_alt_lhs->type_node_arity += n_extra_arguments;
	}
	
	{
		RuleAltP alt_p;
		
		for_l (alt_p,new_rule_p->rule_alts,alt_next){
			int n;
			ArgP *last_lhs_arg_h;
			
			last_lhs_arg_h=&alt_p->alt_lhs_root->node_arguments;
			while (*last_lhs_arg_h)
				last_lhs_arg_h=&(*last_lhs_arg_h)->arg_next;
			
			for (n=0; n<n_extra_arguments; ++n){
				NodeIdP new_node_id_p;
				ArgP new_arg;
				
				new_node_id_p=NewNodeId (NULL);
				new_node_id_p->nid_refcount=-2;
				
				new_arg=NewArgument (NewNodeIdNode (new_node_id_p));
				
				*last_lhs_arg_h=new_arg;
				last_lhs_arg_h=&new_arg->arg_next;
								
				alt_p->alt_rhs_root=add_argument_to_node (alt_p->alt_rhs_root,new_node_id_p);
			}
			
			*last_lhs_arg_h=NULL;
			
			alt_p->alt_lhs_root->node_arity += n_extra_arguments;
		}
		
		new_sdef_p->sdef_arity += n_extra_arguments;
	}
	
	new_rule_p->rule_next=NULL;
	
	*last_new_rule_with_more_arguments_h=new_rule_p;
	last_new_rule_with_more_arguments_h=&new_rule_p->rule_next;
	
	return new_symbol_p;
}

static int create_new_function_with_more_arguments (NodeP node_p,int determine_node_state)
{
	NodeP function_node_p;
	int n_extra_arguments;
	
	n_extra_arguments=1;
	function_node_p=node_p->node_arguments->arg_node;

	if (function_node_p->node_kind==NodeIdNode && function_node_p->node_node_id->nid_refcount==1 && function_node_p->node_node_id->nid_node->node_annotation==NoAnnot){
		function_node_p=function_node_p->node_node_id->nid_node;
		node_p->node_arguments->arg_node=function_node_p;
	}
	
	while (function_node_p->node_kind==NormalNode && function_node_p->node_symbol->symb_kind==apply_symb){
		ArgP next_function_node_p_arg;
		
		next_function_node_p_arg=function_node_p->node_arguments;
		function_node_p=next_function_node_p_arg->arg_node;
		++n_extra_arguments;

		if (function_node_p->node_kind==NodeIdNode && function_node_p->node_node_id->nid_refcount==1 && function_node_p->node_node_id->nid_node->node_annotation==NoAnnot){
			function_node_p=function_node_p->node_node_id->nid_node;
			next_function_node_p_arg->arg_node=function_node_p;
		}
	}
	
	if (function_node_p->node_kind==NormalNode){
		SymbolP function_symbol_p;
		
		function_symbol_p=function_node_p->node_symbol;

		if (function_symbol_p->symb_kind==definition){
			if (function_symbol_p->symb_def->sdef_kind==IMPRULE){
				if (!(function_symbol_p->symb_def->sdef_rule->rule_mark & RULE_CAF_MASK) && function_symbol_p->symb_def->sdef_rule->rule_alts->alt_kind==Contractum
					&& function_node_p->node_arity <= function_symbol_p->symb_def->sdef_arity)
				{
					struct type_node *rhs_type_node_p;
					SymbolP new_function_symbol;
					SymbDef rule_sdef;
					int n_extra_function_arguments,n;

					rule_sdef=function_symbol_p->symb_def;
					rhs_type_node_p=rule_sdef->sdef_rule->rule_type->type_alt_rhs;
					
					n_extra_function_arguments=n_extra_arguments+function_node_p->node_arity-rule_sdef->sdef_arity;
					
					n=n_extra_function_arguments;
					while (n>0){
						if (rhs_type_node_p->type_node_is_var){
#if 0
							n=0;
#endif
							break;
						} else if (rhs_type_node_p->type_node_symbol->symb_kind==fun_type){
							rhs_type_node_p=rhs_type_node_p->type_node_arguments->type_arg_next->type_arg_node;
							--n;
						} else
							break;
					}
										
					if (n<=0){
						if (n_extra_function_arguments>0){
							new_function_symbol=copy_imp_rule_and_add_arguments (rule_sdef,n_extra_function_arguments);					

							node_p->node_symbol=new_function_symbol;
						} else
							node_p->node_symbol=function_node_p->node_symbol;
					} else
						return 0;
				} else
					return 0;
			/* 26-6-2000: added DEFRULE and SYSRULE case */
			} else if (function_symbol_p->symb_def->sdef_kind==DEFRULE || function_symbol_p->symb_def->sdef_kind==SYSRULE){
				if (function_node_p->node_arity + n_extra_arguments <= function_symbol_p->symb_def->sdef_arity){
					node_p->node_symbol=function_node_p->node_symbol;
				} else
					return 0;
			} else
				return 0;

			{
				NodeP function_node_p2;
				ArgP *arg_h;

				function_node_p2=node_p->node_arguments->arg_node;
				node_p->node_arguments=node_p->node_arguments->arg_next;
				
				while (function_node_p2!=function_node_p){
					ArgP second_arg_p;
					
					second_arg_p=function_node_p2->node_arguments->arg_next;
					
					second_arg_p->arg_next=node_p->node_arguments;
					node_p->node_arguments=second_arg_p;
					
					function_node_p2=function_node_p2->node_arguments->arg_node;							
				}
									
				arg_h=&function_node_p->node_arguments;
				while (*arg_h!=NULL)
					arg_h=&(*arg_h)->arg_next;
				
				*arg_h=node_p->node_arguments;
				node_p->node_arguments=function_node_p->node_arguments;
				
				node_p->node_arity=function_node_p->node_arity+n_extra_arguments;
				
				return 1;
			}
		} else if (function_symbol_p->symb_kind==if_symb && function_node_p->node_arity==3){
			NodeP apply_node_p;
			int n_lhs_node_id_applies;
			
			n_lhs_node_id_applies=0;
			apply_node_p=node_p;
			while (apply_node_p->node_kind==NormalNode && apply_node_p->node_symbol->symb_kind==apply_symb){
				ArgP apply_arg1;
				NodeP apply_arg2_node_p;
				
				apply_arg1=apply_node_p->node_arguments;
				apply_arg2_node_p=apply_arg1->arg_next->arg_node;
				
				if (apply_arg2_node_p->node_kind==NodeIdNode && apply_arg2_node_p->node_node_id->nid_refcount<0){
					apply_node_p=apply_arg1->arg_node;
					++n_lhs_node_id_applies;
				} else
					break;
			}
			
			if (n_lhs_node_id_applies==n_extra_arguments){
				int n;
				
				for (n=0; n<n_extra_arguments; ++n){
					int m;
					
					apply_node_p=node_p;

					for (m=0; m<n_extra_arguments-1-n; ++m)
						apply_node_p=apply_node_p->node_arguments->arg_node;

					function_node_p=add_argument_to_node (function_node_p,apply_node_p->node_arguments->arg_next->arg_node->node_node_id);
				}
				
				*node_p=*function_node_p;
				if (determine_node_state)
					DetermineNodeState (node_p);
			}
		}
	}
	
	return 0;
}
#endif

static NodeDefs *CollectSharedNodeIdsInNode (Node* node_p,NodeId parent_node_id,NodeDefs *last)
{
	Node node;
	
	node=*node_p;
	if (node->node_kind==NodeIdNode){
		NodeId node_id;

		node_id=node->node_node_id;

		if (node_id->nid_refcount>0){
			if (!(node_id->nid_mark & SHARED_NODES_COLLECTED_MASK)){
				node_id->nid_mark |= SHARED_NODES_COLLECTED_MASK;

				node_id->nid_ref_count_copy_=node_id->nid_refcount;

				if (node_id->nid_refcount>1 || node_id->nid_node->node_annotation){
					int my_number;
				
					my_number=NodeIdCount++;

					node_id->nid_number=my_number;
					node_id->nid_forward_node_id_=NodeIdStackTop;
					NodeIdStackTop=node_id;
					
					last = CollectSharedNodeIdsInNode (&node_id->nid_node,node_id,last);
				
					if (/* node_id->nid_forward_node_id && */ parent_node_id)
						parent_node_id->nid_number=MINIMUM (parent_node_id->nid_number,node_id->nid_number);

					if (node_id->nid_number==my_number){
						NodeId next_node_id;
						NodeDefs newdef;
							
						next_node_id = NodeIdStackTop;
						NodeIdStackTop = next_node_id->nid_forward_node_id;
						next_node_id->nid_forward_node_id_ = NULL;

						newdef = NewNodeDef (next_node_id,next_node_id->nid_node);
						next_node_id->nid_node_def_ = newdef;
						*last=newdef;
						last=&newdef->def_next;

						if (next_node_id!=node_id){
							next_node_id->nid_number=my_number;
							next_node_id->nid_mark|=ON_A_CYCLE_MASK;

							do {
								next_node_id = NodeIdStackTop;
								NodeIdStackTop = next_node_id->nid_forward_node_id;
								next_node_id->nid_forward_node_id_ = NULL;
						
								next_node_id->nid_number=my_number;
								next_node_id->nid_mark|=ON_A_CYCLE_MASK;

								newdef = NewNodeDef (next_node_id,next_node_id->nid_node);
								next_node_id->nid_node_def_ = newdef;
								*last=newdef;
								last=&newdef->def_next;
							} while (next_node_id!=node_id);
						}
						
						if (node_id->nid_mark & ON_A_CYCLE_MASK)
							MarkComponentNodesOnACycle (node_id->nid_node,node_id->nid_number);
					}
				} else {
					*node_p=node_id->nid_node;
					last = CollectSharedNodeIdsInNode (node_p,parent_node_id,last);
				}
			} else
				if (node_id->nid_forward_node_id){
					node_id->nid_mark|=ON_A_CYCLE_MASK;
					parent_node_id->nid_number=MINIMUM (parent_node_id->nid_number,node_id->nid_number);
				}
		} else
			node_id->nid_ref_count_copy_=node_id->nid_refcount;
	} else {
		DetermineNodeState (node);

		if (node->node_annotation==ParallelAtAnnot){
			Node at_node;
		
			at_node=get_p_at_node (node);

			last = CollectSharedNodeIdsInNode (&at_node,parent_node_id,last);
		}

		if (node->node_kind==IfNode){
			NodeDefs *shared;
			Args cond_arg,then_arg,else_arg;
			int local_scope;
	
			cond_arg=node->node_arguments;
			then_arg=cond_arg->arg_next;
			else_arg=then_arg->arg_next;
						
			local_scope=scope+1;
			scope+=3;

			cond_arg->arg_state=LazyState;
			
			/*{
				Node root_node;
		
				root_node=cond_arg->arg_node;
				if (root_node->node_kind==NodeIdNode && root_node->node_node_id->nid_refcount==1)
					root_node->node_node_id->nid_node->node_annotation=NoAnnot;
			}*/
			
			last = CollectSharedNodeIdsInNode (&cond_arg->arg_node,parent_node_id,last);
			++scope;
	
			shared=last;
			then_arg->arg_state=LazyState;
			
			/*{
				Node root_node;
		
				root_node=then_arg->arg_node;
				if (root_node->node_kind==NodeIdNode && root_node->node_node_id->nid_refcount==1)
					root_node->node_node_id->nid_node->node_annotation=NoAnnot;
			}*/
			
			last=CollectSharedNodeIdsInNode (&then_arg->arg_node,parent_node_id,last);
			last=RemoveLocallySharedNodeDefs (shared,last,&node->node_then_node_defs,local_scope);
	
			++scope;
			
			shared=last;
			else_arg->arg_state=LazyState;
			
			/*{
				Node root_node;
		
				root_node=else_arg->arg_node;
				if (root_node->node_kind==NodeIdNode && root_node->node_node_id->nid_refcount==1)
					root_node->node_node_id->nid_node->node_annotation=NoAnnot;
			}*/
			
			last=CollectSharedNodeIdsInNode (&else_arg->arg_node,parent_node_id,last);
			last=RemoveLocallySharedNodeDefs (shared,last,&node->node_else_node_defs,local_scope);

			AddStrictLhsNodeIdsToNodeDefs (node->node_then_strict_node_ids,&node->node_then_node_defs);
			AddStrictLhsNodeIdsToNodeDefs (node->node_else_strict_node_ids,&node->node_else_node_defs);
		}
		else if (node->node_kind==SwitchNode)
		{
			error_in_function ("CollectSharedNodeIdsInNode");
		}
		else {
			ArgP arg;

#ifdef ADD_ARGUMENTS_TO_HIGHER_ORDER_FUNCTIONS
			if (node->node_kind==NormalNode && node->node_symbol->symb_kind==apply_symb)
				(void) create_new_function_with_more_arguments (node,1);
#endif
			
			for_l (arg,node->node_arguments,arg_next){
				arg->arg_state=LazyState;
				last = CollectSharedNodeIdsInNode (&arg->arg_node,parent_node_id,last);
			}
		}
	}

	return last;
}

/* RWS ... */
/* parent_node_id always NULL? */
static NodeDefs *CollectSharedNodeIdsInRootNode (Node* node_p,NodeId parent_node_id,NodeDefs *last)
{
	NodeP	root_node;

	root_node=*node_p;

	switch (root_node->node_kind){
		case SwitchNode:
		{
			ArgP arg_p;
		
			for_l (arg_p,root_node->node_arguments,arg_next){
				NodeP node;
				
				node=arg_p->arg_node;
				if (node->node_kind==CaseNode){
					NodeP case_alt_node_p;
					NodeDefs *case_last;

					case_alt_node_p=node->node_arguments->arg_node;
					case_last=&node->node_node_defs;
/*	Codewarrior bug			if (case_alt_node_p->node_kind==PushNode){ */
					if (node->node_arguments->arg_node->node_kind==PushNode)
						case_last=CollectSharedNodeIdsInRootNode (&case_alt_node_p->node_arguments->arg_next->arg_node, parent_node_id, case_last);
					else
						case_last=CollectSharedNodeIdsInRootNode (&node->node_arguments->arg_node, parent_node_id, case_last);
					*case_last=NULL;

					AddStrictLhsNodeIdsToNodeDefs (node->node_strict_node_ids,&node->node_node_defs);
				} else if (node->node_kind==DefaultNode){
					NodeDefs *default_last;

					default_last=&node->node_node_defs;
					default_last=CollectSharedNodeIdsInRootNode (&node->node_arguments->arg_node, parent_node_id, default_last);
					*default_last=NULL;

					AddStrictLhsNodeIdsToNodeDefs (node->node_strict_node_ids,&node->node_node_defs);
				} else if (node->node_kind==OverloadedCaseNode){
					NodeP case_node_p;
					NodeDefs *case_last;
					ArgP arg;

					arg=node->node_arguments;
					arg->arg_state=LazyState;
					last = CollectSharedNodeIdsInNode (&arg->arg_node,parent_node_id,last);
					arg=arg->arg_next;
					arg->arg_state=LazyState;
					last = CollectSharedNodeIdsInNode (&arg->arg_node,parent_node_id,last);

					case_node_p=node->node_node;
					case_last=&case_node_p->node_node_defs;
					case_last=CollectSharedNodeIdsInRootNode (&case_node_p->node_arguments->arg_node, parent_node_id, case_last);
					*case_last=NULL;
				} else
					error_in_function ("CollectSharedNodeIdsInRootNode");
			}
			break;
		}
		case GuardNode:
		{
			NodeDefs *guard_last;

			last = CollectSharedNodeIdsInRootNode (&root_node->node_arguments->arg_node, parent_node_id, last);
			guard_last=&root_node->node_node_defs;
			guard_last=CollectSharedNodeIdsInRootNode (&root_node->node_arguments->arg_next->arg_node, parent_node_id, guard_last);
			*guard_last=NULL;
			break;
		}
		default:
			scope=1;
			last=CollectSharedNodeIdsInNode (node_p,parent_node_id,last);
			break;

	}

	return last;
}
/* ... RWS */

static void CollectSharedAndAnnotatedNodesInRhs (NodeS **root_p,NodeDefS **defs_p,StrictNodeIdP strict_node_ids)
{
	NodeDefS **last;
	NodeP root_node;
/*	
	scope=1;
*/
	NodeIdCount=1;
	NodeIdStackTop = (NodeId)-1;

	root_node=*root_p;
	
	/* removed, causes crash if let! in other scope
	if (root_node->node_kind==NodeIdNode && root_node->node_node_id->nid_refcount==1)
		root_node->node_node_id->nid_node->node_annotation=NoAnnot;
	*/

#ifdef ADD_ARGUMENTS_TO_HIGHER_ORDER_FUNCTIONS
	while (root_node->node_kind==NormalNode && 
		((root_node->node_symbol->symb_kind==apply_symb && create_new_function_with_more_arguments (root_node,0)) || 
		 (root_node->node_symbol->symb_kind==definition && root_node->node_symbol->symb_def->sdef_kind==IMPRULE)))
	{		
		if (root_node->node_symbol->symb_def->sdef_kind==IMPRULE){
			ImpRuleP imp_rule_p;
			
			imp_rule_p=root_node->node_symbol->symb_def->sdef_rule;
			
			if ((imp_rule_p->rule_mark & RULE_LAMBDA_FUNCTION_MASK) && 
				root_node->node_symbol->symb_def->sdef_arity==root_node->node_arity &&
				imp_rule_p->rule_alts->alt_next==NULL
# ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
				&& ! (imp_rule_p->rule_alts->alt_rhs_root->node_kind==SwitchNode ||
					  imp_rule_p->rule_alts->alt_rhs_root->node_kind==GuardNode ||
					  imp_rule_p->rule_alts->alt_rhs_root->node_kind==IfNode)
# endif
				)
			{
				ArgP call_arg_p,lhs_arg_p;
				
				for_l (lhs_arg_p,imp_rule_p->rule_alts->alt_lhs_root->node_arguments,arg_next)
					if (lhs_arg_p->arg_node->node_kind!=NodeIdNode ||
						lhs_arg_p->arg_node->node_node_id->nid_refcount==-1 ||
						lhs_arg_p->arg_node->node_node_id->nid_node!=NULL)
					{
						break;
					}
				
				if (lhs_arg_p==NULL){
					NodeP new_root_node;
	/*
					PrintRuleNode (root_node,False,StdOut);
					FPrintF (StdOut,"\n");
					PrintRuleAlt (imp_rule_p->rule_alts,StdOut);
	*/
					for_ll (call_arg_p,lhs_arg_p,root_node->node_arguments,imp_rule_p->rule_alts->alt_lhs_root->node_arguments,arg_next,arg_next){
						NodeP call_node_p;
						NodeIdP lhs_node_id_p,call_node_id_p;
						
						lhs_node_id_p=lhs_arg_p->arg_node->node_node_id;
					
						call_node_p=call_arg_p->arg_node;
						if (call_node_p->node_kind==NodeIdNode)
							call_node_id_p=call_node_p->node_node_id;
						else {
							NodeDefP new_node_def_p;
							
							call_node_id_p=NewNodeId (NULL);
							call_node_id_p->nid_refcount=1;
							call_node_id_p->nid_ref_count_copy_=1;
							call_node_id_p->nid_exp_=NULL;
		
							call_node_id_p->nid_node=call_node_p;
							
							new_node_def_p = NewNodeDef (call_node_id_p,call_node_p);
							new_node_def_p->def_next=*defs_p;
							*defs_p=new_node_def_p;
						}
						
						call_node_id_p->nid_mark &= ~SHARED_NODES_COLLECTED_MASK;
						if (call_node_id_p->nid_refcount<0)
							call_node_id_p->nid_refcount -= -2-lhs_node_id_p->nid_refcount;
						else
							call_node_id_p->nid_refcount += -2-lhs_node_id_p->nid_refcount;
					
						lhs_node_id_p->nid_forward_node_id=call_node_id_p;
					}
					
					copy_rhs_node_defs_and_root (imp_rule_p->rule_alts,&new_root_node,defs_p);
/*				
					PrintRuleNode (new_root_node,False,StdOut);
					FPrintF (StdOut,"\n");
					PrintNodeDefs (*defs_p,False,StdOut);
					FPrintF (StdOut,"\n");
					FPrintF (StdOut,"\n");
*/
					root_node=new_root_node;
					*root_p=new_root_node;
					
					continue;
				}
			}
		}
		break;
	}
#endif

	last=defs_p;
	
/* RWS ...
	last = CollectSharedNodeIdsInNode (root_p,NULL,last);
*/
	last = CollectSharedNodeIdsInRootNode (root_p,NULL,last);
/* ... RWS */
	*last = NULL;

	AddStrictLhsNodeIdsToNodeDefs (strict_node_ids,defs_p);
}

static void AnnotateStrictNodeIds (Node node,StrictNodeIdP strict_node_ids,NodeDefS **node_def_h)
{
	StrictNodeIdP strict_node_id;

	for_l (strict_node_id,strict_node_ids,snid_next){
		NodeId node_id;

		node_id=strict_node_id->snid_node_id;

#ifdef OBSERVE_ARRAY_SELECTS_IN_PATTERN
		if (strict_node_id->snid_array_select_in_pattern && node_id->nid_node->node_symbol->symb_kind==select_symb){
			NodeP array_uselect_node;
			SymbDef uselect_sdef;
			TypeArg *type_arg;
			
			array_uselect_node=node_id->nid_node->node_arguments->arg_node;
			uselect_sdef=array_uselect_node->node_symbol->symb_def;
			
			if (uselect_sdef->sdef_kind==IMPRULE)
				type_arg=uselect_sdef->sdef_rule->rule_type->type_alt_lhs->type_node_arguments;
			else
				type_arg=uselect_sdef->sdef_rule_type->rule_type_rule->type_alt_lhs->type_node_arguments;

			if (!type_arg->type_arg_node->type_node_is_var &&
				(type_arg->type_arg_node->type_node_symbol->symb_kind==strict_array_type ||
				 type_arg->type_arg_node->type_node_symbol->symb_kind==unboxed_array_type)
			){
				node_id->nid_node->node_annotation=StrictAnnot;
			} else {
				NodeIdP uselect_node_id_p;
				NodeDefP new_def;

				uselect_node_id_p=NewNodeId (NULL);
				uselect_node_id_p->nid_refcount=1;

				node_id->nid_node->node_arguments->arg_node=NewNodeIdNode (uselect_node_id_p);
				array_uselect_node->node_annotation=StrictAnnot;
				
				strict_node_id->snid_node_id=uselect_node_id_p;
				
				new_def = NewNodeDef (uselect_node_id_p,array_uselect_node);
				uselect_node_id_p->nid_node=array_uselect_node;
				new_def->def_next=*node_def_h;
				*node_def_h=new_def;
				node_def_h=&new_def->def_next;
			}
		} else
#endif
			if (node_id->nid_refcount>0 && node_id->nid_node)
				node_id->nid_node->node_annotation=StrictAnnot;
	}
	
	switch (node->node_kind){
		case IfNode:
		{
			ArgS *arg;
		
			arg=node->node_arguments;
			AnnotateStrictNodeIds (arg->arg_node,NULL,NULL);
			arg = arg->arg_next;
			AnnotateStrictNodeIds (arg->arg_node,node->node_then_strict_node_ids,&node->node_then_node_defs);
			arg = arg->arg_next;
			AnnotateStrictNodeIds (arg->arg_node,node->node_else_strict_node_ids,&node->node_else_node_defs);
			break;
		}
		case SwitchNode:
		{
			ArgS *arg_p;

			for_l (arg_p,node->node_arguments,arg_next){
				NodeP node;

				node=arg_p->arg_node;
				if (node->node_kind==CaseNode){
					NodeP case_alt_node_p;

					case_alt_node_p=node->node_arguments->arg_node;
		/*	Codewarrior bug			if (case_alt_node_p->node_kind==PushNode){  */
					if (node->node_arguments->arg_node->node_kind==PushNode)
						AnnotateStrictNodeIds (case_alt_node_p->node_arguments->arg_next->arg_node,node->node_strict_node_ids,&node->node_node_defs);
					else
						AnnotateStrictNodeIds (node->node_arguments->arg_node,node->node_strict_node_ids,&node->node_node_defs);
				} else if (node->node_kind==DefaultNode){
					AnnotateStrictNodeIds (node->node_arguments->arg_node,node->node_strict_node_ids,&node->node_node_defs);
				} else if (node->node_kind==OverloadedCaseNode){
					NodeP case_node_p;
					
					case_node_p=node->node_node;
					AnnotateStrictNodeIds (case_node_p->node_arguments->arg_node,case_node_p->node_strict_node_ids,&case_node_p->node_node_defs);
				} else
					error_in_function ("AnnotateStrictNodeIds");
			}
			break;
		}
		case GuardNode:
		{
			AnnotateStrictNodeIds (node->node_arguments->arg_node,strict_node_ids,node_def_h);
			AnnotateStrictNodeIds (node->node_arguments->arg_next->arg_node,node->node_guard_strict_node_ids,&node->node_node_defs);
			break;
		}
		default:
			break;

	}
}

static void DetermineSharedAndAnnotatedNodesOfRule (ImpRuleP rule)
{
	SymbDef rule_sdef;
	RuleAlts alt;

	CurrentSymbol=rule->rule_root->node_symbol;
	
	rule_sdef=CurrentSymbol->symb_def;

	for_l (alt,rule->rule_alts,alt_next)
		if (alt->alt_kind==Contractum){
			CurrentLine = alt->alt_line;

			AnnotateStrictNodeIds (alt->alt_rhs_root,alt->alt_strict_node_ids,&alt->alt_rhs_defs);

			CollectSharedAndAnnotatedNodesInRhs (&alt->alt_rhs_root,&alt->alt_rhs_defs,alt->alt_strict_node_ids);
		}
}

static void reset_states_and_ref_count_copies_of_node_defs (NodeDefS *node_def);

static void reset_states_and_ref_count_copies_of_node (Node node)
{
	if (node->node_kind==NodeIdNode){
		NodeId node_id;

		node_id=node->node_node_id;

		node_id->nid_ref_count_copy_=node_id->nid_refcount;
#if OPTIMIZE_LAZY_TUPLE_RECURSION
		node_id->nid_mark2 &= ~NID_HAS_LAZY_SELECTOR_COUNTER;
#endif
	} else {
		DetermineNodeState (node);

		if (node->node_annotation==ParallelAtAnnot){
			Node at_node;
		
			at_node=get_p_at_node (node);

			reset_states_and_ref_count_copies_of_node (at_node);
		}

		if (node->node_kind!=IfNode){
			ArgP arg;
			
			for_l (arg,node->node_arguments,arg_next){
				arg->arg_state=LazyState;
				reset_states_and_ref_count_copies_of_node (arg->arg_node);
			}
		/* RWS SwitchNode */
		} else {
			Args cond_arg,then_arg,else_arg;
	
			cond_arg=node->node_arguments;
			then_arg=cond_arg->arg_next;
			else_arg=then_arg->arg_next;
			
			cond_arg->arg_state=LazyState;
			reset_states_and_ref_count_copies_of_node (cond_arg->arg_node);
	
			then_arg->arg_state=LazyState;
			reset_states_and_ref_count_copies_of_node (then_arg->arg_node);
				
			else_arg->arg_state=LazyState;
			reset_states_and_ref_count_copies_of_node (else_arg->arg_node);
			
			reset_states_and_ref_count_copies_of_node_defs (node->node_then_node_defs);
			reset_states_and_ref_count_copies_of_node_defs (node->node_else_node_defs);
		}
	}
}

static void reset_states_and_ref_count_copies_of_node_defs (NodeDefS *node_defs)
{
	NodeDefS *node_def;
	
	for_l (node_def,node_defs,def_next)
		if (node_def->def_node!=NULL){
			node_def->def_id->nid_ref_count_copy_=node_def->def_id->nid_refcount;
			node_def->def_id->nid_node_def_=node_def;
			reset_states_and_ref_count_copies_of_node (node_def->def_node);
		}
}

#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
static void reset_states_and_ref_count_copies_of_root_node (NodeP node_p)
{
	if (node_p->node_kind==IfNode){
		Args cond_arg,then_arg,else_arg;

		DetermineNodeState (node_p);
		
		cond_arg=node_p->node_arguments;
		then_arg=cond_arg->arg_next;
		else_arg=then_arg->arg_next;
		
		cond_arg->arg_state=LazyState;
		reset_states_and_ref_count_copies_of_root_node (cond_arg->arg_node);

		then_arg->arg_state=LazyState;
		reset_states_and_ref_count_copies_of_root_node (then_arg->arg_node);
			
		else_arg->arg_state=LazyState;
		reset_states_and_ref_count_copies_of_root_node (else_arg->arg_node);
		
		reset_states_and_ref_count_copies_of_node_defs (node_p->node_then_node_defs);
		reset_states_and_ref_count_copies_of_node_defs (node_p->node_else_node_defs);
	} else if (node_p->node_kind==SwitchNode){
		ArgP arg_p;
		
		for_l (arg_p,node_p->node_arguments,arg_next){
			NodeP node_p;

			node_p=arg_p->arg_node;
			if (node_p->node_kind==CaseNode){
				NodeP case_alt_node_p;
				
				case_alt_node_p=node_p->node_arguments->arg_node;
				if (case_alt_node_p->node_kind==PushNode){
					NodeIdListElementP node_id_list;
					
					for_l (node_id_list,case_alt_node_p->node_node_ids,nidl_next)
						node_id_list->nidl_node_id->nid_ref_count_copy_=node_id_list->nidl_node_id->nid_refcount;
					
					case_alt_node_p=case_alt_node_p->node_arguments->arg_next->arg_node;
				}
				
				reset_states_and_ref_count_copies_of_root_node (case_alt_node_p);
				reset_states_and_ref_count_copies_of_node_defs (node_p->node_node_defs);
			} else if (node_p->node_kind==DefaultNode){
				reset_states_and_ref_count_copies_of_root_node (node_p->node_arguments->arg_node);
				reset_states_and_ref_count_copies_of_node_defs (node_p->node_node_defs);
			} else if (node_p->node_kind==OverloadedCaseNode){
				NodeP case_node_p;

				case_node_p=node_p->node_node;
												
				reset_states_and_ref_count_copies_of_root_node (case_node_p->node_arguments->arg_node);
				reset_states_and_ref_count_copies_of_node_defs (case_node_p->node_node_defs);

				reset_states_and_ref_count_copies_of_node (node_p->node_arguments->arg_node);
				reset_states_and_ref_count_copies_of_node (node_p->node_arguments->arg_next->arg_node);
			} else
				error_in_function ("reset_states_and_ref_count_copies_of_root_node");
		}
	} else if (node_p->node_kind==GuardNode){
		reset_states_and_ref_count_copies_of_root_node (node_p->node_arguments->arg_node);
		reset_states_and_ref_count_copies_of_root_node (node_p->node_arguments->arg_next->arg_node);
		reset_states_and_ref_count_copies_of_node_defs (node_p->node_node_defs);
	} else
		reset_states_and_ref_count_copies_of_node (node_p);
}
#endif

void reset_states_and_ref_count_copies (ImpRuleS *rule)
{
	SymbDef rule_sdef;
	RuleAlts alt;

	CurrentSymbol=rule->rule_root->node_symbol;
	
	rule_sdef=CurrentSymbol->symb_def;

	for_l (alt,rule->rule_alts,alt_next)
		if (alt->alt_kind==Contractum){
			CurrentLine = alt->alt_line;

#ifdef TRANSFORM_PATTERNS_BEFORE_STRICTNESS_ANALYSIS
			reset_states_and_ref_count_copies_of_root_node (alt->alt_rhs_root);
#else
			reset_states_and_ref_count_copies_of_node (alt->alt_rhs_root);
#endif
			reset_states_and_ref_count_copies_of_node_defs (alt->alt_rhs_defs);
		}
}

void DetermineSharedAndAnnotatedNodes (ImpRules rules,SymbolP *im_symbols_h)
{
	ImpRuleP rule;

#ifdef ADD_ARGUMENTS_TO_HIGHER_ORDER_FUNCTIONS
	last_new_rule_with_more_arguments_h=&new_rules_with_more_arguments;
#endif
	
	for_l (rule,rules,rule_next)
		DetermineSharedAndAnnotatedNodesOfRule (rule);

#ifdef ADD_ARGUMENTS_TO_HIGHER_ORDER_FUNCTIONS
	if (new_rules_with_more_arguments!=NULL){
		ImpRuleP *rule_h,new_rule_p;
		
		rule_h=&rules;
		while (*rule_h!=NULL)
			rule_h=&(*rule_h)->rule_next;
			
		while (*im_symbols_h)
			im_symbols_h=&(*im_symbols_h)->symb_next;
		
		while (new_rules_with_more_arguments!=NULL){
			SymbolP new_symbol_p;
			
			new_rule_p=new_rules_with_more_arguments;
		
			DetermineSharedAndAnnotatedNodesOfRule (new_rule_p);

			new_rules_with_more_arguments=new_rule_p->rule_next;
		
			*rule_h=new_rule_p;
			rule_h=&new_rule_p->rule_next;
			
			new_symbol_p=new_rule_p->rule_root->node_symbol;
			*im_symbols_h=new_symbol_p;
			im_symbols_h=&new_symbol_p->symb_next;
		}
		
		*rule_h=NULL;
		*im_symbols_h=NULL;
	}
#endif
}

void InitStatesGen (void)
{
	SetUnaryState (& StrictState, StrictOnA, UnknownObj);
	SetUnaryState (& LazyState, OnA, UnknownObj);
	
	SetUnaryState (& BasicSymbolStates[int_type], OnB, IntObj);
	SetUnaryState (& BasicSymbolStates[bool_type], OnB, BoolObj);
	SetUnaryState (& BasicSymbolStates[char_type], OnB, CharObj);
	SetUnaryState (& BasicSymbolStates[string_type], StrictOnA, StringObj);
	SetUnaryState (& BasicSymbolStates[real_type], OnB, RealObj);
	SetUnaryState (& BasicSymbolStates[file_type], OnB, FileObj);
	SetUnaryState (& BasicSymbolStates[world_type], StrictOnA, WorldObj);
	SetUnaryState (& BasicSymbolStates[procid_type], OnB, ProcIdObj);
	SetUnaryState (& BasicSymbolStates[redid_type], OnB, RedIdObj);
	SetUnaryState (& BasicSymbolStates[int_denot], OnB, IntObj);
	SetUnaryState (& BasicSymbolStates[bool_denot], OnB, BoolObj);
	SetUnaryState (& BasicSymbolStates[char_denot], OnB, CharObj);
	SetUnboxedArrayState (& BasicSymbolStates[string_denot],&BasicSymbolStates[char_type]);
	SetUnaryState (& BasicSymbolStates[real_denot], OnB, RealObj);
	SetUnaryState (& BasicSymbolStates[array_type], StrictOnA, ArrayObj);
	SetUnaryState (& BasicSymbolStates[strict_array_type], StrictOnA, StrictArrayObj);
	SetUnaryState (& BasicSymbolStates[unboxed_array_type], StrictOnA, UnboxedArrayObj);
	SetUnaryState (& BasicSymbolStates[fun_type], StrictOnA, UnknownObj);
	SetUnaryState (& BasicSymbolStates[list_type], StrictOnA, ListObj);
	SetUnaryState (& BasicSymbolStates[tuple_type], StrictOnA, TupleObj);
#ifdef CLEAN2
	SetUnaryState (& BasicSymbolStates[dynamic_type], StrictOnA, DynamicObj);
	SetUnaryState (& BasicSymbolStates[rational_denot], StrictOnA, UnknownObj);
#endif
}