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
|
/*-----------------------------------------------------------------------
File : pcl_proofcheck.c
Author: Stephan Schulz
Contents
Data types and algorithms to realize proof checking for PCL2
protocols.
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> Mon Apr 3 23:02:28 GMT 2000
New
-----------------------------------------------------------------------*/
#include "pcl_proofcheck.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: pcl_run_prover()
//
// Execute command and scan the output for success. If found, return
// true, else return false;
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static bool pcl_run_prover(char* command, char*success)
{
bool res=false;
char line[180],*l;
FILE* ppipe;
if(OutputLevel>1)
{
fprintf(GlobalOut, "# Running %s\n", command);
}
ppipe=popen(command, "r");
if(!ppipe)
{
TmpErrno = errno;
SysError("Cannot open pipe", SYS_ERROR);
}
while((l=fgets(line, 180, ppipe)))
{
if(strstr(line,success))
{
res = true;
}
if(OutputLevel >= 3)
{
fprintf(GlobalOut, "#> %s", line);
}
}
pclose(ppipe);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: pcl_verify_eprover()
//
// Run E on the problem, return true if a proof is found.
//
// Global Variables: -
//
// Side Effects : Memory operations, by subroutines.
//
/----------------------------------------------------------------------*/
static bool pcl_verify_eprover(ClauseSet_p problem,char *executable,
long time_limit)
{
bool res;
DStr_p command = DStrAlloc();
char* name=TempFileName();
FILE* problemfile;
assert(OutputFormat == TPTPFormat);
if(!executable)
{
executable=E_EXEC_DEFAULT;
}
problemfile = OutOpen(name);
ClauseSetPrint(problemfile, problem, true);
OutClose(problemfile);
DStrAppendStr(command, executable);
DStrAppendStr(command, " --tptp-in --prefer-initial-clauses --ac-handling=None"
" --cpu-limit=");
DStrAppendInt(command, time_limit);
DStrAppendChar(command, ' ');
DStrAppendStr(command, name);
res = pcl_run_prover(DStrView(command),"# Proof found!");
if(!res)
{
fprintf(GlobalOut, "# ------------Problem begin--------------\n");
FilePrint(GlobalOut, name);
fprintf(GlobalOut, "# ------------Problem end----------------\n");
}
TempFileRemove(name);
FREE(name);
DStrFree(command);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: eqn_print_otter()
//
// Print a literal in Otter format.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void eqn_print_otter(FILE* out, Eqn_p eqn)
{
assert(OutputFormat == LOPFormat);
if(EqnIsEquLit(eqn))
{
if(EqnIsPositive(eqn))
{
EqnPrint(out, eqn, false, true);
}
else
{
fputc('-', out);
EqnPrint(out, eqn, true, true);
}
}
else
{
if(eqn->lterm==eqn->bank->true_term)
{
assert(eqn->rterm==eqn->bank->true_term);
/* Special case, one more hack */;
if(EqnIsPositive(eqn))
{
fputs("$T", out);
}
else
{
fputs("$F", out);
}
}
else
{
/* Nonequational literal */
if(EqnIsNegative(eqn))
{
fputc('-', out);
}
else
{
fputc(' ', out);
}
TBPrintTerm(out, eqn->bank, eqn->lterm, true);
}
}
}
/*-----------------------------------------------------------------------
//
// Function: clause_print_otter()
//
// Print a clause in Otter format.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void clause_print_otter(FILE* out, Clause_p clause)
{
Eqn_p handle;
if(ClauseIsEmpty(clause))
{
/* For otter, the empty clause is propositional false: */
fputs("$F.", out);
}
else
{
handle=clause->literals;
eqn_print_otter(out, handle);
handle=handle->next;
while(handle)
{
fputs("|\n", out);
eqn_print_otter(out, handle);
handle=handle->next;
}
fputs(".\n", out);
}
}
/*-----------------------------------------------------------------------
//
// Function: clause_set_print_otter()
//
// Print a set of clauses in Otter format (with prolog-variables).
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void clause_set_print_otter(FILE* out, ClauseSet_p set)
{
Clause_p handle;
for(handle = set->anchor->succ; handle!=set->anchor; handle =
handle->succ)
{
clause_print_otter(out, handle);
fputc('\n', out);
}
}
/*-----------------------------------------------------------------------
//
// Function: pcl_verify_otter()
//
// Run Otter on the problem, return true if a proof is found.
//
// Global Variables: -
//
// Side Effects : Memory operations, by subroutines.
//
/----------------------------------------------------------------------*/
static bool pcl_verify_otter(ClauseSet_p problem,char *executable,
long time_limit)
{
bool res;
DStr_p command = DStrAlloc();
char* name=TempFileName();
FILE* problemfile;
if(!executable)
{
executable=OTTER_EXEC_DEFAULT;
}
problemfile = OutOpen(name);
fprintf(problemfile,
"set(prolog_style_variables).\n"
"clear(print_kept).\n"
"clear(print_new_demod).\n"
"clear(print_back_demod).\n"
"clear(print_back_sub).\n"
"set(auto).\n"
"set(input_sos_first).\n"
/* "set(para_from_vars).\n" */
"assign(max_seconds, %ld).\n\n"
"assign(max_mem, 100000).\n\n"
"list(usable).\n\n"
"equal(X,X).\n",time_limit);
clause_set_print_otter(problemfile, problem);
fprintf(problemfile,
"end_of_list.\n");
OutClose(problemfile);
DStrAppendStr(command, executable);
DStrAppendStr(command, " < ");
DStrAppendStr(command, name);
DStrAppendStr(command, " 2> /dev/null");
res = pcl_run_prover(DStrView(command), "-------- PROOF --------");
if(!res)
{
fprintf(GlobalOut, "# ------------Problem begin--------------\n");
FilePrint(GlobalOut, name);
fprintf(GlobalOut, "# ------------Problem end----------------\n");
}
TempFileRemove(name);
FREE(name);
DStrFree(command);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: sig_print_dfg()
//
// Collect function symbols from set and print them in DFG syntax.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void sig_print_dfg(FILE* out, ClauseSet_p set, Sig_p sig)
{
long *symbol_distrib;
FunCode i;
symbol_distrib = SizeMalloc((sig->size)*sizeof(long));
for(i=0; i< sig->size; i++)
{
symbol_distrib[i] = 0;
}
ClauseSetAddSymbolDistribution(set, symbol_distrib);
fprintf(out,"list_of_symbols.\nfunctions[(spass_hack,0)");
for(i=sig->internal_symbols+1; i<sig->size; i++)
{
if(symbol_distrib[i]&&!SigIsPredicate(sig,i))
{
fprintf(out, ",(%s,%d)",
SigFindName(sig, i),
SigFindArity(sig, i));
}
}
fprintf(out,"].\npredicates[(spass_pred_dummy,0)");
for(i=sig->internal_symbols+1; i<sig->size; i++)
{
if(symbol_distrib[i]&&SigIsPredicate(sig,i))
{
fprintf(out, ",(%s,%d)",
SigFindName(sig, i),
SigFindArity(sig, i));
}
}
fprintf(out,"].\nend_of_list.\n");
}
/*-----------------------------------------------------------------------
//
// Function: eqn_print_dfg()
//
// Print an equation in DFG syntax.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void eqn_print_dfg(FILE* out, Eqn_p eqn)
{
assert(OutputFormat == LOPFormat);
assert(!EqnUseInfix);
if(EqnIsNegative(eqn))
{
fputs("not(", out);
}
if(eqn->lterm==eqn->bank->true_term)
{
assert(eqn->rterm==eqn->bank->true_term);
/* Special case, one more hack */;
fputs("equal(spass_hack,spass_hack)", out);
}
else
{
EqnPrint(out,eqn,EqnIsNegative(eqn),true);
}
if(EqnIsNegative(eqn))
{
fputc(')', out);
}
}
/*-----------------------------------------------------------------------
//
// Function: clause_print_dfg()
//
// Print a clause in dfg format.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void clause_print_dfg(FILE* out, Clause_p clause)
{
Eqn_p handle;
PTree_p variables = NULL, cell;
PStack_p stack;
Term_p var;
long var_no;
fprintf(out, "clause(");
var_no = ClauseCollectVariables(clause, &variables);
if(var_no)
{
fprintf(out, "forall([");
stack=PTreeTraverseInit(variables);
cell = PTreeTraverseNext(stack);
if(cell)
{
assert(clause->literals);
var = cell->key;
TBPrintTerm(out, clause->literals->bank, var, true);
while((cell=PTreeTraverseNext(stack)))
{
fputc(',', out);
var = cell->key;
TBPrintTerm(out, clause->literals->bank, var, true);
}
}
PTreeTraverseExit(stack);
PTreeFree(variables);
fprintf(out, "],");
}
fprintf(out, "or(");
handle=clause->literals;
if(handle)
{
eqn_print_dfg(out, handle);
handle=handle->next;
while(handle)
{
fputs(",", out);
eqn_print_dfg(out, handle);
handle=handle->next;
}
}
else
{
fputs("not(equal(spass_hack,spass_hack))",out);
}
fprintf(out, ")%c, c%ld ).", (var_no?')':' '), clause->ident);
}
/*-----------------------------------------------------------------------
//
// Function: clause_set_print_dfg()
//
// Print a set of clauses in DFG format.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static void clause_set_print_dfg(FILE* out, ClauseSet_p set)
{
Clause_p handle;
for(handle = set->anchor->succ; handle!=set->anchor; handle =
handle->succ)
{
clause_print_dfg(out, handle);
fputc('\n', out);
}
}
/*-----------------------------------------------------------------------
//
// Function: pcl_verify_spass()
//
// Run SPASS on the problem, return true if a proof is found.
//
// Global Variables: -
//
// Side Effects : Memory operations, by subroutines.
//
/----------------------------------------------------------------------*/
static bool pcl_verify_spass(ClauseSet_p problem,char *executable,
long time_limit, Sig_p sig)
{
bool res;
DStr_p command = DStrAlloc();
char* name=TempFileName();
FILE* problemfile;
if(!executable)
{
executable=SPASS_EXEC_DEFAULT;
}
problemfile = OutOpen(name);
fprintf(problemfile,
"begin_problem(Unknown).\n");
sig_print_dfg(problemfile, problem, sig);
fprintf(problemfile, "list_of_clauses(axioms,cnf).\n");
clause_set_print_dfg(problemfile, problem);
fprintf(problemfile, "end_of_list.\n"
"list_of_settings(SPASS).\n"
"set_flag(TimeLimit, %ld).\n"
"end_of_list.\n"
"end_problem.\n", time_limit);
OutClose(problemfile);
DStrAppendStr(command, executable);
DStrAppendStr(command, " ");
DStrAppendStr(command, name);
res = pcl_run_prover(DStrView(command), "Proof found.");
if(!res)
{
fprintf(GlobalOut, "# ------------Problem begin--------------\n");
FilePrint(GlobalOut, name);
fprintf(GlobalOut, "# ------------Problem end----------------\n");
}
TempFileRemove(name);
FREE(name);
DStrFree(command);
return res;
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: PCLCollectPreconds()
//
// Collect copies of all clauses quoted in the justification of step
// in set. Return number of clauses.
//
// Global Variables: -
//
// Side Effects : Memory operations, changes set.
//
/----------------------------------------------------------------------*/
long PCLCollectPreconds(PCLProt_p prot, PCLStep_p step, ClauseSet_p
set)
{
PTree_p tree = NULL;
PCLStep_p handle;
Clause_p clause;
long res = 0;
assert(prot && step && set);
PCLExprCollectPreconds(prot, step->just, &tree);
while(tree)
{
handle = PTreeExtractRootKey(&tree);
if(PCLStepIsClausal(handle))
{
clause = ClauseCopy(handle->logic.clause, prot->terms);
ClauseSetInsert(set, clause);
res++;
}
else
{
Warning("Cannot currently handle full first-order format!");
}
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PCLNegSkolemizeClause()
//
// Add the clauses resulting from negating and skolemizing
// step->clause to set. The implementation is not very efficient,
// but that should not matter for our application.
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
long PCLNegSkolemizeClause(PCLProt_p prot, PCLStep_p step, ClauseSet_p
set)
{
long res = 0;
Clause_p clause, new_clause;
Eqn_p handle, copy;
if(PCLStepIsClausal(step))
{
clause = ClauseSkolemize(step->logic.clause, prot->terms);
for(handle=clause->literals; handle; handle=handle->next)
{
copy = EqnCopy(handle,prot->terms);
EqnFlipProp(copy, EPIsPositive);
new_clause=ClauseAlloc(copy);
ClauseSetTPTPType(new_clause, CPTypeHypothesis);
ClauseSetInsert(set, new_clause);
res++;
}
ClauseFree(clause);
}
else
{
Warning("Cannot currently handle full first-order format!");
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PCLGenerateCheck()
//
// Generate a clause set that is unsatisfiable if the clause in step
// is a logical conclusion of the precondition. For initial steps,
// return NULL.
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
ClauseSet_p PCLGenerateCheck(PCLProt_p prot, PCLStep_p step)
{
ClauseSet_p set = ClauseSetAlloc();
if(PCLCollectPreconds(prot, step, set))
{
PCLNegSkolemizeClause(prot, step, set);
}
else
{
ClauseSetFree(set);
set = NULL;
}
return set;
}
/*-----------------------------------------------------------------------
//
// Function: PCLStepCheck()
//
// Check the validity of a single PCL step. Return true if it checks
// ok, false otherwise. At the moment, just print the generated
// problem.
//
// Global Variables: -
//
// Side Effects : By called functions, may print output.
//
/----------------------------------------------------------------------*/
PCLCheckType PCLStepCheck(PCLProt_p prot, PCLStep_p step, ProverType
prover, char* executable, long time_limit)
{
ClauseSet_p problem;
PCLCheckType res=CheckFail;
if(step->just->op==PCLOpSplitClause)
{
return CheckNotImplemented;
}
problem = PCLGenerateCheck(prot, step);
if(!problem)
{
return CheckByAssumption;
}
else
{
switch(prover)
{
case EProver:
if(pcl_verify_eprover(problem,executable,time_limit))
{
res = CheckOk;
}
break;
case Otter:
if(pcl_verify_otter(problem,executable,time_limit))
{
res = CheckOk;
}
break;
case Spass:
if(pcl_verify_spass(problem,executable,time_limit,prot->terms->sig))
{
res = CheckOk;
}
break;
default:
assert(false && "Not yet implemented");
break;
}
ClauseSetFree(problem);
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PCLProtCheck()
//
// Check all steps in a PCL listing. Return number of successful
// steps.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
long PCLProtCheck(PCLProt_p prot, ProverType prover, char* executable,
long time_limit, long *unchecked)
{
PStack_p trav_stack,stack = PStackAlloc();
PStackPointer i;
PTree_p cell;
PCLStep_p step;
long res=0;
PCLCheckType check;
*unchecked = 0;
trav_stack = PTreeTraverseInit(prot->steps);
while((cell=PTreeTraverseNext(trav_stack)))
{
step=cell->key;
PStackPushP(stack, step);
}
PTreeTraverseExit(trav_stack);
for(i=0; i<PStackGetSP(stack); i++)
{
step = PStackElementP(stack,i);
if(OutputLevel)
{
fprintf(GlobalOut, "# Checking ");
PCLStepPrint(GlobalOut, step);
fputc('\n', GlobalOut);
}
check = PCLStepCheck(prot, step, prover, executable,
time_limit);
switch(check)
{
case CheckByAssumption:
OUTPRINT(1,"# Checked (by assumption)\n\n");
res++;
break;
case CheckOk:
OUTPRINT(1,"# Checked (by prover)\n\n");
res++;
break;
case CheckFail:
OUTPRINT(1,"# FAILED\n\n");
break;
case CheckNotImplemented:
OUTPRINT(1,"# Check not implemented, assuming true!\n\n");
(*unchecked)++;
break;
default:
assert(false);
break;
}
}
PStackFree(stack);
return res;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|