/usr/share/doc/haskell98-report/html/haskell98-report-html/exps.html is in haskell98-report 20080907-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <title>The Haskell 98 Report: Expressions</title>
<body bgcolor="#ffffff"> <i>The Haskell 98 Report</i><br> <a href="index.html">top</a> | <a href="lexemes.html">back</a> | <a href="decls.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><hr>
<a name="expressions"></a><a name="sect3"></a>
<h2>3<tt> </tt>Expressions</h2>
<p>
In this chapter, we describe the syntax and informal semantics of
Haskell <I>expressions</I>, including their translations into the
Haskell kernel, where appropriate. Except in the case of <tt>let
</tt>expressions, these translations preserve both the static and dynamic
semantics. Free variables and constructors used in these translations
always refer to entities defined by the <tt>Prelude</tt>. For example,
"<tt>concatMap</tt>" used in the translation of list comprehensions
(Section <a href="exps.html#list-comprehensions">3.11</a>) means the <tt>concatMap</tt> defined by
the <tt>Prelude</tt>, regardless of whether or not the identifier "<tt>concatMap</tt>" is in
scope where the list comprehension is used, and (if it is in scope)
what it is bound to.<p>
In the syntax that follows, there are some families of nonterminals
indexed by precedence levels (written as a superscript). Similarly, the
nonterminals <I>op</I>, <I>varop</I>, and <I>conop</I> may have a double index:
a letter <I>l</I>, <I>r</I>, or <I>n</I> for left-, right- or non-associativity and
a precedence level. A precedence-level variable <I>i</I> ranges from 0 to 9;
an associativity variable <I>a</I> varies over <I>{l, r, n}</I>.
For example
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>(</tt> exp<sup>i+1</sup> qop<sup>(a,i)</sup> <tt>)
</tt></td></tr></table>
actually stands for 30 productions, with 10 substitutions for <I>i
</I>and 3 for <I>a</I>.<p>
<table cellspacing=0 cellspacing=0>
<tr><td width=100></td><td width=20></td><td width=250></td></tr><tr></tr><tr><td>
exp </td><td> <tt>-></tt> </td><td> exp<sup>0</sup> <tt>::</tt> [context <tt>=></tt>] type </td><td> (expression type signature)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> exp<sup>0</sup>
</td></tr><tr><td>
exp<sup>i</sup> </td><td> <tt>-></tt> </td><td> exp<sup>i+1</sup> [qop<sup>(n,i)</sup> exp<sup>i+1</sup>]
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> lexp<sup>i</sup>
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> rexp<sup>i</sup>
</td></tr><tr><td>
lexp<sup>i</sup> </td><td> <tt>-></tt> </td><td> (lexp<sup>i</sup> | exp<sup>i+1</sup>) qop<sup>(l,i)</sup> exp<sup>i+1</sup>
</td></tr><tr><td>
lexp<sup>6</sup> </td><td> <tt>-></tt> </td><td> <tt>-</tt> exp<sup>7</sup>
</td></tr><tr><td>
rexp<sup>i</sup> </td><td> <tt>-></tt> </td><td> exp<sup>i+1</sup> qop<sup>(r,i)</sup> (rexp<sup>i</sup> | exp<sup>i+1</sup>)
</td></tr><tr><td>
exp<sup>10</sup> </td><td> <tt>-></tt> </td><td> <tt>\</tt> apat<sub>1</sub> ... apat<sub>n</sub> <tt>-></tt> exp </td><td> (lambda abstraction, n>=1)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>let</tt> decls <tt>in</tt> exp </td><td> (let expression)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>if</tt> exp <tt>then</tt> exp <tt>else</tt> exp </td><td> (conditional)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>case</tt> exp <tt>of</tt> <tt>{</tt> alts <tt>}</tt> </td><td> (case expression)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>do</tt> <tt>{</tt> stmts <tt>}</tt> </td><td> (do expression)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> fexp
</td></tr><tr><td>
fexp </td><td> <tt>-></tt> </td><td> [fexp] aexp </td><td> (function application)
</td></tr><tr><td>
aexp </td><td> <tt>-></tt> </td><td> qvar </td><td> (variable)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> gcon </td><td> (general constructor)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> literal
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> exp <tt>)</tt> </td><td> (parenthesized expression)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> exp<sub>1</sub> <tt>,</tt> ... <tt>,</tt> exp<sub>k</sub> <tt>)</tt> </td><td> (tuple, k>=2)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>[</tt> exp<sub>1</sub> <tt>,</tt> ... <tt>,</tt> exp<sub>k</sub> <tt>]</tt> </td><td> (list, k>=1)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>[</tt> exp<sub>1</sub> [<tt>,</tt> exp<sub>2</sub>] <tt>..</tt> [exp<sub>3</sub>] <tt>]</tt> </td><td> (arithmetic sequence)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>[</tt> exp <tt>|</tt> qual<sub>1</sub> <tt>,</tt> ... <tt>,</tt> qual<sub>n</sub> <tt>]</tt> </td><td> (list comprehension, n>=1)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> exp<sup>i+1</sup> qop<sup>(a,i)</sup> <tt>)</tt> </td><td> (left section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> lexp<sup>i</sup> qop<sup>(l,i)</sup> <tt>)</tt> </td><td> (left section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> qop<sup>(a,i)</sup><sub><<tt>-</tt>></sub> exp<sup>i+1</sup> <tt>)</tt> </td><td> (right section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> qop<sup>(r,i)</sup><sub><<tt>-</tt>></sub> rexp<sup>i</sup> <tt>)</tt> </td><td> (right section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> qcon <tt>{</tt> fbind<sub>1</sub> <tt>,</tt> ... <tt>,</tt> fbind<sub>n</sub> <tt>}</tt> </td><td> (labeled construction, n>=0)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> aexp<sub><qcon></sub> <tt>{</tt> fbind<sub>1</sub> <tt>,</tt> ... <tt>,</tt> fbind<sub>n</sub> <tt>}</tt> </td><td> (labeled update, n >= 1)
</td></tr></table>
<p>
Expressions involving infix operators are disambiguated by the
operator's fixity (see Section <a href="decls.html#fixity">4.4.2</a>). Consecutive
unparenthesized operators with the same precedence must both be either
left or right associative to avoid a syntax error.
Given an unparenthesized expression "<I>x qop</I><sup><I>(a,i)</I></sup><I> y qop</I><sup><I>(b,j)</I></sup><I> z</I>", parentheses
must be added around either "<I>x qop</I><sup><I>(a,i)</I></sup><I> y</I>" or "<I>y qop</I><sup><I>(b,j)</I></sup>
<I>z</I>" when <I>i=j</I> unless <I>a=b=l</I> or <I>a=b=r</I>.<p>
Negation is the only prefix operator in
Haskell ; it has the same precedence as the infix <tt>-</tt> operator
defined in the Prelude (see Section <a href="decls.html#fixity">4.4.2</a>, Figure <a href="decls.html#prelude-fixities">4.1</a>).<p>
The grammar is ambiguous regarding the extent of lambda abstractions,
let expressions, and conditionals. The ambiguity is resolved by the
meta-rule that each of these constructs extends as far to the right as
possible. <p>
Sample parses are shown below.
<p>
<table >
<tr><td>
This </td><td> Parses as </td></tr><tr><td>
<tt>f x + g y</tt> </td><td> <tt>(f x) + (g y)</tt> </td></tr><tr><td><tt>- f x + y</tt> </td><td> <tt>(- (f x)) + y</tt> </td></tr><tr><td><tt>let { ... } in x + y</tt> </td><td> <tt>let { ... } in (x + y)</tt> </td></tr><tr><td><tt>z + let { ... } in x + y</tt> </td><td> <tt>z + (let { ... } in (x + y))</tt> </td></tr><tr><td><tt>f x y :: Int</tt> </td><td> <tt>(f x y) :: Int</tt> </td></tr><tr><td><tt>\ x -> a+b :: Int</tt> </td><td> <tt>\ x -> ((a+b) :: Int</tt>) </td></tr></table>
<p>
<p>
<I>A note about parsing.</I> Expressions that involve the interaction
of fixities with the let/lambda meta-rule
may be hard to parse. For example, the expression
<tt><br>
<br>
let x = True in x == x == True<br>
<br>
</tt>cannot possibly mean
<tt><br>
<br>
let x = True in (x == x == True)<br>
<br>
</tt>because <tt>(==)</tt> is a non-associative operator; so the expression must parse thus:
<tt><br>
<br>
(let x = True in (x == x)) == True<br>
<br>
</tt>However, implementations may well use a post-parsing pass to deal with fixities,
so they may well incorrectly deliver the former parse. Programmers are advised
to avoid constructs whose parsing involves an interaction of (lack of) associativity
with the let/lambda meta-rule.<p>
For the sake of clarity, the rest of this section shows the syntax of
expressions without their precedences.<a name="basic-errors"></a><p>
<a name="sect3.1"></a>
<h3>3.1<tt> </tt>Errors</h3>
Errors during expression evaluation, denoted by <I>_|_</I>,
are indistinguishable by a Haskell program from non-termination. Since Haskell is a
non-strict language, all Haskell types include <I>_|_</I>. That is, a value
of any type may be bound to a computation that, when demanded, results
in an error. When evaluated, errors cause immediate program
termination and cannot be caught by the user. The Prelude provides
two functions to directly
cause such errors:
<tt><br>
<br>
error :: String -> a<br>
undefined :: a<br>
<br>
</tt>A call to <tt>error</tt> terminates execution of
the program and returns an appropriate error indication to the
operating system. It should also display the string in some
system-dependent manner. When <tt>undefined</tt> is used, the error message
is created by the compiler.<p>
Translations of Haskell expressions use <tt>error</tt> and <tt>undefined</tt> to
explicitly indicate where execution time errors may occur. The actual
program behavior when an error occurs is up to the implementation.
The messages passed to the <tt>error</tt> function in these translations are
only suggestions; implementations may choose to display more or less
information when an error occurs.<a name="vars-and-lits"></a><p>
<a name="sect3.2"></a>
<h3>3.2<tt> </tt>Variables, Constructors, Operators, and Literals</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> qvar </td><td> (variable)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> gcon </td><td> (general constructor)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> literal
</td></tr></table>
<table cellspacing=0 cellspacing=0>
<tr><td width=100></td><td width=20></td><td width=250></td></tr><tr><td>
gcon </td><td> <tt>-></tt> </td><td> <tt>()
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>[]
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(,</tt>{<tt>,</tt>}<tt>)
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> qcon
</td></tr><tr><td>
var </td><td> <tt>-></tt> </td><td> varid | <tt>(</tt> varsym <tt>)</tt> </td><td> (variable)
</td></tr><tr><td>
qvar </td><td> <tt>-></tt> </td><td> qvarid | <tt>(</tt> qvarsym <tt>)</tt> </td><td> (qualified variable)
</td></tr><tr><td>
con </td><td> <tt>-></tt> </td><td> conid | <tt>(</tt> consym <tt>)</tt> </td><td> (constructor)
</td></tr><tr><td>
qcon </td><td> <tt>-></tt> </td><td> qconid | <tt>(</tt> gconsym <tt>)</tt> </td><td> (qualified constructor)
</td></tr><tr><td>
varop </td><td> <tt>-></tt> </td><td> varsym | `varid `</td><td> (variable operator)
</td></tr><tr><td>
qvarop </td><td> <tt>-></tt> </td><td> qvarsym | `qvarid `</td><td> (qualified variable operator)
</td></tr><tr><td>
conop </td><td> <tt>-></tt> </td><td> consym | `conid `</td><td> (constructor operator)
</td></tr><tr><td>
qconop </td><td> <tt>-></tt> </td><td> gconsym | `qconid `</td><td> (qualified constructor operator)
</td></tr><tr><td>
op </td><td> <tt>-></tt> </td><td> varop | conop </td><td> (operator)
</td></tr><tr><td>
qop </td><td> <tt>-></tt> </td><td> qvarop | qconop </td><td> (qualified operator)
</td></tr><tr><td>
gconsym </td><td> <tt>-></tt> </td><td> <tt>:</tt> | qconsym
</td></tr></table>
<p>
Haskell provides special syntax to support infix notation.
An <I>operator</I> is a function that can be applied using infix
syntax (Section <a href="exps.html#operators">3.4</a>), or partially applied using a
<I>section</I> (Section <a href="exps.html#sections">3.5</a>).<p>
An <I>operator</I> is either an <I>operator symbol</I>, such as <tt>+</tt> or <tt>$$</tt>,
or is an ordinary identifier enclosed in grave accents (backquotes), such
as `<tt>op</tt>`. For example, instead of writing the prefix application
<tt>op x y</tt>, one can write the infix application <tt>x</tt> `<tt>op</tt>`<tt> y</tt>.
If no fixity
declaration is given for `<tt>op</tt>` then it defaults
to highest precedence and left associativity
(see Section <a href="decls.html#fixity">4.4.2</a>).<p>
Dually, an operator symbol can be converted to an ordinary identifier
by enclosing it in parentheses. For example, <tt>(+) x y</tt> is equivalent
to <tt>x + y</tt>, and <tt>foldr (*) 1 xs</tt> is equivalent to <tt>foldr (\x y -> x*y) 1 xs</tt>.<p>
Special syntax is used to name some constructors for some of the
built-in types, as found
in the production for <I>gcon</I> and <I>literal</I>. These are described
in Section <a href="basic.html#basic-types">6.1</a>.<p>
An integer literal represents the
application of the function <tt>fromInteger</tt> to the
appropriate value of type
<tt>Integer</tt>. Similarly, a floating point literal stands for an application of
<tt>fromRational</tt> to a value of type <tt>Rational</tt> (that is,
<tt>Ratio Integer</tt>).<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
The integer literal <I>i</I> is equivalent to <tt>fromInteger</tt> <I>i</I>,
where <tt>fromInteger</tt> is a method in class <tt>Num</tt> (see Section
<a href="basic.html#numeric-literals">6.4.1</a>).<p>
The floating point literal <I>f</I> is equivalent to <tt>fromRational
</tt>(<I>n</I> <tt>Ratio.%</tt> <I>d</I>), where <tt>fromRational</tt> is a method in class <tt>Fractional
</tt>and <tt>Ratio.%</tt> constructs a rational from two integers, as defined in
the <tt>Ratio</tt> library.
The integers <I>n</I> and <I>d</I> are chosen so that <I>n/d = f</I>.
</td></tr></table>
<a name="applications"></a><p>
<a name="sect3.3"></a>
<h3>3.3<tt> </tt>Curried Applications and Lambda Abstractions</h3>
<a name="lambda-abstractions"></a>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
fexp </td><td width=20> <tt>-></tt> </td><td width=250> [fexp] aexp </td><td> (function application)
</td></tr><tr><td>
exp </td><td> <tt>-></tt> </td><td> <tt>\</tt> apat<sub>1</sub> ... apat<sub>n</sub> <tt>-></tt> exp </td><td> (lambda abstraction, n>=1)
</td></tr></table>
<p>
<I>Function application</I> is written
<I>e</I><sub><I>1</I></sub><I> e</I><sub><I>2</I></sub>. Application associates to the left, so the
parentheses may be omitted in <tt>(f x) y</tt>. Because <I>e</I><sub><I>1</I></sub> could
be a data constructor, partial applications of data constructors are
allowed. <p>
<I>Lambda abstractions</I> are written
<tt>\</tt><I> p</I><sub><I>1</I></sub><I> ... p</I><sub><I>n</I></sub><I> </I><tt>-></tt><I> e</I>, where the <I>p</I><sub><I>i</I></sub> are <I>patterns</I>.
An expression such as <tt>\x:xs->x</tt> is syntactically incorrect;
it may legally be written as <tt>\(x:xs)->x</tt>.<p>
The set of patterns must be <I>linear</I>---no variable may appear more than once in the set.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
The following identity holds:
<div align=center><table >
<tr><td><tt>\</tt><I> p</I><sub><I>1</I></sub><I> ... p</I><sub><I>n</I></sub><I> </I><tt>-></tt><I> e
</I> </td><td align=center> <I>=</I> </td><td>
<tt>\</tt><I> x</I><sub><I>1</I></sub><I> ... x</I><sub><I>n</I></sub><I> </I><tt>-> case (</tt><I>x</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> x</I><sub><I>n</I></sub><tt>) of (</tt><I>p</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> p</I><sub><I>n</I></sub><tt>) -></tt><I> e
</I></td></tr></table>
</div>
where the <I>x</I><sub><I>i</I></sub> are new identifiers.
</td></tr></table>
Given this translation combined with the semantics of case
expressions and pattern matching described in
Section <a href="exps.html#case-semantics">3.17.3</a>, if the
pattern fails to match, then the result is <I>_|_</I>.<p>
<a name="operators"></a>
<a name="sect3.4"></a>
<h3>3.4<tt> </tt>Operator Applications</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
exp </td><td width=20> <tt>-></tt> </td><td width=250> exp<sub>1</sub> qop exp<sub>2</sub>
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>-</tt> exp </td><td> (prefix negation)
</td></tr><tr><td>
qop </td><td> <tt>-></tt> </td><td> qvarop | qconop </td><td> (qualified operator)
</td></tr></table>
<p>
The form <I>e</I><sub><I>1</I></sub><I> qop e</I><sub><I>2</I></sub> is the infix application of binary
operator <I>qop</I> to expressions <I>e</I><sub><I>1</I></sub> and <I>e</I><sub><I>2</I></sub>. <p>
The special
form <tt>-</tt><I>e</I> denotes prefix negation, the only
prefix operator in Haskell , and is
syntax for <tt>negate </tt><I>(e)</I>. The binary <tt>-</tt> operator
does not necessarily refer
to the definition of <tt>-</tt> in the Prelude; it may be rebound
by the module system. However, unary <tt>-</tt> will always refer to the
<tt>negate</tt> function defined in the Prelude. There is no link between
the local meaning of the <tt>-</tt> operator and unary negation.<p>
Prefix negation has the same precedence as the infix operator <tt>-
</tt>defined in the Prelude (see
Table <a href="decls.html#prelude-fixities">4.1</a>). Because <tt>e1-e2</tt> parses as an
infix application of the binary operator <tt>-</tt>, one must write <tt>e1(-e2)</tt> for
the alternative parsing. Similarly, <tt>(-)</tt> is syntax for
<tt>(\ x y -> x-y)</tt>, as with any infix operator, and does not denote
<tt>(\ x -> -x)</tt>---one must use <tt>negate</tt> for that.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
The following identities hold:
<div align=center><table >
<tr><td><I>e</I><sub><I>1</I></sub><I> op e</I><sub><I>2</I></sub> </td><td align=center> <I>=</I> </td><td> <tt>(</tt><I>op</I><tt>)</tt><I> e</I><sub><I>1</I></sub><I> e</I><sub><I>2</I></sub> </td></tr><tr><td><tt>-</tt><I>e</I> </td><td align=center> <I>=</I> </td><td> <tt>negate</tt><I> (e)
</I></td></tr></table>
</div>
</td></tr></table>
<a name="sections"></a><p>
<a name="sect3.5"></a>
<h3>3.5<tt> </tt>Sections</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>(</tt> exp<sup>i+1</sup> qop<sup>(a,i)</sup> <tt>)</tt> </td><td> (left section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> lexp<sup>i</sup> qop<sup>(l,i)</sup> <tt>)</tt> </td><td> (left section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> qop<sup>(a,i)</sup><sub><<tt>-</tt>></sub> exp<sup>i+1</sup> <tt>)</tt> </td><td> (right section)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> qop<sup>(r,i)</sup><sub><<tt>-</tt>></sub> rexp<sup>i</sup> <tt>)</tt> </td><td> (right section)
</td></tr></table>
<p>
<I>Sections</I> are written as <tt>(</tt><I> op e </I><tt>)</tt> or <tt>(</tt><I> e op </I><tt>)</tt>, where
<I>op</I> is a binary operator and <I>e</I> is an expression. Sections are a
convenient syntax for partial application of binary operators.<p>
Syntactic precedence rules apply to sections as follows.
<tt>(</tt><I>op e</I><tt>)</tt> is legal if and only if <tt>(x</tt><I> op e</I><tt>)</tt> parses
in the same way as <tt>(x</tt><I> op </I><tt>(</tt><I>e</I><tt>))</tt>;
and similarly for <tt>(</tt><I>e op</I><tt>)</tt>.
For example, <tt>(*a+b)</tt> is syntactically invalid, but <tt>(+a*b)</tt> and
<tt>(*(a+b))</tt> are valid. Because <tt>(+)</tt> is left associative, <tt>(a+b+)</tt> is syntactically correct,
but <tt>(+a+b)</tt> is not; the latter may legally be written as <tt>(+(a+b))</tt>.
As another example, the expression
<tt><br>
<br>
(let n = 10 in n +)<br>
<br>
</tt>is invalid because, by the let/lambda meta-rule (Section <a href="exps.html#expressions">3</a>),
the expression
<tt><br>
<br>
(let n = 10 in n + x)<br>
<br>
</tt>parses as
<tt><br>
<br>
(let n = 10 in (n + x))<br>
<br>
</tt>rather than
<tt><br>
<br>
((let n = 10 in n) + x)<br>
<br>
<p>
</tt>Because <tt>-</tt> is treated specially in the grammar,
<tt>(-</tt><I> exp</I><tt>)</tt> is not a section, but an application of prefix
negation, as
described in the preceding section. However, there is a <tt>subtract
</tt>function defined in the Prelude such that
<tt>(subtract</tt><I> exp</I><tt>)</tt> is equivalent to the disallowed section.
The expression <tt>(+ (-</tt><I> exp</I><tt>))</tt> can serve the same purpose.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
The following identities hold:
<div align=center><table >
<tr><td><tt>(</tt><I>op e</I><tt>)</tt> </td><td align=center> <I>=</I> </td><td> <tt>\</tt><I> x </I><tt>-></tt><I> x op e</I> </td></tr><tr><td><tt>(</tt><I>e op</I><tt>)</tt> </td><td align=center> <I>=</I> </td><td> <tt>\</tt><I> x </I><tt>-></tt><I> e op x
</I></td></tr></table>
</div>
where <I>op</I> is a binary operator, <I>e</I> is an expression, and <I>x</I> is a variable
that does not occur free in <I>e</I>.
</td></tr></table>
<a name="conditionals"></a><p>
<a name="sect3.6"></a>
<h3>3.6<tt> </tt>Conditionals</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
exp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>if</tt> exp<sub>1</sub> <tt>then</tt> exp<sub>2</sub> <tt>else</tt> exp<sub>3</sub>
</td></tr></table>
<p>
A <I>conditional expression
</I>has the form
<tt>if</tt><I> e</I><sub><I>1</I></sub><I> </I><tt>then</tt><I> e</I><sub><I>2</I></sub><I> </I><tt>else</tt><I> e</I><sub><I>3</I></sub> and returns the value of <I>e</I><sub><I>2</I></sub> if the
value of <I>e</I><sub><I>1</I></sub> is <tt>True</tt>, <I>e</I><sub><I>3</I></sub> if <I>e</I><sub><I>1</I></sub> is <tt>False</tt>, and <I>_|_
</I>otherwise.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
The following identity holds:
<div align=center><table >
<tr><td><tt>if</tt><I> e</I><sub><I>1</I></sub><I> </I><tt>then</tt><I> e</I><sub><I>2</I></sub><I> </I><tt>else</tt><I> e</I><sub><I>3</I></sub> </td><td align=center> <I>=</I> </td><td> <tt>case</tt><I> e</I><sub><I>1</I></sub><I> </I><tt>of { True -></tt><I> e</I><sub><I>2</I></sub><I> </I><tt>; False -></tt><I> e</I><sub><I>3</I></sub><I> </I><tt>}
</tt></td></tr></table>
</div>
where <tt>True</tt> and <tt>False</tt> are the two nullary constructors from the
type <tt>Bool</tt>, as defined in the Prelude. The type of <I>e</I><sub><I>1</I></sub> must be <tt>Bool</tt>;
<I>e</I><sub><I>2</I></sub> and <I>e</I><sub><I>3</I></sub> must have the same type, which is also the type of the
entire conditional expression.
</td></tr></table>
<a name="lists"></a><p>
<a name="sect3.7"></a>
<h3>3.7<tt> </tt>Lists</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
exp </td><td width=20> <tt>-></tt> </td><td width=250> exp<sub>1</sub> qop exp<sub>2</sub>
</td></tr><tr><td>
aexp </td><td> <tt>-></tt> </td><td> <tt>[</tt> exp<sub>1</sub> <tt>,</tt> ... <tt>,</tt> exp<sub>k</sub> <tt>]</tt> </td><td> (k>=1)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> gcon
</td></tr><tr><td>
gcon </td><td> <tt>-></tt> </td><td> <tt>[]
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> qcon
</td></tr><tr><td>
qcon </td><td> <tt>-></tt> </td><td> <tt>(</tt> gconsym <tt>)
</tt></td></tr><tr><td>
qop </td><td> <tt>-></tt> </td><td> qconop
</td></tr><tr><td>
qconop </td><td> <tt>-></tt> </td><td> gconsym
</td></tr><tr><td>
gconsym </td><td> <tt>-></tt> </td><td> <tt>:
</tt></td></tr></table>
<p>
<I>Lists</I> are written <tt>[</tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> e</I><sub><I>k</I></sub><tt>]</tt>, where
<I>k>=1</I>. The list constructor is <tt>:</tt>, and the empty list is denoted <tt>[]</tt>.
Standard operations on
lists are given in the Prelude (see Section <a href="basic.html#basic-lists">6.1.3</a>, and
Chapter <a href="standard-prelude.html#stdprelude">8</a> notably Section <a href="standard-prelude.html#preludelist">8.1</a>).<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
The following identity holds:
<div align=center><table >
<tr><td><tt>[</tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> e</I><sub><I>k</I></sub><tt>]</tt> </td><td align=center> <I>=</I> </td><td> <I>e</I><sub><I>1</I></sub><I> </I><tt>: (</tt><I>e</I><sub><I>2</I></sub><I> </I><tt>: (</tt><I> ... </I><tt>(</tt><I>e</I><sub><I>k</I></sub><I> </I><tt>: [])))
</tt></td></tr></table>
</div>
where <tt>:</tt> and <tt>[]</tt> are constructors for lists, as defined in
the Prelude (see Section <a href="basic.html#basic-lists">6.1.3</a>). The types
of <I>e</I><sub><I>1</I></sub> through <I>e</I><sub><I>k</I></sub> must all be the same (call it <I>t</I>), and the
type of the overall expression is <tt>[</tt><I>t</I><tt>]</tt> (see Section <a href="decls.html#type-syntax">4.1.2</a>).
</td></tr></table>
The constructor "<tt>:</tt>" is reserved solely for list construction; like
<tt>[]</tt>, it is considered part of the language syntax, and cannot be hidden or redefined.
It is a right-associative operator, with precedence level 5 (Section <a href="decls.html#fixity">4.4.2</a>).<a name="tuples"></a><p>
<a name="sect3.8"></a>
<h3>3.8<tt> </tt>Tuples</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100></td><td width=20></td><td width=250></td></tr><tr><td>
aexp </td><td> <tt>-></tt> </td><td> <tt>(</tt> exp<sub>1</sub> <tt>,</tt> ... <tt>,</tt> exp<sub>k</sub> <tt>)</tt> </td><td> (k>=2)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> qcon
</td></tr><tr><td>
qcon </td><td> <tt>-></tt> </td><td> <tt>(,</tt>{<tt>,</tt>}<tt>)
</tt></td></tr></table>
<p>
<I>Tuples</I> are written <tt>(</tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> e</I><sub><I>k</I></sub><tt>)</tt>, and may be
of arbitrary length <I>k>=2</I>. The constructor for an <I>n</I>-tuple is denoted by
<tt>(,</tt>...<tt>,)</tt>, where there are <I>n-1</I> commas. Thus <tt>(a,b,c)</tt> and
<tt>(,,) a b c</tt> denote the same value.
Standard operations on tuples are given
in the Prelude (see Section <a href="basic.html#basic-tuples">6.1.4</a> and Chapter <a href="standard-prelude.html#stdprelude">8</a>).<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
<tt>(</tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> e</I><sub><I>k</I></sub><tt>)</tt> for <I>k>=2</I> is an instance of a <I>k</I>-tuple as
defined in the Prelude, and requires no translation. If
<I>t</I><sub><I>1</I></sub> through <I>t</I><sub><I>k</I></sub> are the types of <I>e</I><sub><I>1</I></sub> through <I>e</I><sub><I>k</I></sub>,
respectively, then the type of the resulting tuple is
<tt>(</tt><I>t</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> t</I><sub><I>k</I></sub><tt>)</tt> (see Section <a href="decls.html#type-syntax">4.1.2</a>).
</td></tr></table>
<a name="unit-expression"></a><p>
<a name="sect3.9"></a>
<h3>3.9<tt> </tt>Unit Expressions and Parenthesized Expressions</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> gcon
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> exp <tt>)
</tt></td></tr><tr><td>
gcon </td><td> <tt>-></tt> </td><td> <tt>()
</tt></td></tr></table>
<p>
The form <tt>(</tt><I>e</I><tt>)</tt> is simply a <I>parenthesized expression</I>, and is
equivalent to <I>e</I>. The <I>unit expression</I> <tt>()</tt> has type
<tt>()</tt> (see
Section <a href="decls.html#type-syntax">4.1.2</a>). It is the only member of that type apart
from _|_, and can
be thought of as the "nullary tuple" (see Section <a href="basic.html#basic-trivial">6.1.5</a>).<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
<tt>(</tt><I>e</I><tt>)</tt> is equivalent to <I>e</I>.
</td></tr></table>
<a name="arithmetic-sequences"></a><p>
<a name="sect3.10"></a>
<h3>3.10<tt> </tt>Arithmetic Sequences</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>[</tt> exp<sub>1</sub> [<tt>,</tt> exp<sub>2</sub>] <tt>..</tt> [exp<sub>3</sub>] <tt>]</tt>
</td></tr></table>
<p>
The <I>arithmetic sequence
</I><tt>[</tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I> e</I><sub><I>2</I></sub><I> </I><tt>..</tt><I> e</I><sub><I>3</I></sub><tt>]</tt> denotes a list of values of
type <I>t</I>, where each of the <I>e</I><sub><I>i</I></sub> has type <I>t</I>, and <I>t</I> is an
instance of class <tt>Enum</tt>.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
Arithmetic sequences satisfy these identities:
<div align=center><table >
<tr><td><tt>[ </tt><I>e</I><sub><I>1</I></sub><tt>.. ]</tt> </td><td align=center> <I>=</I>
</td><td> <tt>enumFrom</tt> <I>e</I><sub><I>1</I></sub> </td></tr><tr><td><tt>[ </tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I>e</I><sub><I>2</I></sub><tt>.. ]</tt> </td><td align=center> <I>=</I>
</td><td> <tt>enumFromThen</tt> <I>e</I><sub><I>1</I></sub> <I>e</I><sub><I>2</I></sub> </td></tr><tr><td><tt>[ </tt><I>e</I><sub><I>1</I></sub><tt>..</tt><I>e</I><sub><I>3</I></sub><tt> ]</tt> </td><td align=center> <I>=</I>
</td><td> <tt>enumFromTo</tt> <I>e</I><sub><I>1</I></sub> <I>e</I><sub><I>3</I></sub> </td></tr><tr><td><tt>[ </tt><I>e</I><sub><I>1</I></sub><tt>,</tt><I>e</I><sub><I>2</I></sub><tt>..</tt><I>e</I><sub><I>3</I></sub><tt> ]</tt>
</td><td align=center> <I>=</I>
</td><td> <tt>enumFromThenTo</tt> <I>e</I><sub><I>1</I></sub> <I>e</I><sub><I>2</I></sub> <I>e</I><sub><I>3</I></sub>
</td></tr></table>
</div>
where <tt>enumFrom</tt>, <tt>enumFromThen</tt>, <tt>enumFromTo</tt>, and <tt>enumFromThenTo
</tt>are class methods in the class <tt>Enum</tt> as defined in the Prelude
(see Figure <a href="basic.html#standard-classes">6.1</a>).
</td></tr></table>
<p>
The semantics of arithmetic sequences therefore depends entirely
on the instance declaration for the type <I>t</I>.
See Section <a href="basic.html#enum-class">6.3.4</a> for more details of which <tt>Prelude
</tt>types are in <tt>Enum</tt> and their semantics.<a name="list-comprehensions"></a><p>
<a name="sect3.11"></a>
<h3>3.11<tt> </tt>List Comprehensions</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>[</tt> exp <tt>|</tt> qual<sub>1</sub> <tt>,</tt> ... <tt>,</tt> qual<sub>n</sub> <tt>]</tt> </td><td> (list comprehension, n>=1)
</td></tr><tr><td>
qual </td><td> <tt>-></tt> </td><td> pat <tt><-</tt> exp </td><td> (generator)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>let</tt> decls </td><td> (local declaration)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> exp </td><td> (guard)
</td></tr></table>
<p>
A <I>list comprehension</I> has the form <tt>[</tt><I> e </I><tt>|</tt><I> q</I><sub><I>1</I></sub><tt>,</tt><I> ...</I><tt>,</tt><I> q</I><sub><I>n</I></sub><I> </I><tt>]</tt><I>,
n>=1,</I> where the <I>q</I><sub><I>i</I></sub> qualifiers are either
<UL><LI><I>generators</I> of the form <I>p </I><tt><-</tt><I> e</I>, where
<I>p</I> is a
pattern (see Section <a href="exps.html#pattern-matching">3.17</a>) of type <I>t</I> and <I>e</I> is an
expression of type <tt>[</tt><I>t</I><tt>]
</tt><LI><I>guards</I>, which are arbitrary expressions of
type <tt>Bool</tt>
<LI><I>local bindings</I> that provide new definitions for use in
the generated expression <I>e</I> or subsequent guards and generators.
</UL><p>
Such a list comprehension returns the list of elements
produced by evaluating <I>e</I> in the successive environments
created by the nested, depth-first evaluation of the generators in the
qualifier list. Binding of variables occurs according to the normal
pattern matching rules (see Section <a href="exps.html#pattern-matching">3.17</a>), and if a
match fails then that element of the list is simply skipped over. Thus:
<tt><br>
<br>
[ x | xs <- [ [(1,2),(3,4)], [(5,4),(3,2)] ], <br>
(3,x) <- xs ]<br>
<br>
</tt>yields the list <tt>[4,2]</tt>. If a qualifier is a guard, it must evaluate
to <tt>True</tt> for the previous pattern match to succeed.
As usual, bindings in list comprehensions can shadow those in outer
scopes; for example:
<p>
<table >
<tr><td>
<tt>[ x | x <- x, x <- x ]</tt> </td><td> = </td><td> <tt>[ z | y <- x, z <- y]</tt> </td></tr></table>
<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
List comprehensions satisfy these identities, which may be
used as a translation into the kernel:
<div align=center><table >
<tr><td><tt>[ </tt><I> e</I><tt> | True ]</tt> </td><td align=center> = </td><td> <tt>[</tt><I>e</I><tt>]</tt> </td></tr><tr><td><tt>[ </tt><I> e</I><tt> | </tt><I>q</I><tt> ]</tt> </td><td align=center> = </td><td> <tt>[</tt><I> e </I><tt>|</tt><I> q</I><tt>, True ]</tt> </td></tr><tr><td><tt>[ </tt><I> e</I><tt> | </tt><I>b</I><tt>,</tt><I> Q </I><tt>]</tt> </td><td align=center> = </td><td>
<tt>if</tt><I> b </I><tt>then</tt><I> </I><tt>[ </tt><I> e</I><tt> | </tt><I>Q</I><tt> ]</tt><I> </I><tt>else []</tt> </td></tr><tr><td><tt>[ </tt><I> e</I><tt> | </tt><I>p </I><tt><-</tt><I> l</I><tt>,</tt><I> Q</I><tt> ]</tt> </td><td align=center> = </td><td>
<tt>let ok</tt><I> p </I><tt>=</tt><I> </I><tt>[ </tt><I> e</I><tt> | </tt><I>Q</I><tt> ]</tt> </td></tr><tr><td></td><td align=center></td><td> <tt> ok _ = []</tt> </td></tr><tr><td></td><td align=center></td><td> <tt>in concatMap ok</tt><I> l</I> </td></tr><tr><td><tt>[ </tt><I> e</I><tt> | let</tt><I> decls</I><tt>,</tt><I> Q</I><tt> ]</tt> </td><td align=center> = </td><td>
<tt>let</tt><I> decls </I><tt>in</tt><I> </I><tt>[ </tt><I> e</I><tt> | </tt><I>Q</I><tt> ]
</tt></td></tr></table>
</div>
where <I>e</I> ranges over expressions, <I>p</I> over
patterns, <I>l</I> over list-valued expressions, <I>b</I> over
boolean expressions, <I>decls</I> over declaration lists,
<I>q</I> over qualifiers, and <I>Q</I> over sequences of qualifiers. <tt>ok</tt> is a fresh variable.
The function <tt>concatMap</tt>, and boolean value <tt>True</tt>, are defined in the Prelude.
</td></tr></table>
<p>
As indicated by the translation of list comprehensions, variables
bound by <tt>let</tt> have fully polymorphic types while those defined by
<tt><-</tt> are lambda bound and are thus monomorphic (see Section
<a href="decls.html#monomorphism">4.5.4</a>).<a name="let-expressions"></a><p>
<a name="sect3.12"></a>
<h3>3.12<tt> </tt>Let Expressions</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
exp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>let</tt> decls <tt>in</tt> exp
</td></tr></table>
<p>
<I>Let expressions</I> have the general form
<tt>let {</tt><I> d</I><sub><I>1</I></sub><I> </I><tt>;</tt><I> ... </I><tt>;</tt><I> d</I><sub><I>n</I></sub><I> </I><tt>} in</tt><I> e</I>,
and introduce a
nested, lexically-scoped,
mutually-recursive list of declarations (<tt>let</tt> is often called <tt>letrec</tt> in
other languages). The scope of the declarations is the expression <I>e
</I>and the right hand side of the declarations. Declarations are
described in Chapter <a href="decls.html#declarations">4</a>. Pattern bindings are matched
lazily; an implicit <tt>~</tt> makes these patterns
irrefutable.
For example,
<tt><br>
<br>
let (x,y) = undefined in </tt><I>e</I><tt><br>
<br>
</tt>does not cause an execution-time error until <tt>x</tt> or <tt>y</tt> is evaluated.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3> The dynamic semantics of the expression
<tt>let {</tt><I> d</I><sub><I>1</I></sub><I> </I><tt>;</tt><I> ... </I><tt>;</tt><I> d</I><sub><I>n</I></sub><I> </I><tt>} in</tt><I> e</I><sub><I>0</I></sub> are captured by this
translation: After removing all type signatures, each
declaration <I>d</I><sub><I>i</I></sub> is translated into an equation of the form
<I>p</I><sub><I>i</I></sub><I> </I><tt>=</tt><I> e</I><sub><I>i</I></sub>, where <I>p</I><sub><I>i</I></sub> and <I>e</I><sub><I>i</I></sub> are patterns and expressions
respectively, using the translation in
Section <a href="decls.html#function-bindings">4.4.3</a>. Once done, these identities
hold, which may be used as a translation into the kernel:
<div align=center><table >
<tr><td><tt>let {</tt><I>p</I><sub><I>1</I></sub><tt>=</tt><I>e</I><sub><I>1</I></sub><tt>; </tt> ... <tt>; </tt><I>p</I><sub><I>n</I></sub><tt>=</tt><I>e</I><sub><I>n</I></sub><tt>} in</tt> <I>e</I><sub><I>0</I></sub>
</td><td align=center>=</td><td> <tt>let (~</tt><I>p</I><sub><I>1</I></sub><tt>,</tt> ... <tt>,~</tt><I>p</I><sub><I>n</I></sub><tt>) = (</tt><I>e</I><sub><I>1</I></sub><tt>,</tt> ... <tt>,</tt><I>e</I><sub><I>n</I></sub><tt>) in</tt> <I>e</I><sub><I>0</I></sub> </td></tr><tr><td><tt>let </tt><I>p</I><tt> = </tt><I>e</I><sub><I>1</I></sub> <tt> in </tt> <I>e</I><sub><I>0</I></sub>
</td><td align=center>=</td><td> <tt>case </tt><I>e</I><sub><I>1</I></sub><tt> of ~</tt><I>p</I><tt> -> </tt><I>e</I><sub><I>0</I></sub> </td></tr><tr><td></td><td align=center> </td><td> where no variable in <I>p</I> appears free in <I>e</I><sub><I>1</I></sub> </td></tr><tr><td><tt>let </tt><I>p</I><tt> = </tt><I>e</I><sub><I>1</I></sub> <tt> in </tt> <I>e</I><sub><I>0</I></sub>
</td><td align=center>=</td><td> <tt>let </tt><I>p</I><tt> = fix ( \ ~</tt><I>p</I><tt> -> </tt><I>e</I><sub><I>1</I></sub><tt>) in</tt> <I>e</I><sub><I>0</I></sub>
</td></tr></table>
</div>
where <tt>fix</tt> is the least fixpoint operator. Note the use of the
irrefutable patterns <tt>~</tt><I>p</I>. This translation
does not preserve the static semantics because the use of <tt>case
</tt>precludes a fully polymorphic typing of the bound variables.
The static semantics of the bindings in a <tt>let</tt> expression
are described in
Section <a href="decls.html#pattern-bindings">4.4.3</a>.
</td></tr></table>
<a name="case"></a><p>
<a name="sect3.13"></a>
<h3>3.13<tt> </tt>Case Expressions</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100></td><td width=20></td><td width=250></td></tr><tr><td>
exp </td><td> <tt>-></tt> </td><td> <tt>case</tt> exp <tt>of</tt> <tt>{</tt> alts <tt>}
</tt></td></tr><tr><td>
alts </td><td> <tt>-></tt> </td><td> alt<sub>1</sub> <tt>;</tt> ... <tt>;</tt> alt<sub>n</sub> </td><td> (n>=1)
</td></tr><tr><td>
alt </td><td> <tt>-></tt> </td><td> pat <tt>-></tt> exp [<tt>where</tt> decls]
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> pat gdpat [<tt>where</tt> decls]
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> </td><td> (empty alternative)
</td></tr><tr><td>
gdpat </td><td> <tt>-></tt> </td><td> gd <tt>-></tt> exp [ gdpat ]
</td></tr><tr><td>
gd </td><td> <tt>-></tt> </td><td> <tt>|</tt> exp<sup>0</sup>
</td></tr></table>
<p>
A <I>case expression</I> has the general form
<p>
<tt>case</tt><I> e </I><tt>of { </tt><I>p</I><sub><I>1</I></sub><I> match</I><sub><I>1</I></sub><I> </I><tt>;</tt><I> ... </I><tt>;</tt><I> p</I><sub><I>n</I></sub><I> match</I><sub><I>n</I></sub><I> </I><tt>}
<p>
</tt>where each <I>match</I><sub><I>i</I></sub> is of the general form
<p>
<table >
<tr><td>
</td><td> <tt>|</tt><I> g</I><sub><I>i1</I></sub> </td><td> <tt>-></tt><I> e</I><sub><I>i1</I></sub> </td></tr><tr><td></td><td> <I>...</I> </td></tr><tr><td></td><td> <tt>|</tt><I> g</I><sub><I>im</I><sub><I>i</I></sub></sub> </td><td> <tt>-></tt><I> e</I><sub><I>im</I><sub><I>i</I></sub></sub> </td></tr><tr><td></td><td> <tt>where</tt><I> decls</I><sub><I>i</I></sub>
</td></tr></table>
<p>
(Notice that in the syntax rule for <I>gd</I>, the "<tt>|</tt>" is a
terminal symbol, not the syntactic metasymbol for alternation.)
Each alternative <I>p</I><sub><I>i</I></sub><I> match</I><sub><I>i</I></sub> consists of a
pattern <I>p</I><sub><I>i</I></sub> and its matches, <I>match</I><sub><I>i</I></sub>.
Each match in turn
consists of a sequence of pairs of guards
<I>g</I><sub><I>ij</I></sub> and bodies <I>e</I><sub><I>ij</I></sub> (expressions), followed by
optional bindings (<I>decls</I><sub><I>i</I></sub>) that scope over all of the guards and
expressions of the alternative. An alternative of the form
<p>
<I>pat </I><tt>-></tt><I> exp </I><tt>where</tt><I> decls
<p>
</I>is treated as shorthand for:
<p>
<table >
<tr><td>
</td><td> <I>pat </I><tt>| True</tt> </td><td> <tt>-></tt><I> exp</I> </td></tr><tr><td></td><td> <tt>where</tt><I> decls
</I></td></tr></table>
<p>
<p>
A case expression must have at least one alternative and each alternative must
have at least one body. Each body must have the same type, and the
type of the whole expression is that type.<p>
A case expression is evaluated by pattern matching the expression <I>e
</I>against the individual alternatives. The alternatives are tried
sequentially, from top to bottom. If <I>e</I> matches the pattern in the
alternative, the guards for that alternative are tried sequentially
from top to bottom, in the environment of the case expression extended
first by the bindings created during the matching of the pattern, and then
by the <I>decls</I><sub><I>i</I></sub> in the <tt>where</tt> clause associated with that alternative.
If one of the guards
evaluates to <tt>True</tt>, the corresponding right-hand side is evaluated in the
same environment as the guard.
If all the guards evaluate to <tt>False</tt>, matching continues with the
next alternative. If no match succeeds, the result is <I>_|_</I>.
Pattern matching is described in Section <a href="exps.html#pattern-matching">3.17</a>, with
the formal semantics of case expressions in
Section <a href="exps.html#case-semantics">3.17.3</a>.<p>
<I>A note about parsing.</I> The expression
<tt><br>
<br>
case x of { (a,_) | let b = not a in b :: Bool -> a }<br>
<br>
</tt>is tricky to parse correctly. It has a single unambiguous parse, namely
<tt><br>
<br>
case x of { (a,_) | (let b = not a in b :: Bool) -> a }<br>
<br>
</tt>However, the phrase <tt>Bool -> a</tt> is syntactically valid as a type, and
parsers with limited lookahead may incorrectly commit to this choice, and hence
reject the program. Programmers are advised, therefore, to avoid guards that
end with a type signature --- indeed that is why a <I>gd</I> contains
an <I>exp</I><sup><I>0</I></sup> not an <I>exp</I>.<a name="do-expressions"></a><p>
<a name="sect3.14"></a>
<h3>3.14<tt> </tt>Do Expressions</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
exp </td><td width=20> <tt>-></tt> </td><td width=250> <tt>do</tt> <tt>{</tt> stmts <tt>}</tt> </td><td> (do expression)
</td></tr><tr><td>
stmts </td><td> <tt>-></tt> </td><td> stmt<sub>1</sub> ... stmt<sub>n</sub> exp [<tt>;</tt>] </td><td> (n>=0)
</td></tr><tr><td>
stmt </td><td> <tt>-></tt> </td><td> exp <tt>;
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> pat <tt><-</tt> exp <tt>;
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>let</tt> decls <tt>;
</tt></td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>;</tt> </td><td> (empty statement)
</td></tr></table>
<p>
A <I>do expression</I> provides a more conventional syntax for monadic programming.
It allows an expression such as
<tt><br>
<br>
putStr "x: " >> <br>
getLine >>= \l -><br>
return (words l)<br>
<br>
</tt>to be written in a more traditional way as:
<tt><br>
<br>
do putStr "x: "<br>
l <- getLine<br>
return (words l)<br>
<br>
</tt><table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
Do expressions satisfy these identities, which may be
used as a translation into the kernel, after eliminating empty <I>stmts</I>:
<div align=center><table >
<tr><td><tt>do {</tt><I>e</I><tt>}</tt> </td><td align=center>=</td><td> <I>e</I></td></tr><tr><td><tt>do {</tt><I>e</I><tt>;</tt><I>stmts</I><tt>}</tt> </td><td align=center>=</td><td> <I>e</I> <tt>>> do {</tt><I>stmts</I><tt>}</tt> </td></tr><tr><td><tt>do {</tt><I>p</I><tt> <- </tt><I>e</I><tt>; </tt><I>stmts</I><tt>}</tt> </td><td align=center>=</td><td> <tt>let ok </tt><I>p</I><tt> = do {</tt><I>stmts</I><tt>}</tt></td></tr><tr><td></td><td align=center> </td><td> <tt> ok _ = fail "..."</tt></td></tr><tr><td></td><td align=center> </td><td> <tt> in </tt><I>e</I><tt> >>= ok</tt> </td></tr><tr><td><tt>do {let</tt> <I>decls</I><tt>; </tt><I>stmts</I><tt>}</tt> </td><td align=center>=</td><td> <tt>let</tt> <I>decls</I> <tt>in do {</tt><I>stmts</I><tt>}</tt></td></tr></table>
</div>
The ellipsis <tt>"..."</tt> stands for a compiler-generated error message,
passed to <tt>fail</tt>, preferably giving some indication of the location
of the pattern-match failure;
the functions <tt>>></tt>, <tt>>>=</tt>, and <tt>fail</tt> are operations in the class <tt>Monad</tt>,
as defined in the Prelude; and <tt>ok</tt> is a fresh
identifier.
</td></tr></table>
As indicated by the translation of <tt>do</tt>, variables bound by <tt>let</tt> have
fully polymorphic types while those defined by <tt><-</tt> are lambda bound
and are thus monomorphic.<a name="field-ops"></a><p>
<a name="sect3.15"></a>
<h3>3.15<tt> </tt>Datatypes with Field Labels</h3>
A datatype declaration may optionally define field labels
(see Section <a href="decls.html#datatype-decls">4.2.1</a>).
These field labels can be used to
construct, select from, and update fields in a manner
that is independent of the overall structure of the datatype.<p>
Different datatypes cannot share common field labels in the same scope.
A field label can be used at most once in a constructor.
Within a datatype, however, a field label can be used in more
than one constructor provided the field has the same typing in all
constructors. To illustrate the last point, consider:
<tt><br>
<br>
data S = S1 { x :: Int } | S2 { x :: Int } -- OK<br>
data T = T1 { y :: Int } | T2 { y :: Bool } -- BAD<br>
<br>
</tt>Here <tt>S</tt> is legal but <tt>T</tt> is not, because <tt>y</tt> is given
inconsistent typings in the latter.<p>
<a name="sect3.15.1"></a>
<h4>3.15.1<tt> </tt>Field Selection</h4>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> qvar
</td></tr></table>
<p>
Field labels are used as selector functions. When used as a variable,
a field label serves as a function that extracts the field from an
object. Selectors are top level bindings and so they
may be shadowed by local variables but cannot conflict with
other top level bindings of the same name. This shadowing only
affects selector functions; in record construction (Section <a href="exps.html#record-construction">3.15.2</a>)
and update (Section <a href="exps.html#record-update">3.15.3</a>), field labels
cannot be confused with ordinary variables. <p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
A field label <I>f</I> introduces a selector function defined as:
<div align=center><table >
<tr><td>
<I>f</I><tt> x</tt> </td><td align=center>=</td><td><tt>case x of {</tt> <I>C</I><sub><I>1</I></sub><I> p</I><sub><I>11</I></sub><I> ...p</I><sub><I>1k</I></sub> <tt> -> </tt> <I>e</I><sub><I>1</I></sub> <tt>;</tt>
<I>...</I> <tt>;</tt> <I>C</I><sub><I>n</I></sub><I> p</I><sub><I>n1</I></sub><I> ...p</I><sub><I>nk</I></sub> <tt> -> </tt> <I>e</I><sub><I>n</I></sub> <tt>}</tt></td></tr></table>
</div>
where <I>C</I><sub><I>1</I></sub><I> ...C</I><sub><I>n</I></sub> are all the constructors of the datatype containing a
field labeled with <I>f</I>, <I>p</I><sub><I>ij</I></sub> is <tt>y</tt> when <I>f</I> labels the <I>j</I>th
component of <I>C</I><sub><I>i</I></sub> or <tt>_</tt> otherwise, and <I>e</I><sub><I>i</I></sub> is <tt>y</tt> when some field in
<I>C</I><sub><I>i</I></sub> has a label of <I>f</I> or <tt>undefined</tt> otherwise.
</td></tr></table>
<a name="record-construction"></a>
<a name="sect3.15.2"></a>
<h4>3.15.2<tt> </tt>Construction Using Field Labels</h4>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> qcon <tt>{</tt> fbind<sub>1</sub> <tt>,</tt> ... <tt>,</tt> fbind<sub>n</sub> <tt>}</tt> </td><td> (labeled construction, n>=0)
</td></tr><tr><td>
fbind </td><td> <tt>-></tt> </td><td> qvar <tt>=</tt> exp
</td></tr></table>
<p>
A constructor with labeled fields may be used to construct a value
in which the components are specified by name rather than by position.
Unlike the braces used in declaration lists, these are not subject to
layout; the <tt>{</tt> and <tt>}</tt> characters must be explicit. (This is also
true of field updates and field patterns.)
Construction using field labels is subject to the following constraints:
<UL><LI>Only field labels declared with the specified constructor may be
mentioned.
<LI>A field label may not be mentioned more than once.
<LI>Fields not mentioned are initialized to _|_.
<LI>A compile-time error occurs when any strict fields (fields
whose declared types are prefixed by <tt>!</tt>) are omitted during
construction. Strict fields are discussed in Section <a href="decls.html#strictness-flags">4.2.1</a>.
</UL>
The expression <tt>F {}</tt>, where <tt>F</tt> is a data constructor, is legal
<I>whether or not </I><tt>F</tt><I> was declared with record syntax</I> (provided <tt>F</tt> has no strict
fields --- see the third bullet above);
it denotes <tt>F</tt><I> _|_</I><sub><I>1</I></sub><I> ... _|_</I><sub><I>n</I></sub>, where <I>n</I> is the arity of <tt>F</tt>.<p>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
In the binding <I>f</I> <tt>=</tt> <I>v</I>, the field <I>f</I> labels <I>v</I>.
<div align=center><table >
<tr><td>
<I>C</I> <tt>{</tt> <I>bs</I> <tt>}</tt> </td><td align=center>=</td><td> <I>C (pick</I><sup><I>C</I></sup><sub><I>1</I></sub><I> bs </I><tt>undefined</tt><I>) ...(pick</I><sup><I>C</I></sup><sub><I>k</I></sub><I> bs </I><tt>undefined</tt><I>)</I></td></tr></table>
</div>
where <I>k</I> is the arity of <I>C</I>.<p>
The auxiliary function <I>pick</I><sup><I>C</I></sup><sub><I>i</I></sub><I> bs d</I> is defined as follows:
<blockquote>If the <I>i</I>th component of a constructor <I>C</I> has the
field label <I>f</I>, and if <I>f=v</I> appears in the binding list
<I>bs</I>, then <I>pick</I><sup><I>C</I></sup><sub><I>i</I></sub><I> bs d</I> is <I>v</I>. Otherwise, <I>pick</I><sup><I>C</I></sup><sub><I>i</I></sub><I> bs d</I> is
the default value <I>d</I>.
</blockquote>
</td></tr></table>
<a name="record-update"></a><p>
<a name="sect3.15.3"></a>
<h4>3.15.3<tt> </tt>Updates Using Field Labels</h4>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
aexp </td><td width=20> <tt>-></tt> </td><td width=250> aexp<sub><qcon></sub> <tt>{</tt> fbind<sub>1</sub> <tt>,</tt> ... <tt>,</tt> fbind<sub>n</sub> <tt>}</tt> </td><td> (labeled update, n>=1)
</td></tr></table>
<p>
Values belonging to a datatype with field labels may be
non-destructively updated. This creates a new value in which the
specified field values replace those in the existing value.
Updates are restricted in the following ways:
<UL><LI>All labels must be taken from the same datatype.
<LI>At least one constructor must define all of the labels
mentioned in the update.
<LI>No label may be mentioned more than once.
<LI>An execution error occurs when the value being updated does
not contain all of the specified labels.
</UL>
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
Using the prior definition of <I>pick</I>,
<div align=center><table >
<tr><td>
<I>e</I> <tt>{</tt> <I>bs</I> <tt>}</tt> </td><td align=center>=</td><td> <tt>case</tt> <I>e</I> <tt>of</tt></td></tr><tr><td></td><td align=center></td><td><tt> </tt><I>C</I><sub><I>1</I></sub><I> v</I><sub><I>1</I></sub><I> ... v</I><sub><I>k</I><sub><I>1</I></sub></sub> <tt>-></tt> <I>C</I><sub><I>1</I></sub><I> (pick</I><sup><I>C</I><sub><I>1</I></sub></sup><sub><I>1</I></sub><I> bs v</I><sub><I>1</I></sub><I>) ... (pick</I><sup><I>C</I><sub><I>1</I></sub></sup><sub><I>k</I><sub><I>1</I></sub></sub><I> bs v</I><sub><I>k</I><sub><I>1</I></sub></sub><I>)</I></td></tr><tr><td></td><td align=center></td><td><tt> </tt> ... </td></tr><tr><td></td><td align=center></td><td><tt> </tt><I>C</I><sub><I>j</I></sub><I> v</I><sub><I>1</I></sub><I> ... v</I><sub><I>k</I><sub><I>j</I></sub></sub> <tt>-></tt> <I>C</I><sub><I>j</I></sub><I> (pick</I><sup><I>C</I><sub><I>j</I></sub></sup><sub><I>1</I></sub><I> bs v</I><sub><I>1</I></sub><I>) ... (pick</I><sup><I>C</I><sub><I>j</I></sub></sup><sub><I>k</I><sub><I>j</I></sub></sub><I> bs v</I><sub><I>k</I><sub><I>j</I></sub></sub><I>)</I></td></tr><tr><td></td><td align=center></td><td><tt> _ -> error "Update error"</tt></td></tr></table>
</div>
where <I>{C</I><sub><I>1</I></sub><I>,...,C</I><sub><I>j</I></sub><I>}</I> is the set of constructors containing all labels
in <I>bs</I>, and <I>k</I><sub><I>i</I></sub> is the arity of <I>C</I><sub><I>i</I></sub>.
</td></tr></table>
Here are some examples using labeled fields:
<tt><br>
<br>
data T = C1 {f1,f2 :: Int}<br>
| C2 {f1 :: Int,<br>
f3,f4 :: Char}<br>
<br>
<p>
</tt><table >
<tr><td>
Expression </td><td> Translation </td></tr><tr><td>
<tt>C1 {f1 = 3}</tt> </td><td> <tt>C1 3 undefined</tt> </td></tr><tr><td><tt>C2 {f1 = 1, f4 = 'A', f3 = 'B'}</tt> </td><td> <tt>C2 1 'B' 'A'</tt> </td></tr><tr><td><tt>x {f1 = 1}</tt> </td><td> <tt>case x of C1 _ f2 -> C1 1 f2</tt> </td></tr><tr><td></td><td> <tt> C2 _ f3 f4 -> C2 1 f3 f4</tt> </td></tr></table>
<p>
The field <tt>f1</tt> is common to both constructors in T. This
example translates expressions using constructors in field-label
notation into equivalent expressions using the same constructors
without field labels.
A compile-time error will result if no single constructor
defines the set of field labels used in an update, such as
<tt>x {f2 = 1, f3 = 'x'}</tt>. <a name="expression-type-sigs"></a><p>
<a name="sect3.16"></a>
<h3>3.16<tt> </tt>Expression Type-Signatures</h3>
<table cellspacing=0 cellspacing=0>
<tr><td width=100>
exp </td><td width=20> <tt>-></tt> </td><td width=250> exp <tt>::</tt> [context <tt>=></tt>] type
</td></tr></table>
<p>
<I>Expression type-signatures</I> have the form <I>e </I><tt>::</tt><I> t</I>, where <I>e
</I>is an expression and <I>t</I> is a type (Section <a href="decls.html#type-syntax">4.1.2</a>); they
are used to type an expression explicitly
and may be used to resolve ambiguous typings due to overloading (see
Section <a href="decls.html#default-decls">4.3.4</a>). The value of the expression is just that of
<I>exp</I>. As with normal type signatures (see
Section <a href="decls.html#type-signatures">4.4.1</a>), the declared type may be more specific than
the principal type derivable from <I>exp</I>, but it is an error to give
a type that is more general than, or not comparable to, the
principal type.
<table border=2 cellpadding=3>
<tr><td>
<h3>Translation:</h3>
<div align=center><table >
<tr><td>
<I>e </I><tt>::</tt><I> t</I> </td><td align=center> = </td><td> <tt>let {</tt><I> v </I><tt>::</tt><I> t</I><tt>; </tt><I> v </I><tt>=</tt><I> e </I><tt>} in </tt><I>v
</I></td></tr></table>
</div>
</td></tr></table>
<a name="pattern-matching"></a><p>
<a name="sect3.17"></a>
<h3>3.17<tt> </tt>Pattern Matching</h3>
<a name="patterns"></a>
<p>
<I>Patterns</I> appear in lambda abstractions, function definitions, pattern
bindings, list comprehensions, do expressions, and case expressions.
However, the
first five of these ultimately translate into case expressions, so
defining the semantics of pattern matching for case expressions is sufficient.<a name="pattern-definitions"></a><p>
<a name="sect3.17.1"></a>
<h4>3.17.1<tt> </tt>Patterns</h4>
<p>
Patterns have this syntax:
<table cellspacing=0 cellspacing=0>
<tr><td width=100></td><td width=20></td><td width=250></td></tr><tr></tr><tr><td>
pat </td><td> <tt>-></tt> </td><td> var <tt>+</tt> integer </td><td> (successor pattern)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> pat<sup>0</sup>
</td></tr><tr><td>
pat<sup>i</sup> </td><td> <tt>-></tt> </td><td> pat<sup>i+1</sup> [qconop<sup>(n,i)</sup> pat<sup>i+1</sup>]
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> lpat<sup>i</sup>
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> rpat<sup>i</sup>
</td></tr><tr><td>
lpat<sup>i</sup> </td><td> <tt>-></tt> </td><td> (lpat<sup>i</sup> | pat<sup>i+1</sup>) qconop<sup>(l,i)</sup> pat<sup>i+1</sup>
</td></tr><tr><td>
lpat<sup>6</sup> </td><td> <tt>-></tt> </td><td> <tt>-</tt> (integer | float) </td><td> (negative literal)
</td></tr><tr><td>
rpat<sup>i</sup> </td><td> <tt>-></tt> </td><td> pat<sup>i+1</sup> qconop<sup>(r,i)</sup> (rpat<sup>i</sup> | pat<sup>i+1</sup>)
</td></tr><tr><td>
pat<sup>10</sup> </td><td> <tt>-></tt> </td><td> apat
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> gcon apat<sub>1</sub> ... apat<sub>k</sub> </td><td> (arity gcon = k, k>=1)
</td></tr><tr><td>
apat </td><td> <tt>-></tt> </td><td> var [<tt>@</tt> apat] </td><td> (as pattern)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> gcon </td><td> (arity gcon = 0)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> qcon <tt>{</tt> fpat<sub>1</sub> <tt>,</tt> ... <tt>,</tt> fpat<sub>k</sub> <tt>}</tt> </td><td> (labeled pattern, k>=0)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> literal
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>_</tt> </td><td> (wildcard)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> pat <tt>)</tt> </td><td> (parenthesized pattern)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>(</tt> pat<sub>1</sub> <tt>,</tt> ... <tt>,</tt> pat<sub>k</sub> <tt>)</tt> </td><td> (tuple pattern, k>=2)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>[</tt> pat<sub>1</sub> <tt>,</tt> ... <tt>,</tt> pat<sub>k</sub> <tt>]</tt> </td><td> (list pattern, k>=1)
</td></tr><tr><td>
</td><td> <tt>|</tt> </td><td> <tt>~</tt> apat </td><td> (irrefutable pattern)
</td></tr><tr><td>
fpat </td><td> <tt>-></tt> </td><td> qvar <tt>=</tt> pat
</td></tr></table>
<p>
The arity of a constructor must match the number of
sub-patterns associated with it; one cannot match against a
partially-applied constructor.<p>
All patterns must be <I>linear
</I>---no variable may appear more than once. For
example, this definition is illegal:
<tt><br>
f (x,x) = x -- ILLEGAL; x used twice in pattern<br>
<p>
</tt>Patterns of the form <I>var</I><tt>@</tt><I>pat</I> are called <I>as-patterns</I>,
and allow one to use <I>var
</I>as a name for the value being matched by <I>pat</I>. For example,
<tt><br>
<br>
case e of { xs@(x:rest) -> if x==0 then rest else xs }<br>
<br>
</tt>is equivalent to:
<tt><br>
<br>
let { xs = e } in<br>
case xs of { (x:rest) -> if x==0 then rest else xs }<br>
<br>
<p>
</tt>Patterns of the form <tt>_</tt> are
<I>wildcards</I> and are useful when some part of a pattern
is not referenced on the right-hand-side. It is as if an
identifier not used elsewhere were put in its place. For example,
<tt><br>
<br>
case e of { [x,_,_] -> if x==0 then True else False }<br>
<br>
</tt>is equivalent to:
<tt><br>
<br>
case e of { [x,y,z] -> if x==0 then True else False }<br>
<br>
<p>
</tt><a name="sect3.17.2"></a>
<h4>3.17.2<tt> </tt>Informal Semantics of Pattern Matching</h4><p>
Patterns are matched against values. Attempting to match a pattern
can have one of three results: it may <I>fail</I>; it may
<I>succeed</I>, returning a binding for each variable in the pattern; or it
may <I>diverge</I> (i.e. return <I>_|_</I>). Pattern matching proceeds
from left to right, and outside to inside, according to the following rules:
<OL><LI>Matching the pattern <I>var</I>
against a value <I>v</I> always succeeds and binds <I>var</I> to <I>v</I>.<p>
<LI>
Matching the pattern <tt>~</tt><I>apat</I> against a value <I>v</I> always succeeds.
The free variables in <I>apat</I> are bound to the appropriate values if matching
<I>apat</I> against <I>v</I> would otherwise succeed, and to <I>_|_</I> if matching
<I>apat</I> against <I>v</I> fails or diverges. (Binding does
<I>not</I> imply evaluation.)<p>
Operationally, this means that no matching is done on a
<tt>~</tt><I>apat</I> pattern until one of the variables in <I>apat</I> is used.
At that point the entire pattern is matched against the value, and if
the match fails or diverges, so does the overall computation.<p>
<LI>
Matching the wildcard pattern <tt>_</tt> against any value always succeeds,
and no binding is done.<p>
<LI>
Matching the pattern <I>con pat</I> against a value, where <I>con</I> is a
constructor defined by <tt>newtype</tt>, depends on the value:
<UL><LI>If the value is of the form <I>con v</I>, then <I>pat</I> is matched against <I>v</I>.
<LI>If the value is <I>_|_</I>, then <I>pat</I> is matched against <I>_|_</I>.
</UL>
That is, constructors associated with
<tt>newtype</tt> serve only to change the type of a value.<p>
<LI>
Matching the pattern <I>con pat</I><sub><I>1</I></sub><I> ...pat</I><sub><I>n</I></sub> against a value, where <I>con</I> is a
constructor defined by <tt>data</tt>, depends on the value:
<UL><LI>If the value is of the form <I>con v</I><sub><I>1</I></sub><I> ...v</I><sub><I>n</I></sub>,
sub-patterns are matched left-to-right against the components of the data value;
if all matches succeed, the overall match
succeeds; the first to fail or diverge causes the overall match to
fail or diverge, respectively. <p>
<LI>If the value is of the form <I>con' v</I><sub><I>1</I></sub><I> ...v</I><sub><I>m</I></sub>, where <I>con</I> is a different
constructor to <I>con'</I>, the match fails.<p>
<LI>If the value is <I>_|_</I>, the match diverges.
</UL><p>
<LI>
Matching against a constructor using labeled fields is the same as
matching ordinary constructor patterns except that the fields are
matched in the order they are named in the field list. All fields
listed must be declared by the constructor; fields may not be named
more than once. Fields not named by the pattern are ignored (matched
against <tt>_</tt>).<p>
<LI>Matching a numeric, character, or string literal pattern <I>k</I> against a value <I>v
</I>succeeds if <I>v </I><tt>==</tt><I> k</I>, where <tt>==
</tt>is overloaded based on the type of the pattern. The match diverges if
this test diverges.<p>
The interpretation of numeric literals is exactly as described in Section <a href="exps.html#vars-and-lits">3.2</a>;
that is, the overloaded function <tt>fromInteger</tt> or <tt>fromRational</tt> is
applied to an <tt>Integer</tt> or <tt>Rational</tt> literal (resp)
to convert it to the appropriate type.<p>
<LI>Matching an <I>n</I><tt>+</tt><I>k</I> pattern (where <I>n</I> is a variable and <I>k</I> is a
positive integer literal) against a value <I>v</I>
succeeds if <I>v </I><tt>>=</tt><I> k</I>, resulting in the binding
of <I>n</I> to <I>v </I><tt>-</tt><I> k</I>,
and fails otherwise. Again, the functions <tt>>=</tt> and <tt>-</tt> are
overloaded, depending on the type of the pattern.
The match diverges if the comparison diverges.<p>
The interpretation of the literal <I>k</I> is the same as in numeric literal
patterns, except that only integer literals are allowed.<p>
<LI>
Matching an as-pattern <I>var</I><tt>@</tt><I>apat</I> against a value <I>v</I> is
the result of matching <I>apat</I> against <I>v</I>, augmented with the binding of
<I>var</I> to <I>v</I>. If the match of <I>apat</I> against <I>v</I> fails or diverges,
then so does the overall match.
</OL><p>
Aside from the obvious static type constraints (for
example, it is a static error to match a character against a
boolean), the following static class constraints hold:
<UL><LI>An integer
literal pattern
can only be matched against a value in the class
<tt>Num</tt>.
<LI>A floating literal pattern
can only be matched against a value
in the class <tt>Fractional</tt>.
<LI>An <I>n</I><tt>+</tt><I>k</I> pattern
can only be matched
against a value in the class <tt>Integral</tt>.
</UL><p>
Many people feel that <I>n</I><tt>+</tt><I>k</I> patterns should not be used. These
patterns may be removed or changed in future versions of Haskell . <p>
It is sometimes helpful to distinguish two kinds of
patterns. Matching an <I>irrefutable pattern
</I>is non-strict: the pattern matches even if the value to be matched is <I>_|_</I>.
Matching a <I>refutable</I> pattern is strict: if the value to be matched
is <I>_|_</I> the match diverges.
The irrefutable patterns are as follows:
a variable, a wildcard, <I>N apat</I> where <I>N</I> is a constructor
defined by <tt>newtype</tt> and <I>apat</I> is irrefutable (see
Section <a href="decls.html#datatype-renaming">4.2.3</a>),
<I>var</I><tt>@</tt><I>apat</I> where <I>apat</I> is irrefutable,
or of the form <tt>~</tt><I>apat</I> (whether or not <I>apat</I> is irrefutable).
All other patterns are <I>refutable</I>.<p>
Here are some examples:
<OL><LI>If the pattern <tt>['a','b']</tt> is matched against <tt>['x',</tt><I>_|_</I><tt>]</tt>, then <tt>'a'
</tt><I>fails</I> to match against <tt>'x'</tt>, and the result is a failed match. But
if <tt>['a','b']</tt> is matched against <tt>[</tt><I>_|_</I><tt>,'x']</tt>, then attempting to match
<tt>'a'</tt> against <I>_|_</I> causes the match to <I>diverge</I>.<p>
<LI>These examples demonstrate refutable vs. irrefutable
matching:
<tt><br>
<br>
(\ ~(x,y) -> 0) </tt><I>_|_</I><tt> </tt><I>=></I><tt> 0<br>
(\ (x,y) -> 0) </tt><I>_|_</I><tt> </tt><I>=></I><tt> </tt><I>_|_</I><tt><br>
<br>
<br>
<br>
(\ ~[x] -> 0) [] </tt><I>=></I><tt> 0<br>
(\ ~[x] -> x) [] </tt><I>=></I><tt> </tt><I>_|_</I><tt><br>
<br>
<br>
<br>
(\ ~[x,~(a,b)] -> x) [(0,1),</tt><I>_|_</I><tt>] </tt><I>=></I><tt> (0,1)<br>
(\ ~[x, (a,b)] -> x) [(0,1),</tt><I>_|_</I><tt>] </tt><I>=></I><tt> </tt><I>_|_</I><tt><br>
<br>
<br>
<br>
(\ (x:xs) -> x:x:xs) </tt><I>_|_</I><tt> </tt><I>=></I><tt> </tt><I>_|_</I><tt><br>
(\ ~(x:xs) -> x:x:xs) </tt><I>_|_</I><tt> </tt><I>=></I><tt> </tt><I>_|_</I><tt>:</tt><I>_|_</I><tt>:</tt><I>_|_</I><tt><br>
<p>
</tt><LI>
Consider the following declarations:
<tt><br>
<br>
newtype N = N Bool<br>
data D = D !Bool<br>
<br>
</tt>These examples illustrate the difference in pattern matching
between types defined by <tt>data</tt> and <tt>newtype</tt>:
<tt><br>
<br>
(\ (N True) -> True) </tt><I>_|_</I><tt> </tt><I>=></I><tt> </tt><I>_|_</I><tt><br>
(\ (D True) -> True) </tt><I>_|_</I><tt> </tt><I>=></I><tt> </tt><I>_|_</I><tt><br>
(\ ~(D True) -> True) </tt><I>_|_</I><tt> </tt><I>=></I><tt> True<br>
<br>
</tt>Additional examples may be found in Section <a href="decls.html#datatype-renaming">4.2.3</a>.<p>
</OL><p>
Top level patterns in case
expressions and the set of top level patterns in function or pattern
bindings may have zero or more associated <I>guards</I>.
A guard is
a boolean expression that is evaluated only after all of the
arguments have been successfully matched, and it must be true for the
overall pattern match to succeed. The environment of the guard is the same
as the right-hand-side of the case-expression
alternative, function definition, or pattern binding to which it is attached.<p>
The guard semantics have an obvious influence on the
strictness characteristics of a function or case expression. In
particular, an otherwise irrefutable pattern
may be evaluated because of a guard. For example, in
<tt><br>
<br>
f :: (Int,Int,Int) -> [Int] -> Int<br>
f ~(x,y,z) [a] | (a == y) = 1<br>
<br>
</tt>both <tt>a</tt> and <tt>y</tt> will be evaluated by <tt>==</tt> in the guard.<a name="case-semantics"></a><p>
<a name="sect3.17.3"></a>
<h4>3.17.3<tt> </tt>Formal Semantics of Pattern Matching</h4>
<p>
The semantics of all pattern matching constructs other than <tt>case
</tt>expressions are defined by giving identities that relate those
constructs to <tt>case</tt> expressions. The semantics of
<tt>case</tt> expressions themselves are in turn given as a series of
identities, in Figures <a href="exps.html#simple-case-expr-1">3.1</a>--<a href="exps.html#simple-case-expr-2">3.2</a>.
Any implementation should behave so that these identities hold; it is
not expected that it will use them directly, since that
would generate rather inefficient code.
<table border=2 cellpadding=3>
<tr><td>
<div align=center><table border=2 cellpadding=3>
<tr><td>
<table >
<tr><td align=center>(a)</td><td><tt>case </tt>e<tt> of { </tt>alts<tt> } </tt>=<tt> (\</tt>v<tt> -> case </tt>v<tt> of { </tt>alts<tt> }) </tt><I>e</I></td></tr><tr><td align=center></td><td>where v is a new variable</td></tr><tr><td align=center>(b)</td><td><tt>case </tt>v<tt> of { </tt>p<sub>1</sub> match<sub>1</sub><tt>; </tt>...<tt> ; </tt>p<sub>n</sub> match<sub>n</sub><tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> case </tt>v<tt> of { </tt>p<sub>1</sub> match<sub>1</sub><tt> ;</tt></td></tr><tr><td align=center></td><td><tt> _ -> </tt>...<tt> case </tt>v<tt> of {</tt></td></tr><tr><td align=center></td><td><tt> </tt>p<sub>n</sub> match<sub>n</sub> <tt>;</tt></td></tr><tr><td align=center></td><td><tt> _ -> error "No match" }</tt>...<tt>}</tt></td></tr><tr><td align=center></td><td><tt> </tt>where each match<sub>i</sub> has the form:</td></tr><tr><td align=center></td><td><tt> | </tt>g<sub>i,1</sub> <tt> -> </tt>e<sub>i,1</sub><tt> ; </tt>...<tt> ; | </tt>g<sub>i,m<sub>i</sub></sub><tt> -> </tt>e<sub>i,m<sub>i</sub></sub><tt> where { </tt>decls<sub>i</sub><tt> }</tt></td></tr><tr><td align=center>
(c)</td><td><tt>case </tt>v<tt> of { </tt>p<tt> | </tt>g<sub>1</sub><tt> -> </tt>e<sub>1</sub><tt> ; </tt>...</td></tr><tr><td align=center></td><td><tt> | </tt>g<sub>n</sub><tt> -> </tt>e<sub>n</sub><tt> where { </tt>decls<tt> }</tt></td></tr><tr><td align=center></td><td><tt> _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> case </tt>e'<tt> of</tt></td></tr><tr><td align=center></td><td> <tt> {</tt>y<tt> -> </tt> (where <I>y</I> is a new variable)</td></tr><tr><td align=center></td><td><tt> case </tt>v<tt> of {</tt></td></tr><tr><td align=center></td><td><tt> </tt>p<tt> -> let { </tt>decls<tt> } in</tt></td></tr><tr><td align=center></td><td><tt> if </tt>g<sub>1</sub><tt> then </tt>e<sub>1</sub><tt> </tt>...<tt> else if </tt>g<sub>n</sub><tt> then </tt>e<sub>n</sub><tt> else </tt>y <tt>;</tt></td></tr><tr><td align=center></td><td><tt> _ -> </tt>y<tt> }}</tt></td></tr><tr><td align=center>
(d)</td><td><tt>case </tt>v<tt> of { ~</tt>p<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> (\</tt>x<sub>1</sub> ... x<sub>n</sub> <tt>-></tt> e <tt>) (case </tt>v<tt> of { </tt>p<tt>-></tt>
x<sub>1</sub><tt> })</tt> ... <tt>(case </tt>v<tt> of { </tt>p<tt> -> </tt>x<sub>n</sub><tt>})</tt></td></tr><tr><td align=center></td><td>where x<sub>1</sub>, ..., x<sub>n</sub> are all the variables in p</td></tr><tr><td align=center>
(e)</td><td><tt>case </tt>v<tt> of { </tt>x<tt>@</tt>p<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> case </tt>v<tt> of { </tt>p<tt> -> ( \ </tt>x<tt> -> </tt>e<tt> ) </tt>v<tt> ; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center>
(f)</td><td><tt>case </tt>v<tt> of { _ -> </tt>e<tt>; _ -> </tt>e'<tt> } </tt>=<tt> </tt>e</td></tr><tr><td align=center>
</td></tr></table>
</td></tr></table>
</div>
<div align=center> <h4>Figure 3</h4> </div>
<div align=center><h3>Semantics of Case Expressions, Part 1</h3></div><a name="simple-case-expr-1"></a>
</td></tr></table>
<p>
<table border=2 cellpadding=3>
<tr><td>
<div align=center><table border=2 cellpadding=3>
<tr><td>
<table >
<tr><td align=center>(g)</td><td><tt>case </tt>v<tt> of { </tt>K p<sub>1</sub> ...p<sub>n</sub><tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> case </tt>v<tt> of {</tt></td></tr><tr><td align=center></td><td><tt> </tt>K x<sub>1</sub> ...x<sub>n</sub><tt> -> case </tt>x<sub>1</sub><tt> of {</tt></td></tr><tr><td align=center></td><td><tt> </tt>p<sub>1</sub><tt> -> </tt>...<tt> case </tt>x<sub>n</sub><tt> of { </tt>p<sub>n</sub><tt> -> </tt>e<tt> ; _ -> </tt>e'<tt> } </tt>...</td></tr><tr><td align=center></td><td><tt> _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td><tt> _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center>
</td><td>at least one of p<sub>1</sub>, ..., p<sub>n</sub> is not a variable; x<sub>1</sub>, ..., x<sub>n</sub> are new variables</td></tr><tr><td align=center>
(h)</td><td><tt>case </tt>v<tt> of { </tt>k<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> } </tt>=<tt> if (</tt>v<tt>==</tt>k<tt>) then </tt>e<tt> else </tt>e' </td></tr><tr><td align=center></td><td>where k is a numeric, character, or string literal. </td></tr><tr><td align=center>
(i)</td><td><tt>case </tt>v<tt> of { </tt>x<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> } </tt>=<tt> case </tt>v<tt> of { </tt>x<tt> -> </tt>e<tt> }</tt></td></tr><tr><td align=center>
(j)</td><td><tt>case </tt>v<tt> of { </tt>x<tt> -> </tt>e<tt> } </tt>=<tt> ( \ </tt>x<tt> -> </tt>e<tt> ) </tt>v</td></tr><tr><td align=center>
(k)</td><td><tt>case </tt>N v<tt> of { </tt>N<tt> </tt>p<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> case </tt>v<tt> of { </tt>p<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>where N is a <tt>newtype</tt> constructor</td></tr><tr><td align=center>
(l)</td><td><tt>case </tt>_|_<tt> of { </tt>N<tt> </tt>p<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> } </tt>=<tt> case </tt>_|_<tt> of { </tt>p<tt> -> </tt>e<tt> }</tt></td></tr><tr><td align=center></td><td>where N is a <tt>newtype</tt> constructor</td></tr><tr><td align=center>
(m)</td><td> <tt>case </tt> v <tt> of { </tt> K <tt> {</tt> f<sub>1</sub> <tt> = </tt> p<sub>1</sub> <tt> , </tt> f<sub>2</sub> <tt> =
</tt>p<sub>2</sub> <tt> , </tt> ... <tt>} -> </tt> e <tt>; _ -> </tt> e' <tt> }</tt></td></tr><tr><td align=center></td><td>= <tt> case </tt>e'<tt> of {</tt></td></tr><tr><td align=center></td><td><tt> </tt>y<tt> -></tt></td></tr><tr><td align=center></td><td><tt> case </tt> v <tt> of { </tt></td></tr><tr><td align=center></td><td><tt> </tt> K <tt> { </tt> f<sub>1</sub> <tt> = </tt> p<sub>1</sub> <tt> } -></tt></td></tr><tr><td align=center></td><td><tt> case </tt> v <tt> of {</tt> K <tt> {</tt> f<sub>2</sub> <tt> = </tt> p<sub>2</sub> <tt> ,
</tt>... <tt> } -> </tt> e <tt>; _ -> </tt> y <tt> };</tt></td></tr><tr><td align=center></td><td><tt> _ -> </tt> y <tt> }}</tt></td></tr><tr><td align=center></td><td>where f<sub>1</sub>, f<sub>2</sub>, ... are fields of constructor K; y
is a new variable</td></tr><tr><td align=center>
(n)</td><td><tt>case </tt> v <tt> of { </tt> K <tt> {</tt> f <tt> = </tt> p <tt>} -> </tt> e <tt>; _ ->
</tt>e' <tt> }</tt> </td></tr><tr><td align=center></td><td>=<tt> case </tt> v <tt> of {</tt></td></tr><tr><td align=center></td><td> <tt> </tt> K p<sub>1</sub> ... p<sub>n</sub> <tt> -> </tt> e <tt>; _ -> </tt> e' <tt> }</tt></td></tr><tr><td align=center></td><td>where p<sub>i</sub> is p if f labels the ith component of K,
<tt>_</tt> otherwise</td></tr><tr><td align=center>(o)</td><td><tt>case </tt> v <tt> of { </tt> K <tt> {} -> </tt> e <tt>; _ ->
</tt>e' <tt> }</tt> </td></tr><tr><td align=center></td><td>=<tt> case </tt> v <tt> of {</tt></td></tr><tr><td align=center></td><td> <tt> </tt> K <tt>_</tt> ... <tt>_ -> </tt> e <tt>; _ -> </tt> e' <tt> }</tt></td></tr><tr><td align=center>(p)</td><td><tt>case (</tt>K'<tt> </tt>e<sub>1</sub><tt> </tt>...<tt> </tt>e<sub>m</sub><tt>) of { </tt>K<tt> </tt>x<sub>1</sub><tt> </tt>...<tt> </tt>x<sub>n</sub><tt> -> </tt>e<tt>; _ -> </tt>e'<tt> } </tt>=<tt> </tt>e'</td></tr><tr><td align=center></td><td>where K and K' are distinct <tt>data</tt> constructors of arity n and m, respectively</td></tr><tr><td align=center>
(q)</td><td><tt>case (</tt>K<tt> </tt>e<sub>1</sub><tt> </tt>...<tt> </tt>e<sub>n</sub><tt>) of { </tt>K<tt> </tt>x<sub>1</sub><tt> </tt>...<tt> </tt>x<sub>n</sub><tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> (\</tt>x<sub>1</sub> ... x<sub>n</sub><tt> -> </tt>e<tt>) </tt>e<sub>1</sub> ... e<sub>n</sub></td></tr><tr><td align=center></td><td>where K is a <tt>data</tt> constructor of arity n</td></tr><tr><td align=center><p>
(r)</td><td><tt>case</tt> _|_ <tt>of { </tt>K<tt> </tt>x<sub>1</sub><tt> </tt>...<tt> </tt>x<sub>n</sub><tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt> = _|_ </td></tr><tr><td align=center></td><td>where K is a <tt>data</tt> constructor of arity n</td></tr><tr><td align=center><p>
(s)</td><td><tt>case </tt>v<tt> of { </tt>x<tt>+</tt>k<tt> -> </tt>e<tt>; _ -> </tt>e'<tt> }</tt></td></tr><tr><td align=center></td><td>=<tt> if </tt>v<tt> >= </tt>k<tt> then (\</tt>x<tt> -> </tt>e<tt>) (</tt>v<tt>-</tt>k<tt>) else </tt>e'</td></tr><tr><td align=center></td><td>where k is a numeric literal</td></tr></table>
</td></tr></table>
</div>
<div align=center> <h4>Figure 4</h4> </div>
<div align=center><h3>Semantics of Case Expressions, Part 2</h3></div><a name="simple-case-expr-2"></a>
</td></tr></table>
<p>
In Figures <a href="exps.html#simple-case-expr-1">3.1</a>--<a href="exps.html#simple-case-expr-2">3.2</a>:
<I>e</I>, <I>e'</I> and <I>e</I><sub><I>i</I></sub> are expressions;
<I>g</I> and <I>g</I><sub><I>i</I></sub> are boolean-valued expressions;
<I>p</I> and <I>p</I><sub><I>i</I></sub> are patterns;
<I>v</I>, <I>x</I>, and <I>x</I><sub><I>i</I></sub> are variables;
<I>K</I> and <I>K'</I> are algebraic datatype (<tt>data</tt>) constructors (including
tuple constructors); and <I>N</I> is a <tt>newtype</tt> constructor.<p>
Rule (b) matches a general source-language
<tt>case</tt> expression, regardless of whether it actually includes
guards---if no guards are written, then <tt>True</tt> is substituted for the guards <I>g</I><sub><I>i,j</I></sub>
in the <I>match</I><sub><I>i</I></sub> forms.
Subsequent identities manipulate the resulting <tt>case</tt> expression into simpler
and simpler forms.<p>
Rule (h) in Figure <a href="exps.html#simple-case-expr-2">3.2</a> involves the
overloaded operator <tt>==</tt>; it is this rule that defines the
meaning of pattern matching against overloaded constants.<p>
These identities all preserve the static semantics. Rules (d), (e), (j), (q), and (s)
use a lambda rather than a <tt>let</tt>; this indicates that variables bound
by <tt>case</tt> are monomorphically typed (Section <a href="decls.html#type-semantics">4.1.4</a>).<p>
<hr><i>The Haskell 98 Report</i><br><a href="index.html">top</a> | <a href="lexemes.html">back</a> | <a href="decls.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><font size=2>December 2002</font>
<p>
|