-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.c
1904 lines (1756 loc) · 41.2 KB
/
main.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
/* N-Prolog
written by kenichi sasagawa 2016/8~
*/
#include <string.h>
#include <math.h>
#include <signal.h>
#ifndef __APPLE__
#include <stdio_ext.h>
#endif
#include <stdlib.h>
#include <getopt.h>
#include <float.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#include <unistd.h>
#include "npl.h"
//global vers
int proof[THREADSIZE];
int nest = 0;
cell heap[CELLSIZE];
int cell_hash_table[HASHTBSIZE];
int variant[VARIANTSIZE][THREADSIZE];
int bigcell[BIGSIZE];
int stack[STACKSIZE][THREADSIZE];
int record_hash_table[HASHTBSIZE][RECORDMAX]; // for hash record database
int record_pt = 1; // current index of record database
int counter[31]; // counter str_set,str_dec ...
char bridge[BUFSIZE]; // for string_term/2 and parallel buffer
char transfer[BUFSIZE]; // buffer for dp_transfer
token stok = { GO, OTHER };
jmp_buf buf; // for REPL halt and error handling.
jmp_buf buf1; // for n_error/2 error check.
jmp_buf buf2; // for break/0 end_of_file/0 exit break
__thread jmp_buf buf3; // for multi-threaded error handling.
int variables[THREADSIZE];
int variables_save[THREADSIZE];
int end_of_file_answer = NIL;
int end_of_file_rest = NIL;
int predicates = NIL;
int builtins = NIL;
int spy_list = NIL;
int reconsult_list = NIL;
int execute_list = NIL;
int op_list = NIL;
int key_list = NIL;
int error_code = 0;
int bag_list;
int nonfree_list;
int unread = NIL; //for parse
int paren_nest = 0; //for parse check ((()))
int left_margin = 4; //for read_line
int break_nest = 0; //for debugger b command
int leap_point = NIL; //for debugger l command
int port; //for debugger
int line; //for error, display line of error point
int column; //for error, display column of error point
int cursor_color; //for ansi_sgr
int cursor_style; //for ansi_sgr
int cursor_row_store = -1; //for ansi_sgr -1=undefined,0~=defined
int cursor_col_store = -1; //for ansi_sgr
int cursor_color_store = -1; //for ansi_sgr
int cursor_style_store = -1; //for ansi_sgr
double timer; // for timer_microseconds/1
//------pointer----
int hp; //heap pointer
int sp[THREADSIZE]; //stack pointer
int fc; //free counter
int ac[THREADSIZE]; //alpha conversion variable count
int wp[THREADSIZE]; //working pointer
int gc; //invoked GC count
int wp_min[THREADSIZE]; // start wp point in each thread
int wp_max[THREADSIZE]; // end wp point in each thread
// bignum pointer
int big_pt0 = 0; // pointer of temporaly bignum
int big_pt1 = BIGNUM_PARMA; // pointer of parmanent bignum
//flag
int repl_flag = 1; //for editable REPL read_line 1=on, 0=off
int trace_flag = FULL; //for debugger
int open_flag = 0; //for error 0=not int reading file, 1=in reading file
int gbc_flag = 0; // 0=not display massage 1=display message
int simp_flag = 1; //for bignum 1=if bignum become more simple, simplify, 0=not
int assert_flag = 0; // 0=asserta, 1=assertz
int debug_flag = OFF; // 0=normal mode, 1=debug mode
int fskip_flag = OFF; // for debugger f command
int qskip_flag = OFF; // for debugger q command
int sskip_flag = OFF; // for debugger s command
int xskip_flag = OFF; // for debugger x command
int semiskip_flag = OFF; //for debugger ; command
int sexp_flag = 0; // for debug 0=normal, 1=print data as S expression like LISP
int arguments_flag = 1; //1= 1,2,3 -> (1,2,3) 0= 1,2,3 -> 1,2,3
int quoted_flag = 1; // 0=not print ' 1=print '
int ignore_flag = 0; // 0=infix notation 2+2 1=prefix notation +(2,2)
int link_flag = 0; // 0=not-link, 1=linked
int listing_flag = 0; //for print clause, 0=normal, 1=format print
int colon_sets_calling_context_flag = 1; //1=true, 0=false
int prefix_flag = 0; //for parser 0=not prefix, 1=prefix
int syntax_flag = YES; //syntaxerrors/2 YES=normal. NO=ignore syntax-errors
int fileerr_flag = YES; //fileerrors/2 YES=normal. NO=ignore file-errors
int exist_flag = YES; //existerrors/2 YES=normal, NO=ignore existance_errors
int bridge_flag = 0; //for string_term/2 0=normal, 1=readparse from bridge
int ctrl_c_flag = 0; //for ctrl_c to stop prove
int init_flag = 1; //for halt
int script_flag = 0; // script mode, 0=not scriplt-mode, 1=script-mode.
int check_flag = 0; // for n_error/2 error check
int break_flag = 0; // for break/0 0=normal,1=break.
int parallel_exit_flag = 0; /* To exit parallel threads */
int process_flag = 0; /* when invoke as child process, flag is 1 */
int thread_flag = 0; /* when invoke as multi thread, flag is 1 */
int child_flag = 0; /* when invoke as network child, flag is 1 */
int connect_flag = 0; /* when child listen, connect_flag is 1 */
int receiver_exit_flag = 0; /* TO exit child TCP/IP receiver */
int child_busy_flag = 0; /* while executing in child, child_busy_flag is 1 */
int parent_flag = 0; /* while comunicating child, parent_flag = 1 */
int pause_flag = 0; /* while pause in child, pause_flag = 1 */
int shutdown_flag = 0; /* when receive dp_close, shutdown_flag = 1 */
int active_thread = 0; /* for mt_and/1 mt_or/1 */
//stream
int standard_input;
int standard_output;
int standard_error;
int input_stream;
int output_stream;
int error_stream;
/* -----distributed parallel & TCPIP------*/
int parent_sockfd[2];
int child_sockfd[PARASIZE];
socklen_t parent_len;
struct sockaddr_in parent_addr, child_addr[PARASIZE];
int child_num;
pthread_t receiver_thread;
int child_result[PARASIZE];
/* multi thread */
pthread_mutex_t mutex;
pthread_mutex_t mutex1;
int mt_queue[PARASIZE];
int mt_queue_pt;
int mt_queue_num;
int thread_num = 0;
int para_input[PARASIZE];
int para_output[PARASIZE];
pthread_t mt_para_thread[PARASIZE];
pthread_cond_t mt_cond_para[PARASIZE];
pthread_cond_t mt_cond_main;
pthread_cond_t mt_cond_queue;
pthread_attr_t mt_para_attr[PARASIZE];
size_t mt_para_size[PARASIZE];
//-----editor-----
char buffer[BUFSIZE][10];
int ed_tab = 4;
int ed_indent = 0;
int ed_lparen_row;
int ed_lparen_col;
int ed_rparen_row;
int ed_rparen_col;
int ed_lbracket_row;
int ed_lbracket_col;
int ed_rbracket_row;
int ed_rbracket_col;
char ed_candidate[30][30];
int ed_candidate_pt;
int ed_operator_color = 5; //default magenta
int ed_builtin_color = 6; //default cyan
int ed_extended_color = 1; //default red
int ed_quote_color = 3; //default yellow
int ed_comment_color = 4; //default blue
int ed_function_color = 2; //default green
int ed_incomment = -1; /*... */
int ed_hight;
int ed_width;
result rtok; // token type and length for editable REPL
void usage()
{
printf("List of options:\n");
printf("-c filename -- NPL starts after reading the file.\n");
printf("-h -- display help.\n");
printf("-n -- NPL run with network mode.\n");
printf("-r -- NPL does not use editable REPL.\n");
printf("-s filename -- NPL run file with script mode.\n");
printf("-v -- dislplay version number.\n");
}
int main(int argc, char *argv[])
{
int ch, input;
char *home, str[STRSIZE];
struct winsize w;
FILE *fp;
signal(SIGINT, reset);
initcell();
initbuiltin();
initoperator();
initstream();
ioctl(0, TIOCGWINSZ, &w);
ed_hight = w.ws_row;
ed_width = w.ws_col;
input_stream = standard_input;
output_stream = standard_output;
error_stream = standard_error;
wp[0] = HEAPSIZE + 1;
wp_min[0] = HEAPSIZE + 1;
wp_max[0] = CELLSIZE;
init_repl();
int ret = setjmp(buf);
if (!init_flag)
goto repl;
home = getenv("HOME");
strcpy(str, home);
strcat(str, "/nprolog/library/dcg.pl");
fp = fopen(str, "r");
if (fp != NULL) {
fclose(fp);
b_consult(list1(makeconst(str)), NIL, 0);
predicates = NIL;
}
strcpy(str, home);
strcat(str, "/nprolog/library/compiler.pl");
fp = fopen(str, "r");
if (fp != NULL) {
fclose(fp);
b_consult(list1(makeconst(str)), NIL, 0);
predicates = NIL;
}
strcpy(str, home);
strcat(str, "/nprolog/library/multiplex.pl");
fp = fopen(str, "r");
if (fp != NULL) {
fclose(fp);
b_consult(list1(makeconst(str)), NIL, 0);
predicates = NIL;
}
strcpy(str, home);
strcat(str, "/nprolog/library/startup.pl");
fp = fopen(str, "r");
if (fp != NULL) {
fclose(fp);
b_consult(list1(makeconst(str)), NIL, 0);
}
while ((ch = getopt(argc, argv, "c:s:rhvn")) != -1) {
switch (ch) {
case 'c':
fp = fopen(optarg, "r");
if (fp != NULL)
fclose(fp);
else {
printf("Not exist %s\n", optarg);
exit(EXIT_FAILURE);
}
b_consult(list1(makeconst(optarg)), NIL, 0);
break;
case 's':
fp = fopen(optarg, "r");
if (fp != NULL)
fclose(fp);
else {
printf("Not exist %s\n", optarg);
exit(EXIT_FAILURE);
}
script_flag = 1;
b_consult(list1(makeconst(optarg)), NIL, 0);
exit(EXIT_SUCCESS);
case 'r':
repl_flag = 0;
break;
case 'v':
printf("N-Prolog Ver %1.2f\n", VERSION);
exit(EXIT_SUCCESS);
case 'h':
usage();
exit(EXIT_SUCCESS);
case 'n':
printf("N-Prolog runs with network mode.\n");
fp = fopen("network.pl", "r");
if (fp != NULL) {
fclose(fp);
b_consult(list1(makeconst("network.pl")), NIL, 0);
}
child_flag = 1;
init_parent();
init_receiver();
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
if (init_flag) {
printf("N-Prolog Ver %1.2f\n", VERSION);
init_flag = 0;
}
repl:
if (ret == 0)
while (1) {
if (!child_flag) {
input_stream = standard_input;
output_stream = standard_output;
error_stream = standard_error;
init_repl();
printf("?- ");
fflush(stdout);
input = variable_to_call(readparse(0));
if (!repl_flag)
clear_input_buffer();
query(input, 0);
//sexp_flag = 1;print(input);
//printf("proof = %d\n", proof);
fflush(stdout);
} else if (child_flag) {
input_stream = standard_input;
output_stream = standard_output;
error_stream = standard_error;
init_repl();
input =
variable_to_call(convert_to_variable
(str_to_pred(receive_from_parent()),
0));
printf("Receive from parent ");
sprint(input);
printf("\n");
fflush(stdout);
child_busy_flag = 1;
query(input, 0);
child_busy_flag = 0;
printf("Send to parent ");
printf("\n");
fflush(stdout);
}
} else if (ret == 1) {
ret = 0;
goto repl;
} else {
if (shutdown_flag) {
printf("Shutting down the system...\n");
int ret = system("sudo shutdown now");
if (ret == -1)
error(SYSTEM_ERROR, "dp_close shatdown ", NIL, 0);
}
return 0;
}
}
void clear_input_buffer()
{
int c;
while ((c = getchar()) != '\n' && c != EOF) {
}
}
void reset(int i)
{
ctrl_c_flag = 1;
}
void init_repl(void)
{
int i, j;
stok.flag = GO;
nest = 0;
for (i = 0; i < THREADSIZE; i++) {
sp[i] = 0;
proof[i] = 0;
ac[i] = CELLSIZE + 1;
unbind(0, i);
}
for (i = 0; i <= thread_num; i++) {
wp[i] = wp_min[i];
}
ctrl_c_flag = 0;
fskip_flag = OFF;
sskip_flag = OFF;
xskip_flag = OFF;
semiskip_flag = OFF;
leap_point = NIL;
left_margin = 4;
big_pt0 = 0;
//initialize variant variable
for (i = 0; i < VARIANTSIZE; i++) {
for (j = 0; j < THREADSIZE; j++) {
variant[i][j] = UNBIND;
}
}
for (j = 0; j < THREADSIZE; j++) {
i = variables[j];
while (!nullp(i)) {
SET_CAR(car(i), UNBIND);
SET_CDR(car(i), UNBIND);
i = cdr(i);
}
}
//initialize spy-point trace-level
i = spy_list;
while (!nullp(i)) {
SET_TR(cadar(i), 0);
i = cdr(i);
}
}
void query(int x, int th)
{
int res;
// DCG syntax e.g. a-->b.
if (dcgp(x)) {
operate(x, th);
return;
}
//[file1,file2] -> consult(file1),consult(file2).
if (listp(x))
x = list_to_ope(x, th);
if (atomp(x) && !builtinp(x) && !compiledp(x))
x = makepred(GET_NAME(x));
if (wide_variable_p(x))
error(INSTANTATION_ERR, "?- ", x, th);
if (!callablep(x))
error(NOT_CALLABLE, "?- ", x, th);
variables[th] = listreverse(unique(varslist(x)));
res = prove_all(addask(x, th), sp[th], th);
if (!child_flag) {
ESCRST;
print(res);
printf("\n");
} else {
bridge_flag = 1;
if (res == YES) {
printstr("dp_countup(");
printint(proof[th]);
printstr("),");
printstr("true.\n");
} else {
printstr("dp_countup(");
printint(proof[th]);
printstr("),");
printstr("fail.\n");
}
bridge_flag = 0;
send_to_parent_buffer();
}
return;
}
void query_break(int x, int th)
{
int res;
// DCG syntax e.g. a-->b.
if (dcgp(x)) {
operate(x, th);
return;
}
//[file1,file2] -> consult(file1),consult(file2).
if (listp(x))
x = list_to_ope(x, th);
if (atomp(x) && !builtinp(x) && !compiledp(x))
x = makepred(GET_NAME(x));
if (wide_variable_p(x))
error(INSTANTATION_ERR, "?= ", x, th);
if (!callablep(x)) {
error(NOT_CALLABLE, "?= ", x, th);
}
variables_save[th] = variables[th];
variables[th] = listreverse(unique(varslist(x)));
res = prove_all(addask(x, th), sp[th], th);
variables[th] = variables_save[th];
ESCRST;
print(res);
printf("\n");
return;
}
int list_to_ope(int x, int th)
{
if (nullp(x))
error(SYNTAX_ERR, "?-", x, 0);
else if (nullp(cdr(x))) {
if (atomp(car(x)))
return (list2(makeatom("consult", SYS), car(x)));
else if (caar(x) == makeatom("-", OPE))
return (list2(makeatom("reconsult", SYS), cadr(car(x))));
} else {
if (atomp(car(x)))
return (list3(makeatom(",", OPE),
list2(makeatom("consult", SYS), car(x)),
list_to_ope(cdr(x), th)));
else if (caar(x) == makeatom("-", OPE))
return (list3(makeatom(",", OPE),
list2(makeatom("reconsult", SYS), cadr(car(x))),
list_to_ope(cdr(x), th)));
}
return (NIL);
}
int addask(int x, int th)
{
return (addtail_body(makeatom("%ask", SYS), x, th));
}
int addtail_body(int x, int y, int th)
{
if (nullp(y))
return (x);
else if (!conjunctionp(y))
return (wlist3(AND, y, x, th));
else
return (wlist3
(car(y), cadr(y), addtail_body(x, caddr(y), th), th));
}
int prove_all(int goals, int bindings, int th)
{
int res;
if (nullp(goals))
return (YES);
else if (car(goals) != AND)
return (prove(goals, bindings, NIL, th));
else {
if (!has_cut_p(goals)) {
return (prove(cadr(goals), bindings, caddr(goals), th));
} else {
if (prove_all(before_cut(goals), bindings, th) == YES) {
res = prove_all(after_cut(goals), sp[th], th);
if (res == YES)
return (YES);
else if (res == NO)
return (NFALSE);
} else
return (NO);
}
}
return (NO);
}
int prove(int goal, int bindings, int rest, int th)
{
int clause, clauses, clause1, varlis, save1, save2, res;
proof[th]++;
if (ctrl_c_flag) {
printf("ctrl+C\n");
longjmp(buf, 1);
}
if (pause_flag) {
printf("pause\n");
while (1) {
sleep(1);
if (!pause_flag)
break;
}
printf("resume\n");
}
if (nest > 40000)
error(RESOURCE_ERR, "prove recursion over max", NIL, th);
goal = deref(goal, th);
if (nullp(goal)) {
return (prove_all(rest, bindings, th));
} else if (builtinp(goal)) {
if (atomp(goal)) {
if ((res = (GET_SUBR(goal)) (NIL, rest, th)) == YES)
return (YES);
return (res);
} else {
if ((res = (GET_SUBR(car(goal))) (cdr(goal), rest, th)) == YES)
return (YES);
return (res);
}
} else if (compiledp(goal)) {
if (atomp(goal)) {
if ((GET_SUBR(goal)) (NIL, rest, th) == YES)
return (YES);
return (NO);
} else {
if ((GET_SUBR(car(goal))) (cdr(goal), rest, th) == YES)
return (YES);
return (NO);
}
} else if (predicatep(goal) || user_operation_p(goal)) {
//trace
if (debug_flag == ON)
prove_trace(DBCALL, goal, bindings, rest, th);
if (atomp(goal))
clauses = GET_CAR(goal);
else
clauses = GET_CAR(car(goal));
if (clauses == NIL) {
if (exist_flag == YES)
error(EXISTENCE_ERR, "", goal, th);
else if (exist_flag == NO)
return (NO);
}
while (!nullp(clauses)) {
save1 = wp[th];
save2 = ac[th];
clause = car(clauses);
clauses = cdr(clauses);
varlis = GET_VAR(clause);
pthread_mutex_lock(&mutex1);
assign_variant(varlis, th);
clause1 = walpha_conversion(clause, th);
release_variant(varlis);
pthread_mutex_unlock(&mutex1);
// case of predicate
if (predicatep(clause1) || user_operation_p(clause1)) {
if (unify(goal, clause1, th) == YES) {
if (prove_all(rest, sp[th], th) == YES) {
//trace
if (debug_flag == ON)
prove_trace(DBEXIT, goal, bindings, rest, th);
return (YES);
} else {
//trace
if (debug_flag == ON)
prove_trace(DBFAIL, goal, bindings, rest, th);
}
}
}
// case of clause
else {
if (unify(goal, (cadr(clause1)), th) == YES) {
clause1 = addtail_body(rest, caddr(clause1), th);
nest++;
if ((res = prove_all(clause1, sp[th], th)) == YES) {
nest--;
//trace
if (debug_flag == ON)
prove_trace(DBEXIT, goal, bindings, rest, th);
return (YES);
} else {
nest--;
if (res == NFALSE) { // when after cut occurs NO
//trace
if (debug_flag == ON)
prove_trace(DBCUTFAIL, goal, bindings,
rest, th);
wp[th] = save1;
ac[th] = save2;
unbind(bindings, th);
return (NO);
}
}
}
}
//trace
if (debug_flag == ON && !nullp(clauses))
prove_trace(DBREDO, goal, bindings, rest, th);
wp[th] = save1;
ac[th] = save2;
unbind(bindings, th);
}
//trace
if (debug_flag == ON)
prove_trace(DBFAIL, goal, bindings, rest, th);
} else if (disjunctionp(goal)) {
if (ifthenp(cadr(goal))) {
goal =
wcons(IFTHENELSE,
wcons(cadr(cadr(goal)),
wcons(caddr(cadr(goal)),
wcons(caddr(goal), NIL, th), th), th),
th);
// redefine goal = ifthenelse(if,then,else)
return (prove(goal, bindings, rest, th));
} else
if ((res =
prove_all(addtail_body(rest, cadr(goal), th), bindings,
th))
== YES)
return (YES);
else {
if (res == NFALSE) {
unbind(bindings, th);
return (NO);
}
unbind(bindings, th);
if (prove_all
(addtail_body(rest, caddr(goal), th), bindings, th) == YES)
return (YES);
else {
unbind(bindings, th);
return (NO);
}
}
unbind(bindings, th);
return (NO);
}
return (NO);
}
int before_cut(int x)
{
if (x == CUT)
return (NIL);
else if (!conjunctionp(x))
return (x);
else if (conjunctionp(x) && caddr(x) == CUT)
return (cadr(x));
else if (cadr(x) == CUT)
return (NIL);
else if (conjunctionp(caddr(x)) && cadr(caddr(x)) == CUT)
return (cadr(x));
else
return (wlist3(AND, cadr(x), before_cut(caddr(x)), 0));
}
int after_cut(int x)
{
if (x == CUT)
return (NIL);
else if (!conjunctionp(x))
return (x);
else if (cadr(x) == CUT)
return (caddr(x));
else
return (after_cut(caddr(x)));
}
void prove_trace(int port, int goal, int bindings, int rest, int th)
{
int spy, leap;
if (port == DBCALL) {
if (trace_flag != OFF) {
spy = spypointp(goal);
leap = leappointp(goal);
if (spy && !leap && xskip_flag == OFF)
printf("** ");
else if (spy && !leap && xskip_flag == ON)
printf("*> ");
else if (!spy && xskip_flag == OFF)
return;
else if (fskip_flag == ON)
return;
else if (qskip_flag == ON)
return;
else if (sskip_flag == ON)
return;
else if (xskip_flag == ON) {
if (!spy)
printf("> ");
sskip_flag = OFF;
} else if (semiskip_flag == ON)
return;
if (leap)
return;
else
leap_point = NIL;
inctrace(goal);
printf("(%d) CALL: ", gettrace(goal));
print(goal);
port = DBCALL;
debugger(goal, bindings, rest);
}
} else if (port == DBREDO) {
if (trace_flag == FULL || trace_flag == TIGHT
|| trace_flag == HALF) {
spy = spypointp(goal);
leap = leappointp(goal);
if (spy && !leap && semiskip_flag == OFF)
printf("** ");
else if (spy && !leap && semiskip_flag == ON)
printf("*> ");
else if (!spy && semiskip_flag == OFF)
return;
else if (fskip_flag == ON)
return;
else if (qskip_flag == ON)
return;
else if (sskip_flag == ON)
return;
else if (xskip_flag == ON)
return;
else if (semiskip_flag == ON) {
if (spy) {
printf("> ");
semiskip_flag = OFF;
}
}
if (leap)
return;
else
leap_point = NIL;
printf("(%d) REDO: ", gettrace(goal));
print(goal);
port = DBREDO;
debugger(goal, bindings, rest);
}
} else if (port == DBFAIL) {
if (trace_flag == FULL || trace_flag == TIGHT) {
spy = spypointp(goal);
leap = leappointp(goal);
if (spy && !leap && fskip_flag == OFF && qskip_flag == OFF
&& sskip_flag == OFF)
printf("** ");
else if (spy && !leap
&& (fskip_flag == ON || qskip_flag == ON
|| qskip_flag == ON))
printf("*> ");
else if (!spy && fskip_flag == OFF && qskip_flag == OFF
&& sskip_flag == OFF)
return;
else if (fskip_flag == ON) {
printf("> ");
fskip_flag = OFF;
} else if (qskip_flag == ON) {
printf("> ");
qskip_flag = OFF;
} else if (sskip_flag == ON) {
printf("> ");
sskip_flag = OFF;
} else if (xskip_flag == ON)
return;
else if (semiskip_flag == ON)
return;
if (leap)
return;
else
leap_point = NIL;
printf("(%d) FAIL: ", gettrace(goal));
print(goal);
dectrace(goal);
port = DBFAIL;
debugger(goal, bindings, rest);
}
} else if (port == DBCUTFAIL) {
if (trace_flag == FULL || trace_flag == TIGHT) {
spy = spypointp(goal);
leap = leappointp(goal);
if (spy && !leap && fskip_flag == OFF && qskip_flag == OFF
&& sskip_flag == OFF)
printf("** ");
else if (spy && !leap
&& (fskip_flag == ON || qskip_flag == ON
|| qskip_flag == ON))
printf("*> ");
else if (!spy && fskip_flag == OFF && qskip_flag == OFF
&& sskip_flag == OFF)
return;
else if (fskip_flag == ON) {
printf("> ");
fskip_flag = OFF;
} else if (qskip_flag == ON) {
printf("> ");
qskip_flag = OFF;
} else if (sskip_flag == ON) {
printf("> ");
sskip_flag = OFF;
} else if (xskip_flag == ON)
return;
else if (semiskip_flag == ON)
return;
if (leap)
return;
else
leap_point = NIL;
printf("(%d) CUTFAIL: ", gettrace(goal));
print(goal);
clrtrace(goal);
port = DBFAIL;
debugger(goal, bindings, rest);
}
} else if (port == DBEXIT) {
if (trace_flag == FULL) {
spy = spypointp(goal);
leap = leappointp(goal);
if (spy && !leap && qskip_flag == OFF && sskip_flag == OFF
&& xskip_flag == OFF)
printf("** ");
else if (spy && !leap
&& (qskip_flag == ON || sskip_flag == ON
|| xskip_flag == ON))
printf("*> ");
else if (!spy && qskip_flag == OFF && sskip_flag == OFF
&& xskip_flag == OFF)
return;
else if (fskip_flag == ON)
return;
else if (qskip_flag == ON) {
printf("> ");
qskip_flag = OFF;
} else if (sskip_flag == ON) {
printf("> ");
sskip_flag = OFF;
} else if (xskip_flag == ON) {
printf("-> ");
qskip_flag = OFF;
} else if (semiskip_flag == ON)
return;
if (leap)
return;
else
leap_point = NIL;
printf("(%d) EXIT: ", gettrace(goal));
print(goal);
dectrace(goal);
port = DBEXIT;
debugger(goal, bindings, rest);
}
}
}
void inctrace(int goal)
{
if (atomp(goal))
SET_TR(goal, GET_TR(goal) + 1);
else
SET_TR(car(goal), GET_TR(car(goal)) + 1);
}
void dectrace(int goal)
{
if (atomp(goal))
SET_TR(goal, GET_TR(goal) - 1);
else
SET_TR(car(goal), GET_TR(car(goal)) - 1);
}
void clrtrace(int goal)
{
if (atomp(goal))
SET_TR(goal, 0);
else
SET_TR(car(goal), 0);
}
int gettrace(int goal)
{
if (atomp(goal))
return (GET_TR(goal));
else
return (GET_TR(car(goal)));