This file is indexed.

/usr/share/minlog/src/psym.scm is in minlog 4.0.99.20100221-5.2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
; $Id: psym.scm 2359 2009-12-10 17:12:32Z schwicht $
; 5. Predicates
; =============

; To be renamed into pred.scm

; A predicate is
; - a predicate variable
; - a predicate constant
; - an inductively defined predicate constant
; Generalities for all kinds of predicates:

(define (predicate-to-arity predicate)
  (cond ((pvar-form? predicate) (pvar-to-arity predicate))
	((predconst-form? predicate) (predconst-to-arity predicate))
	((idpredconst-form? predicate) (idpredconst-to-arity predicate))
	(else (myerror "predicate-to-arity"  "predicate expected" predicate))))

; Totality matters for the abstracted variables of a cterm, because of
; the inductively defined existential quantifier.  The default is the
; use of partial variables.

(define (predicate-to-cterm predicate)
  (let* ((arity (predicate-to-arity predicate))
	 (types (arity-to-types arity))
	 (vars (map type-to-new-partial-var types))
	 (varterms (map make-term-in-var-form vars))
	 (formula (apply make-predicate-formula (cons predicate varterms))))
      (apply make-cterm (append vars (list formula)))))

(define (predicate-to-cterm-with-total-vars predicate)
  (let* ((arity (predicate-to-arity predicate))
	 (types (arity-to-types arity))
	 (vars (map type-to-new-var types))
	 (varterms (map make-term-in-var-form vars))
	 (formula (apply make-predicate-formula (cons predicate varterms))))
      (apply make-cterm (append vars (list formula)))))

(define (predicate-to-tvars pred)
  (cond ((pvar-form? pred)
	 (let* ((arity (pvar-to-arity pred))
		(types (arity-to-types arity)))
	   (apply union (map type-to-free types))))
	((predconst-form? pred)
	 (let* ((arity (predconst-to-arity pred))
		(types (arity-to-types arity)))
	   (apply union (map type-to-free types))))
	((idpredconst-form? pred)
	 (let* ((types (idpredconst-to-types pred))
		(cterms (idpredconst-to-cterms pred))
		(formulas (map cterm-to-formula cterms)))
	   (apply union (append (map type-to-free types)
				(map formula-to-tvars formulas)))))
	(else (myerror "predicate-to-tvars" "predicate expected" pred))))

(define (predicate-equal? pred1 pred2)
  (cond
   ((pvar-form? pred1)
    (and (pvar-form? pred2) (equal? pred1 pred2)))
   ((predconst-form? pred1)
    (and (predconst-form? pred2)
	 (let ((name1 (predconst-to-name pred1))
	       (arity1 (predconst-to-arity pred1))
	       (index1 (predconst-to-index pred1))
	       (name2 (predconst-to-name pred2))
	       (arity2 (predconst-to-arity pred2))
	       (index2 (predconst-to-index pred2)))
	   (and (string=? name1 name2)
		(equal? arity1 arity2)
		(= index1 index2)))))
;     (and (predconst-form? pred2) (equal? pred1 pred2)))
   ((idpredconst-form? pred1)
    (and (idpredconst-form? pred2)
	 (let ((name1 (idpredconst-to-name pred1))
	       (types1 (idpredconst-to-types pred1))
	       (cterms1 (idpredconst-to-cterms pred1))
	       (name2 (idpredconst-to-name pred2))
	       (types2 (idpredconst-to-types pred2))
	       (cterms2 (idpredconst-to-cterms pred2)))
	   (and (string=? name1 name2)
		(equal? types1 types2)
		(= (length cterms1) (length cterms2))
		(apply and-op (map (lambda (x y) (cterm=? x y))
				   cterms1 cterms2))))))
   (else (myerror "predicate-equal?" "predicate expected" pred1))))	   


; 5-1. Predicate variables
; ========================

; A predicate variable of arity rho_1,..., rho_n is viewed as a
; placeholder for a formula with distinguished (different) variables
; x_1,..., x_n of types rho_1,..., rho_n (a so called comprehension
; term).  

