Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ghilbert.py
blob: 19e7bef2a06599cb1e35d08345595c216bcc37ed (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
# ghilbert.py - ghilbert proof verifier model
# -*- coding: UTF-8 -*-

# ghilbert (www.ghilbert.org) is a formal proof verification language
# (and a python implementation of the proof verifier), developed by 
# Raph Levien.  Ghilbert extends metamath (http://metamath.org/), adding
# features for collaborative theorem-proving on the internet.

import array, os, sys
import logging

from gettext import gettext as _

# TRANS: To preserve interoperability, the ghilbert keywords used in proof
# and interface files ('kind', 'term', 'var', 'stmt', 'kindbind', 'equiv',
# 'param', 'thm', 'def', 'import', 'export', and so on) are not to be
# localized.
#
# This string is used as an error message when a proof or interface
# file names a particular kind which does not yet exist,
# in a context where the name of an existing kind is expected:
KIND_X_DOES_NOT_EXIST = _("Kind '%s' does not exist")

class GhError(Exception):
    """Base class for Ghilbert exceptions"""
    def __init__(self, why):
        self.args = (why,)

class GhProofEndError(GhError):
    pass

class GhMissingDvcsError(GhError):
    def __init__(self, why, needed):
        self.args = (why,)
        self.needed = needed

class GhExtraDvcsError(GhError):
    def __init__(self, why, extra):
        self.args = (why,)
        self.extra = extra

class VerifyError(GhError):
    def __init__(self, why):
        self.args = (why,)

class GhCmdExprError(GhError):
    def __init__(self, why, expr, path=()):
        self.args = (why,)
        self.expr = expr
        self.path = path  # sub-expression path in expr, e.g. (3, 1, 0)

class SyntaxError(GhError):
    def __init__(self, why, scanner):
        self.args = (why,)
        self.scanner = scanner

class StreamScanner:
    def __init__(self, instream):
	self.instream = instream
	self.lineno = 0
	self.toks = []
	self.tokix = 0
    def get_token(self):
	while len(self.toks) == self.tokix:
	    line = self.instream.readline()
	    self.lineno += 1
	    if line == '':
		return None
            line = line.split('#')[0]
            line = line.replace('(', ' ( ')
            line = line.replace(')', ' ) ')
            self.toks = line.split()
	    self.tokix = 0
	result = self.toks[self.tokix]
	self.tokix += 1
	return result

def read_sexp(scanner):
    """Read an s-expression from the specified scanner

    Note: may return ')' when ')' occurs unmatched; caller should
    check this.
    """
    while 1:
	tok = scanner.get_token()
	if tok is None:
	    return None
	if tok == '(':
	    result = []
	    while 1:
		subsexp = read_sexp(scanner)
		if subsexp == ')':
		    break
		elif subsexp is None:
		    raise SyntaxError(_('Incomplete (...) s-expression.'),
                                      scanner)
		result.append(subsexp)
	    return result
	else:
	    return tok

# Proof in Progress
class Pip():
    def __init__(self):
        self.exprs = [] # main expression stack
        self.wvs = []   # wild variable expression stack
        self.dvreqs = set()
        self.hyps = {} # map from hyp. names to hyp. internal expressions
        self.vdict = {} # map from variable names to (Ghilbert.VARIX, k, ix)
        self.vlist = [] # map from variable indices to (Ghilbert.VAR, k, vname)
        self.hyps_concs = []
        self.nwv = 0
        self.nvars = 0
        self.dvs = None
        self.equivFlag = False
        self.proof = []

def has_var(ix, expr, argmap):
    if expr[0] is Ghilbert.VARIX:
        ix2 = expr[2]
        if argmap is None:
            return (ix == ix2)
        return has_var(ix, argmap[ix2][0], argmap[ix2][1])
    # expr is (Ghilbert.TERM, termid[1], termid, subexp, ...)
    for e in expr[3:]:
        if has_var(ix, e, argmap):
            return True
    return False

def relvarsof(expr, vset):
    if expr[0] is Ghilbert.VARIX:
        vset.add(expr[2])
    else:
        # (Ghilbert.TERM, termid[1], termid, subexp, ...)
        for e in expr[3:]:
            relvarsof(e, vset)

# This one probably doesn't need to be a method...
def hyp_match(e_stack, e_proto, vmap):
    """Match expression e_stack against e_proto, extending vmap

    vmap is either None, or a list that maps variable indices (from
    the applied statement's hypotheses and conclusions) to internal
    expressions. vmap[i] is None if index i hasn't been assigned yet.
    """

    # short cut:
    if vmap is None and e_stack is e_proto:
        return

    if e_proto[0] == Ghilbert.VARIX:
        if vmap is None:
            # (e_stack is e_proto) is ruled out by the shortcut case
            raise VerifyError(_("Expression mismatch for a variable"))
        vi = e_proto[2]  # The index
        mv = vmap[vi]
        if mv is None:
            if e_stack[1][2] is not e_proto[1][2]:
                raise VerifyError(_("Kind mismatch for variable %s") % v)
            vmap[vi] = e_stack
            return
        # exact match of mapped variable against e_stack
        hyp_match(e_stack, mv, None)
        return

    if e_stack[0] != Ghilbert.TERM:
        raise VerifyError(_("Expression mismatch"))

    # Matching terms (Ghilbert.TERM, termid[1], termid, subexp, ...)
    # Check for the exact same termid.
    #   This will have to change to support termbind!
    if e_stack[2] is not e_proto[2]:
        raise VerifyError(_("Term mismatch (%(term1)s vs. %(term2)s)") %
                          {"term1" : e_stack[2][2], "term2" :  e_proto[2][2]})
    # match the subexpressions of the term
    for j in xrange(3, len(e_stack)):
        hyp_match(e_stack[j], e_proto[j], vmap)

# When there are definition dummy variables involved in the
# expansion, we rely upon the equivalence proving well-definedness
# to indicate that the dummy variable could be replaced with any other
# variable of the same kind (that is distinct from the other variables
# in the RHS expansion), without changing the meaning of the RHS expansion
# in any context.  That means that we don't have to worry about the
# distinctness of the dummy variables outside of a given expansion.
#
# match conclusion c against remnant expression e.
# map is either None, in which case variables in c are matched literally
# against e, or is a list used to map each definition variable index to either
# a pair (expr, mapPrime), where expr is the expression substituted for
# that definition variable, and mapPrime is used in turn to interpret
# variables in expr; or to None, for a definition dummy variable that has
# not yet been assigned.
def match_expand(e, c, map):

    # (Ghilbert.TERM, termid[1], termid, subexp, ...)
    if c[0] is Ghilbert.TERM:
        termid = c[2]
        # The next line will need to change to support termbind
        if e[0] is Ghilbert.TERM and e[2] is termid:
            for j in xrange(3, len(c)):
                match_expand(e[j], c[j], map)
            return

        # Not an exact match. Unless c's termid indicates that c is
        # a definition term, we must fail. The termid for a definition
        # looks like this:
        # (Ghilbert.TERMID, kind-tuple, term-name, defn, arg-kind, ...)
        defn = termid[3]
        if defn is None:  # not a definition
            raise VerifyError(_("Conclusion mismatch"))
        # defn is (rhs_expr, ndummies, vtuple, proof)
        # proof is None if ndummies is 0
        nargs = (len(c) - 3)
        argmap = [None] * (nargs + defn[1])
        for i in xrange(nargs):
            argmap[i] = (c[i + 3], map)
        match_expand(e, defn[0], argmap)
        # check definition dummy mappings for distinctness from other
        # variables (including other dummies at the same level) occurring
        # in the expansion. Note, a def dummy may still match against a
        # variable occurring in the hypothesis or conclusion, if that variable
        # doesn't occur in the parts of the definition expansion coming from
        # subtitutions for the definition arguments.  Although odd, the
        # well-definedness proof asserts that such a variable could be replaced
        # by any fresh variable of the same kind without changing the 'meaning'
        # of the expression.
        for i in xrange(nargs, nargs + defn[1]):
            # The mapping expression of a dummy is known to be a VARIX.
            # All dummies of the expansion have been matched already;
            # argmap[i] is not still None.
            dummyix = argmap[i][0][2]
            for j in xrange(i):
                if has_var(dummyix, argmap[j][0], argmap[j][1]):
                    raise VerifyError(
                        _("Definition dummy distinctness failed"))
        return

    # c[0] is Ghilbert.VARIX
    if map is None:
        if e[0] is not Ghilbert.VARIX or e[2] is not c[2]:
            raise VerifyError(_("Conclusion mismatch"))
        return
    mv = map[c[2]]
    if mv is None: #c is definition dummy variable, not assigned yet
        if e[0] is not Ghilbert.VARIX or e[1] != c[1]:
            raise VerifyError(_("Conclusion mismatch"))
        # distinctness checked above.
        map[c[2]] = (e, None) # assign dummy
        return

    match_expand(e, mv[0], mv[1])
            
def substitute(exp, vmap):
    """Return 'exp' with all variables substituted according to vmap

    exp is a conclusion of the statement being applied in a proof.
    Its variables live in the 'namespace' of the interned statement;
    the substitutions for them are expressions in the 'namespace' of the
    theorem being proven.
    """

    if exp[0] == Ghilbert.VARIX:
        # here exp[2] is the index of the variable in the interned stat.
        return vmap[exp[2]]   # not expected to fail!

    # assert exp is (Ghilbert.TERM, termid[1], termid, def, subexp, ...)
    x = [exp[0], exp[1], exp[2]]
    for e in exp[3:]:
        x.append(substitute(e, vmap))
    return x

def match_exact(expr, rhs):
    if rhs[0] is Ghilbert.VARIX:
        return (expr[0] is Ghilbert.VARIX and expr[2] == rhs[2])
    # The next line will have to change to support termbind...
    if expr[0] is not Ghilbert.TERM or expr[2] != rhs[2]:
        return False
    for j in xrange(3, len(rhs)):
        if not match_exact(expr[j], rhs[j]):
            return False
    return True

# TRANS:  This is an error message for an error that occurs when checking
# the two resulting expressions of a definition justification proof for a
# definition with dummy variables.  There are several slightly different
# error cases, but we presently treat most of them with the same message.
DEFINITION_JUSTIFICATION_MISMATCH = _("Definition justification mismatch")

def match_defjust(expr, rhs, nvars, ndummies, vmap, image):
    if rhs[0] is Ghilbert.VARIX:
        if expr[0] is not Ghilbert.VARIX:
            raise VerifyError(DEFINITION_JUSTIFICATION_MISMATCH)
        if rhs[2] < nvars:
            # explicit definition argument variable must match exactly.
            if expr[2] != rhs[2]:
                raise VerifyError(DEFINITION_JUSTIFICATION_MISMATCH)
            return
        # rhs[2] is a dummy variable.
        ix = rhs[2] - nvars
        vm = vmap[ix]
        if vm is None:
            # This dummy is not mapped yet. Kinds must match.
            if expr[1][2] != rhs[1][2]:
                raise VerifyError(DEFINITION_JUSTIFICATION_MISMATCH)
            eix = expr[2]
            # The (eix < nvars + ndummies) check makes sure that the
            # dummy is mapped to a variable not in the original expression.
            # The (eix in image) check ensures a 1-1 map.
            if (eix < nvars + ndummies) or (eix in image):
                raise VerifyError(
                    _("Definition justification: 2nd expresson requires "
                      "distinct dummy variables!"))
            vmap[ix] = eix
            image.add(eix)
            return
        if vm != expr[2]:
            raise VerifyError(DEFINITION_JUSTIFICATION_MISMATCH)
        return
    # rhs is a term
    if expr[0] is not Ghilbert.TERM or expr[2] is not rhs[2]:
        raise VerifyError(DEFINITION_JUSTIFICATION_MISMATCH)
    for j in xrange(3, len(rhs)):
        match_defjust(expr[j], rhs[j], nvars, ndummies, vmap, image)
            

def iexpr_to_string(expr, vtuple):
    buf = array.array('c')
    iexpr_to_string_rec(expr, vtuple, buf)
    return buf.tostring()

def iexpr_to_string_rec(expr, vtuple, buf):
    if expr[0] is Ghilbert.VARIX:
        buf.fromstring(vtuple[expr[2]][2])
        return
    buf.fromstring('(')
    buf.fromstring(expr[2][2])  # term name
    for e in expr[3:]:
        buf.fromstring(' ')
        iexpr_to_string_rec(e, vtuple, buf)
    buf.fromstring(')')

def sexp_to_string(sexp):
    buf = array.array('c')
    sexp_to_string_rec(buf, sexp)
    return buf.tostring()

def sexp_to_string_rec(buf, sexp):
    if isinstance(sexp, basestring):
        buf.fromstring(sexp)
    elif type(sexp) == type([]):
        buf.fromstring('(')
        sp_string = ''
        for el in sexp:
            buf.fromstring(sp_string)
            sexp_to_string_rec(buf, el)
            sp_string = ' '
        buf.fromstring(')')

def sexp_subexp_substr(sexp, path):
    buf = array.array('c')
    start, stop = sexp_subexp_substr_rec(buf, sexp, path, 0)
    return (buf.tostring(), start, stop)

def sexp_subexp_substr_rec(buf, sexp, path, depth):
    start = len(buf)
    if isinstance(sexp, basestring):
        buf.fromstring(sexp)
        stop = len(buf)
        return (start, stop)
    stop = start
    # sexp is a list
    buf.fromstring('(')
    space = ''
    i = -1
    if depth < len(path):
        i = path[depth]
    for j in xrange(len(sexp)):
        buf.fromstring(space)
        space = ' '
        p = sexp_subexp_substr_rec(buf, sexp[j], path, depth + 1)
        if i == j:
            start, stop = p
    buf.fromstring(')')
    if depth == len(path):
        stop = len(buf)
    return (start, stop)

def dvs_to_string(dvs, vtuple):
    buf = array.array('c')
    space = ''
    for v, w in dvs:
        buf.fromstring(space)
        buf.fromstring('(')
        buf.fromstring(vtuple[v][2])
        buf.fromstring(' ')
        buf.fromstring(vtuple[w][2])
        buf.fromstring(')')
        space = ' '
    return buf.tostring()

def proof_to_string(steps, vtuple):
    buf = array.array('c')
    space = ''
    buf.fromstring(' (')
    for step in steps:
        buf.fromstring(space)
        space = ' '
        if isinstance(step, basestring): # hypothesis name
            buf.fromstring(step)
        elif step[0] is Ghilbert.TERM:
            buf.fromstring(iexpr_to_string(step, vtuple))
        elif step[0] is Ghilbert.VAR:
            buf.fromstring(step[2])
        else: # statement application
            buf.fromstring(step[1])
    buf.fromstring(')')
    return buf.tostring()



def get_stream(path, curdir, fpath, log):
    # paths in 'import' or 'export' starting with '/' are treated
    # as relative to any of the paths in fpath
    log.info("get_stream %s, %s", path, curdir)
    if path[0] == '/':
        log.info("fpath=%s", repr(fpath))
        for p in fpath:
            # don't use os.path.join()
            while p != '' and p[-1] == '/':
                p = p[:-1]
            rpath = p + path
            try:
                log.info(_("get_stream trying %s"), rpath)
                istream = open(rpath, "rb")
                break
            except IOError:
                continue
        else:
            raise GhError(_('Failed to open %s') % path)

        res = (istream, os.path.dirname(os.path.abspath(rpath)))
        return res
        
    if path[:5] == 'http:':
        raise GhError(_("http: URLs are not supported yet."))
    elif path[:8] == 'file:///':
        rpath = path[7:]
    elif path[:7] == 'file://':
        raise GhError(_("file://host/... URLs not supported."))
    else:
        rpath = os.path.join(curdir, path)

    try:
        log.info(_("get_stream try %s"), rpath)
        istream = open(rpath, "rb")
    except IOError:
        raise GhError(_('Failed to open %s') % rpath)

    res = (istream, os.path.dirname(os.path.abspath(rpath)))
    return res

class Ghilbert():
    STMT = 0
    EQUIV = 1
    TERMID = 2
    VARIX = 3
    VAR = 4
    KIND = 5
    KINDBIND = 6
    IMPORT = 7
    EXPORT = 8
    COMMENT = 9
    TERMBIND = 10
    TERM = 11           # Term expression instance

    def __init__(self, logger):
        # kinds is a mapping of kind names to kind names used to test
        # for kind equivalence
        self.kinds = {}
        self.terms = {}
        # statements and variables. These occur in the same namespace
        # (used as proof steps), but we could separate them, allowing
        # overlaps with some precedence rule... For now, we don't.
        self.syms = {}
        self.interfaces = {}
        self.history = []
        self.history_cb = None
        self.log = logger
        # The parent and prefix attributes are used in interface file context.
        # The parent is the proof file context.  The prefix is the prefix
        # specified in the import or export command.
        self.parent = None
        self.prefix = ''
        # Search path for .gh/.ghi files
        self.fpath = []
        self.curdir = '.'   # ha!
        self.file = ''
        # These are used only by interface files. (Subclass?)
        self.params = []
        self.params_used = 0
        self.mykinds = {}       # kinds introduced by this interface
        self.myterms = {}       # terms introduced by this interface
        self.verbosity = 0
        self.pip = None

    def push_history(self, item):
        self.history.append(item)
        # print self.hist_item_to_string(item)
        if self.history_cb:
            self.history_cb(True) # True indicates 'push'

    def pop_history(self):
        try:
            t = self.history.pop()
        except IndexError:
            raise GhError(_('No history left!'))

        if self.history_cb:
            self.history_cb(False) # False indicates 'pop'

        code = t[0]
        if code == Ghilbert.STMT or code == Ghilbert.EQUIV:
            # (Ghilbert.STMT, namepf, nhyps, hyps_concs, vlist, dvs)
            if len(t) > 7:
                del self.syms[t[1]]
            else:
                del self.parent.syms[t[1]]
        elif code == Ghilbert.COMMENT:
            pass  # That's all.
        elif code == Ghilbert.TERMID:
            # (Ghilbert.TERMID, kind-tuple, term-name, defn, arg-kind, ...)
            plen = len(self.prefix)
            pfname = t[2]
            if t[3] is None:
                del self.parent.terms[pfname]
            del self.terms[pfname[plen:]]
        elif code == Ghilbert.VAR:
            # If we allow variable redefinitions some time in the future,
            # we'll have to remember whether there is a previous overridden
            # definition, and restore it if so.
            for v in t[2]:
                del self.syms[v]
        elif code == Ghilbert.KIND:
            # [Ghilbert.KIND, OrigKind, RepresentativeKind]
            kind, kindrep = t[1:]
            assert kind == kindrep
            del self.kinds[kind]
            del self.parent.kinds[kind]
        elif code == Ghilbert.KINDBIND:
            k1, k2, modkinds, k2oldrep = t[1:]
            # modkinds is None for a kindbind done in the proof file
            if not modkinds:
                del self.kinds[k2]
                return
            for k in modkinds:
                self.kinds[k][2] = k2oldrep
        elif code == Ghilbert.IMPORT:
            child = t[1]
            child.hist_revert(0)
        elif code == Ghilbert.EXPORT:
            child = t[1]
            child.hist_revert(0)
            pass
        elif code == Ghilbert.TERMBIND:
            pass
        else:
            raise GhError(_('Unknown code %d') % code)

    def hist_revert(self, ix):
        self.log.warning ('hist_revert %d %d', ix, len(self.history))
        while len(self.history) > ix:
            self.pop_history()

    def hist_item_to_string(self, t):
        code = t[0]
        if code == Ghilbert.STMT or code == Ghilbert.EQUIV:
            # t = (Ghilbert.[STMT|EQUIV], namepf, nhyps, tuple(hyps_concs),
            #      tuple(vlist), nwv, dvs [, hypnames, proof, extravars])
            nhyps = t[2]
            hyps_concs = t[3]
            vtuple = t[4]
            l = []
            proofstr = ''
            if len(t) > 7:
                hypnames = t[7]
                cmd = 'thm'
                for j in xrange(nhyps):
                    h = hyps_concs[j]
                    hnam = hypnames[j]
                    l.append('(%s %s)' % (hnam, iexpr_to_string(h, vtuple)))
                hyps = ' '.join(l)
                proofstr = proof_to_string(t[8], vtuple + t[9])
##                 buf = array.array('c')
##                 space = ''
##                 buf.fromstring(' (')
##                 for step in t[8]:
##                     buf.fromstring(space)
##                     space = ' '
##                     if isinstance(step, basestring): # hypothesis name
##                         buf.fromstring(step)
##                     elif step[0] is Ghilbert.TERM:
##                         buf.fromstring(iexpr_to_string(step, vtuple + t[9]))
##                     elif step[0] is Ghilbert.VAR:
##                         buf.fromstring(step[2])
##                     else: # statement application
##                         buf.fromstring(step[1])
##                 buf.fromstring(')')
##                 proofstr = buf.tostring()
            else:
                cmd = 'stmt'
                if code == Ghilbert.EQUIV:
                    cmd = 'equiv'
                for h in hyps_concs[:nhyps]:
                    l.append(iexpr_to_string(h, vtuple))
                hyps = ' '.join(l)
            l = []
            for h in hyps_concs[nhyps:]:
                l.append(iexpr_to_string(h, vtuple))
            concs = ' '.join(l)
            dvs = t[6]
            dv = ''
            if dvs is not None:
                dv = dvs_to_string(dvs, vtuple)
            return ('%s (%s (%s) (%s) (%s)%s)' % 
                    (cmd, t[1], dv, hyps, concs, proofstr))
        elif code == Ghilbert.COMMENT:
            return t[1]
        elif code == Ghilbert.TERMID:
            # (Ghilbert.TERMID, resultKind, term-name, defn, arg-kind, ...)
            defn = t[3]
            nargs = len(t) - 4
            space = ''
            if nargs != 0:
                space = ' '
            if defn is None:
                result = ' '.join([ak[1] for ak in t[4:]])
                return 'term (%s (%s%s%s))' % (t[1][1], t[2], space, result)
            rhs, ndummies, vtuple, proof = defn
            args = ' '.join([arg[2] for arg in vtuple[:nargs]])
            rhs_str = iexpr_to_string(rhs, vtuple)
            if proof == None:
                return 'def ((%s%s%s) %s)' % (t[2], space, args, rhs_str)
            proofstr = proof_to_string(proof, vtuple)
            return 'def ((%s%s%s) %s%s)' % \
                   (t[2], args, space, rhs_str, proofstr)
        elif code == Ghilbert.VAR:
            result = ''
            for v in t[2]:
                result = result + ' ' + v
            return 'var (%s%s)' % (t[1][1], result)
        elif code == Ghilbert.KIND:
            return 'kind (%s)' % (t[1])
        elif code == Ghilbert.KINDBIND:
            return 'kindbind (%s %s)' % (t[1:3])
        elif code == Ghilbert.IMPORT or code == Ghilbert.EXPORT:
            child = t[1]
            params = ' '.join([x.ifname for x in child.params])
            cmd = ('import', 'export')[code == Ghilbert.EXPORT]
            return ('%s (%s %s (%s) "%s")' %
                    (cmd, child.ifname, t[2], params, child.prefix))
        elif code == Ghilbert.TERMBIND:
            return ''
        else:
            raise GhError(_('Unknown code %d' % code))

    def add_kind(self, expr):
        """Add the specified new kind name."""
        if len(expr) != 1 or not isinstance(expr[0], basestring):
                raise GhCmdExprError(_("Expected 'kind (KINDNAME)'"), expr, ())
        kind = expr[0]
        kindpf = self.prefix + kind
        if self.parent.kinds.has_key(kindpf):
            raise GhCmdExprError(
                    _("Kind '%s' already exists in proof context") % kindpf,
                    expr, (0,))
        # Suppose kind is 'wff' and self.prefix is 'p', and there is
        # an earlier param command using prefix 'w' for an interface that
        # defines a kind 'ff'.  Then we could already have 'wff' in self.kinds
        # although 'pwff' is not in self.parent.kinds.
        if self.kinds.has_key(kind):
            raise GhCmdExprError(
                    _("Kind '%s' already exists in interface context") % kind,
                    expr, (0,))
        # t must be mutable to allow kindbind; use list rather than tuple
        t = [Ghilbert.KIND, kindpf, kindpf]
        self.parent.kinds[kindpf] = t
        self.kinds[kind] = t
        # XXX Pre-existing kinds may be equivalent in the parent (proof file
        # context) without being _visibly_ equivalent in the interface file
        # context. (The equivalence would be 'visible' if it were the result
        # of kindbinds seen either earlier in the interface file itself or
        # its param interfaces.)  Invisible kind equivalence can occur
        # if some earlier imported interface, which is not one of the
        # parameters of the current interface, did the kindbind. Both
        # kinds may be visible although the kindbind isn't, since we allow
        # an import to kindbind two kinds, neither of which it introduced
        # itself. Kindbind transitivity can also cause something similar.
        # This could result in a use elsewhere of the interface file
        # giving a kind mismatch error, even though its use in the current
        # environment (where the kinds are equivalent) does not generate
        # an error. Since the interface file depends upon the two visible
        # kinds being equivalent, and since none of its parameter interfaces
        # provides this, the interface file itself should contain a kindbind
        # for the two kinds before it uses an expression requiring their
        # equivalence.  But, we don't enforce this.  The problem would only
        # be seen when the interface file is used in a context that doesn't
        # provide the kindbind.  The error would be caught, just not
        # proactively. (This can occur for an export as well as an import;
        # export is supposed to verify that the exported file could
        # subsequently be used as an import, but it doesn't catch the above
        # sort of kindbind issue.)

        # self.kinds is all the kinds visible in the interface file kind
        # namespace. self.mykinds is the kinds added by this interface
        # file (rather than received via a 'param' command)
        self.mykinds[kind] = t
        self.push_history(t) # may not need

    def kind_bind_if(self, expr):
        """Make two existing kinds equivalent. Used in an interface file."""

        if len(expr) != 2 or not isinstance(expr[0], basestring) \
               or not isinstance(expr[1], basestring):
            raise GhCmdExprError(_("Expected 'kindbind (KIND1 KIND2)'"),
                                 expr, ())
        try:
            k1 = self.kinds[expr[0]]
        except KeyError:
            raise VerifyError(KIND_X_DOES_NOT_EXIST % expr[0])
        try:
            k2 = self.kinds[expr[1]]
        except KeyError:
            raise VerifyError(KIND_X_DOES_NOT_EXIST % expr[1])

        # [Ghilbert.KIND, OrigKind, RepresentativeKind]
        k1r = k1[2]
        k2r = k2[2]
        if k1r is k2r:        # already equivalent
            return
        modkinds = []
        # Note, we need to look at self.parent.kinds here rather than
        # self.kinds, since there may be kinds equivalent to k2 in the parent
        # that this interface file doesn't know about.
        for k in self.parent.kinds.itervalues():
            if k[2] == k2r:
                modkinds.append(k[1])
                k[2] = k1r
        self.push_history((Ghilbert.KINDBIND, k1[1], k2[1], modkinds, k2r))

    def kind_bind_pf(self, expr):
        """Add an alias for an old kind. Used in a proof file."""

        if len(expr) != 2 or not isinstance(expr[0], basestring) \
               or not isinstance(expr[1], basestring):
            raise GhCmdExprError(_("Expected 'kindbind (KIND1 KIND2)'"),
                                 expr, ())
        try:
            k1 = self.kinds[expr[0]]
        except KeyError:
            raise VerifyError(KIND_X_DOES_NOT_EXIST % expr[0])
        new_kind = expr[1]
        if self.kinds.has_key(new_kind):
            raise VerifyError(_("Kind '%s' already exists") % new_kind)
        self.kinds[new_kind] = [Ghilbert.KIND, new_kind, k1[2]]
        t = (Ghilbert.KINDBIND, k1[1], new_kind, None, None)
        self.push_history(t)

    def add_vars(self, expr):
        """Add a variable."""
        try:
            kind = expr[0]
        except IndexError:
            raise GhCmdExprError(_("Expected 'var (KINDNAME VARNAME ...)'"),
                                 expr, (0,))
        try:
            kt = self.kinds[kind]
        except KeyError:
            raise GhCmdExprError(KIND_X_DOES_NOT_EXIST % kind,
                                 expr, (0,))
        for j in xrange(1, len(expr)):
            var = expr[j]
            if not isinstance(var, basestring):
                raise GhCmdExprError(
                    _("Expected 'var (KINDNAME VARNAME ...)'"), expr, (j,))
                
            if self.syms.has_key(var):
                valu = self.syms[var]
                # Clean up vars added earlier in this command...
                for i in xrange(1, j):
                    del self.syms[expr[i]]
                if valu[0] == Ghilbert.VAR:
                    raise GhCmdExprError(
                        _("Variable '%s' already exists") % var, expr, (j,))
                else:
                    raise GhCmdExprError(
                        _("'%s' already exists as a statement") % var,
                        expr, (j,))
            # We put 'var' as the [2] element of the tuple to accomplish
            # interning of symbols.
            self.syms[var] = (Ghilbert.VAR, kt, var)

        self.push_history((Ghilbert.VAR, kt, expr[1:]))

    def add_term(self, sexpr):
        """Add a term."""
        errpath = None
        if len(sexpr) != 2:
            errpath = ()
        elif not isinstance(sexpr[0], basestring):
            errpath = (0,)
        elif type(sexpr[1]) != type([]) or len(sexpr[1]) < 1:
            errpath = (1,)

        if errpath != None:
            raise GhCmdExprError(
                    _("Expected 'term (TERMKIND (TERMNAME ARGKIND ...))'"),
                    sexpr, errpath)
        try:
            kr = self.kinds[sexpr[0]]
        except KeyError:
            raise GhCmdExprError(KIND_X_DOES_NOT_EXIST % sexpr[0],
                                 sexpr, (0,))
        expr = sexpr[1]
        term = expr[0]
        pfterm = self.prefix + term
        if pfterm in self.parent.terms:
            raise GhCmdExprError(
                _("(Prefixed) term '%s' already exists") % pfterm, sexpr,
                (1,0))
        tl = [Ghilbert.TERMID, kr, pfterm, None]
        for j in xrange(1, len(expr)):
            try:
                kt = self.kinds[expr[j]]
            except KeyError:
                raise GhCmdExprError(KIND_X_DOES_NOT_EXIST % expr[j],
                                     sexpr, (1,j))
            tl.append(kt)
        t = tuple(tl)
        self.parent.terms[pfterm] = t
        self.terms[term] = t
        # self.myterms is specifically those terms introduced in this
        # interface file (rather than by a previous 'param' interface)
        self.myterms[term] = t
        self.push_history(t)

    def expr_convert(self, expr, vardict, varlist, maketuple=True):
        """Convert an s-expression to internal form, and check well-formedness

        As they are encountered in the s-expression expr, new variables are
        added to the dictionalry 'vardict', mapped to the internal form
        expression which is a (Ghilbert.VARIX, k, index) tuple; the 'index'
        value increments each time a variable is added.  Variables are also
        added to varlist sequentially as (Ghilbert.VAR, k, vname) tuples.
        The 'varlist' will hold the hypothesis/conclusion/dummy variables of
        a statement or theorem, or the argument or dummy variables of a
        definition.

        Term s-expressions are replaced with nested tuples
        of the form (Ghilbert.TERM, resultKind, termid, termargexp, ...)
        where termid is a (Ghilbert.TERMID, resultKind, termname, def,
                           argkind, ...)
        and each termargexp is an internal (term or variable) expression form.
        Returns the internal tuple form of the expression.

        It's worth it to tuple-ize the hypotheses and conclusions of the
        theorem or statement, since this will save space when storing them,
        important on the XO.
        It's likely not worth it to tuple-ize all the expressions that occur
        on the proof stack or wild variable substitution stack during the
        course of a proof; tuples and lists don't have much performance
        difference as far as indexing goes.

        May need to revisit this if we store proofs.
        We now store proofs and tuplize terms that occur as wild variable
        substitutions.
        """
        if isinstance(expr, basestring):
            try:
                vix = vardict[expr]
            except KeyError:
                try:
                    var = self.syms[expr]
                except KeyError:
                    raise GhCmdExprError(_("Unknown variable '%s'") % expr,
                                         expr, ())
                if var[0] != Ghilbert.VAR:
                    raise GhCmdExprError(
                        _("Symbol '%s' denotes a statement, not a variable") %
                        expr, expr, ())

                varlist.append(var)
                vix = (Ghilbert.VARIX, var[1], len(vardict))
                vardict[var[2]] = vix
            return vix

        try:
            termid = self.terms[expr[0]]
        except IndexError:
            raise GhCmdExprError(_("Expected term, got ()"), expr, ())
        except KeyError:
            raise GhCmdExprError(_("Unknown term name '%s'") % expr[0],
                                 expr, (0,))
        except TypeError:
            raise GhCmdExprError(_("Expected term name, got term expression"),
                                 expr, (0,))
        # (Ghilbert.TERMID, kind-tuple, term-name, def, arg-kind, ...)
        if len(expr) != len(termid) - 3:
            raise GhCmdExprError(
                _("Wrong number of arguments for '%s'") % termid[2], expr, ())
        # termid[1] here is separated out here just to make the [1] element
        # of all expressions be the kind tuple, for easier kind comparison.
        # May revisit.
        le = [Ghilbert.TERM, termid[1], termid]
        for j in xrange(1, len(expr)):
            try:
                e = self.expr_convert(expr[j], vardict, varlist, maketuple)
            except GhCmdExprError, x:
                (why,) = x.args
                raise GhCmdExprError(why, expr, (j,) + x.path)
            if e[1][2] is not termid[j + 3][2]:
                raise GhCmdExprError(_("Expected expression of kind '%s'") %
                                     termid[j + 3][1], expr, (j,))
            le.append(e)
        if maketuple:
            return tuple(le)
        return le

    def add_def(self, sexpr):
        """Add a definition."""
        # def ((NAME ARGVAR ...) EXPR [(STEP ... EQUIV)])
        errpath = None
        if len(sexpr) < 2:
            errpath = ()
        else:
            lhs = sexpr[0]
            if not isinstance(lhs, list):
                errpath = (0,)
            else:
                for j in xrange(len(lhs)):
                    if not isinstance(lhs[j], basestring):
                        errpath = (0, j)
                        break
        if errpath != None:
            raise GhCmdExprError(
               _("Expected 'def ((NAME ARGVAR ...) EXPR [(STEP ... EQUIV)])'"),
               sexpr, errpath)
        tname = lhs[0]
        if self.terms.has_key(tname):
            raise GhCmdExprError(_("A term named '%s' already exists") % tname,
                                 sexpr, (0,0))
        vdict = {}
        vlist = []
        termid = [Ghilbert.TERMID, None, tname, None]
        try:
            for j in xrange(1, len(lhs)):
                vname = lhs[j]
                var = self.syms[vname]
                if var[0] != Ghilbert.VAR:
                    raise GhError(_("'%s' is not a variable symbol") % vname)
                if vdict.has_key(var[2]):
                    raise GhError(
                        _("Repeated definition variable '%s'") % var[2])
                vdict[var[2]] = (Ghilbert.VARIX, var[1], j - 1)
                termid.append(var[1])   # The argument kind
                vlist.append(var)
        except KeyError:
            raise GhCmdExprError(_("Unknown variable '%s'") % vname,
                                 sexpr, (0, j))
        except GhError, x:
            (why,) = x.args
            raise GhCmdExprError(why, sexpr, (0, j))

        nvars = len(vlist)
        try:
            rhs = self.expr_convert(sexpr[1], vdict, vlist, True)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, sexpr, (1,) + x.path)
            
        ndummies = len(vlist) - nvars
        proof = None
        if ndummies:
            if len(sexpr) != 3:
                errpath = ()
            elif not isinstance(sexpr[2], list):
                errpath = (2,)
            if errpath != None:
                raise GhCmdExprError(_("Definition has dummy variables, "
                  "expected 'def ((NAME ARGVAR ...) EXPR (STEP ... EQUIV))'"),
                                     sexpr, errpath)
            
            pip = Pip()
            self.pip = pip  # remember it for error display
            # use the same variable mappings in the proof...
            pip.vlist = vlist
            pip.vdict = vdict
            try:
                for j in xrange(len(sexpr[2])):
                    step = sexpr[2][j]
                    self.thm_step(pip, step)
            except GhError, x:
                (why,) = x.args
                raise GhCmdExprError(why, expr, (2, j))

            # End of proof checking.
            if len(pip.wvs):
                raise VerifyError(_("Wild variable substitutions remain at "
                                    "end of definition justification"))
            if not pip.equivFlag:
                raise VerifyError(_("Definition justification proof does "
                                    "not end with an equivalence"))
            exprs = pip.exprs
            if len(exprs) != 2:
                raise VerifyError(
                   _("Expected exactly two expressions on proof stack for "
                     "equivalence. Found %d.") % len(exprs))
            # The first expression on the proof stack must match the
            # definition RHS exactly. The second expression must be the
            # same as the first, except that all dummy variables must be
            # replaced with new variables (of the same kinds) not occurring
            # in the first expression.
            if not match_exact(exprs[0], rhs):
                raise VerifyError(
                    _("Expression 0 does not match definition RHS"))
            vmap = [None] * ndummies
            image = set()
            match_defjust(exprs[1], rhs, nvars, ndummies, vmap, image)

            # Now we must check that the only distinct variables conditions
            # required are those that involve either the original or new
            # set of dummy variables. No distinct variable conditions are
            # allowed between the explicit argument variables of the
            # definition.

            for v, w in pip.dvreqs:
                if v < nvars and w < nvars:
                    raise VerifyError(
                        _("Definition arguments '%(arg1)s' and '%(arg2)s' "
                          "are required to be distinct by the "
                          "definition justification proof") %
                        {"arg1" : vlist[v][2], "arg2" : vlist[w][2]})
            proof = tuple(pip.proof)
        else:
            if len(sexpr) != 2:
                raise GhCmdExprError(
                    _("Definition has no dummy variables, "
                      "expected 'def ((NAME ARGVAR ...) EXPR)'"),
                    sexpr, ())

        termid[1] = rhs[1]      # the term kind
        termid[3] = (rhs, ndummies, tuple(vlist), proof)

        self.terms[tname] = termid
        self.push_history(termid)
        

    def dv_canonical(self, cexpr, vdict):
        """Parse and canonicalize a distinct variables list for stmt or thm

        cexpr is the s-expression for the 'thm' or 'stmt' or 'equiv' command
        vdict is a dictionary of variable names occurring in the hypotheses
          or conclusions of the thm or stmt command; vdict maps these names
          to (Ghilbert.VARIX, k, ix) tuples.

        Returns a set to which this function adds distinct variable pairs
          (tuples). Only relevant variables (occurring in the hypotheses
          or conclusions) are added, and these are represented by the
          corresponding variable index (ix), so the returned set is a set
          of pairs of integer indices.
        """

        # for both stmt and thm, the distinct variables list is cexpr[1]
        i = 0
        dvs = None
        try:
            for dvc in cexpr[1]:
                if not isinstance(dvc, list):
                    raise GhCmdExprError(
                        _("Distinct Variables Condition must be list"),
                        cexpr, (1, i))
                if dvs is None:
                    dvs = set()
                rec = [None] * len(dvc)
                for j in xrange(len(dvc)):
                    vname = dvc[j]
                    # This line may generate TypeError
                    vix = vdict.get(vname)
                    if vix is None:
                        # This line may generate KeyError:
                        sym = self.syms[vname]
                        if sym[0] != Ghilbert.VAR:
                            raise GhCmdExprError(
                                _("'%s' is a statement, not a variable") %
                                vname, cexpr, (1, i, j))
                        v = sym[2]  # == vname
                    else:
                        v = vix[2] # The index
                    rec[j] = v
                    for k in xrange(0, j):
                        w = rec[k]
                        if v == w:
                            raise GhCmdExprError(
                                _("Variable '%s' occurs twice in "
                                  "distinct variables condition") % vname,
                                cexpr, (1, i, j))
                        # ignore if either member is irrelevant
                        if vix is None or isinstance(w, basestring):
                            continue
                        if w < v:
                            v, w = w, v
                        dvs.add((v, w))
                i = i + 1

        except TypeError:
            raise GhCmdExprError(_("Expected (VAR ...)"), cexpr, (1, i, j))
        except KeyError:
            raise GhCmdExprError(_("Unknown variable '%s'") % v1,
                                  cexpr, (1, i, j))
        return dvs

    def add_stmt(self, expr, equiv):
        """Add a statement or equivalence

        stmt (name ((x ...) ...) (hyp ...) (conc ...))
        equiv (name ((x ...) ...) (hyp ...) (expr1 expr2))

        Multiple conclusion syntax is required.
        The 'equiv' argument is False when adding a statement and
        True when adding an equivalence.
        
        Presently, equivalences are allowed and used only for justifying
        the well-definedness of definitions having dummy variables on the
        RHS (the definiens). expr1 and expr2 must be expressions of the
        same kind. The equivalence means that provided the hypotheses
        and the distinct variables conditions are met, the substituted
        values of expr1 and expr2 are equivalent to each other, meaning
        that any occurrence of one of them anywhere in a larger expression
        may be substituted by the other one, regardless of context, without
        changing the 'meaning' in any way significant to the theory.
        As of now, such 'arbitrarily deep' substitutions are only made
        use of during definition expansion at the end of a proof.
        Example: equiv (wff_equiv () ((-> ph ps) (-> ps ph)) (ph ps))

        """
        errpath = None
        if not isinstance(expr, list) or len(expr) != 4:
            errpath = ()
        elif not isinstance(expr[0], basestring):
            errpath = (0,)
        elif not isinstance(expr[1], list):
            errpath = (1,)
        elif not isinstance(expr[2], list):
            errpath = (2,)
        # for now, don't allow single-conclusion syntax, as the determination
        # is not foolproof; a multi-conclusion theorem whose first conclusion
        # is a variable with a name equal to a term identifier may look like
        # a term with that identifier (although the proof isn't likely to work)
        elif not isinstance(expr[3], list):
            errpath = (3,)

        if errpath != None:
            raise GhCmdExprError(
                 _("Expected "
                   "'%s (NAME ((DV1 DV2 ...) ...) (HYP ...) (CONC ...))'") %
                   ('stmt', 'equiv')[equiv], expr, errpath)
        namepf = self.prefix + expr[0]
        if self.parent.syms.has_key(namepf):
            raise GhCmdExprError(_("(Prefixed) symbol '%s' already exists"),
                                 namepf, (0,))

        vdict = {}
        vlist = []
        hyps_concs = []
        try:
            for hyp in expr[2]:
                ihyp = self.expr_convert(hyp, vdict, vlist)
                hyps_concs.append(ihyp)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr, (2, len(hyps_concs)) + x.path)

        nhyps = len(hyps_concs)
        nhv = len(vlist)
        try:
            for conc in expr[3]:
                iconc = self.expr_convert(conc, vdict, vlist)
                hyps_concs.append(iconc)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr,
                                 (3, len(hyps_concs) - nhyps) + x.path)
        tag = Ghilbert.STMT
        if equiv:
            nconcs = len(hyps_concs) - nhyps
            if (nconcs != 2 or 
                hyps_concs[nhyps][1][2] != hyps_concs[nhyps+1][1][2]):
                raise GhCmdExprError(
                    _("Equivalences must have exactly two 'conclusions'"),
                    expr, (3,))
            tag = Ghilbert.EQUIV

        dvs = self.dv_canonical(expr, vdict)

        # Note, a statement with more (or less) than one conclusion
        # doesn't have a single kind, but a possibly empty tuple of kinds
        t = (tag, namepf, nhyps, tuple(hyps_concs),
             tuple(vlist), len(vlist) - nhv, dvs)
        self.parent.syms[namepf] = t
        self.push_history(t)

    def thm_step(self, pip, step):
        """Apply one step in the proof of a theorem."""

        if pip.equivFlag:
            raise VerifyError(
                _("An equivalence was used. No more proof steps allowed."))
        if isinstance(step, list):
            # step represents a term expression which is to be substituted
            # for one of the applied statement's wild variables.
            # Do we want to add variables in step (that haven't already been
            # added from the hypothesis/conclusion processing) to the
            # theorem's variable list? Perhaps not...  But if such
            # 'proof dummy' variables occur in the remnant, there may be
            # issues with definition dummies/distinct variables conditions
            # in the end-of-proof processing. (There aren't, really.)
            #
            # Convert this expression to tuple form now that we save proofs.
            e = self.expr_convert(step, pip.vdict, pip.vlist, True)
            pip.wvs.append(e)
            pip.proof.append(e)
            return

        nwv = len(pip.wvs)
        p = pip.hyps.get(step)
        if p != None:
            nam, e = p
            if nwv != 0:
                raise VerifyError(
                    _("Hypotheses may be pushed only when the "
                      "wild variable substitution stack is empty"))
            pip.exprs.append(e)
            pip.proof.append(nam)
            return

        try:
            e = self.syms[step]
        except KeyError:
            raise VerifyError(_("Unrecognized proof step '%s'") % step)

        if e[0] == Ghilbert.VAR:
            try:
                vix = pip.vdict[e[2]]
            except:
                vix = (Ghilbert.VARIX, e[1], len(pip.vdict))
                pip.vdict[e[2]] = vix
                pip.vlist.append(e)
            pip.wvs.append(vix)
            pip.proof.append(e)
            return

        # Apply an existing statement
        # e is (Ghilbert.[STMT|EQUIV], namepf, nhyps, hyps_concs,
        #       vlist, nwv, dvs, ...)
        nhyps = e[2]
        pip_exprs = pip.exprs
        nstack = len(pip_exprs)
        if nstack < nhyps:
            raise VerifyError(
                _("There are too few hypotheses on the proof stack"))
        # e[5] is the number of wild variable substitutions that the statement
        # requires.
        if e[5] != nwv:
            raise VerifyError(_("%(stmt)s requires exactly %(num)d wild "
                                "variable substitutions") %
                              {"stmt" : e[1], "num" : e[5]})

        j = nstack - nhyps
        hyps_concs = e[3]  # tuple of hypotheses & conclusions for statement
        vlist = e[4]       # tuple of variables used in hyps_concs
        vmap = [None] * len(vlist)
        for i in xrange(nhyps):
            hyp_match(pip_exprs[j], hyps_concs[i], vmap)
            j = j + 1

        j = len(vlist) - nwv  # index of first wild variable of applied stat.
        pip_wvs = pip.wvs
        for i in xrange(nwv):
            x = pip_wvs[i]
            v = vlist[j]
            # check for kind equivalence
            if x[1][2] is not v[1][2]:
                raise VerifyError(
                    _("Value substituted for wild variable %(var)s "
                      "of %(stmt)s is %(kind1)s, but it should be "
                      "%(kind2)s") %
                    {"var" : v[2], "stmt" : e[1], "kind1" : x[1][1],
                     "kind2" : v[1][1]})
            vmap[j] = x
            j = j + 1

        # Disjoint variable handling
        # Only worry about variables that occur in the hypotheses or
        # conclusions of the theorem.
        # But suppose the statement we are applying requires the variables
        # in the substitutions for two of its variables to be disjoint,
        # but they are not, but the intersection involves only variables
        # that are not in the hypotheses or conclusions of the theorem
        # being proven. We still want to prevent such a use.
        relvars = pip.nvars
        dvs = e[6]
        if dvs != None:
            dvarsmap = [None] * len(vlist)
            for v, w in dvs:
                vset1 = dvarsmap[v]
                if vset1 is None:
                    vset1 = set()
                    relvarsof(vmap[v], vset1)
                    dvarsmap[v] = vset1
                vset2 = dvarsmap[w]
                if vset2 is None:
                    vset2 = set()
                    relvarsof(vmap[w], vset2)
                    dvarsmap[w] = vset2
                for u1 in vset1:
                    for u2 in vset2:
                        if u1 == u2:
                            u = pip.vlist[u1][2]
                            raise VerifyError(
                                _("Disjoint variable violation for "
                                  "(%(var1)s, %(var2)s) applying %(stmt)s :"
                                  "substitutions for both contain %(var3)s")
                                % {"var1" : vlist[v][2], "var2" : vlist[w][2],
                                   "stmt" : e[1], "var3" : u})
                        if u1 >= relvars or u2 >= relvars:
                            continue
                        if u1 < u2:
                            pip.dvreqs.add((u1, u2))
                        else:
                            pip.dvreqs.add((u2, u1))

        # Everything's OK with this application; wipe out the used hypotheses
        # and the wild variable substitutions in the Pip, and add the
        # substituted conclusions
        pip.wvs = []
        pip.exprs[(nstack - nhyps):] = []

        if e[0] is Ghilbert.EQUIV:
            pip.equivFlag = True

        for c in hyps_concs[nhyps:]:
            x = substitute(c, vmap)
            pip.exprs.append(x)
        pip.proof.append(e)


    def add_thm(self, expr):
        """Initial processing for a thm statement

        expr is the thm's s-expression
        pip must be a newly created Pip.
        """

        pip = Pip()
        self.pip = pip  # remember it for error output

        # Expect (THM_NAME ((VAR ...) ...)
        #                  ((HYP_NAME HYP_EXPR) ...)
        #                  (CONC ...)
        #                  (STEP ...))
        errpath = None
        if not isinstance(expr, list) or len(expr) != 5:
            errpath = ()
        elif not isinstance(expr[0], basestring):
            errpath = (0,)
        elif not isinstance(expr[1], list):
            errpath = (1,)
        elif not isinstance(expr[2], list):
            errpath = (2,)
        # for now, don't allow single-conclusion syntax, as the determination
        # is not foolproof; a multi-conclusion theorem whose first conclusion
        # is a variable with a name equal to a term identifier may look like
        # a term with that identifier (although the proof isn't likely to work)
        elif not isinstance(expr[3], list):
            errpath = (3,)
        elif not isinstance(expr[4], list):
            errpath = (4,)

        if errpath != None:
            raise GhCmdExprError(_("Expected "
                                   "'thm (NAME ((VAR ...) ...) "
                                              "((HYPNAME HYP) ...) "
                                              "(CONC ...) (STEP ...))'"),
                                 expr, errpath)

        if self.syms.has_key(expr[0]):
            raise GhCmdExprError(_("Symbol '%s' already exists") % expr[0],
                                 expr, (0,))
        pip_vdict = pip.vdict
        pip_vlist = pip.vlist
        hyps_concs = pip.hyps_concs
        hypnames = []
        try:
            for hp in expr[2]:
                if not isinstance(hp, list) or len(hp) != 2 or \
                       not isinstance(hp[0], basestring):
                    raise GhCmdExprError(
                        _("Expected hypothesis form '(HYP_NAME HYP_EXPR)'"),
                        expr, ())
                hypnam = hp[0]
                if pip.hyps.has_key(hypnam):
                    raise GhCmdExprError(
                        _("Repeated hypothesis name '%s'") % hypnam,
                        expr, (0,))
                try:
                    e = self.expr_convert(hp[1], pip_vdict, pip_vlist)
                except GhCmdExprError, x:
                    (why,) = x.args
                    raise GhCmdExprError(why, None, (1,) + x.path)
                pip.hyps[hypnam] = (hypnam, e)
                hypnames.append(hypnam)
                hyps_concs.append(e)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr, (2, len(hyps_concs)) + x.path)

        nhyps = len(hyps_concs)
        nhypvars = len(pip_vlist)

        try:
            for conc in expr[3]:
                e = self.expr_convert(conc, pip_vdict, pip_vlist)
                hyps_concs.append(e)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr,
                                 (3, len(hyps_concs) - nhyps) + x.path)

        nconcs = len(hyps_concs) - nhyps
        pip.nvars = len(pip_vlist)
        pip.nwv = pip.nvars - nhypvars

        # Note, by the end of the proof, we may have more relevant distinct
        # variable pairs that we need to add.
        pip.dvs = self.dv_canonical(expr, pip_vdict)

        try:
            for j in xrange(len(expr[4])):
                step = expr[4][j]
                self.thm_step(pip, step)
        except GhError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr, (4, j))

        # End of proof checking.
        if len(pip.wvs):
            raise GhProofEndError(_("Wild variable substitutions remain at "
                                    "end of proof"))
        if pip.equivFlag:
            raise GhProofEndError(_("Only definition justification proofs may "
                                    "end with an equivalence step."))
        exprs = pip.exprs
        nexprs = len(exprs)
        if nexprs != nconcs:
            raise GhProofEndError(_("%(nexprs)d expressions remain on proof "
                                    "stack; %(nconcs)d expected.") %
                                  {"nexprs" : nexprs, "nconcs" : nconcs})

        for j in xrange(nexprs):
            match_expand(exprs[j], hyps_concs[nhyps + j], None)

        # Check distinct variables
        dvreqs = pip.dvreqs
        dvs = pip.dvs
        if dvs is None:
            needed = dvreqs
        else:
            needed = dvreqs - dvs

        if needed:
            raise GhCmdExprError(
                _("Missing distinct variable conditions:%s") %
                dvs_to_string(needed, pip.vlist), expr, (1,len(expr[1])))

        if dvs is not None:
            extra = dvs - dvreqs
            if extra:
                #print 'Theorem %s has unneeded distinct ' \
                #      'variables conditions:' % expr[0]
                #print dvs_to_string(extra, pip_vlist)
                pass
                #raise GhExtraDvcsError("Extra distinct variables conditions",
                #                       extra)

        # Presently we do keep the proofs, and pip_vlist is restricted
        # to only the variables that occur in the hypothesis or conclusions
        # (Ghilbert.STMT, name, nhyps, hyps_concs, vtuple, nwv, dvs,
        #  hypnames, proof, proof_dummy_vars)
        t = (Ghilbert.STMT, expr[0], nhyps, tuple(hyps_concs),
             tuple(pip_vlist[:pip.nvars]), pip.nwv, dvs, tuple(hypnames),
             tuple(pip.proof), tuple(pip_vlist[pip.nvars:]))
        self.syms[expr[0]] = t
        self.push_history(t)

    def param(self, expr):
        # param (IFACE_NAME PATH (IFACE_NAME ...) "PREFIX")
        errpath = None
        if len(expr) != 4:
            errpath = ()
        elif not isinstance(expr[0], basestring):
            errpath = (0,)
        elif not isinstance(expr[1], basestring):
            errpath = (1,)
        elif not isinstance(expr[2], list):
            errpath = (2,)
        else:
            prefix = expr[3]
            if not isinstance(prefix, basestring) or len(prefix) < 2 or \
                   prefix[0] != '"' or prefix[-1] != '"':
                errpath = (3,)
            prefix = prefix[1:-1]

        if errpath is not None:
            raise GhCmdExprError(
              _('Expected param (IFACE_NAME PATH (PARAM_IFACE ...) "PREFIX")'),
              expr, errpath)
        
        ifname = expr[0]
        if self.interfaces.has_key(ifname):
            raise GhCmdExprError(_("Interface '%s' already exists") % ifname,
                                 expr, (0,))

        # We presently ignore the PATH specified in the 'param' command.
        params = []
        try:
            for j in xrange(len(expr[2])):
                ifn = expr[2][j]
                params.append(self.interfaces[ifn])
        except KeyError:
            raise GhCmdExprError(_("Unknown interface name '%s'") % ifn,
                                 expr, (2,j))
        except TypeError:
            raise GhCmdExprError(_("Interface parameter must be an "
                                   "interface identifier"), expr, (2,j))
        
        try:
            n = self.params_used
            iface = self.params[n]
        except IndexError:
            raise GhCmdExprError(_("There are more 'param' commands in the "
                                   "interface file than interface parameters "
                                   "passed in the 'import' command"), expr, ())

        #
        # From the parameter interface we need to incorporate all
        # kinds and terms that the interface provided itself.
        # Variables, statements, and equivalences from the parameter
        # interface are not incorporated. kindbinds and termbinds
        # done in the parameter interface will still have effect
        # on the kinds and terms (all present in the proof file context),
        # so need not be explicitly handled.
        #
        for k, v in iface.mykinds.iteritems():
            pk = prefix + k
            if self.kinds.has_key(pk):
                raise VerifyError(
                    _("Kind '%(kind)s' provided by '%(iface)s' is already "
                      "visible") % {"kind" : pk, "iface" : ifname})
            self.kinds[pk] = v

        for t, v in iface.myterms.iteritems():
            pt = prefix + t
            if self.terms.has_key(pt):
                raise VerifyError(
                    _("Term '%(term)s' provided by '%(iface)s' is already "
                      "visible") % {"term" : pt, "iface" : ifname})
            self.terms[pt] = v

        self.interfaces[ifname] = iface
        self.params_used = n + 1


    def gh_import(self, expr):
        # import (IFACE_NAME PATH (IFACE_NAME ...) "PREFIX")
        errpath = None
        if len(expr) != 4:
            errpath = ()
        elif not isinstance(expr[0], basestring):
            errpath = (0,)
        elif not isinstance(expr[1], basestring):
            errpath = (1,)
        elif not isinstance(expr[2], list):
            errpath = (2,)
        else:
            prefix = expr[3]
            if not isinstance(prefix, basestring) or len(prefix) < 2 or \
                   prefix[0] != '"' or prefix[-1] != '"':
                errpath = (3,)
            prefix = prefix[1:-1]

        if errpath is not None:
            raise GhCmdExprError(
                _('Expected import (IFACE_NAME PATH (PARAM_IFACE ...) "PREFIX")'),
                expr, errpath)

        ifname = expr[0]
        if self.interfaces.has_key(ifname):
            raise GhCmdExprError(_("Interface '%s' already exists") % ifname,
                                 expr, (0,))

        if self.verbosity:
            print _('Importing %s') % ifname

        ifs = expr[2]
        nifs = len (ifs)
        params = [None] * nifs
        try:
            for j in xrange(nifs):
                params[j] = self.interfaces[ifs[j]]
        except KeyError:
            raise GhCmdExprError(_("Unknown interface name '%s'") % ifs[j],
                                 expr, (2,j))
        except TypeError:
            raise GhCmdExprError(_("Interface parameter must be an "
                                   "interface identifier"), expr, (2,j))

        try:
            (istream, curdir) = get_stream(expr[1], self.curdir,
                                           self.fpath, self.log)
        except GhError:
            raise GhCmdExprError(_("Cannot open interface file at '%s'") %
                                 expr[1], (1,))
        
        try:
            child = Ghilbert(self.log)
            child.parent = self
            child.prefix = prefix
            child.params = params
            child.ifname = ifname
            child.curdir = curdir
            child.file = expr[1]

            scanner = StreamScanner(istream)
            while (1):
                word = scanner.get_token()
                if word is None:
                    break
                if word == '(' or word == ')':
                    raise VerifyError(
                        _("Expected command word, got '%(char)s' "
                          "at line '%(line)d'") %
                        {"char" : word, "line" : scanner.lineno})
                sexpr = read_sexp(scanner)
                if not isinstance(sexpr, list):
                    raise VerifyError(
                        _("Expected s-expression (...) command "
                          "argument at line %d") % scanner.lineno)
                if word == 'stmt':
                    child.add_stmt(sexpr, False)
                elif word == 'term':
                    child.add_term(sexpr)
                elif word == 'var':
                    child.add_vars(sexpr)
                elif word == 'kind':
                    child.add_kind(sexpr)
                elif word == 'kindbind':
                    child.kind_bind_if(sexpr)
                elif word == 'equiv':
                    child.add_stmt(sexpr, True)
                elif word == 'param':
                    child.param(sexpr)
                elif word in ['thm', 'def', 'import', 'export']:
                    raise VerifyError(
                        _("Proof file command '%(cmd)s' encountered "
                          "in interface file at line %(line)d") %
                        {"cmd" : word, "line" : scanner.lineno})
                else:
                    child.log.warning(
                        _("Skipping unknown command '%(cmd)s' "
                          "at line '%(line)d'") %
                        {"cmd" : word, "line" : scanner.lineno})

            if child.params_used != len(params):
                raise VerifyError(
                    _("'import' provided %(num1)d parameters, but only "
                      "%(num2)d were used") %
                    {"num1" : len(params), "num2" : child.params_used})

        except GhError, x:
            msg = _("Error in import: %(file)s line %(line)d") % \
                  {"file" : child.file, "line" : scanner.lineno}
            print >> sys.stderr, msg
            # Forget kinds, etc., introduced by the import.
            child.hist_revert(0)
            (why,) = x.args
            raise GhError (msg + '\n' + why)
        finally:
            istream.close()

        # We should no longer need child.terms and child.kinds. Free
        # them to save memory. (If this interface is used later via 'param',
        # .myterms and .mykinds will still be available.)
        child.terms = None
        child.kinds = None

        self.interfaces[ifname] = child
        self.push_history((Ghilbert.IMPORT, child, expr[1]))

    def xprt_expr_compare(self, expr, proto, vdict, vlist):
        """Compare an s-expr from exported stmt with corresp. proof ctx expr

        This function compares the s-expression <expr> with the prototype
        expression <proto> which is the corresponding hypothesis or conclusion
        of an existing statement or equivalence in the proof context.
        vdict is a dictionary mapping variable names to Ghilbert.VARIX tuples;
        vlist is a list mapping variable indices to Ghilbert.VAR tuples.
        vdict and vlist are grown by this routine, which checks for a
        bijective map from the (indexed) variables in the hypotheses and
        conclusions of the statement being exported, and those variables
        provided by the corresponding s-expressions in the stmt or equiv
        command from the export file.
        """
        if proto[0] is Ghilbert.VARIX:
            var = vlist[proto[2]]
            if var != None:
                if expr != var[2]:
                    raise GhCmdExprError(_("Expression mismatch"), expr, ())
                return
            try:
                var = self.syms[expr]
            except (TypeError, KeyError):
                raise GhCmdExprError(_("Expression mismatch"), expr, ())
            if var[0] != Ghilbert.VAR:
                raise GhCmdExprError(
                    _("Expected variable name, got other symbol"),  expr, ())
            if var[1][2] != proto[1][2]:
                raise GhCmdExprError(_("Kind mismatch"), expr, ())
            if vdict.has_key(var[2]):
                # make sure the map is 1-1
                raise GhCmdExprError(_("Expression mismatch"), expr, ())
            vlist[proto[2]] = var
            vdict[var[2]] = proto
            return

        # proto is (Ghilbert.TERM, resultKind, termid, termargexp, ...)
        nargs = len(proto) - 3
        if (not isinstance(expr, list) or len(expr) != nargs + 1):
            raise GhCmdExprError(_("Expression mismatch"), expr, ())

        # Have to make sure that the term used is actually available in
        # the export context at this point.
        try:
            t = self.terms[expr[0]]
        except (TypeError, KeyError):
            raise GhCmdExprError(_("Unknown term identifier '%s'") % expr[0],
                                 expr, (0,))

        if t is not proto[2]:
            raise GhCmdExprError(_("Expression mismatch"), expr, ())

        try:
            for j in xrange(nargs):
                self.xprt_expr_compare(expr[j+1], proto[j+3], vdict, vlist)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr, (j+1,) + x.path)

    def xprt_stmt(self, expr, equiv):
        """Add a statement or equivalence to an export interface

        stmt (name ((x ...) ...) (hyp ...) (conc ...))
        equiv (name ((x ...) ...) (hyp ...) (expr1 expr2))

        Multiple conclusion syntax is required.
        The 'equiv' argument is False when adding a statement and
        True when adding an equivalence.
        
        Presently, equivalences are allowed and used only for justifying
        the well-definedness of definitions having dummy variables on the
        RHS (the definiens). expr1 and expr2 must be expressions of the
        same kind. The equivalence means that provided the hypotheses
        and the distinct variables conditions are met, the substituted
        values of expr1 and expr2 are equivalent to each other, meaning
        that any occurrence of one of them anywhere in a larger expression
        may be substituted by the other one, regardless of context, without
        changing the 'meaning' in any way significant to the theory.
        As of now, such 'arbitrarily deep' substitutions are only made
        use of during definition expansion at the end of a proof.
        Example: equiv (wff_equiv () ((-> ph ps) (-> ps ph)) (ph ps))

        """
        errpath = None
        if not isinstance(expr, list) or len(expr) != 4:
            errpath = ()
        elif not isinstance(expr[0], basestring):
            errpath = (0,)
        elif not isinstance(expr[1], list):
            errpath = (1,)
        elif not isinstance(expr[2], list):
            errpath = (2,)
        # for now, don't allow single-conclusion syntax, as the determination
        # is not foolproof; a multi-conclusion theorem whose first conclusion
        # is a variable with a name equal to a term identifier may look like
        # a term with that identifier (although the proof isn't likely to work)
        elif not isinstance(expr[3], list):
            errpath = (3,)

        if errpath != None:
            raise GhCmdExprError(
                _("Expected "
                  "'%s (NAME ((DV1 DV2 ...) ...) (HYP ...) (CONC ...))'") %
                ('stmt', 'equiv')[equiv], expr, errpath)

        # Check for conflicts with existing variable names, doubly exported
        # statements, etc.
        if self.syms.has_key(expr[0]):
            raise GhCmdExprError(_("Symbol '%s' already exists in export") %
                                 expr[0], expr, (0,))

        namepf = self.prefix + expr[0]
        try:
            stmt = self.parent.syms[namepf]
        except KeyError:
            # TRANS: The first %s will be either 'stmt' or 'equiv'.
            raise GhCmdExprError(_("%(keyword)s '%(name)s' does not exist") %
                                 {"keyword" : ('stmt', 'equiv')[equiv],
                                  "name" : namepf},
                                 expr, (0,))

        # Note, stmt might be a stmt, a var, or an equiv.
        if equiv:
            if stmt[0] != Ghilbert.EQUIV:
                raise GhCmdExprError(_("'%s' is not an 'equiv'") % namepf,
                                     expr, (0,))
        else:
            if stmt[0] != Ghilbert.STMT:
                raise GhCmdExprError(_("'%s' is not a 'stmt'") % namepf,
                                     expr, (0,))

        # (Ghilbert.[STMT|EQUIV], name, nhyps, hyps_concs, vtuple, nwv, dvs,
        #  [hypnames])

        nhyps = stmt[2]
        hyps = expr[2]
        if len(hyps) != nhyps:
            raise GhCmdExprError(
                _("'%(stmt)s' has %(nhyps)d hypotheses but the export "
                  "interface file provided %(num)d") %
                {"stmt" : namepf, "nhyps" : nhyps, "num" : len(hyps)},
                expr, (2,))

        hyps_concs = stmt[3]
        concs = expr[3]
        nconcs = len(hyps_concs) - nhyps
        if len(concs) != nconcs:
            raise GhCmdExprError(
                _("'%(stmt)s' has %(nconcs)d conclusions but the export "
                  "interface file provided %(num)d") %
                {"stmt" : namepf, "nconcs" : nconcs, "num" : len(concs)},
                expr, (3,))
            
        vdict = {}
        vlist = [None] * len(stmt[4])
        try:
            for j in xrange(nhyps):
                self.xprt_expr_compare(hyps[j], hyps_concs[j], vdict, vlist)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr, (2, j) + x.path)

        nhv = len(vlist)
        try:
            for j in xrange(nconcs):
                self.xprt_expr_compare(concs[j], hyps_concs[j + nhyps],
                                       vdict, vlist)
        except GhCmdExprError, x:
            (why,) = x.args
            raise GhCmdExprError(why, expr, (3, j) + x.path)

        dvs = self.dv_canonical(expr, vdict)

        # stmt[6] is the dvs from the existing statement/equivalence
        dvsproto = stmt[6]
        missing = dvsproto
        extra = dvs
        if dvsproto is not None and dvs is not None:
            missing = dvsproto - dvs
            extra = dvs - dvsproto
        if missing:
            raise VerifyError(
                _("Missing distinct variables pairs (%s)") %
                dvs_to_string(missing, vlist))
        if extra:
            print _("Warning: extra distinct variables pairs (%s)") % \
                  dvs_to_string(extra, vlist)
            pass

        # During the export, save the unprefixed statement name to
        # detect conflicts later
        self.syms[expr[0]] = stmt

    def xprt_term(self, expr):
        """export 'term' command handling

        term (KIND (TERMID KIND ...))
        """
        errpath = None
        try:
            kid, texp = expr
            if not isinstance(kid, basestring):
                errpath = (0,)
            elif (not isinstance(texp, list) or len(texp) < 1 or
                  not isinstance(texp[0], basestring)):
                errpath = (1,)
        except ValueError:
            errpath = ()

        if errpath is not None:
            raise GhCmdExprError(
                _('Expected term (KIND (TERMID KIND ...)'), expr, errpath)

        prefix = self.prefix
        parent = self.parent
        pkid = prefix + kid
        try:
            kr = parent.kinds[pkid]
        except KeyError:
            raise GhCmdExprError(_("Unknown kind '%s'") % pkid, expr, (0,))

        tid = texp[0]
        ptid = prefix + tid

        if self.terms.has_key(tid):
            raise GhCmdExprError(_("Term '%s' already exists in export") % tid,
                                 expr, (1, 0))
        try:
            t = parent.terms[ptid]
        except KeyError:
            raise GhCmdExprError(_("Unknown term '%s'") % ptid, expr, (1, 0))

        # t is (Ghilbert.TERMID, kind-tuple, term-name, def, arg-kind, ...)
        nargs = len(t) - 4

        if len(texp) != nargs + 1:
            raise GhCmdExprError(_("Term arity mismatch for '%s'") % ptid,
                                 expr, (1,))

        # There's an issue regarding the effect of kindbinds in an exported
        # interface file.  The exported file may itself give no indication
        # that two kinds that it uses were equivalent in the proof file,
        # yet may depend upon this equvalence in its terms and statements.
        # If the exported file is subsequently imported by a different proof
        # file, that import might give itself or lead to kind mismatch errors.
        # Perhaps there should be a test that an export file either declares
        # itself, or obtains from its parameter interfaces, all kindbinds
        # necessary to reflect the equivalences in effect in the proof context
        # between the kinds that the export file exports.
        # Ignore this for now.

        # Check that all the argument kinds are correct.
        try:
            for j in xrange(nargs):
                pakn = prefix + texp[j+1]
                ak = parent.kinds[pakn]
                k = t[j+4]
                if ak[2] != k[2]:
                    raise GhError(_("Argument kind mismatch"))
        except (TypeError, KeyError, GhError):
            raise GhCmdExprError(_("Term argument kind incorrect for '%s'") %
                                 ptid, expr, (1, j+1))
        self.myterms[tid] = t
        self.terms[tid] = t

    def xprt_kind(self, expr):
        try:
            (kindn,) = expr
            pkindn = self.prefix + kindn
            if self.kinds.has_key(kindn):
                raise GhCmdExprError(_("Kind '%s' already exists in export") %
                                     kindn, expr, (0,))
            k = self.parent.kinds[pkindn]
        except ValueError:
            raise GhCmdExprError(_("Expected 'kind (KIND)'"), expr, ())
        except KeyError:
            raise GhCmdExprError(_("Unknown kind '%s'") % pkindn, expr, (0,))

        self.kinds[kindn] = k   # for add_vars()
        self.mykinds[kindn] = k

    def kind_bind_ef(self, expr):
        prefix = self.prefix
        parent = self.parent
        try:
            (k1n, k2n) = expr
        except ValueError:
            raise GhCmdExprError(_("Expected 'kindbind (KIND1 KIND2)'"),
                                 expr, ())

        try:
            kn = k1n
            j = 0
            k1 = self.kinds[kn]
            kn = k2n
            j = 1
            k2 = self.kinds[kn]
        except KeyError:
            raise GhCmdExprError(
                _("Kind '%s' not available in export context") %
                kn, expr, (j,))
        except TypeError:
            raise GhCmdExprError(_("Expected 'kindbind (KIND1 KIND2)'"),
                                 expr, (j,))

        if k1[2] != k2[2]:
            raise VerifyError(
                _("Kinds '%(kind1)s' and '%(kind2)s' are not equivalent") %
                {"kind1" : k1n, "kind2" : k2n})
        # Indicate that the two kinds are equivalent not just in the proof
        # file context, but in the export context also? If we do that
        # we could check that kindbinds done in the export file occur early
        # enough that kind equivalence uses in the export file are valid
        # when they occur... ?

    def gh_export(self, expr):
        # export (IFACE_NAME PATH (IFACE_NAME ...) "PREFIX")
        errpath = None
        try:
            ifname, path, plist, prefix = expr
            if not isinstance(ifname, basestring):
                errpath = (0,)
            elif not isinstance(path, basestring):
                errpath = (1,)
            elif not isinstance(plist, list):
                errpath = (2,)
            elif (not isinstance(prefix, basestring) or len(prefix) < 2 or
                  prefix[0] != '"' or prefix[-1] != '"'):
                errpath = (3,)
        except ValueError:
            errpath = ()

        if errpath is not None:
            raise GhCmdExprError(
                _('Expected import (IFACE_NAME PATH (PARAM_IFACE ...) "PREFIX")'),
                expr, errpath)

        prefix = prefix[1:-1]

        if self.interfaces.has_key(ifname):
            raise GhCmdExprError(_("Interface '%s' already exists") % ifname,
                                 expr, (0,))

        if self.verbosity:
            print _('Exporting %s') % ifname

        np = len(plist)
        params = [None] * np
        try:
            for j in xrange(np):
                params[j] = self.interfaces[plist[j]]
        except KeyError:
            raise GhCmdExprError(_("Unknown interface name '%s'") % plist[j],
                                 expr, (2,j))
        except TypeError:
            raise GhCmdExprError(_("Interface parameter must be an "
                                   "interface identifier"), expr, (2,j))

        try:
            (istream, curdir) = get_stream(path, self.curdir,
                                           self.fpath, self.log)
        except GhError:
            raise GhCmdExprError(_("Cannot open interface file at '%s'") %
                                 path, (1,))
        
        try:
            child = Ghilbert(self.log)
            child.parent = self
            child.prefix = prefix
            child.params = params
            child.ifname = ifname
            child.curdir = curdir
            child.file = path

            scanner = StreamScanner(istream)
            while (1):
                word = scanner.get_token()
                if word is None:
                    break
                if word == '(' or word == ')':
                    raise VerifyError(
                        _("Expected command word, got '%(char)s' "
                          "at line %(line)d") %
                        {"char" : word, "line" : scanner.lineno})
                sexpr = read_sexp(scanner)
                if not isinstance(sexpr, list):
                    raise VerifyError(
                        _("Expected s-expression (...) command "
                          "argument at line %d") % scanner.lineno)
                if word == 'stmt':
                    child.xprt_stmt(sexpr, False)
                elif word == 'term':
                    child.xprt_term(sexpr)
                elif word == 'var':
                    child.add_vars(sexpr)
                elif word == 'kind':
                    child.xprt_kind(sexpr)
                elif word == 'kindbind':
                    child.kind_bind_ef(sexpr)
                elif word == 'equiv':
                    child.xprt_stmt(sexpr, True)
                elif word == 'param':
                    child.param(sexpr)
                elif word in ['thm', 'def', 'import', 'export']:
                    raise VerifyError(
                        _("Proof file command '%(cmd)s' encountered "
                          "in interface file at line %(line)d") %
                        {"cmd" : word, "line" : scanner.lineno})
                else:
                    child.log.warning(
                        _("Skipping unknown command '%(cmd)s' at "
                          "line '%(line)d'") %
                        {"cmd" : word, "line" : scanner.lineno})

            if child.params_used != len(params):
                raise VerifyError(
                    _("'export' provided %(num1)d parameters, but only "
                      "%(num2)d were used") %
                    {"num1" :  len(params), "num2" : child.params_used})

        except GhError, x:
            msg = _("Error in export: %(file)s line %(linenum)d") % \
                  {"file" : child.file, "linenum" : scanner.lineno}
            print >> sys.stderr, msg
            # Note, we don't undo history here, as we don't affect the
            # parent.
            (why,) = x.args
            raise GhError(msg + '\n' + why)
        finally:
            istream.close()

        # We should no longer need child.terms and child.kinds. Free
        # them to save memory. (If this interface is used later via 'param',
        # .myterms and .mykinds will still be available.)
        # We also don't need child.syms, since an exported interface
        # introduces no _new_ statements/equivs, we get the debug variable
        # names from the interface that originally introduced statements.
        child.terms = None
        child.kinds = None
        child.syms = None

        self.interfaces[ifname] = child
        self.push_history((Ghilbert.EXPORT, child, path))

    def read_proof_file_from_scanner(self, scanner, one_only):

        while (1):
            self.pip = None
            word = scanner.get_token()
            if word is None:
                break
            if word == '(' or word == ')':
                raise VerifyError(_("Expected command word, got '%s'") % word)
            sexpr = read_sexp(scanner)
            if not isinstance(sexpr, list):
                raise VerifyError(
                    _("Expected s-expression (...) command argument"))
            if word == 'thm':
                self.add_thm(sexpr)
            elif word == 'def':
                self.add_def(sexpr)
            elif word == 'var':
                self.add_vars(sexpr)
            elif word == 'kindbind':
                self.kind_bind_pf(sexpr)
            elif word == 'import':
                self.gh_import(sexpr)
            elif word == 'export':
                self.gh_export(sexpr)
            elif word in ['stmt', 'term', 'kind', 'param', 'equiv']:
                raise VerifyError(
                  _("Interface file command '%s' encountered in proof file") %
                  word)
            else:
                self.log.warning(_("Skipping unknown command '%s'") % word)

            scanner.set_good()
            if one_only:
                break


