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
|
/*-----------------------------------------------------------------------
File : ccl_clausefunc.c
Author: Stephan Schulz
Contents
Clause functions that need to know about sets.
Copyright 1998-2018 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: New, partitioned ccl_clausesets.h
-----------------------------------------------------------------------*/
#include "ccl_clausefunc.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: unif_all_pairs()
//
// Assuming that stack contains [s1, t1, s2, t2, ..., sn, tn]
// computes simultaneous unifier of s1 =?= t1, ..., sn =?= tn
// and stores it in subst.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
bool unif_all_pairs(PStack_p pairs, Subst_p subst)
{
assert(PStackGetSP(pairs) % 2 == 0);
PStackPointer pos = PStackGetSP(subst);
bool unifies = true;
while(unifies && !PStackEmpty(pairs))
{
Term_p s = PStackPopP(pairs);
Term_p t = PStackPopP(pairs);
unifies = SubstMguComplete(s, t, subst);
}
if (!unifies)
{
SubstBacktrackToPos(subst, pos);
}
return unifies;
}
/*-----------------------------------------------------------------------
//
// Function: collect_free_vars()
//
// Returns free variables of term t, except in subterm t|idx_to_skip.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
PTree_p collect_free_vars(Term_p t, TB_p bank, int idx_to_skip)
{
assert(idx_to_skip < t->arity);
PTree_p res = NULL;
TFormulaCollectFreeVars(bank, t, &res);
if (TermIsVar(t->args[idx_to_skip]))
{
bool removed = PTreeDeleteEntry(&res, t->args[idx_to_skip]);
UNUSED(removed); // stiffle the warning in non-debug version
assert(removed);
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseCanonCompareRef()
//
/// Compare two indirectly pointed to clauses with
// ClauseStructWeightLexCompare().
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
int ClauseCanonCompareRef(const void *clause1ref, const void* clause2ref)
{
const Clause_p* c1 = clause1ref;
const Clause_p* c2 = clause2ref;
return CMP(ClauseStructWeightLexCompare(*c1, *c2),0);
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: ClauseRemoveLiteralRef()
//
// Remove *lit from clause, adjusting counters as necessary.
//
// Global Variables: -
//
// Side Effects : Changes clause and possibly clause->set->literals
//
/----------------------------------------------------------------------*/
void ClauseRemoveLiteralRef(Clause_p clause, Eqn_p *lit)
{
Eqn_p handle = *lit;
if(EqnIsPositive(handle))
{
clause->pos_lit_no--;
}
else
{
clause->neg_lit_no--;
}
if(clause->set)
{
clause->set->literals--;
}
clause->weight -= EqnStandardWeight(handle);
EqnListDeleteElement(lit);
}
/*-----------------------------------------------------------------------
//
// Function: ClauseRemoveLiteral()
//
// Remove lit from clause, adjusting counters as necessary. This is
// a lot less efficient then ClauseRemoveLiteralRef(), as we have to
// search for the literal.
//
// Global Variables: -
//
// Side Effects : Changes clause and possibly clause->set->literals
//
/----------------------------------------------------------------------*/
void ClauseRemoveLiteral(Clause_p clause, Eqn_p lit)
{
EqnRef handle = &(clause->literals);
while(*handle!=lit)
{
assert(*handle);
handle = &((*handle)->next);
}
ClauseRemoveLiteralRef(clause, handle);
}
/*-----------------------------------------------------------------------
//
// Function: ClauseFlipLiteralSign()
//
// Change the sign of lit, adjusting counters as necessary.
//
// Global Variables: -
//
// Side Effects : Changes clause.
//
/----------------------------------------------------------------------*/
void ClauseFlipLiteralSign(Clause_p clause, Eqn_p lit)
{
if(EqnIsPositive(lit))
{
clause->pos_lit_no--;
clause->neg_lit_no++;
}
else
{
clause->neg_lit_no--;
clause->pos_lit_no++;
}
EqnFlipProp(lit, EPIsPositive);
}
/*-----------------------------------------------------------------------
//
// Function: ClauseRemoveSuperfluousLiterals()
//
// Remove duplicate and trivial negative literals from the
// clause. Return number of removed literals.
//
// Global Variables: -
//
// Side Effects : Changes clause, termbanks
//
/----------------------------------------------------------------------*/
int ClauseRemoveSuperfluousLiterals(Clause_p clause)
{
Eqn_p handle;
int removed = 0;
assert(!ClauseIsAnyPropSet(clause, CPIsDIndexed|CPIsSIndexed));
removed += EqnListRemoveResolved(&(clause->literals));
removed += EqnListRemoveDuplicates(clause->literals);
if(removed)
{
clause->neg_lit_no = 0;
clause->pos_lit_no = 0;
handle = clause->literals;
ClauseDelProp(clause, CPInitial|CPLimitedRW);
while(handle)
{
if(EqnIsPositive(handle))
{
clause->pos_lit_no++;
}
else
{
clause->neg_lit_no++;
}
handle = handle->next;
}
if(clause->set)
{
clause->set->literals-=removed;
}
}
if(removed)
{
ClausePushDerivation(clause, DCNormalize, NULL, NULL);
}
return removed;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetRemoveSuperflousLiterals()
//
// For all clauses in set remove the trivial and duplicated
// literals. Return number of literals removed.
//
// Global Variables: -
//
// Side Effects : Only as described
//
/----------------------------------------------------------------------*/
long ClauseSetRemoveSuperfluousLiterals(ClauseSet_p set)
{
Clause_p handle;
long res = 0;
for(handle = set->anchor->succ; handle!=set->anchor; handle =
handle->succ)
{
res += ClauseRemoveSuperfluousLiterals(handle);
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetCanonize()
//
// Canonize a clause set by canonizing all
// clauses, and sorting them in the order defined by
// ClauseStructWeightLexCompare().
//
// Global Variables: -
//
// Side Effects : Memory usage.
//
/----------------------------------------------------------------------*/
void ClauseSetCanonize(ClauseSet_p set)
{
Clause_p handle;
for(handle = set->anchor->succ; handle!= set->anchor;
handle = handle->succ)
{
ClauseRemoveSuperfluousLiterals(handle);
ClauseCanonize(handle);
}
ClauseSetSort(set, ClauseCanonCompareRef);
/* printf("Canonized: \n");
ClauseSetPrint(stdout, set, true); */
}
/*-----------------------------------------------------------------------
//
// Function: ClauseRemoveACResolved()
//
// Remove AC-resolved literals.
//
// Global Variables: -
//
// Side Effects : As above
//
/----------------------------------------------------------------------*/
int ClauseRemoveACResolved(Clause_p clause)
{
int removed = 0;
Sig_p sig;
if(clause->neg_lit_no==0)
{
return 0;
}
sig = clause->literals->bank->sig;
removed += EqnListRemoveACResolved(&(clause->literals));
clause->neg_lit_no -= removed ;
if(removed)
{
ClauseDelProp(clause, CPInitial|CPLimitedRW);
DocClauseModification(GlobalOut, OutputLevel, clause,
inf_ac_resolution, NULL, sig, NULL);
ClausePushACResDerivation(clause, sig);
}
if(clause->set)
{
clause->set->literals-=removed;
}
return removed;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseUnitSimplifyTest()
//
// Return true if clause can be simplified by a top-simplify-reflect
// step with the (non-orientable) unit clause simplifier.
//
// Global Variables: -
//
// Side Effects : Changes clause and values in set
//
/----------------------------------------------------------------------*/
bool ClauseUnitSimplifyTest(Clause_p clause, Clause_p simplifier)
{
bool positive,tmp;
EqnRef handle;
Eqn_p simpl;
assert(ClauseIsUnit(simplifier));
simpl = simplifier->literals;
assert(EqnIsNegative(simpl)||!EqnIsOriented(simpl));
positive = EqnIsPositive(simpl);
if(EQUIV(positive, ClauseIsPositive(clause)))
{
return 0;
}
handle = &(clause->literals);
while(*handle)
{
tmp = EqnIsPositive(*handle);
if(XOR(positive,tmp)&&EqnSubsumeP(simpl,*handle))
{
return true;
}
handle = &((*handle)->next);
}
return false;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseArchive()
//
// Move clause into the archive. Create a fresh copy pointing to the
// old clause in its derivation and return it. Also set the
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
Clause_p ClauseArchive(ClauseSet_p archive, Clause_p clause)
{
Clause_p newclause;
assert(archive);
assert(clause);
newclause = ClauseFlatCopy(clause);
ClausePushDerivation(newclause, DCCnfQuote, clause, NULL);
ClauseSetInsert(archive, clause);
return newclause;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseArchiveCopy()
//
// Create an archive copy of clause in archive. The
// archive copy inherits info and derivation. The original loses
// info, and gets a new derivation that points to the archive
// copy. Returns pointer to the archived copy.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
Clause_p ClauseArchiveCopy(ClauseSet_p archive, Clause_p clause)
{
Clause_p archclause;
assert(archive);
assert(clause);
archclause = ClauseFlatCopy(clause);
archclause->info = clause->info;
archclause->derivation = clause->derivation;
clause->info = NULL;
clause->derivation = NULL;
ClausePushDerivation(clause, DCCnfQuote, archclause, NULL);
ClauseSetInsert(archive, archclause);
return archclause;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetArchiveCopy()
//
// Create an archive copy of each clause in set in archive. The
// archive copy inherits info and derivation. The original loses
// info, and gets a new derivation that points to the archive copy.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void ClauseSetArchiveCopy(ClauseSet_p archive, ClauseSet_p set)
{
Clause_p handle;
assert(archive);
assert(set);
for(handle = set->anchor->succ; handle!= set->anchor;
handle = handle->succ)
{
ClauseArchiveCopy(archive, handle);
}
}
/*-----------------------------------------------------------------------
//
// Function:ClauseIsOrphaned()
//
// Return true if the clause is orphaned, i.e. if one of the direct
// premises of the original generating inferences that generated it
// has been back-simplified.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
bool ClauseIsOrphaned(Clause_p clause)
{
PStackPointer i = 1;
DerivationCode op;
Clause_p parent;
assert(clause);
//clause = follow_quote_chain(clause);
if(!clause->derivation)
{
return false;
}
if(PStackEmpty(clause->derivation))
{
return false;
}
op = PStackElementInt(clause->derivation, 0);
if(!DCOpIsGenerating(op))
{
return false;
}
if(DCOpHasCnfArg1(op))
{
parent = PStackElementP(clause->derivation, 1);
if(ClauseQueryProp(parent,CPIsDead))
{
return true;
}
i++;
}
if(DCOpHasCnfArg2(op))
{
parent = PStackElementP(clause->derivation, 2);
if(ClauseQueryProp(parent,CPIsDead))
{
return true;
}
i++;
}
while(i<PStackGetSP(clause->derivation) &&
((op = PStackElementInt(clause->derivation, i))==DCCnfAddArg))
{
i++;
parent = PStackElementP(clause->derivation, i);
if(ClauseQueryProp(parent,CPIsDead))
{
return true;
}
i++;
}
return false;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetDeleteOrphans()
//
// Remove all orphaned clauses, returning the number of clauses
// eliminated.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long ClauseSetDeleteOrphans(ClauseSet_p set)
{
Clause_p handle;
assert(set);
assert(!set->demod_index);
handle = set->anchor->succ;
while(handle != set->anchor)
{
if(ClauseIsOrphaned(handle))
{
ClauseSetProp(handle,CPDeleteClause);
}
else
{
ClauseDelProp(handle,CPDeleteClause);
}
handle = handle->succ;
}
return ClauseSetDeleteMarkedEntries(set);
}
/*-----------------------------------------------------------------------
//
// Function: PStackClausePrint()
//
// Print the clauses on the stack.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void PStackClausePrint(FILE* out, PStack_p stack, char* extra)
{
PStackPointer i;
Clause_p clause;
for(i=0; i<PStackGetSP(stack); i++)
{
clause = PStackElementP(stack, i);
ClausePrint(out, clause, true);
if(extra)
{
fprintf(out, "%s", extra);
}
fputc('\n', out);
}
}
/*-----------------------------------------------------------------------
//
// Function: ClauseEliminateNakedBooleanVariables()
//
// If the clause containts boolean variables X and ~X, convert the
// clause to {$true}. If the clause C contains only X replace the
// clause with C[X |-> $false]. If the clause C contains only ~X replace
// C with C[X |-> $true].
//
// Global Variables: -
//
// Side Effects : literals field may be changed
//
/----------------------------------------------------------------------*/
bool ClauseEliminateNakedBooleanVariables(Clause_p clause)
{
assert(!ClauseIsEmpty(clause));
PStack_p all_lits = ClauseToStack(clause);
Eqn_p lit = NULL;
Term_p var = NULL;
bool eliminated_var = false;
const TB_p bank = clause->literals->bank;
const Term_p true_term = bank->true_term;
const Term_p false_term = bank->false_term;
Eqn_p res = NULL;
Subst_p subst = SubstAlloc();
while(!PStackEmpty(all_lits))
{
lit = PStackPopP(all_lits);
if(EqnIsBoolVar(lit))
{
assert(TermIsVar(lit->lterm));
var = lit->lterm;
if(EqnIsPositive(lit))
{
if(var->binding && var->binding == true_term)
{
// there was a negative equation previously that bound
// this variable -- which means we have X and ~X.
EqnListFree(clause->literals);
clause->literals = EqnCreateTrueLit(bank);
assert(eliminated_var);
break;
}
else
{
if(!var->binding)
{
SubstAddBinding(subst, var, false_term);
}
EqnDelProp(lit, EPIsPositive);
lit->lterm = true_term; // now lit becomes false and will be deleted
eliminated_var = true;
}
}
else
{
if(var->binding && var->binding == false_term)
{
// analogous to the previous case
EqnListFree(clause->literals);
clause->literals = EqnCreateTrueLit(bank);
assert(eliminated_var);
break;
}
else
{
if(!var->binding)
{
SubstAddBinding(subst, var, true_term);
}
lit->lterm = true_term;
eliminated_var = true;
}
}
}
}
if(eliminated_var)
{
res = EqnListCopyOpt(clause->literals);
EqnListFree(clause->literals);
clause->literals = res;
ClauseRemoveSuperfluousLiterals(clause);
}
PStackFree(all_lits);
SubstDelete(subst);
return eliminated_var;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseRecognizeInjectivity()
//
// Create a clause that postulates existence of an inverse function
// for a given expression. In other words:
//
// f X_1 ... X_n != f Y1 ... Y_n \/ X_i = Y_i
// -------------------------------------------------------
// inv_f_i(f sigma(X_1) ... X_i ... sigma(X_n)) = X_i
//
// where for some subset I of indexes from 1 to n, X_i = Y_i
// for each i in I, and for complementary set of indexes J
// there all X_j and Y_j (j \in J) and different from all X_i
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Clause_p ClauseRecognizeInjectivity(TB_p terms, Clause_p clause)
{
assert(clause);
Clause_p res = NULL;
if(clause->pos_lit_no == 1 && clause->neg_lit_no == 1)
{
Eqn_p pos_lit = EqnIsPositive(clause->literals) ?
clause->literals : clause->literals->next;
Eqn_p neg_lit = EqnIsNegative(clause->literals) ?
clause->literals : clause->literals->next;
assert(EqnIsPositive(pos_lit));
assert(EqnIsNegative(neg_lit));
if (EqnIsEquLit(pos_lit) && EqnIsEquLit(neg_lit) &&
TermIsVar(pos_lit->lterm) && TermIsVar(pos_lit->rterm) &&
pos_lit->lterm != pos_lit->rterm &&
!TermIsTopLevelVar(neg_lit->lterm) && !TermIsTopLevelVar(neg_lit->rterm)
&& neg_lit->lterm->f_code == neg_lit->rterm->f_code
&& !TypeIsArrow(neg_lit->lterm->type)
&& !SigQueryFuncProp(neg_lit->bank->sig, neg_lit->lterm->f_code, FPIsInjDefSkolem)
&& TermStandardWeight(neg_lit->lterm) == TermStandardWeight(neg_lit->rterm)
&& TermStandardWeight(neg_lit->lterm)
== (DEFAULT_FWEIGHT + neg_lit->lterm->arity*DEFAULT_VWEIGHT)
&& neg_lit->lterm->arity > 0)
{
assert(neg_lit->lterm->arity == neg_lit->rterm->arity);
int idx_var_occ = -1; // index where X or Y appears in f s1 ... X ... sn
for (int i=0; idx_var_occ == -1 && i<neg_lit->lterm->arity; i++)
{
if (neg_lit->lterm->args[i] == pos_lit->lterm &&
neg_lit->rterm->args[i] == pos_lit->rterm)
{
idx_var_occ = i;
}
else if (neg_lit->lterm->args[i] == pos_lit->rterm &&
neg_lit->rterm->args[i] == pos_lit->lterm)
{
idx_var_occ = i;
}
}
if (idx_var_occ != -1)
{
bool rule_applicable = true;
// TPCheckFlag means that the variable appears as X_i = X_i
// TPOpFlag means that the varaible appears as X_i = Y_i
TermDelProp(neg_lit->lterm, DEREF_NEVER, TPOpFlag|TPCheckFlag);
TermDelProp(neg_lit->rterm, DEREF_NEVER, TPOpFlag|TPCheckFlag);
PStack_p skolem_vars = PStackAlloc();
for(int i=0; rule_applicable && i<neg_lit->lterm->arity; i++)
{
Term_p lvar = neg_lit->lterm->args[i], rvar = neg_lit->rterm->args[i];
assert(TermIsVar(lvar));
assert(TermIsVar(rvar));
if (lvar == rvar)
{
if (TermCellQueryProp(lvar, TPCheckFlag) ||
TermCellQueryProp(rvar, TPCheckFlag))
{
/* Variable can appear again only if it is equal to its rhs-pair */
rule_applicable = false;
}
else if (!TermCellQueryProp(lvar, TPOpFlag))
{
/* We are seeing the variable for the first time */
TermCellSetProp(lvar, TPOpFlag);
PStackPushP(skolem_vars, lvar);
}
}
else
{
if (TermCellIsAnyPropSet(lvar, TPCheckFlag|TPOpFlag) ||
TermCellIsAnyPropSet(rvar, TPCheckFlag|TPOpFlag))
{
/* Variable that is different from tis rhs-pair cannot appear again */
rule_applicable = false;
}
else
{
TermCellSetProp(lvar, TPCheckFlag);
TermCellSetProp(rvar, TPCheckFlag);
}
}
}
// reset term properties again
TermDelProp(neg_lit->lterm, DEREF_NEVER, TPOpFlag|TPCheckFlag);
TermDelProp(neg_lit->rterm, DEREF_NEVER, TPOpFlag|TPCheckFlag);
if (rule_applicable)
{
// substitution did not bind X or Y
assert(!pos_lit->lterm->binding);
assert(!pos_lit->rterm->binding);
Term_p inverse_arg = neg_lit->lterm;
Term_p inverse_var = neg_lit->lterm->args[idx_var_occ];
int vars_num = PStackGetSP(skolem_vars);
Term_p inv_skolem_term; // let the compiler check if it is initialized
if (vars_num)
{
Type_p* arg_tys = TypeArgArrayAlloc(vars_num+1);
Term_p* args = TermArgTmpArrayAlloc(vars_num+1);
for(int i=0; i<vars_num; i++)
{
args[i] = PStackElementP(skolem_vars, i);
arg_tys[i] = args[i]->type;
}
args[vars_num] = inverse_arg;
arg_tys[vars_num] = inverse_arg->type;
FunCode new_inv_skolem_sym =
SigGetNewTypedSkolem(terms->sig, arg_tys, vars_num+1, pos_lit->lterm->type);
SigSetFuncProp(terms->sig, new_inv_skolem_sym, FPIsInjDefSkolem);
inv_skolem_term = TermTopAlloc(new_inv_skolem_sym, vars_num+1);
for(int i=0; i<vars_num+1; i++)
{
inv_skolem_term->args[i] = args[i];
}
inv_skolem_term->type = pos_lit->lterm->type;
inv_skolem_term = TBTermTopInsert(terms, inv_skolem_term);
TypeArgArrayFree(arg_tys, vars_num+1);
TermArgTmpArrayFree(args, vars_num+1);
}
else
{
Type_p args[1] = {neg_lit->lterm->type};
FunCode new_inv_skolem_sym =
SigGetNewTypedSkolem(terms->sig, args, 1, pos_lit->lterm->type);
SigSetFuncProp(terms->sig, new_inv_skolem_sym, FPIsInjDefSkolem);
inv_skolem_term = TermTopAlloc(new_inv_skolem_sym, 1);
inv_skolem_term->args[0] = inverse_arg;
inv_skolem_term->type = pos_lit->lterm->type;
inv_skolem_term = TBTermTopInsert(terms, inv_skolem_term);
}
Eqn_p eqn = EqnAlloc(inv_skolem_term, inverse_var, terms, true);
res = ClauseAlloc(eqn);
res->proof_depth = clause->proof_depth+1;
res->proof_size = clause->proof_size+1;
ClauseSetTPTPType(res, ClauseQueryTPTPType(clause));
ClauseSetProp(res, ClauseGiveProps(clause, CPIsSOS));
ClauseSetProp(res, CPIsPureInjectivity);
// TODO: Clause documentation is not implemented at the moment.
// DocClauseCreationDefault(clause, inf_efactor, clause, NULL);
ClausePushDerivation(res, DCInvRec, clause, NULL);
}
PStackFree(skolem_vars);
}
}
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetInjectivityIsDefined()
//
// Finds definitions of skolem symbols standing for the renaming of
// the injectivity axiom.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
bool ClauseSetInjectivityIsDefined(ClauseSet_p all_defs, Clause_p inj_def)
{
assert(inj_def->pos_lit_no == 1 && inj_def->neg_lit_no == 0);
assert(all_defs);
bool res = false;
TB_p bank = inj_def->literals->bank;
Term_p lhs = TBInsertDisjoint(bank, inj_def->literals->lterm);
Term_p rhs = TBInsertDisjoint(bank, inj_def->literals->rterm);
Clause_p iter = all_defs->anchor->succ;
while(!res && iter != all_defs->anchor)
{
assert(iter);
assert(iter->pos_lit_no == 1 && iter->neg_lit_no == 0);
Term_p cand_lhs = iter->literals->lterm, cand_rhs = iter->literals->rterm;
if (cand_lhs->arity == lhs->arity)
{
PStack_p unif_stack = PStackAlloc();
Subst_p subst = SubstAlloc();
PStackPushP(unif_stack, rhs);
PStackPushP(unif_stack, cand_rhs);
for(int i=0; i<cand_lhs->arity; i++)
{
PStackPushP(unif_stack, lhs->args[i]);
PStackPushP(unif_stack, cand_lhs->args[i]);
}
if (unif_all_pairs(unif_stack, subst) && SubstIsRenaming(subst))
{
res = true;
}
PStackFree(unif_stack);
SubstDelete(subst);
}
iter = iter->succ;
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ReplaceInjectivityDefs()
//
// Replaces defintions of injectivity by clauses that
// define inverse operators.
//
// Global Variables: -
//
// Side Effects : Changes set and term bank.
//
/----------------------------------------------------------------------*/
long ClauseSetReplaceInjectivityDefs(ClauseSet_p set, ClauseSet_p archive, TB_p terms)
{
Clause_p handle, next;
ClauseSet_p tmp = ClauseSetAlloc();
long count = 0;
assert(set);
assert(!set->demod_index);
handle = set->anchor->succ;
while(handle != set->anchor)
{
assert(handle);
next = handle->succ;
Clause_p repl = ClauseRecognizeInjectivity(terms, handle);
if(repl)
{
if (ClauseQueryProp(repl, CPIsPureInjectivity) &&
!ClauseSetInjectivityIsDefined(tmp, repl))
{
ClauseSetMoveClause(archive, handle);
ClauseSetInsert(tmp, repl);
count++;
}
else
{
ClauseFree(repl);
}
}
handle = next;
}
ClauseSetInsertSet(set, tmp);
assert(ClauseSetEmpty(tmp));
ClauseSetFree(tmp);
return count;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|