summaryrefslogtreecommitdiff
path: root/libguile/environments.c
blob: dc2d5fee244423a7189a9267e75b7bee6788ca8b (about) (plain)
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
/* Copyright (C) 1999,2000,2001, 2003 Free Software Foundation, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */



#include "libguile/_scm.h"
#include "libguile/alist.h"
#include "libguile/eval.h"
#include "libguile/gh.h"
#include "libguile/hash.h"
#include "libguile/list.h"
#include "libguile/ports.h"
#include "libguile/smob.h"
#include "libguile/symbols.h"
#include "libguile/vectors.h"
#include "libguile/weaks.h"

#include "libguile/environments.h"



scm_t_bits scm_tc16_environment;
scm_t_bits scm_tc16_observer;
#define DEFAULT_OBARRAY_SIZE 31

SCM scm_system_environment;



/* error conditions */

/*
 * Throw an error if symbol is not bound in environment func
 */
void
scm_error_environment_unbound (const char *func, SCM env, SCM symbol)
{
  /* Dirk:FIXME:: Should throw an environment:unbound type error */
  char error[] = "Symbol `~A' not bound in environment `~A'.";
  SCM arguments = scm_cons2 (symbol, env, SCM_EOL);
  scm_misc_error (func, error, arguments);
}


/*
 * Throw an error if func tried to create (define) or remove
 * (undefine) a new binding for symbol in env
 */
void
scm_error_environment_immutable_binding (const char *func, SCM env, SCM symbol)
{
  /* Dirk:FIXME:: Should throw an environment:immutable-binding type error */
  char error[] = "Immutable binding in environment ~A (symbol: `~A').";
  SCM arguments = scm_cons2 (env, symbol, SCM_EOL);
  scm_misc_error (func, error, arguments);
}


/*
 * Throw an error if func tried to change an immutable location.
 */
void
scm_error_environment_immutable_location (const char *func, SCM env, SCM symbol)
{
  /* Dirk:FIXME:: Should throw an environment:immutable-location type error */
  char error[] = "Immutable location in environment `~A' (symbol: `~A').";
  SCM arguments = scm_cons2 (env, symbol, SCM_EOL);
  scm_misc_error (func, error, arguments);
}



/* generic environments */


/* Create an environment for the given type.  Dereferencing type twice must
 * deliver the initialized set of environment functions.  Thus, type will
 * also determine the signature of the underlying environment implementation.
 * Dereferencing type once will typically deliver the data fields used by the
 * underlying environment implementation.
 */
SCM
scm_make_environment (void *type)
{
  return scm_cell (scm_tc16_environment, (scm_t_bits) type);
}


SCM_DEFINE (scm_environment_p, "environment?", 1, 0, 0, 
	    (SCM obj),
	    "Return @code{#t} if @var{obj} is an environment, or @code{#f}\n"
	    "otherwise.")
#define FUNC_NAME s_scm_environment_p
{
  return scm_from_bool (SCM_ENVIRONMENT_P (obj));
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_bound_p, "environment-bound?", 2, 0, 0, 
	    (SCM env, SCM sym),
	    "Return @code{#t} if @var{sym} is bound in @var{env}, or\n"
	    "@code{#f} otherwise.")
#define FUNC_NAME s_scm_environment_bound_p
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME);

  return scm_from_bool (SCM_ENVIRONMENT_BOUND_P (env, sym));
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_ref, "environment-ref", 2, 0, 0,
	    (SCM env, SCM sym),
	    "Return the value of the location bound to @var{sym} in\n"
	    "@var{env}. If @var{sym} is unbound in @var{env}, signal an\n"
	    "@code{environment:unbound} error.")
#define FUNC_NAME s_scm_environment_ref
{
  SCM val;

  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME);

  val = SCM_ENVIRONMENT_REF (env, sym);

  if (!SCM_UNBNDP (val))
    return val;
  else
    scm_error_environment_unbound (FUNC_NAME, env, sym);
}
#undef FUNC_NAME


/* This C function is identical to environment-ref, except that if symbol is
 * unbound in env, it returns the value SCM_UNDEFINED, instead of signalling
 * an error.
 */ 
SCM
scm_c_environment_ref (SCM env, SCM sym)
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_ref");
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, "scm_c_environment_ref");
  return SCM_ENVIRONMENT_REF (env, sym);
}


static SCM
environment_default_folder (SCM proc, SCM symbol, SCM value, SCM tail)
{
  return gh_call3 (proc, symbol, value, tail);
}


SCM_DEFINE (scm_environment_fold, "environment-fold", 3, 0, 0, 
	    (SCM env, SCM proc, SCM init),
	    "Iterate over all the bindings in @var{env}, accumulating some\n"
	    "value.\n"
	    "For each binding in @var{env}, apply @var{proc} to the symbol\n"
	    "bound, its value, and the result from the previous application\n"
	    "of @var{proc}.\n"
	    "Use @var{init} as @var{proc}'s third argument the first time\n"
	    "@var{proc} is applied.\n"
	    "If @var{env} contains no bindings, this function simply returns\n"
	    "@var{init}.\n"
	    "If @var{env} binds the symbol sym1 to the value val1, sym2 to\n"
	    "val2, and so on, then this procedure computes:\n"
	    "@lisp\n"
	    "  (proc sym1 val1\n"
	    "        (proc sym2 val2\n"
	    "              ...\n"
	    "              (proc symn valn\n"
	    "                    init)))\n"
	    "@end lisp\n"
	    "Each binding in @var{env} will be processed exactly once.\n"
	    "@code{environment-fold} makes no guarantees about the order in\n"
	    "which the bindings are processed.\n"
	    "Here is a function which, given an environment, constructs an\n"
	    "association list representing that environment's bindings,\n"
	    "using environment-fold:\n"
	    "@lisp\n"
	    "  (define (environment->alist env)\n"
	    "    (environment-fold env\n"
	    "                      (lambda (sym val tail)\n"
	    "                        (cons (cons sym val) tail))\n"
	    "                      '()))\n"
	    "@end lisp")
#define FUNC_NAME s_scm_environment_fold
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), 
	      proc, SCM_ARG2, FUNC_NAME);

  return SCM_ENVIRONMENT_FOLD (env, environment_default_folder, proc, init);
}
#undef FUNC_NAME


/* This is the C-level analog of environment-fold. For each binding in ENV,
 * make the call:
 *   (*proc) (data, symbol, value, previous)
 * where previous is the value returned from the last call to *PROC, or INIT
 * for the first call. If ENV contains no bindings, return INIT. 
 */
SCM
scm_c_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_fold");

  return SCM_ENVIRONMENT_FOLD (env, proc, data, init);
}


SCM_DEFINE (scm_environment_define, "environment-define", 3, 0, 0, 
	    (SCM env, SCM sym, SCM val),
	    "Bind @var{sym} to a new location containing @var{val} in\n"
	    "@var{env}. If @var{sym} is already bound to another location\n"
	    "in @var{env} and the binding is mutable, that binding is\n"
	    "replaced.  The new binding and location are both mutable. The\n"
	    "return value is unspecified.\n"
	    "If @var{sym} is already bound in @var{env}, and the binding is\n"
	    "immutable, signal an @code{environment:immutable-binding} error.")
#define FUNC_NAME s_scm_environment_define
{
  SCM status;

  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME);

  status = SCM_ENVIRONMENT_DEFINE (env, sym, val);

  if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS))
    return SCM_UNSPECIFIED;
  else if (scm_is_eq (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE))
    scm_error_environment_immutable_binding (FUNC_NAME, env, sym);
  else
    abort();
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_undefine, "environment-undefine", 2, 0, 0, 
	    (SCM env, SCM sym),
	    "Remove any binding for @var{sym} from @var{env}. If @var{sym}\n"
	    "is unbound in @var{env}, do nothing.  The return value is\n"
	    "unspecified.\n"
	    "If @var{sym} is already bound in @var{env}, and the binding is\n"
	    "immutable, signal an @code{environment:immutable-binding} error.")
#define FUNC_NAME s_scm_environment_undefine
{
  SCM status;

  SCM_ASSERT(SCM_ENVIRONMENT_P(env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT(scm_is_symbol(sym), sym, SCM_ARG2, FUNC_NAME);

  status = SCM_ENVIRONMENT_UNDEFINE (env, sym);

  if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS))
    return SCM_UNSPECIFIED;
  else if (scm_is_eq (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE))
    scm_error_environment_immutable_binding (FUNC_NAME, env, sym);
  else
    abort();
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_set_x, "environment-set!", 3, 0, 0, 
	    (SCM env, SCM sym, SCM val),
	    "If @var{env} binds @var{sym} to some location, change that\n"
	    "location's value to @var{val}.  The return value is\n"
	    "unspecified.\n"
	    "If @var{sym} is not bound in @var{env}, signal an\n"
	    "@code{environment:unbound} error.  If @var{env} binds @var{sym}\n"
	    "to an immutable location, signal an\n"
	    "@code{environment:immutable-location} error.")
