00001
00002
00003
00004
00005
00006 #ifndef lint
00007 static char vcid[] = "$Id: login.c,v 1.4 1995/01/14 00:25:33 duchier Exp $";
00008 #endif
00009
00010 #include "extern.h"
00011 #include "login.h"
00012 #include "trees.h"
00013 #include "copy.h"
00014 #include "parser.h"
00015 #include "token.h"
00016 #include "print.h"
00017 #ifndef OS2_PORT
00018 #include "built_ins.h"
00019 #else
00020 #include "built_in.h"
00021 #endif
00022 #include "types.h"
00023 #include "lefun.h"
00024 #include "memory.h"
00025 #include "error.h"
00026 #include "xpred.h"
00027 #include "modules.h"
00028 #ifndef OS2_PORT
00029 #include "interrupt.h"
00030 #else
00031 #include "interrup.h"
00032 #endif
00033
00034 int global_unify_attr();
00035 int global_unify();
00036
00037
00038
00039 #define CLEAN_TRAIL
00040
00041
00042 long clean_iter=0;
00043 long clean_succ=0;
00044
00045 ptr_stack undo_stack;
00046 ptr_choice_point choice_stack;
00047
00048 #ifdef TS
00049
00050
00051 unsigned long global_time_stamp=INIT_TIME_STAMP;
00052 #endif
00053
00054 ptr_goal goal_stack,aim;
00055
00056
00057 long goal_count;
00058 #ifndef OS2_PORT
00059 struct tms start_time,end_time;
00060 #else
00061 float start_time,end_time;
00062 #endif
00063 long ignore_eff;
00064
00065 long assert_first;
00066 long assert_ok;
00067
00068 long main_loop_ok;
00069 long xeventdelay;
00070 long xcount;
00071
00072 long more_u_attr;
00073 long more_v_attr;
00074
00075 long u_func,v_func;
00076 long new_stat;
00077
00078 char prompt_buffer[PROMPT_BUFFER];
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 void get_two_args(t,a,b)
00096 ptr_node t;
00097 ptr_psi_term *a;
00098 ptr_psi_term *b;
00099 {
00100 ptr_node n;
00101
00102 *a=NULL;
00103 *b=NULL;
00104 if (t) {
00105 if (t->key==one) {
00106 *a=(ptr_psi_term )t->data;
00107 n=t->right;
00108 if (n)
00109 if (n->key==two)
00110 *b=(ptr_psi_term )n->data;
00111 else {
00112 n=find(featcmp,two,t);
00113 if(n==NULL)
00114 *b=NULL;
00115 else
00116 *b=(ptr_psi_term )n->data;
00117 }
00118 else
00119 *b=NULL;
00120 }
00121 else {
00122 n=find(featcmp,one,t);
00123 if (n==NULL)
00124 *a=NULL;
00125 else
00126 *a=(ptr_psi_term )n->data;
00127 n=find(featcmp,two,t);
00128 if (n==NULL)
00129 *b=NULL;
00130 else
00131 *b=(ptr_psi_term )n->data;
00132 }
00133 }
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 void get_one_arg(t,a)
00145 ptr_node t;
00146 ptr_psi_term *a;
00147 {
00148 ptr_node n;
00149
00150 *a=NULL;
00151 if (t) {
00152 if (t->key==one) {
00153 *a=(ptr_psi_term)t->data;
00154 }
00155 else {
00156 n=find(featcmp,one,t);
00157 if (n==NULL)
00158 *a=NULL;
00159 else
00160 *a=(ptr_psi_term)n->data;
00161 }
00162 }
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 void get_one_arg_addr(t,a)
00174 ptr_node t;
00175 ptr_psi_term **a;
00176 {
00177 ptr_node n;
00178 ptr_psi_term *b;
00179
00180 *a=NULL;
00181 if (t) {
00182 if (t->key==one)
00183 *a= (ptr_psi_term *)(&t->data);
00184 else {
00185 n=find(featcmp,one,t);
00186 if (n==NULL)
00187 *a=NULL;
00188 else
00189 *a= (ptr_psi_term *)(&n->data);
00190 }
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 void add_rule(head,body,typ)
00207 ptr_psi_term head;
00208 ptr_psi_term body;
00209 def_type typ;
00210 {
00211 psi_term succ;
00212 ptr_psi_term head2;
00213 ptr_definition def;
00214 ptr_pair_list p, *p2;
00215
00216 if (!body && typ==predicate) {
00217 succ.type=succeed;
00218 succ.value=NULL;
00219 succ.coref=NULL;
00220 succ.resid=NULL;
00221 succ.attr_list=NULL;
00222 body= ≻
00223 }
00224
00225 deref_ptr(head);
00226 head2=head;
00227
00228
00229
00230
00231 if (redefine(head)) {
00232
00233 def=head->type;
00234
00235 if (def->type==undef || def->type==typ)
00236
00237
00238 if(TRUE
00239
00240
00241
00242 ) {
00243 if (def->rule && (unsigned long)def->rule<=MAX_BUILT_INS) {
00244 Errorline("the built-in %T '%s' may not be redefined.\n",
00245 def->type, def->keyword->symbol);
00246 }
00247 else {
00248 def->type=typ;
00249
00250
00251 p=HEAP_ALLOC(pair_list);
00252 clear_copy();
00253
00254
00255
00256 p->a=quote_copy(head2,HEAP);
00257 p->b=quote_copy(body,HEAP);
00258
00259 if (assert_first) {
00260 p->next=def->rule;
00261 def->rule=p;
00262 }
00263 else {
00264 p->next=NULL;
00265 p2= &(def->rule);
00266 while (*p2) {
00267 p2= &((*p2)->next);
00268 }
00269 *p2=p;
00270 }
00271 assert_ok=TRUE;
00272 }
00273 }
00274 else {
00275 Errorline("the %T '%s' may not be redefined from within module %s.\n",
00276 def->type,
00277 def->keyword->combined_name,
00278 current_module->module_name);
00279 }
00280 else {
00281 Errorline("the %T '%s' may not be redefined as a %T.\n",
00282 def->type, def->keyword->symbol, typ);
00283 }
00284 }
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 void assert_rule(t,typ)
00296 psi_term t;
00297 def_type typ;
00298 {
00299 ptr_psi_term head;
00300 ptr_psi_term body;
00301
00302 get_two_args(t.attr_list,&head,&body);
00303 if (head)
00304 if (body)
00305 add_rule(head,body,typ);
00306 else {
00307 Syntaxerrorline("body missing in definition of %T '%P'.\n", typ, head);
00308 }
00309 else {
00310 Syntaxerrorline("head missing in definition of %T.\n",typ);
00311 }
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 void assert_clause(t)
00326 ptr_psi_term t;
00327 {
00328 ptr_psi_term arg1,arg2;
00329 char *str;
00330
00331 assert_ok=FALSE;
00332 deref_ptr(t);
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 if (equ_tok((*t),":-"))
00354 assert_rule((*t),predicate);
00355 else
00356 if (equ_tok((*t),"->"))
00357 assert_rule((*t),function);
00358 else
00359 if (equ_tok((*t),"::"))
00360 assert_attributes(t);
00361 else
00362
00363 #ifdef CLIFE
00364 if (equ_tok((*t),"block_struct"))
00365 define_block(t);
00366 else
00367 #endif
00368
00369
00370
00371
00372
00373
00374 if (equ_tok((*t),"<|") || equ_tok((*t),":="))
00375 assert_complicated_type(t);
00376 else
00377 add_rule(t,NULL,predicate);
00378
00379
00380 }
00381
00382
00383
00384
00385
00386
00387
00388 void start_chrono()
00389 {
00390 times(&start_time);
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418 void push_ptr_value(t,p)
00419 type_ptr t;
00420 GENERIC *p;
00421 {
00422 ptr_stack n;
00423
00424 assert((GENERIC)p<heap_pointer);
00425
00426 assert(VALID_ADDRESS(p));
00427 if (p < (GENERIC *)choice_stack || p > (GENERIC *)stack_pointer)
00428 {
00429 n=STACK_ALLOC(stack);
00430 n->type=t;
00431 n->a= (GENERIC)p;
00432 n->b= *p;
00433 n->next=undo_stack;
00434 undo_stack=n;
00435 }
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 void push_def_ptr_value(q,p)
00448 ptr_psi_term q;
00449 GENERIC *p;
00450 {
00451 ptr_stack m,n;
00452
00453 assert(VALID_ADDRESS(q));
00454 assert(VALID_ADDRESS(p));
00455 #ifdef TS
00456 if (TRAIL_CONDITION(q) &&
00457
00458 (p < (GENERIC *)choice_stack || p > (GENERIC *)stack_pointer))
00459 {
00460 #define TRAIL_TS
00461 #ifdef TRAIL_TS
00462
00463 assert((GENERIC)q<heap_pointer);
00464
00465 m=STACK_ALLOC(stack);
00466 m->type=int_ptr;
00467 m->a= (GENERIC) &(q->time_stamp);
00468 m->b= (GENERIC) (q->time_stamp);
00469 m->next=undo_stack;
00470 n=STACK_ALLOC(stack);
00471 n->type=def_ptr;
00472 n->a= (GENERIC)p;
00473 n->b= *p;
00474 n->next=m;
00475 undo_stack=n;
00476 #else
00477 n=STACK_ALLOC(stack);
00478 n->type=def_ptr;
00479 n->a= (GENERIC)p;
00480 n->b= *p;
00481 n->next=undo_stack;
00482 undo_stack=n;
00483 #endif
00484 q->time_stamp=global_time_stamp;
00485 }
00486 #else
00487 push_ptr_value(def_ptr,p);
00488 #endif
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 void push_psi_ptr_value(q,p)
00502 ptr_psi_term q;
00503 GENERIC *p;
00504 {
00505 ptr_stack m,n;
00506
00507 assert(VALID_ADDRESS(q));
00508 assert(VALID_ADDRESS(p));
00509 #ifdef TS
00510 if (TRAIL_CONDITION(q) &&
00511
00512 (p < (GENERIC *)choice_stack || p > (GENERIC *)stack_pointer))
00513 {
00514 #define TRAIL_TS
00515 #ifdef TRAIL_TS
00516 m=STACK_ALLOC(stack);
00517 m->type=int_ptr;
00518 m->a= (GENERIC) &(q->time_stamp);
00519 m->b= (GENERIC) (q->time_stamp);
00520 m->next=undo_stack;
00521 n=STACK_ALLOC(stack);
00522 n->type=psi_term_ptr;
00523 n->a= (GENERIC)p;
00524 n->b= *p;
00525 n->next=m;
00526 undo_stack=n;
00527 #else
00528 n=STACK_ALLOC(stack);
00529 n->type=psi_term_ptr;
00530 n->a= (GENERIC)p;
00531 n->b= *p;
00532 n->next=undo_stack;
00533 undo_stack=n;
00534 #endif
00535 q->time_stamp=global_time_stamp;
00536 }
00537 #else
00538 push_ptr_value(psi_term_ptr,p);
00539 #endif
00540 }
00541
00542
00543
00544
00545
00546 void push_ptr_value_global(t,p)
00547 type_ptr t;
00548 GENERIC *p;
00549 {
00550 ptr_stack n;
00551
00552 assert(VALID_ADDRESS(p));
00553 n=STACK_ALLOC(stack);
00554 n->type=t;
00555 n->a= (GENERIC)p;
00556 n->b= *p;
00557 n->next=undo_stack;
00558 undo_stack=n;
00559 }
00560
00561
00562
00563
00564
00565
00566
00567
00568 void push_window(type,disp,wind)
00569 long type,disp,wind;
00570 {
00571 ptr_stack n;
00572
00573 assert(type & undo_action);
00574 n=STACK_ALLOC(stack);
00575 n->type=type;
00576 n->a=(GENERIC)disp;
00577 n->b=(GENERIC)wind;
00578 n->next=undo_stack;
00579 undo_stack=n;
00580 }
00581
00582
00583
00584
00585
00586
00587
00588
00589 void push2_ptr_value(t,p,v)
00590 type_ptr t;
00591 GENERIC *p;
00592 GENERIC v;
00593 {
00594 ptr_stack n;
00595
00596 if (p<(GENERIC *)choice_stack || p>(GENERIC *)stack_pointer) {
00597 n=STACK_ALLOC(stack);
00598 n->type=t;
00599 n->a= (GENERIC)p;
00600 n->b= (GENERIC)v;
00601 n->next=undo_stack;
00602 undo_stack=n;
00603 }
00604 }
00605
00606
00607
00608
00609
00610
00611
00612
00613 void push_goal(t,a,b,c)
00614 goals t;
00615 ptr_psi_term a;
00616 ptr_psi_term b;
00617 GENERIC c;
00618 {
00619 ptr_goal thegoal;
00620
00621 thegoal=STACK_ALLOC(goal);
00622
00623 thegoal->type=t;
00624 thegoal->a=a;
00625 thegoal->b=b;
00626 thegoal->c=c;
00627 thegoal->next=goal_stack;
00628 thegoal->pending=FALSE;
00629
00630 goal_stack=thegoal;
00631 }
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 void push_choice_point(t,a,b,c)
00650 goals t;
00651 ptr_psi_term a;
00652 ptr_psi_term b;
00653 GENERIC c;
00654 {
00655 ptr_goal alternative;
00656 ptr_choice_point choice;
00657 GENERIC top;
00658
00659 alternative=STACK_ALLOC(goal);
00660
00661 alternative->type=t;
00662 alternative->a=a;
00663 alternative->b=b;
00664 alternative->c=c;
00665 alternative->next=goal_stack;
00666 alternative->pending=FALSE;
00667
00668 top=stack_pointer;
00669
00670 choice=STACK_ALLOC(choice_point);
00671
00672 choice->undo_point=undo_stack;
00673 choice->goal_stack=alternative;
00674 choice->next=choice_stack;
00675 choice->stack_top=top;
00676
00677 #ifdef TS
00678 choice->time_stamp=global_time_stamp;
00679 global_time_stamp++;
00680 #endif
00681
00682 choice_stack=choice;
00683 }
00684
00685
00686 #define RESTORE_TIME_STAMP global_time_stamp=\
00687 choice_stack?choice_stack->time_stamp:INIT_TIME_STAMP;
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704 void undo(limit)
00705 ptr_stack limit;
00706 {
00707
00708
00709
00710
00711 while ((unsigned long)undo_stack>(unsigned long)limit) {
00712 #ifdef X11
00713 if (undo_stack->type & undo_action) {
00714
00715 switch(undo_stack->type) {
00716 case destroy_window:
00717 x_destroy_window((unsigned long)undo_stack->a,(unsigned long)undo_stack->b);
00718 break;
00719 case show_window:
00720 x_show_window((unsigned long)undo_stack->a,(unsigned long)undo_stack->b);
00721 break;
00722 case hide_window:
00723 x_hide_window((unsigned long)undo_stack->a,(unsigned long)undo_stack->b);
00724 break;
00725 case show_subwindow:
00726 x_show_subwindow((unsigned long)undo_stack->a,(unsigned long)undo_stack->b);
00727 break;
00728 case hide_subwindow:
00729 x_hide_subwindow((unsigned long)undo_stack->a,(unsigned long)undo_stack->b);
00730 break;
00731 }
00732 }
00733 else
00734 #endif
00735
00736 *((GENERIC *)(undo_stack->a))=undo_stack->b;
00737 undo_stack=undo_stack->next;
00738 }
00739 }
00740
00741
00742
00743
00744
00745
00746
00747
00748 void undo_actions()
00749 {
00750 ptr_stack u=undo_stack;
00751
00752 Errorline("undo_actions should not be called.\n");
00753 undo(NULL);
00754 return;
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773 }
00774
00775
00776
00777
00778
00779
00780
00781
00782 void backtrack()
00783 {
00784 long gts;
00785
00786 goal_stack=choice_stack->goal_stack;
00787 undo(choice_stack->undo_point);
00788 #ifdef TS
00789
00790 #endif
00791 stack_pointer=choice_stack->stack_top;
00792 choice_stack=choice_stack->next;
00793 resid_aim=NULL;
00794
00795
00796
00797
00798
00799
00800
00801
00802 }
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815 static void clean_trail(cutpt)
00816 ptr_choice_point cutpt;
00817 {
00818 ptr_stack *prev,u,cut_limit;
00819 GENERIC cut_sp;
00820
00821 u = undo_stack;
00822 prev = &undo_stack;
00823 if (cutpt) {
00824 cut_sp = cutpt->stack_top;
00825 cut_limit = cutpt->undo_point;
00826 }
00827 else {
00828 cut_sp = mem_base;
00829 cut_limit = NULL;
00830 }
00831
00832 while ((unsigned long)u > (unsigned long)cut_limit) {
00833 clean_iter++;
00834 if (!(u->type & undo_action) && VALID_RANGE(u->a) &&
00835 (unsigned long)u->a>(unsigned long)cut_sp && (unsigned long)u->a<=(unsigned long)stack_pointer) {
00836 *prev = u->next;
00837 clean_succ++;
00838 }
00839 prev = &(u->next);
00840 u = u->next;
00841 }
00842 }
00843
00844
00845
00846
00847
00848
00849
00850 void clean_undo_window(disp,wind)
00851 long disp,wind;
00852 {
00853 ptr_stack *prev,u;
00854 ptr_choice_point c;
00855
00856 #ifdef X11
00857
00858 u = undo_stack;
00859 prev = &undo_stack;
00860 while (u) {
00861 if ((u->type & undo_action) &&
00862 ((unsigned long)u->a==disp) && ((unsigned long)u->b==wind)) {
00863 *prev = u->next;
00864 }
00865 prev = &(u->next);
00866 u = u->next;
00867 }
00868
00869
00870
00871
00872
00873 c = choice_stack;
00874 while (c) {
00875 u = c->undo_point;
00876 prev = &(c->undo_point);
00877 while (u && (u->type & undo_action) &&
00878 ((unsigned long)u->a==disp) && ((unsigned long)u->b==wind)) {
00879 *prev = u->next;
00880 prev = &(u->next);
00881 u = u->next;
00882 }
00883 c = c->next;
00884 }
00885 #endif
00886 }
00887
00888
00889
00890
00891 void merge1(u,v)
00892 ptr_node *u,v;
00893 {
00894 long cmp;
00895 ptr_node temp;
00896
00897 if (v) {
00898 if (*u==NULL) {
00899
00900
00901
00902
00903 }
00904 else {
00905 cmp=featcmp((*u)->key,v->key);
00906 if (cmp==0) {
00907 if (v->right)
00908 merge1(&((*u)->right),v->right);
00909
00910 push_goal(unify,(*u)->data,v->data,NULL);
00911
00912 if (v->left)
00913 merge1(&((*u)->left),v->left);
00914 }
00915 else if (cmp>0) {
00916 temp=v->right;
00917 v->right=NULL;
00918 merge1(&((*u)->left),v);
00919 merge1(u,temp);
00920 v->right=temp;
00921 }
00922 else {
00923 temp=v->left;
00924 v->left=NULL;
00925 merge1(&((*u)->right),v);
00926 merge1(u,temp);
00927 v->left=temp;
00928 }
00929 }
00930 }
00931 else if (*u!=NULL) {
00932
00933 }
00934 }
00935
00936
00937
00938
00939
00940
00941
00942 void merge2(u,v)
00943 ptr_node *u,v;
00944 {
00945 long cmp;
00946 ptr_node temp;
00947
00948 if (v) {
00949 if (*u==NULL) {
00950 ptr_psi_term t;
00951 merge2(u,v->right);
00952 t = (ptr_psi_term) v->data;
00953 deref2_rec_eval(t);
00954 merge2(u,v->left);
00955 }
00956 else {
00957 cmp=featcmp((*u)->key,v->key);
00958 if (cmp==0) {
00959
00960 merge2(&((*u)->right),v->right);
00961
00962
00963 merge2(&((*u)->left),v->left);
00964 }
00965 else if (cmp>0) {
00966 temp=v->right;
00967 v->right=NULL;
00968 merge2(&((*u)->left),v);
00969 merge2(u,temp);
00970 v->right=temp;
00971 }
00972 else {
00973 temp=v->left;
00974 v->left=NULL;
00975 merge2(&((*u)->right),v);
00976 merge2(u,temp);
00977 v->left=temp;
00978 }
00979 }
00980 }
00981 else if (*u!=NULL) {
00982 ptr_psi_term t;
00983 merge2(&((*u)->right),v);
00984 t = (ptr_psi_term) (*u)->data;
00985 deref2_rec_eval(t);
00986 merge2(&((*u)->left),v);
00987 }
00988 }
00989
00990
00991
00992 void merge3(u,v)
00993 ptr_node *u,v;
00994 {
00995 long cmp;
00996 ptr_node temp;
00997
00998 if (v) {
00999 if (*u==NULL) {
01000 push_ptr_value(int_ptr,u);
01001 (*u)=STACK_ALLOC(node);
01002 **u= *v;
01003 more_v_attr=TRUE;
01004 }
01005 else {
01006 ptr_psi_term t1,t2;
01007
01008 cmp=featcmp((*u)->key,v->key);
01009 if (cmp==0) {
01010 if (v->right)
01011 merge3(&((*u)->right),v->right);
01012
01013 t1 = (ptr_psi_term) (*u)->data;
01014
01015 deref2_eval(t1);
01016
01017
01018
01019 if (v->left)
01020 merge3(&((*u)->left),v->left);
01021 }
01022 else if (cmp>0) {
01023 temp=v->right;
01024 v->right=NULL;
01025 merge3(&((*u)->left),v);
01026 merge3(u,temp);
01027 v->right=temp;
01028 }
01029 else {
01030 temp=v->left;
01031 v->left=NULL;
01032 merge3(&((*u)->right),v);
01033 merge3(u,temp);
01034 v->left=temp;
01035 }
01036 }
01037 }
01038 else if (*u!=NULL) {
01039 more_u_attr=TRUE;
01040 }
01041 }
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055
01056
01057
01058 #if FALSE
01059
01060 void merge(u,v)
01061 ptr_node *u,v;
01062 {
01063 long cmp;
01064 ptr_node temp;
01065
01066 if (v) {
01067 if (*u==NULL) {
01068 ptr_psi_term t;
01069 merge(u,v->right);
01070
01071 push_ptr_value(int_ptr,u);
01072 (*u)=STACK_ALLOC(node);
01073 **u= *v;
01074 more_v_attr=TRUE;
01075
01076 t = (ptr_psi_term) v->data;
01077 deref2_rec_eval(t);
01078 merge(u,v->left);
01079 }
01080 else {
01081 cmp=featcmp((*u)->key,v->key);
01082 if (cmp==0) {
01083
01084 merge(&((*u)->right),v->right);
01085
01086 push_goal(unify,(*u)->data,v->data,NULL);
01087
01088
01089 merge(&((*u)->left),v->left);
01090 }
01091 else if (cmp>0) {
01092 temp=v->right;
01093 v->right=NULL;
01094 merge(&((*u)->left),v);
01095 merge(u,temp);
01096 v->right=temp;
01097 }
01098 else {
01099 temp=v->left;
01100 v->left=NULL;
01101 merge(&((*u)->right),v);
01102 merge(u,temp);
01103 v->left=temp;
01104 }
01105 }
01106 }
01107 else if (*u!=NULL) {
01108 ptr_psi_term t;
01109 merge(&((*u)->right),v);
01110 t = (ptr_psi_term) (*u)->data;
01111 deref2_rec_eval(t);
01112 merge(&((*u)->left),v);
01113
01114 more_u_attr=TRUE;
01115 }
01116 }
01117 #endif
01118
01119 void merge(u,v)
01120 ptr_node *u,v;
01121 {
01122 merge1(u,v);
01123 merge2(u,v);
01124 merge3(u,v);
01125 }
01126
01127
01128 void merge_unify(u,v)
01129 ptr_node *u,v;
01130 {
01131 merge1(u,v);
01132 merge3(u,v);
01133 }
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143 void show_count()
01144 {
01145 float t;
01146
01147 if (verbose) {
01148 printf(" [");
01149
01150 times(&end_time);
01151 #ifndef OS2_PORT
01152 t = (end_time.tms_utime - start_time.tms_utime)/60.0;
01153 #else
01154 t = (end_time - start_time)/60.0;
01155 #endif
01156 printf("%1.3fs cpu, %ld goal%s",t,goal_count,(goal_count!=1?"s":""));
01157
01158 if (t!=0.0) printf(" (%0.0f/s)",goal_count/t);
01159
01160 printf(", %ld stack",sizeof(mem_base)*(stack_pointer-mem_base));
01161 printf(", %ld heap",sizeof(mem_base)*(mem_limit-heap_pointer));
01162
01163 printf("]");
01164 }
01165
01166 if(NOTQUIET) {
01167 printf("\n");
01168 stack_info(stdout);
01169 }
01170
01171 goal_count=0;
01172 }
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189 void fetch_def(u, allflag)
01190 ptr_psi_term u;
01191 long allflag;
01192 {
01193 ptr_triple_list prop;
01194 ptr_psi_term v,w;
01195 ptr_definition utype;
01196
01197
01198 push2_ptr_value(int_ptr,&(u->status),(u->status & SMASK));
01199 u->status=(4 & SMASK) | (u->status & RMASK);
01200
01201 utype=u->type;
01202 prop=u->type->properties;
01203 if (prop) {
01204
01205 Traceline("fetching definition of %P\n",u);
01206
01207 while (prop) {
01208 if (allflag || prop->c==utype) {
01209 clear_copy();
01210 v=eval_copy(prop->a,STACK);
01211 w=eval_copy(prop->b,STACK);
01212
01213 if (w) push_goal(prove,w,DEFRULES,NULL);
01214
01215 deref_ptr(v);
01216 v->status=4;
01217 push_goal(unify,u,v,NULL);
01218 i_eval_args(v->attr_list);
01219 }
01220 prop=prop->next;
01221 }
01222 }
01223 }
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249 void fetch_def_lazy(u, old1, old2, old1attr, old2attr, old1stat, old2stat)
01250 ptr_psi_term u;
01251 ptr_definition old1, old2;
01252 ptr_node old1attr, old2attr;
01253 long old1stat, old2stat;
01254 {
01255 ptr_triple_list prop;
01256 ptr_psi_term v,w;
01257 long checked1, checked2;
01258 long m1, m2;
01259
01260 if (!u->type->always_check) if (u->attr_list==NULL) return;
01261
01262 push_ptr_value(int_ptr,&(u->status));
01263 u->status=4;
01264
01265 prop=u->type->properties;
01266 if (prop) {
01267 Traceline("fetching partial definition of %P\n",u);
01268
01269 checked1 = old1attr || old1->always_check;
01270 checked2 = old2attr || old2->always_check;
01271
01272
01273
01274
01275 while (prop) {
01276
01277
01278 if (!checked1) m1=FALSE; else matches(old1,prop->c,&m1);
01279 if (!checked2) m2=FALSE; else matches(old2,prop->c,&m2);
01280 if (!m1 && !m2) {
01281
01282
01283 clear_copy();
01284 v=eval_copy(prop->a,STACK);
01285 w=eval_copy(prop->b,STACK);
01286
01287 if (w) push_goal(prove,w,DEFRULES,NULL);
01288
01289 deref_ptr(v);
01290 v->status=4;
01291 push_goal(unify,u,v,NULL);
01292 i_eval_args(v->attr_list);
01293 }
01294 prop=prop->next;
01295 }
01296 }
01297 }
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320 long unify_body();
01321
01322 long unify_aim_noeval()
01323 {
01324 return unify_body(FALSE);
01325 }
01326
01327 long unify_aim()
01328 {
01329 return unify_body(TRUE);
01330 }
01331
01332 long unify_body(eval_flag)
01333 long eval_flag;
01334 {
01335 long success=TRUE,compare;
01336 ptr_psi_term u,v,tmp;
01337 ptr_list lu,lv;
01338 REAL r;
01339 ptr_definition new_type,old1,old2;
01340 ptr_node old1attr, old2attr;
01341 ptr_int_list new_code;
01342 ptr_int_list d=NULL;
01343 long old1stat,old2stat;
01344
01345 u=(ptr_psi_term )aim->a;
01346 v=(ptr_psi_term )aim->b;
01347
01348 deref_ptr(u);
01349 deref_ptr(v);
01350
01351 Traceline("unify %P with %P\n",u,v);
01352
01353 if (eval_flag) {
01354 deref(u);
01355 deref(v);
01356 }
01357
01358 if (u!=v) {
01359
01360
01361 if (u>v) { tmp=v; v=u; u=tmp; }
01362
01363
01364 u_func=(u->type->type==function);
01365 v_func=(v->type->type==function);
01366 old1stat=u->status;
01367 old2stat=v->status;
01368
01369
01370
01371 if (u_func && u->status==4 && !(u->flags"ED_TRUE) && v->attr_list) {
01372 Errorline("attempt to unify with curried function %P\n", u);
01373 return FALSE;
01374 }
01375
01376 if (v_func && v->status==4 && !(v->flags"ED_TRUE) && u->attr_list) {
01377 Errorline("attempt to unify with curried function %P\n", v);
01378 return FALSE;
01379 }
01380
01381
01382 #ifdef ARITY
01383 arity_unify(u,v);
01384 #endif
01385
01386
01387 if((GENERIC)v>=heap_pointer)
01388 return global_unify(u,v);
01389
01390
01391
01392 success=(compare=glb(u->type,v->type,&new_type,&new_code));
01393
01394 if (success) {
01395
01396
01397 old1 = u->type;
01398 old2 = v->type;
01399 old1attr = u->attr_list;
01400 old2attr = v->attr_list;
01401
01402
01403 if (!new_type) {
01404 d=decode(new_code);
01405 if (d) {
01406 new_type=(ptr_definition )d->value;
01407 d=d->next;
01408 }
01409 else
01410 Errorline("undecipherable sort code.\n");
01411 }
01412
01413
01414 if (compare==1)
01415 if (u->value && !v->value)
01416 compare=2;
01417 else
01418 if (v->value && !u->value)
01419 compare=3;
01420
01421
01422 new_stat=4;
01423 switch (compare) {
01424 case 1:
01425 if (u->status <4 && v->status <4)
01426 new_stat=2;
01427 break;
01428 case 2:
01429 if (u->status<4)
01430 new_stat=2;
01431 break;
01432 case 3:
01433 if (v->status<4)
01434 new_stat=2;
01435 break;
01436 case 4:
01437 new_stat=2;
01438 break;
01439 }
01440
01441
01442
01443
01444
01445
01446
01447
01448
01449
01450
01451
01452 if (u->value && sub_type(new_type,integer)) {
01453 r= *(REAL *)u->value;
01454 success=(r==floor(r));
01455 }
01456 if (success && v->value && sub_type(new_type,integer)) {
01457 r= *(REAL *)v->value;
01458 success=(r==floor(r));
01459 }
01460
01461
01462 if (success) {
01463
01464 if (u->value!=v->value)
01465 if (!u->value) {
01466 compare=4;
01467 push_ptr_value(int_ptr,&(u->value));
01468 u->value=v->value;
01469 }
01470 else if (v->value) {
01471 if (overlap_type(new_type,real))
01472 success=(*((REAL *)u->value)==(*((REAL *)v->value)));
01473 else if (overlap_type(new_type,quoted_string))
01474 success=(strcmp((char *)u->value,(char *)v->value)==0);
01475 else if (overlap_type(new_type,sys_bytedata)) {
01476 unsigned long ulen = *((unsigned long *)u->value);
01477 unsigned long vlen = *((unsigned long *)v->value);
01478 success=(ulen==vlen &&
01479 (bcmp((char *)u->value,(char *)v->value,ulen)==0));
01480 }
01481 else if (u->type==cut && v->type==cut) {
01482 GENERIC mincut;
01483 mincut = (u->value<v->value?u->value:v->value);
01484 if (mincut!=u->value) {
01485 push_ptr_value(cut_ptr,&(u->value));
01486 u->value=mincut;
01487 }
01488 }
01489 else {
01490 Warningline("'%s' may not be unified.\n",new_type->keyword->symbol);
01491 success=FALSE;
01492 }
01493 }
01494 else
01495 compare=4;
01496 }
01497
01498
01499 if (success) {
01500
01501 push_psi_ptr_value(v,&(v->coref));
01502 v->coref=u;
01503
01504 if (!equal_types(u->type,new_type)) {
01505 push_ptr_value(def_ptr,&(u->type));
01506
01507
01508 u->type=new_type;
01509 }
01510
01511 if (u->status!=new_stat) {
01512 push_ptr_value(int_ptr,&(u->status));
01513 u->status=new_stat;
01514 }
01515
01516
01517 more_u_attr=FALSE;
01518 more_v_attr=FALSE;
01519
01520
01521 #ifdef ARITY
01522 arity_merge(u->attr_list,v->attr_list);
01523 #endif
01524
01525
01526 if (u->attr_list || v->attr_list)
01527 merge(&(u->attr_list),v->attr_list);
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543 if (v->flags"ED_TRUE && !(u->flags"ED_TRUE)) {
01544 push_ptr_value(int_ptr,&(u->flags));
01545 u->flags|=QUOTED_TRUE;
01546 }
01547
01548
01549
01550 if (u->resid)
01551 release_resid(u);
01552 if (v->resid)
01553 release_resid(v);
01554
01555
01556 if (d) {
01557 Traceline("pushing type disjunction choice point for %P\n",u);
01558 push_choice_point(type_disj,u,d,NULL);
01559 }
01560
01561
01562
01563
01564 if (new_stat<4 && u->type->type==type) {
01565
01566
01567
01568
01569
01570 fetch_def_lazy(u, old1, old2,
01571 old1attr, old2attr,
01572 old1stat, old2stat);
01573 }
01574 }
01575 }
01576 }
01577 return success;
01578 }
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588 long disjunct_aim()
01589 {
01590 ptr_psi_term u,v;
01591 ptr_list l;
01592 long success=TRUE;
01593
01594 printf("Call to disjunct_aim\nThis routine inhibited by RM: Dec 9 1992\n");
01595
01596 return success;
01597 }
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608 long prove_aim()
01609 {
01610 long success=TRUE;
01611 ptr_psi_term thegoal,head,body,arg1,arg2;
01612 ptr_pair_list rule;
01613
01614 thegoal=(ptr_psi_term )aim->a;
01615 rule=(ptr_pair_list )aim->b;
01616
01617 if (thegoal && rule) {
01618
01619 deref_ptr(thegoal);
01620
01621 if (thegoal->type!=and) {
01622 if (thegoal->type!=cut)
01623 if(thegoal->type!=life_or) {
01624
01625
01626
01627
01628 if(i_check_out(thegoal)) {
01629
01630 goal_stack=aim->next;
01631 goal_count++;
01632
01633 if ((unsigned long)rule==DEFRULES) {
01634 rule=(ptr_pair_list)thegoal->type->rule;
01635 if (thegoal->type->type==predicate) {
01636 if (!rule)
01637 success=FALSE;
01638 }
01639 else if ( thegoal->type->type==function
01640 || ( thegoal->type->type==type
01641 && sub_type(boolean,thegoal->type)
01642 )
01643 ) {
01644 if (thegoal->type->type==function && !rule)
01645
01646 success=FALSE;
01647 else {
01648 ptr_psi_term bool_pred;
01649 ptr_node a;
01650
01651
01652
01653 bool_pred=stack_psi_term(0);
01654 bool_pred->type=boolpredsym;
01655 bool_pred->attr_list=(a=STACK_ALLOC(node));
01656 a->key=one;
01657 a->left=a->right=NULL;
01658 a->data=(GENERIC) thegoal;
01659 push_goal(prove,bool_pred,DEFRULES,NULL);
01660 return success;
01661 }
01662 }
01663 else if (!thegoal->type->protected && thegoal->type->type==undef) {
01664
01665
01666 success=FALSE;
01667 }
01668 else if (thegoal->type==true || thegoal->type==false) {
01669
01670 success=(thegoal->type==true);
01671 return success;
01672 }
01673 else {
01674
01675
01676 ptr_psi_term call_handler;
01677
01678
01679
01680
01681
01682 call_handler=stack_psi_term(0);
01683 call_handler->type=call_handlersym;
01684 stack_add_psi_attr(call_handler,"1",thegoal);
01685 push_goal(prove,call_handler,DEFRULES,NULL);
01686 return success;
01687 }
01688 }
01689
01690 if (success) {
01691
01692 if ((unsigned long)rule<=MAX_BUILT_INS) {
01693
01694
01695 curried=FALSE;
01696 can_curry=TRUE;
01697 resid_vars=NULL;
01698
01699
01700 if (thegoal->type!=tracesym)
01701 Traceline("prove built-in %P\n", thegoal);
01702
01703 resid_aim=aim;
01704
01705 success=c_rule[(unsigned long)rule]();
01706
01707 if (curried)
01708 do_currying();
01709 else if (resid_vars)
01710 success=do_residuation_user();
01711 }
01712 else {
01713
01714
01715 deref_args(thegoal,set_empty);
01716
01717 Traceline("prove %P\n", thegoal);
01718
01719
01720 curried=FALSE;
01721 can_curry=TRUE;
01722 resid_vars=NULL;
01723
01724
01725 while (rule && (rule->a==NULL || rule->b==NULL)) {
01726 rule=rule->next;
01727 Traceline("alternative clause has been retracted\n");
01728 }
01729 if (rule) {
01730
01731 clear_copy();
01732 if (TRUE)
01733
01734 head=eval_copy(rule->a,STACK);
01735 else
01736 head=quote_copy(rule->a,STACK);
01737
01738 body=eval_copy(rule->b,STACK);
01739
01740
01741
01742
01743
01744 if (rule->next)
01745 push_choice_point(prove,thegoal,rule->next,NULL);
01746
01747 if (body->type!=succeed)
01748 push_goal(prove,body,DEFRULES,NULL);
01749
01750
01751 push_psi_ptr_value(head,&(head->coref));
01752 head->coref=thegoal;
01753 merge(&(thegoal->attr_list),head->attr_list);
01754 if (!head->status) {
01755 i_eval_args(head->attr_list);
01756 }
01757 }
01758 else {
01759 success=FALSE;
01760 }
01761 }
01762 }
01763 }
01764 }
01765 else {
01766
01767 goal_stack=aim->next;
01768 goal_count++;
01769 get_two_args(thegoal->attr_list,&arg1,&arg2);
01770 push_choice_point(prove,arg2,DEFRULES,NULL);
01771 push_goal(prove,arg1,DEFRULES,NULL);
01772 }
01773 else {
01774 goal_stack=aim->next;
01775 goal_count++;
01776
01777 cut_to(thegoal->value);
01778 #ifdef CLEAN_TRAIL
01779 clean_trail(choice_stack);
01780 #endif
01781 Traceline("cut all choice points back to %x\n",choice_stack);
01782 }
01783 }
01784 else {
01785 goal_stack=aim->next;
01786 goal_count++;
01787 get_two_args(thegoal->attr_list,&arg1,&arg2);
01788 push_goal(prove,arg2,DEFRULES,NULL);
01789 push_goal(prove,arg1,DEFRULES,NULL);
01790 }
01791 }
01792 else
01793 success=FALSE;
01794
01795 resid_aim=NULL;
01796 return success;
01797 }
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807 void type_disj_aim()
01808 {
01809 ptr_psi_term t;
01810 ptr_int_list d;
01811
01812 t=(ptr_psi_term)aim->a;
01813 d=(ptr_int_list)aim->b;
01814
01815 if (d->next) {
01816 Traceline("pushing type disjunction choice point for %P\n", t);
01817 push_choice_point(type_disj,t,d->next,NULL);
01818 }
01819
01820 push_ptr_value(def_ptr,&(t->type));
01821
01822
01823 t->type=(ptr_definition)d->value;
01824
01825 Traceline("setting type disjunction to %s.\n", t->type->keyword->symbol);
01826
01827 if ((t->attr_list || t->type->always_check) && t->status<4)
01828 fetch_def(t, FALSE);
01829 }
01830
01831
01832
01833
01834
01835
01836
01837
01838 long clause_aim(r)
01839 long r;
01840 {
01841 long success=FALSE;
01842 ptr_pair_list *p;
01843 ptr_psi_term head,body,rule_head,rule_body;
01844
01845 head=(ptr_psi_term)aim->a;
01846 body=(ptr_psi_term)aim->b;
01847 p=(ptr_pair_list *)aim->c;
01848
01849 if ((unsigned long)(*p)>MAX_BUILT_INS) {
01850 success=TRUE;
01851
01852
01853 if ((*p)->next) {
01854 if (r) {
01855 Traceline("pushing 'retract' choice point for %P\n", head);
01856 push_choice_point(del_clause,head,body,&((*p)->next));
01857
01858 }
01859 else {
01860 Traceline("pushing 'clause' choice point for %P\n", head);
01861 push_choice_point(clause,head,body,&((*p)->next));
01862 }
01863 }
01864
01865 if (r)
01866 push_goal(retract,p,NULL,NULL);
01867 if ((*p)->a) {
01868 clear_copy();
01869 rule_head=quote_copy((*p)->a,STACK);
01870 rule_body=quote_copy((*p)->b,STACK);
01871
01872 push_goal(unify,body,rule_body,NULL);
01873 push_goal(unify,head,rule_head,NULL);
01874
01875 rule_head->status=4;
01876 rule_body->status=4;
01877
01878 i_eval_args(rule_body->attr_list);
01879 i_eval_args(rule_head->attr_list);
01880
01881 Traceline("fetching next clause for %s\n", head->type->keyword->symbol);
01882 }
01883 else {
01884 success=FALSE;
01885 Traceline("following clause had been retracted\n");
01886 }
01887 }
01888 else if ((unsigned long)(*p)>0) {
01889 if (r)
01890 Errorline("the built-in %P cannot be retracted.\n",head);
01891 else
01892 Errorline("the definition of built-in %P is not accessible.\n",head);
01893 }
01894
01895 return success;
01896 }
01897
01898
01899
01900
01901 long no_choices()
01902 {
01903 return (choice_stack==NULL) || (choice_stack->goal_stack->type==what_next);
01904 }
01905
01906
01907
01908 long num_choices()
01909 {
01910 long num;
01911 ptr_choice_point cp;
01912
01913 num=0;
01914 cp=choice_stack;
01915 while (cp) {
01916 num++;
01917 cp=cp->next;
01918 }
01919 return num;
01920 }
01921
01922
01923
01924 long num_vars(vt)
01925 ptr_node vt;
01926 {
01927 long num;
01928
01929 return (vt?(num_vars(vt->left)+1+num_vars(vt->right)):0);
01930 }
01931
01932
01933
01934
01935 long what_next_cut()
01936 {
01937 long flag=TRUE;
01938 long result=FALSE;
01939
01940 do {
01941 if (choice_stack) {
01942 backtrack();
01943 if (goal_stack->type==what_next) {
01944 flag=FALSE;
01945 result=TRUE;
01946 }
01947 }
01948 else {
01949
01950
01951
01952 undo(NULL);
01953
01954 #ifdef TS
01955
01956 #endif
01957 flag=FALSE;
01958 }
01959 } while (flag);
01960
01961 return result;
01962 }
01963
01964
01965
01966
01967
01968
01969
01970 ptr_choice_point topmost_what_next()
01971 {
01972 ptr_choice_point cp=choice_stack;
01973
01974 while (cp && cp->goal_stack && cp->goal_stack->type!=what_next)
01975 cp=cp->next;
01976
01977 if (cp && cp->goal_stack && cp->goal_stack->type==what_next)
01978 return cp;
01979 else
01980 return (ptr_choice_point) NULL;
01981 }
01982
01983
01984
01985
01986
01987 void reset_stacks()
01988 {
01989 undo(NULL);
01990 goal_stack=NULL;
01991 choice_stack=NULL;
01992 #ifdef TS
01993
01994 #endif
01995 }
01996
01997
01998
01999
02000
02001
02002
02003
02004
02005 long what_next_aim()
02006 {
02007 long result=FALSE;
02008 ptr_psi_term s;
02009 long c, c2;
02010 char *pr;
02011 long sort,cut=FALSE;
02012 long level,i;
02013 long eventflag;
02014 ptr_stack save_undo_stack;
02015
02016 begin_terminal_io();
02017
02018 level=((unsigned long)aim->c);
02019
02020 if (aim->a) {
02021
02022
02023 var_occurred=var_occurred || ((unsigned long)aim->b)&TRUEMASK;
02024 eventflag=(((unsigned long)aim->b)&(TRUEMASK*2))!=0;
02025 if (
02026 !var_occurred && no_choices() && level>0
02027 #ifdef X11
02028
02029 && !x_window_creation && !eventflag
02030 #endif
02031 ) {
02032
02033
02034
02035
02036 level--;
02037 what_next_cut();
02038 if (level==0) { result=TRUE; }
02039 }
02040 }
02041
02042 #ifdef X11
02043 x_window_creation=FALSE;
02044 #endif
02045
02046 Infoline(aim->a?"\n*** Yes":"\n*** No");
02047 show_count();
02048 if (aim->a || level>0) print_variables(NOTQUIET);
02049
02050 {
02051 long lev=(MAX_LEVEL<level?MAX_LEVEL:level);
02052 pr=prompt_buffer;
02053
02054 if(current_module==user_module)
02055 *pr='\0';
02056 else
02057 strcpy(pr,current_module->module_name);
02058 pr += strlen(pr);
02059 for(i=1;i<=lev;i++) { *pr='-'; pr++; *pr='-'; pr++; }
02060 if (level>0)
02061 sprintf(pr,"%ld",level);
02062 strcat(pr,PROMPT);
02063
02064 prompt=prompt_buffer;
02065 }
02066
02067 stdin_cleareof();
02068
02069
02070
02071 #ifdef X11
02072 c=x_read_stdin_or_event(&eventflag);
02073 if (eventflag) {
02074
02075 push_goal(what_next,TRUE,FALSE+2*TRUE,level );
02076 release_resid(xevent_existing);
02077 result=TRUE;
02078 }
02079 else
02080 #else
02081 c=read_char();
02082 #endif
02083 {
02084 while (c!=EOLN && c>0 && c<=32 && c!=EOF) {
02085 c=read_char();
02086 }
02087 if (c==EOF) {
02088 reset_stacks();
02089 }
02090 else if (c==EOLN) {
02091 cut=TRUE;
02092 }
02093 else if (c==';' || c=='.') {
02094 do {
02095 c2=read_char();
02096 } while (c2!=EOLN && c2!=EOF && c2>0 && c2<=32);
02097 if (c=='.') {
02098 reset_stacks();
02099 result=TRUE;
02100 }
02101 }
02102 else {
02103 if (level>0) push_choice_point(what_next,FALSE,FALSE,level);
02104
02105 put_back_char(c);
02106 var_occurred=FALSE;
02107 save_undo_stack=undo_stack;
02108 s=stack_copy_psi_term(parse(&sort));
02109
02110 if (s->type==eof) {
02111 reset_stacks();
02112 put_back_char(EOF);
02113 } else if (sort==QUERY) {
02114 push_goal(what_next,TRUE,var_occurred,level+1);
02115 push_goal(prove,s,DEFRULES,NULL);
02116 reset_step();
02117 result=TRUE;
02118 }
02119 else if (sort==FACT) {
02120 push_goal(what_next,TRUE,FALSE,level+1);
02121 assert_first=FALSE;
02122 assert_clause(s);
02123
02124
02125 undo(save_undo_stack);
02126 encode_types();
02127 result=TRUE;
02128 }
02129 else {
02130
02131 push_goal(what_next,TRUE,FALSE,level+1);
02132 result=TRUE;
02133 }
02134 }
02135 }
02136
02137 if (cut) result = what_next_cut() || result;
02138
02139 end_terminal_io();
02140
02141 var_occurred=FALSE;
02142 start_chrono();
02143
02144 return result;
02145 }
02146
02147
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157 long load_aim()
02158 {
02159 long success=TRUE,exitloop;
02160 ptr_psi_term s;
02161 long sort;
02162 char *fn;
02163 long old_noisy,old_file_date;
02164 ptr_node old_var_tree;
02165 ptr_choice_point cutpt;
02166 long old_var_occurred;
02167 int end_of_file=FALSE;
02168
02169
02170 save_state(input_state);
02171 input_state=(ptr_psi_term)aim->a;
02172 restore_state(input_state);
02173 old_file_date=file_date;
02174 file_date=(unsigned long)aim->b;
02175 old_noisy=noisy;
02176 noisy=FALSE;
02177 fn=(char*)aim->c;
02178 exitloop=FALSE;
02179
02180
02181
02182 do {
02183
02184
02185
02186 old_var_occurred=var_occurred;
02187 old_var_tree=var_tree;
02188 var_tree=NULL;
02189 s=stack_copy_psi_term(parse(&sort));
02190 var_tree=old_var_tree;
02191 var_occurred=old_var_occurred;
02192
02193 if (s->type==eof) {
02194 encode_types();
02195 if (input_stream!=stdin) fclose(input_stream);
02196 exitloop=TRUE;
02197 end_of_file=TRUE;
02198 }
02199 else if (sort==FACT) {
02200 assert_first=FALSE;
02201 assert_clause(s);
02202 }
02203 else if (sort==QUERY) {
02204 encode_types();
02205 save_state(input_state);
02206
02207 cutpt=choice_stack;
02208 push_choice_point(load,input_state,file_date,fn);
02209 push_goal(load,input_state,file_date,fn);
02210 push_goal(general_cut,cutpt,NULL,NULL);
02211 push_goal(prove,s,DEFRULES,NULL);
02212 exitloop=TRUE;
02213 }
02214 else {
02215
02216
02217
02218 if (input_stream!=stdin) fclose(input_stream);
02219 abort_life(TRUE);
02220
02221
02222 }
02223 } while (success && !exitloop);
02224
02225
02226
02227 if(end_of_file || !success) {
02228
02229
02230
02231
02232
02233
02234
02235
02236 set_current_module(
02237 find_module(((ptr_psi_term)get_attr(input_state,
02238 CURRENT_MODULE))->value));
02239 }
02240
02241
02242 noisy=old_noisy;
02243 file_date=old_file_date;
02244 open_input_file("stdin");
02245
02246
02247 return success;
02248 }
02249
02250
02251
02252
02253
02254
02255
02256
02257
02258 void main_prove()
02259 {
02260 long success=TRUE;
02261 ptr_pair_list *p;
02262 ptr_psi_term unused_match_date;
02263
02264 xcount=0;
02265 xeventdelay=XEVENTDELAY;
02266 interrupted=FALSE;
02267 main_loop_ok=TRUE;
02268
02269 while (main_loop_ok && goal_stack) {
02270
02271
02272
02273
02274
02275
02276
02277
02278
02279
02280
02281
02282
02283
02284
02285
02286
02287 aim=goal_stack;
02288 switch(aim->type) {
02289
02290 case unify:
02291 goal_stack=aim->next;
02292 goal_count++;
02293 success=unify_aim();
02294 break;
02295
02296
02297
02298 case unify_noeval:
02299 goal_stack=aim->next;
02300 goal_count++;
02301 success=unify_aim_noeval();
02302 break;
02303
02304 case prove:
02305 success=prove_aim();
02306 break;
02307
02308 case eval:
02309 goal_stack=aim->next;
02310 goal_count++;
02311 success=eval_aim();
02312 break;
02313
02314 case load:
02315 goal_stack=aim->next;
02316 goal_count++;
02317 success=load_aim();
02318 break;
02319
02320 case match:
02321 goal_stack=aim->next;
02322 goal_count++;
02323 success=match_aim();
02324 break;
02325
02326 case disj:
02327 goal_stack=aim->next;
02328 goal_count++;
02329 success=disjunct_aim();
02330 break;
02331
02332 case general_cut:
02333 goal_stack=aim->next;
02334 goal_count++;
02335
02336
02337 cut_to(aim->a);
02338 #ifdef CLEAN_TRAIL
02339 clean_trail(choice_stack);
02340 #endif
02341 #ifdef TS
02342
02343 #endif
02344 break;
02345
02346 case eval_cut:
02347 restore_resid((ptr_resid_block)aim->c, &unused_match_date);
02348 if (curried)
02349 do_currying();
02350 else if (resid_vars) {
02351 success=do_residuation_user();
02352 } else {
02353 if (resid_aim)
02354 Traceline("result of %P is %P\n", resid_aim->a, aim->a);
02355 goal_stack=aim->next;
02356 goal_count++;
02357
02358
02359 i_check_out(aim->a);
02360 }
02361 resid_aim=NULL;
02362 resid_vars=NULL;
02363
02364
02365 if (success) {
02366 cut_to(aim->b);
02367 #ifdef CLEAN_TRAIL
02368 clean_trail(choice_stack);
02369 #endif
02370
02371 #ifdef TS
02372
02373 #endif
02374 }
02375 break;
02376
02377 case freeze_cut:
02378 restore_resid((ptr_resid_block)aim->c, &unused_match_date);
02379 if (curried) {
02380 Warningline("frozen goal has a missing parameter '%P' and fails.\n",aim->a);
02381 success=FALSE;
02382 }
02383 else if (resid_vars) {
02384 success=do_residuation_user();
02385 } else {
02386 if (resid_aim) Traceline("releasing frozen goal: %P\n", aim->a);
02387
02388
02389 goal_stack=aim->next;
02390 goal_count++;
02391 }
02392 resid_aim=NULL;
02393 resid_vars=NULL;
02394 if (success) {
02395
02396
02397 cut_to(aim->b);
02398 #ifdef CLEAN_TRAIL
02399 clean_trail(choice_stack);
02400 #endif
02401
02402 #ifdef TS
02403
02404 #endif
02405 }
02406 break;
02407
02408 case implies_cut:
02409
02410 restore_resid((ptr_resid_block)aim->c, &unused_match_date);
02411 if (curried) {
02412 Warningline("implied goal has a missing parameter '%P' and fails.\n",aim->a);
02413 success=FALSE;
02414 }
02415 else if (resid_vars)
02416 success=FALSE;
02417 else {
02418 if (resid_aim) Traceline("executing implied goal: %P\n", aim->a);
02419 goal_stack=aim->next;
02420 goal_count++;
02421 }
02422 resid_aim=NULL;
02423 resid_vars=NULL;
02424 break;
02425
02426 case fail:
02427 goal_stack=aim->next;
02428 success=FALSE;
02429 break;
02430
02431 case what_next:
02432 goal_stack=aim->next;
02433 success=what_next_aim();
02434 break;
02435
02436 case type_disj:
02437 goal_stack=aim->next;
02438 goal_count++;
02439 type_disj_aim();
02440 break;
02441
02442 case clause:
02443 goal_stack=aim->next;
02444 goal_count++;
02445 success=clause_aim(0);
02446 break;
02447
02448 case del_clause:
02449 goal_stack=aim->next;
02450 goal_count++;
02451 success=clause_aim(1);
02452 break;
02453
02454 case retract:
02455 goal_stack=aim->next;
02456 goal_count++;
02457 p=(ptr_pair_list*)aim->a;
02458 Traceline("deleting clause (%P%s%P)\n",
02459 (*p)->a,((*p)->a->type->type==function?"->":":-"),(*p)->b);
02460 (*p)->a=NULL;
02461 (*p)->b=NULL;
02462 (*p)=(*p)->next;
02463 break;
02464
02465 case c_what_next:
02466 main_loop_ok=FALSE;
02467 break;
02468
02469 default:
02470 Errorline("bad goal on stack %d.\n",goal_stack->type);
02471 goal_stack=aim->next;
02472 }
02473
02474 if (main_loop_ok) {
02475
02476 if (success) {
02477
02478 #ifdef X11
02479
02480 if (xcount<=0 && aim->type==prove) {
02481 if (x_exist_event()) {
02482
02483 xeventdelay=0;
02484 release_resid(xevent_existing);
02485 } else {
02486 if (xeventdelay<XEVENTDELAY)
02487
02488
02489 xeventdelay=(xeventdelay*101)/100+2;
02490 else
02491 xeventdelay=XEVENTDELAY;
02492 }
02493 xcount=xeventdelay;
02494 }
02495 else
02496 xcount--;
02497 #endif
02498
02499 }
02500 else {
02501 if (choice_stack) {
02502 backtrack();
02503 Traceline("backtracking\n");
02504 success=TRUE;
02505 }
02506 else {
02507 undo(NULL);
02508 Infoline("\n*** No");
02509
02510 show_count();
02511 #ifdef TS
02512
02513 #endif
02514 main_loop_ok=FALSE;
02515 }
02516 }
02517
02518 if (heap_pointer-stack_pointer < GC_THRESHOLD)
02519 memory_check();
02520
02521 if (interrupted || (stepflag && steptrace))
02522 handle_interrupt();
02523 else if (stepcount>0) {
02524 stepcount--;
02525 if (stepcount==0 && !stepflag) {
02526 stepflag=TRUE;
02527 handle_interrupt();
02528 }
02529 }
02530 }
02531 }
02532 }
02533
02534
02535 int dummy_printf(f,s,t)
02536
02537 char *f, *s, *t;
02538 {
02539 return strlen(f);
02540 }