home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: WPS_PM / WPS_PM.zip / ICONPL2.ZIP / DOCS.ARC / OVERVIEW.DOC < prev    next >
Text File  |  1988-11-03  |  28KB  |  1,006 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                An Overview of the Icon Programming
  15.                             Language*
  16.  
  17.  
  18.                         Ralph E. Griswold
  19.  
  20.  
  21.  
  22.  
  23.                             TR 83-3f
  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.           May 13, 1983; last revised December 27, 1987
  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 DCR-8401831.
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.                An Overview of the Icon Programming
  77.                             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 lists. Icon has several
  86. novel features, including expressions that may produce sequences
  87. of results, goal-directed evaluation that automatically searches
  88. for a successful result, and string scanning that allows opera-
  89. tions on strings to be formulated at a high conceptual level.
  90.  
  91.    Icon resembles SNOBOL4 [1] in its emphasis on high-level
  92. string processing and a design philosophy that allows ease of
  93. programming and short, concise programs. Like SNOBOL4, storage
  94. allocation and garbage collection are automatic in Icon, and
  95. there are few restrictions on the sizes of objects. Strings,
  96. lists, and other structures are created during program execution
  97. and their size does not need to be known when a program is writ-
  98. ten.  Values are converted to expected types automatically; for
  99. example, numeral strings read in as input can be used in numeri-
  100. cal computations without explicit conversion.  Whereas SNOBOL4
  101. has a pattern-matching facility that is separate from the rest of
  102. the language, string scanning is integrated with the rest of the
  103. language facilities in Icon.  Unlike SNOBOL4, Icon has an
  104. expression-based syntax with reserved words; in appearance, Icon
  105. programs resemble those of several other conventional programming
  106. languages.
  107.  
  108.    Examples of the kinds of problems for which Icon is well
  109. suited are:
  110.  
  111.      +  text analysis, editing, and formatting
  112.  
  113.      +  document preparation
  114.  
  115.      +  symbolic mathematics
  116.  
  117.      +  text generation
  118.  
  119.      +  parsing and translation
  120.  
  121.      +  data laundry
  122.  
  123.      +  graph manipulation
  124.  
  125.    Version 7 of Icon, the most recent version, is implemented in
  126. C [2]. There are UNIX* implementations for many computers,
  127.  
  128.  
  129.  
  130.                               - 1 -
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. including the Amdahl 580, the AT&T 3B series, the HP 9000, the
  140. IBM PC/XT/AT, the IBM RT PC, the PDP-11, the Pyramid 90x, the
  141. Ridge 32, the Sun Workstation, the UNIX PC, and the VAX-11.
  142. There also are implementations for VAX/VMS, MS-DOS, the Amiga,
  143. the Atari ST, and the Macintosh.  Other implementations are in
  144. progress.
  145.  
  146.    A brief description of some of the representative features of
  147. Icon is given in the following sections. This description is not
  148. rigorous and does not include many features of Icon. See [3] for
  149. a complete description and [4] for a description of recent
  150. changes to the language.
  151.  
  152.  
  153. 2.  Strings
  154.  
  155.    Strings of characters may be arbitrarily long, limited only by
  156. the architecture of the computer on which Icon is implemented. A
  157. string may be specified literally by enclosing it in double quo-
  158. tation marks, as in
  159.  
  160.         greeting := "Hello world"
  161.  
  162. which assigns an 11-character string to greeting, and
  163.  
  164.         address := ""
  165.  
  166. which assigns the zero-length empty string to address.  The
  167. number of characters in a string s, its size, is given by *s. For
  168. example, *greeting is 11 and *address is 0.
  169.  
  170.    Icon uses the ASCII character set, extended to 256 characters.
  171. There are escape conventions, similar to those of C, for
  172. representing characters that cannot be keyboarded.
  173.  
  174.    Strings also can be read in and written out, as in
  175.  
  176.         line := read()
  177.  
  178. and
  179.  
  180.         write(line)
  181.  
  182. Strings can be constructed by concatenation, as in
  183.  
  184.         element := "(" || read() || ")"
  185.  
  186. If the concatenation of a number of strings is to be written out,
  187. the write function can be used with several arguments to avoid
  188. actual concatenation:
  189.  
  190.                           
  191. *UNIX is a trademark of AT&T Bell Laboratories.
  192.  
  193.  
  194.  
  195.  
  196.                               - 2 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.         write("(",read(),")")
  206.  
  207.  
  208.    Substrings can be formed by subscripting strings with range
  209. specifications that indicate, by position, the desired range of
  210. characters. For example,
  211.  
  212.         middle := line[10:20]
  213.  
  214. assigns to middle the string of characters of line between posi-
  215. tions 10 and 20.  Similarly,
  216.  
  217.         write(line[2])
  218.  
  219. writes the second character of line.  The value 0 is used to
  220. refer to the position after the last character of a string. Thus
  221.  
  222.         write(line[2:0])
  223.  
  224. writes the substring of line from the second character to the
  225. end, thus omitting the first character.
  226.  
  227.    An assignment can be made to the substring of string-valued
  228. variable to change its value. For example,
  229.  
  230.         line[2] := "..."
  231.  
  232. replaces the second character of line by three dots. Note that
  233. the size of line changes automatically.
  234.  
  235.    There are many functions for analyzing strings. An example is
  236.  
  237.         find(s1,s2)
  238.  
  239. which produces the position in s2 at which s1 occurs as a sub-
  240. string. For example, if the value of greeting is as given ear-
  241. lier,
  242.  
  243.         find("or",greeting)
  244.  
  245. produces the value 8.  See Section 4.2 for the handling of situa-
  246. tions in which s1 does not occur in s2, or in which it occurs at
  247. several different positions.
  248.  
  249.  
  250. 3.  Character Sets
  251.  
  252.    While strings are sequences of characters, csets are sets of
  253. characters in which membership rather than order is significant.
  254. Csets are represented literally using single enclosing quotation
  255. marks, as in
  256.  
  257.         vowels := 'aeiouAEIOU'
  258.  
  259.  
  260.  
  261.  
  262.                               - 3 -
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. Two useful built-in csets are &lcase and &ucase, which consist of
  272. the lowercase and uppercase letters, respectively.  Set opera-
  273. tions are provided for csets. For example,
  274.  
  275.         letters := &lcase ++ &ucase
  276.  
  277. forms the cset union of the lowercase and uppercase letters and
  278. assigns the resulting cset to letters, while
  279.  
  280.         consonants := letters -- 'aeiouAEIOU'
  281.  
  282. forms the cset difference of the letters and the vowels and
  283. assigns the resulting cset to consonants.
  284.  
  285.    Csets are useful in situations in which any one of a number of
  286. characters is significant. An example is the string analysis
  287. function
  288.  
  289.         upto(c,s)
  290.  
  291. which produces the position s at which any character in c occurs.
  292. For example,
  293.  
  294.         upto(vowels,greeting)
  295.  
  296. produces 2. Another string analysis function that uses csets is
  297.  
  298.         many(c,s)
  299.  
  300. which produces the position in s after an initial substring con-
  301. sisting only of characters that occur in s.  An example of the
  302. use of many is in locating words. Suppose, for example, that a
  303. word is defined to consist of a string of letters.  The expres-
  304. sion
  305.  
  306.         write(line[1:many(letters,line)])
  307.  
  308. writes a word at the beginning of line. Note the use of the posi-
  309. tion returned by a string analysis function to specify the end of
  310. a substring.
  311.  
  312.  
  313. 4.  Expression Evaluation
  314.  
  315. 4.1  Conditional Expressions
  316.  
  317.    In Icon there are conditional expressions that may succeed and
  318. produce a result, or may fail and not produce any result. An
  319. example is the comparison operation
  320.  
  321.         i > j
  322.  
  323. which succeeds (and produces the value of j) provided that the
  324. value of i is greater than the value of j, but fails otherwise.
  325.  
  326.  
  327.  
  328.                               - 4 -
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.    The success or failure of conditional operations is used
  338. instead of Boolean values to drive control structures in Icon. An
  339. example is
  340.  
  341.         if i > j then k := i else k := j
  342.  
  343. which assigns the value of i to k if the value of i is greater
  344. than the value of j, but assigns the value of j to k otherwise.
  345.  
  346.    The usefulness of the concepts of success and failure is
  347. illustrated by find(s1,s2), which fails if s1 does not occur as a
  348. substring of s2.  Thus
  349.  
  350.         if i := find("or",line) then write(i)
  351.  
  352. writes the position at which or occurs in line, if it occurs, but
  353. does not write a value if it does not occur.
  354.  
  355.    Many expressions in Icon are conditional. An example is
  356. read(), which produces the next line from the input file, but
  357. fails when the end of the file is reached. The following expres-
  358. sion is typical of programming in Icon and illustrates the
  359. integration of conditional expressions and conventional control
  360. structures:
  361.  
  362.         while line := read() do
  363.            write(line)
  364.  
  365. This expression copies the input file to the output file.
  366.  
  367.    If an argument of a function fails, the function is not
  368. called, and the function call fails as well. This ``inheritance''
  369. of failure allows the concise formulation of many programming
  370. tasks. Omitting the optional do clause in while-do, the previous
  371. expression can be rewritten as
  372.  
  373.         while write(read())
  374.  
  375.  
  376. 4.2  Generators
  377.  
  378.    In some situations, an expression may be capable of producing
  379. more than one result. Consider
  380.  
  381.         sentence := "Store it in the neighboring harbor"
  382.         find("or",sentence)
  383.  
  384. Here or occurs in sentence at positions 3, 23, and 33. Most pro-
  385. gramming languages treat this situation by selecting one of the
  386. positions, such as the first, as the result of the expression. In
  387. Icon, such an expression is a generator and is capable of produc-
  388. ing all three positions.
  389.  
  390.    The results that a generator produces depend on context. In a
  391.  
  392.  
  393.  
  394.                               - 5 -
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403. situation where only one result is needed, the first is produced,
  404. as in
  405.  
  406.         i := find("or",sentence)
  407.  
  408. which assigns the value 3 to i.
  409.  
  410.    If the result produced by a generator does not lead to the
  411. success of an enclosing expression, however, the generator is
  412. resumed to produce another value. An example is
  413.  
  414.         if (i := find("or",sentence)) > 5 then write(i)
  415.  
  416. Here the first result produced by the generator, 3, is assigned
  417. to i, but this value is not greater than 5 and the comparison
  418. operation fails. At this point, the generator is resumed and pro-
  419. duces the second position, 23, which is greater than 5. The com-
  420. parison operation then succeeds and the value 23 is written.
  421. Because of the inheritance of failure and the fact that com-
  422. parison operations return the value of their right argument, this
  423. expression can be written in the following more compact form:
  424.  
  425.         write(5 < find("or",sentence))
  426.  
  427.  
  428.    Goal-directed evaluation is inherent in the expression evalua-
  429. tion mechanism of Icon and can be used in arbitrarily complicated
  430. situations.  For example,
  431.  
  432.         find("or",sentence1) = find("and",sentence2)
  433.  
  434. succeeds if or occurs in sentence1 at the same position as and
  435. occurs in sentence2.
  436.  
  437.    A generator can be resumed repeatedly to produce all its
  438. results by using the every-do control structure. An example is
  439.  
  440.         every i := find("or",sentence)
  441.            do write(i)
  442.  
  443. which writes all the positions at which or occurs in sentence.
  444. For the example above, these are 3, 23, and 33.
  445.  
  446.    Generation is inherited like failure, and this expression can
  447. be written more concisely by omitting the optional do clause:
  448.  
  449.         every write(find("or",sentence))
  450.  
  451.  
  452.    There are several built-in generators in Icon. One of the most
  453. frequently used of these is
  454.  
  455.         i to j
  456.  
  457.  
  458.  
  459.  
  460.                               - 6 -
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469. which generates the integers from i to j. This generator can be
  470. combined with every-do to formulate the traditional for-style
  471. control structure:
  472.  
  473.         every k := i to j do
  474.            f(k)
  475.  
  476. Note that this expression can be written more compactly as
  477.  
  478.         every f(i to j)
  479.  
  480.  
  481.    There are a number of other control structures related to gen-
  482. eration.  One is alternation,
  483.  
  484.         expr1 | expr2
  485.  
  486. which generates the results of expr1 followed by the results of
  487. expr2.  Thus
  488.  
  489.         every write(find("or",sentence1) | find("or",sentence2))
  490.  
  491. writes the positions of or in sentence1 followed by the positions
  492. of or in sentence2. Again, this sentence can be written more com-
  493. pactly by using alternation in the second argument of find:
  494.  
  495.         every write(find("or",sentence1 | sentence2))
  496.  
  497.  
  498.    Another use of alternation is illustrated by
  499.  
  500.         (i | j | k) = (0 | 1)
  501.  
  502. which succeeds if any of i, j, or k has the value 0 or 1.
  503.  
  504.  
  505. 5.  String Scanning
  506.  
  507.    The string analysis and synthesis operations described in Sec-
  508. tions 2 and 3 work best for relatively simple operations on
  509. strings.  For complicated operations, the bookkeeping involved in
  510. keeping track of positions in strings becomes burdensome and
  511. error prone.  In such cases, Icon has a string scanning facility
  512. that is analogous in many respects to pattern matching in SNO-
  513. BOL4. In string scanning, positions are managed automatically and
  514. attention is focused on a current position in a string as it is
  515. examined by a sequence of operations.
  516.  
  517.    The string scanning operation has the form
  518.  
  519.         s ? expr
  520.  
  521. where s is the subject string to be examined and expr is an
  522. expression that performs the examination.  A position in the
  523.  
  524.  
  525.  
  526.                               - 7 -
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535. subject, which starts at 1, is the focus of examination.
  536.  
  537.    Matching functions change this position.  One matching func-
  538. tion, move(i), moves the position by i and produces the substring
  539. of the subject between the previous and new positions. If the
  540. position cannot be moved by the specified amount (because the
  541. subject is not long enough), move(i) fails. A simple example is
  542.  
  543.         line ? while write(move(2))
  544.  
  545. which writes successive two-character substrings of line, stop-
  546. ping when there are no more characters.
  547.  
  548.    Another matching function is tab(i), which sets the position
  549. in the subject to i and also returns the substring of the subject
  550. between the previous and new positions.  For example,
  551.  
  552.         line ? if tab(10) then write(tab(0))
  553.  
  554. first sets the position in the subject to 10 and then to the end
  555. of the subject, writing line[10:0].  Note that no value is writ-
  556. ten if the subject is not long enough.
  557.  
  558.    String analysis functions such as find can be used in string
  559. scanning. In this context, the string that they operate on is not
  560. specified and is taken to be the subject. For example,
  561.  
  562.         line ? while write(tab(find("or")))
  563.            do move(2)
  564.  
  565. writes all the substrings of line prior to occurrences of or.
  566. Note that find produces a position, which is then used by tab to
  567. change the position and produce the desired substring. The
  568. move(2) skips the or that is found.
  569.  
  570.    Another example of the use of string analysis functions in
  571. scanning is
  572.  
  573.         line ? while tab(upto(letters)) do
  574.            write(tab(many(letters)))
  575.  
  576. which writes all the words in line.
  577.  
  578.    As illustrated in the examples above, any expression may occur
  579. in the scanning expression. Unlike SNOBOL4, in which the opera-
  580. tions that are allowed in pattern matching are limited and
  581. idiosyncratic, string scanning is completely integrated with the
  582. rest of the operation repertoire of Icon.
  583.  
  584.  
  585. 6.  Structures
  586.  
  587.    Icon supports several kinds of structures with different
  588. organizations and access methods. Lists are linear structures
  589.  
  590.  
  591.  
  592.                               - 8 -
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601. that can be accessed both by position and by stack and queue
  602. functions. Sets are collections of arbitrary values with no
  603. implied ordering. Tables provide an associative lookup mechanism.
  604.  
  605. 6.1  Lists
  606.  
  607.    While strings are sequences of characters, lists in Icon are
  608. sequences of values of arbitrary types. Lists are created by
  609. enclosing the lists of values in brackets. An example is
  610.  
  611.         car1 := ["buick","skylark",1978,2450]
  612.  
  613. in which the list car1 has four values, two of which are strings
  614. and two of which are integers. Note that the values in a list
  615. need not all be of the same type. In fact, any kind of value can
  616. occur in a list - even another list, as in
  617.  
  618.         inventory := [car1,car2,car3,car4]
  619.  
  620.  
  621.    Lists also can be created by
  622.  
  623.         a := list(i,x)
  624.  
  625. which creates a list of i values, each of which has the value x.
  626.  
  627.    The values in a list can be referenced by position much like
  628. the characters in a string. Thus
  629.  
  630.         car1[4] := 2400
  631.  
  632. changes the last value in car1 to 2400.  A reference that is out
  633. of the range of the list fails. For example,
  634.  
  635.         write(car1[5])
  636.  
  637. fails.
  638.  
  639.    The values in a list a are generated by !a. Thus
  640.  
  641.         every write(!a)
  642.  
  643. writes all the values in a.
  644.  
  645.    Lists can be manipulated like stacks and queues. The function
  646. push(a,x) adds the value of x to the left end of the list a,
  647. automatically increasing the size of a by one. Similarly, pop(a)
  648. removes the leftmost value from a, automatically decreasing the
  649. size of a by one, and produces the removed value.
  650.  
  651.    A list value in Icon is a pointer (reference) to a structure.
  652. Assignment of a structure in Icon does not copy the structure
  653. itself but only the pointer to it. Thus the result of
  654.  
  655.  
  656.  
  657.  
  658.                               - 9 -
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.  
  667.         demo := car1
  668.  
  669. causes demo and car1 to reference the same list. Graphs with
  670. loops can be constructed in this way. For example,
  671.  
  672.         node1 := ["a"]
  673.         node2 := [node1,"b"]
  674.         push(node1,node2)
  675.  
  676. constructs a structure that can be pictured as follows:
  677.  
  678.  
  679.  
  680.         node1   +-> a --+
  681.                 |       |
  682.                 |       |
  683.         node2   +-- b <-+
  684.  
  685.  
  686.  
  687.  
  688.  
  689. 6.2  Sets
  690.  
  691.    Sets are collections of values. A set is obtained from a list
  692. by set(a), where a contains the members of the set. For example,
  693.  
  694.         s := set([1,"abc",[]])
  695.  
  696. assigns to s a set that contains the integer 1, the string "abc",
  697. and an empty list.
  698.  
  699.    The set operations of union, intersection, and difference are
  700. provided.  The function member(s,x) succeeds if x is a member of
  701. the set s but fails otherwise. The function insert(s,x) adds x to
  702. the set s, while delete(s,x) removes x from s. A value only can
  703. occur once in a set, so insert(s,x) has no effect if x is already
  704. in s.
  705.  
  706.    The operation *s produces the number of members in s and !s
  707. generates the members of s.
  708.  
  709.    A simple example of the use of sets is given by the following
  710. segment of code, which lists all the different words that appear
  711. in the input file:
  712.  
  713.         words := set()
  714.         while line := read() do
  715.            line ? while tab(upto(letters)) do
  716.               insert(words,tab(many(letters)))
  717.         every write(!words)
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.                              - 10 -
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734. 6.3  Tables
  735.  
  736.    Icon has a table data type similar to that of SNOBOL4. Tables
  737. essentially are sets of pairs of values, an entry value and a
  738. corresponding assigned value. The entry and assigned values may
  739. be of any type, and the assigned value for any entry value can be
  740. looked up automatically.  Thus tables provide a form of associa-
  741. tive access in contrast with the positional access to values in
  742. lists.
  743.  
  744.    A table is created by an expression such as
  745.  
  746.         symbols := table(x)
  747.  
  748. which assigns to symbols a table with the default assigned value
  749. x.  Subsequently, symbols can be referenced by any entry value,
  750. such as
  751.  
  752.         symbols["there"] := 1
  753.  
  754. which assigns the value 1 to the thereth entry in symbols.
  755.  
  756.    Tables grow automatically as new entry values are added.  For
  757. example, the following program segment produces a table contain-
  758. ing a count of the words that appear in the input file:
  759.  
  760.         words := table(0)
  761.         while line := read() do
  762.            line ? while tab(upto(letters)) do
  763.               words[tab(many(letters))] +:= 1
  764.  
  765. Here the default assigned value for each word is 0, as given in
  766. table(0), and +:= is an augmented assignment operation that
  767. increments the assigned values by one.  There are augmented
  768. assignment operations for all binary operators.
  769.  
  770.    A list can be obtained from a table by the function sort(t,1).
  771. The form of the list depends on the value of i. For example, if i
  772. is 3, the list contains alternate entry and assigned values of t.
  773. For example,
  774.  
  775.         wordlist := sort(words,3)
  776.         while write(pop(wordlist)," : ",pop(wordlist))
  777.  
  778. writes the words and their counts from words.
  779.  
  780.  
  781. 7.  Procedures
  782.  
  783.    An Icon program consists of a sequence of procedure declara-
  784. tions.  An example of a procedure declaration is
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.                              - 11 -
  792.  
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.         procedure max(i,j)
  801.            if i > j then return i else return j
  802.         end
  803.  
  804. where the name of the procedure is max and its formal parameters
  805. are i and j. The return expressions return the value of i or j,
  806. whichever is larger.
  807.  
  808.    Procedures are called like built-in functions. Thus
  809.  
  810.         k := max(*s1,*s2)
  811.  
  812. assigns to k the size of the longer of the strings s1 and s2.
  813.  
  814.    A procedure also may suspend instead of returning. In this
  815. case, a result is produced as in the case of a return, but the
  816. procedure can be resumed to produce other results. An example is
  817. the following procedure that generates the words in the input
  818. file.
  819.  
  820.         procedure genword()
  821.            local line, letters, words
  822.            letters := &lcase ++ &ucase
  823.            while line := read() do
  824.               line ? while tab(upto(letters)) do {
  825.                  word := tab(many(letters))
  826.                  suspend word
  827.                  }
  828.         end
  829.  
  830. The braces enclose a compound expression.
  831.  
  832.    Such a generator is used in the same way that a built-in gen-
  833. erator is used. For example
  834.  
  835.         every word := genword() do
  836.            if find("or",word) then write(word)
  837.  
  838. writes only those words that contain the substring or.
  839.  
  840.  
  841. 8.  An Example
  842.  
  843.    The following program sorts graphs topologically.
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.                              - 12 -
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.         procedure main()
  867.            local sorted, nodes, arcs, roots
  868.            while nodes := read() do {      # get next node list
  869.               arcs := read()               # get arc list
  870.               sorted := ""                 # sorted nodes
  871.                                            # get nodes without predecessors
  872.               while *(roots := nodes -- snodes(arcs)) > 0 do {
  873.                  sorted ||:= roots         # add to sorted nodes
  874.                  nodes --:= roots          # delete these nodes
  875.                  arcs := delarcs(arcs,roots)# delete their arcs
  876.                  }
  877.               if *arcs = 0 then write(sorted)# successfully sorted
  878.               else write("graph has cycle")# cycle if node remains
  879.            }
  880.         end
  881.  
  882.  
  883.         procedure snodes(arcs)
  884.            local nodes
  885.            nodes := ""
  886.            arcs ? while move(1) do {       # predecessor
  887.               move(2)                      # skip "->"
  888.               nodes ||:= move(1)           # successor
  889.               move(1)                      # skip ";"
  890.               }
  891.            return nodes
  892.         end
  893.  
  894.  
  895.         procedure delarcs(arcs,roots)
  896.            local newarcs, node
  897.            newarcs := ""
  898.            arcs ? while node := move(1) do {# get predecessor node
  899.               if many(roots,node) then move(4)# delete arc from root node
  900.               else newarcs ||:= node || move(4)# else keep arc
  901.               }
  902.            return newarcs
  903.         end
  904.  
  905. Graph nodes are represented by single characters with a list of
  906. the nodes on one input line followed by a list of arcs. For exam-
  907. ple, the graph
  908.  
  909.                +---------------+
  910.                |               |
  911.                |               v
  912.                a ----> b ----> c
  913.                ^       |       ^
  914.                |       |       |       
  915.                |       v       |
  916.                d ----> e ------+
  917.  
  918.                
  919.    
  920.  
  921.  
  922.  
  923.  
  924.                                                                                                                                                                                                                                           D'l 0 -50u'D'l -10u 30u'
  925.                                                                                                                                                                                                                                                                   D'l 10u 30u'
  926.                                                                                                                                                                                                                                                                               D'l 0 200u'
  927.                                                                                                                                                                                                                                                                                                            D'l 0 50u'D'l -10u -30u'
  928.                                                                                                                                                                                                                                                                                                                                    D'l 10u -30u'
  929.                                                                                                                                                                                                                                                                                                                                                 D'l 0 -190u'
  930.  
  931. is given as
  932.  
  933.  
  934.  
  935.  
  936.  
  937.                              - 13 -
  938.  
  939.  
  940.  
  941.  
  942.  
  943.  
  944.  
  945.  
  946.         abcde
  947.         a->b;a->c;b->c;b->e;d->a;d->e;e->c;
  948.  
  949. for which the output is
  950.  
  951.         dabec
  952.  
  953.  
  954.    The nodes are represented by csets and automatic type conver-
  955. sion is used to convert strings to csets and vice versa.  Note
  956. the use of augmented assignment operations for concatenation and
  957. in the computation of cset differences.
  958.  
  959. Acknowledgement
  960.  
  961.    Icon was designed by the the author in collaboration with Dave
  962. Hanson, Tim Korb, Cary Coutant, and Steve Wampler. The current
  963. implementation is largely the work of Cary Coutant and Steve
  964. Wampler with recent contributions by Bill Mitchell and Janalee
  965. O'Bagy.  Dave Hanson and Bill Mitchell made several helpful
  966. suggestions on the presentation of material in this paper.
  967.  
  968. References
  969.  
  970.  
  971. 1. Griswold, Ralph E., James F. Poage, and Ivan P. Polonsky.  The
  972.    SNOBOL4 Programming Language, second edition.  Prentice-Hall,
  973.    Inc., Englewood Cliffs, New Jersey. 1971.
  974.  
  975. 2. Griswold, Ralph E. and Madge T. Griswold. The Implementation
  976.    of the Icon Programming Language. Princeton University Press,
  977.    Princeton, New Jersey. 1986.
  978.  
  979. 3. Griswold, Ralph E. and Madge T. Griswold. The Icon Programming
  980.    Language. Prentice-Hall, Inc., Englewood Cliffs, New Jersey.
  981.    1983.
  982.  
  983. 4. Griswold, Ralph E. and Kenneth Walker.  Version 7 of Icon,
  984.    Technical Report TR 88-5, Department of Computer Science, The
  985.    University of Arizona. 1988.
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003.                              - 14 -
  1004.  
  1005.  
  1006.