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
|
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * SUBSTITUTION * */
/* * * */
/* * $Module: SUBSTITUTION * */
/* * * */
/* * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 * */
/* * MPI fuer Informatik * */
/* * * */
/* * This program is free software; you can redistribute * */
/* * it and/or modify it under the terms of the FreeBSD * */
/* * Licence. * */
/* * * */
/* * This program 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 LICENCE file * */
/* * for more details. * */
/* * * */
/* * * */
/* $Revision: 1.3 $ * */
/* $State: Exp $ * */
/* $Date: 2010-02-22 14:09:59 $ * */
/* $Author: weidenb $ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: spass@mpi-inf.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
/* $RCSfile: subst.c,v $ */
#include "subst.h"
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * SUBSTITUTION CREATION AND DELETION * */
/* * * */
/* ********************************************************** */
/**************************************************************/
SUBST subst_Add(SYMBOL Symbol, TERM Codomain, SUBST Subst)
{
SUBST Result;
Result = subst_Get();
Result->next = Subst;
Result->dom = Symbol;
Result->codomain = Codomain;
return Result;
}
void subst_Delete(SUBST Subst)
{
SUBST Next;
while (subst_Exist(Subst)) {
Next = subst_Next(Subst);
if (subst_Cod(Subst))
term_Delete(subst_Cod(Subst));
subst_FreeOneNode(Subst);
Subst = Next;
}
}
void subst_Free(SUBST Subst)
{
SUBST Next;
while (subst_Exist(Subst)) {
Next = subst_Next(Subst);
subst_FreeOneNode(Subst);
Subst = Next;
}
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * FUNCTIONS ON SUBSTITUTIONS * */
/* * * */
/* ********************************************************** */
/**************************************************************/
TERM subst_Term(SYMBOL Symbol, SUBST Subst)
{
for (; subst_Exist(Subst); Subst = subst_Next(Subst))
if (symbol_Equal(Symbol,subst_Dom(Subst)))
return subst_Cod(Subst);
return (TERM)NULL;
}
static TERM subst_ApplyIntern(SUBST Subst, TERM Term)
{
TERM RplacTerm;
LIST Arglist;
SYMBOL Top;
Top = term_TopSymbol(Term);
if (symbol_IsVariable(Top) && (RplacTerm = subst_Term(Top,Subst))) {
Arglist = term_CopyTermList(term_ArgumentList(RplacTerm));
term_RplacTop(Term, term_TopSymbol(RplacTerm));
term_DeleteTermList(term_ArgumentList(Term));
term_RplacArgumentList(Term, Arglist);
} else {
for (Arglist = term_ArgumentList(Term);
!list_Empty(Arglist);
Arglist = list_Cdr(Arglist))
subst_ApplyIntern(Subst, list_Car(Arglist));
}
return Term;
}
TERM subst_Apply(SUBST Subst, TERM Term)
{
if (subst_Empty(Subst))
return Term;
return subst_ApplyIntern(Subst, Term);
}
SUBST subst_Merge(SUBST Source, SUBST Drain)
{
SUBST Scan;
BOOL Changed;
for (; subst_Exist(Source); Source = subst_Next(Source)) {
/* Apply current assignment of Source to all */
/* assignments of Drain. If the current ass. */
/* cannot be applied to any codomain in Drain */
/* the current assignment is added to Drain. */
Changed = FALSE;
for (Scan = Drain;
subst_Exist(Scan);
Scan = subst_Next(Scan))
if (term_SubstituteVariable(Source->dom,
Source->codomain,
Scan->codomain))
Changed = TRUE;
if (!Changed)
Drain = subst_Add(Source->dom,
term_Copy(Source->codomain),
Drain);
}
return Drain;
}
SUBST subst_Compose(SUBST Outer, SUBST Inner)
/**************************************************************
INPUT: Two substitutions.
RETURNS: The substitution corresponding to the composition of
<Outer> and <Inner>.
EFFECT: <Outer> is destructively applied to the codomain of <Inner>
<Inner> is destructively extended
***************************************************************/
{
SUBST Scan1,Scan2,New;
New = subst_Nil();
for (Scan1=Outer; subst_Exist(Scan1); Scan1 = subst_Next(Scan1)) {
for (Scan2 = Inner;subst_Exist(Scan2);Scan2 = subst_Next(Scan2))
term_SubstituteVariable(subst_Dom(Scan1),subst_Cod(Scan1),Scan2->codomain);
if (!subst_BindVar(subst_Dom(Scan1),Inner))
New = subst_Add(subst_Dom(Scan1), term_Copy(subst_Cod(Scan1)),New);
}
return subst_NUnion(Inner,New);
}
BOOL subst_BindVar(SYMBOL Var, SUBST Subst)
/**************************************************************
INPUT: A variable symbol and a substitution.
RETURNS: TRUE iff <Var> is contained in the domain of <Subst>
***************************************************************/
{
SUBST Scan;
for (Scan=Subst; subst_Exist(Scan); Scan = subst_Next(Scan))
if (symbol_Equal(subst_Dom(Scan),Var))
return TRUE;
return FALSE;
}
SUBST subst_Copy(SUBST Subst)
{
SUBST Copy, Result;
for (Result = subst_Nil(),
Copy = subst_Nil();
subst_Exist(Subst);
Subst = subst_Next(Subst))
if (subst_Exist(Result)) {
subst_SetNext(Copy, subst_Add(subst_Dom(Subst),
term_Copy(subst_Cod(Subst)),
subst_Nil()));
Copy = subst_Next(Copy);
} else {
Result = subst_Add(subst_Dom(Subst),
term_Copy(subst_Cod(Subst)),
subst_Nil());
Copy = Result;
}
return Result;
}
BOOL subst_MatchTops(const CONTEXT Context, SUBST Subst)
{
for ( ; subst_Exist(Subst); Subst = subst_Next(Subst))
if (cont_ContextBindingTerm(Context, subst_Dom(Subst)) &&
term_EqualTopSymbols(cont_ContextBindingTerm(Context, subst_Dom(Subst)),
subst_Cod(Subst)))
return TRUE;
return FALSE;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * UNIFICATION TEST * */
/* * * */
/* ********************************************************** */
/**************************************************************/
BOOL subst_Unify(CONTEXT IndexContext, SUBST Subst)
/*********************************************************
INPUT:
RETURNS:
CAUTION: 'Subst' IS ASSUMED TO BE NON-EMPTY.
**********************************************************/
{
while (subst_Exist(Subst)) {
if (!cont_VarIsBound(IndexContext, subst_Dom(Subst))) {
if (unify_OccurCheck(IndexContext, subst_Dom(Subst), IndexContext, subst_Cod(Subst)))
return FALSE;
else
cont_CreateBinding(IndexContext, subst_Dom(Subst), IndexContext, subst_Cod(Subst));
} else if (!unify_UnifyAllOC(IndexContext,
IndexContext,
subst_Cod(Subst),
cont_ContextBindingContext(IndexContext,
subst_Dom(Subst)),
cont_ContextBindingTerm(IndexContext,
subst_Dom(Subst))))
return FALSE;
Subst = subst_Next(Subst);
}
return TRUE;
}
BOOL subst_IsShallow(SUBST Subst) {
/**********************************************************
INPUT: A unifier
RETURNS: TRUE, if the unifier is valid :
a variable or a ground term or a function with only
variables or ground terms as arguments.
***********************************************************/
SUBST SubstScan;
for (SubstScan = Subst; SubstScan != subst_Nil();
SubstScan = subst_Next(SubstScan)) {
TERM Codomain = subst_Cod(SubstScan);
if ((!term_IsVariable(Codomain))
&& (!term_IsGround(Codomain))) {
LIST Scan ;
for (Scan = term_ArgumentList(Codomain); Scan != list_Nil();
Scan = list_Cdr(Scan)) {
if ((!term_IsVariable(list_Car(Scan))
&& (!term_IsGround(list_Car(Scan)))))
return FALSE;
}
}
}
return TRUE;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * GENERALIZATION TEST * */
/* * * */
/* ********************************************************** */
/**************************************************************/
BOOL subst_Match(const CONTEXT Context, SUBST Subst)
/*********************************************************
INPUT:
RETURNS:
CAUTION: 'Subst' IS ASSUMED TO BE NON-EMPTY.
**********************************************************/
{
while (subst_Exist(Subst)) {
if (!cont_VarIsBound(Context, subst_Dom(Subst)) ||
!unify_Match(Context,
subst_Cod(Subst),
cont_ContextBindingTerm(Context, subst_Dom(Subst))))
return FALSE;
Subst = subst_Next(Subst);
}
return TRUE;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * INSTANCE TEST * */
/* * * */
/* ********************************************************** */
/**************************************************************/
BOOL subst_MatchReverse(const CONTEXT IndexContext, SUBST Subst)
/*********************************************************
INPUT:
RETURNS:
CAUTION: 'Subst' IS ASSUMED TO BE NON-EMPTY.
**********************************************************/
{
while (subst_Exist(Subst)) {
if (!cont_VarIsBound(IndexContext, subst_Dom(Subst))) {
if (symbol_IsIndexVariable(subst_Dom(Subst)))
cont_CreateBinding(IndexContext,
subst_Dom(Subst),
cont_InstanceContext(),
subst_Cod(Subst));
else
return FALSE;
}
else if (!unify_MatchReverse(IndexContext,
subst_Cod(Subst),
cont_ContextBindingContext(IndexContext, subst_Dom(Subst)),
cont_ContextBindingTerm(IndexContext, subst_Dom(Subst))))
return FALSE;
Subst = subst_Next(Subst);
}
return TRUE;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * VARIATION TEST * */
/* * * */
/* ********************************************************** */
/**************************************************************/
BOOL subst_Variation(const CONTEXT Context, SUBST Subst)
/*********************************************************
INPUT:
RETURNS:
CAUTION: 'Subst' IS ASSUMED TO BE NON-EMPTY.
**********************************************************/
{
while (subst_Exist(Subst)) {
if (!cont_VarIsBound(Context, subst_Dom(Subst)) ||
!unify_Variation(Context,
subst_Cod(Subst),
cont_ContextBindingTerm(Context, subst_Dom(Subst))))
return FALSE;
Subst = subst_Next(Subst);
}
return TRUE;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * COMMON GENERALIZATIONS * */
/* * * */
/* ********************************************************** */
/**************************************************************/
SUBST subst_ComGen(const CONTEXT Context, SUBST Subst, SUBST* SubstOld,
SUBST* SubstNew)
/*********************************************************
INPUT:
RETURNS:
CAUTION: 'Subst' IS ASSUMED TO BE NON-EMPTY.
**********************************************************/
{
SUBST Result;
Result = *SubstOld = *SubstNew = NULL;
do {
if (!cont_VarIsBound(Context, subst_Dom(Subst)))
*SubstOld=subst_Add(subst_Dom(Subst), term_Copy(subst_Cod(Subst)), *SubstOld);
else if (term_Equal(cont_ContextBindingTerm(Context, subst_Dom(Subst)),
subst_Cod(Subst)))
Result = subst_Add(subst_Dom(Subst), term_Copy(subst_Cod(Subst)), Result);
else
if (!symbol_Equal(term_TopSymbol(cont_ContextBindingTerm(Context,
subst_Dom(Subst))),
term_TopSymbol(subst_Cod(Subst)))) {
*SubstOld=subst_Add(subst_Dom(Subst),
term_Copy(subst_Cod(Subst)),
*SubstOld);
*SubstNew=subst_Add(subst_Dom(Subst),
term_Copy(cont_ContextBindingTerm(Context,
subst_Dom(Subst))),
*SubstNew);
} else
Result = subst_Add(subst_Dom(Subst),
unify_ComGenLinear(Context,
SubstNew,
cont_ContextBindingTerm(Context,
subst_Dom(Subst)),
SubstOld,
subst_Cod(Subst)),
Result);
cont_CloseBinding(Context, subst_Dom(Subst));
Subst = subst_Next(Subst);
} while (subst_Exist(Subst));
return Result;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * CLOSE BINDINGS * */
/* * * */
/* ********************************************************** */
/**************************************************************/
void subst_CloseVariables(const CONTEXT Context, SUBST Subst)
{
for (; subst_Exist(Subst); Subst = subst_Next(Subst))
cont_CloseBinding(Context, subst_Dom(Subst));
}
SUBST subst_CloseOpenVariables(SUBST Result)
{
while (cont_LastBinding()) {
if (cont_LastIsBound())
Result = subst_Add(cont_LastBindingSymbol(),
term_Copy(cont_LastBindingTerm()),
Result);
cont_BackTrackLastBinding();
}
return Result;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * EXTRACT UNIFIER * */
/* * * */
/* ********************************************************** */
/**************************************************************/
void subst_ExtractUnifier(const CONTEXT CL,
SUBST* LeftSubst,
const CONTEXT CR,
SUBST* RightSubst)
/*********************************************************
INPUT: 'LeftSubst', 'RightSubst' for the unifier,
renaming the codomain variables starts at
'MinimumCoVariable' excl., number of
renamings are ADDED to 'Bindings'.
RETURNS: Nothing.
SUMMARY: Extracts the unifier into two substitutions
with renamed variables in the codomain.
CAUTION: DOES NOT RESET THE BINDINGS, CREATES EVEN
MORE BINDINGS BECAUSE OF RENAMING.
**********************************************************/
{
CONTEXT Scan;
*LeftSubst = subst_Nil();
*RightSubst = subst_Nil();
Scan = cont_LastBinding();
while (Scan) {
if (cont_IsInContext(CL,
cont_BindingSymbol(Scan),
Scan))
*LeftSubst = subst_Add(cont_BindingSymbol(Scan),
cont_CopyAndApplyBindings(cont_BindingContext(Scan),
cont_BindingTerm(Scan)),
*LeftSubst);
else if (cont_IsInContext(CR,
cont_BindingSymbol(Scan),
Scan))
*RightSubst = subst_Add(cont_BindingSymbol(Scan),
cont_CopyAndApplyBindings(cont_BindingContext(Scan),
cont_BindingTerm(Scan)),
*RightSubst);
Scan = cont_BindingLink(Scan);
}
}
void subst_ExtractUnifierCom(const CONTEXT Context, SUBST* Subst)
/*********************************************************
INPUT: 'LeftSubst', 'RightSubst' for the unifier,
renaming the codomain variables starts at
'MinimumCoVariable' excl., number of
renamings are ADDED to 'Bindings'.
RETURNS: Nothing.
SUMMARY: Extracts the unifier into two substitutions
with renamed variables in the codomain.
CAUTION: DOES NOT RESET THE BINDINGS, CREATES EVEN
MORE BINDINGS BECAUSE OF RENAMING.
**********************************************************/
{
CONTEXT Scan;
*Subst = subst_Nil();
Scan = cont_LastBinding();
while (Scan) {
*Subst =
subst_Add(cont_BindingSymbol(Scan),
cont_CopyAndApplyBindingsCom(Context, cont_BindingTerm(Scan)),
*Subst);
Scan = cont_BindingLink(Scan);
}
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * EXTRACT MATCHER * */
/* * * */
/* ********************************************************** */
/**************************************************************/
SUBST subst_ExtractMatcher(void)
/*********************************************************
INPUT: None.
RETURNS: The matcher.
SUMMARY: Extracts the matcher without renaming.
CAUTION: DOES NOT RESET THE BINDINGS, DOES NOT COPY
THE CODOMAINS.
**********************************************************/
{
CONTEXT Scan;
SUBST Result;
for (Scan = cont_LastBinding(), Result = subst_Nil();
Scan;
Scan = cont_BindingLink(Scan))
Result = subst_Add(cont_BindingSymbol(Scan),
cont_BindingTerm(Scan),
Result);
return Result;
}
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * OUTPUT * */
/* * * */
/* ********************************************************** */
/**************************************************************/
void subst_Print(SUBST Subst)
{
fputs("{ ", stdout);
for (; subst_Exist(Subst); Subst = subst_Next(Subst)) {
symbol_Print(subst_Dom(Subst));
if (subst_Cod(Subst)) {
fputs(" -> ", stdout);
term_PrintPrefix(subst_Cod(Subst));
}
if (subst_Next(Subst))
fputs("; ", stdout);
}
fputs(" }", stdout);
}
|