pm.tex 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. \documentclass{article}
  2. \usepackage[dvipdfm]{graphicx}
  3. \usepackage[dvipdfm]{color}
  4. \usepackage[dvipdfm]{hyperref}
  5. \usepackage{a4}
  6. \setlength{\parindent}{0cm}
  7. \title{PM - A REDUCE Pattern Matcher}
  8. \author{Kevin McIsaac \\
  9. The University of Western Australia \\
  10. and \\
  11. The RAND Corporation \\
  12. kevin@wri.com}
  13. \date{}
  14. \begin{document}
  15. \maketitle
  16. PM is a general pattern matcher similar in style to those found in systems
  17. such as SMP and Mathematica, and is based on the pattern matcher described
  18. in Kevin McIsaac, \char`\"{}Pattern Matching Algebraic Identities\char`\"{},
  19. SIGSAM Bulletin, 19 (1985), 4-13. \\
  20. \ \\
  21. The following is a description of its structure. \\
  22. \ \\
  23. A template is any expression composed of literal elements (e.g. \char`\"{}5\char`\"{},
  24. \char`\"{}a\char`\"{} or \char`\"{}a+1\char`\"{}) and specially denoted pattern
  25. variables (e.g. ?a or ??b). Atoms beginning with `?' are called generic variables
  26. and match any expression. \\
  27. \ \\
  28. Atoms beginning with `??' are called multi-generic variables and match any
  29. expression or any sequence of expressions including the null or empty
  30. sequence. A sequence is an expression of the form `{[}a1, a2,...{]}'. When
  31. placed in a function argument list the brackets are removed, i.e. f({[}a,1{]})
  32. $->$ f(a,1) and f(a,{[}1,2{]},b) $->$ f(a,1,2,b). \\
  33. \ \\
  34. A template is said to match an expression if the template is literally
  35. equal to the expression or if by replacing any of the generic or
  36. multi-generic symbols occurring in the template, the template can be made
  37. to be literally equal to the expression. These replacements are called the
  38. bindings for the generic variables. A replacement is an expression of the
  39. form `exp1 $->$ exp2', which means exp1 is replaced by exp2, or `exp1 $-->$
  40. exp2', which is the same except exp2 is not simplified until after the
  41. substitution for exp1 is made. If the expression has any of the
  42. properties; associativity, commutativity, or an identity element, they are
  43. used to determine if the expressions match. If an attempt to match the
  44. template to the expression fails the matcher backtracks, unbinding generic
  45. variables, until it reached a place were it can make a different choice.
  46. It then proceeds along the new branch. \\
  47. \ \\
  48. The current matcher proceeds from left to right in a depth first search of
  49. the template expression tree. Rearrangements of the expression are
  50. generated when the match fails and the matcher backtracks. \\
  51. \ \\
  52. The matcher also supports semantic matching. Briefly, if a subtemplate
  53. does not match the corresponding subexpression because they have different
  54. structures then the two are equated and the matcher continues matching the
  55. rest of the expression until all the generic variables in the subexpression
  56. are bound. The equality is then checked. This is controlled by the switch
  57. `semantic'. By default it is on. \\
  58. \pagebreak
  59. {\tt M($exp,temp$)} \\
  60. \ \\
  61. \begin{tabular}{lp{11cm}}
  62. \hspace*{0.2cm} & The template, temp, is matched against the expression, exp. If the
  63. template is literally equal to the expression `T' is returned. If the
  64. template is literally equal to the expression after replacing the
  65. generic variables by their bindings then the set of bindings is returned
  66. as a set of replacements. Otherwise 0 (nil) is returned. \\
  67. \end{tabular} \\
  68. \ \\
  69. \ \\
  70. {\bf Examples:} \\
  71. \ \\
  72. A \char`\"{}literal\char`\"{} template
  73. m(f(a),f(a));
  74. T
  75. Not literally equal
  76. m(f(a),f(b));
  77. 0
  78. Nested operators
  79. m(f(a,h(b)),f(a,h(b)));
  80. T
  81. a \char`\"{}generic\char`\"{} template
  82. m(f(a,b),f(a,?a));
  83. \{?A$->$B\}
  84. m(f(a,b),f(?a,?b));
  85. \{?B$->$B,?A$->$A\}
  86. The Multi-Generic symbol, ??a, takes \char`\"{}rest\char`\"{} of arguments
  87. m(f(a,b),f(??a));
  88. \{??A$->${[}A,B{]}\}
  89. but the Generic symbol, ?a, does not
  90. m(f(a,b),f(?a));
  91. 0
  92. Flag h as associative
  93. flag('(h),'assoc);
  94. Associativity is used to \char`\"{}group\char`\"{} terms together
  95. m(h(a,b,d,e),h(?a,d,?b));
  96. \{?B$->$E,?A'$->$H(A,B)\}
  97. \char`\"{}plus\char`\"{} is a symmetric function
  98. m(a+b+c,c+?a+?b);
  99. \{?B$->$A,?A$->$B\}
  100. it is also associative
  101. m(a+b+c,b+?a);
  102. \{?A$->$C + A\}
  103. Note the affect of using multi-generic symbol is different
  104. m(a+b+c,b+??c);
  105. \{??C$->${[}C,A{]}\}
  106. temp \_= logical-exp \\
  107. \ \\
  108. A template may be qualified by the use of the conditional operator `\_=',
  109. such!-that. When a such!-that condition is encountered in a template it
  110. is held until all generic variables appearing in logical-exp are bound.
  111. On the binding of the last generic variable logical-exp is simplified
  112. and if the result is not `T' the condition fails and the pattern matcher
  113. backtracks. When the template has been fully parsed any remaining held
  114. such-that conditions are evaluated and compared to `T'. \\
  115. \ \\
  116. {\bf Examples:} \\
  117. \ \\
  118. m(f(a,b),f(?a,?b\_=(?a=?b)));
  119. 0
  120. m(f(a,a),f(?a,?b\_=(?a=?b)));
  121. \{?B$->$A,?A$->$A\}
  122. Note that f(?a,?b\_=(?a=?b)) is the same as f(?a,?a)
  123. S(exp,\{temp1$->$sub1,temp2$->$sub2,...\},rept, depth) \\
  124. \ \\
  125. Substitute the set of replacements into exp, resubstituting a maximum of
  126. 'rept' times and to a maximum depth 'depth'. 'Rept' and 'depth' have the
  127. default values of 1 and infinity respectively. Essentially S is a
  128. breadth first search and replace.
  129. Each template is matched against exp until a successful match occurs.
  130. Any replacements for generic variables are applied to the rhs of that
  131. replacement and exp is replaced by the rhs. The substitution process is
  132. restarted on the new expression starting with the first replacement. If
  133. none of the templates match exp then the first replacement is tried
  134. against each sub-expression of exp. If a matching template is found
  135. then the sub-expression is replaced and process continues with the next
  136. sub-expression.
  137. When all sub-expressions have been examined, if a match was found, the
  138. expression is evaluated and the process is restarted on the
  139. sub-expressions of the resulting expression, starting with the first
  140. replacement. When all sub-expressions have been examined and no match
  141. found the sub-expressions are reexamined using the next replacement.
  142. Finally when this has been done for all replacements and no match found
  143. then the process recures on each sub-expression.
  144. The process is terminated after rept replacements or when the expression
  145. no longer changes.
  146. Si(exp,\{temp1$->$sub1,temp2$->$sub2,...\}, depth)
  147. Substitute infinitely many times until expression stops changing.
  148. Short hand notation for S(exp,\{temp1$->$sub1,temp2$->$sub2,...\},Inf,
  149. depth)
  150. Sd(exp,\{temp1$->$sub1,temp2$->$sub2,...\},rept, depth)
  151. Depth first version of Substitute.\\
  152. \ \\
  153. {\bf Examples:} \\
  154. \ \\
  155. s(f(a,b),f(a,?b)$->$?b\^{}2);
  156. 2
  157. B
  158. s(a+b,a+b$->$a{*}b);
  159. B{*}A
  160. \char`\"{}associativity\char`\"{} is used to group a+b+c in to (a+b)
  161. + c
  162. s(a+b+c,a+b$->$a{*}b);
  163. B{*}A + C
  164. The next three examples use a rule set that defines the factorial function.
  165. Substitute once
  166. s(nfac(3),\{nfac(0)$->$1,nfac(?x)$->$?x{*}nfac(?x-1)\});
  167. 3{*}NFAC(2)
  168. Substitute twice
  169. s(nfac(3),\{nfac(0)$->$1,nfac(?x)$->$?x{*}nfac(?x-1)\},2);
  170. 6{*}NFAC(1)
  171. Substitute until expression stops changing
  172. si(nfac(3),\{nfac(0)$->$1,nfac(?x)$->$?x{*}nfac(?x-1)\});
  173. 6
  174. Only substitute at the top level
  175. s(a+b+f(a+b),a+b$->$a{*}b,inf,0);
  176. F(B + A) + B{*}A
  177. temp :- exp \\
  178. \ \\
  179. If during simplification of an expression, temp matches some
  180. sub-expression then that sub-expression is replaced by exp. If there is
  181. a choice of templates to apply the least general is used.
  182. If a old rule exists with the same template then the old rule is
  183. replaced by the new rule. If exp is `nil' the rule is retracted.
  184. temp ::- exp
  185. Same as temp :- exp, but the lhs is not simplified until the replacement
  186. is made \\
  187. \ \\
  188. {\bf Examples:} \\
  189. \ \\
  190. Define the factorial function of a natural number as a recursive function
  191. and a termination condition. For all other values write it as a Gamma
  192. Function. Note that the order of definition is not important as the rules
  193. are reordered so that the most specific rule is tried first.
  194. Note the use of `::-' instead of `:-' to stop simplification of
  195. the LHS. Hold stops its arguments from being simplified. \\
  196. \ \\
  197. fac(?x\_=Natp(?x)) ::- ?x{*}fac(?x-1);
  198. HOLD(FAC(?X-1){*}?X)
  199. fac(0) :- 1;
  200. 1
  201. fac(?x) :- Gamma(?x+1);
  202. GAMMA(?X + 1)
  203. fac(3);
  204. 6
  205. fac(3/2);
  206. GAMMA(5/2)
  207. Arep(\{rep1,rep2,..\}) \\
  208. \ \\
  209. In future simplifications automatically apply replacements rep1,
  210. rep2...~ until the rules are retracted. In effect it replaces the
  211. operator `$->$' by `:-' in the set of replacements \{rep1, rep2,...\}.
  212. Drep(\{rep1,rep2,..\})
  213. Delete the rules rep1, rep2,... \\
  214. \ \\
  215. As we said earlier, the matcher has been constructed along the lines of the
  216. pattern matcher described in McIsaac with the addition of such-that
  217. conditions and `semantic matching' as described in Grief. To make a
  218. template efficient some consideration should be given to the structure of
  219. the template and the position of such-that statements. In general the
  220. template should be constructed to that failure to match is recognize as
  221. early as possible. The multi-generic symbol should be used when ever
  222. appropriate, particularly with symmetric functions. For further details
  223. see McIsaac. \\
  224. \ \\
  225. {\bf Examples:} \\
  226. \ \\
  227. f(?a,?a,?b) is better that f(?a,?b,?c\_=(?a=?b))
  228. ?a+??b is better than ?a+?b+?c...
  229. The template, f(?a+?b,?a,?b), matched against f(3,2,1) is
  230. matched as f(?e\_=(?e=?a+?b),?a,?b) when semantic matching is allowed. \\
  231. {\bf Switches} \\
  232. \ \\
  233. {\tt TRPM} \\
  234. Produces a trace of the rules applied during a substitution. This is
  235. useful to see how the pattern matcher works, or to understand an
  236. unexpected result. \\
  237. \ \\
  238. In general usage the following switches need not be considered. \\
  239. \ \\
  240. {\tt SEMANTIC} \\
  241. Allow semantic matches, e.g. f(?a+?b,?a,?b) will match f(3,2,1) even
  242. though the matcher works from left to right. \\
  243. \ \\
  244. {\tt SYM!-ASSOC} \\
  245. Limits the search space of symmetric associative functions when the
  246. template contains multi-generic symbols so that generic symbols will not
  247. the function. For example: m(a+b+c,?a+??b) will return \{?a $->$ a, ??b$->$
  248. {[}b,c{]}\} or \{?a $->$ b, ??b$->$ {[}a,c{]}\} or \{?a $->$ c, ??b$->$ {[}a,b{]}\}
  249. but no \{?a $->$ a+b, ??b$->$ c\} etc. No sane template should require these
  250. types of matches. However they can be made available by turning the switch off.
  251. \end{document}