ICFP Contest 2006
Team: Abstraction Anonymous

hmonk.txt

Download

  1. % cat README
  2. From: hmonk@cbv.net
  3. Newsgroups: cult.cbv.discuss
  4. Date: 26 Aug 19108 17:21:07
  5. X-Organization: Cult of the Bound Variable
  6. Subject: O'Cult Version 1.0 Available
  7.  
  8. Friends,
  9.  
  10. On my recent journey across the rivers, I was struck with a simply
  11. remarkable idea for a new way to program our Computing Device. As you
  12. all know well, it is currently difficult for a programmer to correct a
  13. mistake of one of his fellows---but no longer! Why, when programming in
  14. O'Cult, one programmer needs to have written barely more than a blank
  15. screen before others can begin debugging his code.
  16.  
  17. I start from a very simple programming language whose terms are
  18. specified as follows:
  19.  
  20. e ::= c | e e | (e)
  21.  
  22. where c ranges over constants and we adopt the convention that
  23. juxtaposition associates to the left. For example,
  24.  
  25. Z
  26. (S Z)
  27. Add Z (S Z)
  28.  
  29. are all well-formed terms, and the last parses as (Add Z) (S Z).
  30.  
  31. Ordinarily, one would enrich this language with more powerful means of
  32. computation. Instead, I take a different tack: a term can be _advised_
  33. by a set of external computation _rules_.
  34.  
  35. A rule is a pair of _patterns_, where a pattern extends the language of
  36. terms with variables. The term (Add Z (S Z)) is quite inert, but if the
  37. term is advised by the following rule,
  38.  
  39. Add Z y => y;
  40.  
  41. then the program computes (S Z), as expected.
  42.  
  43. ******************
  44. Rules and Matching
  45. ******************
  46.  
  47. More formally, a rule is a pair of patterns separated by '=>' and
  48. terminated with ';'. A pattern can contain both constants, which are
  49. sequences of letters and numbers beginning with an *uppercase* letter,
  50. and variables, which are sequences of letters and numbers beginning with
  51. a *lowercase* letter. A well-formed rule is one where the variables in
  52. the right-hand side are a subset of the variables in the left-hand side.
  53.  
  54. To define how a rule acts on a term, we first define when a pattern
  55. _matches_ a term yielding a set of bindings:
  56.  
  57. (1) A constant matches only that same constant, yielding the empty set
  58. of bindings. For example,
  59.  
  60. Z matches Z yielding []
  61. S does not match Z
  62.  
  63. (2) A variable matches any term, yielding a binding to that term. For
  64. example,
  65. x matches (S Z) yielding [x = (S Z)]
  66.  
  67. (3) A juxtaposition-pattern matches a juxtaposition-term if
  68. (a) the pattern's first position matches the term's first position
  69. (b) the pattern's second position matches the term's second position
  70. (c) the bindings from the two positions _unify_: for any variable
  71. bound in both positions, the term associated with that variable
  72. is the same on both positions. That is, a variable is allowed to
  73. appear in a pattern more than once, but it must match the
  74. same term in all locations.
  75. The bindings of the juxtaposition are the union of the bindings from
  76. each position.
  77.  
  78. For example,
  79.  
  80. x y matches S Z yielding [x = S, y = Z]
  81. x x matches S S yielding [x = S]
  82. x x does not match S Z
  83.  
  84. If a rule matches a term, then _applying_ that rule to the term yields
  85. the right-hand component of the rule with the bindings from the match
  86. substituted for the variables.
  87.  
  88. *******************
  89. Sentences of Advice
  90. *******************
  91. This language would be quite boring if a programmer could only specify
  92. one rule. So, a term may be modified by a _sentence_ of advice, which
  93. is a sequence of rules terminated with the '.' character.
  94.  
  95. A program consists of a current term and a sentence of advice. Because
  96. a program is advised by multiple rules, circumstances can arise when
  97. more than one rule in the sentence matches the term. A good programming
  98. language is based on common sense above all else, and my sand-father was
  99. very fond of the following aphorism:
  100.  
  101. "Advice when most needed is least heeded."
  102. - Unknown
  103.  
  104. Therefore, there is clearly only one correct semantics for applying
  105. advice to a term:
  106.  
  107. The rules in the sentence are considered left-to-right.
  108.  
  109. (1) If the current rule matches the current term, the result is the
  110. application of that rule to the term.
  111.  
  112. (2) If the current rule does not match the term directly, it may match
  113. subterms (provided that the term is a juxtaposition). In this case,
  114. whether or not the current rule is applied is determined by:
  115.  
  116. (a) Counting the number of matches in each position of the juxtaposition.
  117. Note that counting does not proceed into subterms that themselves
  118. match the current rule.
  119.  
  120. (b) If the rule does not match in either position, it is not applied.
  121.  
  122. If the rule matches only in one position, it is recursively
  123. considered for application to that position.
  124.  
  125. If the rule matches both positions,
  126. * if one position has strictly more matches, the rule is
  127. recursively considered for application to *other* position.
  128. (The rule is least heeded in the position where it is most
  129. needed.)
  130.  
  131. * if the rule matches the same number of subterms in both
  132. positions, the rule is not applied.
  133.  
  134. When a rule is not applied, consideration proceeds to the next rule.
  135. When a rule is applied, the process repeats on the new term. This
  136. process terminates when no rules in the advice apply.
  137.  
  138. **********
  139. Conclusion
  140. **********
  141.  
  142. I am sure you can see how easy it is to program in O'Cult. Now I need
  143. your assistance. I have included a regression suite as part of the
  144. advise distribution (see the man pages for details), but I need to
  145. collect programs that pass the suite.
  146.  
  147. I think we might be able to get some good publications out of this work,
  148. but only if we can prove that it is easy to write short programs. Try
  149. passing the regression suite with as pithy advice as possible. (The size
  150. of a sentence is the sum of the sizes of the rules in it, where all
  151. variables are constants are considered to have unit size.)
  152.  
  153. I implore you to hold this idea in confidence; its clean modularization
  154. of crosscutting concerns may prove key in our strife with the Cult of
  155. the LValue.
  156.  
  157. Please let me know if you have any questions or suggestions,
  158. -Harmonious
  159. % cat advise.man
  160. advise(1) advise(1)
  161.  
  162. NAME
  163. advise <action> <advice_file> [<tests_file>]
  164.  
  165. DESCRIPTION
  166. advise runs the O'Cult program given in <advice_file>. The
  167. fashion in which the program is run is determined by the <action>
  168. argument. A set of tests is taken from <tests_file>. This is
  169. optional for actions that generate tests automatically; if
  170. included, the supplied tests are added to those generated
  171. automatically.
  172. ACTIONS
  173.  
  174. * run
  175.  
  176. Run the program against tests given in <tests_file>.
  177.  
  178. * step
  179. Run the program against tests given in <tests_file>, printing each
  180. execution step.
  181.  
  182. * arith
  183.  
  184. Run the program against automatically generated arithmetic tests.
  185. To ensure robustness, the number of tests is proportional to the
  186. size of the program.
  187.  
  188. Arithmetic expressions are represented as O'Cult terms as follows:
  189. numeral ::= Z | S numeral
  190. exp ::= numeral | Add exp exp | Mult exp exp
  191.  
  192. For example, the arithmetic expression (1 + 2) * 3 is written
  193.  
  194. Mult (Add (S Z) (S (S Z))) (S (S (S (Z)))).
  195.  
  196. The program should define addition and multiplication for arithmetic
  197. expressions. Its advice should transform the term
  198.  
  199. Compute exp
  200.  
  201. into the numeral representing the value of the arithmetic expression
  202. exp.
  203.  
  204. Sample program and test files for arithmetic are included with the
  205. advise distribution as arith.adv and arith.tests.
  206.  
  207. * xml
  208.  
  209. Run the program against XML tests obtained from the Cult Wide Web.
  210. To ensure robustness, the number of tests is proportional to the
  211. size of the program.
  212.  
  213. The Cult-Wide-Web Consortium (CW2C) has settled on the eXcessively
  214. Malleable Language (XML) as the format for CW2 pages. The abstract
  215. syntax of this language is represented as O'Cult terms as follows:
  216.  
  217. quality ::= Bold | Emph | Maj
  218. doc ::= A | B | Seq doc doc | Tag quality doc
  219.  
  220. There are two base documents, A and B. Documents can be sequenced
  221. using Seq and tagged with a quality using Tag. The possible qualities
  222. are emphasized (Emph), bold (Bold), and majuscule (Maj).
  223.  
  224. The CW2C has specified that documents are to be displayed as follows:
  225. (1) The document A is displayed as the character A; the document B
  226. is displayed as the character B.
  227. (2) A sequence of documents, Seq d1 d2, is displayed by first
  228. displaying the document d1 and then displaying the document d2.
  229. (3) A tagged document, Tag q d, is displayed by displaying the
  230. document d with the quality q. q is added to any other qualities
  231. with which d is rendered. Note that you will need a UMv19101 or
  232. later terminal to properly display characters in any subset of
  233. {emphasized, bold, majuscule}.
  234.  
  235. For example, the document
  236. (Seq (Seq A B) (Seq B (Tag Bold (Seq B A))))
  237. is displayed as the character sequence
  238. A B B *B* *A*
  239. (where, for compatibility with older UMs, I have notated bold by
  240. enclosing each bold character in * *.)
  241.  
  242. As another example, the document
  243. (Tag Bold (Tag Emph A))
  244. is displayed as the character sequence
  245. *_A_*
  246. (where, analogously, I have used the notation _ _ for emphasis).
  247.  
  248. More than one document can display as the same character sequence; for
  249. example:
  250. Seq (Seq A A) A and Seq A (Seq A A)
  251. Tag Emph (Tag Emph A) and Tag Emph A
  252. Seq (Tag Emph A) (Tag Emph A) and Tag Emph (Seq A A)
  253.  
  254. Given the granularity of our current sand, CW2 browsers are too slow
  255. to display CW2 documents unless they are in _short normal form_ (SNF).
  256. An XML document is in SNF iff
  257. (1) No piece of the document matches any of the following three patterns:
  258. Seq (Seq d1 d2) d3
  259. Tag q (Tag q d)
  260. Seq (Tag q d1) (Tag q d2)
  261.  
  262. (2) For all nested Tag expressions (Tag q1 (Tag q2 d)) in the document,
  263. q1 is less than q2, where Bold is less than Emph is less than Maj.
  264.  
  265. The program should implement this SNF transformation. Its advice
  266. should transform the term (SNF d) into a short normal form that
  267. displays as the same character sequence as d.
  268. % cat arith.adv
  269. { comments are enclosed by curly-braces;
  270. no nested comments are allowed }
  271.  
  272. { addition
  273.  
  274. these rules work when the arguments are numerals
  275. but not for all arbitrary expressions
  276.  
  277. }
  278. Add Z y => y;
  279. Add (S x) y => S (Add x y);
  280.  
  281. { define multiplication (Mult) here }
  282.  
  283.  
  284. { when all other computation is done }
  285. Compute x => x;
  286.  
  287. . { end of rules }
  288.  
  289. % cat arith.tests
  290. { tests for arithmetic }
  291.  
  292. { test addition on numerals }
  293. Compute (Add (S (S Z)) (S (S Z))) -> (S (S (S (S Z))));
  294.  
  295. { test multiplication on numerals }
  296. Compute (Mult (S (S Z)) (S (S Z))) -> (S (S (S (S Z))));
  297.  
  298. { test nested expressions }
  299. Compute (Add (S Z) (Mult (S (S Z)) (S (S (S Z))))) -> (S (S (S (S (S (S (S Z)))))));
  300. Compute (Add (Add (S (S Z)) (S Z)) (Add (S (S (S Z))) (S (S Z)))) -> (S (S (S (S (S (S (S (S Z))))))));
  301.  
  302. . { end of tests }
  303. %
  304.  

Hell is other programming languages. -- Sartran
Hell is that programming language! -- Dan
Ordinarily, one would enrich this language with more powerful means of computation. Instead I take a different tack... -- Harmonious Monk