1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
/*
* $Id: $
* $Version: $
*
* Copyright (c) Tanel Tammet 2004,2005,2006,2007,2008,2009,2010
*
* Contact: tanel.tammet@gmail.com
*
* This file is part of WhiteDB
*
* WhiteDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WhiteDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WhiteDB. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @file unify.c
* Unification functions.
*
*/
/* ====== Includes =============== */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "rincludes.h"
//#define DEBUG
#undef DEBUG
/* ====== Private headers and defs ======== */
static gint wr_occurs_in(glb* g, gint x, gint y, gptr vb);
/* ======= Private protos ================ */
/* ====== Functions ============== */
/** Plain term unification using g->unify_samelen and g->unify_maxuseterms
*
* Metainfo is not filtered out. Must be exactly the same.
*
*/
gint wr_unify_term(glb* g, gint x, gint y, int uniquestrflag) {
g->tmp_unify_vc=((gptr)(g->varstack))+1; // pointer arithmetic: &(varstack[1])
g->tmp_unify_occcheck=1;
if (wr_unify_term_aux(g,x,y,uniquestrflag)) {
return 1;
} else {
return 0;
}
}
/** Plain term unification using g->unify_samelen and g->unify_maxuseterms
*
* Metainfo is not filtered out. Must be exactly the same.
*
*/
gint wr_unify_term_aux(glb* g, gint x, gint y, int uniquestrflag) {
gptr db;
gint encx,ency;
gint tmp; // used by VARVAL_F macro
gptr xptr,yptr;
int xlen,ylen,uselen,ilimit,i;
#ifdef DEBUG
printf("wr_unify_term_aux called with x %d ",x);
wr_print_term(g,x);
printf(" and y %d ",y);
wr_print_term(g,y);
printf("\n");
#endif
// first check if immediately same: return 1 if yes
if (x==y) return 1;
// second, fetch var values for var args
if (isvar(x)) x=VARVAL_F(x,(g->varbanks));
if (isvar(y)) y=VARVAL_F(y,(g->varbanks));
// check again if same
if (x==y) return 1;
// go through the ladder of possibilities
// knowing that x and y are different
if (!isdatarec(x)) {
// x is a primitive
if (!isdatarec(y)) {
// both x and y are primitive
if (isvar(x)) {
SETVAR(x,y,g->varbanks,g->varstack,g->tmp_unify_vc);
// set occcheck only if y is a var too
if (g->tmp_unify_do_occcheck && isvar(y)) (g->tmp_unify_occcheck)=1;
return 1;
} else if (isvar(y)) {
SETVAR(y,x,g->varbanks,g->varstack,g->tmp_unify_vc);
// do not set occcheck here: x is a constant!
return 1;
} else {
// x and y are constants
if (wr_equal_ptr_primitives(g,x,y,uniquestrflag)) return 1;
else return 0;
}
// x is primitive, but y is not
} else if (isvar(x)) {
// x is var, y is non-primitive
if (g->tmp_unify_occcheck && wr_occurs_in(g,x,y,(gptr)(g->varbanks))) {
return 0;
} else {
SETVAR(x,y,g->varbanks,g->varstack,g->tmp_unify_vc);
if (g->tmp_unify_do_occcheck) (g->tmp_unify_occcheck)=1;
return 1;
}
} else {
// x is a constant, but y is non-primitive
return 0;
}
// x is not primitive
} else if (isvar(y)) {
// x is non-primitive, y is var
if (g->tmp_unify_occcheck && wr_occurs_in(g,y,x,(gptr)(g->varbanks))) {
return 0;
} else {
SETVAR(y,x,g->varbanks,g->varstack,g->tmp_unify_vc);
if (g->tmp_unify_do_occcheck) (g->tmp_unify_occcheck)=1;
return 1;
}
// x is not primitive, y is non-var
} else if (!isdatarec(y)) {
// x is not primitive, y is constant
return 0;
} else {
// x and y are both complex terms
db=g->db;
xptr=decode_record(db,x);
yptr=decode_record(db,y);
xlen=get_record_len(xptr);
ylen=get_record_len(yptr);
if (g->unify_samelen) {
if (xlen!=ylen) return 0;
uselen=xlen;
} else {
if (xlen<=ylen) uselen=xlen;
else uselen=ylen;
}
if (g->unify_maxuseterms) {
if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen)
uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms);
}
ilimit=RECORD_HEADER_GINTS+uselen;
for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) {
encx=*(xptr+i);
ency=*(yptr+i);
if (encx!=ency && !wr_unify_term_aux(g,encx,ency,uniquestrflag)) return 0;
}
return 1;
}
}
/**
x must be a variable and y must be a nonprimitive
*/
static gint wr_occurs_in(glb* g, gint x, gint y, gptr vb) {
void* db=g->db;
gptr yptr;
gint yi;
int ylen,ilimit,i;
gint tmp; // used by VARVAL_F
#ifdef DEBUG
printf("wr_occurs_in called with x %d ",x);
wr_print_term(g,x);
printf(" and y %d ",y);
wr_print_term(g,y);
printf("\n");
#endif
yptr=decode_record(db,y);
ylen=get_record_len(yptr);
if (g->unify_maxuseterms) {
if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<ylen)
ylen=(g->unify_firstuseterm)+(g->unify_maxuseterms);
}
ilimit=RECORD_HEADER_GINTS+ylen;
for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) {
yi=*(yptr+i);
if (isvar(yi)) yi=VARVAL_F(yi,vb);
if (x==yi) return 1;
if (isdatarec(yi)) {
if (wr_occurs_in(g,x,yi,vb)) return 1;
}
}
// passed vector ok, not finding x inside
return 0;
}
/* ----------------------------------------------------------
Matching: one-sided unification
------------------------------------------------------------- */
/** Match terms: x is the general term, y is an instance of x
using g->unify_samelen and g->unify_maxuseterms
*/
gint wr_match_term(glb* g, gint x, gint y, int uniquestrflag) {
g->tmp_unify_vc=((gptr)(g->varstack))+1; // pointer arithmetic: &(varstack[1])
if (wr_match_term_aux(g,x,y,uniquestrflag)) {
return 1;
} else {
return 0;
}
}
/** Plain term matching using g->unify_samelen and g->unify_maxuseterms
returns 1 iff x subsumes y
assumptions:
??? GBUNASSIGNEDVAL!=1 ????
?? xvars are initially all unassigned ??
?? yvars are initially all unassigned ??
xvars do not have to be different from yvars
*/
gint wr_match_term_aux(glb* g, gint x, gint y, int uniquestrflag) {
gptr db;
gint xval,encx,ency;
gptr xptr,yptr;
int xlen,ylen,uselen,ilimit,i;
gint eqencx; // used by WR_EQUAL_TERM macro
#ifdef DEBUG
printf("wr_match_term_aux called with x %d ",x);
wr_print_term(g,x);
printf(" and y %d ",y);
wr_print_term(g,y);
printf("\n");
#endif
// check x var case immediately
if (isvar(x)) {
xval=VARVAL_DIRECT(x,(g->varbanks));
if (xval==UNASSIGNED) {
// previously unassigned var: assign now and return
SETVAR(x,y,g->varbanks,g->varstack,g->tmp_unify_vc);
return 1;
} else {
// xval must now be equal to y, else match fails
if (WR_EQUAL_TERM(g,xval,y,uniquestrflag)) return 1;
return 0;
}
}
// now x is not var
if (!isdatarec(x)) {
if (WR_EQUAL_TERM(g,x,y,uniquestrflag)) return 1;
}
if (!isdatarec(y)) return 0; // x is datarec but y is not
// now x and y are different datarecs
if (1) {
db=g->db;
xptr=decode_record(db,x);
yptr=decode_record(db,y);
xlen=get_record_len(xptr);
ylen=get_record_len(yptr);
if (g->unify_samelen) {
if (xlen!=ylen) return 0;
uselen=xlen;
} else {
if (xlen<=ylen) uselen=xlen;
else uselen=ylen;
}
if (g->unify_maxuseterms) {
if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen)
uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms);
}
ilimit=RECORD_HEADER_GINTS+uselen;
for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) {
encx=*(xptr+i);
ency=*(yptr+i);
if (!wr_match_term_aux(g,encx,ency,uniquestrflag)) return 0;
}
return 1;
}
}
/* ----------------------------------------------------------
Equality check
------------------------------------------------------------- */
/** Plain term equality check using g->unify_samelen and g->unify_maxuseterms
*
* Metainfo is not filtered out. Must be exactly the same.
*
* NB! For a faster version use macro WR_EQUAL_TERM doing the same thing
* and assuming the presence of the gint eqencx variable
*/
gint wr_equal_term(glb* g, gint x, gint y, int uniquestrflag) {
gptr db;
gint encx,ency;
gptr xptr,yptr;
int xlen,ylen,uselen,i,ilimit;
gint eqencx; // used by the WR_EQUAL_TERM macro
#ifdef DEBUG
printf("wr_equal_term called with x %d and y %d\n",x,y);
#endif
// first check if immediately same: return 1 if yes
if (x==y) return 1;
// handle immediate check cases: for these bit suffixes x is equal to y iff x==y
encx=(x&NORMALPTRMASK);
if ((encx==LONGSTRBITS && uniquestrflag) || encx==SMALLINTBITS || encx==NORMALPTRMASK) return 0;
// immediate value: must be unequal since x==y checked before
if (!isptr(x) || !isptr(y)) return 0;
// here both x and y are ptr types
// quick type check: last two bits
if (((x)&NONPTRBITS)!=((y)&NONPTRBITS)) return 0;
// if one is datarec, the other must be as well
if (!isdatarec(x)) {
if (isdatarec(y)) return 0;
// neither x nor y are datarecs
// need to check pointed values
if (wr_equal_ptr_primitives(g,x,y,uniquestrflag)) return 1;
else return 0;
} else {
if (!isdatarec(y)) return 0;
// both x and y are datarecs
db=g->db;
xptr=decode_record(db,x);
yptr=decode_record(db,y);
xlen=get_record_len(xptr);
ylen=get_record_len(yptr);
if (g->unify_samelen) {
if (xlen!=ylen) return 0;
uselen=xlen;
} else {
if (xlen<=ylen) uselen=xlen;
else uselen=ylen;
}
if (g->unify_maxuseterms) {
if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen)
uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms);
}
ilimit=RECORD_HEADER_GINTS+uselen;
for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) {
encx=*(xptr+i);
ency=*(yptr+i);
if (!WR_EQUAL_TERM(g,encx,ency,uniquestrflag)) return 0;
}
return 1;
}
}
/** Plain term equality check partial version ONLY called by WR_EQUAL_TERM macro
*
*
*/
gint wr_equal_term_macroaux(glb* g, gint x, gint y, int uniquestrflag) {
gptr db;
gint encx,ency;
gptr xptr,yptr;
int xlen,ylen,uselen,i,ilimit;
gint eqencx; // used by WR_EQUAL_TERM macro
#ifdef DEBUG
printf("wr_equal_term_macroaux called with x %d and y %d\n",x,y);
#endif
if (!isdatarec(x)) {
if (isdatarec(y)) return 0;
// neither x nor y are datarecs
// need to check pointed values
if (wr_equal_ptr_primitives(g,x,y,uniquestrflag)) return 1;
else return 0;
} else {
// both x and y are datarecs
db=g->db;
xptr=decode_record(db,x);
yptr=decode_record(db,y);
xlen=get_record_len(xptr);
ylen=get_record_len(yptr);
if (g->unify_samelen) {
if (xlen!=ylen) return 0;
uselen=xlen;
} else {
if (xlen<=ylen) uselen=xlen;
else uselen=ylen;
}
if (g->unify_maxuseterms) {
if (((g->unify_maxuseterms)+(g->unify_firstuseterm))<uselen)
uselen=(g->unify_firstuseterm)+(g->unify_maxuseterms);
}
ilimit=RECORD_HEADER_GINTS+uselen;
for(i=RECORD_HEADER_GINTS+(g->unify_firstuseterm); i<ilimit; i++) {
encx=*(xptr+i);
ency=*(yptr+i);
if (!WR_EQUAL_TERM(g,encx,ency,uniquestrflag)) return 0;
}
return 1;
}
}
int wr_equal_ptr_primitives(glb* g, gint a, gint b, int uniquestrflag) {
gint t1,t2;
gint l1,l2,ol;
char* s1;
char* s2;
//printf("equal_ptr_primitives called with %d and %d\n",a,b);
switch(a&NORMALPTRMASK) { // last 3 bits
case FULLINTBITSV0:
case FULLINTBITSV1:
if (isfullint(b) &&
(dbfetch(g->db,decode_fullint_offset(a))==dbfetch(g->db,decode_fullint_offset(b))) )
return 1;
else
return 0;
case FULLDOUBLEBITS:
if (isfulldouble(b) &&
wg_decode_double(g->db,a)==wg_decode_double(g->db,b) )
return 1;
else
return 0;
case SHORTSTRBITS:
//printf("shortstrbits \n");
if (isshortstr(b) &&
!memcmp((void*)(offsettoptr(g->db,decode_shortstr_offset(a))),
(void*)(offsettoptr(g->db,decode_shortstr_offset(b))),
SHORTSTR_SIZE))
return 1;
else
return 0;
case LONGSTRBITS:
if (uniquestrflag) {
if (a==b)
return 1;
else
return 0;
} else {
t1=wg_get_encoded_type(g->db,a);
t2=wg_get_encoded_type(g->db,b);
if (t1!=t2) return 0;
l1=wg_decode_unistr_lang_len(g->db,a,t1);
l2=wg_decode_unistr_lang_len(g->db,b,t2);
if (11!=l2) return 0;
ol=l1;
l1=wg_decode_unistr_len(g->db,a,t1);
l2=wg_decode_unistr_len(g->db,b,t2);
if (11!=l2) return 0;
s1=wg_decode_unistr_lang(g->db,a,t1);
s2=wg_decode_unistr_lang(g->db,b,t2);
if (s1!=s2 && (s1==NULL || s2==NULL || memcmp(s1,s2,ol))) return 0;
s1=wg_decode_unistr(g->db,a,t1);
s2=wg_decode_unistr(g->db,b,t2);
if (s1!=s2 && (s1==NULL || s2==NULL || memcmp(s1,s2,l1))) return 0;
return 1;
}
}
return 0;
}
/* ------------------------------------------------------
variable handling
------------------------------------------------------
*/
/**
x must be a variable
*/
gint wr_varval(gint x, gptr vb) {
gint y;
// do the first test without loop
y=vb[decode_var(x)];
if (y==UNASSIGNED) {
return x;
} else if (!isvar(y)) {
return y;
} else {
// if variable is assigned to a variable, loop
for (;;) {
x=y;
y=vb[decode_var(x)];
if (y==UNASSIGNED) {
return x;
} else if (!isvar(y)) {
return y;
}
}
}
}
/**
x must be a variable
*/
void setvar(gint x, gint y, gptr vb, gptr vstk, gint* vc) {
vb[decode_var(x)]=y;
vstk[*vc]=(gint)(vb+decode_var(x)); // pointer arithmetic (gint ptr)
(*vc)++;
}
/**
clear single varstack
varstack structure:
0: vector len
1: next free pos on stack (2 for empty stack)
2...N: pointer of the varbank cell corresponding to set var value
*/
void wr_clear_varstack(glb* g,vec vs) {
gptr x;
gptr maxpt;
gint maxnr;
x=vs;
++x;
maxnr= *x;
if (maxnr>2) {
maxpt=vs+maxnr;
for(++x; x<maxpt; ++x) {
*((gptr)(*x))=UNASSIGNED;
}
}
vs[1]=2;
}
/** used in subsumption for clearing a part of varstack
*/
void wr_clear_varstack_topslice(glb* g, vec vs, int y) {
gptr x;
gptr maxpt;
gint maxnr;
x=vs;
++x;
maxnr= *x;
if (maxnr>2) {
maxpt=vs+maxnr;
for(x=vs+y; x<maxpt; ++x) {
*((gptr)(*x))=UNASSIGNED;
}
}
vs[1]=y;
}
void wr_clear_all_varbanks(glb* g) {
gptr x;
gptr xmax;
gint len;
x=(gptr)(g->varbanks);
len=NROF_VARBANKS*NROF_VARSINBANK;
//len=x[0];
xmax=x+len;
//for(x++; x<xmax; x++) *x=GBUNASSIGNEDVAL;
for(;x<xmax; x++) *x=UNASSIGNED;
}
/*---------------------------------------------
debug printing/checking vardata
---------------------------------------------- */
void wr_print_vardata(glb* g){
wr_print_varbank(g,g->varbanks);
wr_print_varstack(g,g->varstack);
}
void wr_print_varbank(glb* g,gptr vb){
int i;
int start, end;
gint cell;
start=0;
end=NROF_VARBANKS*NROF_VARSINBANK;
printf("varbank %d:\n",(gint)vb);
for(i=start;i<end;i++) {
cell=vb[i];
if (cell!=UNASSIGNED) {
printf("%d raw %d: ",i,cell);
wr_print_term(g,cell);
printf("\n");
}
}
}
void wr_print_varstack(glb* g, gptr vs) {
int i;
int start, end;
gint cell;
start=2;
end=vs[1];
printf("varstack len %d firstfree %d:\n",vs[0],vs[1]);
for(i=start;i<end;i++) {
cell=vs[i];
if (cell!=UNASSIGNED) {
printf("%d raw %d: ",i,cell);
//wr_print_term(g,cell);
printf("\n");
}
}
}
int wr_varbanks_are_clear(glb* g, gptr vb) {
int i;
int start, end;
gint cell;
start=0;
end=NROF_VARBANKS*NROF_VARSINBANK;
//printf("varbank %d:\n",(gint)vb);
for(i=start;i<end;i++) {
cell=vb[i];
if (cell!=UNASSIGNED) {
//printf("%d raw %d: ",i,cell);
//wr_print_term(g,cell);
//printf("\n");
return 0;
}
}
return 1;
}
#ifdef __cplusplus
}
#endif
|