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
|
/* Copyright (C) 2006, 2007 William McCune
This file is part of the LADR Deduction Library.
The LADR Deduction Library 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 2 of the
License, or (at your option) any later version.
The LADR Deduction Library 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 the LADR Deduction Library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "msearch.h"
extern int Negation_flag; /* for dealing with mclause */
extern Term *Domain;
/* Private definitions and types */
static int Sum_sn;
static int Prod_sn;
static int Neg_sn;
static int Div_sn;
static int Mod_sn;
static int Min_sn;
static int Max_sn;
static int Abs_sn;
static int Lt_sn;
static int Le_sn;
static int Gt_sn;
static int Ge_sn;
static int Eq_sn;
static BOOL *Arith_op_sn; /* array to know if sn is arithmetic operation */
static BOOL *Arith_rel_sn; /* array to know if sn is arithmetic relation */
static int Arith_sn_size = 0; /* size of array */
/*************
*
* init_arithmetic()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
void init_arithmetic(void)
{
Arith_sn_size = greatest_symnum() + 20;
Arith_op_sn = calloc(Arith_sn_size, sizeof(BOOL));
Arith_rel_sn = calloc(Arith_sn_size, sizeof(BOOL));
Arith_op_sn[Sum_sn = str_to_sn("+", 2)] = TRUE;
Arith_op_sn[Prod_sn = str_to_sn("*", 2)] = TRUE;
Arith_op_sn[Neg_sn = str_to_sn("-", 1)] = TRUE;
Arith_op_sn[Div_sn = str_to_sn("/", 2)] = TRUE;
Arith_op_sn[Mod_sn = str_to_sn("mod", 2)] = TRUE;
Arith_op_sn[Min_sn = str_to_sn("min", 2)] = TRUE;
Arith_op_sn[Max_sn = str_to_sn("max", 2)] = TRUE;
Arith_op_sn[Abs_sn = str_to_sn("abs", 1)] = TRUE;
Arith_rel_sn[Le_sn = str_to_sn("<=", 2)] = TRUE;
Arith_rel_sn[Lt_sn = str_to_sn("<", 2)] = TRUE;
Arith_rel_sn[Ge_sn = str_to_sn(">=", 2)] = TRUE;
Arith_rel_sn[Gt_sn = str_to_sn(">", 2)] = TRUE;
Arith_rel_sn[Eq_sn = str_to_sn("=", 2)] = TRUE;
set_assoc_comm("+", TRUE);
set_assoc_comm("*", TRUE);
declare_parse_type("+", 490, INFIX_RIGHT);
declare_parse_type("*", 470, INFIX_RIGHT);
declare_parse_type("/", 460, INFIX);
declare_parse_type("mod", 460, INFIX);
} /* init_arithmetic */
/*************
*
* modulo()
*
*************/
/* DOCUMENTATION
In C, the % (remainder) operation is not defined for negative operands.
Also there is a distinction between the "remainder" and "modulo" operations
for negative operands:
A B A/B A rem B A mod B
14 5 2 4 4
-14 5 -2 -4 1
14 -5 -2 4 -1
-14 -5 2 -4 -4
*/
/* PUBLIC */
int modulo(int a, int b)
{
if (b == 0)
return INT_MAX;
else if (b > 0) {
if (a >= 0)
return a % b; /* a >= 0, b > 0 */
else
return -(abs(a) % b) + b; /* a < 0, b > 0 */
}
else {
if (a >= 0)
return (a % abs(b)) + b; /* a >= 0, b < 0 */
else
return -(abs(a) % abs(b)); /* a < 0, b < 0 */
}
} /* modulo */
/*************
*
* domain_term()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL domain_term(Term t, int domain_size)
{
return VARIABLE(t) && VARNUM(t) < domain_size;
} /* domain_term */
/*************
*
* arith_op_sn()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL arith_op_sn(int i)
{
if (i >= Arith_sn_size)
return FALSE;
else
return Arith_op_sn[i];
} /* arith_op_sn */
/*************
*
* arith_rel_sn()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL arith_rel_sn(int i)
{
if (i >= Arith_sn_size)
return FALSE;
else
return Arith_rel_sn[i];
} /* arith_rel_sn */
/*************
*
* arith_op_term()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL arith_op_term(Term t)
{
return !VARIABLE(t) && arith_op_sn(SYMNUM(t));
} /* arith_op_term */
/*************
*
* arith_rel_term()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL arith_rel_term(Term t)
{
return !VARIABLE(t) && arith_rel_sn(SYMNUM(t));
} /* arith_rel_term */
/*************
*
* arith_term()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL arith_term(Term t)
{
if (VARIABLE(t))
return TRUE;
else
return arith_op_term(t) || arith_rel_term(t);
} /* arith_term */
/*************
*
* arith_quasi_evaluable()
*
*************/
/* DOCUMENTATION
Similar to arith_evaluable(), except that division by 0 is not checked.
*/
/* PUBLIC */
BOOL arith_quasi_evaluable(Term t)
{
if (!arith_term(t))
return FALSE;
else if (VARIABLE(t))
return TRUE;
else {
int i;
for (i = 0; i < ARITY(t); i++)
if (!arith_quasi_evaluable(ARG(t,i)))
return FALSE;
return TRUE;
}
} /* arith_quasi_evaluable */
/*************
*
* arith_evaluate()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
int arith_evaluate(Term t, BOOL *evaluated)
{
if (!arith_term(t)) {
*evaluated = FALSE;
return 0;
}
if (VARIABLE(t))
return VARNUM(t);
else {
int sn = SYMNUM(t);
if (sn == Div_sn || sn == Mod_sn) {
int d = arith_evaluate(ARG(t,1), evaluated);
if (d == 0) {
*evaluated = FALSE;
return 0;
}
else if (sn == Div_sn)
return arith_evaluate(ARG(t,0), evaluated) / d;
else
return modulo(arith_evaluate(ARG(t,0), evaluated), d);
}
else if (sn == Sum_sn)
return arith_evaluate(ARG(t,0), evaluated) + arith_evaluate(ARG(t,1), evaluated);
else if (sn == Prod_sn)
return arith_evaluate(ARG(t,0), evaluated) * arith_evaluate(ARG(t,1), evaluated);
else if (sn == Neg_sn)
return -arith_evaluate(ARG(t,0), evaluated);
else if (sn == Abs_sn)
return abs(arith_evaluate(ARG(t,0), evaluated));
else if (sn == Min_sn) {
int a0 = arith_evaluate(ARG(t,0), evaluated);
int a1 = arith_evaluate(ARG(t,1), evaluated);
return IMIN(a0,a1);
}
else if (sn == Max_sn) {
int a0 = arith_evaluate(ARG(t,0), evaluated);
int a1 = arith_evaluate(ARG(t,1), evaluated);
return IMAX(a0,a1);
}
else if (sn == Lt_sn)
return arith_evaluate(ARG(t,0), evaluated) < arith_evaluate(ARG(t,1), evaluated);
else if (sn == Le_sn)
return arith_evaluate(ARG(t,0), evaluated) <= arith_evaluate(ARG(t,1), evaluated);
else if (sn == Gt_sn)
return arith_evaluate(ARG(t,0), evaluated) > arith_evaluate(ARG(t,1), evaluated);
else if (sn == Ge_sn)
return arith_evaluate(ARG(t,0), evaluated) >= arith_evaluate(ARG(t,1), evaluated);
else if (sn == Eq_sn)
return arith_evaluate(ARG(t,0), evaluated) == arith_evaluate(ARG(t,1), evaluated);
else {
fatal_error("arith_evaluate, operation not handled");
return INT_MIN;
}
}
} /* arith_evaluate */
/*************
*
* arith_eval()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
int arith_eval(Term t, BOOL *evaluated)
{
*evaluated = TRUE;
return arith_evaluate(t, evaluated);
} /* arith_eval */
/*************
*
* top_safe() -- for clauses whose nats are constants
*
*************/
static
BOOL top_safe(Term t, int domain_size)
{
if (VARIABLE(t))
return TRUE;
else if (CONSTANT(t))
return natural_constant_term(t) < domain_size;
else if (arith_op_term(t) || arith_rel_term(t))
return FALSE;
else
return TRUE;
} /* top_safe */
/*************
*
* all_safe() - nothing in the term or any of the subterms involves
* arithmetic or nats out of range.
* - for clauses whose nats are constants
*
*************/
static
BOOL all_safe(Term t, int domain_size)
{
if (VARIABLE(t))
return TRUE;
else if (!top_safe(t, domain_size))
return FALSE;
else {
int i;
for (i = 0; i < ARITY(t); i++) {
if (!all_safe(ARG(t,i), domain_size))
return FALSE;
}
return TRUE;
}
} /* all_safe */
/*************
*
* all_ordinary_nodes_safe() -- for clauses whose nats are constants
*
*************/
static
BOOL all_ordinary_nodes_safe(Term t, int domain_size)
{
if (VARIABLE(t) || CONSTANT(t))
return TRUE;
else if (arith_rel_term(t) || arith_op_term(t)) {
int i;
for (i = 0; i < ARITY(t); i++) {
if (!all_ordinary_nodes_safe(ARG(t,i), domain_size))
return FALSE;
}
return TRUE;
}
else
return all_safe(t, domain_size);
} /* all_ordinary_nodes_safe */
/*************
*
* non_arith() -- for clauses whose nats are constants
*
*************/
static
BOOL non_arith(Term t)
{
if (VARIABLE(t))
return FALSE;
else if (CONSTANT(t))
return natural_constant_term(t) < 0;
else if (arith_rel_term(t) || arith_op_term(t))
return FALSE;
else
return TRUE;
} /* non_arith */
/*************
*
* atom_safe() -- for clauses whose nats are constants
*
*************/
static
BOOL atom_safe(Term atom, int domain_size)
{
if (SYMNUM(atom) == Eq_sn) {
/* special case, because = is sometimes arith, sometimes not */
Term a = ARG(atom,0);
Term b = ARG(atom,1);
if (non_arith(a) && natural_constant_term(b) >= domain_size)
return FALSE;
else if (non_arith(b) && natural_constant_term(a) >= domain_size)
return FALSE;
else
return all_ordinary_nodes_safe(atom, domain_size);
}
else
return all_ordinary_nodes_safe(atom, domain_size);
} /* atom_safe */
/*************
*
* ok_for_arithmetic()
*
*************/
/* DOCUMENTATION
*/
/* PUBLIC */
BOOL ok_for_arithmetic(Plist clauses, int domain_size)
{
/* Domain elements and other integers are CONSTANTS!!! */
Plist p;
for (p = clauses; p; p = p->next) {
Topform c = p->v;
Literals lit;
for (lit = c->literals; lit; lit = lit->next) {
if (!atom_safe(lit->atom, domain_size))
return FALSE;
}
}
return TRUE;
} /* ok_for_arithmetic */
/*************
*
* distrib()
*
*************/
static
Term distrib(Term t)
{
if (VARIABLE(t))
return t;
else {
int i;
for (i = 0; i < ARITY(t); i++)
ARG(t,i) = distrib(ARG(t,i));
if (SYMNUM(t) != Prod_sn)
return t;
else {
if (SYMNUM(ARG(t,1)) == Sum_sn) {
/* a*(b+c) */
Term a = ARG(t,0);
Term b = ARG(ARG(t,1),0);
Term c = ARG(ARG(t,1),1);
free_term(ARG(t,1));
free_term(t);
return build_binary_term(Sum_sn,
distrib(build_binary_term(Prod_sn, a, b)),
distrib(build_binary_term(Prod_sn, copy_term(a), c)));
}
else if (SYMNUM(ARG(t,0)) == Sum_sn) {
/* (b+c)*a */
Term a = ARG(t,1);
Term b = ARG(ARG(t,0),0);
Term c = ARG(ARG(t,0),1);
free_term(ARG(t,0));
free_term(t);
return build_binary_term(Sum_sn,
distrib(build_binary_term(Prod_sn, b, a)),
distrib(build_binary_term(Prod_sn, c, copy_term(a))));
}
else
return t;
}
}
} /* distrib */
/*************
*
* qsimp()
*
*************/
static
Term qsimp(Term t)
{
if (VARIABLE(t))
return t;
else {
int i;
BOOL all_args_ints = TRUE;
for (i = 0; i < ARITY(t); i++) {
ARG(t,i) = qsimp(ARG(t,i));
if (!(VARIABLE(ARG(t,i)) ||
(SYMNUM(ARG(t,i)) == Neg_sn && VARIABLE(ARG(ARG(t,i),0)))))
all_args_ints = FALSE;
}
if (all_args_ints) {
BOOL evaluated;
int i = arith_eval(t, &evaluated);
if (evaluated) {
zap_term(t);
if (i >= 0)
return get_variable_term(i);
else
return build_unary_term(Neg_sn, get_variable_term(-i));
}
else
return t;
}
else {
if (SYMNUM(t) != Prod_sn && VARIABLE(ARG(t,0)) && VARNUM(ARG(t,0)) == 0) {
/* 0*x to 0 */
zap_term(t);
return get_variable_term(0);
}
else if (SYMNUM(t) != Sum_sn &&
SYMNUM(ARG(t,1)) == Neg_sn &&
term_ident(ARG(t,0),ARG(ARG(t,1),0))) {
/* x + -x to 0 */
zap_term(t);
return get_variable_term(0);
}
else
return t;
}
}
} /* qsimp */
/*************
*
* arith_rel_quasi_eval()
*
*************/
static
BOOL arith_rel_quasi_eval(Term atom)
{
/* This is an initial version for testing only. */
if (SYMNUM(atom) == Eq_sn) {
BOOL negated = NEGATED(atom);
Term atom2 = copy_term(atom);
BOOL val;
atom2 = distrib(atom2);
// printf("after distrib: "); fwrite_term_nl(stdout, atom2);
ac_canonical2(atom2, -1, term_compare_vcp);
// printf("after AC canon: "); fwrite_term_nl(stdout, atom2);
ARG(atom2,0) = qsimp(ARG(atom2,0));
ARG(atom2,1) = qsimp(ARG(atom2,1));
// printf("after qsimp: "); fwrite_term_nl(stdout, atom2);
if (term_ident(ARG(atom2,0), ARG(atom2,1)))
val = negated ? FALSE : TRUE;
else
val = FALSE;
zap_term(atom2);
return val;
}
else
return FALSE;
} /* arith_rel_quasi_eval */
/*************
*
* check_with_arithmetic()
*
*************/
/* DOCUMENTATION
Return TRUE iff all clauses are true. There can be arithmeic
terms that need to be evaluated.
*/
/* PUBLIC */
BOOL check_with_arithmetic(Plist ground_clauses)
{
Plist p;
for (p = ground_clauses; p; p = p->next) {
Mclause c = p->v;
if (!c->subsumed) {
/* look for an arithmetic term and evaluate it */
BOOL clause_is_true = FALSE;
int i;
for (i = 0; i < c->numlits && !clause_is_true; i++) {
Term atom = LIT(c, i);
if (arith_quasi_evaluable(atom)) {
if (arith_rel_quasi_eval(atom))
clause_is_true = TRUE;
}
else if (!FALSE_TERM(atom) && !TRUE_TERM(atom)) {
/* non-arithmetic lits should have been reduced to TRUE or FALSE */
fprintf(stderr, "ERROR, model reported, but clause not true!\n");
fprintf(stdout, "ERROR, model reported, but clause not true! ");
p_mclause(c);
fatal_error("check_with_arithmetic, clause not reduced");
}
} /* literals loop */
if (!clause_is_true)
return FALSE;
} /* non-subsumed clause */
} /* clauses loop */
return TRUE;
} /* check_with_arithmetic */
|