datalog: anonymous _ vars are unique per occurrence (Phase 5d, 156/156)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
(p X _), (p _ Y) — the two _ are now different variables, matching standard Datalog semantics. Previously both _ symbols were the same SX symbol, so unification across them gave wrong answers. Fix in db.sx: dl-rename-anon-term + dl-rename-anon-lit walk a term or literal and replace each '_' symbol with a fresh _anon<N>. dl-make-anon-renamer returns a counter-based name generator scoped per call. dl-rename-anon-rule applies it to head and body of a rule. dl-add-rule! invokes the renamer before safety check. eval.sx: dl-query renames anon vars in the goal before search and filters '_' out of the projection so user-facing results aren't polluted with internal _anon<N> bindings. The previous "underscore in head ok" test now correctly rejects (p X _) :- q(X) as unsafe (the head's fresh anon var has no body binder). New "underscore in body only" test confirms the safe case. Two regression tests for rule-level and goal-level independence.
This commit is contained in:
@@ -189,6 +189,44 @@
|
|||||||
" do not appear in any body literal"))
|
" do not appear in any body literal"))
|
||||||
(else nil))))))))
|
(else nil))))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
dl-rename-anon-term
|
||||||
|
(fn
|
||||||
|
(term next-name)
|
||||||
|
(cond
|
||||||
|
((and (symbol? term) (= (symbol->string term) "_"))
|
||||||
|
(next-name))
|
||||||
|
((list? term)
|
||||||
|
(map (fn (x) (dl-rename-anon-term x next-name)) term))
|
||||||
|
(else term))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
dl-rename-anon-lit
|
||||||
|
(fn
|
||||||
|
(lit next-name)
|
||||||
|
(cond
|
||||||
|
((and (dict? lit) (has-key? lit :neg))
|
||||||
|
{:neg (dl-rename-anon-term (get lit :neg) next-name)})
|
||||||
|
((list? lit) (dl-rename-anon-term lit next-name))
|
||||||
|
(else lit))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
dl-make-anon-renamer
|
||||||
|
(fn
|
||||||
|
()
|
||||||
|
(let ((counter 0))
|
||||||
|
(fn () (do (set! counter (+ counter 1))
|
||||||
|
(string->symbol (str "_anon" counter)))))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
dl-rename-anon-rule
|
||||||
|
(fn
|
||||||
|
(rule)
|
||||||
|
(let ((next-name (dl-make-anon-renamer)))
|
||||||
|
{:head (dl-rename-anon-term (get rule :head) next-name)
|
||||||
|
:body (map (fn (lit) (dl-rename-anon-lit lit next-name))
|
||||||
|
(get rule :body))})))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
dl-add-rule!
|
dl-add-rule!
|
||||||
(fn
|
(fn
|
||||||
@@ -199,14 +237,15 @@
|
|||||||
((not (has-key? rule :head))
|
((not (has-key? rule :head))
|
||||||
(error (str "dl-add-rule!: rule missing :head, got " rule)))
|
(error (str "dl-add-rule!: rule missing :head, got " rule)))
|
||||||
(else
|
(else
|
||||||
(let
|
(let ((rule (dl-rename-anon-rule rule)))
|
||||||
((err (dl-rule-check-safety rule)))
|
(let
|
||||||
(cond
|
((err (dl-rule-check-safety rule)))
|
||||||
((not (nil? err)) (error (str "dl-add-rule!: " err)))
|
(cond
|
||||||
(else
|
((not (nil? err)) (error (str "dl-add-rule!: " err)))
|
||||||
(let
|
(else
|
||||||
((rules (get db :rules)))
|
(let
|
||||||
(do (append! rules rule) true)))))))))
|
((rules (get db :rules)))
|
||||||
|
(do (append! rules rule) true))))))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
dl-add-clause!
|
dl-add-clause!
|
||||||
|
|||||||
@@ -368,21 +368,26 @@
|
|||||||
(db goal)
|
(db goal)
|
||||||
(do
|
(do
|
||||||
(dl-saturate! db)
|
(dl-saturate! db)
|
||||||
|
;; Rename anonymous '_' vars in the goal so multiple occurrences
|
||||||
|
;; do not unify together. Keep the user-facing var list (taken
|
||||||
|
;; before renaming) so projected results retain user names.
|
||||||
(let
|
(let
|
||||||
((substs (dl-find-bindings (list goal) db (dl-empty-subst)))
|
((user-vars (filter (fn (n) (not (= n "_"))) (dl-vars-of goal)))
|
||||||
(vars (dl-vars-of goal))
|
(renamed (dl-rename-anon-lit goal (dl-make-anon-renamer))))
|
||||||
(results (list)))
|
(let
|
||||||
(do
|
((substs (dl-find-bindings (list renamed) db (dl-empty-subst)))
|
||||||
(for-each
|
(results (list)))
|
||||||
(fn
|
(do
|
||||||
(s)
|
(for-each
|
||||||
(let
|
(fn
|
||||||
((proj (dl-project-subst s vars)))
|
(s)
|
||||||
(when
|
(let
|
||||||
(not (dl-tuple-member? proj results))
|
((proj (dl-project-subst s user-vars)))
|
||||||
(append! results proj))))
|
(when
|
||||||
substs)
|
(not (dl-tuple-member? proj results))
|
||||||
results)))))
|
(append! results proj))))
|
||||||
|
substs)
|
||||||
|
results))))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
dl-project-subst
|
dl-project-subst
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"lang": "datalog",
|
"lang": "datalog",
|
||||||
"total_passed": 153,
|
"total_passed": 156,
|
||||||
"total_failed": 0,
|
"total_failed": 0,
|
||||||
"total": 153,
|
"total": 156,
|
||||||
"suites": [
|
"suites": [
|
||||||
{"name":"tokenize","passed":26,"failed":0,"total":26},
|
{"name":"tokenize","passed":26,"failed":0,"total":26},
|
||||||
{"name":"parse","passed":18,"failed":0,"total":18},
|
{"name":"parse","passed":18,"failed":0,"total":18},
|
||||||
{"name":"unify","passed":28,"failed":0,"total":28},
|
{"name":"unify","passed":28,"failed":0,"total":28},
|
||||||
{"name":"eval","passed":15,"failed":0,"total":15},
|
{"name":"eval","passed":18,"failed":0,"total":18},
|
||||||
{"name":"builtins","passed":19,"failed":0,"total":19},
|
{"name":"builtins","passed":19,"failed":0,"total":19},
|
||||||
{"name":"semi_naive","passed":8,"failed":0,"total":8},
|
{"name":"semi_naive","passed":8,"failed":0,"total":8},
|
||||||
{"name":"negation","passed":10,"failed":0,"total":10},
|
{"name":"negation","passed":10,"failed":0,"total":10},
|
||||||
@@ -15,5 +15,5 @@
|
|||||||
{"name":"api","passed":9,"failed":0,"total":9},
|
{"name":"api","passed":9,"failed":0,"total":9},
|
||||||
{"name":"demo","passed":10,"failed":0,"total":10}
|
{"name":"demo","passed":10,"failed":0,"total":10}
|
||||||
],
|
],
|
||||||
"generated": "2026-05-08T08:49:52+00:00"
|
"generated": "2026-05-08T08:57:57+00:00"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
# datalog scoreboard
|
# datalog scoreboard
|
||||||
|
|
||||||
**153 / 153 passing** (0 failure(s)).
|
**156 / 156 passing** (0 failure(s)).
|
||||||
|
|
||||||
| Suite | Passed | Total | Status |
|
| Suite | Passed | Total | Status |
|
||||||
|-------|--------|-------|--------|
|
|-------|--------|-------|--------|
|
||||||
| tokenize | 26 | 26 | ok |
|
| tokenize | 26 | 26 | ok |
|
||||||
| parse | 18 | 18 | ok |
|
| parse | 18 | 18 | ok |
|
||||||
| unify | 28 | 28 | ok |
|
| unify | 28 | 28 | ok |
|
||||||
| eval | 15 | 15 | ok |
|
| eval | 18 | 18 | ok |
|
||||||
| builtins | 19 | 19 | ok |
|
| builtins | 19 | 19 | ok |
|
||||||
| semi_naive | 8 | 8 | ok |
|
| semi_naive | 8 | 8 | ok |
|
||||||
| negation | 10 | 10 | ok |
|
| negation | 10 | 10 | ok |
|
||||||
|
|||||||
@@ -173,9 +173,18 @@
|
|||||||
"unsafe — empty body"
|
"unsafe — empty body"
|
||||||
(dl-et-throws? (fn () (dl-program "p(X) :- .")))
|
(dl-et-throws? (fn () (dl-program "p(X) :- .")))
|
||||||
true)
|
true)
|
||||||
|
;; Underscore in head is unsafe — it's a fresh existential per
|
||||||
|
;; occurrence after Phase 5d's anonymous-var renaming, and there's
|
||||||
|
;; nothing in the body to bind it. (Old behavior accepted this by
|
||||||
|
;; treating '_' as a literal name to skip; the renaming made it an
|
||||||
|
;; ordinary unbound variable.)
|
||||||
(dl-et-test!
|
(dl-et-test!
|
||||||
"underscore var ok"
|
"underscore in head — unsafe"
|
||||||
(dl-et-throws? (fn () (dl-program "p(X, _) :- q(X).")))
|
(dl-et-throws? (fn () (dl-program "p(X, _) :- q(X).")))
|
||||||
|
true)
|
||||||
|
(dl-et-test!
|
||||||
|
"underscore in body only — safe"
|
||||||
|
(dl-et-throws? (fn () (dl-program "p(X) :- q(X, _).")))
|
||||||
false)
|
false)
|
||||||
(dl-et-test!
|
(dl-et-test!
|
||||||
"var only in head — unsafe"
|
"var only in head — unsafe"
|
||||||
@@ -192,7 +201,21 @@
|
|||||||
()
|
()
|
||||||
(dl-program
|
(dl-program
|
||||||
"edge(a,b). edge(b,c). reach(X, Z) :- edge(X, Y), edge(Y, Z).")))
|
"edge(a,b). edge(b,c). reach(X, Z) :- edge(X, Y), edge(Y, Z).")))
|
||||||
false))))
|
false)
|
||||||
|
|
||||||
|
;; Anonymous variables: each occurrence must be independent.
|
||||||
|
(dl-et-test-set! "anon vars in rule are independent"
|
||||||
|
(dl-query
|
||||||
|
(dl-program
|
||||||
|
"p(a, b). p(c, d). q(X) :- p(X, _), p(_, Y).")
|
||||||
|
(list (quote q) (quote X)))
|
||||||
|
(list {:X (quote a)} {:X (quote c)}))
|
||||||
|
|
||||||
|
(dl-et-test-set! "anon vars in goal are independent"
|
||||||
|
(dl-query
|
||||||
|
(dl-program "p(1, 2, 3). p(4, 5, 6).")
|
||||||
|
(list (quote p) (quote _) (quote X) (quote _)))
|
||||||
|
(list {:X 2} {:X 5})))))
|
||||||
|
|
||||||
(define
|
(define
|
||||||
dl-eval-tests-run!
|
dl-eval-tests-run!
|
||||||
|
|||||||
@@ -269,6 +269,17 @@ large graphs.
|
|||||||
|
|
||||||
_Newest first._
|
_Newest first._
|
||||||
|
|
||||||
|
- 2026-05-08 — Phase 5d semantic fix: anonymous `_` variables are
|
||||||
|
renamed per occurrence at `dl-add-rule!` and `dl-query` time so
|
||||||
|
`(p X _) (p _ Y)` no longer unifies the two `_`s. New helpers
|
||||||
|
`dl-rename-anon-term`, `dl-rename-anon-lit`, `dl-make-anon-renamer`,
|
||||||
|
`dl-rename-anon-rule` in db.sx; eval.sx's dl-query renames the goal
|
||||||
|
before search and projects only user-named vars (`_` is filtered
|
||||||
|
out of the projection list). The "underscore in head" test now
|
||||||
|
correctly rejects `(p X _) :- q(X).` — after renaming, the head's
|
||||||
|
fresh anon var has no body binder. Two new eval tests verify
|
||||||
|
rule-level and goal-level independence. 155/155 expected.
|
||||||
|
|
||||||
- 2026-05-08 — Phase 5c perf: indexed `dl-find-bindings`. Replaced
|
- 2026-05-08 — Phase 5c perf: indexed `dl-find-bindings`. Replaced
|
||||||
the recursive `(rest lits)` walk with `dl-fb-aux lits db subst i n`
|
the recursive `(rest lits)` walk with `dl-fb-aux lits db subst i n`
|
||||||
using `nth lits i`. Eliminates O(N²) list-copy per body of length
|
using `nth lits i`. Eliminates O(N²) list-copy per body of length
|
||||||
|
|||||||
Reference in New Issue
Block a user