home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / SOURCE / TYPES.C < prev    next >
C/C++ Source or Header  |  1996-06-16  |  40KB  |  1,741 lines

  1. /* Copyright 1991 Digital Equipment Corporation.
  2. ** All Rights Reserved.
  3. *****************************************************************/
  4. /*     $Id: types.c,v 1.7 1994/12/15 22:28:56 duchier Exp $     */
  5.  
  6. #ifndef lint
  7. static char vcid[] = "$Id: types.c,v 1.7 1994/12/15 22:28:56 duchier Exp $";
  8. #endif /* lint */
  9.  
  10. /****************************************************************************
  11.  
  12.   These routines implement type encoding using the "Transitive Closure"
  13.   binary encoding algorithm.
  14.  
  15.  ****************************************************************************/
  16.  
  17. #include "extern.h"
  18. #include "login.h"
  19. #include "trees.h"
  20. #include "print.h"
  21. #include "memory.h"
  22. #include "error.h"
  23. #include "token.h"
  24.  
  25. long types_modified;
  26. long type_count;
  27.  
  28. ptr_definition *gamma_table;
  29.  
  30. ptr_int_list adults,children;
  31.  
  32. typedef struct wl_pair_def{
  33.   ptr_definition car;
  34.   ptr_definition cdr;
  35. } pair_def;
  36.  
  37.  
  38. void make_type_link(); /* Forward declaration */
  39.  
  40.  
  41.  
  42. /******** PRINT_DEF_TYPE(t)
  43.   This prints type T to stderr, where T=predicate, function or type.
  44. */
  45. void print_def_type(t)
  46. def_type t;
  47. {
  48.   switch (t) {
  49.   case predicate:
  50.     perr("predicate");
  51.     break;
  52.   case function:
  53.     perr("function");
  54.     break;
  55.   case type:
  56.     perr("sort");
  57.     break;
  58.   case global: /*  RM: Feb  8 1993  */
  59.     perr("global variable");
  60.     break;
  61.   default:
  62.     perr("undefined");
  63.   }
  64. }
  65.  
  66.  
  67. /* Confirm an important change */
  68. long yes_or_no()
  69. {
  70.   char *old_prompt;
  71.   long c,d;
  72.   ptr_psi_term old_state;
  73.  
  74.   perr("*** Are you really sure you want to do that ");
  75.   old_prompt=prompt;
  76.   prompt="(y/n)?";
  77.   old_state=input_state;
  78.   open_input_file("stdin");
  79.  
  80.   do {
  81.     do {
  82.       c=read_char();
  83.     } while (c!=EOLN && c>0 && c<=32);
  84.   } while (c!='y' && c!='n');
  85.  
  86.   d=c;
  87.   while (d!=EOLN && d!=EOF) d=read_char();
  88.  
  89.   prompt=old_prompt;
  90.   input_state=old_state;
  91.   restore_state(old_state);
  92.   return (c=='y');
  93. }
  94.  
  95.  
  96. /* Remove references to d in d's children or parents */
  97. remove_cycles(d, dl)
  98. ptr_definition d;
  99. ptr_int_list *dl;
  100. {
  101.   while (*dl) {
  102.     if (((ptr_definition)(*dl)->value)==d)
  103.       *dl = (*dl)->next;
  104.     else
  105.       dl= &((*dl)->next);
  106.   }
  107. }
  108.  
  109.  
  110.  
  111. /******** REDEFINE(t)
  112.   This decides whether a definition (a sort, function, or predicate)
  113.   may be extended or not.
  114. */
  115. long redefine(t)
  116. ptr_psi_term t;
  117. {
  118.   ptr_definition d,d2;
  119.   ptr_int_list l,*l2;
  120.   long success=TRUE;
  121.   
  122.   deref_ptr(t);
  123.   d=t->type;
  124.   if (d->date<file_date) {
  125.     if (d->type==type) {
  126.       /* Except for top, sorts are always unprotected, with a warning. */
  127.       if (FALSE /*d==top*/) {
  128.         Errorline("the top sort '@' may not be extended.\n");
  129.         success=FALSE;
  130.       }
  131.       /*  RM: Mar 25 1993
  132.     else if (d!=top)
  133.         Warningline("extending definition of sort '%s'.\n",d->keyword->symbol);
  134.     */
  135.     }
  136.     else if (d->protected && d->type!=undef) {
  137.       if (d->date>0) {
  138.         /* The term was entered in a previous file, and therefore */
  139.         /* cannot be altered. */
  140.         Errorline("the %T '%s' may not be changed.\n", /*  RM: Jan 27 1993  */
  141.                   d->type, d->keyword->combined_name);
  142.         success=FALSE;
  143.       }
  144.       else {
  145.         if (d->rule && (unsigned long)d->rule<=MAX_BUILT_INS /*&& input_stream==stdin*/) {
  146.           /* d is a built-in, and therefore cannot be altered. */
  147.           Errorline("the built-in %T '%s' may not be extended.\n",
  148.                     d->type, d->keyword->symbol);
  149.           success=FALSE;
  150.         }
  151.         else {
  152.           /* d is not a built-in, and therefore can be altered. */
  153.           Warningline("extending the %T '%s'.\n",d->type,d->keyword->symbol);
  154.           if (warningflag) if (!yes_or_no()) success=FALSE;
  155.         }
  156.       }
  157.     }
  158.     
  159.     if (success) {
  160.       if (d->type==type) { /* d is an already existing type */
  161.         /* Remove cycles in the type hierarchy of d */
  162.         /* This is done by Richard's version, and I don't know why. */
  163.         /* It seems to be a no-op. */
  164.         remove_cycles(d, &(d->children));
  165.         remove_cycles(d, &(d->parents));
  166.         /* d->rule=NULL; */ /* Types must keep their rules! */
  167.         /* d->properties=NULL; */ /* Types get new properties from encode */
  168.       }
  169.       if (d->date==0) d->date=file_date;
  170.       /* d->type=undef; */ /* Objects keep their type! */
  171.       /* d->always_check=TRUE; */
  172.       /* d->protected=TRUE; */
  173.       /* d->children=NULL; */
  174.       /* d->parents=NULL; */
  175.       /* d->code=NOT_CODED; */
  176.     }
  177.   }
  178.  
  179.   return success;
  180. }
  181.  
  182.  
  183.  
  184. /******** CONS(value,list)
  185.   Returns the list [VALUE|LIST]
  186. */
  187. ptr_int_list cons(v,l)
  188. GENERIC v;
  189. ptr_int_list l;
  190. {
  191.   ptr_int_list n;
  192.  
  193.   n=HEAP_ALLOC(int_list);
  194.   n->value=v;
  195.   n->next=l;
  196.   
  197.   return n;
  198. }
  199.  
  200.  
  201.  
  202. /******** ASSERT_LESS(t1,t2)
  203.   Assert that T1 <| T2.
  204.   Return false if some sort of error occurred.
  205. */
  206. long assert_less(t1,t2)
  207. ptr_psi_term t1,t2;
  208. {
  209.   ptr_definition d1,d2; 
  210.   long ok=FALSE;
  211.   deref_ptr(t1);
  212.   deref_ptr(t2);
  213.  
  214.   if (t1->type==top) {
  215.     Errorline("the top sort '@' may not be a subsort.\n");
  216.     return FALSE;
  217.   }
  218.   if (t1->value || t2->value) {
  219.     Errorline("the declaration '%P <| %P' is illegal.\n",t1,t2);
  220.     return FALSE;
  221.   }
  222.   /* Note: A *full* cyclicity check of the hierarchy is done in encode_types. */
  223.   if (t1->type==t2->type) {
  224.     Errorline("cyclic sort declarations are not allowed.\n");
  225.     return FALSE;
  226.   }
  227.     
  228.   if (!redefine(t1)) return FALSE;
  229.   if (!redefine(t2)) return FALSE;
  230.   d1=t1->type;
  231.   d2=t2->type;
  232.   if (d1->type==predicate || d1->type==function) {
  233.     Errorline("the %T '%s' may not be redefined as a sort.\n",  
  234.               d1->type, d1->keyword->symbol);
  235.   }
  236.   else if (d2->type==predicate || d2->type==function) {
  237.     Errorline("the %T '%s' may not be redefined as a sort.\n",  
  238.               d2->type, d2->keyword->symbol);
  239.   }
  240.   else {
  241.     d1->type=type;
  242.     d2->type=type;
  243.     types_modified=TRUE;
  244.     make_type_link(d1, d2); /* 1.7 */
  245.     /* d1->parents=cons(d2,d1->parents); */
  246.     /* d2->children=cons(d1,d2->children); */
  247.     ok=TRUE;
  248.   }
  249.   
  250.   return ok;
  251. }
  252.  
  253.  
  254.  
  255. /******** ASSERT_PROTECTED(n,prot)
  256.   Mark all the nodes in the attribute tree N with protect flag prot.
  257. */
  258. void assert_protected(n,prot)
  259. ptr_node n;
  260. long prot;
  261. {
  262.   ptr_psi_term t;
  263.  
  264.   if (n) {
  265.     assert_protected(n->left,prot);
  266.     
  267.     t=(ptr_psi_term)n->data;
  268.     deref_ptr(t);
  269.     if (t->type) {
  270.       if (t->type->type==type) {
  271.         Warningline("'%s' is a sort. It can be extended without a declaration.\n",
  272.                     t->type->keyword->symbol);
  273.       }
  274.       else if ((unsigned long)t->type->rule<MAX_BUILT_INS &&
  275.                (unsigned long)t->type->rule>0) {
  276.         if (!prot)
  277.           Warningline("'%s' is a built-in--it has not been made dynamic.\n",
  278.                       t->type->keyword->symbol);
  279.       }
  280.       else {
  281.         t->type->protected=prot;
  282.         if (prot) t->type->date&=(~1); else t->type->date|=1;
  283.       }
  284.     }
  285.  
  286.     assert_protected(n->right,prot);
  287.   }
  288. }
  289.  
  290.  
  291.  
  292. /******** ASSERT_ARGS_NOT_EVAL(n)
  293.   Mark all the nodes in the attribute tree N as having unevaluated arguments,
  294.   if they are functions or predicates.
  295. */
  296. void assert_args_not_eval(n)
  297. ptr_node n;
  298. {
  299.   ptr_psi_term t;
  300.  
  301.   if (n) {
  302.     assert_args_not_eval(n->left);
  303.     
  304.     t=(ptr_psi_term)n->data;
  305.     deref_ptr(t);
  306.     if (t->type) {
  307.       if (t->type->type==type) {
  308.         Warningline("'%s' is a sort--only functions and predicates\
  309.  can have unevaluated arguments.\n",t->type->keyword->symbol);
  310.       }
  311.       else
  312.         t->type->evaluate_args=FALSE;
  313.     }
  314.  
  315.     assert_args_not_eval(n->right);
  316.   }
  317. }
  318.  
  319.  
  320.  
  321. /******** ASSERT_DELAY_CHECK(n)
  322.   Assert that the types in the attribute tree N will have their
  323.   properties checked only when they have attributes.  If they
  324.   have no attributes, then no properties are checked.
  325. */
  326. void assert_delay_check(n)
  327. ptr_node n;
  328. {
  329.   if (n) {
  330.     ptr_psi_term t;
  331.     assert_delay_check(n->left);
  332.     
  333.     t=(ptr_psi_term)n->data;
  334.     deref_ptr(t);
  335.     if (t->type) {
  336.       t->type->always_check=FALSE;
  337.     }
  338.  
  339.     assert_delay_check(n->right);
  340.   }
  341. }
  342.  
  343.  
  344.  
  345. /******** CLEAR_ALREADY_LOADED()
  346.   Clear the 'already_loaded' flags in all symbol table entries.
  347.   Done at each top level prompt.
  348. */
  349. void clear_already_loaded(n)
  350. ptr_node n;
  351. {
  352.   ptr_definition d;
  353.  
  354.   if (n) {
  355.     d=((ptr_keyword)n->data)->definition;
  356.     d->already_loaded=FALSE;
  357.     clear_already_loaded(n->left);
  358.     clear_already_loaded(n->right);
  359.   }
  360. }
  361.  
  362.  
  363.  
  364. /******** ASSERT_TYPE(t)
  365.   T is the psi_term <|(type1,type2).
  366.   Add that to the type-definitions.
  367. */
  368. void assert_type(t)
  369. ptr_psi_term t;
  370. {
  371.   ptr_psi_term arg1,arg2;
  372.  
  373.   get_two_args(t->attr_list,&arg1,&arg2);
  374.   if(arg1==NULL || arg2==NULL) {
  375.     Errorline("bad sort declaration '%P' (%E).\n",t);
  376.   }
  377.   else
  378.     assert_ok=assert_less(arg1,arg2);
  379. }
  380.  
  381.  
  382.  
  383. /******** ASSERT_COMPLICATED_TYPE
  384.   This deals with all the type declarations of the form:
  385.   
  386.   a(attr) <| b.                % (a<|b)
  387.   a(attr) <| b | pred.
  388.   
  389.   a(attr) <| {b;c;d}.            % (a<|b, a<|c, a<|d)
  390.   a(attr) <| {b;c;d} | pred.
  391.   
  392.   a := b(attr).                % (a<|b)
  393.   a := b(attr) | pred.
  394.   
  395.   a := {b(attr1);c(attr2);d(attr3)}.    % (b<|a,c<|a,d<|a)
  396.   a := {b(attr1);c(attr2);d(attr3)} | pred.
  397. */
  398. void assert_complicated_type(t)
  399. ptr_psi_term t;
  400. {
  401.   ptr_psi_term arg2,typ1,typ2,pred=NULL;
  402.   ptr_list lst;
  403.   long eqflag = equ_tok((*t),":=");
  404.   long ok, any_ok=FALSE;
  405.   
  406.   get_two_args(t->attr_list,&typ1,&arg2);
  407.   
  408.   if (typ1 && arg2) {
  409.     deref_ptr(typ1);
  410.     deref_ptr(arg2);
  411.     typ2=arg2;
  412.     if (!strcmp(arg2->type->keyword->symbol,"|")) {
  413.       typ2=NULL;
  414.       get_two_args(arg2->attr_list,&arg2,&pred);
  415.       if (arg2) {
  416.         deref_ptr(arg2);
  417.         typ2=arg2;
  418.       }
  419.     }
  420.     if (typ2) {
  421.       if (typ2->type==disjunction) {
  422.     
  423.         if (typ1->attr_list && eqflag) {
  424.           Warningline("attributes ignored left of ':=' declaration (%E).\n");
  425.         }
  426.         while(typ2 && typ2->type!=nil) {
  427.           get_two_args(typ2->attr_list,&arg2,&typ2); /*  RM: Dec 14 1992  */
  428.           if(typ2)
  429.         deref_ptr(typ2);
  430.           if (arg2) {
  431.             deref_ptr(arg2);
  432.             if (eqflag) {
  433.               ok=assert_less(arg2,typ1);
  434.               if (ok) any_ok=TRUE;
  435.               if (ok && (arg2->attr_list || pred!=NULL)) {
  436.                 add_rule(arg2,pred,type);
  437.               }
  438.             }
  439.             else {
  440.               ok=assert_less(typ1,arg2);
  441.               if (ok) any_ok=TRUE;
  442.               if (ok && arg2->attr_list) {
  443.                 Warningline("attributes ignored in sort declaration (%E).\n");
  444.               }
  445.             }
  446.           }
  447.         }
  448.         assert_ok=TRUE;
  449.       }
  450.       else if (eqflag) {
  451.         if (typ1->attr_list) {
  452.           Warningline("attributes ignored left of ':=' declaration (%E).\n");
  453.         }
  454.         ok=assert_less(typ1,typ2);
  455.         if (ok) any_ok=TRUE;
  456.         typ2->type=typ1->type;
  457.         if (ok && (typ2->attr_list || pred!=NULL))
  458.           add_rule(typ2,pred,type);
  459.         else
  460.           assert_ok=TRUE;
  461.       }
  462.       else {
  463.         if (typ2->attr_list) {
  464.           Warningline("attributes ignored right of '<|' declaration (%E).\n");
  465.         }
  466.         ok=assert_less(typ1,typ2);
  467.         if (ok) any_ok=TRUE;
  468.         if (ok && (typ1->attr_list || pred!=NULL))
  469.           add_rule(typ1,pred,type);
  470.         else
  471.           assert_ok=TRUE;
  472.       }
  473.     }
  474.     else {
  475.       Errorline("argument missing in sort declaration (%E).\n");
  476.     }
  477.   }
  478.   else {
  479.     Errorline("argument missing in sort declaration (%E).\n");
  480.   }
  481.   if (!any_ok) assert_ok=FALSE;
  482. }
  483.  
  484.  
  485.  
  486. /******** ASSERT_ATTRIBUTES(t)
  487.   T is of the form ':: type(attributes) | pred', the attributes must be 
  488.   appended to T's definition, and will be propagated after ENCODING to T's
  489.   subtypes.
  490. */
  491. void assert_attributes(t)
  492. ptr_psi_term t;
  493. {
  494.   ptr_psi_term arg1,arg2,pred=NULL,typ;
  495.   ptr_definition d;
  496.   
  497.   get_two_args(t->attr_list,&arg1,&arg2);
  498.   
  499.   if (arg1) {
  500.     typ=arg1;
  501.     deref_ptr(arg1);
  502.     if (!strcmp(arg1->type->keyword->symbol,"|")) {
  503.       get_two_args(arg1->attr_list,&arg1,&pred);
  504.       if (arg1) {
  505.         typ=arg1;
  506.         deref_ptr(arg1);
  507.       }
  508.     }
  509.     
  510.     if (arg1 && wl_const(*arg1)) {
  511.       /* if (!redefine(arg1)) return;   RM: Feb 19 1993  */
  512.       d=arg1->type;
  513.       if (d->type==predicate || d->type==function) {
  514.         Errorline("the %T '%s' may not be redefined as a sort.\n",
  515.                   d->type, d->keyword->symbol);
  516.       }
  517.       else {
  518.         d->type=type;
  519.         types_modified=TRUE;
  520.         add_rule(typ,pred,type);
  521.       }
  522.     }
  523.     else {
  524.       Errorline("bad argument in sort declaration '%P' (%E).\n",t);
  525.     }
  526.   }
  527.   else {
  528.     Errorline("argument missing in sort declaration (%E).\n");
  529.   }
  530. }
  531.  
  532.  
  533.  
  534. /******** FIND_ADULTS()
  535.   Returns the list of all the maximal types (apart from top) in the symbol 
  536.   table. That is, types which have no parents.
  537.   This routine modifies the global variable 'adults'.
  538. */
  539. void find_adults()       /*  RM: Feb  3 1993  */
  540.  
  541. {
  542.   ptr_definition d;
  543.   ptr_int_list l;
  544.  
  545.   for(d=first_definition;d;d=d->next)
  546.     if(d->type==type && d->parents==NULL) {
  547.       l=HEAP_ALLOC(int_list);
  548.       l->value=(GENERIC)d;
  549.       l->next=adults;
  550.       adults=l;
  551.     }
  552. }
  553.  
  554.  
  555.  
  556. /******** INSERT_OWN_PROP(definition)
  557.   Append a type's "rules" (i.e. its own attr. & constr.) to its property list.
  558.   The property list also contains the type's code.
  559.   A type's attributes and constraints are stored in the 'rule' field of the
  560.   definition.
  561. */
  562. void insert_own_prop(d)
  563. ptr_definition d;
  564. {
  565.   ptr_int_list l;
  566.   ptr_pair_list rule;
  567.   ptr_triple_list *t;
  568.   long flag;
  569.  
  570.   l=HEAP_ALLOC(int_list);
  571.   l->value=(GENERIC)d;
  572.   l->next=children;
  573.   children=l;
  574.  
  575.   rule = d->rule;
  576.   while (rule) {
  577.     t= &(d->properties);
  578.     flag=TRUE;
  579.     
  580.     while (flag) {
  581.       if (*t)
  582.         if ((*t)->a==rule->a && (*t)->b==rule->b && (*t)->c==d)
  583.           flag=FALSE;
  584.         else
  585.           t= &((*t)->next);
  586.       else {
  587.         *t = HEAP_ALLOC(triple_list);
  588.         (*t)->a=rule->a;
  589.         (*t)->b=rule->b;
  590.         (*t)->c=d;
  591.         (*t)->next=NULL;
  592.         flag=FALSE;
  593.       }
  594.     } 
  595.     rule=rule->next;
  596.   }
  597. }
  598.  
  599.  
  600. /******** INSERT_PROP(definition,prop)
  601.   Append the properties to the definition if they aren't already present.
  602. */
  603. void insert_prop(d,prop)
  604. ptr_definition d;
  605. ptr_triple_list prop;
  606. {
  607.   ptr_int_list l;
  608.   ptr_triple_list *t;
  609.   long flag;
  610.  
  611.   l=HEAP_ALLOC(int_list);
  612.   l->value=(GENERIC)d;
  613.   l->next=children;
  614.   children=l;
  615.  
  616.   while (prop) {
  617.     t= &(d->properties);
  618.     flag=TRUE;
  619.     
  620.     while (flag) {
  621.       if (*t)
  622.         if ((*t)->a==prop->a && (*t)->b==prop->b && (*t)->c==prop->c)
  623.           flag=FALSE;
  624.         else
  625.           t= &((*t)->next);
  626.       else {
  627.         *t = HEAP_ALLOC(triple_list);
  628.         (*t)->a=prop->a;
  629.         (*t)->b=prop->b;
  630.         (*t)->c=prop->c;
  631.         (*t)->next=NULL;
  632.         flag=FALSE;
  633.       }
  634.     } 
  635.     prop=prop->next;
  636.   }
  637. }
  638.  
  639.  
  640.  
  641. /******** PROPAGATE_DEFINITIONS()
  642.   This routine propagates the definition (attributes,predicates) of a type to 
  643.   all its sons.
  644. */
  645. void propagate_definitions()
  646. {
  647.   ptr_int_list kids;
  648.   ptr_definition d;
  649.   
  650.   adults=NULL;
  651.   find_adults();
  652.   
  653.   while (adults) {
  654.     
  655.     children=NULL;
  656.     
  657.     while (adults) {
  658.       d=(ptr_definition)adults->value;
  659.       
  660.       insert_own_prop(d);
  661.       children=children->next;
  662.       
  663.       kids=d->children;
  664.       
  665.       while(kids) {
  666.         insert_prop(kids->value,d->properties);
  667.         /* if (d->always_check && kids->value)
  668.           ((ptr_definition)kids->value)->always_check=TRUE; */
  669.         kids=kids->next;
  670.       }
  671.       adults=adults->next;
  672.     }
  673.     adults=children;
  674.   }
  675. }
  676.  
  677.  
  678.  
  679. /******************************************************************************
  680.  
  681.   The following routines implement sort encoding.
  682.  
  683. */
  684.  
  685.  
  686.  
  687. /******** COUNT_SORTS(c)
  688.   Count the number of sorts in the symbol table T.
  689.   Overestimates in the module version.  RM: Jan 21 1993 
  690.   No longer !!   RM: Feb  3 1993 
  691.   */
  692. long count_sorts(c0)  /*  RM: Feb  3 1993  */
  693.      long c0;
  694. {
  695.   ptr_definition d;
  696.  
  697.   for(d=first_definition;d;d=d->next)
  698.     if (d->type==type) c0++;
  699.   
  700.   return c0;
  701. }
  702.  
  703.  
  704.  
  705. /******** CLEAR_CODING()
  706.   Clear the bit-vector coding of the sorts.
  707. */
  708. void clear_coding()   /*  RM: Feb  3 1993  */
  709.  
  710. {
  711.   ptr_definition d;
  712.  
  713.   for(d=first_definition;d;d=d->next)
  714.     if (d->type==type) d->code=NOT_CODED;
  715. }
  716.  
  717.  
  718.  
  719. /******** LEAST_SORTS()
  720.   Build the list of terminals (i.e. sorts with no children) in
  721.   nothing->parents.
  722. */
  723. void least_sorts()  /*  RM: Feb  3 1993  */
  724.  
  725. {
  726.   ptr_definition d;
  727.  
  728.   for(d=first_definition;d;d=d->next)
  729.     if (d->type==type && d->children==NULL && d!=nothing)
  730.       nothing->parents=cons(d,nothing->parents);
  731. }
  732.  
  733.  
  734.  
  735. /******** ALL_SORTS()
  736.   Build a list of all sorts (except nothing) in nothing->parents.
  737.   */
  738.  
  739. void all_sorts()   /*  RM: Feb  3 1993  */
  740.      
  741. {
  742.   ptr_definition d;
  743.   
  744.   for(d=first_definition;d;d=d->next)
  745.     if (d->type==type && d!=nothing)
  746.       nothing->parents=cons(d,nothing->parents);
  747. }
  748.   
  749.  
  750.  
  751. /******** TWO_TO_THE(p)
  752.   Return the code worth 2^p.
  753. */
  754. ptr_int_list two_to_the(p)
  755. long p;
  756. {
  757.   ptr_int_list result,code;
  758.   long v=1;
  759.  
  760.   code=HEAP_ALLOC(int_list);
  761.   code->value=0;
  762.   code->next=NULL;
  763.   result=code;
  764.   
  765.   while (p>=INT_SIZE) {
  766.     code->next=HEAP_ALLOC(int_list);
  767.     code=code->next;
  768.     code->value=0;
  769.     code->next=NULL;
  770.     p=p-INT_SIZE;
  771.   }
  772.  
  773.   v= v<<p ;
  774.   code->value=(GENERIC)v;
  775.  
  776.   return result;
  777. }
  778.  
  779.  
  780. /******** copyTypeCode(code)
  781.   returns copy of code on the heap
  782. */
  783. ptr_int_list copyTypeCode(u)
  784. ptr_int_list u;
  785. {
  786.   ptr_int_list code;
  787.  
  788.   code = HEAP_ALLOC(int_list);
  789.   code->value=0;
  790.   code->next=NULL;
  791.  
  792.   or_codes(code, u);
  793.  
  794.   return code;
  795. }
  796.  
  797.  
  798.  
  799. /******** OR_CODES(code1,code2)
  800.   Performs CODE1 := CODE1 or CODE2,
  801.   'or' being the binary logical operator on bits.
  802. */
  803. void or_codes(u,v)
  804. ptr_int_list u,v;
  805. {
  806.   while (v) {
  807.     u->value= (GENERIC)(((unsigned long)(u->value)) | ((unsigned long)(v->value)));
  808.     v=v->next;
  809.     if (u->next==NULL && v) {
  810.       u->next=HEAP_ALLOC(int_list);
  811.       u=u->next;
  812.       u->value=0;
  813.       u->next=NULL;
  814.     }
  815.     else
  816.       u=u->next;
  817.   }
  818. }
  819.  
  820.  
  821.  
  822. /******** EQUALIZE_CODES(w)
  823.   Make sure all codes are w words long, by increasing the length of the
  824.   shorter ones.
  825.   This simplifies greatly the bitvector manipulation routines.
  826.   This operation should be done after encoding.
  827.   For correct operation, w>=maximum number of words used for a code.
  828. */
  829. equalize_codes(len) /*  RM: Feb  3 1993  */
  830.      int len;
  831. {
  832.   ptr_definition d;
  833.   ptr_int_list c,*ci;
  834.   long i;
  835.   int w;
  836.   
  837.   for(d=first_definition;d;d=d->next)
  838.     if (d->type==type) {
  839.       c = d->code;
  840.       ci = &(d->code);  /*  RM: Feb 15 1993  */
  841.       w=len;
  842.       
  843.       /* Count how many words have to be added */
  844.       while (c) {
  845.         ci= &(c->next);
  846.         c=c->next;
  847.         w--;
  848.       }
  849.       assert(w>=0);
  850.       /* Add the words */
  851.       for (i=0; i<w; i++) {
  852.         *ci = HEAP_ALLOC(int_list);
  853.         (*ci)->value=0;
  854.         ci= &((*ci)->next);
  855.       }
  856.       (*ci)=NULL;
  857.     }
  858. }
  859.  
  860.  
  861.  
  862. long type_member();
  863.  
  864.  
  865. /******** MAKE_TYPE_LINK(t1,t2)
  866.   Assert that T1 <| T2, this is used to initialise the built_in type relations
  867.   so that nothing really horrible happens if the user modifies built-in types
  868.   such as INT or LIST.
  869.   This routine also makes sure that top has no links.
  870. */
  871. void make_type_link(t1,t2)
  872. ptr_definition t1, t2;
  873. {
  874. #ifdef OS2_PORT
  875. if (t1)
  876. {
  877. #endif
  878.   if (t2!=top && !type_member(t2,t1->parents))
  879.     t1->parents=cons(t2,t1->parents);
  880.   if (t2!=top && !type_member(t1,t2->children))
  881.     t2->children=cons(t1,t2->children);
  882. #ifdef OS2_PORT
  883. }
  884. #endif
  885. }
  886.  
  887.  
  888.  
  889.  
  890. /******** TYPE_MEMBER(t,tlst)
  891.   Return TRUE iff type t is in the list tlst.
  892. */
  893.  
  894. long type_member(t,tlst)
  895. ptr_definition t;
  896. ptr_int_list tlst;
  897. {
  898.   while (tlst) {
  899.    if (t==(ptr_definition)tlst->value) return TRUE;
  900.    tlst=tlst->next;
  901.   }
  902.   return FALSE;
  903. }
  904.  
  905.  
  906. void perr_sort(d)
  907. ptr_definition d;
  908. {
  909.   perr_s("%s",d->keyword->symbol);
  910. }
  911.  
  912. void perr_sort_list(anc)
  913. ptr_int_list anc;
  914. {
  915.   if (anc) {
  916.     perr_sort_list(anc->next);
  917.     if (anc->next) perr(" <| ");
  918.     perr_sort((ptr_definition)anc->value);
  919.   }
  920. }
  921.  
  922. void perr_sort_cycle(anc)
  923. ptr_int_list anc;
  924. {
  925.   perr_sort((ptr_definition)anc->value);
  926.   perr(" <| ");
  927.   perr_sort_list(anc);
  928. }
  929.  
  930.  
  931.  
  932. /******** TYPE_CYCLICITY(d,anc)
  933.   Check cyclicity of type hierarchy.
  934.   If cyclic, return a TRUE error condition and print an error message
  935.   with a cycle.
  936. */
  937. long type_cyclicity(d,anc)
  938. ptr_definition d;
  939. ptr_int_list anc;
  940. {
  941.   ptr_int_list p=d->parents;
  942.   ptr_definition pd;
  943.   long errflag;
  944.   int_list anc2;
  945.  
  946.   while (p) {
  947.     pd=(ptr_definition)p->value;
  948.     /* If unmarked, mark and recurse */
  949.     if (pd->code==NOT_CODED) {
  950.       pd->code = (ptr_int_list)TRUE;
  951.       anc2.value=(GENERIC)pd;
  952.       anc2.next=anc;
  953.       errflag=type_cyclicity(pd,&anc2);
  954.       if (errflag) return TRUE;
  955.     }
  956.     /* If marked, check if it's in the ancestor list */
  957.     else {
  958.       if (type_member(pd,anc)) {
  959.     Errorline("there is a cycle in the sort hierarchy\n");
  960.         perr("*** Cycle: [");
  961.         perr_sort_cycle(anc);
  962.         perr("]\n");
  963.         exit_life(TRUE);
  964.         return TRUE;
  965.       }
  966.     }
  967.     p=p->next;
  968.   }
  969.   return FALSE;
  970. }
  971.  
  972.  
  973.  
  974. /******** PROPAGATE_ALWAYS_CHECK(d,ch)
  975.   Recursively set the always_check flag to 'FALSE' for all d's
  976.   children.  Continue until encountering only 'FALSE' values. 
  977.   Return a TRUE flag if a change was made somewhere (for the
  978.   closure calculation).
  979. */
  980. void propagate_always_check(d,ch)
  981. ptr_definition d;
  982. long *ch;
  983. {
  984.   ptr_int_list child_list;
  985.   ptr_definition child;
  986.  
  987.   child_list = d->children;
  988.   while (child_list) {
  989.     child = (ptr_definition)child_list->value;
  990.     if (child->always_check) {
  991.       child->always_check = FALSE;
  992.       *ch = TRUE;
  993.       propagate_always_check(child,ch);
  994.     }
  995.     child_list = child_list->next;
  996.   }
  997. }
  998.  
  999.  
  1000.  
  1001. /******** ONE_PASS_ALWAYS_CHECK(ch)
  1002.   Go through the symbol table & propagate all FALSE always_check
  1003.   flags of all sorts to their children.  Return a TRUE flag
  1004.   if a change was made somewhere (for the closure calculation).
  1005. */
  1006. void one_pass_always_check(ch)
  1007.      long *ch;
  1008. {
  1009.   ptr_definition d;
  1010.   
  1011.   
  1012.   for(d=first_definition;d;d=d->next)
  1013.     if (d->type==type && !d->always_check)
  1014.       propagate_always_check(d,ch);
  1015. }
  1016.  
  1017.  
  1018.  
  1019. /******** INHERIT_ALWAYS_CHECK()
  1020.   The 'always_check' flag, if false, should be propagated to a sort's
  1021.   children.  This routine does a closure on this propagation operation
  1022.   for all declared sorts.
  1023. */
  1024. void inherit_always_check()
  1025. {
  1026.   long change;
  1027.  
  1028.   do {
  1029.     change=FALSE;
  1030.     one_pass_always_check(&change);
  1031.   } while (change);
  1032. }
  1033.  
  1034.  
  1035.  
  1036. /******** ENCODE_TYPES()
  1037.   This routine performs type-coding using transitive closure.
  1038.   First any previous coding is undone.
  1039.   Then a new encryption is performed.
  1040.  
  1041.   Some of these routines loop indefinitely if there is a circular type
  1042.   definition (an error should be reported but it isn't implemented (but it's
  1043.   quite easy to do)).
  1044. */
  1045. void encode_types()
  1046. {
  1047.   long p=0,i,possible,ok=TRUE;
  1048.   ptr_int_list layer,l,kids,dads,code;
  1049.   ptr_definition xdef,kdef,ddef,err;
  1050.   
  1051.   if (types_modified) {
  1052.     
  1053.     nothing->parents=NULL;
  1054.     nothing->children=NULL;
  1055.     
  1056.     top->parents=NULL;
  1057.     top->children=NULL;
  1058.  
  1059.     /* The following definitions are vital to avoid crashes */
  1060.     make_type_link(integer,real);
  1061.     make_type_link(true,boolean);
  1062.     make_type_link(false,boolean);
  1063.  
  1064.     /* These just might be useful */
  1065.     make_type_link(quoted_string,built_in);
  1066.     make_type_link(boolean,built_in);
  1067.     make_type_link(real,built_in);
  1068.  
  1069.     make_sys_type_links();
  1070.     
  1071.     type_count=count_sorts(-1); /* bottom does not count */
  1072.     clear_coding();
  1073.     nothing->parents=NULL; /* Must be cleared before all_sorts */
  1074.     all_sorts();
  1075.     if (type_cyclicity(nothing,NULL)) {
  1076.       clear_coding();
  1077.       return;
  1078.     }
  1079.     clear_coding();
  1080.     nothing->parents=NULL; /* Must be cleared before least_sorts */
  1081.     least_sorts();
  1082.     
  1083.     nothing->code=NULL;
  1084.  
  1085.     /*  RM: Feb 17 1993  */
  1086.     Traceline("*** Codes:\n%C= %s\n", NULL, nothing->keyword->symbol);
  1087.     
  1088.     gamma_table=(ptr_definition *) heap_alloc(type_count*sizeof(definition));
  1089.     
  1090.     layer=nothing->parents;
  1091.     
  1092.     while (layer) {
  1093.       l=layer;
  1094.       do {
  1095.         xdef=(ptr_definition)l->value;
  1096.         if (xdef->code==NOT_CODED && xdef!=top) {
  1097.           
  1098.           kids=xdef->children;
  1099.           code=two_to_the(p);
  1100.           
  1101.           while (kids) {
  1102.             kdef=(ptr_definition)kids->value;
  1103.             or_codes(code,kdef->code);
  1104.             kids=kids->next;
  1105.           }
  1106.           
  1107.           xdef->code=code;
  1108.           gamma_table[p]=xdef;
  1109.  
  1110.       /*  RM: Feb 17 1993  */
  1111.           Traceline("%C = %s\n", code, xdef->keyword->symbol);
  1112.           p=p+1;
  1113.         }
  1114.         
  1115.         l=l->next;
  1116.         
  1117.       } while (l);
  1118.       
  1119.       l=layer;
  1120.       layer=NULL;
  1121.       
  1122.       do {
  1123.         xdef=(ptr_definition)l->value;
  1124.         dads=xdef->parents;
  1125.         
  1126.         while (dads) {
  1127.           ddef=(ptr_definition)dads->value;
  1128.           if(ddef->code==NOT_CODED) {
  1129.             
  1130.             possible=TRUE;
  1131.             kids=ddef->children;
  1132.             
  1133.             while(kids && possible) {
  1134.               kdef=(ptr_definition)kids->value;
  1135.               if(kdef->code==NOT_CODED)
  1136.                 possible=FALSE;
  1137.               kids=kids->next;
  1138.             }
  1139.             if(possible)
  1140.               layer=cons(ddef,layer);
  1141.           }
  1142.           dads=dads->next;
  1143.         }
  1144.         l=l->next;
  1145.       } while(l);
  1146.     }
  1147.     
  1148.     top->code=two_to_the(p);
  1149.     for (i=0;i<p;i++)
  1150.       or_codes(top->code,two_to_the(i));
  1151.  
  1152.     gamma_table[p]=top;
  1153.  
  1154.     /*  RM: Jan 13 1993  */
  1155.     /* Added the following line because type_count is now over generous
  1156.        because the same definition can be referenced several times in
  1157.        the symbol table because of modules
  1158.        */
  1159.     type_count=p+1;
  1160.     for(i=type_count;i<type_count;i++)
  1161.       gamma_table[i]=NULL;
  1162.     
  1163.     Traceline("%C = @\n\n", top->code);
  1164.     equalize_codes(p/32+1);
  1165.  
  1166.     propagate_definitions();
  1167.  
  1168.     /* Inherit 'FALSE' always_check flags to all types' children */
  1169.     inherit_always_check();
  1170.     
  1171.     Traceline("*** Encoding done, %d sorts\n",type_count);
  1172.     
  1173.     if (overlap_type(real,quoted_string)) {
  1174.       Errorline("the sorts 'real' and 'string' are not disjoint.\n");
  1175.       ok=FALSE;
  1176.     }
  1177.  
  1178.     /*  RM: Dec 15 1992  I don't think this really matters any more
  1179.     if (overlap_type(real,alist)) {
  1180.     Errorline("the sorts 'real' and 'list' are not disjoint.\n");
  1181.     ok=FALSE;
  1182.     }
  1183.     */
  1184.     
  1185.     /*  RM: Dec 15 1992  I don't think this really matters any more
  1186.     if (overlap_type(alist,quoted_string)) {
  1187.     Errorline("the sorts 'list' and 'string' are not disjoint.\n");
  1188.     ok=FALSE;
  1189.     }
  1190.     */
  1191.     
  1192.     if (!ok) {
  1193.       perr("*** Internal problem:\n");
  1194.       perr("*** Wild_Life may behave abnormally because some basic types\n");
  1195.       perr("*** have been defined incorrectly.\n\n");
  1196.     }
  1197.  
  1198.     types_modified=FALSE;
  1199.     types_done=TRUE;
  1200.   }
  1201. }
  1202.  
  1203.  
  1204.  
  1205. /******** PRINT_CODES()
  1206.   Print all the codes.
  1207. */
  1208. void print_codes()
  1209. {
  1210.   long i;
  1211.  
  1212.   for (i=0; i<type_count; i++) {
  1213.     outputline("%C = %s\n",
  1214.            gamma_table[i]->code,
  1215.            gamma_table[i]->keyword->combined_name);
  1216.   }
  1217. }
  1218.  
  1219.  
  1220. long sub_CodeType();
  1221.  
  1222.  
  1223. /******** GLB_VALUE(result,f,c,value1,value2,value)
  1224.   Do the comparison of the value fields of two psi-terms.
  1225.   This is used in conjunction with glb_code to correctly implement
  1226.   completeness for disequality for psi-terms with non-NULL value fields.
  1227.   This must be preceded by a call to glb_code, since it uses the outputs
  1228.   of that call.
  1229.  
  1230.   result   result of preceding glb_code call (non-NULL iff non-empty intersec.)
  1231.   f,c      sort intersection (sortflag & code) of preceding glb_code call.
  1232.   value1   value field of first psi-term.
  1233.   value2   value field of second psi-term.
  1234.   value    output value field (if any).
  1235. */
  1236. long glb_value(result,f,c,value1,value2,value)
  1237. long result;
  1238. long f;
  1239. GENERIC c;
  1240. GENERIC value1,value2,*value;
  1241. {
  1242.   ptr_int_list code;
  1243.  
  1244.   if (!result) return FALSE;
  1245.   if (value1==NULL) {
  1246.     *value=value2;
  1247.     return TRUE;
  1248.   }
  1249.   if (value2==NULL) {
  1250.     *value=value1;
  1251.     return TRUE;
  1252.   }
  1253.   /* At this point, both value fields are non-NULL */
  1254.   /* and must be compared. */
  1255.  
  1256.   /* Get a pointer to the sort code */
  1257.   code = f ? ((ptr_definition)c)->code : (ptr_int_list)c;
  1258.  
  1259.   /* This rather time-consuming analysis is necessary if both objects */
  1260.   /* have non-NULL value fields.  Note that only those objects with a */
  1261.   /* non-NULL value field needed for disentailment are looked at.     */
  1262.   if (sub_CodeType(code,real->code)) {
  1263.     *value=value1;
  1264.     return (*(REAL *)value1 == *(REAL *)value2);
  1265.   }
  1266.   else if (sub_CodeType(code,quoted_string->code)) {
  1267.     *value=value1;
  1268.     return (!strcmp((char *)value1,(char *)value2));
  1269.   }
  1270.   else {
  1271.     /* All other sorts with 'value' fields always return TRUE, that is, */
  1272.     /* the value field plays no role in disentailment. */
  1273.     *value=value1;
  1274.     return TRUE;
  1275.   }
  1276. }
  1277.  
  1278.  
  1279.  
  1280. /******** GLB_CODE(f1,c1,f2,c2,f3,c3) (21.9)
  1281.   Calculate glb of two type codes C1 and C2, put result in C3.
  1282.   Return a result value (see comments of glb(..)).
  1283.  
  1284.   Sorts are stored as a 'Variant Record':
  1285.     f1==TRUE:  c1 is a ptr_definition (an interned symbol).
  1286.     f1==FALSE: c1 is a ptr_int_list (a sort code).
  1287.   The result (f3,c3) is also in this format.
  1288.   This is needed to correctly handle psi-terms that don't have a sort code
  1289.   (for example, functions, predicates, and singleton sorts).
  1290.   The routine handles a bunch of special cases that keep f3==TRUE.
  1291.   Other than that, it is almost a replica of the inner loop of glb(..).
  1292. */
  1293. long glb_code(f1,c1,f2,c2,f3,c3)
  1294. long f1,f2,*f3;
  1295. GENERIC c1,c2,*c3;
  1296. {
  1297.   long result=0;
  1298.   unsigned long v1,v2,v3;
  1299.   ptr_int_list cd1,cd2,*cd3; /* sort codes */
  1300.  
  1301.   /* First, the cases where c1 & c2 are ptr_definitions: */
  1302.   if (f1 && f2) {
  1303.     if ((ptr_definition)c1==(ptr_definition)c2) {
  1304.       *c3=c1;
  1305.       result=1;
  1306.     }
  1307.     else if ((ptr_definition)c1==top) {
  1308.       *c3=c2;
  1309.       if ((ptr_definition)c2==top)
  1310.         result=1;
  1311.       else
  1312.         result=3;
  1313.     }
  1314.     else if ((ptr_definition)c2==top) {
  1315.       *c3=c1;
  1316.       result=2;
  1317.     }
  1318.     /* If both inputs are either top or the same ptr_definition */
  1319.     /* then can return quickly with a ptr_definition. */
  1320.     if (result) {
  1321.       *f3=TRUE; /* c3 is ptr_definition (an interned symbol) */
  1322.       return result;
  1323.     }
  1324.   }
  1325.   /* In the other cases, can't return with a ptr_definition: */
  1326.   cd1=(ptr_int_list)(f1?(GENERIC)((ptr_definition)c1)->code:c1);
  1327.   cd2=(ptr_int_list)(f2?(GENERIC)((ptr_definition)c2)->code:c2);
  1328.   cd3=(ptr_int_list*)c3;
  1329.   *f3=FALSE; /* cd3 is ptr_int_list (a sort code) */
  1330.   if (cd1==NOT_CODED) {
  1331.     if (cd2==NOT_CODED) {
  1332.       if (c1==c2) {
  1333.         *cd3=cd1;
  1334.         result=1;
  1335.       }
  1336.       else
  1337.         result=0;
  1338.     }
  1339.     else if (cd2==top->code) {
  1340.       *cd3=cd1;
  1341.       result=2;
  1342.     }
  1343.     else
  1344.       result=0;
  1345.   }
  1346.   else if (cd1==top->code) {
  1347.     if (cd2==top->code) {
  1348.       *cd3=cd1;
  1349.       result=1;
  1350.     }
  1351.     else {
  1352.       *cd3=cd2;
  1353.       result=3;
  1354.     }
  1355.   }
  1356.   else if (cd2==NOT_CODED)
  1357.     result=0;
  1358.   else if (cd2==top->code) {
  1359.     *cd3=cd1;
  1360.     result=2;
  1361.   }
  1362.   else while (cd1 && cd2) {
  1363.     /* Bit operations needed only if c1 & c2 coded & different from top */
  1364.     *cd3 = STACK_ALLOC(int_list);
  1365.     (*cd3)->next=NULL;
  1366.     
  1367.     v1=(unsigned long)(cd1->value);
  1368.     v2=(unsigned long)(cd2->value);
  1369.     v3=v1 & v2;
  1370.     (*cd3)->value=(GENERIC)v3;
  1371.     
  1372.     if (v3) {
  1373.       if (v3<v1 && v3<v2)
  1374.         result=4;
  1375.       else if (result!=4)
  1376.         if (v1<v2)
  1377.           result=2;
  1378.         else if (v1>v2)
  1379.           result=3;
  1380.         else
  1381.           result=1;
  1382.     }
  1383.     else if (result)
  1384.       if (v1 || v2)
  1385.         result=4;
  1386.         
  1387.     cd1=cd1->next;
  1388.     cd2=cd2->next;
  1389.     cd3= &((*cd3)->next);
  1390.   }
  1391.  
  1392.   return result;
  1393. }
  1394.  
  1395.  
  1396.  
  1397. /******** GLB(t1,t2,t3)
  1398.   This function returns the Greatest Lower Bound of two types T1 and T2 in T3.
  1399.   
  1400.   T3 = T1 /\ T2
  1401.  
  1402.   If T3 is not a simple type then C3 is its code, and T3=NULL.
  1403.   
  1404.   It also does some type comparing, and returns
  1405.   
  1406.   0 if T3 = bottom
  1407.   1 if T1 = T2
  1408.   2 if T1 <| T2 ( T3 = T1 )
  1409.   3 if T1 |> T2 ( T3 = T2 )
  1410.   4 otherwise   ( T3 strictly <| T1 and T3 strictly <| T2 )
  1411.   
  1412.   These results are used for knowing when to inherit properties or release
  1413.   residuations.
  1414.   The t3 field is NULL iff a new type is needed to represent the
  1415.   result.
  1416. */
  1417. /*  RM: May  7 1993  Fixed bug in when multiple word code */
  1418. long glb(t1,t2,t3,c3)
  1419. ptr_definition t1;
  1420. ptr_definition t2;
  1421. ptr_definition  *t3;
  1422. ptr_int_list *c3;
  1423. {
  1424.   ptr_int_list c1,c2;
  1425.   long result=0;
  1426.   unsigned long v1,v2,v3;
  1427.   int e1,e2,b; /*  RM: May  7 1993  */
  1428.  
  1429.  
  1430.   
  1431.   *c3=NULL;
  1432.   
  1433.   if (t1==t2) { 
  1434.     result=1;
  1435.     *t3= t1;
  1436.   }
  1437.   else if (t1==top) {
  1438.     *t3= t2;
  1439.     if (t2==top)
  1440.       result=1;
  1441.     else
  1442.       result=3;
  1443.   }
  1444.   else if (t2==top) {
  1445.     result=2;
  1446.     *t3= t1;
  1447.   }
  1448.   else {
  1449.     /* printf("glb of %s and %s\n",
  1450.        t1->keyword->combined_name,
  1451.        t2->keyword->combined_name); */
  1452.        
  1453.     c1=t1->code;
  1454.     c2=t2->code;
  1455.  
  1456.     e1=TRUE;e2=TRUE;b=TRUE;
  1457.     
  1458.     if (c1!=NOT_CODED && c2!=NOT_CODED) {
  1459.       result=0;
  1460.       while (c1 && c2) {
  1461.  
  1462.         *c3 = STACK_ALLOC(int_list);
  1463.         (*c3)->next=NULL;
  1464.  
  1465.         v1=(unsigned long)(c1->value);
  1466.         v2=(unsigned long)(c2->value);
  1467.         v3=v1 & v2;
  1468.  
  1469.     /* printf("v1=%d, v2=%d, v3=%d\n",v1,v2,v3); */
  1470.     
  1471.         (*c3)->value=(GENERIC)v3;
  1472.  
  1473.     if(v3!=v1) /*  RM: May  7 1993  */
  1474.       e1=FALSE;
  1475.     if(v3!=v2)
  1476.       e2=FALSE;
  1477.     if(v3)
  1478.       b=FALSE;
  1479.     
  1480.         c1=c1->next;
  1481.         c2=c2->next;
  1482.         c3= &((*c3)->next);
  1483.       }
  1484.       *t3=NULL;
  1485.  
  1486.       if(b) /*  RM: May  7 1993  */
  1487.     result=0; /* 0 if T3 = bottom */
  1488.       else
  1489.     if(e1)
  1490.       if(e2)
  1491.         result=1; /* 1 if T1 = T2 */
  1492.       else
  1493.         result=2; /* 2 if T1 <| T2 ( T3 = T1 ) */
  1494.     else
  1495.       if(e2)
  1496.         result=3; /* 3 if T1 |> T2 ( T3 = T2 ) */
  1497.       else
  1498.         result=4; /* 4 otherwise */
  1499.     }
  1500.   }
  1501.   
  1502.   if (!result) *t3=nothing;
  1503.   
  1504.   /* printf("result=%d\n\n",result); */
  1505.   
  1506.   return result;
  1507. }
  1508.  
  1509.  
  1510.  
  1511. /******** OVERLAP_TYPE(t1,t2)
  1512.   This function returns TRUE if GLB(t1,t2)!=bottom.
  1513.   This is essentially the same thing as GLB, only it's faster 'cause we don't
  1514.   care about the resulting code.
  1515. */
  1516. long overlap_type(t1,t2)
  1517. ptr_definition t1;
  1518. ptr_definition t2;
  1519. {
  1520.   ptr_int_list c1,c2;
  1521.   long result=TRUE;
  1522.   
  1523.   if (t1!=t2 && t1!=top && t2!=top) {
  1524.     
  1525.     c1=t1->code;
  1526.     c2=t2->code;
  1527.     result=FALSE;
  1528.  
  1529.     if (c1!=NOT_CODED && c2!=NOT_CODED) {     
  1530.       while (!result && c1 && c2) {          
  1531.         result=(((unsigned long)(c1->value)) & ((unsigned long)(c2->value)));
  1532.         c1=c1->next;
  1533.         c2=c2->next;
  1534.       }
  1535.     }
  1536.   }
  1537.   
  1538.   /*
  1539.   printf("overlap_type(%s,%s) => %ld\n",t1->def->keyword->symbol,t2->def->keyword->symbol,result);
  1540.   */
  1541.   
  1542.   return result;
  1543. }
  1544.  
  1545.  
  1546. /******** SUB_CodeType(c1,c2)
  1547.   Return TRUE if code C1 is <| than type C2, that is if type represented
  1548.   by code C1 matches type represented by C2.
  1549.  
  1550.   We already know that t1 and t2 are not top.
  1551. */
  1552. long sub_CodeType(c1,c2)
  1553. ptr_int_list c1;
  1554. ptr_int_list c2;
  1555. {
  1556.   if (c1!=NOT_CODED && c2!=NOT_CODED) {
  1557.     while (c1 && c2) {
  1558.       if ((unsigned long)c1->value & ~(unsigned long)c2->value) return FALSE;
  1559.       c1=c1->next;
  1560.       c2=c2->next;
  1561.     }
  1562.   }
  1563.   else
  1564.     return FALSE;
  1565.  
  1566.   return TRUE;
  1567. }
  1568.  
  1569.  
  1570.  
  1571. /******** SUB_TYPE(t1,t2)
  1572.   Return TRUE if type T1 is <| than type T2, that is if T1 matches T2.
  1573. */
  1574. long sub_type(t1,t2)
  1575. ptr_definition t1;
  1576. ptr_definition t2;
  1577. {
  1578.   if (t1!=t2)
  1579.     if (t2!=top)
  1580.     {
  1581.       if (t1==top)
  1582.         return FALSE;
  1583.       else
  1584.         return sub_CodeType(t1->code, t2->code);
  1585.     }
  1586.   return TRUE;
  1587. }
  1588.  
  1589.  
  1590.  
  1591. /******** MATCHES(t1,t2,s)
  1592.   Returns TRUE if GLB(t1,t2)!=bottom.
  1593.   Sets S to TRUE if type T1 is <| than type T2, that is if T1 matches T2.
  1594. */
  1595. long matches(t1,t2,smaller)
  1596. ptr_definition t1;
  1597. ptr_definition t2;
  1598. long *smaller;
  1599. {
  1600.   ptr_int_list c1,c2;
  1601.   long result=TRUE;
  1602.   
  1603.   *smaller=TRUE;
  1604.   
  1605.   if (t1!=t2)
  1606.     if (t2!=top)
  1607.       if (t1==top)
  1608.         *smaller=FALSE;
  1609.       else {
  1610.         c1=t1->code;
  1611.         c2=t2->code;
  1612.         result=FALSE;
  1613.         
  1614.         if (c1!=NOT_CODED && c2!=NOT_CODED) {          
  1615.           while (c1 && c2) {          
  1616.             if ((unsigned long)c1->value &  (unsigned long)c2->value) result=TRUE;
  1617.             if ((unsigned long)c1->value & ~(unsigned long)c2->value) *smaller=FALSE;
  1618.             c1=c1->next;
  1619.             c2=c2->next;
  1620.           }
  1621.         }
  1622.         else
  1623.           *smaller=FALSE;
  1624.       }
  1625.   
  1626.   return result;
  1627. }
  1628.  
  1629.  
  1630.  
  1631. /******** STRICT_MATCHES(t1,t2,s)
  1632.   Almost the same as matches, except that S is set to TRUE only
  1633.   if the type of t1 is strictly less than the type of t2.
  1634.   Because of the implementation of ints, reals, strings, and lists,
  1635.   this has to take the value field into account, and thus must
  1636.   be passed the whole psi-term.
  1637. */
  1638. long strict_matches(t1,t2,smaller)
  1639. ptr_psi_term t1;
  1640. ptr_psi_term t2;
  1641. long *smaller;
  1642. {
  1643.   long result,sm;
  1644.  
  1645.   result=matches(t1->type,t2->type,&sm);
  1646.  
  1647.   if (sm) {
  1648.     /* At this point, t1->type <| t2->type */
  1649.     if (t1->type==t2->type) {
  1650.       /* Same types: strict only if first has a value & second does not */
  1651.       if (t1->value!=NULL && t2->value==NULL)
  1652.         sm=TRUE;
  1653.       else
  1654.         sm=FALSE;
  1655.     }
  1656.     else {
  1657.       /* Different types: the first must be strictly smaller */
  1658.       sm=TRUE;
  1659.     }
  1660.   }
  1661.  
  1662.   *smaller=sm;
  1663.   return result;
  1664. }
  1665.  
  1666.  
  1667.  
  1668. /******** BIT_LENGTH(c)
  1669.   Returns the number of bits needed to code C. That is the rank of the first
  1670.   non NULL bit of C.
  1671.   
  1672.   Examples:
  1673.   C= 1001001000   result=7
  1674.   C= 10000        result=1
  1675.   C= 0000000      result=0
  1676.   
  1677. */
  1678. long bit_length(c)
  1679. ptr_int_list c;
  1680. {
  1681.   unsigned long p=0,dp=0,v=0,dv=0;
  1682.   
  1683.   while (c) {
  1684.     v=(unsigned long)c->value;
  1685.     if(v) {
  1686.       dp=p;
  1687.       dv=v;
  1688.     }
  1689.     c=c->next;
  1690.     p=p+INT_SIZE;
  1691.   }
  1692.   
  1693.   while (dv) {
  1694.     dp++;
  1695.     dv=dv>>1;
  1696.   }
  1697.   
  1698.   return dp;
  1699. }
  1700.  
  1701.  
  1702.  
  1703. /******** DECODE(c)
  1704.   Returns a list of the symbol names which make up the disjunction whose
  1705.   code is C.
  1706. */
  1707.  
  1708. ptr_int_list decode(c)
  1709. ptr_int_list c;
  1710. {
  1711.   ptr_int_list c2,c3,c4,result=NULL,*prev;
  1712.   long p;
  1713.   
  1714.   p=bit_length(c);
  1715.   
  1716.   while (p) {
  1717.     p--;
  1718.     c2=gamma_table[p]->code;
  1719.     result=cons(gamma_table[p],result);
  1720.     prev= &c4;
  1721.     *prev=NULL;
  1722.     
  1723.     while (c2) {
  1724.       c3=STACK_ALLOC(int_list);
  1725.       *prev=c3;
  1726.       prev= &(c3->next);
  1727.       *prev=NULL;
  1728.       
  1729.       c3->value=(GENERIC)(((unsigned long)(c->value)) & ~((unsigned long)(c2->value)));
  1730.       
  1731.       c=c->next;
  1732.       c2=c2->next;
  1733.     }
  1734.     
  1735.     c=c4;
  1736.     p=bit_length(c);
  1737.   }
  1738.   
  1739.   return result;
  1740. }
  1741.