diff --git a/lib/prolog/runtime.sx b/lib/prolog/runtime.sx index 596f051f..816bc84a 100644 --- a/lib/prolog/runtime.sx +++ b/lib/prolog/runtime.sx @@ -309,6 +309,12 @@ (nth (pl-args g) 1) trail k)) + ((and (pl-compound? g) (= (pl-fun g) "is") (= (len (pl-args g)) 2)) + (pl-solve-eq! + (first (pl-args g)) + (list "num" (pl-eval-arith (nth (pl-args g) 1))) + trail + k)) ((and (pl-compound? g) (= (pl-fun g) ",") (= (len (pl-args g)) 2)) (pl-solve! db @@ -429,6 +435,47 @@ (str (pl-fun w) "(" (pl-format-args (pl-args w)) ")")) (true (str w)))))) +(define + pl-eval-arith + (fn + (t) + (let + ((w (pl-walk-deep t))) + (cond + ((pl-num? w) (pl-num-val w)) + ((pl-compound? w) + (let + ((f (pl-fun w)) (args (pl-args w))) + (cond + ((and (= f "+") (= (len args) 2)) + (+ + (pl-eval-arith (first args)) + (pl-eval-arith (nth args 1)))) + ((and (= f "-") (= (len args) 2)) + (- + (pl-eval-arith (first args)) + (pl-eval-arith (nth args 1)))) + ((and (= f "-") (= (len args) 1)) + (- 0 (pl-eval-arith (first args)))) + ((and (= f "*") (= (len args) 2)) + (* + (pl-eval-arith (first args)) + (pl-eval-arith (nth args 1)))) + ((and (= f "/") (= (len args) 2)) + (/ + (pl-eval-arith (first args)) + (pl-eval-arith (nth args 1)))) + ((and (= f "mod") (= (len args) 2)) + (mod + (pl-eval-arith (first args)) + (pl-eval-arith (nth args 1)))) + ((and (= f "abs") (= (len args) 1)) + (let + ((v (pl-eval-arith (first args)))) + (cond ((< v 0) (- 0 v)) (true v)))) + (true 0)))) + (true 0))))) + (define pl-solve-not-eq! (fn diff --git a/lib/prolog/tests/solve.sx b/lib/prolog/tests/solve.sx index 59257757..f043c729 100644 --- a/lib/prolog/tests/solve.sx +++ b/lib/prolog/tests/solve.sx @@ -495,4 +495,124 @@ pl-output-buffer) "hi\n") +(define pl-s-env-arith1 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, 42)" pl-s-env-arith1) + (pl-mk-trail)) + +(pl-s-test! + "is(X, 42) binds X to 42" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith1 "X"))) + 42) + +(define pl-s-env-arith2 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, +(2, 3))" pl-s-env-arith2) + (pl-mk-trail)) + +(pl-s-test! + "is(X, +(2, 3)) binds X to 5" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith2 "X"))) + 5) + +(define pl-s-env-arith3 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, *(2, 3))" pl-s-env-arith3) + (pl-mk-trail)) + +(pl-s-test! + "is(X, *(2, 3)) binds X to 6" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith3 "X"))) + 6) + +(define pl-s-env-arith4 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, -(10, 3))" pl-s-env-arith4) + (pl-mk-trail)) + +(pl-s-test! + "is(X, -(10, 3)) binds X to 7" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith4 "X"))) + 7) + +(define pl-s-env-arith5 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, /(10, 2))" pl-s-env-arith5) + (pl-mk-trail)) + +(pl-s-test! + "is(X, /(10, 2)) binds X to 5" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith5 "X"))) + 5) + +(define pl-s-env-arith6 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, mod(10, 3))" pl-s-env-arith6) + (pl-mk-trail)) + +(pl-s-test! + "is(X, mod(10, 3)) binds X to 1" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith6 "X"))) + 1) + +(define pl-s-env-arith7 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, abs(-(0, 5)))" pl-s-env-arith7) + (pl-mk-trail)) + +(pl-s-test! + "is(X, abs(-(0, 5))) binds X to 5" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith7 "X"))) + 5) + +(define pl-s-env-arith8 {}) + +(pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(X, +(2, *(3, 4)))" pl-s-env-arith8) + (pl-mk-trail)) + +(pl-s-test! + "is(X, +(2, *(3, 4))) binds X to 14 (nested)" + (pl-num-val (pl-walk-deep (dict-get pl-s-env-arith8 "X"))) + 14) + +(pl-s-test! + "is(5, +(2, 3)) succeeds (LHS num matches)" + (pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(5, +(2, 3))" {}) + (pl-mk-trail)) + true) + +(pl-s-test! + "is(6, +(2, 3)) fails (LHS num mismatch)" + (pl-solve-once! + pl-s-empty-db + (pl-s-goal "is(6, +(2, 3))" {}) + (pl-mk-trail)) + false) + +(pl-s-test! + "is propagates bound vars on RHS" + (pl-solve-once! + pl-s-empty-db + (pl-s-goal "=(Y, 4), is(X, +(Y, 1)), =(X, 5)" {}) + (pl-mk-trail)) + true) + (define pl-solve-tests-run! (fn () {:failed pl-s-test-fail :passed pl-s-test-pass :total pl-s-test-count :failures pl-s-test-failures})) diff --git a/plans/prolog-on-sx.md b/plans/prolog-on-sx.md index 336d7e39..5cf35264 100644 --- a/plans/prolog-on-sx.md +++ b/plans/prolog-on-sx.md @@ -54,7 +54,7 @@ Representation choices (finalise in phase 1, document here): - [x] Solver: DFS with choice points backed by delimited continuations (`lib/callcc.sx`). On goal entry, capture; per matching clause, unify head + recurse body; on failure, undo trail, try next — first cut: trail-based undo + CPS k (no shift/reset yet, per briefing gotcha). Built-ins so far: `true/0`, `fail/0`, `=/2`, `,/2`. Refactor to delimited conts later. - [x] Cut (`!`): cut barrier at current choice-point frame; collapse all up to barrier — two-cut-box scheme: each `pl-solve-user!` creates a fresh inner-cut-box (set by `!` in this predicate's body) AND snapshots the outer-cut-box state on entry. After body fails, abandon clause alternatives if (a) inner was set or (b) outer transitioned false→true during this call. Lets post-cut goals backtrack normally while blocking pre-cut alternatives. 6 cut tests cover bare cut, clause-commit, choice-commit, cut+fail, post-cut backtracking, nested-cut isolation. - [x] Built-ins: `=/2`, `\\=/2`, `true/0`, `fail/0`, `!/0`, `,/2`, `;/2`, `->/2` inside `;`, `call/1`, `write/1`, `nl/0` — all 11 done. `write/1` and `nl/0` use a global `pl-output-buffer` string + `pl-output-clear!` for testability; `pl-format-term` walks deep then renders atoms/nums/strs/compounds/vars (var → `_`). Note: cut-transparency via `;` not testable yet without operator support — `;(,(a,!), b)` parser-rejects because `,` is body-operator-only; revisit in phase 4. -- [ ] Arithmetic `is/2` with `+ - * / mod abs` +- [x] Arithmetic `is/2` with `+ - * / mod abs` — `pl-eval-arith` walks deep, recurses on compounds, dispatches on functor; binary `+ - * / mod`, binary AND unary `-`, unary `abs`. `is/2` evaluates RHS, wraps as `("num" v)`, unifies via `pl-solve-eq!`. 11 tests cover each op + nested + ground LHS match/mismatch + bound-var-on-RHS chain. - [ ] Classic programs in `lib/prolog/tests/programs/`: - [ ] `append.pl` — list append (with backtracking) - [ ] `reverse.pl` — naive reverse @@ -88,6 +88,7 @@ Representation choices (finalise in phase 1, document here): _Newest first. Agent appends on every commit._ +- 2026-04-25 — `is/2` arithmetic landed. `pl-eval-arith` recursively evaluates ground RHS expressions (binary `+ - * /`, `mod`; binary+unary `-`; unary `abs`); `is/2` wraps the value as `("num" v)` and unifies via `pl-solve-eq!`, so it works in all three modes — bind unbound LHS, check ground LHS for equality, propagate from earlier var bindings on RHS. 11 tests, total 148 (+11). Without operator support, expressions must be written prefix: `is(X, +(2, *(3, 4)))`. - 2026-04-25 — `write/1` + `nl/0` landed using global string buffer (`pl-output-buffer` + `pl-output-clear!` + `pl-output-write!`). `pl-format-term` walks deep + dispatches on atom/num/str/compound/var; `pl-format-args` recursively comma-joins. 7 new tests cover atom/num/compound formatting, conjunction order, var-walk, and `nl`. Built-ins box (`=/2`, `\\=/2`, `true/0`, `fail/0`, `!/0`, `,/2`, `;/2`, `->/2`, `call/1`, `write/1`, `nl/0`) now ticked. Total 137 (+7). - 2026-04-25 — `->/2` if-then-else landed (both `;(->(C,T), E)` and standalone `->(C, T)` ≡ `(C -> T ; fail)`). `pl-solve-or!` now special-cases `->` in left arg → `pl-solve-if-then-else!`. Cond runs in a fresh local cut-box (ISO opacity for cut inside cond). Then-branch can backtrack, else-branch can backtrack, but cond commits to first solution. 9 new tests covering both forms, both branches, binding visibility, cond-commit, then-backtrack, else-backtrack. Total 130 (+9). - 2026-04-25 — Built-ins `\=/2`, `;/2`, `call/1` landed. `pl-solve-not-eq!` (try unify, always undo, succeed iff unify failed). `pl-solve-or!` (try left, on failure check cut and only try right if not cut). `call/1` opens a fresh inner cut-box (ISO opacity: cut inside `call(G)` commits G, not caller). 11 new tests in `tests/solve.sx` cover atoms+vars for `\=`, both branches + count for `;`, and `call/1` against atoms / compounds / bound goal vars. Total 121 (+11). Box not yet ticked — `->/2`, `write/1`, `nl/0` still pending.