home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mem / Documentation / Plain Text / Icon Overview next >
Encoding:
Text File  |  1992-09-18  |  18.7 KB  |  793 lines  |  [TEXT/MPS ]

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                An Overview of the Icon Programming
  15.                       Language; Version 8*
  16.  
  17.  
  18.                         Ralph E. Griswold
  19.  
  20.  
  21.  
  22.  
  23.                             TR 90-6c
  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.           January 1, 1990; last revised March 11, 1992
  50.  
  51.  
  52.                  Department of Computer Science
  53.  
  54.                     The University of Arizona
  55.  
  56.                       Tucson, Arizona 85721
  57.  
  58.  
  59.  
  60.  
  61. *This work was supported by the National Science Foundation under
  62. Grant CCR-8713690.
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.                An Overview of the Icon Programming
  77.                        Language; Version 8
  78.  
  79.  
  80.  
  81.  
  82. 1.__Introduction
  83.  
  84.    Icon is a high-level programming language with extensive
  85. facilities for processing strings and structures. Icon has
  86. several novel features, including expressions that may produce
  87. sequences of results, goal-directed evaluation that automatically
  88. searches for a successful result, and string scanning that allows
  89. operations on strings to be formulated at a high conceptual
  90. level.
  91.  
  92.    Icon emphasizes high-level string processing and a design phi-
  93. losophy that allows ease of programming and short, concise pro-
  94. grams. Storage allocation and garbage collection are automatic in
  95. Icon, and there are few restrictions on the sizes of objects.
  96. Strings, lists, and other structures are created during program
  97. execution and their size does not need to be known when a program
  98. is written.  Values are converted to expected types automati-
  99. cally; for example, numeral strings read in as input can be used
  100. in numerical computations without explicit conversion.  Icon has
  101. an expression-based syntax with reserved words; in appearance,
  102. Icon programs resemble those of Pascal and C.
  103.  
  104.    Although Icon has extensive facilities for processing strings
  105. and structures, it also has a full repertoire of computational
  106. facilities. It is suitable for a wide variety of applications.
  107. Some examples are:
  108.  
  109.      +  text analysis, editing, and formatting
  110.  
  111.      +  document formating
  112.  
  113.      +  artificial intelligence
  114.  
  115.      +  expert systems
  116.  
  117.      +  rapid prototyping
  118.  
  119.      +  symbolic mathematics
  120.  
  121.      +  text generation
  122.  
  123.      +  data laundry
  124.  
  125.    There are public-domain implementations of Icon for the Acorn
  126. Archimedes, the Amiga, the Atari ST, the Macintosh, MS-DOS, MVS,
  127.  
  128.  
  129.  
  130.                               - 1 -
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. OS/2, many UNIX systems, VM/CMS, and VMS. There also are commer-
  140. cial implementations of enhanced versions of Icon for the Macin-
  141. tosh and for 386 UNIX platforms.
  142.  
  143.    The remainder of this report briefly describes the highlights
  144. of Icon.  For a complete description, see [1].
  145.  
  146.  
  147. 2.__Expression_Evaluation
  148.  
  149. 2.1__Conditional_Expressions
  150.  
  151.    In Icon there are conditional expressions that may succeed and
  152. produce a result, or may fail and not produce any result. An
  153. example is the comparison operation
  154.  
  155.         i > j
  156.  
  157. which succeeds (and produces the value of j) provided that the
  158. value of i is greater than the value of j, but fails otherwise.
  159. Similarly,
  160.  
  161.         i > j > k
  162.  
  163. succeeds if the value of j is between i and k.
  164.  
  165.    The success or failure of conditional operations is used
  166. instead of Boolean values to drive control structures in Icon. An
  167. example is
  168.  
  169.         if i > j then k := i else k := j
  170.  
  171. which assigns the value of i to k if the value of i is greater
  172. than the value of j, but assigns the value of j to k otherwise.
  173.  
  174.    The usefulness of the concepts of success and failure is
  175. illustrated by find(s1,s2), which fails if s1 does not occur as a
  176. substring of s2.  Thus
  177.  
  178.         if i := find("or",line) then write(i)
  179.  
  180. writes the position at which "or" occurs in line, if it occurs,
  181. but does not write a value if it does not occur.
  182.  
  183.    Many expressions in Icon are conditional. An example is
  184. read(), which produces the next line from the input file, but
  185. fails when the end of the file is reached. The following expres-
  186. sion is typical of programming in Icon and illustrates the
  187. integration of conditional expressions and conventional control
  188. structures:
  189.  
  190.         while line := read() do
  191.            write(line)
  192.  
  193.  
  194.  
  195.  
  196.                               - 2 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205. This expression copies the input file to the output file.
  206.  
  207.    If an argument of a function fails, the function is not
  208. called, and the function call fails as well. This ``inheritance''
  209. of failure allows the concise formulation of many programming
  210. tasks. Omitting the optional do clause in while-do, the previous
  211. expression can be rewritten as
  212.  
  213.         while write(read())
  214.  
  215.  
  216. 2.2__Generators
  217.  
  218.    In some situations, an expression may be capable of producing
  219. more than one result. Consider
  220.  
  221.         sentence := "Store it in the neighboring harbor"
  222.         find("or", sentence)
  223.  
  224. Here "or" occurs in sentence at positions 3, 23, and 33. Most
  225. programming languages treat this situation by selecting one of
  226. the positions, such as the first, as the result of the expres-
  227. sion. In Icon, such an expression is a generator and is capable
  228. of producing all three positions.
  229.  
  230.    The results that a generator produces depend on context. In a
  231. situation where only one result is needed, the first is produced,
  232. as in
  233.  
  234.         i := find("or", sentence)
  235.  
  236. which assigns the value 3 to i.
  237.  
  238.    If the result produced by a generator does not lead to the
  239. success of an enclosing expression, however, the generator is
  240. resumed to produce another value. An example is
  241.  
  242.         if (i := find("or", sentence)) > 5 then write(i)
  243.  
  244. Here the first result produced by the generator, 3, is assigned
  245. to i, but this value is not greater than 5 and the comparison
  246. operation fails. At this point, the generator is resumed and pro-
  247. duces the second position, 23, which is greater than 5. The com-
  248. parison operation then succeeds and the value 23 is written.
  249. Because of the inheritance of failure and the fact that com-
  250. parison operations return the value of their right argument, this
  251. expression can be written in the following more compact form:
  252.  
  253.         write(5 < find("or", sentence))
  254.  
  255.  
  256.    Goal-directed evaluation is inherent in the expression evalua-
  257. tion mechanism of Icon and can be used in arbitrarily complicated
  258. situations.  For example,
  259.  
  260.  
  261.  
  262.                               - 3 -
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.         find("or", sentence1) = find("and", sentence2)
  272.  
  273. succeeds if "or" occurs in sentence1 at the same position as and
  274. occurs in sentence2.
  275.  
  276.    A generator can be resumed repeatedly to produce all its
  277. results by using the every-do control structure. An example is
  278.  
  279.         every i := find("or", sentence)
  280.            do write(i)
  281.  
  282. which writes all the positions at which "or" occurs in sentence.
  283. For the example above, these are 3, 23, and 33.
  284.  
  285.    Generation is inherited like failure, and this expression can
  286. be written more concisely by omitting the optional do clause:
  287.  
  288.         every write(find("or", sentence))
  289.  
  290.  
  291.    There are several built-in generators in Icon. One of the most
  292. frequently used of these is
  293.  
  294.         i to j
  295.  
  296. which generates the integers from i to j. This generator can be
  297. combined with every-do to formulate the traditional for-style
  298. control structure:
  299.  
  300.         every k := i to j do
  301.            f(k)
  302.  
  303. Note that this expression can be written more compactly as
  304.  
  305.         every f(i to j)
  306.  
  307.  
  308.    There are a number of other control structures related to gen-
  309. eration.  One is alternation,
  310.  
  311.         expr1 | expr2
  312.  
  313. which generates the results of expr1 followed by the results of
  314. expr2.  Thus
  315.  
  316.         every write(find("or", sentence1) | find("or", sentence2))
  317.  
  318. writes the positions of "or" in sentence1 followed by the posi-
  319. tions of "or" in sentence2. Again, this sentence can be written
  320. more compactly by using alternation in the second argument of
  321. find():
  322.  
  323.         every write(find("or", sentence1 | sentence2))
  324.  
  325.  
  326.  
  327.  
  328.                               - 4 -
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.    Another use of alternation is illustrated by
  338.  
  339.         (i | j | k) = (0 | 1)
  340.  
  341. which succeeds if any of i, j, or k has the value 0 or 1.
  342.  
  343.    Procedures can be used to add generators to Icon's built-in
  344. repertoire. For example,
  345.  
  346.         procedure findodd(s1, s2)
  347.            every i := find(s1, s2) do
  348.               if i % 2 = 1 then suspend i
  349.         end
  350.  
  351. is a procedure that generates the odd-valued positions at which
  352. s1 occurs in s2. The suspend control structure returns a value
  353. from the procedure, but leaves it in suspension so that it can be
  354. resumed for another value. When the loop terminates, control
  355. flows off the end of the procedure without producing another
  356. value.
  357.  
  358.  
  359. 3.__String_Scanning
  360.  
  361.    For complicated operations, the bookkeeping involved in keep-
  362. ing track of positions in strings becomes burdensome and error
  363. prone.  Icon has a string scanning facility that is manages posi-
  364. tions automatically.  Attention is focused on a current position
  365. in a string as it is examined by a sequence of operations.
  366.  
  367.    The string scanning operation has the form
  368.  
  369.         s ? expr
  370.  
  371. where s is the subject string to be examined and expr is an
  372. expression that performs the examination.  A position in the sub-
  373. ject, which starts at 1, is the focus of examination.
  374.  
  375.    Matching functions change this position.  One matching func-
  376. tion, move(i), moves the position by i and produces the substring
  377. of the subject between the previous and new positions. If the
  378. position cannot be moved by the specified amount (because the
  379. subject is not long enough), move(i) fails. A simple example is
  380.  
  381.         line ? while write(move(2))
  382.  
  383. which writes successive two-character substrings of line, stop-
  384. ping when there are no more characters.
  385.  
  386.    Another matching function is tab(i), which sets the position
  387. in the subject to i and also returns the substring of the subject
  388. between the previous and new positions.  For example,
  389.  
  390.  
  391.  
  392.  
  393.  
  394.                               - 5 -
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.         line ? if tab(10) then write(tab(0))
  404.  
  405. first sets the position in the subject to 10 and then to the end
  406. of the subject, writing line[10:0].  Note that no value is writ-
  407. ten if the subject is not long enough.
  408.  
  409.    String analysis functions such as find() can be used in string
  410. scanning. In this context, the string that they operate on is not
  411. specified and is taken to be the subject. For example,
  412.  
  413.         line ? while write(tab(find("or")))
  414.            do move(2)
  415.  
  416. writes all the substrings of line prior to occurrences of "or".
  417. Note that find() produces a position, which is then used by tab
  418. to change the position and produce the desired substring. The
  419. move(2) skips the "or" that is found.
  420.  
  421.    Another example of the use of string analysis functions in
  422. scanning is
  423.  
  424.         line ? while tab(upto(&letters)) do
  425.            write(tab(many(&letters)))
  426.  
  427. which writes all the words in line.
  428.  
  429.    As illustrated in the examples above, any expression may occur
  430. in the scanning expression.
  431.  
  432.  
  433. 4.__Structures
  434.  
  435.    Icon supports several kinds of structures with different
  436. organizations and access methods. Lists are linear structures
  437. that can be accessed both by position and by stack and queue
  438. functions. Sets are collections of arbitrary values with no
  439. implied ordering. Tables provide an associative lookup mechanism.
  440.  
  441. 4.1__Lists
  442.  
  443.    While strings are sequences of characters, lists in Icon are
  444. sequences of values of arbitrary types. Lists are created by
  445. enclosing the lists of values in brackets. An example is
  446.  
  447.         car1 := ["buick", "skylark",  1978, 2450]
  448.  
  449. in which the list car1 has four values, two of which are strings
  450. and two of which are integers. Note that the values in a list
  451. need not all be of the same type. In fact, any kind of value can
  452. occur in a list - even another list, as in
  453.  
  454.         inventory := [car1, car2, car3, car4]
  455.  
  456.  
  457.  
  458.  
  459.  
  460.                               - 6 -
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.    Lists also can be created by
  470.  
  471.         L := list(i, x)
  472.  
  473. which creates a list of i values, each of which has the value x.
  474.  
  475.    The values in a list can be referenced by position much like
  476. the characters in a string. Thus
  477.  
  478.         car1[4] := 2400
  479.  
  480. changes the last value in car1 to 2400.  A reference that is out
  481. of the range of the list fails. For example,
  482.  
  483.         write(car1[5])
  484.  
  485. fails.
  486.  
  487.    The values in a list L are generated by !L. Thus
  488.  
  489.         every write(!L)
  490.  
  491. writes all the values in L.
  492.  
  493.    Lists can be manipulated like stacks and queues. The function
  494. push(L, x) adds the value of x to the left end of the list L,
  495. automatically increasing the size of L by one. Similarly, pop(L)
  496. removes the leftmost value from L, automatically decreasing the
  497. size of L by one, and produces the removed value.
  498.  
  499. 4.2__Sets
  500.  
  501.    A sets is a collection of values.  An empty set is created by
  502. set().  Alternatively, set(L) produces a set with the values in
  503. the list L.  For example,
  504.  
  505.         S := set([1, "abc", []])
  506.  
  507. assigns to S a set that contains the integer 1, the string "abc",
  508. and an empty list.
  509.  
  510.    The set operations of union, intersection, and difference are
  511. provided.  The function member(S, x) succeeds if x is a member of
  512. the set S but fails otherwise. The function insert(S, x) adds x
  513. to the set S, while delete(S, x) removes x from S. A value only
  514. can occur once in a set, so insert(S, x) has no effect if x is
  515. already in S.  !S generates the members of S.
  516.  
  517.    A simple example of the use of sets is given by the following
  518. segment of code, which lists all the different words that appear
  519. in the input file:
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.                               - 7 -
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.         words := set()
  536.         while line := read() do
  537.            line ? while tab(upto(&letters)) do
  538.               insert(words, tab(many(&letters)))
  539.         every write(!words)
  540.  
  541.  
  542. 4.3__Tables
  543.  
  544.    Tables are sets of pairs each of which consists of a key and a
  545. corresponding value. The key and its corresponding value may be
  546. of any type, and the value for any key can be looked up automati-
  547. cally.  Thus, tables provide a form of associative access in con-
  548. trast with the positional access to values in lists.
  549.  
  550.    A table is created by an expression such as
  551.  
  552.         symbols := table(0)
  553.  
  554. which assigns to symbols a table with the default value 0.  The
  555. default value is used for keys that are not assigned another
  556. value.  Subsequently, symbols can be referenced by any key, such
  557. as
  558.  
  559.         symbols["there"] := 1
  560.  
  561. which assigns the value 1 to the key "there" in symbols.
  562.  
  563.    Tables grow automatically as new keys are added.  For example,
  564. the following program segment produces a table containing a count
  565. of the words that appear in the input file:
  566.  
  567.         words := table(0)
  568.         while line := read() do
  569.            line ? while tab(upto(&letters)) do
  570.               words[tab(many(&letters))] +:= 1
  571.  
  572. Here the default value for each word is 0, as given in table(0),
  573. and +:= is an augmented assignment operation that increments the
  574. values by one.  There are augmented assignment operations for all
  575. binary operators.
  576.  
  577.    A list can be obtained from a table T by the function sort(T,
  578. 1).  The form of the list depends on the value of i. For example,
  579. if i is 3, the list contains alternate keys and their correspond-
  580. ing values in T.  For example,
  581.  
  582.         wordlist := sort(words, 3)
  583.         while write(pop(wordlist), " : ", pop(wordlist))
  584.  
  585. writes the words and their counts from words.
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.                               - 8 -
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601. 5.__An_Example
  602.  
  603.    The following program, which produces a concordance of the
  604. words from an input file, illustrates typical Icon programming
  605. techniques. Although not all the features in this program are
  606. described in previous sections, the general idea should be clear.
  607.  
  608.         global uses, lineno, width
  609.  
  610.  
  611.         procedure main(args)
  612.            width := 15                # width of word field
  613.            uses := table()
  614.            lineno := 0
  615.            every tabulate(words())    # tabulate all the citations
  616.            output()                   # print the citations
  617.         end
  618.  
  619.  
  620.         #  Add line number to citations for word
  621.         #
  622.         procedure tabulate(word)
  623.            /uses[word] := set()
  624.            insert(uses[word], lineno)
  625.            return
  626.         end
  627.  
  628.  
  629.         #  Generate words
  630.         #
  631.         procedure words()
  632.            while line := read() do {
  633.               lineno +:= 1
  634.               write(right(lineno, 6), "  ", line)
  635.               map(line) ? while tab(upto(&letters)) do {
  636.                  s := tab(many(&letters))
  637.                  if *s >= 3 then suspend s# skip short words
  638.                  }
  639.               }
  640.         end
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.                               - 9 -
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.         #  Print the results
  668.         #
  669.         procedure output()
  670.            write()                    # blank line
  671.            uses := sort(uses, 3)      # sort citations
  672.            while word := get(uses) do {
  673.               line := ""
  674.               numbers := sort(get(uses))
  675.               while line ||:= get(numbers) || ", "
  676.               write(left(word, width), line[1:-2])
  677.               }
  678.         end
  679.  
  680. The program reads a line, writes it out with an identifying line
  681. number, and processes every word in the line. Words less than
  682. three characters long are considered to be ``noise'' and are dis-
  683. carded. A table, uses, is keyed by the words. Every key has a
  684. corresponding set of line numbers. The first time a word is
  685. encountered, a new set is created for it. The line number is
  686. inserted in any event. Since a value can be in a set only once,
  687. duplicate line numbers are suppressed automatically.
  688.  
  689.    After all the input has been read, the table of words is
  690. sorted by key.  Each corresponding set of line numbers is sorted
  691. and the line numbers are appended to the line to be written.
  692.  
  693.    For example, if the input file is
  694.  
  695.                  On the Future!-how it tells
  696.                  Of the rapture that impells
  697.                 To the swinging and the ringing
  698.                  Of the bells, bells, bells-
  699.               Of the bells, bells, bells, bells,
  700.                         Bells, bells, bells-
  701.           To the rhyming and the chiming of the bells!
  702.  
  703. the output is
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.                              - 10 -
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.                1           On the Future!-how it tells
  734.                2           Of the rapture that impells
  735.                3          To the swinging and the ringing
  736.                4           Of the bells, bells, bells-
  737.                5        Of the bells, bells, bells, bells,
  738.                6                  Bells, bells, bells-
  739.                7    To the rhyming and the chiming of the bells!
  740.  
  741.         and       3, 7
  742.         bells     4, 5, 6, 7
  743.         chiming   7
  744.         future    1
  745.         how       1
  746.         impells   2
  747.         rapture   2
  748.         rhyming   7
  749.         ringing   3
  750.         swinging  3
  751.         tells     1
  752.         that      2
  753.         the       1, 2, 3, 4, 5, 7
  754.  
  755.  
  756. Acknowledgement
  757.  
  758.    Icon was designed by the the author in collaboration with Dave
  759. Hanson, Tim Korb, Cary Coutant, and Steve Wampler. Many other
  760. persons have contributed to its development. The current imple-
  761. mentation is based on the work of Cary Coutant and Steve Wampler
  762. with recent contributions by Bill Mitchell, Janalee O'Bagy, Gregg
  763. Townsend, and Ken Walker.
  764.  
  765. References
  766.  
  767.  
  768. 1.   R. E. Griswold and M. T. Griswold, The Icon Programming
  769.      Language, Prentice-Hall, Inc., Englewood Cliffs, NJ, second
  770.      edition, 1990.
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.                              - 11 -
  791.  
  792.  
  793.