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
|
document {
Key => "The Macaulay2 language",
PARA {
"The Macaulay2 language is a simple yet powerful interpreted language. Every object has a type, such as ", TO "Type", ", ", TO "ZZ", "
(integer), ", TO "String", ", ", TO "HashTable", ", ", TO "Ring", ", ", TO "Matrix", ", ", TO "Function", ", etc. User defined variables may
hold values of any type."
},
PARA{
"The Macaulay2 language is based on expressions, which, generally speaking, are used to create new objects from old ones.
Other computer languages have things called statements, but Macaulay2 does not.
Other computer languages use newline characters and even indentation to indicate separate and nesting of statements, but Macaulay2 does not.
Expressions in Macaulay2 include function calls, control structures (e.g., ", TO "for", " and ", TO "while", " loops), function definitions, and
operator expressions. Every expression returns an object, although the basic object ", TO "null", " is often returned if the expression is being
used only to produce some side effect."
},
PARA{
"There is special syntax for creating and accessing strings, lists, sequences, and hashtables. These are the key data types underlying many new
types. The Macaulay2 engine implements rings, ring elements, and matrices, as instances of low-level types, and various high-level types,
visible to the user, are based on them. Examples include ", TO "Ring", ", ", TO "RingElement", ", ", TO "Matrix", ", ", TO "Ideal", ", ",
TO "RingMap", ", ", TO "Module", ", and ", TO "ChainComplex", "."
},
PARA{
"The user can create new types of higher level mathematical objects, whose types form a hierarchy that facilitates the installation and use of
appropriate methods for computing with them."
},
Subnodes => {
TO "variables",
"basic data types",
TO "numeric types",
TO "strings and nets",
TO "lists and sequences",
TO "hash tables",
"expressions",
TO "operators",
TO "conditional execution",
TO "while",
TO "for",
TO "mapping over hash tables",
TO "error handling",
TO "catch",
"functions",
TO "using functions",
TO "using functions with optional inputs",
TO "making functions",
TO "local variables in a function",
TO "making functions with a variable number of arguments",
TO "making functions with multiple return values",
TO "making new functions with optional arguments",
TO "using hooks",
TO "code",
"classes and types",
TO "what a class is",
TO "installing methods",
TO "binary methods",
TO "inheritance",
TO "making new classes",
TO "new",
TO "printing and formatting for new classes",
TO "making a new method function",
TO "methods",
"input and output",
TO "printing to the screen",
TO "reading files",
TO "getting input from the user",
TO "creating and writing files",
TO "saving polynomials and matrices in files",
TO "two dimensional formatting",
TO "file manipulation",
TO "communicating with programs",
TO "using sockets",
"packages",
TO "packages",
TO "creating a package",
TO "writing documentation",
"parallel programming ",
TO "parallel programming with threads and tasks",
"system facilities",
TO "system facilities",
"debugging",
TO "debugging",
}
}
------------------------------------------------------------------
document {
Key => "saving polynomials and matrices in files",
"Use the function ", TO "toString", " to convert polynomials, matrices, and other mathematical
objects to a linear format that can be saved in a file.",
EXAMPLE lines ///
R = QQ[x..z]
p = (x-y-1)^3
m = matrix {{x^2, x^2-y^2, x*y*z^7 }}
M = image m
f = temporaryFileName()
f << toString (p,m,M) << close
get f
(p',m',M') = value get f
p == p'
m == m'
M == M'
removeFile f
///
}
document {
Key => "variables",
"Valid names for symbols may be constructed using letters, digits, and
the apostrophe, and should not begin with a digit.",
EXAMPLE {
"x",
"q0r55",
"f'"
},
"Some symbols have preassigned meanings and values. For example, symbols
consisting of the letter ", TT "o", " followed by a number are used
to store output values.",
EXAMPLE {
"o2",
},
"Other symbols refer to functions built into Macaulay2 that provide
much of its functionality.",
PARA{},
"Sometimes, one wants the symbol, rather than the particular value it
happens to have at the moment. For example after setting the value of
", TT "x", " to 5, the value of the variable ", TT "x", " is no longer the
symbol ", TT "x", ".
Use ", TO "symbol", " to recover the symbol itself.",
EXAMPLE {
"x=5",
"x",
"symbol x"
},
SeeAlso => {
"symbol",
"local",
"global",
listUserSymbols,
clearOutput,
clearAll,
Symbol
},
Subnodes => {
TO "valid names",
TO "assigning values",
TO "local variables in a file",
TO "viewing the symbols defined so far",
TO "subscripted variables",
TO "numbered variables",
TO "augmented assignment"
}
}
document {
Key => "numeric types",
HEADER3 "Integers and rational numbers",
"In Macaulay2, integers and rational numbers have any number of digits
(up to memory limits at least).",
EXAMPLE {
"21672378126371263123123",
"3748568762746238746278/5876584978947",
},
"Integers are elements of the ring ", TO ZZ, " of integers, and rational
numbers are elements of the ring ", TO QQ, " of rational numbers.",
PARA{},
"One point to notice is that there are two kinds of division, ", TO symbol/,
" and ", TO symbol//, ". The first
returns a rational number (element of ", TO QQ,
"), while the second does division in ", TO ZZ, ".",
EXAMPLE {
"6/3",
"7//3"
},
HEADER3 "Real and complex numbers",
"Real and complex numbers are approximate numbers,
implemented using the ", TO "MPFR", " library.",
EXAMPLE {
"1.372489274987",
"1.3454353 * 10^20",
"sqrt 4.5",
"toRR_200 4.5",
"sqrt oo"
},
EXAMPLE {
"1/(1+ii)",
},
SeeAlso => { Number, ZZ, QQ, RR, CC, toRR, toCC }
}
------------------------------------------------------------------
document {
Key => "valid names",
"Valid names for symbols may be constructed using letters, digits, and
the apostrophe, and should not begin with a digit.",
EXAMPLE {
"x",
"q0r55",
"f'"
},
"Some symbols have preassigned meanings and values. For example, symbols
consisting of the letter ", TT "o", " followed by a number are used
to store output values.",
EXAMPLE {
"o2",
},
"Other symbols refer to functions built into Macaulay2 that provide
much of its functionality, as we will learn.",
PARA{},
"The class of all symbols is ", TO "Symbol", "."
}
document {
Key => "assigning values",
"Use an equal sign to assign values to variables.",
EXAMPLE {
"x = \"abcde\"",
"x"
},
"Before assignment, any reference to a variable provides the symbol
with that name. After assignment, the assigned value is provided.
The variable created is global, in the sense that any code placed
elsewhere that contains a reference to a variable called ", TT "x", "
will refer to the one we just set.",
PARA{},
"It is important to distinguish between a symbol and its value. The
initial value of a global symbol is the symbol itself, and the initial
value of a local variable is ", TO "null", ". One possibility for
confusion comes from the possibility of having a symbol whose value is
another symbol; it's even more confusing if the two symbols have the
same name but different scopes, for example, if one of them is global
and the other is local.",
EXAMPLE {
"y",
"y = z",
"y",
"z = 444",
"z",
"y"
},
"In the example above, the final value of ", TT "y", " is the
symbol ", TT "z", ", even though the symbol z has acquired a
value of its own. The function ", TT "value", " can be used to
get the value of a symbol.",
EXAMPLE {
"value y"
},
"The operator ", TO "<-", " can be used to set the value of a
symbol. This operator differs from ", TO "=", " in that
the symbol or expression on the left hand side is evaluated.",
EXAMPLE {
"y <- 555",
"y",
"z",
"y = 666",
"y",
"z"
},
"One reason the user needs to understand this concept is that
assignments with the operator ", TO "<-", " are occasionally done
on the user's behalf by bits of code already in the system,
for example, when creating a polynomial ring the prospective
variables are given new values that are the generators of the
polynomial ring.",
PARA{},
SeeAlso => { "GlobalAssignHook", "GlobalReleaseHook" }
}
document {
Key => "local variables in a file",
"There is a way to construct variables that can be used within a given
source file, and are invisible to code placed in other files. We use
", TO ":=", " for this. Assume the code below is placed in a file, and
that the file is loaded with the ", TO "load", " command.",
EXAMPLE {
"ff := 5",
"ff"
},
"The variable above is a local one. Its value is not available to code
in other files.",
PARA{},
"Assume the code above is entered directly by the user into Macaulay2. Then
the variable is still a local one and is not available to code previously loaded
or to functions previously defined, but it will be available to code
loaded subsequently. We illustrate this below with the variable ", TT "j", ".",
PARA{},
EXAMPLE {
"j",
"h = () -> j",
"h()",
"j = 444",
"h()",
"j := 555",
"h()",
"j"
}
}
document {
Key => "viewing the symbols defined so far",
"After using Macaulay2 for a while, you may have stored data in
several variables. The system also stores output values for you
in output variables. You may wish to free the memory space occupied
by that data, or you may simply wish to remind yourself where you
stored a previously computed value.",
PARA{},
"We may use the command ", TO "listUserSymbols", " to obtain a list of the user's
symbols encountered so far.",
EXAMPLE {
"ff = 20:4; h = true; kk",
"listUserSymbols"
},
"The symbols are listed in chronological order, and the type of value stored
under it is displayed.",
PARA{},
"We can clear the output symbols with ", TO "clearOutput", ".",
EXAMPLE {
"clearOutput",
"listUserSymbols"
},
"We can clear all the symbols with ", TO "clearAll", ".",
EXAMPLE {
"clearAll",
"listUserSymbols"
},
}
document {
Key => "subscripted variables",
"It is common in mathematics to use subscripted variables. We use the underscore
to represent subscripts. If we haven't assigned a value to ", TT "x", "
we may simply start using it as a subscripted variable. The subscripts can be
anything.",
EXAMPLE {
"x",
"x_4",
"x_(2,3)",
},
"The ", TO "..", " operator knows what to do with subscripted variables.",
EXAMPLE {
"x_10 .. x_20",
"x_(1,1) .. x_(2,3)",
},
"Values can be assigned to these variables with ", TO "=", ".",
EXAMPLE {
"y_10 = 555;",
"y_10",
},
SeeAlso => {"IndexedVariable"}
}
document {
Key => "numbered variables",
"One way to get many variables suitable for use as indeterminates in
a polynomial ring is with the function ", TO "vars", ". It converts
a list or sequence of integers into symbols. It prefers to hand out
symbols whose name consists of a single letter, but there are only 52
such symbols, so it also uses symbols such as ", TT "x55", "
and ", TT "X44", ".",
EXAMPLE {
"vars (0 .. 9,40,100,-100)"
},
"These variables can be used to make polynomial rings.",
EXAMPLE {
"ZZ[vars(0 .. 10)]"
}
}
document {
Key => "using functions",
"There are many functions in Macaulay2 that do various things. You can
get a brief indication of what a function does by typing a ", TT "?", " before
its name. In this case, one would see that the function ", TO "sin", " takes a single argument
", TT "x", ". We apply a function to its argument by typing them in adjacent
positions. It is possible but not necessary to place parentheses around
the argument.",
EXAMPLE {
"sin 1.2",
"sin(1.2)",
"sin(1.0+0.2)",
},
"In parsing the operator ", TO "^", " takes precedence over adjacency, so the
function is applied after the power is computed in the following code. This
may not be what you expect.",
EXAMPLE "print(10 + 1)^2",
"Some functions take more than one argument, and the arguments are separated
by a comma, and then parentheses are needed.",
EXAMPLE {
"append",
"append({a,b,c},d)"
},
"Some functions take a variable number of arguments.",
EXAMPLE {
"join",
"join({a,b},{c,d},{e,f},{g,h,i})"
},
"Functions, like anything else, can be assigned to variables. You may do this
to provide handy private abbreviations.",
EXAMPLE {
"ap = append;",
"ap({a,b,c},d)"
},
}
document {
Key => "making functions",
"The operator ", TO "->", " is used to make new functions. On its left
we provide the names of the parameters to the function, and to the
right we provide the body of the function, an expression involving
those parameters whose value is to be computed when the function
is applied. Let's illustrate this by making a function for squaring
numbers and calling it ", TT "sq", ".",
EXAMPLE {
"sq = i -> i^2",
"sq 10",
"sq(5+5)",
},
"When the function is evaluated, the argument is evaluated and assigned
temporarily as the value of the parameter ", TT "i", ". In the example
above, ", TT "i", " was assigned the value ", TT "10", ", and then the
body of the function was evaluated, yielding ", TT "100", ".",
PARA{},
"Here is how we make a function with more than one argument.",
EXAMPLE {
"tm = (i,j) -> i*j",
"tm(5,7)",
},
"Functions can be used without assigning them to variables.",
EXAMPLE {
"(i -> i^2) 7",
},
"Another way to make new functions is to compose two old ones
with the operator ", TO "@@", ".",
EXAMPLE {
"sincos = sin @@ cos",
"sincos 2.2",
"sin(cos(2.2))",
},
"Code that implements composition of functions is easy to write, because
functions can create new functions and return them. We illustrate
this by writing a function called ", TT "comp", " that will
compose two functions, just as the operator ", TO "@@", " did
above.",
EXAMPLE {
"comp = (f,g) -> x -> f(g x)",
"sincos = comp(sin,cos)",
"cossin = comp(cos,sin)",
"sincos 2.2",
"cossin 2.2",
},
"We created two composite functions in the example above to illustrate an
important point. The parameters ", TT "f", " and ", TT "g", " acquire
values when ", TT "sincos", " is created, and they acquire different values when
", TT "cossin", " is created. These two sets of values do not interfere
with each other, and the memory they occupy will be retained as long
as they are needed. Indeed, the body of both functions is
", TT "x -> f(g(x))", ", and the only difference between them is the
values assigned to the parameters ", TT "f", " and ", TT "g", ".",
PARA{},
"The class of all functions is ", TO "Function", "."
}
document {
Key => "making functions with multiple return values",
PARA {
"A function may return multiple values by returning ", ofClass Sequence, ".
Multiple assignment (see ", TO "=", ") or multiple local assignment (see ", TO ":=", ")
can be used to assign the values to separate variables, if the number of values to be returned
is known."
},
EXAMPLE lines ///
f = i -> (i, i^2, i^3);
(x,y,z) = f 3
x
y
z
///,
PARA {
"Simple assignment may be used if the number of values to be returned is unknown."
},
EXAMPLE lines ///
s = f 4
#s
s_0
s_1
s_2
///
}
document {
Key => "making functions with a variable number of arguments",
"It is easy to write a function with a variable number of arguments.
Define the function with just one parameter, with no parentheses around
it. If the function is called with several arguments, the value of the
single parameter will be a sequence containing the several arguments;
if the function is called with one argument, the value of the parameter
will be that single argument.",
EXAMPLE {
"f = x -> {class x, if class x === Sequence then #x};",
"f()",
"f(3)",
"f(3,4)",
"f(3,4,5)",
},
"We could use the function ", TO "sequence", " to bring the case where there
is just one argument into line with the others. It will enclose anything
that is not a sequence in a sequence of length one.",
EXAMPLE {
"f = x -> (
x = sequence x;
{class x, #x});",
"f()",
"f(3)",
"f(3,4)",
"f(3,4,5)",
},
"As an aside, we reveal that there is a way to define a function of one argument
that will signal an error if it's given more than one argument: put
parentheses around the single parameter in the definition of the function.
As a side effect it can be used to extract the single element from a
singleton sequence.",
EXAMPLE {
"((x) -> x) 3",
"1 : 3",
"((x) -> x) oo",
}
}
document {
Key => "making new functions with optional arguments",
"Let's consider an example where we wish to construct a linear function of ", TT "x", "
called ", TT "f", ", with the slope and y-intercept of the graph being optional
arguments of ", TT "f", ". We use the ", TO ">>", " operator to attach the default
values to our function, coded in a special way.",
EXAMPLE {
"opts = {Slope => 1, Intercept => 1}",
"f = opts >> o -> x -> x * o.Slope + o.Intercept",
"f 5",
"f(5, Slope => 100)",
"f(5, Slope => 100, Intercept => 1000)",
},
"In the example the function body is the code ", TT "x * opts.Slope + opts.Intercept", ".
When it is evaluated, a hash table is assigned to ", TT "opts", "; its
keys are the names of the optional arguments, and the values
are the corresponding current values, obtained either from the default values
specified in the definition of ", TT "f", ", or from the options specified at
the time ", TT "f", " is called.",
PARA{},
"In the example above, the inner function has just one argument, ", TT "x", ",
but handling multiple arguments is just as easy. Here is an example with two
arguments.",
EXAMPLE {
"f = {a => 1000} >> o -> (x,y) -> x * o.a + y;",
"f(3,7)",
"f(5,11,a=>10^20)",
},
}
document {
Key => "conditional execution",
Headline => "if-then-else statements",
"The basic way to control the execution of code is with the ", TO "if", "
expression. Such an expression typically has the form
", PRE "if X then Y else Z", "
and is evaluated as follows. First ", TT "X", " is evaluated. If the
result is ", TT "true", ", then the value of ", TT "Y", " is provided,
and if the result is ", TT "false", ", then the value of ", TT "Z", "
is provided. An error is signalled if the value of ", TT "X", " is
anything but ", TT "true", " or ", TT "false", ".",
EXAMPLE {
///(-4 .. 4) /
(i -> if i < 0 then "neg"
else if i == 0 then "zer"
else "pos")///
},
"The else clause may be omitted from an ", TT "if", " expression. In that case,
if value of the predicate ", TT "X", " is false, then ", TO "null", " is provided
as the value of the ", TT "if", " expression.",
EXAMPLE {
///(-4 .. 4) /
(i -> if i < 0 then "neg"
else if i == 0 then "zer")///
},
"There are a variety of predicate functions (such as ", TT "<", ", used above)
that yield ", TT "true", " or ", TT "false", " and can be used as the predicate
in an ", TT "if", " expression. For a list, see ", TO "Boolean", ". Boolean
results may be combined with ", TO "not", ", ", TO "and", ", and ", TO "or", "."
}
document {
Key => "then",
Headline => "condition testing",
TT "then", " a keyword used with ", TO "if", ".",
EXAMPLE {
"if 5 > 4 then 8 else 7"
},
SeeAlso => {"if", "else"}
}
document {
Key => "else",
Headline => "condition testing",
TT "else", " a keyword used with ", TO "if", ".",
EXAMPLE {
"if 4 > 5 then 8 else 7"
},
SeeAlso => {"if", "then"}
}
document {
Key => {"throw", "catch"},
Headline => "throw and catch exceptions",
Usage => "catch c\nthrow x",
Outputs => {{
TT "catch", " returns the value obtained by evaluating the code ", TT "c",
", or, if a ", TT "throw", " was executed during the evaluation of ", TT "c",
", the argument given to ", TT "throw", "."}},
Consequences => {{
TT "throw", " passes the flow of control to the surrounding ", TT "catch",
", and ", TT "x", " is returned as its value"}},
EXAMPLE lines ///
catch scan(0..10, i -> if i == 5 then throw 18 else print i)
///}
document { Key => "continue",
Headline => "continue with the next iteration of a loop",
Usage => "continue x",
Inputs => {"x"},
Consequences => {
{"the currently executing ", TT "list", "-clause of a ", TO "for", "-loop or ", TO "while", "-loop is
finished, and iteration continues with the ", TO "do", "-clause or the next iteration of
the loop, if any. The value ", TT "x", " is added to the list being accumulated.
If ", TT "x", " is omitted, then no value is added to the list, and the statement may be used in a ", TT "do", "-clause."
},
{"Alternatively, as a debugger command, causes execution to be resumed, starting with the current expression."}
},
EXAMPLE lines ///
for i from 1 to 4 list (continue 4; print ho) do print hi
for i from 1 to 4 list (continue ; 14) do print hi
for i from 1 to 4 list 14 do print hi
i = 0 ; while i < 10 do ( i = i+1; if i == 5 then continue ; print i )
///,
PARA {
"Here is an example of the use of ", TO "continue", " in the debugger after altering a value so continuation will not cause the
error to recur."
},
EXAMPLE lines ///
load "Macaulay2Doc/demo1.m2"
code g
g 2
code f
x
x = 11
continue
///
}
document { Key => "when",
Headline => "a keyword",
"A keyword used in ", TO "for", " loops."
}
document {
Key => "if",
Headline => "condition testing",
TT "if p then x else y", " computes ", TT "p", ", which must yield the value ", TO "true", "
or ", TO "false", ". If true, then the value of ", TT "x", " is provided,
else the value of ", TT "y", " is provided.",
PARA{},
TT "if p then x", " computes ", TT "p", ", which must yield the value ", TO "true", "
or ", TO "false", ". If true, then the value of ", TT "x", " is provided,
else ", TO "null", " is provided."
}
document {
Key => "return",
Headline => "return from a function",
Usage => "return x",
Inputs => {
"x" => {"If ", TT "x", " is omitted, then ", TO "null", " is used."}
},
Consequences => {
{"Returns ", TT "x", " as the value of the function currently being evaluated."},
{"Alternatively, as a debugger command, returns ", TT "x", " as the value of the
current expression and stops execution at the next possible point, entering
the debugger again."}
},
EXAMPLE {
"f = x -> (
if x == 3 then return;
if x > 3 then return x^2;
5);",
"f 2",
"f 3",
"f 4"
},
PARA {
"Here is an example of the use of ", TO "return", " as a debugger command."
},
EXAMPLE lines ///
load "Macaulay2Doc/demo1.m2"
code g
g 2
code f
return 1/11
continue
///,
SeeAlso => { "break" }
}
document {
Key => "list",
Headline => "loop control",
TT "list", " a keyword used with ", TO "while", ", and ", TO "for", "."
}
document {
Key => "from",
Headline => "loop control",
TT "from", " a keyword used with ", TO "for", " and ", TO "new", "."
}
document {
Key => "to",
Headline => "loop control",
TT "to", " a keyword used with ", TO "for", "."
}
document {
Key => "do",
Headline => "loop control",
TT "do", " a keyword used with ", TO "while", ", and ", TO "for", "."
}
document {
Key => "try",
Headline => "catch an error",
Usage => "try x then y else z",
Inputs => {
"x" => "code",
"y" => "code",
"z" => "code"
},
Consequences => {
{"the code ", TT "x", " is run; if no error or ", TO "alarm", " occurs, then the code ", TT "y", " is run; otherwise, the code ", TT "z", " is run"}
},
"The return value is the value returned by ", TT "y", " or ", TT "z", ", as the case may be.",
PARA{},
"The clause '", TT "then y", "' may be omitted, in which case the return value is the value returned by ", TT "x", ", if there is no error or alarm.",
PARA{},
"The clause '", TT "else z", "' may be omitted,
in which case the return value is the value returned by ", TT "y", ",
unless an error or alarm occurs, in which case ", TO "null", " is returned.",
PARA{},
"The clauses '", TT "then y else z", "' may both be omitted, in which case the return value is the value returned by ", TT "x", ", unless an error or
alarm occurs, in which case ", TO "null", " is returned.",
PARA{},
"The behavior of interrupts (other than alarms) is unaffected.",
EXAMPLE "apply(-3..3,i->try 1/i else infinity)",
Caveat => "We will change the behavior of this function soon so that it will be possible to catch errors of a particular type. Meanwhile, users are
recommended to use this function sparingly, if at all."
}
document {
Key => "break",
Headline => "break from a loop",
Usage => "break x",
Consequences => {
{ "this interrupts execution of a loop controlled by ", TO "for", ", ", TO "while", ", ", TO "apply", ", or ", TO "scan", ", returning ",
TT "x", " as the value of the loop currently being evaluated."
},
{ "Alternatively, as a top level command in the debugger, it leaves the debugger, returning the user to top level." }
},
PARA {
"Omitting ", TT "x", ", and executing ", TT "break", ", interrupts execution of a loop as above, returning
", TO "null", " as the value of the function currently being evaluated, except, in the case of
a ", TO "for", " loop or a ", TO "while", " loop with a list clause, the list accumulated so far is returned as the value."
},
EXAMPLE "for i from 1 to 10 do if i == 7 then break 12345",
PARA {
"Warning: trying to break from a loop controlled by ", TO "table", " will probably not do what you
expect, since ", TO "table", " is implemented by two nested
loops controlled by ", TO "apply", ", and only the inner one will stop, as in the following example."
},
EXAMPLE lines ///
table(3,3,(i,j) -> if i == 1 then break 3 else "hi")
table(3,3,(i,j) -> if j == 1 then break 3 else "hi")
///,
PARA {
"Here is an example as a debugger command."
},
EXAMPLE lines ///
load "Macaulay2Doc/demo1.m2"
g 2
x
break
g 3
///,
SeeAlso => { "apply", "scan", "while", "for" }
}
document {
Key => "while",
Headline => "while loops",
Usage => "while p list x do z",
Consequences => {
{ "The expression ", TT "p", " is repeatedly evaluated. As long as the result is true, ", TT "x", " is evaluated and its value is saved, and
", TT "z", " is evaluated and its value is discarded. When the value of ", TT "p", " is false, then the loop terminates, and the list
of values of ", TT "x", " is returned as the value of the entire expression."
}},
EXAMPLE lines ///
i = 0 ; while i < 10 list i^2 do i = i+1
///,
PARA { "The ", TT "list x", " clause may be omitted, in which case no list is accumulated, and ", TO "null", " is returned as the
value of the expression." },
EXAMPLE lines ///
i = 0 ; while i < 4 do (print i; i = i+1)
///,
PARA { "Alternatively, the ", TT "do z", " clause may be omitted." },
EXAMPLE lines ///
i = 0 ; while i < 10 list (i = i+1; i^2)
///,
PARA {
"Observe the use of the semicolon (see ", TT ";", ") in the expression above."
},
PARA { "If ", TO "continue", " is executed by ", TT "x", " then execution of ", TT "x", " is interrupted, no value is added to the list, and iteration of the loop
continues."
},
EXAMPLE lines ///
i = 0 ; while i < 10 list (i = i+1; if odd i then continue; i^2)
///,
PARA { "If ", TT "continue w", " is executed by ", TT "x", " then execution of ", TT "x", " is interrupted, the value of ", TT "w", " is added to the list,
and iteration of the loop continues."
},
EXAMPLE lines ///
i = 0 ; while i < 10 list (i = i+1; if odd i then continue 1234; i^2)
///,
PARA { " If ", TT "break v", " is executed by ", TT "x", ", then the loop is stopped and ", TT "v", " is returned as its value." },
EXAMPLE lines ///
i = 0 ; while i < 10 list (i = i+1; if i == 5 then break i; i^2)
///,
PARA { "If ", TO "break", " is executed by ", TT "x", ", then the loop is stopped and the list accumulated so far is returned as the value." },
EXAMPLE lines ///
i = 0 ; while i < 10 list (i = i+1; if i == 5 then break; i^2)
///,
}
document { Key => "in",
Headline => "a keyword used in for-loops",
SeeAlso => {"for"}
}
document { Key => "for",
Headline => "for loops",
SYNOPSIS (
Usage => "for i from m to n when p list x do z",
Inputs => { "m" => ZZ, "n" => ZZ },
Outputs => {{"the list of values of the clause ", TT "x", ", as described above"}},
Consequences => {
{"The numbers ", TT "m", " and ", TT "n", " must be small integers that fit into a single word.
The variable ", TT "i", " is initialized to ", TT "m", ".
As long as ", TT "i", " is not greater than ", TT "n", ", evaluation of the loop continues.
First ", TT "p", " is evaluated;
as long as the value of ", TT "p", " is true, evaluation of the loop continues.
Next ", TT "x", " is evaluated and its value is saved, and ", TT "z", " is evaluated and its value is discarded.
Then ", TT "i", " is incremented by 1, and the loop repeats.
When the value of ", TT "p", " is false, then the loop terminates, and the list of values of ", TT "x", " is returned as the value of the entire expression."
}}),
SYNOPSIS (
Usage => "for i in v when p list x do z",
Inputs => {"v" => {ofClass{BasicList},
" or an instance of a class with the ", TO iterator,
" method installed"}},
Consequences => {{"The variable ", TT "i", " is set to consecutive values of the list ", TT "v", ". First ", TT "p", " is evaluated.
As long as the value of ", TT "p", " is true, evaluation of the loop continues. Next ", TT "x", " is evaluated, and its value is saved. Then
", TT "z", " is evaluated and its value is discarded. Then the loop repeats with the next element of ", TT "v", ". When the value of ", TT "p", " is false,
then the loop terminates, and the list of values of ", TT "x", " is returned as the value of the entire expression."
}},
),
Caveat => {
"The variable ", TT "i", " is a new local variable whose scope includes only the expressions ", TT "p", ", ", TT "x", ",
and ", TT "z", " in the body of the loop. Moreover, new local variables defined inside the body of the loop will not
be visible outside it."
},
SUBSECTION "examples",
EXAMPLE lines ///
for i from 1 to 5 when i < 15 list i^2 do print i
for i from 1 to 5 when i^2 < 15 list i^2 do print i
///,
PARA {
"The expressions in this construction may be arbitrarily complicated.
Here is an example where ", TT "z", " is a sequence of expressions separated by semicolons (see ", TO ";", ")."
},
EXAMPLE ///for i from 1 to 3 do (
print "The value of i is : ";
print i
)
///,
PARA { "The ", TT "do z", " clause may be omitted." },
EXAMPLE "for i from 1 to 5 when i < 15 list i^2",
PARA { "The ", TT "from m", " clause may be omitted, in which case ", TT "i", " starts with ", TT "0", "." },
EXAMPLE "for i to 5 when i < 15 list i^2",
PARA { "The ", TT "when p", " clause may be omitted." },
EXAMPLE "for i to 5 list i^2",
PARA { "The ", TT "to n", " clause may be omitted." },
EXAMPLE "for i when i < 15 list i^2",
PARA { "The ", TT "list x", " clause may be omitted." },
EXAMPLE "for i when i^2 < 15 do print i",
PARA { "If ", TO "continue", " is executed by ", TT "x", " then execution of ", TT "x", " is interrupted, no value is added to the list, and iteration of the loop
continues."
},
EXAMPLE "for i from 0 when i < 10 list (if odd i then continue; i^2)",
PARA { "If ", TT "continue w", " is executed by ", TT "x", " then execution of ", TT "x", " is interrupted, the value of ", TT "w", " is added to the list,
and iteration of the loop continues."
},
EXAMPLE "for i from 0 when i < 10 list (if odd i then continue 4567; i^2)",
PARA { " If ", TT "break v", " is executed by ", TT "x", ", then the loop is stopped and ", TT "v", " is returned as its value." },
EXAMPLE lines "for i from 0 when i < 10 list (if i== 5 then break i; i^2)",
PARA { "If ", TO "break", " is executed by ", TT "x", ", then the loop is stopped and the list accumulated so far is returned as the value." },
EXAMPLE "for i from 0 when i < 10 list (if i== 5 then break; i^2)",
EXAMPLE "for i in 0..3 list i^2",
PARA { "If ", TT "v", " is an instance of any class with the ",
TO iterator, " method installed (e.g., a string), then the values of ",
TT "i", " are obtained by repeatedly calling ", TO next,
" on the output of ", TT "iterator v", " until ", TO StopIteration,
" is returned."},
EXAMPLE ///for i in "foo" do print i///
}
document {
Key => "local variables in a function",
"A local variable in a function is one that is not visible to
code in other locations. Correct use of local variables is
important, for data stored in global variables will stay around
forever, needlessly occupying valuable memory space. For recursive
functions especially, it is important to use local variables so that
one invocation of the function doesn't interfere with another.",
PARA{},
"The simplest way to create a local variable in a function is with
the assignment operator ", TO ":=", ". The expression ", TT "X := Y", "
means that the value of ", TT "Y", " will be assigned to a newly created local
variable whose name is ", TT "X", ". Once ", TT "X", " has been created, new values can
be assigned to it with expressions like ", TT "X = Z", ".",
EXAMPLE {
"i = 22;",
///f = () -> (i := 0; while i<9 do (<< i << " "; i=i+1); <<endl;)///,
"f()",
"i"
},
"In the example above, we see that the function ", TT "f", " does
its work with a local variable ", TT "i", " that is unrelated to the global
variable ", TT "i", " introduced on the first line.",
PARA{},
"In the next example, we show that the local variables in two
invocations of a function don't interfere with each other. We
write a function ", TT "f", " that returns a newly created counting function.
The counting function simply returns the number of times it has
been called.",
EXAMPLE {
"f = () -> (i := 0; () -> i = i+1)",
},
"Let's use ", TT "f", " to create counting functions and show that they operate
independently.",
EXAMPLE {
"p = f()",
"q = f()",
"p(),p(),p(),p(),q(),p(),p(),q(),p(),p()"
}
}
document {
Key => "hash tables",
"A hash table is a data structure that can implement a function
whose domain is a finite set. An element of the domain is called
a key. The hash table stores the key-value pairs in such a way
that when presented with a key, the corresponding value can be
quickly recovered.",
PARA{},
"A dictionary could be implemented as a hash table: the keys would
be the words in the language, and the values could be the definitions
of the words.",
PARA{},
"A phone book could also be implemented as a hash table: the keys would
be the names of the subscribers, and the values could be the corresponding
phone numbers. (We exclude the possibility of two subscribers with
the same name.)",
PARA{},
"As an example we implement a phone book.",
EXAMPLE {
///book = new HashTable from {
"Joe" => "344-5567",
"Sarah" => "567-4223",
"John" => "322-1456"}///,
},
"We use the operator ", TO "#", " to obtain values from the phone book.",
EXAMPLE ///book#"Sarah"///,
"The operator ", TO "#?", " can be used to tell us whether a given key
has an entry in the hash table.",
EXAMPLE ///book#?"Mary"///,
"We have implemented the notion of set via hash tables in which every value
is the number 1.",
EXAMPLE {
"x = set {a,b,c,r,t}",
"peek x",
"x#?a",
"x#?4",
},
"There is a type of hash table that is mutable, i.e., a hash table
whose entries can be changed. They are changed with assignment
statements of the form ", TT "x#key=value", ".",
EXAMPLE {
///x = new MutableHashTable;///,
///x#"Joe" = "344-5567";///,
///x#3 = {a,b,c};///,
///x#{1,2} = 44;///,
///x#3///,
///x#?4///,
},
"When a mutable hash table is printed, its contents are not displayed.
This prevents infinite loops in printing routines.",
EXAMPLE "x",
"Use ", TO "peek", " to see the contents of a mutable hash table.",
EXAMPLE "peek x",
"A variant of ", TO "#", " is ", TO ".", ". It takes only global symbols
as keys, and ignores their values.",
EXAMPLE {
"p=4;",
"x.p = 444;",
"x.p",
"x#?4"
},
Subnodes => {
"functions for manipulating hash tables",
TO "copy",
TO "hashTable",
TO "keys",
TO "values",
TO "pairs",
TO "mutable",
TO "remove",
TO "applyKeys",
TO "applyValues",
TO "applyPairs",
TO "merge",
TO "combine",
"more information",
TO "hashing",
TO "HashTable",
TO "MutableHashTable"
}
}
binaryOperators := core "binaryOperators"
prefixOperators := core "prefixOperators"
postfixOperators := core "postfixOperators"
flexibleBinaryOperators := core "flexibleBinaryOperators"
flexiblePrefixOperators := core "flexiblePrefixOperators"
flexiblePostfixOperators := core "flexiblePostfixOperators"
augmentedAssignmentOperators := core "augmentedAssignmentOperators"
allOperators := core "allOperators"
getParsing := core "getParsing"
document {
Key => "operators",
PARA {
"Here we present all of the unary and binary operators in the language. They are members of the class ", TO "Keyword", ".
The binary operators are ",
between_" " apply(sort toList binaryOperators, s -> TO {s}),
" , the unary prefix operators are ",
between_" " apply(sort toList prefixOperators, s -> TO {s}),
" , and the unary postfix operators are ",
between_" " apply(sort toList postfixOperators, s -> TO {s}),
" ."
},
PARA {
"Of those, the ones for which users may install new methods are ",
"the binary operators ",
between_" " apply(sort toList join(flexibleBinaryOperators,
augmentedAssignmentOperators), s -> TO {s}),
" , the unary prefix operators ",
between_" " apply(sort toList flexiblePrefixOperators, s -> TO {s}),
" , and the unary postfix operators ",
between_" " apply(sort toList flexiblePostfixOperators, s -> TO {s}),
" ."},
Subnodes => splice {
"assignment",
TO symbol = ,
TO symbol := ,
TO symbol <- ,
"equality testing",
TO symbol == ,
TO symbol != ,
TO symbol === ,
TO symbol =!= ,
"comparison of objects",
TO symbol < ,
TO symbol <= ,
TO symbol > ,
TO symbol >= ,
TO symbol ? ,
"predicates",
TO symbol and ,
TO symbol or ,
TO symbol not ,
"functions",
TO symbol SPACE,
TO symbol -> ,
TO symbol @@ ,
TO symbol \\ ,
TO symbol \ ,
"subscripting and object access",
TO symbol _ ,
TO symbol . ,
TO symbol .? ,
TO symbol # ,
TO symbol #? ,
"arithmetic operators",
TO symbol ! ,
TO symbol + ,
TO symbol - ,
TO symbol * ,
TO symbol / ,
TO symbol // ,
TO symbol % ,
TO symbol ^ ,
TO symbol & ,
TO symbol ++ ,
TO symbol ** ,
TO symbol ^** ,
TO symbol ~ ,
TO symbol (*) ,
TO symbol : ,
"miscellaneous operators",
TO symbol .. ,
TO symbol ..< ,
TO symbol << ,
TO symbol >> ,
TO symbol | ,
TO symbol || ,
TO symbol @ ,
TO symbol ^^ ,
TO symbol ?? ,
TO symbol |- ,
TO symbol <==> ,
TO symbol ===> ,
TO symbol ==> ,
TO symbol <=== ,
TO symbol <== ,
"built in operators" ,
TO symbol => ,
TO symbol , ,
TO symbol ;,
"augmented assignment",
toSequence apply(sort toList augmentedAssignmentOperators,
op -> TO op),
"further information",
TO "precedence of operators",
TO "operatorAttributes"
}
}
document {
Key => "precedence of operators",
SeeAlso => { "parsing precedence, in detail" },
PARA {
"One aspect of parsing precedence is associativity. A ", EM "left-associative", " operator is one, such as ", TO "*", ", with
the property that ", TT "x * y * z", " is parsed as ", TT "(x * y) * z", ".
A ", EM "right-associative", " operator is one, such as ", TO "=", ", with the property that ", TT "x = y = z", " is parsed
as ", TT "x = (y = z)", ". These operators are left associative: ",
between_" " apply(sort select(allOperators, s -> (getParsing s)#0 == (getParsing s)#1), s -> TO {s}),
", and these operators are right associative: ",
between_" " apply(sort select(allOperators, s -> (getParsing s)#0 == (getParsing s)#1 + 1), s -> TO {s}),
" ."
},
PARA {
"Here are the operators arranged in order of increasing parsing precedence. For example,
", TT "*", " has higher parsing precedence than ", TT "+", ", and hence ", TT "2+3*5", " is
parsed as though it had been written as ", TT "2+(3*5)", ". The symbol ", TO "SPACE", " represents the operator that is used when
two things are adjacent in program code."
},
TABLE { "class" => "examples", TR TD PRE net (core "seeOperatorPrecedence")() }
}
sp := seeParsing()
document {
Key => seeParsing,
Usage => "seeParsing()",
Headline => "display parsing precedence table for Macaulay2 operators",
PARA {
"We intend to eliminate this function in favor of ", TO "operatorAttributes", "."
},
SeeAlso => {"parsing precedence, in detail"}
}
document {
Key => "parsing precedence, in detail",
"A newline ends a statement if it can, otherwise it acts like any
white space.",
EXAMPLE "2+\n3+\n4",
PARA{},
"Parsing is determined by a triple of numbers attached to each token.
The following table (produced by the command ", TO "seeParsing", "),
displays each of these numbers.",
TABLE { "class" => "examples", TR TD PRE { toString net sp} },
"Here is the way these numbers work. The parser maintains a number
which called the current parsing level, or simply, the level.
The parser builds up an expression until it encounters an input token
whose parsing precedence is less than or equal to the current level.
The tokens preceding the offending token are bundled into an expression
appropriately and incorporated into the containing expression.",
PARA{},
"When an operator or token is encountered, its binding strength serves
as the level for parsing the subsequent expression, unless the current level
is higher, in which case it is used.",
PARA{},
"Consider a binary operator such as ", TT "*", ". The relationship between
its binary binding strength and its parsing precedence turns out to determine
whether ", TT "a*b*c", " is parsed as ", TT "(a*b)*c", " or as ", TT "a*(b*c)", ".
When the parser encounters the second ", TT "*", ", the current parsing level
is equal to the binding strength of the first ", TT "*", ". If the binding
strength is less than the precedence, then the second ", TT "*", " becomes
part of the right hand operand of the first ", TT "*", ", and the
expression is parsed as ", TT "a*(b*c)", ". Otherwise, the expression is
parsed as ", TT "(a*b)*c", ".",
PARA{},
"For unary operators, the unary binding strength is used instead of the binary
binding strength to reset the current level. The reason for having both numbers
is that some operators can be either unary or binary, depending on the context.
A good example is ", TO "#", " which binds as tightly as ", TO ".", "
when used as an infix operator, and binds as loosely as adjacency or
function application when used as a prefix operator.",
PARA{},
"To handle expressions like ", TT "b c d", ", where there are no tokens present
which can serve as a binary multiplication operator, after parsing ", TT "b", ",
the level will be set to 1 less than the precedence of an identifier,
so that ", TT "b c d", " will be parsed as ", TT "b (c d)", ".",
PARA{},
"The comma and semicolon get special treatment: the empty expression can
occur to the right of the comma or semicolon or to the left of the comma.",
PARA {
"One of the most unusual aspects of the parsing precedence table above is that ", TT "[", " is
assigned a precedence several steps lower than the precedence of symbols and adjacency, and also
lower than the precedence of ", TT "/", ". This was done so expressions like ", TT "R/I[x]", "
would be parsed according to mathematical custom, but it implies that expressions like ", TT "f g [x]", "
will be parsed in a surprising way, with ", TT "f g", " being evaluated first, even if ", TT "f", " and ", TT "g", "
are both functions. Suitably placed parentheses can help, as illustrated in the next example."
},
EXAMPLE lines ///
f = x -> (print x; print)
f f [1,2,3]
f f ([1,2,3])
f (f [1,2,3])
///
}
-- Local Variables:
-- compile-command: "make -C $M2BUILDDIR/Macaulay2/m2 "
-- End:
|