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
|
/*-----------------------------------------------------------------------
File : ccl_splitting.c
Author: Stephan Schulz
Contents
Implements functions for destructive splitting of clauses with at
least two non-propositional variable disjoint subsets of literals.
Copyright 1998, 1999 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
<1> Wed Apr 18 18:24:18 MET DST 2001
New
-----------------------------------------------------------------------*/
#include "ccl_splitting.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: initialize_lit_table()
//
// Initialize the literal table. For each literal, mark them as
// unassigned to any art and collect the variables that are marked
// by var_filter. If ground literals are not split off individually,
// assign them to partition 1.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static void initialize_lit_table(LitSplitDesc_p lit_table,Clause_p
clause, SplitType how, TermProperties
var_filter)
{
int i, lit_no = ClauseLiteralNumber(clause);
Eqn_p handle = clause->literals;
for(i=0; i<lit_no; i++)
{
assert(handle);
lit_table[i].literal = handle;
lit_table[i].part = 0;
lit_table[i].varset = NULL;
(void)EqnCollectPropVariables(handle, &(lit_table[i].varset),
var_filter);
/* We are only calling EqnCollectVariables for the side
effect here */
if(how == SplitGroundOne || how == SplitGroundNone)
{
if(!lit_table[i].varset)
{
lit_table[i].part = 1;
}
}
handle = handle->next;
}
}
/*-----------------------------------------------------------------------
//
// Function: cond_init_lit_table()
//
// Initialize the literal table. For each literal, mark them as
// unassigned to any art and collect the variables that are marked
// by var_filter. If ground literals are not split off individually,
// assign them to partition 1.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static int cond_init_lit_table(LitSplitDesc_p lit_table,
Clause_p clause, SplitType how,
PStack_p split_vars)
{
int split_var_no;
split_var_no = PStackGetSP(split_vars);
if(!split_var_no)
{
initialize_lit_table(lit_table, clause, how, TPIgnoreProps);
}
else
{
PStackPointer sp;
Term_p var;
ClauseTermSetProp(clause, TPCheckFlag);
for(sp=0; sp<split_var_no; sp++)
{
var = PStackElementP(split_vars,sp);
assert(TermCellQueryProp(var, TPCheckFlag));
TermCellDelProp(var,TPCheckFlag);
}
initialize_lit_table(lit_table, clause, how, TPCheckFlag);
}
return split_var_no;
}
/*-----------------------------------------------------------------------
//
// Function: find_free_literal()
//
// Find the first entry in lit_table that corresponds to a literal
// not yet assigned to any clause part and return its index. If none
// exists, return -1.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static int find_free_literal(LitSplitDesc_p lit_table, int lit_no)
{
int res = -1,i;
for(i=0; i<lit_no; i++)
{
if(!lit_table[i].part)
{
res = i;
break;
}
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: build_part()
//
// Given the index of the first unassigned literal in lit_table and
// a part number, assign this number to all literals that are
// transitively variable-linked to this first literal.
//
// Global Variables: -
//
// Side Effects : Accumulates all part variables in
// lit_table[lit_index].varset
//
/----------------------------------------------------------------------*/
static void build_part(LitSplitDesc_p lit_table, int lit_no, int
lit_index, int part)
{
bool new_vars = true;
int i;
lit_table[lit_index].part = part;
while(new_vars)
{
new_vars = false;
for(i=lit_index+1; i< lit_no; i++)
{
if(!lit_table[i].part)
{
if(PTreeSharedElement(&(lit_table[lit_index].varset),
lit_table[i].varset))
{
lit_table[i].part = part;
new_vars = PTreeMerge(&(lit_table[lit_index].varset),
lit_table[i].varset)||new_vars;
lit_table[i].varset = NULL;
}
}
}
}
}
/*-----------------------------------------------------------------------
//
// Function: assemble_part_literals()
//
// Given a partition number, assemble and return all literals
// belonging to that partition.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Eqn_p assemble_part_literals(LitSplitDesc_p lit_table, int lit_no, int part)
{
Eqn_p handle = NULL, tmp;
int j;
for(j=0; j<lit_no; j++)
{
if(lit_table[j].part == part)
{
tmp = lit_table[j].literal;
tmp->next = handle;
handle = tmp;
}
}
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: clause_split_general()
//
// Try to split clause into different clauses according to the
// inference rule below. If successful,
// deposit split clauses into set and return number of clauses
// created. Otherwise return 0.
//
// L1(X) v L2(X) v L3(X) ...
// ----------------------------------------------------
// T1(X) v T2(X) v T3(X) ....,
// ~T1(X) v L1(X), ~T2(X) v L2(X), ~T3(X) v L3(X), ...
//
// if the Li are subsets of the clause that do not share any
// variables but those in the set X and the Ti
// are _new_ predicate symbols. X is a parameter to the function!
//
// If fresh_defs is false (and split_vars is empty), try if an
// existing definition already covers any of the parts and reuse it,
// otherwise always use fresh definitions.
//
// Global Variables: -
//
// Side Effects : Memory allocation, if successful, destroys
// clause!
//
/----------------------------------------------------------------------*/
int clause_split_general(DefStore_p store, Clause_p clause,
ClauseSet_p set, SplitType how,
bool fresh_defs, PStack_p split_vars)
{
int res = 0, part = 0,i,size, lit_no, split_var_no;
LitSplitDesc_p lit_table;
Eqn_p handle,tmp, join;
FunCode new_pred;
long def_id;
TB_p bank;
Clause_p new_clause;
FormulaProperties props;
assert(clause);
assert(!clause->set);
assert(set);
props = ClauseGiveProps(clause, CPTypeMask|CPIsSOS);
lit_no = ClauseLiteralNumber(clause);
if(lit_no<=1 || ClauseHasSplitLiteral(clause))
{
return 0;
}
bank = clause->literals->bank;
size = lit_no*sizeof(LitSplitDescCell);
lit_table = SizeMalloc(size);
split_var_no = cond_init_lit_table(lit_table, clause, how, split_vars);
if((how == SplitGroundOne) && find_free_literal(lit_table, lit_no))
{
part++;
}
/* Find the variable disjoint parts of the clause */
while((i=find_free_literal(lit_table, lit_no))!=-1)
{
part++;
build_part(lit_table, lit_no, i, part);
}
if(part>1)
{
PStack_p def_stack = PStackAlloc();
/* Build split clauses from original literals */
join = NULL;
clause->literals = NULL; /* Literals are stored in lit_table and
are recycled in new clauses, clause
skeleton is refilled below. */
for(i=1; i<=part; i++)
{
if(split_var_no)
{
/* Get new predicate code */
new_pred = SigGetNewPredicateCode(bank->sig,
split_var_no);
SigSetFuncProp(bank->sig, new_pred, FPClSplitDef);
/* Create definition clause (for maintaining completeness) */
handle = GenDefLit(bank, new_pred, true, split_vars);
assert(!handle->next);
handle->next = assemble_part_literals(lit_table, lit_no, i);
new_clause = ClauseAlloc(handle);
assert(new_clause);
def_id = new_clause->ident;
assert(def_id);
assert(new_pred);
}
else
{
WFormula_p new_form;
/* Create definition clause (for maintaining completeness) */
handle = assemble_part_literals(lit_table, lit_no, i);
new_pred = GetDefinitions(store, handle,
&new_form, &new_clause, fresh_defs);
def_id = 0;
if(new_form)
{
def_id = new_form->ident;
ClausePushDerivation(clause, DCApplyDef, new_form, NULL);
}
}
PStackPushInt(def_stack, def_id);
if(new_clause)
{
/* Fix clause properties */
new_clause->properties = props;
/* Note: Potentially recycled definitions have no real
parents, as they are conceptually introduced ad-hoc and
can be reused for many other clauses. */
/* Insert result clause */
ClauseSetInsert(set, new_clause);
/* Document creation -> Now done in ccl_def_handling.c*/
/* DocClauseCreationDefault(new_clause, inf_split, clause,
NULL); */
}
/* Extend remainder clause (after application of definition) */
tmp = GenDefLit(bank, new_pred, false, split_vars);
tmp->next = join;
join = tmp;
}
clause->literals = join;
ClauseRecomputeLitCounts(clause);
ClauseSetInsert(set, clause);
DocClauseApplyDefsDefault(clause, clause->ident, def_stack);
res = part+1;
PStackFree(def_stack);
}
for(i=0; i<lit_no; i++)
{
PTreeFree(lit_table[i].varset);
}
SizeFree(lit_table, size);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: initialize_permute_stack()
//
// We want to generate unordered n-tuples from k elements. This
// initializes a stack to contain the first valid sample (1, 2,
// ...n) of size n.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static void initialize_permute_stack(PStack_p stack, int size)
{
int i;
PStackReset(stack);
for(i=0; i<size; i++)
{
PStackPushInt(stack, i);
}
}
/*-----------------------------------------------------------------------
//
// Function: permute_stack_next()
//
// Generate the next valid permutation and return true if it exists,
// otherwise return false.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static bool permute_stack_next(PStack_p permute_stack, int var_no)
{
PStackPointer limit = PStackGetSP(permute_stack), i;
long tmp;
i = 0;
while(i < limit)
{
tmp = PStackPopInt(permute_stack);
tmp++;
if((tmp) < (var_no-i))
{
while(i>=0)
{
PStackPushInt(permute_stack, tmp);
tmp++;
i--;
}
return true;
}
i++;
}
return false;
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: ClauseHasSplitLiteral()
//
// Return true if a literal in the clause is a split literal.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
bool ClauseHasSplitLiteral(Clause_p clause)
{
Eqn_p handle;
for(handle = clause->literals; handle; handle = handle->next)
{
if(EqnQueryProp(handle, EPIsSplitLit))
{
return true;
}
}
return false;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSplit()
//
// Try to split clause into different clauses according to the
// inference rule below. If successful,
// deposit split clauses into set and return number of clauses
// created. Otherwise return 0.
//
// L1 v L2 v L3 ...
// ----------------------------------------------------
// T1 v T2 v T3 ...., ~T1 v L1, ~T2 v L2, ~T3 v L3, ...
//
// if the Li are variable-disjoint subsets of the clause and the Ti
// are _new_ propositional variables.
//
// Global Variables: -
//
// Side Effects : Memory allocation, if successful, destroys
// clause!
//
/----------------------------------------------------------------------*/
int ClauseSplit(DefStore_p store, Clause_p clause, ClauseSet_p set,
SplitType how, bool fresh_defs)
{
int res ;
PStack_p dummy = PStackAlloc();
res = clause_split_general(store, clause, set, how, fresh_defs, dummy);
PStackFree(dummy);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSplitGeneral()
//
// Wrapper for clause_split_general(). Tries tries different
// variable subsets (partially ordered by cardinality) to find a
// subset that splits the clause. Only used for eground, so I skimp
// on options.
//
// Global Variables: -
//
// Side Effects : Memory allocation, if successful, destroys
// clause!
//
/----------------------------------------------------------------------*/
int ClauseSplitGeneral(DefStore_p store, bool fresh_defs,
Clause_p clause, ClauseSet_p set, long tries)
{
int res, var_no, set_size;
PStackPointer i;
PStack_p vars, split_vars, permute_stack;
PTree_p vars_tree = NULL;
res = ClauseSplit(store, clause, set, SplitGroundOne, fresh_defs);
if(res)
{
return res;
}
var_no = ClauseCollectVariables(clause, &vars_tree);
if(var_no<=2) /* Non-ground splitting is useless here */
{
PTreeFree(vars_tree);
return 0;
}
vars = PStackAlloc();
split_vars = PStackAlloc();
permute_stack = PStackAlloc();
PTreeToPStack(vars, vars_tree);
PTreeFree(vars_tree);
set_size = 1;
initialize_permute_stack(permute_stack, set_size);
while(tries)
{
PStackReset(split_vars);
for(i=0; i<set_size; i++)
{
PStackPushP(split_vars,
PStackElementP(vars,
PStackElementInt(permute_stack, i)));
}
res = clause_split_general(store, clause, set, SplitGroundNone,
fresh_defs, split_vars);
if(res)
{
break;
}
if(!permute_stack_next(permute_stack, var_no))
{
if(set_size == var_no-2)
{
break;
}
set_size++;
initialize_permute_stack(permute_stack, set_size);
}
tries--;
}
PStackFree(permute_stack);
PStackFree(split_vars);
PStackFree(vars);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetSplitClauses()
//
// Split all clauses in from_set and put the result into to_set.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long ClauseSetSplitClauses(DefStore_p store, ClauseSet_p from_set,
ClauseSet_p to_set, SplitType how, bool fresh_defs)
{
long res=0, tmp;
Clause_p handle;
assert(from_set&&to_set);
while(!ClauseSetEmpty(from_set))
{
handle = ClauseSetExtractFirst(from_set);
tmp = ClauseSplit(store, handle, to_set, how, fresh_defs);
if(!tmp)
{
ClauseSetInsert(to_set, handle);
}
res += tmp;
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: ClauseSetSplitClausesGeneral()
//
// Split all clauses in from_set and put the result into to_set.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long ClauseSetSplitClausesGeneral(DefStore_p store, bool fresh_defs,
ClauseSet_p from_set,
ClauseSet_p to_set, long tries)
{
long res=0, tmp;
Clause_p handle;
assert(from_set&&to_set);
while(!ClauseSetEmpty(from_set))
{
handle = ClauseSetExtractFirst(from_set);
tmp = ClauseSplitGeneral(store, fresh_defs, handle, to_set, tries);
if(!tmp)
{
ClauseSetInsert(to_set, handle);
}
res += tmp;
}
return res;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|