Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Three coupled fixes plus a new relations module land together because
each is required for the next: appendo can't terminate without all
three.
1. unify.sx — added (:cons h t) tagged cons-cell shape because SX has no
improper pairs. The unifier treats (:cons h t) and the native list
(h . t) as equivalent. mk-walk* re-flattens cons cells back to flat
lists for clean reification.
2. stream.sx — switched mature stream cells from plain SX lists to a
(:s head tail) tagged shape so a mature head can have a thunk tail.
With the old representation, mk-mplus had to (cons head thunk) which
SX rejects (cons requires a list cdr).
3. conde.sx — wraps each clause in Zzz (inverse-eta delay) for laziness.
Zzz uses (gensym "zzz-s-") for the substitution parameter so it does
not capture user goals that follow the (l s ls) convention. Without
gensym, every relation that uses `s` as a list parameter silently
binds it to the substitution dict.
relations.sx is the new module: nullo, pairo, caro, cdro, conso,
firsto, resto, listo, appendo, membero. 25 new tests.
Canary green:
(run* q (appendo (list 1 2) (list 3 4) q))
→ ((1 2 3 4))
(run* q (fresh (l s) (appendo l s (list 1 2 3)) (== q (list l s))))
→ ((() (1 2 3)) ((1) (2 3)) ((1 2) (3)) ((1 2 3) ()))
(run 3 q (listo q))
→ (() (_.0) (_.0 _.1))
152/152 cumulative.
59 lines
1.7 KiB
Plaintext
59 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.)
|
|
|
|
(define
|
|
onceo
|
|
(fn
|
|
(g)
|
|
(fn
|
|
(s)
|
|
(let
|
|
((peek (stream-take 1 (g s))))
|
|
(if (empty? peek) mzero (unit (first peek)))))))
|
|
|
|
;; 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, recurse to
|
|
;; the 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))))
|