(define (make-arity . x) (cons 'arity x))

(define (arity-to-types arity) (cdr arity))

(define (arity-to-string arity)
  (if (and (list? arity)
           (< 0 (length arity))
           (eq? 'arity (car arity)))
      (let* ((types (arity-to-types arity))
             (strings (map type-to-string types))
             (strings-with-leading-spaces
              (map (lambda (s) (string-append " " s)) strings)))
        (apply string-append
               (append (list "(arity")
                       strings-with-leading-spaces
		       (list ")"))))
      (myerror "arity-to-string" "arity expected" arity)))

(define (d-arity arity)
  (if COMMENT-FLAG (display (arity-to-string arity))))

; Complete test

(define (arity? x)
  (and (list? x)
       (< 0 (length x))
       (eq? 'arity (car x))
       (apply and-op (map type? (arity-to-types x)))))  

(define (arity-to-alg-names arity)
  (apply union (map type-to-alg-names (arity-to-types arity))))

; Predicate variable names are provided in the form of an association
; list, which assigns to the names their arities.  By default we have
; the predicate variable bot of arity (arity), called (logical) falsity.

; For the convenient display of predicate variables, we may provide
; default variable names for certain arities.

(define DEFAULT-PVAR-NAMES '())
(define INITIAL-DEFAULT-PVAR-NAMES DEFAULT-PVAR-NAMES)

(define (default-pvar-name arity)
  (let ((info (assoc arity DEFAULT-PVAR-NAMES)))
    (if info (cadr info) "")))

(define (set-default-pvar-name arity string)
  (set! DEFAULT-PVAR-NAMES (cons (list arity string) DEFAULT-PVAR-NAMES)))

(define PVAR-NAMES (list (list "bot" (make-arity))))
(define INITIAL-PVAR-NAMES PVAR-NAMES)

(define (add-pvar-name . x)
  (if (null? x)
      (myerror "add-pvar-name" "arguments expected")
      (let* ((rev (reverse x))
	     (arity (car rev))
	     (strings (reverse (cdr rev))))
	(if (not (arity? arity))
	    (myerror "add-pvar-name" "arity expected" arity))
	(for-each
	 (lambda (string)
	   (if (and (string? string) (not (string=? string "")))
	       (if (is-used? string arity 'pvar)
		   *the-non-printing-object*
		   (begin
		     (set! PVAR-NAMES
			   (append PVAR-NAMES (list (list string arity))))
		     (add-token string 'pvar-name (cons arity string))
		     (if (string=? "" (default-pvar-name arity))
			 (set-default-pvar-name arity string))
		     (comment
		      "ok, predicate variable " string ": "
		      (arity-to-string arity) " added")))
	       (myerror "add-pvar-name" "string expected" string)))
	 strings))))

(define apv add-pvar-name)

(define (remove-pvar-name . strings)
  (define (rpv1 string)
    (let ((info (assoc string PVAR-NAMES)))
      (if info
	  (let* ((arity (cadr info))
		 (info1 (assoc arity DEFAULT-PVAR-NAMES)))
	    (do ((l PVAR-NAMES (cdr l))
		 (res '() (if (string=? (caar l) string)
			      res
			      (cons (car l) res))))
		((null? l) (set! PVAR-NAMES (reverse res))))
	    (do ((l DEFAULT-PVAR-NAMES (cdr l)) ;added 01-05-24
		 (res '() (if (string=? (cadar l) string)
			      res
			      (cons (car l) res))))
		((null? l) (set! DEFAULT-PVAR-NAMES (reverse res))))
	    (remove-token string)
	    (comment "ok, predicate variable " string " is removed")
	    (if (and info1 (string=? (cadr info1) string))
		(comment
		 "warning: " string " was default pvariable of arity "
		 (arity-to-string arity))))
	  (myerror "remove-pvar-name" "predicate variable name expected"
		   string))))
  (for-each rpv1 strings))

(define rpv remove-pvar-name)

; Predicate variables are implemented as lists ('pvar arity index
; h-deg n-deg name).  If a predicate variable carries no index, we let
; the index be -1.  name is a string (the name of the predicate
; variable), to be used for output.

; To make sure that predicate variables generated by the system are
; different from all user introduced predicate variables, we maintain a
; global counter MAXPVARINDEX.  Whenever the user introduces a
; predicate variable, e.g. p^27, then MAXPVARINDEX is incremented to
; at least 27.

(define MAXPVARINDEX -1)
(define INITIAL-MAXPVARINDEX MAXPVARINDEX)

; Degrees of positivity (Harrop-degree) and negativity.

; Every predicate variable carries a pair h-deg, n-deg.  This
; restricts the admitted comprehension term {x|A} as follows.
; h-deg  n-deg   tau^+(A)    tau^-(A) 
;   0      0     arbitrary   arbitrary 
;   1      0     nulltype    arbitrary 
;   0      1     arbitrary   nulltype
;   1      1     nulltype    nulltype

(define h-deg-zero 0)
(define h-deg-one 1)

(define (h-deg-zero? h-deg)
  (and (integer? h-deg) (zero? h-deg)))

(define (h-deg-one? h-deg)
  (and (integer? h-deg) (positive? h-deg)))

(define (h-deg? x)
  (and (integer? x) (not (negative? x))))

(define n-deg-zero 0)
(define n-deg-one 1)

(define (n-deg-zero? n-deg)
  (and (integer? n-deg) (zero? n-deg)))

(define (n-deg-one? n-deg)
  (and (integer? n-deg) (positive? n-deg)))

(define (n-deg? x)
  (and (integer? x) (not (negative? x))))

; Constructor, accessors and tests for predicate variables:

(define (make-pvar arity index h-deg n-deg name)
  (set! MAXPVARINDEX (max index MAXPVARINDEX))
  (list 'pvar arity index h-deg n-deg name))

(define (pvar-form? x) (and (pair? x) (eq? 'pvar (car x))))

(define pvar-to-arity cadr)
(define pvar-to-index caddr)
(define pvar-to-h-deg cadddr)
(define (pvar-to-n-deg pvar) (car (cddddr pvar)))
(define (pvar-to-name pvar) (cadr (cddddr pvar)))

; Complete test:

(define (pvar? x)
  (and (list? x)
       (= 6 (length x))
       (let ((tag (car x))
	     (arity (cadr x))
	     (index (caddr x))
	     (h-deg (cadddr x))
	     (n-deg (car (cddddr x)))
	     (name (cadr (cddddr x))))
	 (and (eq? 'pvar tag)
	      (arity? arity)
	      (integer? index) (<= -1 index)
	      (h-deg? h-deg)
	      (n-deg? n-deg)
	      (or (string=? "" name)
		  (assoc name PVAR-NAMES))))))

; For convenience we add mk-pvar with options.  Options are index
; (default -1), h-deg (default h-deg-zero), n-deg (default
; n-deg-zero), and name (default given by (default-pvar-name arity)).

(define (mk-pvar arity . options)
  (let ((index -1)
	(h-deg h-deg-zero)
	(n-deg n-deg-zero)
	(name (default-pvar-name arity)))
    (if (pair? options)
	(begin (set! index (car options))
	       (set! options (cdr options))))
    (if (pair? options)
	(begin (set! h-deg (car options))
	       (set! options (cdr options))))
    (if (pair? options)
	(begin (set! n-deg (car options))
	       (set! options (cdr options))))
    (if (pair? options)
	(begin (set! name (car options))
	       (set! options (cdr options))))
    (if (pair? options)
	 (myerror "make-pvar" "unexpected argument" options))
  (cond ((not (and (integer? index) (<= -1 index)))
	 (myerror "make-pvar" "index >= -1 expected" index))
	((not (h-deg? h-deg))
	 (myerror "make-pvar" "h-deg expected" h-deg))
	((not (n-deg? n-deg))
	 (myerror "make-pvar" "n-deg expected" n-deg))
	((not (string? name))
	 (myerror "make-pvar" "string expected" name))
	(else (make-pvar arity index h-deg n-deg name)))))

(define (pvar-with-positive-content? pvar)
  (h-deg-zero? (pvar-to-h-deg pvar)))

(define (pvar-with-negative-content? pvar)
  (n-deg-zero? (pvar-to-n-deg pvar)))

; For display purposes we use

(define (pvar-to-string pvar)
  (let* ((arity (pvar-to-arity pvar))
	 (types (arity-to-types arity))
	 (index (pvar-to-index pvar))
	 (h-deg (pvar-to-h-deg pvar))
	 (n-deg (pvar-to-n-deg pvar))
	 (name (pvar-to-name pvar))
	 (default-pvar-name-with-type-args?
	   (and (string=? "" name) (pair? types)))
	 (proper-name
	  (if (string=? "" name)
	      (let* ((strings (map type-to-string types))
		     (strings-with-leading-spaces
		      (map (lambda (x) (string-append " " x)) strings)))
		(if (null? types)
		    "Pvar"
		    (apply string-append (append (list "(Pvar")
						 strings-with-leading-spaces
						 (list ")")))))
	      name))
	 (modifier
	  (if (h-deg-zero? h-deg)
	      (if (n-deg-zero? n-deg)
		  (if (and (not (= index -1))
			   default-pvar-name-with-type-args?) "_" "") "'")
	      (if (n-deg-zero? n-deg) "^" "^'")))
	 (index-string (if (= index -1) "" (number-to-string index))))
    (string-append proper-name modifier index-string)))

(define (pvar-name? string) (assoc string PVAR-NAMES))

(define (pvar-name-to-arity string)
  (let ((info (assoc string PVAR-NAMES)))
    (if info
	(cadr info)
	(myerror "pvar-name-to-arity" "pvar-name expected"
		 string))))

; For automatic generation of predicate variables we need

(define (numerated-pvar? pvar)
  (and (string=? "" (pvar-to-name pvar))
       (<= 0 (pvar-to-index pvar))))

(define (numerated-pvar-to-index x) (pvar-to-index x))

(define (arity-to-new-pvar arity . rest)
  (if (null? rest)
      (make-pvar arity (+ 1 MAXPVARINDEX) h-deg-one n-deg-one
		 (default-pvar-name arity))
      (make-pvar arity (+ 1 MAXPVARINDEX) 
		 (pvar-to-h-deg (car rest)) (pvar-to-n-deg (car rest))
		 (default-pvar-name arity))))
		
(define (arity-to-new-non-harrop-pvar arity)
  (make-pvar arity (+ 1 MAXPVARINDEX) h-deg-zero n-deg-one
	     (default-pvar-name arity)))

(define (arity-to-new-general-pvar arity)
  (make-pvar arity (+ 1 MAXPVARINDEX) h-deg-zero n-deg-zero
	     (default-pvar-name arity)))

; Occasionally we may want to create a new pvariable with the same name
; (and degree of totality) as a given one.  This is useful e.g. for
; bound renaming.  Therefore we supply

(define (pvar-to-new-pvar pvar)
  (make-pvar
   (pvar-to-arity pvar)
   (+ 1 MAXPVARINDEX)
   (pvar-to-h-deg pvar)
   (pvar-to-n-deg pvar)
   (pvar-to-name pvar)))

(define (compose-p-substitutions psubst1 psubst2)
  (compose-substitutions-wrt
   cterm-substitute equal? pvar-cterm-equal? psubst1 psubst2))


; 5-2. Predicate constants
; ========================

; General reasons for having predicate constants:
; - We need Equal, Total and STotal, which are *not* placeholders for formulas
; - We need predicates to be axiomatized

; General properties of predconsts:
; - They have no computational content.
; - They do not change their name when a tsubst is employed.  Hence from
;   a name one can only read off the uninstantiated type.
; - Their meaning can be fixed by axioms (e.g. for Equal, E and also for
;   Bar(.,.) of arity ('arity tree seq))

; Predicate constant names are provided in the form of an association
; list, which assigns to the names their arities.  By default we have
; the predicate constants Equal of arity (arity alpha alpha) and
; Total, STotal both of arity (arity alpha).

(define PREDCONST-NAMES
  (list
   (list "Equal" (make-arity (make-tvar -1 DEFAULT-TVAR-NAME)
			     (make-tvar -1 DEFAULT-TVAR-NAME)))
   (list "Total" (make-arity (make-tvar -1 DEFAULT-TVAR-NAME)))
   (list "STotal" (make-arity (make-tvar -1 DEFAULT-TVAR-NAME)))))

(define INITIAL-PREDCONST-NAMES PREDCONST-NAMES)

(define (add-predconst-name . x)
  (if (null? x)
      (myerror "add-predconst-name" "arguments expected")
      (let* ((rev (reverse x))
	     (arity (car rev))
	     (strings (reverse (cdr rev))))
	(if (not (arity? arity))
	    (myerror "add-predconst-name" "arity expected" arity))
	(for-each
	 (lambda (string)
	   (if (and (string? string) (not (string=? string "")))
	       (if (is-used? string arity 'predconst)
		   *the-non-printing-object*
		   (begin
		     (set! PREDCONST-NAMES
			   (append PREDCONST-NAMES (list (list string arity))))
		     (add-token
		      string
		      'predconst-name
		      (string-and-arity-to-predconst-parse-function
		       string arity))
		     (comment
		      "ok, predicate constant " string ": "
		      (arity-to-string arity) " added")))
	       (myerror "add-predconst-name" "string expected" string)))
	 strings))))

(define (string-and-arity-and-cterms-to-idpc-parse-function name arity cterms)
  (lambda args
    (let* ((uninst-types (arity-to-types arity))
	   (arg-types
	    (if (= (length uninst-types) (length args))
		(map term-to-type args)
		(apply
		 myerror
		 (append
		  (list "string-and-arity-and-cterms-to-idpc-parse-function"
			"arguments and arity of different lengths"
			name arity)
		  args))))
	   (uninst-type (apply mk-arrow (append uninst-types
						(list (make-alg "boole")))))
	   (type (apply mk-arrow (append arg-types
					 (list (make-alg "boole")))))
	   (tsubst (type-match-modulo-coercion uninst-type type))
	   (tvars (idpredconst-name-to-tvars name)))
      (if tsubst
	  (let ((subst-types
		 (map (lambda (tvar) (let ((info (assoc tvar tsubst)))
				       (if info (cadr info) tvar)))
		      tvars)))
	    (apply make-predicate-formula
		   (cons (make-idpredconst name subst-types cterms) args)))
	  (apply
	   myerror
	   (append
	    (list "string-and-arity-and-cterms-to-idpc-parse-function"
		  "types do not fit for inductively defined predicate constant"
		  name)
	    uninst-types arg-types))))))

(define (string-and-arity-to-predconst-parse-function string arity)
  (lambda (index . args)
    (let* ((uninst-types (arity-to-types arity))
	   (types (map term-to-type args))
	   (uninst-type
	    (apply mk-arrow (append uninst-types (list (make-alg "boole")))))
	   (type (apply mk-arrow (append types (list (make-alg "boole")))))
	   (tsubst (if (= (length uninst-types) (length types))
		       (type-match uninst-type type)
		       #f)))
      (if tsubst
	  (apply
	   make-predicate-formula
	   (cons (make-predconst arity tsubst index string)
		 args))
	  (apply myerror (cons "types do not fit"
			       (cons string
				     (append uninst-types types))))))))

(define apredc add-predconst-name) 

(define (remove-predconst-name . strings)
  (define (rpredc1 string)
    (let ((info (assoc string PREDCONST-NAMES)))
      (if info
	  (begin
	    (do ((l PREDCONST-NAMES (cdr l))
		 (res '() (if (string=? (caar l) string)
			      res
			      (cons (car l) res))))
		((null? l) (set! PREDCONST-NAMES (reverse res))))
	    (remove-token string)
	    (comment "ok, predicate constant " string " is removed"))
	  (myerror "remove-predconst-name" "predicate constant name expected"
		   string))))
  (for-each rpredc1 strings))

(define rpredc remove-predconst-name) 

; Predicate constants are implemented as lists 
; ('predconst uninst-arity tsubst index name).  

; Constructor, accessors and tests for predicate constants:

(define (make-predconst uninst-arity tsubst index name)
  (list 'predconst uninst-arity tsubst index name))

(define (predconst-form? x) (and (pair? x) (eq? 'predconst (car x))))

(define predconst-to-uninst-arity cadr)
(define predconst-to-tsubst caddr)
(define predconst-to-index cadddr)
(define (predconst-to-name predconst) (car (cddddr predconst)))

(define (predconst-to-arity predconst)
  (let* ((uninst-arity (predconst-to-uninst-arity predconst))
	 (tsubst (predconst-to-tsubst predconst))
	 (uninst-types (arity-to-types uninst-arity))
	 (types (map (lambda (x) (type-substitute x tsubst)) uninst-types)))
    (apply make-arity types)))

; (Almost) complete test:

(define (predconst? x)
  (and (list? x)
       (= 5 (length x))
       (let ((tagsymbol (car x))
	     (uninst-arity (cadr x))
	     (tsubst (caddr x))
	     (index (cadddr x))
	     (name (car (cddddr x))))
	 (and (eq? 'predconst tagsymbol)
	      (arity? uninst-arity)
	      (integer? index) (<= -1 index)
	      (tsubst? tsubst)
	      (or (string=? "" name)
		  (assoc name PREDCONST-NAMES))))))

(define (predconst-name? string) (assoc string PREDCONST-NAMES))

(define (predconst-name-to-arity predconst-name)
  (let ((info (assoc string PREDCONST-NAMES)))
    (if info
	(cadr info)
	(myerror "predconst-name-to-arity" "predconst-name expected"
		 predconst-name))))

; To allow for a convenient display, we maintain a global variable
; PREDCONST-DISPLAY consisting of entries (name token-type display-string)

(define PREDCONST-DISPLAY '())
(define INITIAL-PREDCONST-DISPLAY PREDCONST-DISPLAY)

(define (add-predconst-display name token-type display-string)
  (set! PREDCONST-DISPLAY
	(cons (list name token-type display-string) PREDCONST-DISPLAY)))

; For instance, adding for a predconst Less the token type predconst-infix
; and the display string << requires

; (add-token
;  "<<"
;  'predconst-infix
;  (string-and-arity-to-predconst-parse-function
;   "Less" (make-arity (py DEFAULT-TVAR-NAME) (py DEFAULT-TVAR-NAME))))

; (add-predconst-display "Less" 'predconst-infix "<<")

(define (predconst-to-string predconst)
  (let* ((name (predconst-to-name predconst))
	 (index (predconst-to-index predconst))
	 (index-string (if (= index -1) "" (number-to-string index)))
	 (info (assoc name PREDCONST-DISPLAY)))
    (if info
	(string-append (caddr info) index-string)
	(string-append name index-string))))


; 5-3. Inductively defined predicate constants
; ============================================

; Inductively defined predicate constants (idpredconsts) are implemented 
; as lists 

; ('idpredconst name types cterms).

; Constructor, accessors and tests for inductively defined predicate
; constants:

(define (make-idpredconst name types cterms)
  (if
   (and (member name '("ExDT" "ExLT" "ExRT" "ExUT"))
	(not (t-deg-one? (var-to-t-deg (car (cterm-to-vars (car cterms)))))))
   (myerror "make-idpredconst"
	    "comprehension term with total variable expected"
	    (car (cterm-to-vars (car cterms)))))
  (list 'idpredconst name types cterms))

; The following is used in grammar.scm, and involves some tests

(define (idpredconst-name-and-types-and-cterms-to-idpredconst name types
							      cterms)
  (let* ((tvars (idpredconst-name-to-tvars name))
	 (tsubst
	  (if (= (length tvars) (length types))
	      (make-substitution tvars types)
	      (apply
	       myerror
	       (append (list
			"idpredconst-name-and-types-and-cterms-to-idpredconst"
		       "equal lengths of tvars and types expected")
		       tvars types))))
	 (param-pvars (idpredconst-name-to-param-pvars name))
	 (subst-param-pvar-arities
	  (map (lambda (arity)
		 (apply make-arity (map (lambda (type)
					  (type-substitute type tsubst))
					(arity-to-types arity))))
	       (map pvar-to-arity param-pvars)))
	 (cterm-arities
	  (map (lambda (cterm) (apply make-arity
				      (map var-to-type (cterm-to-vars cterm))))
	       cterms)))
    (if (not (equal? subst-param-pvar-arities cterm-arities))
	(apply myerror
	       (append
		(list "idpredconst-name-and-types-and-cterms-to-idpredconst"
		      "equal arities expected")
		subst-param-pvar-arities cterm-arities)))
    (make-idpredconst name types cterms)))

(define (idpredconst-form? x) (and (pair? x) (eq? 'idpredconst (car x))))

(define idpredconst-to-name cadr)
(define idpredconst-to-types caddr)
(define idpredconst-to-cterms cadddr)

(define (idpredconst-to-arity idpc)
  (let* ((name (idpredconst-to-name idpc))
	 (types (idpredconst-to-types idpc))
	 (tsubst (idpredconst-name-and-types-to-tsubst name types))
	 (pvar (idpredconst-name-to-pvar name))
	 (uninst-arity (pvar-to-arity pvar))
	 (uninst-types (arity-to-types uninst-arity))
	 (inst-types
	  (map (lambda (x) (type-substitute x tsubst)) uninst-types)))
    (apply make-arity inst-types)))

; (Almost) complete test:

(define (idpredconst? x)
  (and (list? x)
       (= 4 (length x))
       (let ((tag (car x))
	     (name (cadr x))
	     (types (caddr x))
	     (cterms (cadddr x)))
	 (and (eq? 'idpredconst tag)
	      (assoc name IDS)))))

(define (idpredconst-to-pinst idpc)
  (let* ((name (idpredconst-to-name idpc))
	 (types (idpredconst-to-types idpc))
	 (param-cterms (idpredconst-to-cterms idpc))
	 (idpc-names-with-pvars-and-opt-alg-names
	  (idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names name))
	 (names (map car idpc-names-with-pvars-and-opt-alg-names))
	 (clauses-with-opt-constr-names
	  (apply append
		 (map idpredconst-name-to-clauses-with-names
		      names)))
	 (clauses (map car clauses-with-opt-constr-names))
	 (param-pvars (idpredconst-name-to-param-pvars name))
	 (clause-tvars-list (map formula-to-tvars clauses))
	 (clause-tvars (apply union clause-tvars-list))
	 (tsubst (if (= (length clause-tvars) (length types))
		     (make-substitution clause-tvars types)
		     (apply
		      myerror
		      (append
		       (list
			"idpredconst-to-pinst:"
			"equal lengths of clause-tvars and types expected")
		       clause-tvars types))))
	 (var-lists (map cterm-to-vars param-cterms))
	 (cterm-arities
	  (map (lambda (x) (apply make-arity (map var-to-type x))) var-lists))
	 (param-pvar-arities (map pvar-to-arity param-pvars))
	 (subst-param-pvar-arities
	  (map (lambda (x) (apply make-arity
				  (map (lambda (y) (type-substitute y tsubst))
				       (arity-to-types x))))
	       param-pvar-arities)))
    (if (equal? cterm-arities subst-param-pvar-arities)
	(make-substitution-wrt pvar-cterm-equal? param-pvars param-cterms)
	(apply myerror (append (list "idpredconst-to-pinst"
				     "equal arities expected")
			       cterm-arities subst-param-pvar-arities)))))

; To allow for a convenient display, we maintain a global variable
; IDPREDCONST-DISPLAY consisting of entries (name token-type display-string)

(define IDPREDCONST-DISPLAY '())
(define INITIAL-IDPREDCONST-DISPLAY IDPREDCONST-DISPLAY)

(define (add-idpredconst-display name token-type display-string)
  (set! IDPREDCONST-DISPLAY
	(cons (list name token-type display-string) IDPREDCONST-DISPLAY)))

; For instance, adding for a idpredconst RatEq the token type
; pred-infix and the display string === requires

; (add-token
;  "==="
;  'pred-infix
;  (lambda (x y)
;    (make-predicate-formula (make-idpredconst "RatEq" '() '()) x y)))

; (add-idpredconst-display "RatEq" 'pred-infix "===")

(define (idpredconst-to-string idpc)
  (let* ((name (idpredconst-to-name idpc))
	 (types (idpredconst-to-types idpc))
	 (param-cterms (idpredconst-to-cterms idpc))
	 (type-strings (map type-to-string types))
	 (cterm-strings (map cterm-to-string param-cterms))
	 (strings (append type-strings cterm-strings))
	 (type-strings-with-leading-spaces
	  (map (lambda (x) (string-append " " x)) type-strings))
	 (cterm-strings-with-leading-spaces
	  (map (lambda (x) (string-append " " x)) cterm-strings)))
    (cond
     ((member name '("ExD" "ExDT"))
      (let* ((cterm (car param-cterms))
	     (var (car (cterm-to-vars cterm)))
	     (kernel (cterm-to-formula cterm))
	     (varstring (var-to-string var))
	     (kernelstring (formula-to-string kernel)))
	(string-append "exd" (separator-string "exd" varstring)
		       varstring (separator-string varstring kernelstring)
		       kernelstring)))
     ((member name '("ExL" "ExLT"))
      (let* ((cterm (car param-cterms))
	     (var (car (cterm-to-vars cterm)))
	     (kernel (cterm-to-formula cterm))
	     (varstring (var-to-string var))
	     (kernelstring (formula-to-string kernel)))
	(string-append "exl" (separator-string "exl" varstring)
		       varstring (separator-string varstring kernelstring)
		       kernelstring)))
     ((member name '("ExR" "ExRT"))
      (let* ((cterm (car param-cterms))
	     (var (car (cterm-to-vars cterm)))
	     (kernel (cterm-to-formula cterm))
	     (varstring (var-to-string var))
	     (kernelstring (formula-to-string kernel)))
	(string-append "exr" (separator-string "exr" varstring)
		       varstring (separator-string varstring kernelstring)
		       kernelstring)))
     ((member name '("ExU" "ExUT"))
      (let* ((cterm (car param-cterms))
	     (var (car (cterm-to-vars cterm)))
	     (kernel (cterm-to-formula cterm))
	     (varstring (var-to-string var))
	     (kernelstring (formula-to-string kernel)))
	(string-append "exu" (separator-string "exu" varstring)
		       varstring (separator-string varstring kernelstring)
		       kernelstring)))
;      ((string=? "ExDT" name)
;       (let* ((cterm (car param-cterms))
; 	     (var (car (cterm-to-vars cterm)))
; 	     (kernel (cterm-to-formula cterm))
; 	     (varstring (var-to-string var))
; 	     (kernelstring (formula-to-string kernel)))
; 	(string-append "exdt" (separator-string "exdt" varstring)
; 		       varstring (separator-string varstring kernelstring)
; 		       kernelstring)))
;      ((string=? "ExLT" name)
;       (let* ((cterm (car param-cterms))
; 	     (var (car (cterm-to-vars cterm)))
; 	     (kernel (cterm-to-formula cterm))
; 	     (varstring (var-to-string var))
; 	     (kernelstring (formula-to-string kernel)))
; 	(string-append "exlt" (separator-string "exlt" varstring)
; 		       varstring (separator-string varstring kernelstring)
; 		       kernelstring)))
;      ((string=? "ExRT" name)
;       (let* ((cterm (car param-cterms))
; 	     (var (car (cterm-to-vars cterm)))
; 	     (kernel (cterm-to-formula cterm))
; 	     (varstring (var-to-string var))
; 	     (kernelstring (formula-to-string kernel)))
; 	(string-append "exrt" (separator-string "exrt" varstring)
; 		       varstring (separator-string varstring kernelstring)
; 		       kernelstring)))
;      ((string=? "ExUT" name)
;       (let* ((cterm (car param-cterms))
; 	     (var (car (cterm-to-vars cterm)))
; 	     (kernel (cterm-to-formula cterm))
; 	     (varstring (var-to-string var))
; 	     (kernelstring (formula-to-string kernel)))
; 	(string-append "exut" (separator-string "exut" varstring)
; 		       varstring (separator-string varstring kernelstring)
; 		       kernelstring)))
     ((string=? "AndD" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " andd " 
		       (formula-to-string kernel2))))
     ((string=? "AndL" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " andl " 
		       (formula-to-string kernel2))))
     ((string=? "AndR" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " andr " 
		       (formula-to-string kernel2))))
     ((string=? "AndU" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " andu " 
		       (formula-to-string kernel2))))
     ((string=? "OrD" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " ord " 
		       (formula-to-string kernel2))))
     ((string=? "OrL" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " orl " 
		       (formula-to-string kernel2))))
     ((string=? "OrR" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " orr " 
		       (formula-to-string kernel2))))
     ((string=? "OrU" name)
      (let* ((cterm1 (car param-cterms))
	     (cterm2 (cadr param-cterms))
	     (kernel1 (cterm-to-formula cterm1))
	     (kernel2 (cterm-to-formula cterm2)))
	(string-append (formula-to-string kernel1)
		       " oru " 
		       (formula-to-string kernel2))))
     ((string=? "EqD" name) "eqd")
     (else
      (let* ((info (assoc name IDPREDCONST-DISPLAY))
	     (new-name (if info (caddr info) name))
	     (tvars-inferable-from-arity?
	      (null? (set-minus
		      (idpredconst-name-to-tvars name)
		      (apply union (map type-to-free
					(arity-to-types
					 (pvar-to-arity
					  (idpredconst-name-to-pvar
					   name)))))))))
	(if tvars-inferable-from-arity?
	    (if (null? param-cterms)
		new-name
		(apply string-append
		       (append (list "(" new-name)
			       cterm-strings-with-leading-spaces
			       (list ")"))))
	    (apply string-append
		   (append (list "(" new-name)
			   type-strings-with-leading-spaces
			   cterm-strings-with-leading-spaces
			   (list ")")))))))))

(define (idpredconst-to-free idpc)
  (let* ((types (idpredconst-to-types idpc))
	 (cterms (idpredconst-to-cterms idpc)))
    (apply union (map cterm-to-free cterms))))

; Inductively defined predicate constant names are provided in the form
; of an association list IDS, which assigns all relevant information to
; the name.

; Format of IDS:

; ((idpredconst-name idpredconst-names-with-pvars-and-opt-alg-names
; 	             (clause1 name1) (clause2 name2)...)
;  ...)

; Here the assigned pvars serve for ease of substitutions when forming
; e.g. an elimination axiom.  The presence of an alg-name indicates that
; this idpredconst is to have computational content.  Then all clauses
; with this idpredconst in the conclusion must provide a constr-name.
; If idpredconst is to have no computational content, then all its
; clauses must be invariant (under realizability, a.k.a. `negative').

; How it works: add Acc^ temporarily as a predicate variable.  Then
; parse the clauses.  Create new pvar X.  Substitute Acc^ by X.  Remove
; pvar Acc^.  Create idpredconst Acc.  Form clauses by substituting Acc
; for X.

(define IDS '())
(define INITIAL-IDS IDS)

(define (idpredconst-name? string) (assoc string IDS))

(define (idpredconst-name-to-pvar name)
  (let* ((info1 (assoc name IDS))
	 (idpredconst-names-with-pvars-and-opt-alg-names
	  (if
	   info1 (cadr info1)
	   (myerror
	    "idpredconst-name-to-pvar-name" "idpredconst name expected" name)))
	 (info2 (assoc name idpredconst-names-with-pvars-and-opt-alg-names)))
    (cadr info2)))

(define (idpredconst-name-to-opt-alg-name name)
  (let* ((info1 (assoc name IDS))
	 (idpredconst-names-with-pvars-and-opt-alg-names
	  (if
	   info1 (cadr info1)
	   (myerror
	    "idpredconst-name-to-opt-alg-name" "idpredconst name expected"
	    name)))
	 (info2 (assoc name idpredconst-names-with-pvars-and-opt-alg-names)))
    (cddr info2)))

(define (idpredconst-name-to-alg-name name)
  (let ((opt-alg-name (idpredconst-name-to-opt-alg-name name)))
    (if (pair? opt-alg-name) (car opt-alg-name)
	(myerror "idpredconst-name-to-alg-name"
		 "alg name expected for" name))))

(define (idpredconst-name-to-nbe-alg-name name)
  (string-append "nbe" name))

(define (idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names name)
  (let* ((info (assoc name IDS)))
    (if info (cadr info)
	(myerror "idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names"
		 "idpredconst name expected" name))))

(define (idpredconst-name-to-simidpc-names name)
  (map car (idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names name)))

(define (idpredconst-name-to-pvars name)
  (map cadr
       (idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names name)))

(define (idpredconst-name-to-param-pvars name)
  (let* ((idpc-names-with-pvars-and-opt-alg-names
	  (idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names name))
	 (names (map car idpc-names-with-pvars-and-opt-alg-names))
	 (pvars (map cadr idpc-names-with-pvars-and-opt-alg-names))
	 (clauses-with-names
	  (apply append (map idpredconst-name-to-clauses-with-names names)))
	 (clauses (map car clauses-with-names))
	 (clause-pvars-list (map formula-to-pvars clauses))
	 (clause-pvars (apply union clause-pvars-list)))
    (set-minus clause-pvars pvars)))

(define (idpredconst-name-to-clauses-with-names name)
  (let* ((info (assoc name IDS)))
    (if info (cddr info)
	(myerror "idpredconst-name-to-clauses-with-names"
		 "idpredconst name expected" name))))

(define (idpredconst-name-to-clauses name)
  (map car (idpredconst-name-to-clauses-with-names name)))

(define (idpredconst-name-to-tvars name)
  (let* ((idpredconst-names-with-pvars-and-opt-alg-names
	  (idpredconst-name-to-idpc-names-with-pvars-and-opt-alg-names name))
	 (names (map car idpredconst-names-with-pvars-and-opt-alg-names))
	 (clauses-with-names
	  (apply append
		 (map idpredconst-name-to-clauses-with-names
		      names)))
	 (clauses (map car clauses-with-names))
	 (clause-tvars-list (map formula-to-tvars clauses)))
    (apply union clause-tvars-list)))

(define (idpredconst-name-and-types-to-tsubst name types)
  (let ((tvars (idpredconst-name-to-tvars name)))
    (if (= (length tvars) (length types))
	(make-substitution tvars types)
	(apply myerror (append (list  "idpredconst-name-and-types-to-tsubst"
				      "equal lengths expected")
			       tvars types)))))

; add-ids means add inductively defined predicates.  How it works: We
; are given idpc-names-with-arities-and-opt-alg-names.  For simplicity
; assume that there is only one idpc-name.  We are also given its
; clauses as clause-strings-with-opt-names.  For parsing the
; clause-strings, a new predicate variable is created and (using it)
; clauses-with-new-pvars-and-names are built.  The names of the
; clauses are either given or else created, in the form EvenZero,
; EvenOne etc.  The presence of an alg-name (e.g., algEven) in
; idpc-names-with-arities-and-opt-alg-names indicates that the idpc is
; computationally relevant (c.r.), that is requires witnesses, for
; extraction.  If an alg-name is present, then all clauses with this
; idpredconst as conclusion generate constructors; default-name
; cEvenZero, cEvenOne.  We use nbeEven as alg-name for nbe.

; We also allow computationally irrelevant (c.i.) (or
; non-computational, n.c.) inductively defined predicates.  Then
; idpc-names-with-arities-and-opt-alg-names has no alg-name.
; Important special cases are: (i) For every I its witnessing
; predicate IReal.  It is special in the sense that it just states the
; fact that we do have a realizer for I.  (ii) Leibniz equality EqD,
; and computationally irrelevant versions ExU and AndU (better: ExNC,
; AndNC) of the existential quantifier and of conjunction.  These are
; special in the sense that they are defined by just one clause, which
; contains impnc, allnc only and has no recursive premises.  In all
; other cases the elimination scheme must be (severely) restricted to
; n.c. formulas.  Moreover, all clauses must have non-computational
; invariant (n.c.i.) parameter and premise formulas.  This ensures
; that the soundness theorem continues to hold: all intro and elim
; axioms are invariant, i.e., nullterm mr A is the same as A.

(define (add-ids idpc-names-with-arities-and-opt-alg-names .
		 clause-strings-with-opt-names)
  (if (not (list? idpc-names-with-arities-and-opt-alg-names))
      (myerror
       "add-ids" "list idpc-names-with-arities-and-opt-alg-names expected"
       idpc-names-with-arities-and-opt-alg-names))
  (for-each
   (lambda (x)
     (if
      (not (and (<= 2 (length x))
		(string? (car x))
		(arity? (cadr x))
		(or (= 2 (length x))
		    (and (= 3 (length x)) (string? (caddr x))))))
      (myerror "add-ids" "idpc-name with arity and opt alg-name expected" x)))
   idpc-names-with-arities-and-opt-alg-names)
  (set! OLD-COMMENT-FLAG COMMENT-FLAG)
  (set! COMMENT-FLAG #f)
  (let* ((idpc-names (map car idpc-names-with-arities-and-opt-alg-names))
	 (new-idpc-names-test
	  (if (not (apply and-op (map (lambda (s)
					(and (string? s)
					     (not (is-used? s '()
							    'idpredconst))))
				      idpc-names)))
	      (begin
		(set! COMMENT-FLAG OLD-COMMENT-FLAG)
		(myerror "add-ids" "list of new strings expected"
			 idpc-names))))
	 (clause-strings-with-opt-names-test
	  (for-each
	   (lambda (x)
	     (if (or (not (list? x))
		     (< 2 (length x))
		     (not (string? (car x)))
		     (and (pair? (cdr x)) (not (string? (cadr x)))))
		 (begin
		   (set! COMMENT-FLAG OLD-COMMENT-FLAG)
		   (myerror "add-ids"
			    "list of clause-string and optional name expected"
			    x))))
	   clause-strings-with-opt-names))
	 (all-with-content?
	  (apply and-op (map (lambda (x) (< 2 (length x)))
			     idpc-names-with-arities-and-opt-alg-names)))
	 (all-without-content?
	  (apply and-op (map (lambda (x) (= 2 (length x)))
			     idpc-names-with-arities-and-opt-alg-names)))
	 (all-with-or-all-without-content-test
	  (if
	   (not (or all-with-content? all-without-content?))
	   (begin
	     (set! COMMENT-FLAG OLD-COMMENT-FLAG)
	     (myerror "add-ids" "inductively defined predicate constants"
		      idpc-names
		      "should be either all with or all without content"))))
	 (arities (map cadr idpc-names-with-arities-and-opt-alg-names))
	 (clause-strings (map car clause-strings-with-opt-names))
	 (new-pvars (map arity-to-new-general-pvar arities))
	 (var-lists
	  (map (lambda (arity)
		 (map type-to-new-partial-var (arity-to-types arity)))
	       arities))
	 (atoms (map (lambda (x y)
		       (apply make-predicate-formula
			      (cons x (map make-term-in-var-form y))))
		     new-pvars var-lists))
	 (cterms (map (lambda (x y) (apply make-cterm (append x (list y))))
		      var-lists atoms))
					;now we can parse clause-strings
	 (clauses-with-new-pvars
	  (begin (do ((l1 idpc-names (cdr l1))
		      (l2 arities (cdr l2)))
		     ((null? l1))
		   (add-pvar-name (car l1) (car l2)))
		 (let* ((pvars
			 (map (lambda (x y)
				(make-pvar x -1 h-deg-zero n-deg-zero y))
			      arities idpc-names))
			(psubst (map (lambda (x y) (list x y))
				     pvars cterms)))
		   (map (lambda (x) (formula-substitute (pf x) psubst))
			clause-strings))))
	 (param-pvars (set-minus (apply union (map formula-to-pvars
						   clauses-with-new-pvars))
				 new-pvars))
	 (clause-test
	  (if
	   all-with-content?
	   (for-each
	    (lambda (fla clause-string)
	      (if (not (cr-idpc-clause? fla new-pvars param-pvars))
		  (let ((free (formula-to-free fla)))
		    (set! COMMENT-FLAG OLD-COMMENT-FLAG)
		    (apply remove-var-name idpc-names)
		    (if
		     (pair? free)
		     (apply myerror
			    (append
			     (list "add-ids" "unexpected free variables")
			     free (list "in clause" clause-string))))
		    (myerror
		     "add-ids"
		     "not a clause for a computationally relevant idpc"
		     clause-string))))
	    clauses-with-new-pvars clause-strings)
					;else all without content
	   (for-each
	    (lambda (fla clause-string)
	      (if (not (nc-idpc-clause? fla new-pvars param-pvars))
		  (let ((free (formula-to-free fla)))
		    (set! COMMENT-FLAG OLD-COMMENT-FLAG)
		    (apply remove-var-name idpc-names)
		    (if
		     (pair? free)
		     (apply myerror
			    (append
			     (list "add-ids" "unexpected free variables")
			     free (list "in clause" clause-string))))
		    (myerror
		     "add-ids"
		     "not a clause for a non-computational idpc"
		     clause-string))))
	    clauses-with-new-pvars clause-strings)))
; 	 (clause-test
; 	  (for-each
; 	   (lambda (fla clause-string)
; 	     (if (not (clause? fla new-pvars))
; 		 (let ((free (formula-to-free fla)))
; 		   (set! COMMENT-FLAG OLD-COMMENT-FLAG)
; 		   (apply remove-var-name idpc-names)
; 		   (if
; 		    (pair? free)
; 		    (apply myerror
; 			   (append (list "add-ids" "unexpected free variables")
; 				   free (list "in clause" clause-string))))
; 		   (myerror
; 		    "add-ids"
; 		    "not a clause in the sense of clause?" clause-string))))
; 	   clauses-with-new-pvars clause-strings))
; 	 (uniform-non-recursive-clause-test
; 	  (if
; 	   all-without-content?
; 	   (for-each
; 	    (lambda (fla)
; 	      (if
; 	       (not (apply uniform-non-recursive-clause? (cons fla new-pvars)))
; 	       (begin
; 		 (set! COMMENT-FLAG OLD-COMMENT-FLAG)
; 		 (apply remove-var-name idpc-names)
; 		 (myerror "add-ids" "uniform non-recursive clause expected"
; 			  "for idpc not requiring witnesses" fla))))
; 	    clauses-with-new-pvars)))
	 (clauses-with-new-pvars-and-opt-names
	  (map (lambda (x y) (cons x y))
	       clauses-with-new-pvars
	       (map cdr clause-strings-with-opt-names)))
	 (clauses-with-new-pvars-and-names
	  (do ((l clauses-with-new-pvars-and-opt-names (cdr l))
	       (pvar-counter-alist-and-res
		(list (map (lambda (pvar) (list pvar 0)) new-pvars) '())
		(let* ((pvar-counter-alist (car pvar-counter-alist-and-res))
		       (res (cadr pvar-counter-alist-and-res))
		       (clause-with-new-pvar-and-opt-name (car l))
		       (clause (car clause-with-new-pvar-and-opt-name))
		       (opt-name (cdr clause-with-new-pvar-and-opt-name))
		       (pvar
			(let* ((kernel (all-allnc-form-to-final-kernel clause))
			       (concl
				(imp-impnc-form-to-final-conclusion kernel)))
			  (predicate-form-to-predicate concl)))
		       (idpc-name
			(cadr (assoc pvar (map (lambda (x y) (list x y))
					       new-pvars idpc-names))))
		       (i (cadr (assoc pvar pvar-counter-alist)))
		       (name (if (null? opt-name)
				 (string-append
				  idpc-name
				  (number-to-alphabetic-string i))
				 (car opt-name))))
		  (list (cons (list pvar (+ 1 i))
			      (remove (list pvar i) pvar-counter-alist))
			(cons (list clause name) res)))))
	      ((null? l) (reverse (cadr pvar-counter-alist-and-res)))))
	 (clause-names (map cadr clauses-with-new-pvars-and-names))
	 (idpc-names-and-clauses-with-new-pvars-and-names
	  (map
	   (lambda (idpc-name)
	     (do ((l clauses-with-new-pvars-and-names (cdr l))
		  (res
		   '()
		   (if
		    (let* ((pvar (cadr (assoc idpc-name
					      (map (lambda (x y) (list x y))
						   idpc-names
						   new-pvars))))
			   (kernel (all-allnc-form-to-final-kernel (caar l)))
			   (concl (imp-impnc-form-to-final-conclusion kernel)))
		      (and (predicate-form? concl)
			   (equal? pvar (predicate-form-to-predicate concl))))
		    (cons (car l) res)
		    res)))
		 ((null? l) (cons idpc-name (reverse res)))))
	   idpc-names))
	 (nbe-types (map nbe-formula-to-type clauses-with-new-pvars))
	 (new-tvars (map PVAR-TO-TVAR new-pvars))
	 (new-tvars-cc (if all-with-content?
			   (map PVAR-TO-TVAR new-pvars)
			   '()))
	 (alg-names (if all-with-content?
			(map caddr idpc-names-with-arities-and-opt-alg-names)
			'()))
	 (nbe-alg-names (map idpredconst-name-to-nbe-alg-name idpc-names))
					;for stringtyped-constr-names temp.
					;add nbe-alg-names with token type
					;alg to ALGEBRAS
	 (nbe-tsubst-for-new-tvars
	  (begin 	 
	    (set! OLD-ALGEBRAS ALGEBRAS)
	    (for-each (lambda (x)
			(set! ALGEBRAS
			      (cons (list x nbe-alg-names 'alg) ALGEBRAS)))
		      nbe-alg-names)
	    (map (lambda (x y) (list x (make-alg y)))
		 new-tvars nbe-alg-names)))
	 (nbe-free-tvars-list (map type-to-free nbe-types))
	 (param-pvar-tvars (map PVAR-TO-TVAR param-pvars))
	 (nbe-standard-tvars-list ;alpha1 ... for stringtyped-constr-names
	  (map (lambda (free-tvars)
		 (do ((i 1 (+ 1 i))
		      (res '() (cons (make-tvar i DEFAULT-TVAR-NAME) res)))
		     ((> i (length (union free-tvars param-pvar-tvars)))
		      (reverse res))))
	       nbe-free-tvars-list))
	 (nbe-tsubst-list
	  (map (lambda (free-tvars standard-tvars)
		 (map (lambda (x y) (list x y))
		      (union free-tvars param-pvar-tvars) standard-tvars))
	       nbe-free-tvars-list nbe-standard-tvars-list))
	 (nbe-constr-types
	  (map (lambda (type tsubst)
		 (type-substitute
		  type (append nbe-tsubst-for-new-tvars tsubst)))
	       nbe-types nbe-tsubst-list))
	 (nbe-type-strings (map type-to-string nbe-constr-types))
	 (et-types (if all-without-content? '()
		       (map formula-to-et-type clauses-with-new-pvars)))
	 (et-constr-names
	  (if all-without-content? '()
	      (map (lambda (name) (string-append "c" name)) clause-names)))
	 (et-tvars (set-minus (apply union (map (lambda (x)
						  (type-to-free
						   (formula-to-et-type x)))
						clauses-with-new-pvars))
			      new-tvars))
	 (et-standard-tvars
	  (do ((i 1 (+ 1 i))
	       (res '() (cons (make-tvar i DEFAULT-TVAR-NAME) res)))
	      ((> i (length et-tvars)) (reverse res))))
	 (et-tsubst (map (lambda (x y) (list x y)) et-tvars et-standard-tvars))
	 (tsubst2 ;temporarily add alg-names with token type alg to ALGEBRAS
	  (begin 	 
	    (for-each (lambda (x)
			(set! ALGEBRAS
			      (cons (list x alg-names 'alg) ALGEBRAS)))
		      alg-names)
	    (map (lambda (x y) (list x (make-alg y)))
		 new-tvars-cc alg-names)))
	 (et-constr-types
	  (map (lambda (x) (type-substitute x (append tsubst2 et-tsubst)))
	       et-types))
	 (et-type-strings (map type-to-string et-constr-types))
	 (stringtyped-et-constr-names
	  (map (lambda (x y) (list x y))
	       et-constr-names et-type-strings))
	 (nbe-alg-names-and-number-of-clauses
	  (map (lambda (x y) (list x (length (cdr y))))
	       nbe-alg-names
	       idpc-names-and-clauses-with-new-pvars-and-names))
	 (nbe-constr-names
	  (apply
	   append
	   (map (lambda (nbe-alg-name idpredconst-name)
		  (let ((number-of-clauses
			 (cadr (assoc nbe-alg-name
				      nbe-alg-names-and-number-of-clauses))))
		    (do ((n 0 (+ 1 n))
			 (res '() (cons (string-append
					 (number-to-alphabetic-string n)
					 idpredconst-name)
					res)))
			((= n number-of-clauses) (reverse res)))))
		nbe-alg-names idpc-names)))
	 (stringtyped-nbe-constr-names
	  (map (lambda (x y) (list x y))
	       nbe-constr-names nbe-type-strings))
	 (param-tvars
	  (apply union (map formula-to-tvars clauses-with-new-pvars))))
    (apply remove-pvar-name idpc-names)
    (set! ALGEBRAS OLD-ALGEBRAS)
					;check for nullary clauses (2008-02-24)
    (if (null? clauses-with-new-pvars)
	(begin (set! COMMENT-FLAG OLD-COMMENT-FLAG)
	       (myerror "add-ids" "nullary clause missing")))
    (let
	((remove-safe-clauses
	  (lambda (clauses)
	    (let*
		((concl-pvars
		  (map (lambda (x)
			 (predicate-form-to-predicate
			  (imp-impnc-all-allnc-form-to-final-conclusion x)))
		       clauses))
		 (clauses-wo-rec-calls-to-concl-pvars
		  (list-transform-positive clauses
		    (lambda (clause)
		      (null?
		       (list-transform-positive
			   (imp-form-to-premises
			    (all-allnc-form-to-final-kernel clause))
			 (lambda (x)
			   (let ((concl
				  (imp-impnc-all-allnc-form-to-final-conclusion
				   x)))
			     (and (predicate-form? concl)
				  (member
				   (predicate-form-to-predicate concl)
				   concl-pvars)))))))))
		 (safe-pvars
		  (map (lambda (x)
			 (predicate-form-to-predicate
			  (imp-impnc-all-allnc-form-to-final-conclusion x)))
		       clauses-wo-rec-calls-to-concl-pvars))
		 (safe-clauses
		  (list-transform-positive clauses
		    (lambda (x)
		      (member
		       (predicate-form-to-predicate
			(imp-impnc-all-allnc-form-to-final-conclusion x))
		       safe-pvars)))))
	      (if (null? safe-clauses)
		  (begin
		    (set! COMMENT-FLAG OLD-COMMENT-FLAG)
		    (apply
		     myerror
		     (append
		      (list "add-ids" "nullary clause missing in clauses")
		      clauses))))
	      (set-minus clauses safe-clauses)))))
      (do ((clauses clauses-with-new-pvars (remove-safe-clauses clauses)))
          ((null? clauses))))
    (let ((free (apply union (map type-to-free nbe-constr-types))))
      (if (null? free)
	  (apply add-algs (cons nbe-alg-names stringtyped-nbe-constr-names))
	  (apply add-algebras-with-parameters
		 (append (list nbe-alg-names 'alg-typeop (length free))
			 stringtyped-nbe-constr-names))))
    (if
     (pair? alg-names)
     (let ((et-constr-type-tvars
	    (apply union (map type-to-free et-constr-types))))
       (if (null? et-constr-type-tvars)
	   (apply add-algs (cons alg-names stringtyped-et-constr-names))
	   (apply add-algebras-with-parameters
		  (append
		   (list alg-names 'alg-typeop (length et-constr-type-tvars))
		   stringtyped-et-constr-names)))))
    (for-each ;of idpc-names-and-clauses-with-new-pvars-and-names and arities
     (lambda (x arity)
       (let ((idpc-name (car x))
	     (clauses-with-new-pvars-and-names (cdr x))
	     (idpc-names-with-pvars-and-opt-alg-names
	      (map (lambda (x y) (cons (car x) (cons y (cddr x))))
		   idpc-names-with-arities-and-opt-alg-names
		   new-pvars))
	     (non-inferable-param-tvars
	      (set-minus
	       param-tvars
	       (apply union (map type-to-free (arity-to-types arity))))))
	 (set! COMMENT-FLAG OLD-COMMENT-FLAG)
	 (comment "ok, inductively defined predicate constant "
		  idpc-name " added")
	 (set! IDS (cons (append
			  (list
			   idpc-name
			   idpc-names-with-pvars-and-opt-alg-names)
			  clauses-with-new-pvars-and-names)
			 IDS))
	 (cond
	  ((and (null? param-pvars)
		(null? non-inferable-param-tvars))
	   (add-token ;
	    idpc-name
	    'idpredconst-name
	    (string-and-arity-and-cterms-to-idpc-parse-function
	     idpc-name arity '())))
	  ((and (pair? param-pvars)
		(pair? non-inferable-param-tvars))
	   (add-token ;
	    idpc-name
	    'idpredconstscheme-name
	    idpc-name))
	  ((and (pair? param-pvars)
		(null? non-inferable-param-tvars))
	   (add-token ;
	    idpc-name
	    'idpredconstscheme-name-wit ;wit=with-inferable-types
	    (lambda (cterms)
	      (string-and-arity-and-cterms-to-idpc-parse-function
	       idpc-name arity cterms))))
	  (else
	   (myerror
	    "add-ids"
	    "unexpected idpredconst without cterms whose param-tvars"
	    "cannot be inferred from the arguments" idpc-name)))))
     idpc-names-and-clauses-with-new-pvars-and-names
     arities)
    (for-each ;of idpc-names-and-clauses-with-new-pvars-and-names
     (lambda (x)
       (let ((idpc-name (car x))
	     (clauses-with-new-pvars-and-names (cdr x)))
	 (do ((i 0 (+ 1 i))
	      (names (map cadr clauses-with-new-pvars-and-names) (cdr names)))
	     ((= i (length clauses-with-new-pvars-and-names)))
	   (let* ((cterms
		   (if (member idpc-name '("ExDT" "ExLT" "ExRT" "ExUT"))
		       (map predicate-to-cterm-with-total-vars param-pvars)
		       (map predicate-to-cterm param-pvars)))
		  (aconst (number-and-idpredconst-to-intro-aconst
			   i (make-idpredconst idpc-name param-tvars cterms)))
		  (proof (make-proof-in-aconst-form aconst)))
	     (set! THEOREMS
		   (cons (list (car names) aconst proof) THEOREMS))))))
     idpc-names-and-clauses-with-new-pvars-and-names)))

(define (remove-idpc-name . x)
  (define (rin1 idpc-name)
    (let* ((simidpc-names
	    (idpredconst-name-to-simidpc-names idpc-name))
	   (opt-alg-names
	    (apply union
		   (map (lambda (n) (idpredconst-name-to-opt-alg-name n))
			simidpc-names))))
      (set! OLD-COMMENT-FLAG COMMENT-FLAG)
      (set! COMMENT-FLAG #f)
      (if (pair? opt-alg-names) (remove-alg-name (car opt-alg-names)))
      (remove-alg-name (idpredconst-name-to-nbe-alg-name idpc-name))
      (for-each remove-token simidpc-names)
      (set! COMMENT-FLAG OLD-COMMENT-FLAG)
      (set! IDS (list-transform-positive IDS
		  (lambda (x) (not (member (car x) simidpc-names)))))
      (for-each (lambda (x)
		  (comment
		   "ok, inductively defined predicate constant " x " removed"))
		simidpc-names)))
  (for-each (lambda (idpc-name)
	      (if (not (assoc idpc-name IDS))
		  (myerror "remove-idpc-name" "idpc name expected" idpc-name)))
	    x)
  (for-each rin1 x))

; append-hat appends ^ to every occurrence of name in string, where
; the following character is neither ^ nor alphabetic and the
; preceding character is not alphabetic.

(define (number-to-alphabetic-string i)
  (do ((charlist (reverse (string->list (number-to-string i))) (cdr charlist))
       (res '() (append (let ((char (car charlist)))
			  (cond ((char=? char #\0) (list #\Z #\e #\r #\o))
				((char=? char #\1) (list #\O #\n #\e)) 
				((char=? char #\2) (list #\T #\w #\o)) 
				((char=? char #\3) (list #\T #\h #\r #\e #\e)) 
				((char=? char #\4) (list #\F #\o #\u #\r))
				((char=? char #\5) (list #\F #\i #\v #\e))
				((char=? char #\6) (list #\S #\i #\x))
				((char=? char #\7) (list #\S #\e #\v #\e #\n))
				((char=? char #\8) (list #\E #\i #\g #\h #\t))
				((char=? char #\9) (list #\N #\i #\n #\e))
				(else (myerror "numeric char expected" char))))
			res)))
      ((null? charlist) (list->string res))))

(define (alphabetic-string-to-number string)
  (let ((l (string-length string)))
    (cond
     ((and (<= 4 l) (string=? "Zero" (substring string 0 4))) 0)
     ((and (<= 3 l) (string=? "One" (substring string 0 3))) 1)
     ((and (<= 3 l) (string=? "Two" (substring string 0 3))) 2)
     ((and (<= 5 l) (string=? "Three" (substring string 0 5))) 3)
     ((and (<= 4 l) (string=? "Four" (substring string 0 4))) 4)
     ((and (<= 4 l) (string=? "Five" (substring string 0 4))) 5)
     ((and (<= 3 l) (string=? "Six" (substring string 0 3))) 6)
     ((and (<= 5 l) (string=? "Seven" (substring string 0 5))) 7)
     ((and (<= 5 l) (string=? "Eight" (substring string 0 5))) 8)
     ((and (<= 4 l) (string=? "Nine" (substring string 0 4))) 9)
     (else
      (myerror "alphabetic-string-to-number" "unexpected string" string)))))

(define (strings-and-rest-to-numbers-and-rest string)
  (let ((l (string-length string)))
    (cond
     ((and (<= 4 l) (string=? "Zero" (substring string 0 4)))
      (cons 0 (strings-and-rest-to-numbers-and-rest (substring string 4 l))))
     ((and (<= 3 l) (string=? "One" (substring string 0 3)))
      (cons 1 (strings-and-rest-to-numbers-and-rest (substring string 3 l))))
     ((and (<= 3 l) (string=? "Two" (substring string 0 3)))
      (cons 2 (strings-and-rest-to-numbers-and-rest (substring string 3 l))))
     ((and (<= 5 l) (string=? "Three" (substring string 0 5)))
      (cons 3 (strings-and-rest-to-numbers-and-rest (substring string 5 l))))
     ((and (<= 4 l) (string=? "Four" (substring string 0 4)))
      (cons 4 (strings-and-rest-to-numbers-and-rest (substring string 4 l))))
     ((and (<= 4 l) (string=? "Five" (substring string 0 4)))
      (cons 5 (strings-and-rest-to-numbers-and-rest (substring string 4 l))))
     ((and (<= 3 l) (string=? "Six" (substring string 0 3)))
      (cons 6 (strings-and-rest-to-numbers-and-rest (substring string 3 l))))
     ((and (<= 5 l) (string=? "Seven" (substring string 0 5)))
      (cons 7 (strings-and-rest-to-numbers-and-rest (substring string 5 l))))
     ((and (<= 5 l) (string=? "Eight" (substring string 0 5)))
      (cons 8 (strings-and-rest-to-numbers-and-rest (substring string 5 l))))
     ((and (<= 4 l) (string=? "Nine" (substring string 0 4)))
      (cons 9 (strings-and-rest-to-numbers-and-rest (substring string 4 l))))
     (else (list string)))))

(define (constructor-name-to-i-and-idpredconst-name string)
  (let* ((numbers-and-rest (strings-and-rest-to-numbers-and-rest string))
	 (i (do ((l numbers-and-rest (cdr l))
		 (res 0 (if (integer? (car l))
			    (+ (* 10 res) (car l))
			    (myerror "integer expected" (car l)))))
		((or (string? (car l)) (null? l)) res)))
	 (name (car (last-pair numbers-and-rest))))
    (list i name)))