;; lib/minikanren/tests/path-cycle-free.sx — cycle-free reachability search. ;; ;; Threads a "visited" accumulator through the recursion, using nafc + ;; membero to prevent revisiting nodes. Demonstrates how to make the ;; cyclic-graph divergence problem (see tests/cyclic-graph.sx) tractable ;; for graphs with cycles, without invoking Phase-7 tabling. (define cf-edges (list (list :a :b) (list :b :a) (list :b :c) (list :c :d) (list :d :a))) ; another cycle (define cf-edgeo (fn (from to) (membero (list from to) cf-edges))) (define patho-no-cycles (fn (x y visited path) (conde ((cf-edgeo x y) (nafc (membero y visited)) (== path (list x y))) ((fresh (z mid v-prime) (cf-edgeo x z) (nafc (membero z visited)) (conso z visited v-prime) (patho-no-cycles z y v-prime mid) (conso x mid path)))))) (define cf-patho (fn (x y path) (patho-no-cycles x y (list x) path))) (mk-test "cycle-free-finds-finitely" (let ((paths (run* q (cf-patho :a :d q)))) (and (>= (len paths) 1) (every? (fn (p) (and (= (first p) :a) (= (last p) :d))) paths))) true) (mk-test "cycle-free-direct-edge" (run* q (cf-patho :a :b q)) (list (list :a :b))) (mk-test "cycle-free-no-self-loop" (run* q (cf-patho :a :a q)) (list)) (mk-tests-run!)