diff --git a/lib/minikanren/tests/btree-walko.sx b/lib/minikanren/tests/btree-walko.sx new file mode 100644 index 00000000..0dc617b1 --- /dev/null +++ b/lib/minikanren/tests/btree-walko.sx @@ -0,0 +1,54 @@ +;; lib/minikanren/tests/btree-walko.sx — walk a leaves-of-binary-tree relation +;; using matche dispatch on (:leaf v) and (:node left right) patterns. + +(define + btree-walko + (fn + (tree v) + (matche + tree + ((:leaf x) (== v x)) + ((:node l r) (conde ((btree-walko l v)) ((btree-walko r v))))))) + +;; A small test tree: ((1 2) (3 (4 5))). +(define + test-btree + (list + :node (list :node (list :leaf 1) (list :leaf 2)) + (list + :node (list :leaf 3) + (list :node (list :leaf 4) (list :leaf 5))))) + +(mk-test + "btree-walko-enumerates-all-leaves" + (let + ((leaves (run* q (btree-walko test-btree q)))) + (and + (= (len leaves) 5) + (and + (some (fn (l) (= l 1)) leaves) + (and + (some (fn (l) (= l 2)) leaves) + (and + (some (fn (l) (= l 3)) leaves) + (and + (some (fn (l) (= l 4)) leaves) + (some (fn (l) (= l 5)) leaves))))))) + true) + +(mk-test + "btree-walko-find-3-membership" + (run 1 q (btree-walko test-btree 3)) + (list (make-symbol "_.0"))) + +(mk-test + "btree-walko-find-99-not-present" + (run* q (btree-walko test-btree 99)) + (list)) + +(mk-test + "btree-walko-leaf-only" + (run* q (btree-walko (list :leaf 42) q)) + (list 42)) + +(mk-tests-run!) diff --git a/plans/minikanren-on-sx.md b/plans/minikanren-on-sx.md index f77aa993..0bb00f17 100644 --- a/plans/minikanren-on-sx.md +++ b/plans/minikanren-on-sx.md @@ -173,6 +173,7 @@ _(none yet)_ _Newest first._ +- **2026-05-08** — **btree-walko (matche showcase)**: walks a binary tree (:leaf v) | (:node l r) and emits each leaf value via conde. Demonstrates matche dispatch on tagged-list patterns, recursion through both branches via conde, and run* enumerating all 5 leaves of a small tree. 4 new tests, 441/441 cumulative. - **2026-05-08** — **swap-firsto**: swap the first two elements of a list. Built via four conso constraints. Self-inverse on length-2+ lists; runs forward and backward. 6 new tests, 437/437 cumulative. - **2026-05-08** — **pairlisto**: relational zip — pairs is the zipped list of (l1[i] l2[i]). Runs forward, recovers l1 given l2 and pairs, recovers l2 given l1 and pairs. 5 new tests, 431/431 cumulative. - **2026-05-08** — **iterate-no**: relational iterated application. Applies a 2-arg relation n times (Peano n) starting from x to produce result. Generalises succ-iteration / list-cons-iteration / etc. 4 new tests, 426/426 cumulative.