t-term-rewriting.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;; import lists/standard.scm logic/term-rewrite.scm
  2. (define (test name sys in out)
  3. (print `(applying ,name to ,in))
  4. (let ((res (sys in)))
  5. (if (equal? res out)
  6. (print `(success ==> ,out))
  7. (print `(failed with ,res !)))))
  8. (define trs-logic-1
  9. (compile-system
  10. '(((<-> x y) (and (-> x y) (-> y x)))
  11. ((-> x y) (or (not x) y)))))
  12. (test 'trs-logic-1 trs-logic-1 'foo
  13. 'foo)
  14. (test 'trs-logic-1 trs-logic-1 '(foo bar)
  15. '(foo bar))
  16. (test 'trs-logic-1 trs-logic-1 '(<-> p q)
  17. '(and (or (not p) q) (or (not q) p)))
  18. (newline)
  19. (define trs-arith-1
  20. (compile-system
  21. '(((+ x 0) x)
  22. ((+ 0 x) x)
  23. ((* x 1) x)
  24. ((* 1 x) x)
  25. ((+ (- x) x) 0)
  26. ((+ x (- x)) 0))))
  27. (test 'trs-arith-1 trs-arith-1 '(+ 7 (* 1 0))
  28. '7)
  29. (test 'trs-arith-1 trs-arith-1 '(* (+ 0 1) 5)
  30. '5)
  31. (newline)
  32. (define trs-logic-2
  33. (compile-system
  34. '(((and x (not x)) #f)
  35. ((and (not x) x) #f)
  36. ((or x (not x)) #t)
  37. ((or (not x) x) #t)
  38. ((and #t #t) #t)
  39. ((or #t x) #t)
  40. ((or x #t) #t)
  41. ((and #f x) #f)
  42. ((and x #f) #f)
  43. ((or #f #f) #f))))
  44. (define trs-cnf
  45. (compile-system
  46. '(((not (not x)) x)
  47. ((not (and x y)) (or (not x) (not y)))
  48. ((not (or x y)) (and (not x) (not y)))
  49. ((or x (and y z)) (and (or x y) (or x z)))
  50. ((or (and x y) z) (and (or x z) (or y z))))))
  51. (define trs-arith-2
  52. ;; this really illustrates limitations of TRS
  53. ;;
  54. ;; hacked together to try to repeat the example
  55. ;; (a + b)^2 = a^2 + 2ab + b^2
  56. ;; from
  57. ;;
  58. ;; <http://www.meta-environment.org/doc/books/extraction-transformation/term-rewriting/term-rewriting.html>
  59. (compile-system
  60. '(((^ x 2) (* x x))
  61. ((* k (+ a b)) (+ (* k a) (* k b)))
  62. ((* (+ a b) k) (+ (* a k) (* b k)))
  63. ((+ (* a b) (+ (* b a) z)) (+ (* 2 a b) z))
  64. ((+ (+ u v) w) (+ u (+ v w))))))
  65. (test 'trs-arith-2 trs-arith-2
  66. '(^ (+ a b) 2)
  67. '(+ (* a a) (+ (* 2 b a) (* b b))))
  68. (newline)
  69. (define trs-peano
  70. (compile-system
  71. '(((+ (z) x) x)
  72. ((+ (s x) y) (s (+ x y)))
  73. ((* (z) x) (z))
  74. ((* (s x) y) (+ (* x y) y)))))
  75. (define (number->peano n)
  76. (if (= n 0)
  77. '(z)
  78. `(s ,(number->peano (- n 1)))))
  79. (test 'trs-peano trs-peano `(+ ,(number->peano 10)
  80. ,(number->peano 4))
  81. (number->peano 14))
  82. (test 'trs-peano trs-peano `(* ,(number->peano 8)
  83. ,(number->peano 7))
  84. (number->peano 56))
  85. ;; (test 'trs-peano trs-peano `(* ,(number->peano 10)
  86. ;; ,(number->peano 7))
  87. ;; (number->peano 70))
  88. ;; (test 'trs-peano trs-peano `(* ,(number->peano 8)
  89. ;; ,(number->peano 10))
  90. ;; (number->peano 80))
  91. ;; (test 'trs-peano trs-peano `(* ,(number->peano 12)
  92. ;; ,(number->peano 10))
  93. ;; (number->peano 120))
  94. ;; (test 'trs-peano trs-peano `(* ,(number->peano 11)
  95. ;; ,(number->peano 13))
  96. ;; (number->peano 143))
  97. ;; (test 'trs-peano trs-peano `(* ,(number->peano 21)
  98. ;; ,(number->peano 23))
  99. ;; (number->peano 483))
  100. ;; (test 'trs-peano trs-peano `(* ,(number->peano 28)
  101. ;; ,(number->peano 27))
  102. ;; (number->peano 756))
  103. (newline)
  104. (define trs-klop
  105. ;; taken from
  106. ;;
  107. ;; http://www.informatik.uni-bremen.de/agbkb/lehre/rbs/texte/Klop-TR.pdf
  108. ;; page 17
  109. (compile-system
  110. '((($ ($ ($ (s) x) y) z)
  111. ($ ($ x z) ($ y z)))
  112. (($ ($ (k) x) y)
  113. x)
  114. (($ (i) x)
  115. x)
  116. (($ ($ ($ (b) x) y) z)
  117. ($ x ($ y z)))
  118. (($ ($ ($ (b) x) y) z)
  119. ($ x ($ y z))))))
  120. (define c
  121. '($ ($ (s)
  122. ($ ($ (b) (b)) (s)))
  123. ($ (k) (k))))
  124. (test 'trs-klop trs-klop `($ ($ ($ ,c x) y) z)
  125. '($ ($ x z) y))
  126. (newline)
  127. (define trs-free-group
  128. ;; An example of a Knuth-Bendix completion
  129. ;;
  130. ;; https://en.wikipedia.org/wiki/Word_problem_%28mathematics%29
  131. (compile-system
  132. '(((* (e) x) x)
  133. ((* (/ x) x) (e))
  134. ((* (* x y) z) (* x (* y z)))
  135. ((* (/ x) (* x y)) y)
  136. ((/ (e)) (e))
  137. ((* x (e)) x)
  138. ((/ (/ x)) x)
  139. ((* x (/ x)) (e))
  140. ((* x (* (/ x) y)) y)
  141. ((/ (* x y)) (* (/ y) (/ x))))))
  142. (test 'trs-free-group trs-free-group `(* (* (/ a) a) (* b (/ b)))
  143. '(e))
  144. (test 'trs-free-group trs-free-group
  145. ;; verify the axiom from
  146. ;; G. Higman and B. H. Neumann. Groups as groupoids with one law [1952]
  147. (let ((/ (lambda (a b) `(* ,a (/ ,b)))))
  148. (/ 'x (/ (/ (/ (/ 'x 'x) 'y) 'z) (/ (/ (/ 'x 'x) 'x) 'z))))
  149. 'y)
  150. (newline)