;; lib/minikanren/goals.sx — Phase 2 piece B: core goals.
;;
;; A goal is a function (fn (s) → stream-of-substitutions).
;; Goals built here:
;;   succeed         — always returns (unit s)
;;   fail            — always returns mzero
;;   ==              — unifies two terms; succeeds with a singleton, else fails
;;   ==-check        — opt-in occurs-checked equality
;;   conj2 / mk-conj — sequential conjunction of goals
;;   disj2 / mk-disj — interleaved disjunction of goals (raw — `conde` adds
;;                     the implicit-conj-per-clause sugar in a later commit)

(define succeed (fn (s) (unit s)))

(define fail (fn (s) mzero))

(define
  ==
  (fn
    (u v)
    (fn
      (s)
      (let ((s2 (mk-unify u v s))) (if (= s2 nil) mzero (unit s2))))))

(define
  ==-check
  (fn
    (u v)
    (fn
      (s)
      (let ((s2 (mk-unify-check u v s))) (if (= s2 nil) mzero (unit s2))))))

(define conj2 (fn (g1 g2) (fn (s) (mk-bind (g1 s) g2))))

(define disj2 (fn (g1 g2) (fn (s) (mk-mplus (g1 s) (g2 s)))))

;; Fold goals in a list. (mk-conj-list ()) ≡ succeed; (mk-disj-list ()) ≡ fail.
(define
  mk-conj-list
  (fn
    (gs)
    (cond
      ((empty? gs) succeed)
      ((empty? (rest gs)) (first gs))
      (:else (conj2 (first gs) (mk-conj-list (rest gs)))))))

(define
  mk-disj-list
  (fn
    (gs)
    (cond
      ((empty? gs) fail)
      ((empty? (rest gs)) (first gs))
      (:else (disj2 (first gs) (mk-disj-list (rest gs)))))))

(define mk-conj (fn (&rest gs) (mk-conj-list gs)))

(define mk-disj (fn (&rest gs) (mk-disj-list gs)))
