-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemod.c
More file actions
1479 lines (1377 loc) · 38.7 KB
/
Copy pathdemod.c
File metadata and controls
1479 lines (1377 loc) · 38.7 KB
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.
*
*/
/*
* demod.c -- Demodulation (rewriting) routines.
*
*/
#include "header.h"
/*************
*
* int contract_lin(term, demods, context, demod_id_p,clrt)
*
* Attempt to rewrite the top level of `term', using a
* sequential search of `demods'. If success, term is freed; if fail,
* NULL is returned.
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter clrt.
*
*************/
static int contract_lin(t, demods, c, demod_id_p,clrt)
struct term *t;
int *demods;
struct context *c;
int *demod_id_p;
struct term **clrt;
{
struct term *atom, *contractum, *t2, *t3, *beta;
struct rel *alpha_rel;
struct trail *tr;
struct clause *p;
struct list *d;
int mult_flag, dummy, ok;
int mok;
*clrt = NULL; /* default */
tr = NULL;
d = (struct list *) demods;
if (d == NULL)
return(NO_TROUBLE);
p = d->first_cl;
contractum = NULL;
while (p != NULL && contractum == NULL) {
atom = p->first_lit->atom;
alpha_rel = (atom->varnum == CONDITIONAL_DEMOD ? atom->farg->narg
: atom->farg);
tr = NULL;
if (match(alpha_rel->argval, c, t, &tr, &mok) == TROUBLE)
return(TROUBLE);
if (mok)
{
if (atom->varnum == CONDITIONAL_DEMOD) {
if (apply_demod(atom->farg->argval, c, &dummy,&t2) == TROUBLE)
return(TROUBLE);
if (convenient_demod(t2,&t3) == TROUBLE)
return(TROUBLE);
ok = (t3->type == NAME && str_ident(sn_to_str(t3->sym_num),"$T"));
zap_term_special(t3);
}
else
ok = 1;
if (ok) {
beta = alpha_rel->narg->argval;
mult_flag = 0;
if (apply_demod(beta,c,&mult_flag,&contractum) == TROUBLE)
return(TROUBLE);
if (mult_flag)
c->multiplier++;
/* varnum == LEX_DEP_DEMOD means it's lex-dependent */
if (p->first_lit->atom->varnum != LEX_DEP_DEMOD)
ok = 1;
else if (Flags[LEX_RPO].val)
{
if (lrpo_greater(t, contractum, &ok) == TROUBLE)
return(TROUBLE);
}
else
ok = (lex_check(contractum, t) == LESS_THAN);
if (ok) {
zap_term_special(t);
*demod_id_p = p->id;
}
else {
zap_term_special(contractum);
contractum = NULL;
}
}
clear_subst_1(tr);
}
p = p->next_cl;
}
*clrt = contractum; /* may be NULL */
return(NO_TROUBLE);
} /* contract_lin */
/*************
*
* dollar_out_non_list(t) - Process $OUT(t).
*
*************/
static void dollar_out_non_list(t)
struct term *t;
{
int i;
if (t->sym_num==Chr_sym_num && str_int(sn_to_str(t->farg->argval->sym_num),&i))
fprintf(Fdout,"%c", i);
else
print_term(Fdout, t);
} /* dollar_out_non_list */
/*************
*
* dollar_out(t) - Process $OUT(t) or $OUT([t1,...,tn]).
*
*************/
static void dollar_out(t)
struct term *t;
{
struct term *t1;
if (proper_list(t)) {
fprintf(Fdout,"\n");
for (t1 = t; t1->sym_num != Nil_sym_num; t1 = t1->farg->narg->argval) {
if (t != t1)
fprintf(Fdout," ");
dollar_out_non_list(t1->farg->argval);
}
fprintf(Fdout,".\n\n");
}
else
dollar_out_non_list(t);
} /* dollar_out */
/*************
*
* int dollar_contract(t,rt) - evaluate $EQ, $SUM, ...
*
* If t is evaluated, it is deallocated.
* Return(NULL) if t cannot be evaluated.
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter rt.
*
*************/
static int dollar_contract(t,rt)
struct term *t;
struct term **rt;
{
long i1, i2, i3;
int b1, b3, op_code, op_type, s1t, s1f, s2t, s2f;
char *s1, *s2, str[MAX_NAME];
struct term *t1, *ta, *tb;
int tempint;
*rt = NULL; /* default */
/*
if (t->type != COMPLEX)
return(NULL);
*/
op_code = sn_to_ec(t->sym_num); /* get eval code */
if (op_code < 1)
return(NO_TROUBLE);
switch(op_code) {
case SUM_SYM:
case PROD_SYM:
case DIFF_SYM:
case DIV_SYM:
case MOD_SYM:
case BIT_AND_SYM:
case BIT_OR_SYM:
case BIT_XOR_SYM:
case SHIFT_RIGHT_SYM:
case SHIFT_LEFT_SYM: op_type = 1; break; /* int x int -> int */
case EQ_SYM:
case NE_SYM:
case LT_SYM:
case LE_SYM:
case GT_SYM:
case GE_SYM: op_type = 2; break; /* int x int -> bool */
case AND_SYM:
case OR_SYM: op_type = 3; break; /* bool x bool -> bool */
case TRUE_SYM:
case NOT_SYM: op_type = 4; break; /* bool -> bool */
case IF_SYM: op_type = 5; break; /* bool x term x term -> term */
case LLT_SYM:
case LLE_SYM:
case LGT_SYM:
case LGE_SYM:
case LNE_SYM:
case ID_SYM: op_type = 6; break; /* term x term -> bool */
case NEXT_CL_NUM_SYM: op_type = 7; break; /* -> int */
case ATOMIC_SYM:
case NUMBER_SYM:
case GROUND_SYM:
case VAR_SYM: op_type = 8; break; /* term -> bool */
case T_SYM: return(NO_TROUBLE);
case F_SYM: return(NO_TROUBLE);
case OUT_SYM: op_type = 9; break; /* term -> same_term_with_output */
case BIT_NOT_SYM: op_type = 10; break; /* int -> int */
default: fprintf(Fdout,"ERROR, dollar_contract, bad op_code: %d.\n",op_code); return(NO_TROUBLE);
}
switch (op_type) {
case 1: /* int x int -> int */
ta = t->farg->argval;
tb = t->farg->narg->argval;
if (ta->type != NAME || tb->type != NAME)
return(NO_TROUBLE);
s1 = sn_to_str(ta->sym_num);
s2 = sn_to_str(tb->sym_num);
if (str_long(s1, &i1) == 0 || str_long(s2, &i2) == 0)
return(NO_TROUBLE);
if ((op_code == DIV_SYM || op_code == MOD_SYM) && i2 == 0) {
output_stats(Fdout, 4);
fprintf(Fderr, "ABEND, integer divide by 0.\007\n");
fprintf(Fdout,"ABEND, integer divide by 0: ");
print_term(Fdout, t),
fprintf(Fdout,"\n");
return(TROUBLE);
}
switch (op_code) {
case SUM_SYM: i3 = i1 + i2; break;
case PROD_SYM: i3 = i1 * i2; break;
case DIFF_SYM: i3 = i1 - i2; break;
case DIV_SYM: i3 = i1 / i2; break;
case MOD_SYM: i3 = i1 % i2; break;
case BIT_AND_SYM: i3 = i1 & i2; break;
case BIT_OR_SYM: i3 = i1 | i2; break;
case BIT_XOR_SYM: i3 = i1 ^ i2; break;
case SHIFT_RIGHT_SYM: i3 = i1 >> i2; break;
case SHIFT_LEFT_SYM: i3 = i1 << i2; break;
}
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
long_str(i3, str);
if (str_to_sn(str, 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t1;
return(NO_TROUBLE);
case 2: /* int x int -> bool */
ta = t->farg->argval;
tb = t->farg->narg->argval;
if (ta->type != NAME || tb->type != NAME)
return(NO_TROUBLE);
s1 = sn_to_str(ta->sym_num);
s2 = sn_to_str(tb->sym_num);
if (str_long(s1, &i1) == 0 || str_long(s2, &i2) == 0)
return(NO_TROUBLE);
switch (op_code) {
case EQ_SYM: b3 = i1 == i2; break;
case NE_SYM: b3 = i1 != i2; break;
case LT_SYM: b3 = i1 < i2; break;
case LE_SYM: b3 = i1 <= i2; break;
case GT_SYM: b3 = i1 > i2; break;
case GE_SYM: b3 = i1 >= i2; break;
}
t->occ.lit = NULL; /* in case t is a literal */
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
if (str_to_sn(b3 ? "$T" : "$F", 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t1;
return(NO_TROUBLE);
case 3: /* bool x bool -> bool */
s1 = sn_to_str(t->farg->argval->sym_num);
s2 = sn_to_str(t->farg->narg->argval->sym_num);
s1t = str_ident(s1,"$T");
s1f = str_ident(s1,"$F");
s2t = str_ident(s2,"$T");
s2f = str_ident(s2,"$F");
if ((s1t == 0 && s1f == 0) || (s2t == 0 && s2f == 0))
return(NO_TROUBLE);
switch (op_code) {
case AND_SYM: b3 = s1t && s2t; break;
case OR_SYM: b3 = s1t || s2t; break;
}
t->occ.lit = NULL; /* in case t is a literal */
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
if (str_to_sn(b3 ? "$T" : "$F", 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t1;
return(NO_TROUBLE);
case 4: /* bool -> bool $NOT(x), $TRUE(x) */
s1 = sn_to_str(t->farg->argval->sym_num);
s1t = str_ident(s1,"$T");
s1f = str_ident(s1,"$F");
if (s1t == 0 && s1f == 0)
return(NO_TROUBLE);
t->occ.lit = NULL; /* in case t is a literal */
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
switch (op_code) {
case NOT_SYM: if (str_to_sn(s1t ? "$F" : "$T", 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
break;
case TRUE_SYM: if (str_to_sn(s1t ? "$T" : "$F", 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
break;
}
*rt = t1;
return(NO_TROUBLE);
case 5: /* bool x term x term -> term $IF(x,y,z) */
s1 = sn_to_str(t->farg->argval->sym_num);
s1t = str_ident(s1,"$T");
s1f = str_ident(s1,"$F");
if (s1t == 0 && s1f == 0)
return(NO_TROUBLE);
if (s1t)
t1 = t->farg->narg->argval;
else
t1 = t->farg->narg->narg->argval;
t1->fpa_id++; /* one more pointer to t1 */
zap_term_special(t); /* one less pointer to t */
*rt = t1;
return(NO_TROUBLE);
case 6: /* term x term -> bool (lexical comparisons) */
ta = t->farg->argval;
tb = t->farg->narg->argval;
b1 = lex_check(ta, tb);
switch (op_code) {
case ID_SYM: b3 = (b1 == SAME_AS); break;
case LNE_SYM: b3 = (b1 != SAME_AS); break;
case LLT_SYM: b3 = (b1 == LESS_THAN); break;
case LLE_SYM: b3 = (b1 == LESS_THAN || b1 == SAME_AS); break;
case LGT_SYM: b3 = (b1 == GREATER_THAN); break;
case LGE_SYM: b3 = (b1 == GREATER_THAN || b1 == SAME_AS); break;
}
t->occ.lit = NULL; /* in case t is a literal */
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
if (str_to_sn(b3 ? "$T" : "$F", 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t1;
return(NO_TROUBLE);
case 7: /* -> int */
int_str(next_cl_num(), str);
if (str_to_sn(str, 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t;
return(NO_TROUBLE);
case 8: /* term -> bool (metalogical properties) */
ta = t->farg->argval;
switch (op_code) {
case ATOMIC_SYM: b3 = ta->type == NAME; break;
case NUMBER_SYM:
b3 = ( ta->type == NAME &&
str_long(sn_to_str(ta->sym_num), &i1));
break;
case VAR_SYM: b3 = ta->type == VARIABLE; break;
case GROUND_SYM: b3 = ground(ta); break;
}
t->occ.lit = NULL; /* in case t is a literal */
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
if (str_to_sn(b3 ? "$T" : "$F", 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t1;
return(NO_TROUBLE);
case 9: /* term -> same_term_with_output */
dollar_out(t->farg->argval);
return(NO_TROUBLE);
case 10: /* int -> int */
s1 = sn_to_str(t->farg->argval->sym_num);
if (str_long(s1, &i1) == 0)
return(NO_TROUBLE);
switch (op_code) {
case BIT_NOT_SYM: i3 = ~i1;
break;
}
zap_term_special(t);
if (get_term(&t1) == TROUBLE)
return(TROUBLE);
t1->type = NAME;
long_str(i3, str);
if (str_to_sn(str, 0, &tempint) == TROUBLE)
return(TROUBLE);
t1->sym_num = tempint;
*rt = t1;
return(NO_TROUBLE);
}
fprintf(Fdout,"ERROR, dollar_contract, bad op_type: %d.\n", op_type);
return(NO_TROUBLE);
} /* dollar_contract */
/*************
*
* int demod(term,contract_proc,demods,count,context,histp,drt)
*
* Demodulate a term.
*
* The demodulated term is returned, and the given term
* becomes garbage, so a good way to invoke is `t = demod(t, demods, ...'.
* A context must be allocated before the call--the same one is used
* for all subterms--this saves allocating and deallocating at
* each subterm. `count' is pointer to the maximum number of
* rewrites that will be applied. `contract_proc' is a pointer to the
* routine that looks for demodulators and does the rewriting.
* The type of `demods' depends on `contract_proc'.
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter drt.
*
*************/
static int demod(t,contract_proc,demods,count,c,histp,drt)
struct term *t;
int (*contract_proc)();
int *demods;
long *count;
struct context *c;
struct int_ptr **histp;
struct term **drt;
{
struct rel *r;
struct term *t1;
struct int_ptr *ip;
int demod_id;
struct term *temp;
*drt = NULL; /* default */
if (t->type == VARIABLE || TP_BIT(t->bits, SCRATCH_BIT))
{
*drt = t;
return(NO_TROUBLE);
}
/* don't try to demodulate if a variable or if already fully demodulated */
else if (t->type == COMPLEX) {
/* if $IF, evaluate right now! */
if (Internal_flags[DOLLAR_PRESENT] && sn_to_ec(t->sym_num) == IF_SYM) {
/* first reduce condition */
if (demod(t->farg->argval,contract_proc,demods,count,c,histp,&temp)==TROUBLE)
return(TROUBLE);
t->farg->argval = temp;
/* now evaluate $IF */
if (dollar_contract(t,&t1) == TROUBLE)
return(TROUBLE);
if (t1 != NULL) {
(*count)--;
if (demod(t1,contract_proc,demods,count,c,histp,&temp)==TROUBLE)
return(TROUBLE);
*drt = temp;
return(NO_TROUBLE);
}
}
/* fully demodulate subterms */
r = t->farg;
while (r != NULL && *count > 0) {
if (demod(r->argval, contract_proc, demods, count, c, histp,&temp)==TROUBLE)
return(TROUBLE);
r->argval = temp;
r = r->narg;
}
}
if (*count > 0) {
if ((*contract_proc)(t, demods, c, &demod_id, &t1) == TROUBLE)
return(TROUBLE);
if (t1 != NULL) {
(*count)--;
if (*histp != NULL) {
if (get_int_ptr(&ip) == TROUBLE)
return(TROUBLE);
ip->i = demod_id;
(*histp)->next = ip;
*histp = ip;
}
if (demod(t1,contract_proc,demods,count,c,histp,&temp)==TROUBLE)
return(TROUBLE);
t = temp;
}
else if (Internal_flags[DOLLAR_PRESENT]) {
if (dollar_contract(t,&t1) == TROUBLE)
return(TROUBLE);
if (t1 != NULL) {
(*count)--;
t = t1;
}
}
SET_BIT(t->bits, SCRATCH_BIT);
}
*drt = t;
return(NO_TROUBLE);
} /* demod */
/*************
*
* int left_most_one_step(t, contract_proc, demods, c, histp,lmos)
*
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter lmos.
*
*************/
static int left_most_one_step(t, contract_proc, demods, c, histp,lmos)
struct term *t;
int (*contract_proc)();
int *demods;
struct context *c;
struct int_ptr **histp;
struct term **lmos;
{
struct term *t1;
struct int_ptr *ip;
struct rel *r;
int demod_id;
*lmos = NULL; /* default */
if (t->type == VARIABLE || TP_BIT(t->bits, SCRATCH_BIT))
return(NO_TROUBLE);
else {
if ((*contract_proc)(t, demods, c, &demod_id, &t1) == TROUBLE)
return(TROUBLE);
if (t1 != NULL) {
if (*histp != NULL) {
if (get_int_ptr(&ip) == TROUBLE)
return(TROUBLE);
ip->i = demod_id;
(*histp)->next = ip;
*histp = ip;
}
}
else {
if (Internal_flags[DOLLAR_PRESENT]) {
if (dollar_contract(t,&t1) == TROUBLE)
return(TROUBLE);
}
}
if (t1 != NULL)
{
*lmos = t1;
return(NO_TROUBLE);
}
else {
r = t->farg;
while (r != NULL) {
if (left_most_one_step(r->argval,contract_proc,demods,c,histp,&t1)==TROUBLE)
return(TROUBLE);
if (t1 != NULL) {
r->argval = t1;
*lmos = t;
return(NO_TROUBLE);
}
SET_BIT(r->argval->bits, SCRATCH_BIT);
r = r->narg;
}
*lmos = NULL;
return(NO_TROUBLE);
}
}
} /* left_most_one_step */
/*************
*
* int demod_out_in(term, contract_proc, demods, count, subst, histp,doi)
*
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter doi.
*
*************/
static int demod_out_in(t, contract_proc, demods, count, c, histp,doi)
struct term *t;
int (*contract_proc)();
int *demods;
long *count;
struct context *c;
struct int_ptr **histp;
struct term **doi;
{
struct term *t1;
*doi = NULL; /* default */
if (left_most_one_step(t,contract_proc,demods,c,histp,&t1) == TROUBLE)
return(TROUBLE);
while (t1 != NULL) {
(*count)--;
if (*count <= 0)
{
*doi = t1;
return(NO_TROUBLE);
}
else {
t = t1;
if (left_most_one_step(t,contract_proc,demods,c,histp,&t1) == TROUBLE)
return(TROUBLE);
}
}
*doi = t;
return(NO_TROUBLE);
} /* demod_out_in */
/*************
*
* int un_share_special(term)
*
* Given a term in which some of the subterms (not the term
* itself) may be referenced more than once (fpa_id > 0),
* transform it into a term in which all of the subterms
* are referenced exactly once (a normal nonintegrated term)
* by copying the appropriate subterms.
* Also clear the SCRATCH bit in all subterms visited.
*
* void in Otter, int in Penguin, as it returns TROUBLE/NO_TROUBLE.
*
*************/
static int un_share_special(t)
struct term *t;
{
struct rel *r;
struct term *temp;
CLEAR_BIT(t->bits, SCRATCH_BIT);
if (t->type != COMPLEX)
return(NO_TROUBLE);
else {
r = t->farg;
while (r != NULL) {
if (r->argval->fpa_id != 0) {
r->argval->fpa_id--;
if (copy_term(r->argval, &temp) == TROUBLE)
return(TROUBLE);
r->argval = temp;
}
else
if (un_share_special(r->argval) == TROUBLE)
return(TROUBLE);
r = r->narg;
}
}
return(NO_TROUBLE);
} /* un_share_special */
/*************
*
* int convenient_demod(t,cd)
*
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter cd.
*
*************/
int convenient_demod(t,cd)
struct term *t;
struct term **cd;
{
struct term *t1;
struct context *c;
struct int_ptr *hist;
long limit;
*cd = NULL; /* default */
limit = Parms[DEMOD_LIMIT].val;
limit = (limit == 0 ? MAX_LONG_INT : limit);
hist = NULL; /* so that history will not be kept */
if (get_context(&c) == TROUBLE)
return(TROUBLE);
if (Flags[DEMOD_LINEAR].val)
{
if (demod(t,contract_lin,(int *) Demodulators,&limit,c,&hist,&t1)==TROUBLE)
return(TROUBLE);
}
else
{
if (demod(t,contract_imd,(int *) Demod_imd,&limit,c,&hist,&t1) == TROUBLE)
return(TROUBLE);
}
free_context(c);
*cd = t1;
return(NO_TROUBLE);
} /* convenient_demod */
/*************
*
* zap_term_special(term) -- Special term deletion.
*
* Deletion of nonintegrated term in which the term and
* some of its subterms might be referenced more than once.
* term->fpa_id is a count of the number of extra references.
* If we get to a term with more than one reference, the
* decrement the number of references; else recurse on the
* subterms and free the node.
*
*************/
void zap_term_special(t)
struct term *t;
{
struct rel *r1, *r2;
if (t->occ.rel != NULL) {
fprintf(Fdout,"WARNING, zap_term_special called with contained term: ");
print_term(Fdout, t);
fprintf(Fdout,"\n");
}
else if (t->fpa_id != 0)
t->fpa_id--;
else {
if (t->type == COMPLEX) { /* complex term */
r1 = t->farg;
while (r1 != NULL) {
zap_term_special(r1->argval);
r2 = r1;
r1 = r1->narg;
free_rel(r2);
}
}
free_term(t);
}
} /* zap_term_special */
/*************
*
* int apply_demod(term, context, mult_flag_ptr,ad) -- Special purpose apply.
*
* If `term' is a variable instantiated to some term t2,
* then increment the reference count of t2, and return t2;
* else create a new node and recurse on any subterms.
*
* mult_flag_ptr is a pointer to flag for incrementing multiplier
* for demodulators that have a variable on the right that doesn't
* occur on the left: If an uninstantiated variable is encountered,
* then the flag is set; when finished applying to beta, if the
* flag is set, then the multiplier is incremented.
* (It may be the case that demods of this type are not allowed.)
*
* struct term * in Otter, int in Penguin, as it returns TROUBLE/
* NO_TROUBLE. It returns the pointer to struct term through the
* parameter ad.
*
*************/
int apply_demod(t, c, pf,ad)
struct term *t;
struct context *c;
int *pf;
struct term **ad;
{
struct term *t2;
struct rel *r1, *r2, *r3;
struct term *temp;
*ad = NULL; /* default */
if (t->type == VARIABLE && c->terms[t->varnum] != NULL) { /* bound var */
t2 = c->terms[t->varnum];
t2->fpa_id++; /* count of extra references to a term */
*ad = t2;
return(NO_TROUBLE);
}
if (t->type == VARIABLE) { /* unboud variable */
if (!Flags[ORDER_EQ].val) {
/* order_eq causes clauses to be renumbered before demod. */
/* If not renumbered, new vars in beta may map to existing var. */
output_stats(Fdout, 4);
fprintf(Fderr,"ABEND, must set order_eq, because beta has variable not in alpha.\007\n");
fprintf(Fdout,"ABEND, must set order_eq, because beta has variable not in alpha.");
return(TROUBLE);
}
if (get_term(&t2) == TROUBLE)
return(TROUBLE);
t2->type = VARIABLE;
t2->varnum = c->multiplier * MAX_VARS + t->varnum;
*pf = 1; /* when finished applying to beta, increment multiplier */
*ad = t2;
return(NO_TROUBLE);
}
else if (t->type == NAME) { /* name */
if (get_term(&t2) == TROUBLE)
return(TROUBLE);
t2->type = NAME;
t2->sym_num = t->sym_num;
*ad = t2;
return(NO_TROUBLE);
}
else { /* complex term */
if (get_term(&t2) == TROUBLE)
return(TROUBLE);
t2->type = COMPLEX;
t2->sym_num = t->sym_num;
r3 = NULL;
r1 = t->farg;
while (r1 != NULL ) {
if (get_rel(&r2) == TROUBLE)
return(TROUBLE);
if (r3 == NULL)
t2->farg = r2;
else
r3->narg = r2;
if (apply_demod(r1->argval, c, pf,&temp) == TROUBLE)
return(TROUBLE);
r2->argval = temp;
r3 = r2;
r1 = r1->narg;
}
*ad = t2;
return(NO_TROUBLE);
}
} /* apply_demod */
/*************
*
* int demod_cl(c,changed) -- demodulate a clause
*
* void in Otter, int in Penguin, as it returns TROUBLE/NO_TROUBLE
*
* Penguin adds the parameter changed to say whether the clause has
* been demodulated or not.
*
*************/
int demod_cl(c,changed)
struct clause *c;
int *changed;
{
struct literal *lit;
struct term *atom;
int linear, out_in, hist;
long limit_save, limit;
struct context *subst;
struct int_ptr *ip_save, *ip_send, *ip_d;
int *d;
struct term *temp;
linear = Flags[DEMOD_LINEAR].val;
limit = Parms[DEMOD_LIMIT].val;
out_in = Flags[DEMOD_OUT_IN].val;
limit_save = limit = (limit == 0 ? MAX_LONG_INT : limit);
hist = Flags[DEMOD_HISTORY].val;
if (c->parents == NULL)
{
if (get_int_ptr(&ip_save) == TROUBLE)
return(TROUBLE);
ip_d = ip_save;
}
else {
ip_save = c->parents;
while (ip_save->next != NULL)
ip_save = ip_save->next;
ip_d = NULL;
}
/* ip_save saves position to insert "d" if any demodulation occurs */
ip_send = (hist ? ip_save : NULL);
if (get_context(&subst) == TROUBLE)
return(TROUBLE);
subst->multiplier = 1;
if (linear)
d = (int *) Demodulators;
else
d = (int *) Demod_imd;
lit = c->first_lit;
while (lit != NULL && limit > 0) {
atom = lit->atom;
atom->occ.lit = NULL; /* reset at end of loop */
if (out_in) {
if (linear)
{
if (demod_out_in(atom,contract_lin,d,&limit,subst,&ip_send,&temp) == TROUBLE)
return(TROUBLE);
atom = temp;
}
else
{
if (demod_out_in(atom,contract_imd,d,&limit,subst,&ip_send,&temp) == TROUBLE)
return(TROUBLE);
atom = temp;
}
}
else {
if (linear)
{
if (demod(atom,contract_lin,d,&limit,subst,&ip_send,&temp) == TROUBLE)
return(TROUBLE);
atom = temp;
}
else
{
if (demod(atom,contract_imd,d,&limit,subst,&ip_send,&temp) == TROUBLE)
return(TROUBLE);
atom = temp;
}
}
if (un_share_special(atom) == TROUBLE)
return(TROUBLE);
if (atom->varnum == TERM) { /* if the atom itself was changed */
lit->atom = atom;
mark_literal(lit);
}
atom->occ.lit = lit;
lit = lit->next_lit;
}
if (subst->multiplier != 1) {
/* new variables were introduced */
if (renumber_vars(c) == 0) {
output_stats(Fdout, 4);
fprintf(Fderr,"ABEND, demod_cl, demodulation introduced too many variables.\007\n");
fprintf(Fdout,"ABEND, demod_cl, demodulation introduced too many variables:\n");
print_clause(Fdout, c);
return(TROUBLE);
}
}
if (limit <= 0) {
fprintf(Fderr, "WARNING, demod_limit.\n");
fprintf(Fdout,"WARNING, demod_limit:");
print_clause(Fdout, c);
}
/* if some demodulation occured, insert "d" into parent list */
if (limit_save > limit) {
if (ip_d != NULL) {
c->parents = ip_d;
}
else {
if (get_int_ptr(&ip_d) == TROUBLE)
return(TROUBLE);
ip_d->next = ip_save->next;
ip_save->next = ip_d;
}
ip_d->i = DEMOD_RULE;
*changed = 1;
}
else {
if (ip_d != NULL)
free_int_ptr(ip_d);
*changed = 0;
}
Stats[REWRITES] += (limit_save - limit);
free_context(subst);
return(NO_TROUBLE);
} /* demod_cl */
/*************
*
* int back_demod(d,c,ct,lst) - back demodulate with d
*