Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 57s
Squash merge of 76 commits from loops/minikanren. Adds lib/minikanren/ — a complete miniKanren-on-SX implementation built on top of lib/guest/match.sx, validating the lib-guest unify-and-match kit as intended. Modules (20 .sx files, ~1700 LOC): unify, stream, goals, fresh, conde, condu, conda, run, relations, peano, intarith, project, nafc, matche, fd, queens, defrel, clpfd, tabling Phases 1–5 fully done (core miniKanren API, all classic relations, matche, conda, project, nafc). Phase 6 — native CLP(FD): domain primitives, fd-in / fd-eq / fd-neq / fd-lt / fd-lte / fd-plus / fd-times / fd-distinct / fd-label, with constraint reactivation iterating to fixed point. N-queens via FD: 4-queens 2 solutions, 5-queens 10 solutions (vs naive timeout past N=4). Phase 7 — naive ground-arg tabling: table-1 / table-2 / table-3. Fibonacci canary: tab-fib(25) = 75025 in seconds, naive fib(25) times out at 60s. Ackermann via table-3: A(3,3) = 61. 71 test files, 644+ tests passing across the suite. Producer/consumer SLG (cyclic patho, mutual recursion) deferred — research-grade work. The lib-guest validation experiment is conclusive: lib/minikanren/ unify.sx adds ~50 lines of local logic (custom cfg, deep walk*, fresh counter) over lib/guest/match.sx's ~100-line kit. The kit earns its keep ~3× by line count.
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)))))))
|