Files
rose-ash/lib/minikanren/tests/queens-fd.sx
giles 57a84b372d
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 57s
Merge loops/minikanren into architecture: full miniKanren-on-SX library
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.
2026-05-08 23:01:54 +00:00

98 lines
2.1 KiB
Plaintext

;; lib/minikanren/tests/queens-fd.sx — N-queens via CLP(FD).
;;
;; Native FD propagation makes N-queens tractable: 4-queens finds both
;; solutions instantly; 5-queens finds all 10 in seconds. Compare with
;; the naive enumerate-then-filter version in queens.sx, which struggles
;; past N=4.
(define
fd-no-diag
(fn
(ci cj k)
(fresh
(a b)
(fd-plus cj k a)
(fd-plus ci k b)
(fd-neq ci a)
(fd-neq cj b))))
(define
n-queens-4-fd
(fn
(cs)
(let
((c1 (nth cs 0))
(c2 (nth cs 1))
(c3 (nth cs 2))
(c4 (nth cs 3)))
(mk-conj
(fd-in c1 (list 1 2 3 4))
(fd-in c2 (list 1 2 3 4))
(fd-in c3 (list 1 2 3 4))
(fd-in c4 (list 1 2 3 4))
(fd-distinct cs)
(fd-no-diag c1 c2 1)
(fd-no-diag c1 c3 2)
(fd-no-diag c1 c4 3)
(fd-no-diag c2 c3 1)
(fd-no-diag c2 c4 2)
(fd-no-diag c3 c4 1)
(fd-label cs)))))
(define
n-queens-5-fd
(fn
(cs)
(let
((c1 (nth cs 0))
(c2 (nth cs 1))
(c3 (nth cs 2))
(c4 (nth cs 3))
(c5 (nth cs 4)))
(mk-conj
(fd-in
c1
(list 1 2 3 4 5))
(fd-in
c2
(list 1 2 3 4 5))
(fd-in
c3
(list 1 2 3 4 5))
(fd-in
c4
(list 1 2 3 4 5))
(fd-in
c5
(list 1 2 3 4 5))
(fd-distinct cs)
(fd-no-diag c1 c2 1)
(fd-no-diag c1 c3 2)
(fd-no-diag c1 c4 3)
(fd-no-diag c1 c5 4)
(fd-no-diag c2 c3 1)
(fd-no-diag c2 c4 2)
(fd-no-diag c2 c5 3)
(fd-no-diag c3 c4 1)
(fd-no-diag c3 c5 2)
(fd-no-diag c4 c5 1)
(fd-label cs)))))
(mk-test
"n-queens-4-fd-two-solutions"
(run*
q
(fresh (a b c d) (== q (list a b c d)) (n-queens-4-fd (list a b c d))))
(list
(list 2 4 1 3)
(list 3 1 4 2)))
(mk-test
"n-queens-5-fd-ten-solutions"
(let
((sols (run* q (fresh (a b c d e) (== q (list a b c d e)) (n-queens-5-fd (list a b c d e))))))
(= (len sols) 10))
true)
(mk-tests-run!)