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 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
|
/*-----------------------------------------------------------------------
File : cte_typebanks.c
Author: Petar Vukmirovic
Contents
This module implements type sharing invariant:
-Each two types that are structurally the same,
should be the same object in memory.
Sharing is inspired by term sharing (hashing + splay trees).
Copyright 1998, 1999 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Created: Tue Feb 24 01:23:24 MET 1998 - Ripped out of the now obsolete
cte_terms.h
-----------------------------------------------------------------------*/
#include "cte_typebanks.h"
#include "cte_functypes.h"
#include <clb_verbose.h>
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
#define REALLOC_STEP 16
#define GetNameId(node) ((node)->val1.i_val)
#define GetArity(node) ((node)->val2.i_val)
#define type_a0hash(t) ((t)->f_code)
#define type_a1hash(t) (type_a0hash(t)^(((intptr_t)(t)->args[0])>>3))
#define type_aritynhash(t) (type_a1hash(t)^(((intptr_t)(t)->args[1])>>4))
#define hash_type(type)\
((((type)->arity == 0)?\
type_a0hash(type):\
(((type)->arity == 1)?type_a1hash(type):type_aritynhash(type)))\
&(TYPEBANK_HASH_MASK))
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
typedef struct back_idx_info
{
const char* name;
int arity;
} back_idx_info;
Type_p parse_single_type(Scanner_p in, TypeBank_p bank);
FuncSymbType TermParseOperator(Scanner_p in, DStr_p id);
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: bii_alloc()
//
// Allocates one cell of back_idx_info based on construction arguments.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline back_idx_info* bii_alloc(const char* name, int arity)
{
back_idx_info* ptr = SizeMalloc(sizeof(back_idx_info));
ptr->name = SecureStrdup(name);
ptr->arity = arity;
return ptr;
}
/*-----------------------------------------------------------------------
//
// Function: cmp_types()
//
// Wrapper for ad-hoc type comparison.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
int cmp_types(const void* el1, const void* el2)
{
assert(el1 && el2);
Type_p t1 = (Type_p)el1;
Type_p t2 = (Type_p)el2;
return TypesCmp(t1 , t2);
}
/*-----------------------------------------------------------------------
//
// Function: type_arg_realloc()
//
// Reallocate new argument array if needed.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void type_arg_realloc(Type_p** args, int current, int new)
{
Type_p* new_arr = SizeMalloc(new * sizeof(Type_p));
int min_size = MIN(current, new);
for(int i=0; i<min_size; i++)
{
new_arr[i] = (*args)[i];
}
SizeFree(*args, current * sizeof(Type_p));
*args = new_arr;
}
/*-----------------------------------------------------------------------
//
// Function: force_arg_sharing()
//
// Make sure that arguments are shared.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void force_arg_sharing(TypeBank_p bank, Type_p t)
{
assert(bank);
assert(t);
for(int i=0; i<t->arity; i++)
{
assert(t->args[i]);
t->args[i] = t->args[i]->type_uid == INVALID_TYPE_UID ?
TypeBankInsertTypeShared(bank, t->args[i]) : t->args[i];
}
}
/*-----------------------------------------------------------------------
//
// Function: ensure_not_kind()
//
// Reports an error if argument is kind.
//
// Global Variables: -
//
// Side Effects : Stops program execution on error.
//
/----------------------------------------------------------------------*/
static inline void ensure_not_kind(Type_p arg, Scanner_p in)
{
if(TypeIsKind(arg))
{
AktTokenError(in, "Only ground types supported.", true);
}
}
/*-----------------------------------------------------------------------
//
// Function: parse_single_type()
//
// Parses one type and makes sure it is shared.
//
// Global Variables: -
//
// Side Effects : input reading, memory management
//
/----------------------------------------------------------------------*/
Type_p parse_single_type(Scanner_p in, TypeBank_p bank)
{
DStr_p id;
FuncSymbType id_type;
Type_p type;
Type_p* typeArgs = NULL;
int arity = 0;
id = DStrAlloc();
id_type = TermParseOperator(in, id);
if(id_type != FSIdentFreeFun && id_type != FSIdentInterpreted)
{
AktTokenError(in, "Function identifier expected", true);
}
if(TestInpTok(in, OpenBracket))
{
AcceptInpTok(in, OpenBracket);
int allocated = REALLOC_STEP;
typeArgs = TypeArgArrayAlloc(allocated);
Type_p arg = parse_single_type(in, bank);
ensure_not_kind(arg, in);
typeArgs[arity++] = arg;
while(TestInpTok(in, Comma))
{
AcceptInpTok(in, Comma);
if(arity == allocated)
{
type_arg_realloc(&typeArgs, allocated, allocated + REALLOC_STEP);
allocated += REALLOC_STEP;
}
arg = parse_single_type(in, bank);
ensure_not_kind(arg, in);
typeArgs[arity++] = arg;
}
AcceptInpTok(in, CloseBracket);
//If f_code did not exist previously, it is a type constructor
TypeConsCode tc_code;
if((tc_code = TypeBankFindTCCode(bank, DStrView(id))) == NAME_NOT_FOUND)
{
tc_code = TypeBankDefineTypeConstructor(bank, DStrView(id), arity);
}
else if(TypeBankFindTCArity(bank, tc_code) != arity)
{
DStr_p msg = DStrAlloc();
DStrAppendStr(msg, "Redefinition of type constructor ");
DStrAppendStr(msg, (char*)TypeBankFindTCName(bank, tc_code));
DStrAppendStr(msg, ". Mismatch in number of arguments.");
AktTokenError(in, DStrView(msg), true);
}
type_arg_realloc(&typeArgs, allocated, arity); // cut array to right size
type = TypeAlloc(tc_code, arity, typeArgs);
}
else
{
TypeConsCode tc_code;
if((tc_code = TypeBankFindTCCode(bank, DStrView(id))) == NAME_NOT_FOUND)
{
tc_code = TypeBankDefineSimpleSort(bank, DStrView(id));
}
else if(TypeBankFindTCArity(bank, tc_code) != 0)
{
DStr_p msg = DStrAlloc();
DStrAppendStr(msg, "Type constructor ");
DStrAppendStr(msg, (char*)TypeBankFindTCName(bank, tc_code));
DStrAppendStr(msg, "has not been declared as simple sort.");
AktTokenError(in, DStrView(msg), true);
}
type = AllocSimpleSort(tc_code);
}
DStrFree(id);
return TypeBankInsertTypeShared(bank, type);
}
/*-----------------------------------------------------------------------
//
// Function: tree_free_fun()
//
// Frees the type stored in the tree.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void tree_free_fun(void* a)
{
TypeFree((Type_p) a);
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: TypeBankAlloc()
//
// Allocate TypeBank cell.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
TypeBank_p TypeBankAlloc()
{
TypeBank_p handle = TypeBankCellAlloc();
handle->back_idx = PStackAlloc();
handle->name_idx = NULL;
handle->names_count = 0;
handle->types_count = 0;
for(int i = 0; i < TYPEBANK_SIZE; i++)
{
handle->hash_table[i] = NULL;
}
TypeConsCode res = TypeBankDefineSimpleSort(handle, "$>_type");
UNUSED(res); assert(res == ArrowTypeCons);
res = TypeBankDefineSimpleSort(handle, "$o");
UNUSED(res); assert(res == STBool);
res = TypeBankDefineSimpleSort(handle, "$i");
UNUSED(res); assert(res == STIndividuals);
res = TypeBankDefineSimpleSort(handle, "$tType");
UNUSED(res); assert(res == STKind);
res = TypeBankDefineSimpleSort(handle, "$int");
UNUSED(res); assert(res == STInteger);
res = TypeBankDefineSimpleSort(handle, "$rat");
UNUSED(res); assert(res == STRational);
res = TypeBankDefineSimpleSort(handle, "$real");
UNUSED(res); assert(res == STReal);
handle->bool_type = TypeBankInsertTypeShared(handle, AllocSimpleSort(STBool));
handle->i_type = TypeBankInsertTypeShared(handle, AllocSimpleSort(STIndividuals));
handle->kind_type = TypeBankInsertTypeShared(handle, AllocSimpleSort(STKind));
handle->integer_type = TypeBankInsertTypeShared(handle, AllocSimpleSort(STInteger));
handle->rational_type = TypeBankInsertTypeShared(handle, AllocSimpleSort(STRational));
handle->real_type = TypeBankInsertTypeShared(handle, AllocSimpleSort(STReal));
handle->default_type = handle->i_type;
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankInsertTypeShared()
//
// Insert type t to type bank to make it shared. If the term t was not
// present in the bank, return new type and free the original type.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
Type_p TypeBankInsertTypeShared(TypeBank_p bank, Type_p t)
{
assert(bank);
assert(t);
Type_p res;
force_arg_sharing(bank, t);
if(t->type_uid == INVALID_TYPE_UID)
{
res = (Type_p) PTreeObjStore(&(bank->hash_table[hash_type(t)]), t, cmp_types);
if(res == NULL)
{
res = t;
res->type_uid = ++bank->types_count;
if (Verbose > 1)
{
fprintf(stderr, "# Type ");
TypePrintTSTP(stderr, bank, t);
fprintf(stderr, " is inserted.\n");
}
}
else
{
TypeFree(t);
}
}
else
{
assert(PTreeObjFind(bank->hash_table + hash_type(t), t, cmp_types));
res = t;
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankDefineTypeConstructor()
//
// Register type constructor with given name and arity.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
TypeConsCode TypeBankDefineTypeConstructor(TypeBank_p bank, const char* name, int arity)
{
assert(bank);
StrTree_p node = StrTreeFind(&bank->name_idx, name);
if(node)
{
if(GetArity(node) == arity)
{
return GetNameId(node);
}
else
{
DStr_p err_msg = DStrAlloc();
DStrAppendStr(err_msg, (char*)"Redefinition of type constructor ");
DStrAppendStr(err_msg, (char*)name);
if (Verbose > 1)
{
fprintf(stderr, "# previous arity was %ld, now it is %d.\n", GetArity(node), arity);
}
Error(DStrView(err_msg), SYNTAX_ERROR);
return -1; // stiff warning
}
}
else
{
IntOrP id = {.i_val = bank->names_count++};
IntOrP arity_iop = {.i_val = (long)arity};
StrTreeStore(&bank->name_idx, (char*)name, id, arity_iop);
PStackPushP(bank->back_idx, bii_alloc(name, arity));
assert(PStackGetSP(bank->back_idx) == bank->names_count);
return id.i_val;
}
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankDefineSimpleSort()
//
// Register simple sort with given name.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
TypeConsCode TypeBankDefineSimpleSort(TypeBank_p bank, const char* name)
{
assert(bank);
return TypeBankDefineTypeConstructor(bank, name, 0);
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankFindTCCode()
//
// Find type constructor code corresponding to given name. If
// the name is not found NAME_NOT_FOUND is returned.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
TypeConsCode TypeBankFindTCCode(TypeBank_p bank, const char* name)
{
assert(bank);
StrTree_p node = StrTreeFind(&bank->name_idx, name);
return node ? GetNameId(node) : NAME_NOT_FOUND;
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankFindTCArity()
//
// Return the arity of given type constructor. Behavior is undefined
// if type constructor does not exist (in debug mode error will be
// reported).
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
int TypeBankFindTCArity(TypeBank_p bank, TypeConsCode tc_code)
{
assert(tc_code != INVALID_TYPE_UID && tc_code < PStackGetSP(bank->back_idx));
assert(PStackGetSP(bank->back_idx) == bank->names_count);
return ((back_idx_info*)PStackElementP(bank->back_idx, tc_code))->arity;
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankFindTCName()
//
// Return the name of given type constructor. Behavior is undefined
// if type constructor does not exist (in debug mode error will be
// reported).
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
const char* TypeBankFindTCName(TypeBank_p bank, TypeConsCode tc_code)
{
assert(tc_code != INVALID_TYPE_UID && tc_code < PStackGetSP(bank->back_idx));
assert(PStackGetSP(bank->back_idx) == bank->names_count);
return ((back_idx_info*)PStackElementP(bank->back_idx, tc_code))->name;
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankParseType()
//
// Parses the TPTP FO type syntax (A1 * A2 * ... * An) > B
// or TPTP HO type syntax A1 > A2 > ... > An. Mixing of syntaxes
// is not allowed.
//
// Global Variables: -
//
// Side Effects : input reading, memory management
//
/----------------------------------------------------------------------*/
Type_p TypeBankParseType(Scanner_p in, TypeBank_p bank)
{
Type_p leftArg = NULL;
Type_p rightArg = NULL;
Type_p res = NULL;
if(problemType == PROBLEM_FO)
{
// We support only [(][(]A1 * A2 * ... * An[])] > B [)]
// or C type (constructors)
// parentheses in [] are optional
int open_parens = 0;
for(; open_parens<2 && TestInpTok(in, OpenBracket); open_parens++)
{
AcceptInpTok(in, OpenBracket);
}
int allocated = REALLOC_STEP;
int arity = 1;
Type_p* args = TypeArgArrayAlloc(allocated);
args[0] = parse_single_type(in, bank);
while(TestInpTok(in, Mult))
{
AcceptInpTok(in, Mult);
rightArg = parse_single_type(in, bank);
if(arity == allocated)
{
type_arg_realloc(&args, allocated, allocated + REALLOC_STEP);
allocated += REALLOC_STEP;
}
args[arity++] = rightArg;
}
if(open_parens && TestInpTok(in, CloseBracket))
{
AcceptInpTok(in, CloseBracket);
open_parens--;
}
if(TestInpTok(in, GreaterSign))
{
AcceptInpTok(in, GreaterSign);
rightArg = parse_single_type(in, bank);
if(arity == allocated)
{
type_arg_realloc(&args, allocated, allocated + REALLOC_STEP);
allocated += REALLOC_STEP;
}
args[arity++] = rightArg;
}
if(open_parens)
{
AcceptInpTok(in, CloseBracket);
open_parens--;
}
type_arg_realloc(&args, allocated, arity); // cut array to right size
if(arity == 1)
{
res = args[0];
SizeFree(args, arity*sizeof(Type_p));
}
else
{
res = AllocArrowType(arity, args);
}
}
else
{
// parsing HO types
assert(problemType == PROBLEM_HO);
if(TestInpTok(in, OpenBracket))
{
AcceptInpTok(in, OpenBracket);
leftArg = TypeBankParseType(in, bank);
AcceptInpTok(in, CloseBracket);
}
else
{
leftArg = parse_single_type(in, bank);
}
if(TestInpTok(in, CloseBracket | Fullstop | CloseSquare | Comma
| EqualSign | NegEqualSign | FOFBinOp))
{
res = leftArg;
}
else
{
int allocated = REALLOC_STEP;
Type_p* args = TypeArgArrayAlloc(allocated);
args[0] = leftArg;
int arity = 1;
do
{
if(TestInpTok(in, Mult))
{
AktTokenError(in,"Mixing of first order and higher "
"order syntax is forbidden", true);
}
AcceptInpTok(in, GreaterSign);
if(TestInpTok(in, OpenBracket))
{
/* We can have nested arrow type */
AcceptInpTok(in, OpenBracket);
rightArg = TypeBankParseType(in, bank);
AcceptInpTok(in, CloseBracket);
}
else
{
rightArg = TypeBankParseType(in, bank);
}
if(arity == allocated)
{
type_arg_realloc(&args, allocated, allocated + REALLOC_STEP);
allocated += REALLOC_STEP;
}
args[arity++] = rightArg;
} while(!(TestInpTok(in, CloseBracket | Fullstop | CloseSquare | Comma)));
if(TypeIsArrow(rightArg))
{
// we have to flatten out the rightArg
args[arity-1] = rightArg->args[0];
int i;
for(i=0; i < rightArg->arity-1; i++)
{
if(arity == allocated)
{
type_arg_realloc(&args, allocated, allocated + REALLOC_STEP);
allocated += REALLOC_STEP;
}
args[arity++] = rightArg->args[i+1];
}
}
type_arg_realloc(&args, allocated, arity); // cut array to right size
res = AllocArrowType(arity, args);
}
}
return TypeBankInsertTypeShared(bank, res);
}
/*-----------------------------------------------------------------------
//
// Function: TypePrintTSTP()
//
// Prints type in either FO or HO format, based on problemType
// status.
//
// Global Variables: problemType
//
// Side Effects : writing to output stream
//
/----------------------------------------------------------------------*/
void TypePrintTSTP(FILE* out, TypeBank_p bank, Type_p type)
{
assert(type);
if(TypeIsArrow(type))
{
if(problemType == PROBLEM_FO)
{
int nr_of_args = type->arity -1;
if(nr_of_args == 1)
{
TypePrintTSTP(out, bank, type->args[0]);
fprintf(out, " > ");
TypePrintTSTP(out, bank, type->args[1]);
}
else
{
fprintf(out, "(");
for(int i=0; i<nr_of_args-1; i++)
{
TypePrintTSTP(out, bank, type->args[i]);
fprintf(out, " * ");
}
TypePrintTSTP(out, bank, type->args[nr_of_args-1]);
fprintf(out, ") > ");
TypePrintTSTP(out, bank, type->args[type->arity-1]);
}
}
else
{
for(int i=0; i<type->arity-1; i++)
{
if(TypeIsArrow(type->args[i]))
{
fprintf(out, "(");
}
TypePrintTSTP(out, bank, type->args[i]);
if(TypeIsArrow(type->args[i]))
{
fprintf(out, ")");
}
fprintf(out, " > ");
}
TypePrintTSTP(out, bank, type->args[type->arity-1]);
}
}
else
{
fprintf(out, "%s", TypeBankFindTCName(bank, type->f_code));
if(type->arity)
{
fprintf(out, "(");
for(int i=0; i<type->arity-1; i++)
{
TypePrintTSTP(out, bank, type->args[i]);
fprintf(out, ", ");
}
TypePrintTSTP(out, bank, type->args[type->arity-1]);
fprintf(out, ")");
}
}
}
/*-----------------------------------------------------------------------
//
// Function: TypePrintTSTP()
//
// Changes return type of the given type to new_ret and returns newly
// generated type.
//
//
// Global Variables: problemType
//
// Side Effects : writing to output stream
//
/----------------------------------------------------------------------*/
Type_p TypeChangeReturnType(TypeBank_p bank, Type_p type, Type_p new_ret)
{
assert(TypeIsArrow(type) || type->f_code == STIndividuals);
Type_p res;
if(TypeIsArrow(type))
{
Type_p copy = TypeCopy(type);
copy->args[copy->arity-1] = new_ret;
res = TypeBankInsertTypeShared(bank, copy);
}
else
{
res = bank->bool_type;
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankFree()
//
// Frees the whole typebank.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void TypeBankFree(TypeBank_p bank)
{
for(int i=0; i<PStackGetSP(bank->back_idx); i++)
{
back_idx_info* bii = PStackElementP(bank->back_idx, i);
void* ptr_to_free = (void*)bii->name;
FREE(ptr_to_free); // stiffing warnings
SizeFree(bii, sizeof(back_idx_info));
}
PStackFree(bank->back_idx);
StrTreeFree(bank->name_idx);
for(int i=0; i<TYPEBANK_SIZE; i++)
{
PObjTreeFree(bank->hash_table[i], tree_free_fun);
bank->hash_table[i] = NULL;
}
SizeFree(bank, sizeof(*bank));
}
/*-----------------------------------------------------------------------
//
// Function: TypeBankAppEncodeTypes()
//
// For each term application symbol according to type a > b print
// declaration app_ab_a_b : translation(a>b) * translation(a) > translation(b).
// If print_type_comment is true then the original, higher-order type
// will be printed as well.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
void TypeBankAppEncodeTypes(FILE* out, TypeBank_p tb, bool print_type_comment)
{
int total_types = 0;
for(int i=0; i<TYPEBANK_SIZE; i++)
{
PStack_p iter = PTreeTraverseInit(tb->hash_table[i]);
PObjTree_p node;
while((node = (PObjTree_p)PTreeTraverseNext(iter)))
{
Type_p type = (Type_p) node->key;
if(TypeIsArrow(type) || SortIsUserDefined(type->f_code))
{
total_types++;
DStr_p type_name = TypeAppEncodedName(type);
if(print_type_comment)
{
fprintf(out, "%%-- ");
TypePrintTSTP(out, tb, type);
fprintf(out, ".\n");
}
fprintf(out, "tff(typedecl%d, type, %s: $tType).\n", total_types, DStrView(type_name));
DStrFree(type_name);
}
}
PTreeTraverseExit(iter);
}
}
|