1 /*
2  *  clause.c -- This file has routines associated with the clause data type.
3  *
4  */
5 
6 #include "header.h"
7 
8 #define CLAUSE_TAB_SIZE 3793
9 
10 /* hash table for accessing clauses by ID */
11 static struct clause_ptr *Clause_tab[CLAUSE_TAB_SIZE];
12 
13 /* clause ID counter */
14 static int Clause_id_count;
15 
16 /* back subsumed, demodulated put here, not deleted */
17 static struct clause *Hidden_clauses;
18 
19 /* array to mark mapped literals during subsumption */
20 #define MAX_LITS 100
21 static char Map_array[MAX_LITS];
22 
23 /*************
24  *
25  *   reset_clause_counter()
26  *
27  *************/
28 
reset_clause_counter(void)29 void reset_clause_counter(void)
30 {
31   Clause_id_count = 0;
32 }  /* reset_clause_counter */
33 
34 /*************
35  *
36  *    int next_cl_num()
37  *
38  *    What is the next clause number?  Do not increment the count.
39  *
40  *************/
41 
next_cl_num(void)42 int next_cl_num(void)
43 {
44   return(Clause_id_count + 1);
45 }  /* next_cl_num */
46 
47 /*************
48  *
49  *    assign_cl_id()
50  *
51  *    Assign a unique clause identifier and insert into the clause table.
52  *
53  *************/
54 
assign_cl_id(struct clause * c)55 void assign_cl_id(struct clause *c)
56 {
57   c->id = ++Clause_id_count;
58   cl_insert_tab(c);
59 
60   /* Turn debugging mode on when DEBUG_FIRST - 1 is assigned. */
61   if ( (c->id == (Parms[DEBUG_FIRST].val - 1)) && !Flags[VERY_VERBOSE].val)
62     {
63       Flags[VERY_VERBOSE].val = 1;
64       fprintf(stdout, "\n\n***** Turn Debugging Mode ON *****\n\n");
65     }
66 
67   /* Turn debugging mode off when DEBUG_LAST + 1 is assigned. */
68   if ( (c->id == (Parms[DEBUG_LAST].val + 1)) && Flags[VERY_VERBOSE].val)
69     {
70       Flags[VERY_VERBOSE].val = 0;
71       fprintf(stdout, "\n\n***** Turn Debugging Mode OFF *****\n\n");
72     }
73 }  /* assign_cl_id */
74 
75 /*************
76  *
77  *   hot_cl_integrate(c)
78  *
79  *   Integrate a hot-list clause.  All this does is assign a clause ID.
80  *   The subterms are not put into the shared data structures, because
81  *   this interferes with the kludgy way I implemented hot-list inference.
82  *   In particular, hot paramoudlation from the new clause can't
83  *   handle going into hot-list clauses that are integrated in the
84  *   normal way.
85  *
86  *************/
87 
hot_cl_integrate(struct clause * c)88 void hot_cl_integrate(struct clause *c)
89 {
90   struct literal *lit;
91 
92   assign_cl_id(c);
93 
94   for (lit = c->first_lit; lit; lit = lit->next_lit) {
95     set_up_pointers(lit->atom);
96     lit->atom->occ.lit = lit;
97   }
98 }  /* hot_cl_integrate */
99 
100 /*************
101  *
102  *    cl_integrate(c) -- integrate a clause
103  *
104  *    This routine integrates most subterms of the atoms. (Incoming clause must
105  *    already have back pointers from literal to clause and atom to literal.)
106  *
107  *    The atoms are not shared, and arguments of positive equality literals
108  *    are not shared.
109  *
110  *    A clause is integrated iff its ID is > 0.
111  *
112  *************/
113 
cl_integrate(struct clause * c)114 void cl_integrate(struct clause *c)
115 {
116   struct literal *lit;
117   struct term *atom;
118   struct rel *r, *r1;
119 
120   if (c->id != 0) {
121     fprintf(stdout, "WARNING, cl_integrate gets clause with ID: ");
122     print_clause(stdout, c);
123   }
124   else {
125     if (multi_justifications()) {
126       /* for proof-shortening experiment */
127       possibly_append_parents(c, c->parents);
128     }
129     assign_cl_id(c);
130     lit = c->first_lit;
131     while (lit) {
132       atom = lit->atom;
133       if (atom->varnum == POS_EQ || atom->varnum == LEX_DEP_DEMOD ||
134 	  atom->varnum == CONDITIONAL_DEMOD) {
135 	/* do not share (condition), alpha, beta */
136 	r1 = atom->farg;
137 	while (r1) {  /* for alpha and beta */
138 	  if (Flags[BACK_DEMOD].val)
139 	    /* put it where back demod can find it */
140 	    bd_kludge_insert(r1->argval);
141 	  if (r1->argval->type == COMPLEX) {
142 	    r = r1->argval->farg;
143 	    while (r) {
144 	      r->argval = integrate_term(r->argval);
145 	      r->argof = r1->argval;
146 	      r->nocc = r->argval->occ.rel;
147 	      r->argval->occ.rel = r;
148 	      r = r->narg;
149 	    }
150 	  }
151 	  r1->argof = atom;
152 	  r1->argval->occ.rel = r1;
153 	  r1 = r1->narg;
154 	}
155       }
156       else if (atom->type == COMPLEX) {
157 	r = atom->farg;
158 	while (r) {
159 	  r->argval = integrate_term(r->argval);
160 	  r->argof = atom;
161 	  r->nocc = r->argval->occ.rel;
162 	  r->argval->occ.rel = r;
163 	  r = r->narg;
164 	}
165       }
166       lit = lit->next_lit;
167     }
168   }
169 
170 }  /* cl_integrate */
171 
172 /*************
173  *
174  *    cl_del_int(c) -- deallocate an integrated clause.
175  *
176  *************/
177 
cl_del_int(struct clause * c)178 void cl_del_int(struct clause *c)
179 {
180   struct literal *lit, *plit;
181   struct rel *r, *r2, *pr, *r1;
182   struct term *atom;
183   struct ilist *ip1, *ip2;
184 
185   lit = c->first_lit;
186   while (lit) {
187     atom = lit->atom;
188     if (atom->varnum == POS_EQ || atom->varnum == LEX_DEP_DEMOD ||
189 	atom->varnum == CONDITIONAL_DEMOD) {
190       /* (condition), alpha, beta not shared */
191       r1 = atom->farg;
192       while(r1) {  /* for alpha and beta */
193 	if (Flags[BACK_DEMOD].val)
194 	  bd_kludge_delete(r1->argval);  /* back demod kludge */
195 	r = r1->argval->farg;
196 	while (r) {
197 	  r2 = r->argval->occ.rel;
198 	  pr = NULL;
199 	  while (r2 && r2 != r) {
200 	    pr = r2;
201 	    r2 = r2->nocc;
202 	  }
203 	  if (!r2) {
204 	    print_clause(stdout, c);
205 	    abend("cl_del_int, bad equality clause.");
206 	  }
207 	  if (pr)
208 	    pr->nocc = r->nocc;
209 	  else
210 	    r->argval->occ.rel = r->nocc;
211 	  if (!r->argval->occ.rel)
212 	    disintegrate_term(r->argval);
213 	  pr = r;
214 	  r = r->narg;
215 	  free_rel(pr);
216 	}
217 	free_term(r1->argval);  /* alpha or beta */
218 	pr = r1;
219 	r1 = r1->narg;
220 	free_rel(pr);
221       }
222     }
223     else if (atom->type == COMPLEX) {
224       r = atom->farg;
225       while (r) {
226 	r2 = r->argval->occ.rel;
227 	pr = NULL;
228 	while (r2 && r2 != r) {
229 	  pr = r2;
230 	  r2 = r2->nocc;
231 	}
232 	if (!r2) {
233 	  print_clause(stdout, c);
234 	  abend("cl_del_int, bad clause.");
235 	}
236 	if (!pr)
237 	  r->argval->occ.rel = r->nocc;
238 	else
239 	  pr->nocc = r->nocc;
240 	if (!r->argval->occ.rel)
241 	  disintegrate_term(r->argval);
242 	pr = r;
243 	r = r->narg;
244 	free_rel(pr);
245       }
246     }
247     free_term(atom);
248     plit = lit;
249     lit = lit->next_lit;
250     free_literal(plit);
251   }
252   ip1 = c->parents;
253   while (ip1) {
254     ip2 = ip1;
255     ip1 = ip1->next;
256     free_ilist(ip2);
257   }
258   cl_delete_tab(c);
259   /* If there is other memory associated with clause, free it here */
260   delete_attributes(c);
261   free_clause(c);
262 }  /* cl_del_int */
263 
264 /*************
265  *
266  *    cl_del_non(c) -- deallocate a nonintegrated clause.
267  *
268  *************/
269 
cl_del_non(struct clause * c)270 void cl_del_non(struct clause *c)
271 {
272   struct literal *lit, *plit;
273   struct ilist *ip1, *ip2;
274 
275   lit = c->first_lit;
276   while (lit) {
277     lit->atom->occ.lit = NULL;
278     zap_term(lit->atom);
279     plit = lit;
280     lit = lit->next_lit;
281     free_literal(plit);
282   }
283   ip1 = c->parents;
284   while (ip1) {
285     ip2 = ip1;
286     ip1 = ip1->next;
287     free_ilist(ip2);
288   }
289   /* If there is other memory associated with clause, free it here */
290   delete_attributes(c);
291   free_clause(c);
292 }  /* cl_del_non */
293 
294 /*************
295  *
296  *    cl_int_chk(c) -- check structure of clause -- for debugging
297  *
298  *************/
299 
cl_int_chk(struct clause * c)300 void cl_int_chk(struct clause *c)
301 {
302   struct literal *lit;
303 
304   printf("checking clause, address:%p " , (void *) c);
305   print_clause(stdout, c);
306   lit = c->first_lit;
307   while (lit) {
308     printf("    literal, address:%p sign:%d type:%d; atom:", (void *) lit, lit->sign, lit->atom->varnum);
309     print_term(stdout, lit->atom); printf("\n");
310     printf("    cont_cl:%p, atom container:%p\n", (void *) lit->container, (void *) lit->atom->occ.lit);
311     lit = lit->next_lit;
312   }
313 }  /* cl_int_chk */
314 
315 /*************
316  *
317  *    struct term *literals_to_term(l)
318  *
319  *    Conver list of literals to right-associated "|" term.
320  *
321  *************/
322 
literals_to_term(struct literal * l)323 static struct term *literals_to_term(struct literal *l)
324 {
325   struct term *t, *t1, *t2;
326   struct rel *r1, *r2;
327 
328   if (l->sign)
329     t1 = copy_term(l->atom);
330   else {
331     t1 = get_term();
332     r1 = get_rel();
333     t1->farg = r1;
334     t1->type = COMPLEX;
335     t1->sym_num = str_to_sn("-", 1);
336     r1->argval = copy_term(l->atom);
337   }
338   if (l->next_lit) {
339     t2 = literals_to_term(l->next_lit);
340     t = get_term(); r1 = get_rel(); r2 = get_rel();
341     t->farg = r1; r1->narg = r2;
342     r1->argval = t1; r2->argval = t2;
343     t->type = COMPLEX;
344     t->sym_num = str_to_sn("|", 2);
345   }
346   else
347     t = t1;
348   return(t);
349 }  /* literals_to_term */
350 
351 /*************
352  *
353  *    struct term *clause_to_term(c)
354  *
355  *************/
356 
clause_to_term(struct clause * c)357 struct term *clause_to_term(struct clause *c)
358 {
359   struct term *t;
360 
361   if (c->first_lit)
362     t = literals_to_term(c->first_lit);
363   else {
364     t = get_term();
365     t->type = NAME;
366     t->sym_num = str_to_sn("$F", 0);
367   }
368   return(t);
369 }  /* clause_to_term */
370 
371 /*************
372  *
373  *    struct literal *term_to_literals(t, lits)
374  *
375  *************/
376 
term_to_literals(struct term * t,struct literal * lits)377 static struct literal *term_to_literals(struct term *t,
378 					struct literal *lits)
379 {
380   struct literal *l;
381 
382   if (!is_symbol(t, "|", 2)) {
383     l = get_literal();
384     l->next_lit = lits;
385     l->sign = !is_symbol(t, "-", 1);
386     if (l->sign)
387       l->atom = copy_term(t);
388     else
389       l->atom = copy_term(t->farg->argval);
390     return(l);
391   }
392   else {
393     l = term_to_literals(t->farg->narg->argval, lits);
394     l = term_to_literals(t->farg->argval, l);
395     return(l);
396   }
397 }  /* term_to_literals */
398 
399 /*************
400  *
401  *    struct clause *term_to_clause(t)
402  *
403  *    If error found, print message and return NULL.
404  *
405  *************/
406 
term_to_clause(struct term * t)407 struct clause *term_to_clause(struct term *t)
408 {
409   struct clause *c;
410   struct literal *l;
411 
412   c = get_clause();
413 
414   if (is_symbol(t, "#", 2)) {
415     /* Right arg is attributes, left arg is clause. */
416     c->attributes = term_to_attributes(t->farg->narg->argval);
417     if (!c->attributes)
418       return(NULL);
419     else
420       t = t->farg->argval;
421   }
422 
423   c->first_lit = term_to_literals(t, (struct literal *) NULL);
424 
425   for (l = c->first_lit; l; l = l->next_lit) {
426     if (l->atom->type == VARIABLE) {
427       fprintf(stdout, "\nERROR, clause contains variable literal:\n");
428       print_term(stdout, t); printf(".\n\n");
429       return(NULL);
430     }
431     else {
432       l->container = c;
433       l->atom->occ.lit = l;
434       mark_literal(l);
435     }
436   }
437   return(c);
438 }  /* term_to_clause */
439 
440 /*************
441  *
442  *    struct clause *read_sequent_clause(fp, retcd_ptr)
443  *
444  *    retcd - 0:  error (NULL returned)
445  *            1:  ok    (NULL returned if EOF encountered)
446  *
447  *    a,b,c->d,e,f.
448  *    ->d,e,f.
449  *    a,b,c->.
450  *    ->.
451  *
452  *    This is really ugly, kludgey code.
453  *
454  *************/
455 
read_sequent_clause(FILE * fp,int * rcp)456 struct clause *read_sequent_clause(FILE *fp,
457 				   int *rcp)
458 {
459   struct clause *c;
460   struct literal *l, *prev;
461   struct rel *r1, *r2;
462   char buf1[MAX_BUF+1];
463   char buf2[MAX_BUF+6];
464   int rc, i1, i2;
465   struct term *hyps, *concs, *t;
466 
467   rc = read_buf(fp, buf1);
468   if (rc == 0) {           /* error */
469     *rcp = 0;
470     return(NULL);
471   }
472   if (buf1[0] == '\0') {    /* ok. EOF */
473     *rcp = 1;
474     return(NULL);
475   }
476 
477   /* Kludge - make it into a string readable by regular parser. */
478   /* "a,b,c->d,e,f"  becomes  "$Hyps(a,b,c)->$Concs(d,e,f)" */
479   /* "->d,e,f"  becomes  "$Hyps->$Concs(d,e,f)"  */
480   /* "a,b,c->"  becomes  "$Hyps(a,b,c)->$Concs" */
481   /* "->"  becomes  "$Hyps->$Concs" */
482 
483   i1 = 0;
484   skip_white(buf1, &i1);
485 
486   /* first check for "end_of_list" */
487 
488   if (initial_str("end_of_list", buf1+i1)) {
489     i1 += 11;
490     skip_white(buf1, &i1);
491     if (buf1[i1] == '.') {
492       t = get_term(); t->type = NAME;
493       t->sym_num = str_to_sn("end_of_list", 0);
494       c = get_clause(); l = get_literal(); c->first_lit = l;
495       l->atom = t;
496       *rcp = 1;
497       return(c);
498     }
499   }
500 
501   /* now reset and start again */
502 
503   i1 = 0;
504   skip_white(buf1, &i1);
505 
506   i2 = 0;
507   buf2[i2++] = '$';
508   buf2[i2++] = 'H';
509   buf2[i2++] = 'y';
510   buf2[i2++] = 'p';
511   buf2[i2++] = 's';
512 
513   if (buf1[i1] != '-' || buf1[i1+1] != '>') {
514     /* Hyps not empty */
515     buf2[i2++] = '(';
516     while (buf1[i1] != '-' || buf1[i1+1] != '>') {
517       if (buf1[i1] == '.') {
518 	fprintf(stdout, "\nERROR, arrow not found in sequent:\n");
519 	print_error(stdout, buf1, i1);
520 	*rcp = 0;
521 	return(NULL);
522       }
523       buf2[i2++] = buf1[i1++];
524     }
525     buf2[i2++] = ')';
526   }
527 
528   buf2[i2++] = '-';
529   buf2[i2++] = '>';
530   buf2[i2++] = '$';
531   buf2[i2++] = 'C';
532   buf2[i2++] = 'o';
533   buf2[i2++] = 'n';
534   buf2[i2++] = 'c';
535   buf2[i2++] = 's';
536 
537   i1 += 2;  /* skip over "->" */
538   skip_white(buf1, &i1);
539 
540   if (buf1[i1] != '.') {
541     /* concs not empty */
542     buf2[i2++] = '(';
543     while (buf1[i1] != '.') {
544       buf2[i2++] = buf1[i1++];
545     }
546     buf2[i2++] = ')';
547   }
548   buf2[i2++] = '.';
549   buf2[i2++] = '\0';
550 
551 #if 0
552   printf("before: %s\n", buf1);
553   printf("after:  %s\n", buf2);
554 #endif
555 
556   i2 = 0;
557   t = str_to_term(buf2, &i2, 0);
558   if (!t) {
559     *rcp = 0;
560     return(NULL);
561   }
562   else {
563     skip_white(buf2, &i2);
564     if (buf2[i2] != '.') {
565       fprintf(stdout, "\nERROR, text after term:\n");
566       print_error(stdout, buf2, i2);
567       *rcp = 0;
568       return(NULL);
569     }
570   }
571 
572   t = term_fixup(t);
573 
574   if (!set_vars(t)) {
575     fprintf(stdout, "\nERROR, input clause contains too many variables:\n");
576     print_term(stdout, t); printf(".\n\n");
577     zap_term(t);
578     *rcp = 0;
579     return(NULL);  /* error */
580   }
581   else if (contains_skolem_symbol(t)) {
582     fprintf(stdout, "\nERROR, input clause contains Skolem symbol:\n");
583     print_term(stdout, t); printf(".\n\n");
584     zap_term(t);
585     *rcp = 0;
586     return(NULL);  /* error */
587   }
588 
589   hyps = t->farg->argval;
590   concs = t->farg->narg->argval;
591   free_rel(t->farg->narg);
592   free_rel(t->farg);
593   free_term(t);
594 
595   /* ok, now we have hypotheses and conclusion */
596 
597   c = get_clause();
598 
599   r1 = hyps->farg;
600   prev = NULL;
601   while (r1) {
602     l = get_literal();
603     if (prev)
604       prev->next_lit = l;
605     else
606       c->first_lit = l;
607     prev = l;
608     l->sign = 0;
609     l->atom = r1->argval;
610     r2 = r1;
611     r1 = r1->narg;
612     free_rel(r2);
613   }
614   free_term(hyps);
615 
616   r1 = concs->farg;
617   while (r1) {
618     l = get_literal();
619     if (prev)
620       prev->next_lit = l;
621     else
622       c->first_lit = l;
623     prev = l;
624     l->sign = 1;
625     l->atom = r1->argval;
626     r2 = r1;
627     r1 = r1->narg;
628     free_rel(r2);
629   }
630   free_term(concs);
631 
632   for (l = c->first_lit; l; l = l->next_lit) {
633     l->container = c;
634     l->atom->occ.lit = l;
635     mark_literal(l);
636     if (contains_skolem_symbol(l->atom)) {
637       fprintf(stdout,"\nERROR, input literal contains Skolem symbol:\n");
638       print_term(stdout, l->atom); printf(".\n\n");
639       *rcp = 0;
640       return(NULL);
641     }
642   }
643   *rcp = 1;
644   return(c);
645 
646 }  /* read_sequent_clause */
647 
648 /*************
649  *
650  *    struct clause *read_clause(fp, retcd_ptr)
651  *
652  *    retcd - 0:  error (NULL returned)
653  *            1:  ok    (NULL returned if EOF encountered)
654  *
655  *************/
656 
read_clause(FILE * fp,int * rcp)657 struct clause *read_clause(FILE *fp,
658 			   int *rcp)
659 {
660   struct term *t;
661   struct clause *cl;
662   int rc;
663 
664   if (Flags[INPUT_SEQUENT].val)
665     return(read_sequent_clause(fp, rcp));
666 
667   t = read_term(fp, &rc);
668   if (!rc) {
669     *rcp = 0;
670     return(NULL);  /* error reading term */
671   }
672   else if (!t) {
673     *rcp = 1;
674     return(NULL);  /* EOF */
675   }
676   else if (!set_vars(t)) {
677     fprintf(stdout, "\nERROR, input clause contains too many variables:\n");
678     print_term(stdout, t); printf(".\n\n");
679     zap_term(t);
680     *rcp = 0;
681     return(NULL);  /* error */
682   }
683   else if (contains_skolem_symbol(t)) {
684     fprintf(stdout, "\nERROR, input clause contains Skolem symbol:\n");
685     print_term(stdout, t); printf(".\n\n");
686     zap_term(t);
687     *rcp = 0;
688     return(NULL);  /* error */
689   }
690   else {
691     cl = term_to_clause(t);
692     zap_term(t);
693     if (cl)
694       *rcp = 1;
695     else
696       *rcp = 0;  /* error */
697     return(cl);
698   }
699 
700 }  /* read_clause */
701 
702 /*************
703  *
704  *    struct list *read_cl_list(fp, errors_ptr)
705  *
706  *    Read clauses until EOF or the term `end_of_list' is reached.
707  *
708  *************/
709 
read_cl_list(FILE * fp,int * ep)710 struct list *read_cl_list(FILE *fp,
711 			  int *ep)
712 {
713   struct list *lst;
714   struct clause *cl, *pcl;
715   int rc;
716 
717   Internal_flags[REALLY_CHECK_ARITY] = 1;
718 
719   *ep = 0;
720   lst = get_list();
721   pcl = NULL;
722   cl = read_clause(fp, &rc);
723   while (rc == 0) {  /* while errors */
724     (*ep)++;
725     cl = read_clause(fp, &rc);
726   }
727   while (cl && !(cl->first_lit &&
728 		 is_symbol(cl->first_lit->atom, "end_of_list", 0))) {
729     if (!pcl)
730       lst->first_cl = cl;
731     else
732       pcl->next_cl = cl;
733     cl->prev_cl = pcl;
734     cl->container = lst;
735     pcl = cl;
736     cl = read_clause(fp, &rc);
737     while (rc == 0) {  /* while errors */
738       (*ep)++;
739       cl = read_clause(fp, &rc);
740     }
741   }
742   if (cl)
743     cl_del_non(cl);  /* "end_of_list" term */
744   lst->last_cl = pcl;
745 
746   Internal_flags[REALLY_CHECK_ARITY] = 0;
747   return(lst);
748 }  /* read_cl_list */
749 
750 /*************
751  *
752  *    int set_vars_cl(cl) -- decide which terms are variables
753  *
754  *************/
755 
set_vars_cl(struct clause * cl)756 int set_vars_cl(struct clause *cl)
757 {
758   struct literal *lit;
759   char *varnames[MAX_VARS];
760   int i;
761 
762   for (i=0; i<MAX_VARS; i++)
763     varnames[i] = NULL;
764   lit = cl->first_lit;
765   while (lit) {
766     if (set_vars_term(lit->atom, varnames))
767       lit = lit->next_lit;
768     else
769       return(0);
770   }
771   return(1);
772 }  /* set_vars_cl */
773 
774 /*************
775  *
776  *   print_sequent_clause()
777  *
778  *   Clause number and parents have already been printed.
779  *
780  *************/
781 
print_sequent_clause(FILE * fp,struct clause * c)782 void print_sequent_clause(FILE *fp,
783 			  struct clause *c)
784 {
785   struct literal *l;
786   int first;
787 
788   for (l = c->first_lit, first = 1; l; l = l->next_lit) {
789     if (!l->sign) {
790       if (!first)
791 	fprintf(fp, ", ");
792       print_term(fp, l->atom);
793       first = 0;
794     }
795   }
796   fprintf(fp, " -> ");
797   for (l = c->first_lit, first = 1; l; l = l->next_lit) {
798     if (l->sign) {
799       if (!first)
800 	fprintf(fp, ", ");
801       print_term(fp, l->atom);
802       first = 0;
803     }
804   }
805 
806 }  /* print_sequent_clause */
807 
808 /*************
809  *
810  *    print_justification(fp, clause)
811  *
812  *************/
813 
print_justification(FILE * fp,struct ilist * just)814 void print_justification(FILE *fp,
815 		       struct ilist *just)
816 {
817   struct ilist *ip;
818   fprintf(fp,"[");
819   for (ip = just; ip; ip = ip->next) {
820 
821     if (ip->i <= LIST_RULE) {
822       /* LIST_RULE is a large negative number.
823 	 If ip->i is less than LIST_RULE, then a list
824 	 of length LIST_RULE-ip->i follows.
825       */
826       int i;
827       int j = LIST_RULE - ip->i;
828 
829       for (i = 1; i <= j; i++) {
830 	ip = ip->next;
831 	fprintf(fp, ".%d", ip->i);
832       }
833     }
834     else {
835       if (ip != just)
836 	fprintf(fp, ",");
837 
838       switch (ip->i) {
839       case BINARY_RES_RULE  : fprintf(fp, "binary"); break;
840       case HYPER_RES_RULE   : fprintf(fp, "hyper"); break;
841       case NEG_HYPER_RES_RULE   : fprintf(fp, "neg_hyper"); break;
842       case UR_RES_RULE      : fprintf(fp, "ur"); break;
843       case PARA_INTO_RULE   : fprintf(fp, "para_into"); break;
844       case PARA_FROM_RULE   : fprintf(fp, "para_from"); break;
845       case FACTOR_RULE      : fprintf(fp, "factor"); break;
846       case FACTOR_SIMP_RULE : fprintf(fp, "factor_simp"); break;
847 
848       case NEW_DEMOD_RULE   : fprintf(fp, "new_demod"); break;
849       case BACK_DEMOD_RULE  : fprintf(fp, "back_demod"); break;
850       case DEMOD_RULE       : fprintf(fp, "demod"); break;
851 
852       case UNIT_DEL_RULE    : fprintf(fp, "unit_del"); break;
853 
854       case LINKED_UR_RES_RULE : fprintf(fp, "linked_ur"); break;
855       case EVAL_RULE        : fprintf(fp, "eval"); break;
856       case GEO_ID_RULE      : fprintf(fp, "gL-id"); break;
857       case GEO_RULE         : fprintf(fp, "gL"); break;
858       case COPY_RULE        : fprintf(fp, "copy"); break;
859       case FLIP_EQ_RULE     : fprintf(fp, "flip"); break;
860       case CLAUSIFY_RULE    : fprintf(fp, "clausify"); break;
861       case BACK_UNIT_DEL_RULE : fprintf(fp, "back_unit_del"); break;
862       case SPLIT_RULE       : fprintf(fp, "split"); break;
863       case SPLIT_NEG_RULE   : fprintf(fp, "split_neg"); break;
864       case PROPOSITIONAL_RULE : fprintf(fp, "propositional"); break;
865       default               : fprintf(fp, "%d", ip->i); break;
866       }
867     }
868   }
869   fprintf(fp, "]");
870 }  /* print_justification */
871 
872 /*************
873  *
874  *    print_clause_bare(fp, clause)
875  *
876  *    No ID, justification, period, or newline.
877  *
878  *************/
879 
print_clause_bare(FILE * fp,struct clause * cl)880 void print_clause_bare(FILE *fp,
881 		  struct clause *cl)
882 {
883   struct term *t = clause_to_term(cl);
884   t = term_fixup_2(t);  /* Change -(=(a,b)) to !=(a,b). */
885   print_term(fp, t);
886   zap_term(t);
887 }  /* print_clause_bare */
888 
889 /*************
890  *
891  *    print_clause(fp, clause)
892  *
893  *************/
894 
print_clause(FILE * fp,struct clause * cl)895 void print_clause(FILE *fp,
896 		  struct clause *cl)
897 {
898   struct literal *lit;
899   struct term *t;
900 
901   fprintf(fp, "%d ", cl->id);
902 
903   if (cl->heat_level > 0)
904     fprintf(fp, "(heat=%d) ", (int) (cl->heat_level));
905 
906   if (! multi_justifications()) {
907     print_justification(fp, cl->parents);
908     fprintf(fp, " ");
909   }
910   else {
911     if (cl->multi_parents == NULL)
912       fprintf(fp, " [] ");
913     else {
914       struct g2list *p;
915       for (p = cl->multi_parents; p != NULL; p = p->next) {
916 	if (p == cl->multi_parents)
917 	  fprintf(fp, " ");
918 	else
919 	  fprintf(fp, "\n        ");
920 	print_justification(fp, p->v1);
921       }
922       fprintf(fp, " ");
923     }
924   }
925 
926 
927   if (Flags[PRETTY_PRINT].val) {
928     int parens;
929 
930     fprintf(fp, "\n");
931     lit = cl->first_lit;
932     while (lit) {
933       parens = !lit->sign && sn_to_node(lit->atom->sym_num)->special_op;
934       if (!lit->sign)
935 	fprintf(fp, "-");
936       if (parens)
937 	fprintf(fp, "(");
938       pretty_print_term(fp, lit->atom, 0);
939       if (parens)
940 	fprintf(fp, ")");
941       lit = lit->next_lit;
942       if (lit)
943 	fprintf(fp, " |\n");
944     }
945   }
946   else if (Flags[OUTPUT_SEQUENT].val) {
947     print_sequent_clause(fp, cl);
948   }
949   else {
950 #if 0
951     struct rel *r;
952     lit = cl->first_lit;
953     while (lit) {
954       if (!lit->sign) {
955 	/* This is so that lit gets correctly parenthesized. */
956 	t = get_term();
957 	r = get_rel();
958 	t->farg = r;
959 	t->type = COMPLEX;
960 	r->argval = lit->atom;
961 	t->sym_num = str_to_sn("-", 1);
962 	print_term(fp, t);
963 	free_rel(r);
964 	free_term(t);
965       }
966       else
967 	print_term(fp, lit->atom);
968       lit = lit->next_lit;
969       if (lit)
970 	fprintf(fp, " | ");
971     }
972 #else
973     t = clause_to_term(cl);
974     t = term_fixup_2(t);  /* Change -(=(a,b)) to !=(a,b). */
975     print_term(fp, t);
976     zap_term(t);
977 #endif
978   }
979   if (cl->attributes)
980     print_attributes(fp, cl->attributes);
981 
982   fprintf(fp, ".\n");
983 }  /* print_clause */
984 
985 /*************
986  *
987  *    print_clause_without_just(fp, clause)
988  *
989  *************/
990 
print_clause_without_just(FILE * fp,struct clause * cl)991 void print_clause_without_just(FILE *fp,
992 		  struct clause *cl)
993 {
994   struct literal *lit;
995   struct term *t;
996 
997   fprintf(fp, "%d ", cl->id);
998 
999   if (cl->heat_level > 0)
1000     fprintf(fp, "(heat=%d) ", (int) (cl->heat_level));
1001 
1002   if (Flags[PRETTY_PRINT].val) {
1003     int parens;
1004 
1005     fprintf(fp, "\n");
1006     lit = cl->first_lit;
1007     while (lit) {
1008       parens = !lit->sign && sn_to_node(lit->atom->sym_num)->special_op;
1009       if (!lit->sign)
1010 	fprintf(fp, "-");
1011       if (parens)
1012 	fprintf(fp, "(");
1013       pretty_print_term(fp, lit->atom, 0);
1014       if (parens)
1015 	fprintf(fp, ")");
1016       lit = lit->next_lit;
1017       if (lit)
1018 	fprintf(fp, " |\n");
1019     }
1020   }
1021   else if (Flags[OUTPUT_SEQUENT].val) {
1022     print_sequent_clause(fp, cl);
1023   }
1024   else {
1025 #if 0
1026     struct rel *r;
1027     lit = cl->first_lit;
1028     while (lit) {
1029       if (!lit->sign) {
1030 	/* This is so that lit gets correctly parenthesized. */
1031 	t = get_term();
1032 	r = get_rel();
1033 	t->farg = r;
1034 	t->type = COMPLEX;
1035 	r->argval = lit->atom;
1036 	t->sym_num = str_to_sn("-", 1);
1037 	print_term(fp, t);
1038 	free_rel(r);
1039 	free_term(t);
1040       }
1041       else
1042 	print_term(fp, lit->atom);
1043       lit = lit->next_lit;
1044       if (lit)
1045 	fprintf(fp, " | ");
1046     }
1047 #else
1048     t = clause_to_term(cl);
1049     t = term_fixup_2(t);  /* Change -(=(a,b)) to !=(a,b). */
1050     print_term(fp, t);
1051     zap_term(t);
1052 #endif
1053   }
1054   if (cl->attributes)
1055     print_attributes(fp, cl->attributes);
1056 
1057   fprintf(fp, ".\n");
1058 }  /* print_clause_without_just */
1059 
1060 /*************
1061  *
1062  *    p_clause(clause)
1063  *
1064  *************/
1065 
p_clause(struct clause * cl)1066 void p_clause(struct clause *cl)
1067 {
1068   print_clause(stdout, cl);
1069 }  /* p_clause */
1070 
1071 /*************
1072  *
1073  *    print_cl_list(fp, lst)
1074  *
1075  *************/
1076 
print_cl_list(FILE * fp,struct list * lst)1077 void print_cl_list(FILE *fp,
1078 		   struct list *lst)
1079 {
1080   struct clause *cl;
1081 
1082   if (!lst)
1083     fprintf(fp, "(list nil)\n");
1084   else {
1085     cl = lst->first_cl;
1086     while (cl) {
1087       print_clause(fp, cl);
1088       cl = cl->next_cl;
1089     }
1090     fprintf(fp, "end_of_list.\n");
1091   }
1092 }  /* print_cl_list */
1093 
1094 /*************
1095  *
1096  *    cl_merge(cl) -- merge identical literals (keep leftmost occurrence)
1097  *
1098  *************/
1099 
cl_merge(struct clause * c)1100 void cl_merge(struct clause *c)
1101 {
1102   struct literal *l1, *l2, *l_prev;
1103 
1104   l1 = c->first_lit;
1105   while (l1) {
1106     l2 = l1->next_lit;
1107     l_prev = l1;
1108     while (l2)
1109       if (l1->sign == l2->sign && term_ident(l1->atom, l2->atom)) {
1110 	l_prev->next_lit = l2->next_lit;
1111 	l2->atom->occ.lit = NULL;
1112 	zap_term(l2->atom);
1113 	free_literal(l2);
1114 	l2 = l_prev->next_lit;
1115       }
1116       else {
1117 	l_prev = l2;
1118 	l2 = l2->next_lit;
1119       }
1120     l1 = l1->next_lit;
1121   }
1122 }  /* cl_merge */
1123 
1124 /*************0
1125  *
1126  *     int tautology(c) -- Is clause c a tautology?
1127  *
1128  *************/
1129 
tautology(struct clause * c)1130 int tautology(struct clause *c)
1131 {
1132   struct literal *l1, *l2;
1133   int taut;
1134 
1135   taut = 0;
1136   l1 = c->first_lit;
1137   while (l1 && !taut) {
1138     l2 = l1->next_lit;
1139     while (l2 && !taut) {
1140       taut = (l1->sign != l2->sign && term_ident(l1->atom, l2->atom));
1141       l2 = l2->next_lit;
1142     }
1143     l1 = l1->next_lit;
1144   }
1145   return(taut);
1146 }  /* tautology */
1147 
1148 /*************
1149  *
1150  *   prf_weight()
1151  *
1152  *   Return the number of leaves (i.e., occurrences of input clauses)
1153  *   in the proof tree.
1154  *
1155  *************/
1156 
prf_weight(struct clause * c)1157 int prf_weight(struct clause *c)
1158 {
1159   struct ilist *ip;
1160   struct clause *d;
1161   int sum = 0;
1162 
1163   for (ip = c->parents; ip; ip = ip->next) {
1164     if (ip->i <= LIST_RULE) {
1165       /* LIST_RULE is a large negative number. */
1166       /* If ip->i is less than LIST_RULE, then a list follows. */
1167       int i;
1168       int j = LIST_RULE - ip->i;  /* size of list */
1169       /* Make ip point at the last element of the list. */
1170       for (i = 1; i <= j; i++)
1171 	ip = ip->next;
1172     }
1173     else if (ip->i >= 0) {
1174       d = cl_find(ip->i);
1175       if (d)
1176 	sum += prf_weight(d);
1177     }
1178   }
1179   return(sum == 0 ? 1 : sum);
1180 }  /* prf_weight */
1181 
1182 /*************
1183  *
1184  *    int proof_length(c)
1185  *
1186  *    Return length of proof.  If demod_history is clear, demodulation
1187  *    steps are not counted.  "new_demod" steps are not counted.
1188  *
1189  *************/
1190 
proof_length(struct clause * c)1191 int proof_length(struct clause *c)
1192 {
1193   struct clause_ptr *cp1, *cp2;
1194   struct ilist *ip1, *ip2;
1195   int count, level;
1196 
1197   cp1 = NULL; ip1 = NULL;
1198   level = get_ancestors(c, &cp1, &ip1);
1199 
1200   for (count = 0; cp1; ) {
1201     if (cp1->c->parents && cp1->c->parents->i != NEW_DEMOD_RULE)
1202       count++;
1203     cp2 = cp1; cp1 = cp1->next; free_clause_ptr(cp2);
1204     ip2 = ip1; ip1 = ip1->next; free_ilist(ip2);
1205   }
1206   return(count);
1207 }  /* proof_length */
1208 
1209 /*************
1210  *
1211  *    int subsume(c, d) -- does clause c subsume clause d?
1212  *
1213  *************/
1214 
subsume(struct clause * c,struct clause * d)1215 int subsume(struct clause *c,
1216 	    struct clause *d)
1217 {
1218   struct context *s;
1219   struct trail *tr;
1220   int subsumed;
1221 
1222   s = get_context();
1223   tr = NULL;
1224   subsumed = map_rest(c, d, s, &tr);
1225   if (subsumed)
1226     clear_subst_1(tr);
1227   free_context(s);
1228   return(subsumed);
1229 }  /* subsume */
1230 
1231 /*************
1232  *
1233  *    int map_rest(c, d, s, trp) - map rest of literals - for subsumption
1234  *
1235  *************/
1236 
map_rest(struct clause * c,struct clause * d,struct context * s,struct trail ** trp)1237 int map_rest(struct clause *c,
1238 	     struct clause *d,
1239 	     struct context *s,
1240 	     struct trail **trp)
1241 {
1242   struct literal *c_lit, *d_lit;
1243   struct term *c_atom, *d_atom;
1244   struct trail *t_pos;
1245   int subsumed, i;
1246 
1247   /* get the first unmarked literal */
1248   c_lit = c->first_lit;
1249   i = 0;
1250   while (c_lit && Map_array[i] == 1) {
1251     c_lit = c_lit->next_lit;
1252     i++;
1253   }
1254 
1255   if (!c_lit)
1256     return(1);  /* all lits of c mapped, so c subsumes d */
1257   else if (answer_lit(c_lit)) {   /* if answer literal, skip it */
1258     c_atom = c_lit->atom;
1259     Map_array[i] = 1;      /* mark as mapped */
1260     subsumed = map_rest(c, d, s, trp);
1261     Map_array[i] = 0;      /* remove mark */
1262     return(subsumed);
1263   }
1264   else {
1265     c_atom = c_lit->atom;
1266     Map_array[i] = 1;      /* mark as mapped */
1267     d_lit = d->first_lit;
1268     subsumed = 0;
1269     while (d_lit && !subsumed) {
1270       d_atom = d_lit->atom;
1271       t_pos = *trp;  /* save position in trail in case of failure */
1272       if (c_lit->sign == d_lit->sign && match(c_atom, s, d_atom, trp)) {
1273 	if (map_rest(c, d, s, trp))
1274 	  subsumed = 1;
1275 	else {
1276 	  clear_subst_2(*trp, t_pos);
1277 	  *trp = t_pos;
1278 	}
1279       }
1280       d_lit = d_lit->next_lit;
1281     }
1282     Map_array[i] = 0;      /* remove mark */
1283     return(subsumed);
1284   }
1285 }  /* map_rest */
1286 
1287 /*************
1288  *
1289  *    int anc_subsume(c, d)
1290  *
1291  *    We already know that c subsumes d.  Check if d subsumes c and
1292  *    ancestors(c) <= ancestors(d).
1293  *
1294  *************/
1295 
anc_subsume(struct clause * c,struct clause * d)1296 int anc_subsume(struct clause *c,
1297 		struct clause *d)
1298 {
1299   if (subsume(d,c)) {
1300     if (Flags[PROOF_WEIGHT].val)
1301       return(prf_weight(c) <= prf_weight(d));
1302     else
1303       return(proof_length(c) <= proof_length(d));
1304   }
1305   else
1306     return(1);
1307 }  /* anc_subsume */
1308 
1309 /*************
1310  *
1311  *    struct clause *for_sub_prop(d)
1312  *
1313  *    Attempt to find a clause that propositionally subsumes d.
1314  *
1315  *************/
1316 
for_sub_prop(struct clause * d)1317 struct clause *for_sub_prop(struct clause *d)
1318 {
1319   struct clause *c;
1320 
1321   for (c = Usable->first_cl; c; c = c->next_cl)
1322     if (ordered_sub_clause(c, d))
1323       return(c);
1324 
1325   for (c = Sos->first_cl; c; c = c->next_cl)
1326     if (ordered_sub_clause(c, d))
1327       return(c);
1328 
1329   return(NULL);
1330 
1331 }  /* for_sub_prop */
1332 
1333 /*************
1334  *
1335  *    struct clause *forward_subsume(d)
1336  *
1337  *    Attempt to find a clause that subsumes d.
1338  *
1339  *************/
1340 
forward_subsume(struct clause * d)1341 struct clause *forward_subsume(struct clause *d)
1342 {
1343   int subsumed;
1344   struct literal *d_lit;
1345   struct clause *c = NULL;
1346   struct term *c_atom, *d_atom;
1347   struct context *s;
1348   struct trail *tr;
1349   struct is_tree *is_db;
1350   struct fsub_pos *pos;
1351   struct fpa_index *fpa_db;
1352   struct fpa_tree *ut;
1353   int c_size, factor, i;
1354   int d_size = -1;
1355   struct literal *lit;
1356 
1357   if (Flags[PROPOSITIONAL].val)
1358     return(for_sub_prop(d));
1359 
1360   subsumed = 0;
1361   s = get_context();
1362 
1363   factor = Flags[FACTOR].val;
1364   if (factor)  /* if factor don't let long clauses subsume short */
1365     d_size = num_literals(d);
1366 
1367   if (!Flags[FOR_SUB_FPA].val) {  /* if `is' indexing */
1368 
1369     d_lit = d->first_lit;
1370 
1371     while (d_lit && !subsumed) {
1372       /* Is_pos_lits and Is_neg_lits are global variables */
1373       is_db = d_lit->sign ? Is_pos_lits : Is_neg_lits;
1374       c_atom = fs_retrieve(d_lit->atom, s, is_db, &pos);
1375       while (c_atom && !subsumed) {
1376 	c = c_atom->occ.lit->container;
1377 	c_size = num_literals(c);
1378 	if (c_size > MAX_LITS) {
1379 	  abend("forward_subsume, MAX_LITS too small.");
1380 	}
1381 
1382 	if (literal_number(c_atom->occ.lit) == 1 && (!factor || c_size <= d_size)) {
1383 	  for (i = 0, lit = c->first_lit;
1384 	       lit->atom != c_atom;
1385 	       i++, lit = lit->next_lit);  /* empty body */
1386 	  Map_array[i] = 1;      /* mark as mapped*/
1387 	  tr = NULL;
1388 	  subsumed = map_rest(c, d, s, &tr);
1389 	  Map_array[i] = 0;      /* remove mark */
1390 	  clear_subst_1(tr);
1391 	}
1392 
1393 	if (subsumed && multi_justifications()) {
1394 	  if (subsume(d,c)) {
1395 	    /* c (old) and d (new) are equivalent, so add d's just to c. */
1396 	    if (d->parents != NULL)
1397 	      possibly_append_parents(c, d->parents);
1398 	  }
1399 	}
1400 
1401 	if (subsumed && Flags[ANCESTOR_SUBSUME].val) {
1402 	  /* Removed variable renumbering 4/4/2001; shouldn't be necessary */
1403 	  subsumed = anc_subsume(c,d);
1404 	  if (!subsumed)
1405 	    Stats[CL_NOT_ANC_SUBSUMED]++;
1406 	}
1407 
1408 	/* BV(970327) : forward sub only if sub goes both ways */
1409 	if (subsumed && Flags[FOR_SUB_EQUIVALENTS_ONLY].val)
1410 	  subsumed = subsume(d,c);
1411 
1412 	if (!subsumed)
1413 	  c_atom = fs_retrieve((struct term *) NULL, s, is_db, &pos);
1414 	else
1415 	  canc_fs_pos(pos, s);
1416       }
1417       d_lit = d_lit->next_lit;
1418     }
1419   }
1420   else {  /* fpa indexing */
1421 
1422     d_lit = d->first_lit;
1423     while (d_lit && !subsumed) {
1424       fpa_db = (d_lit->sign ? Fpa_pos_lits : Fpa_neg_lits);
1425       d_atom = d_lit->atom;
1426       ut = build_tree(d_atom, MORE_GEN, Parms[FPA_LITERALS].val, fpa_db);
1427       c_atom = next_term(ut, 0);
1428       while (c_atom && !subsumed) {
1429 	tr = NULL;
1430 	c = c_atom->occ.lit->container;
1431 	c_size = num_literals(c);
1432 	if (c_size > MAX_LITS) {
1433 	  abend("forward_subsume, MAX_LITS too small.");
1434 	}
1435 	if (literal_number(c_atom->occ.lit) == 1 &&
1436 	    (!factor || c_size <= d_size) &&
1437 	    match(c_atom, s, d_atom, &tr)) {
1438 
1439 	  for (i = 0, lit = c->first_lit;
1440 	       lit->atom != c_atom;
1441 	       i++, lit = lit->next_lit);  /* empty body */
1442 	  Map_array[i] = 1;      /* mark as mapped*/
1443 	  subsumed = map_rest(c, d, s, &tr);
1444 	  Map_array[i] = 0;      /* remove mark */
1445 	  clear_subst_1(tr);
1446 	}
1447 
1448 	if (subsumed && Flags[ANCESTOR_SUBSUME].val) {
1449 	  /* Removed variable renumbering 4/4/2001; shouldn't be necessary */
1450 	  subsumed = anc_subsume(c,d);
1451 	  if (!subsumed)
1452 	    Stats[CL_NOT_ANC_SUBSUMED]++;
1453 	}
1454 
1455 	/* BV(970327) : forward sub only if sub goes both ways */
1456 	if (subsumed && Flags[FOR_SUB_EQUIVALENTS_ONLY].val)
1457 	  subsumed = subsume(d,c);
1458 
1459 	if (!subsumed)
1460 	  c_atom = next_term(ut, 0);
1461 	else
1462 	  zap_prop_tree(ut);
1463       }
1464       d_lit = d_lit->next_lit;
1465     }
1466   }
1467   free_context(s);
1468   if (subsumed)
1469     return(c);
1470   else
1471     return(NULL);
1472 }  /* forward_subsume */
1473 
1474 /*************
1475  *
1476  *    struct clause_ptr *back_subsume(c)
1477  *
1478  *    Get the list of clauses subsumed by c.
1479  *
1480  *************/
1481 
back_subsume(struct clause * c)1482 struct clause_ptr *back_subsume(struct clause *c)
1483 {
1484   int subsumed, c_size, factor, i;
1485   struct literal *c_lit;
1486   struct clause *d;
1487   struct clause_ptr *subsumed_clauses;
1488   struct term *c_atom, *d_atom;
1489   struct context *s;
1490   struct fpa_tree *ut;
1491   struct trail *tr;
1492 
1493   factor = Flags[FACTOR].val;
1494 
1495   c_size = num_literals(c);
1496 
1497   if (c_size > MAX_LITS) {
1498     abend("back_subsume, MAX_LITS too small.");
1499   }
1500 
1501   s = get_context();
1502   c_lit = c->first_lit;
1503   /* get first non-answer literal */
1504   i = 0;
1505   while (c_lit && answer_lit(c_lit)) {
1506     c_lit = c_lit->next_lit;
1507     i++;
1508   }
1509 
1510   if (!c_lit) {
1511     fprintf(stdout, "\nNOTE: back_subsume called with empty clause.\n");
1512     return(NULL);
1513   }
1514 
1515   c_atom = c_lit->atom;
1516   ut = build_tree(c_atom, INSTANCE, Parms[FPA_LITERALS].val,
1517 		  c_lit->sign ? Fpa_pos_lits : Fpa_neg_lits);
1518   /* Fpa_pos_lits and Fpa_neg_lits are global variables */
1519 
1520   subsumed_clauses = NULL;
1521   d_atom = next_term(ut, 0);
1522   while (d_atom) {
1523     d = d_atom->occ.lit->container;
1524     tr = NULL;
1525     if (c != d && (!factor || c_size <= num_literals(d))
1526 	&& match(c_atom, s, d_atom, &tr)) {
1527       Map_array[i] = 1;  /* mark as mapped */
1528       subsumed = map_rest(c, d, s, &tr);
1529       Map_array[i] = 0;    /* remove mark */
1530       clear_subst_1(tr);
1531       if (subsumed && Flags[ANCESTOR_SUBSUME].val)
1532 	subsumed = anc_subsume(c, d);
1533       if (subsumed)
1534       insert_clause(d, &subsumed_clauses);
1535     }
1536     d_atom = next_term(ut, 0);
1537   }
1538   free_context(s);
1539   return(subsumed_clauses);
1540 }  /* back_subsume */
1541 
1542 /*************
1543  *
1544  *    struct clause_ptr *unit_conflict(c)
1545  *
1546  *    Search for unit conflict.  Return empty clause if found,
1547  *    return NULL if not found.
1548  *
1549  *    IT IS ASSUMED THAT c IS A UNIT CLAUSE!!
1550  *
1551  *************/
1552 
unit_conflict(struct clause * c)1553 struct clause_ptr *unit_conflict(struct clause *c)
1554 {
1555   struct clause *d, *e;
1556   struct fpa_tree *ut;
1557   struct term *f_atom;
1558   struct literal *lit;
1559   int go, mp, ec;
1560   struct context *c1, *c2;
1561   struct trail *tr;
1562   struct clause_ptr *cp_return, *cp_prev, *cp_curr;
1563 
1564   c1 = get_context();
1565   c1->multiplier = 0;
1566   c2 = get_context();
1567   c2->multiplier = 1;
1568   lit = c->first_lit;
1569   while (answer_lit(lit))  /* skip answer literals */
1570     lit = lit->next_lit;
1571   ut = build_tree(lit->atom, UNIFY, Parms[FPA_LITERALS].val,
1572 		  lit->sign ? Fpa_neg_lits : Fpa_pos_lits);
1573   f_atom = next_term(ut, 0);
1574   go = 1;
1575   cp_return = cp_prev = NULL;
1576   while (go && f_atom) {
1577     tr = NULL;
1578     d = f_atom->occ.lit->container;
1579     if (num_literals(d) == 1 && unify(lit->atom, c1, f_atom, c2, &tr)) {
1580 
1581       e = build_bin_res(lit->atom, c1, f_atom, c2);
1582       clear_subst_1(tr);
1583       cl_merge(e);  /* answer literals */
1584       cp_curr = get_clause_ptr();
1585       cp_curr->c = e;
1586       if (cp_prev)
1587 	cp_prev->next = cp_curr;
1588       else
1589 	cp_return = cp_curr;
1590       cp_prev = cp_curr;
1591 
1592       ec = ++Stats[EMPTY_CLAUSES];
1593       mp = Parms[MAX_PROOFS].val;
1594 
1595       if (mp != -1 && ec >= mp)
1596 	/* do not look for more proofs */
1597 	go = 0;
1598     }
1599     if (go)
1600       f_atom = next_term(ut, 0);
1601     else
1602       zap_prop_tree(ut);
1603   }
1604   free_context(c1);
1605   free_context(c2);
1606   return(cp_return);
1607 }  /* unit_conflict */
1608 
1609 /*************
1610  *
1611  *    int propositional_clause(c)
1612  *
1613  *    Is this a propositional clause?
1614  *
1615  *************/
1616 
propositional_clause(struct clause * c)1617 int propositional_clause(struct clause *c)
1618 {
1619   struct literal *lit;
1620 
1621   lit = c->first_lit;
1622   while (lit)
1623     if (lit->atom->type != NAME)
1624       return(0);
1625     else
1626       lit = lit->next_lit;
1627   return(1);
1628 }  /* propositional_clause */
1629 
1630 /*************
1631  *
1632  *    int xx_resolvable(c)
1633  *
1634  *    Does the nonunit clause c have a literal that can resolve with x = x?
1635  *
1636  *************/
1637 
xx_resolvable(struct clause * c)1638 int xx_resolvable(struct clause *c)
1639 {
1640   if (unit_clause(c))
1641     return(0);
1642   else {
1643     struct literal *lit;
1644 
1645     lit = c->first_lit;
1646     while (lit) {
1647       if (!lit->sign && is_eq(lit->atom->sym_num)) {
1648 	struct term *a1 = lit->atom->farg->argval;
1649 	struct term *a2 = lit->atom->farg->narg->argval;
1650 	if (a1->type == VARIABLE && !occurs_in(a1, a2))
1651 	  return(1);
1652 	else if (a2->type == VARIABLE && !occurs_in(a2, a1))
1653 	  return(1);
1654       }
1655       lit = lit->next_lit;
1656     }
1657     return(0);
1658   }
1659 }  /* xx_resolvable */
1660 
1661 /*************
1662  *
1663  *    int pos_clause(c)
1664  *
1665  *    Is this a positive clause (excluding answer lits) ?
1666  *
1667  *************/
1668 
pos_clause(struct clause * c)1669 int pos_clause(struct clause *c)
1670 {
1671   struct literal *lit;
1672 
1673   lit = c->first_lit;
1674   while (lit)
1675     if (!lit->sign && !answer_lit(lit))
1676       return(0);  /* fail because found negative non-anser literal */
1677     else
1678       lit = lit->next_lit;
1679   return(1);
1680 }  /* pos_clause */
1681 
1682 /*************
1683  *
1684  *    int answer_lit(lit)  --  Is this an answer literal?
1685  *
1686  *************/
1687 
answer_lit(struct literal * lit)1688 int answer_lit(struct literal *lit)
1689 {
1690   return(lit->atom->varnum == ANSWER);
1691 }  /* answer_lit */
1692 
1693 /*************
1694  *
1695  *    int pos_eq_lit(lit)  --  Is this a positive equality literal?
1696  *
1697  *************/
1698 
pos_eq_lit(struct literal * lit)1699 int pos_eq_lit(struct literal *lit)
1700 {
1701   return(lit->atom->varnum == POS_EQ);
1702 }  /* pos_eq_lit */
1703 
1704 /*************
1705  *
1706  *    int neg_eq_lit(lit)  --  Is this a negative equality literal?
1707  *
1708  *************/
1709 
neg_eq_lit(struct literal * lit)1710 int neg_eq_lit(struct literal *lit)
1711 {
1712   return(lit->atom->varnum == NEG_EQ);
1713 }  /* neg_eq_lit */
1714 
1715 /*************
1716  *
1717  *    int eq_lit(lit)  --  Is this an equality literal (pos or neg)?
1718  *
1719  *************/
1720 
eq_lit(struct literal * lit)1721 int eq_lit(struct literal *lit)
1722 {
1723   return(pos_eq_lit(lit) || neg_eq_lit(lit));
1724 }  /* eq_lit */
1725 
1726 /*************
1727  *
1728  *    int neg_clause(c)
1729  *
1730  *    Is this a negative clause (excluding answer lits) ?
1731  *
1732  *************/
1733 
neg_clause(struct clause * c)1734 int neg_clause(struct clause *c)
1735 {
1736   struct literal *lit;
1737 
1738   lit = c->first_lit;
1739   while (lit)
1740     if (lit->sign && !answer_lit(lit))
1741       return(0);  /* fail because found positive non-answer literal */
1742     else
1743       lit = lit->next_lit;
1744   return(1);
1745 }  /* neg_clause */
1746 
1747 /*************
1748  *
1749  *    int num_literals(c)  --  How many literals (excluding answer literals)?
1750  *
1751  *************/
1752 
num_literals(struct clause * c)1753 int num_literals(struct clause *c)
1754 {
1755   int i;
1756   struct literal *lit;
1757 
1758   i = 0;
1759   lit = c->first_lit;
1760   while (lit) {
1761     if (!answer_lit(lit))  /* if not answer literal */
1762       i++;
1763     lit = lit->next_lit;
1764   }
1765   return(i);
1766 }  /* num_literals */
1767 
1768 /*************
1769  *
1770  *    int num_answers(c)  --  How many answer literals?
1771  *
1772  *************/
1773 
num_answers(struct clause * c)1774 int num_answers(struct clause *c)
1775 {
1776   int i;
1777   struct literal *lit;
1778 
1779   i = 0;
1780   lit = c->first_lit;
1781   while (lit) {
1782     if (answer_lit(lit))
1783       i++;
1784     lit = lit->next_lit;
1785   }
1786   return(i);
1787 }  /* num_answers */
1788 
1789 /*************
1790  *
1791  *    int num_literals_including_answers(c)  --  How many literals?
1792  *
1793  *************/
1794 
num_literals_including_answers(struct clause * c)1795 int num_literals_including_answers(struct clause *c)
1796 {
1797   int i;
1798   struct literal *lit;
1799 
1800   for (i = 0, lit = c->first_lit; lit; i++, lit = lit->next_lit);
1801   return(i);
1802 }  /* num_literals_including_answers */
1803 
1804 /*************
1805  *
1806  *    int literal_number(lit)
1807  *
1808  *    lit is which literal (excluding answers) in the clause that contains it.
1809  *
1810  *************/
1811 
literal_number(struct literal * lit)1812 int literal_number(struct literal *lit)
1813 {
1814   int i;
1815   struct literal *l;
1816 
1817   i = 1;
1818   l = lit->container->first_lit;
1819   while (l != lit) {
1820     if (!answer_lit(l))
1821       i++;
1822     l = l->next_lit;
1823   }
1824   return(i);
1825 }  /* literal_number */
1826 
1827 /*************
1828  *
1829  *    int unit_clause(c)  -- Is it a unit clause (don't count answers)?
1830  *
1831  *************/
1832 
unit_clause(struct clause * c)1833 int unit_clause(struct clause *c)
1834 {
1835   return(num_literals(c) == 1);
1836 }  /* unit_clause */
1837 
1838 /*************
1839  *
1840  *    int horn_clause(c)
1841  *
1842  *    Is c a Horn clause (at most one positive literal)?
1843  *
1844  *    Ignore answer literals.
1845  *
1846  *************/
1847 
horn_clause(struct clause * c)1848 int horn_clause(struct clause *c)
1849 {
1850   struct literal *lit;
1851   int i;
1852 
1853   for (lit = c->first_lit, i = 0; lit; lit = lit->next_lit)
1854     if (lit->sign && !answer_lit(lit))
1855       i++;
1856   return(i <= 1);
1857 }  /* horn_clause */
1858 
1859 /*************
1860  *
1861  *    int equality_clause(c)
1862  *
1863  *    Does c contain any equality literals (pos or neg)?
1864  *
1865  *************/
1866 
equality_clause(struct clause * c)1867 int equality_clause(struct clause *c)
1868 {
1869   struct literal *lit;
1870 
1871   for (lit = c->first_lit; lit; lit = lit->next_lit)
1872     if (pos_eq_lit(lit) || neg_eq_lit(lit))
1873       return(1);
1874   return(0);
1875 }  /* equality_clause */
1876 
1877 /*************
1878  *
1879  *    int symmetry_clause(c)
1880  *
1881  *    Is c a clause for symmetry of equality?
1882  *
1883  *************/
1884 
symmetry_clause(struct clause * c)1885 int symmetry_clause(struct clause *c)
1886 {
1887   struct literal *l1, *l2;
1888 
1889   if (num_literals(c) != 2)
1890     return(0);
1891   else {
1892     l1 = c->first_lit; l2 = l1->next_lit;
1893     if (l1->sign == l2->sign)
1894       return(0);
1895     else if (!eq_lit(l1) || l1->atom->sym_num != l2->atom->sym_num)
1896       return(0);
1897     else if (l1->atom->farg->argval->type != VARIABLE)
1898       return(0);
1899     else if (l2->atom->farg->argval->type != VARIABLE)
1900       return(0);
1901     else if (!term_ident(l1->atom->farg->argval,
1902 			 l2->atom->farg->narg->argval))
1903       return(0);
1904     else if (!term_ident(l1->atom->farg->narg->argval,
1905 			 l2->atom->farg->argval))
1906       return(0);
1907     else
1908       return(1);
1909   }
1910 }  /* symmetry_clause */
1911 
1912 /*************
1913  *
1914  *   struct literal *ith_literal(c, n)
1915  *
1916  *   Return the i-th (non-answer) literal.
1917  *
1918  *************/
1919 
ith_literal(struct clause * c,int n)1920 struct literal *ith_literal(struct clause *c,
1921 			    int n)
1922 {
1923   int i;
1924   struct literal *lit;
1925 
1926   lit = c->first_lit;
1927   i = 0;
1928   while(lit) {
1929     if (!answer_lit(lit))
1930       i++;
1931     if (i == n)
1932       return(lit);
1933     else
1934       lit = lit->next_lit;
1935   }
1936   return(lit);
1937 }  /* ith_literal */
1938 
1939 /*************
1940  *
1941  *    append_cl(lst, cl)
1942  *
1943  *************/
1944 
append_cl(struct list * l,struct clause * c)1945 void append_cl(struct list *l,
1946 	       struct clause *c)
1947 {
1948   c->next_cl = NULL;
1949   c->prev_cl = l->last_cl;
1950 
1951   if (!l->first_cl)
1952     l->first_cl = c;
1953   else
1954     l->last_cl->next_cl = c;
1955   l->last_cl = c;
1956   c->container = l;
1957 
1958   if (l == Usable)
1959     Stats[USABLE_SIZE]++;
1960   else if (l == Sos)
1961     Stats[SOS_SIZE]++;
1962   else if (l == Demodulators)
1963     Stats[DEMODULATORS_SIZE]++;
1964 
1965 }  /* append_cl */
1966 
1967 /*************
1968  *
1969  *    prepend_cl(lst, cl)
1970  *
1971  *************/
1972 
prepend_cl(struct list * l,struct clause * c)1973 void prepend_cl(struct list *l,
1974 		struct clause *c)
1975 {
1976   c->prev_cl = NULL;
1977   c->next_cl = l->first_cl;
1978   if (!l->last_cl)
1979     l->last_cl = c;
1980   else
1981     l->first_cl->prev_cl = c;
1982   l->first_cl = c;
1983   c->container = l;
1984 
1985   if (l == Usable)
1986     Stats[USABLE_SIZE]++;
1987   else if (l == Sos)
1988     Stats[SOS_SIZE]++;
1989   else if (l == Demodulators)
1990     Stats[DEMODULATORS_SIZE]++;
1991 }  /* prepend_cl */
1992 
1993 /*************
1994  *
1995  *    insert_before_cl(c, c_new)
1996  *
1997  *************/
1998 
insert_before_cl(struct clause * c,struct clause * c_new)1999 void insert_before_cl(struct clause *c,
2000 		      struct clause *c_new)
2001 {
2002   struct list *l;
2003 
2004   l = c->container;
2005 
2006   c_new->next_cl = c;
2007   c_new->prev_cl = c->prev_cl;
2008   c->prev_cl = c_new;
2009   if (!c_new->prev_cl)
2010     l->first_cl = c_new;
2011   else
2012     c_new->prev_cl->next_cl = c_new;
2013 
2014   c_new->container = l;
2015 
2016   if (l == Usable)
2017     Stats[USABLE_SIZE]++;
2018   else if (l == Sos)
2019     Stats[SOS_SIZE]++;
2020   else if (l == Demodulators)
2021     Stats[DEMODULATORS_SIZE]++;
2022 
2023 }  /* insert_before_cl */
2024 
2025 /*************
2026  *
2027  *    insert_after_cl(c, c_new)
2028  *
2029  *************/
2030 
insert_after_cl(struct clause * c,struct clause * c_new)2031 void insert_after_cl(struct clause *c,
2032 		     struct clause *c_new)
2033 {
2034   struct list *l;
2035 
2036   l = c->container;
2037 
2038   c_new->prev_cl = c;
2039   c_new->next_cl = c->next_cl;
2040   c->next_cl = c_new;
2041   if (!c_new->next_cl)
2042     l->last_cl = c_new;
2043   else
2044     c_new->next_cl->prev_cl = c_new;
2045 
2046   c_new->container = l;
2047 
2048   if (l == Usable)
2049     Stats[USABLE_SIZE]++;
2050   else if (l == Sos)
2051     Stats[SOS_SIZE]++;
2052   else if (l == Demodulators)
2053     Stats[DEMODULATORS_SIZE]++;
2054 
2055 }  /* insert_after_cl */
2056 
2057 /*************
2058  *
2059  *    rem_from_list(c)
2060  *
2061  *************/
2062 
rem_from_list(struct clause * c)2063 void rem_from_list(struct clause *c)
2064 {
2065   struct clause *p, *n;
2066 
2067   p = c->prev_cl;
2068   n = c->next_cl;
2069   if (!n)
2070     c->container->last_cl = p;
2071   else
2072     n->prev_cl = p;
2073   if (!p)
2074     c->container->first_cl = n;
2075   else
2076     p->next_cl = n;
2077 
2078   if (c->container == Usable)
2079     Stats[USABLE_SIZE]--;
2080   else if (c->container == Sos)
2081     Stats[SOS_SIZE]--;
2082   else if (c->container == Demodulators)
2083     Stats[DEMODULATORS_SIZE]--;
2084 
2085   c->container = NULL;
2086   c->prev_cl = NULL;
2087   c->next_cl = NULL;
2088 }  /* rem_from_list */
2089 
2090 /*************
2091  *
2092  *    insert_clause(clause, *clause_ptr)
2093  *
2094  *    If not already there, insert clause into list of clause pointers.
2095  *
2096  *************/
2097 
insert_clause(struct clause * c,struct clause_ptr ** cpp)2098 void insert_clause(struct clause *c,
2099 		   struct clause_ptr **cpp)
2100 {
2101   struct clause_ptr *curr, *prev, *new;
2102 
2103   curr = *cpp;
2104   prev = NULL;
2105   while (curr && curr->c->id > c->id) {
2106     prev = curr;
2107     curr = curr->next;
2108   }
2109   if (!curr || curr->c->id != c->id) {
2110     new = get_clause_ptr();
2111     new->c = c;
2112     new->next = curr;
2113     if (prev)
2114       prev->next = new;
2115     else
2116       *cpp = new;
2117   }
2118 }  /* insert_clause */
2119 
2120 /*************
2121  *
2122  *   max_literal_weight()
2123  *
2124  *************/
2125 
max_literal_weight(struct clause * c,struct is_tree * wt_index)2126 int max_literal_weight(struct clause *c,
2127 		       struct is_tree *wt_index)
2128 {
2129   struct literal *lit;
2130   int wt, max;
2131   max = -INT_MAX;
2132   for (lit = c->first_lit; lit != NULL; lit = lit->next_lit) {
2133     if (!answer_lit(lit)) {
2134       wt = weight(lit->atom, wt_index);
2135       max = (wt > max ? wt : max);
2136     }
2137   }
2138   return(max);
2139 }  /* max_literal_weight */
2140 
2141 /*************
2142  *
2143  *    int weight_cl(c, wt_index)  --  Weigh a clause.
2144  *
2145  *    Also weigh answer lits, which have default weight 0.
2146  *
2147  *************/
2148 
weight_cl(struct clause * c,struct is_tree * wt_index)2149 int weight_cl(struct clause *c,
2150 	      struct is_tree *wt_index)
2151 {
2152   if (Flags[LITERALS_WEIGH_ONE].val)
2153     return num_literals(c);
2154   else {
2155     int w, neg_weight;
2156     struct literal *lit;
2157 
2158     neg_weight = Parms[NEG_WEIGHT].val;
2159     w = 0;
2160     lit = c->first_lit;
2161     while (lit) {
2162       w += weight(lit->atom, wt_index);
2163       if (!answer_lit(lit) && !lit->sign)
2164 	w += neg_weight;
2165       lit = lit->next_lit;
2166     }
2167     return(w);
2168   }
2169 }  /* weight_cl */
2170 
2171 /*************
2172  *
2173  *    hide_clause(c) --  c must be integrated
2174  *
2175  *    Clauses can be hidden instead of deallocated so that they can
2176  *    be printed later on (mostly so that a child can know its parent).
2177  *
2178  *************/
2179 
hide_clause(struct clause * c)2180 void hide_clause(struct clause *c)
2181 {
2182   c->next_cl = Hidden_clauses;
2183   Hidden_clauses = c;
2184 }  /* hide_clause */
2185 
2186 /*************
2187  *
2188  *   proof_last_hidden_empty()
2189  *
2190  *************/
2191 
proof_last_hidden_empty(void)2192 struct clause *proof_last_hidden_empty(void)
2193 {
2194   struct clause *c;
2195   struct clause *e = NULL;
2196 
2197   for (c = Hidden_clauses; c != NULL; c = c->next_cl) {
2198     if (num_literals(c) == 0)
2199       e = c;
2200   }
2201 #if 0
2202   if (e != NULL) {
2203     printf("\n=========== HIDDEN PROOF ===========\n");
2204     print_proof(stdout, e);
2205     printf("\n=========== end hidden proof ===========\n");
2206   }
2207 #endif
2208   return e;
2209 }  /* proof_last_hidden_empty */
2210 
2211 /*************
2212  *
2213  *    del_hidden_clauses() -- deallocate all hidden clauses
2214  *
2215  *************/
2216 
del_hidden_clauses(void)2217 void del_hidden_clauses(void)
2218 {
2219   struct clause *c;
2220 
2221   while (Hidden_clauses) {
2222     c = Hidden_clauses;
2223     Hidden_clauses = Hidden_clauses->next_cl;
2224     cl_del_int(c);
2225   }
2226 }  /* del_hidden_clauses */
2227 
2228 /*************
2229  *
2230  *    struct clause *cl_copy(c)
2231  *
2232  *    Do not copy the list of parents.
2233  *
2234  *************/
2235 
cl_copy(struct clause * c)2236 struct clause *cl_copy(struct clause *c)
2237 {
2238   struct clause *d;
2239   struct literal *l, *l1, *l2;
2240 
2241   d = get_clause();
2242   d->type = c->type;
2243   l = c->first_lit;
2244   l2 = NULL;
2245   while (l) {
2246     l1 = get_literal();
2247     l1->target = l->target;
2248     l1->container = d;
2249     l1->sign = l->sign;
2250     l1->atom = copy_term(l->atom);
2251     remove_var_syms(l1->atom);  /* nullify variable symbols (if present) */
2252     l1->atom->occ.lit = l1;
2253     if (!l2)
2254       d->first_lit = l1;
2255     else
2256       l2->next_lit = l1;
2257     l2 = l1;
2258     l = l->next_lit;
2259   }
2260   return(d);
2261 }  /* cl_copy */
2262 
2263 /*************
2264  *
2265  *    clause_ident(c1, c2)
2266  *
2267  *    Don't check permutations.
2268  *
2269  *************/
2270 
clause_ident(struct clause * c1,struct clause * c2)2271 int clause_ident(struct clause *c1,
2272 		 struct clause *c2)
2273 {
2274   struct literal *l1, *l2;
2275   int ok;
2276 
2277   for (l1 = c1->first_lit, l2 = c2->first_lit, ok = 1;
2278        l1 && l2 && ok;
2279        l1 = l1->next_lit, l2 = l2->next_lit)
2280     ok = (l1->sign == l2->sign && term_ident(l1->atom, l2->atom));
2281 
2282   return(ok && l1 == NULL && l2 == NULL);
2283 }  /* cl_ident */
2284 
2285 /*************
2286  *
2287  *    remove_var_syms(t)
2288  *
2289  *    Variable terms normally do not have sym_nums.  This
2290  *    routine removes any that are present.
2291  *
2292  *************/
2293 
remove_var_syms(struct term * t)2294 void remove_var_syms(struct term *t)
2295 {
2296   struct rel *r;
2297 
2298   if (t->type == VARIABLE)
2299     t->sym_num = 0;
2300   else if (t->type == COMPLEX)
2301     for (r = t->farg; r; r = r->narg)
2302       remove_var_syms(r->argval);
2303 }  /* remove_var_syms */
2304 
2305 /*************
2306  *
2307  *    cl_insert_tab(c)
2308  *
2309  *************/
2310 
cl_insert_tab(struct clause * c)2311 void cl_insert_tab(struct clause *c)
2312 {
2313   struct clause_ptr *cp1, *cp2, *cp3;
2314   int hashval, id;
2315 
2316   id = c->id;
2317   hashval = id % CLAUSE_TAB_SIZE;
2318   cp1 = Clause_tab[hashval];
2319   cp2 = NULL;
2320 
2321   /* keep the chains ordered--increasing id */
2322 
2323   while (cp1 && cp1->c->id < id) {
2324     cp2 = cp1;
2325     cp1 = cp1->next;
2326   }
2327   if (cp1 && cp1->c->id == id) {
2328     print_clause(stdout, c);
2329     abend("cl_insert_tab, clause already there.");
2330   }
2331   else {
2332     cp3 = get_clause_ptr();
2333     cp3->c = c;
2334     cp3->next = cp1;
2335     if (!cp2)
2336       Clause_tab[hashval] = cp3;
2337     else
2338       cp2->next = cp3;
2339   }
2340 }  /* cl_insert_tab */
2341 
2342 /*************
2343  *
2344  *    cl_delete_tab(c)
2345  *
2346  *************/
2347 
cl_delete_tab(struct clause * c)2348 void cl_delete_tab(struct clause *c)
2349 {
2350   struct clause_ptr *cp1, *cp2;
2351   int hashval, id;
2352 
2353   id = c->id;
2354   hashval = id % CLAUSE_TAB_SIZE;
2355   cp1 = Clause_tab[hashval];
2356   cp2 = NULL;
2357 
2358   /* chains are ordered--increasing id */
2359 
2360   while (cp1 && cp1->c->id < id) {
2361     cp2 = cp1;
2362     cp1 = cp1->next;
2363   }
2364   if (!cp1 || cp1->c->id != id) {
2365     print_clause(stdout, c);
2366     abend("cl_delete_tab, clause not found.");
2367   }
2368   else {
2369     if (!cp2)
2370       Clause_tab[hashval] = cp1->next;
2371     else
2372       cp2->next = cp1->next;
2373     free_clause_ptr(cp1);
2374   }
2375 }  /* cl_delete_tab */
2376 
2377 /*************
2378  *
2379  *    struct clause *cl_find(id)
2380  *
2381  *************/
2382 
cl_find(int id)2383 struct clause *cl_find(int id)
2384 {
2385   struct clause_ptr *cp1;
2386   int hashval;
2387 
2388   hashval = id % CLAUSE_TAB_SIZE;
2389   cp1 = Clause_tab[hashval];
2390 
2391   /* lists are ordered--increasing id */
2392 
2393   while (cp1 && cp1->c->id < id)
2394     cp1 = cp1->next;
2395   if (!cp1 || cp1->c->id != id)
2396     return(NULL);
2397   else
2398     return(cp1->c);
2399 }  /* cl_find */
2400 
2401 /*************
2402  *
2403  *     int lit_compare(l1, l2)
2404  *
2405  *     1. positive > negative
2406  *     2. answer > nonanswer
2407  *     3. lex
2408  *
2409  *************/
2410 
lit_compare(struct literal * l1,struct literal * l2)2411 int lit_compare(struct literal *l1,
2412 		struct literal *l2)
2413 {
2414   if (l1->sign > l2->sign)
2415     return(GREATER_THAN);
2416   else if (l1->sign < l2->sign)
2417     return(LESS_THAN);
2418   else if (answer_lit(l1) && !answer_lit(l2))
2419     return(GREATER_THAN);
2420   else if (!answer_lit(l1) && answer_lit(l2))
2421     return(LESS_THAN);
2422 
2423   else {
2424     if (Flags[PROPOSITIONAL].val) {
2425       if (l1->atom->sym_num == l2->atom->sym_num)
2426 	return(SAME_AS);
2427       else if (l1->atom->sym_num > l2->atom->sym_num)
2428 	return(GREATER_THAN);
2429       else
2430 	return(LESS_THAN);
2431     }
2432     else
2433       return(lex_order_vars(l1->atom, l2->atom));
2434   }
2435 }  /* lit_compare */
2436 
2437 /*************
2438  *
2439  *     int ordered_sub_clause(c, d)
2440  *
2441  *     True iff each literal of c occurs in d.
2442  *     Literals assumed to be ordered by lit_compare.
2443  *
2444  *     This routine treats any answer literals as ordinary literals.
2445  *     Although this might be considered a bug, I decided to write it
2446  *     this way, because it is designed for propositional clauses
2447  *     which usually don't have answer literals, and checking for
2448  *     answer literals will slow it too much.
2449  *
2450  *************/
2451 
ordered_sub_clause(struct clause * c1,struct clause * c2)2452 int ordered_sub_clause(struct clause *c1,
2453 		       struct clause *c2)
2454 {
2455   struct literal *l1, *l2;
2456   int i;
2457 
2458   l1 = c1->first_lit;
2459   l2 = c2->first_lit;
2460 
2461   while (l1 && l2) {
2462     i = lit_compare(l1, l2);
2463     if (i == SAME_AS) {
2464       l1 = l1->next_lit;
2465       l2 = l2->next_lit;
2466     }
2467     else if (i == GREATER_THAN)
2468       l2 = l2->next_lit;
2469     else if (i == LESS_THAN)
2470       l2 = NULL;
2471     else
2472       abend("ordered_sub_clause: not total.");
2473   }
2474 
2475 #if 0
2476   printf("\n c1:%d ", l1 == NULL); p_clause(c1);
2477   printf("\n c2:  "); p_clause(c2);
2478 #endif
2479   return(l1 == NULL);
2480 }  /* ordered_sub_clause */
2481 
2482 /*************
2483  *
2484  *     int sub_clause(c, d)
2485  *
2486  *     True iff each literal of c occurs in d.
2487  *     Literals are not assumed to be ordered.
2488  *
2489  *     This routine treats any answer literals as ordinary literals.
2490  *     Although this might be considered a bug, I decided to write it
2491  *     this way, because it is designed for propositional clauses
2492  *     which usually don't have answer literals, and checking for
2493  *     answer literals will slow it too much.
2494  *
2495  *************/
2496 
sub_clause(struct clause * c1,struct clause * c2)2497 int sub_clause(struct clause *c1,
2498 	       struct clause *c2)
2499 {
2500   struct literal *l1, *l2;
2501   int found;
2502 
2503   for(l1 = c1->first_lit; l1; l1 = l1->next_lit) {
2504     l2 = c2->first_lit;
2505     found = 0;
2506     while (l2 && !found) {
2507       if (l1->sign == l2->sign && l1->atom->sym_num == l2->atom->sym_num)
2508 	found = 1;
2509       else
2510 	l2 = l2->next_lit;
2511     }
2512     if (!found)
2513       return(0);
2514   }
2515   return(1);
2516 }  /* sub_clause */
2517 
2518 /*************
2519  *
2520  *    sort_lits(c)  --  sort literals
2521  *
2522  *************/
2523 
sort_lits(struct clause * c)2524 int sort_lits(struct clause *c)
2525 {
2526   struct literal *sorted, *prev, *curr, *next, *insert;
2527   int changed = 0;
2528 
2529   /* This is an insertion sort.  Use lit_compare */
2530 
2531   if (c->first_lit) {
2532     sorted = c->first_lit;
2533     insert = sorted->next_lit;
2534     sorted->next_lit = NULL;
2535 
2536     while(insert) {
2537       prev = NULL;
2538       curr = sorted;
2539       while (curr && lit_compare(insert, curr) == GREATER_THAN) {
2540 	prev = curr;
2541 	curr = curr->next_lit;
2542       }
2543       if (curr != NULL)
2544 	changed = 1;
2545       next = insert->next_lit;
2546       insert->next_lit = curr;
2547       if (prev)
2548 	prev->next_lit = insert;
2549       else
2550 	sorted = insert;
2551       insert = next;
2552     }
2553     c->first_lit = sorted;
2554   }
2555   return changed;
2556 }  /* sort lits */
2557 
2558 /*************
2559  *
2560  *    all_cont_cl(t, cpp) - insert containing clauses of t into *cpp
2561  *
2562  *************/
2563 
all_cont_cl(struct term * t,struct clause_ptr ** cpp)2564 void all_cont_cl(struct term *t,
2565 		 struct clause_ptr **cpp)
2566 {
2567   struct rel *r;
2568   struct clause *c;
2569   struct list *l;
2570 
2571   if (t->type != VARIABLE && t->varnum != 0) {  /* atom */
2572     c = t->occ.lit->container;
2573     l = c->container;
2574     if (l == Usable || l == Sos || l == Demodulators)
2575       insert_clause(c, cpp);
2576   }
2577   else {  /* term */
2578     r = t->occ.rel;
2579     while (r) {
2580       all_cont_cl(r->argof, cpp);
2581       r = r->nocc;
2582     }
2583   }
2584 }  /* all_cont_cl */
2585 
2586 /*************
2587  *
2588  *    zap_cl_list(lst)
2589  *
2590  *************/
2591 
zap_cl_list(struct list * lst)2592 void zap_cl_list(struct list *lst)
2593 {
2594   struct clause *c1, *c2;
2595 
2596   c1 = lst->first_cl;
2597   while (c1) {
2598     c2 = c1;
2599     c1 = c1->next_cl;
2600     cl_del_non(c2);
2601   }
2602   free_list(lst);
2603 }  /* zap_cl_list */
2604 
2605 /*************
2606  *
2607  *   is_eq()
2608  *
2609  *************/
2610 
is_eq(int sym_num)2611 int is_eq(int sym_num)
2612 {
2613   char *name;
2614 
2615   name = sn_to_str(sym_num);
2616 
2617   if (Flags[TPTP_EQ].val)
2618     return(sn_to_arity(sym_num) == 2 && str_ident("equal", name));
2619   else
2620     return(sn_to_arity(sym_num) == 2 &&
2621 	   (initial_str("EQ", name) ||
2622 	    initial_str("Eq", name) ||
2623 	    initial_str("eq", name) ||
2624 	    str_ident("=", name)));
2625 }  /* is_eq */
2626 
2627 /*************
2628  *
2629  *    mark_literal(lit)
2630  *
2631  *    Atoms have varnum > 0.  This routine inserts the appropriate code.
2632  *
2633  *************/
2634 
mark_literal(struct literal * lit)2635 void mark_literal(struct literal *lit)
2636 {
2637   char *name;
2638   struct term *a;
2639 
2640   a = lit->atom;
2641 
2642   name = sn_to_str(a->sym_num);
2643 
2644   if (initial_str("$ANS", name) || initial_str("$Ans", name) ||
2645       initial_str("$ans", name))
2646     a->varnum = ANSWER;  /* answer literal */
2647 
2648   else if (is_eq(a->sym_num)) {
2649     if (lit->sign)
2650       a->varnum = POS_EQ;  /* positive equality */
2651     else
2652       a->varnum = NEG_EQ;  /* negative equality */
2653   }
2654 
2655   else if (sn_to_ec(a->sym_num) > 0)
2656     a->varnum = EVALUABLE;  /* $ID, $LE, $AND, ... */
2657 
2658   else if (is_symbol(a, "->", 2) && is_eq(a->farg->narg->argval->sym_num))
2659     a->varnum = CONDITIONAL_DEMOD;
2660 
2661   else
2662     a->varnum = NORM_ATOM;  /* normal atom */
2663 
2664 }  /* mark_literal */
2665 
2666 /*************
2667  *
2668  *    int get_ancestors(c, cpp, ipp)
2669  *
2670  *    cpp is the list under construction, sorted by ID.
2671  *    ipp is a corresponding list of the level of each clause.
2672  *
2673  *    Return the level of the proof.
2674  *
2675  *  The justification list of a clause (c->parents) is a list
2676  *  of integers.  Usually, negative integers represent inference
2677  *  rules, and positive integers are the IDs of parent clauses.
2678  *  Exception:  LIST_RULE is a large negative integer.  If a member
2679  *  is <= LIST_RULE, then a list of length (LIST_RULE-member) follows
2680  *  (and typically represents a position in a clause).
2681  *
2682  *************/
2683 
get_ancestors(struct clause * c,struct clause_ptr ** cpp,struct ilist ** ipp)2684 int get_ancestors(struct clause *c,
2685 		  struct clause_ptr **cpp,
2686 		  struct ilist **ipp)
2687 {
2688   struct clause_ptr *cp1, *cp2, *cp3;
2689   struct ilist *ip1, *ip2, *ip3;
2690   struct ilist *ip;
2691   struct clause *d;
2692   int max, lev, n;
2693 
2694   cp1 = *cpp; ip1 = *ipp;
2695   cp3 = NULL; ip3 = NULL;
2696   /* First check to see if the clause has already been processed. */
2697   while (cp1 && cp1->c->id < c->id) {
2698     cp3 = cp1; cp1 = cp1->next;
2699     ip3 = ip1; ip1 = ip1->next;
2700   }
2701   if (!cp1 || cp1->c->id > c->id) {
2702     /*  Process the clause. */
2703     cp2 = get_clause_ptr();
2704     ip2 = get_ilist();
2705     cp2->c = c;
2706     if (!cp3) {
2707       cp2->next = *cpp; *cpp = cp2;
2708       ip2->next = *ipp; *ipp = ip2;
2709     }
2710     else {
2711       cp2->next = cp3->next; cp3->next = cp2;
2712       ip2->next = ip3->next; ip3->next = ip2;
2713     }
2714 
2715     max = -1;
2716     for (ip = c->parents; ip; ip = ip->next) {
2717 
2718       if (ip->i <= LIST_RULE) {
2719 	/* LIST_RULE is a large negative number. */
2720 	/* If ip->i is less than LIST_RULE, then a list follows. */
2721 	int i;
2722 	int j = LIST_RULE - ip->i;  /* size of list */
2723 	/* Make ip point at the last element of the list. */
2724 	for (i = 1; i <= j; i++)
2725 	  ip = ip->next;
2726       }
2727 
2728       else if (ip->i >= 0) {
2729 	/* < 0 means it's a code for an inference rule */
2730 	d = cl_find(ip->i);
2731 	if (!d)
2732 	  printf("WARNING, clause %d not found, proof is incomplete.\n", ip->i);
2733 
2734 	else {
2735 	  n = get_ancestors(d, cpp, ipp);
2736 	  max = (n > max ? n : max);
2737 	}
2738       }
2739     }
2740 
2741     if (!c->parents)
2742       lev = 0;
2743     else if (c->parents->i == NEW_DEMOD_RULE)
2744       lev = max;
2745     else
2746       lev = max + 1;
2747     ip2->i = lev;
2748 #if 0
2749     printf("level %d: ", lev); p_clause(c);
2750 #endif
2751     return(lev);
2752   }
2753   else {
2754     /* The clause has already been processed, so just return its level. */
2755     return(ip1->i);
2756   }
2757 }  /* get_ancestors */
2758 
2759 /*************
2760  *
2761  *   clauses_to_ids()
2762  *
2763  *************/
2764 
clauses_to_ids(struct clause_ptr * p)2765 struct ilist *clauses_to_ids(struct clause_ptr *p)
2766 {
2767   if (p == NULL)
2768     return NULL;
2769   else {
2770     struct ilist *ip = get_ilist();
2771     ip->i = p->c->id;
2772     ip->next = clauses_to_ids(p->next);
2773     return ip;
2774   }
2775 }  /* clauses_to_ids */
2776 
2777 /*************
2778  *
2779  *   free_clause_ptr_list()
2780  *
2781  *************/
2782 
free_clause_ptr_list(struct clause_ptr * p)2783 void free_clause_ptr_list(struct clause_ptr *p)
2784 {
2785   if (p != NULL) {
2786     free_clause_ptr_list(p->next);
2787     free_clause_ptr(p);
2788   }
2789 }  /* free_clause_ptr_list */
2790 
2791 /*************
2792  *
2793  *    get_ancestors2(c)
2794  *
2795  *    This is a more convenient version of get_ancestors.
2796  *
2797  *************/
2798 
get_ancestors2(struct clause * c)2799 struct ilist *get_ancestors2(struct clause *c)
2800 {
2801   struct clause_ptr *cp = NULL;
2802   struct ilist *ip = NULL;
2803   int level;
2804 
2805   level = get_ancestors(c, &cp, &ip);
2806   free_ilist_list(ip);
2807   ip = clauses_to_ids(cp);
2808   free_clause_ptr_list(cp);
2809   return ip;
2810 }  /* get_ancestors2 */
2811 
2812 /*************
2813  *
2814  *    int just_to_supporters(ip)
2815  *
2816  *    Given a justification list, return a set of the parents.
2817  *
2818  *************/
2819 
just_to_supporters(struct ilist * ip)2820 struct ilist *just_to_supporters(struct ilist *ip)
2821 {
2822   struct ilist *ip_construct = NULL;
2823 
2824   for ( ; ip; ip = ip->next) {
2825 
2826     if (ip->i <= LIST_RULE) {
2827       /* LIST_RULE is a large negative number. */
2828       /* If ip->i is less than LIST_RULE, then a list follows. */
2829       int i;
2830       int j = LIST_RULE - ip->i;  /* size of list */
2831       /* Make ip point at the last element of the list. */
2832       for (i = 1; i <= j; i++)
2833 	ip = ip->next;
2834     }
2835 
2836     else if (ip->i >= 0) {
2837       /* < 0 means it's a code for an inference rule */
2838 #if 1
2839       ip_construct = iset_add(ip->i, ip_construct);
2840 #else
2841       struct ilist *ip_new = get_ilist();
2842       ip_new->i = ip->i;
2843       ip_new->next = ip_construct;
2844       ip_construct = ip_new;
2845 #endif
2846     }
2847   }
2848 #if 1
2849   return ip_construct;
2850 #else
2851   return reverse_ip(ip_construct, NULL);
2852 #endif
2853 }  /* just_to_supporters */
2854 
2855 /*************
2856  *
2857  *    int renumber_vars_term(c)
2858  *
2859  *        Renumber the variables of a term, starting with 0.  `c' must
2860  *    be nonintegrated.  return(0) if more than MAXVARS distinct variables.
2861  *
2862  *    This is very special-purpose.  Ordinarily, you'll call renumber_vars(c)
2863  *    on clauses.
2864  *
2865  *************/
2866 
renumber_vars_term(struct term * t)2867 int renumber_vars_term(struct term *t)
2868 {
2869   int varnums[MAX_VARS];
2870   int i;
2871 
2872   for (i = 0; i < MAX_VARS; i++)
2873     varnums[i] = -1;
2874 
2875   return renum_vars_term(t, varnums);
2876 
2877 }  /* renumber_vars_term */
2878 
2879 /*************
2880  *
2881  *    int renumber_vars(c)
2882  *
2883  *        Renumber the variables of a clause, starting with 0.  `c' must
2884  *    be nonintegrated.  return(0) if more than MAXVARS distinct variables.
2885  *
2886  *************/
2887 
renumber_vars(struct clause * c)2888 int renumber_vars(struct clause *c)
2889 {
2890   struct literal *lit;
2891   int varnums[MAX_VARS];
2892   int i, ok;
2893 
2894   ok = 1;
2895   for (i = 0; i < MAX_VARS; i++)
2896     varnums[i] = -1;
2897 
2898   lit = c->first_lit;
2899   while (lit) {
2900     if (renum_vars_term(lit->atom, varnums) == 0)
2901       ok = 0;
2902     lit = lit->next_lit;
2903   }
2904 
2905   return(ok);
2906 
2907 }  /* renumber_vars */
2908 
2909 /*************
2910  *
2911  *    int renum_vars_term(term, varnums) -- called from renumber_vars.
2912  *
2913  *************/
2914 
renum_vars_term(struct term * t,int * varnums)2915 int renum_vars_term(struct term *t,
2916 		    int *varnums)
2917 {
2918   struct rel *r;
2919   int i, ok;
2920 
2921   if (t->type == NAME)
2922     return(1);
2923   else if (t->type == COMPLEX) {
2924     ok = 1;
2925     r = t->farg;
2926     while (r) {
2927       if (renum_vars_term(r->argval, varnums) == 0)
2928 	ok = 0;
2929       r = r->narg;
2930     }
2931     return(ok);
2932   }
2933   else {
2934     i = 0;
2935     while (i < MAX_VARS && varnums[i] != -1 && varnums[i] != t->varnum)
2936       i++;
2937     if (i == MAX_VARS)
2938       return(0);
2939     else {
2940       if (varnums[i] == -1) {
2941 	varnums[i] = t->varnum;
2942 	t->varnum = i;
2943       }
2944       else
2945 	t->varnum = i;
2946       return(1);
2947     }
2948   }
2949 }  /* renum_vars_term */
2950 
2951 /*************
2952  *
2953  *    clear_var_names(t) -- set sym_num field of all variables to NULL
2954  *
2955  *************/
2956 
clear_var_names(struct term * t)2957 void clear_var_names(struct term *t)
2958 {
2959   struct rel *r;
2960 
2961   if (t->type == VARIABLE)
2962     t->sym_num = 0;
2963   else {
2964     for (r = t->farg; r; r = r->narg)
2965       clear_var_names(r->argval);
2966   }
2967 }  /* clear_var_names */
2968 
2969 /*************
2970  *
2971  *    cl_clear_vars(c)
2972  *
2973  *************/
2974 
cl_clear_vars(struct clause * c)2975 void cl_clear_vars(struct clause *c)
2976 {
2977   struct literal *lit;
2978 
2979   for (lit = c->first_lit; lit; lit = lit->next_lit)
2980     clear_var_names(lit->atom);
2981 }
2982 
2983 /*************
2984  *
2985  *    void distinct_vars_rec(t, a, max) -- called by distinct_vars
2986  *
2987  *************/
2988 
distinct_vars_rec(struct term * t,int * a,int * max)2989 static void distinct_vars_rec(struct term *t,
2990 			      int *a,
2991 			      int *max)
2992 {
2993   struct rel *r;
2994   int i, vn;
2995 
2996   if (t->type == VARIABLE) {
2997     vn = t->varnum;
2998     for (i = 0; i < MAX_VARS && a[i] != -1 && a[i] != vn; i++);
2999     if (i != MAX_VARS && a[i] == -1) {
3000       a[i] = vn;
3001       *max = i+1;
3002     }
3003   }
3004   else if (t->type == COMPLEX) {
3005     for (r = t->farg; r && *max < MAX_VARS; r = r->narg)
3006       distinct_vars_rec(r->argval, a, max);
3007   }
3008 }  /* distinct_vars_rec */
3009 
3010 /*************
3011  *
3012  *    int distinct_vars(c) -- number of variables in a clause.
3013  *
3014  *    if >= MAX_VARS, return MAX_VARS.
3015  *
3016  *************/
3017 
distinct_vars(struct clause * c)3018 int distinct_vars(struct clause *c)
3019 {
3020   struct literal *lit;
3021   int a[MAX_VARS], i, max;
3022 
3023   for (i = 0; i < MAX_VARS; i++)
3024     a[i] = -1;
3025 
3026   for (lit = c->first_lit, max = 0; lit; lit = lit->next_lit)
3027     distinct_vars_rec(lit->atom, a, &max);
3028 
3029   return(max);
3030 
3031 }  /* distinct_vars */
3032 
3033 /*************
3034  *
3035  *    struct clause *find_first_cl(l)
3036  *
3037  *************/
3038 
find_first_cl(struct list * l)3039 struct clause *find_first_cl(struct list *l)
3040 {
3041   struct clause *c;
3042 
3043   if (!l->first_cl)
3044     return(NULL);
3045   else {
3046     c = l->first_cl;
3047     return(c);
3048   }
3049 }  /* find_first_cl */
3050 
3051 /*************
3052  *
3053  *    struct clause *find_last_cl(l)
3054  *
3055  *************/
3056 
find_last_cl(struct list * l)3057 struct clause *find_last_cl(struct list *l)
3058 {
3059   struct clause *c;
3060 
3061   if (!l->last_cl)
3062     return(NULL);
3063   else {
3064     c = l->last_cl;
3065     return(c);
3066   }
3067 }  /* find_last_cl */
3068 
3069 /*************
3070  *
3071  *    struct clause *find_random_cl(l)
3072  *
3073  *************/
3074 
find_random_cl(struct list * l)3075 struct clause *find_random_cl(struct list *l)
3076 {
3077   struct clause *c;
3078   int i, j;
3079 
3080   if (l->first_cl == NULL)
3081     return(NULL);
3082   else {
3083     j = (rand() % Stats[SOS_SIZE]) + 1;
3084     c = l->first_cl;
3085     i = 1;
3086     while (i < j && c != NULL) {
3087       c = c->next_cl;
3088       i++;
3089     }
3090     if (c == NULL)
3091       abend("find_random_cl, sos bad.");
3092     return(c);
3093   }
3094 }  /* find_random_cl */
3095 
3096 /*************
3097  *
3098  *   get_clauses_of_wt_range()
3099  *
3100  *************/
3101 
get_clauses_of_wt_range(struct clause * c,int min,int max)3102 struct clause_ptr *get_clauses_of_wt_range(struct clause *c,
3103 					   int min, int max)
3104 {
3105   if (c == NULL)
3106     return NULL;
3107   else if (c->pick_weight >= min && c->pick_weight <= max) {
3108     struct clause_ptr *p = get_clause_ptr();
3109     p->c = c;
3110     p->next = get_clauses_of_wt_range(c->next_cl, min, max);
3111     return p;
3112   }
3113   else
3114     return get_clauses_of_wt_range(c->next_cl, min, max);
3115 }  /* get_clauses_of_wt_range */
3116 
3117 /*************
3118  *
3119  *   clause_ptr_list_size()
3120  *
3121  *************/
3122 
clause_ptr_list_size(struct clause_ptr * p)3123 int clause_ptr_list_size(struct clause_ptr *p)
3124 {
3125   if (p == NULL)
3126     return 0;
3127   else
3128     return (1 + clause_ptr_list_size(p->next));
3129 }  /* clause_ptr_list_size */
3130 
3131 /*************
3132  *
3133  *   nth_clause() -- this counts from 1.
3134  *
3135  *************/
3136 
nth_clause(struct clause_ptr * p,int n)3137 struct clause *nth_clause(struct clause_ptr *p, int n)
3138 {
3139   if (p == NULL)
3140     return NULL;
3141   else if (n == 1)
3142     return p->c;
3143   else
3144     return nth_clause(p->next, n-1);
3145 }  /* nth_clause */
3146 
3147 /*************
3148  *
3149  *   zap_clause_ptr_list(p)
3150  *
3151  *   Free the nodes, but not the clauses they point to.
3152  *
3153  *************/
3154 
zap_clause_ptr_list(struct clause_ptr * p)3155 void zap_clause_ptr_list(struct clause_ptr *p)
3156 {
3157   if (p != NULL) {
3158     zap_clause_ptr_list(p->next);
3159     free_clause_ptr(p);
3160   }
3161 }  /* zap_clause_ptr_list */
3162 
3163 /*************
3164  *
3165  *    struct clause *find_random_lightest_cl(l)
3166  *
3167  *************/
3168 
find_random_lightest_cl(struct list * l)3169 struct clause *find_random_lightest_cl(struct list *l)
3170 {
3171   struct clause *c = find_lightest_cl(l);
3172   if (c == NULL)
3173     return NULL;
3174   else {
3175     int wt = c->pick_weight;
3176     struct clause_ptr *p = get_clauses_of_wt_range(l->first_cl, wt, wt);
3177     int n = clause_ptr_list_size(p);
3178     int j = (rand() % n) + 1;
3179 
3180     c = nth_clause(p, j);
3181     zap_clause_ptr_list(p);
3182     return c;
3183   }
3184 }  /* find_random_lightest_cl */
3185 
3186 /*************
3187  *
3188  *    struct clause *find_mid_lightest_cl(l)
3189  *
3190  *************/
3191 
find_mid_lightest_cl(struct list * l)3192 struct clause *find_mid_lightest_cl(struct list *l)
3193 {
3194   struct clause *c = find_lightest_cl(l);
3195   if (c == NULL)
3196     return NULL;
3197   else {
3198     int wt = c->pick_weight;
3199     struct clause_ptr *p = get_clauses_of_wt_range(l->first_cl, wt, wt);
3200     int n = clause_ptr_list_size(p);
3201     int j = (n / 2) + 1;
3202 
3203     c = nth_clause(p, j);
3204     zap_clause_ptr_list(p);
3205     return c;
3206   }
3207 }  /* find_mid_lightest_cl */
3208 
3209 /*************
3210  *
3211  *    struct clause *find_lightest_cl(l)
3212  *
3213  *    If more than one of mimimum weight, return first or last of those,
3214  *    according to the flag PICK_LAST_LIGHTEST.
3215  *
3216  *    Input sos clauses might have weight field set to -MAX_INT so that
3217  *    they are returned first (in order).
3218  *
3219  *************/
3220 
find_lightest_cl(struct list * l)3221 struct clause *find_lightest_cl(struct list *l)
3222 {
3223   if (l->first_cl == NULL)
3224     return NULL;
3225   else {
3226     struct clause *cm = l->first_cl;
3227     int wm = cm->pick_weight;
3228     struct clause *c = cm->next_cl;
3229     while (c) {
3230       int w = c->pick_weight;
3231       if (Flags[PICK_LAST_LIGHTEST].val ? (w <= wm) : (w < wm)) {
3232 	wm = w;
3233 	cm = c;
3234       }
3235       c = c->next_cl;
3236     }
3237     return cm;
3238   }
3239 }  /* find_lightest_cl */
3240 
3241 /*************
3242  *
3243  *    struct clause *find_lightest_geo_child(l)
3244  *
3245  *    Find the lightest clause c for which child_of_geometry(c) holds.
3246  *    If there are no children of geometry, return NULL.
3247  *    If more than one of mimimum weight, return first of those.
3248  *
3249  *************/
3250 
find_lightest_geo_child(struct list * l)3251 struct clause *find_lightest_geo_child(struct list *l)
3252 {
3253   struct clause *c, *cmin;
3254   int w, wmin;
3255 
3256   for (c = l->first_cl, cmin = NULL, wmin = MAX_INT; c; c = c->next_cl) {
3257     if (child_of_geometry(c)) {
3258       w = c->pick_weight;
3259       if (!cmin || w < wmin) {
3260 	wmin = w;
3261 	cmin = c;
3262       }
3263     }
3264   }
3265   return(cmin);
3266 }  /* find_lightest_geo_child */
3267 
3268 /*************
3269  *
3270  *    struct clause *find_interactive_cl()
3271  *
3272  *************/
3273 
find_interactive_cl(void)3274 struct clause *find_interactive_cl(void)
3275 {
3276   FILE *fin, *fout;
3277 
3278   fin  = fopen("/dev/tty", "r");
3279   fout = fopen("/dev/tty", "w");
3280 
3281   if (!fin || !fout) {
3282     printf("interaction failure: cannot find tty.\n");
3283     fprintf(stderr, "interaction failure: cannot find tty.\n");
3284     return(NULL);
3285   }
3286   else {
3287     int id;
3288     struct clause *c = NULL;
3289     int done = 0;
3290     char s[256];
3291 
3292     while (!done) {
3293       fprintf(fout,"\nEnter clause number of next given clause, or 0 to terminate\ninteractive_given mode, or -1 to print list sos.\n? ");
3294       fscanf(fin, "%s", s);
3295       if (!str_int(s, &id)) {
3296 	fprintf(fout, "%c\nNot an integer: \"%s\", try again.\n", Bell, s);
3297       }
3298       else if (id == 0) {
3299 	printf("\nTurning off interactive_given mode.\n");
3300 	fprintf(fout, "\nTurning off interactive_given mode.\n");
3301 	Flags[INTERACTIVE_GIVEN].val = 0;
3302 	c = NULL;
3303 	done = 1;
3304       }
3305       else if (id == -1) {
3306 	struct clause *c;
3307 	for (c = Sos->first_cl; c; c = c->next_cl)
3308 	  print_clause(fout, c);
3309       }
3310       else {
3311 	c = cl_find(id);
3312 	if (!c)
3313 	  fprintf(fout, "%c\nClause %d not found.\n", Bell, id);
3314 	else if (c->container != Sos)
3315 	  fprintf(fout, "%c\nClause %d not in sos.\n", Bell, id);
3316 	else {
3317 	  done = 1;
3318 	  fprintf(fout, "\nOk, clause %d will be given.\n", id);
3319 	}
3320       }
3321     }
3322     fclose(fin);
3323     fclose(fout);
3324     return(c);
3325   }
3326 }  /* find_interactive_cl */
3327 
3328 /*************
3329  *
3330  *    struct clause *find_given_clause()
3331  *
3332  *************/
3333 
find_given_clause(void)3334 struct clause *find_given_clause(void)
3335 {
3336   struct clause *giv_cl;
3337 
3338   if (Flags[INTERACTIVE_GIVEN].val) {
3339     giv_cl = find_interactive_cl();
3340     if (giv_cl)
3341       return(giv_cl);
3342   }
3343   if (Flags[SOS_QUEUE].val)
3344       giv_cl = find_first_cl(Sos);
3345 
3346   else if (Flags[SOS_STACK].val)
3347     giv_cl = find_last_cl(Sos);
3348 
3349   else if (Parms[PICK_GIVEN_RATIO].val != -1 &&
3350 	   Stats[CL_GIVEN] % (Parms[PICK_GIVEN_RATIO].val + 1) == 0)
3351     giv_cl = find_first_cl(Sos);
3352 
3353   else if (Parms[PICK_DIFF].val != -1)
3354     giv_cl = find_pickdiff_cl(Sos, Usable);
3355 
3356   else if (Flags[PICK_RANDOM_LIGHTEST].val)
3357     giv_cl = find_random_lightest_cl(Sos);
3358 
3359   else if (Flags[PICK_MID_LIGHTEST].val)
3360     giv_cl = find_mid_lightest_cl(Sos);
3361 
3362   else if (Parms[GEO_GIVEN_RATIO].val != -1) {
3363     if (Stats[CL_GIVEN] % (Parms[GEO_GIVEN_RATIO].val + 1) == 0) {
3364       giv_cl = find_lightest_geo_child(Sos);
3365       if (!giv_cl)
3366 	giv_cl = find_lightest_cl(Sos);
3367     }
3368     else
3369       giv_cl = find_lightest_cl(Sos);
3370   }
3371 
3372   else  /* this is the default */
3373     giv_cl = find_lightest_cl(Sos);
3374 
3375   return(giv_cl);
3376 }  /* find_given_clause */
3377 
3378 /*************
3379  *
3380  *    struct clause *extract_given_clause()
3381  *
3382  *************/
3383 
extract_given_clause(void)3384 struct clause *extract_given_clause(void)
3385 {
3386   struct clause *giv_cl;
3387 
3388   CLOCK_START(PICK_GIVEN_TIME);
3389   giv_cl = find_given_clause();
3390   if (giv_cl) {
3391     rem_from_list(giv_cl);
3392   }
3393   CLOCK_STOP(PICK_GIVEN_TIME);
3394   return(giv_cl);
3395 }  /* extract_given_clause */
3396 
3397 /*************
3398  *
3399  *    int unit_del(c)  --  unit deletion
3400  *
3401  *    Delete any literals that are subsumed by a unit with opposite sign.
3402  *
3403  *    Answer literals on the units complicate things.  In particular,
3404  *    if they contain variables not in the regular literal.
3405  *
3406  *    This assumes that FOR_SUB_FPA is clear, because the discrimination
3407  *    index is used.
3408  *
3409  *    Return 1 if any deletions occur.
3410  *
3411  *************/
3412 
unit_del(struct clause * c)3413 int unit_del(struct clause *c)
3414 {
3415   struct clause *d;
3416   struct literal *prev, *curr, *next, *answers, *l1, *l2;
3417   struct term *d_atom;
3418   struct context *s;
3419   struct is_tree *is_db;
3420   struct fsub_pos *pos;
3421   struct ilist *ip0, *ip, *lp;
3422   int deleted, return_val;
3423 
3424   return_val = 0;
3425   s = get_context();
3426   s->multiplier = 1;
3427 
3428   /* first get last parent */
3429   lp = c->parents;
3430   if (lp)
3431     while (lp->next)
3432       lp = lp->next;
3433 
3434   ip0 = lp;  /* save position to insert "ud" if any deleted */
3435 
3436   answers = NULL;
3437   prev = NULL;
3438   next = c->first_lit;
3439   while (next) {
3440     curr = next;
3441     next = next->next_lit;
3442     is_db = curr->sign ? Is_neg_lits : Is_pos_lits;
3443     d_atom = fs_retrieve(curr->atom, s, is_db, &pos);
3444     deleted = 0;
3445     while (d_atom && !deleted) {
3446       d = d_atom->occ.lit->container;
3447       if (d->container != Passive && num_literals(d) == 1) {
3448 	return_val = 1;
3449 	if (prev)
3450 	  prev->next_lit = next;
3451 	else
3452 	  c->first_lit = next;
3453 	ip = get_ilist();  /* append to history */
3454 	ip->i = d->id;
3455 	if (!lp)
3456 	  c->parents = ip;
3457 	else
3458 	  lp->next = ip;
3459 	lp = ip;
3460 
3461 	l2 = d->first_lit;  /* now append any answer literals */
3462 	while (l2) {
3463 	  if (answer_lit(l2)) {
3464 	    l1 = get_literal();
3465 	    l1->container = c;
3466 	    l1->sign = l2->sign;
3467 	    l1->atom = apply(l2->atom, s);
3468 	    s->multiplier++;  /* in case answer has lone vars */
3469 	    l1->atom->varnum = ANSWER;
3470 	    l1->atom->occ.lit = l1;
3471 	    l1->next_lit = answers;
3472 	    answers = l1;
3473 	  }
3474 	  l2 = l2->next_lit;
3475 	}
3476 
3477 	curr->atom->occ.lit = NULL;  /* so zap_term won't complain */
3478 	zap_term(curr->atom);
3479 	free_literal(curr);
3480 
3481 	canc_fs_pos(pos, s);
3482 	Stats[UNIT_DELETES]++;
3483 	deleted = 1;
3484       }
3485       else
3486 	d_atom = fs_retrieve((struct term *) NULL, s, is_db, &pos);
3487     }
3488     if (!deleted)
3489       prev = curr;
3490   }
3491   if (!prev)
3492     c->first_lit = answers;
3493   else
3494     prev->next_lit = answers;
3495   if (lp != ip0) {  /* at least one deletion occurred */
3496     if (s->multiplier != 1) {
3497       /* Answer lits added; renumber in case new vars introduced. */
3498       if (renumber_vars(c) == 0) {
3499 	print_clause(stdout, c);
3500 	abend("unit_del, too many variables introduced.");
3501       }
3502     }
3503     ip = get_ilist();
3504     ip->i = UNIT_DEL_RULE;
3505     if (!ip0) {
3506       ip->next = c->parents;
3507       c->parents = ip;
3508     }
3509     else {
3510       ip->next = ip0->next;
3511       ip0->next = ip;
3512     }
3513   }
3514   free_context(s);
3515   return(return_val);
3516 }  /* unit_del */
3517 
3518 /*************
3519  *
3520  *   back_unit_deletion()
3521  *
3522  *************/
3523 
back_unit_deletion(struct clause * c,int input)3524 void back_unit_deletion(struct clause *c,
3525 			int input)
3526 {
3527   struct clause *d, *resolvent;
3528   struct literal *c_lit;
3529   struct term *c_atom, *d_atom;
3530   struct context *c_subst, *d_subst;
3531   struct fpa_tree *ut;
3532   struct trail *tr;
3533   struct list *source_list;
3534 
3535   c_lit = ith_literal(c,1);
3536   c_atom = c_lit->atom;
3537 
3538   ut = build_tree(c_lit->atom, INSTANCE, Parms[FPA_LITERALS].val,
3539 		  c_lit->sign ? Fpa_neg_lits : Fpa_pos_lits);
3540 
3541   d_atom = next_term(ut, 0);
3542   c_subst = get_context();
3543   d_subst = get_context();  /* This will stay empty */
3544 
3545   while (d_atom) {
3546     d = d_atom->occ.lit->container;
3547     source_list = d->container;
3548     tr = NULL;
3549     if (source_list == Usable || source_list == Sos) {
3550       if (match(c_atom, c_subst, d_atom, &tr)) {
3551 	resolvent = build_bin_res(c_atom, c_subst, d_atom, d_subst);
3552 	resolvent->parents->i = BACK_UNIT_DEL_RULE;
3553 	clear_subst_1(tr);
3554 	Stats[CL_GENERATED]++;
3555 	Stats[BACK_UNIT_DEL_GEN]++;
3556 
3557 #if 1
3558 	if (source_list == Usable) {
3559 	  SET_BIT(resolvent->bits, SCRATCH_BIT);
3560 	  /* printf("Clause destined for Usable:\n"); */
3561 	  p_clause(resolvent);
3562 	}
3563 #endif
3564 
3565 	CLOCK_STOP(BACK_UNIT_DEL_TIME);
3566         pre_process(resolvent, input, Sos);
3567 	CLOCK_START(BACK_UNIT_DEL_TIME);
3568       }
3569     }
3570     d_atom = next_term(ut, 0);
3571   }
3572   free_context(c_subst);
3573   free_context(d_subst);
3574 
3575 }  /* back_unit_deletion */
3576