prolog: Hyperscript bridge (+19)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 51s

pl-hs-query, pl-hs-predicate/1,2,3, pl-hs-install in hs-bridge.sx.
No parser/compiler changes: Hyperscript already compiles
`when allowed(user, action)` to (allowed user action).
Total 590/590.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-06 08:30:46 +00:00
parent ef736112ef
commit f07b6e497e
6 changed files with 249 additions and 8 deletions

View File

@@ -44,6 +44,7 @@ SUITES=(
"compiler:lib/prolog/tests/compiler.sx:pl-compiler-tests-run!"
"cross_validate:lib/prolog/tests/cross_validate.sx:pl-cross-validate-tests-run!"
"integration:lib/prolog/tests/integration.sx:pl-integration-tests-run!"
"hs_bridge:lib/prolog/tests/hs_bridge.sx:pl-hs-bridge-tests-run!"
)
SCRIPT='(epoch 1)
@@ -51,7 +52,8 @@ SCRIPT='(epoch 1)
(load "lib/prolog/parser.sx")
(load "lib/prolog/runtime.sx")
(load "lib/prolog/query.sx")
(load "lib/prolog/compiler.sx")'
(load "lib/prolog/compiler.sx")
(load "lib/prolog/hs-bridge.sx")'
for entry in "${SUITES[@]}"; do
IFS=: read -r _ file _ <<< "$entry"
SCRIPT+=$'\n(load "'"$file"$'")'

72
lib/prolog/hs-bridge.sx Normal file
View File

@@ -0,0 +1,72 @@
;; lib/prolog/hs-bridge.sx — Prolog↔Hyperscript bridge
;;
;; Creates SX functions backed by a Prolog DB, callable directly from
;; Hyperscript DSL conditions. No parser/compiler changes needed:
;; when allowed(user, action) then …
;; compiles to (allowed user action) — a plain SX call.
;;
;; Setup:
;; (define pl-db (pl-load "role(alice,admin). permission(admin,edit). allowed(U,A) :- role(U,R), permission(R,A)."))
;; (define allowed (pl-hs-predicate/2 pl-db "allowed"))
;;
;; Requires tokenizer.sx, parser.sx, runtime.sx, query.sx loaded first.
;; Test whether a ground Prolog goal succeeds against db.
;; Returns true/false (not a solution dict).
(define
pl-hs-query
(fn (db goal-str) (not (nil? (pl-query-one db goal-str)))))
;; Build a Prolog goal string from a predicate name and arg list.
;; SX values: strings/keywords (already strings in SX) pass through;
;; numbers are stringified via str.
(define
pl-hs-build-goal
(fn
(pred-name args)
(str pred-name "(" (join ", " (map (fn (a) (str a)) args)) ")")))
;; Return a 1-arg SX function that succeeds iff pred(a) holds in db.
(define
pl-hs-predicate/1
(fn
(db pred-name)
(fn (a) (pl-hs-query db (pl-hs-build-goal pred-name (list a))))))
;; Return a 2-arg SX function that succeeds iff pred(a, b) holds in db.
(define
pl-hs-predicate/2
(fn
(db pred-name)
(fn (a b) (pl-hs-query db (pl-hs-build-goal pred-name (list a b))))))
;; Return a 3-arg SX function that succeeds iff pred(a, b, c) holds in db.
(define
pl-hs-predicate/3
(fn
(db pred-name)
(fn (a b c) (pl-hs-query db (pl-hs-build-goal pred-name (list a b c))))))
;; Install every predicate in install-list as a named def in the caller's
;; environment. install-list: list of (name arity) pairs.
;; Returns a dict {name → fn} for the caller to destructure.
(define
pl-hs-install
(fn
(db install-list)
(reduce
(fn
(acc entry)
(let
((pred-name (first entry)) (arity (nth entry 1)))
(dict-set!
acc
pred-name
(cond
((= arity 1) (pl-hs-predicate/1 db pred-name))
((= arity 2) (pl-hs-predicate/2 db pred-name))
((= arity 3) (pl-hs-predicate/3 db pred-name))
(true (fn (a b) false))))
acc))
{}
install-list)))

View File

