examples.scm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;(define-module (trs examples)
  2. ; #:use-module (trs trs)
  3. ; #:export (logic-1
  4. ; arith-1
  5. ; logic-2
  6. ; cnf
  7. ; arith-2
  8. ; peano
  9. ; klop
  10. ; free-group))
  11. (library (examples)
  12. (export logic-1 arith-1 logic-2 cnf arith-2 peano klop free-group)
  13. (import (chezscheme) (trs))
  14. (define-trs logic-1
  15. (==> (<-> x y) (and (-> x y) (-> y x)))
  16. (==> (-> x y) (or (not x) y)))
  17. (define-trs arith-1
  18. (==> (+ x 0) x)
  19. (==> (+ 0 x) x)
  20. (==> (* x 1) x)
  21. (==> (* 1 x) x)
  22. (==> (+ (- x) x) 0)
  23. (==> (+ x (- x)) 0))
  24. (define-trs logic-2
  25. (==> (and x (not x)) #f)
  26. (==> (and (not x) x) #f)
  27. (==> (or x (not x)) #t)
  28. (==> (or (not x) x) #t)
  29. (==> (and #t #t) #t)
  30. (==> (or #t x) #t)
  31. (==> (or x #t) #t)
  32. (==> (and #f x) #f)
  33. (==> (and x #f) #f)
  34. (==> (or #f #f) #f))
  35. (define-trs cnf
  36. (==> (not (not x)) x)
  37. (==> (not (and x y)) (or (not x) (not y)))
  38. (==> (not (or x y)) (and (not x) (not y)))
  39. (==> (or x (and y z)) (and (or x y) (or x z)))
  40. (==> (or (and x y) z) (and (or x z) (or y z))))
  41. (define-trs arith-2
  42. ;; this really illustrates limitations of TRS
  43. ;;
  44. ;; hacked together to try to repeat the example
  45. ;; (a + b)^2 = a^2 + 2ab + b^2
  46. ;; from
  47. ;;
  48. ;; <http://www.meta-environment.org/doc/books/extraction-transformation/term-rewriting/term-rewriting.html>
  49. (==> (^ x 2) (* x x))
  50. (==> (* k (+ a b)) (+ (* k a) (* k b)))
  51. (==> (* (+ a b) k) (+ (* a k) (* b k)))
  52. (==> (+ (* a b) (+ (* b a) z)) (+ (* 2 a b) z))
  53. (==> (+ (+ u v) w) (+ u (+ v w))))
  54. (define-trs peano
  55. (==> (+ (z) x) x)
  56. (==> (+ (s x) y) (s (+ x y)))
  57. (==> (* (z) x) (z))
  58. (==> (* (s x) y) (+ (* x y) y)))
  59. (define-trs klop
  60. ;; taken from
  61. ;;
  62. ;; http://www.informatik.uni-bremen.de/agbkb/lehre/rbs/texte/Klop-TR.pdf
  63. ;; page 17
  64. (==> ($ ($ ($ (s) x) y) z)
  65. ($ ($ x z) ($ y z)))
  66. (==> ($ ($ (k) x) y)
  67. x)
  68. (==> ($ (i) x)
  69. x)
  70. (==> ($ ($ ($ (b) x) y) z)
  71. ($ x ($ y z)))
  72. (==> ($ ($ ($ (b) x) y) z)
  73. ($ x ($ y z))))
  74. (define-trs free-group
  75. ;; An example of a Knuth-Bendix completion
  76. ;;
  77. ;; https://en.wikipedia.org/wiki/Word_problem_%28mathematics%29
  78. (==> (* (e) x) x)
  79. (==> (* (/ x) x) (e))
  80. (==> (* (* x y) z) (* x (* y z)))
  81. (==> (* (/ x) (* x y)) y)
  82. (==> (/ (e)) (e))
  83. (==> (* x (e)) x)
  84. (==> (/ (/ x)) x)
  85. (==> (* x (/ x)) (e))
  86. (==> (* x (* (/ x) y)) y)
  87. (==> (/ (* x y)) (* (/ y) (/ x))))
  88. )