datalog: reject compound terms in fact / rule-head args
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s

Datalog has no function symbols in argument positions, but the
existing dl-add-fact! / dl-add-rule! validators only checked that
literals were ground (no free variables). A compound like `+(1, 2)`
contains no variables, so:

  p(+(1, 2)).
  => stored as the unreduced tuple `(p (+ 1 2))`

  double(*(X, 2)) :- n(X).  n(3).
  => saturates `double((* 3 2))` instead of `double(6)`

Added dl-simple-term? (number / string / symbol) and an
args-simple? walker, used by:

  - dl-add-fact!: all args must be simple terms
  - dl-add-rule!: rule head args must be simple terms (variables
    are symbols, so they pass)

Compounds remain legal in body literals where they encode `is` /
arithmetic / aggregate sub-goals. Error messages name the offending
literal and point the user at the body-only mechanism.

2 new regression tests; conformance 271/271.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-11 08:44:30 +00:00
parent 7a94a47e26
commit 6bae94bae1
5 changed files with 63 additions and 7 deletions

View File

@@ -251,6 +251,24 @@
(dl-index-add! db rel-key lit)
true)))))))
;; A simple term — number, string, or symbol — i.e. anything legal
;; as an EDB fact arg. Compound (list) args belong only in body
;; literals where they encode arithmetic / aggregate sub-goals.
(define
dl-simple-term?
(fn
(term)
(or (number? term) (string? term) (symbol? term))))
(define
dl-args-simple?
(fn
(lit i n)
(cond
((>= i n) true)
((not (dl-simple-term? (nth lit i))) false)
(else (dl-args-simple? lit (+ i 1) n)))))
(define
dl-add-fact!
(fn
@@ -261,6 +279,11 @@
((dl-reserved-rel? (dl-rel-name lit))
(error (str "dl-add-fact!: '" (dl-rel-name lit)
"' is a reserved name (built-in / aggregate / negation)")))
((not (dl-args-simple? lit 1 (len lit)))
(error (str "dl-add-fact!: fact args must be numbers, strings, "
"or symbols — compound args (e.g. arithmetic "
"expressions) are body-only and aren't evaluated "
"in fact position. got " lit)))
((not (dl-ground? lit (dl-empty-subst)))
(error (str "dl-add-fact!: expected ground literal, got " lit)))
(else
@@ -373,6 +396,11 @@
(error (str "dl-add-rule!: head must be a non-empty list "
"starting with a relation-name symbol, got "
(get rule :head))))
((not (dl-args-simple? (get rule :head) 1 (len (get rule :head))))
(error (str "dl-add-rule!: rule head args must be variables or "
"constants — compound terms (e.g. `(*(X, 2))`) are "
"not legal in head position; introduce an `is`-bound "
"intermediate in the body. got " (get rule :head))))
((not (list? (if (has-key? rule :body) (get rule :body) (list))))
(error (str "dl-add-rule!: body must be a list of literals, got "
(get rule :body))))