1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
|
<html>
<!----------------------------------------------------------------------------
Authors:
M. Dunstan (mnd@uk.ac.st-andrews)
{enter_new_authors_here}
History:
2nd December 1995:
Original version from LaTeX. (mnd)
2nd May, 1996:
Alterations of my own and from suggestions by Mike Dewar (NAG). (mnd)
{enter_major_changes_here}
---------------------------------------------------------------------------->
<head>
<title>A Tutorial Introduction To AXIOM</title>
<link rev="made" href="mailto:mnd@dcs.st-andrews.ac.uk (Martin Dunstan)">
</head>
<body>
<hr>
<center>
<h1>An Introduction To Programming In</h1>
<h1><img align=middle alt="AXIOM" src="images/axiom_logo.gif"></h1>
</center>
<hr>
<center>
<h1>By Martin N. Dunstan</h1>
May 2, 1996
</center>
<hr>
<!------------------------------------------------------>
<!-- Table of contents: use hyperlinks where possible -->
<!------------------------------------------------------>
<h2>Contents</h2>
<ol>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#pocket">Using <b>AXIOM</b> As A Pocket Calculator</a></li>
<ol>
<li><a href="#basicArith">Basic Arithmetic</a></li>
<li><a href="#typeConversionA">Type Conversion</a></li>
<li><a href="#usefulFunctions">Useful Functions</a></li>
</ol>
<li><a href="#symbolic">Using <b>AXIOM</b> As A Symbolic Calculator</a></li>
<ol>
<li><a href="#exprSymbols">Expressions Involving Symbols</a></li>
<li><a href="#complexNumbers">Complex Numbers</a></li>
<li><a href="#numberReps">Number Representations</a></li>
<li><a href="#modularArith">Modular Arithmetic</a></li>
</ol>
<li><a href="#generalPoints">General Points About <b>AXIOM</b></a></li>
<ol>
<li><a href="#compNoOutput">Computation Without Output</a></li>
<li><a href="#earlierResults">Accessing Earlier Result</a></li>
<li><a href="#splittingExprs">Splitting Expressions Over Several Lines</a></li>
<li><a href="#comments">Comments And Descriptions</a></li>
<li><a href="#typeConversionB">Control Of Result Types</a></li>
</ol>
<li><a href="#data">Data Structures In <b>AXIOM</b></a></li>
<ol>
<li><a href="#lists">Lists</a></li>
<li><a href="#segLists">Segmented Lists</a></li>
<li><a href="#Streams">Streams</a></li>
<li><a href="#arraysEtc">Arrays, Vectors, Strings and Bits</a></li>
<li><a href="#flexibleArrays">Flexible Arrays</a></li>
</ol>
<li><a href="#functionsEtc">Functions, Choices And Loops</a></li>
<ol>
<li><a href="#readFromFile">Reading Code From A File</a></li>
<li><a href="#Blocks">Blocks</a></li>
<li><a href="#functions">Functions</a></li>
<li><a href="#choices">Choices</a></li>
<li><a href="#loops">Loops</a></li>
<ol>
<li><a href="#repeat">The repeat Loop</a></li>
<li><a href="#while">The while Loop</a></li>
<li><a href="#for">The for Loop</a></li>
</ol>
</ol>
</ol>
<hr>
<!------------------------------------------------------------------------>
<!------------------------------------------------------>
<!-- List of figures: use hyperlinks where possible -->
<!------------------------------------------------------>
<h2>List Of Figures</h2>
<blockquote>
<dl compact>
<dt>2.1</dt>
<dd><a href="#figure2point1">Table of simple <b>AXIOM</b> functions.</a></dd>
<dt>2.2</dt>
<dd><a href="#figure2point2">Table of simple <b>AXIOM</b> operators.</a></dd>
<dt>2.3</dt>
<dd><a href="#figure2point3">Table of some useful <b>AXIOM</b> macros.</a></dd>
<xmp></xmp>
<dt>5.1</dt>
<dd><a href="#figure5point1">List <em>u</em> before <tt>setrest(endOfu,partOfu)</tt>.</a></dd>
<dt>5.2</dt>
<dd><a href="#figure5point2">List <em>u</em> after <tt>setrest(endOfu,partOfu)</tt>.</a></dd>
</dl>
</blockquote>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="introduction">
<h2>Chapter 1</h2>
<h2>Introduction</h2>
</a>
<p>
This document is intended to be a tutorial introduction to the <b>AXIOM</b>
(Release 2.0) system and its language. Initially the examples and
discussion will relate to the interactive system and the use of input
files will be touched on later.
</p>
<p>
Although
<a href="#jenks1992">[jenks1992]</a>
is an extremely useful book for those wanting to use the <b>AXIOM</b>
system it is not quite so helpful for learning to program in <b>AXIOM</b>.
Generally one learns a new programming language by writing short programs
("one-liners") before learning about the various flow-control
instructions such as <tt>if</tt> and <tt>while</tt>. However, learning to
use a large system such as <b>AXIOM</b> requires a text that covers a
significant part of the available features while keeping the number of
pages to a minimum as
<a href="#jenks1992">[jenks1992]</a>
does. This is not to say that
<a href="#jenks1992">[jenks1992]</a>
does not explain how to program in <b>AXIOM</b> since this is how the
author learned!
</p>
<p>
As a result the author has decided to produce this text as an introduction to
the <b>AXIOM</b> language for readers who are already familiar with at least
one programming language. Knowledge of a functional programming language
would be a benefit since many of the concepts found in functional languages
are present in <b>AXIOM</b> but is by no means essential. <b>AXIOM</b> is
<em>not</em> a functional language since it has a notion of state and
functions can (and sometimes do) have side-effects.
</p>
<p>
This document has been compiled by working through
<a href="#jenks1992">[jenks1992]</a>
in parallel with interactive sessions of <b>AXIOM</b> (a new session is
started for each chapter). The structure of the text is the author's but
the majority of the information has come from
<a href="#jenks1992">[jenks1992]</a>
in some form or another since this appears to be the
only large body of information on <b>AXIOM</b> (apart from the HyperDoc
program). However, the examples are mostly of the author's own design
and are all extracted directly from the output produced by <b>AXIOM</b> - the
code for each example was copied from the <b>LaTeX</b> version of this document
into the <b>AXIOM</b> interpreter and the results copied back. As a result
there shouldn't be any mistakes.
</p>
<hr>
<blockquote>
Please let me
(<b><tt><a href="mailto:mnd@dcs.st-andrews.ac.uk">mnd@dcs.st-andrews.ac.uk</a></tt></b>)
know of any errors or any comments/criticisms you have regarding this
document as I would like to make it as useful as possible.
</blockquote>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="pocket">
<h2>Chapter 2</h2>
<h2>Using <b>AXIOM</b> As A Pocket Calculator</h2>
</a>
<p>
At the simplest level <b>AXIOM</b> can be used as a pocket calculator where
expressions involving numbers and operators are entered directly in infix
notation. In this sense the more advanced features of the calculator can
be regarded as operators (e.g. <b>sin</b>, <b>cos</b> etc.).
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="basicArith">
<h3>2.1 Basic Arithmetic</h3>
</a>
<p>
An example of this might be to calculate the cosine of 2.45 (in radians).
To do this one would type:
</p>
<xmp>
(1) -> cos 2.45
(1) -0.7702312540 473073417
Type: Float
</xmp>
<p>
Before proceeding any further it would be best to explain the previous
three lines. Firstly the text ``<tt>(1) -> </tt>'' is part of the prompt
that the <b>AXIOM</b> system provides when in interactive mode. The full prompt
probably has other text preceding this but it is not relevent here. The
number in parenthesis is the step number of the input which may be used
to refer to the <em>results</em> of previous calculations. The step number
appears at the start of the second line to tell you which step the result
belongs to. Since the interpreter probably loaded numerous libraries to
calculate the result given above and listed each one in the process, there
could easily be several pages of text between your input and the answer.
</p>
<p>
The last line contains the type of the result. The type <b>Float</b>
is used to represent real numbers of arbitrary size and precision (where
the user is able to define how big arbitrary is - the default is 20 digits
but can be as large as your computer system can handle). The type of the
result can help track down mistakes in your input if you don't get the
answer you expected.
</p>
<p>
Other arithmetic operations such as addition, subtraction, multiplication
behave as expected:
</p>
<xmp>
(2) -> 6.93 * 4.1328
(2) 28.640304
Type: Float
(3) -> 6.93 / 4.1328
(3) 1.6768292682 926829268
Type: Float
</xmp>
<p>
but integer division isn't quite so obvious. For example, if one types:
</p>
<xmp>
(4) -> 4/6
2
(4) -
3
Type: Fraction Integer
</xmp>
<p>
a fractional result is obtained. The function used to display fractions
attempts to produce the most readable answer. In the example:
</p>
<xmp>
(5) -> 4/2
(5) 2
Type: Fraction Integer
</xmp>
<p>
the result is stored as the faction 2/1 but is displayed as
the integer 2. This fraction could be converted to type
<b>Integer</b> with no loss of information <b>AXIOM</b> will not
do so automatically.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="typeConversionA">
<h3>2.2 Type Conversion</h3>
</a>
<p>
To obtain the floating point value of a fraction one must convert
(according to the documentation, <em>coercions</em> are conversions that
are automatically applied by the kernel) it to type <b>Float</b>
using the ``<tt>::</tt>'' operator (see
<a href="#typeConversionB">Section 4.5</a>)
as follows (the parentheses are required due to the relative
precedences of ``<tt>::</tt>'' and ``<tt>/</tt>''):
</p>
<xmp>
(6) -> (4/6) :: Float
(6) 0.6666666666 6666666667
Type: Float
</xmp>
<p>
Although <b>AXIOM</b> can convert this back to a fraction it might
not be the same fraction it started as due to rounding errors. For
example, the following conversion appears to be without error but
others might not:
</p>
<xmp>
(7) -> % :: Fraction Integer
2
(7) -
3
Type: Fraction Integer
</xmp>
<p>
where ``<tt>%</tt>'' represents the previous <em>result</em> (not the
calculation).
</p>
<p>
Although <b>AXIOM</b> has the ability to work with floating-point numbers
to very high precision it must be remembered that calculations
with these numbers are <em>not</em> exact. Since <b>AXIOM</b> is a computer
algebra package and not a numerical solutions package this should
not create too many problems. The idea is that the user should
use <b>AXIOM</b> to do all the necessary symbolic manipulation and only
at the end should actual numerical results be extracted.
</p>
<p>
If you bear in mind that <b>AXIOM</b> appears to store expressions just as
you have typed them and does not perform any evaluation of them unless
forced to then programming in the system will be much easier. It means
that anything you ask <b>AXIOM</b> to do (within reason) will be carried
with complete accuracy.
</p>
<p>
In the previous examples the ``<tt>::</tt>'' operator was used to
convert values from one type to another. This type conversion is
not possible for all values for instance, it is not possible to
convert the number 3.4 to an integer type since it can't be
represented as an integer. The number 4.0 however can be converted
to an integer type since it has no fractional part. Refer to
<a href="#typeConversionB">Section 4.5</a>)
for more details on type conversion.
</p>
<p>
Conversion from floating point values to integers is performed using
the functions <b>round</b> and <b>truncate</b>.
The first of these rounds a floating point number to the nearest
integer while the other truncates i.e. removes the fractional part.
Both functions return the result as a <em>floating point</em> number.
To extract the fractional part of a floating point number use the
function <b>fractionPart</b> but note that the sign of
the result depends on the sign of the argument (<b>AXIOM</b> obtains
the fractional part of ``<tt>x</tt>'' using ``<tt>x - truncate x</tt>''):
</p>
<xmp>
(8) -> round(3.77623)
(8) 4.0
Type: Float
(9) -> round(-3.77623)
(9) -4.0
Type: Float
(10) -> truncate(9.235)
(10) 9.0
Type: Float
(11) -> truncate(-9.654)
(11) -9.0
Type: Float
(12) -> fractionPart(-3.77623)
(12) -0.77623
Type: Float
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="usefulFunctions">
<h3>2.3 Useful Functions</h3>
</a>
<p>
To obtain the absolute value of a number the <b>abs</b> function
can be used. This takes a single argument which is usually an integer or
a floating point value but doesn't necessarily have to be. The sign of
a value can be obtained via the <b>sign</b> function which
returns -1, 0, or 1 depending on the sign of the argument:
</p>
<xmp>
(13) -> abs(4)
(13) 4
Type: PositiveInteger
(14) -> abs(-3)
(14) 3
Type: PositiveInteger
(15) -> abs(-34254.12314)
(15) 34254.12314
Type: Float
(16) -> sign(-49543.2345346)
(16) -1
Type: Integer
(17) -> sign(0)
(17) 0
Type: NonNegativeInteger
(18) -> sign(234235.42354)
(18) 1
Type: PositiveInteger
</xmp>
<p>
Tests on values can be done using using various functions which
are generally more efficient than using relational operators such as
``<tt>=</tt>'' particularly if the value is a matrix. Examples
of some of these functions are:
</p>
<xmp>
(19) -> positive?(-234)
(19) false
Type: Boolean
(20) -> negative?(-234)
(20) true
Type: Boolean
(21) -> zero?(42)
(21) false
Type: Boolean
(22) -> one?(1)
(22) true
Type: Boolean
(23) -> odd?(23)
(23) true
Type: Boolean
(24) -> odd?(9.435)
(24) false
Type: Boolean
(25) -> even?(-42)
(25) true
Type: Boolean
(26) -> prime?(37)
(26) true
Type: Boolean
(27) -> prime?(-37)
(27) false
Type: Boolean
</xmp>
<p>
Some other functions that are quite useful for manipulating numerical
values are shown in
<a href="#figure2point1">Figure 2.1</a>
while
<a href="#figure2point2">Figure 2.2</a>
contains a table of simple infix and prefix operators.
<a href="#figure2point3">Figure 2.3</a>
is a table of predefined macros that can be used
in various situations.
</p>
<a name="figure2point1">
<xmp>
+---------------+------------------------------------+
| Function | Operation |
+---------------+------------------------------------+
| sin(x) | Sine of x |
| cos(x) | Cosine of x |
| tan(x) | Tangent of x |
| asin(x) | Arcsin of x |
| acos(x) | Arccos of x |
| atan(x) | Arctangent of x |
| gcd(x,y) | Greatest common divisor of x and y |
| lcm(x,y) | Lowest common multiple of x and y |
| max(x,y) | Maximum of x and y |
| min(x,y) | Minimum of x and y |
| factorial(x) | Factorial of x |
| factor(x) | Prime factors of x |
| divide(x,y) | Quotient and remainder of x/y |
+---------------+------------------------------------+
Figure 2.1: Table of simple AXIOM functions.
</xmp>
</a>
<a name="figure2point2">
<xmp>
+--------+--------------------+--------+------------------+
| Symbol | Operation | Symbol | Operation |
+--------+--------------------+--------+------------------+
| + | Addition | - | Subtraction |
| - | Numerical Negation | ~ | Logical Negation |
| /\ | Conjunction (AND) | \/ | Disjunction (OR) |
| and | Logical AND (/\) | or | Logical OR (\/) |
| not | Logical Negation | ** | Exponentiation |
| * | Multiplication | / | Division |
| quo | Quotient | rem | Remainder |
| < | Less than | > | Greater than |
| <= | Less or equal | >= | Greater or equal |
+--------+--------------------+--------+------------------+
Figure 2.2: Table of simple AXIOM operators.
</xmp>
</a>
<a name="figure2point3">
<xmp>
+----------------+------------------------------------+
| Macro | Value |
+----------------+------------------------------------+
| %i | The square root of -1. |
| %e | The base of the natural logarithm. |
| %pi | Pi. |
| %infinity | Infinity. |
| %plusInfinity | Positive Infinity. |
| %minusInfinity | Negative Infinity. |
+----------------+------------------------------------+
Figure 2.3: Table of some useful AXIOM macros.
</xmp>
</a>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="symbolic">
<h2>Chapter 3</h2>
<h2>Using <b>AXIOM</b> As A Symbolic Calculator</h2>
</a>
<p>
In the previous section all the examples involved numbers and simple
functions. Also none of the expressions entered were assigned to anything.
In this section we will move on to simple algebra i.e. expressions
involving symbols and other features available on more sophisticated
calculators.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="exprSymbols">
<h3>3.1 Expressions Involving Symbols</h3>
</a>
<p>
Expressions involving symbols are entered just as they
are written down, for example:
</p>
<xmp>
(1) -> xSquared := x**2
2
(1) x
Type: Polynomial Integer
</xmp>
<p>
where the assignment operator ``<tt>:=</tt>'' represents
immediate assignment. Later it will be seen that this form of assignment
is not always desirable and the use of the delayed assignment operator
``<tt>==</tt>'' will be introduced. The type of the result is
<b>Polynomial Integer</b> which is used to represent polynomials
with integer coefficients. Some other examples along similar lines are:
</p>
<xmp>
(2) -> xDummy := 3.21*x**2
2
(2) 3.21 x
Type: Polynomial Float
(3) -> xDummy := x**2.5
2 +-+
(3) x \|x
Type: Expression Float
(4) -> xDummy := x**3.3
3 10+-+3
(4) x \|x
Type: Expression Float
(5) -> xyDummy := x**2 - y**2
2 2
(5) -y + x
Type: Polynomial Integer
</xmp>
<p>
Given that we can define expressions involving symbols, how do we actually
compute the result when the symbols are assigned values? The answer is to
use the <b>eval</b> function which takes an expression as it's
first argument followed by a list of assignments. For example, to evaluate
the expressions <b>xDummy</b> and <b>xyDummy</b> resulting
from their respective assignments above we type:
</p>
<xmp>
(6) -> eval(xDummy, x=3)
(6) 37.5405075985 29552193
Type: Expression Float
(7) -> eval(xyDummy, [x=3, y=2.1])
(7) 4.59
Type: Polynomial Float
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="complexNumbers">
<h3>3.2 Complex Numbers</h3>
</a>
<p>
For many scientific calculations real numbers aren't sufficient and
support for complex numbers is also required. Complex numbers are
handled in an intuitive manner by <b>AXIOM</b> which uses the <tt>%i</tt>
macro to represent <em>i</em>, the square root of -1. Thus expressions
involving complex numbers are entered just like other expressions:
</p>
<xmp>
(8) -> (2/3 + %i)**3
46 1
(8) - -- + - %i
27 3
Type: Complex Fraction Integer
</xmp>
<p>
The real and imaginary parts of a complex number can be extracted using
the <b>real</b> and <b>imag</b> functions and the
complex conjugate of a number can be obtained using <b>conjugate</b>:
</p>
<xmp>
(9) -> real(3 + 2*%i)
(9) 3
Type: PositiveInteger
(10) -> imag(3 + 2*%i)
(10) 2
Type: PositiveInteger
(11) -> conjugate(3 + 2*%i)
(11) 3 - 2%i
Type: Complex Integer
</xmp>
<p>
The function <b>factor</b> can also be applied to complex
numbers but the results aren't quite so obvious as for factoring integers:
</p>
<xmp>
(12) -> factor(144 + 24*%i)
6
(12) %i (1 + %i) 3 (6 + %i)
Type: Factored Complex Integer
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="numberReps">
<h3>3.3 Number Representations</h3>
</a>
<p>
By default all numerical results are displayed in decimal with real
numbers shown to 20 significant figures. If the integer part of a
number is longer than 20 digits then nothing after the decimal point
is shown and the integer part is given in full. To alter the number
of digits shown the function <b>digits</b> can be called. The result
returned by this function is the previous setting. For example, to
find the value of <i>pi</i> to 40 digits we type:
</p>
<xmp>
(13) -> digits(40)
(13) 20
Type: PositiveInteger
(14) -> %pi :: Float
(14) 3.1415926535 8979323846 2643383279 502884197
Type: Float
</xmp>
<p>
As can be seen in the example above, there is a gap after every ten
digits. This can be changed using the <b>outputSpacing</b>
function where the argument is the number of digits to be displayed
before a space is inserted. If no spaces are desired then use the
value 0. Two other functions controlling the appearance of real
numbers are <b>outputFloating</b> and
<b>outputFixed</b>. The former causes <b>AXIOM</b> to display
floating-point values in exponent notation and the latter causes
it to use fixed point notation. For example:
</p>
<xmp>
(15) -> outputFloating(); %
(15) 0.3141592653 5897932384 6264338327 9502884197 E 1
Type: Float
(16) -> outputFloating(3); 0.00345
(16) 0.345 E -2
Type: Float
(17) -> outputFixed(); %
(17) 0.00345
Type: Float
(18) -> outputFixed(3); %
(18) 0.003
Type: Float
(19) -> outputGeneral(); %
(19) 0.00345
Type: Float
</xmp>
<p>
Note that the semicolon ``<tt>;</tt>'' in the examples above allows several
expressions to be entered on one line. The result of the last expression
is displayed. Remember also that the percent symbol ``<tt>%</tt>'' is used
to represent the result of a previous calculation.
</p>
<p>
To display rational numbers in a base other than 10 the function
<b>radix</b> is used. The first argument of this function is
the expression to be displayed and the second is the base to be used:
</p>
<xmp>
(20) -> radix(10**10,32)
(20) 9A0NP00
Type: RadixExpansion 32
(21) -> radix(3/21,5)
______
(21) 0.032412
Type: RadixExpansion 5
</xmp>
<p>
Rational numbers can be represented as a repeated decimal expansion
using the <b>decimal</b> function or as a continued fraction
using <b>continuedFraction</b>. Any attempt to call these
functions with irrational values will fail.
</p>
<xmp>
(22) -> decimal(22/7)
______
(22) 3.142857
Type: DecimalExpansion
(23) -> continuedFraction(6543/210)
1 | 1 | 1 | 1 |
(23) 31 + +---+ + +---+ + +---+ + +---+
| 6 | 2 | 1 | 3
Type: ContinuedFraction Integer
</xmp>
<p>
Finally, partial fractions in compact and expanded form are available
via the functions <b>partialFraction</b> and
<b>padicFraction</b> respectively. The former takes two
arguments, the first being the numerator of the fraction and the second
being the denominator. The latter function takes a fraction and
expands it further while the function <b>compactFraction</b>
does the reverse:
</p>
<xmp>
(24) -> partialFraction(234,40)
3 3
(24) 6 - -- + -
2 5
2
Type: PartialFraction Integer
(25) -> padicFraction(%)
1 1 3
(25) 6 - - - -- + -
2 2 5
2
Type: PartialFraction Integer
(26) -> compactFraction(%)
3 3
(26) 6 - -- + -
2 5
2
Type: PartialFraction Integer
(27) -> padicFraction(234/40)
117
(27) ---
20
Type: PartialFraction Fraction Integer
</xmp>
<p>
To extract parts of a partial fraction the function
<b>nthFractionalTerm</b> is available and returns a partial
fraction of one term. To decompose this further the numerator can
be obtained using <b>firstNumer</b> and the denominator with
<b>firstDenom</b>. The whole part of a partial fraction can
be retrieved using <b>wholePart</b> and the number of fractional
parts can be found using the function
<b>numberOfFractionalTerms</b>:
</p>
<xmp>
(28) -> t := partialFraction(234,40)
3 3
(28) 6 - -- + -
2 5
2
Type: PartialFraction Integer
(29) -> wholePart(t)
(29) 6
Type: PositiveInteger
(30) -> numberOfFractionalTerms(t)
(30) 2
Type: PositiveInteger
(31) -> p := nthFractionalTerm(t,1)
3
(31) - --
2
2
Type: PartialFraction Integer
(32) -> firstNumer(p)
(32) -3
Type: Integer
(33) -> firstDenom(p)
2
(33) 2
Type: Factored Integer
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="modularArith">
<h3>3.4 Modular Arithmetic</h3>
</a>
<p>
By using the type constructor <b>PrimeField</b> it is possible
to do arithmetic modulo some prime number. For example, arithmetic
modulo 7 can be performed as follows:
</p>
<xmp>
(34) -> x : PrimeField 7 := 5
(34) 5
Type: PrimeField 7
(35) -> x**5 + 6
(35) 2
Type: PrimeField 7
(36) -> 1/x
(36) 3
Type: PrimeField 7
</xmp>
<p>
The first example should be read as
</p>
<blockquote>
``Let <i>x</i> be of type <b>PrimeField 7</b> and
assign to it the value 5.''
</blockquote>
<p>
Note that it is only possible to invert non-zero values if the
arithmetic is performed modulo a prime number. Thus arithmetic
modulo a non-prime integer is possible but the reciprocal operation
is undefined and will generate an error. Attempting to use the
<b>PrimeField</b> type constructor with a non-prime
argument will generate an error. An example of non-prime
modulo arithmetic is:
</p>
<xmp>
(37) -> y : IntegerMod 8 := 11
(37) 3
Type: IntegerMod 8
(38) -> y*4 + 27
(38) 7
Type: IntegerMod 8
</xmp>
<p>
Note that polynomials can be constructed in a similar way:
<xmp>
(39) -> (3*a**4 + 27*a - 36)::Polynomial PrimeField 7
4
(39) 3a + 6a + 6
Type: Polynomial PrimeField 7
</xmp>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="generalPoints">
<h2>Chapter 4</h2>
<h2>General Points About <b>AXIOM</b></h2>
</a>
<p>
This chapter contains a jumble of various points about <b>AXIOM</b>.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="compNoOutput">
<h3>4.1 Computation Without Output</h3>
</a>
<p>
It is sometimes desirable to enter an expression and prevent <b>AXIOM</b>
from displaying the result. To do this the expression should be
terminated with a semicolon ``<tt>;</tt>''. In
<a href="#numberReps">Section 3.3</a>
it was mentioned that a set of expressions separated by semicolons
would be evaluated and the result of the last one displayed. Thus
if a single expression is followed by a semicolon no output will
be produced (except for its type):
</p>
<xmp>
(1) -> 2 + 4*5;
Type: PositiveInteger
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="earlierResults">
<h3>4.2 Accessing Earlier Results</h3>
</a>
<p>
As mentioned in previous sections the ``<tt>%</tt>'' macro
represents the result of the previous computation and some other
macros were given in
<a href="#figure2point3">Figure 2.3</a>.
To refer to earlier calculations
the ``<tt>%%</tt>'' macro is available which takes a single
integer argument. If the argument is positive then it refers to
the step number of the calculation where the numbering begins
from one and can be seen at the end of each prompt (the number in
parentheses). If the argument is negative then it refers to
previous results counting backwards from the last result
i.e. ``<tt>%%(-1)</tt>'' is the same as ``<tt>%</tt>''. The value
of ``<tt>%%(0)</tt>'' is not defined and will generate an error
if requested.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="splittingExprs">
<h3>4.3 Splitting Expressions Over Several Lines</h3>
</a>
<p>
Although <b>AXIOM</b> will quite happily accept expressions that are
longer than the width of the screen (just keep typing without
pressing the <b>Return</b> key) it is often preferable to split the
expression being entered at a point where it would result in
more readable input. To do this the underscore ``_'' symbol
is placed before the break point and then the <b>Return</b> key pressed.
The rest of the expression is typed on the next line and can be
preceded by any number of whitespace characters, for example:
</p>
<xmp>
(2) -> 2_
+_
3
(2) 5
Type: PositiveInteger
</xmp>
<p>
The underscore symbol is an escape character and its presence alters
the meaning of the characters that follow it. As mentioned above
whitespace following an underscore is ignored (the <b>Return</b> key
generates a whitespace character). Any other character following
an underscore loses whatever special meaning it may have had. Thus
one can create the identifier ``<tt>a+b</tt>'' by typing
``<tt>a_+b</tt>'' although this might lead to confusions. Also
note the result of the following example:
</p>
<xmp>
(3) -> ThisIsAVeryLong_
VariableName
(3) ThisIsAVeryLongVariableName
Type: Variable ThisIsAVeryLongVariableName
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="comments">
<h3>4.4 Comments And Descriptions</h3>
</a>
<p>
Comments and descriptions are really only of use in files of <b>AXIOM</b>
code but can be used when the output of an interactive session is
being spooled to a file (via the system command <b><tt>)spool</tt></b>).
A comment begins with two dashes ``<tt>--</tt>'' and continues until
the end of the line. Multi-line comments are only possible if each
individual line begins with two dashes.
</p>
<p>
Descriptions are the same as comments except that the <b>AXIOM</b> compiler
will include them in the object files produced and make them available
to the user for documentation purposes. For more details on this refer
to <a href="#watt1995">[watt1995]</a>.
</p>
<p>
A description placed <em>before</em> a calculation begins with three plus
symbols ``<tt>+++</tt>'' and a description placed after a calculation
begins with two plus symbols ``<tt>++</tt>''.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="typeConversionB">
<h3>4.5 Control Of Result Types</h3>
</a>
<hr>
<b>
<blockquote>
<blockquote>
<blockquote>
This section needs to be rewritten and extended to give a proper
description of converting values from one type to another.
</blockquote>
</blockquote>
</blockquote>
</b>
<hr>
<p>
In earlier sections the type of an expression was converted to another
via the ``<tt>::</tt>'' operator. However, this is not the only method
for converting between types and two other operators need to be
introduced and explained.
</p>
<p>
The first operator is ``<tt>$</tt>'' and is used to specify the
package to be used to calculate the result. Thus:
</p>
<xmp>
(4) -> (2/3)$Float
(4) 0.6666666666 6666666667
Type: Float
</xmp>
<p>
tells <b>AXIOM</b> to use the ``<tt>/</tt>'' operator from the
<b>Float</b> package to evaluate the expression 2/3. This does not
necessarily mean that the result will be of the same type as the
domain from which the operator was taken. In the following example
the <b>sign</b> operator is taken from the <b>Float</b> package
but the result is of type <b>Integer</b>.
</p>
<xmp>
(5) -> sign(2.3)$Float
(5) 1
Type: Integer
</xmp>
<p>
The other operator is ``<tt>@</tt>'' which is used to tell <b>AXIOM</b>
what the desired type of the result of the calculation is. In most
situations all three operators yield the same results but the example
below should help to distinguish them.
</p>
<xmp>
(6) -> (2 + 3)::String
(6) "5"
Type: String
(7) -> (2 + 3)@String
An expression involving @ String actually evaluated to
one of type PositiveInteger . Perhaps you should use
:: String .
(7) -> (2 + 3)$@String
The function + is not implemented in String .
</xmp>
<p>
If an expression <i>X</i> is converted using one of the three operators to
type <i>T</i> the interpretations are follows:
</p>
<blockquote>
<dl compact>
<dt><b><tt>::</tt></b></dt>
<dd>means explicitly convert <i>X</i> to type <i>T</i> if possible.</dd>
<dt><b><tt>$ </tt></b></dt>
<dd>means use the available operators for type <i>T</i> to compute <i>X</i>.</dd>
<dt><b><tt>@ </tt></b></dt>
<dd>means choose operators to compute <i>X</i> so that the result
is of type <i>T</i>.</dd>
</dl>
</blockquote>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="data">
<h2>Chapter 5</h2>
<h2>Data Structures in <b>AXIOM</b></h2>
</a>
<p>
This chapter is an overview of <em>some</em> of the data structures
provided by <b>AXIOM</b>.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="lists">
<h3>5.1 Lists</h3>
</a>
<p>
The <b>AXIOM</b> <b>List</b> type constructor is used to create
homogenous lists of finite size. The notation for lists and the
names of the functions that operate over them are similar to those
found in functional languages such as ML.
</p>
<p>
Lists can be created by placing a comma separated list of values
inside square brackets or if a list with just one element is desired
then the function <b>list</b> is available:
</p>
<xmp>
(1) -> [4]
(1) [4]
Type: List PositiveInteger
(2) -> list(4)
(2) [4]
Type: List PositiveInteger
(3) -> [1,2,3,5,7,11]
(3) [1,2,3,5,7,11]
Type: List PositiveInteger
</xmp>
<p>
The function <b>append</b> takes two lists as arguments and
returns the list consisting of the second argument appended to the first.
A single element can be added to the front of a list using
<b>cons</b>:
</p>
<xmp>
(4) -> append([1,2,3,5],[7,11])
(4) [1,2,3,5,7,11]
Type: List PositiveInteger
(5) -> cons(23,[65,42,19])
(5) [23,65,42,19]
Type: List PositiveInteger
</xmp>
<p>
Lists are accessed sequentially so if <b>AXIOM</b> is asked for the value
of the twentieth element in the list it will move from the start of the
list over nineteen elements before it reaches the desired element. Each
element of a list is stored as a node consisting of the value of the
element and a pointer to the rest of the list. As a result the two main
operations on a list are called <b>first</b> and
<b>rest</b>. Both of these functions take a second optional
argument which specifies the length of the first part of the list:
</p>
<xmp>
(6) -> first([1,5,6,2,3]
(6) 1
Type: PositiveInteger
(7) -> first([1,5,6,2,3],2)
(7) [1,5]
Type: List PositiveInteger
(8) -> rest([1,5,6,2,3])
(8) [5,6,2,3]
Type: List PositiveInteger
(9) -> rest([1,5,6,2,3],2)
(9) [6,2,3]
Type: List PositiveInteger
</xmp>
<p>
Other functions are <b>empty?</b> which tests to see if a list
contains no elements, <b>member?</b> which tests to see if the
first argument is a member of the second, <b>reverse</b>
which reverses the order of the list, <b>sort</b> which sorts
a list and <b>removeDuplicates</b> which removes any duplicates.
The length of a list can be obtained using the ``<tt>#</tt>'' operator.
</p>
<xmp>
(10) -> empty?([7,2,-1,2])
(10) false
Type: Boolean
(11) -> member?(-1,[7,2,-1,2])
(11) true
Type: Boolean
(12) -> reverse([7,2,-1,2])
(12) [2,- 1,2,7]
Type: List Integer
(13) -> sort([7,2,-1,2])
(13) [-1,2,2,7]
Type: List Integer
(14) -> removeDuplicates([1,5,3,5,1,1,2])
(14) [1,5,3,2]
Type: List PositiveInteger
(15) -> #[7,2,-1,2]
(15) 4
Type: PositiveInteger
</xmp>
<p>
Lists in <b>AXIOM</b> are mutable and so their contents (the elements and
the links) can be modified <i>in situ</i>. Functions that operate
over lists in this way have names ending with the symbol ``<tt>!</tt>''.
For example, <b>concat!</b> takes two lists as arguments
and appends the second argument to the first (except when the first
argument is an empty list) and <b>setrest!</b> changes the link emanating
from the first argument to point to the second argument (see
<a href="#figure5point1">Figure 5.1</a>
and
<a href="#figure5point2">Figure 5.2</a>
for a graphical explanation of the following example of <b>setrest!</b>):
</p>
<xmp>
(16) -> u := [9,2,4,7]
(16) [9,2,4,7]
Type: List PositiveInteger
(17) -> concat!(u,[1,5,42]); u
(17) [9,2,4,7,1,5,42]
Type: List PositiveInteger
(18) -> endOfu := rest(u,4)
(18) [1,5,42]
Type: List PositiveInteger
(19) -> partOfu := rest(u,2)
(19) [4,7,1,5,42]
Type: List PositiveInteger
(20) -> setrest!(endOfu,partOfu); u
-----
(20) [9,2,4,7,1]
Type: List PositiveInteger
</xmp>
<p></p><br>
<a name="figure5point1">
<center>
<img src="images/setrest.1.gif" alt="[Image]"><br><br>
<b>Figure</b> 5.1: List <i>u</i> before <tt>setrest(endOfu,partOfu)</tt>.
</center>
</a>
<p></p><br><br>
<a name="figure5point2">
<center>
<img src="images/setrest.2.gif" alt="[Image]"><br><br>
<b>Figure</b> 5.2: List <i>u</i> after <tt>setrest(endOfu,partOfu)</tt>.
</center>
</a>
<p>
From this it can be seen that the lists returned by <b>first</b>
and <b>rest</b> are pointers to the original list and <em>not</em> a
copy. Thus great care must be taken when dealing with lists in <b>AXIOM</b>.
</p>
<p>
Although the <i>n</i>th element of a list <i>l</i> can be obtained by applying
the <b>first</b> function to <i>n</i>-1 applications of
<b>rest</b> to <i>l</i>, <b>AXIOM</b> provides a more useful access
method in the form of the ``<tt>.</tt>'' operator:
</p>
<xmp>
(21) -> u.3
(21) 4
Type: PositiveInteger
(22) -> u.5
(22) 1
Type: PositiveInteger
(23) -> u.6
(23) 4
Type: PositiveInteger
(24) -> first rest rest u -- Same as u.3
(24) 4
Type: PositiveInteger
(25) -> u.first
(25) 9
Type: PositiveInteger
(26) -> u(3)
(26) 4
Type: PositiveInteger
</xmp>
<p>
The operation <i>u.i</i> is referred to as <em>indexing into <i>u</i></em> or
<em>elting into <i>u</i></em>. The latter term comes from the <b>elt</b>
function which is used to extract elements (the first element of the
list is at index 1).
</p>
<xmp>
(27) -> elt(u,4) -- Same as u.4
(27) 7
Type: PositiveInteger
</xmp>
<p>
If a list has no cycles then any attempt to access an element beyond
the end of the list will generate an error. However, in the example above
there was a cycle starting at the third element so the access to the
sixth element wrapped around to give the third element. Since lists are
mutable it is possible to modify elements directly:
</p>
<xmp>
(28) -> u.3 := 42; u
------
(28) [9,2,42,7,1]
Type: List PositiveInteger
</xmp>
<p>
Other list operations are:
</p>
<xmp>
(29) -> L := [9,3,4,7]; #L
(29) 4
Type: PositiveInteger
(30) -> last(L)
(30) 7
Type: PositiveInteger
(31) -> L.last
(31) 7
Type: PositiveInteger
(32) -> L.(#L - 1)
(32) 4
Type: PositiveInteger
</xmp>
<p>
<hr>
<center>
<img src="http://www-theory.dcs.st-andrews.ac.uk/~mnd/icons/warning.gif"
align=center
alt="WARNING: ">
Using the ``<tt>#</tt>'' operator on a list with cycles causes
<b>AXIOM</b> to enter an infinite loop.
<img src="http://www-theory.dcs.st-andrews.ac.uk/~mnd/icons/warning.gif"
align=center
alt="WARNING: ">
</center>
<hr>
</p>
<p>
Note that any operation on a list <i>L</i> that returns a list
<i>L'</i> will, in general, be such that any changes to <i>L'</i> will have
the side-effect of altering <i>L</i>. For example:
</p>
<xmp>
(33) -> m := rest(L,2)
(33) [4,7]
Type: List PositiveInteger
(34) -> m.1 := 20; L
(34) [9,3,20,7]
Type: List PositiveInteger
(35) -> n := L
(35) [9,3,20,7]
Type: List PositiveInteger
(36) -> n.2 := 99; L
(36) [9,99,20,7]
Type: List PositiveInteger
(37) -> n
(37) [9,99,20,7]
Type: List PositiveInteger
</xmp>
<p>
Thus the only safe way of copying lists is to copy each
element from one to another and not use the assignment operator:
</p>
<xmp>
(38) -> p := [i for i in n] -- Same as `p := copy(n)'
(38) [9,99,20,7]
Type: List PositiveInteger
(39) -> p.2 := 5; p
(39) [9,5,20,7]
Type: List PositiveInteger
(40) -> n
(40) [9,99,20,7]
Type: List PositiveInteger
</xmp>
<p>
In the previous example a new way of constructing lists was given.
This is a powerful method which gives the reader more information
about the contents of the list than before and which is extremely
flexible. The example,
</p>
<xmp>
(41) -> [i for i in 1..10]
(41) [1,2,3,4,5,6,7,8,9,10]
Type: List PositiveInteger
</xmp>
<p>
should be read as
</p>
<blockquote>
``Using the expression <i>i</i>, generate each element of the list by
iterating the symbol <i>i</i> over the range of integers [1,10].''
</blockquote>
<p>
To generate the list of the squares of the first ten elements we
just use:
</p>
<xmp>
(42) -> [i**2 for i in 1..10]
(42) [1,4,9,16,25,36,49,64,81,100]
Type: List PositiveInteger
</xmp>
<p>
For more complex lists we can apply a condition to the elements that
are to be placed into the list thus to obtain a list of even numbers
between 0 and 11:
</p>
<xmp>
(43) -> [i for i in 1..10 | even?(i)]
(43) [2,4,6,8,10]
Type: List PositiveInteger
</xmp>
<p>
This example should be read as
</p>
<blockquote>
``Using the expression <i>i</i>, generate each element of the list by
iterating the symbol <i>i</i> over the range of integers [1,10] such that
<i>i</i> is even.''
</blockquote>
<p>
The following achieves the same result:
</p>
<xmp>
(44) -> [i for i in 2..10 by 2]
(44) [2,4,6,8,10]
Type: List PositiveInteger
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="segLists">
<h3>5.2 Segmented Lists</h3>
</a>
<p>
A segmented list is one in which some of the elements are ranges of
values. The <b>expand</b> function converts lists of this
type into ordinary lists:
</p>
<xmp>
(45) -> [1..10]
(45) [1..10]
Type: List Segment PositiveInteger
(46) -> [1..3,5,6,8..10]
(46) [1..3,5..5,6..6,8..10]
Type: List Segment PositiveInteger
(47) -> expand(%)
(47) [1,2,3,5,6,8,9,10]
Type: List Integer
</xmp>
<p>
If the upper bound of a segment is omitted then a different type of
segmented list is obtained and expanding it will produce a stream (which
will be considered in the next section):
</p>
<xmp>
(48) -> [1..]
(48) [1..]
Type: List UniversalSegment PositiveInteger
(49) -> expand(%)
(49) [1,2,3,4,5,6,7,8,9,10,...]
Type: Stream Integer
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="Streams">
<h3>5.3 Streams</h3>
</a>
<p>
Streams are infinite lists which have the ability to calculate the
next element should it be required. For example, a stream of positive
integers and a list of prime numbers can be generated by:
</p>
<xmp>
(50) -> [i for i in 1..]
(50) [1,2,3,4,5,6,7,8,9,10,...]
Type: Stream PositiveInteger
(51) -> [i for i in 1.. | prime?(i)]
(51) [2,3,5,7,11,13,17,19,23,29,...]
Type: Stream PositiveInteger
</xmp>
<p>
In each case the first few elements of the stream are calculated for
display purposes but the rest of the stream remains unevaluated. The value
of items in a stream are only calculated when they are needed which gives
rise to their alternative name of ``lazy lists''.
</p>
<p>
Another method of creating streams is to use the <b>generate(f,a)</b>
function. This applies it's first argument repeatedly onto it's second to
produce the stream [<i>a,f(a),f(f(a)),f(f(f(a))) ...</i>]. Given
that the function <b>nextPrime</b> returns the lowest prime
number greater than it's argument we can generate a stream of primes
as follows:
</p>
<xmp>
(52) -> generate(nextPrime,2)$Stream Integer
(52) [2,3,5,7,11,13,17,19,23,29,...]
Type: Stream Integer
</xmp>
<p>
As a longer example a stream of Fibonacci numbers will be computed. The
Fibobacci numbers start at 1 and each following number is the addition
of the two numbers that precede it i.e. 1,1,2,3,5,8,... The idea
behind this example is from
<a href="#jenks1992">[jenks1992, page 457]</a>
but the explanation and construction below is that of the author's.
</p>
<p>
Since the generation of any Fibonacci number only relies on knowing the
previous two numbers we can look at the series through a window of two
elements. To create the series the window is placed at the start over
the values [1,1] and their sum obtained. The window is now
shifted to the right by one position and the sum placed into the empty
slot of the window; the process is then repeated. To implement this we
require a function that takes a list of two elements (the current view
of the window), adds them and outputs the new window. The result is the
function <i>[a,b] -> [b,a+b]</i>:
</p>
<xmp>
(53) -> win : List Integer -> List Integer
Type: Void
(54) -> win(x) == [x.2, x.1 + x.2]
Type: Void
(55) -> win([1,1])
(55) [1,2]
Type: List Integer
(56) -> win(%)
(56) [2,3]
Type: List Integer
(57) -> win(%)
(57) [3,5]
Type: List Integer
</xmp>
<p>
(For more details on functions definitions see
<a href="#functions">Section 6.3</a>.)
</p>
<p>
Thus it can be seen that repeatedly applying <b>win</b>
to the results of the previous invocation, each element of the series
is obtained. Clearly <b>win</b> is an ideal function to
construct streams using the <b>generate</b> function:
</p>
<xmp>
(58) -> fibs := [generate(win,[1,1])]
(58) [[1,1],[1,2],[2,3],[3,5],[5,8],[8,13],...]
Type: Stream List Integer
</xmp>
<p>
This isn't quite what is wanted - we need to extract the first element of
each list and place that in our series:
</p>
<xmp>
(59) -> fibs := [i.1 for i in [generate(win,[1,1])]]
(59) [1,1,2,3,5,8,13,21,34,55,...]
Type: Stream Integer
</xmp>
<p>
Obtaining the 200th Fibonacci number is trivial:
</p>
<xmp>
(60) -> fibs.200
(60) 280571172992510140037611932413038677189525
Type: PositiveInteger
</xmp>
<p>
One other function of interest is <b>complete</b> which
expands a finite stream derived from an infinite one (and thus
was still stored as an infinite stream) to form a finite stream.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="arraysEtc">
<h3>5.4 Arrays, Vectors, Strings and Bits</h3>
</a>
<p>
The simplest array data structure is the <em>one-dimensional array</em>
which can be obtained by applying the <b>oneDimensionalArray</b>
function to a list:
</p>
<xmp>
(61) -> oneDimensionalArray([7,2,5,4,1,9])
(61) [7,2,5,4,1,9]
Type: OneDimensionalArray PositiveInteger
</xmp>
<p>
One-dimensional arrays are homogenous (all the elements must have the
same type) and mutable like lists but unlike lists they are constant in
size and have uniform access time (it is just as quick to read the last
element of a one-dimensional array as it is to read the first: this is
not true for lists).
</p>
<p>
Since these arrays are mutable all the warnings that apply to lists
apply to arrays i.e. it is possible to modify an element in a copy
of an array and change the original:
</p>
<xmp>
(62) -> x := %
(62) [7,2,5,4,1,9]
Type: OneDimensionalArray PositiveInteger
(63) -> y := x
(63) [7,2,5,4,1,9]
Type: OneDimensionalArray PositiveInteger
(64) -> y.3 := 20; x
(64) [7,2,20,4,1,9]
Type: OneDimensionalArray PositiveInteger
</xmp>
<p>
Note that because these arrays are of fixed size the <b>concat!</b>
function cannot be applied to them without generating an error.
If arrays of this type are required use the <b>FlexibleArray</b>
type constructor (see
<a href="#flexibleArrays">Section 5.5</a>).
</p>
<p>
One-dimensional arrays can be created using <b>new</b>
which specifies the size of the array and the initial value for each
of the elements. Other operations that can be applied to one-dimensional
arrays are <b>map!</b> which applies a mapping onto each
element, <b>swap!</b> which swaps two elements and
<b>copyInto!(a,b,c)</b> which copies the array <i>b</i> onto <i>a</i>
starting at position <i>c</i>:
</p>
<xmp>
(65) -> a : ARRAY1 PositiveInteger := new(10,3)
(65) [3,3,3,3,3,3,3,3,3,3]
Type: OneDimensionalArray PositiveInteger
(66) -> map!(i +-> i+1,a); a
(66) [4,4,4,4,4,4,4,4,4,4]
Type: OneDimensionalArray Integer
(67) -> b := oneDimensionalArray([2,3,4,5,6])
(67) [2,3,4,5,6]
Type: OneDimensionalArray PositiveInteger
(68) -> swap!(b,2,3); b
(68) [2,4,3,5,6]
Type: OneDimensionalArray PositiveInteger
(69) -> copyInto!(a,b,3)
(69) [4,4,2,4,3,5,6,4,4,4]
Type: OneDimensionalArray PositiveInteger
(70) -> a
(70) [4,4,2,4,3,5,6,4,4,4]
Type: OneDimensionalArray PositiveInteger
</xmp>
<p>
(note that <b>ARRAY1</b> is an abbreviation for the type
<b>OneDimensionalArray</b>.) Other types based on one-dimensional
arrays are <b>Vector</b>, <b>String</b> and <b>Bits</b>:
</p>
<xmp>
(71) -> vector([1/2,1/3,1/4])
1 1 1
(71) [-,-,-]
2 3 4
Type: Vector Fraction Integer
(72) -> "Hello World"
(72) "Hello World"
Type: String
(73) -> bits(8,true)
(73) "11111111"
Type: Bits
</xmp>
<p>
A vector is similar to a one-dimensional array except that if it's
components belong to a ring then arithmetic operations are provided.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="flexibleArrays">
<h3>5.5 Flexible Arrays</h3>
</a>
<p>
Flexible arrays are designed to provide the efficiency of one-dimensional
arrays while retaining the flexibility of lists. They are implemented by
allocating a fixed block of storage for the array. If the array needs to
be expanded then a larger block of storage is allocated and the contents
of the old block are copied into the new one.
</p>
<p>
There are several operations that can be applied to this type, most
of which modify the array <i>in situ</i>. As a result these functions
all have names ending in ``<tt>!</tt>''. All the functions in the example
below are fairly obvious. The <b>physicalLength</b> returns
the actual length of the array as stored in memory while the
<b>physicalLength!</b> allows this value to be changed by the
user.
</p>
<xmp>
(74) -> f : FARRAY INT := new(6,1)
(74) [1,1,1,1,1,1]
Type: FlexibleArray Integer
(75) -> f.1 := 4;f.2 := 3;f.3 := 8;f.5 := 2;f
(75) [4,3,8,1,2,1]
Type: FlexibleArray Integer
(76) -> insert!(42,f,3); f
(76) [4,3,42,8,1,2,1]
Type: FlexibleArray Integer
(77) -> insert!(28,f,8); f
(77) [4,3,42,8,1,2,1,28]
Type: FlexibleArray Integer
(78) -> removeDuplicates!(f)
(78) [4,3,42,8,1,2,28]
Type: FlexibleArray Integer
(79) -> delete!(f,5)
(79) [4,3,42,8,2,28]
Type: FlexibleArray Integer
(80) -> g := f(3..5)
(80) [42,8,2]
Type: FlexibleArray Integer
(81) -> g.2 := 7; f
(81) [4,3,42,8,2,28]
Type: FlexibleArray Integer
(82) -> insert!(g,f,1)
(82) [42,7,2,4,3,42,8,2,28]
Type: FlexibleArray Integer
(83) -> physicalLength(f)
(83) 10
Type: PositiveInteger
(84) -> physicalLength!(f,20)
(84) [42,7,2,4,3,42,8,2,28]
Type: FlexibleArray Integer
(85) -> merge!(sort!(f),sort!(g))
(85) [2,2,2,3,4,7,7,8,28,42,42,42]
Type: FlexibleArray Integer
(86) -> shrinkable(false)$FlexibleArray(Integer)
(86) true
Type: Boolean
</xmp>
<p>
There are several things to point out concerning the previous examples.
Firstly although flexible arrays are mutable, making copies of these
arrays creates separate entities. This can be seen by the fact that the
modification of element <i>b.2</i> above did not alter <i>a</i>. Secondly, the
<b>merge!</b> function can take an extra argument before the
two arrays being merged. The argument is a comparison function and
defaults to <tt><=</tt> if omitted. Lastly, <b>shrinkable</b>
tells the <b>AXIOM</b> system whether or not to let flexible arrays
contract when elements are deleted from them. An explicit package
reference must be given as in the example above.
</xmp>
<hr>
<b>
<blockquote>
<blockquote>
<blockquote>
Although there are several other data structures that ought to
be covered here such as sets and records, I want to move on to
more important areas such as functions and iteration before finishing
this chapter off.
</blockquote>
</blockquote>
</blockquote>
</b>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="functionsEtc">
<h2>Chapter 6</h2>
<h2>Functions, Choices And Loops</h3>
</a>
<p>
By now the reader should be able to construct simple one-line
expressions involving variables and different data structures. This
chapter builds on this knowledge and shows how to use iteration,
make choices and build functions in <b>AXIOM</b>. At the moment it is
assumed that the reader has a rough idea of how types are specified
and constructed so that they can follow the examples given. A more
detailed coverage of types in <b>AXIOM</b> will be provided in a later
chapter (sometime in the future!).
</p>
<p>
From this point on most examples will be taken from input files.
The prompt for such examples is faked and should actually contain
a command such as ``<tt>)read eg01.input</tt>''. There may be jumps
in the step numbers of the examples due to the way <b>AXIOM</b>
calculates them and due to the editing done on the output. If the
example code is free from bugs then a value and type for the code
will be given otherwise just the code will be displayed
</p>
<p>
Although the author has not covered this topic yet please make do
with the following snippet until I write that section:
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="readFromFile">
<h3>6.1 Reading Code From A File</h3>
</a>
<ul>
<li>The names of all input files in <b>AXIOM</b> should end in the suffix
``<tt>.input</tt>'' otherwise <b>AXIOM</b> will refuse to consult them.</li>
<li>Use the command <b>)read fred.input</b> to read and
interpret the contents of the file ``<tt>fred.input</tt>''.</li>
<li>To start everything afresh before reading in an input file
enter the command <b>)clear all</b> or put it at the start of
the file.</li>
</ul>
<!------------------------------------------------------------------------>
<p></p>
<a name="Blocks">
<h3>6.2 Blocks</h3>
</a>
<p>
The <b>AXIOM</b> constructs that provide looping, choices and user-defined
functions all rely on the notion of blocks. According to
<a href="#jenks1992">[jenks1992, page 112]</a>
a block is a sequence of expressions which are evaluated in the order that
they appear except when it is modified by control expressions such as loops.
To leave a block prematurely use the expression <i>BoolExpr => Expr</i>
where <i>BoolExpr</i> is any <b>AXIOM</b> expression that has type
<b>Boolean</b>. The value and type of <i>Expr</i> determines the value and
type returned by the block.
</p>
<p>
If blocks are entered at the keyboard (as opposed to reading code
in from a text file) then there is only one way of creating them;
the syntax is:
</p>
<blockquote>
<tt>(</tt> <i>expression1</i> <tt>;</tt> <i>expression2</i> <tt>;</tt> ...
<tt>;</tt> <i>expressionN</i> <tt>)</tt>
</blockquote>
<p>
In an input file a block can be constructed as above or by placing
all the statements at the same indentation level. In this situation
the block is called a <em>pile</em>. As an example of a simple block a
list of three integers can be constructed using parentheses
</p>
<xmp>
(1) -> (a := 4;b := 1; c := 9; L := [a,b,c])
(1) [4,1,9]
Type: List PositiveInteger
</xmp>
<p>
or using piles
</p>
<xmp>
(2) -> L :=
a := 4
b := 1
c := 9
[a,b,c]
(2) [4,1,9]
Type: List PositiveInteger
</xmp>
<p>
Since blocks have a type and a value they can be used as arguments to
functions or as part of other expressions. It should be pointed out
that the following example is not recommended practice but helps to
illustrate the idea of blocks and their ability to return values:
</p>
<xmp>
(3) -> sqrt(4.0 +
a := 3.0
b := 1.0
c := a + b
c
)
(3) 2.8284271247 461900976
Type: Float
</xmp>
<p>
Note that indentation is <em>extremely</em> important. If the example
given above had the pile starting at ``<tt>a :=</tt>'' moved left by
two spaces so that the ``<tt>a</tt>'' was under the ``<tt>(</tt>'' of the
first line then the interpreter would signal an error. Furthermore,
if the closing bracket ``<tt>)</tt>'' is moved up to give:
</p>
<xmp>
(4) -> sqrt(4.0 +
a := 3.0
b := 1.0
c := a + b
c)
</xmp>
<p>
then the parser will generate errors and if the bracket is
shifted right by several spaces so that it is in line with the ``<tt>c</tt>''
similar errors will be raised. Finally, the ``<tt>)</tt>'' must be
indented by at least one space otherwise more errors will be generated.
</p>
<p>
Thus it can be seen that great care needs to be taken when
constructing input files consisting of piles of expressions. It would
seem prudent to add one pile at a time and check it is acceptable
before adding more, particularly if piles are nested. However, it
should be pointed out that the use of piles as values for functions
is not very readable and so perhaps the delicate nature of their
interpretation should deter programmers from using them in these
situations. Using piles should really be restricted to constructing
functions etc. and a small amount of rewriting can remove the
need to use them as arguments. For example, the previous block could
easily be implemented as:
</p>
<xmp>
(4) -> a := 3.0
b := 1.0
c := a + b
sqrt(4.0 + c)
(4) 2.8284271247 461900976
Type: Float
</xmp>
<p>
which achieves the same result and is easier to understand. Note
that this is still a pile but it is not as fragile as the previous
version.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="functions">
<h3>6.3 Functions</h3>
</a>
<p>
Definitions of functions in <b>AXIOM</b> are quite simple providing two
things are observed. Firstly the type of the function must either be
completely specifed or completely unspecified and secondly the body
of the function is assigned to the function identifier using the
delayed assignment operator ``<tt>==</tt>''.
</p>
<p>
To specify the type of something the ``<tt>:</tt>'' operator is
used. Thus to define a variable <i>x</i> to be of type
<b>Fraction Integer</b> we enter:
</p>
<xmp>
(5) -> x : Fraction Integer
Type: Void
</xmp>
<p>
For functions the method is the same except that the arguments to
are placed in parentheses and the return type is placed after the
symbol ``<tt>-></tt>''. More details on type definitions will be
given later which will explain this more fully. For now an example
of type definitions for functions taking zero, one, two and three
integer arguments and returning a list of integers are given:
</p>
<xmp>
(6) -> f : () -> List Integer
Type: Void
(7) -> g : (Integer) -> List Integer
Type: Void
(8) -> h : (Integer, Integer) -> List Integer
Type: Void
(9) -> k : (Integer, Integer, Integer) -> List Integer
Type: Void
</xmp>
<p>
Now the actual definition of the functions might be:
</p>
<xmp>
(10) -> f() == []
Type: Void
(11) -> g(a) == [a]
Type: Void
(12) -> h(a,b) == [a,b]
Type: Void
(13) -> k(a,b,c) == [a,b,c]
Type: Void
</xmp>
<p>
with some invokations of these functions:
</p>
<xmp>
(14) -> f()
(14) []
Type: List Integer
(15) -> g(4)
(15) [4]
Type: List Integer
(16) -> h(2,9)
(16) [2,9]
Type: List Integer
(17) -> k(-3,42,100)
(17) [-3,42,100]
Type: List Integer
</xmp>
<p>
The value returned by a function is either the value of the last
expression evaluated or the result of a <b>return</b>
statement. For example the following are effectively the same:
</p>
<xmp>
(18) -> p : Integer -> Integer
p x ==
a := 1
b := 2
a + b + x
Type: Void
(19) -> p : Integer -> Integer
p x ==
a := 1
b := 2
return a + b + x
Type: Void
</xmp>
<p>
Note that a block (pile) is assigned to the function identifier
<b>p</b> and thus all the rules about blocks apply to
function definitions. Also there was only one argument so the
parentheses were discarded.
</p>
<p>
This is basically all that one needs to know about defining functions
in <b>AXIOM</b> - first specify the complete type and then assign a block
to the function. The rest of this chapter is concerned with defining
more complex blocks than those in this section and as a result function
definitions will crop up continually particularly since they are a
good way of testing examples.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="choices">
<h3>6.4 Choices</h3>
</a>
<p>
Apart from the ``<tt>=></tt>'' operator that allows a block to be
left before the end <b>AXIOM</b> provides the standard <b>if-then-else</b>
construct. The general syntax is:
</p>
<blockquote>
<tt>if </tt><i>BooleanExpr</i> <tt>then </tt><i>Expr1</i> <tt>else </tt><i>Expr2</i>
</blockquote>
<p>
where ``<tt>else </tt><i>Expr2</i>'' can be omitted. If the
expression <i>BooleanExpr</i> evaluates to <i>true</i> then <i>Expr1</i> is
executed otherwise <i>Expr2</i> (if present) will be. An example of
piles and <b>if-then-else</b> in <b>AXIOM</b> from
<a href="#jenks1992">[jenks1992, page 114]</a>
is given below:
</p>
<xmp>
(20) -> h := 2.0
if h > 3.1 then
1.0
else
z := cos(h)
max(z,0.5)
(20) 0.5
Type: Float
</xmp>
<p>
Note the indentation - the ``<tt>else</tt>'' must be indented relative to
the ``<tt>if</tt>'' otherwise it will generate an error (<b>AXIOM</b> will think
there are two piles, the second one beginning with ``<tt>else</tt>'').
</p>
<p>
Any expression that has type <b>Boolean</b> can be used as
<i>BooleanExpr</i> and the most common will be those involving the relational
operators ``<tt>></tt>'', ``<tt><</tt>'' and ``<tt>=</tt>''. Usually
the type of an expression involving the equality operator ``<tt>=</tt>'' will
be <b>Boolean</b> but in those situations when it isn't you may need to use
the ``<tt>@</tt>'' operator to ensure that it is.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="loops">
<h3>6.5 Loops</h3>
</a>
<p>
Loops in <b>AXIOM</b> are regarded as expressions containing another expression
called the <em>loop body</em>. The loop body is executed zero or more
times depending on the type of the loop. Loops can be nested to any depth.
</p>
<!------------------------------------------------------------------------>
<p></p>
<a name="repeat">
<h4>6.5.1 The repeat Loop</h4>
</a>
<p>
The simplest type of loop provided by <b>AXIOM</b> is the <b>repeat</b>
loop. The general syntax of this is:
</p>
<blockquote>
<tt>repeat </tt><i>loopBody</i>
</blockquote>
<p>
This will cause <b>AXIOM</b> to execute <i>loopBody</i> repeatedly until
either a <b>break</b> or
<b>return</b> statement are encountered. If <i>loopBody</i>
contains neither of these statements then it will loop forever. The
following piece of code will display the numbers from 1 to 4:
</p>
<xmp>
(21) -> i := 1
repeat
if i > 4 then break
output(i)
i := i + 1
1
2
3
4
Type: Void
</xmp>
<p>
<b>Note:</b> In <b>AXIOM</b> Release 2.0 the
<b>break</b> keyword replaces <b>leave</b> which was
used in earlier versions. Use of <b>leave</b> will generate an
error if it is used with <b>AXIOM</b> Release 2.0 or higher.
</p>
<p>
It was mentioned that loops will only be left when either a
<b>break</b> or <b>return</b> statement is
encountered so why can't one use the ``<tt>=></tt>'' operator?
The reason is that the ``<tt>=></tt>'' operator tells <b>AXIOM</b>
to leave the current block whereas <b>break</b> and
<b>return</b> tell <b>AXIOM</b> to leave the current loop
(the latter actually tells <b>AXIOM</b> to leave the current function
which could mean leaving several levels of loop if necessary).
An example of this is given in
<a href="#jenks1992">[jenks1992]</a>
and is provided here for reference:
</p>
<xmp>
(22) -> i := 0
repeat
i := i + 1
i > 3 => i
output(i)
1
2
3
^C
>> Sytem error:
Console interrupt.
</xmp>
<p>
Here the ``<tt>=></tt>'' operator instructs <b>AXIOM</b> to leave
the block when the value of <i>i</i> reaches 4 but then the loop continues
to execute the first statement (increment <i>i</i>) forever (or until the
user interrupts it). However, the <b>break</b> keyword can be used to
represent a return value so we can combine the two previous examples as
follows:
</p>
<xmp>
(22) -> i := 0
repeat
i := i + 1
i > 3 => break
output(i)
1
2
3
Type: Void
</xmp>
<p>
To skip the rest of a loop body and begin the next iteration of
the loop one uses the <b>iterate</b> keyword:
</p>
<xmp>
(23) -> i := 0
repeat
i := i + 1
if i > 6 then break
-- Return to start if i is odd.
if odd?(i) then iterate
output(i)
2
4
6
Type: Void
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="while">
<h4>6.5.2 The while Loop</h4>
</a>
<p>
The <b>while</b> statement extends the basic
<b>repeat</b> loop to place the control of leaving the
loop at the start rather than have it buried in the middle. Since
the body of the loop is still part of a <b>repeat</b> loop,
<b>break</b> and ``<tt>=></tt>'' work in the same way as in the
previous section. The general syntax of a <b>while</b> loop is:
</p>
<blockquote>
<tt>while </tt><i>BoolExpr</i><tt> repeat </tt><i>loopBody</i>
</blockquote>
<p>
As before, <i>BoolExpr</i> must be an expression of type <b>Boolean</b>.
Before the body of the loop is executed <i>BoolExpr</i> is tested. If it
evaluates to <b>true</b> then the loop body is entered otherwise the loop
is terminated. Multiple conditions can be applied using the logical
operators such as <b>and</b> or by using several
<b>while</b> statements before the <b>repeat</b>.
The following examples are from
<a href="#jenks1992">[jenks1992, page 122]</a>:
</p>
<xmp>
(24) -> x := 1; y := 1
while x < 4 and y < 10 repeat
output [x,y]
x := x + 1
y := y + 2
[1,1]
[2,3]
[3,5]
Type: Void
(25) -> x := 1; y := 1
while x < 4 while y < 10 repeat
output [x,y]
x := x + 1
y := y + 2
[1,1]
[2,3]
[3,5]
Type: Void
</xmp>
<p>
Note that the last example using two <b>while</b> statements
is <em>not</em> a nested loop but the following one is:
</p>
<xmp>
(26) -> x := 1; y := 1
while x < 4 repeat
while y < 10 repeat
output [x,y]
x := x + 1
y := y + 2
[1,1]
[2,3]
[3,5]
[4,7]
[5,9]
Type: Void
</xmp>
<p>
As a longer example the following is taken from
<a href="#jenks1992">[jenks1992, page 123]</a>
although the matrix used is different. Given a matrix of arbitrary size, find
the position and value of the first negative element by examining the
matrix in row-major order:
</p>
<xmp>
(27) -> m := matrix [[21,37,53,14],_
[8,22,-24,16],_
[2,10,15,14],_
[26,33,55,-13]]
+21 37 53 14 +
| |
|8 22 - 24 16 |
(27) | |
|2 10 15 14 |
| |
+26 33 55 - 13+
Type: Matrix Integer
(28) -> lastrow := nrows(m); lastcol := ncols(m)
r := 1
while r <= lastrow repeat
c := 1 -- Index of first column
while c <= lastcol repeat
if elt(m,r,c) < 0 then
output [r,c,elt(m,r,c)]
r := lastrow
break -- Don't look any further
c := c + 1
r := r + 1
[2,3,-24]
Type: Void
</xmp>
<!------------------------------------------------------------------------>
<p></p>
<a name="for">
<h4>6.5.3 The for Loop</h4>
</a>
<p>
The last loop of interest is the <b>for</b> loop. There are
two ways of creating a <b>for</b> loop and the general syntax
for both is as follows (with and without the ``such that'' predicates):
</p>
<blockquote>
<tt>for </tt><i>var</i><tt> in </tt><i>seg</i><tt> repeat </tt><i>loopBody</i><br>
<tt>for </tt><i>var</i><tt> in </tt><i>list</i><tt> repeat </tt><i>loopBody</i><br>
<tt>for </tt><i>var</i><tt> in </tt><i>seg</i><tt> | </tt><i>BoolExpr</i><tt>
repeat </tt><i>loopBody</i><br>
<tt>for </tt><i>var</i><tt> in </tt><i>list</i><tt> | </tt><i>BoolExpr</i><tt>
repeat </tt><i>loopBody</i><br>
</blockquote>
<p>
where <i>var</i> is an index variable which is iterated over
the values in <i>seg</i> or <i>list</i>. The value <i>seg</i> is
a segment e.g. <tt>1..10</tt> or <tt>1..</tt> and <i>list</i> is
a list of some type. The last two types of <b>for</b> loop will
only execute the statements in <i>loopBody</i> if <i>BoolExpr</i>
evaluates to <b>true</b>. Some examples of this type of loop are
given below:
</p>
<xmp>
(29) -> for i in 1..10 repeat
~prime?(i) => iterate
output(i)
2
3
5
7
Type: Void
</xmp>
<xmp>
(30) -> for i in 1..10 | prime?(i) repeat
output(i)
2
3
5
7
Type: Void
</xmp>
<xmp>
(31) -> for w in ["This", "Is", "Your", "Life!"] repeat
output(w)
This
Is
Your
Life
Type: Void
</xmp>
<xmp>
(32) -> for i in 1.. repeat
if even?(i) then output(i)
if i < 7 then iterate
break
2
4
6
Type: Void
</xmp>
<p>
The last example loop can be simplified by adding a
<b>while</b> clause:
</p>
<xmp>
(33) -> for i in 1.. while i < 7 repeat
if even?(i) then output(i)
2
4
6
Type: Void
</xmp>
<p>
and by using the ``such that'' clause it becomes even
simpler:
</p>
<xmp>
(34) -> for i in 1.. | even?(i) while i < 7 repeat
output(i)
2
4
6
Type: Void
</xmp>
<p>
Similarly it is possible to have multiple <b>for</b>
clauses on a line to iterate over several symbols in parallel:
</p>
<xmp>
(35) -> for a in 1..4 for b in 5..8 repeat
output [a,b]
[1,5]
[2,6]
[3,7]
[4,8]
Type: Void
</xmp>
<p>
As a general point it should be noted that any symbols referred
to in the ``such that'' and <b>while</b> clauses must be
pre-defined. This either means that the symbols must have been
defined in an outer level (e.g. in an enclosing loop) or in a
<b>for</b> clause appearing before the ``such that'' or
<b>while</b>. For example,
</p>
<xmp>
(36) -> for a in 1..4 repeat
for j in 7..9 | prime?(a + b) repeat
output [a,b,a + b]
[2,9,11]
[3,8,11]
[4,7,11]
[4,9,13]
Type: Void
</xmp>
<p>
is legal but
</p>
<xmp>
(37) -> for b in 7..9 | prime?(a + b) repeat
for a in 1..4 repeat
output [a,b,a + b]
</xmp>
<p>
is not and will generate an error because <i></i> is not defined
in the first line.
</p>
<p>
Finally it is often useful to be able to use a segment in which the
elements are in decreasing order. To do this the <b>by</b>
keyword is used:
</p>
<xmp>
(38) -> for a in 1..4 for b in 8..5 by -1 repeat
output [a,b]
[1,8]
[2,7]
[3,6]
[4,5]
Type: Void
</xmp>
<p>
Note that without ``<b>by -1</b>'' the loop body would never
be executed (the segment 8..5 is empty so there is nothing to
iterate over):
</p>
<xmp>
(39) -> for a in 1..4 for b in 8..5 by -1 repeat
output [a,b]
Type: Void
</xmp>
<hr>
<!------------------------------------------------------------------------>
<p></p>
<a name="biblio">
<h2>Bibliography</h2>
</a>
<dl>
<dt><a name="jenks1992">[jenks1992]</a></dt>
<dd>Richard D. Jenks and Robert S. Sutor.
<i>AXIOM: the scientific computation system.</i>
NAG Ltd., 1992.</dd>
<dt><a name="watt1995">[watt1995]</a></dt>
<dd>Stephen M. Watt, Peter A. Broadbery, Samuel S. Dooley, Pietro Iglio,
Scott C. Morrison, Jonathon M. Steinbach, and Robert S. Sutor.
<i>AXIOM Library Compiler User Guide.</i>
NAG Ltd., first edition, March 1995. Reprinted with
corrections from November 1994.</dd>
</dl>
<hr>
<address>
<b>Martin Dunstan</b><tt> (mnd@dcs.st-andrews.ac.uk)</tt><br>
Updated: 2nd May, 1996.
</address>
</body>
</html>
|