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
|
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/****************************************************************************/
/* */
/* File hopu.c. This file contains the main routines implementing the */
/* interpretive part of higher-order pattern unification. */
/* */
/****************************************************************************/
#ifndef HOPU_C
#define HOPU_C
#include "hopu.h"
#include "mctypes.h"
#include "dataformats.h"
#include "hnorm.h"
#include "abstmachine.h"
#include "types.h"
#include "trail.h"
#include "../system/error.h"
#include "../system/memory.h"
#include <stdio.h>
/* Unify types associated with constants. */
static void HOPU_typesUnify(DF_TypePtr tyEnv1, DF_TypePtr tyEnv2, int n)
{
AM_pdlError(2*n);
AM_initTypesPDL();
TY_pushPairsToPDL((MemPtr)tyEnv1, (MemPtr)tyEnv2, n);
TY_typesUnify();
}
/* Return the dereference of the abstraction body of the given term. */
DF_TermPtr HOPU_lamBody(DF_TermPtr tmPtr)
{
tmPtr = DF_termDeref(tmPtr);
while (DF_isLam(tmPtr)) tmPtr = DF_termDeref(DF_lamBody(tmPtr));
return tmPtr;
}
/***************************************************************************/
/* Globalize functions needed for HOPU_patternUnidyPair */
/***************************************************************************/
/* Globalize a rigid term. */
/* If the term pointer is not one referring to a heap address, the atomic */
/* content is then copied onto the current top of heap; the term pointer */
/* is updated to the new heap term. */
static DF_TermPtr HOPU_globalizeRigid(DF_TermPtr rPtr)
{
if (AM_nHeapAddr((MemPtr)rPtr)) {//rPtr must refer to const (no type), int,
//float, str, (stream), nil, cons
MemPtr nhreg = AM_hreg + DF_TM_ATOMIC_SIZE;
AM_heapError(nhreg);
DF_copyAtomic(rPtr, AM_hreg);
rPtr = (DF_TermPtr)AM_hreg;
AM_hreg = nhreg;
}
return rPtr;
}
/* Globalize a rigid term and make a variable binding. */
/* If the term pointer to the rigid term is not one referring to a heap */
/* address, its atomic content is then copied into the variable to be bound*/
/* Otherwise, the variable is made a reference to the rigid term. */
void HOPU_globalizeCopyRigid(DF_TermPtr rPtr, DF_TermPtr vPtr)
{
if (AM_nHeapAddr((MemPtr)rPtr)) //rPtr must refer to rigid atomic term
DF_copyAtomic(rPtr, (MemPtr)vPtr);
else DF_mkRef((MemPtr)vPtr, rPtr); //rPtr could also be app
}
/* Globalize a flex term. */
/* If the term pointer is one referring to a stack address, (in which case */
/* the flex term must be a free variable itself), the atomic content is */
/* copied onto the current top of heap; the free variable on stack is then */
/* bound to the new heap term, and the binding is trailed if necessary; the */
/* term pointer is updated to the new heap term. */
DF_TermPtr HOPU_globalizeFlex(DF_TermPtr fPtr)
{
if (AM_stackAddr((MemPtr)fPtr)) {//fPtr must be a reference to var
MemPtr nhreg = AM_hreg + DF_TM_ATOMIC_SIZE;
AM_heapError(nhreg);
DF_copyAtomic(fPtr, AM_hreg);
TR_trailETerm(fPtr);
DF_mkRef((MemPtr)fPtr, (DF_TermPtr)AM_hreg);
fPtr = (DF_TermPtr)AM_hreg;
AM_hreg = nhreg;
}
return fPtr;
}
/***************************************************************************/
/* Explicit eta expansion (on a rigid term) */
/***************************************************************************/
/* Eta expands a rigid term whose term pointer and decomposition are given */
/* by arguments. The new lambda body is returned. (It is unnecessary to */
/* create a new lambda term for the abstractions in the front of the eta */
/* expanded form. Note that the term head and argument vector are updated */
/* as side-effect. */
/* Note globalization on the term head is always performed and no */
/* specialized version of this function is provided based on the assumption*/
/* that explicit eta-expansion is rarely needed. */
static DF_TermPtr HOPU_etaExpand(DF_TermPtr *h, DF_TermPtr *args, int nargs,
int nabs)
{
DF_TermPtr hPtr = *h, oldArgs = *args, rtPtr;
MemPtr suspLoc; //where susps are to be created
int newArity = nargs + nabs;
if (DF_isBV(hPtr)){ //lift index by nabs if the head is a bound variable
int ind = DF_bvIndex(hPtr) + nabs;
AM_embedError(ind);
AM_heapError(AM_hreg + DF_TM_ATOMIC_SIZE);
*h = hPtr =(DF_TermPtr)AM_hreg; //update head pointer
DF_mkBV(AM_hreg,ind);
AM_hreg += DF_TM_ATOMIC_SIZE;
} else
//always perform globalization; eta expansion is rarely needed
*h = hPtr = HOPU_globalizeRigid(hPtr);
AM_arityError(newArity);
AM_heapError(AM_hreg + nargs * DF_TM_SUSP_SIZE + newArity*DF_TM_ATOMIC_SIZE
+ DF_TM_APP_SIZE);
suspLoc = AM_hreg;
AM_hreg += nargs * DF_TM_SUSP_SIZE; //allocate space for nargs suspensions
rtPtr = (DF_TermPtr)AM_hreg; //new application
DF_mkApp(AM_hreg, newArity, hPtr, (DF_TermPtr)(AM_hreg + DF_TM_APP_SIZE));
AM_hreg += DF_TM_APP_SIZE;
*args = (DF_TermPtr)AM_hreg; //update arg vector pointer
for (; nargs > 0; nargs--){//create suspensions over original arguments
DF_mkSusp(suspLoc, 0, nabs, DF_termDeref(oldArgs), DF_EMPTY_ENV);
DF_mkRef(AM_hreg, (DF_TermPtr)suspLoc);
suspLoc += DF_TM_SUSP_SIZE; AM_hreg += DF_TM_ATOMIC_SIZE;
oldArgs = (DF_TermPtr)(((MemPtr)oldArgs) + DF_TM_ATOMIC_SIZE);
}
for (; nabs > 0; nabs--){//create de Bruijn indices from #nabs to #1
DF_mkBV(AM_hreg, nabs);
AM_hreg += DF_TM_ATOMIC_SIZE;
}
return rtPtr;
}
/***************************************************************************/
/* PATTERN RECOGNITION */
/* */
/* Auxiliary functions for recognizing LLambda pattens for flexible terms. */
/***************************************************************************/
/* Whether a bound variable occurs in the given arguments. */
/* It is assumned that the given arguments can only contain bound variables*/
/* and constants. */
static Boolean HOPU_uniqueBV(int bvInd, DF_TermPtr args, int n)
{
DF_TermPtr tPtr;
for ( ; n > 0 ; n-- ){
tPtr = DF_termDeref(args);
if (DF_isBV(tPtr) && (bvInd == DF_bvIndex(tPtr))) return FALSE;
//otherwise different bv or constant, check the next
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
}
return TRUE;
}
/* Whether a constant occurs in the given arguments. */
/* It is assumned that the given arguments can only contain bound variables*/
/* and constants. */
/* CHANGES have to be made here if the semantics of local constants are */
/* changed with respect to polymorphism. */
static Boolean HOPU_uniqueConst(DF_TermPtr cPtr, DF_TermPtr args, int n)
{
DF_TermPtr tPtr;
for ( ; n > 0 ; n--){
tPtr = DF_termDeref(args);
if (DF_isConst(tPtr) && DF_sameConsts(tPtr, cPtr)) {
if (DF_isTConst(tPtr)) {
EM_TRY {
HOPU_typesUnify(DF_constType(tPtr), DF_constType(cPtr),
AM_cstTyEnvSize(DF_constTabIndex(cPtr)));
} EM_CATCH {
if (EM_CurrentExnType == EM_FAIL) {
AM_resetTypesPDL();//remove tys from pdl for ty unif
return FALSE;
} else EM_RETHROW();
}
} else return FALSE;
} //otherwise different constant or bv, check the next
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
} //for loop
return TRUE;
}
/* Checking whether the argments of the head normal form given by registers*/
/* AM_argVec, AM_numArgs and AM_numAbs are those of an eta-expanded form. */
/* Specifically, the arguments are attempted to match de Bruijn indices */
/* #n ... #1, where n is the current value of AM_numAbs. */
/* It is assumed that the argument vector is not empty. */
static Boolean HOPU_isEtaExpArgs()
{
if (AM_numArgs != AM_numAbs) return FALSE;
else {
int i = AM_numAbs;
Boolean match = TRUE;
DF_TermPtr oneArg = AM_argVec;
DF_TermPtr head = AM_head;
while (match && (i > 0)){
HN_hnorm(oneArg);
if (AM_numArgs == 0)
match = ((AM_numArgs == 0) && DF_isBV(AM_head) &&
(DF_bvIndex(AM_head) == i));
else
match = (DF_isBV(AM_head) && (DF_bvIndex(AM_head)-AM_numAbs==i)
&& HOPU_isEtaExpArgs());
oneArg = (DF_TermPtr)(((MemPtr)oneArg + DF_TM_ATOMIC_SIZE));
i--;
}
AM_head = head;
return match;
}
}
/* Checking whether the arguments of a flexible term satisfy with the */
/* LLambda pattern with respect to the universe count of its flex head. */
/* CHANGES have to be made here if the semantics of local constants are */
/* changed with respect to polymorphism. */
static Boolean HOPU_isLLambda(int uc, int nargs, DF_TermPtr args)
{
if (nargs == 0) return TRUE;
else {
int i;
DF_TermPtr myArgs = args;
for (i = 0; i < nargs; i++){
HN_hnorm(args);
if (AM_numArgs == 0) {
if (AM_numAbs != 0) return FALSE; //abstraction
if (DF_isBV(AM_head)) { //bound variable
if (!HOPU_uniqueBV(DF_bvIndex(AM_head), myArgs, i))
return FALSE;
} else if (DF_isConst(AM_head)) { //constant
if (!(uc < DF_constUnivCount(AM_head) &&
HOPU_uniqueConst(AM_head, myArgs, i))) return FALSE;
} else return FALSE; //other sort of terms
} else { //AM_numArgs != 0
if (DF_isBV(AM_head)) { //bound variable head
int dbInd = DF_bvIndex(AM_head) - AM_numAbs; //eta-norm
if (dbInd > 0 && HOPU_uniqueBV(dbInd, myArgs, i) &&
HOPU_isEtaExpArgs()) {
TR_trailHTerm(args);
DF_mkBV((MemPtr)args, dbInd);
} else return FALSE;
} else { //!(DF_isBV(AM_head))
if (DF_isConst(AM_head)) { //constant head
if (uc < DF_constUnivCount(AM_head) &&
HOPU_uniqueConst(AM_head, myArgs, i) &&
HOPU_isEtaExpArgs()) {
TR_trailHTerm(args);
if (DF_isTConst(AM_head))
DF_mkRef((MemPtr)args, AM_head);
else DF_copyAtomic(AM_head, (MemPtr)args);
} else return FALSE;
} else return FALSE; //other sort of terms
} //!(DF_isBV(AM_head))
} //AM_numArgs != 0
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
} //for loop
return TRUE;
} //nargs != 0
}
/***************************************************************************/
/* BINDING */
/* */
/* Attempt to find bindings for free variables (counter part of mksubst in */
/* the sml pattern unfication code). */
/***************************************************************************/
/* A flag denoting whether new structure is created during the process of */
/* finding substitutions. */
Boolean HOPU_copyFlagGlb = FALSE;
/* Return a non-zero index of a bound variable appears in a list of */
/* arguments. Note the index is the position from the right and the */
/* embedding level is taken into account. */
static int HOPU_bvIndex(int dbInd, DF_TermPtr args, int nargs, int lev)
{
int ind;
dbInd -= lev;
for (ind = nargs; ind > 0; ind--){
DF_TermPtr tPtr = DF_termDeref(args);
if (DF_isBV(tPtr) && (dbInd == DF_bvIndex(tPtr))) return (ind+lev);
//otherwise try the next
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
}
return 0; //not found
}
/* Return a non-zero index if a constant appears in a list of arguments. */
/* Note the index is the position from the right and the embedding level */
/* is taken into account. */
/* CHANGES have to be made here if the semantics of local constants are */
/* changed with respect to polymorphism. */
static int HOPU_constIndex(DF_TermPtr cPtr, DF_TermPtr args, int nargs, int lev)
{
int ind;
for (ind = nargs; ind > 0; ind--){
DF_TermPtr tPtr = DF_termDeref(args);
if (DF_isConst(tPtr) && DF_sameConsts(tPtr, cPtr)) {
if (DF_isTConst(tPtr)) {
Boolean found = FALSE;
EM_TRY {
HOPU_typesUnify(DF_constType(tPtr), DF_constType(cPtr),
AM_cstTyEnvSize(DF_constTabIndex(cPtr)));
found = TRUE;
} EM_CATCH {//remove types added for ty unif from the PDL
if (EM_CurrentExnType == EM_FAIL) AM_resetTypesPDL();
else EM_RETHROW();
}
if (found) return (ind+lev);
} else return (ind+lev); //cPtr does not have type associated
} //otherwise try the next
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
}
return 0; //not found
}
/***************************************************************************/
/* BINDING FOR FLEX-FLEX */
/* */
/* Auxiliary functions for solving flex-flex pairs. */
/* Non-LLambda pairs are delayed onto the disagreement list. */
/***************************************************************************/
/* Collect raising components for internal variable in the LLambda case */
/* when it is known it has a higher universe index than the outside llambda*/
/* variable. */
/* It is assumned that the incoming argument vector has a size larger than */
/* zero. */
/* As a result of this process, segments of the argument vectors for both */
/* variables are decided. That for the internal variable is created on the */
/* current top of heap, while that for the outside variable, each */
/* argument of which must be a de Bruijn index, is recorded into an integer*/
/* array which is set by side-effect. */
/* The number returned by this procedure is the length of both of the */
/* argument vector segements. Raising occured when this number is non-zero.*/
/* CHANGES have to be made here if the semantics of local constants are */
/* changed with respect to polymorphism. */
static int HOPU_raise(int varuc, DF_TermPtr args, int nargs, int emblev,
int *args11)
{
int numRaised = 0; //number of args that have been raised
AM_heapError(AM_hreg + nargs * DF_TM_ATOMIC_SIZE);//max possible size
for (; nargs > 0; nargs--){
DF_TermPtr tmPtr = DF_termDeref(args);
if (DF_isConst(tmPtr) && (DF_constUnivCount(tmPtr) <= varuc)){
args11[numRaised] = nargs + emblev; //args11
if (DF_isTConst(tmPtr)) DF_mkRef(AM_hreg, tmPtr); //args21
else DF_copyAtomic(tmPtr, AM_hreg);
AM_hreg += DF_TM_ATOMIC_SIZE;
numRaised++;
}
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
}
return numRaised;
}
/* Generate the indices for items not to be pruned when the internal */
/* variable is known to have a universe index greater than that of the */
/* external one. */
/* It is assumned that arg vector of the internal flex term has a size */
/* larger than zero. */
/* As a result of this process, segments of the argument vectors for both */
/* variables are decided. That for the internal variable is created on the */
/* current top of heap, while that for the outside variable, each */
/* argument of which must be a de Bruijn index, is recorded into an integer*/
/* array which is set by side-effect. */
/* The number returned by this procedure is the length of both of the */
/* argument vector segements. Pruning occured when this number is smaller */
/* than the size of the arg vector of the internal term. */
static int HOPU_prune(DF_TermPtr args1, int nargs1, DF_TermPtr args2,
int nargs2, int emblev, int *args12)
{
int numNotPruned = 0;
AM_heapError(AM_hreg + nargs2 * DF_TM_ATOMIC_SIZE);//max possible size
for (; nargs2 > 0; nargs2--){
DF_TermPtr tmPtr = DF_termDeref(args2);
if (DF_isConst(tmPtr)) {
int ind = HOPU_constIndex(tmPtr, args1, nargs1, emblev);
if (ind > 0) {
args12[numNotPruned] = ind; //args12
DF_mkBV(AM_hreg, nargs2); //args22
AM_hreg += DF_TM_ATOMIC_SIZE;
numNotPruned ++;
HOPU_copyFlagGlb = TRUE;
} //ind == 0 the argument is pruned
} else {//bv
int ind = DF_bvIndex(tmPtr);
if (ind > emblev) {
int newind = HOPU_bvIndex(ind, args1, nargs1, emblev);
if (newind > 0) {
args12[numNotPruned] = newind; //args12
DF_mkBV(AM_hreg, nargs2); //args22
AM_hreg += DF_TM_ATOMIC_SIZE;
numNotPruned ++;
if (ind != newind) HOPU_copyFlagGlb = TRUE;
} //newind == 0 the argument is pruned
} else {//ind <= lev
args12[numNotPruned] = ind; //args12
DF_mkBV(AM_hreg, nargs2); //args22
AM_hreg += DF_TM_ATOMIC_SIZE;
numNotPruned ++;
}
} //bv
args2 = (DF_TermPtr)(((MemPtr)args2) + DF_TM_ATOMIC_SIZE);
} //for loop
return numNotPruned;
}
/* When the index of the internal variable is less than or equal to that */
/* of the external one in the LLambda case, we have to raise the outside */
/* variable over those constants in the internal list that have smaller */
/* index and we have to prune other constants and bound variables in this */
/* list that are not shared. */
/* It is assumned that the arg vector of the internal flex term has a size */
/* larger than zero. */
/* As a result of this process, the argument vectors for both variables */
/* are decided. That for the outside variable is created on the current */
/* top of heap, while that for the internal variable, each argument of */
/* which must be a de Bruijn index, is recorded into an integer array which*/
/* is set by side-effect. */
/* The number returned by this procedure is the length of both of the */
/* argument vectors. Pruning occured when this number is smaller than the */
/* size of the arg vector of the internal term. */
/* CHANGES have to be made here if the semantics of local constants are */
/* changed with respect to polymorphism. */
static int HOPU_pruneAndRaise(DF_TermPtr args1, int nargs1, DF_TermPtr args2,
int nargs2, int emblev, int *args)
{
int numNotPruned = 0;
AM_heapError(AM_hreg + nargs2 * DF_TM_ATOMIC_SIZE); //max possible size
for (; nargs2 > 0; nargs2 --){
DF_TermPtr tmPtr = DF_termDeref(args2);
if (DF_isBV(tmPtr)){
int ind = DF_bvIndex(tmPtr);
if (ind > emblev) {
int newind = HOPU_bvIndex(ind, args1, nargs1, emblev);
if (newind > 0) {
DF_mkBV(AM_hreg, newind); //args for outside var
AM_hreg += DF_TM_ATOMIC_SIZE;
args[numNotPruned] = nargs2; //args for internal var
numNotPruned ++;
if (ind != newind) HOPU_copyFlagGlb = TRUE;
} // newind == 0, the argument is prubed
} else { //ind <= emblev
DF_mkBV(AM_hreg, ind); //args for outside var
AM_hreg += DF_TM_ATOMIC_SIZE;
args[numNotPruned] = nargs2; //args for internal var
numNotPruned ++;
}
} else { //tmPtr is const
if (DF_constUnivCount(tmPtr) > AM_adjreg){
int ind = HOPU_constIndex(tmPtr, args1, nargs1, emblev);
if (ind > 0) {
DF_mkBV(AM_hreg, ind); //args for outside var
AM_hreg += DF_TM_ATOMIC_SIZE;
args[numNotPruned] = nargs2; //args for internal var
numNotPruned ++;
HOPU_copyFlagGlb = TRUE;
} //else ind = 0, the argument is pruned
} else { //const uc <= AM_adjreg
if (DF_isTConst(tmPtr)) DF_mkRef(AM_hreg, tmPtr);//args out var
else DF_copyAtomic(tmPtr, AM_hreg);
AM_hreg += DF_TM_ATOMIC_SIZE;
args[numNotPruned] = nargs2; //args for internal var
numNotPruned ++;
}
}
args2 = (DF_TermPtr)(((MemPtr)args2) + DF_TM_ATOMIC_SIZE);
} //for loop
return numNotPruned;
}
/* Generating the arguments of a pruning substitution for the case when */
/* when trying to unify two flexible terms of the form */
/* (F a1 ... an) = lam(k, (F b1 ... bm)) */
/* The resulted argument vector is created on the current top of heap, and */
/* the integer returned by this procedure is the length of the argument */
/* vector resulted from pruning. Pruning takes place if this value is */
/* smaller that nargs2. */
/* It is assumed that the sum of n and k is the same as m. */
/* CHANGES have to be made here if the semantics of local constants are */
/* changed with respect to polymorphism. */
static int HOPU_pruneSameVar(DF_TermPtr args1, int nargs1, DF_TermPtr args2,
int nargs2, int lev)
{
if (nargs2 == 0) return 0;
else {
int numNotPruned = 0;
DF_TermPtr tPtr2;
AM_heapError(AM_hreg + nargs2 * DF_TM_ATOMIC_SIZE); //max possible size
nargs1 = nargs2 - nargs1; //reused nargs1
for (; nargs2 > nargs1; nargs2 --){
DF_TermPtr tPtr1 = DF_termDeref(args1);
tPtr2 = DF_termDeref(args2);
if (DF_isBV(tPtr1)){
int ind = DF_bvIndex(tPtr1) + lev;
if (DF_isBV(tPtr2) && (ind == DF_bvIndex(tPtr2))){
DF_mkBV(AM_hreg, nargs2); AM_hreg += DF_TM_ATOMIC_SIZE;
numNotPruned++;
if (nargs2 != ind) HOPU_copyFlagGlb = TRUE;
} //else this argument is pruned
} else {// tPtr1 is a constant
if (DF_isConst(tPtr2) && DF_sameConsts(tPtr1, tPtr2)){
if (DF_isTConst(tPtr2)) {
EM_TRY {
HOPU_typesUnify(DF_constType(tPtr1),DF_constType(tPtr2),
AM_cstTyEnvSize(DF_constTabIndex(tPtr1)));
DF_mkBV(AM_hreg, nargs2); AM_hreg += DF_TM_ATOMIC_SIZE;
numNotPruned++;
HOPU_copyFlagGlb = TRUE;
} EM_CATCH {//remove tys for type unif from the PDL
if (EM_CurrentExnType == EM_FAIL)
AM_resetTypesPDL();
else EM_RETHROW();
} //EM_catch
} else {//no type association
DF_mkBV(AM_hreg, nargs2); AM_hreg+=DF_TM_ATOMIC_SIZE;
numNotPruned++;
HOPU_copyFlagGlb = TRUE;
}
}//else pruned
} //tPtr1 is a constant
args1 = (DF_TermPtr)(((MemPtr)args1) + DF_TM_ATOMIC_SIZE);
args2 = (DF_TermPtr)(((MemPtr)args2) + DF_TM_ATOMIC_SIZE);
} //for (; nargs2 > nargs1; nargs2--)
for (; nargs2 > 0; nargs2--){
tPtr2 = DF_termDeref(args2);
if (DF_isBV(tPtr2) && (DF_bvIndex(tPtr2) == nargs2)){
DF_mkBV(AM_hreg, nargs2); AM_hreg += DF_TM_ATOMIC_SIZE;
numNotPruned++;
} //else pruned
args2 = (DF_TermPtr)(((MemPtr)args2) + DF_TM_ATOMIC_SIZE);
} //for (; nargs2 > 0; nargs2--)
return numNotPruned;
} //nargs2 != 0
}
/* Push a new free variable with given universe count onto the current heap */
/* top. */
static void HOPU_pushVarToHeap(int uc)
{
MemPtr newhtop = AM_hreg + DF_TM_ATOMIC_SIZE;
AM_heapError(newhtop);
DF_mkVar(AM_hreg, uc);
AM_hreg = newhtop;
}
/* Perform substitution to realize pruning and raising for an internal */
/* variable in the LLambda situation when the variable has an index greater*/
/* than that of the outside one */
/* This procedure is also used to perform substitution for flex-flex pairs */
/* with same variable heads in the LLambda situation. */
static void HOPU_mkPandRSubst(DF_TermPtr hPtr, DF_TermPtr args, int nargs,
DF_TermPtr vPtr, int nabs)
{
TR_trailTerm(vPtr); AM_bndFlag = ON;
if (nargs == 0) {
if (nabs == 0) DF_mkRef((MemPtr)vPtr, hPtr);
else DF_mkLam((MemPtr)vPtr, nabs, hPtr);
} else { //nargs > 0
DF_TermPtr tPtr = (DF_TermPtr)AM_hreg;
AM_heapError(AM_hreg + DF_TM_APP_SIZE);
AM_arityError(nargs);
DF_mkApp(AM_hreg, nargs, hPtr, args); //application body
AM_hreg += DF_TM_APP_SIZE;
AM_embedError(nabs);
if (nabs == 0) DF_mkRef((MemPtr)vPtr, tPtr);
else DF_mkLam((MemPtr)vPtr, nabs, tPtr);
}
}
/* Perform substitution to realize pruning and raising for an internal */
/* variable in the LLambda situation when the variable has an index smaller*/
/* than or equal to that of the outside one */
/* The arguments of the substitution which should be de Bruijn indices */
/* are given by an integer array. */
static void HOPU_mkPrunedSubst(DF_TermPtr hPtr, int *args, int nargs,
DF_TermPtr vPtr, int nabs)
{
AM_bndFlag = ON;
TR_trailTerm(vPtr);
if (nargs == 0) {
if (nabs == 0) DF_mkRef((MemPtr)vPtr, hPtr);
else DF_mkLam((MemPtr)vPtr, nabs, hPtr);
} else { //nargs > 0;
DF_TermPtr argvec = (DF_TermPtr)AM_hreg, appPtr;
int i;
AM_heapError(AM_hreg + DF_TM_APP_SIZE + nargs * DF_TM_ATOMIC_SIZE);
for (i = 0; i < nargs; i++){//commit bvs in args onto heap
DF_mkBV(AM_hreg, args[i]);
AM_hreg += DF_TM_ATOMIC_SIZE;
}
appPtr = (DF_TermPtr)AM_hreg;
DF_mkApp(AM_hreg, nargs, hPtr, argvec);
AM_hreg += DF_TM_APP_SIZE;
if (nabs == 0) DF_mkRef((MemPtr)vPtr, appPtr);
else DF_mkLam((MemPtr)vPtr, nabs, appPtr);
}
}
/* Generating the partial structure of a substitution to realize pruning */
/* and raising for an outside variable in the LLambda situation when the */
/* variable has an index smaller than that of the internal one. */
/* The arguments of the susbsitution consists of two segments of de Burijn */
/* indices, which are given by two integer arrays respectively. */
static DF_TermPtr HOPU_mkPandRTerm(DF_TermPtr hPtr, int args1[], int nargs1,
int args2[], int nargs2)
{
if ((nargs1 == 0) && (nargs2 == 0)) return hPtr;
else {
DF_TermPtr args = (DF_TermPtr)AM_hreg, rtPtr;
int nargs = nargs1 + nargs2; //new arity (non-zero)
int i;
AM_arityError(nargs);
AM_heapError(AM_hreg + DF_TM_APP_SIZE + nargs * DF_TM_ATOMIC_SIZE);
for (i = 0; i < nargs1 ; i++){ //commit bvs in a11 onto heap
DF_mkBV(AM_hreg, args1[i]);
AM_hreg += DF_TM_ATOMIC_SIZE;
}
for (i = 0; i < nargs2 ; i++){ //commit bvs in a12 onto heap
DF_mkBV(AM_hreg, args2[i]);
AM_hreg += DF_TM_ATOMIC_SIZE;
}
rtPtr = (DF_TermPtr)AM_hreg;
DF_mkApp(AM_hreg, nargs, hPtr, args);
AM_hreg += DF_TM_APP_SIZE;
return rtPtr;
}
}
/* Generating the partial structure of a substitution to realize pruning */
/* and raising for an internal variable in the LLambda situation when the */
/* variable has an index greater than or equal to that of the outside one. */
static DF_TermPtr HOPU_mkPrunedTerm(DF_TermPtr hPtr, DF_TermPtr args, int nargs)
{
if (nargs == 0) return hPtr;
else {
DF_TermPtr rtPtr = (DF_TermPtr)AM_hreg;
AM_heapError(AM_hreg + DF_TM_APP_SIZE);
DF_mkApp(AM_hreg, nargs, hPtr, args);
AM_hreg += DF_TM_APP_SIZE;
return rtPtr;
}
}
/* Find the (partial) structure of the substitution for a flex head of a */
/* LLambda term corresponding to an internal flex term which is known to be */
/* LLambda. The internal free variable is bound to a proper substitution as */
/* side-effect. */
/* The arguments of this procedure are: */
/* args1 : reference to the argument vector of the outside flex term */
/* nargs1: number of arguments of the outside flex term */
/* uc : universe count of the internal free variable */
/* tPtr2 : refers to the dereference of ABSTRACTION BODY of the internal */
/* flex term */
/* fhPtr : refers to the head of the internal flex term */
/* args2 : refers to the argument vector of the internal flex term */
/* nargs2: number of arguments of the internal flex term */
/* lev : the abstraction context of the internal flex term */
/* Note that the outside free variable and its universe count are assumed to */
/* be given by the global variables (registers) AM_vbbreg and AM_adjreg. */
static DF_TermPtr HOPU_flexNestedLLambda(DF_TermPtr args1, int nargs1, int uc,
DF_TermPtr tPtr2, DF_TermPtr fhPtr, DF_TermPtr args2,
int nargs2, int lev)
{
DF_TermPtr bnd; //(partial) binding for the outside free var
MemPtr oldhtop = AM_hreg;
DF_TermPtr heapArgs = (DF_TermPtr)AM_hreg;
if (AM_adjreg < uc){
int *args11 = NULL, *args12 = NULL; //hold args of bnd of the outside v
int nargs11 = 0, nargs12 = 0;
if (nargs1 != 0) {
args11 = (int*)EM_malloc(nargs1 * sizeof(int));
nargs11 = HOPU_raise(uc, args1, nargs1, lev, args11);
}
if (nargs2 != 0) {
args12 = (int*)EM_malloc(nargs2 * sizeof(int));
nargs12 = HOPU_prune(args1, nargs1, args2, nargs2, lev, args12);
}
if ((nargs11 == 0) && (nargs12 == nargs2)) {//neither raised nor pruned
AM_hreg = oldhtop; //the internal free var remains unbound
TR_trailTerm(fhPtr); AM_bndFlag = ON;
DF_modVarUC(fhPtr, AM_adjreg);
if (HOPU_copyFlagGlb)
bnd = HOPU_mkPandRTerm(fhPtr, args11, nargs11, args12, nargs12);
else bnd = tPtr2;
} else { //raised or pruned
DF_TermPtr newVar = (DF_TermPtr)AM_hreg;
HOPU_pushVarToHeap(AM_adjreg);
HOPU_mkPandRSubst(newVar, heapArgs, nargs11+nargs12, fhPtr, nargs2);
bnd = HOPU_mkPandRTerm(newVar, args11, nargs11, args12, nargs12);
HOPU_copyFlagGlb = TRUE;
}
if (nargs1 != 0) free(args11); if (nargs2 != 0) free(args12);
} else { //AM_adjreg >= uc
int *newargs2 = NULL;
int nnewargs2 = 0;
if (nargs2 != 0) {
newargs2 = (int*)EM_malloc(nargs2 * sizeof(int));
nnewargs2 = HOPU_pruneAndRaise(args1,nargs1,args2,nargs2,lev,
newargs2);
}
if (nnewargs2 == nargs2){//not pruned
if (HOPU_copyFlagGlb)
bnd = HOPU_mkPrunedTerm(fhPtr, heapArgs, nnewargs2);
else { AM_hreg = oldhtop; bnd = tPtr2; }
} else { //pruned
DF_TermPtr newVar = (DF_TermPtr)AM_hreg;
HOPU_pushVarToHeap(uc);
HOPU_mkPrunedSubst(newVar, newargs2, nnewargs2, fhPtr, nargs2);
bnd = HOPU_mkPrunedTerm(newVar, heapArgs, nnewargs2);
HOPU_copyFlagGlb = TRUE;
}
if (nargs2 != 0) free(newargs2);
} //AM_adjreg >= uc
return bnd;
}
/* Checking the arguments of a flex (non-LLambda) term to see whetehr a */
/* free variable same as that currently in the AM_vbbreg register, a free */
/* variable with higher univ count than that currently in the AM_adjreg */
/* register, a constant with higher univ count than that currently in */
/* AM_adjreg, or a de Bruijn index bound by abstractions over the variable */
/* for which a substitution is being constructed occurs. */
/* If one of the situations occurs, exception is raised. */
static void HOPU_flexCheck(DF_TermPtr args, int nargs, int emblev)
{
for (; nargs > 0; nargs --){
int nemblev;
HN_hnorm(args);
nemblev = emblev + AM_numAbs;
if (AM_rigFlag){
if (DF_isBV(AM_head)) {
if (DF_bvIndex(AM_head) > nemblev) EM_THROW(EM_FAIL);
} else {
if (DF_isConst(AM_head)&&(DF_constUnivCount(AM_head)>AM_adjreg))
EM_THROW(EM_FAIL);
} //otherwise succeeds
} else { //AM_rigFlag == FALSE
if ((AM_vbbreg == AM_head) || (DF_fvUnivCount(AM_head)>AM_adjreg))
EM_THROW(EM_FAIL);
}
HOPU_flexCheck(AM_argVec, AM_numArgs, nemblev);
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
}
}
/* This version of flexCheckC is needed in the compiled form of pattern */
/* unification. The essential difference from the other version is that the */
/* variable being bound is already partially bound to a structure. */
/* The difference from the other procedure is the head normalization */
/* procedure invoked is one performs the occurs checking on partially bound */
/* variables */
static void HOPU_flexCheckC(DF_TermPtr args, int nargs, int emblev)
{
for (; nargs > 0; nargs--){
int nemblev;
HN_hnormOcc(args);
nemblev = emblev + AM_numAbs;
if (AM_rigFlag) {
if (DF_isBV(AM_head)) {
if (DF_bvIndex(AM_head) > nemblev) EM_THROW(EM_FAIL);
} else {
if (DF_isConst(AM_head)&&(DF_constUnivCount(AM_head)>AM_adjreg))
EM_THROW(EM_FAIL);
} //otherwise succeeds
} else //AM_rigFlag == FALSE
if (DF_fvUnivCount(AM_head) > AM_adjreg) EM_THROW(EM_FAIL);
HOPU_flexCheckC(AM_argVec, AM_numArgs, nemblev);
args = (DF_TermPtr)(((MemPtr)args)+DF_TM_ATOMIC_SIZE);
}
}
/* Generating a term on the top of heap which is to be added into a */
/* disagreement pair. */
/* The term has the following structure: */
/* (h [|a1, 0, lev, nil|] ... [|an, 0, lev, nil|] #lev ... #1) */
/* It is assumed that nargs and lev are not equal to zero. */
static void HOPU_mkTermNLL(DF_TermPtr h, DF_TermPtr args, int nargs, int lev)
{
int newArity = nargs + lev;
MemPtr newArgs = AM_hreg + DF_TM_APP_SIZE; //spare app (head) size on heap
AM_arityError(newArity);
AM_heapError(AM_hreg + nargs*DF_TM_SUSP_SIZE + newArity*DF_TM_ATOMIC_SIZE
+ DF_TM_APP_SIZE);
DF_mkApp(AM_hreg, newArity, h, (DF_TermPtr)newArgs);
AM_hreg += (DF_TM_APP_SIZE + newArity * DF_TM_ATOMIC_SIZE);//alloc arg vec
for (; nargs > 0; nargs--){ //[|ai, 0, lev, nil|], for i <= nargs
DF_mkRef(newArgs, (DF_TermPtr)AM_hreg);
DF_mkSusp(AM_hreg, 0, lev, DF_termDeref(args), DF_EMPTY_ENV);
newArgs += DF_TM_ATOMIC_SIZE; AM_hreg += DF_TM_SUSP_SIZE;
args = (DF_TermPtr)(((MemPtr)args) + DF_TM_ATOMIC_SIZE);
}
for (; lev > 0; lev--){ //#i, for i <= lev
DF_mkBV(newArgs, lev);
newArgs += DF_TM_ATOMIC_SIZE;
}
}
/* Generating a partial subsitution for the free head of a LLambda term */
/* corresponding to an internal flex term which is known to be non-LLambda.*/
/* The partial substitution is of form: */
/* (h #n ... #1) */
/* It is assumed that n is not equal to zero. */
static void HOPU_mkSubstNLL(DF_TermPtr h, int n)
{
AM_arityError(n);
AM_heapError(AM_hreg + DF_TM_APP_SIZE + n * DF_TM_ATOMIC_SIZE);
DF_mkApp(AM_hreg, n, h, (DF_TermPtr)(AM_hreg + DF_TM_APP_SIZE));
AM_hreg += DF_TM_APP_SIZE;
for (; n > 0; n--){
DF_mkBV(AM_hreg, n);
AM_hreg += DF_TM_ATOMIC_SIZE;
}
}
/* Try to solve G = ...(F a1 ... an)..., where F and G are different free */
/* variables, and (F a1 ... an) is non-LLambda. */
/* Either G is bound to (F a1 ... an) or an exception is raised. In the */
/* latter case, the caller of this function is responsible to add a */
/* disagreement pair to the live list. */
static void HOPU_bndVarNestedFlex(DF_TermPtr fhPtr, DF_TermPtr args, int nargs,
int lev)
{
HOPU_flexCheck(args, nargs, lev);
if (DF_fvUnivCount(fhPtr) > AM_adjreg) {
TR_trailTerm(fhPtr);
AM_bndFlag = ON;
DF_modVarUC(fhPtr, AM_adjreg);
}
}
/* Try to find the (partial) structure of the substitution for a flex head */
/* of a LLambda term corresponding to an internal flex term which is not */
/* known to be LLambda. */
/* If the internal flex term is LLambda, HOPU_flexNestedLLambda is invoked */
/* to generate the (parital) substitution for the outside variable, and */
/* perform proper substitutions on the internal free variable if necessary. */
/* Otherwise, a disagreement pair is added into the live list. */
static DF_TermPtr HOPU_flexNestedSubst(DF_TermPtr args1, int nargs1,
DF_TermPtr fhPtr, DF_TermPtr args2,
int nargs2, DF_TermPtr tmPtr, int emblev)
{
DF_TermPtr bnd;
int varuc = DF_fvUnivCount(fhPtr);
if (HOPU_isLLambda(varuc, nargs2, args2)){
if (fhPtr == AM_vbbreg) EM_THROW(EM_FAIL); //occurs check
bnd = HOPU_flexNestedLLambda(args1, nargs1, varuc, tmPtr, fhPtr, args2,
nargs2, emblev);
} else {// the internal flex term is not LLambda: delay (opt possible)
DF_TermPtr newVar;
DF_TermPtr newTerm;
Boolean found = FALSE;
if ((fhPtr != AM_vbbreg) && (nargs1 == 0)) {
EM_TRY{
HOPU_bndVarNestedFlex(fhPtr, args2, nargs2, emblev);
bnd = tmPtr;
found = TRUE;
} EM_CATCH {if (EM_CurrentExnType != EM_FAIL) EM_RETHROW();}
}
if (!found) {
newVar = (DF_TermPtr)AM_hreg;
HOPU_pushVarToHeap(AM_adjreg);
HOPU_copyFlagGlb = TRUE;
if ((nargs1 == 0) && (emblev == 0)) {
bnd = newVar;
AM_addDisPair(bnd, tmPtr);
} else {
newTerm = (DF_TermPtr)AM_hreg;
HOPU_mkTermNLL(newVar, args1, nargs1, emblev);
AM_addDisPair(newTerm, tmPtr);
bnd = (DF_TermPtr)AM_hreg;
HOPU_mkSubstNLL(newVar, emblev + nargs1);
}
}
}
return bnd;
}
/* This version of flexNestedSubst is needed in the compiled form of pattern */
/* unification. The essential difference from the other version is that the */
/* variable being bound is already partially bound to a structure. */
/* The difference from the other procedure is first the head normalization */
/* process invokded is one performs occurs checking on partially bound */
/* variables, and second, the "top-level" flexible term is a free variable: */
/* so there is no need to distinguish whether the other flex term is Llambda */
/* or not: the substitution can be found by an invocation of flexCheckC */
DF_TermPtr HOPU_flexNestedSubstC(DF_TermPtr fhPtr, DF_TermPtr args, int nargs,
DF_TermPtr tmPtr, int emblev)
{
DF_TermPtr bnd, newVar, newTerm;
int varuc;
Boolean found = FALSE;
EM_TRY {
HOPU_flexCheckC(args, nargs, emblev);
if (DF_fvUnivCount(fhPtr) > AM_adjreg){
TR_trailTerm(fhPtr);
AM_bndFlag = ON;
DF_modVarUC(fhPtr, AM_adjreg);
}
bnd = tmPtr;
found = TRUE;
} EM_CATCH { if (EM_CurrentExnType != EM_FAIL) EM_RETHROW(); }
if (!found) {
varuc = DF_fvUnivCount(fhPtr);
if (HOPU_isLLambda(varuc, nargs, args)){
bnd = HOPU_flexNestedLLambda(NULL, 0, varuc, tmPtr, fhPtr, args, nargs,
emblev);
} else {//otherwise delay this pair onto the live list
HOPU_copyFlagGlb = TRUE;
newVar = (DF_TermPtr)AM_hreg;
HOPU_pushVarToHeap(AM_adjreg);
if (emblev == 0) {
bnd = newVar;
AM_addDisPair(bnd, tmPtr);
} else {
newTerm = (DF_TermPtr)AM_hreg;
HOPU_mkTermNLL(newVar, NULL, 0, emblev);
AM_addDisPair(newTerm, tmPtr);
bnd = (DF_TermPtr)AM_hreg;
HOPU_mkSubstNLL(newVar, emblev);
}
}
}
return bnd;
}
/* Try to solve G = (F a1 ... an), where F and G are different free */
/* variables, and (F a1 ... an) is non-LLambda. */
/* Either G is bound to (F a1 ... an) or an exception is raised. In the */
/* latter case, the caller of this function is responsible to add a */
/* disagreement pair to the live list. */
static void HOPU_bndVarFlex(DF_TermPtr vPtr, DF_TermPtr fPtr, DF_TermPtr fhPtr,
DF_TermPtr args, int nargs)
{
AM_vbbreg = vPtr; AM_adjreg = DF_fvUnivCount(vPtr);
HOPU_flexCheck(args, nargs, 0);
if (DF_fvUnivCount(fhPtr) > AM_adjreg) {
TR_trailTerm(fPtr);
AM_bndFlag = ON;
DF_modVarUC(fhPtr, AM_adjreg);
}
TR_trailTerm(vPtr);
AM_bndFlag = ON;
DF_mkRef((MemPtr)vPtr, fPtr);
}
/* Try to solve (F a1 ... an) = lam(k, (G b1 ... bm)), where F and G are */
/* both free variables. */
/* The arguments are: */
/* tPtr1 : reference to the ABSTRACTION BODY of the first flex term */
/* h1 : reference to the flex head of the first term */
/* nargs1: number of arguments of the first flex term */
/* args1 : reference to the argument vector of the first flex term */
/* tPtr2 : reference to the ABSTRACTION BODY of the second flex term */
/* h2 : reference to the flex head of the second flex term */
/* nargs2: number of arguments of the second flex term */
/* args2 : reference to the argument vector of the second flex term */
/* lev : abstraction context of the second term with respect to the */
/* first one. */
/* */
/* Non-Llambda pairs could be encountered during this process, and in */
/* this situation, the pair is delayed onto the disagreement list. */
static void HOPU_flexMkSubst(DF_TermPtr tPtr1, DF_TermPtr h1, int nargs1,
DF_TermPtr args1, DF_TermPtr tPtr2, DF_TermPtr h2,
int nargs2, DF_TermPtr args2, int lev)
{
int uc = DF_fvUnivCount(h1);
if (HOPU_isLLambda(uc, nargs1, args1)){ //the first term is LLambda
DF_TermPtr bndBody;
if (h1 == h2) { //same variable (comparing addresses)
if (HOPU_isLLambda(uc, nargs2, args2)) {//same var common uc
MemPtr oldhtop = AM_hreg;
DF_TermPtr newArgs = (DF_TermPtr)AM_hreg;
HOPU_copyFlagGlb = FALSE;
nargs1 = HOPU_pruneSameVar(args1, nargs1, args2, nargs2, lev);
if ((nargs1 != nargs2) || HOPU_copyFlagGlb){
DF_TermPtr newVar = (DF_TermPtr)AM_hreg;
HOPU_pushVarToHeap(uc);
HOPU_mkPandRSubst(newVar, newArgs, nargs1, h1, nargs2);
} else AM_hreg = oldhtop; //unbound
} else { //(F a1 ... an)[ll] = (lam(k, (F b1 ... bm)))[non-ll]
if (lev == 0) AM_addDisPair(tPtr1, tPtr2);
else {
MemPtr nhtop = AM_hreg + DF_TM_LAM_SIZE;
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg;
AM_heapError(AM_hreg);
DF_mkLam(AM_hreg, lev, tPtr2);
AM_hreg = nhtop;
AM_addDisPair(tPtr1, tmPtr);
} //(lev != 0)
} //tPtr2 not LLambda
} else { //different variable
int nabs;
AM_vbbreg = h1; AM_adjreg = uc; //set regs for occ
HOPU_copyFlagGlb = FALSE;
bndBody = HOPU_flexNestedSubst(args1, nargs1, h2, args2, nargs2,
tPtr2, lev);
nabs = lev + nargs1;
TR_trailTerm(h1); AM_bndFlag = ON;
if (nabs == 0) DF_mkRef((MemPtr)h1, bndBody);
else {
AM_embedError(nabs);
DF_mkLam((MemPtr)h1, nabs, bndBody);
}
} //different variable
} else { //the first term is non-LLambda
Boolean found = FALSE;
if ((nargs2 == 0) && (lev == 0) && (h1 != h2)) { // (F t1 ... tm) = G
EM_TRY{
HOPU_bndVarFlex(h2, tPtr1, h1, args1, nargs1);
found = TRUE;
} EM_CATCH {
if (EM_CurrentExnType != EM_FAIL) EM_RETHROW();
}
}
if (!found) {
if (lev == 0) AM_addDisPair(tPtr1, tPtr2);
else {
MemPtr nhtop = AM_hreg + DF_TM_LAM_SIZE;
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg;
AM_heapError(AM_hreg);
DF_mkLam(AM_hreg, lev, tPtr2);
AM_hreg = nhtop;
AM_addDisPair(tPtr1, tmPtr);
} //(lev != 0)
}
} //the first term is non-LLambda
}
/* The counterpart of HOPU_flexMkSubst invoked from HOPU_patternUnifyPair. */
/* Care is taken to avoid making a reference to a stack address in binding */
/* and creating disagreement pairs. */
/* It is assumed that the first term (F a1 ... an) given by its */
/* is not embedded in any abstractions. */
static void HOPU_flexMkSubstGlb(DF_TermPtr tPtr1, DF_TermPtr h1, int nargs1,
DF_TermPtr args1,
DF_TermPtr tPtr2, DF_TermPtr h2, int nargs2,
DF_TermPtr args2,
DF_TermPtr topPtr2, int lev)
{
int uc = DF_fvUnivCount(h1);
if (HOPU_isLLambda(uc, nargs1, args1)) { //the first term is LLambda
DF_TermPtr bndBody;
if (h1 == h2) { //same variable (comparing addresses)
if (HOPU_isLLambda(uc, nargs2, args2)){//same var; common uc
MemPtr oldhtop = AM_hreg;
DF_TermPtr newArgs = (DF_TermPtr)AM_hreg;
HOPU_copyFlagGlb = FALSE;
nargs1 = HOPU_pruneSameVar(args1, nargs1, args2, nargs2, lev);
if ((nargs1 != nargs2) || HOPU_copyFlagGlb) {
DF_TermPtr newVar = (DF_TermPtr)AM_hreg;
HOPU_pushVarToHeap(uc);
HOPU_mkPandRSubst(newVar, newArgs, nargs1, h1, nargs2);
} else AM_hreg = oldhtop; //variable remain unbound
} else { //(F a1 ... an)[ll] = (lam(k, (F b1 ... bm)))[non-ll]
//non-LLambda term must locate on the heap
if (nargs1 == 0) tPtr1 = HOPU_globalizeFlex(tPtr1);
if (lev == 0) AM_addDisPair(tPtr1, tPtr2);
else AM_addDisPair(tPtr1, DF_termDeref(topPtr2));
} //tPtr2 not LLambda
} else { //different variable
int nabs;
AM_vbbreg = h1; AM_adjreg = uc; //set regs for occ
HOPU_copyFlagGlb = FALSE;
bndBody = HOPU_flexNestedSubst(args1, nargs1, h2, args2, nargs2,
tPtr2, lev);
nabs = nargs1 + lev;
TR_trailTerm(h1); AM_bndFlag = ON;
if (HOPU_copyFlagGlb == FALSE)
bndBody = HOPU_globalizeFlex(bndBody);
if (nabs == 0) DF_mkRef((MemPtr)h1, bndBody);
else {
AM_embedError(nabs);
DF_mkLam((MemPtr)h1, nabs, bndBody);
}
}
} else {//the first term is non-LLambda (must locate on heap)
Boolean found = FALSE;
if ((nargs2 == 0) && (lev == 0) && (h1 != h2)) {//(F t1...tm)[nll] = G
EM_TRY {
HOPU_bndVarFlex(h2, tPtr1, h1, args1, nargs1);
found = TRUE;
} EM_CATCH {
if (EM_CurrentExnType == EM_FAIL)
tPtr2 = HOPU_globalizeFlex(tPtr2);
else EM_RETHROW();
}
}
if (!found) {
if (lev == 0) AM_addDisPair(tPtr1, tPtr2);
else AM_addDisPair(tPtr1, DF_termDeref(topPtr2));
}
} //the first term is non-LLambda
}
/***************************************************************************/
/* BINDING FOR FLEX-RIGID */
/* */
/* Auxiliary functions for solving flex-rigid pairs. */
/* Non-LLambda pairs are delayed onto the disagreement list. */
/***************************************************************************/
/* Try to find the (partial) binding of the head of a flex term correponding */
/* to a rigid atom during the process of unifying the flex term with a */
/* rigid one. The global variable HOPU_copyFlagGlb is used to indicate */
/* whether a new term is created during this process. */
/* Note it is assumed that rPtr refers to the dereference of a rigid atom */
/* or cons. */
static DF_TermPtr HOPU_getHead(DF_TermPtr rPtr, DF_TermPtr args, int nargs,
int emblev)
{
DF_TermPtr rtPtr;
switch(DF_termTag(rPtr)){
case DF_TM_TAG_CONST:{
if (DF_constUnivCount(rPtr) > AM_adjreg){
MemPtr newhtop;
int ind = HOPU_constIndex(rPtr, args, nargs, emblev);
if (ind == 0) EM_THROW(EM_FAIL); //occurs-check
AM_embedError(ind);
newhtop = AM_hreg + DF_TM_ATOMIC_SIZE;
AM_heapError(newhtop);
HOPU_copyFlagGlb = TRUE; //new structure is created
rtPtr = (DF_TermPtr)AM_hreg; //create a db on the heap top
DF_mkBV(AM_hreg, ind);
AM_hreg = newhtop;
} else rtPtr = rPtr; //DF_constUnivCount(rPtr <= AM_adjreg)
break;
}
case DF_TM_TAG_BVAR: {
int dbInd = DF_bvIndex(rPtr);
if (dbInd > emblev){
int ind = HOPU_bvIndex(dbInd, args, nargs, emblev);
if (ind == 0) EM_THROW(EM_FAIL); //occurs-check
AM_embedError(ind);
if (ind == dbInd) rtPtr = rPtr; //use the old db term
else { //create a db on the heap top
MemPtr newhtop = AM_hreg + DF_TM_ATOMIC_SIZE;
AM_heapError(newhtop);
HOPU_copyFlagGlb = TRUE; //new structure is created
rtPtr = (DF_TermPtr)AM_hreg;
DF_mkBV(AM_hreg, ind);
AM_hreg = newhtop;
}
} else rtPtr = rPtr; //dbInd <= emlev
break;
}
default: { rtPtr = rPtr; break;} //other rigid head: cons,nil,int,fl,str
} //switch
return rtPtr;
}
/* Create a new cons or app term on the current heap top. */
static void HOPU_mkConsOrApp(DF_TermPtr tmPtr, DF_TermPtr funcPtr,
DF_TermPtr argvec, int nargs)
{
MemPtr newhtop;
if (DF_isCons(tmPtr)) {
newhtop = AM_hreg + DF_TM_CONS_SIZE;
AM_heapError(newhtop);
DF_mkCons(AM_hreg, argvec);
} else {// application
newhtop = AM_hreg + DF_TM_APP_SIZE;
AM_heapError(newhtop);
DF_mkApp(AM_hreg, nargs, funcPtr, argvec);
}
AM_hreg = newhtop;
}
/* Try to find the (partial) binding of the head of a flex term when */
/* unifying it with a rigid term possible under abstractions. */
/* The arguments are: */
/* fargs: reference to the arguments of the flex term */
/* fnargs: number of arguments of the flex term */
/* rhPtr: reference to the rigid head */
/* rPtr: reference to the ABSTRACTION BODY of the rigid term */
/* rargs: reference to the arguments of the rigid term */
/* rnargs: number of arguments of the rigid term */
/* emblev: abstraction context of the rigid term */
/* The global variable HOPU_copyFlagGlb is used to indicate whether new */
/* term is created in this process. */
/* Note that if the rigid term is app or cons, it is first assumed that */
/* a new argument vector is to be created. However, after all the args in */
/* the binding are calculated, a checking is made on whether this is */
/* really necessary. If it is not, the old arg vector is used, and the new */
/* one is abandoned. (Heap space for it is deallocated.) */
/* It is assumed that the flexible head and its universe count are */
/* in registers AM_vbbreg and AM_adjreg. */
static DF_TermPtr HOPU_rigNestedSubst(DF_TermPtr fargs, int fnargs,
DF_TermPtr rhPtr, DF_TermPtr rPtr,
DF_TermPtr rargs, int rnargs, int emblev)
{
rhPtr = HOPU_getHead(rhPtr, fargs, fnargs, emblev); //head of the binding
if (rnargs == 0) return rhPtr; //the rigid term is atomic
else { //the rigid term is cons or app
Boolean myCopyFlagHead = HOPU_copyFlagGlb, myCopyFlagArgs = FALSE;
int i;
MemPtr oldHreg = AM_hreg; //the old heap top
MemPtr argLoc = AM_hreg; //arg vector location
DF_TermPtr newArgs = (DF_TermPtr)AM_hreg; //new argument vector
DF_TermPtr oldArgs = rargs; //old argument vector
AM_heapError(AM_hreg + rnargs * DF_TM_ATOMIC_SIZE);
AM_hreg += rnargs * DF_TM_ATOMIC_SIZE; //allocate space for argvec
HOPU_copyFlagGlb = FALSE;
for (i = 0; i < rnargs; i++){
DF_TermPtr bnd;
int nabs;
MemPtr tmpHreg = AM_hreg;
HN_hnorm(rargs); nabs = AM_numAbs; //dereference of hnf
if (AM_hreg != tmpHreg) {myCopyFlagArgs = TRUE; }
if (AM_rigFlag){
bnd = HOPU_rigNestedSubst(fargs, fnargs, AM_head,
HOPU_lamBody(rargs), AM_argVec, AM_numArgs, nabs+emblev);
} else { //AM_rigFlag = FALSE
bnd = HOPU_flexNestedSubst(fargs, fnargs, AM_head, AM_argVec,
AM_numArgs, HOPU_lamBody(rargs), nabs+emblev);
}
if (nabs == 0) DF_mkRef(argLoc, bnd); //compact atomic??
else DF_mkLam(argLoc, nabs, bnd);
argLoc += DF_TM_ATOMIC_SIZE; //note: abs has atomic size
if (HOPU_copyFlagGlb) {myCopyFlagArgs=TRUE; HOPU_copyFlagGlb=FALSE;}
rargs = (DF_TermPtr)(((MemPtr)rargs)+DF_TM_ATOMIC_SIZE); //next arg
} //for loop
if (myCopyFlagArgs) {
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg; //new cons or app
HOPU_mkConsOrApp(rPtr, rhPtr, newArgs, rnargs);
HOPU_copyFlagGlb = TRUE;
return tmPtr;
} else { //myCopyFlagBody == FALSE
AM_hreg = oldHreg; //deallocate space for the argument vector
//note no new terms are created form any argument
if (myCopyFlagHead){
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg; //new cons or app
HOPU_mkConsOrApp(rPtr, rhPtr, oldArgs, rnargs);
HOPU_copyFlagGlb = TRUE;
return tmPtr;
} else return rPtr; //myCopyFlagHead==FALSE, myCopyFlagArgs==FALSE
}
}//rnargs > 0
}
/* This version of rigNestedSubstC is needed in the compiled form of pattern */
/* unification. The essential difference from the other version is that the */
/* variable being bound is already partially bound to a structure. */
/* The difference from the other procedure is first the head normalization */
/* procedure invoked is one performs the occurs checking on partially bound */
/* variables, and second, the incoming flexible term is in fact a free */
/* variable. */
DF_TermPtr HOPU_rigNestedSubstC(DF_TermPtr rhPtr, DF_TermPtr rPtr,
DF_TermPtr rargs, int rnargs, int emblev)
{
rhPtr = HOPU_getHead(rhPtr, NULL, 0, emblev);
if (rnargs == 0) return rhPtr;
else {
Boolean myCopyFlagHead = HOPU_copyFlagGlb, myCopyFlagArgs = FALSE;
int i;
MemPtr oldHreg = AM_hreg; //the old heap top
MemPtr argLoc = AM_hreg; //arg vector location
DF_TermPtr newArgs = (DF_TermPtr)AM_hreg; //new arg vector
DF_TermPtr oldArgs = rargs; //old arg vector
AM_heapError(AM_hreg + rnargs * DF_TM_ATOMIC_SIZE);
AM_hreg += rnargs * DF_TM_ATOMIC_SIZE; //alloc space for new args
HOPU_copyFlagGlb = FALSE;
for (i = 0; i < rnargs; i++) {
DF_TermPtr bnd;
int nabs;
MemPtr tmpHreg = AM_hreg;
HN_hnormOcc(rargs); nabs = AM_numAbs;
if (tmpHreg != AM_hreg) {myCopyFlagArgs = TRUE; }
if (AM_rigFlag)
bnd = HOPU_rigNestedSubstC(AM_head, HOPU_lamBody(rargs),
AM_argVec, AM_numArgs, nabs+emblev);
else //AM_rigFlag == FALSE
bnd = HOPU_flexNestedSubstC(AM_head, AM_argVec, AM_numArgs,
HOPU_lamBody(rargs), nabs+emblev);
if (nabs == 0) DF_mkRef(argLoc, bnd);
else DF_mkLam(argLoc, nabs, bnd);
argLoc += DF_TM_ATOMIC_SIZE;
if (HOPU_copyFlagGlb) {myCopyFlagArgs=TRUE; HOPU_copyFlagGlb=FALSE;}
rargs = (DF_TermPtr)(((MemPtr)rargs)+DF_TM_ATOMIC_SIZE);
} //for loop
if (myCopyFlagArgs) {
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg; //new cons or app
HOPU_mkConsOrApp(rPtr, rhPtr, newArgs, rnargs);
HOPU_copyFlagGlb = TRUE;
return tmPtr;
} else { //myCopyFlagArgs == FALSE
AM_hreg = oldHreg;//deallocate space for arg vector
if (myCopyFlagHead) {
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg;
HOPU_mkConsOrApp(rPtr, rhPtr, oldArgs, rnargs);
HOPU_copyFlagGlb = TRUE;
return tmPtr;
} else return rPtr; ////myCopyFlagHead==FALSE, myCopyFlagArgs==FALSE
}
}//rnargs > 0
}
/* Try to solve (F a1 ... an) = lam(k, (r b1 ... bm)), where r is rigid. */
/* The arguments are: */
/* fPtr : reference to the ABSTRACTION BODY of the flex term */
/* fhPtr : reference to the flex head */
/* fnargs: number of arguments of the flex term */
/* fargs : reference to the argument vector of the flex term */
/* rPtr : reference to the ABSTRACTION BODY of the rigid term */
/* rhPtr : reference to the rigid head (Note it could be cons) */
/* rnargs: number of arguments of the rigid term */
/* rargs : reference to the argument vector of the rigid term */
/* */
/* Non-Llambda pairs could be encountered during this process, and in */
/* this situation, the pair is delayed onto the disagreement list. */
static void HOPU_rigMkSubst(DF_TermPtr fPtr, DF_TermPtr fhPtr, int fnargs,
DF_TermPtr fargs, DF_TermPtr rPtr, DF_TermPtr rhPtr,
int rnargs, DF_TermPtr rargs, int emblev)
{
int uc = DF_fvUnivCount(fhPtr);
if (HOPU_isLLambda(uc, fnargs, fargs)){//Llambda pattern
DF_TermPtr bndBody; //abs body of bnd of the fv
int nabs;
AM_vbbreg = fhPtr; AM_adjreg = uc; //set regs for occurs check
HOPU_copyFlagGlb = FALSE;
bndBody = HOPU_rigNestedSubst(fargs, fnargs, rhPtr, rPtr,
rargs, rnargs, emblev);
nabs = emblev + fnargs; //# abs in the front of the binding
TR_trailTerm(fhPtr); AM_bndFlag = ON;
if (nabs == 0) DF_mkRef((MemPtr)fhPtr, bndBody);
else {
AM_embedError(nabs);
DF_mkLam((MemPtr)fhPtr, nabs, bndBody);
}
} else { //non-Llambda pattern
if (emblev == 0) AM_addDisPair(fPtr, rPtr);
else {
MemPtr nhtop = AM_hreg + DF_TM_LAM_SIZE;
DF_TermPtr tmPtr = (DF_TermPtr)AM_hreg;
AM_heapError(AM_hreg);
DF_mkLam(AM_hreg, emblev, rPtr);
AM_hreg = nhtop;
AM_addDisPair(fPtr, tmPtr);
} // (emblev != 0)
} //non-LLambda pattern
}
/* The counter part of HOPU_rigMkSubst invoked by HOPU_patternUnifyPair. */
/* Care is taken to avoid making a reference to a register/stack address in */
/* binding and creating disagreement pair. */
/* It is assumed that the pair of terms are not embedded in any abstractions*/
/* ie. (F a1 ... an) = (r b1 ... bm) */
/* Note both fPtr and rPtr are not dereferenced. */
static void HOPU_rigMkSubstGlb(DF_TermPtr fPtr, DF_TermPtr fhPtr, int fnargs,
DF_TermPtr fargs,
DF_TermPtr rPtr, DF_TermPtr rhPtr, int rnargs,
DF_TermPtr rargs)
{
int uc = DF_fvUnivCount(fhPtr);
if (HOPU_isLLambda(uc, fnargs, fargs)) { //LLambda pattern
DF_TermPtr bndBody;
AM_vbbreg = fhPtr; AM_adjreg = uc;
HOPU_copyFlagGlb = FALSE;
bndBody = HOPU_rigNestedSubst(fargs, fnargs, rhPtr, DF_termDeref(rPtr),
rargs, rnargs, 0);
TR_trailTerm(fhPtr); AM_bndFlag = ON;
if (HOPU_copyFlagGlb) {//bndBody must locate on the heap
if (fnargs == 0) DF_mkRef((MemPtr)fhPtr, bndBody);
else {
AM_embedError(fnargs);
DF_mkLam((MemPtr)fhPtr, fnargs, bndBody);
}
} else { //HOPU_copyFlagGlb == FALSE
/* //note: rPtr is the undereferenced rigid term; in this case,
// it is assumed rPtr cannot be a reference to the stack.
// This assumption should be ensured by the fact that atomic
// rigid terms on stack are alway copied into registers in
// binding.
if (fnargs == 0) DF_copyAtomic(rPtr, (MemPtr)fhPtr); */
if (fnargs == 0) HOPU_globalizeCopyRigid(bndBody, fhPtr);
else {
bndBody = HOPU_globalizeRigid(bndBody);
AM_embedError(fnargs);
DF_mkLam((MemPtr)fhPtr, fnargs, bndBody);
}
} //HOPU_copyFlagGlb == FALSE
} else //non_LLambda flex (must locate on the heap)
AM_addDisPair(DF_termDeref(fPtr),
HOPU_globalizeRigid(DF_termDeref(rPtr)));
}
/***************************************************************************/
/* TERM SIMPLIFICATION (RIGID-RIGID) */
/* */
/* Auxiliary functions for solving rigid-rigid pairs. */
/***************************************************************************/
/* Matching heads of two rigid terms. Eta-expansion is considered when */
/* necessary. It is assumed that the heads have been dereferenced. */
static void HOPU_matchHeads(DF_TermPtr hPtr1, DF_TermPtr hPtr2, int nabs)
{
switch(DF_termTag(hPtr1)){
case DF_TM_TAG_CONST:{
if (!(DF_isConst(hPtr2) && (DF_sameConsts(hPtr1, hPtr2))))
EM_THROW(EM_FAIL);
if (DF_isTConst(hPtr1)){ //(first-order) unify type environments
HOPU_typesUnify(DF_constType(hPtr1), DF_constType(hPtr2),
AM_cstTyEnvSize(DF_constTabIndex(hPtr1)));
}
break;
}
case DF_TM_TAG_BVAR: {
if (!DF_isBV(hPtr2)) EM_THROW(EM_FAIL);
else {
int ind = DF_bvIndex(hPtr2) + nabs; //lifting for eta-expansion
AM_embedError(ind);
if (DF_bvIndex(hPtr1) != ind) EM_THROW(EM_FAIL);
}
break;
}
case DF_TM_TAG_NIL: { if (!DF_isNil(hPtr2)) EM_THROW(EM_FAIL); break;}
case DF_TM_TAG_INT: {
if (!(DF_isInt(hPtr2) && (DF_intValue(hPtr2) == DF_intValue(hPtr1))))
EM_THROW(EM_FAIL);
break;
}
case DF_TM_TAG_FLOAT:{
if (!(DF_isFloat(hPtr2)&&(DF_floatValue(hPtr2)==DF_floatValue(hPtr1))))
EM_THROW(EM_FAIL);
break;
}
case DF_TM_TAG_STR: {
if (!(DF_isStr(hPtr2) && (DF_sameStrs(hPtr1, hPtr2))))
EM_THROW(EM_FAIL);
break;
}
case DF_TM_TAG_CONS: {
if (!(DF_isCons(hPtr2))) EM_THROW(EM_FAIL);
break;
}
} //switch
}
/* Set up PDL by sub problems resulted from rigid-rigid pairs upon */
/* successful matching of their heads. Eta-expansion is performed on-a-fly */
/* when necessary. */
void HOPU_setPDL(MemPtr args1, MemPtr args2, int nargs, int nabs)
{
if (nabs == 0){ //no need for eta-expansion
AM_pdlError(nargs * 2);
for (; nargs > 0; nargs --){
AM_pushPDL(args1); args1 += DF_TM_ATOMIC_SIZE;
AM_pushPDL(args2); args2 += DF_TM_ATOMIC_SIZE;
}
} else { //nabs > 0 (eta-expansion)
AM_pdlError((nargs + nabs) * 2);
AM_heapError(AM_hreg + nargs*DF_TM_SUSP_SIZE + nabs*DF_TM_ATOMIC_SIZE);
for (; nargs > 0; nargs --){ //[|ai, 0, nabs, nil|]
AM_pushPDL(args1); AM_pushPDL(AM_hreg);
DF_mkSusp(AM_hreg, 0, nabs, DF_termDeref((DF_TermPtr)args2),
DF_EMPTY_ENV);
AM_hreg += DF_TM_SUSP_SIZE;
args1 += DF_TM_ATOMIC_SIZE; args2 += DF_TM_ATOMIC_SIZE;
}
for (; nabs > 0; nabs --){ // bv(i)
AM_pushPDL(args1); AM_pushPDL(AM_hreg);
DF_mkBV(AM_hreg, nabs);
args1 += DF_TM_ATOMIC_SIZE; AM_hreg += DF_TM_ATOMIC_SIZE;
}
}
}
/***************************************************************************/
/* HIGHER_ORDER PATTERN UNIFICATION */
/* */
/* The main routines of this file. */
/***************************************************************************/
/* Perform higher-order pattern unification over the pairs delayed on the */
/* PDL stack. The PDL stack is empty upon successful termination of this */
/* procedure. */
void HOPU_patternUnifyPDL()
{
DF_TermPtr tPtr1, tPtr2, //pointers to terms to be unified
hPtr, //pointer to head of hnf
args; //arg vec of hnf
Flag rig; //rigid flag and cons flags
int nabs, nargs; //binder length and # of arguments of hnf
while (AM_nemptyPDL()){
//retrieve the pair of terms on the current top of PDL
tPtr1 = (DF_TermPtr)AM_popPDL(); tPtr2 = (DF_TermPtr)AM_popPDL();
HN_hnorm(tPtr1); //hnorm tPtr1
hPtr = AM_head; args = AM_argVec; nabs = AM_numAbs; nargs = AM_numArgs;
rig = AM_rigFlag; //bookkeeping relevant info of hnf of tPtr1
HN_hnorm(tPtr2); //hnorm tPtr2
if (rig){
if (AM_rigFlag){// rigid - rigid
if (nabs > AM_numAbs) {
nabs = nabs - AM_numAbs; //reuse nabs
HOPU_matchHeads(hPtr, AM_head, nabs);
HOPU_setPDL((MemPtr)args,(MemPtr)AM_argVec,AM_numArgs,nabs);
} else { //nabs <= AM_numAbs
nabs = AM_numAbs - nabs; //reuse nabs
HOPU_matchHeads(AM_head, hPtr, nabs);
HOPU_setPDL((MemPtr)AM_argVec, (MemPtr)args, nargs, nabs);
}
} else { // rigid - flex
DF_TermPtr rigBody = HOPU_lamBody(tPtr1);
DF_TermPtr flexBody = HOPU_lamBody(tPtr2);
if (nabs < AM_numAbs) { //eta expand rigid term first
nabs = AM_numAbs - nabs; //reuse nabs
rigBody = HOPU_etaExpand(&hPtr, &args, nargs, nabs);
HOPU_rigMkSubst(flexBody, AM_head, AM_numArgs, AM_argVec,
rigBody, hPtr, (nargs+nabs), args, 0);
} else HOPU_rigMkSubst(flexBody,AM_head, AM_numArgs, AM_argVec,
rigBody,hPtr,nargs,args,nabs-AM_numAbs);
} // rigid-flex
} else { //(rig == FALSE)
DF_TermPtr absBody1 = HOPU_lamBody(tPtr1);
DF_TermPtr absBody2 = HOPU_lamBody(tPtr2);
if (AM_rigFlag){// flex - rigid
if (AM_numAbs < nabs) { //eta expand rigid term first
nabs = nabs - AM_numAbs; //reuse nabs
absBody2 = HOPU_etaExpand(&AM_head, &AM_argVec, AM_numArgs,
nabs);
HOPU_rigMkSubst(absBody1, hPtr, nargs, args, absBody2,
AM_head, AM_numArgs+nabs, AM_argVec, 0);
}else HOPU_rigMkSubst(absBody1,hPtr,nargs,args,absBody2,AM_head,
AM_numArgs,AM_argVec,AM_numAbs-nabs);
} else { // flex - flex
if (AM_numAbs > nabs)
HOPU_flexMkSubst(absBody1, hPtr, nargs, args, absBody2,
AM_head, AM_numArgs, AM_argVec,
AM_numAbs-nabs);
else HOPU_flexMkSubst(absBody2, AM_head, AM_numArgs, AM_argVec,
absBody1,hPtr,nargs,args,nabs-AM_numAbs);
} // flex - flex
} //(rig == FALSE)
} // while (AM_nemptyPDL())
}
/* Interpretively pattern unify first the pairs delayed on the PDL, then */
/* those delayed on the live list, if binding occured during the first step */
/* or previous compiled unification process. */
/* Upon successful termination, PDL should be empty and pairs left on the */
/* live list should be those other than LLambda. */
void HOPU_patternUnify()
{
HOPU_patternUnifyPDL(); //first solve those left from compiled unification
while (AM_bndFlag && AM_nempLiveList()){
DF_DisPairPtr dset = AM_llreg;
do { //move everything in live list to PDL
AM_pdlError(2);
AM_pushPDL((MemPtr)DF_disPairSecondTerm(dset));
AM_pushPDL((MemPtr)DF_disPairFirstTerm(dset));
dset = DF_disPairNext(dset);
} while (DF_isNEmpDisSet(dset));
AM_bndFlag = OFF;
AM_llreg = DF_EMPTY_DIS_SET;
HOPU_patternUnifyPDL(); //unsolvable pairs are added to live list
}
}
/* Interpretively pattern unify a pair of terms given as parameters. This is*/
/* the counter part of HOPU_patterUnifyPDL that is invoked from the compiled*/
/* part of unification. In this situation, the procedure has to be applied */
/* to two terms as opposed to pairs delayed on the PDL stack. */
/* */
/* The input term pointers may dereference to register and stack addresses */
/* Care must be taken to avoid making a reference to a register (stack) */
/* address in binding a variable, and in making a disagreement pair. */
void HOPU_patternUnifyPair(DF_TermPtr tPtr1, DF_TermPtr tPtr2)
{
DF_TermPtr h1Ptr, h2Ptr, args1, args2;
Flag rig1, rig2;
int nabs1, nabs2, nargs1, nargs2;
MemPtr oldPdlBot = AM_pdlBot;
AM_pdlBot = AM_pdlTop;
HN_hnorm(tPtr1); h1Ptr = AM_head; args1 = AM_argVec;
nabs1 = AM_numAbs; nargs1 = AM_numArgs; rig1 = AM_rigFlag;
HN_hnorm(tPtr2); h2Ptr = AM_head; args2 = AM_argVec;
nabs2 = AM_numAbs; nargs2 = AM_numArgs; rig2 = AM_rigFlag;
if (rig1) {
if (rig2) { //rigid-rigid
if (nabs1 > nabs2) {
nabs1 = nabs1 - nabs2;
HOPU_matchHeads(h1Ptr, h2Ptr, nabs1);
HOPU_setPDL((MemPtr)args1, (MemPtr)args2, nargs2, nabs1);
} else {//nabs1 <= nabs2
nabs1 = nabs2 - nabs1;
HOPU_matchHeads(h2Ptr, h1Ptr, nabs1);
HOPU_setPDL((MemPtr)args2, (MemPtr)args1, nargs1, nabs1);
}
} else { //rigid-flex
if ((nabs1 == 0) && (nabs2 == 0))
HOPU_rigMkSubstGlb(tPtr2, h2Ptr, nargs2, args2,
tPtr1, h1Ptr, nargs1, args1);
else {
DF_TermPtr rigBody = HOPU_lamBody(tPtr1);
DF_TermPtr flexBody = HOPU_lamBody(tPtr2);
if (nabs1 < nabs2) {
nabs1 = nabs2 - nabs1;
rigBody = HOPU_etaExpand(&h1Ptr, &args1, nargs1, nabs1);
//now rigBody must locate on heap
HOPU_rigMkSubst(flexBody, h2Ptr, nargs2, args2, rigBody,
h1Ptr, nargs1+nabs1, args1, 0);
} else // (nabs1 >= nabs2)
HOPU_rigMkSubst(flexBody, h2Ptr, nargs2, args2, rigBody,
h1Ptr, nargs1, args1, nabs1-nabs2);
} // !(nabs1 == nabs2 == 0)
} //rigid-flex
} else { // rig1 = FALSE
if (rig2) { //flex-rigid
if ((nabs2 == 0) && (nabs1 == 0))
HOPU_rigMkSubstGlb(tPtr1, h1Ptr, nargs1, args1,
tPtr2, h2Ptr, nargs2, args2);
else { //!(nabs1 == nabs2 == 0)
DF_TermPtr rigBody = HOPU_lamBody(tPtr2);
DF_TermPtr flexBody = HOPU_lamBody(tPtr1);
if (nabs2 < nabs1) {
nabs1 = nabs2 - nabs1;
rigBody = HOPU_etaExpand(&h2Ptr, &args2, nargs2, nabs1);
//now rigBody must locate on heap
HOPU_rigMkSubst(flexBody, h1Ptr, nargs1, args1, rigBody,
h2Ptr, nargs2+nabs1, args2, 0);
} else //(nabs2 >= nabs1)
HOPU_rigMkSubst(flexBody, h1Ptr, nargs1, args1, rigBody,
h2Ptr, nargs2, args2, nabs2-nabs1);
} //!(nabs1 == nabs2 == 0)
} else { //flex-flex
if (nabs1 == 0) //nabs2 >= nabs1
HOPU_flexMkSubstGlb(DF_termDeref(tPtr1), h1Ptr, nargs1, args1,
HOPU_lamBody(tPtr2), h2Ptr, nargs2, args2,
tPtr2, nabs2);
else { //(nabs1 > 0)
if (nabs2 == 0) //nabs2 < nabs1
HOPU_flexMkSubstGlb(DF_termDeref(tPtr2),h2Ptr,nargs2,args2,
HOPU_lamBody(tPtr1),h1Ptr,nargs1,args1,
tPtr1,nabs1);
else { //nabs1 != 0 && nabs2 != 0
DF_TermPtr flexBody1 = HOPU_lamBody(tPtr1);
DF_TermPtr flexBody2 = HOPU_lamBody(tPtr2);
if (nabs2 > nabs1)
HOPU_flexMkSubst(flexBody1, h1Ptr, nargs1, args1,
flexBody2, h2Ptr, nargs2, args2,
nabs2-nabs1);
else //nabs2 <= nabs1
HOPU_flexMkSubst(flexBody2, h2Ptr, nargs2, args2,
flexBody1, h1Ptr, nargs1, args1,
nabs1-nabs2);
} //nabs1 != 0 && nabs2 != 0
} //(nabs1 > 0)
} //flex-flex
} //rig1 = FALSE
//solve the pairs (which must locate on heap) remaining on the PDL
HOPU_patternUnifyPDL();
AM_pdlBot = oldPdlBot;
}
#endif //HOPU_C
|