@@ -1,7 +1,7 @@
{
"total_passed": 571,
"total_passed": 590,
"total_failed": 0,
"total": 571,
"suites": {"parse":{"passed":25,"total":25,"failed":0},"unify":{"passed":47,"total":47,"failed":0},"clausedb":{"passed":14,"total":14,"failed":0},"solve":{"passed":62,"total":62,"failed":0},"operators":{"passed":19,"total":19,"failed":0},"dynamic":{"passed":11,"total":11,"failed":0},"findall":{"passed":11,"total":11,"failed":0},"term_inspect":{"passed":14,"total":14,"failed":0},"append":{"passed":6,"total":6,"failed":0},"reverse":{"passed":6,"total":6,"failed":0},"member":{"passed":7,"total":7,"failed":0},"nqueens":{"passed":6,"total":6,"failed":0},"family":{"passed":10,"total":10,"failed":0},"atoms":{"passed":34,"total":34,"failed":0},"query_api":{"passed":16,"total":16,"failed":0},"iso_predicates":{"passed":29,"total":29,"failed":0},"meta_predicates":{"passed":25,"total":25,"failed":0},"list_predicates":{"passed":33,"total":33,"failed":0},"meta_call":{"passed":15,"total":15,"failed":0},"set_predicates":{"passed":15,"total":15,"failed":0},"char_predicates":{"passed":27,"total":27,"failed":0},"io_predicates":{"passed":24,"total":24,"failed":0},"assert_rules":{"passed":15,"total":15,"failed":0},"string_agg":{"passed":25,"total":25,"failed":0},"advanced":{"passed":21,"total":21,"failed":0},"compiler":{"passed":17,"total":17,"failed":0},"cross_validate":{"passed":17,"total":17,"failed":0},"integration":{"passed":20,"total":20,"failed":0}},
"generated": "2026-05-05T20:36:53+00:00"
"total": 590,
"suites": {"parse":{"passed":25,"total":25,"failed":0},"unify":{"passed":47,"total":47,"failed":0},"clausedb":{"passed":14,"total":14,"failed":0},"solve":{"passed":62,"total":62,"failed":0},"operators":{"passed":19,"total":19,"failed":0},"dynamic":{"passed":11,"total":11,"failed":0},"findall":{"passed":11,"total":11,"failed":0},"term_inspect":{"passed":14,"total":14,"failed":0},"append":{"passed":6,"total":6,"failed":0},"reverse":{"passed":6,"total":6,"failed":0},"member":{"passed":7,"total":7,"failed":0},"nqueens":{"passed":6,"total":6,"failed":0},"family":{"passed":10,"total":10,"failed":0},"atoms":{"passed":34,"total":34,"failed":0},"query_api":{"passed":16,"total":16,"failed":0},"iso_predicates":{"passed":29,"total":29,"failed":0},"meta_predicates":{"passed":25,"total":25,"failed":0},"list_predicates":{"passed":33,"total":33,"failed":0},"meta_call":{"passed":15,"total":15,"failed":0},"set_predicates":{"passed":15,"total":15,"failed":0},"char_predicates":{"passed":27,"total":27,"failed":0},"io_predicates":{"passed":24,"total":24,"failed":0},"assert_rules":{"passed":15,"total":15,"failed":0},"string_agg":{"passed":25,"total":25,"failed":0},"advanced":{"passed":21,"total":21,"failed":0},"compiler":{"passed":17,"total":17,"failed":0},"cross_validate":{"passed":17,"total":17,"failed":0},"integration":{"passed":20,"total":20,"failed":0},"hs_bridge":{"passed":19,"total":19,"failed":0}},
"generated": "2026-05-06T08:29:09+00:00"
}

View File

@@ -1,7 +1,7 @@
# Prolog scoreboard
**571 / 571 passing** (0 failure(s)).
Generated 2026-05-05T20:36:53+00:00.
**590 / 590 passing** (0 failure(s)).
Generated 2026-05-06T08:29:09+00:00.
| Suite | Passed | Total | Status |
|-------|--------|-------|--------|
@@ -33,6 +33,7 @@ Generated 2026-05-05T20:36:53+00:00.
| compiler | 17 | 17 | ok |
| cross_validate | 17 | 17 | ok |
| integration | 20 | 20 | ok |
| hs_bridge | 19 | 19 | ok |
Run `bash lib/prolog/conformance.sh` to refresh. Override the binary
with `SX_SERVER=path/to/sx_server.exe bash …`.

View File

