pm.txt 10 KB

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