#define FUNC_NAME s_scm_environment_set_x
{
  SCM status;

  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME);

  status = SCM_ENVIRONMENT_SET (env, sym, val);

  if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS))
    return SCM_UNSPECIFIED;
  else if (SCM_UNBNDP (status))
    scm_error_environment_unbound (FUNC_NAME, env, sym);
  else if (scm_is_eq (status, SCM_ENVIRONMENT_LOCATION_IMMUTABLE))
    scm_error_environment_immutable_binding (FUNC_NAME, env, sym);
  else
    abort();
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_cell, "environment-cell", 3, 0, 0, 
	    (SCM env, SCM sym, SCM for_write),
	    "Return the value cell which @var{env} binds to @var{sym}, or\n"
	    "@code{#f} if the binding does not live in a value cell.\n"
	    "The argument @var{for-write} indicates whether the caller\n"
	    "intends to modify the variable's value by mutating the value\n"
	    "cell.  If the variable is immutable, then\n"
	    "@code{environment-cell} signals an\n"
	    "@code{environment:immutable-location} error.\n"
	    "If @var{sym} is unbound in @var{env}, signal an\n"
	    "@code{environment:unbound} error.\n"
	    "If you use this function, you should consider using\n"
	    "@code{environment-observe}, to be notified when @var{sym} gets\n"
	    "re-bound to a new value cell, or becomes undefined.")
#define FUNC_NAME s_scm_environment_cell
{
  SCM location;

  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME);
  SCM_ASSERT (scm_is_bool (for_write), for_write, SCM_ARG3, FUNC_NAME);

  location = SCM_ENVIRONMENT_CELL (env, sym, scm_is_true (for_write));
  if (!SCM_IMP (location))
    return location;
  else if (SCM_UNBNDP (location))
    scm_error_environment_unbound (FUNC_NAME, env, sym);
  else if (scm_is_eq (location, SCM_ENVIRONMENT_LOCATION_IMMUTABLE))
    scm_error_environment_immutable_location (FUNC_NAME, env, sym);
  else /* no cell */
    return location;
}
#undef FUNC_NAME


/* This C function is identical to environment-cell, with the following
 * exceptions:   If symbol is unbound in env, it returns the value
 * SCM_UNDEFINED, instead of signalling an error.  If symbol is bound to an
 * immutable location but the cell is requested for write, the value 
 * SCM_ENVIRONMENT_LOCATION_IMMUTABLE is returned.
 */
SCM
scm_c_environment_cell(SCM env, SCM sym, int for_write)
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_cell");
  SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, "scm_c_environment_cell");

  return SCM_ENVIRONMENT_CELL (env, sym, for_write);
}


static void
environment_default_observer (SCM env, SCM proc)
{
  gh_call1 (proc, env);
}


SCM_DEFINE (scm_environment_observe, "environment-observe", 2, 0, 0, 
	    (SCM env, SCM proc),
	    "Whenever @var{env}'s bindings change, apply @var{proc} to\n"
	    "@var{env}.\n"
	    "This function returns an object, token, which you can pass to\n"
	    "@code{environment-unobserve} to remove @var{proc} from the set\n"
	    "of procedures observing @var{env}.  The type and value of\n"
	    "token is unspecified.")
#define FUNC_NAME s_scm_environment_observe
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return SCM_ENVIRONMENT_OBSERVE (env, environment_default_observer, proc, 0);
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_observe_weak, "environment-observe-weak", 2, 0, 0,
	    (SCM env, SCM proc),
	    "This function is the same as environment-observe, except that\n"
	    "the reference @var{env} retains to @var{proc} is a weak\n"
	    "reference. This means that, if there are no other live,\n"
	    "non-weak references to @var{proc}, it will be\n"
	    "garbage-collected, and dropped from @var{env}'s\n"
	    "list of observing procedures.")
#define FUNC_NAME s_scm_environment_observe_weak
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return SCM_ENVIRONMENT_OBSERVE (env, environment_default_observer, proc, 1);
}
#undef FUNC_NAME


/* This is the C-level analog of the Scheme functions environment-observe and
 * environment-observe-weak.  Whenever env's bindings change, call the
 * function proc, passing it env and data. If weak_p is non-zero, env will
 * retain only a weak reference to data, and if data is garbage collected, the
 * entire observation will be dropped.  This function returns a token, with
 * the same meaning as those returned by environment-observe and
 * environment-observe-weak.
 */
SCM
scm_c_environment_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p)
#define FUNC_NAME "scm_c_environment_observe"
{
  SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return SCM_ENVIRONMENT_OBSERVE (env, proc, data, weak_p);
}
#undef FUNC_NAME


SCM_DEFINE (scm_environment_unobserve, "environment-unobserve", 1, 0, 0, 
	    (SCM token),
	    "Cancel the observation request which returned the value\n"
	    "@var{token}.  The return value is unspecified.\n"
	    "If a call @code{(environment-observe env proc)} returns\n"
	    "@var{token}, then the call @code{(environment-unobserve token)}\n"
	    "will cause @var{proc} to no longer be called when @var{env}'s\n"
	    "bindings change.")
#define FUNC_NAME s_scm_environment_unobserve
{
  SCM env;

  SCM_ASSERT (SCM_OBSERVER_P (token), token, SCM_ARG1, FUNC_NAME);

  env = SCM_OBSERVER_ENVIRONMENT (token);
  SCM_ENVIRONMENT_UNOBSERVE (env, token);

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


static SCM
environment_mark (SCM env)
{
  return (*(SCM_ENVIRONMENT_FUNCS (env)->mark)) (env);
}


static size_t
environment_free (SCM env)
{
  (*(SCM_ENVIRONMENT_FUNCS (env)->free)) (env);
  return 0;
}


static int
environment_print (SCM env, SCM port, scm_print_state *pstate)
{
  return (*(SCM_ENVIRONMENT_FUNCS (env)->print)) (env, port, pstate);
}



/* observers */

static SCM
observer_mark (SCM observer)
{
  scm_gc_mark (SCM_OBSERVER_ENVIRONMENT (observer));
  scm_gc_mark (SCM_OBSERVER_DATA (observer));
  return SCM_BOOL_F;
}


static int
observer_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED)
{
  SCM address = scm_from_size_t (SCM_UNPACK (type));
  SCM base16 = scm_number_to_string (address, scm_from_int (16));

  scm_puts ("#<observer ", port);
  scm_display (base16, port);
  scm_puts (">", port);

  return 1;
}



/* obarrays
 *
 * Obarrays form the basic lookup tables used to implement most of guile's
 * built-in environment types.  An obarray is implemented as a hash table with
 * symbols as keys.  The content of the data depends on the environment type.
 */


/*
 * Enter symbol into obarray.  The symbol must not already exist in obarray.
 * The freshly generated (symbol . data) cell is returned.
 */
static SCM
obarray_enter (SCM obarray, SCM symbol, SCM data)
{
  size_t hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (obarray);
  SCM entry = scm_cons (symbol, data);
  SCM slot = scm_cons (entry, SCM_HASHTABLE_BUCKETS (obarray)[hash]);
  SCM_SET_HASHTABLE_BUCKET  (obarray, hash, slot);
  if (SCM_HASHTABLE_N_ITEMS (obarray) > SCM_HASHTABLE_UPPER (obarray))
    scm_i_rehash (obarray, scm_i_hash_symbol, 0, "obarray_enter");

  return entry;
}


/*
 * Enter symbol into obarray.  An existing entry for symbol is replaced.  If
 * an entry existed, the old (symbol . data) cell is returned, #f otherwise.
 */
static SCM
obarray_replace (SCM obarray, SCM symbol, SCM data)
{
  size_t hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (obarray);
  SCM new_entry = scm_cons (symbol, data);
  SCM lsym;
  SCM slot;

  for (lsym = SCM_HASHTABLE_BUCKETS (obarray)[hash];
       !scm_is_null (lsym);
       lsym = SCM_CDR (lsym))
    {
      SCM old_entry = SCM_CAR (lsym);
      if (scm_is_eq (SCM_CAR (old_entry), symbol))
	{
	  SCM_SETCAR (lsym, new_entry);
	  return old_entry;
	}
    }

  slot = scm_cons (new_entry, SCM_HASHTABLE_BUCKETS (obarray)[hash]);
  SCM_SET_HASHTABLE_BUCKET (obarray, hash, slot);
  if (SCM_HASHTABLE_N_ITEMS (obarray) > SCM_HASHTABLE_UPPER (obarray))
    scm_i_rehash (obarray, scm_i_hash_symbol, 0, "obarray_replace");

  return SCM_BOOL_F;
}


