mk: flatteno — nested list flattener
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 49s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 49s
Three conde clauses: nullo tree -> nullo flat; pairo tree -> recurse on
car & cdr, appendo their flattenings; otherwise tree must be a ground
non-list atom (nafc nullo + nafc pairo) and flat = (list tree).
Works on ground inputs of arbitrary nesting:
(run* q (flatteno (list 1 (list 2 3) (list (list 4) 5)) q))
-> ((1 2 3 4 5))
7 tests, 253/253 cumulative. Phase 4 list relations now complete.
This commit is contained in:
@@ -81,3 +81,18 @@
|
||||
(conde
|
||||
((nullo l) (nullo p))
|
||||
((fresh (a d perm-d) (conso a d l) (permuteo d perm-d) (inserto a perm-d p))))))
|
||||
|
||||
(define
|
||||
flatteno
|
||||
(fn
|
||||
(tree flat)
|
||||
(conde
|
||||
((nullo tree) (nullo flat))
|
||||
((pairo tree)
|
||||
(fresh
|
||||
(h t hf tf)
|
||||
(conso h t tree)
|
||||
(flatteno h hf)
|
||||
(flatteno t tf)
|
||||
(appendo hf tf flat)))
|
||||
((nafc (nullo tree)) (nafc (pairo tree)) (== flat (list tree))))))
|
||||
|
||||
42
lib/minikanren/tests/flatteno.sx
Normal file
42
lib/minikanren/tests/flatteno.sx
Normal file
@@ -0,0 +1,42 @@
|
||||
(mk-test "flatteno-empty" (run* q (flatteno (list) q)) (list (list)))
|
||||
|
||||
(mk-test
|
||||
"flatteno-atom"
|
||||
(run* q (flatteno 5 q))
|
||||
(list (list 5)))
|
||||
|
||||
(mk-test
|
||||
"flatteno-flat-list"
|
||||
(run* q (flatteno (list 1 2 3) q))
|
||||
(list (list 1 2 3)))
|
||||
|
||||
(mk-test
|
||||
"flatteno-singleton"
|
||||
(run* q (flatteno (list 1) q))
|
||||
(list (list 1)))
|
||||
|
||||
(mk-test
|
||||
"flatteno-nested-once"
|
||||
(run*
|
||||
q
|
||||
(flatteno (list 1 (list 2 3) 4) q))
|
||||
(list (list 1 2 3 4)))
|
||||
|
||||
(mk-test
|
||||
"flatteno-nested-twice"
|
||||
(run*
|
||||
q
|
||||
(flatteno
|
||||
(list
|
||||
1
|
||||
(list 2 (list 3 4))
|
||||
5)
|
||||
q))
|
||||
(list (list 1 2 3 4 5)))
|
||||
|
||||
(mk-test
|
||||
"flatteno-keywords"
|
||||
(run* q (flatteno (list :a (list :b :c) :d) q))
|
||||
(list (list :a :b :c :d)))
|
||||
|
||||
(mk-tests-run!)
|
||||
@@ -111,7 +111,11 @@ Key semantic mappings:
|
||||
- [x] `caro` / `cdro` / `conso` / `firsto` / `resto`
|
||||
- [x] `reverseo` `l` `r` — reverse of list. Forward is fast; backward is `run 1`-clean,
|
||||
`run*` diverges due to interleaved unbounded list search (canonical TRS issue).
|
||||
- [ ] `flatteno` `l` `f` — flatten nested lists (deferred — needs atom predicate)
|
||||
- [x] `flatteno` `l` `f` — flatten nested lists. Three conde clauses:
|
||||
empty → empty; pair → recurse on car & cdr + appendo; otherwise atom →
|
||||
`(== flat (list tree))`. Atom-ness is asserted via `nafc (nullo tree)
|
||||
+ nafc (pairo tree)` — both succeed only when tree is a ground non-list.
|
||||
Works on ground inputs.
|
||||
- [x] `permuteo` `l` `p` — permutation of list. Built on `inserto` (insert at any
|
||||
position) and recursive permutation of tail. All 6 perms of (1 2 3) generated
|
||||
forward; backward `run 1 q (permuteo q (a b c))` finds the input.
|
||||
@@ -169,6 +173,11 @@ _(none yet)_
|
||||
|
||||
_Newest first._
|
||||
|
||||
- **2026-05-08** — **flatteno**: nested-list flattener via conde + appendo.
|
||||
Atom-ness is detected via `nafc (nullo ...) + nafc (pairo ...)` so the
|
||||
third clause only fires when the input is a ground non-list. Works for
|
||||
`()` / atoms / flat lists / arbitrarily-nested lists. 7 new tests,
|
||||
253/253 cumulative. Phase 4 list relations all complete.
|
||||
- **2026-05-08** — **Laziness tests**: explicitly verifies the
|
||||
Zzz-on-conde-clauses + mk-mplus-swap-on-paused machinery: an
|
||||
infinitely-recursive relation truncated via `run 4 q (listo-aux q)`
|
||||
|
||||
Reference in New Issue
Block a user