ICFP Contest 2006
Team: Abstraction Anonymous

arith.adv

Download

  1. {
  2.  
  3. O'Cult Unary Arithmetic Tests
  4. Daniel Lyons - Abstraction Anonymous
  5. <fusion AT storytotell dot org>
  6.  
  7. O'Cult is a cool language. Unfortunately, it has an annoying "balance" rule,
  8. apparently a common thread of thinking in the Cult of the Bound Variable.
  9. In O'Cult it means that if the current rule doesn't apply to the current
  10. term, but applies equally many times to the left and right subterms, then
  11. the rule *does not* apply and the next rule is considered.
  12.  
  13. This broken forces us to create extraneous rules for the sake of unbalancing
  14. the term such that the next pass will find rules that apply different numbers
  15. of times on the left and the right subterms. This wasn't particularly hard
  16. for math, which has identity functions and properties which you can use to
  17. handle symmetric expressions. In the XML file, on the other hand, I wasn't
  18. able to discover how to deal with those cases in a general way.
  19.  
  20. -- Daniel Lyons, 2006-07-24 @ 1:34 PM.
  21.  
  22. }
  23.  
  24. { addition
  25.  
  26. these rules work when the arguments are numerals
  27. but not for all arbitrary expressions
  28. }
  29.  
  30. Add Z y => y;
  31. Add (S x) y => S (Add x y);
  32.  
  33. {
  34. This rule is obsoleted by the associative property below.
  35.  
  36. -- done -- Add (Add x y) (Add z q) => Add x (Add y (Add z q));
  37. }
  38.  
  39. {
  40. I know this is a mathematical property, but I couldn't figure out how to
  41. restate this to combine it with the similar multiplication rule below.
  42.  
  43. I fail middle school math. :(
  44. }
  45. Add (Mult Z y) (Mult z q) => Mult z q;
  46.  
  47.  
  48. Add (Mult (S Z) y) (Mult z q) => Add y (Mult z q);
  49. Add (Mult (S x) y) (Mult z q) => Add (Add y (Mult x y)) (Mult z q);
  50.  
  51. {
  52. Multiplication. We use a recursive definition that works like this:
  53.  
  54. 4 * 3 = 4 + (4 * 2) = 4 + 4 + (4 * 1) = 4 + 4 + 4
  55. }
  56. Mult Z y => Z;
  57. Mult (S Z) y => y;
  58. Mult (S x) y => Add y (Mult x y);
  59.  
  60. {
  61. This is a helpful unbalancing rule. I'm sure it could be made abstract and
  62. better.
  63. }
  64. Mult (Add (S x) y) z => Mult (S (Add x y)) z;
  65.  
  66. {
  67. This property is obsoleted by the associative property rule below.
  68. -- done -- Mult (Mult x y) (Mult z q) => Mult x (Mult y (Mult z q));
  69. }
  70.  
  71. {
  72. This is the associative property. We need it to unbalance the expression
  73. and allow computation to continue in O'Cult.
  74. }
  75. a (a x y) (a z q) => a x (a y (a z q));
  76.  
  77.  
  78. { when all other computation is done }
  79. Compute x => x;
  80.  
  81. . { end of rules }
  82.  
  83. STOP
  84.  

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