datalog: dl-walk handles circular substitutions without infinite loop (257/257)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Bug: dl-walk would infinite-loop on a circular substitution (e.g. A→B and B→A simultaneously). The walk endlessly chased the cycle. This couldn't be produced through dl-unify (which has cycle-safe behavior via existing bindings), but raw dl-bind calls or external manipulation of the subst dict could create it. Fix: dl-walk now threads a visited-names list through the recursion. If a variable name is already in the list, the walk stops and returns the current term unchanged. Normal chained walks are unaffected (A→B→C→42 still resolves to 42). 1 new unify test verifies circular substitutions don't hang.
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"lang": "datalog",
|
"lang": "datalog",
|
||||||
"total_passed": 256,
|
"total_passed": 257,
|
||||||
"total_failed": 0,
|
"total_failed": 0,
|
||||||
"total": 256,
|
"total": 257,
|
||||||
"suites": [
|
"suites": [
|
||||||
{"name":"tokenize","passed":30,"failed":0,"total":30},
|
{"name":"tokenize","passed":30,"failed":0,"total":30},
|
||||||
{"name":"parse","passed":22,"failed":0,"total":22},
|
{"name":"parse","passed":22,"failed":0,"total":22},
|
||||||
{"name":"unify","passed":28,"failed":0,"total":28},
|
{"name":"unify","passed":29,"failed":0,"total":29},
|
||||||
{"name":"eval","passed":38,"failed":0,"total":38},
|
{"name":"eval","passed":38,"failed":0,"total":38},
|
||||||
{"name":"builtins","passed":23,"failed":0,"total":23},
|
{"name":"builtins","passed":23,"failed":0,"total":23},
|
||||||
{"name":"semi_naive","passed":8,"failed":0,"total":8},
|
{"name":"semi_naive","passed":8,"failed":0,"total":8},
|
||||||
@@ -16,5 +16,5 @@
|
|||||||
{"name":"magic","passed":36,"failed":0,"total":36},
|
{"name":"magic","passed":36,"failed":0,"total":36},
|
||||||
{"name":"demo","passed":21,"failed":0,"total":21}
|
{"name":"demo","passed":21,"failed":0,"total":21}
|
||||||
],
|
],
|
||||||
"generated": "2026-05-10T21:16:53+00:00"
|
"generated": "2026-05-11T07:20:07+00:00"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
# datalog scoreboard
|
# datalog scoreboard
|
||||||
|
|
||||||
**256 / 256 passing** (0 failure(s)).
|
**257 / 257 passing** (0 failure(s)).
|
||||||
|
|
||||||
| Suite | Passed | Total | Status |
|
| Suite | Passed | Total | Status |
|
||||||
|-------|--------|-------|--------|
|
|-------|--------|-------|--------|
|
||||||
| tokenize | 30 | 30 | ok |
|
| tokenize | 30 | 30 | ok |
|
||||||
| parse | 22 | 22 | ok |
|
| parse | 22 | 22 | ok |
|
||||||
| unify | 28 | 28 | ok |
|
| unify | 29 | 29 | ok |
|
||||||
| eval | 38 | 38 | ok |
|
| eval | 38 | 38 | ok |
|
||||||
| builtins | 23 | 23 | ok |
|
| builtins | 23 | 23 | ok |
|
||||||
| semi_naive | 8 | 8 | ok |
|
| semi_naive | 8 | 8 | ok |
|
||||||
|
|||||||
@@ -129,6 +129,15 @@
|
|||||||
((s2 (dl-unify (quote Y) (quote tom) s1)))
|
((s2 (dl-unify (quote Y) (quote tom) s1)))
|
||||||
(dl-walk (quote X) s2)))
|
(dl-walk (quote X) s2)))
|
||||||
(quote tom))
|
(quote tom))
|
||||||
|
|
||||||
|
;; Walk with circular substitution must not infinite-loop.
|
||||||
|
;; Cycles return the current term unchanged.
|
||||||
|
(dl-ut-test!
|
||||||
|
"walk circular subst no hang"
|
||||||
|
(let ((s (dl-bind (quote B) (quote A)
|
||||||
|
(dl-bind (quote A) (quote B) (dl-empty-subst)))))
|
||||||
|
(dl-walk (quote A) s))
|
||||||
|
(quote A))
|
||||||
(dl-ut-test!
|
(dl-ut-test!
|
||||||
"apply subst on tuple"
|
"apply subst on tuple"
|
||||||
(let
|
(let
|
||||||
|
|||||||
@@ -33,16 +33,28 @@
|
|||||||
;; variable. The result is either a non-variable term or an unbound var.
|
;; variable. The result is either a non-variable term or an unbound var.
|
||||||
(define
|
(define
|
||||||
dl-walk
|
dl-walk
|
||||||
|
(fn (term subst) (dl-walk-aux term subst (list))))
|
||||||
|
|
||||||
|
;; Internal: walk with a visited-var set so circular substitutions
|
||||||
|
;; (from raw dl-bind misuse) don't infinite-loop. Cycles return the
|
||||||
|
;; current term unchanged.
|
||||||
|
(define
|
||||||
|
dl-walk-aux
|
||||||
(fn
|
(fn
|
||||||
(term subst)
|
(term subst visited)
|
||||||
(if
|
(if
|
||||||
(dl-var? term)
|
(dl-var? term)
|
||||||
(let
|
(let
|
||||||
((name (symbol->string term)))
|
((name (symbol->string term)))
|
||||||
(if
|
(cond
|
||||||
(and (dict? subst) (has-key? subst name))
|
((dl-member? name visited) term)
|
||||||
(dl-walk (get subst name) subst)
|
((and (dict? subst) (has-key? subst name))
|
||||||
term))
|
(let ((seen (list)))
|
||||||
|
(do
|
||||||
|
(for-each (fn (v) (append! seen v)) visited)
|
||||||
|
(append! seen name)
|
||||||
|
(dl-walk-aux (get subst name) subst seen))))
|
||||||
|
(else term)))
|
||||||
term)))
|
term)))
|
||||||
|
|
||||||
;; Bind a variable symbol to a value in subst, returning a new subst.
|
;; Bind a variable symbol to a value in subst, returning a new subst.
|
||||||
|
|||||||
Reference in New Issue
Block a user