C:/Users/Dennis/src/lang/Life_start/Life/life-1.02/source/types.c

Go to the documentation of this file.
00001 /* Copyright 1991 Digital Equipment Corporation.
00002 ** All Rights Reserved.
00003 *****************************************************************/
00004 /*      $Id: types.c,v 1.7 1994/12/15 22:28:56 duchier Exp $     */
00005 
00006 #ifndef lint
00007 static char vcid[] = "$Id: types.c,v 1.7 1994/12/15 22:28:56 duchier Exp $";
00008 #endif /* lint */
00009 
00010 /****************************************************************************
00011 
00012   These routines implement type encoding using the "Transitive Closure"
00013   binary encoding algorithm.
00014 
00015  ****************************************************************************/
00016 
00017 #include "extern.h"
00018 #include "login.h"
00019 #include "trees.h"
00020 #include "print.h"
00021 #include "memory.h"
00022 #include "error.h"
00023 #include "token.h"
00024 
00025 long types_modified;
00026 long type_count;
00027 
00028 ptr_definition *gamma_table;
00029 
00030 ptr_int_list adults,children;
00031 
00032 typedef struct wl_pair_def{
00033   ptr_definition car;
00034   ptr_definition cdr;
00035 } pair_def;
00036 
00037 
00038 void make_type_link(); /* Forward declaration */
00039 
00040 
00041 
00042 /******** PRINT_DEF_TYPE(t)
00043   This prints type T to stderr, where T=predicate, function or type.
00044 */
00045 void print_def_type(t)
00046 def_type t;
00047 {
00048   switch (t) {
00049   case predicate:
00050     perr("predicate");
00051     break;
00052   case function:
00053     perr("function");
00054     break;
00055   case type:
00056     perr("sort");
00057     break;
00058   case global: /*  RM: Feb  8 1993  */
00059     perr("global variable");
00060     break;
00061   default:
00062     perr("undefined");
00063   }
00064 }
00065 
00066 
00067 /* Confirm an important change */
00068 long yes_or_no()
00069 {
00070   char *old_prompt;
00071   long c,d;
00072   ptr_psi_term old_state;
00073 
00074   perr("*** Are you really sure you want to do that ");
00075   old_prompt=prompt;
00076   prompt="(y/n)?";
00077   old_state=input_state;
00078   open_input_file("stdin");
00079 
00080   do {
00081     do {
00082       c=read_char();
00083     } while (c!=EOLN && c>0 && c<=32);
00084   } while (c!='y' && c!='n');
00085 
00086   d=c;
00087   while (d!=EOLN && d!=EOF) d=read_char();
00088 
00089   prompt=old_prompt;
00090   input_state=old_state;
00091   restore_state(old_state);
00092   return (c=='y');
00093 }
00094 
00095 
00096 /* Remove references to d in d's children or parents */
00097 remove_cycles(d, dl)
00098 ptr_definition d;
00099 ptr_int_list *dl;
00100 {
00101   while (*dl) {
00102     if (((ptr_definition)(*dl)->value)==d)
00103       *dl = (*dl)->next;
00104     else
00105       dl= &((*dl)->next);
00106   }
00107 }
00108 
00109 
00110 
00111 /******** REDEFINE(t)
00112   This decides whether a definition (a sort, function, or predicate)
00113   may be extended or not.
00114 */
00115 long redefine(t)
00116 ptr_psi_term t;
00117 {
00118   ptr_definition d,d2;
00119   ptr_int_list l,*l2;
00120   long success=TRUE;
00121   
00122   deref_ptr(t);
00123   d=t->type;
00124   if (d->date<file_date) {
00125     if (d->type==type) {
00126       /* Except for top, sorts are always unprotected, with a warning. */
00127       if (FALSE /*d==top*/) {
00128         Errorline("the top sort '@' may not be extended.\n");
00129         success=FALSE;
00130       }
00131       /*  RM: Mar 25 1993
00132         else if (d!=top)
00133         Warningline("extending definition of sort '%s'.\n",d->keyword->symbol);
00134         */
00135     }
00136     else if (d->protected && d->type!=undef) {
00137       if (d->date>0) {
00138         /* The term was entered in a previous file, and therefore */
00139         /* cannot be altered. */
00140         Errorline("the %T '%s' may not be changed.\n", /*  RM: Jan 27 1993  */
00141                   d->type, d->keyword->combined_name);
00142         success=FALSE;
00143       }
00144       else {
00145         if (d->rule && (unsigned long)d->rule<=MAX_BUILT_INS /*&& input_stream==stdin*/) {
00146           /* d is a built-in, and therefore cannot be altered. */
00147           Errorline("the built-in %T '%s' may not be extended.\n",
00148                     d->type, d->keyword->symbol);
00149           success=FALSE;
00150         }
00151         else {
00152           /* d is not a built-in, and therefore can be altered. */
00153           Warningline("extending the %T '%s'.\n",d->type,d->keyword->symbol);
00154           if (warningflag) if (!yes_or_no()) success=FALSE;
00155         }
00156       }
00157     }
00158     
00159     if (success) {
00160       if (d->type==type) { /* d is an already existing type */
00161         /* Remove cycles in the type hierarchy of d */
00162         /* This is done by Richard's version, and I don't know why. */
00163         /* It seems to be a no-op. */
00164         remove_cycles(d, &(d->children));
00165         remove_cycles(d, &(d->parents));
00166         /* d->rule=NULL; */ /* Types must keep their rules! */
00167         /* d->properties=NULL; */ /* Types get new properties from encode */
00168       }
00169       if (d->date==0) d->date=file_date;
00170       /* d->type=undef; */ /* Objects keep their type! */
00171       /* d->always_check=TRUE; */
00172       /* d->protected=TRUE; */
00173       /* d->children=NULL; */
00174       /* d->parents=NULL; */
00175       /* d->code=NOT_CODED; */
00176     }
00177   }
00178 
00179   return success;
00180 }
00181 
00182 
00183 
00184 /******** CONS(value,list)
00185   Returns the list [VALUE|LIST]
00186 */
00187 ptr_int_list cons(v,l)
00188 GENERIC v;
00189 ptr_int_list l;
00190 {
00191   ptr_int_list n;
00192 
00193   n=HEAP_ALLOC(int_list);
00194   n->value=v;
00195   n->next=l;
00196   
00197   return n;
00198 }
00199 
00200 
00201 
00202 /******** ASSERT_LESS(t1,t2)
00203   Assert that T1 <| T2.
00204   Return false if some sort of error occurred.
00205 */
00206 long assert_less(t1,t2)
00207 ptr_psi_term t1,t2;
00208 {
00209   ptr_definition d1,d2; 
00210   long ok=FALSE;
00211   deref_ptr(t1);
00212   deref_ptr(t2);
00213 
00214   if (t1->type==top) {
00215     Errorline("the top sort '@' may not be a subsort.\n");
00216     return FALSE;
00217   }
00218   if (t1->value || t2->value) {
00219     Errorline("the declaration '%P <| %P' is illegal.\n",t1,t2);
00220     return FALSE;
00221   }
00222   /* Note: A *full* cyclicity check of the hierarchy is done in encode_types. */
00223   if (t1->type==t2->type) {
00224     Errorline("cyclic sort declarations are not allowed.\n");
00225     return FALSE;
00226   }
00227     
00228   if (!redefine(t1)) return FALSE;
00229   if (!redefine(t2)) return FALSE;
00230   d1=t1->type;
00231   d2=t2->type;
00232   if (d1->type==predicate || d1->type==function) {
00233     Errorline("the %T '%s' may not be redefined as a sort.\n",  
00234               d1->type, d1->keyword->symbol);
00235   }
00236   else if (d2->type==predicate || d2->type==function) {
00237     Errorline("the %T '%s' may not be redefined as a sort.\n",  
00238               d2->type, d2->keyword->symbol);
00239   }
00240   else {
00241     d1->type=type;
00242     d2->type=type;
00243     types_modified=TRUE;
00244     make_type_link(d1, d2); /* 1.7 */
00245     /* d1->parents=cons(d2,d1->parents); */
00246     /* d2->children=cons(d1,d2->children); */
00247     ok=TRUE;
00248   }
00249   
00250   return ok;
00251 }
00252 
00253 
00254 
00255 /******** ASSERT_PROTECTED(n,prot)
00256   Mark all the nodes in the attribute tree N with protect flag prot.
00257 */
00258 void assert_protected(n,prot)
00259 ptr_node n;
00260 long prot;
00261 {
00262   ptr_psi_term t;
00263 
00264   if (n) {
00265     assert_protected(n->left,prot);
00266     
00267     t=(ptr_psi_term)n->data;
00268     deref_ptr(t);
00269     if (t->type) {
00270       if (t->type->type==type) {
00271         Warningline("'%s' is a sort. It can be extended without a declaration.\n",
00272                     t->type->keyword->symbol);
00273       }
00274       else if ((unsigned long)t->type->rule<MAX_BUILT_INS &&
00275                (unsigned long)t->type->rule>0) {
00276         if (!prot)
00277           Warningline("'%s' is a built-in--it has not been made dynamic.\n",
00278                       t->type->keyword->symbol);
00279       }
00280       else {
00281         t->type->protected=prot;
00282         if (prot) t->type->date&=(~1); else t->type->date|=1;
00283       }
00284     }
00285 
00286     assert_protected(n->right,prot);
00287   }
00288 }
00289 
00290 
00291 
00292 /******** ASSERT_ARGS_NOT_EVAL(n)
00293   Mark all the nodes in the attribute tree N as having unevaluated arguments,
00294   if they are functions or predicates.
00295 */
00296 void assert_args_not_eval(n)
00297 ptr_node n;
00298 {
00299   ptr_psi_term t;
00300 
00301   if (n) {
00302     assert_args_not_eval(n->left);
00303     
00304     t=(ptr_psi_term)n->data;
00305     deref_ptr(t);
00306     if (t->type) {
00307       if (t->type->type==type) {
00308         Warningline("'%s' is a sort--only functions and predicates\
00309  can have unevaluated arguments.\n",t->type->keyword->symbol);
00310       }
00311       else
00312         t->type->evaluate_args=FALSE;
00313     }
00314 
00315     assert_args_not_eval(n->right);
00316   }
00317 }
00318 
00319 
00320 
00321 /******** ASSERT_DELAY_CHECK(n)
00322   Assert that the types in the attribute tree N will have their
00323   properties checked only when they have attributes.  If they
00324   have no attributes, then no properties are checked.
00325 */
00326 void assert_delay_check(n)
00327 ptr_node n;
00328 {
00329   if (n) {
00330     ptr_psi_term t;
00331     assert_delay_check(n->left);
00332     
00333     t=(ptr_psi_term)n->data;
00334     deref_ptr(t);
00335     if (t->type) {
00336       t->type->always_check=FALSE;
00337     }
00338 
00339     assert_delay_check(n->right);
00340   }
00341 }
00342 
00343 
00344 
00345 /******** CLEAR_ALREADY_LOADED()
00346   Clear the 'already_loaded' flags in all symbol table entries.
00347   Done at each top level prompt.
00348 */
00349 void clear_already_loaded(n)
00350 ptr_node n;
00351 {
00352   ptr_definition d;
00353 
00354   if (n) {
00355     d=((ptr_keyword)n->data)->definition;
00356     d->already_loaded=FALSE;
00357     clear_already_loaded(n->left);
00358     clear_already_loaded(n->right);
00359   }
00360 }
00361 
00362 
00363 
00364 /******** ASSERT_TYPE(t)
00365   T is the psi_term <|(type1,type2).
00366   Add that to the type-definitions.
00367 */
00368 void assert_type(t)
00369 ptr_psi_term t;
00370 {
00371   ptr_psi_term arg1,arg2;
00372 
00373   get_two_args(t->attr_list,&arg1,&arg2);
00374   if(arg1==NULL || arg2==NULL) {
00375     Errorline("bad sort declaration '%P' (%E).\n",t);
00376   }
00377   else
00378     assert_ok=assert_less(arg1,arg2);
00379 }
00380 
00381 
00382 
00383 /******** ASSERT_COMPLICATED_TYPE
00384   This deals with all the type declarations of the form:
00385   
00386   a(attr) <| b.                         % (a<|b)
00387   a(attr) <| b | pred.
00388   
00389   a(attr) <| {b;c;d}.                   % (a<|b, a<|c, a<|d)
00390   a(attr) <| {b;c;d} | pred.
00391   
00392   a := b(attr).                         % (a<|b)
00393   a := b(attr) | pred.
00394   
00395   a := {b(attr1);c(attr2);d(attr3)}.    % (b<|a,c<|a,d<|a)
00396   a := {b(attr1);c(attr2);d(attr3)} | pred.
00397 */
00398 void assert_complicated_type(t)
00399 ptr_psi_term t;
00400 {
00401   ptr_psi_term arg2,typ1,typ2,pred=NULL;
00402   ptr_list lst;
00403   long eqflag = equ_tok((*t),":=");
00404   long ok, any_ok=FALSE;
00405   
00406   get_two_args(t->attr_list,&typ1,&arg2);
00407   
00408   if (typ1 && arg2) {
00409     deref_ptr(typ1);
00410     deref_ptr(arg2);
00411     typ2=arg2;
00412     if (!strcmp(arg2->type->keyword->symbol,"|")) {
00413       typ2=NULL;
00414       get_two_args(arg2->attr_list,&arg2,&pred);
00415       if (arg2) {
00416         deref_ptr(arg2);
00417         typ2=arg2;
00418       }
00419     }
00420     if (typ2) {
00421       if (typ2->type==disjunction) {
00422         
00423         if (typ1->attr_list && eqflag) {
00424           Warningline("attributes ignored left of ':=' declaration (%E).\n");
00425         }
00426         while(typ2 && typ2->type!=nil) {
00427           get_two_args(typ2->attr_list,&arg2,&typ2); /*  RM: Dec 14 1992  */
00428           if(typ2)
00429             deref_ptr(typ2);
00430           if (arg2) {
00431             deref_ptr(arg2);
00432             if (eqflag) {
00433               ok=assert_less(arg2,typ1);
00434               if (ok) any_ok=TRUE;
00435               if (ok && (arg2->attr_list || pred!=NULL)) {
00436                 add_rule(arg2,pred,type);
00437               }
00438             }
00439             else {
00440               ok=assert_less(typ1,arg2);
00441               if (ok) any_ok=TRUE;
00442               if (ok && arg2->attr_list) {
00443                 Warningline("attributes ignored in sort declaration (%E).\n");
00444               }
00445             }
00446           }
00447         }
00448         assert_ok=TRUE;
00449       }
00450       else if (eqflag) {
00451         if (typ1->attr_list) {
00452           Warningline("attributes ignored left of ':=' declaration (%E).\n");
00453         }
00454         ok=assert_less(typ1,typ2);
00455         if (ok) any_ok=TRUE;
00456         typ2->type=typ1->type;
00457         if (ok && (typ2->attr_list || pred!=NULL))
00458           add_rule(typ2,pred,type);
00459         else
00460           assert_ok=TRUE;
00461       }
00462       else {
00463         if (typ2->attr_list) {
00464           Warningline("attributes ignored right of '<|' declaration (%E).\n");
00465         }
00466         ok=assert_less(typ1,typ2);
00467         if (ok) any_ok=TRUE;
00468         if (ok && (typ1->attr_list || pred!=NULL))
00469           add_rule(typ1,pred,type);
00470         else
00471           assert_ok=TRUE;
00472       }
00473     }
00474     else {
00475       Errorline("argument missing in sort declaration (%E).\n");
00476     }
00477   }
00478   else {
00479     Errorline("argument missing in sort declaration (%E).\n");
00480   }
00481   if (!any_ok) assert_ok=FALSE;
00482 }
00483 
00484 
00485 
00486 /******** ASSERT_ATTRIBUTES(t)
00487   T is of the form ':: type(attributes) | pred', the attributes must be 
00488   appended to T's definition, and will be propagated after ENCODING to T's
00489   subtypes.
00490 */
00491 void assert_attributes(t)
00492 ptr_psi_term t;
00493 {
00494   ptr_psi_term arg1,arg2,pred=NULL,typ;
00495   ptr_definition d;
00496   
00497   get_two_args(t->attr_list,&arg1,&arg2);
00498   
00499   if (arg1) {
00500     typ=arg1;
00501     deref_ptr(arg1);
00502     if (!strcmp(arg1->type->keyword->symbol,"|")) {
00503       get_two_args(arg1->attr_list,&arg1,&pred);
00504       if (arg1) {
00505         typ=arg1;
00506         deref_ptr(arg1);
00507       }
00508     }
00509     
00510     if (arg1 && wl_const(*arg1)) {
00511       /* if (!redefine(arg1)) return;   RM: Feb 19 1993  */
00512       d=arg1->type;
00513       if (d->type==predicate || d->type==function) {
00514         Errorline("the %T '%s' may not be redefined as a sort.\n",
00515                   d->type, d->keyword->symbol);
00516       }
00517       else {
00518         d->type=type;
00519         types_modified=TRUE;
00520         add_rule(typ,pred,type);
00521       }
00522     }
00523     else {
00524       Errorline("bad argument in sort declaration '%P' (%E).\n",t);
00525     }
00526   }
00527   else {
00528     Errorline("argument missing in sort declaration (%E).\n");
00529   }
00530 }
00531 
00532 
00533 
00534 /******** FIND_ADULTS()
00535   Returns the list of all the maximal types (apart from top) in the symbol 
00536   table. That is, types which have no parents.
00537   This routine modifies the global variable 'adults'.
00538 */
00539 void find_adults()       /*  RM: Feb  3 1993  */
00540 
00541 {
00542   ptr_definition d;
00543   ptr_int_list l;
00544 
00545   for(d=first_definition;d;d=d->next)
00546     if(d->type==type && d->parents==NULL) {
00547       l=HEAP_ALLOC(int_list);
00548       l->value=(GENERIC)d;
00549       l->next=adults;
00550       adults=l;
00551     }
00552 }
00553 
00554 
00555 
00556 /******** INSERT_OWN_PROP(definition)
00557   Append a type's "rules" (i.e. its own attr. & constr.) to its property list.
00558   The property list also contains the type's code.
00559   A type's attributes and constraints are stored in the 'rule' field of the
00560   definition.
00561 */
00562 void insert_own_prop(d)
00563 ptr_definition d;
00564 {
00565   ptr_int_list l;
00566   ptr_pair_list rule;
00567   ptr_triple_list *t;
00568   long flag;
00569 
00570   l=HEAP_ALLOC(int_list);
00571   l->value=(GENERIC)d;
00572   l->next=children;
00573   children=l;
00574 
00575   rule = d->rule;
00576   while (rule) {
00577     t= &(d->properties);
00578     flag=TRUE;
00579     
00580     while (flag) {
00581       if (*t)
00582         if ((*t)->a==rule->a && (*t)->b==rule->b && (*t)->c==d)
00583           flag=FALSE;
00584         else
00585           t= &((*t)->next);
00586       else {
00587         *t = HEAP_ALLOC(triple_list);
00588         (*t)->a=rule->a;
00589         (*t)->b=rule->b;
00590         (*t)->c=d;
00591         (*t)->next=NULL;
00592         flag=FALSE;
00593       }
00594     } 
00595     rule=rule->next;
00596   }
00597 }
00598 
00599 
00600 /******** INSERT_PROP(definition,prop)
00601   Append the properties to the definition if they aren't already present.
00602 */
00603 void insert_prop(d,prop)
00604 ptr_definition d;
00605 ptr_triple_list prop;
00606 {
00607   ptr_int_list l;
00608   ptr_triple_list *t;
00609   long flag;
00610 
00611   l=HEAP_ALLOC(int_list);
00612   l->value=(GENERIC)d;
00613   l->next=children;
00614   children=l;
00615 
00616   while (prop) {
00617     t= &(d->properties);
00618     flag=TRUE;
00619     
00620     while (flag) {
00621       if (*t)
00622         if ((*t)->a==prop->a && (*t)->b==prop->b && (*t)->c==prop->c)
00623           flag=FALSE;
00624         else
00625           t= &((*t)->next);
00626       else {
00627         *t = HEAP_ALLOC(triple_list);
00628         (*t)->a=prop->a;
00629         (*t)->b=prop->b;
00630         (*t)->c=prop->c;
00631         (*t)->next=NULL;
00632         flag=FALSE;
00633       }
00634     } 
00635     prop=prop->next;
00636   }
00637 }
00638 
00639 
00640 
00641 /******** PROPAGATE_DEFINITIONS()
00642   This routine propagates the definition (attributes,predicates) of a type to 
00643   all its sons.
00644 */
00645 void propagate_definitions()
00646 {
00647   ptr_int_list kids;
00648   ptr_definition d;
00649   
00650   adults=NULL;
00651   find_adults();
00652   
00653   while (adults) {
00654     
00655     children=NULL;
00656     
00657     while (adults) {
00658       d=(ptr_definition)adults->value;
00659       
00660       insert_own_prop(d);
00661       children=children->next;
00662       
00663       kids=d->children;
00664       
00665       while(kids) {
00666         insert_prop(kids->value,d->properties);
00667         /* if (d->always_check && kids->value)
00668           ((ptr_definition)kids->value)->always_check=TRUE; */
00669         kids=kids->next;
00670       }
00671       adults=adults->next;
00672     }
00673     adults=children;
00674   }
00675 }
00676 
00677 
00678 
00679 /******************************************************************************
00680 
00681   The following routines implement sort encoding.
00682 
00683 */
00684 
00685 
00686 
00687 /******** COUNT_SORTS(c)
00688   Count the number of sorts in the symbol table T.
00689   Overestimates in the module version.  RM: Jan 21 1993 
00690   No longer !!   RM: Feb  3 1993 
00691   */
00692 long count_sorts(c0)  /*  RM: Feb  3 1993  */
00693      long c0;
00694 {
00695   ptr_definition d;
00696 
00697   for(d=first_definition;d;d=d->next)
00698     if (d->type==type) c0++;
00699   
00700   return c0;
00701 }
00702 
00703 
00704 
00705 /******** CLEAR_CODING()
00706   Clear the bit-vector coding of the sorts.
00707 */
00708 void clear_coding()   /*  RM: Feb  3 1993  */
00709 
00710 {
00711   ptr_definition d;
00712 
00713   for(d=first_definition;d;d=d->next)
00714     if (d->type==type) d->code=NOT_CODED;
00715 }
00716 
00717 
00718 
00719 /******** LEAST_SORTS()
00720   Build the list of terminals (i.e. sorts with no children) in
00721   nothing->parents.
00722 */
00723 void least_sorts()  /*  RM: Feb  3 1993  */
00724 
00725 {
00726   ptr_definition d;
00727 
00728   for(d=first_definition;d;d=d->next)
00729     if (d->type==type && d->children==NULL && d!=nothing)
00730       nothing->parents=cons(d,nothing->parents);
00731 }
00732 
00733 
00734 
00735 /******** ALL_SORTS()
00736   Build a list of all sorts (except nothing) in nothing->parents.
00737   */
00738 
00739 void all_sorts()   /*  RM: Feb  3 1993  */
00740      
00741 {
00742   ptr_definition d;
00743   
00744   for(d=first_definition;d;d=d->next)
00745     if (d->type==type && d!=nothing)
00746       nothing->parents=cons(d,nothing->parents);
00747 }
00748   
00749 
00750 
00751 /******** TWO_TO_THE(p)
00752   Return the code worth 2^p.
00753 */
00754 ptr_int_list two_to_the(p)
00755 long p;
00756 {
00757   ptr_int_list result,code;
00758   long v=1;
00759 
00760   code=HEAP_ALLOC(int_list);
00761   code->value=0;
00762   code->next=NULL;
00763   result=code;
00764   
00765   while (p>=INT_SIZE) {
00766     code->next=HEAP_ALLOC(int_list);
00767     code=code->next;
00768     code->value=0;
00769     code->next=NULL;
00770     p=p-INT_SIZE;
00771   }
00772 
00773   v= v<<p ;
00774   code->value=(GENERIC)v;
00775 
00776   return result;
00777 }
00778 
00779 
00780 /******** copyTypeCode(code)
00781   returns copy of code on the heap
00782 */
00783 ptr_int_list copyTypeCode(u)
00784 ptr_int_list u;
00785 {
00786   ptr_int_list code;
00787 
00788   code = HEAP_ALLOC(int_list);
00789   code->value=0;
00790   code->next=NULL;
00791 
00792   or_codes(code, u);
00793 
00794   return code;
00795 }
00796 
00797 
00798 
00799 /******** OR_CODES(code1,code2)
00800   Performs CODE1 := CODE1 or CODE2,
00801   'or' being the binary logical operator on bits.
00802 */
00803 void or_codes(u,v)
00804 ptr_int_list u,v;
00805 {
00806   while (v) {
00807     u->value= (GENERIC)(((unsigned long)(u->value)) | ((unsigned long)(v->value)));
00808     v=v->next;
00809     if (u->next==NULL && v) {
00810       u->next=HEAP_ALLOC(int_list);
00811       u=u->next;
00812       u->value=0;
00813       u->next=NULL;
00814     }
00815     else
00816       u=u->next;
00817   }
00818 }
00819 
00820 
00821 
00822 /******** EQUALIZE_CODES(w)
00823   Make sure all codes are w words long, by increasing the length of the
00824   shorter ones.
00825   This simplifies greatly the bitvector manipulation routines.
00826   This operation should be done after encoding.
00827   For correct operation, w>=maximum number of words used for a code.
00828 */
00829 equalize_codes(len) /*  RM: Feb  3 1993  */
00830      int len;
00831 {
00832   ptr_definition d;
00833   ptr_int_list c,*ci;
00834   long i;
00835   int w;
00836   
00837   for(d=first_definition;d;d=d->next)
00838     if (d->type==type) {
00839       c = d->code;
00840       ci = &(d->code);  /*  RM: Feb 15 1993  */
00841       w=len;
00842       
00843       /* Count how many words have to be added */
00844       while (c) {
00845         ci= &(c->next);
00846         c=c->next;
00847         w--;
00848       }
00849       assert(w>=0);
00850       /* Add the words */
00851       for (i=0; i<w; i++) {
00852         *ci = HEAP_ALLOC(int_list);
00853         (*ci)->value=0;
00854         ci= &((*ci)->next);
00855       }
00856       (*ci)=NULL;
00857     }
00858 }
00859 
00860 
00861 
00862 long type_member();
00863 
00864 
00865 /******** MAKE_TYPE_LINK(t1,t2)
00866   Assert that T1 <| T2, this is used to initialise the built_in type relations
00867   so that nothing really horrible happens if the user modifies built-in types
00868   such as INT or LIST.
00869   This routine also makes sure that top has no links.
00870 */
00871 void make_type_link(t1,t2)
00872 ptr_definition t1, t2;
00873 {
00874 #ifdef OS2_PORT
00875 if (t1)
00876 {
00877 #endif
00878   if (t2!=top && !type_member(t2,t1->parents))
00879     t1->parents=cons(t2,t1->parents);
00880   if (t2!=top && !type_member(t1,t2->children))
00881     t2->children=cons(t1,t2->children);
00882 #ifdef OS2_PORT
00883 }
00884 #endif
00885 }
00886 
00887 
00888 
00889 
00890 /******** TYPE_MEMBER(t,tlst)
00891   Return TRUE iff type t is in the list tlst.
00892 */
00893 
00894 long type_member(t,tlst)
00895 ptr_definition t;
00896 ptr_int_list tlst;
00897 {
00898   while (tlst) {
00899    if (t==(ptr_definition)tlst->value) return TRUE;
00900    tlst=tlst->next;
00901   }
00902   return FALSE;
00903 }
00904 
00905 
00906 void perr_sort(d)
00907 ptr_definition d;
00908 {
00909   perr_s("%s",d->keyword->symbol);
00910 }
00911 
00912 void perr_sort_list(anc)
00913 ptr_int_list anc;
00914 {
00915   if (anc) {
00916     perr_sort_list(anc->next);
00917     if (anc->next) perr(" <| ");
00918     perr_sort((ptr_definition)anc->value);
00919   }
00920 }
00921 
00922 void perr_sort_cycle(anc)
00923 ptr_int_list anc;
00924 {
00925   perr_sort((ptr_definition)anc->value);
00926   perr(" <| ");
00927   perr_sort_list(anc);
00928 }
00929 
00930 
00931 
00932 /******** TYPE_CYCLICITY(d,anc)
00933   Check cyclicity of type hierarchy.
00934   If cyclic, return a TRUE error condition and print an error message
00935   with a cycle.
00936 */
00937 long type_cyclicity(d,anc)
00938 ptr_definition d;
00939 ptr_int_list anc;
00940 {
00941   ptr_int_list p=d->parents;
00942   ptr_definition pd;
00943   long errflag;
00944   int_list anc2;
00945 
00946   while (p) {
00947     pd=(ptr_definition)p->value;
00948     /* If unmarked, mark and recurse */
00949     if (pd->code==NOT_CODED) {
00950       pd->code = (ptr_int_list)TRUE;
00951       anc2.value=(GENERIC)pd;
00952       anc2.next=anc;
00953       errflag=type_cyclicity(pd,&anc2);
00954       if (errflag) return TRUE;
00955     }
00956     /* If marked, check if it's in the ancestor list */
00957     else {
00958       if (type_member(pd,anc)) {
00959         Errorline("there is a cycle in the sort hierarchy\n");
00960         perr("*** Cycle: [");
00961         perr_sort_cycle(anc);
00962         perr("]\n");
00963         exit_life(TRUE);
00964         return TRUE;
00965       }
00966     }
00967     p=p->next;
00968   }
00969   return FALSE;
00970 }
00971 
00972 
00973 
00974 /******** PROPAGATE_ALWAYS_CHECK(d,ch)
00975   Recursively set the always_check flag to 'FALSE' for all d's
00976   children.  Continue until encountering only 'FALSE' values. 
00977   Return a TRUE flag if a change was made somewhere (for the
00978   closure calculation).
00979 */
00980 void propagate_always_check(d,ch)
00981 ptr_definition d;
00982 long *ch;
00983 {
00984   ptr_int_list child_list;
00985   ptr_definition child;
00986 
00987   child_list = d->children;
00988   while (child_list) {
00989     child = (ptr_definition)child_list->value;
00990     if (child->always_check) {
00991       child->always_check = FALSE;
00992       *ch = TRUE;
00993       propagate_always_check(child,ch);
00994     }
00995     child_list = child_list->next;
00996   }
00997 }
00998 
00999 
01000 
01001 /******** ONE_PASS_ALWAYS_CHECK(ch)
01002   Go through the symbol table & propagate all FALSE always_check
01003   flags of all sorts to their children.  Return a TRUE flag
01004   if a change was made somewhere (for the closure calculation).
01005 */
01006 void one_pass_always_check(ch)
01007      long *ch;
01008 {
01009   ptr_definition d;
01010   
01011   
01012   for(d=first_definition;d;d=d->next)
01013     if (d->type==type && !d->always_check)
01014       propagate_always_check(d,ch);
01015 }
01016 
01017 
01018 
01019 /******** INHERIT_ALWAYS_CHECK()
01020   The 'always_check' flag, if false, should be propagated to a sort's
01021   children.  This routine does a closure on this propagation operation
01022   for all declared sorts.
01023 */
01024 void inherit_always_check()
01025 {
01026   long change;
01027 
01028   do {
01029     change=FALSE;
01030     one_pass_always_check(&change);
01031   } while (change);
01032 }
01033 
01034 
01035 
01036 /******** ENCODE_TYPES()
01037   This routine performs type-coding using transitive closure.
01038   First any previous coding is undone.
01039   Then a new encryption is performed.
01040 
01041   Some of these routines loop indefinitely if there is a circular type
01042   definition (an error should be reported but it isn't implemented (but it's
01043   quite easy to do)).
01044 */
01045 void encode_types()
01046 {
01047   long p=0,i,possible,ok=TRUE;
01048   ptr_int_list layer,l,kids,dads,code;
01049   ptr_definition xdef,kdef,ddef,err;
01050   
01051   if (types_modified) {
01052     
01053     nothing->parents=NULL;
01054     nothing->children=NULL;
01055     
01056     top->parents=NULL;
01057     top->children=NULL;
01058 
01059     /* The following definitions are vital to avoid crashes */
01060     make_type_link(integer,real);
01061     make_type_link(true,boolean);
01062     make_type_link(false,boolean);
01063 
01064     /* These just might be useful */
01065     make_type_link(quoted_string,built_in);
01066     make_type_link(boolean,built_in);
01067     make_type_link(real,built_in);
01068 
01069     make_sys_type_links();
01070     
01071     type_count=count_sorts(-1); /* bottom does not count */
01072     clear_coding();
01073     nothing->parents=NULL; /* Must be cleared before all_sorts */
01074     all_sorts();
01075     if (type_cyclicity(nothing,NULL)) {
01076       clear_coding();
01077       return;
01078     }
01079     clear_coding();
01080     nothing->parents=NULL; /* Must be cleared before least_sorts */
01081     least_sorts();
01082     
01083     nothing->code=NULL;
01084 
01085     /*  RM: Feb 17 1993  */
01086     Traceline("*** Codes:\n%C= %s\n", NULL, nothing->keyword->symbol);
01087     
01088     gamma_table=(ptr_definition *) heap_alloc(type_count*sizeof(definition));
01089     
01090     layer=nothing->parents;
01091     
01092     while (layer) {
01093       l=layer;
01094       do {
01095         xdef=(ptr_definition)l->value;
01096         if (xdef->code==NOT_CODED && xdef!=top) {
01097           
01098           kids=xdef->children;
01099           code=two_to_the(p);
01100           
01101           while (kids) {
01102             kdef=(ptr_definition)kids->value;
01103             or_codes(code,kdef->code);
01104             kids=kids->next;
01105           }
01106           
01107           xdef->code=code;
01108           gamma_table[p]=xdef;
01109 
01110           /*  RM: Feb 17 1993  */
01111           Traceline("%C = %s\n", code, xdef->keyword->symbol);
01112           p=p+1;
01113         }
01114         
01115         l=l->next;
01116         
01117       } while (l);
01118       
01119       l=layer;
01120       layer=NULL;
01121       
01122       do {
01123         xdef=(ptr_definition)l->value;
01124         dads=xdef->parents;
01125         
01126         while (dads) {
01127           ddef=(ptr_definition)dads->value;
01128           if(ddef->code==NOT_CODED) {
01129             
01130             possible=TRUE;
01131             kids=ddef->children;
01132             
01133             while(kids && possible) {
01134               kdef=(ptr_definition)kids->value;
01135               if(kdef->code==NOT_CODED)
01136                 possible=FALSE;
01137               kids=kids->next;
01138             }
01139             if(possible)
01140               layer=cons(ddef,layer);
01141           }
01142           dads=dads->next;
01143         }
01144         l=l->next;
01145       } while(l);
01146     }
01147     
01148     top->code=two_to_the(p);
01149     for (i=0;i<p;i++)
01150       or_codes(top->code,two_to_the(i));
01151 
01152     gamma_table[p]=top;
01153 
01154     /*  RM: Jan 13 1993  */
01155     /* Added the following line because type_count is now over generous
01156        because the same definition can be referenced several times in
01157        the symbol table because of modules
01158        */
01159     type_count=p+1;
01160     for(i=type_count;i<type_count;i++)
01161       gamma_table[i]=NULL;
01162     
01163     Traceline("%C = @\n\n", top->code);
01164     equalize_codes(p/32+1);
01165 
01166     propagate_definitions();
01167 
01168     /* Inherit 'FALSE' always_check flags to all types' children */
01169     inherit_always_check();
01170     
01171     Traceline("*** Encoding done, %d sorts\n",type_count);
01172     
01173     if (overlap_type(real,quoted_string)) {
01174       Errorline("the sorts 'real' and 'string' are not disjoint.\n");
01175       ok=FALSE;
01176     }
01177 
01178     /*  RM: Dec 15 1992  I don't think this really matters any more
01179         if (overlap_type(real,alist)) {
01180         Errorline("the sorts 'real' and 'list' are not disjoint.\n");
01181         ok=FALSE;
01182         }
01183         */
01184     
01185     /*  RM: Dec 15 1992  I don't think this really matters any more
01186         if (overlap_type(alist,quoted_string)) {
01187         Errorline("the sorts 'list' and 'string' are not disjoint.\n");
01188         ok=FALSE;
01189         }
01190         */
01191     
01192     if (!ok) {
01193       perr("*** Internal problem:\n");
01194       perr("*** Wild_Life may behave abnormally because some basic types\n");
01195       perr("*** have been defined incorrectly.\n\n");
01196     }
01197 
01198     types_modified=FALSE;
01199     types_done=TRUE;
01200   }
01201 }
01202 
01203 
01204 
01205 /******** PRINT_CODES()
01206   Print all the codes.
01207 */
01208 void print_codes()
01209 {
01210   long i;
01211 
01212   for (i=0; i<type_count; i++) {
01213     outputline("%C = %s\n",
01214                gamma_table[i]->code,
01215                gamma_table[i]->keyword->combined_name);
01216   }
01217 }
01218 
01219 
01220 long sub_CodeType();
01221 
01222 
01223 /******** GLB_VALUE(result,f,c,value1,value2,value)
01224   Do the comparison of the value fields of two psi-terms.
01225   This is used in conjunction with glb_code to correctly implement
01226   completeness for disequality for psi-terms with non-NULL value fields.
01227   This must be preceded by a call to glb_code, since it uses the outputs
01228   of that call.
01229 
01230   result   result of preceding glb_code call (non-NULL iff non-empty intersec.)
01231   f,c      sort intersection (sortflag & code) of preceding glb_code call.
01232   value1   value field of first psi-term.
01233   value2   value field of second psi-term.
01234   value    output value field (if any).
01235 */
01236 long glb_value(result,f,c,value1,value2,value)
01237 long result;
01238 long f;
01239 GENERIC c;
01240 GENERIC value1,value2,*value;
01241 {
01242   ptr_int_list code;
01243 
01244   if (!result) return FALSE;
01245   if (value1==NULL) {
01246     *value=value2;
01247     return TRUE;
01248   }
01249   if (value2==NULL) {
01250     *value=value1;
01251     return TRUE;
01252   }
01253   /* At this point, both value fields are non-NULL */
01254   /* and must be compared. */
01255 
01256   /* Get a pointer to the sort code */
01257   code = f ? ((ptr_definition)c)->code : (ptr_int_list)c;
01258 
01259   /* This rather time-consuming analysis is necessary if both objects */
01260   /* have non-NULL value fields.  Note that only those objects with a */
01261   /* non-NULL value field needed for disentailment are looked at.     */
01262   if (sub_CodeType(code,real->code)) {
01263     *value=value1;
01264     return (*(REAL *)value1 == *(REAL *)value2);
01265   }
01266   else if (sub_CodeType(code,quoted_string->code)) {
01267     *value=value1;
01268     return (!strcmp((char *)value1,(char *)value2));
01269   }
01270   else {
01271     /* All other sorts with 'value' fields always return TRUE, that is, */
01272     /* the value field plays no role in disentailment. */
01273     *value=value1;
01274     return TRUE;
01275   }
01276 }
01277 
01278 
01279 
01280 /******** GLB_CODE(f1,c1,f2,c2,f3,c3) (21.9)
01281   Calculate glb of two type codes C1 and C2, put result in C3.
01282   Return a result value (see comments of glb(..)).
01283 
01284   Sorts are stored as a 'Variant Record':
01285     f1==TRUE:  c1 is a ptr_definition (an interned symbol).
01286     f1==FALSE: c1 is a ptr_int_list (a sort code).
01287   The result (f3,c3) is also in this format.
01288   This is needed to correctly handle psi-terms that don't have a sort code
01289   (for example, functions, predicates, and singleton sorts).
01290   The routine handles a bunch of special cases that keep f3==TRUE.
01291   Other than that, it is almost a replica of the inner loop of glb(..).
01292 */
01293 long glb_code(f1,c1,f2,c2,f3,c3)
01294 long f1,f2,*f3;
01295 GENERIC c1,c2,*c3;
01296 {
01297   long result=0;
01298   unsigned long v1,v2,v3;
01299   ptr_int_list cd1,cd2,*cd3; /* sort codes */
01300 
01301   /* First, the cases where c1 & c2 are ptr_definitions: */
01302   if (f1 && f2) {
01303     if ((ptr_definition)c1==(ptr_definition)c2) {
01304       *c3=c1;
01305       result=1;
01306     }
01307     else if ((ptr_definition)c1==top) {
01308       *c3=c2;
01309       if ((ptr_definition)c2==top)
01310         result=1;
01311       else
01312         result=3;
01313     }
01314     else if ((ptr_definition)c2==top) {
01315       *c3=c1;
01316       result=2;
01317     }
01318     /* If both inputs are either top or the same ptr_definition */
01319     /* then can return quickly with a ptr_definition. */
01320     if (result) {
01321       *f3=TRUE; /* c3 is ptr_definition (an interned symbol) */
01322       return result;
01323     }
01324   }
01325   /* In the other cases, can't return with a ptr_definition: */
01326   cd1=(ptr_int_list)(f1?(GENERIC)((ptr_definition)c1)->code:c1);
01327   cd2=(ptr_int_list)(f2?(GENERIC)((ptr_definition)c2)->code:c2);
01328   cd3=(ptr_int_list*)c3;
01329   *f3=FALSE; /* cd3 is ptr_int_list (a sort code) */
01330   if (cd1==NOT_CODED) {
01331     if (cd2==NOT_CODED) {
01332       if (c1==c2) {
01333         *cd3=cd1;
01334         result=1;
01335       }
01336       else
01337         result=0;
01338     }
01339     else if (cd2==top->code) {
01340       *cd3=cd1;
01341       result=2;
01342     }
01343     else
01344       result=0;
01345   }
01346   else if (cd1==top->code) {
01347     if (cd2==top->code) {
01348       *cd3=cd1;
01349       result=1;
01350     }
01351     else {
01352       *cd3=cd2;
01353       result=3;
01354     }
01355   }
01356   else if (cd2==NOT_CODED)
01357     result=0;
01358   else if (cd2==top->code) {
01359     *cd3=cd1;
01360     result=2;
01361   }
01362   else while (cd1 && cd2) {
01363     /* Bit operations needed only if c1 & c2 coded & different from top */
01364     *cd3 = STACK_ALLOC(int_list);
01365     (*cd3)->next=NULL;
01366     
01367     v1=(unsigned long)(cd1->value);
01368     v2=(unsigned long)(cd2->value);
01369     v3=v1 & v2;
01370     (*cd3)->value=(GENERIC)v3;
01371     
01372     if (v3) {
01373       if (v3<v1 && v3<v2)
01374         result=4;
01375       else if (result!=4)
01376         if (v1<v2)
01377           result=2;
01378         else if (v1>v2)
01379           result=3;
01380         else
01381           result=1;
01382     }
01383     else if (result)
01384       if (v1 || v2)
01385         result=4;
01386         
01387     cd1=cd1->next;
01388     cd2=cd2->next;
01389     cd3= &((*cd3)->next);
01390   }
01391 
01392   return result;
01393 }
01394 
01395 
01396 
01397 /******** GLB(t1,t2,t3)
01398   This function returns the Greatest Lower Bound of two types T1 and T2 in T3.
01399   
01400   T3 = T1 /\ T2
01401 
01402   If T3 is not a simple type then C3 is its code, and T3=NULL.
01403   
01404   It also does some type comparing, and returns
01405   
01406   0 if T3 = bottom
01407   1 if T1 = T2
01408   2 if T1 <| T2 ( T3 = T1 )
01409   3 if T1 |> T2 ( T3 = T2 )
01410   4 otherwise   ( T3 strictly <| T1 and T3 strictly <| T2 )
01411   
01412   These results are used for knowing when to inherit properties or release
01413   residuations.
01414   The t3 field is NULL iff a new type is needed to represent the
01415   result.
01416 */
01417 /*  RM: May  7 1993  Fixed bug in when multiple word code */
01418 long glb(t1,t2,t3,c3)
01419 ptr_definition t1;
01420 ptr_definition t2;
01421 ptr_definition  *t3;
01422 ptr_int_list *c3;
01423 {
01424   ptr_int_list c1,c2;
01425   long result=0;
01426   unsigned long v1,v2,v3;
01427   int e1,e2,b; /*  RM: May  7 1993  */
01428 
01429 
01430   
01431   *c3=NULL;
01432   
01433   if (t1==t2) { 
01434     result=1;
01435     *t3= t1;
01436   }
01437   else if (t1==top) {
01438     *t3= t2;
01439     if (t2==top)
01440       result=1;
01441     else
01442       result=3;
01443   }
01444   else if (t2==top) {
01445     result=2;
01446     *t3= t1;
01447   }
01448   else {
01449     /* printf("glb of %s and %s\n",
01450        t1->keyword->combined_name,
01451        t2->keyword->combined_name); */
01452            
01453     c1=t1->code;
01454     c2=t2->code;
01455 
01456     e1=TRUE;e2=TRUE;b=TRUE;
01457     
01458     if (c1!=NOT_CODED && c2!=NOT_CODED) {
01459       result=0;
01460       while (c1 && c2) {
01461 
01462         *c3 = STACK_ALLOC(int_list);
01463         (*c3)->next=NULL;
01464 
01465         v1=(unsigned long)(c1->value);
01466         v2=(unsigned long)(c2->value);
01467         v3=v1 & v2;
01468 
01469         /* printf("v1=%d, v2=%d, v3=%d\n",v1,v2,v3); */
01470         
01471         (*c3)->value=(GENERIC)v3;
01472 
01473         if(v3!=v1) /*  RM: May  7 1993  */
01474           e1=FALSE;
01475         if(v3!=v2)
01476           e2=FALSE;
01477         if(v3)
01478           b=FALSE;
01479         
01480         c1=c1->next;
01481         c2=c2->next;
01482         c3= &((*c3)->next);
01483       }
01484       *t3=NULL;
01485 
01486       if(b) /*  RM: May  7 1993  */
01487         result=0; /* 0 if T3 = bottom */
01488       else
01489         if(e1)
01490           if(e2)
01491             result=1; /* 1 if T1 = T2 */
01492           else
01493             result=2; /* 2 if T1 <| T2 ( T3 = T1 ) */
01494         else
01495           if(e2)
01496             result=3; /* 3 if T1 |> T2 ( T3 = T2 ) */
01497           else
01498             result=4; /* 4 otherwise */
01499     }
01500   }
01501   
01502   if (!result) *t3=nothing;
01503   
01504   /* printf("result=%d\n\n",result); */
01505   
01506   return result;
01507 }
01508 
01509 
01510 
01511 /******** OVERLAP_TYPE(t1,t2)
01512   This function returns TRUE if GLB(t1,t2)!=bottom.
01513   This is essentially the same thing as GLB, only it's faster 'cause we don't
01514   care about the resulting code.
01515 */
01516 long overlap_type(t1,t2)
01517 ptr_definition t1;
01518 ptr_definition t2;
01519 {
01520   ptr_int_list c1,c2;
01521   long result=TRUE;
01522   
01523   if (t1!=t2 && t1!=top && t2!=top) {
01524     
01525     c1=t1->code;
01526     c2=t2->code;
01527     result=FALSE;
01528 
01529     if (c1!=NOT_CODED && c2!=NOT_CODED) {     
01530       while (!result && c1 && c2) {          
01531         result=(((unsigned long)(c1->value)) & ((unsigned long)(c2->value)));
01532         c1=c1->next;
01533         c2=c2->next;
01534       }
01535     }
01536   }
01537   
01538   /*
01539   printf("overlap_type(%s,%s) => %ld\n",t1->def->keyword->symbol,t2->def->keyword->symbol,result);
01540   */
01541   
01542   return result;
01543 }
01544 
01545 
01546 /******** SUB_CodeType(c1,c2)
01547   Return TRUE if code C1 is <| than type C2, that is if type represented
01548   by code C1 matches type represented by C2.
01549 
01550   We already know that t1 and t2 are not top.
01551 */
01552 long sub_CodeType(c1,c2)
01553 ptr_int_list c1;
01554 ptr_int_list c2;
01555 {
01556   if (c1!=NOT_CODED && c2!=NOT_CODED) {
01557     while (c1 && c2) {
01558       if ((unsigned long)c1->value & ~(unsigned long)c2->value) return FALSE;
01559       c1=c1->next;
01560       c2=c2->next;
01561     }
01562   }
01563   else
01564     return FALSE;
01565 
01566   return TRUE;
01567 }
01568 
01569 
01570 
01571 /******** SUB_TYPE(t1,t2)
01572   Return TRUE if type T1 is <| than type T2, that is if T1 matches T2.
01573 */
01574 long sub_type(t1,t2)
01575 ptr_definition t1;
01576 ptr_definition t2;
01577 {
01578   if (t1!=t2)
01579     if (t2!=top)
01580     {
01581       if (t1==top)
01582         return FALSE;
01583       else
01584         return sub_CodeType(t1->code, t2->code);
01585     }
01586   return TRUE;
01587 }
01588 
01589 
01590 
01591 /******** MATCHES(t1,t2,s)
01592   Returns TRUE if GLB(t1,t2)!=bottom.
01593   Sets S to TRUE if type T1 is <| than type T2, that is if T1 matches T2.
01594 */
01595 long matches(t1,t2,smaller)
01596 ptr_definition t1;
01597 ptr_definition t2;
01598 long *smaller;
01599 {
01600   ptr_int_list c1,c2;
01601   long result=TRUE;
01602   
01603   *smaller=TRUE;
01604   
01605   if (t1!=t2)
01606     if (t2!=top)
01607       if (t1==top)
01608         *smaller=FALSE;
01609       else {
01610         c1=t1->code;
01611         c2=t2->code;
01612         result=FALSE;
01613         
01614         if (c1!=NOT_CODED && c2!=NOT_CODED) {          
01615           while (c1 && c2) {          
01616             if ((unsigned long)c1->value &  (unsigned long)c2->value) result=TRUE;
01617             if ((unsigned long)c1->value & ~(unsigned long)c2->value) *smaller=FALSE;
01618             c1=c1->next;
01619             c2=c2->next;
01620           }
01621         }
01622         else
01623           *smaller=FALSE;
01624       }
01625   
01626   return result;
01627 }
01628 
01629 
01630 
01631 /******** STRICT_MATCHES(t1,t2,s)
01632   Almost the same as matches, except that S is set to TRUE only
01633   if the type of t1 is strictly less than the type of t2.
01634   Because of the implementation of ints, reals, strings, and lists,
01635   this has to take the value field into account, and thus must
01636   be passed the whole psi-term.
01637 */
01638 long strict_matches(t1,t2,smaller)
01639 ptr_psi_term t1;
01640 ptr_psi_term t2;
01641 long *smaller;
01642 {
01643   long result,sm;
01644 
01645   result=matches(t1->type,t2->type,&sm);
01646 
01647   if (sm) {
01648     /* At this point, t1->type <| t2->type */
01649     if (t1->type==t2->type) {
01650       /* Same types: strict only if first has a value & second does not */
01651       if (t1->value!=NULL && t2->value==NULL)
01652         sm=TRUE;
01653       else
01654         sm=FALSE;
01655     }
01656     else {
01657       /* Different types: the first must be strictly smaller */
01658       sm=TRUE;
01659     }
01660   }
01661 
01662   *smaller=sm;
01663   return result;
01664 }
01665 
01666 
01667 
01668 /******** BIT_LENGTH(c)
01669   Returns the number of bits needed to code C. That is the rank of the first
01670   non NULL bit of C.
01671   
01672   Examples:
01673   C= 1001001000   result=7
01674   C= 10000        result=1
01675   C= 0000000      result=0
01676   
01677 */
01678 long bit_length(c)
01679 ptr_int_list c;
01680 {
01681   unsigned long p=0,dp=0,v=0,dv=0;
01682   
01683   while (c) {
01684     v=(unsigned long)c->value;
01685     if(v) {
01686       dp=p;
01687       dv=v;
01688     }
01689     c=c->next;
01690     p=p+INT_SIZE;
01691   }
01692   
01693   while (dv) {
01694     dp++;
01695     dv=dv>>1;
01696   }
01697   
01698   return dp;
01699 }
01700 
01701 
01702 
01703 /******** DECODE(c)
01704   Returns a list of the symbol names which make up the disjunction whose
01705   code is C.
01706 */
01707 
01708 ptr_int_list decode(c)
01709 ptr_int_list c;
01710 {
01711   ptr_int_list c2,c3,c4,result=NULL,*prev;
01712   long p;
01713   
01714   p=bit_length(c);
01715   
01716   while (p) {
01717     p--;
01718     c2=gamma_table[p]->code;
01719     result=cons(gamma_table[p],result);
01720     prev= &c4;
01721     *prev=NULL;
01722     
01723     while (c2) {
01724       c3=STACK_ALLOC(int_list);
01725       *prev=c3;
01726       prev= &(c3->next);
01727       *prev=NULL;
01728       
01729       c3->value=(GENERIC)(((unsigned long)(c->value)) & ~((unsigned long)(c2->value)));
01730       
01731       c=c->next;
01732       c2=c2->next;
01733     }
01734     
01735     c=c4;
01736     p=bit_length(c);
01737   }
01738   
01739   return result;
01740 }

Generated on Sat Jan 26 08:48:08 2008 for WildLife by  doxygen 1.5.4