-
Notifications
You must be signed in to change notification settings - Fork 3
/
prlush.c
1027 lines (903 loc) · 27.7 KB
/
prlush.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
/* prlush.c */
/* Lush resolution .
* Along with unification these are the most important routines in Prolog.
* If you want to embed Small Prolog then you won't need query_loop as such.
* See Chris Hogger's "An Introduction to Logic Programming" (Academic Press)
* if you are hungry for more explanation.
*/
/* Small Prolog uses a control stack , a substitution stack and a
* "trail" to represent its run-time state.
* The control stack is the stack of "activation records".
* A clause packet is the linked list of clauses that correspond to
* a predicate. It's prolog's equivalent of a procedure.
* You enter a procedure when a goal is unified successfully with the
* head of a clause. You can enter the procedure at different clauses.
* Execution tries the clauses in the order of their occurence in the
* list.
* Each time a procedure (clause packet) is entered, a corresponding "frame"
* (or what is called "activation record" in languages like C)
* is pushed on the control stack, and a corresponding frame is pushed
* on the substitution stack(representing the parameters).
* However the situation is much more complicated than what occurs in
* familiar languages because a procedure might have to be redone
* (with the next clause of the packet) after exit of the procedure.
* So you cant just pop the frame on successful return.
* Popping the frame only occurs on backtracking, unless you implement
* an optimisation that recognises that no more clauses could be tried.
* I have not implemented this optimisation in the current version, sorry.
* Each frame must remember its parent: a pointer to the frame of
* the clause which contains the goal that led to the current clause.
* This is used when every goal of the clause has been successfully solved.
* This is not necessarily the previous frame because the previous frame
* comes from the elder brother goal.
* We need to remember also the next goal to try if the current goal
* ends up being successful.
* We need to remember the last "backtrack point": this is a frame
* where there seemed to be the possibility of another solution
* for its procedure.
* We backtrack to that point when there is a failure.
* There is a global variable that does this. But this variable
* will have to be updated when we backtrack so we need to save
* the previous values of it on the stack too. To save a little
* space only some frames need to store the last backtrack point,
* These are "non-deterministic frames", frames in which there
* was a remaining candidate to the current clause.
* Sometimes substitions are created low in the substitution stack
* -before the environment of the current goal. This is why
* we use a trail to be able to unset these substitutions on backtracking.
* We have been lazy and recorded all substitutions on the
* trail.
*/
#include <stdio.h>
#include <setjmp.h> /* added this on Dec 21 1991 */
#include <assert.h> /* added this on Dec 21 1991 */
#include "prtypes.h"
#include "prlush.h"
#include "prparse.h"
#include "prprint.h"
#include "prmachine.h"
#include "prunify.h"
#include "pralloc.h"
#define NOTVARPRED "A variable can't be used as a predicate\n"
#define NOPRED "Predicate not atom\n"
#define STACKCONTENTS "Ancestors of current goal:\n"
#define INIQUERY "Syntax error ini initial query"
#define STRINGQUERY "Syntax error ini query passed as string"
#if TRACE_CAPABILITY
/* values of where in tracing */
#define G_BTK 'B' /* goto bactrack */
#define G_AGA 'P' /* goto AGAIN */
#define KEEP_GOING 'K'
#define TRACE(X) if(Trace_flag > 0){\
if(X == 0)return ABNORMAL_LUSH_RETURN;\
switch(where){case G_AGA:goto AGAIN;case G_BTK:goto BACKTRACK;}}
#define CAN_SKIP 1
#define CANT_SKIP 0
#else
#define TRACE(X)
#endif
/* Although the Trace_flag can be set by a builtin
actual tracing can be suspended while stepping over a call .
The following two variables are used for this purpose.
*/
static int Copy_tracing_now;
#ifdef HUNTBUGS
extern int Bug_hunt_flag;
#endif
extern subst_ptr_t DerefSubst; /* enviroment of last dereferenced
object */
extern subst_ptr_t my_Subst_alloc();
extern node_ptr_t DerefNode; /* skeleton of last dereferenced object */
extern node_ptr_t NilNodeptr; /* node corresponding to empty list */
extern atom_ptr_t Nil; /* object of empty list */
extern clause_ptr_t Bltn_pseudo_clause;
extern dyn_ptr_t HighDyn_ptr,Dyn_mem, Dyn_ptr, my_Dyn_alloc(); /* for control stack */
extern subst_ptr_t Subst_mem, Subst_ptr; /* for substitution stack */
extern node_ptr_t **Trail_mem, **Trail_ptr;
extern FILE * Curr_outfile;
void ini_lush(), reset_zones();
clause_ptr_t Curr_clause; /* current clause containing Goals */
clause_ptr_t Candidate; /*Used when we look for a clause whose
head might match goal */
dyn_ptr_t QueryCframe; /* Control frame of Query */
dyn_ptr_t LastCframe; /* most recent Cframe */
dyn_ptr_t LastBack; /* points to cframe of most recent backtrack point */
dyn_ptr_t Parent; /* parent Cframe of current goal */
dyn_ptr_t BackFrame; /* parent frame in case of reverse tracing */
node_ptr_t Goals; /* what remains of current goals of the clause.
The head of the list is the goal to satisfy first.
Note that there will be (in general) other goals to
satisfy on completion of solution of Goals
*/
node_ptr_t Query; /* the initial query */
node_ptr_t Arguments;/* arguments of current goal (points to a list) */
subst_ptr_t Subst_goals, /* substituion env of Goals */
SubstGoal, /* can be different to Subst_goals if there is a "call" */
OldSubstTop; /* for backtracking */
atom_ptr_t Predicate; /* predicate of current goal */
node_ptr_t **OldTrailTop;
int ErrorGlobal; /* used to communicate the fact there was an error */
int Deterministic_flag; /*used to minimise "more?" questions after a solution */
int ReverseTraceMode = 0; /* if set to 1 means you can trace backwards
but this is greedy on control stack space
because all frames are like the non-deterministic
frames.
*/
integer Nunifications = 0; /* just for statistics */
/******************************/
/* Dec 21 1991 new variables: */
/******************************/
node_ptr_t ND_builtin_next_nodeptr;/* This is for
non deterministic builtins. When this variable is NULL it
is the first call of the predicate. Otherwise it is the nodeptr
that guides the non deterministic builtin.
*/
jmp_buf Bltin_env; /* see do_builtin() */
jmp_buf Query_jenv; /* see query_loop() */
int QJusable = 0; /* Determines if we can longjump to Query_jenv */
#if TRACE_CAPABILITY
extern int Trace_flag; /* sets trace mode */
extern int Tracing_now; /* sets trace mode */
dyn_ptr_t Skip_above;
int Unleash_flag = 0;
#endif
/*******************************************************************
read_goals()
Called by query_loop.
Updates Goals, Nvars and Query.
*******************************************************************/
int read_goals(FILE *ifp)
{
extern node_ptr_t read_list(), get_node();
extern FILE * Curr_infile;
node_ptr_t head;
FILE * save_cif;
ENTER("read_goals");
save_cif = Curr_infile;
Curr_infile = ifp;
Goals = read_list(PERMANENT);
if(Goals == NULL)return(0);
copy_varnames();
head = NODEPTR_HEAD(Goals);
if(NODEPTR_TYPE(head) == ATOM)
{
pair_ptr_t pairptr, get_pair();
pairptr = get_pair(DYNAMIC);
NODEPTR_TYPE(PAIRPTR_HEAD(pairptr)) = PAIR;
NODEPTR_PAIR(PAIRPTR_HEAD(pairptr)) = NODEPTR_PAIR(Goals);
NODEPTR_TYPE(PAIRPTR_TAIL(pairptr)) = ATOM;
NODEPTR_ATOM(PAIRPTR_TAIL(pairptr)) = Nil;
Goals = get_node(DYNAMIC);
NODEPTR_TYPE(Goals) = PAIR;
NODEPTR_PAIR(Goals) = pairptr;
}
Query = Goals;
Curr_infile = save_cif;
return(1);
}
/******************************************************************************
initial_query
This executes the query from a file . It is silent if the file does not exist.
******************************************************************************/
int initial_query(char *filename)
{
FILE *ifp;
ENTER("initial_query");
if((ifp = fopen(filename, "r")) == NULL)
return(1); /* silent */
reset_zones();
Skip_above = HighDyn_ptr;
if(!read_goals(ifp))
{
fatal(INIQUERY);
}
fclose(ifp);
ini_lush();
return(lush(TRUE) != FINAL_LUSH_RETURN );
}
#ifdef PROLOG_IS_CALLED_FROM_OTHER_PROGRAM
/******************************************************************************
execute_query()
This could be used to execute a query passed as a string.
This means you could embed Small Prolog in another program.
Not currently used, but why not modify main() to make use of it?
******************************************************************************/
int execute_query(char *s)
{
extern int String_input_flag;
extern char *Curr_string_input;
ENTER("execute_query");
String_input_flag = 1;
Curr_string_input = s;
reset_zones();
Skip_above = HighDyn_ptr;
if(!read_goals(stdin/* ignored */))
{
fatal(STRINGQUERY);
}
String_input_flag = 0;
ini_lush();
return(lush(1) != FINAL_LUSH_RETURN);
}
#endif
/*******************************************************************
query_loop()
Called by main().
This is the interactive question-answer loop driver.
*******************************************************************/
void query_loop()
{
int first_time = TRUE;/* have not backtracked yet */
int stop_state;
varindx nvar_query;
extern varindx Nvars;
ENTER("query_loop");
do {
setjmp(Query_jenv);
QJusable = 1; /* indicates we can longjump to Query_jenv */
reset_zones();
Skip_above = HighDyn_ptr;
prompt_user();
if(!read_goals(stdin))
continue;/* updates Goals, Nvars */
tty_getc();/* read the carriage return */
nvar_query = Nvars;
ini_lush(); /* Updates Curr_clause... */
do{
stop_state = lush(first_time);
switch(stop_state)
{
case SUCCESS_LUSH_RETURN:
first_time = FALSE;
pr_solution(nvar_query, BASE_SUBST);
if(LastBack < QueryCframe)
Deterministic_flag = 1;
if(!Deterministic_flag &&
more_y_n())/* want another solution? */
{/* answers yes */
break;
}
else
{
stop_state = FAIL_LUSH_RETURN;
}
first_time = TRUE;
break;
case FINAL_LUSH_RETURN:
tty_pr_string("Bye ...\n");
return;
case ABNORMAL_LUSH_RETURN:
first_time = TRUE;
stop_state = FAIL_LUSH_RETURN;
break;/* just a way of avoiding a goto */
case FAIL_LUSH_RETURN:
tty_pr_string("No\n");
first_time = TRUE;
break;
default:
INTERNAL_ERROR("lush return");
}
}while(stop_state != FAIL_LUSH_RETURN);
}while(1);
}
/*******************************************************************
ini_lush()
Sets up the global variables, stack etc...
Note that a pseudo-clause is created whose goals represent
the query. Gasp, it's headless!
*******************************************************************/
void ini_lush()
{
clause_ptr_t get_clause();
extern varindx Nvars;
ENTER("ini_lush");
Deterministic_flag = 1;
QueryCframe = Dyn_ptr;
Parent = QueryCframe;
OldSubstTop = BASE_SUBST;
OldTrailTop = Trail_ptr;
LastBack = NULL;
my_Subst_alloc((unsigned int)(Nvars * sizeof(struct subst)));
Curr_clause = get_clause(PERMANENT); /* could be DYNAMIC !! */
CLAUSEPTR_GOALS(Curr_clause) = Goals;
CLAUSEPTR_HEAD(Curr_clause) = NilNodeptr; /* Who said one head
is better than none ? */
CLAUSEPTR_NEXT(Curr_clause) = NULL;
/* Currclause is artificial */
LastCframe = Dyn_ptr;
FRAME_PARENT(LastCframe) = Parent;
FRAME_SUBST(LastCframe) = OldSubstTop;
FRAME_GOALS(LastCframe) = Goals;
#ifdef DFRAMES_HAVE_TYPE_FIELD
FRAME_TYPE(LastCframe) = D_FRAME;
#endif
my_Dyn_alloc(SIZE_DCFRAME);
}
/*******************************************************************
reset_zones()
Clean things up .
*******************************************************************/
void reset_zones()
{
ENTER("reset_zones");
Dyn_ptr = BASE_CSTACK;
Subst_ptr = BASE_SUBST;
reset_trail(BASE_TRAIL);
}
/*******************************************************************
do_builtin()
Call a builtin. Notice that it uses global variables to get
at the arguments of the builtin.
*******************************************************************/
int do_builtin(intfun bltn) /* a function */
{
int ret;
ENTER("do_builtin");
if(setjmp(Bltin_env))
return(0);/* fail */
ret = (*bltn)();
/* BUGHUNT(ATOMPTR_NAME(Predicate)); a good place
to do a checksum when looking for bugs; See Robert Ward's book
"Debugging C"
*/
return(ret);
}
/*******************************************************************
determine_predicate()
Returns current predicate by dereferencing goal expression.
Updates Arguments, SubstGoal and others.
This is more complicated than might appear at first sight because
there are several cases involving goals that are variables.
*******************************************************************/
atom_ptr_t determine_predicate()
{
node_ptr_t goal, headnode;
ENTER("determine_predicate");
goal = NODEPTR_HEAD(Goals);
if(!dereference(goal, Subst_goals))
{
errmsg(NOTVARPRED);
ErrorGlobal = ABORT;
return(NULL);
}
switch(NODEPTR_TYPE(DerefNode))
{
case ATOM:
Arguments = NilNodeptr;
SubstGoal = DerefSubst;
return(NODEPTR_ATOM(DerefNode));
case PAIR:
SubstGoal = DerefSubst;
headnode = NODEPTR_HEAD(DerefNode);
Arguments = NODEPTR_TAIL(DerefNode);
if(!dereference(headnode, SubstGoal))
{
errmsg(NOTVARPRED);
ErrorGlobal = ABORT;
return(NULL);
}
headnode = DerefNode;
if(NODEPTR_TYPE(headnode) != ATOM)
{
errmsg(NOPRED);
ErrorGlobal = ABORT;
return(NULL);
}
return(NODEPTR_ATOM(headnode));
default:
ErrorGlobal = ABORT;
return(NULL);
}
}
/****************************************************************************
do_cut()
Implements the infamous cut. This is called by Pcut in prbuiltin.c
As an excercise try reclaiming space on the control stack.
***************************************************************************/
void do_cut()
{
ENTER("do_cut");
if (LastBack == NULL)return;
else
while (LastBack >= QueryCframe && LastBack >= Parent)
LastBack = FRAME_BACKTRACK(LastBack);
}
/****************************************************************************
dump_ancestors()
Lets you look at the ancestors of the call when something has gone wrong.
Should not be called dump_ancestors because it only shows the ancestors.
***************************************************************************/
void dump_ancestors(dyn_ptr_t cframe)
{
int i;
node_ptr_t goals;
Curr_outfile = stdout;
tty_pr_string(STACKCONTENTS);
tty_pr_string("\n");
i = 1;
if (cframe == LastCframe)
{
i++;
out_node(Goals, Subst_goals);
tty_pr_string("\n");
}
while(cframe != QueryCframe)
{
goals = FRAME_GOALS( cframe );
out_node(goals, FRAME_SUBST( cframe ));
cframe = FRAME_PARENT( cframe );
tty_pr_string("\n");
if(i++ == MAX_LINES)
{
i = 1;
if (!more_y_n())
break;
}
}
}
#if TRACE_CAPABILITY
/************************************************************************/
/* Tracing routines */
/************************************************************************/
/*************************************************************************
function trace_pause()
*************************************************************************/
int trace_pause(pwhere, skipp)
int *pwhere, skipp;
{
char c;
*pwhere = KEEP_GOING; /* default */
if(Unleash_flag){
tty_pr_string("\n");
return(1);
}
c = tty_getche();
tty_pr_string("\n");
switch(c)
{
case '\n':
case '\r':
Skip_above = HighDyn_ptr;
break;
case 'n':
Trace_flag = 0; /* stop tracing, continue execution */
Tracing_now = 0;
break;
case 's':
if(skipp == CANT_SKIP)
return 1;
Skip_above = Parent;
Copy_tracing_now = Tracing_now;
Tracing_now = 0;
break;
case '2':
Trace_flag = 2;
break;
case '1':
Trace_flag = 1;
break;
case 'a':
Trace_flag = 0;
Tracing_now = 0;
return 0;
case 'P':
BackFrame = Parent;
if( (BackFrame < BASE_CSTACK)
|| (FRAME_TYPE(BackFrame) == D_FRAME)
|| !ReverseTraceMode
)
{
tty_pr_string("Cannot reverse step just here\n");
tty_pr_string("Please retype your command:");
return trace_pause();
}
*pwhere = G_AGA;
break;
case 'B':
*pwhere = G_BTK;
break;
case 'U':
Unleash_flag = 1;
break;
default:
tty_pr_string("type:\n");
tty_pr_string("Enter to see next step\n");
tty_pr_string("2 to increase the trace details\n");
tty_pr_string("1 to return to normal trace details\n");
tty_pr_string("a to abort trace and execution\n");
tty_pr_string("n to abort trace but continue execution\n");
tty_pr_string("s to step over current goal \n");
tty_pr_string("P to come back to parent goal\n");
tty_pr_string("B to backtrack\n");
tty_pr_string("U to trace without prompt\n");
tty_pr_string("please retype your command:");
return trace_pause();
}
return 1;
}
static int tr_immediate_exit(pwhere)
int *pwhere;
{
int res = 1;
if(!Tracing_now && Skip_above >= Parent)
Tracing_now= Copy_tracing_now;
if(Tracing_now){
pr_string(" Exit ");
out_node(NODEPTR_HEAD(Goals),SubstGoal);
res = trace_pause(pwhere,CANT_SKIP);
}
return (res);
}
static int tr_exit(int *pwhere)
{
int res = 1;
if(!Tracing_now && Skip_above >= Parent)
Tracing_now = Copy_tracing_now;
if(Tracing_now){
pr_string("...exit ");
out_node(NODEPTR_HEAD(Goals),FRAME_SUBST(Parent));
res = trace_pause(pwhere,CANT_SKIP);
}
return(res);
}
static int tr_redo(int *pwhere)
{
int res = 1;
if(!Tracing_now && Skip_above > Parent)
Tracing_now = Copy_tracing_now;
if(Tracing_now){
pr_string(" Redo ");
out_node(NODEPTR_HEAD(Goals),SubstGoal);
res = trace_pause(pwhere,CAN_SKIP);
}
return(res);
}
static int tr_call(int *pwhere)
{
int res = 1;
if(Tracing_now){
pr_string(" Call ");
out_node(NODEPTR_HEAD(Goals), SubstGoal);
res = trace_pause(pwhere,CAN_SKIP);
}
return(res);
}
static int tr_again(pwhere)
int *pwhere;
{
int res = 1;
if(Tracing_now){
pr_string(" Again ");
out_node(NODEPTR_HEAD(Goals),SubstGoal);
res = trace_pause(pwhere,CAN_SKIP);
}
return(res);
}
static void tr_fail()
{
if(!Tracing_now)
{
while( Parent > LastBack && Parent > QueryCframe)
{
Goals = FRAME_GOALS(Parent);
Parent = FRAME_PARENT(Parent);
if(Skip_above >= Parent){
Tracing_now = Copy_tracing_now;
SubstGoal = FRAME_SUBST(Parent);
break;
}
}/* while */
}/* if */
if(!Tracing_now && (Parent <= Skip_above))
Tracing_now = Copy_tracing_now;
if(Tracing_now)
{
if( ! IS_NIL(Goals)){
pr_string(" Fail ");
out_node(NODEPTR_HEAD(Goals),SubstGoal);
pr_string("\n");
}
/* print fail of Parent goals at this point */
while( Parent > LastBack && Parent > QueryCframe)
{
Goals = FRAME_GOALS(Parent);
Parent = FRAME_PARENT(Parent);
if( ! IS_NIL(Goals)){
pr_string(" Fail ");
out_node(NODEPTR_HEAD(Goals),FRAME_SUBST(Parent));
pr_string("\n");
}
}
}
}
/******************************************************************************
function trace_clause()
******************************************************************************/
void trace_clause(clauseptr)
clause_ptr_t clauseptr;
{
pr_string("Trying rule ");
pr_clause(clauseptr);
}
#endif /* #if TRACE_CAPABILITY */
/*******************************************************************
lush()
Lush resolution algorithm.
This routine tries to solve the query and sets the stacks aworking.
Probably the most important routine.
Warning:
If you add lots of features this becomes a big function,
you may have to set a switch in your compiler to handle the size
of the function or it will runout of space.
Microsoft and Zortech and Mark Williams have this.
Turbo C seems not to need a special option .
*******************************************************************/
int lush(first_time)
int first_time;
{
int retval;
int where;
ENTER("lush");
Unleash_flag = 0;
if(first_time != TRUE)
goto BACKTRACK; /* e.g. if you want to see a 2nd solution */
SELECT_GOAL:
if(IS_FACT(Curr_clause)) /* no goals - came to leaf of proof tree */
{
Goals = FRAME_GOALS(LastCframe);
TRACE(tr_immediate_exit(&where));
Goals = NODEPTR_TAIL(Goals);
while(Parent != QueryCframe && IS_NIL(Goals))
{
Goals = FRAME_GOALS(Parent);
Parent = FRAME_PARENT(Parent);
TRACE(tr_exit(&where));
Goals = NODEPTR_TAIL(Goals);
}
Subst_goals = FRAME_SUBST(Parent);
if(IS_NIL(Goals))
{
return(SUCCESS_LUSH_RETURN);
}
}
else /* the clause just entered has conditions, euh, goals */
{
Goals = CLAUSEPTR_GOALS(Curr_clause);
Parent = LastCframe;
Subst_goals = OldSubstTop;
}
/* Now you've got the goal (via Goals)
determine what the predicate is : */
Predicate = determine_predicate();/* argument list updated here */
TRACE(tr_call(&where));
ND_builtin_next_nodeptr = NULL;
if(Predicate == NULL)
{
retval = ErrorGlobal;
goto BUILTIN_ACTION;
}
if(!IS_BUILTIN(Predicate))
{
Candidate = ATOMPTR_CLAUSE(Predicate);
/* - this was the FIRST clause */
#ifdef HUNTBUGS
if(Candidate!=NULL && !check_object((char *)Candidate))
{
Curr_outfile = stdout;
pr_string(Predicate->name);
pr_string(" is the predicate \n****************************************\n");
fatal("error1 in code");
}
#endif
#if TRACE_CAPABILITY
if(Trace_flag > 0 && Candidate == NULL)
{
pr_string(" Undefined ");
pr_string(Predicate->name);
pr_string("\n");
}
}
#endif
SELECT_CLAUSE: /* we can come here from BACTRACK and Candidate can be
set in the backtrack section too */
if(IS_BUILTIN(Predicate))
{
/* OldSubstTop = Subst_ptr;
OldTrailTop = Trail_ptr;
Let the builtin do this if necessary
*/
retval = do_builtin(ATOMPTR_BUILTIN(Predicate));
BUILTIN_ACTION:
switch( retval)
{
case FALSE:
goto BACKTRACK;
case TRUE:
Curr_clause = Bltn_pseudo_clause;
LastCframe = Dyn_ptr; /* top of stack */
goto DFRAME;
case ABORT:
reset_zones();
return(ABNORMAL_LUSH_RETURN);
case CRASH:
dump_ancestors( LastCframe );
reset_zones();
return(ABNORMAL_LUSH_RETURN);
case QUIT:
return(FINAL_LUSH_RETURN);
case ND_SUCCESS:
Curr_clause = Bltn_pseudo_clause;
LastCframe = Dyn_ptr;
my_Dyn_alloc(SIZE_NDCFRAME);
Deterministic_flag = 0; /* maybe there is a choice point */
FRAME_PARENT(LastCframe) = Parent;
FRAME_SUBST(LastCframe) = OldSubstTop;
FRAME_GOALS(LastCframe) = Goals;
FRAME_ND_BLTIN_NEXT(LastCframe) = ND_builtin_next_nodeptr;
/* It is the builtin's responsability to update
ND_builtin_next_nodeptr
*/
FRAME_BACKTRACK(LastCframe) = LastBack;
FRAME_TRAIL(LastCframe) = OldTrailTop;
FRAME_TYPE(LastCframe) = ND_BUILTIN;
LastBack = LastCframe;
goto SELECT_GOAL;
}
}
else /* It's not builtin so look amongst candidate clauses for
the one whose head will unify with the goal.
*/
{
while (Candidate != NULL)
{
#if TRACE_CAPABILITY
if(Trace_flag >= 2 && Tracing_now)
trace_clause(Candidate);
#endif
OldSubstTop = Subst_ptr; /* save for backtrack */
OldTrailTop = Trail_ptr; /* ditto */
my_Subst_alloc((unsigned int)
CLAUSEPTR_NVARS(Candidate));
if(!unify(NODEPTR_TAIL(CLAUSEPTR_HEAD(Candidate)),
(subst_ptr_t)OldSubstTop,
Arguments, (subst_ptr_t)SubstGoal))
{/* shallow backtrack, reset substitution and
try next clause
*/
reset_trail(OldTrailTop);
Subst_ptr = OldSubstTop;
Candidate = CLAUSEPTR_NEXT(Candidate);
}
else
{/* successful unification */
Nunifications++; /* statistics only */
Curr_clause = Candidate;
goto CFRAME_CREATION;
}
}/* end while */
goto BACKTRACK; /* no candidate found */
}/* end it's not builtin */
CFRAME_CREATION:
LastCframe = Dyn_ptr;
if(CLAUSEPTR_NEXT(Candidate) == NULL
#if TRACE_CAPABILITY
&& !ReverseTraceMode
#endif
)/* it's not a backtrack point.
Mild bug: This ignores possible
assertz(Predicate(... calls in the clause
To fix this bug I guess I would need a flag.
*/
{/* deterministic frame */
DFRAME:
my_Dyn_alloc(SIZE_DCFRAME);
FRAME_PARENT(LastCframe) = Parent;
FRAME_SUBST(LastCframe) = OldSubstTop;
FRAME_GOALS(LastCframe) = Goals;
#ifdef DFRAMES_HAVE_TYPE_FIELD
FRAME_TYPE(LastCframe) = D_FRAME;
#endif
}
else
{ /* non deterministic frame */
my_Dyn_alloc(SIZE_NDCFRAME);
Deterministic_flag = 0; /* maybe there is a choice point */
FRAME_PARENT(LastCframe) = Parent;
FRAME_SUBST(LastCframe) = OldSubstTop;
FRAME_GOALS(LastCframe) = Goals;
FRAME_CLAUSE(LastCframe) = Curr_clause;
FRAME_BACKTRACK(LastCframe) = LastBack;
FRAME_TRAIL(LastCframe) = OldTrailTop;
FRAME_TYPE(LastCframe) = ND_CLAUSE;
LastBack = LastCframe;
}
goto SELECT_GOAL;
BACKTRACK:
#if TRACE_CAPABILITY
if(Trace_flag > 0)
{
tr_fail();
}
#endif
if(LastBack < BASE_CSTACK)
{
return(FAIL_LUSH_RETURN);
}
Parent = FRAME_PARENT(LastBack);
Subst_goals = FRAME_SUBST(Parent);
Goals = FRAME_GOALS(LastBack);
Predicate = determine_predicate();
Dyn_ptr = LastBack;
Subst_ptr = FRAME_SUBST(LastBack);
OldTrailTop = FRAME_TRAIL(LastBack);
reset_trail(OldTrailTop);
if(FRAME_TYPE(LastBack) == ND_CLAUSE)
{
Candidate = FRAME_CLAUSE(LastBack);
Candidate = CLAUSEPTR_NEXT(Candidate);
}
else
{
assert(FRAME_TYPE(LastBack) == ND_BUILTIN);
ND_builtin_next_nodeptr = FRAME_ND_BLTIN_NEXT(LastBack);
}
LastBack = FRAME_BACKTRACK(LastBack);
TRACE(tr_redo(&where));
#ifdef HUNTBUGS
if(Candidate!=NULL && !check_object((char *)Candidate))
{
dump_ancestors( LastCframe );
fatal("error2 in code");
}
#endif
goto SELECT_CLAUSE;
#if TRACE_CAPABILITY
/*
25/12/91
This is for debugging only:
When tracing you may have gone too far, so you might want
to step back to the parent by answering 'P' during trace.
In that case this is where execution goes to.
This is different from backtracking because the latter tries
the next clause.
*/
AGAIN:
assert(ReverseTraceMode);
Parent = FRAME_PARENT(BackFrame);
Subst_goals = FRAME_SUBST(Parent);
Goals = FRAME_GOALS(BackFrame);
Predicate = determine_predicate();
Dyn_ptr = BackFrame;
Subst_ptr = FRAME_SUBST(BackFrame);
OldTrailTop = FRAME_TRAIL(BackFrame);
reset_trail(OldTrailTop);
if(FRAME_TYPE(BackFrame) == ND_CLAUSE)
{
Candidate = FRAME_CLAUSE(BackFrame);
/* Candidate = CLAUSEPTR_NEXT(Candidate); */
}
else
if(FRAME_TYPE(BackFrame) == ND_BUILTIN)