prolog: nqueens.pl + nqueens.sx (N=1..5), 6 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
27
lib/prolog/tests/programs/nqueens.pl
Normal file
27
lib/prolog/tests/programs/nqueens.pl
Normal file
@@ -0,0 +1,27 @@
|
||||
%% nqueens — permutation-and-test formulation.
|
||||
%% Caller passes the row list [1..N]; queens/2 finds N column placements
|
||||
%% s.t. no two queens attack on a diagonal. Same-column attacks are
|
||||
%% structurally impossible — Qs is a permutation, all distinct.
|
||||
%%
|
||||
%% No `>/2` `</2` `=</2` built-ins yet, so range/3 is omitted; tests pass
|
||||
%; the literal range list. Once the operator table lands and arithmetic
|
||||
%% comparison built-ins are in, range/3 can be added.
|
||||
queens(L, Qs) :- permute(L, Qs), safe(Qs).
|
||||
|
||||
permute([], []).
|
||||
permute(L, [H|T]) :- select(H, L, R), permute(R, T).
|
||||
|
||||
select(X, [X|T], T).
|
||||
select(X, [H|T], [H|R]) :- select(X, T, R).
|
||||
|
||||
safe([]).
|
||||
safe([Q|Qs]) :- safe(Qs), no_attack(Q, Qs, 1).
|
||||
|
||||
no_attack(_, [], _).
|
||||
no_attack(Q, [Q1|Qs], D) :-
|
||||
is(D2, +(Q, D)),
|
||||
\=(D2, Q1),
|
||||
is(D3, -(Q, D)),
|
||||
\=(D3, Q1),
|
||||
is(D1, +(D, 1)),
|
||||
no_attack(Q, Qs, D1).
|
||||
108
lib/prolog/tests/programs/nqueens.sx
Normal file
108
lib/prolog/tests/programs/nqueens.sx
Normal file
@@ -0,0 +1,108 @@
|
||||
;; lib/prolog/tests/programs/nqueens.sx — N-queens via permute + safe.
|
||||
|
||||
(define pl-nq-test-count 0)
|
||||
(define pl-nq-test-pass 0)
|
||||
(define pl-nq-test-fail 0)
|
||||
(define pl-nq-test-failures (list))
|
||||
|
||||
(define
|
||||
pl-nq-test!
|
||||
(fn
|
||||
(name got expected)
|
||||
(begin
|
||||
(set! pl-nq-test-count (+ pl-nq-test-count 1))
|
||||
(if
|
||||
(= got expected)
|
||||
(set! pl-nq-test-pass (+ pl-nq-test-pass 1))
|
||||
(begin
|
||||
(set! pl-nq-test-fail (+ pl-nq-test-fail 1))
|
||||
(append!
|
||||
pl-nq-test-failures
|
||||
(str name "\n expected: " expected "\n got: " got)))))))
|
||||
|
||||
(define
|
||||
pl-nq-term-to-sx
|
||||
(fn
|
||||
(t)
|
||||
(cond
|
||||
((pl-num? t) (pl-num-val t))
|
||||
((pl-atom? t) (pl-atom-name t))
|
||||
(true (list :complex)))))
|
||||
|
||||
(define
|
||||
pl-nq-list-walked
|
||||
(fn
|
||||
(w)
|
||||
(cond
|
||||
((and (pl-atom? w) (= (pl-atom-name w) "[]")) (list))
|
||||
((and (pl-compound? w) (= (pl-fun w) ".") (= (len (pl-args w)) 2))
|
||||
(cons
|
||||
(pl-nq-term-to-sx (first (pl-args w)))
|
||||
(pl-nq-list-walked (nth (pl-args w) 1))))
|
||||
(true (list :not-list)))))
|
||||
|
||||
(define pl-nq-list-to-sx (fn (t) (pl-nq-list-walked (pl-walk-deep t))))
|
||||
|
||||
(define
|
||||
pl-nq-goal
|
||||
(fn
|
||||
(src env)
|
||||
(pl-instantiate (nth (first (pl-parse (str "g :- " src "."))) 2) env)))
|
||||
|
||||
(define
|
||||
pl-nq-prog-src
|
||||
"queens(L, Qs) :- permute(L, Qs), safe(Qs). permute([], []). permute(L, [H|T]) :- select(H, L, R), permute(R, T). select(X, [X|T], T). select(X, [H|T], [H|R]) :- select(X, T, R). safe([]). safe([Q|Qs]) :- safe(Qs), no_attack(Q, Qs, 1). no_attack(_, [], _). no_attack(Q, [Q1|Qs], D) :- is(D2, +(Q, D)), \\=(D2, Q1), is(D3, -(Q, D)), \\=(D3, Q1), is(D1, +(D, 1)), no_attack(Q, Qs, D1).")
|
||||
|
||||
(define pl-nq-db (pl-mk-db))
|
||||
(pl-db-load! pl-nq-db (pl-parse pl-nq-prog-src))
|
||||
|
||||
(pl-nq-test!
|
||||
"queens([1], Qs) → 1 solution"
|
||||
(pl-solve-count!
|
||||
pl-nq-db
|
||||
(pl-nq-goal "queens([1], Qs)" {})
|
||||
(pl-mk-trail))
|
||||
1)
|
||||
|
||||
(pl-nq-test!
|
||||
"queens([1, 2], Qs) → 0 solutions"
|
||||
(pl-solve-count!
|
||||
pl-nq-db
|
||||
(pl-nq-goal "queens([1, 2], Qs)" {})
|
||||
(pl-mk-trail))
|
||||
0)
|
||||
|
||||
(pl-nq-test!
|
||||
"queens([1, 2, 3], Qs) → 0 solutions"
|
||||
(pl-solve-count!
|
||||
pl-nq-db
|
||||
(pl-nq-goal "queens([1, 2, 3], Qs)" {})
|
||||
(pl-mk-trail))
|
||||
0)
|
||||
|
||||
(pl-nq-test!
|
||||
"queens([1, 2, 3, 4], Qs) → 2 solutions"
|
||||
(pl-solve-count!
|
||||
pl-nq-db
|
||||
(pl-nq-goal "queens([1, 2, 3, 4], Qs)" {})
|
||||
(pl-mk-trail))
|
||||
2)
|
||||
|
||||
(pl-nq-test!
|
||||
"queens([1, 2, 3, 4, 5], Qs) → 10 solutions"
|
||||
(pl-solve-count!
|
||||
pl-nq-db
|
||||
(pl-nq-goal "queens([1, 2, 3, 4, 5], Qs)" {})
|
||||
(pl-mk-trail))
|
||||
10)
|
||||
|
||||
(define pl-nq-env-1 {})
|
||||
(define pl-nq-goal-1 (pl-nq-goal "queens([1, 2, 3, 4], Qs)" pl-nq-env-1))
|
||||
(pl-solve-once! pl-nq-db pl-nq-goal-1 (pl-mk-trail))
|
||||
|
||||
(pl-nq-test!
|
||||
"queens([1..4], Qs) first solution = [2, 4, 1, 3]"
|
||||
(pl-nq-list-to-sx (dict-get pl-nq-env-1 "Qs"))
|
||||
(list 2 4 1 3))
|
||||
|
||||
(define pl-nqueens-tests-run! (fn () {:failed pl-nq-test-fail :passed pl-nq-test-pass :total pl-nq-test-count :failures pl-nq-test-failures}))
|
||||
@@ -59,7 +59,7 @@ Representation choices (finalise in phase 1, document here):
|
||||
- [x] `append.pl` — list append (with backtracking) — `lib/prolog/tests/programs/append.{pl,sx}`. 6 tests cover: build (`append([], L, X)`, `append([1,2], [3,4], X)`), check ground match/mismatch, full split-backtracking (`append(X, Y, [1,2,3])` → 4 solutions), single-deduce (`append(X, [3], [1,2,3])` → X=[1,2]).
|
||||
- [x] `reverse.pl` — naive reverse — `lib/prolog/tests/programs/reverse.{pl,sx}`. Naive reverse via append: `reverse([H|T], R) :- reverse(T, RT), append(RT, [H], R)`. 6 tests cover empty, singleton, 3-list, 4-atom-list, ground match, ground mismatch.
|
||||
- [x] `member.pl` — generate all solutions via backtracking — `lib/prolog/tests/programs/member.{pl,sx}`. Classic 2-clause `member(X, [X|_])` + `member(X, [_|T]) :- member(X, T)`. 7 tests cover bound-element hit/miss, empty list, generator (count = list length), first-solution binding, duplicate matches counted twice, anonymous head-cell unification.
|
||||
- [ ] `nqueens.pl` — 8-queens
|
||||
- [x] `nqueens.pl` — 8-queens — `lib/prolog/tests/programs/nqueens.{pl,sx}`. Permute-and-test formulation: `queens(L, Qs) :- permute(L, Qs), safe(Qs)` + `select` + `safe` + `no_attack`. Tested at N=1 (1), N=2 (0), N=3 (0), N=4 (2), N=5 (10) plus first-solution check at N=4 = `[2, 4, 1, 3]`. N=8 omitted — interpreter is too slow (40320 perms); add once compiled clauses or constraint-style placement land. `range/3` skipped pending arithmetic-comparison built-ins (`>/2` etc.).
|
||||
- [ ] `family.pl` — facts + rules (parent/ancestor)
|
||||
- [ ] `lib/prolog/conformance.sh` + runner, `scoreboard.json` + `scoreboard.md`
|
||||
- [ ] Target: all 5 classic programs passing
|
||||
@@ -88,6 +88,7 @@ Representation choices (finalise in phase 1, document here):
|
||||
|
||||
_Newest first. Agent appends on every commit._
|
||||
|
||||
- 2026-04-25 — `nqueens.pl` fourth classic program. Permute-and-test variant exercises every Phase-3 feature: lists with `[H|T]` cons sugar, multi-clause backtracking, recursive `permute`/`select`/`safe`/`no_attack`, `is/2` arithmetic on diagonals, `\=/2` for diagonal-conflict check. 6 tests at N ∈ {1,2,3,4,5} with expected counts {1,0,0,2,10} + first-solution `[2,4,1,3]`. N=5 takes ~30s (120 perms × safe-check); N=8 omitted as it would be ~thousands of seconds. Total 173 (+6).
|
||||
- 2026-04-25 — `member.pl` third classic program. Standard 2-clause definition; 7 tests cover bound-element hit/miss, empty-list fail, generator-count = list length, first-solution binding (X=11), duplicate elements matched twice on backtrack, anonymous-head unification (`member(a, [X, b, c])` binds X=a). Total 167 (+7).
|
||||
- 2026-04-25 — `reverse.pl` second classic program. Naive reverse defined via append. 6 tests (empty/singleton/3-list/4-atom-list/ground match/ground mismatch). Confirms the solver handles non-trivial recursive composition: `reverse([1,2,3], R)` recurses to depth 3 then unwinds via 3 nested `append`s. Total 160 (+6).
|
||||
- 2026-04-25 — `append.pl` first classic program. `lib/prolog/tests/programs/append.pl` is the canonical 2-clause source; `append.sx` embeds the source as a string (no file-read primitive in SX yet) and runs 6 tests covering build, check, full split-backtrack (4 solutions), and deduction modes. Helpers `pl-ap-list-to-sx` / `pl-ap-term-to-sx` convert deep-walked Prolog lists (`("compound" "." (h t))` / `("atom" "[]")`) to SX lists for structural assertion. Total 154 (+6).
|
||||
|
||||
Reference in New Issue
Block a user