home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / SIM / unify.c < prev   
Text File  |  1991-08-10  |  5KB  |  151 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* unify.c */
  26.  
  27. #include "sim.h"
  28. #include "aux.h"
  29.  
  30. extern prettymuch_equal();
  31. extern double floatval();
  32.  
  33. unify(op1, op2)
  34. word op1, op2;
  35. { /* begin unify */
  36.   register pw top;
  37.   long arity, i;
  38.  
  39.   nun: switch ((int)(op1 & 3)) {
  40.   case FREE: /* op1 */
  41.     nderef(op1, nun);
  42.     nunify_with_free: /* op1 is a dereffed free node */
  43.         switch ((int)(op2 & 3)) {
  44.         case FREE:  /* op1 = free var, op2 = free var */
  45.         nderef(op2,  nunify_with_free);
  46.         if ( op1 != op2 ) {
  47.             if ( op1 < op2 ) {
  48.             if ( op1 < (word)hreg )  /* op1 not in loc stack */
  49.                 {follow(op2) = op1;
  50.                  pushtrail(op2);}
  51.             else  /* op1 points to op2 */
  52.                 {follow(op1) = op2;
  53.                  pushtrail(op1);}
  54.             }
  55.             else { /* op1 > op2 */
  56.             if ( op2 < (word)hreg ) 
  57.                 {follow(op1) = op2;
  58.                  pushtrail(op1);}
  59.             else
  60.                 {follow(op2) = op1;
  61.                  pushtrail(op2);}
  62.             }
  63.         }
  64.         return 1; /* op1=free, op2=free */
  65.         case CS:      /* op1=free, op2=con/str */
  66.         case LIST:      /* op1=free, op2=list */
  67.         case NUM:     /* op1=free, op2=num */
  68.         follow(op1) = op2;
  69.         pushtrail(op1);
  70.         return 1;    /* op1=free, op2=c/s,list,num*/
  71.         }
  72.  
  73.   case CS: /* op1=c/s */
  74.     nu2: switch ((int)(op2 & 3)) {
  75.     case FREE:  /* op1=con/str, op2=free */
  76.     nderef(op2, nu2);
  77.     follow(op2) = op1;
  78.     pushtrail(op2);
  79.         return 1; /* op1=con/str, op2=free */
  80.     case CS:   /* op1=con/str, op2=con/str */
  81.         if (op1 != op2) {    /* a != b */
  82.         untag(op1);
  83.         untag(op2);
  84.         if (follow(op1) != follow(op2)) { /* 0(a) != 0(b) */
  85.         return 0; /* op1=c/s, op2=c/s */
  86.         }
  87.         else {
  88.                arity = get_str_arity(op1);
  89.                for ( i=1; i <= arity;  i++ ) 
  90.                  if(!unify(*((pw)op1+i), *((pw)op2+i)))
  91.             return 0;
  92.         }
  93.     }
  94.         return 1; /* out of con/str, con/str */
  95.     case LIST:    /* op1 = con/str, op2 = list */
  96.     case NUM:
  97.       return 0;
  98.        /* op1=c/s, op2=list, */
  99.     } /* end case op1=c/s */
  100.  
  101.   case LIST:    /* op1=list */
  102.     nu3: switch ((int)(op2 & 3)) {
  103.     case FREE:  /* op1=list, op2=free */
  104.       nderef(op2, nu3);
  105.       follow(op2) = op1;
  106.       pushtrail(op2);
  107.       return 1; /* op1-list, op2=free */
  108.     case CS:    /* op1=list, op2=con/str */
  109.     case NUM:    /* op1=list, op2=num */
  110.       return 0;    /* op1=list, op2=c/s,num */
  111.     case LIST:   /* op1=list, op2=list */
  112.       if (op1 != op2) {
  113.          untag(op1);
  114.          untag(op2);
  115.          if ( !unify(*(pw)op1, *(pw)op2)
  116.              || !unify( *(((pw)op1)+1), *(((pw)op2)+1) ) )
  117.         return 0;
  118.      }
  119.       return 1; /* op1=list, op2=list */
  120.     }
  121.  
  122.   case NUM:    /* op1=num */
  123.     nwn: switch ((int)(op2 & 3)) {
  124.     case FREE:  /* op1=num, op2=free */
  125.         nderef(op2, nwn);
  126.         follow(op2) = op1;
  127.         pushtrail(op2);
  128.         return 1; /* op1=num, op2=free */
  129.         case NUM:   /*op1=num, op2=num */
  130.         if (op1 == op2) return 1;        /* op1=num, op2=num */
  131.         else
  132. #ifndef OS9
  133.         if ((isfloat(op1) || isfloat(op2)) && prettymuch_equal((double)numval(op2), (double)numval(op1)))
  134.         return 1;
  135. #else
  136.         if ((isfloat(op1) && isinteger(op2) &&
  137.             prettymuch_equal((double)intval(op2), (double)floatval(op1))) ||
  138.             (isinteger(op1) && isfloat(op2) &&
  139.             prettymuch_equal((double)floatval(op2), (double)intval(op1))) ||
  140.             (isfloat(op1) && isfloat(op2) &&
  141.             prettymuch_equal((double)floatval(op2), (double)floatval(op1))))
  142.         return 1;
  143. #endif
  144.     case CS:
  145.     case LIST:
  146.         return 0; /* op1=num, op2=c/s,list */
  147.     }    /* disp on op2 */
  148.   }    /* end of disp on op1 */
  149. }  /* end of unify */
  150.  
  151.