/*
 * Look up symbol in obarray
 */
static SCM
obarray_retrieve (SCM obarray, SCM sym)
{
  size_t hash = scm_i_symbol_hash (sym) % SCM_HASHTABLE_N_BUCKETS (obarray);
  SCM lsym;

  for (lsym = SCM_HASHTABLE_BUCKETS (obarray)[hash];
       !scm_is_null (lsym);
       lsym = SCM_CDR (lsym))
    {
      SCM entry = SCM_CAR (lsym);
      if (scm_is_eq (SCM_CAR (entry), sym))
	return entry;
    }

  return SCM_UNDEFINED;
}


/*
 * Remove entry from obarray.  If the symbol was found and removed, the old
 * (symbol . data) cell is returned, #f otherwise.
 */
static SCM
obarray_remove (SCM obarray, SCM sym)
{
  size_t hash = scm_i_symbol_hash (sym) % SCM_HASHTABLE_N_BUCKETS (obarray);
  SCM table_entry = SCM_HASHTABLE_BUCKETS (obarray)[hash];
  SCM handle = scm_sloppy_assq (sym, table_entry);

  if (scm_is_pair (handle))
    {
      SCM new_table_entry = scm_delq1_x (handle, table_entry);
      SCM_SET_HASHTABLE_BUCKET (obarray, hash, new_table_entry);
      SCM_HASHTABLE_DECREMENT (obarray);
    }

  return handle;
}


static void
obarray_remove_all (SCM obarray)
{
  size_t size = SCM_HASHTABLE_N_BUCKETS (obarray);
  size_t i;

  for (i = 0; i < size; i++)
    {
      SCM_SET_HASHTABLE_BUCKET (obarray, i, SCM_EOL);
    }
  SCM_SET_HASHTABLE_N_ITEMS (obarray, 0);
}



/* core environments base
 *
 * This struct and the corresponding functions form a base class for guile's
 * built-in environment types.
 */


struct core_environments_base {
  struct scm_environment_funcs *funcs;

  SCM observers;
  SCM weak_observers;
};


#define CORE_ENVIRONMENTS_BASE(env) \
  ((struct core_environments_base *) SCM_CELL_WORD_1 (env))
#define CORE_ENVIRONMENT_OBSERVERS(env) \
  (CORE_ENVIRONMENTS_BASE (env)->observers)
#define SCM_SET_CORE_ENVIRONMENT_OBSERVERS(env, v) \
  (CORE_ENVIRONMENT_OBSERVERS (env) = (v))
#define CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR(env) \
  (CORE_ENVIRONMENTS_BASE (env)->weak_observers)
#define CORE_ENVIRONMENT_WEAK_OBSERVERS(env) \
  (SCM_VELTS (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env)) [0])
#define SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS(env, v) \
  (SCM_VECTOR_SET (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env), 0, (v)))



static SCM
core_environments_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p)
{
  SCM observer = scm_double_cell (scm_tc16_observer,
				  SCM_UNPACK (env),
				  SCM_UNPACK (data),
				  (scm_t_bits) proc);

  if (!weak_p)
    {
      SCM observers = CORE_ENVIRONMENT_OBSERVERS (env);
      SCM new_observers = scm_cons (observer, observers);
      SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env, new_observers);
    }
  else
    {
      SCM observers = CORE_ENVIRONMENT_WEAK_OBSERVERS (env);
      SCM new_observers = scm_acons (SCM_BOOL_F, observer, observers);
      SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env, new_observers);
    }

  return observer;
}


static void
core_environments_unobserve (SCM env, SCM observer)
{
  unsigned int handling_weaks;
  for (handling_weaks = 0; handling_weaks <= 1; ++handling_weaks)
    {
      SCM l = handling_weaks
	? CORE_ENVIRONMENT_WEAK_OBSERVERS (env)
	: CORE_ENVIRONMENT_OBSERVERS (env);

      if (!scm_is_null (l))
	{
	  SCM rest = SCM_CDR (l);
	  SCM first = handling_weaks
	    ? SCM_CDAR (l)
	    : SCM_CAR (l);

	  if (scm_is_eq (first, observer))
	    {
	      /* Remove the first observer */
	      handling_weaks
		? SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env, rest)
		: SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env, rest);
	      return;
	    }

	  do {
	    SCM rest = SCM_CDR (l);

	    if (!scm_is_null (rest)) 
	      {
		SCM next = handling_weaks
		  ? SCM_CDAR (l)
		  : SCM_CAR (l);

		if (scm_is_eq (next, observer))
		  {
		    SCM_SETCDR (l, SCM_CDR (rest));
		    return;
		  }
	      }

	    l = rest;
	  } while (!scm_is_null (l));
	}
    }

  /* Dirk:FIXME:: What to do now, since the observer is not found? */
}


static SCM
core_environments_mark (SCM env)
{
  scm_gc_mark (CORE_ENVIRONMENT_OBSERVERS (env));
  return CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env);
}


static void
core_environments_finalize (SCM env SCM_UNUSED)
{
}


static void
core_environments_preinit (struct core_environments_base *body)
{
  body->funcs = NULL;
  body->observers = SCM_BOOL_F;
  body->weak_observers = SCM_BOOL_F;
}


static void
core_environments_init (struct core_environments_base *body,
			       struct scm_environment_funcs *funcs)
{
  body->funcs = funcs;
  body->observers = SCM_EOL;
  body->weak_observers = scm_make_weak_value_alist_vector (scm_from_int (1));
}


/* Tell all observers to clear their caches.
 *
 * Environments have to be informed about changes in the following cases:
 * - The observed env has a new binding.  This must be always reported.
 * - The observed env has dropped a binding.  This must be always reported.
 * - A binding in the observed environment has changed.  This must only be
 *   reported, if there is a chance that the binding is being cached outside.
 *   However, this potential optimization is not performed currently.
 *
 * Errors that occur while the observers are called are accumulated and
 * signalled as one single error message to the caller.
 */

struct update_data
{
  SCM observer;
  SCM environment;
};


static SCM
update_catch_body (void *ptr)
{
  struct update_data *data = (struct update_data *) ptr;
  SCM observer = data->observer;

  (*SCM_OBSERVER_PROC (observer)) 
    (data->environment, SCM_OBSERVER_DATA (observer));

  return SCM_UNDEFINED;
}


static SCM
update_catch_handler (void *ptr, SCM tag, SCM args)
{
  struct update_data *data = (struct update_data *) ptr;
  SCM observer = data->observer;
  SCM message =
    scm_from_locale_string ("Observer `~A' signals `~A' error: ~S");

  return scm_cons (message, scm_list_3 (observer, tag, args));
}


static void
core_environments_broadcast (SCM env)
#define FUNC_NAME "core_environments_broadcast"
{
  unsigned int handling_weaks;
  SCM errors = SCM_EOL;

  for (handling_weaks = 0; handling_weaks <= 1; ++handling_weaks)
    {
      SCM observers = handling_weaks
	? CORE_ENVIRONMENT_WEAK_OBSERVERS (env)
	: CORE_ENVIRONMENT_OBSERVERS (env);

      for (; !scm_is_null (observers); observers = SCM_CDR (observers))
	{
          struct update_data data;
	  SCM observer = handling_weaks
	    ? SCM_CDAR (observers)
	    : SCM_CAR (observers);
          SCM error;

          data.observer = observer;
          data.environment = env;

          error = scm_internal_catch (SCM_BOOL_T, 
                                      update_catch_body, &data, 
                                      update_catch_handler, &data);

          if (!SCM_UNBNDP (error))
            errors = scm_cons (error, errors);
	}
    }

  if (!scm_is_null (errors))
    {
      /* Dirk:FIXME:: As soon as scm_misc_error is fixed to handle the name
       * parameter correctly it should not be necessary any more to also pass
       * namestr in order to get the desired information from the error
       * message.
       */
      SCM ordered_errors = scm_reverse (errors);
      scm_misc_error 
        (FUNC_NAME,
         "Observers of `~A' have signalled the following errors: ~S",
         scm_cons2 (env, ordered_errors, SCM_EOL));
    }
}
#undef FUNC_NAME



/* leaf environments
 *
 * A leaf environment is simply a mutable set of definitions. A leaf
 * environment supports no operations beyond the common set.
 *
 * Implementation:  The obarray of the leaf environment holds (symbol . value)
 * pairs.  No further information is necessary, since all bindings and
 * locations in a leaf environment are mutable.
 */


struct leaf_environment {
  struct core_environments_base base;