@@ -0,0 +1,165 @@
;; lib/prolog/tests/hs_bridge.sx — tests for Prolog↔Hyperscript bridge
;;
;; Verifies pl-hs-query, pl-hs-predicate/N, and pl-hs-install.
;; Also demonstrates the end-to-end DSL pattern:
;; (define allowed (pl-hs-predicate/2 db "allowed"))
;; → (allowed "alice" "edit") is what Hyperscript compiles
;; `when allowed(alice, edit)` to.
(define pl-hsb-test-count 0)
(define pl-hsb-test-pass 0)
(define pl-hsb-test-fail 0)
(define pl-hsb-test-failures (list))
(define
pl-hsb-test!
(fn
(name got expected)
(begin
(set! pl-hsb-test-count (+ pl-hsb-test-count 1))
(if
(= got expected)
(set! pl-hsb-test-pass (+ pl-hsb-test-pass 1))
(begin
(set! pl-hsb-test-fail (+ pl-hsb-test-fail 1))
(append!
pl-hsb-test-failures
(str name "\n expected: " expected "\n got: " got)))))))
;; ── shared KB ──
(define
pl-hsb-perm-src
"role(alice, admin). role(bob, editor). role(charlie, viewer). permission(admin, read). permission(admin, write). permission(admin, delete). permission(editor, read). permission(editor, write). permission(viewer, read). allowed(U, A) :- role(U, R), permission(R, A).")
(define pl-hsb-db (pl-load pl-hsb-perm-src))
;; ── pl-hs-query ──
(pl-hsb-test!
"pl-hs-query: ground fact succeeds"
(pl-hs-query pl-hsb-db "role(alice, admin)")
true)
(pl-hsb-test!
"pl-hs-query: absent fact fails"
(pl-hs-query pl-hsb-db "role(alice, viewer)")
false)
(pl-hsb-test!
"pl-hs-query: rule derivation succeeds"
(pl-hs-query pl-hsb-db "allowed(alice, delete)")
true)
(pl-hsb-test!
"pl-hs-query: rule derivation fails"
(pl-hs-query pl-hsb-db "allowed(charlie, delete)")
false)
(pl-hsb-test!
"pl-hs-query: arithmetic goal"
(pl-hs-query pl-hsb-db "X is 3 + 4, X = 7")
true)
;; ── pl-hs-predicate/2 ──
(define pl-hsb-allowed (pl-hs-predicate/2 pl-hsb-db "allowed"))
(pl-hsb-test!
"predicate/2: alice can read"
(pl-hsb-allowed "alice" "read")
true)
(pl-hsb-test!
"predicate/2: alice can delete"
(pl-hsb-allowed "alice" "delete")
true)
(pl-hsb-test!
"predicate/2: charlie cannot write"
(pl-hsb-allowed "charlie" "write")
false)
(pl-hsb-test!
"predicate/2: bob can write"
(pl-hsb-allowed "bob" "write")
true)
(pl-hsb-test!
"predicate/2: unknown user fails"
(pl-hsb-allowed "eve" "read")
false)
;; ── DSL simulation ──
;; Hyperscript compiles `when allowed(user, action) then …`
;; to `(allowed user action)` — a direct SX function call.
;; Here we verify that pattern works end-to-end.
(define pl-hsb-user "alice")
(define pl-hsb-action "write")
(pl-hsb-test!
"DSL simulation: (allowed user action) true path"
(pl-hsb-allowed pl-hsb-user pl-hsb-action)
true)
(define pl-hsb-user2 "charlie")
(pl-hsb-test!
"DSL simulation: (allowed user action) false path"
(pl-hsb-allowed pl-hsb-user2 pl-hsb-action)
false)
;; ── pl-hs-predicate/1 ──
(define pl-hsb-viewer-src "color(red). color(green). color(blue).")
(define pl-hsb-color-db (pl-load pl-hsb-viewer-src))
(define pl-hsb-color? (pl-hs-predicate/1 pl-hsb-color-db "color"))
(pl-hsb-test! "predicate/1: color(red) succeeds" (pl-hsb-color? "red") true)
(pl-hsb-test!
"predicate/1: color(purple) fails"
(pl-hsb-color? "purple")
false)
;; ── pl-hs-predicate/3 ──
(define pl-hsb-3ary-src "between_vals(X, Lo, Hi) :- X >= Lo, X =< Hi.")
(define pl-hsb-3ary-db (pl-load pl-hsb-3ary-src))
(define pl-hsb-in-range? (pl-hs-predicate/3 pl-hsb-3ary-db "between_vals"))
(pl-hsb-test!
"predicate/3: 5 in range [1,10]"
(pl-hsb-in-range? "5" "1" "10")
true)
(pl-hsb-test!
"predicate/3: 15 not in range [1,10]"
(pl-hsb-in-range? "15" "1" "10")
false)
;; ── pl-hs-install ──
(define
pl-hsb-installed
(pl-hs-install
pl-hsb-db
(list (list "allowed" 2) (list "role" 2) (list "permission" 2))))
(pl-hsb-test!
"pl-hs-install: returns dict with allowed key"
(not (nil? (dict-get pl-hsb-installed "allowed")))
true)
(pl-hsb-test!
"pl-hs-install: installed allowed fn works"
((dict-get pl-hsb-installed "allowed") "alice" "delete")
true)
(pl-hsb-test!
"pl-hs-install: installed role fn works"
((dict-get pl-hsb-installed "role") "bob" "editor")
true)
(define pl-hs-bridge-tests-run! (fn () {:failed pl-hsb-test-fail :passed pl-hsb-test-pass :total pl-hsb-test-count :failures pl-hsb-test-failures}))