Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
condu.sx: defmacro `condu` folds clauses through a runtime `condu-try` walker. First clause whose head yields a non-empty stream commits its single first answer; later clauses are not tried. `onceo` is the simpler sibling — stream-take 1 over a goal's output. 10 tests cover: onceo trimming success/failure/conde, condu first-clause wins, condu skips failing heads, condu commits-and-cannot-backtrack to later clauses if the rest of the chosen clause fails. 110/110 cumulative. Phase 2 complete.
53 lines
1.7 KiB
Plaintext
53 lines
1.7 KiB
Plaintext
;; lib/minikanren/condu.sx — Phase 2 piece D: `condu` and `onceo`.
|
|
;;
|
|
;; Both are commitment forms (no backtracking into discarded options):
|
|
;;
|
|
;; (onceo g) — succeeds at most once: takes the first answer
|
|
;; stream-take produces from (g s).
|
|
;;
|
|
;; (condu (g0 g ...) (h0 h ...) ...)
|
|
;; — first clause whose head goal succeeds wins; only
|
|
;; the first answer of the head is propagated to the
|
|
;; rest of that clause; later clauses are not tried.
|
|
;; (Reasoned Schemer chapter 10; Byrd 5.4.)
|
|
;;
|
|
;; `conda` (the variant that propagates ALL answers of the head) lives in
|
|
;; Phase 5 with `project` and `matche`.
|
|
|
|
(define onceo (fn (g) (fn (s) (stream-take 1 (g s)))))
|
|
|
|
;; condu-try — runtime walker over a list of clauses (each clause a list of goals).
|
|
;; Forces the head with stream-take 1; if head fails, falls to next clause;
|
|
;; if head succeeds, commits its single answer through the rest of the clause.
|
|
(define
|
|
condu-try
|
|
(fn
|
|
(clauses s)
|
|
(cond
|
|
((empty? clauses) mzero)
|
|
(:else
|
|
(let
|
|
((cl (first clauses)))
|
|
(let
|
|
((head-goal (first cl)) (rest-goals (rest cl)))
|
|
(let
|
|
((peek (stream-take 1 (head-goal s))))
|
|
(if
|
|
(empty? peek)
|
|
(condu-try (rest clauses) s)
|
|
((mk-conj-list rest-goals) (first peek))))))))))
|
|
|
|
(defmacro
|
|
condu
|
|
(&rest clauses)
|
|
(quasiquote
|
|
(fn
|
|
(s)
|
|
(condu-try
|
|
(list
|
|
(splice-unquote
|
|
(map
|
|
(fn (cl) (quasiquote (list (splice-unquote cl))))
|
|
clauses)))
|
|
s))))
|