  SCM obarray;
};


#define LEAF_ENVIRONMENT(env) \
  ((struct leaf_environment *) SCM_CELL_WORD_1 (env))



static SCM
leaf_environment_ref (SCM env, SCM sym)
{
  SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
  SCM binding = obarray_retrieve (obarray, sym);
  return SCM_UNBNDP (binding) ? binding : SCM_CDR (binding);
}


static SCM
leaf_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
{
  size_t i;
  SCM result = init;
  SCM obarray = LEAF_ENVIRONMENT (env)->obarray;

  for (i = 0; i < SCM_HASHTABLE_N_BUCKETS (obarray); i++)
    {
      SCM l;
      for (l = SCM_HASHTABLE_BUCKETS (obarray)[i];
	   !scm_is_null (l);
	   l = SCM_CDR (l))
	{
	  SCM binding = SCM_CAR (l);
	  SCM symbol = SCM_CAR (binding);
	  SCM value = SCM_CDR (binding);
	  result = (*proc) (data, symbol, value, result);
	}
    }
  return result;
}


static SCM
leaf_environment_define (SCM env, SCM sym, SCM val)
#define FUNC_NAME "leaf_environment_define"
{
  SCM obarray = LEAF_ENVIRONMENT (env)->obarray;

  obarray_replace (obarray, sym, val);
  core_environments_broadcast (env);

  return SCM_ENVIRONMENT_SUCCESS;
}
#undef FUNC_NAME


static SCM
leaf_environment_undefine (SCM env, SCM sym)
#define FUNC_NAME "leaf_environment_undefine"
{
  SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
  SCM removed = obarray_remove (obarray, sym);
  
  if (scm_is_true (removed))
    core_environments_broadcast (env);

  return SCM_ENVIRONMENT_SUCCESS;
}
#undef FUNC_NAME


static SCM
leaf_environment_set_x (SCM env, SCM sym, SCM val)
#define FUNC_NAME "leaf_environment_set_x"
{
  SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
  SCM binding = obarray_retrieve (obarray, sym);

  if (!SCM_UNBNDP (binding))
    {
      SCM_SETCDR (binding, val);
      return SCM_ENVIRONMENT_SUCCESS;
    }
  else
    {
      return SCM_UNDEFINED;
    }
}
#undef FUNC_NAME


static SCM
leaf_environment_cell (SCM env, SCM sym, int for_write SCM_UNUSED)
{
  SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
  SCM binding = obarray_retrieve (obarray, sym);
  return binding;
}


static SCM
leaf_environment_mark (SCM env)
{
  scm_gc_mark (LEAF_ENVIRONMENT (env)->obarray);
  return core_environments_mark (env);
}


static void
leaf_environment_free (SCM env)
{
  core_environments_finalize (env);
  scm_gc_free (LEAF_ENVIRONMENT (env), sizeof (struct leaf_environment),
	       "leaf environment");
}


static int
leaf_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED)
{
  SCM address = scm_from_size_t (SCM_UNPACK (type));
  SCM base16 = scm_number_to_string (address, scm_from_int (16));

  scm_puts ("#<leaf environment ", port);
  scm_display (base16, port);
  scm_puts (">", port);

  return 1;
}


static struct scm_environment_funcs leaf_environment_funcs = {
  leaf_environment_ref,
  leaf_environment_fold,
  leaf_environment_define,
  leaf_environment_undefine,
  leaf_environment_set_x,
  leaf_environment_cell,
  core_environments_observe,
  core_environments_unobserve,
  leaf_environment_mark,
  leaf_environment_free,
  leaf_environment_print
};


void *scm_type_leaf_environment = &leaf_environment_funcs;


SCM_DEFINE (scm_make_leaf_environment, "make-leaf-environment", 0, 0, 0, 
	    (),
	    "Create a new leaf environment, containing no bindings.\n"
	    "All bindings and locations created in the new environment\n"
	    "will be mutable.")
#define FUNC_NAME s_scm_make_leaf_environment
{
  size_t size = sizeof (struct leaf_environment);
  struct leaf_environment *body = scm_gc_malloc (size, "leaf environment");
  SCM env;

  core_environments_preinit (&body->base);
  body->obarray = SCM_BOOL_F;

  env = scm_make_environment (body);

  core_environments_init (&body->base, &leaf_environment_funcs);
  body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE);  

  return env;
}
#undef FUNC_NAME


SCM_DEFINE (scm_leaf_environment_p, "leaf-environment?", 1, 0, 0, 
	    (SCM object),
	    "Return @code{#t} if object is a leaf environment, or @code{#f}\n"
	    "otherwise.")
#define FUNC_NAME s_scm_leaf_environment_p
{
  return scm_from_bool (SCM_LEAF_ENVIRONMENT_P (object));
}
#undef FUNC_NAME



/* eval environments
 *
 * A module's source code refers to definitions imported from other modules,
 * and definitions made within itself.  An eval environment combines two
 * environments -- a local environment and an imported environment -- to
 * produce a new environment in which both sorts of references can be
 * resolved.
 *
 * Implementation:  The obarray of the eval environment is used to cache
 * entries from the local and imported environments such that in most of the
 * cases only a single lookup is necessary.  Since for neither the local nor
 * the imported environment it is known, what kind of environment they form,
 * the most general case is assumed.  Therefore, entries in the obarray take
 * one of the following forms:
 *
 * 1) (<symbol> location mutability . source-env), where mutability indicates
 *    one of the following states:  IMMUTABLE if the location is known to be
 *    immutable, MUTABLE if the location is known to be mutable, UNKNOWN if
 *    the location has only been requested for non modifying accesses.
 *
 * 2) (symbol . source-env) if the symbol has a binding in the source-env, but
 *    if the source-env can't provide a cell for the binding.  Thus, for every
 *    access, the source-env has to be contacted directly.
 */


struct eval_environment {
  struct core_environments_base base;

  SCM obarray;

  SCM imported;
  SCM imported_observer;
  SCM local;
  SCM local_observer;
};


#define EVAL_ENVIRONMENT(env) \
  ((struct eval_environment *) SCM_CELL_WORD_1 (env))

#define IMMUTABLE SCM_I_MAKINUM (0)
#define MUTABLE   SCM_I_MAKINUM (1)
#define UNKNOWN   SCM_I_MAKINUM (2)

#define CACHED_LOCATION(x) SCM_CAR (x)
#define CACHED_MUTABILITY(x) SCM_CADR (x)
#define SET_CACHED_MUTABILITY(x, v) SCM_SETCAR (SCM_CDR (x), (v))
#define CACHED_SOURCE_ENVIRONMENT(x) SCM_CDDR (x)



/* eval_environment_lookup will report one of the following distinct results:
 * a) (<object> . value) if a cell could be obtained.
 * b) <environment> if the environment has to be contacted directly.
 * c) IMMUTABLE if an immutable cell was requested for write.
 * d) SCM_UNDEFINED if there is no binding for the symbol.
 */
static SCM
eval_environment_lookup (SCM env, SCM sym, int for_write)
{
  SCM obarray = EVAL_ENVIRONMENT (env)->obarray;
  SCM binding = obarray_retrieve (obarray, sym);

  if (!SCM_UNBNDP (binding))
    {
      /* The obarray holds an entry for the symbol. */

      SCM entry = SCM_CDR (binding);

      if (scm_is_pair (entry))
	{
	  /* The entry in the obarray is a cached location. */

	  SCM location = CACHED_LOCATION (entry);
	  SCM mutability;

	  if (!for_write)
	    return location;

	  mutability = CACHED_MUTABILITY (entry);
	  if (scm_is_eq (mutability, MUTABLE))
	    return location;

	  if (scm_is_eq (mutability, UNKNOWN))
	    {
	      SCM source_env = CACHED_SOURCE_ENVIRONMENT (entry);
	      SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, 1);

	      if (scm_is_pair (location))
		{
		  SET_CACHED_MUTABILITY (entry, MUTABLE);
		  return location;
		}
	      else /* IMMUTABLE */
		{
		  SET_CACHED_MUTABILITY (entry, IMMUTABLE);
		  return IMMUTABLE;
		}
	    }

	  return IMMUTABLE;
	}
      else
	{
	  /* The obarray entry is an environment */

	  return entry;
	}
    }
  else
    {
      /* There is no entry for the symbol in the obarray.  This can either
       * mean that there has not been a request for the symbol yet, or that
       * the symbol is really undefined.  We are looking for the symbol in
       * both the local and the imported environment.  If we find a binding, a
       * cached entry is created.
       */

      struct eval_environment *body = EVAL_ENVIRONMENT (env);
      unsigned int handling_import;

      for (handling_import = 0; handling_import <= 1; ++handling_import)
	{
	  SCM source_env = handling_import ? body->imported : body->local;
	  SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, for_write);

	  if (!SCM_UNBNDP (location))
	    {
	      if (scm_is_pair (location))
		{
		  SCM mutability = for_write ? MUTABLE : UNKNOWN;
		  SCM entry = scm_cons2 (location, mutability, source_env);
		  obarray_enter (obarray, sym, entry);
		  return location;
		}
	      else if (scm_is_eq (location, SCM_ENVIRONMENT_LOCATION_NO_CELL))
		{
		  obarray_enter (obarray, sym, source_env);
		  return source_env;
		}
	      else
		{
		  return IMMUTABLE;
		}
	    }
	}

      return SCM_UNDEFINED;
    }
}


