1 /*-----------------------------------------------------------------------
2 
3 File  : ccl_clausesets.c
4 
5 Author: Stephan Schulz
6 
7 Contents
8 
9   Implementation of clause sets based on AVL trees.
10 
11   Copyright 1998, 1999 by the author.
12   This code is released under the GNU General Public Licence and
13   the GNU Lesser General Public License.
14   See the file COPYING in the main E directory for details..
15   Run "eprover -h" for contact information.
16 
17 Changes
18 
19 <1> Sun May 10 03:03:20 MET DST 1998
20     New
21 
22 -----------------------------------------------------------------------*/
23 
24 #include "ccl_clausesets.h"
25 
26 
27 
28 /*---------------------------------------------------------------------*/
29 /*                        Global Variables                             */
30 /*---------------------------------------------------------------------*/
31 
32 
33 /*---------------------------------------------------------------------*/
34 /*                      Forward Declarations                           */
35 /*---------------------------------------------------------------------*/
36 
37 
38 /*---------------------------------------------------------------------*/
39 /*                         Internal Functions                          */
40 /*---------------------------------------------------------------------*/
41 
42 /*-----------------------------------------------------------------------
43 //
44 // Function: print_var_pattern()
45 //
46 //   Print a template for a function/predicate symbol.
47 //
48 // Global Variables: -
49 //
50 // Side Effects    : Output
51 //
52 /----------------------------------------------------------------------*/
53 
print_var_pattern(FILE * out,char * symbol,int arity,char * var,char * alt_var,int exception)54 static void print_var_pattern(FILE* out, char* symbol, int arity, char*
55                var, char* alt_var, int exception)
56 {
57    int i;
58    char* prefix = "";
59 
60    fprintf(out, "%s(", symbol);
61 
62    for(i=1; i<= arity; i++)
63    {
64       fputs(prefix, out);
65       if(i==exception)
66       {
67     fputs(alt_var, out);
68       }
69       else
70       {
71     fprintf(out, "%s%d", var, i);
72       }
73       prefix = ",";
74    }
75    fprintf(out, ")");
76 }
77 
78 
79 /*-----------------------------------------------------------------------
80 //
81 // Function:  eq_func_axiom_print()
82 //
83 //   Print the LOP substitutivity axiom(s) for a function symbol.
84 //
85 // Global Variables: -
86 //
87 // Side Effects    : Output
88 //
89 /----------------------------------------------------------------------*/
90 
eq_func_axiom_print(FILE * out,char * symbol,int arity,bool single_subst)91 static void eq_func_axiom_print(FILE* out, char* symbol, int arity,
92             bool single_subst)
93 {
94    int i;
95    char *prefix = "";
96 
97    if(single_subst)
98    {
99       for(i=1; i<=arity; i++)
100       {
101     fprintf(out, "equal(");
102     print_var_pattern(out, symbol, arity, "X", "Y", i);
103     fprintf(out, ",");
104     print_var_pattern(out, symbol, arity, "X", "Z", i);
105     fprintf(out, ") <- ");
106     fprintf(out, "equal(Y,Z).\n");
107       }
108    }
109    else
110    {
111       fprintf(out, "equal(");
112       print_var_pattern(out, symbol, arity, "X", NULL,0);
113       fprintf(out, ",");
114       print_var_pattern(out, symbol, arity, "Y", NULL,0);
115       fprintf(out, ") <- ");
116       for(i=1; i<=arity; i++)
117       {
118     fprintf(out, "%sequal(X%d,Y%d)", prefix, i, i);
119     prefix = ",";
120       }
121       fprintf(out, ".\n");
122    }
123 }
124 
125 /*-----------------------------------------------------------------------
126 //
127 // Function:  eq_pred_axiom_print()
128 //
129 //   Print the LOP substitutivity axiom(s) for a predicate symbol.
130 //
131 // Global Variables: -
132 //
133 // Side Effects    : Output
134 //
135 /----------------------------------------------------------------------*/
136 
eq_pred_axiom_print(FILE * out,char * symbol,int arity,bool single_subst)137 static void eq_pred_axiom_print(FILE* out, char* symbol, int arity,
138             bool single_subst)
139 {
140    int i;
141 
142    if(single_subst)
143    {
144       for(i=1; i<=arity; i++)
145       {
146     print_var_pattern(out, symbol, arity, "X", "Y", i);
147     fprintf(out, " <- ");
148     print_var_pattern(out, symbol, arity, "X", "Z", i);
149     fprintf(out, ", equal(Y,Z).\n");
150       }
151    }
152    else
153    {
154       print_var_pattern(out, symbol, arity, "X", NULL,0);
155       fprintf(out, " <- ");
156       print_var_pattern(out, symbol, arity, "Y", NULL,0);
157       for(i=1; i<=arity; i++)
158       {
159     fprintf(out, ",equal(X%d,Y%d)", i, i);
160       }
161       fprintf(out, ".\n");
162    }
163 }
164 
165 
166 /*-----------------------------------------------------------------------
167 //
168 // Function:  tptp_eq_func_axiom_print()
169 //
170 //   Print the TPTP substitutivity axiom(s) for a function symbol.
171 //
172 // Global Variables: -
173 //
174 // Side Effects    : Output
175 //
176 /----------------------------------------------------------------------*/
177 
tptp_eq_func_axiom_print(FILE * out,char * symbol,int arity,bool single_subst)178 static void tptp_eq_func_axiom_print(FILE* out, char* symbol, int arity,
179                  bool single_subst)
180 {
181    int i;
182 
183    if(single_subst)
184    {
185       for(i=1; i<=arity; i++)
186       {
187     fprintf(out, "input_clause(eq_subst_%s%d, axiom, [++equal(",
188        symbol, i);
189     print_var_pattern(out, symbol, arity, "X", "Y", i);
190     fprintf(out, ",");
191     print_var_pattern(out, symbol, arity, "X", "Z", i);
192     fprintf(out, "),");
193     fprintf(out, "--equal(Y,Z)]).\n");
194       }
195    }
196    else
197    {
198       fprintf(out, "input_clause(eq_subst_%s, axiom, [++equal(",
199          symbol);
200       print_var_pattern(out, symbol, arity, "X", NULL,0);
201       fprintf(out, ",");
202       print_var_pattern(out, symbol, arity, "Y", NULL,0);
203       fprintf(out, ")");
204       for(i=1; i<=arity; i++)
205       {
206     fprintf(out, ",--equal(X%d,Y%d)", i, i);
207       }
208       fprintf(out, "]).\n");
209    }
210 }
211 
212 /*-----------------------------------------------------------------------
213 //
214 // Function:  tptp_eq_pred_axiom_print()
215 //
216 //   Print the TPTP substitutivity axiom(s) for a predicate symbol.
217 //
218 // Global Variables: -
219 //
220 // Side Effects    : Output
221 //
222 /----------------------------------------------------------------------*/
223 
tptp_eq_pred_axiom_print(FILE * out,char * symbol,int arity,bool single_subst)224 static void tptp_eq_pred_axiom_print(FILE* out, char* symbol, int arity,
225                  bool single_subst)
226 {
227    int i;
228 
229    if(single_subst)
230    {
231       for(i=1; i<=arity; i++)
232       {
233     fprintf(out, "input_clause(eq_subst_%s%d, axiom, [++",
234        symbol, i);
235     print_var_pattern(out, symbol, arity, "X", "Y", i);
236     fprintf(out, ",--");
237     print_var_pattern(out, symbol, arity, "X", "Z", i);
238     fprintf(out, ",--equal(Y,Z)]).\n");
239       }
240    }
241    else
242    {
243       fprintf(out, "input_clause(eq_subst_%s, axiom, [++",
244          symbol);
245       print_var_pattern(out, symbol, arity, "X", NULL,0);
246       fprintf(out, ",--");
247       print_var_pattern(out, symbol, arity, "Y", NULL,0);
248       for(i=1; i<=arity; i++)
249       {
250     fprintf(out, ",--equal(X%d,Y%d)", i, i);
251       }
252       fprintf(out, "]).\n");
253    }
254 }
255 
256 
257 /*-----------------------------------------------------------------------
258 //
259 // Function: clause_set_extract_entry()
260 //
261 //   Remove a plain clause from a plain clause set.
262 //
263 // Global Variables: -
264 //
265 // Side Effects    : Changes set
266 //
267 /----------------------------------------------------------------------*/
268 
clause_set_extract_entry(Clause_p clause)269 static void clause_set_extract_entry(Clause_p clause)
270 {
271    int     i;
272 #ifndef NDEBUG
273    Eval_p test;
274 #endif
275    Eval_p *root;
276 
277    assert(clause);
278    assert(clause->set);
279    assert(clause->set->members);
280 
281    if(clause->evaluations)
282    {
283       for(i=0; i<clause->evaluations->eval_no; i++)
284       {
285     root = (void*)&PDArrayElementP(clause->set->eval_indices, i);
286 #ifndef NDEBUG
287          test =
288 #endif
289             EvalTreeExtractEntry(root,
290                                  clause->evaluations,
291                                  i);
292          assert(test);
293          assert(test->object == clause);
294       }
295    }
296    clause->pred->succ = clause->succ;
297    clause->succ->pred = clause->pred;
298    clause->set->literals-=ClauseLiteralNumber(clause);
299    clause->set->members--;
300    clause->set = NULL;
301    clause->succ = NULL;
302    clause->pred = NULL;
303 }
304 
305 
306 /*---------------------------------------------------------------------*/
307 /*                         Exported Functions                          */
308 /*---------------------------------------------------------------------*/
309 
310 /*-----------------------------------------------------------------------
311 //
312 // Function: ClauseSetAlloc()
313 //
314 //   Allocate an empty clause set that uses SysDate for (logical)
315 //   time-keeping.
316 //
317 // Global Variables: -
318 //
319 // Side Effects    : Memory management
320 //
321 /----------------------------------------------------------------------*/
322 
ClauseSetAlloc(void)323 ClauseSet_p ClauseSetAlloc(void)
324 {
325    ClauseSet_p handle;
326 
327    handle = ClauseSetCellAlloc();
328 
329    handle->members = 0;
330    handle->literals = 0;
331    handle->anchor = ClauseCellAlloc();
332    handle->anchor->literals = NULL;
333    handle->anchor->pred = handle->anchor->succ = handle->anchor;
334    handle->date = SysDateCreationTime();
335    SysDateInc(&handle->date);
336    handle->demod_index = NULL;
337    handle->fvindex = NULL;
338 
339    handle->eval_indices = PDArrayAlloc(4,4);
340    handle->eval_no = 0;
341 
342    handle->identifier = DStrAlloc();
343 
344    return handle;
345 }
346 
347 
348 /*-----------------------------------------------------------------------
349 //
350 // Function: ClauseSetFreeClauses()
351 //
352 //   Delete all clauses in set.
353 //
354 // Global Variables: -
355 //
356 // Side Effects    : Memory operations.
357 //
358 /----------------------------------------------------------------------*/
359 
ClauseSetFreeClauses(ClauseSet_p set)360 void ClauseSetFreeClauses(ClauseSet_p set)
361 {
362    Clause_p handle;
363 
364    assert(set);
365 
366    while(!ClauseSetEmpty(set))
367    {
368       handle = ClauseSetExtractFirst(set);
369       ClauseFree(handle);
370    }
371 }
372 
373 
374 /*-----------------------------------------------------------------------
375 //
376 // Function: ClauseSetFree()
377 //
378 //   Delete a clauseset.
379 //
380 // Global Variables: -
381 //
382 // Side Effects    : Memory operations
383 //
384 /----------------------------------------------------------------------*/
385 
ClauseSetFree(ClauseSet_p junk)386 void ClauseSetFree(ClauseSet_p junk)
387 {
388    assert(junk);
389 
390    ClauseSetFreeClauses(junk);
391 
392    if(junk->demod_index)
393    {
394       PDTreeFree(junk->demod_index);
395    }
396    if(junk->fvindex)
397    {
398       FVIAnchorFree(junk->fvindex);
399    }
400    PDArrayFree(junk->eval_indices);
401    ClauseCellFree(junk->anchor);
402    DStrFree(junk->identifier);
403    ClauseSetCellFree(junk);
404 }
405 
406 
407 
408 /*-----------------------------------------------------------------------
409 //
410 // Function: ClauseSetStackCardinality()
411 //
412 //   Assume stack is a stack of clause sets. Return the number of
413 //   clauses in all the sets.
414 //
415 // Global Variables: -
416 //
417 // Side Effects    : -
418 //
419 /----------------------------------------------------------------------*/
420 
ClauseSetStackCardinality(PStack_p stack)421 long ClauseSetStackCardinality(PStack_p stack)
422 {
423    ClauseSet_p handle;
424    PStackPointer i;
425    long res = 0;
426 
427    for(i=0; i<PStackGetSP(stack); i++)
428    {
429       handle = PStackElementP(stack, i);
430       res += ClauseSetCardinality(handle);
431    }
432    return res;
433 }
434 
435 
436 
437 /*-----------------------------------------------------------------------
438 //
439 // Function: ClauseSetGCMarkTerms()
440 //
441 //   Mark all terms in the clause set for the garbage collection.
442 //
443 // Global Variables:
444 //
445 // Side Effects    :
446 //
447 /----------------------------------------------------------------------*/
448 
ClauseSetGCMarkTerms(ClauseSet_p set)449 void ClauseSetGCMarkTerms(ClauseSet_p set)
450 {
451    Clause_p handle;
452 
453    for(handle = set->anchor->succ; handle!=set->anchor; handle =
454      handle->succ)
455    {
456       ClauseGCMarkTerms(handle);
457    }
458 }
459 
460 /*-----------------------------------------------------------------------
461 //
462 // Function: ClauseSetInsert()
463 //
464 //   Insert a clause as the last clause into the clauseset.
465 //
466 // Global Variables: -
467 //
468 // Side Effects    : Changes set
469 //
470 /----------------------------------------------------------------------*/
471 
ClauseSetInsert(ClauseSet_p set,Clause_p newclause)472 void ClauseSetInsert(ClauseSet_p set, Clause_p newclause)
473 {
474    int    i;
475 #ifndef NDEBUG
476    Eval_p test;
477 #endif
478    Eval_p *root;
479 
480    assert(!newclause->set);
481 
482    newclause->succ = set->anchor;
483    newclause->pred = set->anchor->pred;
484    set->anchor->pred->succ = newclause;
485    set->anchor->pred = newclause;
486    newclause->set = set;
487    set->members++;
488    set->literals+=ClauseLiteralNumber(newclause);
489    if(newclause->evaluations)
490    {
491       for(i=0; i<newclause->evaluations->eval_no; i++)
492       {
493          root = (void*)&(PDArrayElementP(newclause->set->eval_indices,i));
494 #ifndef NDEBUG
495          test =
496 #endif
497             EvalTreeInsert(root, newclause->evaluations, i);
498          assert(!test);
499       }
500       set->eval_no = MAX(newclause->evaluations->eval_no, set->eval_no);
501    }
502 }
503 
504 
505 /*-----------------------------------------------------------------------
506 //
507 // Function: ClauseSetInsertSet()
508 //
509 //   Move all clauses from from into set (leaving from empty, but not
510 //   deleted).
511 //
512 // Global Variables: -
513 //
514 // Side Effects    : -
515 //
516 /----------------------------------------------------------------------*/
517 
ClauseSetInsertSet(ClauseSet_p set,ClauseSet_p from)518 long ClauseSetInsertSet(ClauseSet_p set, ClauseSet_p from)
519 {
520    Clause_p handle;
521    long res = 0;
522 
523    while(!ClauseSetEmpty(from))
524    {
525       handle = ClauseSetExtractFirst(from);
526       ClauseSetInsert(set, handle);
527       res++;
528    }
529    return res;
530 }
531 
532 
533 /*-----------------------------------------------------------------------
534 //
535 // Function: ClauseSetPDTIndexedInsert()
536 //
537 //   Insert a demodulator into the set and the sets index.
538 //
539 // Global Variables: -
540 //
541 // Side Effects    : -
542 //
543 /----------------------------------------------------------------------*/
544 
ClauseSetPDTIndexedInsert(ClauseSet_p set,Clause_p newclause)545 void ClauseSetPDTIndexedInsert(ClauseSet_p set, Clause_p newclause)
546 {
547    ClausePos_p pos;
548 
549    assert(set->demod_index);
550    assert(ClauseIsUnit(newclause));
551 
552    ClauseSetInsert(set, newclause);
553    pos          = ClausePosCellAlloc();
554    pos->clause  = newclause;
555    pos->literal = newclause->literals;
556    pos->side    = LeftSide;
557    pos->pos     = NULL;
558    PDTreeInsert(set->demod_index, pos);
559    if(!EqnIsOriented(newclause->literals))
560    {
561       pos          = ClausePosCellAlloc();
562       pos->clause  = newclause;
563       pos->literal = newclause->literals;
564       pos->side    = RightSide;
565       pos->pos     = NULL;
566       PDTreeInsert(set->demod_index, pos);
567    }
568    ClauseSetProp(newclause, CPIsDIndexed);
569 }
570 
571 
572 
573 /*-----------------------------------------------------------------------
574 //
575 // Function: ClauseSetIndexedInsert()
576 //
577 //   Insert an FVPackedClause clause into the set, taking care od of
578 //   all existing indexes.
579 //
580 // Global Variables: -
581 //
582 // Side Effects    : -
583 //
584 /----------------------------------------------------------------------*/
585 
ClauseSetIndexedInsert(ClauseSet_p set,FVPackedClause_p newclause)586 void ClauseSetIndexedInsert(ClauseSet_p set, FVPackedClause_p newclause)
587 {
588    if(!set->demod_index)
589    {
590       ClauseSetInsert(set, newclause->clause);
591    }
592    else
593    {
594       ClauseSetPDTIndexedInsert(set, newclause->clause);
595    }
596    if(set->fvindex)
597    {
598       FVIndexInsert(set->fvindex, newclause);
599       ClauseSetProp(newclause->clause, CPIsSIndexed);
600    }
601 }
602 
603 
604 /*-----------------------------------------------------------------------
605 //
606 // Function: ClauseSetIndexedInsertClause()
607 //
608 //   Insert a plain clause into the set, taking care od of
609 //   all existing indexes.
610 //
611 // Global Variables: -
612 //
613 // Side Effects    : -
614 //
615 /----------------------------------------------------------------------*/
616 
ClauseSetIndexedInsertClause(ClauseSet_p set,Clause_p newclause)617 void ClauseSetIndexedInsertClause(ClauseSet_p set, Clause_p newclause)
618 {
619    FVPackedClause_p pclause = FVIndexPackClause(newclause, set->fvindex);
620    assert(newclause->weight == ClauseStandardWeight(newclause));
621    ClauseSetIndexedInsert(set, pclause);
622    FVUnpackClause(pclause);
623 }
624 
625 
626 /*-----------------------------------------------------------------------
627 //
628 // Function:
629 //
630 //
631 //
632 // Global Variables:
633 //
634 // Side Effects    :
635 //
636 /----------------------------------------------------------------------*/
637 
ClauseSetIndexedInsertClauseSet(ClauseSet_p set,ClauseSet_p source)638 void ClauseSetIndexedInsertClauseSet(ClauseSet_p set, ClauseSet_p source)
639 {
640    Clause_p handle;
641 
642    while(!ClauseSetEmpty(source))
643    {
644       handle = ClauseSetExtractFirst(source);
645       handle->weight = ClauseStandardWeight(handle);
646       ClauseSetIndexedInsertClause(set, handle);
647    }
648 }
649 
650 
651 /*-----------------------------------------------------------------------
652 //
653 // Function: ClauseSetExtractEntry()
654 //
655 //   Remove a (possibly indexed) clause from a clause set.
656 //
657 // Global Variables: -
658 //
659 // Side Effects    : Changes set
660 //
661 /----------------------------------------------------------------------*/
662 
ClauseSetExtractEntry(Clause_p clause)663 Clause_p ClauseSetExtractEntry(Clause_p clause)
664 {
665    assert(clause);
666    assert(clause->set);
667 
668    /* ClausePCLPrint(stdout, clause, true); */
669 
670    if(ClauseQueryProp(clause, CPIsDIndexed))
671    {
672       assert(clause->set->demod_index);
673       if(clause->set->demod_index)
674       {
675     assert(ClauseIsUnit(clause));
676     PDTreeDelete(clause->set->demod_index, clause->literals->lterm,
677             clause);
678     if(!EqnIsOriented(clause->literals))
679     {
680        PDTreeDelete(clause->set->demod_index,
681           clause->literals->rterm, clause);
682     }
683     ClauseDelProp(clause, CPIsDIndexed);
684       }
685    }
686    if(ClauseQueryProp(clause, CPIsSIndexed))
687    {
688       FVIndexDelete(clause->set->fvindex, clause);
689       ClauseDelProp(clause, CPIsSIndexed);
690    }
691    clause_set_extract_entry(clause);
692    return clause;
693 }
694 
695 
696 /*-----------------------------------------------------------------------
697 //
698 // Function: ClauseSetExtractFirst()
699 //
700 //   Extract the first element of the set and return it. Return NULL
701 //   if set is empty.
702 //
703 // Global Variables: -
704 //
705 // Side Effects    : As above
706 //
707 /----------------------------------------------------------------------*/
708 
ClauseSetExtractFirst(ClauseSet_p set)709 Clause_p ClauseSetExtractFirst(ClauseSet_p set)
710 {
711    Clause_p handle;
712 
713    if(ClauseSetEmpty(set))
714    {
715       return NULL;
716    }
717    handle = set->anchor->succ;
718    assert(handle->set == set);
719    ClauseSetExtractEntry(handle);
720 
721    return handle;
722 }
723 
724 
725 /*-----------------------------------------------------------------------
726 //
727 // Function: ClauseSetDeleteEntry()
728 //
729 //   Delete a clause from the clause set.
730 //
731 // Global Variables: -
732 //
733 // Side Effects    : Changes tree, memory operations
734 //
735 /----------------------------------------------------------------------*/
736 
ClauseSetDeleteEntry(Clause_p clause)737 void ClauseSetDeleteEntry(Clause_p clause)
738 {
739    assert(clause);
740    ClauseSetExtractEntry(clause);
741    ClauseFree(clause);
742 }
743 
744 
745 /*-----------------------------------------------------------------------
746 //
747 // Function: ClauseSetFindBest()
748 //
749 //   Find the best clause (i.e. the clause with the smallest
750 //   evaluation).
751 //
752 // Global Variables: -
753 //
754 // Side Effects    : Changes clause set
755 //
756 /----------------------------------------------------------------------*/
757 
ClauseSetFindBest(ClauseSet_p set,int idx)758 Clause_p ClauseSetFindBest(ClauseSet_p set, int idx)
759 {
760    Clause_p clause;
761    Eval_p   evaluation;
762 
763    /* printf("I: %d", idx); */
764    evaluation =
765       EvalTreeFindSmallest(PDArrayElementP(set->eval_indices, idx), idx);
766 
767    if(!evaluation)
768    {
769       assert(set->anchor->succ == set->anchor);
770       return NULL;
771    }
772    assert(evaluation->object);
773    clause = evaluation->object;
774    assert(clause->set == set);
775    return clause;
776 }
777 
778 
779 
780 
781 /*-----------------------------------------------------------------------
782 //
783 // Function: ClauseSetPrint()
784 //
785 //   Print the clause set to the given stream.
786 //
787 // Global Variables: Only for term output
788 //
789 // Side Effects    : Output
790 //
791 /----------------------------------------------------------------------*/
792 
ClauseSetPrint(FILE * out,ClauseSet_p set,bool fullterms)793 void ClauseSetPrint(FILE* out, ClauseSet_p set, bool fullterms)
794 {
795    Clause_p handle;
796 
797    for(handle = set->anchor->succ; handle!=set->anchor; handle =
798      handle->succ)
799    {
800       ClausePrint(out, handle, fullterms);
801       fputc('\n', out);
802    }
803 }
804 
805 
806 /*-----------------------------------------------------------------------
807 //
808 // Function: ClauseSetTSTPPrint()
809 //
810 //   Print the clause set in TSTP format to the given stream.
811 //
812 // Global Variables: Only for term output
813 //
814 // Side Effects    : Output
815 //
816 /----------------------------------------------------------------------*/
817 
ClauseSetTSTPPrint(FILE * out,ClauseSet_p set,bool fullterms)818 void ClauseSetTSTPPrint(FILE* out, ClauseSet_p set, bool fullterms)
819 {
820    Clause_p handle;
821 
822    for(handle = set->anchor->succ; handle!=set->anchor; handle =
823      handle->succ)
824    {
825       ClauseTSTPPrint(out, handle, fullterms, true);
826       fputc('\n', out);
827    }
828 }
829 
830 /*-----------------------------------------------------------------------
831 //
832 // Function: ClauseSetPrintPrefix()
833 //
834 //   Print the clause set, one clause per line, with prefix prefix on
835 //   each line.
836 //
837 // Global Variables: For format (read only)
838 //
839 // Side Effects    : Output
840 //
841 /----------------------------------------------------------------------*/
842 
ClauseSetPrintPrefix(FILE * out,char * prefix,ClauseSet_p set)843 void ClauseSetPrintPrefix(FILE* out, char* prefix, ClauseSet_p set)
844 {
845    Clause_p handle;
846 
847    for(handle = set->anchor->succ; handle!=set->anchor; handle =
848      handle->succ)
849    {
850       fputs(prefix, out);
851       ClausePrint(out, handle, true);
852       fputc('\n', out);
853    }
854 }
855 
856 
857 
858 
859 /*-----------------------------------------------------------------------
860 //
861 // Function: ClauseSetSort()
862 //
863 //   Sort a clause set according to the comparison function
864 //   given. Note: This is unnecssarily inefficient for evaluated
865 //   clauses! Reimplement if you need to use it for large evaluatied
866 //   sets!
867 //
868 // Global Variables: -
869 //
870 // Side Effects    : Memory operations, will reorganize evaluation
871 //                   tree
872 //
873 /----------------------------------------------------------------------*/
874 
ClauseSetSort(ClauseSet_p set,ComparisonFunctionType cmp_fun)875 void ClauseSetSort(ClauseSet_p set, ComparisonFunctionType cmp_fun)
876 {
877    PStack_p stack = PStackAlloc();
878    Clause_p clause;
879    PStackPointer i;
880 
881    while((clause = ClauseSetExtractFirst(set)))
882    {
883       clause->weight = ClauseStandardWeight(clause);
884       PStackPushP(stack, clause);
885    }
886    assert(ClauseSetEmpty(set));
887 
888    PStackSort(stack, cmp_fun);
889 
890    for(i=0; i<PStackGetSP(stack); i++)
891    {
892       clause = PStackElementP(stack,i);
893       ClauseSetInsert(set, clause);
894    }
895    /* ClauseSetPrint(GlobalOut, set, true); */
896    PStackFree(stack);
897 }
898 
899 
900 /*-----------------------------------------------------------------------
901 //
902 // Function: ClauseSetSetProp()
903 //
904 //   Set prop in all clauses in set.
905 //
906 // Global Variables: -
907 //
908 // Side Effects    : -
909 //
910 /----------------------------------------------------------------------*/
911 
ClauseSetSetProp(ClauseSet_p set,FormulaProperties prop)912 void ClauseSetSetProp(ClauseSet_p set, FormulaProperties prop)
913 {
914    Clause_p handle;
915 
916    for(handle = set->anchor->succ; handle!=set->anchor; handle =
917      handle->succ)
918    {
919       ClauseSetProp(handle, prop);
920    }
921 }
922 
923 
924 /*-----------------------------------------------------------------------
925 //
926 // Function: ClauseSetDelProp()
927 //
928 //   Delete prop in all clauses in set.
929 //
930 // Global Variables: -
931 //
932 // Side Effects    : -
933 //
934 /----------------------------------------------------------------------*/
935 
ClauseSetDelProp(ClauseSet_p set,FormulaProperties prop)936 void ClauseSetDelProp(ClauseSet_p set, FormulaProperties prop)
937 {
938    Clause_p handle;
939 
940    for(handle = set->anchor->succ; handle!=set->anchor; handle =
941      handle->succ)
942    {
943       ClauseDelProp(handle, prop);
944    }
945 }
946 
947 
948 /*-----------------------------------------------------------------------
949 //
950 // Function: ClauseSetSetTPTPType()
951 //
952 //   Set TPTP type in all clauses in set.
953 //
954 // Global Variables: -
955 //
956 // Side Effects    : -
957 //
958 /----------------------------------------------------------------------*/
959 
ClauseSetSetTPTPType(ClauseSet_p set,FormulaProperties type)960 void ClauseSetSetTPTPType(ClauseSet_p set, FormulaProperties type)
961 {
962    Clause_p handle;
963 
964    for(handle = set->anchor->succ; handle!=set->anchor; handle =
965      handle->succ)
966    {
967       ClauseSetTPTPType(handle, type);
968    }
969 }
970 
971 
972 
973 
974 /*-----------------------------------------------------------------------
975 //
976 // Function: ClauseSetMarkCopies()
977 //
978 //   Mark clauses that are equivalent (modulo ClauseCompareFun) to
979 //   clauses that occur earlier in set. Returns number of marked
980 //   clauses.
981 //
982 // Global Variables: -
983 //
984 // Side Effects    : Sets property, memory operations, deletes parents
985 //                   of unmarked copies.
986 //
987 /----------------------------------------------------------------------*/
988 
ClauseSetMarkCopies(ClauseSet_p set)989 long ClauseSetMarkCopies(ClauseSet_p set)
990 {
991    long res = 0;
992    Clause_p handle, exists;
993    PTree_p  store = NULL;
994 
995    assert(set);
996 
997    for(handle = set->anchor->succ; handle!=set->anchor; handle =
998      handle->succ)
999    {
1000       if((exists = PTreeObjStore(&store, handle,
1001                                  ClauseCompareFun)))
1002       {
1003     if(!ClauseParentsAreSubset(exists, handle))
1004     {
1005        ClauseDetachParents(exists);
1006     }
1007     ClauseSetProp(handle, CPDeleteClause);
1008     res++;
1009       }
1010    }
1011    PTreeFree(store);
1012 
1013    return res;
1014 }
1015 
1016 
1017 
1018 /*-----------------------------------------------------------------------
1019 //
1020 // Function: ClauseSetDeleteMarkedEntries()
1021 //
1022 //   Remove all clauses with property CPDeleteClause set. Returns
1023 //   number of deleted clauses.
1024 //
1025 // Global Variables: -
1026 //
1027 // Side Effects    : Changes set.
1028 //
1029 /----------------------------------------------------------------------*/
1030 
ClauseSetDeleteMarkedEntries(ClauseSet_p set)1031 long ClauseSetDeleteMarkedEntries(ClauseSet_p set)
1032 {
1033    long deleted = 0;
1034    Clause_p clause, handle;
1035 
1036    assert(set);
1037 
1038    handle = set->anchor->succ;
1039 
1040    while(handle != set->anchor)
1041    {
1042       clause = handle;
1043       handle = handle->succ;
1044 
1045       if(ClauseQueryProp(clause, CPDeleteClause))
1046       {
1047     deleted++;
1048     ClauseDetachParents(clause); /* If no parents, nothing should
1049                 happen! */
1050     ClauseSetDeleteEntry(clause);
1051       }
1052    }
1053    return deleted;
1054 }
1055 
1056 
1057 /*-----------------------------------------------------------------------
1058 //
1059 // Function: ClauseSetDeleteCopies()
1060 //
1061 //   Delete all but one occurence of a clause in set.
1062 //
1063 // Global Variables: -
1064 //
1065 // Side Effects    : Changes clause set, changes parent-rlation of
1066 //                   kept copies.
1067 //
1068 /----------------------------------------------------------------------*/
1069 
ClauseSetDeleteCopies(ClauseSet_p set)1070 long ClauseSetDeleteCopies(ClauseSet_p set)
1071 {
1072    long res1, res2;
1073 
1074    assert(set);
1075 
1076    res1 = ClauseSetMarkCopies(set);
1077    res2 = ClauseSetDeleteMarkedEntries(set);
1078    UNUSED(res1); UNUSED(res2); assert(res1==res2);
1079 
1080    return res1;
1081 }
1082 
1083 
1084 /*-----------------------------------------------------------------------
1085 //
1086 // Function: ClauseSetDeleteNonUnits()
1087 //
1088 //   Remove all non-empty-non-unit-clauses from set, return number of
1089 //   clauses eliminated.
1090 //
1091 // Global Variables: -
1092 //
1093 // Side Effects    : -
1094 //
1095 /----------------------------------------------------------------------*/
1096 
ClauseSetDeleteNonUnits(ClauseSet_p set)1097 long ClauseSetDeleteNonUnits(ClauseSet_p set)
1098 {
1099    Clause_p handle;
1100 
1101    assert(set);
1102    assert(!set->demod_index);
1103 
1104    handle = set->anchor->succ;
1105    while(handle != set->anchor)
1106    {
1107       if(ClauseLiteralNumber(handle)>1)
1108       {
1109     ClauseSetProp(handle,CPDeleteClause);
1110       }
1111       else
1112       {
1113     ClauseDelProp(handle,CPDeleteClause);
1114       }
1115       handle = handle->succ;
1116    }
1117    return ClauseSetDeleteMarkedEntries(set);
1118 }
1119 
1120 
1121 /*-----------------------------------------------------------------------
1122 //
1123 // Function: ClauseSetGetTermNodes()
1124 //
1125 //   Count the nodes of terms in the clauses of set as though they
1126 //   were unshared.
1127 //
1128 // Global Variables: -
1129 //
1130 // Side Effects    : Potentially via ClauseWeight()
1131 //
1132 /----------------------------------------------------------------------*/
1133 
ClauseSetGetTermNodes(ClauseSet_p set)1134 long ClauseSetGetTermNodes(ClauseSet_p set)
1135 {
1136    long     res = 0;
1137    Clause_p handle;
1138 
1139    for(handle = set->anchor->succ; handle != set->anchor; handle =
1140      handle->succ)
1141    {
1142       res += ClauseWeight(handle, 1, 1, 1, 1, 1, true);
1143    }
1144    return res;
1145 }
1146 
1147 /*-----------------------------------------------------------------------
1148 //
1149 // Function: ClauseSetMarkSOS()
1150 //
1151 //   Mark Set-of-Support clauses in set with CPIsSOS. Return size of
1152 //   SOS.
1153 //
1154 // Global Variables: -
1155 //
1156 // Side Effects    : -
1157 //
1158 /----------------------------------------------------------------------*/
1159 
ClauseSetMarkSOS(ClauseSet_p set,bool tptp_types)1160 long ClauseSetMarkSOS(ClauseSet_p set, bool tptp_types)
1161 {
1162    long     res = 0;
1163    Clause_p handle;
1164 
1165    for(handle = set->anchor->succ; handle != set->anchor; handle =
1166      handle->succ)
1167    {
1168       if((tptp_types && (ClauseQueryTPTPType(handle) == CPTypeConjecture))||
1169     (!tptp_types && (ClauseIsGoal(handle))))
1170       {
1171     ClauseSetProp(handle, CPIsSOS);
1172     res++;
1173       }
1174       else
1175       {
1176     ClauseDelProp(handle, CPIsSOS);
1177       }
1178    }
1179    return res;
1180 }
1181 
1182 
1183 /*-----------------------------------------------------------------------
1184 //
1185 // Function: ClauseSetTermSetProp()
1186 //
1187 //   Set prop in all term nodes in clause set.
1188 //
1189 // Global Variables: -
1190 //
1191 // Side Effects    : No serious ones
1192 //
1193 /----------------------------------------------------------------------*/
1194 
ClauseSetTermSetProp(ClauseSet_p set,TermProperties prop)1195 void ClauseSetTermSetProp(ClauseSet_p set, TermProperties prop)
1196 {
1197    Clause_p handle;
1198 
1199    for(handle = set->anchor->succ; handle != set->anchor; handle =
1200      handle->succ)
1201    {
1202       ClauseTermSetProp(handle, prop);
1203    }
1204 }
1205 
1206 /*-----------------------------------------------------------------------
1207 //
1208 // Function: ClauseSetTBTermPropDelCount()
1209 //
1210 //   Delete prop in all term cells, return number of props encountered
1211 //
1212 // Global Variables: -
1213 //
1214 // Side Effects    : No serious ones
1215 //
1216 /----------------------------------------------------------------------*/
1217 
ClauseSetTBTermPropDelCount(ClauseSet_p set,TermProperties prop)1218 long ClauseSetTBTermPropDelCount(ClauseSet_p set, TermProperties prop)
1219 {
1220    Clause_p handle;
1221    long res = 0;
1222 
1223    for(handle = set->anchor->succ; handle != set->anchor; handle =
1224      handle->succ)
1225    {
1226       res += ClauseTBTermDelPropCount(handle, prop);
1227    }
1228    return res;
1229 }
1230 
1231 /*-----------------------------------------------------------------------
1232 //
1233 // Function: ClauseSetGetSharedTermNodes()
1234 //
1235 //   Return the number of shared term nodes used by set.
1236 //
1237 // Global Variables: -
1238 //
1239 // Side Effects    : Changes TPOpFlag
1240 //
1241 /----------------------------------------------------------------------*/
1242 
ClauseSetGetSharedTermNodes(ClauseSet_p set)1243 long ClauseSetGetSharedTermNodes(ClauseSet_p set)
1244 {
1245    long res;
1246 
1247    ClauseSetTermSetProp(set, TPOpFlag);
1248    res = ClauseSetTBTermPropDelCount(set, TPOpFlag);
1249 
1250    return res;
1251 }
1252 
1253 
1254 
1255 /*-----------------------------------------------------------------------
1256 //
1257 // Function: ClauseSetParseList()
1258 //
1259 //   Parse a list of clauses into the set. Clauses are not
1260 //   evaluated. Returns number of clauses parsed.
1261 //
1262 // Global Variables: Only for parsing of terms.
1263 //
1264 // Side Effects    : Input, memory operations, changes set.
1265 //
1266 /----------------------------------------------------------------------*/
1267 
ClauseSetParseList(Scanner_p in,ClauseSet_p set,TB_p bank)1268 long ClauseSetParseList(Scanner_p in, ClauseSet_p set, TB_p bank)
1269 {
1270    long     count = 0;
1271    Clause_p clause;
1272 
1273    while(ClauseStartsMaybe(in))
1274    {
1275       clause = ClauseParse(in, bank);
1276       count++;
1277       ClauseSetInsert(set, clause);
1278    }
1279    return count;
1280 }
1281 
1282 
1283 
1284 /*-----------------------------------------------------------------------
1285 //
1286 // Function: ClauseSetMarkMaximalTerms()
1287 //
1288 //   Orient all literals and mark all maximal terms and literals in
1289 //   the set.
1290 //
1291 // Global Variables: -
1292 //
1293 // Side Effects    : Orients and marks literals
1294 //
1295 /----------------------------------------------------------------------*/
1296 
ClauseSetMarkMaximalTerms(OCB_p ocb,ClauseSet_p set)1297 void ClauseSetMarkMaximalTerms(OCB_p ocb, ClauseSet_p set)
1298 {
1299    Clause_p handle;
1300 
1301    for(handle = set->anchor->succ; handle!=set->anchor; handle =
1302      handle->succ)
1303    {
1304       ClauseMarkMaximalTerms(ocb, handle);
1305    }
1306 }
1307 
1308 
1309 /*-----------------------------------------------------------------------
1310 //
1311 // Function: ClauseSetSortLiterals()
1312 //
1313 //   Sort literals in all clauses by cmp_fun.
1314 //
1315 // Global Variables:
1316 //
1317 // Side Effects    :
1318 //
1319 /----------------------------------------------------------------------*/
1320 
ClauseSetSortLiterals(ClauseSet_p set,ComparisonFunctionType cmp_fun)1321 void ClauseSetSortLiterals(ClauseSet_p set, ComparisonFunctionType cmp_fun)
1322 {
1323    Clause_p handle;
1324 
1325    for(handle = set->anchor->succ; handle!=set->anchor; handle =
1326      handle->succ)
1327    {
1328       ClauseSortLiterals(handle, cmp_fun);
1329    }
1330 }
1331 
1332 
1333 /*-----------------------------------------------------------------------
1334 //
1335 // Function: ClauseSetListGetMaxDate()
1336 //
1337 //   Return the oldest date of the first limit elements from set of
1338 //   demodulators in the array demodulators.
1339 //
1340 // Global Variables: -
1341 //
1342 // Side Effects    : -
1343 //
1344 /----------------------------------------------------------------------*/
1345 
ClauseSetListGetMaxDate(ClauseSet_p * demodulators,int limit)1346 SysDate ClauseSetListGetMaxDate(ClauseSet_p *demodulators, int limit)
1347 {
1348    int i;
1349    SysDate res = SysDateCreationTime();
1350 
1351    for(i=0; i<limit; i++)
1352    {
1353       assert(demodulators[i]);
1354       res = SysDateMaximum(res, demodulators[i]->date);
1355    }
1356    return res;
1357 }
1358 
1359 
1360 /*-----------------------------------------------------------------------
1361 //
1362 // Function: ClauseSetFind()
1363 //
1364 //   Given a clause and a clause set, try to find the clause in the
1365 //   set. This is only useful for debugging, as usually clause should
1366 //   know about the set it is in!
1367 //
1368 // Global Variables: -
1369 //
1370 // Side Effects    : -
1371 //
1372 /----------------------------------------------------------------------*/
1373 
ClauseSetFind(ClauseSet_p set,Clause_p clause)1374 Clause_p ClauseSetFind(ClauseSet_p set, Clause_p clause)
1375 {
1376    Clause_p handle, res = NULL;
1377 
1378    assert(set);
1379    assert(clause);
1380 
1381    for(handle = set->anchor->succ; handle!=set->anchor;
1382        handle=handle->succ)
1383    {
1384       if(handle == clause)
1385       {
1386     res = handle;
1387     assert(clause->set == set);
1388     break;
1389       }
1390    }
1391    return res;
1392 }
1393 
1394 
1395 /*-----------------------------------------------------------------------
1396 //
1397 // Function: ClauseSetFindById()
1398 //
1399 //   Given a clause ident and a clause set, try to find the clause in
1400 //   the set.
1401 //
1402 // Global Variables: -
1403 //
1404 // Side Effects    : -
1405 //
1406 /----------------------------------------------------------------------*/
1407 
ClauseSetFindById(ClauseSet_p set,long ident)1408 Clause_p ClauseSetFindById(ClauseSet_p set, long ident)
1409 {
1410    Clause_p handle, res = NULL;
1411 
1412    assert(set);
1413 
1414    for(handle = set->anchor->succ; handle!=set->anchor;
1415        handle=handle->succ)
1416    {
1417       if(handle->ident == ident)
1418       {
1419     res = handle;
1420     assert(handle->set == set);
1421     break;
1422       }
1423    }
1424    return res;
1425 }
1426 
1427 
1428 /*-----------------------------------------------------------------------
1429 //
1430 // Function: ClauseSetRemoveEvaluations()
1431 //
1432 //   Remove all evaluations from the clauses in set.
1433 //
1434 // Global Variables: -
1435 //
1436 // Side Effects    : Memory operations...
1437 //
1438 /----------------------------------------------------------------------*/
1439 
ClauseSetRemoveEvaluations(ClauseSet_p set)1440 void ClauseSetRemoveEvaluations(ClauseSet_p set)
1441 {
1442    int i;
1443    Clause_p handle;
1444    for(i=0; i<set->eval_indices->size; i++)
1445    {
1446       PDArrayAssignP(set->eval_indices, i, NULL);
1447    }
1448    for(handle = set->anchor->succ; handle!=set->anchor;
1449        handle=handle->succ)
1450    {
1451       EvalsFree(handle->evaluations);
1452       handle->evaluations = NULL;
1453    }
1454 }
1455 
1456 
1457 /*-----------------------------------------------------------------------
1458 //
1459 // Function: ClauseSetFilterTrivial()
1460 //
1461 //   Given a clause set, remove all trivial tautologies from
1462 //   it. Return number of clauses removed.
1463 //
1464 // Global Variables: -
1465 //
1466 // Side Effects    : Changes set.
1467 //
1468 /----------------------------------------------------------------------*/
1469 
ClauseSetFilterTrivial(ClauseSet_p set)1470 long ClauseSetFilterTrivial(ClauseSet_p set)
1471 {
1472    Clause_p handle, next;
1473    long count = 0;
1474 
1475    assert(set);
1476    assert(!set->demod_index);
1477 
1478    handle = set->anchor->succ;
1479    while(handle != set->anchor)
1480    {
1481       next = handle->succ;
1482 
1483       assert(handle);
1484 
1485       if(ClauseIsTrivial(handle))
1486       {
1487     ClauseDetachParents(handle);
1488     ClauseSetDeleteEntry(handle);
1489     count++;
1490       }
1491       handle = next;
1492    }
1493    return count;
1494 }
1495 
1496 
1497 /*-----------------------------------------------------------------------
1498 //
1499 // Function: ClauseSetFilterTautologies()
1500 //
1501 //   Given a clause set, remove all tautologies from it. Return number
1502 //   of clauses removed.
1503 //
1504 // Global Variables: -
1505 //
1506 // Side Effects    : Changes set.
1507 //
1508 /----------------------------------------------------------------------*/
1509 
ClauseSetFilterTautologies(ClauseSet_p set,TB_p work_bank)1510 long ClauseSetFilterTautologies(ClauseSet_p set, TB_p work_bank)
1511 {
1512    Clause_p handle, next;
1513    long count = 0;
1514 
1515    assert(set);
1516    assert(!set->demod_index);
1517 
1518    handle = set->anchor->succ;
1519    while(handle != set->anchor)
1520    {
1521       next = handle->succ;
1522 
1523       assert(handle);
1524 
1525       if(ClauseIsTautology(work_bank, handle))
1526       {
1527     ClauseDetachParents(handle);
1528     ClauseSetDeleteEntry(handle);
1529     count++;
1530       }
1531       handle = next;
1532    }
1533    return count;
1534 }
1535 
1536 /*-----------------------------------------------------------------------
1537 //
1538 // Function: ClauseSetFindMaxStandardWeight()
1539 //
1540 //   Return a pointer to a clause with the largest standard weight
1541 //   among clauses in set (or NULL if set is empty).
1542 //
1543 // Global Variables: -
1544 //
1545 // Side Effects    : -
1546 //
1547 /----------------------------------------------------------------------*/
1548 
ClauseSetFindMaxStandardWeight(ClauseSet_p set)1549 Clause_p ClauseSetFindMaxStandardWeight(ClauseSet_p set)
1550 {
1551    long max_weight = 0, weight;
1552    Clause_p handle, res = NULL;
1553 
1554    assert(set);
1555 
1556    handle = set->anchor->succ;
1557    while(handle != set->anchor)
1558    {
1559       weight = ClauseStandardWeight(handle);
1560       if(weight > max_weight)
1561       {
1562     res = handle;
1563     max_weight = weight;
1564       }
1565       handle = handle->succ;
1566    }
1567    return res;
1568 }
1569 
1570 
1571 /*-----------------------------------------------------------------------
1572 //
1573 // Function: ClauseSetFindEqDefinition()
1574 //
1575 //   If set contains an equality definition at or after start, return
1576 //   the potential matching side (as a reduced clause position),
1577 //   otherwise NULL.
1578 //
1579 // Global Variables: -
1580 //
1581 // Side Effects    : -
1582 //
1583 /----------------------------------------------------------------------*/
1584 
ClauseSetFindEqDefinition(ClauseSet_p set,int min_arity,Clause_p start)1585 ClausePos_p ClauseSetFindEqDefinition(ClauseSet_p set, int min_arity,
1586                                       Clause_p start)
1587 {
1588    Clause_p handle;
1589    ClausePos_p res = NULL;
1590    EqnSide side;
1591 
1592    if(!start)
1593    {
1594       start = set->anchor->succ;
1595    }
1596 
1597    for(handle = start;
1598        handle!=set->anchor;
1599        handle = handle->succ)
1600    {
1601       side = ClauseIsEqDefinition(handle, min_arity);
1602       if(side!=NoSide)
1603       {
1604     res = ClausePosCellAlloc();
1605     res->clause = handle;
1606     res->literal = handle->literals;
1607     res->side = side;
1608     res->pos = NULL;
1609     break;
1610       }
1611    }
1612    /* if(res)
1613    {
1614       printf("# EqDef found: ");
1615       ClausePrint(stdout, res->clause, true);
1616       printf(" Side %d\n", res->side);
1617       } */
1618    return res;
1619 }
1620 
1621 /*-----------------------------------------------------------------------
1622 //
1623 // Function: ClausesSetDocInital()
1624 //
1625 //   If level >= 2, print all clauses as axioms.
1626 //
1627 // Global Variables: -
1628 //
1629 // Side Effects    : Output
1630 //
1631 /----------------------------------------------------------------------*/
1632 
ClauseSetDocInital(FILE * out,long level,ClauseSet_p set)1633 void ClauseSetDocInital(FILE* out, long level, ClauseSet_p set)
1634 {
1635    Clause_p handle;
1636 
1637    if(level>=2)
1638    {
1639       for(handle = set->anchor->succ; handle!=set->anchor; handle =
1640         handle->succ)
1641       {
1642     DocClauseCreation(out, OutputLevel, handle,
1643             inf_initial, NULL, NULL,
1644             NULL);
1645       }
1646    }
1647 }
1648 
1649 
1650 /*-----------------------------------------------------------------------
1651 //
1652 // Function: ClauseSetPropDocQuote()
1653 //
1654 //   Quote all clauses in set for which all props are set.
1655 //
1656 // Global Variables: -
1657 //
1658 // Side Effects    : Output
1659 //
1660 /----------------------------------------------------------------------*/
1661 
ClauseSetPropDocQuote(FILE * out,long level,FormulaProperties prop,ClauseSet_p set,char * comment)1662 void ClauseSetPropDocQuote(FILE* out, long level, FormulaProperties prop,
1663             ClauseSet_p set, char* comment)
1664 {
1665    Clause_p handle;
1666 
1667    if(level>=2)
1668    {
1669       for(handle = set->anchor->succ; handle!=set->anchor; handle =
1670         handle->succ)
1671       {
1672     if(ClauseQueryProp(handle, prop))
1673     {
1674        DocClauseQuote(out, level, 2, handle, comment , NULL);
1675     }
1676       }
1677    }
1678 }
1679 
1680 #ifndef NDBUG
1681 
1682 /*-----------------------------------------------------------------------
1683 //
1684 // Function: ClauseSetVerifyDemod()
1685 //
1686 //   Return true if pos->clause is in clause set, is a demodulator,
1687 //   and if pos describes a potential maximal side in clause.
1688 //
1689 // Global Variables: -
1690 //
1691 // Side Effects    : -
1692 //
1693 /----------------------------------------------------------------------*/
1694 
ClauseSetVerifyDemod(ClauseSet_p demods,ClausePos_p pos)1695 bool ClauseSetVerifyDemod(ClauseSet_p demods, ClausePos_p pos)
1696 {
1697    assert(demods);
1698    assert(pos);
1699    assert(TermPosIsTopPos(pos->pos));
1700 
1701    if(!ClauseSetFind(demods, pos->clause))
1702    {
1703       return false;
1704    }
1705    if(!ClauseIsDemodulator(pos->clause))
1706    {
1707       return false;
1708    }
1709    if((pos->side == RightSide) && EqnIsOriented(pos->clause->literals))
1710    {
1711       return false;
1712    }
1713    return true;
1714 }
1715 
1716 
1717 /*-----------------------------------------------------------------------
1718 //
1719 // Function: PDTreeVerifyIndex()
1720 //
1721 //   Check if all clauses in index are in demod as well.
1722 //
1723 // Global Variables: -
1724 //
1725 // Side Effects    : -
1726 //
1727 /----------------------------------------------------------------------*/
1728 
PDTreeVerifyIndex(PDTree_p tree,ClauseSet_p demods)1729 bool PDTreeVerifyIndex(PDTree_p tree, ClauseSet_p demods)
1730 {
1731    PStack_p stack = PStackAlloc(), trav;
1732    PDTNode_p handle;
1733    long             i;
1734    IntMapIter_p     iter;
1735    ClausePos_p      pos;
1736    PTree_p          entry;
1737    bool             res = true;
1738 
1739    PStackPushP(stack, tree->tree);
1740 
1741    while(!PStackEmpty(stack))
1742    {
1743       handle = PStackPopP(stack);
1744 
1745       if(!handle->entries)
1746       {
1747          iter = IntMapIterAlloc(handle->f_alternatives, 0, LONG_MAX);
1748          while((handle = IntMapIterNext(iter, &i)))
1749     {
1750        if(handle)
1751        {
1752           PStackPushP(stack, handle);
1753        }
1754     }
1755          IntMapIterFree(iter);
1756     for(i=1; i<=handle->max_var; i++)
1757     {
1758        if(PDArrayElementP(handle->v_alternatives, i))
1759        {
1760           PStackPushP(stack, PDArrayElementP(handle->v_alternatives, i));
1761        }
1762     }
1763       }
1764       else
1765       {
1766     trav = PTreeTraverseInit(handle->entries);
1767     while((entry = PTreeTraverseNext(trav)))
1768     {
1769        pos = entry->key;
1770        if(!ClauseSetFind(demods, pos->clause))
1771        {
1772           res = false;
1773           /* printf("Fehler: %d\n", (int)pos->clause);
1774         ClausePrint(stdout, pos->clause, true);
1775         printf("\n"); */
1776        }
1777     }
1778     PTreeTraverseExit(trav);
1779          iter = IntMapIterAlloc(handle->f_alternatives, 0, LONG_MAX);
1780          while((handle = IntMapIterNext(iter, &i)))
1781     {
1782             assert(!handle);
1783          }
1784          IntMapIterFree(iter);
1785     for(i=0; i<handle->max_var; i++)
1786     {
1787        assert(!PDArrayElementP(handle->v_alternatives, i));
1788     }
1789       }
1790    }
1791    PStackFree(stack);
1792 
1793    return res;
1794 }
1795 
1796 #endif
1797 
1798 /*-----------------------------------------------------------------------
1799 //
1800 // Function: EqAxiomsPrint()
1801 //
1802 //   Print the equality axioms (symmetry, transitivity, refexivity,
1803 //   substitutivity) for the given signature.
1804 //
1805 // Global Variables: -
1806 //
1807 // Side Effects    : -
1808 //
1809 /----------------------------------------------------------------------*/
1810 
EqAxiomsPrint(FILE * out,Sig_p sig,bool single_subst)1811 void EqAxiomsPrint(FILE* out, Sig_p sig, bool single_subst)
1812 {
1813    FunCode i;
1814    int arity;
1815 
1816    if(OutputFormat == TPTPFormat)
1817    {
1818       fprintf(out,
1819          "input_clause(eq_reflexive, axiom, [++equal(X,X)]).\n"
1820          "input_clause(eq_symmetric, axiom,"
1821          " [++equal(X,Y),--equal(Y,X)]).\n"
1822          "input_clause(eq_transitive, axiom,"
1823          " [++equal(X,Z),--equal(X,Y),--equal(Y,Z)]).\n");
1824       for(i=sig->internal_symbols+1; i<=sig->f_count; i++)
1825       {
1826     if((arity=SigFindArity(sig, i)))
1827     {
1828        if(SigIsPredicate(sig,i))
1829        {
1830           tptp_eq_pred_axiom_print(out, SigFindName(sig,i),
1831                arity, single_subst);
1832        }
1833        else
1834        {
1835           tptp_eq_func_axiom_print(out, SigFindName(sig,i),
1836                arity, single_subst);
1837        }
1838     }
1839       }
1840    }
1841    else if(OutputFormat == TSTPFormat)
1842    {
1843       Error("Adding of equality axioms not (yet) supported for TSTP/TPTP-3 format.",
1844             OTHER_ERROR);
1845    }
1846    else
1847    {
1848       fprintf(out,
1849          "equal(X,X) <- .\n"
1850          "equal(X,Y) <- equal(Y,X).\n"
1851          "equal(X,Z) <- equal(X,Y), equal(Y,Z).\n");
1852       for(i=sig->internal_symbols+1; i<=sig->f_count; i++)
1853       {
1854     if((arity=SigFindArity(sig, i)))
1855     {
1856        if(SigIsPredicate(sig,i))
1857        {
1858           eq_pred_axiom_print(out, SigFindName(sig,i), arity,
1859                single_subst);
1860        }
1861        else
1862        {
1863           eq_func_axiom_print(out, SigFindName(sig,i), arity,
1864                single_subst);
1865        }
1866     }
1867       }
1868    }
1869 }
1870 
1871 
1872 
1873 /*-----------------------------------------------------------------------
1874 //
1875 // Function: ClauseSetAddSymbolDistribution()
1876 //
1877 //   Count the occurrences of function symbols in set.
1878 //
1879 // Global Variables: -
1880 //
1881 // Side Effects    : -
1882 //
1883 /----------------------------------------------------------------------*/
1884 
ClauseSetAddSymbolDistribution(ClauseSet_p set,long * dist_array)1885 void ClauseSetAddSymbolDistribution(ClauseSet_p set, long *dist_array)
1886 {
1887    Clause_p handle;
1888 
1889    for(handle = set->anchor->succ; handle!=set->anchor; handle =
1890      handle->succ)
1891    {
1892       ClauseAddSymbolDistribution(handle, dist_array);
1893    }
1894 }
1895 
1896 
1897 /*-----------------------------------------------------------------------
1898 //
1899 // Function: ClauseSetAddConjSymbolDistribution()
1900 //
1901 //   Count the occurrences of function symbols in conjectures in set.
1902 //
1903 // Global Variables: -
1904 //
1905 // Side Effects    : -
1906 //
1907 /----------------------------------------------------------------------*/
1908 
ClauseSetAddConjSymbolDistribution(ClauseSet_p set,long * dist_array)1909 void ClauseSetAddConjSymbolDistribution(ClauseSet_p set, long *dist_array)
1910 {
1911    Clause_p handle;
1912 
1913    for(handle = set->anchor->succ; handle!=set->anchor; handle =
1914      handle->succ)
1915    {
1916       if(ClauseIsConjecture(handle))
1917       {
1918          ClauseAddSymbolDistribution(handle, dist_array);
1919       }
1920    }
1921 }
1922 
1923 
1924 /*-----------------------------------------------------------------------
1925 //
1926 // Function: ClauseSetComputeFunctionRanks()
1927 //
1928 //   Assign to each function symbol a uniq number based on its
1929 //   position in the clause set.
1930 //
1931 // Global Variables: -
1932 //
1933 // Side Effects    : -
1934 //
1935 /----------------------------------------------------------------------*/
1936 
ClauseSetComputeFunctionRanks(ClauseSet_p set,long * rank_array,long * count)1937 void ClauseSetComputeFunctionRanks(ClauseSet_p set, long *rank_array,
1938                 long* count)
1939 {
1940    Clause_p handle;
1941 
1942    for(handle = set->anchor->succ; handle!=set->anchor; handle =
1943      handle->succ)
1944    {
1945       ClauseComputeFunctionRanks(handle, rank_array, count);
1946    }
1947 }
1948 
1949 
1950 /*-----------------------------------------------------------------------
1951 //
1952 // Function: ClauseSetFindFreqSymbol()
1953 //
1954 //   Find the most/least frequent non-special, non-predicate symbol of the
1955 //   given arity in the clause set.
1956 //
1957 // Global Variables: -
1958 //
1959 // Side Effects    : Memory Operations
1960 //
1961 /----------------------------------------------------------------------*/
1962 
ClauseSetFindFreqSymbol(ClauseSet_p set,Sig_p sig,int arity,bool least)1963 FunCode ClauseSetFindFreqSymbol(ClauseSet_p set, Sig_p sig, int arity,
1964             bool least)
1965 {
1966    FunCode       i, selected=0;
1967    long          *dist_array,freq=least?LONG_MAX:0;
1968 
1969    dist_array = SizeMalloc((sig->f_count+1) * sizeof(long));
1970 
1971    for(i=0; i<=sig->f_count; i++)
1972    {
1973       dist_array[i] = 0;
1974    }
1975    ClauseSetAddSymbolDistribution(set, dist_array);
1976 
1977    for(i=sig->internal_symbols+1; i<= sig->f_count; i++)
1978    {
1979       if((SigFindArity(sig,i)==arity) && !SigIsPredicate(sig,i) &&
1980     !SigIsSpecial(sig,i))
1981       {
1982     if((least && (dist_array[i]<=freq)) ||
1983        (dist_array[i]>=freq))
1984     {
1985        freq = dist_array[i];
1986        selected = i;
1987     }
1988       }
1989    }
1990    SizeFree(dist_array, (sig->f_count+1)*sizeof(long));
1991 
1992    return selected;
1993 }
1994 
1995 
1996 /*-----------------------------------------------------------------------
1997 //
1998 // Function: ClauseSetMaxVarNumber()
1999 //
2000 //   Return the largest number of variables occurring in any clause.
2001 //
2002 // Global Variables: -
2003 //
2004 // Side Effects    : Memory operations
2005 //
2006 /----------------------------------------------------------------------*/
2007 
ClauseSetMaxVarNumber(ClauseSet_p set)2008 long ClauseSetMaxVarNumber(ClauseSet_p set)
2009 {
2010    long res=0, tmp;
2011    PTree_p tree;
2012    Clause_p handle;
2013 
2014    for(handle = set->anchor->succ; handle!= set->anchor; handle =
2015      handle->succ)
2016    {
2017       tree = NULL;
2018       tmp = ClauseCollectVariables(handle, &tree);
2019       PTreeFree(tree);
2020       res = MAX(res, tmp);
2021    }
2022    return res;
2023 }
2024 
2025 
2026 /*-----------------------------------------------------------------------
2027 //
2028 // Function: ClauseSetFindCharFreqVectors()
2029 //
2030 //   Compute the characteristic frequency vectors for set. Vectors are
2031 //   re-initialized. Returns number of clauses in set.
2032 //
2033 // Global Variables: -
2034 //
2035 // Side Effects    : Memory operations.
2036 //
2037 /----------------------------------------------------------------------*/
2038 
ClauseSetFindCharFreqVectors(ClauseSet_p set,FreqVector_p fsum,FreqVector_p fmax,FreqVector_p fmin,FVCollect_p cspec)2039 long ClauseSetFindCharFreqVectors(ClauseSet_p set, FreqVector_p fsum,
2040               FreqVector_p fmax, FreqVector_p fmin,
2041               FVCollect_p cspec)
2042 {
2043    Clause_p handle;
2044    FreqVector_p current;
2045 
2046    assert(set && fsum && fmax && fmin);
2047 
2048    FreqVectorInitialize(fsum, 0);
2049    FreqVectorInitialize(fmax, 0);
2050    FreqVectorInitialize(fmin, LONG_MAX);
2051 
2052    for(handle = set->anchor->succ;
2053        handle!= set->anchor;
2054        handle = handle->succ)
2055    {
2056       current = VarFreqVectorCompute(handle, cspec);
2057       FreqVectorAdd(fsum, fsum, current);
2058       FreqVectorMax(fmax, fmax, current);
2059       FreqVectorMin(fmin, fmin, current);
2060       FreqVectorFree(current);
2061    }
2062    return set->members;
2063 }
2064 
2065 
2066 /*-----------------------------------------------------------------------
2067 //
2068 // Function: PermVectorCompute()
2069 //
2070 //   Given a clause set and parameters for an index, compute a
2071 //   suitable permutation vector (may be NULL if the parameters do not call
2072 //   for a permutation vector!)
2073 //
2074 // Global Variables: -
2075 //
2076 // Side Effects    : Memory operations
2077 //
2078 /----------------------------------------------------------------------*/
2079 
PermVectorCompute(ClauseSet_p set,FVCollect_p cspec,bool eliminate_uninformative)2080 PermVector_p PermVectorCompute(ClauseSet_p set, FVCollect_p cspec,
2081                                bool eliminate_uninformative)
2082 {
2083    PermVector_p res;
2084    FreqVector_p fsum, fmax, fmin;
2085    long vlen;
2086 
2087    if(cspec->features == FVINoFeatures)
2088    {
2089       return NULL;
2090    }
2091 
2092    if(cspec->features == FVICollectFeatures)
2093    {
2094       vlen = cspec->res_vec_len;
2095    }
2096    else
2097    {
2098       vlen = FVSize(cspec->max_symbols, cspec->features);
2099    }
2100    fsum = FreqVectorAlloc(vlen);
2101    fmax = FreqVectorAlloc(vlen);
2102    fmin = FreqVectorAlloc(vlen);
2103 
2104    ClauseSetFindCharFreqVectors(set,
2105                                 fsum,
2106                                 fmax,
2107                                 fmin,
2108                                 cspec);
2109 
2110    res = PermVectorComputeInternal(fmax, fmin, fsum,
2111                                    cspec->max_symbols,
2112                                    eliminate_uninformative);
2113    FreqVectorFree(fsum);
2114    FreqVectorFree(fmax);
2115    FreqVectorFree(fmin);
2116 
2117    return res;
2118 }
2119 
2120 
2121 /*-----------------------------------------------------------------------
2122 //
2123 // Function: ClauseSetFVIndexify()
2124 //
2125 //   Remove all clauses from set and insert them again as indexed
2126 //   clauses. Return number of clauses in set.
2127 //
2128 // Global Variables: -
2129 //
2130 // Side Effects    : Memory operations
2131 //
2132 /----------------------------------------------------------------------*/
2133 
ClauseSetFVIndexify(ClauseSet_p set)2134 long ClauseSetFVIndexify(ClauseSet_p set)
2135 {
2136    PStack_p stack = PStackAlloc();
2137    Clause_p clause;
2138 
2139    assert(set);
2140    assert(set->fvindex);
2141 
2142    while((clause = ClauseSetExtractFirst(set)))
2143    {
2144       PStackPushP(stack, clause);
2145    }
2146    while(!PStackEmpty(stack))
2147    {
2148       clause = PStackPopP(stack);
2149       assert(clause->weight == ClauseStandardWeight(clause));
2150       ClauseSetIndexedInsertClause(set, clause);
2151    }
2152    PStackFree(stack);
2153    return set->members;
2154 }
2155 
2156 
2157 /*-----------------------------------------------------------------------
2158 //
2159 // Function: ClauseSetNewTerms()
2160 //
2161 //   Substitute all clause in set with otherwise identical copies
2162 //   taking terms from the new termbank.
2163 //
2164 // Global Variables: -
2165 //
2166 // Side Effects    : May indexify the clause set.
2167 //
2168 /----------------------------------------------------------------------*/
2169 
ClauseSetNewTerms(ClauseSet_p set,TB_p terms)2170 long ClauseSetNewTerms(ClauseSet_p set, TB_p terms)
2171 {
2172    PStack_p stack = PStackAlloc();
2173    Clause_p clause, copy;
2174 
2175    assert(set);
2176 
2177    while((clause = ClauseSetExtractFirst(set)))
2178    {
2179       PStackPushP(stack, clause);
2180    }
2181    while(!PStackEmpty(stack))
2182    {
2183       clause = PStackPopP(stack);
2184       copy = ClauseCopy(clause, terms);
2185       assert(copy->weight == ClauseStandardWeight(copy));
2186       ClauseSetIndexedInsertClause(set, copy);
2187       ClauseFree(clause);
2188    }
2189    PStackFree(stack);
2190    return set->members;
2191 }
2192 
2193 
2194 
2195 /*-----------------------------------------------------------------------
2196 //
2197 // Function: ClauseSetSplitConjectures()
2198 //
2199 //   Find all (real or negated) conjectures in set and sort them into
2200 //   conjectures. Collect the rest in rest. Return number of
2201 //   conjectures found.
2202 //
2203 // Global Variables: -
2204 //
2205 // Side Effects    : Memory operations
2206 //
2207 /----------------------------------------------------------------------*/
2208 
ClauseSetSplitConjectures(ClauseSet_p set,PList_p conjectures,PList_p rest)2209 long ClauseSetSplitConjectures(ClauseSet_p set,
2210                                PList_p conjectures, PList_p rest)
2211 {
2212    Clause_p handle;
2213    long     res = 0;
2214 
2215    for(handle = set->anchor->succ;
2216        handle!=set->anchor;
2217        handle = handle->succ)
2218    {
2219       if(ClauseIsConjecture(handle))
2220       {
2221          PListStoreP(conjectures, handle);
2222          res++;
2223       }
2224       else
2225       {
2226          PListStoreP(rest, handle);
2227       }
2228    }
2229    return res;
2230 }
2231 
2232 /*-----------------------------------------------------------------------
2233 //
2234 // Function: ClauseSetStandardWeight()
2235 //
2236 //   Return the sum of the standardweight of all clauses in set.
2237 //
2238 // Global Variables: -
2239 //
2240 // Side Effects    : -
2241 //
2242 /----------------------------------------------------------------------*/
2243 
ClauseSetStandardWeight(ClauseSet_p set)2244 long long ClauseSetStandardWeight(ClauseSet_p set)
2245 {
2246    Clause_p  handle;
2247    long long res = 0;
2248 
2249    for(handle = set->anchor->succ;
2250        handle!=set->anchor;
2251        handle = handle->succ)
2252    {
2253       res += ClauseStandardWeight(handle);
2254    }
2255    return res;
2256 
2257 }
2258 
2259 
2260 /*-----------------------------------------------------------------------
2261 //
2262 // Function: ClauseSetDerivationStackStatistics()
2263 //
2264 //   Compute and print the stack depth distribution of the clauses in
2265 //   set.
2266 //
2267 // Global Variables:
2268 //
2269 // Side Effects    :
2270 //
2271 /----------------------------------------------------------------------*/
2272 
ClauseSetDerivationStackStatistics(ClauseSet_p set)2273 void ClauseSetDerivationStackStatistics(ClauseSet_p set)
2274 {
2275    Clause_p handle;
2276    long     i;
2277    PDArray_p dist = PDArrayAlloc(8,8);
2278    double    sum = 0.0;
2279 
2280    for(handle = set->anchor->succ; handle!=set->anchor; handle = handle->succ)
2281    {
2282       if(handle->derivation)
2283       {
2284          PDArrayElementIncInt(dist, PStackGetSP(handle->derivation), 1);
2285       }
2286       else
2287       {
2288          PDArrayElementIncInt(dist, 0, 1);
2289       }
2290    }
2291    for(i=0; i<PDArraySize(dist); i++)
2292    {
2293       printf("# %5ld: %6ld\n", i, PDArrayElementInt(dist,i));
2294       sum += PDArrayElementInt(dist,i)*i;
2295    }
2296    printf("# Average over %ld clauses: %f\n",
2297           ClauseSetCardinality(set),
2298           sum/ClauseSetCardinality(set));
2299    PDArrayFree(dist);
2300 }
2301 
2302 
2303 /*-----------------------------------------------------------------------
2304 //
2305 // Function: ClauseSetPushClauses()
2306 //
2307 //   Push all clauses in set onto stack. Return number pushed.
2308 //
2309 // Global Variables: -
2310 //
2311 // Side Effects    : Memory operations
2312 //
2313 /----------------------------------------------------------------------*/
2314 
ClauseSetPushClauses(PStack_p stack,ClauseSet_p set)2315 long ClauseSetPushClauses(PStack_p stack, ClauseSet_p set)
2316 {
2317    Clause_p handle;
2318    long     res = 0;
2319 
2320    for(handle = set->anchor->succ; handle!=set->anchor; handle = handle->succ)
2321    {
2322       PStackPushP(stack, handle);
2323       res++;
2324    }
2325    return res;
2326 }
2327 
2328 /*-----------------------------------------------------------------------
2329 //
2330 // Function: ClauseSetDefaultWeighClauses()
2331 //
2332 //   Set the (standard) weight in all clauses in set.
2333 //
2334 // Global Variables: -
2335 //
2336 // Side Effects    : -
2337 //
2338 /----------------------------------------------------------------------*/
2339 
ClauseSetDefaultWeighClauses(ClauseSet_p set)2340 void ClauseSetDefaultWeighClauses(ClauseSet_p set)
2341 {
2342    Clause_p handle;
2343 
2344    for(handle = set->anchor->succ; handle!=set->anchor; handle = handle->succ)
2345    {
2346       handle->weight = ClauseStandardWeight(handle);
2347    }
2348 }
2349 
2350 
2351 /*-----------------------------------------------------------------------
2352 //
2353 // Function: ClauseSetCountConjectures()
2354 //
2355 //   Count and return number of conjectures (and negated_conjectures)
2356 //   in set. Also find number of hypotheses,  and add it to *hypos.
2357 //
2358 // Global Variables: -
2359 //
2360 // Side Effects    : -
2361 //
2362 /----------------------------------------------------------------------*/
2363 
ClauseSetCountConjectures(ClauseSet_p set,long * hypos)2364 long ClauseSetCountConjectures(ClauseSet_p set, long* hypos)
2365 {
2366    long ret = 0;
2367    Clause_p handle;
2368 
2369 
2370    for(handle = set->anchor->succ;
2371        handle != set->anchor;
2372        handle = handle->succ)
2373    {
2374       if(ClauseIsConjecture(handle))
2375       {
2376          ret++;
2377       }
2378       if(ClauseIsHypothesis(handle))
2379       {
2380          (*hypos)++;
2381       }
2382    }
2383    return ret;
2384 }
2385 
2386 
2387 /*-----------------------------------------------------------------------
2388 //
2389 // Function: ClauseSetIsUntyped
2390 //
2391 //   Returns true iff all clauses of the set are untyped
2392 //
2393 // Global Variables: -
2394 //
2395 // Side Effects    : Memory operations
2396 //
2397 /----------------------------------------------------------------------*/
ClauseSetIsUntyped(ClauseSet_p set)2398 bool ClauseSetIsUntyped(ClauseSet_p set)
2399 {
2400    Clause_p handle;
2401 
2402    for(handle = set->anchor->succ; handle!=set->anchor; handle = handle->succ)
2403    {
2404       if (!ClauseIsUntyped(handle))
2405       {
2406     return false;
2407       }
2408    }
2409    return true;
2410 }
2411 
2412 
2413 /*---------------------------------------------------------------------*/
2414 /*                        End of File                                  */
2415 /*---------------------------------------------------------------------*/
2416