mk: phase 1 — unify.sx + 48 tests, kit-driven
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
lib/minikanren/unify.sx wraps lib/guest/match.sx with a miniKanren-flavoured cfg: native SX lists as cons-pairs, occurs-check off by default. ~22 lines of local logic over kit's walk-with / unify-with / extend / occurs-with. 48 tests in lib/minikanren/tests/unify.sx exercise: var fresh-distinct, walk chains, walk* deep into nested lists, atom/var/list unification with positional matching, failure modes, opt-in occurs check.
This commit is contained in:
52
lib/minikanren/unify.sx
Normal file
52
lib/minikanren/unify.sx
Normal file
@@ -0,0 +1,52 @@
|
||||
;; lib/minikanren/unify.sx — Phase 1: variables + unification.
|
||||
;;
|
||||
;; 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 shape (designed for natural SX use):
|
||||
;; logic var : (:var NAME) — kit's mk-var
|
||||
;; pair : any non-empty SX list — head + tail unified positionally
|
||||
;; atom : number / string / symbol / boolean / nil / ()
|
||||
;; Substitution: SX dict mapping VAR-NAME → term. Empty = (empty-subst).
|
||||
|
||||
(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) 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-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)))
|
||||
Reference in New Issue
Block a user