static SCM
eval_environment_ref (SCM env, SCM sym)
#define FUNC_NAME "eval_environment_ref"
{
  SCM location = eval_environment_lookup (env, sym, 0);

  if (scm_is_pair (location))
    return SCM_CDR (location);
  else if (!SCM_UNBNDP (location))
    return SCM_ENVIRONMENT_REF (location, sym);
  else
    return SCM_UNDEFINED;
}
#undef FUNC_NAME


static SCM
eval_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail)
{
  SCM local = SCM_CAR (extended_data);

  if (!SCM_ENVIRONMENT_BOUND_P (local, symbol))
    {
      SCM proc_as_nr = SCM_CADR (extended_data);
      unsigned long int proc_as_ul = scm_to_ulong (proc_as_nr);
      scm_environment_folder proc = (scm_environment_folder) proc_as_ul;
      SCM data = SCM_CDDR (extended_data);

      return (*proc) (data, symbol, value, tail);
    }
  else
    {
      return tail;
    }
}


static SCM
eval_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
{
  SCM local = EVAL_ENVIRONMENT (env)->local;
  SCM imported = EVAL_ENVIRONMENT (env)->imported;
  SCM proc_as_nr = scm_from_ulong ((unsigned long) proc);
  SCM extended_data = scm_cons2 (local, proc_as_nr, data);
  SCM tmp_result = scm_c_environment_fold (imported, eval_environment_folder, extended_data, init);

  return scm_c_environment_fold (local, proc, data, tmp_result);
}


static SCM
eval_environment_define (SCM env, SCM sym, SCM val)
#define FUNC_NAME "eval_environment_define"
{
  SCM local = EVAL_ENVIRONMENT (env)->local;
  return SCM_ENVIRONMENT_DEFINE (local, sym, val);
}
#undef FUNC_NAME


static SCM
eval_environment_undefine (SCM env, SCM sym)
#define FUNC_NAME "eval_environment_undefine"
{
  SCM local = EVAL_ENVIRONMENT (env)->local;
  return SCM_ENVIRONMENT_UNDEFINE (local, sym);
}
#undef FUNC_NAME


static SCM
eval_environment_set_x (SCM env, SCM sym, SCM val)
#define FUNC_NAME "eval_environment_set_x"
{
  SCM location = eval_environment_lookup (env, sym, 1);

  if (scm_is_pair (location))
    {
      SCM_SETCDR (location, val);
      return SCM_ENVIRONMENT_SUCCESS;
    }
  else if (SCM_ENVIRONMENT_P (location))
    {
      return SCM_ENVIRONMENT_SET (location, sym, val);
    }
  else if (scm_is_eq (location, IMMUTABLE))
    {
      return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
    }
  else
    {
      return SCM_UNDEFINED;
    }
}
#undef FUNC_NAME


static SCM
eval_environment_cell (SCM env, SCM sym, int for_write)
#define FUNC_NAME "eval_environment_cell"
{
  SCM location = eval_environment_lookup (env, sym, for_write);

  if (scm_is_pair (location))
    return location;
  else if (SCM_ENVIRONMENT_P (location))
    return SCM_ENVIRONMENT_LOCATION_NO_CELL;
  else if (scm_is_eq (location, IMMUTABLE))
    return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
  else
    return SCM_UNDEFINED;
}
#undef FUNC_NAME


static SCM
eval_environment_mark (SCM env)
{
  struct eval_environment *body = EVAL_ENVIRONMENT (env);

  scm_gc_mark (body->obarray);
  scm_gc_mark (body->imported);
  scm_gc_mark (body->imported_observer);
  scm_gc_mark (body->local);
  scm_gc_mark (body->local_observer);

  return core_environments_mark (env);
}


static void
eval_environment_free (SCM env)
{
  core_environments_finalize (env);
  scm_gc_free (EVAL_ENVIRONMENT (env), sizeof (struct eval_environment),
	       "eval environment");
}


static int
eval_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED)
{
  SCM address = scm_from_size_t (SCM_UNPACK (type));
  SCM base16 = scm_number_to_string (address, scm_from_int (16));

  scm_puts ("#<eval environment ", port);
  scm_display (base16, port);
  scm_puts (">", port);

  return 1;
}


static struct scm_environment_funcs eval_environment_funcs = {
    eval_environment_ref,
    eval_environment_fold,
    eval_environment_define,
    eval_environment_undefine,
    eval_environment_set_x,
    eval_environment_cell,
    core_environments_observe,
    core_environments_unobserve,
    eval_environment_mark,
    eval_environment_free,
    eval_environment_print
};


void *scm_type_eval_environment = &eval_environment_funcs;


static void
eval_environment_observer (SCM caller SCM_UNUSED, SCM eval_env)
{
  SCM obarray = EVAL_ENVIRONMENT (eval_env)->obarray;

  obarray_remove_all (obarray);
  core_environments_broadcast (eval_env);
}


SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0, 
	    (SCM local, SCM imported),
	    "Return a new environment object eval whose bindings are the\n"
	    "union of the bindings in the environments @var{local} and\n"
	    "@var{imported}, with bindings from @var{local} taking\n"
	    "precedence. Definitions made in eval are placed in @var{local}.\n"
	    "Applying @code{environment-define} or\n"
	    "@code{environment-undefine} to eval has the same effect as\n"
	    "applying the procedure to @var{local}.\n"
	    "Note that eval incorporates @var{local} and @var{imported} by\n"
	    "reference:\n"
	    "If, after creating eval, the program changes the bindings of\n"
	    "@var{local} or @var{imported}, those changes will be visible\n"
	    "in eval.\n"
	    "Since most Scheme evaluation takes place in eval environments,\n"
	    "they transparently cache the bindings received from @var{local}\n"
	    "and @var{imported}. Thus, the first time the program looks up\n"
	    "a symbol in eval, eval may make calls to @var{local} or\n"
	    "@var{imported} to find their bindings, but subsequent\n"
	    "references to that symbol will be as fast as references to\n"
	    "bindings in finite environments.\n"
	    "In typical use, @var{local} will be a finite environment, and\n"
	    "@var{imported} will be an import environment")
#define FUNC_NAME s_scm_make_eval_environment
{
  SCM env;
  struct eval_environment *body;

  SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME);

  body = scm_gc_malloc (sizeof (struct eval_environment), "eval environment");

  core_environments_preinit (&body->base);
  body->obarray = SCM_BOOL_F;
  body->imported = SCM_BOOL_F;
  body->imported_observer = SCM_BOOL_F;
  body->local = SCM_BOOL_F;
  body->local_observer = SCM_BOOL_F;

  env = scm_make_environment (body);

  core_environments_init (&body->base, &eval_environment_funcs);
  body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE);  
  body->imported = imported;
  body->imported_observer
    = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1);
  body->local = local;
  body->local_observer
    = SCM_ENVIRONMENT_OBSERVE (local, eval_environment_observer, env, 1);

  return env;
}
#undef FUNC_NAME


SCM_DEFINE (scm_eval_environment_p, "eval-environment?", 1, 0, 0,
	    (SCM object),
	    "Return @code{#t} if object is an eval environment, or @code{#f}\n"
	    "otherwise.")
#define FUNC_NAME s_scm_eval_environment_p
{
  return scm_from_bool (SCM_EVAL_ENVIRONMENT_P (object));
}
#undef FUNC_NAME


SCM_DEFINE (scm_eval_environment_local, "eval-environment-local", 1, 0, 0, 
	    (SCM env),
	    "Return the local environment of eval environment @var{env}.")
#define FUNC_NAME s_scm_eval_environment_local
{
  SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return EVAL_ENVIRONMENT (env)->local;
}
#undef FUNC_NAME


SCM_DEFINE (scm_eval_environment_set_local_x, "eval-environment-set-local!", 2, 0, 0, 
	    (SCM env, SCM local),
	    "Change @var{env}'s local environment to @var{local}.")
