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.
83 lines
2.6 KiB
Plaintext
83 lines
2.6 KiB
Plaintext
;; lib/minikanren/unify.sx — Phase 1 + cons-cell extension.
|
|
;;
|
|
;; miniKanren-on-SX, built on lib/guest/match.sx. The kit ships the heavy
|
|
;; lifting (walk-with, unify-with, occurs-with, extend, empty-subst,
|
|
;; mk-var/is-var?/var-name); this file supplies a miniKanren-shaped cfg
|
|
;; and a thin public API.
|
|
;;
|
|
;; Term shapes:
|
|
;; logic var : (:var NAME) — kit's mk-var
|
|
;; cons cell : (:cons HEAD TAIL) — for relational programming
|
|
;; (built by mk-cons; lets relations decompose lists by
|
|
;; head/tail without proper improper pairs in the host)
|
|
;; native list : SX list (a b c) — also unifies pair-style:
|
|
;; args = (head, tail) so (1 2 3) ≡ (:cons 1 (:cons 2 (:cons 3 ())))
|
|
;; atom : number / string / symbol / boolean / nil / ()
|
|
;;
|
|
;; Substitution: SX dict mapping VAR-NAME → term. Empty = (empty-subst).
|
|
|
|
(define mk-cons (fn (h t) (list :cons h t)))
|
|
|
|
(define
|
|
mk-cons-cell?
|
|
(fn (t) (and (list? t) (not (empty? t)) (= (first t) :cons))))
|
|
|
|
(define mk-cons-head (fn (t) (nth t 1)))
|
|
(define mk-cons-tail (fn (t) (nth t 2)))
|
|
|
|
(define
|
|
mk-list-pair?
|
|
(fn (t) (and (list? t) (not (empty? t)) (not (is-var? t)))))
|
|
|
|
(define mk-list-pair-head (fn (t) :pair))
|
|
|
|
(define
|
|
mk-list-pair-args
|
|
(fn
|
|
(t)
|
|
(cond
|
|
((mk-cons-cell? t) (list (mk-cons-head t) (mk-cons-tail t)))
|
|
(:else (list (first t) (rest t))))))
|
|
|
|
(define mk-cfg {:ctor-head mk-list-pair-head :var? is-var? :ctor? mk-list-pair? :occurs-check? false :var-name var-name :ctor-args mk-list-pair-args})
|
|
|
|
(define mk-cfg-occurs {:ctor-head mk-list-pair-head :var? is-var? :ctor? mk-list-pair? :occurs-check? true :var-name var-name :ctor-args mk-list-pair-args})
|
|
|
|
(define empty-s (empty-subst))
|
|
|
|
(define mk-fresh-counter 0)
|
|
|
|
(define
|
|
make-var
|
|
(fn
|
|
()
|
|
(begin
|
|
(set! mk-fresh-counter (+ mk-fresh-counter 1))
|
|
(mk-var (str "_." mk-fresh-counter)))))
|
|
|
|
(define mk-var? is-var?)
|
|
|
|
(define mk-walk (fn (t s) (walk-with mk-cfg t s)))
|
|
|
|
(define
|
|
mk-walk*
|
|
(fn
|
|
(t s)
|
|
(let
|
|
((w (mk-walk t s)))
|
|
(cond
|
|
((mk-cons-cell? w)
|
|
(let
|
|
((h (mk-walk* (mk-cons-head w) s))
|
|
(tl (mk-walk* (mk-cons-tail w) s)))
|
|
(cond
|
|
((empty? tl) (list h))
|
|
((mk-cons-cell? tl) tl)
|
|
((list? tl) (cons h tl))
|
|
(:else (mk-cons h tl)))))
|
|
((mk-list-pair? w) (map (fn (a) (mk-walk* a s)) w))
|
|
(:else w)))))
|
|
|
|
(define mk-unify (fn (u v s) (unify-with mk-cfg u v s)))
|
|
(define mk-unify-check (fn (u v s) (unify-with mk-cfg-occurs u v s)))
|