-
Notifications
You must be signed in to change notification settings - Fork 337
/
Copy pathcnf.c
4787 lines (4250 loc) · 162 KB
/
cnf.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
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * CNF TRANSLATOR * */
/* * * */
/* * $Module: CNF * */
/* * * */
/* * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 * */
/* * MPI fuer Informatik * */
/* * * */
/* * This program is free software; you can redistribute * */
/* * it and/or modify it under the terms of the GNU * */
/* * General Public License as published by the Free * */
/* * Software Foundation; either version 2 of the License, * */
/* * or (at your option) any later version. * */
/* * * */
/* * This program is distributed in the hope that it will * */
/* * be useful, but WITHOUT ANY WARRANTY; without even * */
/* * the implied warranty of MERCHANTABILITY or FITNESS * */
/* * FOR A PARTICULAR PURPOSE. See the GNU General Public * */
/* * License for more details. * */
/* * * */
/* * You should have received a copy of the GNU General * */
/* * Public License along with this program; if not, write * */
/* * to the Free Software Foundation, Inc., 59 Temple * */
/* * Place, Suite 330, Boston, MA 02111-1307 USA * */
/* * * */
/* * * */
/* $Revision$ * */
/* $State$ * */
/* $Date$ * */
/* $Author$ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: weidenb@mpi-sb.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/* $RCSfile$ */
#include "cnf.h"
#include "rules-inf.h"
#include "rules-red.h"
static TERM cnf_AntiPrenexPath(TERM, TERM);
static TERM cnf_ApplyDefinitionInternOnce(TERM, TERM, TERM, TERM, BOOL*);
static SYMBOL cnf_GetDualSymbol(SYMBOL symbol);
static TERM cnf_IsDefinition(TERM);
static void cnf_OptimizedSkolemFormula(PROOFSEARCH, TERM, char*, BOOL, TERM,
LIST*, LIST*, BOOL, HASH, int);
static int cnf_PredicateOccurrences(TERM, SYMBOL);
static void cnf_RplacVar(TERM, LIST, LIST);
static LIST cnf_SatUnit(PROOFSEARCH, LIST);
static LIST cnf_SkolemFunctionFormula(TERM, LIST, LIST, PRECEDENCE);
/* For every variable the depth in the current term, required for */
/* strong skolemization */
static int* cnf_VARIABLEDEPTHARRAY;
/* Holds a copy of the ProofSearch--Object built by cnf_Flotter */
/* during cnf_QueryFlotter */
static PROOFSEARCH cnf_SEARCHCOPY;
/* Proofsearch--Object for the function cnf_HaveProof. We need this */
/* to reduce the number of term stamps required. */
static PROOFSEARCH cnf_HAVEPROOFPS;
void cnf_Init(FLAGSTORE Flags)
/***************************************************************
INPUT: A flag store.
RETURNS: None.
SUMMARY: Initializes the CNF Module.
EFFECTS: Initializes global variables.
CAUTION: MUST BE CALLED BEFORE ANY OTHER CNF-FUNCTION.
***************************************************************/
{
/* If strong skolemization is performed, allocate array for variable depth */
if (flag_GetFlagValue(Flags, flag_CNFSTRSKOLEM))
cnf_VARIABLEDEPTHARRAY = (int*) memory_Malloc(sizeof(int[symbol__MAXSTANDARDVAR + 1]));
else
cnf_VARIABLEDEPTHARRAY = NULL;
cnf_SEARCHCOPY = prfs_Create();
cnf_HAVEPROOFPS = prfs_Create();
}
void cnf_Free(FLAGSTORE Flags)
/**************************************************************
INPUT: A flag store.
RETURNS: None.
SUMMARY: Frees the CNF Module.
***************************************************************/
{
/* If strong skolemization is performed, free array for variable depth */
if (flag_GetFlagValue(Flags, flag_CNFSTRSKOLEM)) {
memory_Free(cnf_VARIABLEDEPTHARRAY, sizeof(int) * (symbol__NOOFSTANDARDVAR + 1));
cnf_VARIABLEDEPTHARRAY = NULL;
}
prfs_Delete(cnf_SEARCHCOPY);
cnf_SEARCHCOPY = NULL;
prfs_Delete(cnf_HAVEPROOFPS);
cnf_HAVEPROOFPS = NULL;
}
static int cnf_GetFormulaPolarity(TERM term, TERM subterm)
/**********************************************************
INPUT: Two terms term and subterm where subterm is a
subterm of term.
RETURNS: The polarity of subterm in term.
********************************************************/
{
LIST scan;
TERM term1;
int polterm1,bottom;
bottom = vec_ActMax();
vec_Push((POINTER) 1);
vec_Push(term);
do {
term1 = (TERM)vec_PopResult();
polterm1 = (int)vec_PopResult();
if (term1 == subterm) {
vec_SetMax(bottom);
return polterm1;
}else
if (symbol_Equal(term_TopSymbol(term1),fol_Not())) {
vec_Push((POINTER) (- polterm1));
vec_Push(list_Car(term_ArgumentList(term1)));
}
if (symbol_Equal(term_TopSymbol(term1),fol_Exist()) ||
symbol_Equal(term_TopSymbol(term1),fol_All())) {
vec_Push((POINTER)polterm1);
vec_Push(list_Second(term_ArgumentList(term1)));
}
else
if (symbol_Equal(term_TopSymbol(term1),fol_Implies())) {
vec_Push((POINTER) (- polterm1));
vec_Push(list_Car(term_ArgumentList(term1)));
vec_Push((POINTER) polterm1);
vec_Push(list_Second(term_ArgumentList(term1)));
}
else
if (symbol_Equal(term_TopSymbol(term1),fol_Equiv())) {
vec_Push(0);
vec_Push(list_Car(term_ArgumentList(term1)));
vec_Push(0);
vec_Push(list_Second(term_ArgumentList(term1)));
}
else
if (symbol_Equal(term_TopSymbol(term1),fol_And()) ||
symbol_Equal(term_TopSymbol(term1),fol_Or())) {
for (scan = term_ArgumentList(term1);
!list_Empty(scan);
scan = list_Cdr(scan)) {
vec_Push((POINTER) polterm1);
vec_Push(list_Car(scan));
}
}
} while (bottom != vec_ActMax());
vec_SetMax(bottom);
misc_StartErrorReport();
misc_ErrorReport("\n In cnf_GetFormulaPolarity: Wrong arguments !\n");
misc_FinishErrorReport();
return -2;
}
static BOOL cnf_ContainsDefinitionIntern(TERM TopDef, TERM Def, int Polarity,
TERM* FoundPred)
/**********************************************************
INPUT: A term TopDef which is the top level term of the recursion.
A term Def which is searched for a definition.
A pointer to a term into which the predicate of the definition
is stored if it is found.
RETURNS: TRUE if Def contains a definition that can be converted
to standard form.
********************************************************/
{
/* AND / OR */
/* In these cases Def cannot be converted to standard form */
if ((symbol_Equal(term_TopSymbol(Def),fol_And()) && (Polarity == 1)) ||
(symbol_Equal(term_TopSymbol(Def),fol_Or()) && (Polarity == -1)))
return FALSE;
if (symbol_Equal(term_TopSymbol(Def), fol_And()) ||
symbol_Equal(term_TopSymbol(Def),fol_Or())) {
/* Polarity is ok */
LIST l;
for (l=term_ArgumentList(Def); !list_Empty(l); l=list_Cdr(l))
if (cnf_ContainsDefinitionIntern(TopDef, list_Car(l), Polarity, FoundPred))
return TRUE;
return FALSE;
}
/* Quantifiers */
if (fol_IsQuantifier(term_TopSymbol(Def)))
return cnf_ContainsDefinitionIntern(TopDef, term_SecondArgument(Def), Polarity, FoundPred);
/* Negation */
if (symbol_Equal(term_TopSymbol(Def),fol_Not()))
return cnf_ContainsDefinitionIntern(TopDef, term_FirstArgument(Def), -Polarity, FoundPred);
/* Implication */
if (symbol_Equal(term_TopSymbol(Def),fol_Implies())) {
if (Polarity==1) {
if (cnf_ContainsDefinitionIntern(TopDef, term_FirstArgument(Def), -Polarity, FoundPred))
return TRUE;
return cnf_ContainsDefinitionIntern(TopDef, term_SecondArgument(Def), Polarity, FoundPred);
}
return FALSE;
}
/* Equivalence */
if (symbol_Equal(term_TopSymbol(Def),fol_Equiv()) && (Polarity==1)) {
/* Check if equivalence itself is in correct form */
TERM defpredicate;
defpredicate = cnf_IsDefinition(Def);
if (defpredicate != (TERM) NULL) {
LIST predicate_vars, l, defpath;
BOOL allquantifierfound;
TERM super;
int pol;
/* Check if predicate occurs several times in TopDef */
/* if (cnf_PredicateOccurrences(TopDef, term_TopSymbol(defpredicate)) > 1) {}
puts("\n Predicate occurs more than once.");
return FALSE; */
/* Now make sure that the variables of the predicate are */
/* all--quantified and not in the scope of an exist quantifier */
/* predicate_vars = list_Copy(term_ArgumentList(defpredicate)); */
predicate_vars = term_ListOfVariables(defpredicate);
predicate_vars = term_DeleteDuplicatesFromList(predicate_vars);
/* So far (going bottom--up) no all-quantifier was found for */
/* a variable of the predicates' arguments */
allquantifierfound = FALSE;
/* Build defpath here by going bottom up */
/* At first, list of superterms on path is top down */
defpath = list_Nil();
super = Def;
while (super != (TERM) NULL) {
defpath = list_Cons(super, defpath);
super = term_Superterm(super);
}
/* No go top down and add polarities */
pol = 1;
for (l=defpath; !list_Empty(l); l=list_Cdr(l)) {
list_Rplaca(l, list_PairCreate((TERM) list_Car(l), (LIST) pol));
if (symbol_Equal(term_TopSymbol((TERM) list_Car(l)), fol_Not()))
pol = -pol;
else {
if (symbol_Equal(term_TopSymbol((TERM) list_Car(l)), fol_Implies()) &&
(term_FirstArgument((TERM) list_Car(l)) == (TERM) list_Car(list_Cdr(l))))
pol = -pol;
else
if (symbol_Equal(term_TopSymbol((TERM) list_Car(l)), fol_Equiv()))
pol = 0;
}
}
/* <defpath> is now a list of pairs (term, polarity) */
for (l=defpath; !list_Empty(l) && !list_Empty(predicate_vars); l=list_Cdr(l)) {
LIST pair;
TERM t;
int p;
/* Pair Term t / Polarity p */
pair = (LIST) list_Car(l);
t = (TERM) list_PairFirst(pair);
p = (int) list_PairSecond(pair);
if (fol_IsQuantifier(term_TopSymbol(t))) {
/* Variables of the predicate that are universally quantified are no problem */
if ((symbol_Equal(term_TopSymbol(t), fol_All()) && (p==1)) ||
(symbol_Equal(term_TopSymbol(t), fol_Exist()) && (p==-1))) {
LIST scan;
allquantifierfound = TRUE;
for (scan=fol_QuantifierVariables(t); !list_Empty(scan); scan=list_Cdr(scan))
predicate_vars = list_DeleteElement(predicate_vars,(TERM) list_Car(scan),
(BOOL (*)(POINTER,POINTER))term_Equal);
}
else {
/* Check if allquantified variables of the predicate are in scope of an exist--quantifier */
/* We already found an all quantified variable */
if (allquantifierfound) {
list_Delete(predicate_vars);
list_DeletePairList(defpath);
return FALSE;
}
else {
LIST scan;
/* Check if a variable of the predicate is exist--quantified */
for (scan=fol_QuantifierVariables(t); !list_Empty(scan); scan=list_Cdr(scan)) {
if (term_ListContainsTerm(predicate_vars, list_Car(scan))) {
list_Delete(predicate_vars);
list_DeletePairList(defpath);
return FALSE;
}
}
}
}
}
}
#ifdef CHECK
if (!list_Empty(predicate_vars)) {
list_Delete(predicate_vars);
misc_StartErrorReport();
misc_ErrorReport("\n In cnf_ContainsDefinitionIntern: Definition has free variables.\n");
misc_FinishErrorReport();
}
#endif
list_DeletePairList(defpath);
*FoundPred = defpredicate;
return TRUE;
}
}
return FALSE;
}
BOOL cnf_ContainsDefinition(TERM Def, TERM* FoundPred)
/**********************************************************
INPUT: A term Def which is searched for a definition of a predicate.
A pointer to a term into which the predicate of the definition
is stored if it is found.
RETURNS: TRUE if Def contains a definition that can be converted to
standard form.
***********************************************************/
{
BOOL result;
#ifdef CHECK
fol_CheckFatherLinks(Def);
#endif
result = cnf_ContainsDefinitionIntern(Def, Def, 1, FoundPred);
#ifdef CHECK
fol_CheckFatherLinks(Def);
#endif
return result;
}
static TERM cnf_IsDefinition(TERM Def)
/**********************************************************
INPUT: A term Def.
RETURNS: The Def term where the arguments of the equivalence are exchanged
iff the first one is not a predicate.
**********************************************************/
{
LIST l,freevars, predicatevars;
#ifdef CHECK
if (Def == NULL) {
misc_StartErrorReport();
misc_ErrorReport("\n In cnf_IsDefinition: Empty formula.\n");
misc_FinishErrorReport();
}
if (!symbol_Equal(term_TopSymbol(Def),fol_Equiv())) {
misc_StartErrorReport();
misc_ErrorReport("\n In cnf_IsDefinition: Formula is no equivalence.\n");
misc_FinishErrorReport();
}
#endif
/* If the predicate is the second argument of the equivalence, exchange them */
if (!symbol_IsPredicate(term_TopSymbol(term_FirstArgument(Def)))) {
TERM arg1;
arg1 = term_FirstArgument(Def);
term_RplacFirstArgument(Def, term_SecondArgument(Def));
term_RplacSecondArgument(Def, arg1);
}
/* Check if the first argument is a predicate */
if (!symbol_IsPredicate(term_TopSymbol(term_FirstArgument(Def))))
return NULL;
/* Check if first argument is a predicate and not fol_Equality */
/* if (!symbol_IsPredicate(term_TopSymbol(term_FirstArgument(Def)))
|| symbol_Equal(term_TopSymbol(term_FirstArgument(Def)), fol_Equality()))) {
return NULL;
}*/
/* The free variables of the non-predicate term must occur in the predicate term */
freevars = fol_FreeVariables(term_SecondArgument(Def));
freevars = term_DeleteDuplicatesFromList(freevars);
predicatevars = term_ListOfVariables(term_FirstArgument(Def));
predicatevars = term_DeleteDuplicatesFromList(predicatevars);
for (l=predicatevars; !list_Empty(l); l=list_Cdr(l))
freevars = list_DeleteElement(freevars, list_Car(l),
(BOOL (*)(POINTER,POINTER))term_Equal);
if (!list_Empty(freevars)) {
list_Delete(freevars);
list_Delete(predicatevars);
return NULL;
}
list_Delete(predicatevars);
return term_FirstArgument(Def);
}
static BOOL cnf_ContainsPredicateIntern(TERM Target, SYMBOL Predicate,
int Polarity, TERM* TargetPredicate,
TERM* ToTopLevel, LIST* TargetVars,
LIST* VarsForTopLevel)
/**********************************************************
INPUT: A term (sub--) Target
A symbol Predicate which is searched in the target term.
The polarity of the subterm.
A pointer to the term TargetPredicate into which the found
predicate term is stored.
A pointer to the list TargetVars into which the variables found
in the predicates' arguments are stored.
A pointer to a list VarsForTopLevel into which all variables are
stored that are all--quantified and can be moved to top level.
RETURNS: TRUE if Formula contains the predicate for which a definition
was found.
********************************************************/
{
/* AND / OR */
/* In these cases the predicate (if it exists) can not be moved to a higher level */
if ((symbol_Equal(term_TopSymbol(Target),fol_And()) && Polarity == 1) ||
(symbol_Equal(term_TopSymbol(Target),fol_Or()) && Polarity == -1) ||
(symbol_Equal(term_TopSymbol(Target),fol_Implies()) && Polarity != 1) ||
symbol_Equal(term_TopSymbol(Target), fol_Equiv())) {
TERM s;
LIST l;
/* Try to find Predicate in Target */
s = term_FindSubterm(Target, Predicate);
if (s == NULL)
return FALSE;
/* Store variables found in the predicates arguments */
for (l=term_ArgumentList(s); !list_Empty(l); l = list_Cdr(l))
*TargetVars = list_Nconc(fol_FreeVariables((TERM) list_Car(l)), *TargetVars);
*TargetVars = term_DeleteDuplicatesFromList(*TargetVars);
/* Keep found predicate */
*TargetPredicate = s;
*ToTopLevel = Target;
return TRUE;
}
/* AND / OR continued */
if (symbol_Equal(term_TopSymbol(Target),fol_And()) || symbol_Equal(term_TopSymbol(Target),fol_Or())) {
/* The polarity is ok here */
LIST l;
for (l=term_ArgumentList(Target); !list_Empty(l); l = list_Cdr(l))
if (cnf_ContainsPredicateIntern((TERM) list_Car(l), Predicate, Polarity,
TargetPredicate, ToTopLevel, TargetVars,
VarsForTopLevel))
return TRUE;
return FALSE;
}
/* Quantifiers */
if (fol_IsQuantifier(term_TopSymbol(Target))) {
if (cnf_ContainsPredicateIntern(term_SecondArgument(Target), Predicate,
Polarity, TargetPredicate, ToTopLevel,
TargetVars, VarsForTopLevel)) {
/* Quantifiers for free variables of the predicate should be moved
to top level to make the proof easier */
if ((symbol_Equal(term_TopSymbol(Target), fol_All()) && Polarity == 1) ||
(symbol_Equal(term_TopSymbol(Target), fol_Exist()) && Polarity == -1)) {
LIST l;
/* Check for all variables found in the predicates arguments */
for (l = *TargetVars; !list_Empty(l); l=list_Cdr(l)) {
if (term_ListContainsTerm(fol_QuantifierVariables(Target),list_Car(l)))
*VarsForTopLevel = list_Cons(list_Car(l), *VarsForTopLevel);
}
}
return TRUE;
}
return FALSE;
}
/* Negation */
if (symbol_Equal(term_TopSymbol(Target),fol_Not()))
return cnf_ContainsPredicateIntern(term_FirstArgument(Target), Predicate,
-Polarity, TargetPredicate, ToTopLevel,
TargetVars, VarsForTopLevel);
/* Implication */
if (symbol_Equal(term_TopSymbol(Target),fol_Implies())) {
/* In this case the predicate (if it exists) can be moved to a higher level */
if (cnf_ContainsPredicateIntern(term_FirstArgument(Target), Predicate,
-Polarity, TargetPredicate, ToTopLevel,
TargetVars, VarsForTopLevel))
return TRUE;
return cnf_ContainsPredicateIntern(term_SecondArgument(Target), Predicate,
Polarity, TargetPredicate, ToTopLevel,
TargetVars, VarsForTopLevel);
}
/* Found the predicate */
if (symbol_Equal(term_TopSymbol(Target), Predicate)) {
LIST l;
for (l = term_ArgumentList(Target); !list_Empty(l); l = list_Cdr(l))
*TargetVars = list_Nconc(fol_FreeVariables((TERM) list_Car(l)), *TargetVars);
*TargetVars = term_DeleteDuplicatesFromList(*TargetVars);
*TargetPredicate = Target;
*ToTopLevel = Target;
return TRUE;
}
/* In all other cases the predicate was not found */
return FALSE;
}
BOOL cnf_ContainsPredicate(TERM Target, SYMBOL Predicate,
TERM* TargetPredicate, TERM* ToTopLevel,
LIST* TargetVars, LIST* VarsForTopLevel)
/**********************************************************
INPUT: A term Target without implications.
A symbol Predicate which is searched in the target term.
A pointer to the predicate found in TargetTerm is recorded.
A pointer to the term TargetPredicate into which the found
predicate term is stored.
A pointer to the list TargetVars into which the variables
found in the predicates' arguments are stored.
A pointer to a list VarsForTopLevel into which all variables
are stored that are all--quantified and can be moved to top level.
RETURNS: TRUE if Formula contains the predicate for which a definition
was found.
********************************************************/
{
BOOL result;
#ifdef CHECK
fol_CheckFatherLinks(Target);
#endif
result = cnf_ContainsPredicateIntern(Target, Predicate, 1, TargetPredicate,
ToTopLevel, TargetVars, VarsForTopLevel);
#ifdef CHECK
fol_CheckFatherLinks(Target);
#endif
return result;
}
static int cnf_PredicateOccurrences(TERM Term, SYMBOL P)
/****************************************************
INPUT: A term and a predicate symbol.
RETURNS: The number of occurrences of the predicate symbol in Term
**************************************************/
{
/* Quantifiers */
if (fol_IsQuantifier(term_TopSymbol(Term)))
return cnf_PredicateOccurrences(term_SecondArgument(Term), P);
/* Junctors and NOT */
if (fol_IsJunctor(term_TopSymbol(Term)) ||
symbol_Equal(term_TopSymbol(Term),fol_Not())) {
LIST scan;
int count;
count = 0;
for (scan=term_ArgumentList(Term); !list_Empty(scan); scan=list_Cdr(scan)) {
count += cnf_PredicateOccurrences(list_Car(scan), P);
/* Only the cases count==1 and count>1 are important */
if (count > 1)
return count;
}
return count;
}
if (symbol_Equal(term_TopSymbol(Term), P))
return 1;
return 0;
}
static TERM cnf_NegationNormalFormulaPath(TERM Term, TERM PredicateTerm)
/**********************************************************
INPUT: A term and a predicate term which is a subterm of term
RETURNS: The negation normal form of the term along the path.
CAUTION: The term is destructively changed.
This works only if the superterm member of Term and its subterms
are set.
********************************************************/
{
TERM subterm, termL, term1;
LIST scan;
SYMBOL symbol;
BOOL set;
term1 = Term;
while (term1 != NULL) {
set = FALSE;
if (symbol_Equal(term_TopSymbol(term1),fol_Not())) {
subterm = term_FirstArgument(term1);
if (symbol_Equal(term_TopSymbol(subterm),fol_Not())) {
LIST l;
term_RplacTop(term1,term_TopSymbol(term_FirstArgument(subterm)));
list_Delete(term_ArgumentList(term1));
term_RplacArgumentList(term1,term_ArgumentList(term_FirstArgument(subterm)));
term_Free(term_FirstArgument(subterm));
list_Delete(term_ArgumentList(subterm));
term_Free(subterm);
/* Set superterm member to new superterm */
for (l=term_ArgumentList(term1); !list_Empty(l); l=list_Cdr(l))
term_RplacSuperterm(list_Car(l), term1);
/* term1 weiter betrachten */
set = TRUE;
}
else {
if (fol_IsQuantifier(term_TopSymbol(subterm))) {
LIST l;
symbol = (SYMBOL)cnf_GetDualSymbol(term_TopSymbol(subterm));
termL = term_CreateAddFather(fol_Not(),
list_List(term_SecondArgument(subterm)));
list_RplacSecond(term_ArgumentList(subterm), termL);
term_RplacSuperterm(termL, subterm);
term_RplacTop(term1,symbol);
list_Delete(term_ArgumentList(term1));
term_RplacArgumentList(term1, term_ArgumentList(subterm));
for (l=term_ArgumentList(term1); !list_Empty(l); l = list_Cdr(l))
term_RplacSuperterm(list_Car(l), term1);
term_RplacArgumentList(subterm, list_Nil());
term_Delete(subterm);
term1 = termL;
/* Next term to check is not(subterm) */
set = TRUE;
}
else {
if (symbol_Equal(term_TopSymbol(subterm),fol_Or()) ||
(symbol_Equal(term_TopSymbol(subterm),fol_And()))) {
LIST l;
symbol = (SYMBOL)cnf_GetDualSymbol(term_TopSymbol(subterm));
for (scan = term_ArgumentList(subterm); !list_Empty(scan);
scan = list_Cdr(scan)) {
TERM new;
termL = list_Car(scan);
new = term_CreateAddFather(fol_Not(),list_List(termL));
list_Rplaca(scan, new);
term_RplacSuperterm(new, subterm);
}
term_RplacTop(term1,symbol);
list_Delete(term_ArgumentList(term1));
term_RplacArgumentList(term1,term_ArgumentList(subterm));
for (l = term_ArgumentList(term1); !list_Empty(l); l=list_Cdr(l))
term_RplacSuperterm(list_Car(l), term1);
term_RplacArgumentList(subterm, list_Nil());
term_Delete(subterm);
}
}
}
}
if (!set) {
if (!list_Empty(term_ArgumentList(term1)))
for (scan=term_ArgumentList(term1);!list_Empty(scan);scan=list_Cdr(scan))
if (term_HasProperSuperterm(PredicateTerm, list_Car(scan))) {
term1 = list_Car(scan);
set = TRUE;
break;
}
if (!set)
term1 = NULL;
}
}
return Term;
}
TERM cnf_ApplyDefinitionOnce(TERM Predicate, TERM Formula, TERM TargetTerm,
TERM TargetPredicate, FLAGSTORE Flags)
/*********************************************************
INPUT: A term Predicate which is a predicate found in a definition.
A term Formula which is a term equivalent to the predicate.
A term TargetTerm in which one occurrence of the predicate may be
replaced by the Formula.
A term TargetPredicate which is the subterm of the TargetTerm
to be replaced.
A flag store.
RETURNS: The changed TargetTerm.
*************************************************************/
{
SYMBOL maxvar, maxvar_temp;
LIST bound, scan;
BOOL success;
/* Init varcounter */
maxvar = term_MaxVar(TargetTerm);
maxvar_temp = term_MaxVar(Formula);
if (maxvar_temp > maxvar)
maxvar = maxvar_temp;
symbol_SetStandardVarCounter(maxvar);
/* Find bound variables in formula for renaming them */
bound = fol_BoundVariables(Formula);
for (scan=bound; !list_Empty(scan); scan=list_Cdr(scan)) {
/* Bound variable in definition is already used in term */
if (term_ContainsSymbol(TargetTerm, term_TopSymbol(list_Car(scan))))
term_ExchangeVariable(Formula, term_TopSymbol(list_Car(scan)),
symbol_CreateStandardVariable());
}
list_Delete(bound);
TargetTerm = cnf_ApplyDefinitionInternOnce(Predicate, Formula, TargetTerm,
TargetPredicate,&success);
if (flag_GetFlagValue(Flags, flag_PAPPLYDEFS)) {
if (success) {
fputs("\nTarget after applying def:\n", stdout);
fol_PrettyPrint(TargetTerm);
puts("\n");
}
}
return TargetTerm;
}
static TERM cnf_ApplyDefinitionInternOnce(TERM Predicate, TERM Formula,
TERM TargetTerm, TERM TargetPredicate,
BOOL* Success)
/**********************************************************
INPUT: A term Predicate which is equivalence to
Formula and Term
RETURNS: The term in which all occurrences of P(..) are
replaced by Formula modulo the proper bindings
CAUTION: Term is destructively changed!
***********************************************************/
{
/* Quantifiers */
if (fol_IsQuantifier(term_TopSymbol(TargetTerm))) {
term_RplacSecondArgument(TargetTerm,
cnf_ApplyDefinitionInternOnce(Predicate, Formula,
term_SecondArgument(TargetTerm),
TargetPredicate, Success));
term_RplacSuperterm(term_SecondArgument(TargetTerm), TargetTerm);
return TargetTerm;
}
/* Junctors and NOT */
if (fol_IsJunctor(term_TopSymbol(TargetTerm)) ||
symbol_Equal(term_TopSymbol(TargetTerm),fol_Not())) {
LIST scan;
for (scan=term_ArgumentList(TargetTerm); !list_Empty(scan); scan=list_Cdr(scan)) {
list_Rplaca(scan, cnf_ApplyDefinitionInternOnce(Predicate, Formula,
list_Car(scan),
TargetPredicate, Success));
term_RplacSuperterm((TERM) list_Car(scan), TargetTerm);
}
return TargetTerm;
}
if (symbol_Equal(term_TopSymbol(TargetTerm), term_TopSymbol(Predicate))) {
if (TargetTerm == TargetPredicate) {
TERM result;
result = Formula;
cnf_RplacVar(result, term_ArgumentList(Predicate),
term_ArgumentList(TargetTerm));
term_AddFatherLinks(result);
term_Delete(TargetTerm);
*Success = TRUE;
return result;
}
}
return TargetTerm;
}
static TERM cnf_RemoveEquivImplFromFormula(TERM term)
/**********************************************************
INPUT: A term.
RETURNS: The term with replaced implications and equivalences.
CAUTION: The term is destructively changed.
********************************************************/
{
TERM term1,termL,termR,termLneg,termRneg;
LIST scan;
int bottom,pol;
bottom = vec_ActMax();
vec_Push(term);
while (bottom != vec_ActMax()) {
term1 = (TERM)vec_PopResult();
if (symbol_Equal(term_TopSymbol(term1),fol_Implies())) {
term_RplacTop(term1, fol_Or());
list_Rplaca(term_ArgumentList(term1),
term_Create(fol_Not(), list_List(list_Car(term_ArgumentList(term1)))));
}else
if (symbol_Equal(term_TopSymbol(term1),fol_Equiv())) {
pol = cnf_GetFormulaPolarity(term,term1);
termL = (TERM)list_Car(term_ArgumentList(term1));
termR = (TERM)list_Second(term_ArgumentList(term1));
termLneg = term_Create(fol_Not(),list_List(term_Copy(termL)));
termRneg = term_Create(fol_Not(),list_List(term_Copy(termR)));
if (pol == 1 || pol == 0) {
term_RplacTop(term1, fol_And());
list_Rplaca(term_ArgumentList(term1), term_Create(fol_Or(),list_Cons(termLneg,list_List(termR))));
list_RplacSecond(term_ArgumentList(term1),term_Create(fol_Or(),list_Cons(termRneg,list_List(termL))));
}else
if (pol == -1) {
term_RplacTop(term1, fol_Or());
list_Rplaca(term_ArgumentList(term1), term_Create(fol_And(),list_Cons(termLneg,list_List(termRneg))));
list_RplacSecond(term_ArgumentList(term1), term_Create(fol_And(),list_Cons(termL,list_List(termR))));
}
}
if (!list_Empty(term_ArgumentList(term1)))
for (scan=term_ArgumentList(term1);!list_Empty(scan);scan=list_Cdr(scan))
vec_Push(list_Car(scan));
}
vec_SetMax(bottom);
return term;
}
static TERM cnf_MovePredicateVariablesUp(TERM Term, TERM TargetPredicateTerm,
LIST VarsForTopLevel)
/**********************************************************
INPUT: A term and a predicate term which is a subterm of term
an equivalence.
RETURNS: The term where the free variables of the equivalence, which
must be allquantified and not in the scope of an
exist quantifier, are moved to toplevel.
CAUTION: The term is destructively changed.
********************************************************/
{
TERM term1;
LIST scan;
int bottom;
bottom = vec_ActMax();
vec_Push(Term);
while (bottom != vec_ActMax()) {
term1 = (TERM)vec_PopResult();
if (!list_Empty(term_ArgumentList(term1)))
for (scan=term_ArgumentList(term1);!list_Empty(scan);scan=list_Cdr(scan)) {
TERM arg;
arg = (TERM) list_Car(scan);
if (term_HasProperSuperterm(TargetPredicateTerm, arg)) {
if (symbol_Equal(term_TopSymbol(arg), fol_All())) {
LIST predicatevarscan, quantifiervars;
quantifiervars = fol_QuantifierVariables(arg);
for (predicatevarscan=VarsForTopLevel; !list_Empty(predicatevarscan);
predicatevarscan = list_Cdr(predicatevarscan))
quantifiervars = list_DeleteElementFree(quantifiervars,
(TERM) list_Car(predicatevarscan),
(BOOL (*)(POINTER,POINTER))term_Equal,
(void (*)(POINTER))term_Delete);
if (!list_Empty(quantifiervars))
term_RplacArgumentList(term_FirstArgument(arg), quantifiervars);
else {
TERM subterm;
subterm = term_SecondArgument(arg);
term_Free(term_FirstArgument(arg));
list_Delete(term_ArgumentList(arg));
term_Free(arg);
list_Rplaca(scan, subterm);
term_RplacSuperterm(subterm, term1);
}
}
vec_Push((TERM) list_Car(scan));
}
}
}
for (scan=VarsForTopLevel; !list_Empty(scan); scan = list_Cdr(scan))
list_Rplaca(scan, term_Copy((TERM) list_Car(scan)));
if (symbol_Equal(term_TopSymbol(Term), fol_All())) {
LIST vars;
vars = fol_QuantifierVariables(Term);
vars = list_Nconc(vars, list_Copy(VarsForTopLevel));
vars = term_DestroyDuplicatesInList(vars);
term_RplacArgumentList(term_FirstArgument(Term), vars);
}
else {
TERM newtop;
newtop = fol_CreateQuantifier(fol_All(), list_Copy(VarsForTopLevel), list_List(Term));
term_RplacSuperterm(Term, newtop);
Term = newtop;
}
vec_SetMax(bottom);
return Term;
}
static TERM cnf_RemoveImplFromFormulaPath(TERM Term, TERM PredicateTerm)
/**********************************************************
INPUT: A term and a predicate term which is a subterm of term
RETURNS: The term where implications along the path to PredicateTerm
are replaced.
CAUTION: The term is destructively changed.
This works only if the superterm member of Term and its
subterms are set.
********************************************************/
{
TERM term1;
LIST scan;
int bottom;
bottom = vec_ActMax();
vec_Push(Term);
while (bottom != vec_ActMax()) {
term1 = (TERM)vec_PopResult();
if (term_HasProperSuperterm(PredicateTerm, term1)) {
if (symbol_Equal(term_TopSymbol(term1),fol_Implies())) {
TERM newterm;
term_RplacTop(term1, fol_Or());
newterm = term_CreateAddFather(fol_Not(), list_List(list_Car(term_ArgumentList(term1))));
list_Rplaca(term_ArgumentList(term1), newterm);
term_RplacSuperterm(newterm, term1);
}
if (!list_Empty(term_ArgumentList(term1)))
for (scan=term_ArgumentList(term1);!list_Empty(scan);scan=list_Cdr(scan))
vec_Push(list_Car(scan));
}
}
vec_SetMax(bottom);
return Term;
}
static SYMBOL cnf_GetDualSymbol(SYMBOL symbol)
/********************************************************
INPUT: A predefined symbol.
RETURNS: The dual symbol.
********************************************************/
{
SYMBOL dual;
dual = symbol;
if (symbol_Equal(symbol,fol_All()))
dual = fol_Exist();
else
if (symbol_Equal(symbol,fol_Exist()))
dual = fol_All();
else
if (symbol_Equal(symbol,fol_Or()))
dual = fol_And();
else
if (symbol_Equal(symbol,fol_And()))
dual = fol_Or();
return dual;
}
TERM cnf_NegationNormalFormula(TERM term)
/********************************************************
INPUT: A term.
RETURNS: The negation normal form of the term.
CAUTION: The term is destructively changed.
********************************************************/
{
TERM term1,subterm,termL;
LIST scan;
SYMBOL symbol;
int bottom;
bottom = vec_ActMax();
vec_Push(term);
while (bottom != vec_ActMax()) {
term1 = (TERM)vec_PopResult();
if (symbol_Equal(term_TopSymbol(term1),fol_Not())) {
subterm = (TERM)list_Car(term_ArgumentList(term1));
if (symbol_Equal(term_TopSymbol(subterm),fol_Not())) {
term_RplacTop(term1,term_TopSymbol(term_FirstArgument(subterm)));
list_Delete(term_ArgumentList(term1));
term_RplacArgumentList(term1,term_ArgumentList(term_FirstArgument(subterm)));
term_Free(term_FirstArgument(subterm));
list_Delete(term_ArgumentList(subterm));
term_Free(subterm);
vec_Push(term1);
}else
if (fol_IsQuantifier(term_TopSymbol(subterm))) {
symbol = (SYMBOL)cnf_GetDualSymbol(term_TopSymbol(subterm));
termL = term_Create(fol_Not(),
list_List(term_SecondArgument(subterm)));
list_RplacSecond(term_ArgumentList(subterm), termL);
term_RplacTop(term1,symbol);