if __name__ == "__main__":
    i = 1
    try:
        fpath = os.environ['GHILBERT_PATH'].split(':')
    except KeyError:
        fpath = ['/']
    file = ''
    convert = False
    verbosity = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if arg[0] != '-':
            break
        if arg == '--':
            i = i + 1
            break
        if arg[:2] == '-v':
            if len(arg) > 2:
                param = arg[2:]
            else:
                i += 1
                param = sys.argv[i]
            verbosity = int(param)
        elif arg[:2] == '-c':
            convert = True
        else:
            print >> sys.stderr, 'Unknown argument:', arg
        i += 1
    if i < len(sys.argv):
        file = sys.argv[i]
        if i + 1 < len(sys.argv):
            print >> sys.stderr, 'Warning, ignoring extra arguments'

    gh = Ghilbert(logging.getLogger())
    gh.fpath = fpath
    gh.curdir = os.getcwd()
    gh.verbosity = verbosity
    if file:
        gh.file = file
        (instream, gh.curdir) = get_stream(file, gh.curdir, gh.fpath, gh.log)
    else:
        # TRANS: This is a fake filename used in some error messages when
        # the proof file is read from standard input:
        gh.file = _('<standard input>')
        instream = sys.stdin
    if convert: # convert to multi-conclusion form
        scanner = StreamScanner(instream)
        while True:
            word = scanner.get_token()
            if word == None:
                break
            arg = read_sexp(scanner)
            if word == 'stmt' or word == 'thm':
                c = arg[3]
                arg[3] = [c]
            s = sexp_to_string(arg)
            sys.stdout.write(word + ' ' + s + '\n')
    else:
        scanner = StreamScanner(instream)
        try:
            gh.read_proof_file_from_scanner(scanner, False)
        except GhCmdExprError, x:
            (why,) = x.args
            print >> sys.stderr, 'GhCmdExprError path:', x.path
            print >> sys.stderr, 'expr: ', x.expr
            print >> sys.stderr, 'at %s line %d' % (self.file, scanner.lineno)
            raise
        #print "Enter a line to continue:"
        #sys.stdin.readline()