home *** CD-ROM | disk | FTP | other *** search
/ Between Heaven & Hell 2 / BetweenHeavenHell.cdr / 100 / 88 / exten.doc < prev    next >
Text File  |  1985-12-04  |  13KB  |  595 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                Extensions to Version 5 of the Icon
  8.                       Programming Language
  9.  
  10.  
  11.  
  12.  
  13. 1.  Introduction
  14.  
  15.    The standard features of Version 5 of Icon are described in
  16. Reference 1. Since Icon is the byproduct of a research effort
  17. that is concerned with the development of novel programming
  18. language facilities for processing nonnumeric data, it is inevit-
  19. able that some extensions to the standard language will develop.
  20.  
  21.    Some of these extensions are incorporated as features of new
  22. releases.  Others are available as options that can be selected
  23. when the Icon system is installed. This report describes the
  24. extensions that are included in Version 5.9 of Icon.
  25.  
  26.    All the extensions are upward-compatible with standard Version
  27. 5 Icon.  Their inclusion should not interfere with any program
  28. that works properly under the standard version.
  29.  
  30.  
  31. 2.  New Version 5.9 Features
  32.  
  33. 2.1  The Link Directive
  34.  
  35.    Version 5.9 contains a link directive that simplifies the
  36. inclusion of separately translated libraries of Icon procedures.
  37. If icont is run with the -c option, source files are translated
  38. into intermediate ucode files (with names ending in .u1 and .u2).
  39. For example,
  40.  
  41.         icont -c libe.icn
  42.  
  43. produces the ucode files libe.u1 and libe.u2. The ucode files can
  44. be incorporated in another program with the new link directive,
  45. which has the form
  46.  
  47.         link libe
  48.  
  49. The argument of link is, in general, a list of identifiers or
  50. string literals that specify the names of files to be linked
  51. (without the .u1 or .u2). Thus, when running under UNIX*,
  52.  
  53.         link libe, "/usr/icon/ilib/collate"
  54.  
  55. specifies the linking of libe in the current directory and col-
  56. late in /usr/icon/ilib. Syntax appropriate to VMS should be used
  57. when running under that system.
  58.                           
  59. *UNIX is a trademark of AT&T Bell Laboratories.
  60.  
  61.  
  62.  
  63.  
  64.                              - 1 -
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.    The environment variable IPATH controls the location of files
  74. specified in link directives. IPATH should be have a value of the
  75. form p1:p2: ... pn where each pi names a directory.  Each direc-
  76. tory is searched in turn to locate files named in link direc-
  77. tives. The default value of IPATH is ".", that is, the current
  78. directory.
  79.  
  80. 2.2  Installation Options
  81.  
  82.    When an Icon system is installed, various configuration
  83. options are specified [2]. The value of the keyword &options is a
  84. string that contains the command line arguments that were used to
  85. configure Icon.
  86.  
  87.  
  88. 3.  Optional Extensions
  89.  
  90.    There are two extension options: sets (-sets in &options), and
  91. a collection of experimental features (-xpx in &options).
  92.  
  93. 3.1  Sets
  94.  
  95.    Sets are unordered collections of values and have the proper-
  96. ties normally associated with sets in the mathematical sense.
  97. The function
  98.  
  99.         set(a)
  100.  
  101. creates a set that contains the distinct elements of the list a.
  102. For example,
  103.  
  104.         set(["abc",3])
  105.  
  106. creates a set with two members, abc and 3.  Note that
  107.  
  108.         set([])
  109.  
  110. creates an empty set.  Sets, like other data aggregates in Icon,
  111. need not be homogeneous -- a set may contain members of different
  112. types.
  113.  
  114.    Sets, like other Icon data aggregates, are represented by
  115. pointers to the actual data. Sets can be members of sets, as in
  116.  
  117.         s1 := set([1,2,3])
  118.         s2 := set([s1,[]])
  119.  
  120. in which s2 contains two members, one of which is a set of three
  121. members and the other of which is an empty list.
  122.  
  123.    Any specific value can occur only once in a set. For example,
  124.  
  125.         set([1,2,3,3,1])
  126.  
  127.  
  128.  
  129.  
  130.                              - 2 -
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. creates a set with the three members 1, 2, and 3.  Set membership
  140. is determined the same way the equivalence of values is deter-
  141. mined in the operation
  142.  
  143.         x === y
  144.  
  145. For example,
  146.  
  147.         set([[],[]])
  148.  
  149. creates a set that contains two distinct empty lists.
  150.  
  151.    The functions and operations of Icon that apply to other data
  152. aggregates apply to sets as well. For example, if s is a set,
  153.  
  154.         *s
  155.  
  156. is the size of s (the number of members in it). Similarly,
  157.  
  158.         type(s)
  159.  
  160. produces the string set and
  161.  
  162.         s := set(["abc",3])
  163.         write(image(s))
  164.  
  165. writes set(2). Note that the string images of sets are in the
  166. same style as for other aggregates, with the size enclosed in
  167. parentheses.
  168.  
  169.    The operation
  170.  
  171.         !s
  172.  
  173. generates the members of s, but in no predictable order. Simi-
  174. larly,
  175.  
  176.         ?s
  177.  
  178. produces a randomly selected member of s.  These operations pro-
  179. duce values, not variables -- it is not possible to assign a
  180. value to !s or ?s.
  181.  
  182.    The function
  183.  
  184.         copy(s)
  185.  
  186. produces a new set, distinct from s, but which contains the same
  187. members as s. The copy is made in the same fashion as the copy of
  188. a list -- the members themselves are not copied.
  189.  
  190.    The function
  191.  
  192.  
  193.  
  194.  
  195.  
  196.                              - 3 -
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.         sort(s)
  206.  
  207. produces a list containing the members of s in sorted order.
  208. Sets themselves occur after tables but before records in the
  209. sorting order.
  210.  
  211.    The customary set operations are provided. The function
  212.  
  213.         member(s,x)
  214.  
  215. succeeds and returns the value of x if x is a member of s, but
  216. fails otherwise. Note that
  217.  
  218.         member(s1,member(s2,x))
  219.  
  220. succeeds if x is a member of both s1 and s2.
  221.  
  222.    The function
  223.  
  224.         insert(s,x)
  225.  
  226. inserts x into the set s and returns the value of s (it is simi-
  227. lar to put(a,x) in form). Note that
  228.  
  229.         insert(s,s)
  230.  
  231. adds s as an member of itself.
  232.  
  233.    The function
  234.  
  235.         delete(s,x)
  236.  
  237. deletes the member x from the set s and returns the value of s.
  238.  
  239.    The functions insert(s,x) and delete(s,x) always succeed,
  240. whether or not x is in s. This allows their use in loops in which
  241. failure may occur for other reasons. For example,
  242.  
  243.         s := set([])
  244.         while insert(s,read())
  245.  
  246. builds a set that consists of the (distinct) lines from the stan-
  247. dard input file.
  248.  
  249.    The operations
  250.  
  251.         s1 ++ s2
  252.         s1 ** s2
  253.         s1 -- s2
  254.  
  255. create the union, intersection, and difference of s1 and s2,
  256. respectively. In each case, the result is a new set.
  257.  
  258.    The use of these operations on csets is unchanged. There is no
  259.  
  260.  
  261.  
  262.                              - 4 -
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. automatic type conversion between csets and sets; the result of
  272. the operation depends on the types of the arguments. For example,
  273.  
  274.         'aeiou' ++ 'abcde'
  275.  
  276. produces the cset abcdeiou, while
  277.  
  278.         set([1,2,3]) ++ set([2,3,4])
  279.  
  280. produces a set that contains 1, 2, 3, and 4. On the other hand,
  281.  
  282.         set([1,2,3]) ++ 4
  283.  
  284. results in Run-time Error 119 (set expected).
  285.  
  286. Examples
  287.  
  288. Word Counting:
  289.  
  290.    The following program lists, in alphabetical order, all the
  291. different words that occur in the standard input file:
  292.  
  293.         procedure main()
  294.            letter := &lcase ++ &ucase
  295.            words := set([])
  296.            while text := read() do
  297.               text ? while tab(upto(letter)) do
  298.                  insert(words,tab(many(letter)))
  299.            every write(!sort(words))
  300.         end
  301.  
  302.  
  303. The Sieve of Eratosthenes:
  304.  
  305.    The follow program produces prime numbers, using the classical
  306. "Sieve of Eratosthenes":
  307.  
  308.         procedure main(a)
  309.            local limit, s, i
  310.            limit := a[1] | 5000            # limit to 5000 if not specified
  311.            s := set([])
  312.            every insert(s,1 to limit)
  313.            every member(s,i := 2 to limit) do
  314.               every delete(s,i + i to limit by i)
  315.            primes := sort(s)
  316.            write("There are ",*primes," primes in the first ",limit," integers.")
  317.            write("The primes are:")
  318.            every write(right(!primes,*limit + 1))
  319.         end
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.                              - 5 -
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337. 4.  Expermental Features
  338.  
  339. 4.1  PDCO Invocation Syntax
  340.  
  341.    The experimental features include the procedure invocation
  342. syntax that is used for programmer-defined control operations
  343. [3].  In this syntax, when braces are used in place of
  344. parentheses to enclose an argument list, the arguments are passed
  345. as a list of co-expressions. That is,
  346.  
  347.         p{expr1, expr2, ..., exprn}
  348.  
  349. is equivalent to
  350.  
  351.         p([create expr1, create expr2, ..., create exprn])
  352.  
  353. Note that
  354.  
  355.         p{}
  356.  
  357. is equivalent to
  358.  
  359.         p([])
  360.  
  361.  
  362. 4.2  Invocation Via String Name
  363.  
  364.    The experimental features allow a string-valued expression
  365. that corresponds to the name of a procedure or operation to be
  366. used in place of the procedure or operation in an invocation
  367. expression. For example,
  368.  
  369.         "image"(x)
  370.  
  371. produces the same call as
  372.  
  373.         image(x)
  374.  
  375. and
  376.  
  377.         "-"(i,j)
  378.  
  379. is equivalent to
  380.  
  381.         i - j
  382.  
  383.  
  384.    In the case of operations, the number of arguments determines
  385. the operation. Thus
  386.  
  387.         "-"(i)
  388.  
  389. is equivalent to
  390.  
  391.  
  392.  
  393.  
  394.                              - 6 -
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.         -i
  404.  
  405. Since to-by is an operation, despite its reserved-word syntax, it
  406. is included in this facility with the string name ... .  Thus
  407.  
  408.         "..."(1,10,2)
  409.  
  410. is equivalent to
  411.  
  412.         1 to 10 by 2
  413.  
  414. Similarly, range specifications are represented by ":", so that
  415.  
  416.         ":"(s,i,j)
  417.  
  418. is equivalent to
  419.  
  420.         s[i:j]
  421.  
  422.  
  423.    Defaults are not provided for omitted or null-valued arguments
  424. in this facility. Consequently,
  425.  
  426.         "..."(1,10)
  427.  
  428. results in a run-time error when it is evaluated.
  429.  
  430.    The subscripting operation also is available with the string
  431. name []. Thus
  432.  
  433.         "[]"(&lcase,3)
  434.  
  435. produces c.
  436.  
  437.    String names are available for the operations in Icon, but not
  438. for control structures. Thus
  439.  
  440.         "|"(expr1,expr2)
  441.  
  442. is erroneous.  Note that string scanning is a control structure.
  443. In addition, conjunction is not available via string invocation,
  444. since no operation is actually performed.
  445.  
  446.    Field references, of the form
  447.  
  448.         expr . fieldname
  449.  
  450. are not operations in the ordinary sense and are not available
  451. via string invocation.
  452.  
  453.    String names for procedures are available through global iden-
  454. tifiers.  Note that the names of functions, such as image, are
  455. global identifiers. Similarly, any procedure-valued global iden-
  456. tifier may be used as the string name of a procedure. Thus in
  457.  
  458.  
  459.  
  460.                              - 7 -
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.         global q
  470.  
  471.         procedure main()
  472.            q := p
  473.            "q"("hi")
  474.         end
  475.  
  476.         procedure p(s)
  477.            write(s)
  478.         end
  479.  
  480. the procedure p is invoked via the global identifier q.
  481.  
  482. 4.3  Conversion to Procedure
  483.  
  484.    The experimental features include the function proc(x,i),
  485. which converts x to a procedure, if possible.  If x is
  486. procedure-valued, its value is returned unchanged. If the value
  487. of x is a string that corresponds to the name of a procedure as
  488. described in the preceding section, the corresponding procedure
  489. value is returned.  The value of i is used to distinguish between
  490. unary and binary operators.  For example, proc("^",2) produces
  491. the exponentiation operator, while proc("^",1) produces the co-
  492. expression refresh operator.  If x cannot be converted to a pro-
  493. cedure, proc(x,i) fails.
  494.  
  495. 4.4  Integer Sequences
  496.  
  497.    To facilitate the generation of integer sequences that have no
  498. limit, the experimental features include the function seq(i,j).
  499. This function has the result sequence {i, i+j, i+2j, ... }. Omit-
  500. ted or null values for i and j default to 1. Thus the result
  501. sequence for seq() is {1, 2, 3, ... }.
  502.  
  503. Acknowledgements
  504.  
  505.    Rob McConeghy and Bill Mitchell made major contributions to
  506. the design and implementation of Version 5.9.
  507.  
  508.  
  509.    The design of sets for Icon was done as part of a class pro-
  510. ject.  The following persons participated in the design:  John
  511. Bolding, Owen Fonorow, Roger Hayes, Tom Hicks, Robert Kohout,
  512. Mark Langley, Rob McConeghy, Susan Moore, Maylee Noah, Janalee
  513. O'Bagy, Gregg Townsend, and Alan Wendt.
  514.  
  515. References
  516.  
  517. 1.  Griswold, Ralph E. and Madge T. Griswold. The Icon Program-
  518. ming Language, Prentice-Hall, Inc., Englewood Cliffs, New Jersey.
  519. 1983.
  520.  
  521.  
  522. 2.  Griswold, Ralph E. The Translation and Execution of Icon
  523.  
  524.  
  525.  
  526.                              - 8 -
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535. Programs under MS-DOS, Technical report, Department of Computer
  536. Science, The University of Arizona.  September 1985.
  537.  
  538. 3.  Griswold, Ralph E. and Michael Novak. "Programmer-Defined
  539. Control Operations", The Computer Journal, Vol. 26, No. 2 (May
  540. 1983).  pp. 175-183.
  541.  
  542.  
  543.  
  544.  
  545. Ralph E. Griswold
  546. Department of Computer Science
  547. The University of Arizona
  548.  
  549. September 30, 1985
  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.                              - 9 -
  593.  
  594.  
  595.