#define FUNC_NAME s_scm_eval_environment_set_local_x
{
  struct eval_environment *body;

  SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG2, FUNC_NAME);

  body = EVAL_ENVIRONMENT (env);

  obarray_remove_all (body->obarray);
  SCM_ENVIRONMENT_UNOBSERVE (body->local, body->local_observer);

  body->local = local;
  body->local_observer
    = SCM_ENVIRONMENT_OBSERVE (local, eval_environment_observer, env, 1);

  core_environments_broadcast (env);

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_eval_environment_imported, "eval-environment-imported", 1, 0, 0,
	    (SCM env),
	    "Return the imported environment of eval environment @var{env}.")
#define FUNC_NAME s_scm_eval_environment_imported
{
  SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return EVAL_ENVIRONMENT (env)->imported;
}
#undef FUNC_NAME


SCM_DEFINE (scm_eval_environment_set_imported_x, "eval-environment-set-imported!", 2, 0, 0, 
	    (SCM env, SCM imported),
	    "Change @var{env}'s imported environment to @var{imported}.")
#define FUNC_NAME s_scm_eval_environment_set_imported_x
{
  struct eval_environment *body;

  SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME);

  body = EVAL_ENVIRONMENT (env);

  obarray_remove_all (body->obarray);
  SCM_ENVIRONMENT_UNOBSERVE (body->imported, body->imported_observer);

  body->imported = imported;
  body->imported_observer
    = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1);

  core_environments_broadcast (env);

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME



/* import environments
 *
 * An import environment combines the bindings of a set of argument
 * environments, and checks for naming clashes.
 *
 * Implementation:  The import environment does no caching at all.  For every
 * access, the list of imported environments is scanned.
 */


struct import_environment {
  struct core_environments_base base;

  SCM imports;
  SCM import_observers;

  SCM conflict_proc;
};


#define IMPORT_ENVIRONMENT(env) \
  ((struct import_environment *) SCM_CELL_WORD_1 (env))



/* Lookup will report one of the following distinct results:
 * a) <environment> if only environment binds the symbol.
 * b) (env-1 env-2 ...) for conflicting bindings in env-1, ...
 * c) SCM_UNDEFINED if there is no binding for the symbol.
 */
static SCM
import_environment_lookup (SCM env, SCM sym)
{
  SCM imports = IMPORT_ENVIRONMENT (env)->imports;
  SCM result = SCM_UNDEFINED;
  SCM l;

  for (l = imports; !scm_is_null (l); l = SCM_CDR (l))
    {
      SCM imported = SCM_CAR (l);

      if (SCM_ENVIRONMENT_BOUND_P (imported, sym))
	{
	  if (SCM_UNBNDP (result))
	    result = imported;
	  else if (scm_is_pair (result))
	    result = scm_cons (imported, result);
	  else
	    result = scm_cons2 (imported, result, SCM_EOL);
	}
    }

  if (scm_is_pair (result))
    return scm_reverse (result);
  else
    return result;
}


static SCM
import_environment_conflict (SCM env, SCM sym, SCM imports)
{
  SCM conflict_proc = IMPORT_ENVIRONMENT (env)->conflict_proc;
  SCM args = scm_cons2 (env, sym, scm_cons (imports, SCM_EOL));

  return scm_apply_0 (conflict_proc, args);
}


static SCM
import_environment_ref (SCM env, SCM sym)
#define FUNC_NAME "import_environment_ref"
{
  SCM owner = import_environment_lookup (env, sym);

  if (SCM_UNBNDP (owner))
    {
      return SCM_UNDEFINED;
    }
  else if (scm_is_pair (owner))
    {
      SCM resolve = import_environment_conflict (env, sym, owner);

      if (SCM_ENVIRONMENT_P (resolve))
	return SCM_ENVIRONMENT_REF (resolve, sym);
      else
	return SCM_UNSPECIFIED;
    }
  else
    {
      return SCM_ENVIRONMENT_REF (owner, sym);
    }
}
#undef FUNC_NAME


static SCM
import_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail)
#define FUNC_NAME "import_environment_fold"
{
  SCM import_env = SCM_CAR (extended_data);
  SCM imported_env = SCM_CADR (extended_data);
  SCM owner = import_environment_lookup (import_env, symbol);
  SCM proc_as_nr = SCM_CADDR (extended_data);
  unsigned long int proc_as_ul = scm_to_ulong (proc_as_nr);
  scm_environment_folder proc = (scm_environment_folder) proc_as_ul;
  SCM data = SCM_CDDDR (extended_data);

  if (scm_is_pair (owner) && scm_is_eq (SCM_CAR (owner), imported_env))
    owner = import_environment_conflict (import_env, symbol, owner);

  if (SCM_ENVIRONMENT_P (owner))
    return (*proc) (data, symbol, value, tail);
  else /* unresolved conflict */
    return (*proc) (data, symbol, SCM_UNSPECIFIED, tail);
}
#undef FUNC_NAME


static SCM
import_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
{
  SCM proc_as_nr = scm_from_ulong ((unsigned long) proc);
  SCM result = init;
  SCM l;

  for (l = IMPORT_ENVIRONMENT (env)->imports; !scm_is_null (l); l = SCM_CDR (l))
    {
      SCM imported_env = SCM_CAR (l);
      SCM extended_data = scm_cons (env, scm_cons2 (imported_env, proc_as_nr, data));

      result = scm_c_environment_fold (imported_env, import_environment_folder, extended_data, result);
    }

  return result;
}


static SCM
import_environment_define (SCM env SCM_UNUSED,
			   SCM sym SCM_UNUSED,
			   SCM val SCM_UNUSED)
#define FUNC_NAME "import_environment_define"
{
  return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
}
#undef FUNC_NAME


static SCM
import_environment_undefine (SCM env SCM_UNUSED,
			     SCM sym SCM_UNUSED)
#define FUNC_NAME "import_environment_undefine"
{
  return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
}
#undef FUNC_NAME


static SCM
import_environment_set_x (SCM env, SCM sym, SCM val)
#define FUNC_NAME "import_environment_set_x"
{
  SCM owner = import_environment_lookup (env, sym);

  if (SCM_UNBNDP (owner))
    {
      return SCM_UNDEFINED;
    }
  else if (scm_is_pair (owner))
    {
      SCM resolve = import_environment_conflict (env, sym, owner);

      if (SCM_ENVIRONMENT_P (resolve))
	return SCM_ENVIRONMENT_SET (resolve, sym, val);
      else
	return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
    }
  else
    {
      return SCM_ENVIRONMENT_SET (owner, sym, val);
    }
}
#undef FUNC_NAME


static SCM
import_environment_cell (SCM env, SCM sym, int for_write)
#define FUNC_NAME "import_environment_cell"
{
  SCM owner = import_environment_lookup (env, sym);

  if (SCM_UNBNDP (owner))
    {
      return SCM_UNDEFINED;
    }
  else if (scm_is_pair (owner))
    {
      SCM resolve = import_environment_conflict (env, sym, owner);

      if (SCM_ENVIRONMENT_P (resolve))
	return SCM_ENVIRONMENT_CELL (resolve, sym, for_write);
      else
	return SCM_ENVIRONMENT_LOCATION_NO_CELL;
    }
  else
    {
      return SCM_ENVIRONMENT_CELL (owner, sym, for_write);
    }
}
#undef FUNC_NAME


static SCM
import_environment_mark (SCM env)
{
  scm_gc_mark (IMPORT_ENVIRONMENT (env)->imports);
  scm_gc_mark (IMPORT_ENVIRONMENT (env)->import_observers);
  scm_gc_mark (IMPORT_ENVIRONMENT (env)->conflict_proc);
  return core_environments_mark (env);
}


static void
import_environment_free (SCM env)
{
  core_environments_finalize (env);
  scm_gc_free (IMPORT_ENVIRONMENT (env), sizeof (struct import_environment),
	       "import environment");
}


static int
import_environment_print (SCM type, SCM port, 
			  scm_print_state *pstate SCM_UNUSED)
{
  SCM address = scm_from_size_t (SCM_UNPACK (type));
  SCM base16 = scm_number_to_string (address, scm_from_int (16));

  scm_puts ("#<import environment ", port);
  scm_display (base16, port);
  scm_puts (">", port);

  return 1;
}


static struct scm_environment_funcs import_environment_funcs = {
  import_environment_ref,
  import_environment_fold,
  import_environment_define,
  import_environment_undefine,
  import_environment_set_x,
  import_environment_cell,
  core_environments_observe,
  core_environments_unobserve,
  import_environment_mark,
  import_environment_free,
  import_environment_print
};


void *scm_type_import_environment = &import_environment_funcs;


static void
import_environment_observer (SCM caller SCM_UNUSED, SCM import_env)
{
  core_environments_broadcast (import_env);
}


