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 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
|
%-----------------------------------------------------------------------------%
% Copyright (C) 1994-2000 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
% File: type_util.m.
% Main author: fjh.
% This file provides some utility predicates which operate on types.
% It is used by various stages of the compilation after type-checking,
% include the mode checker and the code generator.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module type_util.
:- interface.
:- import_module hlds_module, hlds_pred, hlds_data, prog_data.
:- import_module term.
:- import_module std_util, list, map.
%-----------------------------------------------------------------------------%
% Succeed iff type is an "atomic" type - one which can be
% unified using a simple_test rather than a complicated_unify.
:- pred type_is_atomic(type, module_info).
:- mode type_is_atomic(in, in) is semidet.
:- pred type_id_is_atomic(type_id, module_info).
:- mode type_id_is_atomic(in, in) is semidet.
% type_is_higher_order(Type, PredOrFunc, ArgTypes) succeeds iff
% Type is a higher-order predicate or function type with the specified
% argument types (for functions, the return type is appended to the
% end of the argument types).
:- pred type_is_higher_order(type, pred_or_func,
lambda_eval_method, list(type)).
:- mode type_is_higher_order(in, out, out, out) is semidet.
% Succeed if the given type is a tuple type, returning
% the argument types.
:- pred type_is_tuple(type, list(type)).
:- mode type_is_tuple(in, out) is semidet.
% type_id_is_higher_order(TypeId, PredOrFunc) succeeds iff
% TypeId is a higher-order predicate or function type.
:- pred type_id_is_higher_order(type_id, pred_or_func, lambda_eval_method).
:- mode type_id_is_higher_order(in, out, out) is semidet.
% type_id_is_tuple(TypeId) succeeds iff TypeId is a tuple type.
:- pred type_id_is_tuple(type_id).
:- mode type_id_is_tuple(in) is semidet.
% return true iff there was a `where equality is <predname>'
% declaration for the specified type, and return the name of
% the equality predicate and the context of the type declaration.
:- pred type_has_user_defined_equality_pred(module_info, (type), sym_name).
:- mode type_has_user_defined_equality_pred(in, in, out) is semidet.
% Certain types, e.g. io__state and store__store(S),
% are just dummy types used to ensure logical semantics;
% there is no need to actually pass them, and so when
% importing or exporting procedures to/from C, we don't
% include arguments with these types.
:- pred type_util__is_dummy_argument_type(type).
:- mode type_util__is_dummy_argument_type(in) is semidet.
:- pred type_is_io_state(type).
:- mode type_is_io_state(in) is semidet.
:- pred type_is_aditi_state(type).
:- mode type_is_aditi_state(in) is semidet.
% Remove an `aditi:state' from the given list if one is present.
:- pred type_util__remove_aditi_state(list(type), list(T), list(T)).
:- mode type_util__remove_aditi_state(in, in, out) is det.
% A test for types that are defined in Mercury, but whose definitions
% are `lies', i.e. they are not sufficiently accurate for RTTI
% structures describing the types. Since the RTTI will be hand defined,
% the compiler shouldn't generate RTTI for these types.
:- pred type_id_has_hand_defined_rtti(type_id).
:- mode type_id_has_hand_defined_rtti(in) is semidet.
% A test for type_info-related types that are introduced by
% polymorphism.m. Mode inference never infers unique modes
% for these types, since it would not be useful, and since we
% want to minimize the number of different modes that we infer.
:- pred is_introduced_type_info_type(type).
:- mode is_introduced_type_info_type(in) is semidet.
% In the forwards mode, this predicate checks for a "new " prefix
% at the start of the functor name, and removes it if present;
% it fails if there is no such prefix.
% In the reverse mode, this predicate prepends such a prefix.
% (These prefixes are used for construction unifications
% with existentially typed functors.)
:- pred remove_new_prefix(sym_name, sym_name).
:- mode remove_new_prefix(in, out) is semidet.
:- mode remove_new_prefix(out, in) is det.
% Given a type, determine what sort of type it is.
:- pred classify_type(type, module_info, builtin_type).
:- mode classify_type(in, in, out) is det.
% Given a type_id, determine what sort of type it is.
:- pred classify_type_id(module_info, type_id, builtin_type).
:- mode classify_type_id(in, in, out) is det.
:- type builtin_type ---> int_type
; char_type
; str_type
; float_type
; pred_type
; tuple_type
; enum_type
; polymorphic_type
; user_type.
% Given a non-variable type, return its type-id and argument types.
:- pred type_to_type_id(type, type_id, list(type)).
:- mode type_to_type_id(in, out, out) is semidet.
% Given a variable type, return its type variable.
:- pred type_util__var(type, tvar).
:- mode type_util__var(in, out) is semidet.
:- mode type_util__var(out, in) is det.
% Given a type_id, a list of argument types and maybe a context,
% construct a type.
:- pred construct_type(type_id, list(type), (type)).
:- mode construct_type(in, in, out) is det.
:- pred construct_type(type_id, list(type), prog_context, (type)).
:- mode construct_type(in, in, in, out) is det.
:- pred construct_higher_order_type(pred_or_func, lambda_eval_method,
list(type), (type)).
:- mode construct_higher_order_type(in, in, in, out) is det.
:- pred construct_higher_order_pred_type(lambda_eval_method,
list(type), (type)).
:- mode construct_higher_order_pred_type(in, in, out) is det.
:- pred construct_higher_order_func_type(lambda_eval_method,
list(type), (type), (type)).
:- mode construct_higher_order_func_type(in, in, in, out) is det.
% Construct builtin types.
:- func int_type = (type).
:- func string_type = (type).
:- func float_type = (type).
:- func char_type = (type).
% Given a constant and an arity, return a type_id.
% Fails if the constant is not an atom.
:- pred make_type_id(const, int, type_id).
:- mode make_type_id(in, in, out) is semidet.
% Given a type_id, look up its module/name/arity
:- pred type_util__type_id_module(module_info, type_id, module_name).
:- mode type_util__type_id_module(in, in, out) is det.
:- pred type_util__type_id_name(module_info, type_id, string).
:- mode type_util__type_id_name(in, in, out) is det.
:- pred type_util__type_id_arity(module_info, type_id, arity).
:- mode type_util__type_id_arity(in, in, out) is det.
% If the type is a du type or a tuple type,
% return the list of its constructors.
:- pred type_constructors(type, module_info, list(constructor)).
:- mode type_constructors(in, in, out) is semidet.
% Given a type on which it is possible to have a complete switch,
% return the number of alternatives. (It is possible to have a complete
% switch on any du type and on the builtin type character. It is not
% feasible to have a complete switch on the builtin types integer,
% float, and switch. One cannot have a switch on an abstract type,
% and equivalence types will have been expanded out by the time
% we consider switches.)
:- pred type_util__switch_type_num_functors(module_info::in, (type)::in,
int::out) is semidet.
% Work out the types of the arguments of a functor.
% Aborts if the functor is existentially typed.
% The cons_id is expected to be un-module-qualified.
:- pred type_util__get_cons_id_arg_types(module_info::in, (type)::in,
cons_id::in, list(type)::out) is det.
% The same as type_util__get_cons_id_arg_types except that it
% fails rather than aborting if the functor is existentially
% typed.
% The cons_id is expected to be un-module-qualified.
:- pred type_util__get_cons_id_non_existential_arg_types(module_info::in,
(type)::in, cons_id::in, list(type)::out) is semidet.
% The same as type_util__get_cons_id_arg_types except that the
% cons_id is output non-deterministically.
% The cons_id is not module-qualified.
:- pred type_util__cons_id_arg_types(module_info::in, (type)::in,
cons_id::out, list(type)::out) is nondet.
% Given a type and a cons_id, look up the definitions of that
% type and constructor. Aborts if the cons_id is not user-defined.
:- pred type_util__get_type_and_cons_defn(module_info, (type), cons_id,
hlds_type_defn, hlds_cons_defn).
:- mode type_util__get_type_and_cons_defn(in, in, in, out, out) is det.
% Given a type and a cons_id, look up the definition of that
% constructor; if it is existentially typed, return its definition,
% otherwise fail.
:- pred type_util__get_existq_cons_defn(module_info::in,
(type)::in, cons_id::in, ctor_defn::out) is semidet.
:- pred type_util__is_existq_cons(module_info::in,
(type)::in, cons_id::in) is semidet.
% This type is used to return information about a constructor
% definition, extracted from the hlds_type_defn and hlds_cons_defn
% data types.
:- type ctor_defn
---> ctor_defn(
tvarset,
existq_tvars,
list(class_constraint), % existential constraints
list(type), % functor argument types
(type) % functor result type
).
% Check whether a type is a no_tag type
% (i.e. one with only one constructor, and
% whose one constructor has only one argument,
% and which is not private_builtin:type_info/1),
% and if so, return its constructor symbol and argument type.
:- pred type_is_no_tag_type(module_info, type, sym_name, type).
:- mode type_is_no_tag_type(in, in, out, out) is semidet.
% Check whether some constructors are a no_tag type
% (i.e. one with only one constructor, and
% whose one constructor has only one argument,
% and which is not private_builtin:type_info/1),
% and if so, return its constructor symbol, argument type,
% and the argument's name (if it has one).
%
% This doesn't do any checks for options that might be set
% (such as turning off no_tag_types). If you want those checks
% you should use type_is_no_tag_type/4, or if you really know
% what you are doing, perform the checks yourself.
:- pred type_constructors_are_no_tag_type(list(constructor), sym_name, type,
maybe(string)).
:- mode type_constructors_are_no_tag_type(in, out, out, out) is semidet.
% Unify (with occurs check) two types with respect to a type
% substitution and update the type bindings.
% The third argument is a list of type variables which cannot
% be bound (i.e. head type variables).
:- pred type_unify(type, type, list(tvar), tsubst, tsubst).
:- mode type_unify(in, in, in, in, out) is semidet.
:- pred type_unify_list(list(type), list(type), list(tvar), tsubst, tsubst).
:- mode type_unify_list(in, in, in, in, out) is semidet.
% Return a list of the type variables of a type.
:- pred type_util__vars(type, list(tvar)).
:- mode type_util__vars(in, out) is det.
% Return a list of the type variables of a type,
% ignoring any type variables if the variable in
% question is a type-info
:- pred type_util__real_vars(type, list(tvar)).
:- mode type_util__real_vars(in, out) is det.
% type_list_subsumes(TypesA, TypesB, Subst) succeeds iff the list
% TypesA subsumes (is more general than) TypesB, producing a
% type substitution which when applied to TypesA will give TypesB.
:- pred type_list_subsumes(list(type), list(type), tsubst).
:- mode type_list_subsumes(in, in, out) is semidet.
% arg_type_list_subsumes(TVarSet, ArgTypes,
% CalleeTVarSet, CalleeExistQVars, CalleeArgTypes).
%
% Check that the argument types of the called predicate,
% function or constructor subsume the types of the
% arguments of the call. This checks that none
% of the existentially quantified type variables of
% the callee are bound.
:- pred arg_type_list_subsumes(tvarset, list(type),
tvarset, existq_tvars, list(type)).
:- mode arg_type_list_subsumes(in, in, in, in, in) is semidet.
% apply a type substitution (i.e. map from tvar -> type)
% to all the types in a variable typing (i.e. map from var -> type).
:- pred apply_substitution_to_type_map(map(prog_var, type), tsubst,
map(prog_var, type)).
:- mode apply_substitution_to_type_map(in, in, out) is det.
% same thing as above, except for a recursive substitution
% (i.e. we keep applying the substitution recursively until
% there are no more changes).
:- pred apply_rec_substitution_to_type_map(map(prog_var, type), tsubst,
map(prog_var, type)).
:- mode apply_rec_substitution_to_type_map(in, in, out) is det.
% Update a map from tvar to type_info_locn, using the type renaming
% and substitution to rename tvars and a variable substitution to
% rename vars. The type renaming is applied before the type
% substitution.
%
% If tvar maps to a another type variable, we keep the new
% variable, if it maps to a type, we remove it from the map.
:- pred apply_substitutions_to_var_map(map(tvar, type_info_locn), tsubst,
map(tvar, type), map(prog_var, prog_var), map(tvar, type_info_locn)).
:- mode apply_substitutions_to_var_map(in, in, in, in, out) is det.
% Update a map from class_constraint to var, using the type renaming
% and substitution to rename tvars and a variable substition to
% rename vars. The type renaming is applied before the type
% substitution.
:- pred apply_substitutions_to_typeclass_var_map(
map(class_constraint, prog_var), tsubst, map(tvar, type),
map(prog_var, prog_var), map(class_constraint, prog_var)).
:- mode apply_substitutions_to_typeclass_var_map(in, in, in, in, out) is det.
:- pred apply_rec_subst_to_constraints(tsubst, class_constraints,
class_constraints).
:- mode apply_rec_subst_to_constraints(in, in, out) is det.
:- pred apply_rec_subst_to_constraint_list(tsubst,
list(class_constraint), list(class_constraint)).
:- mode apply_rec_subst_to_constraint_list(in, in, out) is det.
:- pred apply_rec_subst_to_constraint(tsubst, class_constraint,
class_constraint).
:- mode apply_rec_subst_to_constraint(in, in, out) is det.
:- pred apply_subst_to_constraints(tsubst, class_constraints,
class_constraints).
:- mode apply_subst_to_constraints(in, in, out) is det.
:- pred apply_subst_to_constraint_list(tsubst, list(class_constraint),
list(class_constraint)).
:- mode apply_subst_to_constraint_list(in, in, out) is det.
:- pred apply_subst_to_constraint(tsubst, class_constraint,
class_constraint).
:- mode apply_subst_to_constraint(in, in, out) is det.
:- pred apply_subst_to_constraint_proofs(tsubst,
map(class_constraint, constraint_proof),
map(class_constraint, constraint_proof)).
:- mode apply_subst_to_constraint_proofs(in, in, out) is det.
:- pred apply_rec_subst_to_constraint_proofs(tsubst,
map(class_constraint, constraint_proof),
map(class_constraint, constraint_proof)).
:- mode apply_rec_subst_to_constraint_proofs(in, in, out) is det.
:- pred apply_variable_renaming_to_type_map(map(tvar, tvar),
vartypes, vartypes).
:- mode apply_variable_renaming_to_type_map(in, in, out) is det.
:- pred apply_variable_renaming_to_constraints(map(tvar, tvar),
class_constraints, class_constraints).
:- mode apply_variable_renaming_to_constraints(in, in, out) is det.
:- pred apply_variable_renaming_to_constraint_list(map(tvar, tvar),
list(class_constraint), list(class_constraint)).
:- mode apply_variable_renaming_to_constraint_list(in, in, out) is det.
:- pred apply_variable_renaming_to_constraint(map(tvar, tvar),
class_constraint, class_constraint).
:- mode apply_variable_renaming_to_constraint(in, in, out) is det.
% Apply a renaming (partial map) to a list.
% Useful for applying a variable renaming to a list of variables.
:- pred apply_partial_map_to_list(list(T), map(T, T), list(T)).
:- mode apply_partial_map_to_list(in, in, out) is det.
% strip out the prog_context fields, replacing them with empty
% prog_context (as obtained by term__context_init/1)
% in a type or list of types
:- pred strip_prog_contexts(list(term(T))::in, list(term(T))::out) is det.
:- pred strip_prog_context(term(T)::in, term(T)::out) is det.
% cons_id_adjusted_arity(ModuleInfo, Type, ConsId):
% Returns the number of arguments of specified constructor id,
% adjusted to include the extra typeclassinfo and typeinfo
% arguments inserted by polymorphism.m for existentially
% typed constructors.
%
:- func cons_id_adjusted_arity(module_info, type, cons_id) = int.
% constraint_list_get_tvars(Constraints, TVars):
% return the list of type variables contained in a
% list of constraints
%
:- pred constraint_list_get_tvars(list(class_constraint), list(tvar)).
:- mode constraint_list_get_tvars(in, out) is det.
% constraint_list_get_tvars(Constraint, TVars):
% return the list of type variables contained in a constraint.
:- pred constraint_get_tvars(class_constraint, list(tvar)).
:- mode constraint_get_tvars(in, out) is det.
:- pred get_unconstrained_tvars(list(tvar), list(class_constraint), list(tvar)).
:- mode get_unconstrained_tvars(in, in, out) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module prog_io, prog_io_goal, prog_util, options, globals.
:- import_module bool, char, int, string.
:- import_module assoc_list, require, varset.
type_util__type_id_module(_ModuleInfo, TypeName - _Arity, ModuleName) :-
sym_name_get_module_name(TypeName, unqualified(""), ModuleName).
type_util__type_id_name(_ModuleInfo, Name0 - _Arity, Name) :-
unqualify_name(Name0, Name).
type_util__type_id_arity(_ModuleInfo, _Name - Arity, Arity).
type_is_atomic(Type, ModuleInfo) :-
type_to_type_id(Type, TypeId, _),
type_id_is_atomic(TypeId, ModuleInfo).
type_id_is_atomic(TypeId, ModuleInfo) :-
classify_type_id(ModuleInfo, TypeId, BuiltinType),
BuiltinType \= polymorphic_type,
BuiltinType \= tuple_type,
BuiltinType \= pred_type,
BuiltinType \= user_type.
type_util__var(term__variable(Var), Var).
type_id_has_hand_defined_rtti(qualified(PB, "type_info") - 1) :-
mercury_private_builtin_module(PB).
type_id_has_hand_defined_rtti(qualified(PB, "type_ctor_info") - 1) :-
mercury_private_builtin_module(PB).
type_id_has_hand_defined_rtti(qualified(PB, "typeclass_info") - 1) :-
mercury_private_builtin_module(PB).
type_id_has_hand_defined_rtti(qualified(PB, "base_typeclass_info") - 1) :-
mercury_private_builtin_module(PB).
is_introduced_type_info_type(Type) :-
sym_name_and_args(Type, TypeName, _),
TypeName = qualified(PrivateBuiltin, Name),
( Name = "type_info"
; Name = "type_ctor_info"
; Name = "typeclass_info"
; Name = "base_typeclass_info"
),
mercury_private_builtin_module(PrivateBuiltin).
remove_new_prefix(unqualified(Name0), unqualified(Name)) :-
string__append("new ", Name, Name0).
remove_new_prefix(qualified(Module, Name0), qualified(Module, Name)) :-
string__append("new ", Name, Name0).
%-----------------------------------------------------------------------------%
% Given a type, determine what sort of type it is.
classify_type(VarType, ModuleInfo, Type) :-
( type_to_type_id(VarType, TypeId, _) ->
classify_type_id(ModuleInfo, TypeId, Type)
;
Type = polymorphic_type
).
classify_type_id(ModuleInfo, TypeId, Type) :-
( TypeId = unqualified("character") - 0 ->
Type = char_type
; TypeId = unqualified("int") - 0 ->
Type = int_type
; TypeId = unqualified("float") - 0 ->
Type = float_type
; TypeId = unqualified("string") - 0 ->
Type = str_type
; type_id_is_higher_order(TypeId, _, _) ->
Type = pred_type
; type_id_is_tuple(TypeId) ->
Type = tuple_type
; type_id_is_enumeration(TypeId, ModuleInfo) ->
Type = enum_type
;
Type = user_type
).
type_is_higher_order(Type, PredOrFunc, EvalMethod, PredArgTypes) :-
(
Type = term__functor(term__atom("="),
[FuncEvalAndArgs, FuncRetType], _)
->
get_lambda_eval_method_and_args("func", FuncEvalAndArgs,
EvalMethod, FuncArgTypes),
list__append(FuncArgTypes, [FuncRetType], PredArgTypes),
PredOrFunc = function
;
get_lambda_eval_method_and_args("pred",
Type, EvalMethod, PredArgTypes),
PredOrFunc = predicate
).
type_is_tuple(Type, ArgTypes) :-
type_to_type_id(Type, TypeId, ArgTypes),
type_id_is_tuple(TypeId).
% From the type of a lambda expression, work out how it should
% be evaluated and extract the argument types.
:- pred get_lambda_eval_method_and_args(string, (type),
lambda_eval_method, list(type)) is det.
:- mode get_lambda_eval_method_and_args(in, in, out, out) is semidet.
get_lambda_eval_method_and_args(PorFStr, Type0, EvalMethod, ArgTypes) :-
Type0 = term__functor(term__atom(Functor), Args, _),
( Functor = PorFStr ->
EvalMethod = normal,
ArgTypes = Args
;
Args = [Type1],
Type1 = term__functor(term__atom(PorFStr), ArgTypes, _),
( Functor = "aditi_bottom_up" ->
EvalMethod = (aditi_bottom_up)
;
Functor = "aditi_top_down",
EvalMethod = (aditi_top_down)
)
).
type_id_is_higher_order(SymName - _Arity, PredOrFunc, EvalMethod) :-
(
SymName = qualified(unqualified(EvalMethodStr), PorFStr),
(
EvalMethodStr = "aditi_bottom_up",
EvalMethod = (aditi_bottom_up)
;
EvalMethodStr = "aditi_top_down",
EvalMethod = (aditi_top_down)
)
;
SymName = unqualified(PorFStr),
EvalMethod = normal
),
(
PorFStr = "pred",
PredOrFunc = predicate
;
PorFStr = "func",
PredOrFunc = function
).
type_id_is_tuple(unqualified("{}") - _).
type_has_user_defined_equality_pred(ModuleInfo, Type, SymName) :-
module_info_types(ModuleInfo, TypeTable),
type_to_type_id(Type, TypeId, _TypeArgs),
map__search(TypeTable, TypeId, TypeDefn),
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
TypeBody = du_type(_, _, _, yes(SymName)).
% Certain types, e.g. io__state and store__store(S),
% are just dummy types used to ensure logical semantics;
% there is no need to actually pass them, and so when
% importing or exporting procedures to/from C, we don't
% include arguments with these types.
type_util__is_dummy_argument_type(Type) :-
Type = term__functor(term__atom(":"), [
term__functor(term__atom(ModuleName), [], _),
term__functor(term__atom(TypeName), TypeArgs, _)
], _),
list__length(TypeArgs, TypeArity),
type_util__is_dummy_argument_type_2(ModuleName, TypeName, TypeArity).
:- pred type_util__is_dummy_argument_type_2(string::in, string::in, arity::in)
is semidet.
% XXX should we include aditi:state/0 in this list?
type_util__is_dummy_argument_type_2("io", "state", 0). % io:state/0
type_util__is_dummy_argument_type_2("store", "store", 1). % store:store/1.
type_is_io_state(Type) :-
type_to_type_id(Type,
qualified(unqualified("io"), "state") - 0, []).
type_is_aditi_state(Type) :-
type_to_type_id(Type,
qualified(unqualified("aditi"), "state") - 0, []).
type_util__remove_aditi_state([], [], []).
type_util__remove_aditi_state([], [_|_], _) :-
error("type_util__remove_aditi_state").
type_util__remove_aditi_state([_|_], [], _) :-
error("type_util__remove_aditi_state").
type_util__remove_aditi_state([Type | Types], [Arg | Args0], Args) :-
( type_is_aditi_state(Type) ->
type_util__remove_aditi_state(Types, Args0, Args)
;
type_util__remove_aditi_state(Types, Args0, Args1),
Args = [Arg | Args1]
).
:- pred type_id_is_enumeration(type_id, module_info).
:- mode type_id_is_enumeration(in, in) is semidet.
type_id_is_enumeration(TypeId, ModuleInfo) :-
module_info_types(ModuleInfo, TypeDefnTable),
map__search(TypeDefnTable, TypeId, TypeDefn),
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
TypeBody = du_type(_, _, IsEnum, _),
IsEnum = yes.
type_to_type_id(Type, SymName - Arity, Args) :-
Type \= term__variable(_),
% higher order types may have representations where
% their arguments don't directly correspond to the
% arguments of the term.
(
type_is_higher_order(Type, PredOrFunc,
EvalMethod, PredArgTypes)
->
Args = PredArgTypes,
list__length(Args, Arity0),
adjust_func_arity(PredOrFunc, Arity, Arity0),
(
PredOrFunc = predicate,
PorFStr = "pred"
;
PredOrFunc = function,
PorFStr = "func"
),
(
EvalMethod = (aditi_bottom_up),
SymName = qualified(unqualified("aditi_bottom_up"),
PorFStr)
;
EvalMethod = (aditi_top_down),
SymName = qualified(unqualified("aditi_top_down"),
PorFStr)
;
EvalMethod = normal,
SymName = unqualified(PorFStr)
)
;
sym_name_and_args(Type, SymName, Args),
% `private_builtin:constraint' is introduced by polymorphism,
% and should only appear as the argument of a
% `typeclass:info/1' type.
% It behaves sort of like a type variable, so according to the
% specification of `type_to_type_id', it should cause failure.
% There isn't a definition in the type table.
\+ (
SymName = qualified(ModuleName, UnqualName),
UnqualName = "constraint",
mercury_private_builtin_module(PrivateBuiltin),
ModuleName = PrivateBuiltin
),
list__length(Args, Arity)
).
construct_type(TypeId, Args, Type) :-
term__context_init(Context),
construct_type(TypeId, Args, Context, Type).
construct_type(TypeId, Args, Context, Type) :-
( type_id_is_higher_order(TypeId, PredOrFunc, EvalMethod) ->
construct_higher_order_type(PredOrFunc, EvalMethod, Args, Type)
;
TypeId = SymName - _,
construct_qualified_term(SymName, Args, Context, Type)
).
construct_higher_order_type(PredOrFunc, EvalMethod, ArgTypes, Type) :-
(
PredOrFunc = predicate,
construct_higher_order_pred_type(EvalMethod, ArgTypes, Type)
;
PredOrFunc = function,
pred_args_to_func_args(ArgTypes, FuncArgTypes, FuncRetType),
construct_higher_order_func_type(EvalMethod, FuncArgTypes,
FuncRetType, Type)
).
construct_higher_order_pred_type(EvalMethod, ArgTypes, Type) :-
term__context_init(Context),
construct_qualified_term(unqualified("pred"),
ArgTypes, Context, Type0),
qualify_higher_order_type(EvalMethod, Type0, Type).
construct_higher_order_func_type(EvalMethod, ArgTypes, RetType, Type) :-
term__context_init(Context),
construct_qualified_term(unqualified("func"),
ArgTypes, Context, Type0),
qualify_higher_order_type(EvalMethod, Type0, Type1),
Type = term__functor(term__atom("="), [Type1, RetType], Context).
:- pred qualify_higher_order_type(lambda_eval_method, (type), (type)).
:- mode qualify_higher_order_type(in, in, out) is det.
qualify_higher_order_type(normal, Type, Type).
qualify_higher_order_type((aditi_top_down), Type0,
term__functor(term__atom("aditi_top_down"), [Type0], Context)) :-
term__context_init(Context).
qualify_higher_order_type((aditi_bottom_up), Type0,
term__functor(term__atom("aditi_bottom_up"), [Type0], Context)) :-
term__context_init(Context).
int_type = Type :- construct_type(unqualified("int") - 0, [], Type).
string_type = Type :- construct_type(unqualified("string") - 0, [], Type).
float_type = Type :- construct_type(unqualified("float") - 0, [], Type).
char_type = Type :- construct_type(unqualified("character") - 0, [], Type).
%-----------------------------------------------------------------------------%
% Given a constant and an arity, return a type_id.
% This really ought to take a name and an arity -
% use of integers/floats/strings as type names should
% be rejected by the parser in prog_io.m, not in module_qual.m.
make_type_id(term__atom(Name), Arity, unqualified(Name) - Arity).
%-----------------------------------------------------------------------------%
% If the type is a du type, return the list of its constructors.
type_constructors(Type, ModuleInfo, Constructors) :-
type_to_type_id(Type, TypeId, TypeArgs),
( type_id_is_tuple(TypeId) ->
% Tuples are never existentially typed.
ExistQVars = [],
ClassConstraints = [],
CtorArgs = list__map((func(ArgType) = no - ArgType), TypeArgs),
Constructors = [ctor(ExistQVars, ClassConstraints,
unqualified("{}"), CtorArgs)]
;
module_info_types(ModuleInfo, TypeTable),
map__search(TypeTable, TypeId, TypeDefn),
hlds_data__get_type_defn_tparams(TypeDefn, TypeParams),
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
TypeBody = du_type(Constructors0, _, _, _),
substitute_type_args(TypeParams, TypeArgs, Constructors0,
Constructors)
).
%-----------------------------------------------------------------------------%
type_util__switch_type_num_functors(ModuleInfo, Type, NumFunctors) :-
type_to_type_id(Type, TypeId, _),
( TypeId = unqualified("character") - 0 ->
% XXX the following code uses the source machine's character
% size, not the target's, so it won't work if cross-compiling
% to a machine with a different size character.
char__max_char_value(MaxChar),
char__min_char_value(MinChar),
NumFunctors is MaxChar - MinChar + 1
; type_id_is_tuple(TypeId) ->
NumFunctors = 1
;
module_info_types(ModuleInfo, TypeTable),
map__search(TypeTable, TypeId, TypeDefn),
hlds_data__get_type_defn_body(TypeDefn, TypeBody),
TypeBody = du_type(_, ConsTable, _, _),
map__count(ConsTable, NumFunctors)
).
%-----------------------------------------------------------------------------%
type_util__get_cons_id_arg_types(ModuleInfo, Type, ConsId, ArgTypes) :-
type_util__get_cons_id_arg_types_2(abort_on_exist_qvar,
ModuleInfo, Type, ConsId, ArgTypes).
type_util__get_cons_id_non_existential_arg_types(ModuleInfo, Type, ConsId,
ArgTypes) :-
type_util__get_cons_id_arg_types_2(fail_on_exist_qvar,
ModuleInfo, Type, ConsId, ArgTypes).
:- type exist_qvar_action
---> fail_on_exist_qvar
; abort_on_exist_qvar.
:- pred type_util__get_cons_id_arg_types_2(exist_qvar_action,
module_info, (type), cons_id, list(type)).
:- mode type_util__get_cons_id_arg_types_2(in(bound(fail_on_exist_qvar)),
in, in, in, out) is semidet.
:- mode type_util__get_cons_id_arg_types_2(in(bound(abort_on_exist_qvar)),
in, in, in, out) is det.
type_util__get_cons_id_arg_types_2(EQVarAction, ModuleInfo, VarType, ConsId,
ArgTypes) :-
(
type_to_type_id(VarType, TypeId, TypeArgs)
->
(
% The argument types of a tuple cons_id are the
% arguments of the tuple type.
type_id_is_tuple(TypeId)
->
ArgTypes = TypeArgs
;
type_util__do_get_type_and_cons_defn(ModuleInfo, TypeId,
ConsId, TypeDefn, ConsDefn),
ConsDefn = hlds_cons_defn(ExistQVars0, _Constraints0,
Args, _, _),
Args \= []
->
hlds_data__get_type_defn_tparams(TypeDefn, TypeDefnParams),
term__term_list_to_var_list(TypeDefnParams, TypeDefnVars),
% XXX handle ExistQVars
( ExistQVars0 = [] ->
true
;
(
EQVarAction = abort_on_exist_qvar,
error("type_util__get_cons_id_arg_types: existentially typed cons_id")
;
EQVarAction = fail_on_exist_qvar,
fail
)
),
map__from_corresponding_lists(TypeDefnVars, TypeArgs, TSubst),
assoc_list__values(Args, ArgTypes0),
term__apply_substitution_to_list(ArgTypes0, TSubst, ArgTypes)
;
ArgTypes = []
)
;
ArgTypes = []
).
type_util__cons_id_arg_types(ModuleInfo, VarType, ConsId, ArgTypes) :-
type_to_type_id(VarType, TypeId, TypeArgs),
module_info_types(ModuleInfo, Types),
map__search(Types, TypeId, TypeDefn),
hlds_data__get_type_defn_body(TypeDefn, TypeDefnBody),
TypeDefnBody = du_type(_, ConsTags, _, _),
map__member(ConsTags, ConsId, _),
module_info_ctors(ModuleInfo, Ctors),
map__lookup(Ctors, ConsId, ConsDefns),
list__member(ConsDefn, ConsDefns),
ConsDefn = hlds_cons_defn(ExistQVars0, _, Args, TypeId, _),
% XXX handle ExistQVars
ExistQVars0 = [],
hlds_data__get_type_defn_tparams(TypeDefn, TypeDefnParams),
term__term_list_to_var_list(TypeDefnParams, TypeDefnVars),
map__from_corresponding_lists(TypeDefnVars, TypeArgs, TSubst),
assoc_list__values(Args, ArgTypes0),
term__apply_substitution_to_list(ArgTypes0, TSubst, ArgTypes).
type_util__is_existq_cons(ModuleInfo, VarType, ConsId) :-
type_util__is_existq_cons(ModuleInfo, VarType, ConsId, _).
:- pred type_util__is_existq_cons(module_info::in,
(type)::in, cons_id::in, hlds_cons_defn::out) is semidet.
type_util__is_existq_cons(ModuleInfo, VarType, ConsId, ConsDefn) :-
type_to_type_id(VarType, TypeId, _),
type_util__get_cons_defn(ModuleInfo, TypeId, ConsId, ConsDefn),
ConsDefn = hlds_cons_defn(ExistQVars, _, _, _, _),
ExistQVars \= [].
% Given a type and a cons_id, look up the definition of that
% constructor; if it is existentially typed, return its definition,
% otherwise fail.
type_util__get_existq_cons_defn(ModuleInfo, VarType, ConsId, CtorDefn) :-
type_util__is_existq_cons(ModuleInfo, VarType, ConsId, ConsDefn),
ConsDefn = hlds_cons_defn(ExistQVars, Constraints, Args, _, _),
assoc_list__values(Args, ArgTypes),
module_info_types(ModuleInfo, Types),
type_to_type_id(VarType, TypeId, _),
map__lookup(Types, TypeId, TypeDefn),
hlds_data__get_type_defn_tvarset(TypeDefn, TypeVarSet),
hlds_data__get_type_defn_tparams(TypeDefn, TypeDefnParams),
type_to_type_id(VarType, TypeId, _),
construct_type(TypeId, TypeDefnParams, RetType),
CtorDefn = ctor_defn(TypeVarSet, ExistQVars, Constraints,
ArgTypes, RetType).
type_util__get_type_and_cons_defn(ModuleInfo, Type, ConsId,
TypeDefn, ConsDefn) :-
(
type_to_type_id(Type, TypeId, _),
type_util__do_get_type_and_cons_defn(ModuleInfo,
TypeId, ConsId, TypeDefn0, ConsDefn0)
->
TypeDefn = TypeDefn0,
ConsDefn = ConsDefn0
;
error("type_util__get_type_and_cons_defn")
).
:- pred type_util__do_get_type_and_cons_defn(module_info::in,
type_id::in, cons_id::in, hlds_type_defn::out,
hlds_cons_defn::out) is semidet.
type_util__do_get_type_and_cons_defn(ModuleInfo, TypeId, ConsId,
TypeDefn, ConsDefn) :-
type_util__get_cons_defn(ModuleInfo, TypeId, ConsId, ConsDefn),
module_info_types(ModuleInfo, Types),
map__lookup(Types, TypeId, TypeDefn).
:- pred type_util__get_cons_defn(module_info::in, type_id::in, cons_id::in,
hlds_cons_defn::out) is semidet.
type_util__get_cons_defn(ModuleInfo, TypeId, ConsId, ConsDefn) :-
module_info_ctors(ModuleInfo, Ctors),
% will fail for builtin cons_ids.
map__search(Ctors, ConsId, ConsDefns),
MatchingCons = lambda([ThisConsDefn::in] is semidet, (
ThisConsDefn = hlds_cons_defn(_, _, _, TypeId, _)
)),
list__filter(MatchingCons, ConsDefns, [ConsDefn]).
%-----------------------------------------------------------------------------%
type_is_no_tag_type(ModuleInfo, Type, Ctor, ArgType) :-
type_to_type_id(Type, TypeId, TypeArgs),
module_info_no_tag_types(ModuleInfo, NoTagTypes),
map__search(NoTagTypes, TypeId, NoTagType),
NoTagType = no_tag_type(TypeParams0, Ctor, ArgType0),
( TypeParams0 = [] ->
ArgType = ArgType0
;
term__term_list_to_var_list(TypeParams0, TypeParams),
map__from_corresponding_lists(TypeParams, TypeArgs, Subn),
term__apply_substitution(ArgType0, Subn, ArgType)
).
% The checks for type_info and type_ctor_info
% are needed because those types lie about their
% arity; it might be cleaner to change that in
% private_builtin.m, but that would cause some
% bootstrapping difficulties.
% It might be slightly better to check for private_builtin:type_info
% etc. rather than just checking the unqualified type name,
% but I found it difficult to verify that the constructors
% would always be fully module-qualified at points where
% type_constructors_are_no_tag_type/3 is called.
type_constructors_are_no_tag_type(Ctors, Ctor, ArgType, MaybeArgName) :-
Ctors = [SingleCtor],
SingleCtor = ctor(ExistQVars, _Constraints, Ctor,
[MaybeSymName - ArgType]),
ExistQVars = [],
unqualify_name(Ctor, Name),
Name \= "type_info",
Name \= "type_ctor_info",
Name \= "typeclass_info",
Name \= "base_typeclass_info",
% We don't handle unary tuples as no_tag types --
% they are rare enough that it's not worth
% the implementation effort.
Name \= "{}",
(
MaybeSymName = yes(SymName),
unqualify_name(SymName, ArgName),
MaybeArgName = yes(ArgName)
;
MaybeSymName = no,
MaybeArgName = no
).
%-----------------------------------------------------------------------------%
% Substitute the actual values of the type parameters
% in list of constructors, for a particular instance of
% a polymorphic type.
:- pred substitute_type_args(list(type_param), list(type),
list(constructor), list(constructor)).
:- mode substitute_type_args(in, in, in, out) is det.
substitute_type_args(TypeParams0, TypeArgs, Constructors0, Constructors) :-
( TypeParams0 = [] ->
Constructors = Constructors0
;
term__term_list_to_var_list(TypeParams0, TypeParams),
map__from_corresponding_lists(TypeParams, TypeArgs, Subst),
substitute_type_args_2(Constructors0, Subst, Constructors)
).
:- pred substitute_type_args_2(list(constructor), tsubst,
list(constructor)).
:- mode substitute_type_args_2(in, in, out) is det.
substitute_type_args_2([], _, []).
substitute_type_args_2([Ctor0| Ctors0], Subst, [Ctor | Ctors]) :-
% Note: prog_io.m ensures that the existentially quantified
% variables, if any, are distinct from the parameters,
% and that the (existential) constraints can only contain
% existentially quantified variables, so there's
% no need to worry about applying the substitution to
% ExistQVars or Constraints
Ctor0 = ctor(ExistQVars, Constraints, Name, Args0),
Ctor = ctor(ExistQVars, Constraints, Name, Args),
substitute_type_args_3(Args0, Subst, Args),
substitute_type_args_2(Ctors0, Subst, Ctors).
:- pred substitute_type_args_3(list(constructor_arg), tsubst,
list(constructor_arg)).
:- mode substitute_type_args_3(in, in, out) is det.
substitute_type_args_3([], _, []).
substitute_type_args_3([Name - Arg0 | Args0], Subst, [Name - Arg | Args]) :-
term__apply_substitution(Arg0, Subst, Arg),
substitute_type_args_3(Args0, Subst, Args).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Check whether TypesA subsumes TypesB, and if so return
% a type substitution that will map from TypesA to TypesB.
type_list_subsumes(TypesA, TypesB, TypeSubst) :-
%
% TypesA subsumes TypesB iff TypesA can be unified with TypesB
% without binding any of the type variables in TypesB.
%
term__vars_list(TypesB, TypesBVars),
map__init(TypeSubst0),
type_unify_list(TypesA, TypesB, TypesBVars, TypeSubst0, TypeSubst).
arg_type_list_subsumes(TVarSet, ArgTypes, CalleeTVarSet,
CalleeExistQVars0, CalleeArgTypes0) :-
%
% rename the type variables in the callee's argument types.
%
varset__merge_subst(TVarSet, CalleeTVarSet, _TVarSet1, Subst),
term__apply_substitution_to_list(CalleeArgTypes0, Subst,
CalleeArgTypes),
map__apply_to_list(CalleeExistQVars0, Subst, CalleeExistQTypes0),
%
% check that the types of the candidate predicate/function
% subsume the actual argument types
% [This is the right thing to do even for calls to
% existentially typed preds, because we're using the
% type variables from the callee's pred decl (obtained
% from the pred_info via pred_info_arg_types) not the types
% inferred from the callee's clauses (and stored in the
% clauses_info and proc_info) -- the latter
% might not subsume the actual argument types.]
%
type_list_subsumes(CalleeArgTypes, ArgTypes, TypeSubst),
%
% check that the type substitution did not bind any
% existentially typed variables to non-ground types
%
( CalleeExistQTypes0 = [] ->
% optimize common case
true
;
term__apply_rec_substitution_to_list(CalleeExistQTypes0,
TypeSubst, CalleeExistQTypes),
all [T] (list__member(T, CalleeExistQTypes) =>
type_util__var(T, _))
% it might make sense to also check that
% the type substitution did not bind any
% existentially typed variables to universally
% quantified type variables in the caller's
% argument types
).
%-----------------------------------------------------------------------------%
% Types are represented as terms, but we can't just use term__unify
% because we need to avoid binding any of the "head type params"
% (the type variables that occur in the head of the clause),
% and because one day we might want to handle equivalent types.
type_unify(term__variable(X), term__variable(Y), HeadTypeParams,
Bindings0, Bindings) :-
( list__member(Y, HeadTypeParams) ->
type_unify_head_type_param(X, Y, HeadTypeParams,
Bindings0, Bindings)
; list__member(X, HeadTypeParams) ->
type_unify_head_type_param(Y, X, HeadTypeParams,
Bindings0, Bindings)
; map__search(Bindings0, X, BindingOfX) ->
( map__search(Bindings0, Y, BindingOfY) ->
% both X and Y already have bindings - just
% unify the types they are bound to
type_unify(BindingOfX, BindingOfY, HeadTypeParams,
Bindings0, Bindings)
;
term__apply_rec_substitution(BindingOfX,
Bindings0, SubstBindingOfX),
% Y is a type variable which hasn't been bound yet
( SubstBindingOfX = term__variable(Y) ->
Bindings = Bindings0
;
\+ term__occurs(SubstBindingOfX, Y,
Bindings0),
map__det_insert(Bindings0, Y, SubstBindingOfX,
Bindings)
)
)
;
( map__search(Bindings0, Y, BindingOfY) ->
term__apply_rec_substitution(BindingOfY,
Bindings0, SubstBindingOfY),
% X is a type variable which hasn't been bound yet
( SubstBindingOfY = term__variable(X) ->
Bindings = Bindings0
;
\+ term__occurs(SubstBindingOfY, X,
Bindings0),
map__det_insert(Bindings0, X, SubstBindingOfY,
Bindings)
)
;
% both X and Y are unbound type variables -
% bind one to the other
( X = Y ->
Bindings = Bindings0
;
map__det_insert(Bindings0, X,
term__variable(Y), Bindings)
)
)
).
type_unify(term__variable(X), term__functor(F, As, C), HeadTypeParams,
Bindings0, Bindings) :-
(
map__search(Bindings0, X, BindingOfX)
->
type_unify(BindingOfX, term__functor(F, As, C),
HeadTypeParams, Bindings0, Bindings)
;
\+ term__occurs_list(As, X, Bindings0),
\+ list__member(X, HeadTypeParams),
map__det_insert(Bindings0, X, term__functor(F, As, C),
Bindings)
).
type_unify(term__functor(F, As, C), term__variable(X), HeadTypeParams,
Bindings0, Bindings) :-
(
map__search(Bindings0, X, BindingOfX)
->
type_unify(term__functor(F, As, C), BindingOfX,
HeadTypeParams, Bindings0, Bindings)
;
\+ term__occurs_list(As, X, Bindings0),
\+ list__member(X, HeadTypeParams),
map__det_insert(Bindings0, X, term__functor(F, As, C),
Bindings)
).
type_unify(term__functor(FX, AsX, _CX), term__functor(FY, AsY, _CY),
HeadTypeParams, Bindings0, Bindings) :-
list__length(AsX, ArityX),
list__length(AsY, ArityY),
(
FX = FY,
ArityX = ArityY
->
type_unify_list(AsX, AsY, HeadTypeParams, Bindings0, Bindings)
;
fail
).
% XXX Instead of just failing if the functors' name/arity is different,
% we should check here if these types have been defined
% to be equivalent using equivalence types. But this
% is difficult because the relevant variable
% TypeTable hasn't been passed in to here.
/*******
...
;
replace_eqv_type(FX, ArityX, AsX, EqvType)
->
type_unify(EqvType, term__functor(FY, AsY, CY),
HeadTypeParams, Bindings0, Bindings)
;
replace_eqv_type(FY, ArityY, AsY, EqvType)
->
type_unify(term__functor(FX, AsX, CX), EqvType,
HeadTypeParams, Bindings0, Bindings)
;
fail
).
:- pred replace_eqv_type(const, int, list(type), type).
:- mode replace_eqv_type(in, in, in, out) is semidet.
replace_eqv_type(Functor, Arity, Args, EqvType) :-
% XXX magically_obtain(TypeTable)
make_type_id(Functor, Arity, TypeId),
map__search(TypeTable, TypeId, TypeDefn),
get_type_defn_body(TypeDefn, TypeBody),
TypeBody = eqv_type(EqvType0),
get_type_defn_tparams(TypeDefn, TypeParams0),
type_param_to_var_list(TypeParams0, TypeParams),
term__substitute_corresponding(EqvType0, TypeParams, AsX,
EqvType).
******/
type_unify_list([], [], _) --> [].
type_unify_list([X | Xs], [Y | Ys], HeadTypeParams) -->
type_unify(X, Y, HeadTypeParams),
type_unify_list(Xs, Ys, HeadTypeParams).
:- pred type_unify_head_type_param(tvar, tvar, list(tvar), tsubst, tsubst).
:- mode type_unify_head_type_param(in, in, in, in, out) is semidet.
type_unify_head_type_param(Var, HeadVar, HeadTypeParams, Bindings0,
Bindings) :-
( map__search(Bindings0, Var, BindingOfVar) ->
BindingOfVar = term__variable(Var2),
type_unify_head_type_param(Var2, HeadVar, HeadTypeParams,
Bindings0, Bindings)
;
( Var = HeadVar ->
Bindings = Bindings0
;
\+ list__member(Var, HeadTypeParams),
map__det_insert(Bindings0, Var,
term__variable(HeadVar), Bindings)
)
).
%-----------------------------------------------------------------------------%
type_util__vars(Type, Tvars) :-
term__vars(Type, Tvars).
type_util__real_vars(Type, Tvars) :-
( is_introduced_type_info_type(Type) ->
% for these types, we don't add the type parameters
Tvars = []
;
type_util__vars(Type, Tvars)
).
%-----------------------------------------------------------------------------%
apply_substitution_to_type_map(VarTypes0, Subst, VarTypes) :-
% optimize the common case of an empty type substitution
( map__is_empty(Subst) ->
VarTypes = VarTypes0
;
map__keys(VarTypes0, Vars),
apply_substitution_to_type_map_2(Vars, VarTypes0, Subst,
VarTypes)
).
:- pred apply_substitution_to_type_map_2(list(prog_var)::in,
map(prog_var, type)::in, tsubst::in, map(prog_var, type)::out)
is det.
apply_substitution_to_type_map_2([], VarTypes, _Subst, VarTypes).
apply_substitution_to_type_map_2([Var | Vars], VarTypes0, Subst,
VarTypes) :-
map__lookup(VarTypes0, Var, VarType0),
term__apply_substitution(VarType0, Subst, VarType),
map__det_update(VarTypes0, Var, VarType, VarTypes1),
apply_substitution_to_type_map_2(Vars, VarTypes1, Subst, VarTypes).
%-----------------------------------------------------------------------------%
apply_rec_substitution_to_type_map(VarTypes0, Subst, VarTypes) :-
% optimize the common case of an empty type substitution
( map__is_empty(Subst) ->
VarTypes = VarTypes0
;
map__keys(VarTypes0, Vars),
apply_rec_substitution_to_type_map_2(Vars, VarTypes0, Subst,
VarTypes)
).
:- pred apply_rec_substitution_to_type_map_2(list(prog_var)::in,
map(prog_var, type)::in, tsubst::in, map(prog_var, type)::out)
is det.
apply_rec_substitution_to_type_map_2([], VarTypes, _Subst, VarTypes).
apply_rec_substitution_to_type_map_2([Var | Vars], VarTypes0, Subst,
VarTypes) :-
map__lookup(VarTypes0, Var, VarType0),
term__apply_rec_substitution(VarType0, Subst, VarType),
map__det_update(VarTypes0, Var, VarType, VarTypes1),
apply_rec_substitution_to_type_map_2(Vars, VarTypes1, Subst, VarTypes).
%-----------------------------------------------------------------------------%
apply_substitutions_to_var_map(VarMap0, TRenaming, TSubst, Subst, VarMap) :-
% optimize the common case of empty substitutions
(
map__is_empty(Subst),
map__is_empty(TSubst),
map__is_empty(TRenaming)
->
VarMap = VarMap0
;
map__keys(VarMap0, TVars),
map__init(NewVarMap),
apply_substitutions_to_var_map_2(TVars, VarMap0,
TRenaming, TSubst, Subst, NewVarMap, VarMap)
).
:- pred apply_substitutions_to_var_map_2(list(tvar)::in, map(tvar,
type_info_locn)::in, tsubst::in, map(tvar, type)::in,
map(prog_var, prog_var)::in, map(tvar, type_info_locn)::in,
map(tvar, type_info_locn)::out) is det.
apply_substitutions_to_var_map_2([], _VarMap0, _, _, _, NewVarMap, NewVarMap).
apply_substitutions_to_var_map_2([TVar | TVars], VarMap0, TRenaming,
TSubst, VarSubst, NewVarMap0, NewVarMap) :-
map__lookup(VarMap0, TVar, Locn),
type_info_locn_var(Locn, Var),
% find the new var, if there is one
( map__search(VarSubst, Var, NewVar0) ->
NewVar = NewVar0
;
NewVar = Var
),
type_info_locn_set_var(Locn, NewVar, NewLocn),
% find the new tvar, if there is one, otherwise just
% create the old var as a type variable.
(
map__search(TRenaming, TVar, NewTVar0)
->
( NewTVar0 = term__variable(NewTVar1) ->
NewTVar2 = NewTVar1
;
% varset__merge_subst only returns var->var mappings,
% never var->term.
error(
"apply_substitution_to_var_map_2: weird type renaming")
)
;
% The variable wasn't renamed.
NewTVar2 = TVar
),
term__apply_rec_substitution(term__variable(NewTVar2),
TSubst, NewType),
% if the tvar is still a variable, insert it into the
% map with the new var.
( type_util__var(NewType, NewTVar) ->
% Don't abort if two old type variables
% map to the same new type variable.
map__set(NewVarMap0, NewTVar, NewLocn, NewVarMap1)
;
NewVarMap1 = NewVarMap0
),
apply_substitutions_to_var_map_2(TVars, VarMap0, TRenaming,
TSubst, VarSubst, NewVarMap1, NewVarMap).
%-----------------------------------------------------------------------------%
apply_substitutions_to_typeclass_var_map(VarMap0,
TRenaming, TSubst, Subst, VarMap) :-
map__to_assoc_list(VarMap0, VarAL0),
list__map(apply_substitutions_to_typeclass_var_map_2(TRenaming,
TSubst, Subst), VarAL0, VarAL),
map__from_assoc_list(VarAL, VarMap).
:- pred apply_substitutions_to_typeclass_var_map_2(tsubst, map(tvar, type),
map(prog_var, prog_var), pair(class_constraint, prog_var),
pair(class_constraint, prog_var)).
:- mode apply_substitutions_to_typeclass_var_map_2(in, in,
in, in, out) is det.
apply_substitutions_to_typeclass_var_map_2(TRenaming, TSubst, VarRenaming,
Constraint0 - Var0, Constraint - Var) :-
apply_subst_to_constraint(TRenaming, Constraint0, Constraint1),
apply_rec_subst_to_constraint(TSubst, Constraint1, Constraint),
( map__search(VarRenaming, Var0, Var1) ->
Var = Var1
;
Var = Var0
).
%-----------------------------------------------------------------------------%
apply_rec_subst_to_constraints(Subst, Constraints0, Constraints) :-
Constraints0 = constraints(UnivCs0, ExistCs0),
apply_rec_subst_to_constraint_list(Subst, UnivCs0, UnivCs),
apply_rec_subst_to_constraint_list(Subst, ExistCs0, ExistCs),
Constraints = constraints(UnivCs, ExistCs).
apply_rec_subst_to_constraint_list(Subst, Constraints0, Constraints) :-
list__map(apply_rec_subst_to_constraint(Subst), Constraints0,
Constraints).
apply_rec_subst_to_constraint(Subst, Constraint0, Constraint) :-
Constraint0 = constraint(ClassName, Types0),
term__apply_rec_substitution_to_list(Types0, Subst, Types1),
% we need to maintain the invariant that types in class constraints
% do not have any information in their prog_context fields
strip_prog_contexts(Types1, Types),
Constraint = constraint(ClassName, Types).
apply_subst_to_constraints(Subst,
constraints(UniversalCs0, ExistentialCs0),
constraints(UniversalCs, ExistentialCs)) :-
apply_subst_to_constraint_list(Subst, UniversalCs0, UniversalCs),
apply_subst_to_constraint_list(Subst, ExistentialCs0, ExistentialCs).
apply_subst_to_constraint_list(Subst, Constraints0, Constraints) :-
list__map(apply_subst_to_constraint(Subst), Constraints0, Constraints).
apply_subst_to_constraint(Subst, Constraint0, Constraint) :-
Constraint0 = constraint(ClassName, Types0),
term__apply_substitution_to_list(Types0, Subst, Types),
Constraint = constraint(ClassName, Types).
apply_subst_to_constraint_proofs(Subst, Proofs0, Proofs) :-
map__init(Empty),
map__foldl(
lambda([Constraint0::in, Proof0::in, Map0::in, Map::out] is det,
(
apply_subst_to_constraint(Subst, Constraint0,
Constraint),
(
Proof0 = apply_instance(_),
Proof = Proof0
;
Proof0 = superclass(Super0),
apply_subst_to_constraint(Subst, Super0,
Super),
Proof = superclass(Super)
),
map__set(Map0, Constraint, Proof, Map)
)),
Proofs0, Empty, Proofs).
apply_rec_subst_to_constraint_proofs(Subst, Proofs0, Proofs) :-
map__init(Empty),
map__foldl(
lambda([Constraint0::in, Proof0::in, Map0::in, Map::out] is det,
(
apply_rec_subst_to_constraint(Subst, Constraint0,
Constraint),
(
Proof0 = apply_instance(_),
Proof = Proof0
;
Proof0 = superclass(Super0),
apply_rec_subst_to_constraint(Subst, Super0,
Super),
Proof = superclass(Super)
),
map__set(Map0, Constraint, Proof, Map)
)),
Proofs0, Empty, Proofs).
apply_variable_renaming_to_type_map(Renaming, Map0, Map) :-
map__map_values(
(pred(_::in, Type0::in, Type::out) is det :-
term__apply_variable_renaming(Type0, Renaming, Type)
), Map0, Map).
apply_variable_renaming_to_constraints(Renaming,
constraints(UniversalCs0, ExistentialCs0),
constraints(UniversalCs, ExistentialCs)) :-
apply_variable_renaming_to_constraint_list(Renaming,
UniversalCs0, UniversalCs),
apply_variable_renaming_to_constraint_list(Renaming,
ExistentialCs0, ExistentialCs).
apply_variable_renaming_to_constraint_list(Renaming, Constraints0,
Constraints) :-
list__map(apply_variable_renaming_to_constraint(Renaming),
Constraints0, Constraints).
apply_variable_renaming_to_constraint(Renaming, Constraint0, Constraint) :-
Constraint0 = constraint(ClassName, ClassArgTypes0),
term__apply_variable_renaming_to_list(ClassArgTypes0,
Renaming, ClassArgTypes),
Constraint = constraint(ClassName, ClassArgTypes).
%-----------------------------------------------------------------------------%
apply_partial_map_to_list([], _PartialMap, []).
apply_partial_map_to_list([X|Xs], PartialMap, [Y|Ys]) :-
( map__search(PartialMap, X, Y0) ->
Y = Y0
;
Y = X
),
apply_partial_map_to_list(Xs, PartialMap, Ys).
%-----------------------------------------------------------------------------%
strip_prog_contexts(Terms, StrippedTerms) :-
list__map(strip_prog_context, Terms, StrippedTerms).
strip_prog_context(term__variable(V), term__variable(V)).
strip_prog_context(term__functor(F, As0, _C0),
term__functor(F, As, C)) :-
term__context_init(C),
strip_prog_contexts(As0, As).
%-----------------------------------------------------------------------------%
cons_id_adjusted_arity(ModuleInfo, Type, ConsId) = AdjustedArity :-
% figure out the arity of this constructor,
% _including_ any type-infos or typeclass-infos
% inserted for existential data types.
cons_id_arity(ConsId, ConsArity),
(
type_util__get_existq_cons_defn(ModuleInfo, Type, ConsId,
ConsDefn)
->
ConsDefn = ctor_defn(_TVarSet, ExistQTVars, Constraints,
_ArgTypes, _ResultType),
list__length(Constraints, NumTypeClassInfos),
constraint_list_get_tvars(Constraints, ConstrainedTVars),
list__delete_elems(ExistQTVars, ConstrainedTVars,
UnconstrainedExistQTVars),
list__length(UnconstrainedExistQTVars, NumTypeInfos),
AdjustedArity = ConsArity + NumTypeClassInfos + NumTypeInfos
;
AdjustedArity = ConsArity
).
%-----------------------------------------------------------------------------%
constraint_list_get_tvars(Constraints, TVars) :-
list__map(constraint_get_tvars, Constraints, TVarsList),
list__condense(TVarsList, TVars).
constraint_get_tvars(constraint(_Name, Args), TVars) :-
term__vars_list(Args, TVars).
get_unconstrained_tvars(Tvars, Constraints, Unconstrained) :-
constraint_list_get_tvars(Constraints, ConstrainedTvars),
list__delete_elems(Tvars, ConstrainedTvars, Unconstrained0),
list__remove_dups(Unconstrained0, Unconstrained).
%-----------------------------------------------------------------------------%
|