home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / pop / 177 < prev    next >
Encoding:
Internet Message Format  |  1993-01-01  |  10.7 KB

  1. From: sfk@otter.hpl.hp.com (Steve Knight)
  2. Date: Fri, 1 Jan 1993 17:00:15 GMT
  3. Subject: Re: Syntax Misdesign
  4. Message-ID: <116670040@otter.hpl.hp.com>
  5. Organization: Hewlett-Packard Laboratories, Bristol, UK.
  6. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!col.hp.com!news.dtc.hp.com!hpscit.sc.hp.com!hplextra!otter.hpl.hp.com!otter!sfk
  7. Newsgroups: comp.lang.pop
  8. References: <116670032@otter.hpl.hp.com>
  9. Lines: 255
  10.  
  11. I view this discussion as an exploration of the two syntaxs for
  12. quotation and sequence construction in Pepper and POP-11.
  13.  
  14. Aaron argues as follows:
  15. > My contention was that if the default is not to quote then more
  16. > typing errors will occur when people want to quote things. That's an
  17. > empirical claim which could well be false.
  18.  
  19. This claim isn't correct in the context of the quasi-quotation solution
  20. used in Pepper.  This is because lists and vector construction is
  21. respected by quasi-quotation in Pepper.  And there is less mechanism in
  22. the Pepper approach so it seems unlikely that there are more errors
  23. due to mislearning, too.  My expectation would be that empirical tests
  24. would show no significant difference between the two approaches in
  25. everyday programming but the Pepper approach would be superior in
  26. ease of learning and understanding of corner-cases.
  27.  
  28.  
  29. Aaron goes on to criticise the Pepper syntax as it has been 
  30. explained so far:
  31. > That won't do as it stands, because the double quote symbol " is
  32. > itself a word in Pop-11 and needs to be capable of occurring in a
  33. > list. 
  34.  
  35. Since the Pepper syntax is a quasi-quotation mechanism it 
  36. includes an escape mechanism that forces quotation of meta-symbols.  
  37. This mechanism is illustrated a little later.
  38.  
  39. Perhaps what is less obvious is that POP-11's list and vector 
  40. syntax are quasi-quote contexts that lack an escape mechanism 
  41. for the meta-symbols '{', '}', '[', ']', '%', '^', and '^^'.  This 
  42. is a great nuisance as anyone who has tried to create lists such as
  43. the following will agree
  44.     [ } % ^ the cat on the mat ^^ % { ]
  45. These get written as
  46.     [% "}", "%", "^", % the cat on the mat % "^^", "%", "{" %]
  47.  
  48.  
  49. Aaron pursues a particular example which requires a forcing-quotation
  50. mechanism for convenience:
  51. > At present [in POP-11] you can create lists like ["cat" -> x] which, if
  52. > given to the compiler (previously to popval) will assign the word
  53. > "cat" to x.
  54. >
  55. > On the new notation, you'd have to do
  56. >     [""", "cat", """, "->", "x"]
  57.  
  58. In fact this is an incorrect understanding of the extended quote syntax.
  59. Without an escape mechanism the only way to denote the quote-word would
  60. be an expression evaluating to it, such as
  61.       consword( '"' )
  62. and therefore the example is properly written as
  63.       lconstant quote = consword( '"' );
  64.       [quote, "cat", quote, "->", "x"]
  65. or more naturally
  66.       "[ ^quote cat ^quote -> x ]"
  67.  
  68. However, Pepper introduces a single escape mechanism for forcing quotation
  69. which is introduced by the '\' token.  The example becomes
  70.       "[ \" cat \" -> x ]"
  71. Although this is not as neat as the ["cat" -> x] notation it is not
  72. particularly ugly either.  Furthermore, it tackles POP-11's awkward
  73. cases without trouble either
  74.       "[ \} \% \^ the cat on the mat \^^ \% \{ ]"
  75.  
  76.  
  77. Aaron puts weight behind his example with the claim:
  78. > Given the frequency with which lists containing quoted words are
  79. > created (e.g. in macros, etc.) I think this sort of thing is still
  80. > intolerable.
  81.  
  82. I can't agree that this is a common case from my own experience.  But
  83. even should it prove to be true, Pepper's syntax provides a single,
  84. neat means of quoting all symbols inside a quasi-quotation.  POP-11
  85. has a hotch-potch of solutions that don't cover all cases.  In effect,
  86. POP-11 relies on not missing the common cases -- and when it does it
  87. is irritating.  And from my experience, I find the other meta-symbols, 
  88. particularly '[', ']', and '%' occurring more frequently than I expect,
  89. and POP-11's syntax finds them quite awkward.
  90.  
  91.  
  92. Aaron then puts forward a slightly different solution:
  93. > An alternative would be to have lists non-quoting by default, but
  94. > allow "%" to toggle quoting as it does now, except immediately
  95. > after "(" and before ")", when it would signify partial application,
  96. > as in
  97. >     maplist(% sqrt %)
  98. > So
  99. >     %cat dog mouse%
  100. > would become equivalent to
  101. >     "cat", "dog", "mouse"
  102. > Then the above example would be expressed thus
  103. >     [% "cat" -> x %]
  104. > which I think is much clearer!
  105.  
  106. The crux of this solution is to distinguish two types of quotation,
  107. quasi-quotation introduced by the '%' symbol and single-word quotation
  108. introduced by the '"' symbol.  When designing Pepper, this solution was
  109. rejected only because we couldn't find another quotation symbol that
  110. wouldn't cause problems.  The suggestion of "%" is profoundly flawed, 
  111. as Aaron immediately notes.  Since we wish to employ multi-word
  112. quotation in the context of function application, we have to delete this
  113. particular suggestion.  (Or change the syntax for partial application
  114. and dlocal bindings.)
  115.  
  116. However, the general idea is plainly viable.  In the design of Pepper, 
  117. we felt that once the escape mechanism was introduced it wasn't worth 
  118. adding a single-word quotation.  But note that this idea has the merit
  119. of allowing '"' to retain its traditional meaning.
  120.  
  121. As a closing note, Chris's example below, is an instance which argues
  122. for the need for quasi-quotation to respect list and vector syntax.
  123. >   [
  124. >      ["bill and ben and little weed"],
  125. >      ["andy and teddy and looby loo"],
  126. >      ["thomas the tank engine and friends"]
  127. >   ]
  128.  
  129. The point of this example is that to a POP-11 programmer, having to put
  130. quotes in all those places is irritating.  The solution adopted in Pepper
  131. is to allow list and vector brackets inside quotes to build lists and
  132. vectors.  This example therefore becomes ....
  133.  
  134.     "[
  135.       [ bill and ben and little weed ]
  136.       [ andy and teddy and looby loo ] 
  137.       [ thomas the tank engine and friends ]
  138.     ]"
  139.  
  140.  
  141. In summary, the design principles behind the Pepper (quasi-)quotation 
  142. syntax are
  143.     (1) To make quotation independent of list and vector syntax.
  144.     (2) To only have *one* quasi-quotation mechanism.
  145.     (3) To provide a single force-quotation mechanism (\).
  146. Our design goals were
  147.     (4) To be easy to learn for beginners and instantly learnable by
  148.         experienced POP-11 programmers.
  149.     (5) To be a complete system of quotation, by which we mean being able
  150.         to quote all words and to cleanly interact with all syntax for
  151.         data construction (lists, vectors, and strings).
  152.  
  153. Note that the design principles (1) to (3) are not supported in POP-11 so we
  154. knew we had to significantly depart from the existing syntax.  The solution
  155. adopted is outlined below.  It is plain that it satisfies the principles.
  156. We are reasonably confident that the design goals are also satisfied.
  157. Judge for yourselves.
  158.  
  159. Steve 
  160.  
  161. PS. I'm writing this without Chris Dollin's permission and, as he's
  162.     the principal designer of Pepper, I must apologise in advance for
  163.     any errors I may have committed in the following outline of
  164.     Pepper's quotation syntax.
  165.  
  166.  
  167.  
  168. Quasi-Quotation In Pepper
  169. -------------------------
  170. A quasi-quotation context is introduced (and closed) by the 
  171. symbol '"'.  All symbols other than the following meta-symbols 
  172. denote a PUSHQ of themselves.
  173.  
  174.     Meta-Symbols:  [ ] { } % ^ ^^ \
  175.  
  176. The meta-symbol '\' forces the next item to be treated as a 
  177. ordinary word whether or not it is a meta-symbol or not.  If the
  178. next item is a string it is converted to a word before being PUSHQ'd.
  179. The next item must be a word or string or a compiler error is raised.
  180.  
  181. The meta-symbol '%' flips the context to an evaluation context up
  182. to the next matching '%'. 
  183.  
  184. The meta-symbol '^' PUSH's the next word unless it is the word '('.
  185. If it is '(' then a single expression is evaluated up to the
  186. next matching ')'.
  187.  
  188. The meta-symbol '^^' is similar to '^' except that it plants a call
  189. to -explode- immediately afterwards.  
  190.  
  191. The meta-symbols '[' and '{' introduce a list and vector opening
  192. respectively.  Each must be paired with an appropriate list or
  193. vector closing symbol ']' and '}'.  The meaning of an open/close
  194. pair is to construct the list or vector from the items deposited
  195. on the stack between the pair.  
  196.  
  197.  
  198. Learning Pepper Syntax
  199. ----------------------
  200. It is very simple for experienced POP-11 programmers to use Pepper
  201. syntax.  There are only a few things to remember.  Firstly, lists
  202. and vectors don't quote their contents.  So where you might have written
  203.     [ ^x > ^y ]            in POP-11
  204. you might write
  205.     [ x, ">", y ]          in Pepper
  206.  
  207. Secondly, the '"' symbol now works like the inside of POP-11's vector
  208. syntax and can be used to put a sequence of values on the stack.  So
  209. where you might have written
  210.     f( "cat", "dog", [] )  in POP-11
  211. you might would write
  212.     f( "cat dog []" )      in Pepper.
  213. And where you would have written
  214.     { ^^x ^y ^^z }         in POP-11
  215. you might write
  216.     "{ ^^x ^y ^^z }"       in Pepper
  217.  
  218. Thirdly, the '\' symbol can be used to force the quotation of any
  219. word or string as a word.  So where you would have written
  220.     "%"                    in POP-11
  221. you will write
  222.     "\%"                   in Pepper
  223. And where you might have written
  224.     [% "[" %]              in POP-11
  225. you would write
  226.     "[ \[ ]"               in Pepper
  227.  
  228. And that's just about it.  There only thing remaining is to deal with
  229. the issue of list tails.  In POP-11, the meta-symbol '^^' has a very
  230. special role inside lists -- if it appears at the end of a list it
  231. causes list sharing (store aliasing) by making its value the tail
  232. of the newly constructed list.
  233.  
  234. In other words
  235.     [ ^^x ^y ^^z ]          in POP-11
  236. is different from
  237.     "[ ^^x ^y ^^z ]"        in Pepper
  238. since the POP-11 version forces the newly constructed list to share with
  239. the spine of -z- and the Pepper version doesn't.  
  240.  
  241. Pepper has a different syntax for denoting the tail of lists, derived
  242. from the Prolog syntax.  In Pepper, you would write
  243.     [ x1, x2, ..., xN | t ]
  244. to denote a list whose first N elements were x1 to xN and whose tail was
  245. t.  The advantage of this approach is that the notation highlights the
  246. important semantic difference between the two approaches.  
  247.  
  248. So you might write
  249.     [ ^^x ^y ^^z ]           in POP-11
  250. and
  251.     "[ ^^x ^y | ^z ]"        in Pepper
  252.  
  253. Furthermore, you would write
  254.     [ ^^x ^y % z.explode %]  in POP-11
  255. to achieve the effect of
  256.     [ ^^x ^y ^^z ]           in Pepper
  257.  
  258.  
  259. As I hope is clear, converting from one to the other is very easy indeed.
  260. The only complexity stems from the subtle treatment of '^^' in POP-11 that
  261. often isn't properly understood.  Comparing the two notations is really
  262. best done by working through a few examples.  (If you do this, please
  263. note that the proposed Pepper matcher is quite different to the POP-11 
  264. matcher.  This is because the POP-11 matcher is geared up for its own
  265. quotation syntax and is quite unsuitable for Pepper syntax.)
  266.