SCM_DEFINE (scm_make_import_environment, "make-import-environment", 2, 0, 0, 
	    (SCM imports, SCM conflict_proc),
	    "Return a new environment @var{imp} whose bindings are the union\n"
	    "of the bindings from the environments in @var{imports};\n"
	    "@var{imports} must be a list of environments. That is,\n"
	    "@var{imp} binds a symbol to a location when some element of\n"
	    "@var{imports} does.\n"
	    "If two different elements of @var{imports} have a binding for\n"
	    "the same symbol, the @var{conflict-proc} is called with the\n"
	    "following parameters:  the import environment, the symbol and\n"
	    "the list of the imported environments that bind the symbol.\n"
	    "If the @var{conflict-proc} returns an environment @var{env},\n"
	    "the conflict is considered as resolved and the binding from\n"
	    "@var{env} is used.  If the @var{conflict-proc} returns some\n"
	    "non-environment object, the conflict is considered unresolved\n"
	    "and the symbol is treated as unspecified in the import\n"
	    "environment.\n"
	    "The checking for conflicts may be performed lazily, i. e. at\n"
	    "the moment when a value or binding for a certain symbol is\n"
	    "requested instead of the moment when the environment is\n"
	    "created or the bindings of the imports change.\n"
	    "All bindings in @var{imp} are immutable. If you apply\n"
	    "@code{environment-define} or @code{environment-undefine} to\n"
	    "@var{imp}, Guile will signal an\n"
	    " @code{environment:immutable-binding} error. However,\n"
	    "notice that the set of bindings in @var{imp} may still change,\n"
	    "if one of its imported environments changes.")
#define FUNC_NAME s_scm_make_import_environment
{
  size_t size = sizeof (struct import_environment);
  struct import_environment *body = scm_gc_malloc (size, "import environment");
  SCM env;

  core_environments_preinit (&body->base);
  body->imports = SCM_BOOL_F;
  body->import_observers = SCM_BOOL_F;
  body->conflict_proc = SCM_BOOL_F;

  env = scm_make_environment (body);

  core_environments_init (&body->base, &import_environment_funcs);
  body->imports = SCM_EOL;
  body->import_observers = SCM_EOL;
  body->conflict_proc = conflict_proc;

  scm_import_environment_set_imports_x (env, imports);

  return env;
}
#undef FUNC_NAME


SCM_DEFINE (scm_import_environment_p, "import-environment?", 1, 0, 0, 
	    (SCM object),
	    "Return @code{#t} if object is an import environment, or\n"
	    "@code{#f} otherwise.")
#define FUNC_NAME s_scm_import_environment_p
{
  return scm_from_bool (SCM_IMPORT_ENVIRONMENT_P (object));
}
#undef FUNC_NAME


SCM_DEFINE (scm_import_environment_imports, "import-environment-imports", 1, 0, 0, 
	    (SCM env),
	    "Return the list of environments imported by the import\n"
	    "environment @var{env}.")
#define FUNC_NAME s_scm_import_environment_imports
{
  SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return IMPORT_ENVIRONMENT (env)->imports;
}
#undef FUNC_NAME


SCM_DEFINE (scm_import_environment_set_imports_x, "import-environment-set-imports!", 2, 0, 0, 
	    (SCM env, SCM imports),
	    "Change @var{env}'s list of imported environments to\n"
	    "@var{imports}, and check for conflicts.")
#define FUNC_NAME s_scm_import_environment_set_imports_x
{
  struct import_environment *body = IMPORT_ENVIRONMENT (env);
  SCM import_observers = SCM_EOL;
  SCM l;

  SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  for (l = imports; scm_is_pair (l); l = SCM_CDR (l))
    {
      SCM obj = SCM_CAR (l);
      SCM_ASSERT (SCM_ENVIRONMENT_P (obj), imports, SCM_ARG2, FUNC_NAME);
    }
  SCM_ASSERT (scm_is_null (l), imports, SCM_ARG2, FUNC_NAME);

  for (l = body->import_observers; !scm_is_null (l); l = SCM_CDR (l))
    {
      SCM obs = SCM_CAR (l);
      SCM_ENVIRONMENT_UNOBSERVE (env, obs);
    }

  for (l = imports; !scm_is_null (l); l = SCM_CDR (l))
    {
      SCM imp = SCM_CAR (l);
      SCM obs = SCM_ENVIRONMENT_OBSERVE (imp, import_environment_observer, env, 1);
      import_observers = scm_cons (obs, import_observers);
    }

  body->imports = imports;
  body->import_observers = import_observers;

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME



/* export environments
 *
 * An export environment restricts an environment to a specified set of
 * bindings.
 *
 * Implementation:  The export environment does no caching at all.  For every
 * access, the signature is scanned.  The signature that is stored internally
 * is an alist of pairs (symbol . (mutability)).
 */


struct export_environment {
  struct core_environments_base base;

  SCM private;
  SCM private_observer;

  SCM signature;
};


#define EXPORT_ENVIRONMENT(env) \
  ((struct export_environment *) SCM_CELL_WORD_1 (env))


SCM_SYMBOL (symbol_immutable_location, "immutable-location");
SCM_SYMBOL (symbol_mutable_location, "mutable-location");



static SCM
export_environment_ref (SCM env, SCM sym)
#define FUNC_NAME "export_environment_ref"
{
  struct export_environment *body = EXPORT_ENVIRONMENT (env);
  SCM entry = scm_assq (sym, body->signature);

  if (scm_is_false (entry))
    return SCM_UNDEFINED;
  else
    return SCM_ENVIRONMENT_REF (body->private, sym);
}
#undef FUNC_NAME


static SCM
export_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
{
  struct export_environment *body = EXPORT_ENVIRONMENT (env);
  SCM result = init;
  SCM l;

  for (l = body->signature; !scm_is_null (l); l = SCM_CDR (l))
    {
      SCM symbol = SCM_CAR (l);
      SCM value = SCM_ENVIRONMENT_REF (body->private, symbol);
      if (!SCM_UNBNDP (value))
	result = (*proc) (data, symbol, value, result);
    }
  return result;
}


static SCM
export_environment_define (SCM env SCM_UNUSED, 
			   SCM sym SCM_UNUSED, 
			   SCM val SCM_UNUSED)
#define FUNC_NAME "export_environment_define"
{
  return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
}
#undef FUNC_NAME


static SCM
export_environment_undefine (SCM env SCM_UNUSED, SCM sym SCM_UNUSED)
#define FUNC_NAME "export_environment_undefine"
{
  return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
}
#undef FUNC_NAME


static SCM
export_environment_set_x (SCM env, SCM sym, SCM val)
#define FUNC_NAME "export_environment_set_x"
{
  struct export_environment *body = EXPORT_ENVIRONMENT (env);
  SCM entry = scm_assq (sym, body->signature);

  if (scm_is_false (entry))
    {
      return SCM_UNDEFINED;
    }
  else
    {
      if (scm_is_eq (SCM_CADR (entry), symbol_mutable_location))
	return SCM_ENVIRONMENT_SET (body->private, sym, val);
      else
	return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
    }
}
#undef FUNC_NAME


static SCM
export_environment_cell (SCM env, SCM sym, int for_write)
#define FUNC_NAME "export_environment_cell"
{
  struct export_environment *body = EXPORT_ENVIRONMENT (env);
  SCM entry = scm_assq (sym, body->signature);

  if (scm_is_false (entry))
    {
      return SCM_UNDEFINED;
    }
  else
    {
      if (!for_write || scm_is_eq (SCM_CADR (entry), symbol_mutable_location))
	return SCM_ENVIRONMENT_CELL (body->private, sym, for_write);
      else
	return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
    }
}
#undef FUNC_NAME


static SCM
export_environment_mark (SCM env)
{
  struct export_environment *body = EXPORT_ENVIRONMENT (env);

  scm_gc_mark (body->private);
  scm_gc_mark (body->private_observer);
  scm_gc_mark (body->signature);

  return core_environments_mark (env);
}


static void
export_environment_free (SCM env)
{
  core_environments_finalize (env);
  scm_gc_free (EXPORT_ENVIRONMENT (env), sizeof (struct export_environment),
	       "export environment");
}


static int
export_environment_print (SCM type, SCM port,
			  scm_print_state *pstate SCM_UNUSED)
{
  SCM address = scm_from_size_t (SCM_UNPACK (type));
  SCM base16 = scm_number_to_string (address, scm_from_int (16));

  scm_puts ("#<export environment ", port);
  scm_display (base16, port);
  scm_puts (">", port);

  return 1;
}


static struct scm_environment_funcs export_environment_funcs = {
  export_environment_ref,
  export_environment_fold,
  export_environment_define,
  export_environment_undefine,
  export_environment_set_x,
  export_environment_cell,
  core_environments_observe,
  core_environments_unobserve,
  export_environment_mark,
  export_environment_free,
  export_environment_print
};


void *scm_type_export_environment = &export_environment_funcs;


static void
export_environment_observer (SCM caller SCM_UNUSED, SCM export_env)
{
  core_environments_broadcast (export_env);
}


SCM_DEFINE (scm_make_export_environment, "make-export-environment", 2, 0, 0, 
	    (SCM private, SCM signature),
	    "Return a new environment @var{exp} containing only those\n"
	    "bindings in private whose symbols are present in\n"
	    "@var{signature}. The @var{private} argument must be an\n"
	    "environment.\n\n"
	    "The environment @var{exp} binds symbol to location when\n"
	    "@var{env} does, and symbol is exported by @var{signature}.\n\n"
	    "@var{signature} is a list specifying which of the bindings in\n"
	    "@var{private} should be visible in @var{exp}. Each element of\n"
	    "@var{signature} should be a list of the form:\n"
	    "  (symbol attribute ...)\n"
	    "where each attribute is one of the following:\n"
	    "@table @asis\n"
	    "@item the symbol @code{mutable-location}\n"
	    "  @var{exp} should treat the\n"
	    "  location bound to symbol as mutable. That is, @var{exp}\n"
	    "  will pass calls to @code{environment-set!} or\n"
	    "  @code{environment-cell} directly through to private.\n"
	    "@item the symbol @code{immutable-location}\n"
	    "  @var{exp} should treat\n"
	    "  the location bound to symbol as immutable. If the program\n"
	    "  applies @code{environment-set!} to @var{exp} and symbol, or\n"
	    "  calls @code{environment-cell} to obtain a writable value\n"
	    "  cell, @code{environment-set!} will signal an\n"
	    "  @code{environment:immutable-location} error. Note that, even\n"
	    "  if an export environment treats a location as immutable, the\n"
	    "  underlying environment may treat it as mutable, so its\n"
	    "  value may change.\n"
	    "@end table\n"
	    "It is an error for an element of signature to specify both\n"
	    "@code{mutable-location} and @code{immutable-location}. If\n"
	    "neither is specified, @code{immutable-location} is assumed.\n\n"
	    "As a special case, if an element of signature is a lone\n"
	    "symbol @var{sym}, it is equivalent to an element of the form\n"
	    "@code{(sym)}.\n\n"
	    "All bindings in @var{exp} are immutable. If you apply\n"
	    "@code{environment-define} or @code{environment-undefine} to\n"
	    "@var{exp}, Guile will signal an\n"
	    "@code{environment:immutable-binding} error. However,\n"
	    "notice that the set of bindings in @var{exp} may still change,\n"
	    "if the bindings in private change.")
#define FUNC_NAME s_scm_make_export_environment
{
  size_t size;
  struct export_environment *body;
  SCM env;

  SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG1, FUNC_NAME);

  size = sizeof (struct export_environment);
  body = scm_gc_malloc (size, "export environment");

  core_environments_preinit (&body->base);
  body->private = SCM_BOOL_F;
  body->private_observer = SCM_BOOL_F;
  body->signature = SCM_BOOL_F;

  env = scm_make_environment (body);

  core_environments_init (&body->base, &export_environment_funcs);
  body->private = private;
  body->private_observer
    = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer, env, 1);
  body->signature = SCM_EOL;

  scm_export_environment_set_signature_x (env, signature);

  return env;
}
#undef FUNC_NAME


