;; lib/minikanren/tests/peano.sx — Peano arithmetic.
;;
;; Builds Peano numbers via a host-side helper so tests stay readable.
;; (mk-nat 3) → (:s (:s (:s :z))).

(define
  mk-nat
  (fn (n) (if (= n 0) :z (list :s (mk-nat (- n 1))))))

;; --- zeroo ---

(mk-test
  "zeroo-zero-succeeds"
  (run* q (zeroo :z))
  (list (make-symbol "_.0")))
(mk-test
  "zeroo-non-zero-fails"
  (run* q (zeroo (mk-nat 1)))
  (list))

;; --- pluso forward ---

(mk-test
  "pluso-forward-2-3"
  (run* q (pluso (mk-nat 2) (mk-nat 3) q))
  (list (mk-nat 5)))

(mk-test "pluso-forward-zero-zero" (run* q (pluso :z :z q)) (list :z))

(mk-test
  "pluso-forward-zero-n"
  (run* q (pluso :z (mk-nat 4) q))
  (list (mk-nat 4)))

(mk-test
  "pluso-forward-n-zero"
  (run* q (pluso (mk-nat 4) :z q))
  (list (mk-nat 4)))

;; --- pluso backward ---

(mk-test
  "pluso-recover-augend"
  (run* q (pluso q (mk-nat 2) (mk-nat 5)))
  (list (mk-nat 3)))

(mk-test
  "pluso-recover-addend"
  (run* q (pluso (mk-nat 2) q (mk-nat 5)))
  (list (mk-nat 3)))

(mk-test
  "pluso-enumerate-pairs-summing-to-3"
  (run*
    q
    (fresh (a b) (pluso a b (mk-nat 3)) (== q (list a b))))
  (list
    (list :z (mk-nat 3))
    (list (mk-nat 1) (mk-nat 2))
    (list (mk-nat 2) (mk-nat 1))
    (list (mk-nat 3) :z)))

;; --- minuso ---

(mk-test
  "minuso-5-2-3"
  (run* q (minuso (mk-nat 5) (mk-nat 2) q))
  (list (mk-nat 3)))

(mk-test
  "minuso-n-n-zero"
  (run* q (minuso (mk-nat 7) (mk-nat 7) q))
  (list :z))

;; --- *o ---

(mk-test
  "times-2-3"
  (run* q (*o (mk-nat 2) (mk-nat 3) q))
  (list (mk-nat 6)))

(mk-test
  "times-zero-anything-zero"
  (run* q (*o :z (mk-nat 99) q))
  (list :z))

(mk-test
  "times-3-4"
  (run* q (*o (mk-nat 3) (mk-nat 4) q))
  (list (mk-nat 12)))

;; --- lteo / lto ---

(mk-test
  "lteo-success"
  (run 1 q (lteo (mk-nat 2) (mk-nat 5)))
  (list (make-symbol "_.0")))

(mk-test
  "lteo-equal-success"
  (run 1 q (lteo (mk-nat 3) (mk-nat 3)))
  (list (make-symbol "_.0")))

(mk-test
  "lteo-greater-fails"
  (run* q (lteo (mk-nat 5) (mk-nat 2)))
  (list))

(mk-test
  "lto-strict-success"
  (run 1 q (lto (mk-nat 2) (mk-nat 5)))
  (list (make-symbol "_.0")))

(mk-test
  "lto-equal-fails"
  (run* q (lto (mk-nat 3) (mk-nat 3)))
  (list))

(mk-tests-run!)
