showrule.red 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. module showrule; % Display rules for an operator.
  2. % Author: Herbert Melenk, ZIB, Berlin. E-mail: melenk@zib.de.
  3. % Copyright (c) 1992 ZIB Berlin. All rights reserved.
  4. % Modified by: Francis J. Wright <F.J.Wright@Maths.QMW.ac.uk>
  5. % Time-stamp: <10 November 1998>
  6. % $Id: showrule.red 1.2 1998-11-10 08:33:09+00 fjw Exp $
  7. global '(!*match);
  8. fluid '(asymplis!* powlis!*);
  9. % All let-rules for an operator are collected as rule set.
  10. % Usage in algebraic mode:
  11. % e.g. SHOWRULES SIN;
  12. % The rules for exponentiation can be listed by
  13. % SHOWRULES EXPT;
  14. symbolic procedure showrules opr;
  15. begin scalar r;
  16. r := showruleskvalue opr;
  17. r:=append(r,showrulesopmtch opr);
  18. r:=append(r,showrules!*match opr);
  19. r:=append(r,showrulesdfn opr);
  20. if opr eq 'expt then
  21. <<r:=append(r,showrulespowlis!*());
  22. r:=append(r,showrulespowlis1!*());
  23. r:=append(r,showrulesasymplis!*())>>
  24. else
  25. %% FJW: Show rules for powers of opr:
  26. <<r:=append(r,showrulespowlis!*opr opr);
  27. r:=append(r,showrulespowlis1!*opr opr);
  28. r:=append(r,showrulesasymplis!*opr opr)>>;
  29. return 'list.r;
  30. end;
  31. symbolic procedure showruleskvalue opr;
  32. for each rule in get(opr,'kvalue) collect
  33. begin scalar pattern, vars, target;
  34. pattern := car rule;
  35. vars := selectletvars pattern;
  36. vars := arbvars vars;
  37. pattern := subla(vars,pattern);
  38. target := cadr rule;
  39. target := subla(vars,target);
  40. return mkrule(nil,pattern,target)
  41. end;
  42. symbolic procedure showonerule(test,pattern,target);
  43. % central routine produces one rule.
  44. begin scalar vars;
  45. vars := selectletvars pattern;
  46. vars := arbvars vars;
  47. pattern := subla(vars,pattern);
  48. test := subla(vars,test);
  49. target := subla(vars,target);
  50. test := simpletsymbolic test;
  51. if test=t then test:=nil;
  52. target := simpletsymbolic target;
  53. return mkrule(test,pattern,target)
  54. end;
  55. symbolic procedure showrulesopmtch opr;
  56. for each rule in get(opr,'opmtch) collect
  57. showonerule(cdadr rule,opr . car rule,caddr rule);
  58. symbolic procedure showrulesdfn opr;
  59. append(showrulesdfn1 opr, showrulesdfn2 opr);
  60. symbolic procedure showrulesdfn1 opr;
  61. for i:=1:5 join showrulesdfn1!*(opr,i);
  62. symbolic procedure showrulesdfn1!*(opr,n);
  63. % simple derivatives
  64. begin scalar dfn,pl,rule,pattern,target;
  65. dfn:=dfn_prop(for j:=0:n collect j);
  66. if(pl:=get(opr,dfn)) then return
  67. for j:=1:n join
  68. if (rule:=nth(pl,j)) then
  69. << pattern := car rule;
  70. pattern := {'df,opr . pattern,nth(pattern,j)};
  71. target := cdr rule;
  72. {showonerule(nil,pattern,target)}
  73. >>;
  74. end;
  75. symbolic procedure mkrule(c,a,b);
  76. <<b:=strip!~ b; c:=strip!~ c;
  77. {'replaceby,separate!~ a,if c then {'when,b,c} else b}>>;
  78. symbolic procedure strip!~ u;
  79. if null u then u else
  80. if idp u then
  81. (if eqcar(w,'!~) then intern compress cdr w else u)
  82. where w=explode2 u
  83. else if atom u then u
  84. else if car u = '!~ then strip!~ cadr u
  85. else strip!~ car u . strip!~ cdr u;
  86. symbolic procedure separate!~ u;
  87. if null u or u='!~ then u else
  88. if idp u then
  89. (if eqcar(w,'!~) then {'!~,intern compress cdr w} else u)
  90. where w=explode2 u
  91. else if atom u then u
  92. else separate!~ car u . separate!~ cdr u;
  93. symbolic procedure showrulesdfn2 opr;
  94. % collect possible rules from df
  95. for each rule in get('df,'opmtch) join
  96. if eqcar(caar rule,opr) then
  97. {showonerule(cdadr rule,'df . car rule,caddr rule)};
  98. symbolic procedure showrules!*match opr;
  99. for each rule in !*match join if smember(opr,rule) then
  100. begin scalar pattern,target,test,p1,p2;
  101. pattern := car rule;
  102. p1 := car pattern;
  103. p2 := cadr pattern;
  104. pattern := list('times,prepsq !*p2q p1,
  105. prepsq !*p2q p2);
  106. test := cdadr rule;
  107. target := caddr rule;
  108. return {showonerule(test,pattern,target)}
  109. end;
  110. symbolic procedure showrulespowlis!*();
  111. for each rule in powlis!* collect
  112. begin scalar pattern,target;
  113. pattern := list ('expt,car rule,cadr rule);
  114. target := cadddr rule;
  115. return mkrule(nil,pattern,target);
  116. end;
  117. symbolic procedure showrulespowlis1!*();
  118. for each rule in powlis1!* collect
  119. begin scalar pattern,target,test,p1,p2;
  120. pattern := car rule;
  121. p1 := car pattern;
  122. p2 := cdr pattern;
  123. pattern := list ('expt, p1, p2);
  124. test := cdadr rule;
  125. target := caddr rule;
  126. return showonerule(test,pattern,target);
  127. end;
  128. symbolic procedure showrulesasymplis!*();
  129. for each rule in asymplis!* collect
  130. mkrule(nil,{'expt,car rule,cdr rule},0);
  131. symbolic procedure showrulespowlis!*opr opr;
  132. %% FJW: Pick rules in powlis!* for operator opr:
  133. for each rule in powlis!* join
  134. if eqcar(car rule, opr) then
  135. begin scalar pattern,target;
  136. pattern := list ('expt,car rule,cadr rule);
  137. target := cadddr rule;
  138. return mkrule(nil,pattern,target) . nil
  139. end;
  140. symbolic procedure showrulespowlis1!*opr opr;
  141. %% FJW: Pick rules in powlis1!* for operator opr:
  142. for each rule in powlis1!* join
  143. if eqcar(caar rule, opr) then
  144. begin scalar pattern,target,test,p1,p2;
  145. pattern := car rule;
  146. p1 := car pattern;
  147. p2 := cdr pattern;
  148. pattern := list ('expt, p1, p2);
  149. test := cdadr rule;
  150. target := caddr rule;
  151. return showonerule(test,pattern,target) . nil
  152. end;
  153. symbolic procedure showrulesasymplis!*opr opr;
  154. %% FJW: Pick rules in asymplis!* for operator opr:
  155. for each rule in asymplis!* join
  156. if eqcar(car rule, opr) then
  157. mkrule(nil,{'expt,car rule,cdr rule},0) . nil;
  158. symbolic procedure selectletvars u;
  159. if null u then nil else
  160. if memq(u,frlis!*) then {u} else
  161. if atom u then nil else
  162. union (selectletvars car u, selectletvars cdr u);
  163. symbolic procedure simpletsymbolic u;
  164. if atom u then u else
  165. if car u eq 'quote then simpletsymbolic cadr u else
  166. if car u memq '(aeval reval revalx boolvalue!*) then
  167. if needs!-lisp!-tag cadr u
  168. then {'symbolic,simpletsymbolic cadr u}
  169. else simpletsymbolic cadr u
  170. else
  171. if car u eq 'list then simpletsymbolic cdr u else
  172. if isboolfn car u then simpletsymbolic (isboolfn car u . cdr u)
  173. else simpletsymbolic car u . simpletsymbolic cdr u;
  174. symbolic procedure needs!-lisp!-tag u;
  175. if numberp u then nil else
  176. if atom u then t else
  177. if car u memq '(aeval reval revalx boolvalue!* quote) then nil else
  178. if car u eq 'list then needs!-lisp!-tag1 cdr u
  179. else if car u eq 'cons then
  180. needs!-lisp!-tag cadr u or needs!-lisp!-tag caddr u
  181. else t;
  182. symbolic procedure needs!-lisp!-tag1 u;
  183. if null u then nil else
  184. needs!-lisp!-tag car u or needs!-lisp!-tag1 cdr u;
  185. fluid '(bool!-functions!*);
  186. bool!-functions!* :=
  187. for each x in {'equal,'greaterp,'lessp,'geq,'leq,'neq,'numberp}
  188. collect get(x,'boolfn).x;
  189. symbolic procedure isboolfn u;
  190. if idp u and (u:=assoc(u,bool!-functions!*)) then cdr u;
  191. symbolic procedure arbvars vars;
  192. for each var in vars collect
  193. var . {'!~, intern compress cddr explode var};
  194. symbolic operator showrules;
  195. endmodule;
  196. end;