SCM_DEFINE (scm_export_environment_p, "export-environment?", 1, 0, 0, 
	    (SCM object),
	    "Return @code{#t} if object is an export environment, or\n"
	    "@code{#f} otherwise.")
#define FUNC_NAME s_scm_export_environment_p
{
  return scm_from_bool (SCM_EXPORT_ENVIRONMENT_P (object));
}
#undef FUNC_NAME


SCM_DEFINE (scm_export_environment_private, "export-environment-private", 1, 0, 0, 
	    (SCM env),
	    "Return the private environment of export environment @var{env}.")
#define FUNC_NAME s_scm_export_environment_private
{
  SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return EXPORT_ENVIRONMENT (env)->private;
}
#undef FUNC_NAME


SCM_DEFINE (scm_export_environment_set_private_x, "export-environment-set-private!", 2, 0, 0, 
	    (SCM env, SCM private),
	    "Change the private environment of export environment @var{env}.")
#define FUNC_NAME s_scm_export_environment_set_private_x
{
  struct export_environment *body;

  SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG2, FUNC_NAME);

  body = EXPORT_ENVIRONMENT (env);
  SCM_ENVIRONMENT_UNOBSERVE (private, body->private_observer);

  body->private = private;
  body->private_observer
    = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer, env, 1);

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME


SCM_DEFINE (scm_export_environment_signature, "export-environment-signature", 1, 0, 0, 
	    (SCM env),
	    "Return the signature of export environment @var{env}.")
#define FUNC_NAME s_scm_export_environment_signature
{
  SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);

  return EXPORT_ENVIRONMENT (env)->signature;
}
#undef FUNC_NAME


static SCM
export_environment_parse_signature (SCM signature, const char* caller)
{
  SCM result = SCM_EOL;
  SCM l;

  for (l = signature; scm_is_pair (l); l = SCM_CDR (l))
    {
      SCM entry = SCM_CAR (l);

      if (scm_is_symbol (entry))
	{
	  SCM new_entry = scm_cons2 (entry, symbol_immutable_location, SCM_EOL);
	  result = scm_cons (new_entry, result);
	}
      else
	{
	  SCM sym;
	  SCM new_entry;
	  int immutable = 0;
	  int mutable = 0;
	  SCM mutability;
	  SCM l2;

	  SCM_ASSERT (scm_is_pair (entry), entry, SCM_ARGn, caller);
	  SCM_ASSERT (scm_is_symbol (SCM_CAR (entry)), entry, SCM_ARGn, caller);

	  sym = SCM_CAR (entry);

	  for (l2 = SCM_CDR (entry); scm_is_pair (l2); l2 = SCM_CDR (l2))
	    {
	      SCM attribute = SCM_CAR (l2);
	      if (scm_is_eq (attribute, symbol_immutable_location))
		immutable = 1;
	      else if (scm_is_eq (attribute, symbol_mutable_location))
		mutable = 1;
	      else
		SCM_ASSERT (0, entry, SCM_ARGn, caller);
	    }
	  SCM_ASSERT (scm_is_null (l2), entry, SCM_ARGn, caller);
	  SCM_ASSERT (!mutable || !immutable, entry, SCM_ARGn, caller);

	  if (!mutable && !immutable)
	    immutable = 1;

	  mutability = mutable ? symbol_mutable_location : symbol_immutable_location;
	  new_entry = scm_cons2 (sym, mutability, SCM_EOL);
	  result = scm_cons (new_entry, result);
	}
    }
  SCM_ASSERT (scm_is_null (l), signature, SCM_ARGn, caller);

  /* Dirk:FIXME:: Now we know that signature is syntactically correct.  There
   * are, however, no checks for symbols entered twice with contradicting
   * mutabilities.  It would be nice, to implement this test, to be able to
   * call the sort functions conveniently from C.
   */

  return scm_reverse (result);
}


SCM_DEFINE (scm_export_environment_set_signature_x, "export-environment-set-signature!", 2, 0, 0, 
	    (SCM env, SCM signature),
	    "Change the signature of export environment @var{env}.")
#define FUNC_NAME s_scm_export_environment_set_signature_x
{
  SCM parsed_sig;

  SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
  parsed_sig = export_environment_parse_signature (signature, FUNC_NAME);

  EXPORT_ENVIRONMENT (env)->signature = parsed_sig;

  return SCM_UNSPECIFIED;
}
#undef FUNC_NAME



void
scm_environments_prehistory ()
{
  /* create environment smob */
  scm_tc16_environment = scm_make_smob_type ("environment", 0);
  scm_set_smob_mark (scm_tc16_environment, environment_mark);
  scm_set_smob_free (scm_tc16_environment, environment_free);
  scm_set_smob_print (scm_tc16_environment, environment_print);

  /* create observer smob */
  scm_tc16_observer = scm_make_smob_type ("observer", 0);
  scm_set_smob_mark (scm_tc16_observer, observer_mark);
  scm_set_smob_print (scm_tc16_observer, observer_print);

  /* create system environment */
  scm_system_environment = scm_make_leaf_environment ();
  scm_permanent_object (scm_system_environment);
}


void
scm_init_environments ()
{
#include "libguile/environments.x"
}


/*
  Local Variables:
  c-file-style: "gnu"
  End:
*/