/************************************************************************ * * * The SB-Prolog System * * Copyright SUNY at Stony Brook, 1986 * * * ************************************************************************/ /*----------------------------------------------------------------- SB-Prolog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the SB-Prolog General Public License for full details. Everyone is granted permission to copy, modify and redistribute SB-Prolog, but only under the conditions described in the SB-Prolog General Public License. A copy of this license is supposed to have been given to you along with SB-Prolog so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. ------------------------------------------------------------------ */ /* unify.c */ #include "sim.h" #include "aux.h" extern prettymuch_equal(); extern double floatval(); unify(op1, op2) word op1, op2; { /* begin unify */ register pw top; long arity, i; nun: switch ((int)(op1 & 3)) { case FREE: /* op1 */ nderef(op1, nun); nunify_with_free: /* op1 is a dereffed free node */ switch ((int)(op2 & 3)) { case FREE: /* op1 = free var, op2 = free var */ nderef(op2, nunify_with_free); if ( op1 != op2 ) { if ( op1 < op2 ) { if ( op1 < (word)hreg ) /* op1 not in loc stack */ {follow(op2) = op1; pushtrail(op2);} else /* op1 points to op2 */ {follow(op1) = op2; pushtrail(op1);} } else { /* op1 > op2 */ if ( op2 < (word)hreg ) {follow(op1) = op2; pushtrail(op1);} else {follow(op2) = op1; pushtrail(op2);} } } return 1; /* op1=free, op2=free */ case CS: /* op1=free, op2=con/str */ case LIST: /* op1=free, op2=list */ case NUM: /* op1=free, op2=num */ follow(op1) = op2; pushtrail(op1); return 1; /* op1=free, op2=c/s,list,num*/ } case CS: /* op1=c/s */ nu2: switch ((int)(op2 & 3)) { case FREE: /* op1=con/str, op2=free */ nderef(op2, nu2); follow(op2) = op1; pushtrail(op2); return 1; /* op1=con/str, op2=free */ case CS: /* op1=con/str, op2=con/str */ if (op1 != op2) { /* a != b */ untag(op1); untag(op2); if (follow(op1) != follow(op2)) { /* 0(a) != 0(b) */ return 0; /* op1=c/s, op2=c/s */ } else { arity = get_str_arity(op1); for ( i=1; i <= arity; i++ ) if(!unify(*((pw)op1+i), *((pw)op2+i))) return 0; } } return 1; /* out of con/str, con/str */ case LIST: /* op1 = con/str, op2 = list */ case NUM: return 0; /* op1=c/s, op2=list, */ } /* end case op1=c/s */ case LIST: /* op1=list */ nu3: switch ((int)(op2 & 3)) { case FREE: /* op1=list, op2=free */ nderef(op2, nu3); follow(op2) = op1; pushtrail(op2); return 1; /* op1-list, op2=free */ case CS: /* op1=list, op2=con/str */ case NUM: /* op1=list, op2=num */ return 0; /* op1=list, op2=c/s,num */ case LIST: /* op1=list, op2=list */ if (op1 != op2) { untag(op1); untag(op2); if ( !unify(*(pw)op1, *(pw)op2) || !unify( *(((pw)op1)+1), *(((pw)op2)+1) ) ) return 0; } return 1; /* op1=list, op2=list */ } case NUM: /* op1=num */ nwn: switch ((int)(op2 & 3)) { case FREE: /* op1=num, op2=free */ nderef(op2, nwn); follow(op2) = op1; pushtrail(op2); return 1; /* op1=num, op2=free */ case NUM: /*op1=num, op2=num */ if (op1 == op2) return 1; /* op1=num, op2=num */ else if ((isfloat(op1) || isfloat(op2)) && prettymuch_equal((double)numval(op2), (double)numval(op1))) return 1; case CS: case LIST: return 0; /* op1=num, op2=c/s,list */ } /* disp on op2 */ } /* end of disp on op1 */ } /* end of unify */