home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ICONV8.ZIP / DOCS.ZIP / TR90-6.DOC < prev    next >
Text File  |  1990-03-29  |  20KB  |  793 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                 An Overview of Version 8 the Icon
  15.                       Programming Language*
  16.  
  17.  
  18.                         Ralph E. Griswold
  19.  
  20.  
  21.  
  22.  
  23.                             TR 90-6a
  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 February 13, 1990
  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 Version 8 the Icon
  77.                       Programming Language
  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 Amiga,
  126. the Atari ST, the Macintosh under MPW, MS-DOS, MVS, OS/2, many
  127.  
  128.  
  129.  
  130.                               - 1 -
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. UNIX systems, VM/CMS, and VMS. There also is a commercial imple-
  140. mentation of an enhanced version of Icon for the Macintosh [1].
  141.  
  142.    The remainder of this report briefly describes the highlights
  143. of Icon.  For a complete description, see [2,3].
  144.  
  145.  
  146. 2.__Expression_Evaluation
  147.  
  148. 2.1__Conditional_Expressions
  149.  
  150.    In Icon there are conditional expressions that may succeed and
  151. produce a result, or may fail and not produce any result. An
  152. example is the comparison operation
  153.  
  154.         i > j
  155.  
  156. which succeeds (and produces the value of j) provided that the
  157. value of i is greater than the value of j, but fails otherwise.
  158. Similarly,
  159.  
  160.         i > j > k
  161.  
  162. succeeds if the value of j is between i and k.
  163.  
  164.    The success or failure of conditional operations is used
  165. instead of Boolean values to drive control structures in Icon. An
  166. example is
  167.  
  168.         if i > j then k := i else k := j
  169.  
  170. which assigns the value of i to k if the value of i is greater
  171. than the value of j, but assigns the value of j to k otherwise.
  172.  
  173.    The usefulness of the concepts of success and failure is
  174. illustrated by find(s1,s2), which fails if s1 does not occur as a
  175. substring of s2.  Thus
  176.  
  177.         if i := find("or",line) then write(i)
  178.  
  179. writes the position at which "or" occurs in line, if it occurs,
  180. but does not write a value if it does not occur.
  181.  
  182.    Many expressions in Icon are conditional. An example is
  183. read(), which produces the next line from the input file, but
  184. fails when the end of the file is reached. The following expres-
  185. sion is typical of programming in Icon and illustrates the
  186. integration of conditional expressions and conventional control
  187. structures:
  188.  
  189.         while line := read() do
  190.            write(line)
  191.  
  192. This expression copies the input file to the output file.
  193.  
  194.  
  195.  
  196.                               - 2 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.    If an argument of a function fails, the function is not
  206. called, and the function call fails as well. This ``inheritance''
  207. of failure allows the concise formulation of many programming
  208. tasks. Omitting the optional do clause in while-do, the previous
  209. expression can be rewritten as
  210.  
  211.         while write(read())
  212.  
  213.  
  214. 2.2__Generators
  215.  
  216.    In some situations, an expression may be capable of producing
  217. more than one result. Consider
  218.  
  219.         sentence := "Store it in the neighboring harbor"
  220.         find("or",sentence)
  221.  
  222. Here "or" occurs in sentence at positions 3, 23, and 33. Most
  223. programming languages treat this situation by selecting one of
  224. the positions, such as the first, as the result of the expres-
  225. sion. In Icon, such an expression is a generator and is capable
  226. of producing all three positions.
  227.  
  228.    The results that a generator produces depend on context. In a
  229. situation where only one result is needed, the first is produced,
  230. as in
  231.  
  232.         i := find("or",sentence)
  233.  
  234. which assigns the value 3 to i.
  235.  
  236.    If the result produced by a generator does not lead to the
  237. success of an enclosing expression, however, the generator is
  238. resumed to produce another value. An example is
  239.  
  240.         if (i := find("or",sentence)) > 5 then write(i)
  241.  
  242. Here the first result produced by the generator, 3, is assigned
  243. to i, but this value is not greater than 5 and the comparison
  244. operation fails. At this point, the generator is resumed and pro-
  245. duces the second position, 23, which is greater than 5. The com-
  246. parison operation then succeeds and the value 23 is written.
  247. Because of the inheritance of failure and the fact that com-
  248. parison operations return the value of their right argument, this
  249. expression can be written in the following more compact form:
  250.  
  251.         write(5 < find("or",sentence))
  252.  
  253.  
  254.    Goal-directed evaluation is inherent in the expression evalua-
  255. tion mechanism of Icon and can be used in arbitrarily complicated
  256. situations.  For example,
  257.  
  258.  
  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 to
  513. the set S, while delete(S,x) removes x from S. A value only can
  514. occur once in a set, so insert(S,x) has no effect if x is already
  515. 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
  578. sort(T,1).  The form of the list depends on the value of i. For
  579. example, if i is 3, the list contains alternate keys and their
  580. corresponding 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.         procedure main(args)
  611.            width := 15                # width of word field
  612.            uses := table()
  613.            lineno := 0
  614.            every tabulate(words())    # tabulate all the citations
  615.            output()                   # print the citations
  616.         end
  617.  
  618.  
  619.         #  Add line number to citations for word
  620.         #
  621.         procedure tabulate(word)
  622.            /uses[word] := table()
  623.            uses[word][lineno] := 1
  624.            return
  625.         end
  626.  
  627.  
  628.         #  Generate words
  629.         #
  630.         procedure words()
  631.            while line := read() do {
  632.               lineno +:= 1
  633.               write(right(lineno,6),"  ",line)
  634.               map(line) ? while tab(upto(&letters)) do {
  635.                  s := tab(many(&letters))
  636.                  if *s < 3 then next  # skip short words
  637.                  suspend s
  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),3)
  675.               while line ||:= get(numbers) || ", " do
  676.                  get(numbers)         # skip marking value
  677.               write(left(word,width),line[1:-2])
  678.               }
  679.         end
  680.  
  681. The program reads a line, writes it out with an identifying line
  682. number, and processes every word in the line. Words less than
  683. three characters long are considered to be ``noise'' and are dis-
  684. carded. A table, uses, is keyed by the words. Every key has a
  685. corresponding set of line numbers. The first time a word is
  686. encountered, a new set is created for it. The line number is
  687. inserted in any event. Since a value can be in a set only once,
  688. duplicate line numbers are suppressed automatically.
  689.  
  690.    After all the input has been read, the table of words is
  691. sorted by key.  Each corresponding set of line numbers is sorted
  692. and the line numbers are appended to the line to be written.
  693.  
  694.    For example, if the input file is
  695.  
  696.                  On the Future!-how it tells
  697.                  Of the rapture that impells
  698.                 To the swinging and the ringing
  699.                  Of the bells, bells, bells-
  700.               Of the bells, bells, bells, bells,
  701.                         Bells, bells, bells-
  702.           To the rhyming and the chiming of the bells!
  703.  
  704. the output is
  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 ProIcon Programming
  769.      Language for the Apple Macintosh Computers, The Bright
  770.      Forest Company, Tucson, AZ, 1989.
  771.  
  772. 2.   R. E. Griswold and M. T. Griswold, The Icon Programming
  773.      Language, Prentice-Hall, Inc., Englewood Cliffs, NJ, 1983.
  774.  
  775. 3.   R. E. Griswold, Version 8 of Icon, The Univ. of Arizona
  776.      Tech. Rep. 90-1, 1990.
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.                              - 11 -
  791.  
  792.  
  793.