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:
@@ -173,9 +173,18 @@
|
||||
"unsafe — empty body"
|
||||
(dl-et-throws? (fn () (dl-program "p(X) :- .")))
|
||||
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!
|
||||
"underscore var ok"
|
||||
"underscore in head — unsafe"
|
||||
(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)
|
||||
(dl-et-test!
|
||||
"var only in head — unsafe"
|
||||
@@ -192,7 +201,21 @@
|
||||
()
|
||||
(dl-program
|
||||
"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
|
||||
dl-eval-tests-run!
|
||||
|
||||
Reference in New Issue
Block a user