-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula.c
4200 lines (3822 loc) · 90.7 KB
/
formula.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Penguin */
/*
* Copyright (C) Argonne National Laboratory
*
* Argonne does not guarantee this software in any manner and is
* not responsible for any damages that may result from its use.
* Furthermore, Argonne does not provide any formal support for this
* software. This is an experimental program. This software
* or any part of it may be freely copied and redistributed,
* provided that this paragraph is included in each source file.
*
*/
/*
* formula.c
*
* This file has routines to input and output quantified formulas and
* to convert them to lists of clauses (Skolemization and CNF translation).
*
* Well-formed-formulas:
* - an atom is a WFF
* - if F is a non-negated WFF, then -F is a WFF.
* - if F and G are WFFs, then (F <-> G) and (F -> G) are WFFs.
* - if F1, ..., Fn are WFFs, then (F1 & ... & Fn) and (F1 | ... | Fn)
* are WFFs.
* - if F is a WFF, Q1 ... Qn are quantifiers ("all" or "exists"),
* and X1 ... Xn are symbols, then (Q1 X1 ... Qn Xn F) is a WFF.
*
* Note, double negations are not allowed, and all parentheses must be
* included: --F is not a WFF, and (A & B -> C) is not a WFF.
*
*/
/*
* In the first implementation, formulas were stored as terms.
* Later, a formula type was introduced, but I never got around
* to updating the parsing routine str_term. Therefore, formulas
* are input to terms, then translated to the formula type in
* term_to_formula below.
*
*/
#include "header.h"
#define F_TYPE fpa_id /* for when formulas as kept as terms during input */
#define EQ_FORM 100 /* for when formulas as kept as terms during input */
static int Sk_func_num, Sk_const_num; /* for creating new skolem symbols */
/*************
*
* print_formula(fp, t) -- print a formula to a file.
*
*************/
void print_formula(fp, f)
FILE *fp;
struct formula *f;
{
char op[MAX_NAME];
struct formula *f1;
if (f == NULL)
fprintf(fp, "(nil)");
else if (f->type == ATOM_FORM) {
print_term(fp, f->t);
}
else if (f->type == NOT_FORM) {
fprintf(fp, "-");
print_formula(fp, f->first_child);
}
else if (f->type == AND_FORM && f->first_child == NULL)
fprintf(fp, "TRUE");
else if (f->type == OR_FORM && f->first_child == NULL)
fprintf(fp, "FALSE");
else if (f->type == QUANT_FORM) {
fprintf(fp, "(");
if (f->quant_type == ALL_QUANT)
fprintf(fp, "all ");
else
fprintf(fp, "exists ");
print_term(fp, f->t);
fprintf(fp, " ");
print_formula(fp, f->first_child);
fprintf(fp, ")");
}
else {
if (f->type == AND_FORM)
str_copy("& ", op);
else if (f->type == OR_FORM)
str_copy("| ", op);
else if (f->type == IMP_FORM)
str_copy("-> ", op);
else if (f->type == IFF_FORM)
str_copy("<-> ", op);
else
op[0] = '\0';
fprintf(fp, "(");
for (f1 = f->first_child; f1; f1 = f1->next) {
print_formula(fp, f1);
if (f1->next)
fprintf(fp, " %s", op);
}
fprintf(fp, ")");
}
} /* print_formula */
/*************
*
* p_formula(f) -- print formula to standard output
*
*************/
void p_formula(f)
struct formula *f;
{
print_formula(Fdout, f);
} /* p_formula */
/*************
*
* static void str_print_formula(str, ip, f)
*
* Print a formula to a string and count the length of the string.
*
*************/
static void str_print_formula(str, ip, f)
char *str;
int *ip;
struct formula *f;
{
char op[MAX_NAME];
struct formula *f1;
if (f == NULL) {
str_copy("(nil)", str+*ip);
*ip += 5;
}
else if (f->type == ATOM_FORM) {
str_print_term(str, ip, f->t);
}
else if (f->type == NOT_FORM) {
str_copy("-", str+*ip);
*ip += 1;
str_print_formula(str, ip, f->first_child);
}
else if (f->type == AND_FORM && f->first_child == NULL) {
str_copy("TRUE", str+*ip);
*ip += 4;
}
else if (f->type == OR_FORM && f->first_child == NULL) {
str_copy("FALSE", str+*ip);
*ip += 5;
}
else if (f->type == QUANT_FORM) {
str_copy("(", str+*ip);
*ip += 1;
if (f->quant_type == ALL_QUANT) {
str_copy("all ", str+*ip);
*ip += 4;
}
else {
str_copy("exists ", str+*ip);
*ip += 7;
}
str_print_term(str, ip, f->t);
str_copy(" ", str+*ip);
*ip += 1;
str_print_formula(str, ip, f->first_child);
str_copy(")", str+*ip);
*ip += 1;
}
else {
if (f->type == AND_FORM)
str_copy(" & ", op);
else if (f->type == OR_FORM)
str_copy(" | ", op);
else if (f->type == IMP_FORM)
str_copy(" -> ", op);
else if (f->type == IFF_FORM)
str_copy(" <-> ", op);
else
op[0] = '\0';
str_copy("(", str+*ip);
*ip += 1;
for (f1 = f->first_child; f1; f1 = f1->next) {
str_print_formula(str, ip, f1);
if (f1->next) {
str_copy(op, str+*ip);
*ip += strlen(op);
}
}
str_copy(")", str+*ip);
*ip += 1;
}
} /* str_print_formula */
/*************
*
* int sprint_formula(s, f) -- return length of s.
*
*************/
int sprint_formula(s, f)
char *s;
struct formula *f;
{
int i;
i = 0;
str_print_formula(s, &i, f);
s[i] = '\0';
return(i);
} /* sprint_formula */
/*************
*
* static int str_form_term(buf, bufp, sft)
* convert part of a string
* representing a formula into a term.
*
* After it is parsed into a term, it will be translated to a formula type.
*
* *bufp is an integer giving the current position in the string.
* *bufp is updated by this routine.
*
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. The pointer to struct term is returned through the
* parameter sft.
*
*************/
static int str_form_term(buf, bufp, sft)
char buf[];
int *bufp;
struct term **sft;
{
struct term *t1, *t2;
struct rel *r1, *r2;
char word[MAX_NAME], *s;
int i, save_pos, sign, formula_type, quant_done;
int tempint;
*sft = NULL; /* default */
skip_white(buf, bufp);
if (buf[*bufp] == '-' && buf[(*bufp)+1] != '>') {
sign = 0;
(*bufp)++;
skip_white(buf, bufp);
if (buf[*bufp] == '-') {
fprintf(Fdout, "ERROR, double negation:\n");
print_error(Fdout, buf, *bufp);
*sft = NULL;
return(NO_TROUBLE);
}
}
else
sign = 1;
if (buf[*bufp] == '(') {
(*bufp)++; /* skip past open paren */
i = 0; /* count arguments */
formula_type = 0;
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = COMPLEX;
r1 = NULL;
save_pos = *bufp; /* in case of error */
while (buf[*bufp] != ')') {
i++;
save_pos = *bufp;
skip_white(buf, bufp);
/* '|' must be handled specially, because it is a delimiter */
if (buf[*bufp] == '|' && buf[(*bufp) + 1] == ' ') {
(*bufp)++; /* skip past '|' */
skip_white(buf, bufp);
if (get_term(&t2) == TROUBLE)
return(TROUBLE);
t2->type = NAME;
if (str_to_sn("|", 0, &tempint) == TROUBLE)
return(TROUBLE);
t2->sym_num = tempint;
}
else
{
if (str_form_term(buf, bufp, &t2) == TROUBLE)
return(TROUBLE);
}
if (t2 == NULL)
{
*sft = NULL;
return(NO_TROUBLE); /* an error has already been handled */
}
else {
s = sn_to_str(t2->sym_num);
if (i == 1) {
if (t2->type == NAME && (str_ident(s,"all") || str_ident(s,"exists"))) {
formula_type = QUANT_FORM;
quant_done = 0;
}
}
else if (formula_type == QUANT_FORM) {
if (quant_done) {
fprintf(Fdout, "ERROR, bad quantified formula:\n");
print_error(Fdout, buf, save_pos);
*sft = NULL;
return(NO_TROUBLE);
}
else if (i % 2 == 0 && t2->type != NAME) {
fprintf(Fdout, "ERROR, variable name expected:\n");
print_error(Fdout, buf, save_pos);
*sft = NULL;
return(NO_TROUBLE);
}
else if (i % 2 == 1 && (str_ident(s,"all") == 0 && str_ident(s,"exists") == 0))
quant_done = 1;
}
else if (i == 2)
{ /* must be AND, OR, IMP, or IFF, or =, or != */
if (str_ident(s, "&"))
formula_type = AND_FORM;
else if (str_ident(s, "|"))
formula_type = OR_FORM;
else if (str_ident(s, "->"))
formula_type = IMP_FORM;
else if (str_ident(s, "<->"))
formula_type = IFF_FORM;
else if (str_ident(s, "="))
formula_type = EQ_FORM;
else if (str_ident(s, "!=")) {
formula_type = EQ_FORM;
sign = (sign ? 0 : 1);
}
if (formula_type == 0 || t2->type != NAME) {
fprintf(Fdout, "ERROR, logical operator, '=', or '!=' expected:\n");
print_error(Fdout, buf, save_pos);
*sft = NULL;
return(NO_TROUBLE);
}
}
else if (i > 3 && (formula_type == IMP_FORM ||
formula_type == IFF_FORM ||
formula_type == EQ_FORM)) {
fprintf(Fdout, "ERROR, too many arguments:\n");
print_error(Fdout, buf, save_pos);
*sft = NULL;
return(NO_TROUBLE);
}
else if (i % 2 == 0) {
if ((formula_type == AND_FORM && str_ident(s, "&") == 0) ||
(formula_type == OR_FORM && str_ident(s, "|") == 0)) {
fprintf(Fdout, "ERROR, operators switched:\n");
print_error(Fdout, buf, save_pos);
*sft = NULL;
return(NO_TROUBLE);
}
}
/* else ok: AND or OR, and odd-numbered argument */
if (i % 2 == 1 || formula_type == QUANT_FORM) {
if (get_rel(&r2) == TROUBLE)
return(TROUBLE);
r2->argval = t2;
if (r1 == NULL)
t1->farg = r2;
else
r1->narg = r2;
r1 = r2;
}
else
free_term(t2); /* free operator */
}
}
if (i < 3) {
fprintf(Fdout, "ERROR, too few arguments:\n");
print_error(Fdout, buf, save_pos);
*sft = NULL;
return(NO_TROUBLE);
}
else {
(*bufp)++; /* skip past close paren */
skip_white(buf, bufp);
if (formula_type == EQ_FORM)
t1->sym_num = Eq_sym_num;
else
t1->F_TYPE = formula_type;
if (sign)
SET_BIT(t1->bits, SCRATCH_BIT);
else
CLEAR_BIT(t1->bits, SCRATCH_BIT);
*sft = t1;
return(NO_TROUBLE);
}
}
else {
i = *bufp;
while (is_delim(buf[i]) == 0)
i++;
if (buf[i] == ' ') { /* next thing is not an atom with arguments */
get_word(buf, bufp, word);
if (word[0] == '\0') {
fprintf(Fdout, "ERROR, bad word:\n");
print_error(Fdout, buf, *bufp);
*sft = NULL;
return(NO_TROUBLE);
}
else {
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
if (str_to_sn(word, 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
if (sign)
SET_BIT(t1->bits, SCRATCH_BIT);
else
CLEAR_BIT(t1->bits, SCRATCH_BIT);
*sft = t1;
return(NO_TROUBLE);
}
}
else { /* next thing is atom with arguments */
if (str_term(buf, bufp, &t1) == TROUBLE)
return(TROUBLE);
if (t1 == NULL)
{
*sft = NULL;
return(NO_TROUBLE);
}
else {
if (sign)
SET_BIT(t1->bits, SCRATCH_BIT);
else
CLEAR_BIT(t1->bits, SCRATCH_BIT);
*sft = t1;
return(NO_TROUBLE);
}
}
}
} /* str_form_term */
/*************
*
* static int term_to_formula(t, ttf)
*
* struct formula * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula through the
* parameter ttf.
*
*************/
static int term_to_formula(t, ttf)
struct term *t;
struct formula **ttf;
{
struct formula *f1, *f2, *f3;
struct rel *r;
struct formula *temp;
struct term *tempterm;
*ttf = NULL; /* default */
/* handle negation at end */
if (t->F_TYPE == 0) {
if (get_formula(&f1) == TROUBLE)
return(TROUBLE);
f1->type = ATOM_FORM;
if (copy_term(t, &tempterm) == TROUBLE)
return(TROUBLE);
f1->t = tempterm;
f1->t->varnum = NORM_ATOM;
}
else if (t->F_TYPE == QUANT_FORM) {
r = t->farg;
f1 = f3 = NULL;
while (r->narg) {
if (get_formula(&f2) == TROUBLE)
return(TROUBLE);
f2->type = QUANT_FORM;
if (f3)
f3->first_child = f2;
else
f1 = f2;
f3 = f2;
if (str_ident(sn_to_str(r->argval->sym_num), "all"))
f2->quant_type = ALL_QUANT;
else
f2->quant_type = EXISTS_QUANT;
/* copy variable */
r = r->narg;
if (copy_term(r->argval, &tempterm) == TROUBLE)
return(TROUBLE);
f2->t = tempterm;
r = r->narg;
}
if (f3)
{
if (term_to_formula(r->argval, &temp) == TROUBLE)
return(TROUBLE);
f3->first_child = temp;
}
else
{
if (term_to_formula(r->argval, &f1) == TROUBLE)
/* no quantifiers */ return(TROUBLE);
}
}
else {
if (get_formula(&f1) == TROUBLE)
return(TROUBLE);
f1->type = t->F_TYPE;
f3 = NULL;
for (r = t->farg; r; r = r->narg) {
if (term_to_formula(r->argval, &f2) == TROUBLE)
return(TROUBLE);
if (f3)
f3->next = f2;
else
f1->first_child = f2;
f3 = f2;
}
}
if ( ! TP_BIT(t->bits, SCRATCH_BIT) )
{
if (negate_formula(f1,&temp) == TROUBLE)
return(TROUBLE);
f1 = temp;
}
*ttf = f1;
return(NO_TROUBLE);
} /* term_to_formula */
/*************
*
* int str_formula(buf, bufp, sf) -- convert a string
* into a formula.
*
* *bufp is an integer giving the current position in the string.
* *bufp is updated by this routine.
*
* struct formula * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula through the
* parameter sf.
*
*************/
int str_formula(buf, bufp, sf)
char buf[];
int *bufp;
struct formula **sf;
{
struct term *t;
struct formula *f;
*sf = NULL; /* default */
if (str_form_term(buf, bufp, &t) == TROUBLE)
return(TROUBLE);
if (t)
{
if (term_to_formula(t, &f) == TROUBLE)
return(TROUBLE);
}
else
f = NULL;
*sf = f;
return(NO_TROUBLE);
} /* str_formula */
/*************
*
* int read_formula(fp, rcp, rf) -- read a formula from a file
*
* The return code *rcp:
* 0 - an error was encountered and reported; NULL is returned.
* 1 - OK; if EOF was found instead of a formula, NULL is returned.
*
* struct formula * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula through the
* parameter rf.
*
*************/
int read_formula(fp, rcp, rf)
FILE *fp;
int *rcp;
struct formula **rf;
{
char buf[MAX_BUF];
int p, rc;
struct formula *f;
struct term *t;
*rf = NULL; /* default */
rc = read_buf(fp, buf);
if (rc == 0) { /* error */
*rcp = 0;
return(NO_TROUBLE);
}
else if (buf[0] == '\0') { /* ok. EOF */
*rcp = 1;
return(NO_TROUBLE);
}
else {
p = 0;
if (str_form_term(buf, &p, &t) == TROUBLE)
return(TROUBLE);
if (t == NULL) {
*rcp = 0;
return(NO_TROUBLE);
}
else {
skip_white(buf, &p);
if (buf[p] != '.') {
fprintf(Fdout, "ERROR, text after formula:\n");
print_error(Fdout, buf, p);
*rcp = 0;
return(NO_TROUBLE);
}
if (term_to_formula(t, &f) == TROUBLE)
return(TROUBLE);
if (contains_skolem_symbol(t)) {
fprintf(Fdout, "\nERROR, input formula contains Skolem symbol:\n");
print_formula(Fdout, f);
fprintf(Fdout,".\n\n");
zap_formula(f);
*rcp = 0;
return(NO_TROUBLE);
}
else {
if (term_to_formula(t, &f) == TROUBLE)
return(TROUBLE);
zap_term(t);
*rcp = 1;
*rf = f;
return(NO_TROUBLE);
}
}
}
} /* read_formula */
/*************
*
* int read_formula_list(file_ptr, errors_ptr, rfl)
*
* Read and return a list of quantified formulas.
*
* The list must be terminated either with the term `end_of_list.'
* or with an actual EOF.
* Set errors_ptr to point to the number of errors found.
*
* struct formula_ptr * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula_ptr through the
* parameter rfl.
*
*************/
int read_formula_list(fp, ep, rfl)
FILE *fp;
int *ep;
struct formula_ptr **rfl;
{
struct formula_ptr *p1, *p2, *p3;
struct formula *f;
int rc;
*rfl = NULL; /* default */
*ep = 0;
p3 = NULL;
p2 = NULL;
if (read_formula(fp, &rc, &f) == TROUBLE)
return(TROUBLE);
while (rc == 0) {
(*ep)++;
if (read_formula(fp, &rc, &f) == TROUBLE)
return(TROUBLE);
}
/* keep going until f == NULL || f is end marker */
while (f != NULL && (f->type != ATOM_FORM ||
str_ident(sn_to_str(f->t->sym_num), "end_of_list") == 0)) {
if (get_formula_ptr(&p1) == TROUBLE)
return(TROUBLE);
p1->f = f;
if (p2 == NULL)
p3 = p1;
else
p2->next = p1;
p2 = p1;
if (read_formula(fp, &rc, &f) == TROUBLE)
return(TROUBLE);
while (rc == 0) {
(*ep)++;
if (read_formula(fp, &rc, &f) == TROUBLE)
return(TROUBLE);
}
}
if (f != NULL)
zap_formula(f);
*rfl = p3;
return(NO_TROUBLE);
} /* read_formula_list */
/*************
*
* print_formula_list(file_ptr, term_ptr) -- Print a list of quantified formulas.
*
* The list is printed with periods after each quantified formula, and
* the list is terminated with `end_of_list.' so that it can
* be read with read_formula_list.
*
*************/
void print_formula_list(fp, p)
FILE *fp;
struct formula_ptr *p;
{
while (p != NULL) {
print_formula(fp, p->f); fprintf(fp, ".\n");
p = p->next;
}
fprintf(fp, "end_of_list.\n");
} /* print_formula_list */
/*************
*
* int copy_formula(f, cf)
*
* Copy a formula. copy_term is used to copy atoms and quantified vars.
*
* struct formula * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula through the
* parameter cf.
*
*************/
int copy_formula(f, cf)
struct formula *f;
struct formula **cf;
{
struct formula *f_new, *f_sub, *f_prev, *f3;
struct term *tempterm;
struct formula *temp;
*cf = NULL; /* default */
if (get_formula(&f_new) == TROUBLE)
return(TROUBLE);
f_new->type = f->type;
if (f->type == ATOM_FORM)
{
if (copy_term(f->t, &tempterm) == TROUBLE)
return(TROUBLE);
f_new->t = tempterm;
}
else if (f->type == QUANT_FORM) {
f_new->quant_type = f->quant_type;
if (copy_term(f->t, &tempterm) == TROUBLE)
return(TROUBLE);
f_new->t = tempterm;
if (copy_formula(f->first_child, &temp) == TROUBLE)
return(TROUBLE);
f_new->first_child = temp;
}
else {
f_prev = NULL;
for (f_sub = f->first_child; f_sub; f_sub = f_sub->next) {
if (copy_formula(f_sub, &f3) == TROUBLE)
return(TROUBLE);
if (f_prev)
f_prev->next = f3;
else
f_new->first_child = f3;
f_prev = f3;
}
}
*cf = f_new;
return(NO_TROUBLE);
} /* copy_formula */
/*************
*
* void zap_formula(f)
*
* Free a formula and all of its subformulas and subterms.
*
*************/
void zap_formula(f)
struct formula *f;
{
struct formula *f1, *f2;
if (f->type == ATOM_FORM)
zap_term(f->t);
else {
f1 = f->first_child;
while (f1) {
f2 = f1;
f1 = f1->next;
zap_formula(f2);
}
if (f->type == QUANT_FORM)
zap_term(f->t);
}
free_formula(f);
} /* zap_formula */
/*************
*
* int negate_formula(f, nf)
*
* f is changed to its negation. (Do not move negation signs inward.)
*
* struct formula * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula through the
* parameter nf.
*
*************/
int negate_formula(f, nf)
struct formula *f;
struct formula **nf;
{
struct formula *f1, *f_save;
*nf = NULL; /* default */
/* save next pointer */
f_save = f->next; f->next = NULL;
if (f->type == NOT_FORM) {
f1 = f->first_child;
free_formula(f);
}
else {
if (get_formula(&f1) == TROUBLE)
return(TROUBLE);
f1->type = NOT_FORM;
f1->first_child = f;
}
/* restore next pointer */
f1->next = f_save;
*nf = f1;
return(NO_TROUBLE);
} /* negate_formula */
/*************
*
* int nnf(f, nf)
*
* f is changed into its negation normal form (NNF) by removing
* -> and <-> and moving negation signs all the way in.
*
* (A <-> B) (not negated) rewrites to ((-a | b) & (-b | a)).
* -(A <-> B) rewrites to ((a | b) & (-a | -b)).
*
* because conjunctions are favored.
*
* struct formula * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct formula through the
* parameter nf.
*
*************/
int nnf(f, nf)
struct formula *f;
struct formula **nf;
{
struct formula *f1, *f2, *next, *prev, *fn;
struct formula *temp, *temp2;
*nf = NULL; /* default */
switch (f->type) {
case ATOM_FORM:
*nf = f;
return(NO_TROUBLE); /* f is atomic */
case IFF_FORM:
if (get_formula(&f1) == TROUBLE)
return(TROUBLE);
f1->type = AND_FORM;
f1->first_child = f;
f1->next = f->next;
if (copy_formula(f, &f2) == TROUBLE)
return(TROUBLE);
f2->type = OR_FORM;
if (negate_formula(f2->first_child->next,&temp) == TROUBLE)
return(TROUBLE);
f2->first_child->next = temp;
f->type = OR_FORM;
if (negate_formula(f->first_child,&temp) == TROUBLE)
return(TROUBLE);
f->first_child = temp;
f->next = f2;
if (nnf(f1,&temp) == TROUBLE)
return(TROUBLE);
*nf = temp;
return(NO_TROUBLE);
case IMP_FORM:
f->type = OR_FORM;
if (negate_formula(f->first_child,&temp) == TROUBLE)
return(TROUBLE);
f->first_child = temp;
if (nnf(f,&temp) == TROUBLE)
return(TROUBLE);
*nf = temp;
return(NO_TROUBLE);
case QUANT_FORM:
if (nnf(f->first_child,&temp) == TROUBLE)
return(TROUBLE);
f->first_child = temp;
*nf = f;
return(NO_TROUBLE);
case AND_FORM:
case OR_FORM:
prev = NULL;
f1 = f->first_child;
while(f1) {
next = f1->next; f1->next = NULL;
if (nnf(f1,&f2) == TROUBLE)
return(TROUBLE);
if (prev)
prev->next = f2;
else
f->first_child = f2;
prev = f2;
f1 = next;
}
*nf = f;
return(NO_TROUBLE);
case NOT_FORM:
fn = f->first_child;
switch (fn->type) {
case ATOM_FORM:
*nf = f;
return(NO_TROUBLE);
case IFF_FORM:
if (copy_formula(fn, &f2) == TROUBLE)
return(TROUBLE);
f2->type = OR_FORM;
fn->type = OR_FORM;
if (negate_formula(f2->first_child,&temp) == TROUBLE)
return(TROUBLE);
f2->first_child = temp;
if (negate_formula(f2->first_child->next,&temp) == TROUBLE)
return(TROUBLE);
f2->first_child->next = temp;
fn->next = f2;
f->type = AND_FORM;
f->first_child = fn;
if (nnf(f,&temp) == TROUBLE)
return(TROUBLE);
*nf = temp;
return(NO_TROUBLE);
case IMP_FORM:
fn->type = OR_FORM;
if (negate_formula(fn->first_child,&temp) == TROUBLE)
return(TROUBLE);
fn->first_child = temp;
if (nnf(f,&temp) == TROUBLE)
return(TROUBLE);
*nf = temp;
return(NO_TROUBLE);
case QUANT_FORM:
fn->quant_type = (fn->quant_type == ALL_QUANT ? EXISTS_QUANT : ALL_QUANT);
if (negate_formula(fn->first_child,&temp) == TROUBLE)
return(TROUBLE);
if (nnf(temp,&temp2) == TROUBLE)
return(TROUBLE);
fn->first_child = temp2;
fn->next = f->next;
free_formula(f);
*nf = fn;
return(NO_TROUBLE);
case AND_FORM:
case OR_FORM:
prev = NULL;
f1 = fn->first_child;
while(f1) {
next = f1->next; f1->next = NULL;