pmrules.red 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. module pmrules; % Basic rules for PM pattern matcher.
  2. % Author: Kevin McIsaac.
  3. create!-package('(pmrules), '(contrib pm));
  4. % Other packages needed.
  5. load!-package 'pm;
  6. algebraic;
  7. % Define logical operators;
  8. % These routines are used so often they should be coded in LISP
  9. % for efficiency.
  10. operator ~; deflist('((!~ !~)),'unary); %precedence ~,not;
  11. infix &; deflist('((!& !&)),'unary); precedence &, and;
  12. infix |; deflist('((!| !|)),'unary); precedence |, or;
  13. flag('( & |), 'nary);
  14. flag('( & |),'symmetric);
  15. &(t) :- t; % We must have this else the fourth rule => &(t) -> &() -> 0
  16. &(0) :- 0;
  17. &(0, ??b) :- 0;
  18. &(t, ??b) ::- &(??b);
  19. &(?a,?a,??b) ::- &(?a,??b);
  20. &(?a,~?a,??b) ::- 0;
  21. |(t) :- t;
  22. |(0) :- 0;
  23. |(t,??a) :- t;
  24. |(0,??a) ::- |(??a);
  25. |(?a,?a,??b) ::- |(?a,??b);
  26. |(?a,~?a) :- t;
  27. |(?a,~?a,??b) ::- |(??b);
  28. ~(t) :- 0;
  29. ~(0) :- t;
  30. % Define SMP predicates in terms of their REDUCE equivalents.
  31. symbolic procedure simpbool u;
  32. begin scalar x;
  33. x := get(car u,'boolfn) or car u;
  34. u := for each j in cdr u collect reval j;
  35. u := apply (x, u);
  36. return (if u then !*k2f T else 0) ./ 1
  37. end;
  38. flag('(numberp fixp), 'full);
  39. put('numberp,'simpfn,'simpbool);
  40. put('fixp,'simpfn,'simpbool);
  41. operator numbp, posp, intp, natp, oddp, evnp, complexp, listp;
  42. numbp(?n _=numberp(?n)) :- t;
  43. numbp(?n/?m _=(numberp(?n)&numberp(?m))) :- t;
  44. posp(?n _=(numbp(?n)&?n > 0)) :- t;
  45. posp(?n _=(numbp(?n)&~(?n > 0))) :- 0;
  46. intp(?n _=(numbp(?n)&fixp(?n))) :- t;
  47. intp(?n _=(numbp(?n)&~ fixp(?n))) :- 0;
  48. natp(?i _=(numbp(?i)& intp(?i)&?i>0)) :-t;
  49. natp(?i _=(numbp(?i)&~(intp(?i)&?i>0))) :- 0;
  50. oddp(?x _=(numbp(?x)&intp((?x+1)/2))) :- t;
  51. oddp(?x _=(numbp(?x)&~ intp((?x+1)/2))) :- 0;
  52. evnp(?x _=(numbp(?x)&intp(?x/2))) :- t;
  53. evnp(?x _=(numbp(?x)&~ intp(?x/2))) :- 0;
  54. complexp(i) :- t;
  55. complexp(??b*i) :- t;
  56. complexp(??a + i) :- t;
  57. complexp(??a + ??b*i) :- t;
  58. listp({??x}) :- t;
  59. listp(?x) :- 'nil;
  60. %Polyp
  61. %Primep
  62. %Projp
  63. %Ratp
  64. %Contp
  65. %Fullp
  66. %Symbp
  67. endmodule;
  68. end;