1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
|
/*-----------------------------------------------------------------------
File : cte_termbanks.c
Author: Stephan Schulz
Contents
Functions for term banks (efficient dag-representations for term
sets).
Bindings (i.e. values in term->binding) make only sense in dynamical
contexts, not in (mostly) static term banks. Thus, bindings are
followed whenever a term is treated as an individual term, but not
when they form part of a term bank and are manipulated as such.
Copyright 1998-2017 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Changes
Created: Sat Nov 15 16:26:30 MET 1997
-----------------------------------------------------------------------*/
#include "cte_termbanks.h"
#include "cte_typecheck.h"
#include <cte_typebanks.h>
#include <ccl_tformulae.h>
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
bool TBPrintInternalInfo = false; /* Print internal information about
term nodes, e.g. the flag field */
bool TBPrintDetails = false; /* Collect potentially expensive
information (number of nodes,
number of unshared nodes, size of
various sub-data structures) and
print them if required */
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
typedef Term_p (*TermParseFun)(Scanner_p in, TB_p bank);
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: tb_print_dag()
//
// Print the terms as a dag in the order of insertion.
//
// Global Variables: TBPrintInternalInfo
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void tb_print_dag(FILE *out, NumTree_p in_index, Sig_p sig)
{
Term_p term;
if(!in_index)
{
return;
}
tb_print_dag(out, in_index->lson, sig);
term = in_index->val1.p_val;
fprintf(out, "*%ld : ", term->entry_no);
if(TermIsVar(term))
{
VarPrint(out, term->f_code);
}
else
{
fputs(SigFindName(sig, term->f_code), out);
if(!TermIsConst(term))
{
int i;
assert(term->arity>=1);
assert(term->args);
putc('(', out);
fprintf(out, "*%ld", TBCellIdent(term->args[0]));
for(i=1; i<term->arity; i++)
{
putc(',', out);
fprintf(out, "*%ld", TBCellIdent(term->args[i]));
}
putc(')', out);
}
fprintf(out, " = ");
TermPrint(out, term, sig, DEREF_NEVER);
}
if(TBPrintInternalInfo)
{
fprintf(out, "\t/* Properties: %10d */",
term->properties);
}
fprintf(out, "\n");
tb_print_dag(out, in_index->rson, sig);
}
/*-----------------------------------------------------------------------
//
// Function: tb_termtop_insert()
//
// Insert a term into the term bank for which the subterms are
// already in the term bank. Will reuse or destroy the top cell!
//
// Global Variables: TBSupportReplace
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
static Term_p tb_termtop_insert(TB_p bank, Term_p t)
{
Term_p new;
assert(t);
assert(!TermIsVar(t));
assert(!TermIsAppliedVar(t) || TermIsVar(t->args[0]));
#ifndef NDEBUG
for(int i=0; i<t->arity; i++)
{
assert(TermIsShared(t->args[i]));
}
#endif
/* Infer the sort of this term (may be temporary) */
if(t->type == NULL)
{
TypeInferSort(bank->sig, t, NULL);
assert(t->type != NULL);
}
bank->insertions++;
new = TermCellStoreInsert(&(bank->term_store), t);
if(new) /* Term node already existed, just add properties */
{
assert(!TermIsShared(t));
new->properties = (new->properties | t->properties)/*& bank->prop_mask*/;
TermTopFree(t);
t = new;
}
else
{
t->entry_no = ++(bank->in_count);
TermCellAssignProp(t,TPGarbageFlag, bank->garbage_state);
TermCellSetProp(t, TPIsShared); /* Groundness may change below */
if(TermIsPhonyApp(t) && TermIsLambda(t->args[0]))
{
TermCellSetProp(t, TPIsBetaReducible);
}
t->v_count = 0;
t->f_count = !TermIsAppliedVar(t) ? 1 : 0;
t->weight = DEFAULT_FWEIGHT*t->f_count;
for(int i=0; i<t->arity; i++)
{
assert(TermIsShared(t->args[i])||TermIsVar(t->args[i]));
TermCellSetProp(t, TermCellGiveProps(t->args[i], TPIsBetaReducible));
if(TermIsVar(t->args[i]))
{
t->v_count += 1;
t->weight += DEFAULT_VWEIGHT;
}
else
{
t->v_count +=t->args[i]->v_count;
t->f_count +=t->args[i]->f_count;
t->weight +=t->args[i]->weight;
}
}
if(t->v_count == 0)
{
TermCellSetProp(t, TPIsGround);
}
assert(TermWeight(t, DEFAULT_VWEIGHT, DEFAULT_FWEIGHT) == TermWeightCompute(t, DEFAULT_VWEIGHT, DEFAULT_FWEIGHT));
assert((t->v_count == 0) == TermIsGround(t));
assert(TBFind(bank, t));
//assert(TermIsGround(t) == TermIsGroundCompute(t));
}
TermSetBank(t, bank);
return t;
}
/*-----------------------------------------------------------------------
//
// Function: tb_parse_cons_list()
//
// Parse a LOP list into an (shared) internal $cons list.
//
// Global Variables: -
//
// Side Effects : Input, Memory operations
//
/----------------------------------------------------------------------*/
static Term_p tb_parse_cons_list(Scanner_p in, TB_p bank, bool check_symb_prop)
{
Term_p handle;
Term_p current;
PStack_p stack;
assert(SigSupportLists);
stack = PStackAlloc();
AcceptInpTok(in, OpenSquare);
handle = TermDefaultCellArityAlloc(2);
current = handle;
if(!TestInpTok(in, CloseSquare))
{
current->f_code = SIG_CONS_CODE;
current->args[0] = TBTermParseReal(in, bank, check_symb_prop);
current->args[1] = TermDefaultCellArityAlloc(2);
current = current->args[1];
PStackPushP(stack, current);
while(TestInpTok(in, Comma))
{
NextToken(in);
current->f_code = SIG_CONS_CODE;
current->args[0] = TBTermParseReal(in, bank, check_symb_prop);
current->args[1] = TermDefaultCellArityAlloc(2);
current = current->args[1];
PStackPushP(stack, current);
}
current = PStackPopP(stack);
}
AcceptInpTok(in, CloseSquare);
current->f_code = SIG_NIL_CODE;
/* Now insert the list into the bank */
handle = TBInsert(bank, current, DEREF_NEVER);
while(!PStackEmpty(stack))
{
current = PStackPopP(stack);
current->args[1] = handle;
handle = tb_termtop_insert(bank, current);
}
PStackFree(stack);
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: parse_let_typedecl()
//
// Parses a single type declaration that constitutes of the first
// part of a let term. For each parsed symbol, on type_decl it stores
// symbol name (DStr), fresh symbol ID (regardless of whether
// the symbol is already in the signature), and symbol type
// ACHTUNG: Dynamically allocated DStr is put on the stack.
//
// Global Variables: -
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static void parse_let_typedecl(Scanner_p in, TB_p bank, PStack_p type_decls)
{
DStr_p name = DStrAlloc();
FuncSymbType sym_type = FuncSymbParse(in, name);
if (sym_type == FSIdentFreeFun)
{
AcceptInpTok(in, Colon);
Type_p type = TypeBankParseType(in, bank->sig->type_bank);
PStackPushP(type_decls, name);
PStackPushInt(type_decls, SigInsertLetId(bank->sig, DStrView(name), type));
}
else
{
AktTokenError(in, "let declaration expects a function symbol",
true);
}
}
/*-----------------------------------------------------------------------
//
// Function: parse_let_definition()
//
// Parses a single type declaration that constitutes of the first
// part of a let term. For each parsed symbol, on type_decl it stores
// symbol name (DStr), fresh symbol ID (regardless of whether
// the symbol is already in the signature), and symbol type
// ACHTUNG: Dynamically allocated DStr is put on the stack.
//
// Global Variables: -
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static Term_p parse_let_sym_def(Scanner_p in, TB_p bank, PStack_p type_decls)
{
DStr_p name = DStrAlloc();
FuncSymbType sym_type = FuncSymbParse(in, name);
FunCode id = 0;
for(PStackPointer i = 0; !id && i<PStackGetSP(type_decls); i += 2)
{
if(!strcmp(DStrView(name), DStrView(PStackElementP(type_decls, i))))
{
id = PStackElementInt(type_decls, i+1);
}
}
if(id)
{
Type_p type = SigGetType(bank->sig, id);
int arity = TypeGetMaxArity(type);
Term_p vars[arity]; // Variable length array
IntOrP dummy = {0};
if(arity)
{
AcceptInpTok(in, OpenBracket);
StrTree_p prev_vars = NULL;
for(int i=0; i<arity; i++)
{
DStrReset(name);
sym_type = FuncSymbParse(in, name);
if(StrTreeFind(&prev_vars, DStrView(name)))
{
AktTokenError(in, "variables must be distinct", true);
}
StrTreeStore(&prev_vars, DStrView(name), dummy, dummy);
if(sym_type != FSIdentVar)
{
AktTokenError(in, "variable is expected", true);
}
Type_p arg_type = type->args[i];
VarBankPushEnv(bank->vars);
vars[i] = VarBankExtNameAssertAllocSort(bank->vars,
DStrView(name), arg_type);
if(i!=arity-1)
{
AcceptInpTok(in, Comma);
}
}
AcceptInpTok(in, CloseBracket);
StrTreeFree(prev_vars);
}
AcceptInpTok(in, Colon);
AcceptInpTok(in, EqualSign);
Term_p rhs = TypeIsPredicate(type) ?
TFormulaTSTPParse(in, bank) : TBTermParse(in, bank);
Term_p lhs = TermTopAlloc(id, arity);
for(int i=0; i<arity; i++)
{
lhs->args[i] = vars[i];
VarBankPopEnv(bank->vars);
}
lhs = TBTermTopInsert(bank, lhs);
DStrFree(name);
return EqnTermsTBTermEncode(bank, lhs, rhs, true, PENormal);
}
else
{
AktTokenError(in, "symbol not in let declaration list", true);
return NULL;
}
}
/*-----------------------------------------------------------------------
//
// Function: make_let()
//
// Bulids the variable-arity let term.
//
// Global Variables: -
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static Term_p make_let(TB_p bank, PStack_p definitions, Term_p body)
{
Term_p let_t = TermTopAlloc(SIG_LET_CODE, PStackGetSP(definitions) + 1);
for(PStackPointer i=0; i<PStackGetSP(definitions); i++)
{
let_t->args[i] = PStackElementP(definitions, i);
}
let_t->args[PStackGetSP(definitions)] = body;
let_t->type = body->type;
return TBTermTopInsert(bank, let_t);
}
/*-----------------------------------------------------------------------
//
// Function: tb_subterm_parse()
//
// Parse a subterm, i.e. a term which cannot start with a predicate
// symbol.
//
// Global Variables: -
//
// Side Effects : Input, memory operations, changes termbank.
//
/----------------------------------------------------------------------*/
static Term_p tb_subterm_parse(Scanner_p in, TB_p bank)
{
Term_p res = TBTermParseReal(in, bank, true);
if(!TermIsVar(res))
{
if(SigIsPredicate(bank->sig, res->f_code))
{
if(SigIsFixedType(bank->sig, res->f_code))
{
AktTokenError(in,
"Predicate used as function symbol in preceding term",
false);
}
else
{
SigDeclareIsFunction(bank->sig, res->f_code);
TypeInferSort(bank->sig, res, in);
assert(res->type);
}
}
else
{
SigFixType(bank->sig, res->f_code);
}
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: choose_subterm_parse_fun()
//
// If the argument to be parsed should be of boolean type, parse
// the argument as a formula. Otherwise, parse it as before.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static Term_p choose_subterm_parse_fun(bool check_symb_prop,
Type_p type, int arg,
Scanner_p in, TB_p bank)
{
Term_p res = NULL;
if(type && arg < TypeGetMaxArity(type) && TypeIsBool(type->args[arg]))
{
res = TFormulaTSTPParse(in,bank);
}
else
{
res = check_symb_prop ? tb_subterm_parse(in, bank) : TBTermParse(in, bank);
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: normalize_boolean_terms()
//
// If term_ref points to an equation of type X=true that appears
// under context, replace this equation by X.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static void normalize_boolean_terms(Term_p* term_ref, TB_p bank)
{
assert(term_ref && *term_ref);
Term_p term = *term_ref;
Sig_p sig = bank->sig;
if(term->f_code == sig->eqn_code)
{
if(TermIsVar(term->args[0]) && term->args[1]->f_code == SIG_TRUE_CODE)
{
*term_ref = term->args[0]; // garbage collection will deal with parent
}
else if(term->args[0]->f_code == SIG_TRUE_CODE)
{
assert(term->args[1]->f_code == SIG_TRUE_CODE);
assert(term->args[0] == bank->true_term);
assert(term->args[1] == bank->true_term);
*term_ref = term->args[0];
}
}
else if(term->f_code == sig->neqn_code
&& term->args[0]->f_code == SIG_TRUE_CODE)
{
assert(term->args[1]->f_code == SIG_TRUE_CODE);
assert(term->args[0] == bank->true_term);
assert(term->args[1] == bank->true_term);
*term_ref = bank->false_term;
}
}
/*-----------------------------------------------------------------------
//
// Function: tb_term_parse_arglist()
//
// Parse a list of terms (comma-separated and enclosed in brackets)
// into an array of (shared) term pointers. See TermParseArgList()
// in cte_terms.c for more.
//
// Global Variables: -
//
// Side Effects : Input, memory operations, changes term bank
//
/----------------------------------------------------------------------*/
static Term_p tb_term_parse_arglist(Scanner_p in, TB_p bank,
bool check_symb_prop, Type_p type)
{
Term_p tmp;
Term_p result;
PStackPointer i=0, arity;
PStack_p args;
AcceptInpTok(in, OpenBracket);
if(TestInpTok(in, CloseBracket))
{
NextToken(in);
result = TermDefaultCellAlloc();
return result;
}
args = PStackAlloc();
tmp = choose_subterm_parse_fun(check_symb_prop, type, i, in, bank);
normalize_boolean_terms(&tmp, bank);
PStackPushP(args, tmp);
i++;
while(TestInpTok(in, Comma))
{
NextToken(in);
tmp = choose_subterm_parse_fun(check_symb_prop, type, i, in, bank);
normalize_boolean_terms(&tmp, bank);
PStackPushP(args, tmp);
i++;
}
AcceptInpTok(in, CloseBracket);
arity = PStackGetSP(args);
result = TermDefaultCellArityAlloc(arity);
for(i=0;i<arity;i++)
{
result->args[i] = PStackElementP(args,i);
}
PStackFree(args);
return result;
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: TBAlloc()
//
// Allocate an empty, initialized termbank.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
TB_p TBAlloc(Sig_p sig)
{
TB_p handle;
Term_p term;
assert(sig);
handle = TBCellAlloc();
handle->in_count = 0;
handle->insertions = 0;
handle->rewrite_steps = 0;
handle->ext_index = PDIntArrayAlloc(1,100000);
handle->garbage_state = TPIgnoreProps;
handle->sig = sig;
handle->vars = VarBankAlloc(sig->type_bank);
TermCellStoreInit(&(handle->term_store));
term = TermConstCellAlloc(SIG_TRUE_CODE);
term->type = sig->type_bank->bool_type;
TermCellSetProp(term, TPPredPos);
handle->true_term = TBInsert(handle, term, DEREF_NEVER);
TermFree(term);
term = TermConstCellAlloc(SIG_FALSE_CODE);
term->type = sig->type_bank->bool_type;
TermCellSetProp(term, TPPredPos);
handle->false_term = TBInsert(handle, term, DEREF_NEVER);
TermFree(term);
handle->min_terms = PDArrayAlloc(16, 0);
//handle->freevarsets = NULL;
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: TBFree()
//
// Free a term bank (if the signature alread has been
// extracted). Voids all pointers to terms in the bank!
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
void TBFree(TB_p junk)
{
assert(!junk->sig);
/* printf("TBFree(): %ld\n", TermCellStoreNodes(&(junk->term_store)));
*/
TermCellStoreExit(&(junk->term_store));
PDArrayFree(junk->ext_index);
VarBankFree(junk->vars);
PDArrayFree(junk->min_terms);
//assert(!junk->freevarsets);
TBCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: TBVarSetStoreFree()
//
// Free and reset the VarSetStore in bank.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void TBVarSetStoreFree(TB_p bank)
{
// VarSetStoreFree(bank->freevarsets);
// bank->freevarsets = NULL;
}
/*-----------------------------------------------------------------------
//
// Function: TBTermNodes()
//
// Return the number of term nodes (variables and non-variables) in
// the term bank.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long TBTermNodes(TB_p bank)
{
assert(TermCellStoreNodes(&(bank->term_store))==
TermCellStoreCountNodes(&(bank->term_store)));
return TermCellStoreNodes(&(bank->term_store))+VarBankCardinality(bank->vars);
}
/*-----------------------------------------------------------------------
//
// Function: TBInsert()
//
// Insert the term into the termbank. The original term will remain
// untouched. The routine returns a pointer to a new, shared term of
// the same structure.
//
// TermProperties are masked with bank->prop_mask.
//
// Global Variables: -
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
Term_p TBInsert(TB_p bank, Term_p term, DerefType deref)
{
int i;
Term_p t;
const int limit = DEREF_LIMIT(term, deref);
assert(term);
term = TermDeref(term, &deref);
if(TermIsVar(term))
{
t = VarBankVarAssertAlloc(bank->vars, term->f_code, term->type);
TermSetBank(t, bank);
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsert(bank, term->args[i],
CONVERT_DEREF(i, limit, deref));
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertIgnoreVar()
//
// As TBInsert, but does instead of using variables from the term bank,
// uses the ones already present in the temr.
//
// TermProperties are masked with bank->prop_mask.
//
// Global Variables: -
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
Term_p TBInsertIgnoreVar(TB_p bank, Term_p term, DerefType deref)
{
int i;
Term_p t;
const int limit = DEREF_LIMIT(term, deref);
assert(term);
term = TermDeref(term, &deref);
if(TermIsVar(term))
{
return term;
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertIgnoreVar(bank, term->args[i],
CONVERT_DEREF(i, limit, deref));
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertNoProps()
//
// As TBInsert, but will set all properties of the new term to 0
// first.
//
// Global Variables: TBSupportReplace
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
Term_p TBInsertNoProps(TB_p bank, Term_p term, DerefType deref)
{
int i;
Term_p t;
assert(term);
const int limit = DEREF_LIMIT(term, deref);
term = TermDeref(term, &deref);
if(TermIsVar(term))
{
t = VarBankVarAssertAlloc(bank->vars, term->f_code, term->type);
TermSetBank(t, bank);
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
t->properties = TPIgnoreProps;
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertNoProps(bank, term->args[i],
CONVERT_DEREF(i, limit, deref));
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertRepl()
//
// As TBInsertNoProps, but when old is encountered as a subterm
// (regardless of instantiation), replace it with uninstantiated
// repl (which _must_ be in bank).
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p TBInsertRepl(TB_p bank, Term_p term, DerefType deref, Term_p old, Term_p repl)
{
int i;
Term_p t;
assert(term);
if(term == old)
{
assert(TBFind(bank, repl));
return repl;
}
const int limit = DEREF_LIMIT(term, deref);
term = TermDeref(term, &deref);
if(TermIsVar(term))
{
t = VarBankVarAssertAlloc(bank->vars, term->f_code, term->type);
TermSetBank(t, bank);
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
t->properties = TPIgnoreProps;
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertRepl(bank, term->args[i],
CONVERT_DEREF(i, limit, deref), old, repl);
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertReplPlain()
//
// As TBInsertReplPlain, but terms are not instantiated.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p TBInsertReplPlain(TB_p bank, Term_p term, Term_p old, Term_p repl)
{
int i;
Term_p t;
bool changed = false;
assert(term);
if(term == old)
{
assert(TBFind(bank, repl));
return repl;
}
if(TermStandardWeight(term) <= TermStandardWeight(old))
{
return term;
}
if(TermIsVar(term))
{
return term;
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
t->properties = TPIgnoreProps;
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertReplPlain(bank, term->args[i],
old, repl);
if(t->args[i]!=term->args[i])
{
changed = true;
}
}
if(changed)
{
t = tb_termtop_insert(bank, t);
}
else
{
TermTopFree(t);
t = term;
}
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertInstantiatedFO()
//
// Insert a term into the termbank under the assumption that it is a
// right side of a rule (or equation) composed of terms from bank,
// and (possibly) instantiated with terms from bank - i.e. don't
// insert terms that are bound to variables and ground terms, but
// assume that they are in the term bank. Properties in newly
// created nodes are deleted.
//
// Global Variables: TBSupportReplace
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
Term_p TBInsertInstantiatedFO(TB_p bank, Term_p term)
{
int i;
Term_p t;
assert(term);
if(TermIsGround(term))
{
assert(TBFind(bank, term));
return term;
}
if(term->binding)
{
assert(TBFind(bank, term->binding));
return term->binding;
}
if(TermIsVar(term))
{
t = VarBankVarAssertAlloc(bank->vars, term->f_code, term->type);
TermSetBank(t, bank);
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
t->properties = TPIgnoreProps;
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertInstantiatedFO(bank, term->args[i]);
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertInstantiatedHO()
//
// Differs from TBInsertInstantiatedFO by inserting every binding in
// the termbank. The reason is that bindings might be unshared terms,
// so we need to make sure we share them.
//
// Global Variables: TBSupportReplace
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
Term_p TBInsertInstantiatedHO(TB_p bank, Term_p term, bool follow_bind)
{
int i;
Term_p t;
assert(term);
if(TermIsGround(term) && TermIsShared(term))
{
return term;
}
if(TermIsVar(term) && term->binding)
{
t = follow_bind ? TBInsert(bank, term->binding, DEREF_NEVER) : term;
TermSetBank(t, bank);
return t;
}
int ignore_args = 0;
if(TermIsAppliedVar(term) && term->args[0]->binding && follow_bind)
{
Term_p binding = term->args[0]->binding;
ignore_args =
TermIsLambda(binding) ? 1 : (binding->arity + (TermIsVar(binding) ? 1 : 0));
DerefType d = DEREF_ONCE;
term = TermDeref(term, &d);
}
if(TermIsVar(term))
{
t = VarBankVarAssertAlloc(bank->vars, term->f_code, term->type);
TermSetBank(t, bank);
}
else
{
Term_p binding_copy = NULL;
if(TFormulaIsQuantified(bank->sig, term))
{
if(term->args[0]->binding)
{
// making sure to avoid capture
binding_copy = term->args[0]->binding;
term->args[0]->binding = NULL;
}
else
{
term->args[0]->binding = VarBankGetFreshVar(bank->vars,
term->args[0]->type);
}
}
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
t->properties = TPIgnoreProps;
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertInstantiatedHO(bank, term->args[i], follow_bind && (i >= ignore_args));
}
t = tb_termtop_insert(bank, t);
if(TFormulaIsQuantified(bank->sig, term))
{
term->args[0]->binding = binding_copy;
}
}
return t;
}
#ifdef ENABLE_LFHO
/*-----------------------------------------------------------------------
//
// Function: TBInsertInstantiated()
//
// Wrapper that chooses which function to call based on the
// problem type.
//
// Global Variables: TBSupportReplace
//
// Side Effects : Changes term bank
//
/----------------------------------------------------------------------*/
__inline__ Term_p TBInsertInstantiated(TB_p bank, Term_p term)
{
if(problemType == PROBLEM_HO)
{
return TBInsertInstantiatedHO(bank, term, true);
}
else
{
return TBInsertInstantiatedFO(bank, term);
}
}
#endif
/*-----------------------------------------------------------------------
//
// Function: TBInsertOpt()
//
// Insert term into bank under the assumption that it it already is
// in the bank (except possibly for variables appearing as
// bindings). This allows us to just return term for ground terms.
//
// Global Variables: -
//
// Side Effects : Changes term bank.
//
/----------------------------------------------------------------------*/
Term_p TBInsertOpt(TB_p bank, Term_p term, DerefType deref)
{
int i;
Term_p t;
assert(term);
const int limit = DEREF_LIMIT(term, deref);
term = TermDeref(term, &deref);
if(TermIsGround(term))
{
assert(TermIsShared(term));
return term;
}
if(TermIsVar(term))
{
t = VarBankVarAssertAlloc(bank->vars, term->f_code, term->type);
TermSetBank(t, bank);
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertOpt(bank, term->args[i], CONVERT_DEREF(i, limit, deref));
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBInsertDisjoint()
//
// Create a copy of (uninstantiated) term with disjoint
// variables. This assumes that all variables in term are odd or
// even, the returned copy will have variable ids shifted by -1.
//
// Global Variables: -
//
// Side Effects : Changes bank.
//
/----------------------------------------------------------------------*/
Term_p TBInsertDisjoint(TB_p bank, Term_p term)
{
int i;
Term_p t;
assert(term);
if(TermIsGround(term))
{
return term;
}
if(TermIsVar(term))
{
t = VarBankGetAltVar(bank->vars, term);
TermSetBank(t, bank);
// t = VarBankVarAssertAlloc(bank->vars, term->f_code+1, term->sort);
}
else
{
t = TermTopCopyWithoutArgs(term); /* This is an unshared term cell at the moment */
assert(SysDateIsCreationDate(t->rw_data.nf_date[0]));
assert(SysDateIsCreationDate(t->rw_data.nf_date[1]));
for(i=0; i<t->arity; i++)
{
t->args[i] = TBInsertDisjoint(bank, term->args[i]);
}
t = tb_termtop_insert(bank, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBTermTopInsert()
//
// See tb_termtop_insert, for export without hurting inlining
// capabilities.
//
// Global Variables: -
//
// Side Effects : Changes bank
//
/----------------------------------------------------------------------*/
Term_p TBTermTopInsert(TB_p bank, Term_p t)
{
return tb_termtop_insert(bank,t);
}
/*-----------------------------------------------------------------------
//
// Function: TBAllocNewSkolem()
//
// Create a news Skolem term (or definition atom) with the given
// variables in the term bank and return the pointer to it.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
Term_p TBAllocNewSkolem(TB_p bank, PStack_p variables, Type_p ret_type)
{
Term_p handle, res;
handle = TermAllocNewSkolem(bank->sig, variables, ret_type);
res = TBInsert(bank, handle, DEREF_NEVER);
TermFree(handle);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TBFind()
//
// Find a term in the term cell bank and return it.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p TBFind(TB_p bank, Term_p term)
{
if(TermIsVar(term))
{
return VarBankFCodeFind(bank->vars, term->f_code);
}
return TermCellStoreFind(&(bank->term_store), term);
}
/*-----------------------------------------------------------------------
//
// Function: TBPrintBankInOrder()
//
// Print the DAG in the order of ascending entry_no.
//
// Global Variables: TBPrintTermsFlat
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
void TBPrintBankInOrder(FILE* out, TB_p bank)
{
NumTree_p tree = NULL;
long i;
PStack_p stack;
Term_p cell;
IntOrP dummy;
for(i=0; i<TERM_STORE_HASH_SIZE; i++)
{
stack = TermTreeTraverseInit(bank->term_store.store[i]);
while((cell = TermTreeTraverseNext(stack)))
{
dummy.p_val = cell;
NumTreeStore(&tree, cell->entry_no,dummy, dummy);
}
TermTreeTraverseExit(stack);
}
tb_print_dag(out, tree, bank->sig);
NumTreeFree(tree);
}
/*-----------------------------------------------------------------------
//
// Function: TBPrintTermCompact()
//
// Print a term bank term. Introduce abbreviations for all subterms
// encountered. Subterms with TPOutputFlag are not
// printed, but are assumed to be known. Does _not_ follow bindings
// (they are temporary and as such make little sense in the term
// bank context)
//
// Global Variables: -
//
// Side Effects : Output, set TPOutputFlag in subterms printed with
// abbreviation.
//
/----------------------------------------------------------------------*/
void TBPrintTermCompact(FILE* out, TB_p bank, Term_p term)
{
int i;
if(TermCellQueryProp(term, TPOutputFlag))
{
fprintf(out, "*%ld", term->entry_no);
}
else
{
if(TermIsVar(term))
{
VarPrint(out, term->f_code);
}
else
{
fprintf(out, "*%ld:", term->entry_no);
TermCellSetProp(term, TPOutputFlag);
fputs(SigFindName(bank->sig, term->f_code), out);
if(!TermIsConst(term))
{
fputc('(',out);
assert(term->arity>0);
TBPrintTermCompact(out, bank, term->args[0]);
for(i=1;i<term->arity;i++)
{
fputc(',', out);
TBPrintTermCompact(out, bank, term->args[i]);
}
fputc(')',out);
}
}
}
}
/*-----------------------------------------------------------------------
//
// Function: TBPrintTerm()
//
// Print a term from a term bank either in compact form (with
// abbreviations) or as a conventional term.
//
// Global Variables: -
//
// Side Effects : By the called functions
//
/----------------------------------------------------------------------*/
void TBPrintTerm(FILE* out, TB_p bank, Term_p term, bool fullterms)
{
if(fullterms)
{
TBPrintTermFull(out, bank, term);
}
else
{
TBPrintTermCompact(out, bank, term);
}
}
/*-----------------------------------------------------------------------
//
// Function: TBPrintBankTerms()
//
// Print the terms inserted into the term bank with abbreviations.
//
// Global Variables: -
//
// Side Effects : Output, changes by TBPrintTermCompact()
//
/----------------------------------------------------------------------*/
void TBPrintBankTerms(FILE* out, TB_p bank)
{
PStack_p stack = PStackAlloc();
Term_p term;
int i;
for(i=0; i<TERM_STORE_HASH_SIZE; i++)
{
PStackPushP(stack, bank->term_store.store[i]);
while(!PStackEmpty(stack))
{
term = PStackPopP(stack);
if(term)
{
PStackPushP(stack, term->lson);
PStackPushP(stack, term->rson);
if(TermCellQueryProp(term, TPTopPos))
{
TBPrintTermCompact(out, bank, term);
fprintf(out, "\n");
}
}
}
}
PStackFree(stack);
}
/*-----------------------------------------------------------------------
//
// Function: TBTermParseReal()
//
// Parse a term from the given scanner object directly into the
// termbank. Supports abbreviations. This function will _not_ set
// the TPTopPos property on top terms while parsing. It will or will
// not check and set symbol properties (function symbol, predicate
// symbol), depending on the check_symb_prop parameter.
//
// Global Variables: -
//
// Side Effects : Input, memory operations, changes termbank.
//
/----------------------------------------------------------------------*/
Term_p TBTermParseReal(Scanner_p in, TB_p bank, bool check_symb_prop)
{
Term_p handle;
DStr_p id;
FuncSymbType id_type;
DStr_p source_name, errpos;
Type_p type;
long line, column;
StreamType type_stream;
source_name = DStrGetRef(AktToken(in)->source);
type_stream = AktToken(in)->stream_type;
line = AktToken(in)->line;
column = AktToken(in)->column;
/* Normal term stuff, bloated because of the nonsensical SETHEO
syntax */
if(SigSupportLists && TestInpTok(in, OpenSquare))
{
handle = tb_parse_cons_list(in, bank, check_symb_prop);
}
else
{
id = DStrAlloc();
if(TestInpTok(in, LetToken))
{
handle = ParseLet(in, bank);
}
else if(TestInpTok(in, IteToken))
{
handle = ParseIte(in, bank);
}
else if((id_type=TermParseOperator(in, id))==FSIdentVar)
{
/* A variable may be annotated with a sort */
if(TestInpTok(in, Colon))
{
AcceptInpTok(in, Colon);
type = TypeBankParseType(in, bank->sig->type_bank);
handle = VarBankExtNameAssertAllocSort(bank->vars,
DStrView(id), type);
}
else
{
handle = VarBankExtNameAssertAlloc(bank->vars, DStrView(id));
}
}
else
{
handle = NULL;
if(TestInpTok(in, OpenBracket))
{
if((id_type == FSIdentInt)
&&(bank->sig->distinct_props & FPIsInteger))
{
AktTokenError(in,
"Number cannot have argument list "
"(consider --free-numbers)",
false);
}
if((id_type == FSIdentFloat)
&&(bank->sig->distinct_props & FPIsFloat))
{
AktTokenError(in,
"Floating point number cannot have argument list "
"(consider --free-numbers)",
false);
}
if((id_type == FSIdentRational)
&&(bank->sig->distinct_props & FPIsRational))
{
AktTokenError(in,
"Rational number cannot have argument list "
"(consider --free-numbers)",
false);
}
if((id_type == FSIdentObject)
&&(bank->sig->distinct_props & FPIsObject))
{
AktTokenError(in,
"Object cannot have argument list "
"(consider --free-objects)",
false);
}
// in TFX all symbols must be declared beforehand
// thus, if we have a formula at the argument, symbol with name
// id already has a type declared with $o in appropriate places
FunCode sym_code = SigFindFCode(bank->sig, DStrView(id));
Type_p sym_type = sym_code ? SigGetType(bank->sig, sym_code) : NULL;
handle = tb_term_parse_arglist(in, bank,
check_symb_prop, sym_type);
}
else
{
handle = TermDefaultCellAlloc();
handle->arity = 0;
}
handle->f_code = TermSigInsert(bank->sig, DStrView(id),
handle->arity, false, id_type);
if(!handle->f_code)
{
errpos = DStrAlloc();
DStrAppendStr(errpos, PosRep(type_stream, source_name, line, column));
DStrAppendStr(errpos, DStrView(id));
DStrAppendStr(errpos, " used with arity ");
DStrAppendInt(errpos, (long)handle->arity);
DStrAppendStr(errpos, ", but registered with arity ");
DStrAppendInt(errpos,
(long)(bank->sig)->
f_info[SigFindFCode(bank->sig, DStrView(id))].arity);
Error(DStrView(errpos), SYNTAX_ERROR);
DStrFree(errpos);
}
handle = tb_termtop_insert(bank, handle);
}
DStrFree(id);
}
DStrReleaseRef(source_name);
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: TBRefSetProp()
//
// Make ref point to a term of the same structure as *ref, but with
// properties prop set. Properties do not work for variables!
//
// Global Variables: -
//
// Side Effects : Changes bank, *ref
//
/----------------------------------------------------------------------*/
void TBRefSetProp(TB_p bank, TermRef ref, TermProperties prop)
{
Term_p term, new;
assert(!TermIsVar(*ref));
term = *ref;
if(TermCellQueryProp(term, prop)||TermIsVar(term))
{
return;
}
new = TermTopCopy(term);
TermCellSetProp(new, prop);
new = tb_termtop_insert(bank, new);
*ref = new;
/* Old term will be garbage-collected eventually */
}
/*-----------------------------------------------------------------------
//
// Function: TBRefDelProp()
//
// Make ref point to a term of the same structure as *ref, but with
// properties prop deleted. If the term is a variable, do nothing!
//
// Global Variables: -
//
// Side Effects : Changes bank, *ref
//
/----------------------------------------------------------------------*/
void TBRefDelProp(TB_p bank, TermRef ref, TermProperties prop)
{
Term_p term, new;
term = *ref;
if(!TermCellIsAnyPropSet(term, prop)||TermIsVar(term))
{
return;
}
new = TermTopCopy(term);
TermCellDelProp(new, prop);
new = tb_termtop_insert(bank, new);
*ref = new;
}
/*-----------------------------------------------------------------------
//
// Function: TBTermDelPropCount()
//
// Delete properties prop in term, return number of term cells with
// this property. Does assume that all subterms of a term without
// this property also do not carry it!
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
long TBTermDelPropCount(Term_p term, TermProperties prop)
{
long count = 0;
int i;
PStack_p stack = PStackAlloc();
PStackPushP(stack, term);
while(!PStackEmpty(stack))
{
term = PStackPopP(stack);
if(TermCellQueryProp(term, prop))
{
TermCellDelProp(term, prop);
count++;
for(i=0; i<term->arity; i++)
{
PStackPushP(stack, term->args[i]);
}
}
}
PStackFree(stack);
return count;
}
/*-----------------------------------------------------------------------
//
// Function: TBGCMarkTerm()
//
// Mark a term as used for the garbage collector.
//
// Global Variables: -
//
// Side Effects : Marks the term, memory operations
//
/----------------------------------------------------------------------*/
void TBGCMarkTerm(TB_p bank, Term_p term)
{
PStack_p stack = PStackAlloc();
int i;
assert(bank);
assert(term);
PStackPushP(stack, term);
while(!PStackEmpty(stack))
{
term = PStackPopP(stack);
if(!TBTermCellIsMarked(bank,term))
{
TermCellFlipProp(term, TPGarbageFlag);
for(i=0; i<term->arity; i++)
{
PStackPushP(stack, term->args[i]);
}
if(TermIsRewritten(term))
{
PStackPushP(stack, TermRWReplaceField(term));
}
if(TermIsAppliedVar(term) && TermGetCache(term))
{
PStackPushP(stack, TermGetCache(term));
}
}
}
PStackFree(stack);
}
/*-----------------------------------------------------------------------
//
// Function: TBGCSweep()
//
// Sweep the term bank and free all unmarked term
// cells. bank->true_term will be marked automatically. Returns the
// number of term cells recovered.
//
// Global Variables: -
//
// Side Effects : Memory operations, flips bank->garbage_state
//
/----------------------------------------------------------------------*/
long TBGCSweep(TB_p bank)
{
long recovered = 0, i;
Term_p t;
assert(bank);
assert(!TermIsRewritten(bank->true_term));
TBGCMarkTerm(bank, bank->true_term);
TBGCMarkTerm(bank, bank->false_term);
for(i=0; i< bank->min_terms->size; i++)
{
t = PDArrayElementP(bank->min_terms, i);
if(t)
{
TBGCMarkTerm(bank, t);
}
}
VERBOUT("Garbage collection started.\n");
recovered = TermCellStoreGCSweep(&(bank->term_store),
bank->garbage_state);
VERBOSE(fprintf(stderr, "Garbage collection reclaimed %ld unused term cells.\n",recovered););
/* #ifdef PRINT_SOMEERRORS_STDOUT */
#ifdef NEVER_DEFINED
if(OutputLevel)
{
fprintf(GlobalOut,
"# Garbage collection reclaimed %ld unused term cells.\n",
recovered);
}
#endif
bank->garbage_state =
bank->garbage_state?TPIgnoreProps:TPGarbageFlag;
return recovered;
}
/*-----------------------------------------------------------------------
//
// Function: TBCreateConstTerm()
//
// Create constant term for a given symbol.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p TBCreateConstTerm(TB_p bank, FunCode fconst)
{
Term_p res,
t = TermConstCellAlloc(fconst);
res = TBInsert(bank, t, DEREF_NEVER);
TermFree(t);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TBCreateMinTerm()
//
// If bank->min_term exists, return it. Otherwise create and return
// it.
//
// Global Variables: -
//
// Side Effects : Might set bank->min_term
//
/----------------------------------------------------------------------*/
Term_p TBCreateMinTerm(TB_p bank, FunCode min_const)
{
Type_p type = SigGetType(bank->sig, min_const);
long sort = GetReturnSort(type)->type_uid;
Term_p t = PDArrayElementP(bank->min_terms, sort);
if(!t)
{
t = TBCreateConstTerm(bank, min_const);
PDArrayAssignP(bank->min_terms, sort, t);
}
return t;
}
/*-----------------------------------------------------------------------
//
// Function: TBTermCollectSubterms()
//
// Collect all subterms of term onto collector. Assumes that
// TPOpFlag is set if and only if the term is already in the
// collection. Returns the number of new terms found.
//
// Global Variables: -
//
// Side Effects : Sets the OpFlag of newly collected terms.
//
/----------------------------------------------------------------------*/
long TBTermCollectSubterms(Term_p term, PStack_p collector)
{
long res = 0;
int i;
assert(term);
assert(TermIsShared(term));
if(!TermCellQueryProp(term, TPOpFlag))
{
res = 1;
TermCellSetProp(term, TPOpFlag);
PStackPushP(collector, term);
for(i=0; i<term->arity; i++)
{
res += TBTermCollectSubterms(term->args[i], collector);
}
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TBFindRepr()
//
// Find the representation of a term from another (or none) bank in
// this bank.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p TBFindRepr(TB_p bank, Term_p term)
{
int i;
Term_p work;
Term_p repr;
if (TermIsVar(term) || TermIsConst(term))
{
return TBFind(bank, term);
}
work = TermTopCopy(term);
for (i=0; i<work->arity; i++)
{
work->args[i] = TBFindRepr(bank, term->args[i]);
}
repr = TBFind(bank, work);
TermTopFree(work);
return repr;
}
/*-----------------------------------------------------------------------
//
// Function: TBGetFirstConstTerm()
//
// Return a constant term with the first constant of the proper
// sort in sig.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
Term_p TBGetFirstConstTerm(TB_p bank, Type_p type)
{
PStack_p cand_stack = PStackAlloc();
Term_p res = NULL;
SigCollectSortConsts(bank->sig, type, cand_stack);
if(!PStackEmpty(cand_stack))
{
res = TBCreateConstTerm(bank, PStackElementInt(cand_stack, 0));
}
PStackFree(cand_stack);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TBGetFreqConstTerm()
//
// Find the best (according to is_better) constant of the give sort,
// and return a shared term with this constant. If no suitable
// constant exists, returns NULL. conj_dist_array contains number of
// occurrences for each symbol in conjecture clauses, dist_array the
// same for all clauses.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Term_p TBGetFreqConstTerm(TB_p terms, Type_p type,
long* conj_dist_array,
long* dist_array, FunConstCmpFunType is_better)
{
Term_p res = NULL;
long cand_no;
PStackPointer i;
FunCode f = 0, cand;
PStack_p candidates = PStackAlloc();
cand_no = SigCollectSortConsts(terms->sig, type, candidates);
if(cand_no)
{
f = PStackElementInt(candidates, 0);
for(i=1; i<PStackGetSP(candidates); i++)
{
cand = PStackElementInt(candidates, i);
if(is_better(cand, f, conj_dist_array, dist_array))
{
f = cand;
}
}
res = TBCreateConstTerm(terms, f);
}
PStackFree(candidates);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TermMap()
//
// Applies the function f to term t to obtain t'. If t' != t,
// it continues mapping t'. Else, it recursively applies f to
// arguments of t. Result term is guaranteed to be shared.
// Term mapper must also return shared term of the same type
// as the original one.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
Term_p TermMap(TB_p bank, Term_p t, TermMapper f)
{
Term_p s = f(bank, t);
assert(TermIsShared(s) && s->type == t->type);
if(s!=t)
{
s = TermMap(bank, s, f);
}
else
{
bool changed = false;
s = TermTopCopyWithoutArgs(t);
for(int i=0; i < t->arity; i++)
{
s->args[i] = TermMap(bank, t->args[i], f);
assert(TermIsShared(s->args[i]) && s->args[i]->type == t->args[i]->type);
changed = changed || (s->args[i] != t->args[i]);
}
if(changed)
{
s = TBTermTopInsert(bank, s);
}
else
{
TermTopFree(s);
s = t;
}
}
return s;
}
/*-----------------------------------------------------------------------
//
// Function: ParseLet()
//
// Parses let according to the TPTP description:
// http://ceur-ws.org/Vol-2162/paper-07.pdf. If top_level is true,
// let appears at the formula level and its body must be Bool.
// Otherwise, its body is parsed as a non-Bool.
//
// Global Variables: -
//
// Side Effects :
//
/----------------------------------------------------------------------*/
Term_p ParseLet(Scanner_p in, TB_p bank)
{
AcceptInpTok(in, LetToken);
AcceptInpTok(in, OpenBracket);
PStack_p type_decls = PStackAlloc();
/* parsing type declarations */
if(TestInpTok(in, OpenSquare))
{
AcceptInpTok(in, OpenSquare);
parse_let_typedecl(in, bank, type_decls);
while(TestInpTok(in, Comma))
{
AcceptInpTok(in, Comma);
parse_let_typedecl(in, bank, type_decls);
}
AcceptInpTok(in, CloseSquare);
}
else
{
parse_let_typedecl(in, bank, type_decls);
}
AcceptInpTok(in, Comma);
/* parsing symbol definitions */
PStack_p definitions = PStackAlloc();
if(TestInpTok(in, OpenSquare))
{
AcceptInpTok(in, OpenSquare);
PStackPushP(definitions, parse_let_sym_def(in, bank, type_decls));
while(TestInpTok(in, Comma))
{
AcceptInpTok(in, Comma);
PStackPushP(definitions, parse_let_sym_def(in, bank, type_decls));
}
AcceptInpTok(in, CloseSquare);
}
else
{
PStackPushP(definitions, parse_let_sym_def(in, bank, type_decls));
}
AcceptInpTok(in, Comma);
SigEnterLetScope(bank->sig, type_decls);
Term_p body = TFormulaTPTPParse(in, bank);
SigExitLetScope(bank->sig);
Term_p let_term = make_let(bank, definitions, body);
while(!(PStackEmpty(type_decls)))
{
UNUSED(PStackPopP(type_decls));
DStrFree(PStackPopP(type_decls));
}
AcceptInpTok(in, CloseBracket);
PStackFree(type_decls);
PStackFree(definitions);
return let_term;
}
/*-----------------------------------------------------------------------
//
// Function: ParseIte()
//
// Parses ite according to the TPTP description:
// http://ceur-ws.org/Vol-2162/paper-07.pdf. If top_level is true,
// ite appears at the formula level and its body must be Bool.
// Otherwise, its body is parsed as a non-Bool.
//
// Global Variables: -
//
// Side Effects :
//
/----------------------------------------------------------------------*/
Term_p ParseIte(Scanner_p in, TB_p bank)
{
AcceptInpTok(in, IteToken);
AcceptInpTok(in, OpenBracket);
Term_p cond = TFormulaTPTPParse(in, bank);
AcceptInpTok(in, Comma);
Term_p if_true = TFormulaTPTPParse(in, bank);
AcceptInpTok(in, Comma);
Term_p if_false = TFormulaTPTPParse(in, bank);
AcceptInpTok(in, CloseBracket);
Term_p res = TermTopAlloc(SIG_ITE_CODE, 3);
res->args[0] = cond;
res->args[1] = if_true;
res->args[2] = if_false;
TermAssertSameSort(bank->sig, cond, bank->true_term);
TermAssertSameSort(bank->sig, if_false, if_true);
res->type = if_true->type;
return TBTermTopInsert(bank, res);
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|