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.
67 lines
2.0 KiB
Plaintext
67 lines
2.0 KiB
Plaintext
;; lib/minikanren/stream.sx — Phase 2 piece A: lazy streams of substitutions.
|
|
;;
|
|
;; SX has no improper pairs (cons requires a list cdr), so we use a
|
|
;; tagged stream-cell shape for mature stream elements:
|
|
;;
|
|
;; stream ::= mzero empty (the SX empty list)
|
|
;; | (:s HEAD TAIL) mature cell, TAIL is a stream
|
|
;; | thunk (fn () ...) → stream when forced
|
|
;;
|
|
;; HEAD is a substitution dict. TAIL is again a stream (possibly a thunk),
|
|
;; which is what gives us laziness — mk-mplus can return a mature head with
|
|
;; a thunk in the tail, deferring the rest of the search.
|
|
|
|
(define mzero (list))
|
|
|
|
(define s-cons (fn (h t) (list :s h t)))
|
|
|
|
(define
|
|
s-cons?
|
|
(fn (s) (and (list? s) (not (empty? s)) (= (first s) :s))))
|
|
|
|
(define s-car (fn (s) (nth s 1)))
|
|
(define s-cdr (fn (s) (nth s 2)))
|
|
|
|
(define unit (fn (s) (s-cons s mzero)))
|
|
|
|
(define stream-pause? (fn (s) (and (not (list? s)) (callable? s))))
|
|
|
|
;; mk-mplus — interleave two streams. If s1 is paused we suspend and
|
|
;; swap (Reasoned Schemer "interleave"); otherwise mature-cons head with
|
|
;; mk-mplus of the rest.
|
|
(define
|
|
mk-mplus
|
|
(fn
|
|
(s1 s2)
|
|
(cond
|
|
((empty? s1) s2)
|
|
((stream-pause? s1) (fn () (mk-mplus s2 (s1))))
|
|
(:else (s-cons (s-car s1) (mk-mplus (s-cdr s1) s2))))))
|
|
|
|
;; mk-bind — apply goal g to every substitution in stream s, mk-mplus-ing.
|
|
(define
|
|
mk-bind
|
|
(fn
|
|
(s g)
|
|
(cond
|
|
((empty? s) mzero)
|
|
((stream-pause? s) (fn () (mk-bind (s) g)))
|
|
(:else (mk-mplus (g (s-car s)) (mk-bind (s-cdr s) g))))))
|
|
|
|
;; stream-take — force up to n results out of a (possibly lazy) stream
|
|
;; into a flat SX list of substitutions. n = -1 means take all.
|
|
(define
|
|
stream-take
|
|
(fn
|
|
(n s)
|
|
(cond
|
|
((= n 0) (list))
|
|
((empty? s) (list))
|
|
((stream-pause? s) (stream-take n (s)))
|
|
(:else
|
|
(cons
|
|
(s-car s)
|
|
(stream-take
|
|
(if (= n -1) -1 (- n 1))
|
|
(s-cdr s)))))))
|