298 Commits

Author SHA1 Message Date
0231bb46a6 ocaml: phase 5.1 trapping_rain.ml baseline (LeetCode trapped water = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Classic trapped-rain-water two-pass DP:

  left_max[i]  = max(heights[0..i])    (forward sweep)
  right_max[i] = max(heights[i..n-1])  (downto sweep)
  water        = sum over i of (min(left_max[i], right_max[i])
                                  - heights[i])

For [0; 1; 0; 2; 1; 0; 1; 3; 2; 1; 2; 1]: water = 6.

Tests dual sweep (forward + downto), array of running maxes,
inline-if rhs of <- for running-max update (uses iter-236 fix
for <- accepting if/match RHS).

203 baseline programs total.
2026-05-11 05:54:39 +00:00
fed07059a3 ocaml: phase 5.1 gas_station.ml baseline (circular tour start = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Classic O(n) greedy gas-station algorithm:

  walk once, tracking
    total = sum of (gas[i] - cost[i])  -- if negative, no answer
    curr  = running tank since start   -- on negative, advance
                                          start past i+1 and reset

  if total < 0 then -1 else start

For gas = [1;2;3;4;5], cost = [3;4;5;1;2], unique start = 3.

Tests `total` + `curr` parallel accumulators, reset-on-failure
pattern.

202 baseline programs total.
2026-05-11 05:44:38 +00:00
c8327823ee ocaml: phase 5.1 min_jumps.ml baseline (greedy BFS-like min jumps = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Greedy BFS-frontier style — track the farthest reach within the
current jump's reachable range, and bump the jump counter when i
runs into the current frontier:

  while !i < n - 1 do
    farthest := max(farthest, i + arr.(i));
    if !i = !cur_end then begin
      jumps := !jumps + 1;
      cur_end := !farthest
    end;
    i := !i + 1
  done

For [2; 3; 1; 1; 2; 4; 2; 0; 1; 1] (n = 10), the optimal jump
sequence 0 -> 1 -> 4 -> 5 -> 9 uses 4 jumps.

Tests greedy-with-frontier pattern, three parallel refs
(jumps, cur_end, farthest), mixed for-style index loop using ref.

201 baseline programs total.
2026-05-11 05:34:46 +00:00
fad81e0b0c ocaml: phase 5.1 combinations.ml baseline (C(9, 4) = 126)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Pascal-recursion combination enumerator:

  let rec choose k xs =
    if k = 0 then [[]]
    else match xs with
      | [] -> []
      | h :: rest ->
        List.map (fun c -> h :: c) (choose (k - 1) rest)
        @ choose k rest

  C(9, 4) = |choose 4 [1; ...; 9]| = 126

Tests pure-functional enumeration with List.map + closure over h,
@ append, [] | h :: rest pattern match on shrinking input.

200 baseline programs total -- milestone.
2026-05-11 05:25:03 +00:00
3ccce58e0a ocaml: phase 5.1 unique_paths_obs.ml baseline (4x4 grid w/ obstacles = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Standard 2D unique-paths DP with obstacles gating each cell:

  dp[i][j] = if grid[i][j] = 1 then 0
             else dp[i-1][j] + dp[i][j-1]

Grid (1s are obstacles):
  . . . .
  . # . .
  . . . #
  # . . .

dp:
  1 1 1 1
  1 0 1 2
  1 1 2 0
  0 1 3 3

Returns dp[3][3] = 3.

Complements grid_paths.ml (no-obstacles version) — same DP shape
but obstacles zero out cells and reshape the path count.

199 baseline programs total.
2026-05-11 05:14:47 +00:00
8ab2f80615 ocaml: phase 5.1 daily_temperatures.ml baseline (sum of waits = 10)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Monotonic decreasing stack — for each day i, pop entries from
the stack whose temperature is strictly less than today's; their
answer is (i - popped_index).

  temps  = [73; 74; 75; 71; 69; 72; 76; 73]
  answer = [ 1;  1;  4;  2;  1;  1;  0;  0]
  sum    = 10

Complementary to next_greater.ml (iter 256) — same monotonic-stack
skeleton but stores the distance to the next greater element
rather than its value.

Tests `match !stack with | top :: rest when …` pattern with
guard inside a while-cont-flag loop.

198 baseline programs total.
2026-05-11 05:04:37 +00:00
230f803abb ocaml: phase 5.1 count_bits.ml baseline (sum popcount 0..100 = 319)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
DP recurrence for popcount that avoids host bitwise operations:

  result[i] = result[i / 2] + (i mod 2)

Drops the low bit (i / 2 stands in for i lsr 1) and adds it back
if it was 1 (i mod 2 stands in for i land 1).

  sum over 0..100 of popcount(i) = 319

Tests pure-arithmetic popcount, accumulating ref + DP array,
classic look-back to half-index pattern.

197 baseline programs total.
2026-05-11 04:54:36 +00:00
b240408a4c ocaml: phase 5.1 bs_rotated.ml baseline (rotated array search, encoded -66)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Binary search in a rotated sorted array. Standard sorted-half
test at each step:

  if arr.(lo) <= arr.(mid) then
    left half [lo, mid] is sorted -> check whether target is in it
  else
    right half [mid, hi] is sorted -> check whether target is in it

For [4; 5; 6; 7; 0; 1; 2]:
  search 0 -> index 4
  search 7 -> index 3
  search 3 -> -1 (absent)

Encoded fingerprint: 4 + 3*10 + (-1)*100 = -66.

First baseline returning a negative top-level value; the runner
uses literal grep -qF so leading minus parses fine.

196 baseline programs total.
2026-05-11 04:44:37 +00:00
67ece98ba1 ocaml: phase 5.1 task_scheduler.ml baseline ("AAABBC" cooldown 2 -> 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Task-scheduler closed-form min total intervals:

  m = max letter frequency
  k = number of letters tied at frequency m
  answer = max((m - 1) * (n + 1) + k, total_tasks)

For "AAABBC" with cooldown n = 2:
  freq A = 3, freq B = 2, freq C = 1 -> m = 3, k = 1
  formula = (3 - 1) * (2 + 1) + 1 = 7
  total tasks = 6
  answer = 7

Witness schedule: A, B, C, A, B, idle, A.

Tests String.iter with side-effecting count update via
Char.code arithmetic, fixed-size 26-bucket histogram.

195 baseline programs total.
2026-05-11 04:34:40 +00:00
33be068c01 ocaml: phase 5.1 min_subarr_target.ml baseline (min subarray sum >= 7 = 2)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Classic two-pointer / sliding window: expand right, then shrink
left while the window still satisfies the >= constraint, recording
the smallest valid length.

  for r = 0 to n - 1 do
    sum := !sum + arr.(r);
    while !sum >= target do
      ... record (r - !l + 1) if smaller ...
      sum := !sum - arr.(!l);
      l := !l + 1
    done
  done

For [2; 3; 1; 2; 4; 3], target 7 -> window [4, 3] of length 2.

Sentinel n+1 marks "not found"; final guard reduces to 0.

Tests for + inner while shrinking loop, ref-tracked sum updated
on both expansion and contraction.

194 baseline programs total.
2026-05-11 04:24:29 +00:00
bf468e5ec3 ocaml: phase 5.1 min_meeting_rooms.ml baseline (8 meetings, min 4 rooms)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Sweep-line algorithm via separately-sorted starts / ends arrays:

  while i < n do
    if starts[i] < ends[j] then begin busy++; rooms = max; i++ end
    else begin busy--; j++ end
  done

  intervals: (0,30) (5,10) (15,20) (10,25) (5,12)
             (20,35) (0,5)  (8,18)

At time 8, meetings (0,30), (5,10), (5,12), (8,18) are all active
simultaneously -> answer = 4.

Tests local helper bound via let (`let bubble a = ...`) for
in-place sort, dual-pointer sweep on parallel ordered event streams.

193 baseline programs total.
2026-05-11 04:14:33 +00:00
90ba37ecc8 ocaml: phase 5.1 activity_select.ml baseline (greedy non-overlap = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Activity selection by earliest end time. Manual bubble sort over
(start, end) tuple array, then sweep accepting whenever the next
start >= last_end.

  intervals: (1,4) (3,5) (0,6) (5,7) (3,8) (5,9) (6,10) (8,11)
             (8,12) (2,13) (12,14)

  selection: (1,4) -> (5,7) -> (8,11) -> (12,14)  = 4 activities

Tests double-loop bubble sort on tuple array with let-pattern
destructure for swap-key extraction, in-place swap of tuple cells.

192 baseline programs total.
2026-05-11 04:04:42 +00:00
3f00e62577 ocaml: phase 5.1 count_paths_dag.ml baseline (source-to-sink paths = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Count source-to-sink paths in a DAG via Kahn's topological sort
plus accumulation:

  paths[source] = 1
  for u in topological order:
    for v in adj[u]: paths[v] += paths[u]

Same 6-node DAG as topo_sort.ml:
  0 -> {1, 2}    1 -> {3}    2 -> {3, 4}    3 -> {5}    4 -> {5}

The three witnesses 0 -> 5:
  0 -> 1 -> 3 -> 5
  0 -> 2 -> 3 -> 5
  0 -> 2 -> 4 -> 5

Tests Queue-driven Kahn order + List.rev to recover topological
order, module-level mutable arrays (in_deg, paths), accumulation
in topological traversal.

191 baseline programs total.
2026-05-11 03:54:52 +00:00
97a29c6bac ocaml: phase 5.1 stock_two.ml baseline (best of 2 transactions = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Two-pass partition DP for max profit with at most 2 transactions:

  left[i]  = max single-trans profit in prices[0..i]
              (forward scan tracking running min)
  right[i] = max single-trans profit in prices[i..n-1]
              (backward scan tracking running max)
  answer   = max over i of (left[i] + right[i])

For [3; 3; 5; 0; 0; 3; 1; 4]:
  optimal partition i = 2:
    left[2]  = sell@5 after buy@3            = 2
    right[2] = sell@4 after buy@0 in [2..7]  = 4
                                       total = 6

Tests parallel forward + backward passes on parallel DP arrays,
mixed ref + array state, for downto + for ascending scans on
the same data.

190 baseline programs total.
2026-05-11 03:44:40 +00:00
73efd229be ocaml: phase 5.1 house_robber.ml baseline (max non-adjacent sum = 22)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Classic House Robber linear DP:

  dp[i] = max(dp[i-2] + houses[i], dp[i-1])

For [2; 7; 9; 3; 1; 5; 8; 6]:

  dp = [2, 7, 11, 11, 12, 16, 20, 22]
  max sum = 22

Optimal pick is {indices 0, 2, 5, 7}: 2 + 9 + 5 + 6 = 22 (all
non-adjacent).

Tests linear DP with early-return edge cases for n=0 and n=1,
inline-if rhs for max-of-two, dual look-back into dp[i-2] and
dp[i-1].

189 baseline programs total.
2026-05-11 03:34:48 +00:00
6d89da9380 ocaml: phase 5.1 interval_overlap.ml baseline (7 intervals, 6 overlapping pairs)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
For each (i, j) pair with i < j, test whether intervals (s1, e1)
and (s2, e2) overlap via the standard `s1 <= e2 && s2 <= e1`.

  intervals: (1,4) (2,5) (7,9) (3,6) (8,10) (11,12) (0,2)

  overlapping pairs:
    (1,4) & (2,5)        (1,4) & (3,6)        (1,4) & (0,2)
    (2,5) & (3,6)        (2,5) & (0,2)
    (7,9) & (8,10)
                                                     = 6

Tests double for-loop with both-side tuple destructure via
`let (s1, e1) = arr.(i) in`, conjunctive comparison, list-to-
array conversion.

188 baseline programs total.
2026-05-11 03:24:45 +00:00
d3340107e6 ocaml: phase 5.1 count_palindromes.ml baseline ("aabaa" -> 9 palindromes)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Expand-around-center linear-time palindrome counting:

  for c = 0 to 2*n - 2 do
    let l = ref (c / 2) in
    let r = ref ((c + 1) / 2) in
    while !l >= 0 && !r < n && s.[!l] = s.[!r] do
      count := !count + 1; l := !l - 1; r := !r + 1
    done
  done

The 2n-1 centers cover both odd (c even -> l = r) and even
(c odd -> l = r - 1) palindromes.

For "aabaa":
  5 singletons + 2 "aa" + 1 "aba" + 1 "aabaa" = 9

Complements lps_dp.ml (longest subsequence) and manacher.ml
(longest substring); this one *counts* all palindromic substrings.

187 baseline programs total.
2026-05-11 03:14:23 +00:00
aaa6020037 ocaml: phase 5.1 count_subarrays_k.ml baseline (sum-k subarrays = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Count contiguous subarrays summing to a target k using a prefix
sum table:

  prefix[i+1] = prefix[i] + arr[i]
  count = |{ (i, j) : 0 <= i < j <= n,  prefix[j] - prefix[i] = k }|

For arr = [1; 1; 1; 2; -1; 3; 1; -2; 4] and k = 3, the seven
witnesses are:

  [1, 1, 1]            (0..2)
  [1, 1, 2, -1]        (1..4)
  [1, 2]               (2..3)
  [2, -1, 3, 1, -2]    (3..7)
  [-1, 3, 1]           (4..6)
  [3]                  (5..5)
  [1, -2, 4]           (6..8)

Negative-valued arrays exercise mixed-sign integer arithmetic.

186 baseline programs total.
2026-05-11 03:04:14 +00:00
8ef24847d3 ocaml: phase 5.1 floyd_cycle.ml baseline (tortoise-hare, mu=0 lam=8 -> 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Floyd's cycle detection on a numeric function f(x) = (2x + 5) mod 17.
Three phases:

  Phase 1: advance slow/fast until collision inside the cycle
            (fast double-steps, slow single-steps)
  Phase 2: restart slow from x0; advance both by 1 until they
            meet — count is mu (length of tail before cycle)
  Phase 3: advance fast around cycle once until it meets slow
            — count is lam (cycle length)

For x0 = 1, the orbit visits 1, 7, 2, 9, 6, 0, 5, 15 then returns
to 1 — pure cycle of length 8, mu = 0, lam = 8. Encoded as
mu*100 + lam = 8.

Tests three sequential while loops sharing ref state,
double-step `fast := f (f !fast)`, meeting-condition flag.

185 baseline programs total.
2026-05-11 02:54:50 +00:00
b3ee88e9bb ocaml: phase 5.1 kth_two.ml baseline (8th smallest of two sorted = 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Two-pointer merge advancing the smaller-head pointer k times,
without materializing the merged array:

  while !count < k do
    let pick_a =
      if !i = m then false        (* a exhausted, take from b *)
      else if !j = n then true    (* b exhausted, take from a *)
      else a.(!i) <= b.(!j)
    in
    if pick_a then ... else ...;
    count := !count + 1
  done

For a = [1;3;5;7;9;11;13], b = [2;4;6;8;10;12]:
  merged order: 1,2,3,4,5,6,7,8,9,10,11,12,13
  8th element = 8.

Tests nested if/else if/else flowing into a bool, dual-ref
two-pointer loop, separate count counter for k-th constraint.

184 baseline programs total.
2026-05-11 02:44:40 +00:00
2c7a1bfc47 ocaml: phase 5.1 permutations_gen.ml baseline (24 perms, 12 with a<b ends)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Recursive permutation generator via fold-pick-recurse:

  let rec permutations xs = match xs with
    | [] -> [[]]
    | _ ->
      List.fold_left (fun acc x ->
        let rest = List.filter (fun y -> y <> x) xs in
        let subs = permutations rest in
        acc @ List.map (fun p -> x :: p) subs
      ) [] xs

For permutations of [1; 2; 3; 4] (24 total), count those whose
first element is less than the last:

  match p with
  | [a; _; _; b] when a < b -> count := !count + 1
  | _ -> ()

By symmetry, exactly half satisfy a < b = 12.

Tests List.filter, recursive fold with append, fixed-length
list pattern [a; _; _; b] with multiple wildcards + when guard.

183 baseline programs total.
2026-05-11 02:34:58 +00:00
047ea62d43 ocaml: phase 5.1 regex_simple.ml baseline (./* matcher, 7/28 match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Recursive regex matcher with Leetcode-style semantics:
  .   matches any single character
  <c>* matches zero or more of <c>

  let rec is_match s i p j =
    if j = String.length p then i = String.length s
    else
      let first = i < String.length s
                  && (p.[j] = '.' || p.[j] = s.[i])
      in
      if j + 1 < String.length p && p.[j+1] = '*' then
           is_match s i p (j + 2)                   (* skip * group *)
        || (first && is_match s (i + 1) p j)        (* consume one  *)
      else
        first && is_match s (i + 1) p (j + 1)

Patterns vs texts:
  .a.b    | aabb axb "" abcd abc aaabbbc x   -> 1 match
  a.*b    | aabb axb "" abcd abc aaabbbc x   -> 2 matches
  x*      | aabb axb "" abcd abc aaabbbc x   -> 2 matches
  a*b*c   | aabb axb "" abcd abc aaabbbc x   -> 2 matches
                                             total = 7

Complements wildcard_match.ml which uses LIKE-style * / ?.

182 baseline programs total.
2026-05-11 02:25:04 +00:00
2726ed9b8a ocaml: phase 5.1 palindrome_part.ml baseline (min cuts "aabba" = 1)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Two-phase palindrome-partition DP for the minimum-cuts variant:

  Phase 1: is_pal[i][j] palindrome table via length-major fill
           (single chars, then pairs, then expand inward).

  Phase 2: cuts[i] = 0 if s[0..i] is itself a palindrome,
                  = min over j of (cuts[j-1] + 1)
                    where s[j..i] is a palindrome.

  min_cut "aabba" = 1   ("a" | "abba")

Tests two sequential 2D DPs sharing the same is_pal matrix,
inline begin/end branches inside the length-major fill, mixed
bool and int 2D arrays.

181 baseline programs total.
2026-05-11 02:14:44 +00:00
6d7df11224 ocaml: phase 5.1 island_count.ml baseline (6x7 grid, 5 components)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Count 4-connected components of 1-cells via DFS flood from every
unvisited 1-cell.

Grid (1s shown as #):
  # # . . . # #
  # . . . # # .
  . . # # . . .
  . # # # . # #
  # . . . . # .
  # # . # # # .

Components:
  {(0,0),(0,1),(1,0)}
  {(0,5),(0,6),(1,4),(1,5)}
  {(2,2),(2,3),(3,1),(3,2),(3,3)}
  {(3,5),(3,6),(4,5),(5,3),(5,4),(5,5)}
  {(4,0),(5,0),(5,1)}
                              -> 5 islands

Complementary to flood_fill.ml (largest component); this counts
total components.

Tests recursive function returning () at early-exit branches,
ordered double-for entry pass triggering one fill per island root.

180 baseline programs total.
2026-05-11 02:04:08 +00:00
8a80bd3923 ocaml: phase 5.1 dp_word_break.ml baseline (4/5 strings segmentable)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Classic word-break DP — for each position i, check whether any
dictionary word ends at i with a prior reachable position:

  dp[i] = exists w in dict with wl <= i and
          dp[i - wl] && s.sub (i - wl) wl = w

Dictionary: apple, pen, pine, pineapple, cats, cat, and, sand, dog
Inputs:
  applepenapple        yes  (apple pen apple)
  pineapplepenapple    yes  (pineapple pen apple)
  catsanddog           yes  (cats and dog)
  catsandog            no   (no segmentation reaches the end)
  applesand            yes  (apple sand)

Tests bool-typed Array, String.sub primitive, nested List.iter
over the dict inside for-loop over end positions, closure capture
of the outer dp.

179 baseline programs total.
2026-05-11 01:53:21 +00:00
609205b551 ocaml: phase 5.1 histogram_area.ml baseline (largest rectangle = 10)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Linear-time stack algorithm for largest rectangle in histogram:

  for i = 0 to n do
    let h = if i = n then 0 else heights.(i) in
    while top-of-stack's height > h do
      pop the top, compute its max-width rectangle:
        width = (no-stack ? i : i - prev_top - 1)
        area  = height * width
      update best
    done;
    if i < n then push i
  done

Sentinel pass at i=n with h=0 flushes the remaining stack.

For [2; 1; 5; 6; 2; 3], bars at indices 2 (h=5) and 3 (h=6) form
a width-2 rectangle of height 5 = 10.

Tests guarded patterns with `when` inside while-cont-flag, nested
`match !stack with | [] -> i | t :: _ -> i - t - 1` for width
computation.

178 baseline programs total.
2026-05-11 01:42:26 +00:00
f9371e7d22 ocaml: phase 5.1 lps_dp.ml baseline (LPS "BBABCBCAB" = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Standard O(n^2) length-major DP for longest palindromic
subsequence:

  dp[i][j] = dp[i+1][j-1] + 2                if s[i] = s[j]
           = max(dp[i+1][j], dp[i][j-1])     otherwise

  lps "BBABCBCAB" = 7   (witness "BABCBAB" etc.)

Complementary to manacher.ml (longest palindromic *substring*,
also length 7 on that input by coincidence) — this is the
subsequence variant which doesn't require contiguity.

Tests length-major fill order, inline if for the length-2 base
case, double-nested for with derived j = i + len - 1.

177 baseline programs total.
2026-05-11 01:32:41 +00:00
7f310a4da7 ocaml: phase 5.1 bs_bounds.ml baseline (lower/upper bound, fingerprint 3211)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
C++-style lower_bound / upper_bound on a sorted array:

  lower_bound — first index >= x  (loop uses arr.(mid) < x)
  upper_bound — first index >  x  (loop uses arr.(mid) <= x)

  a = [|1; 2; 2; 3; 3; 3; 5; 7; 9|]

  upper(x) - lower(x) gives the count of x in a:
    cnt3 = 3   cnt2 = 2   cnt5 = 1   cnt9 = 1   cnt4 = 0

  fingerprint = 3*1000 + 2*100 + 1*10 + 1 + 0 = 3211

Tests parallel while loops with bisection on ref, mixed strict
and non-strict comparison branches, count-via-subtraction idiom.

176 baseline programs total.
2026-05-11 01:22:31 +00:00
6780acd0af ocaml: phase 5.1 distinct_subseq.ml baseline ("rabbit" in "rabbbit" = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Classic distinct-subsequences 2D DP:

  dp[i][j] = dp[i-1][j] + (s[i-1] = t[j-1] ? dp[i-1][j-1] : 0)
  dp[i][0] = 1   (empty t is a subseq of any prefix of s)

  count_subseq "rabbbit" "rabbit" = 3

The three witnesses correspond to which 'b' in "rabbbit" is
dropped (positions 2, 3, or 4 zero-indexed of the run of bs).

Complements subseq_check.ml (just tests presence); this one counts
distinct embeddings.

Tests 2D DP with Array.init n (fun _ -> Array.make m 0), base row
initialization, mixed string + array indexing.

175 baseline programs total.
2026-05-11 01:12:33 +00:00
b771ea306c ocaml: phase 5.1 bracket_match.ml baseline (5/9 balanced strings)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Stack-based multi-bracket parenthesis matching for ( [ { ) ] }.
Non-bracket chars are skipped (treated as content).

Tests:
  ()           yes
  [{()}]       yes
  ({[}])       no  (mismatched closer)
  ""           yes
  ((           no  (unclosed)
  ()[](){}     yes
  (a(b)c)      yes  (a/b/c skipped)
  (()          no
  ])           no
                 5 balanced

Body uses begin/end-wrapped match inside while:

  else if c = ')' || c = ']' || c = '}' then begin
    match !stack with
    | [] -> ok := false
    | top :: rest ->
      let pair =
        (c = ')' && top = '(') ||
        (c = ']' && top = '[') ||
        (c = '}' && top = '{')
      in
      if pair then stack := rest else ok := false
  end

Tests side-effecting match arms inside while body, ref-of-list as
stack, multi-char pairing dispatch.

174 baseline programs total.
2026-05-11 01:02:59 +00:00
6c77dec495 ocaml: phase 5.1 wildcard_match.ml baseline (* / ? matcher, 6/18 match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Recursive wildcard matcher:

  let rec is_match s i p j =
    if j = String.length p then i = String.length s
    else if p.[j] = '*' then
         is_match s i p (j + 1)             (* * matches empty   *)
      || (i < String.length s && is_match s (i + 1) p j)  (* * eats char *)
    else
      i < String.length s
      && (p.[j] = '?' || p.[j] = s.[i])
      && is_match s (i + 1) p (j + 1)

Patterns vs texts:
  a*b      | aaab abc abxyz xy xyz axby   -> 1 match
  ?b*c     | aaab abc abxyz xy xyz axby   -> 1 match
  *x*y*    | aaab abc abxyz xy xyz axby   -> 4 matches
                                  total   = 6

Tests deeply nested short-circuit && / ||, char equality on
pattern bytes, doubly-nested List.iter cross product.

173 baseline programs total.
2026-05-11 00:52:42 +00:00
0a3f02d636 ocaml: phase 5.1 powerset_target.ml baseline (subsets of {1..10} summing to 15 = 20)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Recursive powerset construction by doubling:

  let rec gen xs = match xs with
    | [] -> [[]]
    | h :: rest ->
      let sub = gen rest in
      sub @ List.map (fun s -> h :: s) sub

Enumerates all 2^10 = 1024 subsets, filters by sum:

  count = |{ S ⊆ {1..10} | Σ S = 15 }| = 20

Examples: {1,2,3,4,5}, {2,3,4,6}, {1,4,10}, {7,8}, {6,9}, ...

Tests recursive subset construction via List.map + closures,
pattern matching with h :: rest, List.fold_left (+) 0 sum reduce,
exhaustive O(2^n * n) traversal.

172 baseline programs total.
2026-05-11 00:42:08 +00:00
800dca67ca ocaml: parser accepts top-level tuple patterns in match cases
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Real OCaml accepts `match e1, e2 with | p1, p2 -> …` without
surrounding parens. parse-pattern previously stopped at the cons
layer (`p :: rest`) and treated a trailing `,` as a separator
the outer caller couldn't handle, surfacing as
"expected op -> got op ,".

Fix: `parse-pattern` now collects comma-separated patterns into a
:ptuple after parse-pattern-cons, before the optional `as` alias.
The scrutinee side already built tuples via parse-tuple, so both
sides are now symmetric.

lru_cache.ml (iter 258) reverts its workaround back to the natural
form:

  let rec take n lst = match n, lst with
    | 0, _ -> []
    | _, [] -> []
    | _, h :: r -> h :: take (n - 1) r

607/607 regressions clean.
2026-05-11 00:31:08 +00:00
fd1f94f292 ocaml: phase 5.1 lru_cache.ml baseline (cap=3 LRU, fingerprint 499)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Functional LRU cache via association-list ordered most-recent-first.
Get / put both:
  - find or remove the existing entry
  - cons the fresh (k, v) to the front
  - on put, trim the tail when over capacity

Sequence:
  put 1 100; put 2 200; put 3 300
  a = get 1  -> 100  (moves 1 to front)
  put 4 400          (evicts 2)
  b = get 2  -> -1   (no longer cached)
  c = get 3  -> 300
  d = get 1  -> 100
  a + b + c + d = 499

Tests `match … with (k', v) :: rest when k' = k -> …` tuple-cons
patterns with `when` guards, `function` keyword for arg-less
match, recursive find/remove/take over the same list.

Parser limit found: `match n, lst with` ad-hoc tuple-scrutinee is
not yet supported (got "expected op -> got op ,"); workaround
uses outer `if` plus inner match.

171 baseline programs total.
2026-05-11 00:20:30 +00:00
1d1c35a438 ocaml: phase 5.1 convex_hull.ml baseline (Andrew monotone chain, 5 vertices)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Andrew's monotone chain hull over 8 integer points:

  pts = [(0,0); (1,1); (2,0); (2,2); (0,2); (1,0); (3,3); (5,1)]

Sort lex, build lower hull L->R then upper R->L, popping while
the cross product is non-positive (collinear included on hull).

Hull traverse: (0,0) -> (2,0) -> (5,1) -> (3,3) -> (0,2) = 5
((2,0) lies on the lower edge from (0,0) to (5,1)).

Tests List.sort with 2-tuple comparator using nested pair
destructure, repeated `let (x, y) = arr.(i) in` array tuple
destructure across both passes, while + cont-flag pattern.

170 baseline programs total.
2026-05-11 00:09:06 +00:00
ca34cede88 ocaml: phase 5.1 next_greater.ml baseline (monotonic stack, sum 153)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Right-to-left monotonic stack for next-greater-element:

  for i = n - 1 downto 0 do
    while (match !stack with [] -> false | h :: _ -> h <= arr.(i)) do
      stack := List.tl !stack
    done;
    (match !stack with
     | [] -> res.(i) <- -1
     | h :: _ -> res.(i) <- h);
    stack := arr.(i) :: !stack
  done

For [4; 5; 2; 25; 7; 8; 1; 30; 12]:
  results: [5; 25; 25; 30; 8; 30; 30; -1; -1]
  sum of non-negative = 5+25+25+30+8+30+30 = 153

Tests stack as ref list with match-driven peek, match-as-bool in
while-guard, inline parenthesized match driving <-.

169 baseline programs total.
2026-05-10 23:58:50 +00:00
cb626fc402 ocaml: phase 5.1 fenwick_tree.ml baseline (BIT over 8 elements, fingerprint 228)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Fenwick / Binary Indexed Tree for prefix sums. The classic
`i & -i` low-bit trick needs negative-aware AND, but our `land`
evaluator (iter 127, bitwise via floor/mod arithmetic) only handles
non-negative operands. Workaround: a portable lowbit helper that
finds the largest power of 2 dividing i:

  let lowbit i =
    let r = ref 1 in
    while !r * 2 <= i && i mod (!r * 2) = 0 do
      r := !r * 2
    done;
    !r

After building from [1;3;5;7;9;11;13;15]:
  total       = prefix_sum 8 = 64
  update 1 by +100
  after       = prefix_sum 8 = 164
  total + after = 228

Tests recursive update / prefix_sum chains via helper-extracted
lowbit; documents a non-obvious limit of the bitwise-emulation
layer.

168 baseline programs total.
2026-05-10 23:48:46 +00:00
175a77fba5 ocaml: phase 5.1 segment_tree.ml baseline (range-sum tree, fingerprint 4232)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Power-of-two-indexed segment tree over [1;3;5;7;9;11;13;15]:
  build sums (root holds total = 64)
  query returns range sum in O(log n)
  update propagates a point delta back up the path

Sequence:
  r1 = query [2,5] = 5 + 7 + 9 + 11 = 32
  update idx 3 += 10  (so a[3] becomes 17)
  r2 = query [2,5] = 5 + 17 + 9 + 11 = 42
  encoded fingerprint = r1 + r2*100 = 32 + 4200 = 4232

Tests three mutually independent recursive functions with array
index arithmetic on 2*node / 2*node+1, half-bisection on mid =
(l+r)/2, bottom-up combine pattern.

167 baseline programs total.
2026-05-10 23:38:40 +00:00
3fe3b7b66f ocaml: phase 5.1 magic_square.ml baseline (5x5 Siamese, diag sum = 65)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Siamese construction for odd-order magic squares:

  - place 1 at (0, n/2)
  - for k = 2..n^2, move up-right with (x-1+n) mod n wrap
  - if the target cell is taken, drop down one row instead

  for n=5, magic constant = n*(n^2+1)/2 = 5*26/2 = 65

Returns the main-diagonal sum (65 by construction).

Tests 2D array via Array.init + Array.make, mod arithmetic with
the (x-1+n) mod n idiom for negative-safe wrap, nested begin/end
branches inside for-loop body.

166 baseline programs total.
2026-05-10 23:28:29 +00:00
689438d12e ocaml: phase 5.1 matrix_power.ml baseline (F(30) = 832040 via 2x2 matrix pow)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Fibonacci via repeated-squaring matrix exponentiation:

  [[1, 1], [1, 0]] ^ n = [[F(n+1), F(n)], [F(n), F(n-1)]]

Recursive O(log n) power:

  let rec mpow m n =
    if n = 0 then identity
    else if n mod 2 = 0 then let h = mpow m (n / 2) in mul h h
    else mul m (mpow m (n - 1))

Returns the .b cell after raising to the 30th power -> 832040 = F(30).

Tests record literal construction inside recursive function returns,
record field access (x.a etc), and pure integer arithmetic in the
matrix multiply.

165 baseline programs total.
2026-05-10 23:18:26 +00:00
d1a4616ac4 ocaml: phase 5.1 bipartite.ml baseline (7-node bipartite, 4 in color 0)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
BFS-based 2-coloring of an undirected graph:

  edges (undirected):
    0-1  0-3  1-2  1-4  2-5  3-4  3-6  5-6

  Partition: {0, 2, 4, 6} vs {1, 3, 5} (no odd cycles)

Returns count of color-0 vertices (4) on success, -1 on odd-cycle
detection.

Tests Queue-based BFS with a source-loop wrapper for disconnected
graphs, `1 - color.(u)` toggle, color-equality conflict check
on already-colored neighbors.

164 baseline programs total.
2026-05-10 23:08:16 +00:00
32f6c4ee0c ocaml: phase 5.1 egg_drop.ml baseline (2 eggs, 36 floors -> 8 trials)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Classic egg-drop puzzle DP:

  dp[e][f] = 1 + min over k in [1, f] of
              max(dp[e-1][k-1], dp[e][f-k])

For 2 eggs over 36 floors, the optimal worst-case is 8 trials
(closed form: triangular number bound).

Tests 2D DP with triple-nested for-loops, max-of-two via inline
if, large sentinel constant (100000000), mixed shifted indexing
(e-1) and (f-k) where both shift independently.

163 baseline programs total.
2026-05-10 22:58:13 +00:00
62712accdd ocaml: phase 5.1 polygon_area.ml baseline (pentagon 2x area = 32)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Shoelace formula on a pentagon with integer vertices:

  pts = [(0,0); (4,0); (4,3); (2,5); (0,3)]

  2 * area = | Σ (x_i * y_{i+1} - x_{i+1} * y_i) |
           = | 0*0 - 4*0 + 4*3 - 4*0 + 4*5 - 2*3 + 2*3 - 0*5
                + 0*0 - 0*3 |
           = 32

Returns the doubled form (32) to stay integral.

Tests:
  - let (x1, y1) = arr.(i) in   -- tuple destructure from array
  - arr.((i + 1) mod n)         -- modular wrap-around index
  - if a < 0 then - a else a    -- prefix - negation

162 baseline programs total.
2026-05-10 22:47:22 +00:00
c69a7694c8 ocaml: phase 5.1 min_cost_path.ml baseline (4x4 grid DP, optimal cost 12)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Standard 2D DP for min-cost path with right/down moves only:

  dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + cost[i][j]

  cost:                  dp:
    1 3 1 2                1  4  5  7
    1 5 1 3                2  7  6  9
    4 2 1 4                6  8  7 11
    1 6 2 3                7 13  9 12

Optimal cost from (0,0) to (3,3) = 12.

Tests nested 2D arrays via Array.init + Array.make, double-nested
for-loops with branched edges (first row, first column, general),
mixed .(i-1).(j) read + .(i).(j)<- write on the same DP array.

161 baseline programs total.
2026-05-10 22:37:44 +00:00
5384ff6c42 ocaml: phase 5.1 topo_dfs.ml baseline (DFS topo sort, fingerprint 24135)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
DFS topological sort — recurse on out-edges first, prepend after:

  let rec dfs v =
    if not visited.(v) then begin
      visited.(v) <- true;
      List.iter dfs adj.(v);
      order := v :: !order
    end

Same 6-node DAG as iter 230's Kahn's-algorithm baseline:
  0 -> {1, 2}
  1 -> {3}
  2 -> {3, 4}
  3 -> {5}
  4 -> {5}
  5

DFS order: [0; 2; 4; 1; 3; 5]
Horner fold: 0->0->2->24->241->2413->24135.

Complementary to topo_sort.ml (Kahn's BFS); tests recursive DFS
with no explicit stack + List.fold_left as a horner reduction.

160 baseline programs total.
2026-05-10 22:27:18 +00:00
bcb7db2ea4 ocaml: phase 5.1 radix_sort.ml baseline (LSD radix sort, sentinel 802002)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
LSD radix sort over base 10 digits. Per pass:
  - 10 bucket-refs created via Array.init 10 (fun _ -> ref []) (each
    closure call yields a distinct list cell)
  - scan array, append each value to its digit's bucket
  - flatten buckets back to the array in order

Input  [170;45;75;90;802;24;2;66]
Output [2;24;45;66;75;90;170;802]

Sentinel: a.(0) + a.(7)*1000 = 2 + 802*1000 = 802002.

Tests array-of-refs with !buckets.(d) deref, list-mode bucket
sort within in-place array sort, unused for-loop var (`for _ =
1 to maxd`).

159 baseline programs total.
2026-05-10 22:17:40 +00:00
5eed0dd5f5 ocaml: phase 5.1 coin_min.ml baseline (67 cents in US coins = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Minimum-coin DP with -1 sentinel for unreachable values:

  let coin_min coins amount =
    let dp = Array.make (amount + 1) (-1) in
    dp.(0) <- 0;
    for i = 1 to amount do
      List.iter (fun c ->
        if c <= i && dp.(i - c) >= 0 then begin
          let cand = dp.(i - c) + 1 in
          if dp.(i) < 0 || cand < dp.(i) then dp.(i) <- cand
        end
      ) coins
    done;
    dp.(amount)

  coin_min [1; 5; 10; 25] 67 = 6   (* 25+25+10+5+1+1 *)

Tests `if c <= i && dp.(i-c) >= 0 then` short-circuit guard;
relies on iter-242 fix so dp.(i-c) is not evaluated when c > i.

158 baseline programs total.
2026-05-10 22:07:17 +00:00
3ea8967571 ocaml: phase 5.1 flood_fill.ml baseline (largest grid component = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Recursive 4-way flood fill from every unvisited 1-cell:

  let rec flood visited r c =
    if r < 0 || r >= h || c < 0 || c >= w then 0
    else if visited.(r).(c) || grid.(r).(c) = 0 then 0
    else begin
      visited.(r).(c) <- true;
      1 + flood visited (r - 1) c
        + flood visited (r + 1) c
        + flood visited r (c - 1)
        + flood visited r (c + 1)
    end

Grid (1s shown as #, 0s as .):
  # # . # #
  # . . . #
  . . # . .
  # # # # .
  . . . # #

Largest component: {(2,2),(3,0),(3,1),(3,2),(3,3),(4,3),(4,4)} = 7.

Bounds check r >= 0 must short-circuit before visited/grid reads;
relies on the && / || fix from iter 242.

157 baseline programs total.
2026-05-10 21:57:42 +00:00
e057d9f18f ocaml: phase 5.1 next_permutation.ml baseline (5! - 1 = 119 successors)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Standard in-place next-permutation (Narayana's algorithm):

  let next_perm a =
    let n = Array.length a in
    let i = ref (n - 2) in
    while !i >= 0 && a.(!i) >= a.(!i + 1) do i := !i - 1 done;
    if !i < 0 then false
    else begin
      let j = ref (n - 1) in
      while a.(!j) <= a.(!i) do j := !j - 1 done;
      swap a.(!i) a.(!j);
      reverse a (!i + 1) (n - 1);
      true
    end

Starting from [1;2;3;4;5], next_perm returns true 119 times then
false (when reverse-sorted). 5! - 1 = 119.

Tests guarded `while … && a.(!i) … do` loops that rely on the
iter-242 short-circuit fix.

156 baseline programs total.
2026-05-10 21:47:52 +00:00
4761d41a0d ocaml: && / || short-circuit fix + bfs_grid.ml baseline (5x5 grid, dist 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Before: `:op` handler always evaluated both operands before dispatching
to ocaml-eval-op. For pure binops that's fine, but `&&` / `||` MUST
short-circuit:

  if nr >= 0 && grid.(nr).(nc) = 0 then ...

When nr = -1, real OCaml never evaluates `grid.(-1)`. Our evaluator
did, and crashed with "nth: list/string and number".

Fix: special-case `&&` and `||` in :op dispatch, mirroring the same
pattern already used for `:=` and `<-`. Evaluate lhs, branch on it,
and only evaluate rhs when needed.

Latent since baseline 1 — earlier programs never triggered it because
the rhs was unconditionally safe.

bfs_grid.ml: shortest path through a 5x5 grid with walls. Standard
BFS using Queue.{push,pop,is_empty} + Array.init for the 2D distance
matrix. Path 0,0 -> ... -> 4,4 has length 8. 155 baseline programs
total.
2026-05-10 21:37:41 +00:00
bed374c9e1 ocaml: phase 5.1 tarjan_scc.ml baseline (8-node digraph, 4 SCCs)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Tarjan's strongly-connected components in a single DFS using
index/lowlink:

  graph (8 nodes, directed):
    0 -> 1 -> 2 -> 0   (3-cycle)
    2 -> 3
    3 -> 4
    4 -> 5 -> 6 -> 4   (3-cycle)
    4 -> 7

  SCCs: {0,1,2}, {3}, {4,5,6}, {7}  =  4 components

Module-level ref + array state (index_arr, lowlink, on_stack,
stack, scc_count). When lowlink(v) = index(v), pop from stack
until v is removed; that's a complete SCC.

Tests: recursive function with module-level mutable state,
nested begin/end branches inside List.iter closure, inner
`let rec pop ()` traversing a ref-of-list, pattern match on
[] / h :: rest cons-list shape.

154 baseline programs total.
2026-05-10 07:06:29 +00:00
b4571f0f9f ocaml: phase 5.1 lev_iter.ml baseline (sum of 5 edit distances = 16)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Iterative Levenshtein DP with rolling 1D arrays for O(min(m,n))
space. Distances:

  kitten    -> sitting    : 3
  saturday  -> sunday     : 3
  abc       -> abc        : 0
  ""        -> abcde      : 5
  intention -> execution  : 5
  ----------------------------
  total                   : 16

Complementary to the existing levenshtein.ml which uses the
exponential recursive form (only sums tiny strings); this one is
the practical iterative variant used for real ED.

Tests the recently-fixed <- with bare `if` rhs:

  curr.(j) <- (if m1 < c then m1 else c) + 1

153 baseline programs total.
2026-05-10 06:53:38 +00:00
0ef26b20f3 ocaml: phase 5.1 binary_heap.ml baseline (min-heap sort 9 vals -> 123456789)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Array-backed binary min-heap with explicit size tracking via ref:

  let push a size x =
    a.(!size) <- x; size := !size + 1; sift_up a (!size - 1)

  let pop a size =
    let m = a.(0) in
    size := !size - 1;
    a.(0) <- a.(!size);
    sift_down a !size 0;
    m

Push [9;4;7;1;8;3;5;2;6], pop nine times -> 1,2,3,4,5,6,7,8,9.
Fold-as-decimal: ((((((((1*10+2)*10+3)*10+4)*10+5)*10+6)*10+7)*10+8)*10+9 = 123456789.

Tests recursive sift_up + sift_down, in-place array swap,
parent/lchild/rchild index arithmetic, combined push/pop session
with refs.

152 baseline programs total.
2026-05-10 06:43:46 +00:00
19d0ef0f38 ocaml: phase 5.1 rolling_hash.ml baseline (Rabin-Karp, 6 "abc" matches)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Polynomial rolling hash mod 1000003 with base 257:
  - precompute base^(m-1)
  - slide window updating hash in O(1) per step
  - verify hash match with O(m) memcmp to skip false positives

  rolling_match "abcabcabcabcabcabc" "abc" = 6

Six non-overlapping copies of "abc" at positions 0,3,6,9,12,15.

Tests `for _ = 0 to m - 2 do … done` unused loop variable
(uses underscore wildcard pattern), Char.code arithmetic, mod
arithmetic with intermediate negative subtractions, complex nested
if/begin branching with inner break-via-flag.

151 baseline programs total.
2026-05-10 06:34:13 +00:00
1dd350d592 ocaml: phase 5.1 huffman.ml baseline (Huffman tree WPL = 224)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Classic CLRS Huffman code example. ADT:

  type tree = Leaf of int * char | Node of int * tree * tree

Build by repeatedly merging two lightest trees (sorted-list pq):

  let rec build_tree lst = match lst with
    | [t] -> t
    | a :: b :: rest ->
      let merged = Node (weight a + weight b, a, b) in
      build_tree (insert merged rest)

  weighted path length (= total Huffman bits):
    leaves {(5,a) (9,b) (12,c) (13,d) (16,e) (45,f)} -> 224

Tests sum-typed ADT with mixed arities, `function` keyword
pattern matching, recursive sorted insert, depth-counting recursion.

150 baseline programs total.
2026-05-10 06:21:06 +00:00
4fdf6980da ocaml: parser accepts if/match/let/fun as rhs of <- and :=
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Previously `a.(i) <- if c then x else y` failed with
"unexpected token keyword if" because parse-binop-rhs called
parse-prefix for the rhs, which doesn't accept if/match/let/fun.

Real OCaml allows full expressions on the rhs of <-/:=. Fix:
special-case prec-1 ops in parse-binop-rhs to call parse-expr-no-seq
instead of parse-prefix. The recursive parse-binop-rhs with
min-prec restored after picks up any further chained <- (since both
ops are right-associative with no higher-prec binops above them).

Manacher baseline updated to use bare `if` on rhs of <-,
removing the parens workaround from iter 235. 607/607 regressions
remain clean.
2026-05-10 06:11:57 +00:00
cccef832d9 ocaml: phase 5.1 manacher.ml baseline (longest palindrome "babadaba" = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Manacher's algorithm: insert # separators (length 2n+1) to unify
odd/even cases, then maintain palindrome radii p[] alongside a
running (center, right) pair to skip work via mirror reflection.
Linear time.

  manacher "babadaba" = 7   (* witness: "abadaba", positions 1..7 *)

Note: requires parenthesizing the if-expression on the rhs of <-:

  p.(i) <- (if pm < v then pm else v)

Real OCaml parses bare `if` at <-rhs since the rhs is at expr
level; our parser places <-rhs at binop level which doesn't include
`if` / `match` / `let`. Workaround until we relax the binop
RHS grammar.

149 baseline programs total.
2026-05-10 05:58:05 +00:00
526ffbb5f0 ocaml: phase 5.1 floyd_warshall.ml baseline (4-node APSP, dist(0,3)=9)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 53s
Floyd-Warshall all-pairs shortest path with triple-nested for-loop:

  for k = 0 to n - 1 do
    for i = 0 to n - 1 do
      for j = 0 to n - 1 do
        if d.(i).(k) + d.(k).(j) < d.(i).(j) then
          d.(i).(j) <- d.(i).(k) + d.(k).(j)
      done
    done
  done

Graph (4 nodes, directed):
  0->1 weight 5, 0->3 weight 10, 1->2 weight 3, 2->3 weight 1

Direct edge 0->3 = 10, but path 0->1->2->3 = 5+3+1 = 9.

Tests 2D array via Array.init with closure, nested .(i).(j) read
+ write, triple-nested for, in-place mutation under aliasing.

148 baseline programs total.
2026-05-10 05:41:02 +00:00
99f321f532 ocaml: phase 5.1 mst_kruskal.ml baseline (5-node MST weight 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Kruskal's minimum spanning tree using path-compressing union-find:

  edges (w, u, v):
    (1, 0, 1) (2, 1, 2) (3, 0, 3) (4, 2, 3) (5, 3, 4) (6, 0, 4)

After sorting by weight and greedily unioning:
  pick (1,0,1) -> components: {0,1} {2} {3} {4}
  pick (2,1,2) -> {0,1,2} {3} {4}
  pick (3,0,3) -> {0,1,2,3} {4}
  skip (4,2,3) -- already connected
  pick (5,3,4) -> {0,1,2,3,4}
  skip (6,0,4) -- already connected

  MST weight = 1 + 2 + 3 + 5 = 11

Tests List.sort with 3-tuple destructuring lambda, compare on int,
Array.init with closure, in-place array mutation in find, boolean
union returning true iff merge happened.

147 baseline programs total.
2026-05-10 05:21:14 +00:00
dfd89d998e ocaml: phase 5.1 trie.ml baseline (prefix tree, 6/9 word lookups match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Mutable-record trie with linked-list children:

  type trie = {
    mutable terminal : bool;
    mutable children : (char * trie) list
  }

Insert {cat, car, card, cart, dog, doge}; lookup 9 words. Hits are
exactly the inserted set: cat, car, card, cart, dog, doge = 6.
Misses: ca (prefix not terminal), dogs (extends 'dog' but no 'dogs'
node), x (no path).

Tests:
  - recursive type definition with self-referential field
  - mutable record fields with .field <- v
  - Option pattern matching (Some / None)
  - tuple-cons pattern (k, v) :: rest

146 baseline programs total.
2026-05-10 05:11:12 +00:00
74d8ade089 ocaml: phase 5.1 count_inversions.ml baseline (12 inversions via merge sort)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Modified merge sort that counts inversions during the merge step:
when an element from the right half is selected, the remaining
elements of the left half (mid - i + 1) all form inversions with
that right element.

  count_inv [|8; 4; 2; 1; 3; 5; 7; 6|] = 12

Inversions of [8;4;2;1;3;5;7;6]:
  with 8: (8,4)(8,2)(8,1)(8,3)(8,5)(8,7)(8,6) = 7
  with 4: (4,2)(4,1)(4,3) = 3
  with 2: (2,1) = 1
  with 7: (7,6) = 1
                                        total = 12

Tests: let rec ... and ... mutual recursion, while + ref + array
mutation, in-place sort with auxiliary scratch array.

145 baseline programs total.
2026-05-10 05:01:08 +00:00
872302ede1 ocaml: phase 5.1 topo_sort.ml baseline (6-node DAG, all 6 ordered)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Kahn's algorithm BFS topological sort:

  let topo_sort n adj =
    let in_deg = Array.make n 0 in
    for i = 0 to n - 1 do
      List.iter (fun j -> in_deg.(j) <- in_deg.(j) + 1) adj.(i)
    done;
    let q = Queue.create () in
    for i = 0 to n - 1 do
      if in_deg.(i) = 0 then Queue.push i q
    done;
    let count = ref 0 in
    while not (Queue.is_empty q) do
      let u = Queue.pop q in
      count := !count + 1;
      List.iter (fun v ->
        in_deg.(v) <- in_deg.(v) - 1;
        if in_deg.(v) = 0 then Queue.push v q
      ) adj.(u)
    done;
    !count

Graph: 0->{1,2}; 1->{3}; 2->{3,4}; 3->{5}; 4->{5}; 5.
Acyclic, so all 6 nodes can be ordered.

Tests Queue.{create,push,pop,is_empty}, mutable array via closure.

144 baseline programs total.
2026-05-10 04:51:15 +00:00
57a63826e3 ocaml: phase 5.1 knapsack.ml baseline (0/1 knapsack, cap=8 -> 36)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 15s
Standard 1D 0/1 knapsack DP with reverse inner loop:

  let knapsack values weights cap =
    let n = Array.length values in
    let dp = Array.make (cap + 1) 0 in
    for i = 0 to n - 1 do
      let v = values.(i) and w = weights.(i) in
      for c = cap downto w do
        let take = dp.(c - w) + v in
        if take > dp.(c) then dp.(c) <- take
      done
    done;
    dp.(cap)

  values: [|6; 10; 12; 15; 20|]
  weights: [|1; 2;  3;  4;  5|]
  knapsack v w 8 = 36   (* take items with weights 1, 2, 5 *)

Tests for-downto + array literal access in the same hot loop.

143 baseline programs total.
2026-05-10 04:38:59 +00:00
7a67637826 ocaml: phase 5.1 lcs.ml baseline (LCS of "ABCBDAB" and "BDCAB" = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Classic 2D DP for longest common subsequence, optimized to use
two rolling 1D arrays (prev / curr) for O(min(m,n)) space:

  for i = 1 to m do
    for j = 1 to n do
      if s1.[i-1] = s2.[j-1] then curr.(j) <- prev.(j-1) + 1
      else if prev.(j) >= curr.(j-1) then curr.(j) <- prev.(j)
      else curr.(j) <- curr.(j-1)
    done;
    for j = 0 to n do prev.(j) <- curr.(j) done
  done;
  prev.(n)

  lcs "ABCBDAB" "BDCAB" = 4

Two valid LCS witnesses: BCAB and BDAB.

Avoids Array.make_matrix (not in our runtime) by manual rolling.

142 baseline programs total.
2026-05-10 04:29:58 +00:00
42a506faff ocaml: phase 5.1 dijkstra.ml baseline (5-node SSSP, dist(0,4) = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Array-based O(n^2) Dijkstra on a small directed weighted graph:

  edges = [|
    [(1, 4); (2, 1)];   (* 0 -> 1 (w=4), 2 (w=1) *)
    [(3, 1)];           (* 1 -> 3 (w=1)         *)
    [(1, 2); (3, 5)];   (* 2 -> 1 (w=2), 3 (w=5) *)
    [(4, 3)];           (* 3 -> 4 (w=3)         *)
    []                  (* 4 sink              *)
  |]

Optimal path 0->2->1->3->4 has weight 1+2+1+3 = 7.

Tests: array-of-list-of-int-pair literal, List.iter with tuple
destructuring closure, in-place dist mutation, nested for + ref.

141 baseline programs total.
2026-05-10 04:20:47 +00:00
713d506bb8 ocaml: phase 5.1 kmp.ml baseline (5 occurrences of "abab" in haystack)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Knuth-Morris-Pratt linear-time string search:
  - kmp_table builds failure function in O(|pattern|)
  - kmp_search scans text once in O(|text|), counting matches
  - After a hit, k := t.(n-1) so overlapping matches still count

  kmp_search "abababcabababcababcc" "abab" = 5

Hits at positions 0, 2, 7, 9, 14 (overlapping at 0/2 and 7/9).

Tests: nested while-inside-for, char inequality (.<>), pat.[i]
string indexing, Array.make 0, combined string + array indexing.

140 baseline programs total.
2026-05-10 04:08:53 +00:00
bcaa41d1ae ocaml: phase 5.1 union_find.ml baseline (10 nodes, 6 unions, 4 components)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Disjoint-set union with path compression:

  let make_uf n = Array.init n (fun i -> i)
  let rec find p x =
    if p.(x) = x then x
    else begin let r = find p p.(x) in p.(x) <- r; r end
  let union p x y =
    let rx = find p x in let ry = find p y in
    if rx <> ry then p.(rx) <- ry

After unioning (0,1), (2,3), (4,5), (6,7), (0,2), (4,6):
  {0,1,2,3} {4,5,6,7} {8} {9}  --> 4 components.

Tests Array.init with closure, recursive find, in-place .(i)<-r.

139 baseline programs total.
2026-05-10 03:59:56 +00:00
edbb03e205 ocaml: phase 5.1 quickselect.ml baseline (median of 9 elements = 5)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Hoare quickselect with Lomuto partition: recursively narrows the
range to whichever side contains the kth index. Mutates the array
in place via .(i)<-v. The median (k=4) of [7;2;9;1;5;6;3;8;4] is 5.

  let rec quickselect arr lo hi k =
    if lo = hi then arr.(lo)
    else begin
      let pivot = arr.(hi) in
      let i = ref lo in
      for j = lo to hi - 1 do
        if arr.(j) < pivot then begin
          let t = arr.(!i) in
          arr.(!i) <- arr.(j); arr.(j) <- t;
          i := !i + 1
        end
      done;
      ...
    end

Exercises array literal syntax + in-place mutation in the same
program, ensuring [|...|] yields a mutable backing.

138 baseline programs total.
2026-05-10 03:50:59 +00:00
551ed44f7f ocaml: phase 5.1 array literals [|...|] + lis.ml baseline (LIS = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Added parser support for OCaml array literal syntax:

  [| e1; e2; ...; en |]  -->  Array.of_list [e1; e2; ...; en]
  [||]                   -->  Array.of_list []

Desugaring keeps the array representation unchanged (ref-of-list)
since Array.of_list is a no-op constructor for that backing.

Tokenizer emits [, |, |, ] as separate ops; parser detects [ followed
by | and enters array-literal mode, terminating on |].

Baseline lis.ml exercises the syntax:

  let lis arr =
    let n = Array.length arr in
    let dp = Array.make n 1 in
    for i = 1 to n - 1 do
      for j = 0 to i - 1 do
        if arr.(j) < arr.(i) && dp.(j) + 1 > dp.(i) then
          dp.(i) <- dp.(j) + 1
      done
    done;
    let best = ref 0 in
    for i = 0 to n - 1 do
      if dp.(i) > !best then best := dp.(i)
    done;
    !best

  lis [|10; 22; 9; 33; 21; 50; 41; 60; 80|] = 6

137 baseline programs total.
2026-05-10 03:41:19 +00:00
76de0a20f8 ocaml: phase 5.1 josephus.ml baseline (n=50 k=3, survivor at position 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Classic Josephus problem solved with the standard recurrence:

  let rec josephus n k =
    if n = 1 then 0
    else (josephus (n - 1) k + k) mod n

  josephus 50 3 + 1 = 11

50 people stand in a circle, every 3rd is eliminated; the last
survivor is at position 11 (1-indexed). Tests recursion + mod.

136 baseline programs total.
2026-05-10 03:22:29 +00:00
353dcb67d6 ocaml: phase 5.1 partition_count.ml baseline (p(15) = 176)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Counts integer partitions via classic DP:

  let partition_count n =
    let dp = Array.make (n + 1) 0 in
    dp.(0) <- 1;
    for k = 1 to n do
      for i = k to n do
        dp.(i) <- dp.(i) + dp.(i - k)
      done
    done;
    dp.(n)

  partition_count 15 = 176

Tests Array.make, .(i)<-/.(i) array access, nested for-loops, refs.

135 baseline programs total.
2026-05-10 03:13:36 +00:00
36e02c906a ocaml: phase 5.1 pythagorean.ml baseline (primitive Pythagorean triples with hyp <= 100 = 16)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Uses Euclid's formula: for coprime m > k of opposite parity, the
triple (m^2 - k^2, 2mk, m^2 + k^2) is a primitive Pythagorean.

  let count_primitive_triples n =
    let c = ref 0 in
    for m = 2 to 50 do
      let kk = ref 1 in
      while !kk < m do
        if (m - !kk) mod 2 = 1 && gcd m !kk = 1 then begin
          let h = m * m + !kk * !kk in
          if h <= n then c := !c + 1
        end;
        kk := !kk + 1
      done
    done;
    !c

  count_primitive_triples 100 = 16

The 16 triples include the classics (3,4,5), (5,12,13), (8,15,17),
(7,24,25), and end with (65,72,97).

134 baseline programs total.
2026-05-10 03:02:17 +00:00
5c1b4349aa ocaml: phase 5.1 harshad.ml baseline (count Niven/Harshad numbers <= 100 = 33)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
A Harshad (or Niven) number is divisible by its digit sum:

  let count_harshad limit =
    let c = ref 0 in
    for n = 1 to limit do
      if n mod (digit_sum n) = 0 then c := !c + 1
    done;
    !c

  count_harshad 100 = 33

All single-digit numbers (1..9) qualify trivially. Plus 10, 12, 18,
20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80,
81, 84, 90, 100 (24 more) = 33 total under 100.

133 baseline programs total.
2026-05-10 02:46:20 +00:00
e23aa9c273 ocaml: phase 5.1 reverse_int.ml baseline (digit-reverse sum = 54329)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Walks digits via mod 10 / div 10, accumulating the reversed value:

  let reverse_int n =
    let m = ref n in
    let r = ref 0 in
    while !m > 0 do
      r := !r * 10 + !m mod 10;
      m := !m / 10
    done;
    !r

  reverse 12345 + reverse 100 + reverse 7
  = 54321 + 1 + 7
  = 54329

Trailing zeros collapse (reverse 100 = 1, not 001).

132 baseline programs total.
2026-05-10 02:36:37 +00:00
da54c3ea53 ocaml: phase 5.1 bowling.ml baseline (10-pin bowling score, sample game = 167)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Walks the pin-knockdown list applying strike/spare bonuses through
a 10-frame counter:

  strike (10):           score 10 + next 2 throws,  advance i+1
  spare  (a + b = 10):   score 10 + next 1 throw,   advance i+2
  open  (a + b < 10):    score a + b,                advance i+2

Frame ten special-cases are handled implicitly: the input includes
bonus throws naturally and the while-loop terminates after frame 10.

  bowling_score [10; 7; 3; 9; 0; 10; 0; 8; 8; 2; 0; 6;
                 10; 10; 10; 8; 1]
  = 20+19+9+18+8+10+6+30+28+19
  = 167

131 baseline programs total.
2026-05-10 02:26:10 +00:00
63901931c4 ocaml: phase 5.1 tail_factorial.ml baseline (12! via tail-recursion = 479001600)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Single-helper tail-recursive loop threading an accumulator:

  let factorial n =
    let rec go n acc =
      if n <= 1 then acc
      else go (n - 1) (n * acc)
    in
    go n 1

  factorial 12 = 479_001_600

Companion to factorial.ml (10! = 3628800 via doubly-recursive
style); same answer-shape, different evaluator stress: this version
has constant stack depth.

130 baseline programs total — milestone.
2026-05-10 02:05:09 +00:00
e77a2d3a81 ocaml: phase 5.1 zerosafe.ml baseline (Option-chained safe division, sum = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
safe_div returns None on division by zero; safe_chain stitches two
divisions, propagating None on either failure:

  let safe_div a b =
    if b = 0 then None else Some (a / b)

  let safe_chain a b c =
    match safe_div a b with
    | None -> None
    | Some q -> safe_div q c

Test:
  safe_chain 100 2 5    = Some 10
  safe_chain 100 0 5    = None  -> -1
  safe_chain 50 5 0     = None  -> -1
  safe_chain 1000 10 5  = Some 20
  10 - 1 - 1 + 20 = 28

Tests Option chaining + match-on-result with sentinel default.
Demonstrates the canonical 'fail-early on None' pattern.

129 baseline programs total.
2026-05-10 01:49:23 +00:00
836e01dbb4 ocaml: phase 5.1 number_words.ml baseline (letter count of 1..19 spelled out = 106)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
19-arm match returning the English word for each number 1..19, then
sum String.length:

  let number_to_words n =
    match n with
    | 1 -> 'one' | 2 -> 'two' | ... | 19 -> 'nineteen'
    | _ -> ''

  total_letters 19 = 36 + 70 = 106
                    (1-9)  (10-19)

Real PE17 covers 1..1000 (answer 21124) but needs more elaborate
number-to-words logic (compounds, 'and', 'thousand'). 1..19 keeps
the program small while exercising literal-pattern match dispatch
on many arms.

128 baseline programs total.
2026-05-10 01:39:25 +00:00
fb0e83d3a1 ocaml: phase 5.1 palindrome_sum.ml baseline (sum of 3-digit palindromes = 49500)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
let palindrome_sum lo hi =
    let total = ref 0 in
    for n = lo to hi do
      if is_pal n then total := !total + n
    done;
    !total

  palindrome_sum 100 999 = 49500

There are 90 three-digit palindromes (form aba; 9 choices for a, 10
for b). Average value 550, sum 49500.

Companion to palindrome.ml (predicate-only) and paren_depth.ml.

127 baseline programs total.
2026-05-10 01:29:52 +00:00
0b79d4d4b4 ocaml: phase 5.1 triangle_div.ml baseline (first triangle with >10 divisors = 120)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
PE12 with target = 10:

  let count_divisors n =
    let c = ref 0 in
    let i = ref 1 in
    while !i * !i <= n do
      if n mod !i = 0 then begin
        c := !c + 1;
        if !i * !i <> n then c := !c + 1
      end;
      i := !i + 1
    done;
    !c

  let first_triangle_with_divs target =
    walk triangles T(n) = T(n-1) + n until count_divisors T > target

T(15) = 120 has 16 divisors — first to exceed 10. Real PE12 uses
target 500 (answer 76576500); 10 stays well under budget.

126 baseline programs total.
2026-05-10 01:17:11 +00:00
58ea001f12 ocaml: phase 5.1 perfect.ml baseline (count perfect numbers <= 500 = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Perfect numbers = those where the proper-divisor sum equals n. Three
exist under 500: 6, 28, 496. (8128 is the next; 33550336 the one
after that.)

Same div_sum machinery as euler21_small.ml / abundant.ml (the
trial-division up to sqrt-n).

Original 10000 limit timed out at 10 minutes under contention (496
itself takes thousands of trials at the inner loop). 500 stays under
budget while still finding all three small perfects.

125 baseline programs total — milestone.
2026-05-10 01:02:18 +00:00
da96a79104 ocaml: phase 5.1 abundant.ml baseline (count abundant numbers < 100 = 21)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
A number n is abundant if its proper-divisor sum exceeds n. Reuses
the trial-division div_sum helper:

  let count_abundant n =
    let c = ref 0 in
    for i = 12 to n - 1 do
      if div_sum i > i then c := !c + 1
    done;
    !c

  count_abundant 100 = 21

Abundant numbers under 100, starting at 12, 18, 20, 24, 30, 36, 40,
42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96 -> 21.

Companion to euler21_small.ml (amicable). The classification:
  perfect:  d(n) = n   (e.g. 6, 28)
  abundant: d(n) > n   (e.g. 12, 18)
  deficient:d(n) < n   (everything else)

124 baseline programs total.
2026-05-10 00:36:29 +00:00
ed8aaf8af7 ocaml: phase 5.1 euler36.ml baseline (sum of double-base palindromes <= 1000 = 1772)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Numbers that read the same in base 10 and base 2:

  1, 3, 5, 7, 9, 33, 99, 313, 585, 717
  sum = 1772

Implementation:
  pal_dec n        check decimal palindrome via index walk
  to_binary n      build binary string via mod 2 / div 2 stack
  pal_bin n        check binary palindrome
  euler36 limit    scan 1..limit-1, sum where both palindromes

Real PE36 uses 10^6 (answer 872187). 1000 takes ~9 minutes on
contended host but stays within reasonable budget for the
spec-level evaluator.

123 baseline programs total.
2026-05-10 00:26:46 +00:00
37f7405dcf ocaml: phase 5.1 euler40_small.ml baseline (Champernowne digit product = 15)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Build the Champernowne string '12345678910111213...' until at
least 1500 chars; product of digits at positions 1, 10, 100, 1000
is 1 * 1 * 5 * 3 = 15.

Initial implementation timed out: 'String.length (Buffer.contents
buf) < 1500' rebuilt the full string each iteration (O(n^2) in our
spec-level evaluator). Fixed by tracking length separately from
the Buffer:

  let len = ref 0 in
  while !len < 1500 do
    let s = string_of_int !i in
    Buffer.add_string buf s;
    len := !len + String.length s;
    i := !i + 1
  done

Real PE40 uses positions up to 10^6 (answer 210); 1000 keeps under
budget while exercising the same string-build + char-pick pattern.

122 baseline programs total.
2026-05-10 00:09:57 +00:00
4e6a345342 ocaml: phase 5.1 euler34_small.ml baseline (factorions <= 2000 = 145)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Number that equals the sum of factorials of its digits:
  145 = 1! + 4! + 5! = 1 + 24 + 120

Implementation:
  fact n           iterative factorial
  digit_fact_sum n walk digits, sum fact(digit)
  euler34 limit    scan 3..limit, accumulate matches

The only other factorion is 40585 = 4!+0!+5!+8!+5!. Real PE34 sums
both (= 40730); 2000 keeps under our search budget.

121 baseline programs total.
2026-05-09 23:50:25 +00:00
21dbd195d5 ocaml: phase 5.1 euler29_small.ml baseline (distinct a^b for 2<=a,b<=5 = 15)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Compute every power a^b for a, b in [2..N] and count distinct
values. Hashtbl as a set with unit-payload (iter-168 idiom):

  let euler29 n =
    let h = Hashtbl.create 64 in
    for a = 2 to n do
      for b = 2 to n do
        let p = ref 1 in
        for _ = 1 to b do p := !p * a done;
        Hashtbl.replace h !p ()
      done
    done;
    Hashtbl.length h

For N=5: 16 powers minus one duplicate (4^2 = 2^4 = 16) -> 15.

Real PE29 uses N=100 (answer 9183).

120 baseline programs total — milestone.
2026-05-09 23:37:36 +00:00
87f9a84365 ocaml: phase 5.1 euler21_small.ml baseline (sum of amicable numbers <= 300 = 504)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
div_sum computes proper divisor sum via trial division up to sqrt(n):

  let div_sum n =
    let s = ref 1 in
    let i = ref 2 in
    while !i * !i <= n do
      if n mod !i = 0 then begin
        s := !s + !i;
        let q = n / !i in
        if q <> !i then s := !s + q
      end;
      i := !i + 1
    done;
    if n = 1 then 0 else !s

Outer loop finds amicable pairs (a, b) with d(a) = b, d(b) = a,
a != b. Only pair under 300 is (220, 284); 220 + 284 = 504.

Real PE21 uses 10000 (answer 31626). 300 keeps the run under
budget while exercising the same divisor-sum trick.

119 baseline programs total.
2026-05-09 23:27:15 +00:00
46e49dc947 ocaml: phase 5.1 euler30_cube.ml baseline (sum of digit-cube narcissistic numbers <= 999 = 1301)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Numbers equal to the sum of cubes of their digits:
  153 = 1 + 125 + 27
  370 = 27 + 343 + 0
  371 = 27 + 343 + 1
  407 = 64 + 0 + 343
  sum = 1301

Implementation:
  pow_digit_sum n p   walk digits of n, accumulate d^p
  euler30 p limit     scan 2..limit and sum where pow_digit_sum n p = n

Real PE30 uses 5th powers (answer 443839); the cube version
exercises the same algorithm in a smaller search space.

118 baseline programs total.
2026-05-09 23:17:00 +00:00
ea7120751d ocaml: phase 5.1 euler28.ml baseline (sum of diagonals in 7x7 spiral = 261)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
For each layer 1..(n-1)/2, the four corners of an Ulam spiral are
spaced 2*layer apart. Step k four times per layer, accumulate:

  let euler28 n =
    let s = ref 1 in
    let k = ref 1 in
    for layer = 1 to (n - 1) / 2 do
      let step = 2 * layer in
      for _ = 1 to 4 do
        k := !k + step;
        s := !s + !k
      done
    done;
    !s

  euler28 7 = 1 + (3+5+7+9) + (13+17+21+25) + (31+37+43+49) = 261

Real PE28 uses 1001x1001 (answer 669171001); 7x7 is fast.

117 baseline programs total.
2026-05-09 23:03:06 +00:00
89a807a1ed ocaml: phase 5.1 euler14.ml baseline (longest Collatz under 100, starting n = 97)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
collatz_len walks n through n/2 if even, 3n+1 if odd, counting
steps. Outer loop scans 2..N tracking the best length and arg-best:

  let euler14 limit =
    let best = ref 0 in
    let best_n = ref 0 in
    for n = 2 to limit do
      let l = collatz_len n in
      if l > !best then begin
        best := l;
        best_n := n
      end
    done;
    !best_n

  euler14 100 = 97   (97 generates a 118-step chain)

Real PE14 uses limit = 1_000_000 (answer 837799); 100 exercises the
same algorithm in <2 minutes on our contended host.

116 baseline programs total.
2026-05-09 22:53:27 +00:00
391a2d0c4f ocaml: phase 5.1 euler16.ml baseline (digit sum of 2^15 = 26)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Computes 2^n via for-loop multiplication, then walks the digits via
mod 10 / div 10:

  let euler16 n =
    let p = ref 1 in
    for _ = 1 to n do p := !p * 2 done;
    let sum = ref 0 in
    let m = ref !p in
    while !m > 0 do
      sum := !sum + !m mod 10;
      m := !m / 10
    done;
    !sum

  euler16 15 = 3 + 2 + 7 + 6 + 8 = 26  (= digit sum of 32768)

Real PE16 asks for 2^1000 which exceeds float precision; 2^15 stays
safe and exercises the same digit-decomposition pattern.

115 baseline programs total.
2026-05-09 22:43:45 +00:00
5959989324 ocaml: phase 5.1 euler25.ml baseline (first 12-digit Fibonacci index = 55)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Iteratively grows two refs while the larger is below 10^(n-1),
counting iterations:

  let euler25 n =
    let a = ref 1 in
    let b = ref 1 in
    let i = ref 2 in
    let target = ref 1 in
    for _ = 1 to n - 1 do target := !target * 10 done;
    while !b < !target do
      let c = !a + !b in
      a := !b;
      b := c;
      i := !i + 1
    done;
    !i

  euler25 12 = 55   (F(55) = 139_583_862_445, 12 digits)

Real PE25 asks for 1000 digits (answer 4782); 12 keeps within
safe-int while exercising the identical algorithm.

114 baseline programs total — 200 iterations landed.
2026-05-09 22:31:27 +00:00
320d78a993 ocaml: phase 5.1 euler3.ml baseline (largest prime factor of 13195 = 29)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
PE3's worked example. Trial-division streaming: when the current
factor divides m, divide and update largest; otherwise bump factor:

  let largest_prime_factor n =
    let m = ref n in
    let factor = ref 2 in
    let largest = ref 0 in
    while !m > 1 do
      if !m mod !factor = 0 then begin
        largest := !factor;
        m := !m / !factor
      end else factor := !factor + 1
    done;
    !largest

  largest_prime_factor 13195 = 29  (= 5 * 7 * 13 * 29)

The full PE3 number 600851475143 exceeds JS safe-int (2^53 ≈ 9e15
in float terms; 6e11 is fine but the intermediate 'i mod !factor'
on the way to 6857 can overflow precision). 13195 keeps the program
portable across hosts.

113 baseline programs total.
2026-05-09 22:21:16 +00:00
2a01758f28 ocaml: phase 5.1 euler7.ml baseline (100th prime = 541)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Scaled-down PE7 (real version asks for the 10001st prime = 104743).
Trial-division within an outer while loop searching forward from 2,
short-circuited via bool ref:

  let nth_prime n =
    let count = ref 0 in
    let i = ref 1 in
    let result = ref 0 in
    while !count < n do
      i := !i + 1;
      let p = ref true in
      let j = ref 2 in
      while !j * !j <= !i && !p do
        if !i mod !j = 0 then p := false;
        j := !j + 1
      done;
      if !p then begin
        count := !count + 1;
        if !count = n then result := !i
      end
    done;
    !result

  nth_prime 100 = 541

100 keeps the run under our 3-minute budget while exercising the
same algorithm.

112 baseline programs total.
2026-05-09 22:11:11 +00:00
533be5b36b ocaml: phase 5.1 euler4_small.ml baseline (largest 2-digit palindrome product = 9009)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Scaled-down Project Euler #4. Real version uses 3-digit numbers
yielding 906609 = 913 * 993; that's an 810k-iteration nested loop
that times out under our contended-host spec-level evaluator.

The 2-digit version (10..99) is fast enough and tests the same
algorithm:
  9009 = 91 * 99   (the only 2-digit-product palindrome > 9000)

Implementation:
  is_pal n     index-walk comparing s.[i] to s.[len-1-i]
  euler4 lo hi nested for with running max + early-skip via
                'p > !m && is_pal p' short-circuit

111 baseline programs total.
2026-05-09 21:59:23 +00:00
853504642f ocaml: phase 5.1 euler10.ml baseline (sum of primes <= 100 = 1060, scaled-down PE10)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Sieve of Eratosthenes followed by a sum loop:

  let sieve_sum n =
    let s = Array.make (n + 1) true in
    s.(0) <- false;
    s.(1) <- false;
    for i = 2 to n do
      if s.(i) then begin
        let j = ref (i * i) in
        while !j <= n do
          s.(!j) <- false;
          j := !j + i
        done
      end
    done;
    let total = ref 0 in
    for i = 2 to n do
      if s.(i) then total := !total + i
    done;
    !total

Real PE10 asks for sum below 2,000,000; that's a ~2-3 second loop in
native OCaml but minutes-to-hours under our contended-host
spec-level evaluator. 100 keeps the run under 3 minutes while still
exercising the same algorithm.

110 baseline programs total.
2026-05-09 21:46:16 +00:00
00ffba9306 ocaml: phase 5.1 euler5.ml baseline (smallest multiple of 1..20 = 232792560)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Iteratively takes lcm of running result with i:

  let rec gcd a b = if b = 0 then a else gcd b (a mod b)
  let lcm a b = a * b / gcd a b
  let euler5 n =
    let r = ref 1 in
    for i = 2 to n do
      r := lcm !r i
    done;
    !r

  euler5 20 = 232792560
            = 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19

Tests gcd_lcm composition (iter 140) on a fresh problem.

109 baseline programs total.
2026-05-09 21:35:59 +00:00
cecde8733a ocaml: phase 5.1 partition.ml baseline (stable partition, evens*100 + odds = 3025)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Two ref lists accumulating in reverse, then List.rev'd — preserves
original order:

  let partition pred xs =
    let yes = ref [] in
    let no = ref [] in
    List.iter (fun x ->
      if pred x then yes := x :: !yes
      else no := x :: !no
    ) xs;
    (List.rev !yes, List.rev !no)

  partition (fun x -> x mod 2 = 0) [1..10]
  -> ([2;4;6;8;10], [1;3;5;7;9])

  evens sum * 100 + odds sum = 30 * 100 + 25 = 3025

Tests higher-order predicate, tuple return, and iter-98 let-tuple
destructuring on the call site.

108 baseline programs total.
2026-05-09 21:26:31 +00:00
c16a8f2d53 ocaml: phase 5.1 is_prime.ml baseline (count primes <= 100 = 25)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Trial division up to sqrt(n) with early-exit via bool ref:

  let is_prime n =
    if n < 2 then false
    else
      let p = ref true in
      let i = ref 2 in
      while !i * !i <= n && !p do
        if n mod !i = 0 then p := false;
        i := !i + 1
      done;
      !p

Outer count_primes loops 2..n calling is_prime, accumulating count.
Returns 25 — the canonical prime-counting function pi(100).

107 baseline programs total.
2026-05-09 21:16:40 +00:00
d4eb57fa07 ocaml: phase 5.1 catalan.ml baseline (Catalan number C(5) = 42)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
DP recurrence:

  C(0) = 1
  C(n) = sum_{j=0}^{n-1} C(j) * C(n-1-j)

  let catalan n =
    let dp = Array.make (n + 1) 0 in
    dp.(0) <- 1;
    for i = 1 to n do
      for j = 0 to i - 1 do
        dp.(i) <- dp.(i) + dp.(j) * dp.(i - 1 - j)
      done
    done;
    dp.(n)

C(5) = 42 — also the count of distinct binary trees with 5 internal
nodes, balanced paren strings of length 10, monotonic lattice paths,
etc.

106 baseline programs total.
2026-05-09 21:06:10 +00:00
73917745a0 ocaml: phase 5.1 fizz_classifier.ml baseline (FizzBuzz with polymorphic variants, 1..30 weighted = 540)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Two functions:
  classify n   maps i to a polymorphic variant
                  FizzBuzz | Fizz | Buzz | Num of int
  score x      pattern-matches the variant to a weight
                  FizzBuzz=100, Fizz=10, Buzz=5, Num n=n

For i in 1..30:
  FizzBuzz at 15, 30:                  2 * 100 = 200
  Fizz at 3,6,9,12,18,21,24,27:        8 *  10 =  80
  Buzz at 5,10,20,25:                  4 *   5 =  20
  Num: rest (16 numbers)                       = 240
                                          total = 540

Exercises polymorphic-variant match (iter 87) including a
payload-bearing 'Num n' arm.

105 baseline programs total.
2026-05-09 20:56:31 +00:00
c8206e718a ocaml: phase 5.1 max_product3.ml baseline (max product of 3, with negatives -> 300)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Sort, then compare two candidates:
  p1 = product of three largest values
  p2 = product of two smallest (potentially negative) values and the largest

For [-10;-10;1;3;2]:
  sorted    = [-10;-10;1;2;3]
  p1        = 3 * 2 * 1                = 6
  p2        = (-10) * (-10) * 3        = 300
  max       = 300

Tests List.sort + Array.of_list + arr.(n-i) end-walk + candidate-pick
via if-then-else.

104 baseline programs total.
2026-05-09 20:46:42 +00:00
288c0f8c3e ocaml: phase 5.1 euler9.ml baseline (Project Euler #9, abc = 31875000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Find the unique Pythagorean triple with a + b + c = 1000 and
return their product.

The naive triple loop timed out under host contention (10-minute
cap exceeded with ~333 * 999 ~= 333k inner iterations of complex
checks). Rewritten with algebraic reduction:

  a + b + c = 1000  AND  a^2 + b^2 = c^2
  =>  b = (500000 - 1000 * a) / (1000 - a)

so only the outer a-loop is needed (333 iterations). Single-pass
form:

  for a = 1 to 333 do
    let num = 500000 - 1000 * a in
    let den = 1000 - a in
    if num mod den = 0 then begin
      let b = num / den in
      if b > a then
        let c = 1000 - a - b in
        if c > b then result := a * b * c
    end
  done

Triple (200, 375, 425), product 31875000.

103 baseline programs total.
2026-05-09 20:36:43 +00:00
2c7246e11d ocaml: phase 5.1 euler6.ml baseline (Project Euler #6, sum^2 - sum_sq for 1..100 = 25164150)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Project Euler #6: difference between square of sum and sum of squares
for 1..100.

  let euler6 n =
    let sum = ref 0 in
    let sum_sq = ref 0 in
    for i = 1 to n do
      sum := !sum + i;
      sum_sq := !sum_sq + i * i
    done;
    !sum * !sum - !sum_sq

  euler6 100 = 5050^2 - 338350 = 25502500 - 338350 = 25164150

102 baseline programs total.
2026-05-09 20:16:49 +00:00
4840a9f660 ocaml: phase 5.1 euler2.ml baseline (Project Euler #2, even Fib <= 4M = 4613732)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Sum of even-valued Fibonacci numbers up to 4,000,000:

  let euler2 limit =
    let a = ref 1 in
    let b = ref 2 in
    let sum = ref 0 in
    while !a <= limit do
      if !a mod 2 = 0 then sum := !sum + !a;
      let c = !a + !b in
      a := !b;
      b := c
    done;
    !sum

Sequence: 1, 2, 3, 5, 8, 13, 21, 34, ... Only every third term
(2, 8, 34, 144, ...) is even. Sum below 4M: 4613732.

101 baseline programs total.
2026-05-09 20:05:45 +00:00
53968c2480 ocaml: phase 5.1 euler1.ml baseline (Project Euler #1, multiples of 3 or 5 below 1000 = 233168)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Project Euler #1: sum of all multiples of 3 or 5 below 1000.

  let euler1 limit =
    let sum = ref 0 in
    for i = 1 to limit - 1 do
      if i mod 3 = 0 || i mod 5 = 0 then sum := !sum + i
    done;
    !sum

  euler1 1000 = 233168

Trivial DSL exercise but symbolically meaningful: this is the 100th
baseline program.

100 baseline programs total — milestone.
2026-05-09 19:56:58 +00:00
3759aad7a6 ocaml: phase 5.1 anagram_groups.ml baseline (group by canonical anagram, 3 groups)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
canonical builds a sorted-by-frequency string representation:

  let canonical s =
    let chars = Array.make 26 0 in
    for i = 0 to String.length s - 1 do
      let k = Char.code s.[i] - Char.code 'a' in
      if k >= 0 && k < 26 then chars.(k) <- chars.(k) + 1
    done;
    expand into a-z order via a Buffer

For 'eat', 'tea', 'ate' -> all canonicalise to 'aet'. For 'tan',
'nat' -> 'ant'. For 'bat' -> 'abt'.

group_anagrams folds the input, accumulating per-key string lists;
final answer is Hashtbl.length (number of distinct groups):

  ['eat'; 'tea'; 'tan'; 'ate'; 'nat'; 'bat']  -> 3 groups

99 baseline programs total.
2026-05-09 19:47:21 +00:00
14575a9cd7 ocaml: phase 5.1 monotonic.ml baseline (monotonicity check, 4/5 inputs monotonic)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Tracks two bool refs (inc, dec). For each pair of consecutive
elements: if h < prev clear inc, if h > prev clear dec. Returns
inc OR dec at the end:

  let is_monotonic xs =
    match xs with
    | [] -> true
    | [_] -> true
    | _ ->
      let inc = ref true in
      let dec = ref true in
      let rec walk prev rest = ... in
      (match xs with h :: t -> walk h t | [] -> ());
      !inc || !dec

Five test cases:
  [1;2;3;4]    inc only        true
  [4;3;2;1]    dec only        true
  [1;2;1]      neither          false
  [5;5;5]      both (constant)  true
  []            empty            true (vacuous)
  sum = 4

98 baseline programs total.
2026-05-09 19:37:11 +00:00
be13f2daba ocaml: phase 5.1 majority_vote.ml baseline (Boyer-Moore majority, [3;3;4;2;4;4;2;4;4] = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
O(n) time / O(1) space majority vote algorithm:

  let majority xs =
    let cand = ref 0 in
    let count = ref 0 in
    List.iter (fun x ->
      if !count = 0 then begin
        cand := x;
        count := 1
      end else if x = !cand then count := !count + 1
      else count := !count - 1
    ) xs;
    !cand

The candidate is updated to the current element whenever count
reaches zero. When a strict majority exists, this guarantees the
result.

  majority [3;3;4;2;4;4;2;4;4] = 4   (5 of 9, > n/2)

97 baseline programs total.
2026-05-09 19:27:14 +00:00
810f61a1c1 ocaml: phase 5.1 adler32.ml baseline (Adler-32 of 'Wikipedia' = 300286872 = 0x11E60398)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 19s
Two running sums modulo 65521:

  a = (1 + sum of bytes)            mod 65521
  b = sum of running 'a' values     mod 65521
  checksum = b * 65536 + a

  let adler32 s =
    let a = ref 1 in
    let b = ref 0 in
    let m = 65521 in
    for i = 0 to String.length s - 1 do
      a := (!a + Char.code s.[i]) mod m;
      b := (!b + !a) mod m
    done;
    !b * 65536 + !a

For 'Wikipedia': 0x11E60398 = 300286872 (the canonical test value).

Tests for-loop accumulating two refs together, modular arithmetic,
and Char.code on s.[i].

96 baseline programs total.
2026-05-09 19:18:01 +00:00
37a514d566 ocaml: phase 5.1 gray_code.ml baseline (4-bit reflected Gray code, sum+len = 136)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Single-formula generation:

  gray[i] = i lxor (i lsr 1)

For n = 4, generates 16 values, each differing from its neighbour
by one bit. Output is a permutation of 0..15, so its sum equals the
natural-sequence sum 120; +16 entries -> 136.

Tests lsl / lxor / lsr together (the iter-127 bitwise ops) plus
Array.make / Array.fold_left.

95 baseline programs total.
2026-05-09 19:08:19 +00:00
7e838bb62b ocaml: phase 5.1 max_run.ml baseline (longest consecutive run, 4+1+0 = 5)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Walks list keeping a previous-value reference; increments cur on
match, resets to 1 otherwise. Uses 'Some y when y = x' guard pattern
in match for the prev-value comparison:

  let max_run xs =
    let max_so_far = ref 0 in
    let cur = ref 0 in
    let last = ref None in
    List.iter (fun x ->
      (match !last with
       | Some y when y = x -> cur := !cur + 1
       | _ -> cur := 1);
      last := Some x;
      if !cur > !max_so_far then max_so_far := !cur
    ) xs;
    !max_so_far

Three test cases:
  [1;1;2;2;2;2;3;3;1;1;1]   max run = 4 (the 2's)
  [1;2;3;4;5]                max run = 1
  []                          max run = 0
  sum = 5

Tests 'when' guard pattern in match arm + Option ref + ref-mutation
sequence inside List.iter closure body.

94 baseline programs total.
2026-05-09 18:58:32 +00:00
b2ff367c6b ocaml: phase 5.1 subseq_check.ml baseline (subsequence test, 3/5 yes)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Two-pointer walk:

  let is_subseq s t =
    let i = ref 0 in
    let j = ref 0 in
    while !i < n && !j < m do
      if s.[!i] = t.[!j] then i := !i + 1;
      j := !j + 1
    done;
    !i = n

advance i only on match; always advance j. Pattern matches if i
reaches n.

Five test cases:
  'abc'  in 'ahbgdc'    yes
  'axc'  in 'ahbgdc'    no  (no x in t)
  ''     in 'anything'  yes (empty trivially)
  'abc'  in 'abc'       yes
  'abcd' in 'abc'       no  (s longer)
  sum = 3

93 baseline programs total.
2026-05-09 18:49:00 +00:00
17a7a91d73 ocaml: phase 5.1 merge_intervals.ml baseline (LeetCode #56, total length 9+3 = 12)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s
Sort intervals by start, then sweep maintaining a current (cs, ce)
window — extend ce if next start <= ce, else push current and start
fresh:

  let merge_intervals xs =
    let sorted = List.sort (fun (a, _) (b, _) -> a - b) xs in
    let rec aux acc cur xs =
      match xs with
      | [] -> List.rev (cur :: acc)
      | (s, e) :: rest ->
        let (cs, ce) = cur in
        if s <= ce then aux acc (cs, max e ce) rest
        else aux (cur :: acc) (s, e) rest
    in
    match sorted with
    | [] -> []
    | h :: rest -> aux [] h rest

  [(1,3);(2,6);(8,10);(15,18);(5,9)]
  -> [(1,10); (15,18)]
  total length = 9 + 3 = 12

Tests List.sort with custom comparator using tuple patterns, plus
tuple destructuring in lambda + let-tuple from accumulator + match
arms.

92 baseline programs total.
2026-05-09 18:39:46 +00:00
df6efeb68e ocaml: phase 5.1 hamming.ml baseline (Hamming distance, 3+2-1 = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Counts position-wise differences between two strings of equal
length; returns -1 sentinel for length mismatch:

  let hamming s t =
    if String.length s <> String.length t then -1
    else
      let d = ref 0 in
      for i = 0 to String.length s - 1 do
        if s.[i] <> t.[i] then d := !d + 1
      done;
      !d

Three test cases:
  'karolin'  vs 'kathrin'    3   (positions 2,3,4)
  '1011101'  vs '1001001'    2   (positions 2,4)
  'abc'      vs 'abcd'      -1   (length mismatch)
  sum                        4

91 baseline programs total.
2026-05-09 18:27:50 +00:00
60e3ce1c96 ocaml: phase 5.1 xor_cipher.ml baseline (XOR roll-key encryption, round-trip = 601)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
For each character, XOR with the corresponding key char (key cycled
via 'i mod kn'):

  let xor_cipher key text =
    let buf = Buffer.create n in
    for i = 0 to n - 1 do
      let c = Char.code text.[i] in
      let k = Char.code key.[i mod kn] in
      Buffer.add_string buf (String.make 1 (Char.chr (c lxor k)))
    done;
    Buffer.contents buf

XOR is its own inverse, so encrypt + decrypt with the same key yields
the original. Test combines:
  - String.length decoded            = 6
  - decoded = 'Hello!'                  -> 1
  - 6 * 100 + 1                          = 601

Tests Char.code + Char.chr round-trip, the iter-127 lxor operator,
Buffer.add_string + String.make 1, and key-cycling via mod.

90 baseline programs total.
2026-05-09 18:19:02 +00:00
eb621240d7 ocaml: phase 5.1 simpson_int.ml baseline (Simpson 1/3 rule, integral_0^1 x^2 -> 10000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Composite Simpson's 1/3 rule with 100 panels:

  let simpson f a b n =
    let h = (b -. a) /. float_of_int n in
    let sum = ref (f a +. f b) in
    for i = 1 to n - 1 do
      let x = a +. float_of_int i *. h in
      let coef = if i mod 2 = 0 then 2.0 else 4.0 in
      sum := !sum +. coef *. f x
    done;
    h *. !sum /. 3.0

The 1-4-2-4-...-4-1 coefficient pattern is implemented via even/odd
index dispatch. Endpoints get coefficient 1.

For x^2 over [0, 1], exact value is 1/3 ~= 0.33333. Scaled by 30000
gives 9999.99..., int_of_float -> 10000.

Tests higher-order function (passing the integrand 'fun x -> x *. x'),
float arithmetic in for-loop, and float_of_int for index->x conversion.

89 baseline programs total.
2026-05-09 18:09:33 +00:00
e8a0c86de0 ocaml: phase 5.1 int_sqrt.ml baseline (Newton integer sqrt, 12+14+1000+1 = 1027)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Newton's method on integers, converging when y >= x:

  let isqrt n =
    if n < 2 then n
    else
      let x = ref n in
      let y = ref ((!x + 1) / 2) in
      while !y < !x do
        x := !y;
        y := (!x + n / !x) / 2
      done;
      !x

Test cases:
  isqrt 144      = 12     (perfect square)
  isqrt 200      = 14     (floor of sqrt(200) ~= 14.14)
  isqrt 1000000  = 1000
  isqrt 2        = 1
  sum            = 1027

Companion to newton_sqrt.ml (iter 124, float Newton). Tests integer
division semantics from iter 94 and a while-until-convergence loop.

88 baseline programs total.
2026-05-09 17:55:07 +00:00
4eeb7e59b4 ocaml: phase 5.1 grid_paths.ml baseline (count paths in 4x6 grid = 210)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
DP filling a flattened 2D array:

  dp.(0, 0)         = 1
  dp.(i, j)         = dp.(i-1, j) + dp.(i, j-1)
  index             = i * (n+1) + j

For a 4x6 grid (5x7 dp matrix), the count is C(10, 4) = 210.

Tests Array as 2D via row-major flatten + nested for + multi-step
conditional access (above/left guarded by 'if i > 0' / 'if j > 0').

87 baseline programs total.
2026-05-09 17:45:52 +00:00
f1df5b1b72 ocaml: phase 5.1 fib_doubling.ml baseline (Fibonacci by doubling, fib(40) = 102334155)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Uses the identities:
  F(2k)   = F(k) * (2 * F(k+1) - F(k))
  F(2k+1) = F(k)^2 + F(k+1)^2

to compute Fibonacci in O(log n) recursive depth instead of O(n).

  let rec fib_pair n =
    if n = 0 then (0, 1)
    else
      let (a, b) = fib_pair (n / 2) in
      let c = a * (2 * b - a) in
      let d = a * a + b * b in
      if n mod 2 = 0 then (c, d)
      else (d, c + d)

Each call returns the pair (F(n), F(n+1)). fib(40) = 102334155 fits
in JS safe-int (< 2^53). Tests tuple returns with let-tuple
destructuring + recursion on n / 2.

86 baseline programs total.
2026-05-09 17:36:24 +00:00
254ef0daff ocaml: phase 5.1 merge_two.ml baseline (merge two sorted lists, length*sum = 441)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Standard two-finger merge with nested match-in-match:

  let rec merge xs ys =
    match xs with
    | [] -> ys
    | x :: xs' ->
      match ys with
      | [] -> xs
      | y :: ys' ->
        if x <= y then x :: merge xs' (y :: ys')
        else y :: merge (x :: xs') ys'

Used as a building block in merge_sort.ml (iter 104) but called out
as its own baseline here.

  merge [1;4;7;10] [2;3;5;8;9]   = [1;2;3;4;5;7;8;9;10]
  length 9, sum 49, product 441.

85 baseline programs total.
2026-05-09 17:24:53 +00:00
b6e723fc3e ocaml: phase 5.1 pow_mod.ml baseline (modular exponentiation, sum = 738639)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Fast exponentiation by squaring with modular reduction:

  let rec pow_mod base exp m =
    if exp = 0 then 1
    else if exp mod 2 = 0 then
      let half = pow_mod base (exp / 2) m in
      (half * half) mod m
    else
      (base * pow_mod base (exp - 1) m) mod m

Even exponent halves and squares (O(log n)); odd decrements and
multiplies. mod-reduction at each step keeps intermediates bounded.

  pow_mod 2 30 1000003 + pow_mod 3 20 13 + pow_mod 5 17 100 = 738639

84 baseline programs total.
2026-05-09 17:15:47 +00:00
2e84492d96 ocaml: phase 5.1 tree_depth.ml baseline (binary tree depth, longest path = 4)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Same 'tree = Leaf | Node of int * tree * tree' ADT as iter-159
max_path_tree.ml, but the recursion ignores the value:

  let rec depth t = match t with
    | Leaf -> 0
    | Node (_, l, r) ->
      let dl = depth l in
      let dr = depth r in
      1 + (if dl > dr then dl else dr)

For the test tree:
        1
       /       2   3
     /     4   5
       /
      8

longest path is 1 -> 2 -> 5 -> 8, depth = 4.

Tests wildcard pattern in constructor 'Node (_, l, r)', two nested
let-bindings in match arm, inline if-as-expression for max.

83 baseline programs total.
2026-05-09 17:06:10 +00:00
1bde4e834f ocaml: phase 5.1 stable_unique.ml baseline (Hashtbl dedupe preserving order, 8+38 = 46)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Walk input with Hashtbl.mem + Hashtbl.add seen x () (unit-payload
turns the table into a set); on first occurrence cons to the result
list; reverse at the end:

  let stable_unique xs =
    let seen = Hashtbl.create 8 in
    let result = ref [] in
    List.iter (fun x ->
      if not (Hashtbl.mem seen x) then begin
        Hashtbl.add seen x ();
        result := x :: !result
      end
    ) xs;
    List.rev !result

For [3;1;4;1;5;9;2;6;5;3;5;8;9]:
  result = [3;1;4;5;9;2;6;8]   (input order, dupes dropped)
  length = 8, sum = 38         total = 46

Tests Hashtbl as a set abstraction (unit-payload), the rev-build
idiom, and 'not (Hashtbl.mem seen x)' membership negation.

82 baseline programs total.
2026-05-09 16:56:46 +00:00
554ef48c63 ocaml: phase 5.1 run_decode.ml baseline (RLE decode, expansion sum = 21)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Inverse of run_length.ml from iteration 130. Takes a list of
(value, count) tuples and expands:

  let rec rle_decode pairs =
    match pairs with
    | [] -> []
    | (x, n) :: rest ->
      let rec rep k = if k = 0 then [] else x :: rep (k - 1) in
      rep n @ rle_decode rest

  rle_decode [(1,3); (2,2); (3,4); (1,2)]
  = [1;1;1; 2;2; 3;3;3;3; 1;1]
  sum = 3 + 4 + 12 + 2 = 21.

Tests tuple-cons pattern, inner-let recursion, list concat (@), and
the 'List.fold_left (+) 0' invariant on encoding round-trips.

81 baseline programs total.
2026-05-09 16:47:56 +00:00
b7b841821c ocaml: phase 5.1 peano.ml baseline (Peano arithmetic, 5*6 = 30)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Defines unary Peano numerals with two recursive functions for
arithmetic:

  type peano = Zero | Succ of peano

  let rec plus a b = match a with
    | Zero -> b
    | Succ a' -> Succ (plus a' b)

  let rec mul a b = match a with
    | Zero -> Zero
    | Succ a' -> plus b (mul a' b)

mul is defined inductively: mul Zero _ = Zero; mul (Succ a) b =
b + (a * b).

  to_int (mul (from_int 5) (from_int 6)) = 30

The result is a Peano value with 30 nested Succ wrappers; to_int
unrolls them to a host int. Tests recursive ADT with a single-arg
constructor + four mutually-defined recursive functions (no rec/and
needed since each is defined separately).

80 baseline programs total — milestone.
2026-05-09 16:38:09 +00:00
2129e04bfd ocaml: phase 5.1 count_change.ml baseline (ways to make 50c from [1;2;5;10;25] = 406)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Companion to coin_change.ml (min coins). Counts distinct multisets
via the unbounded-knapsack DP:

  let count_ways coins target =
    let dp = Array.make (target + 1) 0 in
    dp.(0) <- 1;
    List.iter (fun c ->
      for i = c to target do
        dp.(i) <- dp.(i) + dp.(i - c)
      done
    ) coins;
    dp.(target)

Outer loop over coins, inner DP relaxes dp.(i) += dp.(i - c). The
order matters — coin in outer, amount in inner — to count multisets
rather than ordered sequences.

count_ways [1; 2; 5; 10; 25] 50 = 406.

79 baseline programs total.
2026-05-09 16:28:15 +00:00
89726ed6c2 ocaml: phase 5.1 paren_depth.ml baseline (max nesting depth, 3+3+1 = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
One-pass walk tracking current depth and a high-water mark:

  let max_depth s =
    let d = ref 0 in
    let m = ref 0 in
    for i = 0 to String.length s - 1 do
      if s.[i] = '(' then begin
        d := !d + 1;
        if !d > !m then m := !d
      end
      else if s.[i] = ')' then d := !d - 1
    done;
    !m

Three inputs:
  '((1+2)*(3-(4+5)))'   3   (innermost (4+5) at depth 3)
  '(((deep)))'           3
  '()()()'               1   (no nesting)
  sum                    7

Tests for-loop char comparison s.[i] = '(' and the high-water-mark
idiom with two refs.

78 baseline programs total.
2026-05-09 16:13:05 +00:00
5d71be364e ocaml: phase 5.1 pancake_sort.ml baseline (in-place pancake sort, 9 flips -> 910)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Each pass:
  1. find_max in [0..size-1]
  2. if max not at the right end, flip max to position 0 (if needed)
  3. flip the size-prefix to push max to the end

Inner 'flip k' reverses prefix [0..k] using two pointer refs lo/hi.
Inner 'find_max k' walks 1..k tracking the max-position.

  pancake_sort [3;1;4;1;5;9;2;6]
  = 9 flips * 100 + a.(0) + a.(n-1)
  = 9 * 100 + 1 + 9
  = 910

The output combines flip count and sorted endpoints, so the test
verifies both that the sort terminates and that it sorts correctly.

Tests two inner functions closing over the same Array, ref-based
two-pointer flip, and downto loop with conditional flip dispatch.

77 baseline programs total.
2026-05-09 16:03:22 +00:00
ce013fa138 ocaml: phase 5.1 fib_mod.ml baseline (Fibonacci mod prime, fib(100) mod 1000003 = 391360)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Iterative two-ref Fibonacci with modular reduction every step:

  let fib_mod n m =
    let a = ref 0 in
    let b = ref 1 in
    for _ = 1 to n do
      let c = (!a + !b) mod m in
      a := !b;
      b := c
    done;
    !a

The 100th Fibonacci is 354_224_848_179_261_915_075, well past JS
safe-int (2^53). Modular reduction every step keeps intermediate
values within int53 precision so the answer is exact in our
runtime. fib(100) mod 1000003 = 391360.

76 baseline programs total.
2026-05-09 15:53:47 +00:00
07de86365e ocaml: phase 5.1 luhn.ml baseline (Luhn check-digit, 2/4 inputs valid)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Walks digits right-to-left, doubles every other starting from the
second-from-right; if a doubled value > 9, subtract 9. Sum must be
divisible by 10:

  let luhn s =
    let n = String.length s in
    let total = ref 0 in
    for i = 0 to n - 1 do
      let d = Char.code s.[n - 1 - i] - Char.code '0' in
      let v = if i mod 2 = 1 then
        let dd = d * 2 in
        if dd > 9 then dd - 9 else dd
      else d
      in
      total := !total + v
    done;
    !total mod 10 = 0

Test cases:
  '79927398713'        valid
  '79927398710'        invalid
  '4532015112830366'   valid (real Visa test)
  '1234567890123456'   invalid
  sum = 2

Tests right-to-left index walk via 'n - 1 - i', Char.code '0'
arithmetic for digit conversion, and nested if-then-else.

75 baseline programs total.
2026-05-09 15:42:01 +00:00
5b38f4d499 ocaml: phase 5.1 triangle.ml baseline (Pascal-shape min path sum, 2+3+5+1 = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Bottom-up DP minimum-path through a triangle:

       2
      3 4
     6 5 7
    4 1 8 3

  let min_path_triangle rows =
    initialise dp from last row;
    for r = n - 2 downto 0 do
      for c = 0 to row_len - 1 do
        dp.(c) <- row.(c) + min(dp.(c), dp.(c+1))
      done
    done;
    dp.(0)

The optimal path 2 -> 3 -> 5 -> 1 sums to 11.

Tests downto loop, Array.of_list inside loop body, nested arr.(i)
reads + writes, and inline if-then-else for min.

74 baseline programs total.
2026-05-09 15:32:11 +00:00
a3a93c20b8 ocaml: phase 5.1 max_path_tree.ml baseline (max root-to-leaf sum, 1+3+7 = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Recursive ADT for binary trees:

  type tree = Leaf | Node of int * tree * tree

  let rec max_path t =
    match t with
    | Leaf -> 0
    | Node (v, l, r) ->
      let lp = max_path l in
      let rp = max_path r in
      v + (if lp > rp then lp else rp)

For the test tree:
       1
      /      2   3
    / \   \
   4   5   7

paths sum:    1+2+4=7, 1+2+5=8, 1+3+7=11.  max = 11.

Tests 3-arg Node constructor with positional arg destructuring, two
nested let-bindings, and if-then-else as an inline expression.

73 baseline programs total.
2026-05-09 15:22:28 +00:00
30b237a891 ocaml: phase 5.1 mod_inverse.ml baseline (extended Euclidean, inverse sum = 27)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Extended Euclidean returns a triple (gcd, x, y) such that
a*x + b*y = gcd:

  let rec ext_gcd a b =
    if b = 0 then (a, 1, 0)
    else
      let (g, x1, y1) = ext_gcd b (a mod b) in
      (g, y1, x1 - (a / b) * y1)

  let mod_inverse a m =
    let (_, x, _) = ext_gcd a m in
    ((x mod m) + m) mod m

Three invariants checked:

  inv(3, 11)  = 4      (3*4  = 12  = 1 mod 11)
  inv(5, 26)  = 21     (5*21 = 105 = 1 mod 26)
  inv(7, 13)  = 2      (7*2  = 14  = 1 mod 13)
  sum         = 27

Tests recursive triple-tuple return, tuple-pattern destructuring on
let-binding (with wildcard for unused fields), and nested
let-binding inside the recursive call site.

72 baseline programs total.
2026-05-09 15:11:46 +00:00
667dfcfd7c ocaml: phase 5.1 hist.ml baseline (Hashtbl int histogram, total * max = 75)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Three small functions:

  hist xs       build a Hashtbl of count-by-value
  max_value h   Hashtbl.fold to find the max bin
  total h       Hashtbl.fold to sum all bins

For the 15-element list [1;2;3;1;4;5;1;2;6;7;1;8;9;1;0]:
  total      = 15
  max_value  =  5 (the number 1 appears 5 times)
  product    = 75

Companion to bag.ml (string keys) and frequency.ml (char keys) —
same Hashtbl.fold + Hashtbl.find_opt pattern, exercised on int
keys this time.

71 baseline programs total.
2026-05-09 15:02:13 +00:00
7f8bf5f455 ocaml: phase 5.1 mortgage.ml baseline (monthly payment, 200k @ 5% / 30y = $1073)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Standard amortising-mortgage formula:

  payment = P * r * (1 + r)^n / ((1 + r)^n - 1)

where r = annual_rate / 12, n = years * 12.

  let payment principal annual_rate years =
    let r = annual_rate /. 12.0 in
    let n = years * 12 in
    let pow_r = ref 1.0 in
    for _ = 1 to n do pow_r := !pow_r *. (1.0 +. r) done;
    principal *. r *. !pow_r /. (!pow_r -. 1.0)

For 200,000 at 5% over 30 years: monthly payment ~= $1073.64,
int_of_float -> 1073.

Manual (1+r)^n via for-loop instead of Float.pow keeps the program
portable to any environment where pow is restricted.

Tests float arithmetic precedence, for-loop accumulation in a float
ref, int_of_float on the result.

70 baseline programs total — milestone.
2026-05-09 14:52:13 +00:00
a98d683e60 ocaml: phase 5.1 group_consec.ml baseline (group consecutive equals, 5*10+3 = 53)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Inner 'collect cur acc tail' walks the tail while head matches
'cur', accumulating into 'acc'. Returns (rev acc, remaining) on
first mismatch. Outer 'group' recurses on the remaining list.

  group [1;1;2;2;2;3;1;1;4]
   = [[1;1]; [2;2;2]; [3]; [1;1]; [4]]

  List.length groups        = 5
  List.length (gs.(1))      = 3
  5 * 10 + 3                = 53

Tests nested recursion (inner aux + outer recursion), tuple
destructuring 'let (g, tail) = ...' inside the outer match arm,
and List.nth.

69 baseline programs total.
2026-05-09 14:40:22 +00:00
a2f3c533b8 ocaml: phase 5.1 zigzag.ml baseline (interleave two lists, sum 1..10 = 55)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
One-liner that swaps the lists on every recursive call:

  let rec zigzag xs ys =
    match xs with
    | [] -> ys
    | x :: xs' -> x :: zigzag ys xs'

This works because each call emits the head of xs and recurses with
ys as the new xs and the rest of xs as the new ys.

  zigzag [1;3;5;7;9] [2;4;6;8;10] = [1;2;3;4;5;6;7;8;9;10]   sum = 55

Tests recursive list cons + arg-swap idiom that is concise but
non-obvious to readers expecting symmetric-handling.

68 baseline programs total.
2026-05-09 14:30:55 +00:00
0f2eb45f5c ocaml: phase 5.1 prefix_sum.ml baseline (precomputed sums + range queries, 14+25+27 = 66)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Two utility functions:

  prefix_sums xs   builds Array of len n+1 such that
                   arr.(i) = sum of xs[0..i-1]
  range_sum p lo hi = p.(hi+1) - p.(lo)

For [3;1;4;1;5;9;2;6;5;3]:
  range_sum 0 4   = 14   (3+1+4+1+5)
  range_sum 5 9   = 25   (9+2+6+5+3)
  range_sum 2 7   = 27   (4+1+5+9+2+6)
  sum             = 66

Tests List.iter mutating Array indexed by a ref counter, plus the
classic prefix-sum technique for O(1) range queries.

67 baseline programs total.
2026-05-09 14:21:24 +00:00
1c40fec8fa ocaml: phase 5.1 tic_tac_toe.ml baseline (3x3 winner check, X wins top row = 1)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Board as 9-element flat int array, 0=empty, 1=X, 2=O. Three
predicate functions:

  check_row b r       check_col b c       check_diag b

each return the winning player's mark or 0. Main 'winner' loops
i = 0..2 calling row(i)/col(i) then check_diag, threading via a
result ref.

Test board:
  X X X
  . O .
  . . O

X wins on row 0 -> winner returns 1.

Tests Array.of_list with row-major 'b.(r * 3 + c)' indexing,
multi-fn collaboration, and structural equality on int values.

66 baseline programs total.
2026-05-09 14:00:49 +00:00
b94a47a9a9 ocaml: phase 5.1 subset_sum.ml baseline (count subsets of [1..8] summing to 10 = 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 45s
Pure recursion — at each element, take it or don't:

  let rec count_subsets xs target =
    match xs with
    | [] -> if target = 0 then 1 else 0
    | x :: rest ->
      count_subsets rest target
      + count_subsets rest (target - x)

For [1;2;3;4;5;6;7;8] target 10, the recursion tree has 2^8 = 256
leaves. Returns 8 — number of subsets summing to 10:

  1+2+3+4, 1+2+7, 1+3+6, 1+4+5, 2+3+5, 2+8, 3+7, 4+6 = 8

Tests doubly-recursive list traversal pattern with single-arg list
+ accumulator-via-target.

65 baseline programs total.
2026-05-09 13:46:40 +00:00
7de014cd75 ocaml: phase 5.1 hailstone.ml baseline (Collatz length from 27 = 111 steps)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 40s
Iterative Collatz / hailstone sequence:

  let collatz_length n =
    let m = ref n in
    let count = ref 0 in
    while !m > 1 do
      if !m mod 2 = 0 then m := !m / 2
      else m := 3 * !m + 1;
      count := !count + 1
    done;
    !count

27 is the famous 'long-running' Collatz starter. Reaches a peak of
9232 mid-sequence and takes 111 steps to bottom out at 1.

64 baseline programs total.
2026-05-09 13:30:46 +00:00
0eef5bc8e6 ocaml: phase 5.1 twosum.ml baseline (LeetCode #1 one-pass hashmap, index sum = 5)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Walks list with List.iteri, checking if target - x is already in
the hashtable; if yes, the earlier index plus current is the
answer; otherwise record the current pair.

  twosum [2;7;11;15] 9   = (0, 1)   2+7
  twosum [3;2;4]     6   = (1, 2)   2+4
  twosum [3;3]       6   = (0, 1)   3+3

Sum of i+j over each pair: 1 + 3 + 1 = 5.

Tests Hashtbl.find_opt + add (the iter-99 cleanup), List.iteri, and
tuple destructuring on let-binding (iter 98 'let (i, j) = twosum
... in').

63 baseline programs total.
2026-05-09 13:15:05 +00:00
50981a2a9b ocaml: phase 5.1 bisect.ml baseline (root-finding, sqrt(2)*100 = 141)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Bisection method searching for f(x) = 0 in [lo, hi] over 50
iterations:

  let bisect f lo hi =
    let lo = ref lo and hi = ref hi in
    for _ = 1 to 50 do
      let mid = (!lo +. !hi) /. 2.0 in
      if f mid = 0.0 || f !lo *. f mid < 0.0 then hi := mid
      else lo := mid
    done;
    !lo

Solving x^2 - 2 = 0 in [1, 2] via 'bisect (fun x -> x *. x -. 2.0)
1.0 2.0' converges to ~1.41421356... -> int_of_float (r *. 100) =
141.

Tests:
  - higher-order function passing
  - multi-let 'let lo = ref ... and hi = ref ...'
  - float arithmetic
  - int_of_float truncate-toward-zero (iter 117)

62 baseline programs total.
2026-05-09 13:02:17 +00:00
05487b497d ocaml: phase 5.1 base_n.ml baseline (int to base-N string, length sum = 17)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
36-character digit alphabet '0..9A..Z' supports any base 2..36. Loop
divides the magnitude by base and prepends the digit:

  while !m > 0 do
    acc := String.make 1 digits.[!m mod base] ^ !acc;
    m := !m / base
  done

Special-cases n = 0 -> '0' and prepends '-' for negatives.

Test cases (length, since the strings differ in alphabet):
  255 hex      'FF'              2
  1024 binary  '10000000000'    11
  100 dec      '100'             3
  0 any base   '0'               1
  sum                            17

Combines digits.[i] (string indexing) + String.make 1 ch + String
concatenation in a loop.

61 baseline programs total.
2026-05-09 12:52:55 +00:00
af38d98583 ocaml: phase 5.1 prime_factors.ml baseline (trial-division, 360 factor sum = 17)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Three refs threading through a while loop:
  m       remaining quotient
  d       current divisor
  result  accumulator (built in reverse, List.rev at end)

  while !m > 1 do
    if !m mod !d = 0 then begin
      result := !d :: !result;
      m := !m / !d
    end else
      d := !d + 1
  done

360 = 2^3 * 3^2 * 5 factors to [2;2;2;3;3;5], sum 17.

60 baseline programs total — milestone.
2026-05-09 12:44:02 +00:00
f5122a9a5d ocaml: phase 5.1 atm.ml baseline (mutable record + exception + try/with, balance = 120)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Models a bank account using a mutable record + a user exception:

  type account = { mutable balance : int }
  exception Insufficient

  let withdraw acct amt =
    if amt > acct.balance then raise Insufficient
    else acct.balance <- acct.balance - amt

Sequence:
  start             100
  deposit 50        150
  withdraw 30       120
  withdraw 200      raises Insufficient
  handler returns   acct.balance        (= 120, transaction rolled back)

Combines mutable record fields, user exception declaration,
try-with-bare-pattern, and verifies that a raise in the middle of a
sequence doesn't leave a partial mutation.

59 baseline programs total.
2026-05-09 12:34:36 +00:00
097c7f4590 ocaml: phase 5.1 bf_full.ml baseline (full Brainfuck with [] loops, +++[.-] = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Extends the iter-92 brainfuck.ml subset interpreter with bracket
matching:

  '['  if mem[ptr] = 0, jump past matching ']'
       (forward depth-counting scan: '[' increments depth, ']' decrements)
  ']'  if mem[ptr] <> 0, jump back to matching '['
       (backward depth-counting scan)

Test program '+++[.-]':
  +++   set cell 0 = 3
  [     enter loop (cell != 0)
    .   acc += cell
    -   cell -= 1
  ]     loop while cell != 0
  result: acc = 3 + 2 + 1 = 6

Tests deeply nested while loops, mutable pc / ptr / acc, multi-arm
if/else if dispatch on chars + nested begin/end blocks for loop
body conditionals.

58 baseline programs total.
2026-05-09 12:24:48 +00:00
5c587c0f61 ocaml: phase 5.1 anagram_check.ml baseline (char-frequency array, 2/4 anagrams)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
to_counts builds a 256-slot int array of character frequencies:

  let to_counts s =
    let counts = Array.make 256 0 in
    for i = 0 to String.length s - 1 do
      let c = Char.code s.[i] in
      counts.(c) <- counts.(c) + 1
    done;
    counts

same_counts compares two arrays element-by-element via for loop +
bool ref. is_anagram composes them.

Four pairs:
  listen ~ silent       true
  hello !~ world        false
  anagram ~ nagaram     true
  abc !~ abcd           false (length differs)
  sum                   2

Exercises Array.make + arr.(i) + arr.(i) <- v + nested for loops +
Char.code + s.[i].

57 baseline programs total.
2026-05-09 12:14:32 +00:00
acc8b01ddb ocaml: phase 5.1 exception_user.ml baseline (user exception with payload, 4+5+7+10 = 26)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Defines a user exception with int payload:

  exception Negative of int

  let safe_sqrt n =
    if n < 0 then raise (Negative n)
    else <integer sqrt via while loop>

  let try_sqrt n =
    try safe_sqrt n with
    | Negative x -> -x

  try_sqrt  16  -> 4
  try_sqrt  25  -> 5
  try_sqrt -7   -> 7  (handler returns -(-7) = 7)
  try_sqrt 100  -> 10
  sum           -> 26

Tests exception declaration with int payload, raise with carry, and
try-with arm pattern-matching the constructor with payload binding.

56 baseline programs total.
2026-05-09 12:04:42 +00:00
027678f31e ocaml: phase 5.1 flatten_tree.ml baseline (parametric ADT flatten, sum 1..7 = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Defines a parametric tree:

  type 'a tree = Leaf of 'a | Node of 'a tree list

  let rec flatten t =
    match t with
    | Leaf x -> [x]
    | Node ts -> List.concat (List.map flatten ts)

Test tree has 3 levels of nesting:

  Node [Leaf 1; Node [Leaf 2; Leaf 3];
        Node [Node [Leaf 4]; Leaf 5; Leaf 6];
        Leaf 7]

flattens to [1;2;3;4;5;6;7] -> sum = 28.

Tests parametric ADT, mutual recursion via map+self, List.concat.

55 baseline programs total.
2026-05-09 11:52:19 +00:00
cca3a28206 ocaml: phase 5.1 gcd_lcm.ml baseline (Euclidean gcd + lcm, 12+12+36 = 60)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 19s
Two-line baseline:

  let rec gcd a b = if b = 0 then a else gcd b (a mod b)
  let lcm a b = a * b / gcd a b

  gcd 36 48  = 12
  lcm 4 6    = 12
  lcm 12 18  = 36
  sum        = 60

Tests mod arithmetic and the integer-division fix from iteration 94
(without truncate-toward-zero, 'lcm 4 6 = 4 * 6 / 2 = 12.0' rather
than the expected 12).

54 baseline programs total.
2026-05-09 11:42:52 +00:00
b8dfc080dd ocaml: phase 5.1 zip_unzip.ml baseline (zip/unzip round-trip, sum-product = 1000)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
zip walks both lists in lockstep, truncating at the shorter. unzip
uses tuple-pattern destructuring on the recursive result.

  let pairs = zip [1;2;3;4] [10;20;30;40] in
  let (xs, ys) = unzip pairs in
  List.fold_left (+) 0 xs * List.fold_left (+) 0 ys
  = 10 * 100
  = 1000

Exercises:
  - tuple-cons patterns in match scrutinee: 'match (xs, ys) with'
  - tuple constructor in return value: '(a :: la, b :: lb)'
  - the iter-98 let-tuple destructuring: 'let (la, lb) = unzip rest'

53 baseline programs total.
2026-05-09 11:33:30 +00:00
ac19b7aced ocaml: phase 5.1 bigint_add.ml baseline (digit-list bignum add, 1+18+9 = 28)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Recursive 4-arm match on (a, b) tuples threading a carry:

  match (a, b) with
  | ([], []) -> if carry = 0 then [] else [carry]
  | (x :: xs, []) -> (s mod 10) :: aux xs [] (s / 10)   where s = x + carry
  | ([], y :: ys) -> ...
  | (x :: xs, y :: ys) -> ...                           where s = x + y + carry

Little-endian digit lists. Three tests:

  [9;9;9] + [1]                 = [0;0;0;1]      (=1000, digit sum 1)
  [5;6;7] + [8;9;1]             = [3;6;9]        (=963, digit sum 18)
  [9;9;9;9;9;9;9;9] + [1]       length 9         (carry propagates 8x)

Sum = 1 + 18 + 9 = 28.

Exercises tuple-pattern match on nested list-cons with the integer
arithmetic and carry-threading idiom typical of multi-precision
implementations.

52 baseline programs total.
2026-05-09 11:19:23 +00:00
aa0a7fa1a2 ocaml: phase 5.1 expr_simp.ml baseline (symbolic simplifier, eval(simp e) = 22)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Recursive ADT with three constructors (Num/Add/Mul). simp does
bottom-up rewrite using algebraic identities:

  x + 0  -> x
  0 + x  -> x
  x * 0  -> 0
  0 * x  -> 0
  x * 1  -> x
  1 * x  -> x
  constant folding for Num + Num and Num * Num

Uses tuple pattern in nested match: 'match (simp a, simp b) with'.

  Add (Mul (Num 3, Num 5), Add (Num 0, Mul (Num 1, Num 7)))
   -> simp ->  Add (Num 15, Num 7)
   -> eval ->  22

51 baseline programs total.
2026-05-09 11:09:23 +00:00
bafa2410e4 ocaml: phase 5.1 mat_mul.ml baseline (3x3 row-major matrix multiply, sum = 621)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Triple-nested for loop with row-major indexing:

  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      for k = 0 to n - 1 do
        c.(i * n + j) <- c.(i * n + j) + a.(i * n + k) * b.(k * n + j)
      done
    done
  done

For 3x3 matrices A=[[1..9]] and B=[[9..1]], the resulting C has sum
621. Tests deeply nested for loops on Array, Array.make + arr.(i) +
arr.(i) <- v + Array.fold_left.

50 baseline programs total — milestone.
2026-05-09 11:00:00 +00:00
a91ff62730 ocaml: phase 5.1 bsearch.ml baseline (binary search, position sum = 7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Iterative binary search on a sorted int array:

  let bsearch arr target =
    let n = Array.length arr in
    let lo = ref 0 and hi = ref (n - 1) in
    let found = ref (-1) in
    while !lo <= !hi && !found = -1 do
      let mid = (!lo + !hi) / 2 in
      if arr.(mid) = target then found := mid
      else if arr.(mid) < target then lo := mid + 1
      else hi := mid - 1
    done;
    !found

For [1;3;5;7;9;11;13;15;17;19;21]:
  bsearch a 13   = 6
  bsearch a 5    = 2
  bsearch a 100  = -1
  sum            = 7

Exercises Array.of_list + arr.(i) + multi-let 'let lo = ... and
hi = ...' + while + multi-arm if/else if/else.

49 baseline programs total.
2026-05-09 10:40:49 +00:00
073ea44fdb ocaml: phase 5.1 palindrome.ml baseline (two-pointer check, 4/6 inputs match)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Two-pointer palindrome check:

  let is_palindrome s =
    let n = String.length s in
    let rec check i j =
      if i >= j then true
      else if s.[i] <> s.[j] then false
      else check (i + 1) (j - 1)
    in
    check 0 (n - 1)

Tests on six strings:
  racecar = true
  hello   = false
  abba    = true
  ''      = true (vacuously, i >= j on entry)
  'a'     = true
  'ab'    = false

Sum = 4.

Uses s.[i] <> s.[j] (string-get + structural inequality), recursive
2-arg pointer advancement, and a multi-clause if/else if/else for
the three cases.

48 baseline programs total.
2026-05-09 10:31:22 +00:00
aee7226b9c ocaml: phase 5.1 coin_change.ml baseline (DP, 67c with [1;5;10;25] = 6 coins)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Bottom-up dynamic programming. dp[i] = minimum coins to make
amount i.

  let dp = Array.make (target + 1) (target + 1) in   (* sentinel *)
  dp.(0) <- 0;
  for i = 1 to target do
    List.iter (fun c ->
      if c <= i && dp.(i - c) + 1 < dp.(i) then
        dp.(i) <- dp.(i - c) + 1
    ) coins
  done

Sentinel 'target + 1' means impossible — any real solution uses at
most 'target' coins.

  coin_change [1; 5; 10; 25] 67   = 6   (= 25+25+10+5+1+1)

Exercises Array.make + arr.(i) + arr.(i) <- v + nested
for/List.iter + guard 'c <= i'.

47 baseline programs total.
2026-05-09 10:21:11 +00:00
b3d5da5361 ocaml: phase 5.1 kadane.ml baseline (max subarray sum = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Kadane's algorithm in O(n):

  let max_subarray xs =
    let max_so_far = ref min_int in
    let cur = ref 0 in
    List.iter (fun x ->
      cur := max x (!cur + x);
      max_so_far := max !max_so_far !cur
    ) xs;
    !max_so_far

For [-2;1;-3;4;-1;2;1;-5;4] the optimal subarray is [4;-1;2;1] = 6.

Exercises min_int (iter 94), max as global, ref / ! / :=, and
List.iter with two side-effecting steps in one closure body.

46 baseline programs total.
2026-05-09 10:07:12 +00:00
da6d8e39c9 ocaml: phase 5.1 pascal.ml baseline (Pascal triangle row 10 middle = C(10,5) = 252)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
next_row prepends 1, walks adjacent pairs (x, y) emitting x+y,
appends a final 1:

  let rec next_row prev =
    let rec aux a =
      match a with
      | [_] -> [1]
      | x :: y :: rest -> (x + y) :: aux (y :: rest)
      | [] -> []
    in
    1 :: aux prev

row n iterates next_row n times starting from [1] using a ref +
'for _ = 1 to n do r := next_row !r done'.

  row 10 = [1;10;45;120;210;252;210;120;45;10;1]
  List.nth (row 10) 5 = 252 = C(10, 5)

Exercises three-arm match including [_] singleton wildcard, x :: y
:: rest binding, and the for-loop with wildcard counter. 45 baseline
programs total.
2026-05-09 09:57:18 +00:00
32aba1823d ocaml: phase 5.1 run_length.ml baseline (RLE, sum-of-counts = 11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Run-length encoding via tail-recursive 4-arg accumulator:

  let rle xs =
    let rec aux xs cur n acc =
      match xs with
      | [] -> List.rev ((cur, n) :: acc)
      | h :: t ->
        if h = cur then aux t cur (n + 1) acc
        else aux t h 1 ((cur, n) :: acc)
    in
    match xs with
    | [] -> []
    | h :: t -> aux t h 1 []

  rle [1;1;1;2;2;3;3;3;3;1;1] = [(1,3);(2,2);(3,4);(1,2)]
  sum of counts                = 11 (matches input length)

The sum-of-counts test verifies that the encoding preserves total
length — drops or duplicates would diverge.

44 baseline programs total.
2026-05-09 09:47:25 +00:00
3be2dc6e78 ocaml: phase 5.1 grep_count.ml baseline (substring-aware line filter, 3 matches)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Defines a recursive str_contains that walks the haystack with
String.sub to find a needle substring. Real OCaml's String.contains
only accepts a single char, so this baseline implements its own
substring search to stay portable.

  let rec str_contains s sub i =
    if i + sl > nl then false
    else if String.sub s i sl = sub then true
    else str_contains s sub (i + 1)

count_matching splits text on newlines, folds with the predicate.

  'the quick brown fox\nfox runs fast\nthe dog\nfoxes are clever'
   needle = 'fox'
   matches = 3 (lines 1, 2, 4 — 'foxes' contains 'fox')

43 baseline programs total.
2026-05-09 09:34:40 +00:00
b0cbdaf713 ocaml: phase 5.1 pretty_table.ml baseline (Buffer + Printf widths, len = 64)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Builds a 4-row scoreboard via Buffer + Printf.sprintf:

  Buffer.add_string buf (Printf.sprintf '%-10s %4d\n' name score)

Each row is exactly 16 chars regardless of actual name/score length:
  10 name padding + 1 space + 4 score padding + 1 newline.
4 rows -> 64 chars total.

Combines:
  - Buffer.add_string + Printf.sprintf
  - %-Ns left-justified string and %Nd right-justified int width
  - List.iter on tuple-pattern args (iter 101 fun (a, b))

42 baseline programs total.
2026-05-09 09:24:41 +00:00
aaaf054441 ocaml: phase 4 bitwise ops land/lor/lxor/lsl/lsr/asr + bits.ml baseline (+5 tests, 607 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
The binop precedence table already had land/lor/lxor/lsl/lsr/asr
(iter 0 setup) but eval-op fell through to 'unknown operator' for
all of them. SX doesn't expose host bitwise primitives, so each is
implemented in eval.sx via arithmetic on the host:

  land/lor/lxor:  mask & shift loop, accumulating 1<<k digits
  lsl k:          repeated * 2 k times
  lsr k:          repeated floor (/ 2) k times
  asr:            aliased to lsr (no sign extension at our bit width)

bits.ml baseline: popcount via 'while m > 0 do if m land 1 = 1 then
... ; m := m lsr 1 done'. Sum of popcount(1023, 5, 1024, 0xff) = 10
+ 2 + 1 + 8 = 21.

  5 land 3 = 1
  5 lor 3 = 7
  5 lxor 3 = 6
  1 lsl 8 = 256
  256 lsr 4 = 16

41 baseline programs total.
2026-05-09 09:15:00 +00:00
70b9b4f6cf ocaml: phase 5.1 ackermann.ml baseline (ack(3, 4) = 125)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Classic Ackermann function:

  let rec ack m n =
    if m = 0 then n + 1
    else if n = 0 then ack (m - 1) 1
    else ack (m - 1) (ack m (n - 1))

ack(3, 4) = 125, expanding to ~6700 evaluator frames — a useful
stress test of the call stack and control transfer. Real OCaml
evaluates this in milliseconds; ours takes ~2 minutes on a
contended host but completes correctly.

40 baseline programs total.
2026-05-09 08:50:12 +00:00
095bb62ef9 ocaml: phase 5.1 rpn.ml baseline (Reverse Polish Notation evaluator, [3 4 + 2 * 5 -] = 9)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Stack-based RPN evaluator:

  let eval_rpn tokens =
    let stack = Stack.create () in
    List.iter (fun tok ->
      if tok is operator then
        let b = Stack.pop stack in
        let a = Stack.pop stack in
        Stack.push (apply tok a b) stack
      else
        Stack.push (int_of_string tok) stack
    ) tokens;
    Stack.pop stack

For tokens [3 4 + 2 * 5 -]:
  3 4 +  ->  7
  7 2 *  ->  14
  14 5 - ->  9

Exercises Stack.create / push / pop, mixed branch on string
equality, multi-arm if/else if for operator dispatch, int_of_string
for token parsing.

39 baseline programs total.
2026-05-09 08:39:56 +00:00
13fb1bd7a9 ocaml: phase 5.1 newton_sqrt.ml baseline (Newton's method, sqrt(2)*1000 = 1414)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Newton's method for square root:

  let sqrt_newton x =
    let g = ref 1.0 in
    for _ = 1 to 20 do
      g := (!g +. x /. !g) /. 2.0
    done;
    !g

20 iterations is more than enough to converge for x=2 — result is
~1.414213562. Multiplied by 1000 and int_of_float'd: 1414.

First baseline exercising:
  - for _ = 1 to N do ... done (wildcard loop variable)
  - pure float arithmetic with +. /.
  - the int_of_float truncate-toward-zero fix from iter 117

38 baseline programs total.
2026-05-09 08:29:01 +00:00
39f4c7a9a8 ocaml: phase 5.1 hanoi.ml baseline (Tower of Hanoi move count, n=10 -> 1023)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Classic doubly-recursive solution returning the move count:

  hanoi n from to via =
    if n = 0 then 0
    else hanoi (n-1) from via to + 1 + hanoi (n-1) via to from

For n = 10, returns 2^10 - 1 = 1023.

Exercises 4-arg recursion, conditional base case, and tail-position
addition. Uses 'to_' instead of 'to' for the destination param to
avoid collision with the 'to' keyword in for-loops — the OCaml
conventional workaround.

37 baseline programs total.
2026-05-09 08:21:19 +00:00
1a828d5b9f ocaml: phase 5.1 validate.ml baseline (Either-based validation, 3 errs * 100 + 117 = 417)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
validate_int returns Left msg on empty / non-digit, Right
(int_of_string s) on a digit-only string. process folds inputs with a
tuple accumulator (errs, sum), branching on the result.

  ['12'; 'abc'; '5'; ''; '100'; 'x']
  -> 3 errors (abc, '', x), valid sum = 12+5+100 = 117
  -> errs * 100 + sum = 417

Exercises:
  - Either constructors used bare (Left/Right without 'Either.'
    qualification)
  - char range comparison: c >= '0' && c <= '9'
  - tuple-pattern destructuring on let-binding (iter 98)
  - recursive helper defined inside if-else
  - List.fold_left with tuple accumulator

36 baseline programs total.
2026-05-09 08:11:07 +00:00
5c70747ac7 ocaml: phase 5.1 word_freq.ml baseline (Map.Make on String, distinct = 8)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
First baseline using Map.Make on a string-keyed map:

  module StringOrd = struct
    type t = string
    let compare = String.compare
  end
  module SMap = Map.Make (StringOrd)

  let count_words text =
    let words = String.split_on_char ' ' text in
    List.fold_left (fun m w ->
      let n = match SMap.find_opt w m with
              | Some n -> n
              | None -> 0
      in
      SMap.add w (n + 1) m
    ) SMap.empty words

For 'the quick brown fox jumps over the lazy dog' ('the' appears
twice), SMap.cardinal -> 8.

Complements bag.ml (Hashtbl-based) and unique_set.ml (Set.Make)
with a sorted Map view of the same kind of counting problem. 35
baseline programs total.
2026-05-09 08:01:21 +00:00
c272b1ea04 ocaml: phase 6 Either module + Hashtbl.copy (+4 tests, 602 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Either module (mirrors OCaml 4.12+ stdlib):
  left x / right x
  is_left / is_right
  find_left / find_right (return Option)
  map_left / map_right (single-side mappers)
  fold lf rf e            (case dispatch)
  equal eq_l eq_r a b
  compare cmp_l cmp_r a b (Left < Right)

Constructors are bare 'Left x' / 'Right x' (OCaml 4.12+ exposes them
directly without an explicit type-decl).

Hashtbl.copy:
  build a fresh cell with _hashtbl_create
  walk _hashtbl_to_list and re-add each (k, v)
  mutating one copy doesn't touch the other
  (Hashtbl.length t + Hashtbl.length t2 = 3 after fork-and-add
   verifies that adds to t2 don't appear in t)
2026-05-09 07:50:24 +00:00
9a8bbff5b2 ocaml: phase 5.1 json_pretty.ml baseline (recursive ADT to string, len = 24)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Defines a JSON-like algebraic data type:

  type json =
    | JNull
    | JBool of bool
    | JInt of int
    | JStr of string
    | JList of json list

Recursively serialises to a string via match-on-constructor, then
measures the length:

  JList [JInt 1; JBool true; JNull; JStr 'hi'; JList [JInt 2; JInt 3]]
  -> '[1,true,null,"hi",[2,3]]'   length 24

Exercises:
  - five-constructor ADT (one nullary, three single-arg, one list-arg)
  - recursive match
  - String.concat ',' (List.map to_string xs)
  - string-cat with embedded escaped quotes

34 baseline programs total.
2026-05-09 07:41:01 +00:00
75a1adbbd5 ocaml: phase 5.1 shuffle.ml baseline (Fisher-Yates with deterministic Random)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
In-place Fisher-Yates shuffle using:

  Random.init 42                  deterministic seed
  let a = Array.of_list xs
  for i = n - 1 downto 1 do       reverse iteration
    let j = Random.int (i + 1)
    let tmp = a.(i) in
    a.(i) <- a.(j);
    a.(j) <- tmp
  done

Sum is invariant under permutation, so the test value (55 for
[1..10] = 1+2+...+10) verifies the shuffle is a valid permutation
regardless of which permutation the seed yields.

Exercises Random.init / Random.int + Array.of_list / to_list /
length / arr.(i) / arr.(i) <- v + downto loop + multi-statement
sequencing within for-body.

33 baseline programs total.
2026-05-09 07:31:33 +00:00
90418c120b ocaml: phase 5.1 pi_leibniz.ml baseline + int_of_float fix (1000 terms x 100 = 314)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
pi_leibniz.ml: Leibniz formula for pi.

  pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...
  pi  ~= 4 * sum_{k=0}^{n-1} (-1)^k / (2k+1)

For n=1000, pi ~= 3.140593. Multiply by 100 and int_of_float -> 314.

Side-quest: int_of_float was wrongly defined as identity in
iteration 94. Fixed to:

  let int_of_float f =
    if f < 0.0 then _float_ceil f else _float_floor f

(truncate toward zero, mirroring real OCaml's int_of_float). The
identity definition was a stub from when integer/float dispatch was
not yet split — now they're separate, the stub is wrong.

Float.to_int still uses floor since OCaml's docs say the result is
unspecified for nan / out-of-range; close enough for our scope.

32 baseline programs total.
2026-05-09 07:19:52 +00:00
e42ff3b1f6 ocaml: phase 6 Float module fleshed out (+6 tests, 598 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
New Float members:
  zero / one / minus_one
  abs / neg
  add / sub / mul / div     (lift host '+.' '-.' '*.' '/.')
  max / min                 (if-based)
  equal / compare           (Float.compare returns -1 / 0 / 1)
  to_int                    (host floor)
  of_int                    (identity in dynamic runtime)
  of_string                 (delegates to _int_of_string)

Aligns Float with Int's API and lets baselines use Float.add /
Float.compare / etc without lifting the symbols themselves.

  Float.add 3.5 4.5         = 8
  Float.compare 2.5 5.0     = -1
  Float.abs -3.7            = 3.7
  Float.max 3.14 2.71       = 3.14
2026-05-09 07:09:29 +00:00
97a8c06690 ocaml: phase 5.1 balance.ml baseline (paren/bracket/brace balance via Stack)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 51s
is_balanced walks a string; on each char:
  '(', '[', '{'  -> Stack.push c
  ')', ']', '}'  -> require stack non-empty AND top = expected opener,
                     else mark ok = false
  others         -> skip

At end: !ok && Stack.is_empty stack.

Five test cases:
  '({[abc]d}e)'  -> true
  '(a]'           -> false  (no matching opener)
  '{[}]'          -> false  (mismatched closer)
  '(())'          -> true
  ''              -> true

Sum of (if balanced then 1 else 0) -> 3.

Exercises:
  Stack.create / push / pop / is_empty
  s.[!i] string indexing
  while loop + bool ref short-circuit
  multi-arm if/else if/else if dispatch

31 baseline programs total.
2026-05-09 06:59:22 +00:00
0c3b5d21fa ocaml: phase 5.1 safe_div.ml baseline + Result.equal/compare/iter_error (+3 tests, 592 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
safe_div.ml: integer division returning Result. Sum-safe folds pairs,
skipping the Error branches.

  [(10,2); (20,4); (30,0); (50,5)]   ->  5 + 5 + 0 + 10 = 20

Result module additions (mirroring real OCaml's signatures):

  equal eq_ok eq_err a b
  compare cmp_ok cmp_err a b      Ok < Error (i.e. Ok x compared to
                                  Error e returns -1)
  iter_error f r

  Result.equal (=) (=) (Ok 1) (Ok 1)              = true
  Result.compare compare compare (Ok 5) (Ok 3)    = 1
  Result.compare compare compare (Ok 1) (Error _) = -1

30 baseline programs total.
2026-05-09 06:47:47 +00:00
98ba772acd ocaml: phase 6 List.equal / List.compare (+5 tests, 589 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Both take an inner predicate / comparator and walk both lists in
lockstep:

  equal eq a b     short-circuits on first mismatch
  compare cmp a b  -1 if a is a strict prefix
                    1 if b is
                    0 if both empty
                    otherwise first non-zero element comparison

Mirrors real OCaml's signatures.

  List.equal (=) [1;2;3] [1;2;3]            = true
  List.equal (=) [1;2;3] [1;2;4]            = false
  List.compare compare [1;2;3] [1;2;4]      = -1
  List.compare compare [1;2] [1;2;3]        = -1
  List.compare compare [] []                = 0
2026-05-09 06:35:42 +00:00
4d32c80a99 ocaml: phase 6 Bool module + Option.equal/Option.compare (+5 tests, 584 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s
Bool module:
  equal a b      = a = b
  compare a b    = 0 if equal, 1 if a, -1 if b (false < true)
  to_string      'true' / 'false'
  of_string s    = s = 'true'
  not_           wraps host not
  to_int         true=1, false=0

Option additions (take eq/cmp parameter for the inner value):
  equal eq a b   None=None, otherwise eq the inner values
  compare cmp a b  None < Some _; otherwise cmp inner

  Option.equal (=) (Some 1) (Some 1)        = true
  Option.equal (=) (Some 1) None             = false
  Option.compare compare (Some 5) (Some 3)  = 1
2026-05-09 06:26:33 +00:00
ddd1e40d00 ocaml: phase 5.1 bag.ml baseline + String.equal/compare/cat/empty (+3 tests, 579 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
bag.ml: split a sentence on spaces, count each word in a Hashtbl,
return the maximum count via Hashtbl.fold.

  count_words 'the quick brown fox jumps over the lazy dog the fox'
  -> Hashtbl with 'the' = 3 as the max
  -> 3

Exercises String.split_on_char + Hashtbl.find_opt/replace +
Hashtbl.fold (k v acc -> ...). Together with frequency.ml from
iter 84 we now have two Hashtbl-counting baselines exercising
slightly different idioms. 29 baseline programs total.

String additions:
  equal a b      = a = b
  compare a b    = -1 / 0 / 1 via host < / >
  cat a b        = a ^ b
  empty          = '' (constant)
2026-05-09 06:15:03 +00:00
7ca5bfbb70 ocaml: phase 5.1 fraction.ml baseline (rational arithmetic, 4/3 -> num+den=7)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Defines:

  type frac = { num : int; den : int }
  let rec gcd a b = if b = 0 then a else gcd b (a mod b)
  let make n d =                        (* canonicalise: gcd-reduce and
                                           force den > 0 *)
  let add x y = make (x.num * y.den + y.num * x.den) (x.den * y.den)
  let mul x y = make (x.num * y.num) (x.den * y.den)

Test:
  let r = add (make 1 2) (make 1 3) in     (* 5/6 *)
  let s = mul (make 2 3) (make 3 4) in     (* 1/2 *)
  let t = add r s in                       (* 5/6 + 1/2 = 4/3 *)
  t.num + t.den                            (* = 7 *)

Exercises records, recursive gcd, mod, abs, integer division (the
truncate-toward-zero semantics from iter 94 are essential here —
make would diverge from real OCaml's behaviour with float division).
28 baseline programs total.
2026-05-09 06:05:31 +00:00
2d519461c4 ocaml: phase 6 Seq module (eager, list-backed) (+4 tests, 576 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Real OCaml's Seq.t is 'unit -> Cons of elt * Seq.t | Nil' — a lazy
thunk that lets you build infinite sequences. Ours is just a list,
which gives the right shape for everything in baseline programs that
don't rely on laziness (taking from infinite sequences would force
memory).

API: empty, cons, return, is_empty, iter, iteri, map, filter,
filter_map, fold_left, length, take, drop, append, to_list,
of_list, init, unfold.

unfold takes a step fn 'acc -> Option (elt * acc)' and threads
through until it returns None:

  Seq.fold_left (+) 0
    (Seq.unfold (fun n -> if n > 4 then None
                          else Some (n, n + 1))
                1)
  = 1 + 2 + 3 + 4 = 10
2026-05-09 05:56:10 +00:00
24416f8cef ocaml: phase 5.1 unique_set.ml baseline (Set.Make + IntOrd, count = 9)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
First baseline that exercises the functor pipeline end to end:

  module IntOrd = struct
    type t = int
    let compare a b = a - b
  end

  module IntSet = Set.Make (IntOrd)

  let unique_count xs =
    let s = List.fold_left (fun s x -> IntSet.add x s) IntSet.empty xs in
    IntSet.cardinal s

Counts unique elements in [3;1;4;1;5;9;2;6;5;3;5;8;9;7;9]:
  {1,2,3,4,5,6,7,8,9} -> 9

The input has 15 elements with 9 unique values. The 'type t = int'
declaration in IntOrd is required by real OCaml; OCaml-on-SX is
dynamic and would accept it without, but we include it for source
fidelity. 27 baseline programs total.
2026-05-09 05:44:35 +00:00
ec12b721e8 ocaml: phase 4 Set.Make / Map.Make functor application smoke tests (+3 tests, 572 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Functors were already wired through ocaml-make-functor in eval.sx
(curried host closure consuming module dicts) but had no explicit
tests for the user-defined Ord application path. This commit adds
three smoke tests that confirm:

  module IntOrd = struct let compare a b = a - b end
  module S = Set.Make (IntOrd)

  S.elements (fold-add [5;1;3;1;5])    sums to 9 (dedupe + sort)
  S.mem 2 (S.add 1 (S.add 2 (S.add 3 S.empty)))   = true
  M.cardinal (M.add 1 'a' (M.add 2 'b' M.empty))  = 2

The Ord parameter is properly threaded through the functor body —
elements are sorted in compare order and dedupe works.
2026-05-09 05:35:19 +00:00
5d33f8f20b ocaml: phase 6 Filename module + Char.compare/equal/escaped (+7 tests, 569 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
Filename module (forward-slash only, no Windows-separator detection):

  basename '/foo/bar/baz.ml'        = 'baz.ml'
  dirname  '/foo/bar/baz.ml'        = '/foo/bar'
  extension 'baz.tar.gz'            = '.gz'
  chop_extension 'hello.ml'         = 'hello'
  concat 'a' 'b'                    = 'a/b'
  is_relative 'a/b'                 = true
  current_dir_name = '.', parent_dir_name = '..', dir_sep = '/'

Char additions:

  equal a b                         = (a = b)
  compare a b                       = code(a) - code(b)
  escaped '\n'                      = '\\n'    (likewise t, r, \\, ")
2026-05-09 05:24:37 +00:00
7773c40337 ocaml: phase 4 basic labeled / optional argument syntax (label dropped) (+3 tests, 562 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Three parser changes:

  1. at-app-start? returns true on op '~' or '?' so the app loop
     keeps consuming labeled args.
  2. The app arg parser handles:
       ~name:VAL    drop label, parse VAL as the arg
       ?name:VAL    same
       ~name        punning -- treat as (:var name)
       ?name        same
  3. try-consume-param! drops '~' or '?' and treats the following
     ident as a regular positional param name.

Caveats:
  - Order in the call must match definition order; we don't reorder
    by label name.
  - Optional args don't auto-wrap in Some, so the function body sees
    the raw value for ?x:V.

Lets us write idiomatic-looking OCaml even though the runtime is
positional underneath:

  let f ~x ~y = x + y in f ~x:3 ~y:7         = 10
  let x = 4 in let y = 5 in f ~x ~y          = 20  (punning)
  let f ?x ~y = x + y in f ?x:1 ~y:2         = 3
2026-05-09 05:12:34 +00:00
7c40506571 ocaml: phase 5.1 merge_sort.ml baseline (user mergesort, sum=44)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
User-implemented mergesort that exercises features added across the
last few iterations:

  let rec split lst = match lst with
    | x :: y :: rest ->
      let (a, b) = split rest in       (* iter 98 let-tuple destruct *)
      (x :: a, y :: b)
    | ...

  let rec merge xs ys = match xs with
    | x :: xs' ->
      match ys with                     (* nested match-in-match *)
      | y :: ys' -> ...

  ...

  List.fold_left (+) 0 (sort [...])     (* iter 89 (op) section *)

Sum of [3;1;4;1;5;9;2;6;5;3;5] = 44 regardless of order, so the
result is also a smoke test of the implementation correctness — if
merge_sort drops or duplicates an element the sum diverges. 26
baseline programs total.
2026-05-09 05:00:50 +00:00
82ffc695a5 ocaml: phase 4 top-level 'let f (a, b) = body' tuple-param decl (+3 tests, 559 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s
parse-decl-let lives in the outer ocaml-parse-program scope and does
not have access to parse-pattern (which is local to ocaml-parse).
Source-slicing approach instead:

  1. detect '(IDENT, ...)' in collect-params
  2. scan tokens to the matching ')' (tracking nested parens)
  3. slice the pattern source string from src
  4. push (synth_name, pat_src) onto tuple-srcs

Then after collecting params, the rhs source string gets wrapped with
'match SN with PAT_SRC -> (RHS_SRC)' for each tuple-param,
innermost-first, and the final string is fed through ocaml-parse.

End result is the same AST shape as the iteration-102 inner-let
case: a function whose body destructures a synthetic name.

  let f (a, b) = a + b ;; f (3, 7)            = 10
  let g x (a, b) = x + a + b ;; g 1 (2, 3)    = 6
  let h (a, b) (c, d) = a * b + c * d
  ;; h (1, 2) (3, 4)                          = 14
2026-05-09 04:51:11 +00:00
b526d81a4c ocaml: phase 4 'let f (a, b) = body' tuple-param on inner-let (+3 tests, 556 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Mirrors iteration 101's parse-fun change inside parse-let's
parse-one!:

  - same '(IDENT, ...)' detection on collect-params
  - same __pat_N synth name for the function param
  - same innermost-first match-wrapping

Difference: for inner-let the wrapping is applied to the rhs of the
let-binding (which is the function value), not directly to a fun
body.

  let f (a, b) = a + b in f (3, 7)            = 10
  let g x (a, b) = x + a + b in g 1 (2, 3)    = 6
  let h (a, b) (c, d) = a * b + c * d
    in h (1, 2) (3, 4)                        = 14
2026-05-09 04:36:33 +00:00
64f4f10c32 ocaml: phase 4 'fun (a, b) -> body' tuple-param destructuring (+4 tests, 553 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
parse-fun's collect-params now detects '(IDENT, ...)' as a
tuple-pattern parameter (lookahead at peek-tok-at 1/2 distinguishes
from '(x : T)' and '()' cases that try-consume-param! already
handles). For each tuple param it:

  1. parse-pattern to get the full pattern AST
  2. generate a synthetic __pat_N name as the actual fun parameter
  3. push (synth_name, pattern) onto tuple-binds

After parsing the body, wraps it innermost-first with one
'match __pat_N with PAT -> ...' per tuple-param. The user-visible
result is a (:fun (params...) body) where params are all simple
names but the body destructures.

Also retroactively simplifies Hashtbl.keys/values from
'fun pair -> match pair with (k, _) -> k' to plain
'fun (k, _) -> k', closing the iteration-99 workaround.

  (fun (a, b) -> a + b) (3, 7)              = 10
  List.map (fun (a, b) -> a * b)
           [(1, 2); (3, 4); (5, 6)]         = [2; 12; 30]
  List.map (fun (k, _) -> k)
           [("a", 1); ("b", 2)]              = ["a"; "b"]
  (fun a (b, c) d -> a + b + c + d) 1 (2, 3) 4 = 10
2026-05-09 04:25:18 +00:00
8ca3ef342d ocaml: phase 6 Random module (deterministic LCG) (+4 tests, 549 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
Linear-congruential PRNG with mutable seed (_state ref). API:

  init s        seed the PRNG
  self_init ()  default seed (1)
  int bound     0 <= n < bound
  bool ()       fair coin
  float bound   uniform in [0, bound)
  bits ()       30 bits

Stepping rule:
  state := (state * 1103515245 + 12345) mod 2147483647
  result := |state| mod bound

Same seed reproduces the same sequence. Real OCaml's Random uses
Lagged Fibonacci; ours is simpler but adequate for shuffles and
Monte Carlo demos in baseline programs.

  Random.init 42; Random.int 100  = 48
  Random.init 1;  Random.int 10   = 0
2026-05-09 04:12:16 +00:00
41190c6d23 ocaml: phase 6 Hashtbl.keys/values/bindings/remove/clear (+4 tests, 545 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Two new host primitives:
  _hashtbl_remove t k   -> dissoc the key from the underlying dict
  _hashtbl_clear  t     -> reset the cell to {}

Eight new OCaml-syntax helpers in runtime.sx Hashtbl module:
  bindings t            = _hashtbl_to_list t
  keys t                = List.map (fun (k, _) -> k) (...)
  values t              = List.map (fun (_, v) -> v) (...)
  to_seq t              = bindings t
  to_seq_keys / to_seq_values
  remove / clear / reset

The keys/values implementations use a 'fun pair -> match pair with
(k, _) -> k' indirection because parse-fun does not currently allow
tuple patterns directly on parameters. Same restriction we worked
around in iteration 98's let-pattern desugaring.

Also: a detour attempting to add top-level 'let (a, b) = expr'
support was started but reverted — parse-decl-let in the outer
ocaml-parse-program scope does not have access to parse-pattern
(which is local to ocaml-parse). Will need a slice + re-parse trick
later.
2026-05-09 03:59:20 +00:00
dab8718289 ocaml: phase 4 'let PATTERN = expr in body' tuple destructuring (+3 tests, 541 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 44s
When 'let' is followed by '(', parse-let now reads a full pattern
(via the existing parse-pattern used by match), expects '=', then
'in', and desugars to:

  let PATTERN = EXPR in BODY  =>  match EXPR with PATTERN -> BODY

This reuses the entire pattern-matching machinery, so any pattern
the match parser accepts works here too — paren-tuples, nested
tuples, cons patterns, list patterns. No 'rec' allowed for pattern
bindings (real OCaml's restriction).

  let (a, b) = (1, 2) in a + b              = 3
  let (a, b, c) = (10, 20, 30) in a + b + c = 60
  let pair = (5, 7) in
  let (x, y) = pair in x * y                = 35

Also retroactively cleaned up Printf's iter-97 width-pos packing
hack ('width * 1000000 + spec_pos') — it's now
'let (width, spec_pos) = parse_width_loop after_flags in ...' like
real OCaml.
2026-05-09 03:40:38 +00:00
7e64695a74 ocaml: phase 6 Printf width specifiers %5d/%-5d/%05d/%4s (+5 tests, 538 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
The Printf walker now parses optional flags + width digits between
'%' and the spec letter:

  -  left-align (default is right-align)
  0  zero-pad (default is space-pad; only honoured when not left-aligned)
  Nd... decimal width digits (any number)

After formatting the argument into a base string with the existing
spec dispatch (%d/%i/%u/%s/%f/%c/%b/%x/%X/%o), the result is padded
to the requested width.

Workaround: width and spec_pos are returned packed as
  width * 1000000 + spec_pos
because the parser does not yet support tuple destructuring in let
('let (a, b) = expr in body' fails with 'expected ident'). TODO: lift
that limitation; for now the encoding round-trips losslessly for any
practical width.

  Printf.sprintf '%5d'  42      = '   42'
  Printf.sprintf '%-5d|' 42     = '42   |'
  Printf.sprintf '%05d' 42      = '00042'
  Printf.sprintf '%4s' 'hi'     = '  hi'
  Printf.sprintf 'hi=%-3d, hex=%04x' 9 15 = 'hi=9  , hex=000f'
2026-05-09 03:25:50 +00:00
cb14a07413 ocaml: phase 6 Printf %i/%u/%x/%X/%o + int_to_hex/octal host primitives (+5 tests, 533 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 41s
Three new host primitives in eval.sx:
  _int_to_hex_lower n  -> string of hex digits (lowercase)
  _int_to_hex_upper n  -> string of hex digits (uppercase)
  _int_to_octal    n   -> string of octal digits

Each builds the digit string by repeated floor(n / base) + mod,
prepending the digit at each step. Negative numbers prefix '-' so the
output round-trips through int_of_string with a sign.

Printf walker now fans out:
  %d, %i, %u  -> _string_of_int
  %f          -> _string_of_float
  %x          -> _int_to_hex_lower
  %X          -> _int_to_hex_upper
  %o          -> _int_to_octal
  %s, %c, %b  -> existing handling

  Printf.sprintf '%x' 255          = 'ff'
  Printf.sprintf '%X' 4096         = '1000'
  Printf.sprintf '%o' 8            = '10'
  Printf.sprintf '%x %X %o' 255 4096 8 = 'ff 1000 10'
2026-05-09 03:12:28 +00:00
8188a82a58 ocaml: phase 6 List.sort upgraded to mergesort (+3 tests, 528 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
The previous List.sort was O(n^2) insertion sort. Replaced with a
straightforward mergesort:

  split lst    -> alternating-take into ([odd], [even])
  merge xs ys  -> classic two-finger merge under cmp
  sort cmp xs  -> base cases [], [x]; otherwise split + recursive
                  sort on each half + merge

Tuple destructuring on the split result is expressed via nested
match — let-tuple-destructuring would be cleaner but works today.

This benefits sort_uniq (which calls sort first), Set.Make.add via
sort etc., and any user program using List.sort. Stable_sort is
already aliased to sort.
2026-05-09 03:01:28 +00:00
a0e8b64f5c ocaml: phase 4 integer division semantics + Int module + max_int/min_int (+5 tests, 525 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Three things in this commit:

1. Integer / is now truncate-toward-zero on ints, IEEE on floats. The
   eval-op handler for '/' checks (number? + (= (round x) x)) on both
   sides; if both integral, applies host floor/ceil based on sign;
   otherwise falls through to host '/'.

2. Fixes Int.rem, which was returning 0 because (a - b * (a / b))
   was using float division: 17 - 5 * 3.4 = 0.0. Now Int.rem 17 5 = 2.

3. Int module fleshed out:
   max_int / min_int / zero / one / minus_one,
   succ / pred / neg, add / sub / mul / div / rem,
   equal, compare.

Also adds globals: max_int, min_int, abs_float, float_of_int,
int_of_float (the latter two are identity in our dynamic runtime).

  17 / 5         = 3
  -17 / 5        = -3   (trunc toward zero)
  Int.rem 17 5   = 2
  Int.compare 5 3 = 1
2026-05-09 02:50:21 +00:00
55fe1e4468 ocaml: phase 6 Array.sort/sub/append/exists/for_all/mem (+5 tests, 520 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Eight new Array functions, all in OCaml syntax inside runtime.sx,
delegating to the corresponding List operation on the cell's
underlying list:

  sort cmp a    -> a := List.sort cmp !a    (* mutates the cell *)
  stable_sort   = sort
  fast_sort     = sort
  append a b    -> ref (List.append !a !b)
  sub a pos n   -> ref (take n (drop pos !a))
  exists p      -> List.exists p !a
  for_all p     -> List.for_all p !a
  mem x a       -> List.mem x !a

Round-trip:
  let a = Array.of_list [3;1;4;1;5;9;2;6] in
  Array.sort compare a;
  Array.to_list a    = [1;1;2;3;4;5;6;9]
2026-05-09 02:35:55 +00:00
f68ea63e46 ocaml: phase 5.1 brainfuck.ml baseline (subset interpreter)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Five '+++++.' groups, cumulative accumulator 5+10+15+20+25 = 75.

This is a brainfuck *subset* — only > < + - . (no [ ] looping). That's
intentional: the goal is to stress imperative idioms that the recently
added Array module + array indexing syntax + s.[i] make ergonomic, all
in one program.

Exercises:
  Array.make 256 0
  arr.(!ptr)
  arr.(!ptr) <- arr.(!ptr) + 1
  prog.[!pc]
  ref / ! / :=
  while + nested if/else if/else if for op dispatch

25 baseline programs total.
2026-05-09 02:24:45 +00:00
a66b262267 ocaml: phase 5.1 sieve.ml baseline (Sieve of Eratosthenes)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Counts primes <= 50, expected 15.

Stresses the recently-added Array module + the new array-indexing
syntax together with nested control flow:

  let sieve = Array.make (n + 1) true in
  sieve.(0) <- false;
  sieve.(1) <- false;
  for i = 2 to n do
    if sieve.(i) then begin
      let j = ref (i * i) in
      while !j <= n do
        sieve.(!j) <- false;
        j := !j + i
      done
    end
  done;
  ...

Exercises: Array.make, arr.(i), arr.(i) <- v, nested for/while,
begin..end blocks, ref/!/:=, integer arithmetic. 24 baseline
programs total.
2026-05-09 02:16:18 +00:00
073588812a ocaml: phase 4 'arr.(i)' and 'arr.(i) <- v' array indexing (+3 tests, 515 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
parse-atom-postfix's '.()' branch now disambiguates between let-open
and array-get based on whether the head is a module path (':con' or
':field' chain rooted in ':con'). Module paths still emit
(:let-open M EXPR); everything else emits (:array-get ARR I).

Eval handles :array-get by reading the cell's underlying list at
index. The '<-' assignment handler now also accepts :array-get lhs
and rewrites the cell with one position changed.

Idiomatic OCaml array code now works:

  let a = Array.make 5 0 in
  for i = 0 to 4 do a.(i) <- i * i done;
  a.(3) + a.(4)               = 25

  let a = Array.init 4 (fun i -> i + 1) in
  a.(0) + a.(1) + a.(2) + a.(3)  = 10

  List.(length [1;2;3])         = 3   (* unchanged: List is a module *)
2026-05-09 02:08:21 +00:00
1ed3216ba6 ocaml: phase 6 Array module + (op) operator sections (+6 tests, 512 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Array module (runtime.sx, OCaml syntax):
  Backed by a 'ref of list'. make/length/get/init build the cell;
  set rewrites the underlying list with one cell changed (O(n) but
  works for short arrays in baseline programs). Includes
  iter/iteri/map/mapi/fold_left/to_list/of_list/copy/blit/fill.

(op) operator sections (parser.sx, parse-atom):
  When the token after '(' is a binop (any op with non-zero
  precedence in the binop table) and the next token is ')', emit
  (:fun ('a' 'b') (:op OP a b)) — i.e. (+)  becomes fun a b -> a + b.
  Recognises every binop including 'mod', 'land', '^', '@', '::',
  etc.

Lets us write:
  List.fold_left (+) 0 [1;2;3;4;5]    = 15
  let f = ( * ) in f 6 7              = 42
  List.map ((-) 10) [1;2;3]           = [9;8;7]
  let a = Array.make 5 7 in
  Array.set a 2 99;
  Array.fold_left (+) 0 a             = 127
2026-05-09 01:59:13 +00:00
5618dd1ef5 ocaml: phase 5.1 csv.ml baseline (split + int_of_string + fold_left)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Inline CSV-like text:
  a,1,extra
  b,2,extra
  c,3,extra
  d,4,extra

Two-stage String.split_on_char: first on '\n' for rows, then on ','
for fields per row. List.fold_left accumulates int_of_string of the
second field across rows. Result = 1+2+3+4 = 10.

Exercises char escapes inside string literals ('\n'), nested
String.split_on_char, List.fold_left with a non-trivial closure body,
and int_of_string. 23 baseline programs total.
2026-05-09 01:47:27 +00:00
19497c9fba ocaml: phase 4 polymorphic variants confirmation (+3 tests, 506 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Tokenizer already classified backtick-uppercase as a ctor identical
to a nominal one, but it had never been exercised by the suite. This
commit adds three smoke tests confirming that nullary, n-ary, and
list-of-polyvariant patterns all match:

  let x = polyvar(Foo) in match x with polyvar(Foo) -> 1 | polyvar(Bar) -> 2

  let x = polyvar(Pair) (5, 7) in
  match x with polyvar(Pair) (a, b) -> a + b | _ -> 0

  List.map (fun x -> match x with polyvar(On) -> 1 | polyvar(Off) -> 0)
           [polyvar(On); polyvar(Off); polyvar(On)]

(In the actual SX, polyvar(X) is the literal backtick-X — backticks
in this commit message are escaped to avoid shell interpretation.)
Since OCaml-on-SX is dynamic, there's no structural row inference,
but matching by tag works.
2026-05-09 01:38:09 +00:00
a34cfe69dc ocaml: phase 6 List.sort_uniq + List.find_map (+2 tests, 503 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 41s
sort_uniq:
  Sort with the user comparator, then walk the sorted list dropping
  any element equal to its predecessor. Output is sorted and unique.

  List.sort_uniq compare [3;1;2;1;3;2;4]  =  [1;2;3;4]

find_map:
  Walk until the user fn returns Some v; return that. If all None,
  return None.

  List.find_map (fun x -> if x > 5 then Some (x * 2) else None)
                [1;2;3;6;7]
  = Some 12

Both defined in OCaml syntax in runtime.sx — no host primitive
needed since they're pure list traversals over existing operations.
2026-05-09 01:29:02 +00:00
8af3630625 ocaml: phase 6 String.iter/iteri/fold_left/fold_right/to_seq/of_seq (+3 tests, 501 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Six new String functions, all in OCaml syntax inside runtime.sx:

  iter      : index-walk with side-effecting f
  iteri     : iter with index
  fold_left : thread accumulator left-to-right
  fold_right: thread accumulator right-to-left
  to_seq    : return a char list (lazy in real OCaml; eager here)
  of_seq    : concat a char list back to a string

Round-trip:
  String.of_seq (List.rev (String.to_seq "hello"))   = "olleh"

Note: real OCaml's Seq is lazy. We return a plain list because the
existing stdlib already provides exhaustive list operations and we
don't yet have lazy sequences. If a baseline needs Seq.unfold or
similar, we'll graduate to a proper Seq module then.
2026-05-09 01:19:28 +00:00
34d518d555 ocaml: phase 5.1 frequency.ml baseline + Format module alias (+2 tests, 498 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
frequency.ml exercises the recently-added Hashtbl.iter / fold +
Hashtbl.find_opt + s.[i] indexing + for-loop together: build a
char-count table for 'abracadabra' then take the max via
Hashtbl.fold. Expected = 5 (a x 5). Total 25 baseline programs.

Format module added as a thin alias of Printf — sprintf, printf, and
asprintf all delegate to Printf.sprintf. The dynamic runtime doesn't
distinguish boxes/breaks, so format strings work the same as in
Printf and most Format-using OCaml programs now compile.
2026-05-09 01:11:53 +00:00
9907c1c58c ocaml: phase 4 'lazy EXPR' + Lazy.force (+2 tests, 496 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Tokenizer already had 'lazy' as a keyword. This commit wires it through:

  parser  : parse-prefix emits (:lazy EXPR), like the existing 'assert'
            handler.
  eval    : creates a one-element cell with state ('Thunk' expr env).
  host    : _lazy_force flips the cell to ('Forced' v) on first call
            and returns the cached value thereafter.
  runtime : module Lazy = struct let force lz = _lazy_force lz end.

Memoisation confirmed by tracking a side-effect counter through two
forces of the same lazy:

  let counter = ref 0 in
  let lz = lazy (counter := !counter + 1; 42) in
  let a = Lazy.force lz in
  let b = Lazy.force lz in
  (a + b) * 100 + !counter        = 8401   (= 84*100 + 1)
2026-05-09 01:03:40 +00:00
207dfc60ad ocaml: phase 6 Hashtbl.iter / Hashtbl.fold (+2 tests, 494 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
New host primitive _hashtbl_to_list returns the entries as a list of
OCaml tuples — ('tuple' k v) form, matching the AST representation
that the pattern-match VM (:ptuple) expects. Without that exact
shape, '(k, v) :: rest' patterns fail to match.

Hashtbl.iter / Hashtbl.fold in runtime walk that list with the user
fn. This closes a long-standing gap: previously Hashtbl was opaque
once values were written (we could only find_opt one key at a time).

  let t = Hashtbl.create 4 in
  Hashtbl.add t "a" 1; Hashtbl.add t "b" 2; Hashtbl.add t "c" 3;
  Hashtbl.fold (fun _ v acc -> acc + v) t 0   = 6
2026-05-09 00:53:32 +00:00
1b38f89055 ocaml: phase 6 Printf.sprintf %d/%s/%f/%c/%b/%% + global string_of_* (+5 tests, 492 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Replaces the stub sprintf in runtime.sx with a real implementation:
walk fmt char-by-char accumulating a prefix; on recognised %X return a
one-arg fn that formats the arg and recurses on the rest of fmt. The
function self-curries to the spec count — there's no separate arity
machinery, just a closure chain.

Specs: %d (int), %s (string), %f (float), %c (char/string in our model),
%b (bool), %% (literal). Unknown specs pass through.

Same expression returns a string (no specs) or a function (>=1 spec) —
OCaml proper would reject this; works fine in OCaml-on-SX's dynamic
runtime.

Also adds top-level aliases:
  string_of_int   = _string_of_int
  string_of_float = _string_of_float
  string_of_bool  = if b then "true" else "false"
  int_of_string   = _int_of_string

  Printf.sprintf "x=%d" 42              = "x=42"
  Printf.sprintf "%s = %d" "answer" 42  = "answer = 42"
  Printf.sprintf "%d%%" 50               = "50%"
2026-05-09 00:42:35 +00:00
14b52cfaa7 ocaml: phase 4 'assert EXPR' (+3 tests, 487 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Tokenizer already classified 'assert' as a keyword; this commit wires
it through:
  parser  : parse-prefix dispatches like 'not' — advance, recur, wrap
            as (:assert EXPR).
  eval    : evaluate operand; nil on truthy, host-error 'Assert_failure'
            on false. Caught cleanly by existing try/with.

  assert true; 42                          = 42
  let x = 5 in assert (x = 5); x + 1       = 6
  try (assert false; 0) with _ -> 99       = 99
2026-05-09 00:32:35 +00:00
bd2cd8aad1 ocaml: phase 5.1 levenshtein.ml baseline (no-memo edit distance, sum=11)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Recursive Levenshtein edit distance with no memoization (the test
strings are short enough for the exponential-without-memo version to
fit in <2 minutes on contended hosts). Sums distances for five short
pairs:

  ('abc','abx') + ('ab','ba') + ('abc','axyc') + ('','abcd') + ('ab','')
   = 1 + 2 + 2 + 4 + 2 = 11

Exercises:
  * curried four-arg recursion
  * s.[i] equality test (char comparison)
  * min nested twice for the three-way recurrence
  * mixed empty-string base cases
2026-05-09 00:23:58 +00:00
0234ae329e ocaml: phase 5.1 caesar.ml baseline (ROT13 + s.[i] + Char ops)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Side-quests required to land caesar.ml:

1. Top-level 'let r = expr in body' is now an expression decl, not a
   broken decl-let. ocaml-parse-program's dispatch now checks
   has-matching-in? at every top-level let; if matched, slices via
   skip-let-rhs-boundary (which already opens depth on a leading let
   with matching in) and ocaml-parse on the slice, wrapping as :expr.

2. runtime.sx: added String.make / String.init / String.map. Used by
   caesar.ml's encode = String.init n (fun i -> shift_char s.[i] k).

3. baseline run.sh per-program timeout 240->480s (system load on the
   shared host frequently exceeds 240s for large baselines).

caesar.ml exercises:
  * the new top-level let-in expression dispatch
  * s.[i] string indexing
  * Char.code / Char.chr round-trip math
  * String.init with a closure that captures k

Test value: Char.code r.[0] + Char.code r.[4] after ROT13(ROT13('hello')) = 104 + 111 = 215.
2026-05-09 00:13:11 +00:00
f895a118fb ocaml: phase 4 's.[i]' string indexing syntax (+3 tests, 484 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 44s
parse-atom-postfix now dispatches three cases after consuming '.':

  .field  -> existing field/module access
  .(EXPR) -> existing local-open
  .[EXPR] -> new string-get syntax  (this commit)

Eval reduces (:string-get S I) to host (nth S I), which already returns
a one-character string for OCaml's char model.

Lets us write idiomatic OCaml string traversal:

  let s = "hi" in
  let n = ref 0 in
  for i = 0 to String.length s - 1 do
    n := !n + Char.code s.[i]
  done;
  !n  (* = 209 *)
2026-05-08 23:58:37 +00:00
bc4f4a5477 ocaml: phase 5.1 roman.ml baseline + top-level 'let () = expr'
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 51s
Side-quest emerged from adding roman.ml baseline (Roman numeral greedy
encoding): top-level 'let () = expr' was unsupported because
ocaml-parse-program's parse-decl-let consumed an ident strictly. Now
parse-decl-let recognises a leading '()' as a unit binding and
synthesises a __unit_NN name (matching how parse-let already handles
inner-let unit patterns).

roman.ml exercises:
  * tuple list literal [(int * string); ...]
  * recursive pattern match on tuple-cons
  * String.length + List.fold_left
  * the new top-level let () support (sanity in a comment, even though
    the program ends with a bare expression for the test harness)

Bumped lib/ocaml/test.sh server timeout 180->360s — the recent surge in
test count plus a CPU-contended host was crowding out the sole epoch
reaching the deeper smarts.
2026-05-08 23:40:36 +00:00
982e9680fe ocaml: phase 4 'M.(expr)' local-open expression form (+3 tests, 481 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
In parse-atom-postfix, after consuming '.', if the next token is '(',
parse the inner expression and emit (:let-open M EXPR) instead of
:field. Cleanly composes with the existing :let-open evaluator and
loops to allow chained dot postfixes.

  List.(length [1;2;3])                = 3
  List.(map (fun x -> x + 1) [1;2;3])   = [2;3;4]
  Option.(map (fun x -> x * 10) (Some 4)) = Some 40
2026-05-08 21:43:38 +00:00
6dc535dde3 ocaml: phase 4 'let open M in body' local opens (+3 tests, 478 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 21s
Parser detects 'let open' as a separate let-form, parses M as a path
(Ctor(.Ctor)*) directly via inline AST construction (no source slicing
since cur-pos is only available in ocaml-parse-program), and emits
(:let-open PATH BODY).

Eval resolves the path to a module dict and merges its bindings into
the env for body evaluation. Now:

  let open List in map (fun x -> x * 2) [1;2;3]   = [2;4;6]
  let open Option in map (fun x -> x + 1) (Some 5) = Some 6
2026-05-08 21:33:14 +00:00
0530120bc7 ocaml: phase 4 def-mut / def-rec-mut inside modules (+2 tests, 475 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
ocaml-eval-module now handles :def-mut and :def-rec-mut decls so
'module M = struct let rec a n = ... and b n = ... end' works. The
def-rec-mut version uses cell-based mutual recursion exactly as the
top-level version.
2026-05-08 21:14:07 +00:00
6d9ac1e55a ocaml: phase 5.1 bfs.ml baseline (20/20 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 20s
Graph BFS using Queue + Hashtbl visited-set + List.assoc_opt + List.iter.
Returns 6 for a graph where A reaches B/C/D/E/F. Demonstrates 4 stdlib
modules (Queue, Hashtbl, List) cooperating in a real algorithm.
2026-05-08 21:05:32 +00:00
a4ef9a8ec9 ocaml: phase 1 type annotations on let / (e : T) (+4 tests, 473 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
let NAME [PARAMS] : T = expr and (expr : T) parse and skip the type
source. Runtime no-op since SX is dynamic. Works in inline let,
top-level let, and parenthesised expressions:

  let x : int = 5 ;; x + 1                        -> 6
  let f (x : int) : int = x + 1 in f 41           -> 42
  (5 : int)                                       -> 5
  ((1 + 2) : int) * 3                             -> 9
2026-05-08 20:58:50 +00:00
ce75bd6848 ocaml: phase 1+5.1 type aliases + poly_stack baseline (+3 tests, 469 / 19 baseline)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 39s
Parser: in parse-decl-type, dispatch on the post-= token:
  '|' or Ctor   -> sum type
  '{'           -> record type
  otherwise     -> type alias (skip to boundary)
AST (:type-alias NAME PARAMS) with body discarded. Runtime no-op since
SX has no nominal types.

poly_stack.ml baseline exercises:
  module type ELEMENT = sig type t val show : t -> string end
  module IntElem = struct type t = int let show x = ... end
  module Make (E : ELEMENT) = struct ... use E.show ... end
  module IntStack = Make(IntElem)

Demonstrates the substrate handles signature decls + abstract types +
functor parameter with sig constraint.
2026-05-08 20:49:26 +00:00
c7d8b7dd62 ocaml: phase 2+3 'when' guard in try/with (+3 tests, 467 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
parse-try now consumes optional 'when GUARD-EXPR' before -> and emits
(:case-when PAT GUARD BODY). Eval try clause loop dispatches on case /
case-when and falls through on guard false — same semantics as match.

Examples:
  try raise (E 5) with | E n when n > 0 -> n | _ -> 0   = 5
  try raise (E (-3)) with | E n when n > 0 -> n | _ -> 0 = 0
  try raise (E 5) with | E n when n > 100 -> n | E n -> n + 1000  = 1005
2026-05-08 20:36:02 +00:00
029c1783f4 ocaml: phase 1+3 'when' guard in 'function | pat -> body' (+3 tests, 464 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
parse-function now consumes optional 'when GUARD-EXPR' before -> and
emits (:case-when PAT GUARD BODY) — same handling as match clauses.
function-style sign extraction now works:
  (function | n when n > 0 -> 1 | n when n < 0 -> -1 | _ -> 0)
2026-05-08 20:26:28 +00:00
b92a98fb45 ocaml: refresh scoreboard (480/480 across 15 suites incl. 18 baseline programs)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
2026-05-08 20:12:35 +00:00
8fab20c8bc ocaml: phase 5.1 anagrams.ml baseline (18/18 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Group anagrams by canonical (sorted-chars) key using Hashtbl +
List.sort. Demonstrates char-by-char traversal via String.get + for-loop +
ref accumulator + Hashtbl as a multi-valued counter.
2026-05-08 19:57:09 +00:00
de8b1dd681 ocaml: phase 5.1 lambda_calc.ml baseline (17/17 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Untyped lambda calculus interpreter inside OCaml-on-SX:
  type term = Var | Abs of string * term | App | Num of int
  type value = VNum of int | VClos of string * term * env
  let rec eval env t = match t with ...

(\x.\y.x) 7 99 = 7. The substrate handles two ADTs, recursive eval,
closure-based env, and pattern matching all written as a single
self-contained OCaml program — strong validation.
2026-05-08 19:49:08 +00:00
ce81ce2e95 ocaml: phase 6 Char predicates (+7 tests, 461 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Char.is_digit / is_alpha / is_alnum / is_whitespace / is_upper /
is_lower / is_space — all written in OCaml using Char.code + ASCII
range checks.
2026-05-08 19:42:00 +00:00
8c7ad62b44 ocaml: phase 5 HM def-mut + def-rec-mut at top level (+3 tests, 454 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
ocaml-type-of-program now handles :def-mut (sequential generalize) and
:def-rec-mut (pre-bind tvs, infer rhs, unify, generalize all, infer
body — same algorithm as the inline let-rec-mut version).

Mutual top-level recursion now type-checks:
  let rec even n = ... and odd n = ...;; even 10           : Bool
  let rec map f xs = ... and length lst = ...;; map :
                              ('a -> 'b) -> 'a list -> 'b list
2026-05-08 19:19:17 +00:00
fff8fe2dc8 ocaml: phase 5.1 memo_fib.ml baseline (16/16 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Memoized fibonacci using Hashtbl.find_opt + Hashtbl.add.
fib(25) = 75025. Demonstrates mutable Hashtbl through the OCaml
stdlib API in real recursive code.
2026-05-08 19:10:49 +00:00
360a3ed51f ocaml: phase 5.1 queens.ml baseline (15/15 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
4-queens via recursive backtracking + List.fold_left. Returns 2 (the
two solutions of 4-queens). Per-program timeout in run.sh bumped to
240s — the tree-walking interpreter is slow on heavy recursion but
correct.

The substrate handles full backtracking + safe-check recursion +
list-driven candidate enumeration end-to-end.
2026-05-08 19:04:04 +00:00
50a219b688 ocaml: phase 5.1 mutable_record.ml baseline (14/14 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s
Counter-style record with two mutable fields. Validates the new
r.f <- v field mutation end-to-end through type decl + record literal
+ field access + field assignment + sequence operator.

  type counter = { mutable count : int; mutable last : int }
  let bump c = c.count <- c.count + 1 ; c.last <- c.count

After 5 bumps: count=5, last=5, sum=10.
2026-05-08 18:43:19 +00:00
d9979eaf6c ocaml: phase 2 mutable record fields r.f <- v (+4 tests, 451 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
<- added to op-table at level 1 (same as :=). Eval short-circuits on
<- to mutate the lhs's field via host SX dict-set!. The lhs must be a
:field expression; otherwise raises.

Tested:
  let r = { x = 1; y = 2 } in r.x <- 5; r.x       (5)
  let r = { x = 0 } in for i = 1 to 5 do r.x <- r.x + i done; r.x  (15)
  let r = { name = ...; age = 30 } in r.name <- "Alice"; r.name

The 'mutable' keyword in record type decls is parsed-and-discarded;
runtime semantics: every field is mutable. Phase 2 closes this gap
without changing the dict-based record representation.
2026-05-08 18:35:31 +00:00
66da0e5b84 ocaml: phase 1+3 record type declarations (+3 tests, 447 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
type r = { x : int; mutable y : string } parses to
(:type-def-record NAME PARAMS FIELDS) with FIELDS each (NAME) or
(:mutable NAME). Parser dispatches on { after = to parse field list.
Field-type sources are skipped (HM registration TBD). Runtime no-op
since records already work as dynamic dicts.
2026-05-08 18:26:34 +00:00
f070bddb0e ocaml: phase 5.1 conformance.sh integrates baseline (458/458 across 15 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
bash lib/ocaml/conformance.sh now runs lib/ocaml/baseline/run.sh and
aggregates pass/fail counts under a 'baseline' suite. Full-suite
scoreboard now reports both unit-test results and end-to-end OCaml
program runs in a single artifact.
2026-05-08 18:12:23 +00:00
0858986877 ocaml: phase 5.1 btree.ml baseline (13/13 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Polymorphic binary search tree with insert + in-order traversal.
Exercises parametric ADT (type 'a tree = Leaf | Node of 'a * 'a tree
* 'a tree), recursive match, List.append, List.fold_left.
2026-05-08 17:52:49 +00:00
d8f1882b50 ocaml: phase 5.1 fizzbuzz.ml baseline (12/12 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Classic fizzbuzz using ref-cell accumulator, for-loop, mod, if/elseif
chain, String.concat, Int.to_string. Output verified via String.length
of the comma-joined result for n=15: 57.
2026-05-08 17:44:07 +00:00
0bc6dbd233 ocaml: phase 2+6 print primitives wire to host display (+2 tests, 444 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s
print_string / print_endline / print_int / print_newline now route to
SX display primitive (not the non-existent print/println). print_endline
appends '\n'.

let _ = expr ;; at top level confirmed working via the
wildcard-param parser.
2026-05-08 17:37:00 +00:00
cabf5dc9c3 ocaml: phase 5 HM let-mut / let-rec-mut (+3 tests, 442 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
ocaml-infer-let-mut: each rhs inferred in parent env, generalized
sequentially before adding to body env.

ocaml-infer-let-rec-mut: pre-bind all names with fresh tvs; infer
each rhs against the joint env, unify each with its tv, then
generalize all and infer body.

Mutual recursion now type-checks:
  let rec even n = if n = 0 then true else odd (n - 1)
  and odd n = if n = 0 then false else even (n - 1)
  in even   : Int -> Bool
2026-05-08 17:28:27 +00:00
4909ebe2ad ocaml: phase 6 Option/Result/Bytes extensions (+9 tests, 439 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Option: join, to_result, some, none.
Result: value, iter, fold.
Bytes: length, get, of_string, to_string, concat, sub — thin alias of
String (SX has no separate immutable byte type).

Ordering fix: Bytes module placed after String so its closures capture
String in scope. Earlier draft put Bytes before String which made
String.* lookups fail with 'not a record/module' (treated as nullary
ctor).
2026-05-08 17:19:16 +00:00
f05d405bac ocaml: phase 6 Stack + Queue modules (+5 tests, 430 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Stack: ref-holding-list LIFO. push/pop/top/length/is_empty/clear.
Queue: two-list (front, back) amortised O(1) queue. push/pop/length/
is_empty/clear. Both in OCaml syntax in runtime.sx.
2026-05-08 17:03:32 +00:00
ffa74399fd ocaml: phase 5.1 calc.ml baseline (11/11 pass) + inline let-rec-and parser fix
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
Recursive-descent calculator parses '(1 + 2) * 3 + 4' = 13. Two parser
bugs fixed:

1. parse-let now handles inline 'let rec a () = ... and b () = ... in
   body' via new (:let-rec-mut BINDINGS BODY) and (:let-mut BINDINGS
   BODY) AST shapes; eval handles both.

2. has-matching-in? lookahead no longer stops at 'and' — 'and' is
   internal to let-rec, not a decl boundary. Without this fix, the
   inner 'let rec a () = ... and b () = ...' inside a let-decl rhs
   would have been treated as the start of a new top-level decl.

Baseline exercises mutually-recursive functions, while-loops, ref-cell
imperative parsing, and ADT-based AST construction.
2026-05-08 16:53:44 +00:00
ecdd90345e ocaml: refresh scoreboard (426/426 across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
2026-05-08 16:18:21 +00:00
2f271fa6a6 ocaml: phase 1+6 Buffer + parser !x in app args (+3 tests, 425 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
Parser fix: at-app-start? and parse-app's loop recognise prefix !
as a deref of the next app arg. So 'List.rev !b' parses as
'(:app List.rev (:deref b))' instead of stalling at !.

Buffer module backed by a ref holding string list:
  create _ = ref []
  add_string b s = b := s :: !b
  contents b = String.concat "" (List.rev !b)
  add_char/length/clear/reset
2026-05-08 16:16:52 +00:00
dbe3c6c203 ocaml: phase 5.1 word_count.ml baseline (10/10 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Uses Map.Make(StrOrd) + List.fold_left to count word frequencies;
exercises the full functor pipeline with a real-world idiom:

  let inc_count m word =
    match StrMap.find_opt word m with
    | None -> StrMap.add word 1 m
    | Some n -> StrMap.add word (n + 1) m
  let count words = List.fold_left inc_count StrMap.empty words

10/10 baseline programs pass.
2026-05-08 16:11:03 +00:00
404c908a9a ocaml: phase 6 Map/Set extensions iter/fold/filter/union/inter (+4 tests, 422 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Map.Make: iter, fold, map, filter, is_empty added.
Set.Make: iter, fold, filter, is_empty, union, inter added.

All written in OCaml inside the existing functor bodies. Tested:
  IntMap.fold (fun k v acc -> acc + v) m 0           = 30
  IntSet.elements (IntSet.union {1,2} {2,3})         = [1; 2; 3]
  IntSet.elements (IntSet.inter {1,2,3} {2,3,4})     = [2; 3]
2026-05-08 16:02:45 +00:00
b297c83b1d ocaml: refresh scoreboard (419/419 across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
2026-05-08 15:51:36 +00:00
85867e329b ocaml: phase 6 Map.Make / Set.Make functors (+4 tests, 418 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 45s
Both written in OCaml inside lib/ocaml/runtime.sx:

  module Map = struct module Make (Ord) = struct
    let empty = []
    let add k v m = ... (* sorted insert via Ord.compare *)
    let find_opt / find / mem / remove / bindings / cardinal
  end end

  module Set = struct module Make (Ord) = struct
    let empty = []
    let mem / add / remove / elements / cardinal
  end end

Sorted association list / sorted list backing — linear ops but
correct. Strong substrate-validation: Map.Make is a non-trivial
functor implemented entirely on top of the OCaml-on-SX evaluator.
2026-05-08 15:50:22 +00:00
cd93b11328 ocaml: phase 6 Sys module constants (+5 tests, 414 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
os_type="SX", word_size=64, max_array_length, max_string_length,
executable_name="ocaml-on-sx", big_endian=false, unix=true,
win32=false, cygwin=false. Constants-only for now — argv/getenv_opt/
command would need host platform integration.
2026-05-08 15:46:33 +00:00
4bca2cacff ocaml: phase 5 parse ctor arg types in user type-defs (+3 tests, 409 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 36s
ocaml-hm-parse-type-src recognises primitive type names (int/bool/
string/float/unit), tyvars 'a, and simple parametric T list / T option.
Replaces the previous int-by-default placeholder in
ocaml-hm-register-type-def!.

So 'type tag = TStr of string | TInt of int' correctly registers
TStr : string -> tag and TInt : int -> tag. Pattern-match on tag
gives proper field types in the body. Multi-arg / function types
still fall back to a fresh tv.
2026-05-08 15:43:16 +00:00
d61ee088c5 ocaml: refresh scoreboard (407/407 across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
2026-05-08 15:35:28 +00:00
f40dfbbeb5 ocaml: phase 6 String extensions (+6 tests, 406 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s
ends_with, contains, trim, split_on_char, replace_all, index_of —
wrap host SX primitives via new _string_* builtins. String module
now substantively covers OCaml's Stdlib.String.
2026-05-08 15:34:18 +00:00
9f05e24c52 ocaml: phase 6 List.take/drop/filter_map/flat_map (+6 tests, 400 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 37s
Common functional helpers written in OCaml. flat_map / concat_map
share an implementation. 400-test milestone.
2026-05-08 15:30:29 +00:00
86343345dc ocaml: phase 1+3 or-patterns (P1 | P2 | ...) parens-only (+5 tests, 394 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 41s
Parser: when | follows a pattern inside parens, build (:por ALT1 ALT2
...). Eval: try alternatives, succeed on first match. Top-level |
remains the clause separator — parens-only avoids ambiguity without
lookahead.

Examples now work:
  match n with | (1 | 2 | 3) -> 100 | _ -> 0
  match c with | (Red | Green) -> 1 | Blue -> 2
2026-05-08 15:22:34 +00:00
ad252088c3 ocaml: phase 4 module type S = sig … end parser (+3 tests, 389 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 33s
module type S = sig DECLS end is parsed-and-discarded — sig..end
balanced skipping in parse-decl-module-type. AST (:module-type-def
NAME). Runtime no-op (signatures are type-level only).

Allows real OCaml programs with module type decls to parse and run
without stripping the sig blocks.
2026-05-08 15:11:45 +00:00
76ccbfbab6 ocaml: refresh scoreboard (387/387 across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
2026-05-08 15:07:55 +00:00
98049d5458 ocaml: phase 1+3 record patterns { f = pat } (+4 tests, 386 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 48s
Parser: { f1 = pat; f2 = pat; ... } in pattern position emits
(:precord (FIELDNAME PAT)...). Mixed with the existing { in
expression position via the at-pattern-atom? whitelist.

Eval: :precord matches against a dict; required fields must be present
and each pat must match the field's value. Can mix literal+var:
'match { x = 1; y = y } with | { x = 1; y = y } -> y' matches only
when x is 1.
2026-05-08 15:06:44 +00:00
0cf5c8f219 ocaml: phase 5.1 expr_eval.ml baseline (9/9 pass)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
A tiny arithmetic-expression evaluator using:
  type expr = Lit of int | Add of expr*expr | Mul of expr*expr | Neg of expr
  let rec eval e = match e with | Lit n -> n | Add (a,b) -> ...

Exercises type-decl + multi-arg ctor + recursive match end-to-end.
Per-program timeout in run.sh bumped to 120s.
2026-05-08 15:01:04 +00:00
9f539ab392 ocaml: phase 3 polymorphic variants (+4 tests, 382 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s
Tokenizer recognises backtick followed by an upper ident, emitting a
ctor token identical to a nominal ctor. Parser and evaluator treat
polyvariants as ctors — same tagged-list runtime. So:

  `Red                   -> ("Red")
  `Some 42               -> ("Some" 42)
  match `Red with | `Red -> 1 | `Green -> 2 | `Blue -> 3
                          -> 1
  `Pair (1,2)            -> ("Pair" 1 2) (with tuple-arg flatten)

Proper row types in HM deferred.
2026-05-08 14:03:09 +00:00
986b15c0e5 ocaml: phase 6 Float module: sqrt/sin/cos/pow/floor/ceil/round/pi (+6 tests, 378 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Wraps host SX math primitives via _float_* builtins. Float.pi is a
Float literal in the OCaml-side module.
2026-05-08 13:58:52 +00:00
ee002f2e02 ocaml: phase 1/5/6 float arithmetic +./-./*./. (+5 tests, 372 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Tokenizer: +. -. *. /. (with -. avoiding clash with negative float
literals). Parser table places dotted ops at int-precedence levels.
Eval routes to host SX +/-/*//. HM types them Float->Float->Float;
literal floats now infer as Float (was Int).

OCaml-style 1.5 +. 2.5 : Float works end-to-end through tokenize +
parse + eval + infer.
2026-05-08 13:55:04 +00:00
16df48ff74 ocaml: phase 6 List.combine/split/iter2/fold_left2/map2 (+4, 367 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Mechanical pair-walk OCaml implementations. failwith on length
mismatch matches Stdlib semantics. List module now covers 30+
functions.
2026-05-08 13:48:48 +00:00
dac9cf124f ocaml: refresh scoreboard (364/364 across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 31s
2026-05-08 13:45:29 +00:00
46d0eb258e ocaml: phase 5.1 baseline 8/8 — quicksort + exceptions + closures
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 38s
Added 3 baseline programs:
- closures.ml — curried make_adder; verifies closure capture
- quicksort.ml — recursive sort using List.filter + List.append, sums result
- exception_handle.ml — exception NegArg of int + raise + try/with

All 8/8 baseline programs pass through ocaml-run-program. Combined the
suite exercises: let-rec, modules, refs, for-loops, pattern matching,
exceptions, lambdas, list ops (map/filter/append/fold), arithmetic.

run.sh streamlined to one sx_server invocation per program. End-to-end
runtime ≈2 min.
2026-05-08 13:44:28 +00:00
de7be332c8 ocaml: phase 5.1 baseline OCaml programs (5/5 pass) + lookahead boundary
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 33s
lib/ocaml/baseline/{factorial,list_ops,option_match,module_use,sum_squares}.ml
exercised through ocaml-run-program (file-read F). lib/ocaml/baseline/
run.sh runs them and compares against expected.json — all 5 pass.

To make module_use.ml (with nested let-in) parse, parser's
skip-let-rhs-boundary! now uses has-matching-in? lookahead: a let at
depth 0 in a let-decl rhs opens a nested block IFF a matching in
exists before any decl-keyword. Without that in, the let is a new
top-level decl (preserves test 274 'let x = 1 let y = 2').

This is the first piece of Phase 5.1 'vendor a slice of OCaml
testsuite' — handcrafted fixtures for now, real testsuite TBD.
2026-05-08 13:33:24 +00:00
756d5fba64 ocaml: phase 5 HM with user type declarations (+6 tests, 363 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
ocaml-hm-ctors is now a mutable list cell; user type-defs register
their constructors via ocaml-hm-register-type-def!. New
ocaml-type-of-program processes top-level decls in order:
- type-def: register ctors with the scheme inferred from PARAMS+CTORS
- def/def-rec: generalize and bind in the type env
- exception-def: no-op for typing
- expr: return inferred type

Examples:
  type color = Red | Green | Blue;; Red : color
  type shape = Circle of int | Square of int;;
  let area s = match s with
    | Circle r -> r * r
    | Square s -> s * s;;
  area : shape -> Int

Caveat: ctor arg types parsed as raw source strings; registry defaults
to int for any single-arg ctor. Proper type-source parsing pending.
2026-05-08 13:12:07 +00:00
5bc7895ce0 ocaml: phase 5 HM let-rec + cons / append op types (+6 tests, 357 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
ocaml-infer-let-rec pre-binds the function name to a fresh tv before
inferring rhs (which may recursively call the name), unifies the
inferred rhs type with the tv, generalizes, then infers body.

Builtin env types :: : 'a -> 'a list -> 'a list and @ : 'a list ->
'a list -> 'a list — needed because :op compiles to (:app (:app (:var
OP) L) R) and previously these var lookups failed.

Examples now infer:
  let rec fact n = if ... in fact : Int -> Int
  let rec len lst = ... in len    : 'a list -> Int
  let rec map f xs = ... in map   : ('a -> 'b) -> 'a list -> 'b list
  1 :: [2; 3]                      : Int list
  let rec sum lst = ... in sum [1;2;3] : Int

Scoreboard refreshed: 358/358 across 14 suites.
2026-05-08 13:08:51 +00:00
81247eb6ea ocaml: phase 5 HM ctor inference for option/result (+7 tests, 351 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
ocaml-hm-ctor-env registers None/Some : 'a -> 'a option, Ok/Error :
'a -> ('a, 'b) result. :con NAME instantiates the scheme; :pcon NAME
ARG-PATS walks arg patterns through the constructor's arrow type,
unifying each.

Pretty-printer renders 'Int option' and '(Int, 'b) result'.

Examples now infer:
  fun x -> Some x : 'a -> 'a option
  match Some 5 with | None -> 0 | Some n -> n : Int
  fun o -> match o with | None -> 0 | Some n -> n : Int option -> Int
  Ok 1 : (Int, 'b) result
  Error "oops" : ('a, String) result

User type-defs would extend the registry — pending.
2026-05-08 13:05:22 +00:00
d2bf0c0d00 ocaml: phase 5 HM pattern-match inference (+5 tests, 344 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
ocaml-infer-pat covers :pwild, :pvar, :plit, :pcons, :plist, :ptuple,
:pas. Returns {:type T :env ENV2 :subst S} where ENV2 has the pattern's
bound names threaded through.

ocaml-infer-match unifies each clause's pattern type with the scrutinee,
runs the body in the env extended with pattern bindings, and unifies
all body types via a fresh result tv.

Examples:
  fun lst -> match lst with | [] -> 0 | h :: _ -> h : Int list -> Int
  match (1, 2) with | (a, b) -> a + b              : Int

Constructor patterns (:pcon) fall through to a fresh tv for now —
proper handling needs a ctor type registry from 'type' declarations.
2026-05-08 13:02:15 +00:00
202ea9cf5f ocaml: phase 6 List.sort + compare (+7 tests, 339 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
compare is a host builtin returning -1/0/1 (Stdlib.compare semantics)
deferred to host SX </>. List.sort is insertion-sort in OCaml: O(n²)
but works correctly. List.stable_sort = sort.

Tested: ascending int sort, descending via custom comparator (b - a),
empty list, string sort.
2026-05-08 12:59:50 +00:00
812aa75d43 ocaml: phase 6 Hashtbl (+6 tests, 332 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 32s
Backing store is a one-element list cell holding a SX dict; keys
coerced to strings via str so int/string keys work uniformly. API:
create, add, replace, find, find_opt, mem, length.

_hashtbl_create / _hashtbl_add / _hashtbl_replace / _hashtbl_find_opt /
_hashtbl_mem / _hashtbl_length primitives wired in eval.sx; OCaml-side
Hashtbl module wraps them in lib/ocaml/runtime.sx.
2026-05-08 12:57:22 +00:00
6d7197182e ocaml: phase 5 HM tuple + list types (+7 tests, 326 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s
Tuple type (hm-con "*" TYPES); list type (hm-con "list" (TYPE)).
ocaml-infer-tuple threads substitution through each item left-to-right.
ocaml-infer-list unifies all items with a fresh 'a (giving 'a list for
empty []).

Pretty-printer renders 'Int * Int' for tuples and 'Int list' for lists,
matching standard OCaml notation.

Examples:
  fun x y -> (x, y) : 'a -> 'b -> 'a * 'b
  fun x -> [x; x] : 'a -> 'a list
  []                : 'a list
2026-05-08 12:54:15 +00:00
a0abdcf520 ocaml: refresh scoreboard (320/320 across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
2026-05-08 12:50:39 +00:00
88c02c7c73 ocaml: phase 6 expanded stdlib (+15 tests, 319 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
List: concat/flatten, init, find/find_opt, partition, mapi/iteri,
assoc/assoc_opt. Option: iter/fold/to_list. Result: get_ok/get_error/
map_error/to_option.

Fixed skip-to-boundary! in parser to track let..in / begin..end /
struct..end / for/while..done nesting via a depth counter — without
this, nested-let inside a top-level decl body trips over the
decl-boundary detector. Stdlib functions like List.init / mapi / iteri
use begin..end to make their nested-let intent explicit.
2026-05-08 12:49:23 +00:00
bc557a5ad2 ocaml: phase 3 exception declarations (+4 tests, 304 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s
exception NAME [of TYPE] parses to (:exception-def NAME [ARG-SRC]).
Runtime is a no-op: raise/match already work on tagged ctor values, so
'exception E of int;; try raise (E 5) with | E n -> n' end-to-end with
zero new eval logic.
2026-05-08 12:37:58 +00:00
d8f6250962 ocaml: phase 3 type declarations (+5 tests, 300 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
Parser: type [PARAMS] NAME = | Ctor [of T1 [* T2]*] | ...
- PARAMS: optional 'a or ('a, 'b) tyvar list
- AST: (:type-def NAME PARAMS CTORS) with each CTOR (NAME ARG-SOURCES)
- Argument types captured as raw source strings (treated opaquely at
  runtime since ctor dispatch is dynamic)

Runtime is a no-op — constructors and pattern matching already work
dynamically. Phase 5 will use these decls to register ctor types for
HM checking.
2026-05-08 12:32:39 +00:00
851e0585cf ocaml: phase 3 'as' alias + 'when' guard in match (+6 tests, 295 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 50s
Pattern parser top wraps cons-pat with 'as ident' -> (:pas PAT NAME).
Match clause parser consumes optional 'when GUARD-EXPR' before -> and
emits (:case-when PAT GUARD BODY) instead of :case.

Eval: :pas matches inner pattern then binds the alias name; case-when
checks the guard after a successful match and falls through to the next
clause if the guard is false.

Or-patterns deferred — ambiguous with clause separator without
parens-only support.
2026-05-08 12:28:07 +00:00
7fb65cd26a ocaml: phase 1+2 records {x=1;y=2} + with-update (+6 tests, 289 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 51s
Parser: { f = e; f = e; ... } -> (:record (F E)...). { base with f = e;
... } -> (:record-update BASE (F E)...). Eval builds a dict from field
bindings; record-update merges the new fields over the base dict — the
same dict representation already used for modules.

{ also added to at-app-start? so records are valid arg atoms. Field
access via the existing :field postfix unifies record/module access.

Record patterns deferred to a later iteration.
2026-05-08 09:26:24 +00:00
9473911cf3 ocaml: phase 5.1 conformance.sh + scoreboard (283 tests across 14 suites)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 45s
lib/ocaml/conformance.sh runs the full test suite, classifies each
result by description prefix into one of 14 suites (tokenize, parser,
eval-core, phase2-refs/loops/function/exn, phase3-adt, phase4-modules,
phase5-hm, phase6-stdlib, let-and, phase1-params, misc), and emits
scoreboard.json + scoreboard.md.

Per the briefing: "Once the scoreboard exists (Phase 5.1), it is your
north star." Real OCaml testsuite vendoring deferred — needs more
stdlib + ADT decls to make .ml files runnable.
2026-05-08 09:23:06 +00:00
74b80e6b0e ocaml: phase 1 unit/wildcard params + 180s timeout (+5 tests, 283 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Parser: try-consume-param! handles ident, wildcard _ (fresh __wild_N
name), unit () (fresh __unit_N), typed (x : T) (skips signature).
parse-fun and parse-let (inline) reuse the helper; top-level
parse-decl-let inlines a similar test.

test.sh timeout bumped from 60s to 180s — the growing suite was hitting
the cap and reporting spurious failures.
2026-05-08 09:21:06 +00:00
c8bfd22786 ocaml: phase 6 String/Char/Int/Float/Printf modules (+13 tests, 278 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 53s
Host primitives _string_length / _string_sub / _char_code / etc. exposed
in the base env (underscore-prefixed to avoid user clash). lib/ocaml/
runtime.sx wraps them into OCaml-syntax modules: String (length, get,
sub, concat, uppercase/lowercase_ascii, starts_with), Char (code, chr,
lowercase/uppercase_ascii), Int (to_string, of_string, abs, max, min),
Float.to_string, Printf stubs.

Also added print_string / print_endline / print_int IO builtins.
2026-05-08 09:10:06 +00:00
26863242a0 ocaml: phase 5 HM type inference — closes lib-guest step 8 (+14 tests, 265 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 53s
OCaml-on-SX is the deferred second consumer for lib/guest/hm.sx step 8.
lib/ocaml/infer.sx assembles Algorithm W on top of the shipped algebra:

- Var: lookup + hm-instantiate.
- Fun: fresh-tv per param, auto-curried via recursion.
- App: unify against hm-arrow, fresh-tv for result.
- Let: generalize rhs over (ftv(t) - ftv(env)) — let-polymorphism.
- If: unify cond with Bool, both branches with each other.
- Op (+, =, <, etc.): builtin signatures (int*int->int monomorphic,
  =/<> polymorphic 'a->'a->bool).

Tests pass for: literals, fun x -> x : 'a -> 'a, let id ... id 5/id true,
fun f x -> f (f x) : ('a -> 'a) -> 'a -> 'a (twice).

Pending: tuples, lists, pattern matching, let-rec, modules in HM.
2026-05-08 09:02:25 +00:00
4c6790046c ocaml: phase 2 let..and.. mutual recursion (+3 tests, 251 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 51s
Parser collects multiple bindings via 'and', emitting (:def-rec-mut
BINDINGS) for let-rec chains and (:def-mut BINDINGS) for non-rec.
Single bindings keep the existing (:def …) / (:def-rec …) shapes.

Eval (def-rec-mut): allocate placeholder cell per binding, build joint
env where each name forwards through its cell, then evaluate each rhs
against the joint env and fill the cells. Even/odd mutual-rec works.
2026-05-08 08:53:53 +00:00
19f1cad11d ocaml: phase 6 stdlib slice (List/Option/Result, +23 tests, 248 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 45s
lib/ocaml/runtime.sx defines the stdlib in OCaml syntax (not SX): every
function exercises the parser, evaluator, match engine, and module
machinery built in earlier phases. Loaded once via ocaml-load-stdlib!,
cached in ocaml-stdlib-env, layered under user code via ocaml-base-env.

List: length, rev, rev_append, map, filter, fold_left/right, append,
iter, mem, for_all, exists, hd, tl, nth.
Option: map, bind, value, get, is_none, is_some.
Result: map, bind, is_ok, is_error.

Substrate validation: this stdlib is a nontrivial OCaml program — its
mere existence proves the substrate works.
2026-05-08 08:49:44 +00:00
5603ecc3a6 ocaml: phase 4 functors + module aliases (+5 tests, 225 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 53s
Parser: module F (M) (N) ... = struct DECLS end -> (:functor-def NAME
PARAMS DECLS). module N = expr (non-struct) -> (:module-alias NAME
BODY-SRC). Functor params accept (P) or (P : Sig) — signatures
parsed-and-skipped via skip-optional-sig.

Eval: ocaml-make-functor builds curried host-SX closures from module
dicts to a module dict. ocaml-resolve-module-path extended for :app so
F(A), F(A)(B), and Outer.Inner all resolve to dicts.

Phase 4 LOC ~290 cumulative (still well under 2000).
2026-05-08 08:44:54 +00:00
d45e653a87 ocaml: phase 4 open / include (+5 tests, 220 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 57s
Parser: open Path and include Path top-level decls; Path is Ctor (.Ctor)*.
Eval resolves via ocaml-resolve-module-path (same :con-as-module-lookup
escape hatch used by :field). open extends the env with the module's
bindings; include also merges into the surrounding module's exports
(when inside a struct...end).

Path resolver lets M.Sub.x work for nested modules. Phase 4 LOC ~165.
2026-05-08 08:39:13 +00:00
317f93b2af ocaml: phase 4 modules + field access (+11 tests, 215 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
module M = struct DECLS end parsed by sub-tokenising the body source
between struct and the matching end (nesting tracked via struct/begin/
sig/end). Field access is a postfix layer above parse-atom, binding
tighter than application: f r.x -> (:app f (:field r "x")).

Eval (:module-def NAME DECLS) builds a dict via ocaml-eval-module
running decls in a sub-env. (:field EXPR NAME) looks up dict fields,
treating (:con NAME) heads as module-name lookups instead of nullary
ctors so M.x works with M as a module.

Phase 4 LOC so far: ~110 lines (well under 2000 budget).
2026-05-08 08:33:34 +00:00
6a1f63f0d1 ocaml: phase 2 try/with + raise (+6 tests, 204 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 57s
Parser: try EXPR with | pat -> handler | ... -> (:try EXPR CLAUSES).
Eval delegates to SX guard with else matching the raised value against
clause patterns; re-raises on no-match. raise/failwith/invalid_arg
shipped as builtins. failwith "msg" raises ("Failure" msg) so
| Failure msg -> ... patterns match.
2026-05-08 08:20:11 +00:00
937342bbf0 ocaml: phase 2 function | pat -> body (+4 tests, 198 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 54s
Sugar for fun + match. AST (:function CLAUSES) -> unary closure that
runs ocaml-match-clauses on its arg. let rec recognises :function as a
recursive rhs and ties the knot via cell, so

  let rec map f = function | [] -> [] | h::t -> f h :: map f t

works. ocaml-match-eval refactored to share clause-walk with function.
2026-05-08 08:15:38 +00:00
9b8b0b4325 ocaml: phase 2 for/while loops (+5 tests, 194 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
Parser: for i = lo to|downto hi do body done, while cond do body done.
AST: (:for NAME LO HI :ascend|:descend BODY) and (:while COND BODY).
Eval re-binds the loop var per iteration; both forms evaluate to unit.
2026-05-08 08:11:13 +00:00
a11f3c33b6 ocaml: phase 2 references ref/!/:= (+6 tests, 189 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 55s
ref is a builtin boxing its arg in a one-element list. Prefix ! parses
to (:deref ...) and reads via (nth cell 0). := joins the binop
precedence table at level 1 right-assoc and mutates via set-nth!.
Closures share the underlying cell.
2026-05-08 08:07:26 +00:00
9b833a9442 ocaml: phase 3 pattern matching + constructors (+18 tests, 183 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 56s
Constructor app: (:app (:con NAME) arg) -> (NAME …args). Tuple args
flatten so Pair(a,b) -> ("Pair" a b), matching the parser's pattern
flatten. Standalone (:con NAME) -> (NAME) nullary.

Pattern matcher: :pwild, :pvar, :plit, :pcon (head + arity), :pcons
(decompose), :plist (length match), :ptuple (after tuple tag). Match
walks clauses until first success; runtime error on exhaustion.
Recursive list functions (len, sum, fact) work end-to-end.
2026-05-08 08:02:56 +00:00
4dca583ee3 ocaml: phase 2 evaluator slice (+42 tests, 165 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
ocaml-eval walks the AST and yields SX values. ocaml-run / ocaml-run-program
wrap parse + eval. Coverage: atoms, vars, app (curried), 22 binary ops,
prefix - and not, if/seq/tuple/list, fun (auto-curried via host SX
lambdas), let, let-rec (mutable-cell knot for recursive functions).
Initial env: not/succ/pred/abs/max/min/fst/snd/ignore. Tests: arithmetic,
comparison, string concat, closures, fact 5 / fib 10 / sum 100,
top-level decls, |> pipe.
2026-05-08 07:57:20 +00:00
a6ab944c39 ocaml: phase 1 sequence operator ; (+10 tests, 123 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 53s
Two-phase grammar: parse-expr-no-seq (prior entry) + parse-expr wraps
it with ;-chaining. List bodies keep parse-expr-no-seq so ; remains a
separator inside [...]. Match clause bodies use the seq variant and stop
at | — real OCaml semantics. Trailing ; before end/)/|/in/then/else/eof
permitted.
2026-05-08 07:48:52 +00:00
9102e57d89 ocaml: phase 1 match/with + pattern parser (+9 tests, 113 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
Patterns: wildcard, literal, var, ctor (nullary + arg, flattens tuple
args so Pair(a,b) -> (:pcon "Pair" PA PB)), tuple, list literal, cons
:: (right-assoc), unit. Match: leading | optional, (:match SCRUT
CLAUSES) with each clause (:case PAT BODY). Body parsed via parse-expr
because | is below level-1 binop precedence.
2026-05-08 07:29:02 +00:00
9648dac88d ocaml: phase 1 top-level decls (+9 tests, 104 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 49s
ocaml-parse-program: program = decls + bare exprs, ;;-separated.
Each decl is (:def …), (:def-rec …), or (:expr …). Body parsing
re-feeds the source slice through ocaml-parse — shared-state refactor
deferred.
2026-05-08 07:25:11 +00:00
9a090c6e42 ocaml: phase 1 expression parser (+37 tests, 95 total) — consumes lib/guest/pratt.sx
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 55s
Atoms (literals/var/con/unit/list), application (left-assoc), prefix - / not,
29-op precedence table via pratt-op-lookup (incl. keyword-spelled mod/land/
lor/lxor/lsl/lsr/asr), tuples, parens, if/then/else, fun, let, let rec
with function shorthand. AST follows Haskell-on-SX (:int / :op / :fun / etc).
2026-05-07 23:26:48 +00:00
85b7fed4fc ocaml: phase 1 tokenizer (+58 tests) — consumes lib/guest/lex.sx
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 52s
Idents, ctors, 51 keywords, numbers (int/float/hex/exp/underscored),
strings + chars with escapes, type variables, 26 op/punct tokens, and
nested (* ... *) block comments. Tests via epoch protocol against
sx_server.exe.
2026-05-07 23:04:40 +00:00
246 changed files with 15278 additions and 7725 deletions

View File

@@ -1,157 +0,0 @@
;; lib/datalog/aggregates.sx — count / sum / min / max / findall.
;;
;; Surface form (always 3-arg after the relation name):
;;
;; (count Result Var GoalLit)
;; (sum Result Var GoalLit)
;; (min Result Var GoalLit)
;; (max Result Var GoalLit)
;; (findall List Var GoalLit)
;;
;; Parsed naturally because arg-position compounds are already allowed
;; (Phase 4 needs them for arithmetic). At evaluation time the aggregator
;; runs `dl-find-bindings` on `GoalLit` under the current subst, collects
;; the distinct values of `Var`, and binds `Result`.
;;
;; Aggregation is non-monotonic — `count(C, X, p(X))` shrinks as p loses
;; tuples. The stratifier (lib/datalog/strata.sx) treats every aggregate's
;; goal relation as a negation-like edge so the inner relation is fully
;; derived before the aggregate fires.
;;
;; Empty input: count → 0, sum → 0, min/max → no binding (rule fails).
(define dl-aggregate-rels (list "count" "sum" "min" "max" "findall"))
(define
dl-aggregate?
(fn
(lit)
(and
(list? lit)
(>= (len lit) 4)
(let ((rel (dl-rel-name lit)))
(cond
((nil? rel) false)
(else (dl-member-string? rel dl-aggregate-rels)))))))
;; Apply aggregation operator to a list of (already-distinct) numeric or
;; symbolic values. Returns the aggregated value, or :empty if min/max
;; has no input.
(define
dl-do-aggregate
(fn
(op vals)
(cond
((= op "count") (len vals))
((= op "sum") (dl-sum-vals vals 0))
((= op "findall") vals)
((= op "min")
(cond
((= (len vals) 0) :empty)
(else (dl-min-vals vals 1 (first vals)))))
((= op "max")
(cond
((= (len vals) 0) :empty)
(else (dl-max-vals vals 1 (first vals)))))
(else (error (str "datalog: unknown aggregate " op))))))
(define
dl-sum-vals
(fn
(vals acc)
(cond
((= (len vals) 0) acc)
(else (dl-sum-vals (rest vals) (+ acc (first vals)))))))
(define
dl-min-vals
(fn
(vals i cur)
(cond
((>= i (len vals)) cur)
(else
(let ((v (nth vals i)))
(dl-min-vals vals (+ i 1) (if (< v cur) v cur)))))))
(define
dl-max-vals
(fn
(vals i cur)
(cond
((>= i (len vals)) cur)
(else
(let ((v (nth vals i)))
(dl-max-vals vals (+ i 1) (if (> v cur) v cur)))))))
;; Membership check by deep equality (so 30 == 30.0 etc).
(define
dl-val-member?
(fn
(v xs)
(cond
((= (len xs) 0) false)
((dl-tuple-equal? v (first xs)) true)
(else (dl-val-member? v (rest xs))))))
;; Evaluate an aggregate body lit under `subst`. Returns the list of
;; extended substitutions (0 or 1 element).
(define
dl-eval-aggregate
(fn
(lit db subst)
(let
((op (dl-rel-name lit))
(result-var (nth lit 1))
(agg-var (nth lit 2))
(goal (nth lit 3)))
(cond
((not (dl-var? agg-var))
(error (str "datalog aggregate (" op
"): second arg must be a variable, got " agg-var)))
((not (and (list? goal) (> (len goal) 0)
(symbol? (first goal))))
(error (str "datalog aggregate (" op
"): third arg must be a positive literal, got "
goal)))
((not (dl-member-string?
(symbol->string agg-var)
(dl-vars-of goal)))
(error (str "datalog aggregate (" op
"): aggregation variable " agg-var
" does not appear in the goal " goal
" — without it every match contributes the same "
"(unbound) value and the result is meaningless")))
(else
(let ((vals (list)))
(do
(for-each
(fn
(s)
(let ((v (dl-apply-subst agg-var s)))
(when (not (dl-val-member? v vals))
(append! vals v))))
(dl-find-bindings (list goal) db subst))
(let ((agg-val (dl-do-aggregate op vals)))
(cond
((= agg-val :empty) (list))
(else
(let ((s2 (dl-unify result-var agg-val subst)))
(if (nil? s2) (list) (list s2)))))))))))))
;; Stratification edges from aggregates: like negation, the goal's
;; relation must be in a strictly lower stratum so that the aggregate
;; fires only after the underlying tuples are settled.
(define
dl-aggregate-dep-edge
(fn
(lit)
(cond
((dl-aggregate? lit)
(let ((goal (nth lit 3)))
(cond
((and (list? goal) (> (len goal) 0))
(let ((rel (dl-rel-name goal)))
(if (nil? rel) nil {:rel rel :neg true})))
(else nil))))
(else nil))))

View File

@@ -1,303 +0,0 @@
;; lib/datalog/api.sx — SX-data embedding API.
;;
;; Where Phase 1's `dl-program` takes a Datalog source string,
;; this module exposes a parser-free API that consumes SX data
;; directly. Two rule shapes are accepted:
;;
;; - dict: {:head <literal> :body (<literal> ...)}
;; - list: (<head-elements...> <- <body-literal> ...)
;; — `<-` is an SX symbol used as the rule arrow.
;;
;; Examples:
;;
;; (dl-program-data
;; '((parent tom bob) (parent tom liz) (parent bob ann))
;; '((ancestor X Y <- (parent X Y))
;; (ancestor X Z <- (parent X Y) (ancestor Y Z))))
;;
;; (dl-query db '(ancestor tom X)) ; same query API as before
;;
;; Variables follow the parser convention: SX symbols whose first
;; character is uppercase or `_` are variables.
(define
dl-rule
(fn (head body) {:head head :body body}))
(define
dl-rule-arrow?
(fn
(x)
(and (symbol? x) (= (symbol->string x) "<-"))))
(define
dl-find-arrow
(fn
(rl i n)
(cond
((>= i n) nil)
((dl-rule-arrow? (nth rl i)) i)
(else (dl-find-arrow rl (+ i 1) n)))))
;; Given a list of the form (head-elt ... <- body-lit ...) returns
;; {:head (head-elt ...) :body (body-lit ...)}. If no arrow is
;; present, the whole list is treated as the head and the body is
;; empty (i.e. a fact written rule-style).
(define
dl-rule-from-list
(fn
(rl)
(let ((n (len rl)))
(let ((idx (dl-find-arrow rl 0 n)))
(cond
((nil? idx) {:head rl :body (list)})
(else
(let
((head (slice rl 0 idx))
(body (slice rl (+ idx 1) n)))
{:head head :body body})))))))
;; Coerce a rule given as either a dict or a list-with-arrow to a dict.
(define
dl-coerce-rule
(fn
(r)
(cond
((dict? r) r)
((list? r) (dl-rule-from-list r))
(else (error (str "dl-coerce-rule: expected dict or list, got " r))))))
;; Build a db from SX data lists.
(define
dl-program-data
(fn
(facts rules)
(let ((db (dl-make-db)))
(do
(for-each (fn (lit) (dl-add-fact! db lit)) facts)
(for-each
(fn (r) (dl-add-rule! db (dl-coerce-rule r)))
rules)
db))))
;; Add a single fact at runtime, then re-saturate the db so derived
;; tuples reflect the change. Returns the db.
(define
dl-assert!
(fn
(db lit)
(do
(dl-add-fact! db lit)
(dl-saturate! db)
db)))
;; Remove a fact and re-saturate. Mixed relations (which have BOTH
;; user-asserted facts AND rules) are supported via :edb-keys provenance
;; — explicit facts are marked at dl-add-fact! time, the saturator uses
;; dl-add-derived! which doesn't mark them, so the retract pass can
;; safely wipe IDB-derived tuples while preserving the user's EDB.
;;
;; Effect:
;; - remove tuples matching `lit` from :facts and :edb-keys
;; - for every relation that has a rule (i.e. potentially IDB or
;; mixed), drop the IDB-derived portion (anything not in :edb-keys)
;; so the saturator can re-derive cleanly
;; - re-saturate
(define
dl-retract!
(fn
(db lit)
(let
((rel-key (dl-rel-name lit)))
(do
;; Drop the matching tuple from its relation list, its facts-keys,
;; its first-arg index, AND from :edb-keys (if present).
(when
(has-key? (get db :facts) rel-key)
(let
((existing (get (get db :facts) rel-key))
(kept (list))
(kept-keys {})
(kept-index {})
(edb-rel (cond
((has-key? (get db :edb-keys) rel-key)
(get (get db :edb-keys) rel-key))
(else nil)))
(kept-edb {}))
(do
(for-each
(fn
(t)
(when
(not (dl-tuple-equal? t lit))
(do
(append! kept t)
(let ((tk (dl-tuple-key t)))
(do
(dict-set! kept-keys tk true)
(when
(and (not (nil? edb-rel))
(has-key? edb-rel tk))
(dict-set! kept-edb tk true))))
(when
(>= (len t) 2)
(let ((k (dl-arg-key (nth t 1))))
(do
(when
(not (has-key? kept-index k))
(dict-set! kept-index k (list)))
(append! (get kept-index k) t)))))))
existing)
(dict-set! (get db :facts) rel-key kept)
(dict-set! (get db :facts-keys) rel-key kept-keys)
(dict-set! (get db :facts-index) rel-key kept-index)
(when
(not (nil? edb-rel))
(dict-set! (get db :edb-keys) rel-key kept-edb)))))
;; For each rule-head relation, strip the IDB-derived tuples
;; (anything not marked in :edb-keys) so the saturator can
;; cleanly re-derive without leaving stale tuples that depended
;; on the now-removed fact.
(let ((rule-heads (dl-rule-head-rels db)))
(for-each
(fn
(k)
(when
(has-key? (get db :facts) k)
(let
((existing (get (get db :facts) k))
(kept (list))
(kept-keys {})
(kept-index {})
(edb-rel (cond
((has-key? (get db :edb-keys) k)
(get (get db :edb-keys) k))
(else {}))))
(do
(for-each
(fn
(t)
(let ((tk (dl-tuple-key t)))
(when
(has-key? edb-rel tk)
(do
(append! kept t)
(dict-set! kept-keys tk true)
(when
(>= (len t) 2)
(let ((kk (dl-arg-key (nth t 1))))
(do
(when
(not (has-key? kept-index kk))
(dict-set! kept-index kk (list)))
(append! (get kept-index kk) t))))))))
existing)
(dict-set! (get db :facts) k kept)
(dict-set! (get db :facts-keys) k kept-keys)
(dict-set! (get db :facts-index) k kept-index)))))
rule-heads))
(dl-saturate! db)
db))))
;; ── Convenience: single-call source + query ───────────────────
;; (dl-eval source query-source) parses both, builds a db, saturates,
;; runs the query, returns the substitution list. The query source
;; should be `?- goal[, goal ...].` — the parser produces a clause
;; with :query containing a list of literals which is fed straight
;; to dl-query.
(define
dl-eval
(fn
(source query-source)
(let
((db (dl-program source))
(queries (dl-parse query-source)))
(cond
((= (len queries) 0) (error "dl-eval: query string is empty"))
((not (has-key? (first queries) :query))
(error "dl-eval: second arg must be a `?- ...` query clause"))
(else
(dl-query db (get (first queries) :query)))))))
;; (dl-eval-magic source query-source) — like dl-eval but routes a
;; single-positive-literal query through `dl-magic-query` for goal-
;; directed evaluation. Multi-literal query bodies fall back to the
;; standard dl-query path (magic-sets is currently only wired for
;; single-positive goals). The caller's source is parsed afresh
;; each call so successive invocations are independent.
(define
dl-eval-magic
(fn
(source query-source)
(let
((db (dl-program source))
(queries (dl-parse query-source)))
(cond
((= (len queries) 0) (error "dl-eval-magic: query string is empty"))
((not (has-key? (first queries) :query))
(error
"dl-eval-magic: second arg must be a `?- ...` query clause"))
(else
(let
((qbody (get (first queries) :query)))
(cond
((and (= (len qbody) 1)
(list? (first qbody))
(> (len (first qbody)) 0)
(symbol? (first (first qbody))))
(dl-magic-query db (first qbody)))
(else (dl-query db qbody)))))))))
;; List rules whose head's relation matches `rel-name`. Useful for
;; inspection ("show me how this relation is derived") without
;; exposing the internal `:rules` list.
(define
dl-rules-of
(fn
(db rel-name)
(let ((out (list)))
(do
(for-each
(fn
(rule)
(when
(= (dl-rel-name (get rule :head)) rel-name)
(append! out rule)))
(dl-rules db))
out))))
(define
dl-rule-head-rels
(fn
(db)
(let ((seen (list)))
(do
(for-each
(fn
(rule)
(let ((h (dl-rel-name (get rule :head))))
(when
(and (not (nil? h)) (not (dl-member-string? h seen)))
(append! seen h))))
(dl-rules db))
seen))))
;; Wipe every relation that has at least one rule (i.e. every IDB
;; relation) — leaves EDB facts and rule definitions intact. Useful
;; before a follow-up `dl-saturate!` if you want a clean restart, or
;; for inspection of the EDB-only baseline.
(define
dl-clear-idb!
(fn
(db)
(let ((rule-heads (dl-rule-head-rels db)))
(do
(for-each
(fn
(k)
(do
(dict-set! (get db :facts) k (list))
(dict-set! (get db :facts-keys) k {})
(dict-set! (get db :facts-index) k {})))
rule-heads)
db))))

View File

@@ -1,406 +0,0 @@
;; lib/datalog/builtins.sx — comparison + arithmetic body literals.
;;
;; Built-in predicates filter / extend candidate substitutions during
;; rule evaluation. They are not stored facts and do not participate in
;; the Herbrand base.
;;
;; (< a b) (<= a b) (> a b) (>= a b) ; numeric (or string) compare
;; (= a b) ; unify (binds vars)
;; (!= a b) ; ground-only inequality
;; (is X expr) ; bind X to expr's value
;;
;; Arithmetic expressions are SX-list compounds:
;; (+ a b) (- a b) (* a b) (/ a b)
;; or numbers / variables (must be bound at evaluation time).
(define
dl-comparison?
(fn
(lit)
(and
(list? lit)
(> (len lit) 0)
(let
((rel (dl-rel-name lit)))
(cond
((nil? rel) false)
(else (dl-member-string? rel (list "<" "<=" ">" ">=" "!="))))))))
(define
dl-eq?
(fn
(lit)
(and
(list? lit)
(> (len lit) 0)
(let ((rel (dl-rel-name lit))) (and (not (nil? rel)) (= rel "="))))))
(define
dl-is?
(fn
(lit)
(and
(list? lit)
(> (len lit) 0)
(let
((rel (dl-rel-name lit)))
(and (not (nil? rel)) (= rel "is"))))))
;; Evaluate an arithmetic expression under subst. Returns the numeric
;; result, or raises if any operand is unbound or non-numeric.
(define
dl-eval-arith
(fn
(expr subst)
(let
((w (dl-walk expr subst)))
(cond
((number? w) w)
((dl-var? w)
(error (str "datalog arith: unbound variable " (symbol->string w))))
((list? w)
(let
((rel (dl-rel-name w)) (args (rest w)))
(cond
((not (= (len args) 2))
(error (str "datalog arith: need 2 args, got " w)))
(else
(let
((a (dl-eval-arith (first args) subst))
(b (dl-eval-arith (nth args 1) subst)))
(cond
((= rel "+") (+ a b))
((= rel "-") (- a b))
((= rel "*") (* a b))
((= rel "/")
(cond
((= b 0)
(error
(str "datalog arith: division by zero in "
w)))
(else (/ a b))))
(else (error (str "datalog arith: unknown op " rel)))))))))
(else (error (str "datalog arith: not a number — " w)))))))
;; Comparable types — both operands must be the same primitive type
;; (both numbers, both strings). `!=` is the exception: it's defined
;; for any pair (returns true iff not equal) since dl-tuple-equal?
;; handles type-mixed comparisons.
(define
dl-compare-typeok?
(fn
(rel a b)
(cond
((= rel "!=") true)
((and (number? a) (number? b)) true)
((and (string? a) (string? b)) true)
(else false))))
(define
dl-eval-compare
(fn
(lit subst)
(let
((rel (dl-rel-name lit))
(a (dl-walk (nth lit 1) subst))
(b (dl-walk (nth lit 2) subst)))
(cond
((or (dl-var? a) (dl-var? b))
(error
(str
"datalog: comparison "
rel
" has unbound argument; "
"ensure prior body literal binds the variable")))
((not (dl-compare-typeok? rel a b))
(error
(str "datalog: comparison " rel " requires same-type "
"operands (both numbers or both strings), got "
a " and " b)))
(else
(let
((ok (cond ((= rel "<") (< a b)) ((= rel "<=") (<= a b)) ((= rel ">") (> a b)) ((= rel ">=") (>= a b)) ((= rel "!=") (not (dl-tuple-equal? a b))) (else (error (str "datalog: unknown compare " rel))))))
(if ok subst nil)))))))
(define
dl-eval-eq
(fn
(lit subst)
(dl-unify (nth lit 1) (nth lit 2) subst)))
(define
dl-eval-is
(fn
(lit subst)
(let
((target (nth lit 1)) (expr (nth lit 2)))
(let
((value (dl-eval-arith expr subst)))
(dl-unify target value subst)))))
(define
dl-eval-builtin
(fn
(lit subst)
(cond
((dl-comparison? lit) (dl-eval-compare lit subst))
((dl-eq? lit) (dl-eval-eq lit subst))
((dl-is? lit) (dl-eval-is lit subst))
(else (error (str "dl-eval-builtin: not a built-in: " lit))))))
;; ── Safety analysis ──────────────────────────────────────────────
;;
;; Walks body literals left-to-right tracking a "bound" set. The check
;; understands these literal kinds:
;;
;; positive non-built-in → adds its vars to bound
;; (is X expr) → vars(expr) ⊆ bound, then add X (if var)
;; <,<=,>,>=,!= → all vars ⊆ bound (no binding)
;; (= a b) where:
;; both non-vars → constraint check, no binding
;; a var, b not → bind a
;; b var, a not → bind b
;; both vars → at least one in bound; bind the other
;; {:neg lit} → all vars ⊆ bound (Phase 7 enforces fully)
;;
;; At end, head vars (minus `_`) must be ⊆ bound.
(define
dl-vars-not-in
(fn
(vs bound)
(let
((out (list)))
(do
(for-each
(fn
(v)
(when (not (dl-member-string? v bound)) (append! out v)))
vs)
out))))
;; Filter a list of variable-name strings to exclude anonymous-renamed
;; vars (`_` in source → `_anon*` by dl-rename-anon-term). Used by
;; the negation safety check, where anonymous vars are existential
;; within the negated literal.
(define
dl-non-anon-vars
(fn
(vs)
(let
((out (list)))
(do
(for-each
(fn
(v)
(when
(not (and (>= (len v) 5)
(= (slice v 0 5) "_anon")))
(append! out v)))
vs)
out))))
(define
dl-rule-check-safety
(fn
(rule)
(let
((head (get rule :head))
(body (get rule :body))
(bound (list))
(err nil))
(do
(define
dl-add-bound!
(fn
(vs)
(for-each
(fn
(v)
(when (not (dl-member-string? v bound)) (append! bound v)))
vs)))
(define
dl-process-eq!
(fn
(lit)
(let
((a (nth lit 1)) (b (nth lit 2)))
(let
((va (dl-var? a)) (vb (dl-var? b)))
(cond
((and (not va) (not vb)) nil)
((and va (not vb))
(dl-add-bound! (list (symbol->string a))))
((and (not va) vb)
(dl-add-bound! (list (symbol->string b))))
(else
(let
((sa (symbol->string a)) (sb (symbol->string b)))
(cond
((dl-member-string? sa bound)
(dl-add-bound! (list sb)))
((dl-member-string? sb bound)
(dl-add-bound! (list sa)))
(else
(set!
err
(str
"= between two unbound variables "
(list sa sb)
" — at least one must be bound by an "
"earlier positive body literal")))))))))))
(define
dl-process-cmp!
(fn
(lit)
(let
((needed (dl-vars-of (list (nth lit 1) (nth lit 2)))))
(let
((missing (dl-vars-not-in needed bound)))
(when
(> (len missing) 0)
(set!
err
(str
"comparison "
(dl-rel-name lit)
" requires bound variable(s) "
missing
" (must be bound by an earlier positive "
"body literal)")))))))
(define
dl-process-is!
(fn
(lit)
(let
((tgt (nth lit 1)) (expr (nth lit 2)))
(let
((needed (dl-vars-of expr)))
(let
((missing (dl-vars-not-in needed bound)))
(cond
((> (len missing) 0)
(set!
err
(str
"is RHS uses unbound variable(s) "
missing
" — bind them via a prior positive body "
"literal")))
(else
(when
(dl-var? tgt)
(dl-add-bound! (list (symbol->string tgt)))))))))))
(define
dl-process-neg!
(fn
(lit)
(let
((inner (get lit :neg)))
(let
((inner-rn
(cond
((and (list? inner) (> (len inner) 0))
(dl-rel-name inner))
(else nil)))
;; Anonymous variables (`_` in source → `_anon*` after
;; renaming) are existentially quantified within the
;; negated literal — they don't need to be bound by
;; an earlier body lit, since `not p(X, _)` is a
;; valid idiom for "no Y exists s.t. p(X, Y)". Filter
;; them out of the safety check.
(needed (dl-non-anon-vars (dl-vars-of inner)))
(missing (dl-vars-not-in needed bound)))
(cond
((and (not (nil? inner-rn)) (dl-reserved-rel? inner-rn))
(set! err
(str "negated literal uses reserved name '"
inner-rn
"' — nested `not(...)` / negated built-ins are "
"not supported; introduce an intermediate "
"relation and negate that")))
((> (len missing) 0)
(set! err
(str "negation refers to unbound variable(s) "
missing
" — they must be bound by an earlier "
"positive body literal"))))))))
(define
dl-process-agg!
(fn
(lit)
(let
((result-var (nth lit 1)))
;; Aggregate goal vars are existentially quantified within
;; the aggregate; nothing required from outer context. The
;; result var becomes bound after the aggregate fires.
(when
(dl-var? result-var)
(dl-add-bound! (list (symbol->string result-var)))))))
(define
dl-process-lit!
(fn
(lit)
(when
(nil? err)
(cond
((and (dict? lit) (has-key? lit :neg))
(dl-process-neg! lit))
;; A bare dict that is not a recognised negation is
;; almost certainly a typo (e.g. `{:negs ...}` instead
;; of `{:neg ...}`). Without this guard the dict would
;; silently fall through every clause; the head safety
;; check would then flag the head variables as unbound
;; even though the real bug is the malformed body lit.
((dict? lit)
(set! err
(str "body literal is a dict but lacks :neg — "
"the only dict-shaped body lit recognised is "
"{:neg <positive-lit>} for stratified "
"negation, got " lit)))
((dl-aggregate? lit) (dl-process-agg! lit))
((dl-eq? lit) (dl-process-eq! lit))
((dl-is? lit) (dl-process-is! lit))
((dl-comparison? lit) (dl-process-cmp! lit))
((and (list? lit) (> (len lit) 0))
(let ((rn (dl-rel-name lit)))
(cond
((and (not (nil? rn)) (dl-reserved-rel? rn))
(set! err
(str "body literal uses reserved name '" rn
"' — built-ins / aggregates have their own "
"syntax; nested `not(...)` is not supported "
"(use stratified negation via an "
"intermediate relation)")))
(else (dl-add-bound! (dl-vars-of lit))))))
(else
;; Anything that's not a dict, not a list, or an
;; empty list. Numbers / strings / symbols as body
;; lits don't make sense — surface the type.
(set! err
(str "body literal must be a positive lit, "
"built-in, aggregate, or {:neg ...} dict, "
"got " lit)))))))
(for-each dl-process-lit! body)
(when
(nil? err)
(let
((head-vars (dl-vars-of head)) (missing (list)))
(do
(for-each
(fn
(v)
(when
(and (not (dl-member-string? v bound)) (not (= v "_")))
(append! missing v)))
head-vars)
(when
(> (len missing) 0)
(set!
err
(str
"head variable(s) "
missing
" do not appear in any positive body literal"))))))
err))))

View File

@@ -1,32 +0,0 @@
# Datalog conformance config — sourced by lib/guest/conformance.sh.
LANG_NAME=datalog
MODE=dict
PRELOADS=(
lib/datalog/tokenizer.sx
lib/datalog/parser.sx
lib/datalog/unify.sx
lib/datalog/db.sx
lib/datalog/builtins.sx
lib/datalog/aggregates.sx
lib/datalog/strata.sx
lib/datalog/eval.sx
lib/datalog/api.sx
lib/datalog/magic.sx
lib/datalog/demo.sx
)
SUITES=(
"tokenize:lib/datalog/tests/tokenize.sx:(dl-tokenize-tests-run!)"
"parse:lib/datalog/tests/parse.sx:(dl-parse-tests-run!)"
"unify:lib/datalog/tests/unify.sx:(dl-unify-tests-run!)"
"eval:lib/datalog/tests/eval.sx:(dl-eval-tests-run!)"
"builtins:lib/datalog/tests/builtins.sx:(dl-builtins-tests-run!)"
"semi_naive:lib/datalog/tests/semi_naive.sx:(dl-semi-naive-tests-run!)"
"negation:lib/datalog/tests/negation.sx:(dl-negation-tests-run!)"
"aggregates:lib/datalog/tests/aggregates.sx:(dl-aggregates-tests-run!)"
"api:lib/datalog/tests/api.sx:(dl-api-tests-run!)"
"magic:lib/datalog/tests/magic.sx:(dl-magic-tests-run!)"
"demo:lib/datalog/tests/demo.sx:(dl-demo-tests-run!)"
)

View File

@@ -1,3 +0,0 @@
#!/usr/bin/env bash
# Thin wrapper — see lib/guest/conformance.sh and lib/datalog/conformance.conf.
exec bash "$(dirname "$0")/../guest/conformance.sh" "$(dirname "$0")/conformance.conf" "$@"

View File

@@ -1,97 +0,0 @@
;; lib/datalog/datalog.sx — public API documentation index.
;;
;; This file is reference-only — `load` is an epoch-protocol command,
;; not an SX function, so it cannot reload a list of files from inside
;; another `.sx` file. To set up a fresh sx_server session with all
;; modules in scope, issue these loads in order:
;;
;; (load "lib/datalog/tokenizer.sx")
;; (load "lib/datalog/parser.sx")
;; (load "lib/datalog/unify.sx")
;; (load "lib/datalog/db.sx")
;; (load "lib/datalog/builtins.sx")
;; (load "lib/datalog/aggregates.sx")
;; (load "lib/datalog/strata.sx")
;; (load "lib/datalog/eval.sx")
;; (load "lib/datalog/api.sx")
;; (load "lib/datalog/magic.sx")
;; (load "lib/datalog/demo.sx")
;;
;; (lib/datalog/conformance.sh runs this load list automatically.)
;;
;; ── Public API surface ─────────────────────────────────────────────
;;
;; Source / data:
;; (dl-tokenize "src") → token list
;; (dl-parse "src") → parsed clauses
;; (dl-program "src") → db built from a source string
;; (dl-program-data facts rules) → db from SX data lists; rules
;; accept either dict form or
;; list form with `<-` arrow
;;
;; Construction (mutates db):
;; (dl-make-db) empty db
;; (dl-add-fact! db lit) rejects non-ground
;; (dl-add-rule! db rule) rejects unsafe rules
;; (dl-rule head body) dict-rule constructor
;; (dl-add-clause! db clause) parser output → fact or rule
;; (dl-load-program! db src) string source
;; (dl-set-strategy! db strategy) :semi-naive default; :magic
;; is informational, use
;; dl-magic-query for actual
;; magic-sets evaluation
;;
;; Mutation:
;; (dl-assert! db lit) add + re-saturate
;; (dl-retract! db lit) drop EDB, wipe IDB, re-saturate
;; (dl-clear-idb! db) wipe rule-headed relations
;;
;; Query / inspection:
;; (dl-saturate! db) stratified semi-naive default
;; (dl-saturate-naive! db) reference (slow on chains)
;; (dl-saturate-rules! db rules) per-rule-set semi-naive worker
;; (dl-query db goal) list of substitution dicts
;; (dl-relation db rel-name) tuple list for a relation
;; (dl-rules db) rule list
;; (dl-fact-count db) total ground tuples
;; (dl-summary db) {<rel>: count} for inspection
;;
;; Single-call convenience:
;; (dl-eval source query-source) parse, run, return substs
;; (dl-eval-magic source query-source) single-goal → magic-sets
;;
;; Magic-sets (lib/datalog/magic.sx):
;; (dl-adorn-goal goal) "b/f" adornment string
;; (dl-rule-sips rule head-adn) SIPS analysis per body lit
;; (dl-magic-rewrite rules rel adn args)
;; rewritten rule list + seed
;; (dl-magic-query db query-goal) end-to-end magic-sets query
;;
;; ── Body literal kinds ─────────────────────────────────────────────
;;
;; Positive (rel arg ... arg)
;; Negation {:neg (rel arg ...)}
;; Comparison (< X Y), (<= X Y), (> X Y), (>= X Y),
;; (= X Y), (!= X Y)
;; Arithmetic (is Z (+ X Y)) and (- * /)
;; Aggregation (count R V Goal), (sum R V Goal),
;; (min R V Goal), (max R V Goal),
;; (findall L V Goal)
;;
;; ── Variable conventions ───────────────────────────────────────────
;;
;; Variables: SX symbols whose first char is uppercase AZ or '_'.
;; Anonymous '_' is renamed to a fresh _anon<N> per occurrence at
;; rule/query load time so multiple '_' don't unify.
;;
;; ── Demo programs ──────────────────────────────────────────────────
;;
;; See lib/datalog/demo.sx — federation, content, permissions, and
;; the canonical "cooking posts by people I follow (transitively)"
;; example.
;;
;; ── Status ─────────────────────────────────────────────────────────
;;
;; See plans/datalog-on-sx.md — phase-by-phase progress log and
;; roadmap. Run `bash lib/datalog/conformance.sh` to refresh
;; `lib/datalog/scoreboard.{json,md}`.

View File

@@ -1,575 +0,0 @@
;; lib/datalog/db.sx — Datalog database (EDB + IDB + rules) + safety hook.
;;
;; A db is a mutable dict:
;; {:facts {<rel-name-string> -> (literal ...)}
;; :rules ({:head literal :body (literal ...)} ...)}
;;
;; Facts are stored as full literals `(rel arg ... arg)` so they unify
;; directly against rule body literals. Each relation's tuple list is
;; deduplicated on insert.
;;
;; Phase 3 introduced safety analysis for head variables; Phase 4 (in
;; lib/datalog/builtins.sx) swaps in the real `dl-rule-check-safety`,
;; which is order-aware and understands built-in predicates.
(define
dl-make-db
(fn ()
{:facts {}
:facts-keys {}
:facts-index {}
:edb-keys {}
:rules (list)
:strategy :semi-naive}))
;; Record (rel-key, tuple-key) as user-asserted EDB. dl-add-fact! calls
;; this when an explicit fact is added; the saturator (which uses
;; dl-add-derived!) does NOT, so derived tuples never appear here.
;; dl-retract! consults :edb-keys to know which tuples must survive
;; the wipe-and-resaturate round-trip.
(define
dl-mark-edb!
(fn
(db rel-key tk)
(let
((edb (get db :edb-keys)))
(do
(when
(not (has-key? edb rel-key))
(dict-set! edb rel-key {}))
(dict-set! (get edb rel-key) tk true)))))
(define
dl-edb-fact?
(fn
(db rel-key tk)
(let
((edb (get db :edb-keys)))
(and (has-key? edb rel-key)
(has-key? (get edb rel-key) tk)))))
;; Evaluation strategy. Default :semi-naive (used by dl-saturate!).
;; :naive selects dl-saturate-naive! (slower but easier to reason
;; about). :magic is a marker — goal-directed magic-sets evaluation
;; is invoked separately via `dl-magic-query`; setting :magic here
;; is purely informational. Any other value is rejected so typos
;; don't silently fall back to the default.
(define
dl-strategy-values
(list :semi-naive :naive :magic))
(define
dl-set-strategy!
(fn
(db strategy)
(cond
((not (dl-keyword-member? strategy dl-strategy-values))
(error (str "dl-set-strategy!: unknown strategy " strategy
" — must be one of " dl-strategy-values)))
(else
(do
(dict-set! db :strategy strategy)
db)))))
(define
dl-keyword-member?
(fn
(k xs)
(cond
((= (len xs) 0) false)
((= k (first xs)) true)
(else (dl-keyword-member? k (rest xs))))))
(define
dl-get-strategy
(fn
(db)
(if (has-key? db :strategy) (get db :strategy) :semi-naive)))
(define
dl-rel-name
(fn
(lit)
(cond
((and (dict? lit) (has-key? lit :neg)) (dl-rel-name (get lit :neg)))
((and (list? lit) (> (len lit) 0) (symbol? (first lit)))
(symbol->string (first lit)))
(else nil))))
(define dl-builtin-rels (list "<" "<=" ">" ">=" "=" "!=" "is"))
(define
dl-member-string?
(fn
(s xs)
(cond
((= (len xs) 0) false)
((= (first xs) s) true)
(else (dl-member-string? s (rest xs))))))
(define
dl-builtin?
(fn
(lit)
(and
(list? lit)
(> (len lit) 0)
(let
((rel (dl-rel-name lit)))
(cond
((nil? rel) false)
(else (dl-member-string? rel dl-builtin-rels)))))))
(define
dl-positive-lit?
(fn
(lit)
(cond
((and (dict? lit) (has-key? lit :neg)) false)
((dl-builtin? lit) false)
((and (list? lit) (> (len lit) 0)) true)
(else false))))
(define
dl-tuple-equal?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-tuple-equal-list? a b 0)))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-tuple-equal-list?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-tuple-equal? (nth a i) (nth b i))) false)
(else (dl-tuple-equal-list? a b (+ i 1))))))
(define
dl-tuple-member?
(fn
(lit lits)
(dl-tuple-member-aux? lit lits 0 (len lits))))
(define
dl-tuple-member-aux?
(fn
(lit lits i n)
(cond
((>= i n) false)
((dl-tuple-equal? lit (nth lits i)) true)
(else (dl-tuple-member-aux? lit lits (+ i 1) n)))))
(define
dl-ensure-rel!
(fn
(db rel-key)
(let
((facts (get db :facts))
(fk (get db :facts-keys))
(fi (get db :facts-index)))
(do
(when
(not (has-key? facts rel-key))
(dict-set! facts rel-key (list)))
(when
(not (has-key? fk rel-key))
(dict-set! fk rel-key {}))
(when
(not (has-key? fi rel-key))
(dict-set! fi rel-key {}))
(get facts rel-key)))))
;; First-arg index helpers. Tuples are keyed by their first-after-rel
;; arg's `(str ...)`; when that arg is a constant, dl-match-positive
;; uses the index instead of scanning the full relation.
(define
dl-arg-key
(fn
(v)
(str v)))
(define
dl-index-add!
(fn
(db rel-key lit)
(let
((idx (get db :facts-index))
(n (len lit)))
(when
(and (>= n 2) (has-key? idx rel-key))
(let
((rel-idx (get idx rel-key))
(k (dl-arg-key (nth lit 1))))
(do
(when
(not (has-key? rel-idx k))
(dict-set! rel-idx k (list)))
(append! (get rel-idx k) lit)))))))
(define
dl-index-lookup
(fn
(db rel-key arg-val)
(let
((idx (get db :facts-index)))
(cond
((not (has-key? idx rel-key)) (list))
(else
(let ((rel-idx (get idx rel-key))
(k (dl-arg-key arg-val)))
(if (has-key? rel-idx k) (get rel-idx k) (list))))))))
(define dl-tuple-key (fn (lit) (str lit)))
(define
dl-rel-tuples
(fn
(db rel-key)
(let
((facts (get db :facts)))
(if (has-key? facts rel-key) (get facts rel-key) (list)))))
;; Reserved relation names: built-in / aggregate / negation / arrow.
;; Rules and facts may not have these as their head's relation, since
;; the saturator treats them specially or they are not relation names
;; at all.
(define
dl-reserved-rel-names
(list "not" "count" "sum" "min" "max" "findall" "is"
"<" "<=" ">" ">=" "=" "!=" "+" "-" "*" "/" ":-" "?-"))
(define
dl-reserved-rel?
(fn
(name) (dl-member-string? name dl-reserved-rel-names)))
;; Internal: append a derived tuple to :facts without the public
;; validation pass and without marking :edb-keys. Used by the saturator
;; (eval.sx) and magic-sets (magic.sx). Returns true if the tuple was
;; new, false if already present.
(define
dl-add-derived!
(fn
(db lit)
(let
((rel-key (dl-rel-name lit)))
(let
((tuples (dl-ensure-rel! db rel-key))
(key-dict (get (get db :facts-keys) rel-key))
(tk (dl-tuple-key lit)))
(cond
((has-key? key-dict tk) false)
(else
(do
(dict-set! key-dict tk true)
(append! tuples lit)
(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
(db lit)
(cond
((not (and (list? lit) (> (len lit) 0)))
(error (str "dl-add-fact!: expected literal list, got " lit)))
((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
(let
((rel-key (dl-rel-name lit)) (tk (dl-tuple-key lit)))
(do
;; Always mark EDB origin — even if the tuple key was already
;; present (e.g. previously derived), so an explicit assert
;; promotes it to EDB and protects it from the IDB wipe.
(dl-mark-edb! db rel-key tk)
(dl-add-derived! db lit)))))))
;; The full safety check lives in builtins.sx (it has to know which
;; predicates are built-ins). dl-add-rule! calls it via forward
;; reference; load builtins.sx alongside db.sx in any setup that
;; adds rules. The fallback below is used if builtins.sx isn't loaded.
(define
dl-rule-check-safety
(fn
(rule)
(let
((head-vars (dl-vars-of (get rule :head))) (body-vars (list)))
(do
(for-each
(fn
(lit)
(when
(and
(list? lit)
(> (len lit) 0)
(not (and (dict? lit) (has-key? lit :neg))))
(for-each
(fn
(v)
(when
(not (dl-member-string? v body-vars))
(append! body-vars v)))
(dl-vars-of lit))))
(get rule :body))
(let
((missing (list)))
(do
(for-each
(fn
(v)
(when
(and
(not (dl-member-string? v body-vars))
(not (= v "_")))
(append! missing v)))
head-vars)
(cond
((> (len missing) 0)
(str
"head variable(s) "
missing
" do not appear in any body literal"))
(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
(start)
(let ((counter start))
(fn () (do (set! counter (+ counter 1))
(string->symbol (str "_anon" counter)))))))
;; Scan a rule for variables already named `_anon<N>` (which would
;; otherwise collide with the renamer's output). Returns the max N
;; seen, or 0 if none. The renamer then starts at that max + 1, so
;; freshly-introduced anonymous names can't shadow a user-written
;; `_anon<N>` symbol.
(define
dl-max-anon-num
(fn
(term acc)
(cond
((symbol? term)
(let ((s (symbol->string term)))
(cond
((and (>= (len s) 6) (= (slice s 0 5) "_anon"))
(let ((n (dl-try-parse-int (slice s 5 (len s)))))
(cond
((nil? n) acc)
((> n acc) n)
(else acc))))
(else acc))))
((dict? term)
(cond
((has-key? term :neg)
(dl-max-anon-num (get term :neg) acc))
(else acc)))
((list? term) (dl-max-anon-num-list term acc 0))
(else acc))))
(define
dl-max-anon-num-list
(fn
(xs acc i)
(cond
((>= i (len xs)) acc)
(else
(dl-max-anon-num-list xs (dl-max-anon-num (nth xs i) acc) (+ i 1))))))
;; Cheap "is this string a decimal int" check. Returns the number or
;; nil. Avoids relying on host parse-number, which on non-int strings
;; might raise rather than return nil.
(define
dl-try-parse-int
(fn
(s)
(cond
((= (len s) 0) nil)
((not (dl-all-digits? s 0 (len s))) nil)
(else (parse-number s)))))
(define
dl-all-digits?
(fn
(s i n)
(cond
((>= i n) true)
((let ((c (slice s i (+ i 1))))
(not (and (>= c "0") (<= c "9"))))
false)
(else (dl-all-digits? s (+ i 1) n)))))
(define
dl-rename-anon-rule
(fn
(rule)
(let
((start (dl-max-anon-num (get rule :head)
(dl-max-anon-num-list (get rule :body) 0 0))))
(let ((next-name (dl-make-anon-renamer start)))
{: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
dl-add-rule!
(fn
(db rule)
(cond
((not (dict? rule))
(error (str "dl-add-rule!: expected rule dict, got " rule)))
((not (has-key? rule :head))
(error (str "dl-add-rule!: rule missing :head, got " rule)))
((not (and (list? (get rule :head))
(> (len (get rule :head)) 0)
(symbol? (first (get rule :head)))))
(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))))
((dl-reserved-rel? (dl-rel-name (get rule :head)))
(error (str "dl-add-rule!: '" (dl-rel-name (get rule :head))
"' is a reserved name (built-in / aggregate / negation)")))
(else
(let ((rule (dl-rename-anon-rule rule)))
(let
((err (dl-rule-check-safety rule)))
(cond
((not (nil? err)) (error (str "dl-add-rule!: " err)))
(else
(let
((rules (get db :rules)))
(do (append! rules rule) true))))))))))
(define
dl-add-clause!
(fn
(db clause)
(cond
((has-key? clause :query) false)
((and (has-key? clause :body) (= (len (get clause :body)) 0))
(dl-add-fact! db (get clause :head)))
(else (dl-add-rule! db clause)))))
(define
dl-load-program!
(fn
(db source)
(let
((clauses (dl-parse source)))
(do (for-each (fn (c) (dl-add-clause! db c)) clauses) db))))
(define
dl-program
(fn (source) (let ((db (dl-make-db))) (dl-load-program! db source))))
(define dl-rules (fn (db) (get db :rules)))
(define
dl-fact-count
(fn
(db)
(let
((facts (get db :facts)) (total 0))
(do
(for-each
(fn (k) (set! total (+ total (len (get facts k)))))
(keys facts))
total))))
;; Returns {<rel-name>: tuple-count} for debugging. Includes
;; relations with any tuples plus all rule-head relations (so empty
;; IDB shows as 0). Skips empty EDB-only entries that are placeholders
;; from internal `dl-ensure-rel!` calls.
(define
dl-summary
(fn
(db)
(let
((facts (get db :facts))
(out {})
(rule-heads (list)))
(do
(for-each
(fn
(rule)
(let ((h (dl-rel-name (get rule :head))))
(when
(and (not (nil? h)) (not (dl-member-string? h rule-heads)))
(append! rule-heads h))))
(dl-rules db))
(for-each
(fn
(k)
(let ((c (len (get facts k))))
(when
(or (> c 0) (dl-member-string? k rule-heads))
(dict-set! out k c))))
(keys facts))
;; Add rule heads that have no facts (yet).
(for-each
(fn
(k)
(when (not (has-key? out k)) (dict-set! out k 0)))
rule-heads)
out))))

View File

@@ -1,162 +0,0 @@
;; lib/datalog/demo.sx — example programs over rose-ash-shaped data.
;;
;; Phase 10 prototypes Datalog as a rose-ash query language. Wiring
;; the EDB to actual PostgreSQL is out of scope for this loop (it
;; would touch service code outside lib/datalog/), but the programs
;; below show the shape of queries we want, and the test suite runs
;; them against synthetic in-memory tuples loaded via dl-program-data.
;;
;; Seven thematic demos:
;;
;; 1. Federation — follow graph, transitive reach, mutuals, FOAF.
;; 2. Content — posts, tags, likes, popularity, "for you" feed.
;; 3. Permissions — group membership and resource access.
;; 4. Cooking-posts — canonical "posts about cooking by people I
;; follow (transitively)" multi-domain query.
;; 5. Tag co-occurrence — distinct (T1, T2) pairs with counts.
;; 6. Shortest path — weighted-DAG path enumeration + min agg.
;; 7. Org chart — transitive subordinate + headcount per mgr.
;; ── Demo 1: federation follow graph ─────────────────────────────
;; EDB: (follows ACTOR-A ACTOR-B) — A follows B.
;; IDB:
;; (mutual A B) — A follows B and B follows A
;; (reachable A B) — transitive follow closure
;; (foaf A C) — friend of a friend (mutual filter)
(define
dl-demo-federation-rules
(quote
((mutual A B <- (follows A B) (follows B A))
(reachable A B <- (follows A B))
(reachable A C <- (follows A B) (reachable B C))
(foaf A C <- (follows A B) (follows B C) (!= A C)))))
;; ── Demo 2: content recommendation ──────────────────────────────
;; EDB:
;; (authored ACTOR POST)
;; (tagged POST TAG)
;; (liked ACTOR POST)
;; IDB:
;; (post-likes POST N) — count of likes per post
;; (popular POST) — posts with >= 3 likes
;; (tagged-by-mutual ACTOR POST) — post tagged TOPIC by someone
;; A's mutuals follow.
(define
dl-demo-content-rules
(quote
((post-likes P N <- (authored Author P) (count N L (liked L P)))
(popular P <- (authored Author P) (post-likes P N) (>= N 3))
(interesting Me P
<-
(follows Me Buddy)
(authored Buddy P)
(popular P)))))
;; ── Demo 3: role-based permissions ──────────────────────────────
;; EDB:
;; (member ACTOR GROUP)
;; (subgroup CHILD PARENT)
;; (allowed GROUP RESOURCE)
;; IDB:
;; (in-group ACTOR GROUP) — direct or via subgroup chain
;; (can-access ACTOR RESOURCE) — actor inherits group permission
(define
dl-demo-perm-rules
(quote
((in-group A G <- (member A G))
(in-group A G <- (member A H) (subgroup-trans H G))
(subgroup-trans X Y <- (subgroup X Y))
(subgroup-trans X Z <- (subgroup X Y) (subgroup-trans Y Z))
(can-access A R <- (in-group A G) (allowed G R)))))
;; ── Demo 4: cooking-posts (the canonical Phase 10 query) ────────
;; "Posts about cooking by people I follow (transitively)."
;; Combines federation (follows + transitive reach), authoring,
;; tagging — the rose-ash multi-domain join.
;;
;; EDB:
;; (follows ACTOR-A ACTOR-B)
;; (authored ACTOR POST)
;; (tagged POST TAG)
(define
dl-demo-cooking-rules
(quote
((reach Me Them <- (follows Me Them))
(reach Me Them <- (follows Me X) (reach X Them))
(cooking-post-by-network Me P
<-
(reach Me Author)
(authored Author P)
(tagged P cooking)))))
;; ── Demo 5: tag co-occurrence ───────────────────────────────────
;; "Posts tagged with both T1 AND T2." Useful for narrowed-down
;; recommendations like "vegetarian cooking" posts.
;;
;; EDB:
;; (tagged POST TAG)
;; IDB:
;; (cotagged POST T1 T2) — post has both T1 and T2 (T1 != T2)
;; (popular-pair T1 T2 N) — count of posts cotagged (T1, T2)
(define
dl-demo-tag-cooccur-rules
(quote
((cotagged P T1 T2 <- (tagged P T1) (tagged P T2) (!= T1 T2))
;; Distinct (T1, T2) pairs that occur somewhere.
(tag-pair T1 T2 <- (cotagged P T1 T2))
(tag-pair-count T1 T2 N
<-
(tag-pair T1 T2)
(count N P (cotagged P T1 T2))))))
;; ── Demo 6: weighted-DAG shortest path ─────────────────────────
;; "What's the cheapest way from X to Y?" Edge weights with `is`
;; arithmetic to sum costs, then `min` aggregation to pick the
;; shortest. Termination requires the graph to be a DAG (cycles
;; would produce infinite distances without a bound; programs
;; built on this should add a depth filter `(<, D, MAX)` if cycles
;; are possible).
;;
;; EDB:
;; (edge FROM TO COST)
;; IDB:
;; (path FROM TO COST) — any path
;; (shortest FROM TO COST) — minimum cost path
(define
dl-demo-shortest-path-rules
(quote
((path X Y W <- (edge X Y W))
(path X Z W
<-
(edge X Y W1)
(path Y Z W2)
(is W (+ W1 W2)))
(shortest X Y W <- (path X Y _) (min W C (path X Y C))))))
;; ── Demo 7: org chart + transitive headcount ───────────────────
;; Manager graph: each employee has a single manager. Compute the
;; transitive subordinate set and headcount per manager.
;;
;; EDB:
;; (manager EMP MGR) — EMP reports directly to MGR
;; IDB:
;; (subordinate MGR EMP) — EMP is in MGR's subtree
;; (headcount MGR N) — number of subordinates under MGR
(define
dl-demo-org-rules
(quote
((subordinate Mgr Emp <- (manager Emp Mgr))
(subordinate Mgr Emp
<- (manager Mid Mgr) (subordinate Mid Emp))
(headcount Mgr N
<- (subordinate Mgr Anyone) (count N E (subordinate Mgr E))))))
;; ── Loader stub ──────────────────────────────────────────────────
;; Wiring to PostgreSQL would replace these helpers with calls into
;; rose-ash's internal HTTP RPC (fetch_data → /internal/data/...).
;; The shape returned by dl-load-from-edb! is the same in either case.
(define
dl-demo-make
(fn
(facts rules)
(dl-program-data facts rules)))

View File

@@ -1,512 +0,0 @@
;; lib/datalog/eval.sx — fixpoint evaluator (naive + semi-naive).
;;
;; Two saturators are exposed:
;; `dl-saturate-naive!` — re-joins each rule against the full DB every
;; iteration. Reference implementation; useful for differential tests.
;; `dl-saturate!` — semi-naive default. Tracks per-relation delta
;; sets and substitutes one positive body literal per rule with the
;; delta of its relation, joining the rest against the previous-
;; iteration DB. Same fixpoint, dramatically less work on recursive
;; rules.
;;
;; Body literal kinds:
;; positive (rel arg ... arg) → match against EDB+IDB tuples
;; built-in (< X Y), (is X e) → constraint via dl-eval-builtin
;; negation {:neg lit} → Phase 7
(define
dl-match-positive
(fn
(lit db subst)
(let
((rel (dl-rel-name lit)) (results (list)))
(cond
((nil? rel) (error (str "dl-match-positive: bad literal " lit)))
(else
(let
;; If the first argument walks to a non-variable (constant
;; or already-bound var), use the first-arg index for
;; this relation. Otherwise scan the full tuple list.
((tuples
(cond
((>= (len lit) 2)
(let ((walked (dl-walk (nth lit 1) subst)))
(cond
((dl-var? walked) (dl-rel-tuples db rel))
(else (dl-index-lookup db rel walked)))))
(else (dl-rel-tuples db rel)))))
(do
(for-each
(fn
(tuple)
(let
((s (dl-unify lit tuple subst)))
(when (not (nil? s)) (append! results s))))
tuples)
results)))))))
;; Match a positive literal against the delta set for its relation only.
(define
dl-match-positive-delta
(fn
(lit delta subst)
(let
((rel (dl-rel-name lit)) (results (list)))
(let
((tuples (if (has-key? delta rel) (get delta rel) (list))))
(do
(for-each
(fn
(tuple)
(let
((s (dl-unify lit tuple subst)))
(when (not (nil? s)) (append! results s))))
tuples)
results)))))
;; Naive matcher (for dl-saturate-naive! and dl-query post-saturation).
(define
dl-match-negation
(fn
(inner db subst)
(let
((walked (dl-apply-subst inner subst))
(matches (dl-match-positive inner db subst)))
(cond
((= (len matches) 0) (list subst))
(else (list))))))
(define
dl-match-lit
(fn
(lit db subst)
(cond
((and (dict? lit) (has-key? lit :neg))
(dl-match-negation (get lit :neg) db subst))
((dl-aggregate? lit) (dl-eval-aggregate lit db subst))
((dl-builtin? lit)
(let
((s (dl-eval-builtin lit subst)))
(if (nil? s) (list) (list s))))
((and (list? lit) (> (len lit) 0))
(dl-match-positive lit db subst))
(else (error (str "datalog: unknown body-literal shape: " lit))))))
(define
dl-find-bindings
(fn (lits db subst) (dl-fb-aux lits db subst 0 (len lits))))
(define
dl-fb-aux
(fn
(lits db subst i n)
(cond
((nil? subst) (list))
((>= i n) (list subst))
(else
(let
((options (dl-match-lit (nth lits i) db subst))
(results (list)))
(do
(for-each
(fn
(s)
(for-each
(fn (s2) (append! results s2))
(dl-fb-aux lits db s (+ i 1) n)))
options)
results))))))
;; Naive: apply each rule against full DB until no new tuples.
(define
dl-apply-rule!
(fn
(db rule)
(let
((head (get rule :head)) (body (get rule :body)) (new? false))
(do
(for-each
(fn
(s)
(let
((derived (dl-apply-subst head s)))
(when (dl-add-derived! db derived) (set! new? true))))
(dl-find-bindings body db (dl-empty-subst)))
new?))))
;; Returns true iff one more saturation step would derive no new
;; tuples (i.e. the db is at fixpoint). Useful in tests that want
;; to assert "no work left" after a saturation call. Works under
;; either saturator since both compute the same fixpoint.
(define
dl-saturated?
(fn
(db)
(let ((any-new false))
(do
(for-each
(fn
(rule)
(when (not any-new)
(for-each
(fn
(s)
(let ((derived (dl-apply-subst (get rule :head) s)))
(when
(and (not any-new)
(not (dl-tuple-member?
derived
(dl-rel-tuples
db (dl-rel-name derived)))))
(set! any-new true))))
(dl-find-bindings (get rule :body) db (dl-empty-subst)))))
(dl-rules db))
(not any-new)))))
(define
dl-saturate-naive!
(fn
(db)
(let
((changed true))
(do
(define
dl-snloop
(fn
()
(when
changed
(do
(set! changed false)
(for-each
(fn (r) (when (dl-apply-rule! db r) (set! changed true)))
(dl-rules db))
(dl-snloop)))))
(dl-snloop)
db))))
;; ── Semi-naive ───────────────────────────────────────────────────
;; Take a snapshot dict {rel -> tuples} of every relation currently in
;; the DB. Used as initial delta for the first iteration.
(define
dl-snapshot-facts
(fn
(db)
(let
((facts (get db :facts)) (out {}))
(do
(for-each
(fn (k) (dict-set! out k (dl-copy-list (get facts k))))
(keys facts))
out))))
(define
dl-copy-list
(fn
(xs)
(let
((out (list)))
(do (for-each (fn (x) (append! out x)) xs) out))))
;; Does any relation in `delta` have ≥1 tuple?
(define
dl-delta-empty?
(fn
(delta)
(let
((ks (keys delta)) (any-non-empty false))
(do
(for-each
(fn
(k)
(when
(> (len (get delta k)) 0)
(set! any-non-empty true)))
ks)
(not any-non-empty)))))
;; Find substitutions such that `lits` are all satisfied AND `delta-idx`
;; is matched against the per-relation delta only. The other positive
;; literals match against the snapshot DB (db.facts read at iteration
;; start). Built-ins and negations behave as in `dl-match-lit`.
(define
dl-find-bindings-semi
(fn
(lits db delta delta-idx subst)
(dl-fbs-aux lits db delta delta-idx 0 subst)))
(define
dl-fbs-aux
(fn
(lits db delta delta-idx i subst)
(cond
((nil? subst) (list))
((>= i (len lits)) (list subst))
(else
(let
((lit (nth lits i))
(options
(cond
((and (dict? lit) (has-key? lit :neg))
(dl-match-negation (get lit :neg) db subst))
((dl-aggregate? lit) (dl-eval-aggregate lit db subst))
((dl-builtin? lit)
(let
((s (dl-eval-builtin lit subst)))
(if (nil? s) (list) (list s))))
((and (list? lit) (> (len lit) 0))
(if
(= i delta-idx)
(dl-match-positive-delta lit delta subst)
(dl-match-positive lit db subst)))
(else (error (str "datalog: unknown body-lit: " lit)))))
(results (list)))
(do
(for-each
(fn
(s)
(for-each
(fn (s2) (append! results s2))
(dl-fbs-aux lits db delta delta-idx (+ i 1) s)))
options)
results))))))
;; Collect candidate head tuples from a rule using delta. Walks every
;; positive body position and unions the resulting heads. For rules
;; with no positive body literal, falls back to a naive single-pass
;; (so static facts like `(p X) :- (= X 5).` derive on iteration 1).
(define
dl-collect-rule-candidates
(fn
(rule db delta)
(let
((head (get rule :head))
(body (get rule :body))
(out (list))
(saw-pos false))
(do
(define
dl-cri
(fn
(i)
(when
(< i (len body))
(do
(let
((lit (nth body i)))
(when
(dl-positive-lit? lit)
(do
(set! saw-pos true)
(for-each
(fn (s) (append! out (dl-apply-subst head s)))
(dl-find-bindings-semi
body
db
delta
i
(dl-empty-subst))))))
(dl-cri (+ i 1))))))
(dl-cri 0)
(when
(not saw-pos)
(for-each
(fn (s) (append! out (dl-apply-subst head s)))
(dl-find-bindings body db (dl-empty-subst))))
out))))
;; Add a list of candidate tuples to db; collect newly-added ones into
;; the new-delta dict (keyed by relation name).
(define
dl-commit-candidates!
(fn
(db candidates new-delta)
(for-each
(fn
(lit)
(when
(dl-add-derived! db lit)
(let
((rel (dl-rel-name lit)))
(do
(when
(not (has-key? new-delta rel))
(dict-set! new-delta rel (list)))
(append! (get new-delta rel) lit)))))
candidates)))
(define
dl-saturate-rules!
(fn
(db rules)
(let
((delta (dl-snapshot-facts db)))
(do
(define
dl-sr-step
(fn
()
(let
((pending (list)) (new-delta {}))
(do
(for-each
(fn
(rule)
(for-each
(fn (cand) (append! pending cand))
(dl-collect-rule-candidates rule db delta)))
rules)
(dl-commit-candidates! db pending new-delta)
(cond
((dl-delta-empty? new-delta) nil)
(else (do (set! delta new-delta) (dl-sr-step))))))))
(dl-sr-step)
db))))
;; Stratified driver: rejects non-stratifiable programs at saturation
;; time, then iterates strata in increasing order, running semi-naive on
;; the rules whose head sits in that stratum.
(define
dl-saturate!
(fn
(db)
(let
((err (dl-check-stratifiable db)))
(cond
((not (nil? err)) (error (str "dl-saturate!: " err)))
(else
(let
((strata (dl-compute-strata db)))
(let
((grouped (dl-group-rules-by-stratum db strata)))
(let
((groups (get grouped :groups))
(max-s (get grouped :max)))
(do
(define
dl-strat-loop
(fn
(s)
(when
(<= s max-s)
(let
((sk (str s)))
(do
(when
(has-key? groups sk)
(dl-saturate-rules! db (get groups sk)))
(dl-strat-loop (+ s 1)))))))
(dl-strat-loop 0)
db)))))))))
;; ── Querying ─────────────────────────────────────────────────────
;; Coerce a query argument to a list of body literals. A single literal
;; like `(p X)` (positive — head is a symbol) or `{:neg ...}` becomes
;; `((p X))`. A list of literals like `((p X) (q X))` is returned as-is.
(define
dl-query-coerce
(fn
(goal)
(cond
((and (dict? goal) (has-key? goal :neg)) (list goal))
((and (list? goal) (> (len goal) 0) (symbol? (first goal)))
(list goal))
((list? goal) goal)
(else (error (str "dl-query: unrecognised goal shape: " goal))))))
(define
dl-query
(fn
(db goal)
(do
(dl-saturate! db)
;; Rename anonymous '_' vars in each goal literal so multiple
;; occurrences do not unify together. Keep the user-facing var
;; list (taken before renaming) so projected results retain user
;; names.
(let
((goals (dl-query-coerce goal))
;; Start the renamer past any `_anon<N>` symbols the user
;; may have written in the query — avoids collision.
(renamer
(dl-make-anon-renamer (dl-max-anon-num-list goal 0 0))))
(let
((user-vars (dl-query-user-vars goals))
(renamed (map (fn (g) (dl-rename-anon-lit g renamer)) goals)))
(let
((substs (dl-find-bindings renamed db (dl-empty-subst)))
(results (list)))
(do
(for-each
(fn
(s)
(let
((proj (dl-project-subst s user-vars)))
(when
(not (dl-tuple-member? proj results))
(append! results proj))))
substs)
results)))))))
(define
dl-query-user-vars
(fn
(goals)
(let ((seen (list)))
(do
(for-each
(fn
(g)
(cond
((and (dict? g) (has-key? g :neg))
(for-each
(fn
(v)
(when
(and (not (= v "_")) (not (dl-member-string? v seen)))
(append! seen v)))
(dl-vars-of (get g :neg))))
((dl-aggregate? g)
;; Only the result var (first arg of the aggregate
;; literal) is user-facing. The aggregated var and
;; any vars in the inner goal are internal.
(let ((r (nth g 1)))
(when
(dl-var? r)
(let ((rn (symbol->string r)))
(when
(and (not (= rn "_"))
(not (dl-member-string? rn seen)))
(append! seen rn))))))
(else
(for-each
(fn
(v)
(when
(and (not (= v "_")) (not (dl-member-string? v seen)))
(append! seen v)))
(dl-vars-of g)))))
goals)
seen))))
(define
dl-project-subst
(fn
(subst names)
(let
((out {}))
(do
(for-each
(fn
(n)
(let
((sym (string->symbol n)))
(let
((v (dl-walk sym subst)))
(dict-set! out n (dl-apply-subst v subst)))))
names)
out))))
(define dl-relation (fn (db name) (dl-rel-tuples db name)))

View File

@@ -1,464 +0,0 @@
;; lib/datalog/magic.sx — adornment analysis + sideways info passing.
;;
;; First step of the magic-sets transformation (Phase 6). Right now
;; the saturator does not consume these — they are introspection
;; helpers that future magic-set rewriting will build on top of.
;;
;; Definitions:
;; - An *adornment* of an n-ary literal is an n-character string
;; of "b" (bound — value already known at the call site) and
;; "f" (free — to be derived).
;; - SIPS (Sideways Information Passing Strategy) walks the body
;; of an adorned rule left-to-right tracking which variables
;; have been bound so far, computing each body literal's
;; adornment in turn.
;;
;; Usage:
;;
;; (dl-adorn-goal '(ancestor tom X))
;; => "bf"
;;
;; (dl-rule-sips
;; {:head (ancestor X Z)
;; :body ((parent X Y) (ancestor Y Z))}
;; "bf")
;; => ({:lit (parent X Y) :adornment "bf"}
;; {:lit (ancestor Y Z) :adornment "bf"})
;; Per-arg adornment under the current bound-var name set.
(define
dl-adorn-arg
(fn
(arg bound)
(cond
((dl-var? arg)
(if (dl-member-string? (symbol->string arg) bound) "b" "f"))
(else "b"))))
;; Adornment for the args of a literal (after the relation name).
(define
dl-adorn-args
(fn
(args bound)
(cond
((= (len args) 0) "")
(else
(str
(dl-adorn-arg (first args) bound)
(dl-adorn-args (rest args) bound))))))
;; Adornment of a top-level goal under the empty bound-var set.
(define
dl-adorn-goal
(fn (goal) (dl-adorn-args (rest goal) (list))))
;; Adornment of a literal under an explicit bound set.
(define
dl-adorn-lit
(fn (lit bound) (dl-adorn-args (rest lit) bound)))
;; The set of variable names made bound by walking a positive
;; literal whose adornment is known. Free positions add their
;; vars to the bound set.
(define
dl-vars-bound-by-lit
(fn
(lit bound)
(let ((args (rest lit)) (out (list)))
(do
(for-each
(fn (a)
(when
(and (dl-var? a)
(not (dl-member-string? (symbol->string a) bound))
(not (dl-member-string? (symbol->string a) out)))
(append! out (symbol->string a))))
args)
out))))
;; Walk the rule body left-to-right tracking bound vars seeded by the
;; head adornment. Returns a list of {:lit :adornment} entries.
;;
;; Negation, comparison, and built-ins are passed through with their
;; adornment computed from the current bound set; they don't add new
;; bindings (except `is`, which binds its left arg if a var). Aggregates
;; are treated like is — the result var becomes bound.
(define
dl-init-head-bound
(fn
(head adornment)
(let ((args (rest head)) (out (list)))
(do
(define
dl-ihb-loop
(fn
(i)
(when
(< i (len args))
(do
(let
((c (slice adornment i (+ i 1)))
(a (nth args i)))
(when
(and (= c "b") (dl-var? a))
(let ((n (symbol->string a)))
(when
(not (dl-member-string? n out))
(append! out n)))))
(dl-ihb-loop (+ i 1))))))
(dl-ihb-loop 0)
out))))
(define
dl-rule-sips
(fn
(rule head-adornment)
(let
((bound (dl-init-head-bound (get rule :head) head-adornment))
(out (list)))
(do
(for-each
(fn
(lit)
(cond
((and (dict? lit) (has-key? lit :neg))
(let ((target (get lit :neg)))
(append!
out
{:lit lit :adornment (dl-adorn-lit target bound)})))
((dl-builtin? lit)
(let ((adn (dl-adorn-lit lit bound)))
(do
(append! out {:lit lit :adornment adn})
;; `is` binds its left arg (if var) once RHS is ground.
(when
(and (= (dl-rel-name lit) "is") (dl-var? (nth lit 1)))
(let ((n (symbol->string (nth lit 1))))
(when
(not (dl-member-string? n bound))
(append! bound n)))))))
((and (list? lit) (dl-aggregate? lit))
(let ((adn (dl-adorn-lit lit bound)))
(do
(append! out {:lit lit :adornment adn})
;; Result var (first arg) becomes bound.
(when (dl-var? (nth lit 1))
(let ((n (symbol->string (nth lit 1))))
(when
(not (dl-member-string? n bound))
(append! bound n)))))))
((and (list? lit) (> (len lit) 0))
(let ((adn (dl-adorn-lit lit bound)))
(do
(append! out {:lit lit :adornment adn})
(for-each
(fn (n)
(when (not (dl-member-string? n bound))
(append! bound n)))
(dl-vars-bound-by-lit lit bound)))))))
(get rule :body))
out))))
;; ── Magic predicate naming + bound-args extraction ─────────────
;; These are building blocks for the magic-sets *transformation*
;; itself. The transformation (which generates rewritten rules
;; with magic_<rel>^<adornment> filters) is future work — for now
;; these helpers can be used to inspect what such a transformation
;; would produce.
;; "magic_p^bf" given relation "p" and adornment "bf".
(define
dl-magic-rel-name
(fn (rel adornment) (str "magic_" rel "^" adornment)))
;; A magic predicate literal:
;; (magic_<rel>^<adornment> arg1 arg2 ...)
(define
dl-magic-lit
(fn
(rel adornment bound-args)
(cons (string->symbol (dl-magic-rel-name rel adornment)) bound-args)))
;; Extract bound args (those at "b" positions in `adornment`) from a
;; literal `(rel arg1 arg2 ... argN)`. Returns the list of arg values.
(define
dl-bound-args
(fn
(lit adornment)
(let ((args (rest lit)) (out (list)))
(do
(define
dl-ba-loop
(fn
(i)
(when
(< i (len args))
(do
(when
(= (slice adornment i (+ i 1)) "b")
(append! out (nth args i)))
(dl-ba-loop (+ i 1))))))
(dl-ba-loop 0)
out))))
;; ── Magic-sets rewriter ─────────────────────────────────────────
;;
;; Given the original rule list and a query (rel, adornment) pair,
;; generates the magic-rewritten program: a list of rules that
;; (a) gate each original rule with a `magic_<rel>^<adn>` filter and
;; (b) propagate the magic relation through SIPS so that only
;; query-relevant tuples are derived. Seed facts are returned
;; separately and must be added to the db at evaluation time.
;;
;; Output: {:rules <rewritten-rules> :seed <magic-seed-literal>}
;;
;; The rewriter only rewrites IDB rules; EDB facts pass through.
;; Built-in predicates and negation in body literals are kept in
;; place but do not generate propagation rules of their own.
(define
dl-magic-pair-key
(fn (rel adornment) (str rel "^" adornment)))
(define
dl-magic-rewrite
(fn
(rules query-rel query-adornment query-args)
(let
((seen (list))
(queue (list))
(out (list)))
(do
(define
dl-mq-mark!
(fn
(rel adornment)
(let ((k (dl-magic-pair-key rel adornment)))
(when
(not (dl-member-string? k seen))
(do
(append! seen k)
(append! queue {:rel rel :adn adornment}))))))
(define
dl-mq-rewrite-rule!
(fn
(rule adn)
(let
((head (get rule :head))
(body (get rule :body))
(sips (dl-rule-sips rule adn)))
(let
((magic-filter
(dl-magic-lit
(dl-rel-name head)
adn
(dl-bound-args head adn))))
(do
;; Adorned rule: head :- magic-filter, body...
(let ((new-body (list)))
(do
(append! new-body magic-filter)
(for-each
(fn (lit) (append! new-body lit))
body)
(append! out {:head head :body new-body})))
;; Propagation rules for each positive non-builtin
;; body literal at position i.
(define
dl-mq-prop-loop
(fn
(i)
(when
(< i (len body))
(do
(let
((lit (nth body i))
(sip-entry (nth sips i)))
(when
(and (list? lit)
(> (len lit) 0)
(not (and (dict? lit) (has-key? lit :neg)))
(not (dl-builtin? lit))
(not (dl-aggregate? lit)))
(let
((lit-adn (get sip-entry :adornment))
(lit-rel (dl-rel-name lit)))
(let
((prop-head
(dl-magic-lit
lit-rel
lit-adn
(dl-bound-args lit lit-adn))))
(let ((prop-body (list)))
(do
(append! prop-body magic-filter)
(define
dl-mq-prefix-loop
(fn
(j)
(when
(< j i)
(do
(append!
prop-body
(nth body j))
(dl-mq-prefix-loop (+ j 1))))))
(dl-mq-prefix-loop 0)
(append!
out
{:head prop-head :body prop-body})
(dl-mq-mark! lit-rel lit-adn)))))))
(dl-mq-prop-loop (+ i 1))))))
(dl-mq-prop-loop 0))))))
(dl-mq-mark! query-rel query-adornment)
(let ((idx 0))
(define
dl-mq-process
(fn
()
(when
(< idx (len queue))
(let ((item (nth queue idx)))
(do
(set! idx (+ idx 1))
(let
((rel (get item :rel)) (adn (get item :adn)))
(for-each
(fn
(rule)
(when
(= (dl-rel-name (get rule :head)) rel)
(dl-mq-rewrite-rule! rule adn)))
rules))
(dl-mq-process))))))
(dl-mq-process))
{:rules out
:seed
(dl-magic-lit
query-rel
query-adornment
query-args)}))))
;; ── Top-level magic-sets driver ─────────────────────────────────
;;
;; (dl-magic-query db query-goal) — run `query-goal` under magic-sets
;; evaluation. Builds a fresh internal db with:
;; - the caller's EDB facts (relations not headed by any rule),
;; - the magic seed fact, and
;; - the rewritten rules.
;; Saturates and queries, returning the substitution list. The
;; caller's db is untouched.
;;
;; Useful primarily as a perf alternative for queries that only
;; need a small slice of a recursive relation. Equivalent to
;; dl-query for any single fully-stratifiable program.
(define
dl-magic-rule-heads
(fn
(rules)
(let ((seen (list)))
(do
(for-each
(fn
(r)
(let ((h (dl-rel-name (get r :head))))
(when
(and (not (nil? h)) (not (dl-member-string? h seen)))
(append! seen h))))
rules)
seen))))
;; True iff any rule's body contains a literal kind that the magic
;; rewriter doesn't propagate magic to — i.e. an aggregate or a
;; negation. Used by dl-magic-query to decide whether to pre-saturate
;; the source db (for correctness on stratified programs) or skip
;; that step (preserving full magic-sets efficiency for pure
;; positive programs).
(define
dl-rule-has-nonprop-lit?
(fn
(body i n)
(cond
((>= i n) false)
((let ((lit (nth body i)))
(or (and (dict? lit) (has-key? lit :neg))
(dl-aggregate? lit)))
true)
(else (dl-rule-has-nonprop-lit? body (+ i 1) n)))))
(define
dl-rules-need-presaturation?
(fn
(rules)
(cond
((= (len rules) 0) false)
((let ((body (get (first rules) :body)))
(dl-rule-has-nonprop-lit? body 0 (len body)))
true)
(else (dl-rules-need-presaturation? (rest rules))))))
(define
dl-magic-query
(fn
(db query-goal)
;; Magic-sets only applies to positive non-builtin / non-aggregate
;; literals against rule-defined relations. For other goal shapes
;; (built-ins, aggregates, EDB-only relations) the seed is either
;; non-ground or unused; fall back to dl-query.
(cond
((not (and (list? query-goal)
(> (len query-goal) 0)
(symbol? (first query-goal))))
(error (str "dl-magic-query: goal must be a positive literal "
"(non-empty list with a symbol head), got " query-goal)))
((or (dl-builtin? query-goal)
(dl-aggregate? query-goal)
(and (dict? query-goal) (has-key? query-goal :neg)))
(dl-query db query-goal))
(else
(do
;; If the rule set has aggregates or negation, pre-saturate
;; the source db before copying facts. The magic rewriter
;; passes aggregate body lits and negated lits through
;; unchanged (no magic propagation generated for them) — so
;; if their inner-goal relation is IDB, it would be empty in
;; the magic db. Pre-saturating ensures equivalence with
;; `dl-query` for every stratified program. Pure positive
;; programs skip this and keep the full magic-sets perf win
;; from goal-directed re-derivation.
(when
(dl-rules-need-presaturation? (dl-rules db))
(dl-saturate! db))
(let
((query-rel (dl-rel-name query-goal))
(query-adn (dl-adorn-goal query-goal)))
(let
((query-args (dl-bound-args query-goal query-adn))
(rules (dl-rules db)))
(let
((rewritten (dl-magic-rewrite rules query-rel query-adn query-args))
(mdb (dl-make-db))
(rule-heads (dl-magic-rule-heads rules)))
(do
;; Copy ALL existing facts. EDB-only relations bring their
;; tuples; mixed EDB+IDB relations bring both their EDB
;; portion and any pre-saturated IDB tuples (which the
;; rewritten rules would re-derive anyway). Skipping facts
;; for rule-headed relations would leave the magic run
;; without the EDB portion of mixed relations.
(for-each
(fn
(rel)
(for-each
(fn (t) (dl-add-fact! mdb t))
(dl-rel-tuples db rel)))
(keys (get db :facts)))
;; Seed + rewritten rules.
(dl-add-fact! mdb (get rewritten :seed))
(for-each (fn (r) (dl-add-rule! mdb r)) (get rewritten :rules))
(dl-query mdb query-goal))))))))))

View File

@@ -1,252 +0,0 @@
;; lib/datalog/parser.sx — Datalog tokens → AST
;;
;; Output shapes:
;; Literal (positive) := (relname arg ... arg) — SX list
;; Literal (negative) := {:neg (relname arg ... arg)} — dict
;; Argument := var-symbol | atom-symbol | number | string
;; | (op-name arg ... arg) — arithmetic compound
;; Fact := {:head literal :body ()}
;; Rule := {:head literal :body (lit ... lit)}
;; Query := {:query (lit ... lit)}
;; Program := list of facts / rules / queries
;;
;; Variables and constants are both SX symbols; the evaluator dispatches
;; on first-char case ('A'..'Z' or '_' = variable, otherwise constant).
;;
;; The parser permits nested compounds in arg position to support
;; arithmetic (e.g. (is Z (+ X Y))). Safety analysis at rule-load time
;; rejects compounds whose head is not an arithmetic operator.
(define
dl-pp-peek
(fn
(st)
(let
((i (get st :idx)) (tokens (get st :tokens)))
(if (< i (len tokens)) (nth tokens i) {:type "eof" :value nil :pos 0}))))
(define
dl-pp-peek2
(fn
(st)
(let
((i (+ (get st :idx) 1)) (tokens (get st :tokens)))
(if (< i (len tokens)) (nth tokens i) {:type "eof" :value nil :pos 0}))))
(define
dl-pp-advance!
(fn (st) (dict-set! st :idx (+ (get st :idx) 1))))
(define
dl-pp-at?
(fn
(st type value)
(let
((t (dl-pp-peek st)))
(and
(= (get t :type) type)
(or (= value nil) (= (get t :value) value))))))
(define
dl-pp-error
(fn
(st msg)
(let
((t (dl-pp-peek st)))
(error
(str
"Parse error at pos "
(get t :pos)
": "
msg
" (got "
(get t :type)
" '"
(if (= (get t :value) nil) "" (get t :value))
"')")))))
(define
dl-pp-expect!
(fn
(st type value)
(let
((t (dl-pp-peek st)))
(if
(dl-pp-at? st type value)
(do (dl-pp-advance! st) t)
(dl-pp-error
st
(str "expected " type (if (= value nil) "" (str " '" value "'"))))))))
;; Argument: variable, atom, number, string, or compound (relname/op + parens).
(define
dl-pp-parse-arg
(fn
(st)
(let
((t (dl-pp-peek st)))
(let
((ty (get t :type)) (vv (get t :value)))
(cond
((= ty "number") (do (dl-pp-advance! st) vv))
((= ty "string") (do (dl-pp-advance! st) vv))
((= ty "var") (do (dl-pp-advance! st) (string->symbol vv)))
;; Negative numeric literal: `-` op directly followed by a
;; number (no `(`) is parsed as a single negative number.
;; This keeps `(-X Y)` (compound) and `-N` (literal) distinct.
((and (= ty "op") (= vv "-")
(= (get (dl-pp-peek2 st) :type) "number"))
(do
(dl-pp-advance! st)
(let
((n (get (dl-pp-peek st) :value)))
(do (dl-pp-advance! st) (- 0 n)))))
((or (= ty "atom") (= ty "op"))
(do
(dl-pp-advance! st)
(if
(dl-pp-at? st "punct" "(")
(do
(dl-pp-advance! st)
(let
((args (dl-pp-parse-arg-list st)))
(do
(dl-pp-expect! st "punct" ")")
(cons (string->symbol vv) args))))
(string->symbol vv))))
(else (dl-pp-error st "expected term")))))))
;; Comma-separated args inside parens.
(define
dl-pp-parse-arg-list
(fn
(st)
(let
((args (list)))
(do
(append! args (dl-pp-parse-arg st))
(define
dl-pp-arg-loop
(fn
()
(when
(dl-pp-at? st "punct" ",")
(do
(dl-pp-advance! st)
(append! args (dl-pp-parse-arg st))
(dl-pp-arg-loop)))))
(dl-pp-arg-loop)
args))))
;; A positive literal: relname (atom or op) followed by optional (args).
(define
dl-pp-parse-positive
(fn
(st)
(let
((t (dl-pp-peek st)))
(let
((ty (get t :type)) (vv (get t :value)))
(if
(or (= ty "atom") (= ty "op"))
(do
(dl-pp-advance! st)
(if
(dl-pp-at? st "punct" "(")
(do
(dl-pp-advance! st)
(let
((args (dl-pp-parse-arg-list st)))
(do
(dl-pp-expect! st "punct" ")")
(cons (string->symbol vv) args))))
(list (string->symbol vv))))
(dl-pp-error st "expected literal head"))))))
;; A body literal: positive, or not(positive).
(define
dl-pp-parse-body-lit
(fn
(st)
(let
((t1 (dl-pp-peek st)) (t2 (dl-pp-peek2 st)))
(if
(and
(= (get t1 :type) "atom")
(= (get t1 :value) "not")
(= (get t2 :type) "punct")
(= (get t2 :value) "("))
(do
(dl-pp-advance! st)
(dl-pp-advance! st)
(let
((inner (dl-pp-parse-positive st)))
(do (dl-pp-expect! st "punct" ")") {:neg inner})))
(dl-pp-parse-positive st)))))
;; Comma-separated body literals.
(define
dl-pp-parse-body
(fn
(st)
(let
((lits (list)))
(do
(append! lits (dl-pp-parse-body-lit st))
(define
dl-pp-body-loop
(fn
()
(when
(dl-pp-at? st "punct" ",")
(do
(dl-pp-advance! st)
(append! lits (dl-pp-parse-body-lit st))
(dl-pp-body-loop)))))
(dl-pp-body-loop)
lits))))
;; Single clause: fact, rule, or query. Consumes trailing dot.
(define
dl-pp-parse-clause
(fn
(st)
(cond
((dl-pp-at? st "op" "?-")
(do
(dl-pp-advance! st)
(let
((body (dl-pp-parse-body st)))
(do (dl-pp-expect! st "punct" ".") {:query body}))))
(else
(let
((head (dl-pp-parse-positive st)))
(cond
((dl-pp-at? st "op" ":-")
(do
(dl-pp-advance! st)
(let
((body (dl-pp-parse-body st)))
(do (dl-pp-expect! st "punct" ".") {:body body :head head}))))
(else (do (dl-pp-expect! st "punct" ".") {:body (list) :head head}))))))))
(define
dl-parse-program
(fn
(tokens)
(let
((st {:tokens tokens :idx 0}) (clauses (list)))
(do
(define
dl-pp-prog-loop
(fn
()
(when
(not (dl-pp-at? st "eof" nil))
(do
(append! clauses (dl-pp-parse-clause st))
(dl-pp-prog-loop)))))
(dl-pp-prog-loop)
clauses))))
(define dl-parse (fn (src) (dl-parse-program (dl-tokenize src))))

View File

@@ -1,20 +0,0 @@
{
"lang": "datalog",
"total_passed": 276,
"total_failed": 0,
"total": 276,
"suites": [
{"name":"tokenize","passed":31,"failed":0,"total":31},
{"name":"parse","passed":23,"failed":0,"total":23},
{"name":"unify","passed":29,"failed":0,"total":29},
{"name":"eval","passed":44,"failed":0,"total":44},
{"name":"builtins","passed":26,"failed":0,"total":26},
{"name":"semi_naive","passed":8,"failed":0,"total":8},
{"name":"negation","passed":12,"failed":0,"total":12},
{"name":"aggregates","passed":23,"failed":0,"total":23},
{"name":"api","passed":22,"failed":0,"total":22},
{"name":"magic","passed":37,"failed":0,"total":37},
{"name":"demo","passed":21,"failed":0,"total":21}
],
"generated": "2026-05-11T09:40:12+00:00"
}

View File

@@ -1,17 +0,0 @@
# datalog scoreboard
**276 / 276 passing** (0 failure(s)).
| Suite | Passed | Total | Status |
|-------|--------|-------|--------|
| tokenize | 31 | 31 | ok |
| parse | 23 | 23 | ok |
| unify | 29 | 29 | ok |
| eval | 44 | 44 | ok |
| builtins | 26 | 26 | ok |
| semi_naive | 8 | 8 | ok |
| negation | 12 | 12 | ok |
| aggregates | 23 | 23 | ok |
| api | 22 | 22 | ok |
| magic | 37 | 37 | ok |
| demo | 21 | 21 | ok |

View File

@@ -1,323 +0,0 @@
;; lib/datalog/strata.sx — dependency graph, SCC analysis, stratum assignment.
;;
;; A program is stratifiable iff no cycle in its dependency graph passes
;; through a negative edge. The stratum of relation R is the depth at which
;; R can first be evaluated:
;;
;; stratum(R) = max over edges (R → S) of:
;; stratum(S) if the edge is positive
;; stratum(S) + 1 if the edge is negative
;;
;; All relations in the same SCC share a stratum (and the SCC must have only
;; positive internal edges, else the program is non-stratifiable).
;; Build dep graph: dict {head-rel-name -> ({:rel str :neg bool} ...)}.
(define
dl-build-dep-graph
(fn
(db)
(let ((g {}))
(do
(for-each
(fn
(rule)
(let
((head-rel (dl-rel-name (get rule :head))))
(when
(not (nil? head-rel))
(do
(when
(not (has-key? g head-rel))
(dict-set! g head-rel (list)))
(let ((existing (get g head-rel)))
(for-each
(fn
(lit)
(cond
((dl-aggregate? lit)
(let
((edge (dl-aggregate-dep-edge lit)))
(when
(not (nil? edge))
(append! existing edge))))
(else
(let
((target
(cond
((and (dict? lit) (has-key? lit :neg))
(dl-rel-name (get lit :neg)))
((dl-builtin? lit) nil)
((and (list? lit) (> (len lit) 0))
(dl-rel-name lit))
(else nil)))
(neg?
(and (dict? lit) (has-key? lit :neg))))
(when
(not (nil? target))
(append!
existing
{:rel target :neg neg?}))))))
(get rule :body)))))))
(dl-rules db))
g))))
;; All relations referenced — heads of rules + EDB names + body relations.
(define
dl-all-relations
(fn
(db)
(let ((seen (list)))
(do
(for-each
(fn
(k)
(when (not (dl-member-string? k seen)) (append! seen k)))
(keys (get db :facts)))
(for-each
(fn
(rule)
(do
(let ((h (dl-rel-name (get rule :head))))
(when
(and (not (nil? h)) (not (dl-member-string? h seen)))
(append! seen h)))
(for-each
(fn
(lit)
(let
((t
(cond
((dl-aggregate? lit)
(let ((edge (dl-aggregate-dep-edge lit)))
(if (nil? edge) nil (get edge :rel))))
((and (dict? lit) (has-key? lit :neg))
(dl-rel-name (get lit :neg)))
((dl-builtin? lit) nil)
((and (list? lit) (> (len lit) 0))
(dl-rel-name lit))
(else nil))))
(when
(and (not (nil? t)) (not (dl-member-string? t seen)))
(append! seen t))))
(get rule :body))))
(dl-rules db))
seen))))
;; reach: dict {from: dict {to: edge-info}} where edge-info is
;; {:any bool :neg bool}
;; meaning "any path from `from` to `to`" and "exists a negative-passing
;; path from `from` to `to`".
;;
;; Floyd-Warshall over the dep graph. The 'neg' flag propagates through
;; concatenation: if any edge along the path is negative, the path's
;; flag is true.
(define
dl-build-reach
(fn
(graph nodes)
(let ((reach {}))
(do
(for-each
(fn (n) (dict-set! reach n {}))
nodes)
(for-each
(fn
(head)
(when
(has-key? graph head)
(for-each
(fn
(edge)
(let
((target (get edge :rel)) (n (get edge :neg)))
(let ((row (get reach head)))
(cond
((has-key? row target)
(let ((cur (get row target)))
(dict-set!
row
target
{:any true :neg (or n (get cur :neg))})))
(else
(dict-set! row target {:any true :neg n}))))))
(get graph head))))
nodes)
(for-each
(fn
(k)
(for-each
(fn
(i)
(let ((row-i (get reach i)))
(when
(has-key? row-i k)
(let ((ik (get row-i k)) (row-k (get reach k)))
(for-each
(fn
(j)
(when
(has-key? row-k j)
(let ((kj (get row-k j)))
(let
((combined-neg (or (get ik :neg) (get kj :neg))))
(cond
((has-key? row-i j)
(let ((cur (get row-i j)))
(dict-set!
row-i
j
{:any true
:neg (or combined-neg (get cur :neg))})))
(else
(dict-set!
row-i
j
{:any true :neg combined-neg})))))))
nodes)))))
nodes))
nodes)
reach))))
;; Returns nil on success, or error message string on failure.
(define
dl-check-stratifiable
(fn
(db)
(let
((graph (dl-build-dep-graph db))
(nodes (dl-all-relations db)))
(let ((reach (dl-build-reach graph nodes)) (err nil))
(do
(for-each
(fn
(rule)
(when
(nil? err)
(let ((head-rel (dl-rel-name (get rule :head))))
(for-each
(fn
(lit)
(cond
((and (dict? lit) (has-key? lit :neg))
(let ((tgt (dl-rel-name (get lit :neg))))
(when
(and (not (nil? tgt))
(dl-reach-cycle? reach head-rel tgt))
(set!
err
(str "non-stratifiable: relation " head-rel
" transitively depends through negation on "
tgt
" which depends back on " head-rel)))))
((dl-aggregate? lit)
(let ((edge (dl-aggregate-dep-edge lit)))
(when
(not (nil? edge))
(let ((tgt (get edge :rel)))
(when
(and (not (nil? tgt))
(dl-reach-cycle? reach head-rel tgt))
(set!
err
(str "non-stratifiable: relation "
head-rel
" aggregates over " tgt
" which depends back on "
head-rel)))))))))
(get rule :body)))))
(dl-rules db))
err)))))
(define
dl-reach-cycle?
(fn
(reach a b)
(and
(dl-reach-row-has? reach b a)
(dl-reach-row-has? reach a b))))
(define
dl-reach-row-has?
(fn
(reach from to)
(let ((row (get reach from)))
(and (not (nil? row)) (has-key? row to)))))
;; Compute stratum per relation. Iteratively propagate from EDB roots.
;; Uses the per-relation max-stratum-of-deps formula. Stops when stable.
(define
dl-compute-strata
(fn
(db)
(let
((graph (dl-build-dep-graph db))
(nodes (dl-all-relations db))
(strata {}))
(do
(for-each (fn (n) (dict-set! strata n 0)) nodes)
(let ((changed true))
(do
(define
dl-cs-loop
(fn
()
(when
changed
(do
(set! changed false)
(for-each
(fn
(head)
(when
(has-key? graph head)
(for-each
(fn
(edge)
(let
((tgt (get edge :rel))
(n (get edge :neg)))
(let
((tgt-strat
(if (has-key? strata tgt)
(get strata tgt) 0))
(cur (get strata head)))
(let
((needed
(if n (+ tgt-strat 1) tgt-strat)))
(when
(> needed cur)
(do
(dict-set! strata head needed)
(set! changed true)))))))
(get graph head))))
nodes)
(dl-cs-loop)))))
(dl-cs-loop)))
strata))))
;; Group rules by their head's stratum. Returns dict {stratum-int -> rules}.
(define
dl-group-rules-by-stratum
(fn
(db strata)
(let ((groups {}) (max-s 0))
(do
(for-each
(fn
(rule)
(let
((head-rel (dl-rel-name (get rule :head))))
(let
((s (if (has-key? strata head-rel)
(get strata head-rel) 0)))
(do
(when (> s max-s) (set! max-s s))
(let
((sk (str s)))
(do
(when
(not (has-key? groups sk))
(dict-set! groups sk (list)))
(append! (get groups sk) rule)))))))
(dl-rules db))
{:groups groups :max max-s}))))

View File

@@ -1,357 +0,0 @@
;; lib/datalog/tests/aggregates.sx — count / sum / min / max.
(define dl-at-pass 0)
(define dl-at-fail 0)
(define dl-at-failures (list))
(define
dl-at-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-at-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let ((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-at-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-at-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-at-deep=? (nth a i) (nth b i))) false)
(else (dl-at-deq-l? a b (+ i 1))))))
(define
dl-at-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i)))
(not (dl-at-deep=? (get a k) (get b k))))
false)
(else (dl-at-deq-d? a b ka (+ i 1))))))
(define
dl-at-set=?
(fn
(a b)
(and
(= (len a) (len b))
(dl-at-subset? a b)
(dl-at-subset? b a))))
(define
dl-at-subset?
(fn
(xs ys)
(cond
((= (len xs) 0) true)
((not (dl-at-contains? ys (first xs))) false)
(else (dl-at-subset? (rest xs) ys)))))
(define
dl-at-contains?
(fn
(xs target)
(cond
((= (len xs) 0) false)
((dl-at-deep=? (first xs) target) true)
(else (dl-at-contains? (rest xs) target)))))
(define
dl-at-test!
(fn
(name got expected)
(if
(dl-at-deep=? got expected)
(set! dl-at-pass (+ dl-at-pass 1))
(do
(set! dl-at-fail (+ dl-at-fail 1))
(append!
dl-at-failures
(str
name
"\n expected: " expected
"\n got: " got))))))
(define
dl-at-test-set!
(fn
(name got expected)
(if
(dl-at-set=? got expected)
(set! dl-at-pass (+ dl-at-pass 1))
(do
(set! dl-at-fail (+ dl-at-fail 1))
(append!
dl-at-failures
(str
name
"\n expected (set): " expected
"\n got: " got))))))
(define
dl-at-throws?
(fn
(thunk)
(let
((threw false))
(do
(guard
(e (#t (set! threw true)))
(thunk))
threw))))
(define
dl-at-run-all!
(fn
()
(do
;; count
(dl-at-test-set! "count siblings"
(dl-query
(dl-program
"parent(p, bob). parent(p, alice). parent(p, charlie).
sibling(X, Y) :- parent(P, X), parent(P, Y), !=(X, Y).
sib_count(N) :- count(N, S, sibling(bob, S)).")
(list (quote sib_count) (quote N)))
(list {:N 2}))
;; sum
(dl-at-test-set! "sum prices"
(dl-query
(dl-program
"price(apple, 5). price(pear, 7). price(plum, 3).
total(T) :- sum(T, X, price(F, X)).")
(list (quote total) (quote T)))
(list {:T 15}))
;; min
(dl-at-test-set! "min score"
(dl-query
(dl-program
"score(alice, 80). score(bob, 65). score(carol, 92).
lo(M) :- min(M, S, score(P, S)).")
(list (quote lo) (quote M)))
(list {:M 65}))
;; max
(dl-at-test-set! "max score"
(dl-query
(dl-program
"score(alice, 80). score(bob, 65). score(carol, 92).
hi(M) :- max(M, S, score(P, S)).")
(list (quote hi) (quote M)))
(list {:M 92}))
;; count over derived relation (stratification needed).
(dl-at-test-set! "count over derived"
(dl-query
(dl-program
"parent(a, b). parent(a, c). parent(b, d). parent(c, e).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).
num_ancestors(N) :- count(N, X, ancestor(a, X)).")
(list (quote num_ancestors) (quote N)))
(list {:N 4}))
;; count with no matches → 0.
(dl-at-test-set! "count empty"
(dl-query
(dl-program
"p(1). p(2).
zero(N) :- count(N, X, q(X)).")
(list (quote zero) (quote N)))
(list {:N 0}))
;; sum with no matches → 0.
(dl-at-test-set! "sum empty"
(dl-query
(dl-program
"p(1). p(2).
total(T) :- sum(T, X, q(X)).")
(list (quote total) (quote T)))
(list {:T 0}))
;; min with no matches → rule does not fire.
(dl-at-test-set! "min empty"
(dl-query
(dl-program
"p(1). p(2).
lo(M) :- min(M, X, q(X)).")
(list (quote lo) (quote M)))
(list))
;; Aggregate with comparison filter on result.
(dl-at-test-set! "popularity threshold"
(dl-query
(dl-program
"post(p1). post(p2).
liked(u1, p1). liked(u2, p1). liked(u3, p1).
liked(u1, p2). liked(u2, p2).
popular(P) :- post(P), count(N, U, liked(U, P)), >=(N, 3).")
(list (quote popular) (quote P)))
(list {:P (quote p1)}))
;; findall: collect distinct values into a list.
(dl-at-test-set! "findall over EDB"
(dl-query
(dl-program
"p(a). p(b). p(c).
all_p(L) :- findall(L, X, p(X)).")
(list (quote all_p) (quote L)))
(list {:L (list (quote a) (quote b) (quote c))}))
(dl-at-test-set! "findall over derived"
(dl-query
(dl-program
"parent(a, b). parent(b, c). parent(c, d).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).
desc(L) :- findall(L, X, ancestor(a, X)).")
(list (quote desc) (quote L)))
(list {:L (list (quote b) (quote c) (quote d))}))
(dl-at-test-set! "findall empty"
(dl-query
(dl-program
"p(1).
all_q(L) :- findall(L, X, q(X)).")
(list (quote all_q) (quote L)))
(list {:L (list)}))
;; Aggregate vs single distinct.
;; Group-by via aggregate-in-rule-body. Per-user friend count
;; over a friends relation. The U var is bound by the prior
;; positive lit u(U) so the aggregate counts only U-rooted
;; friends per group.
(dl-at-test-set! "group-by per-user friend count"
(dl-query
(dl-program
"u(alice). u(bob). u(carol).
f(alice, x). f(alice, y). f(bob, x).
counts(U, N) :- u(U), count(N, X, f(U, X)).")
(list (quote counts) (quote U) (quote N)))
(list
{:U (quote alice) :N 2}
{:U (quote bob) :N 1}
{:U (quote carol) :N 0}))
;; Stratification: recursion through aggregation is rejected.
;; Aggregate validates that second arg is a variable.
(dl-at-test! "agg second arg must be var"
(dl-at-throws?
(fn () (dl-eval "p(1). q(N) :- count(N, 5, p(X))." "?- q(N).")))
true)
;; Aggregate validates that third arg is a positive literal.
(dl-at-test! "agg third arg must be a literal"
(dl-at-throws?
(fn () (dl-eval "p(1). q(N) :- count(N, X, 42)." "?- q(N).")))
true)
;; Aggregate validates that the agg-var (2nd arg) appears in the
;; goal. Without it every match contributes the same unbound
;; symbol — count silently returns 1, sum raises a confusing
;; "expected number" error, etc. Catch the mistake at safety
;; check time instead.
(dl-at-test! "agg-var must appear in goal"
(dl-at-throws?
(fn ()
(dl-eval
"p(1). p(2). c(N) :- count(N, Y, p(X))."
"?- c(N).")))
true)
;; Indirect recursion through aggregation also rejected.
;; q -> r (via positive lit), r -> q (via aggregate body).
;; The aggregate edge counts as negation for stratification.
(dl-at-test! "indirect agg cycle rejected"
(dl-at-throws?
(fn ()
(let ((db (dl-make-db)))
(do
(dl-add-rule! db
{:head (list (quote q) (quote N))
:body (list (list (quote r) (quote N)))})
(dl-add-rule! db
{:head (list (quote r) (quote N))
:body (list (list (quote count) (quote N) (quote X)
(list (quote q) (quote X))))})
(dl-saturate! db)))))
true)
(dl-at-test! "agg recursion rejected"
(dl-at-throws?
(fn ()
(let ((db (dl-make-db)))
(do
(dl-add-rule! db
{:head (list (quote q) (quote N))
:body (list (list (quote count) (quote N) (quote X)
(list (quote q) (quote X))))})
(dl-saturate! db)))))
true)
;; Negation + aggregation in the same body — different strata.
(dl-at-test-set! "neg + agg coexist"
(dl-query
(dl-program
"u(a). u(b). u(c). banned(b).
active(X) :- u(X), not(banned(X)).
cnt(N) :- count(N, X, active(X)).")
(list (quote cnt) (quote N)))
(list {:N 2}))
;; Min over a derived empty relation: no result.
(dl-at-test-set! "min over empty derived"
(dl-query
(dl-program
"s(50). s(60).
score(N) :- s(N), >(N, 100).
low(M) :- min(M, X, score(X)).")
(list (quote low) (quote M)))
(list))
;; Aggregates as the top-level query goal (regression for
;; dl-match-lit aggregate dispatch and projection cleanup).
(dl-at-test-set! "count as query goal"
(dl-query
(dl-program "p(1). p(2). p(3). p(4).")
(list (quote count) (quote N) (quote X) (list (quote p) (quote X))))
(list {:N 4}))
(dl-at-test-set! "findall as query goal"
(dl-query
(dl-program "p(1). p(2). p(3).")
(list (quote findall) (quote L) (quote X)
(list (quote p) (quote X))))
(list {:L (list 1 2 3)}))
(dl-at-test-set! "distinct counted once"
(dl-query
(dl-program
"rated(alice, x). rated(alice, y). rated(bob, x).
rater_count(N) :- count(N, U, rated(U, F)).")
(list (quote rater_count) (quote N)))
(list {:N 2})))))
(define
dl-aggregates-tests-run!
(fn
()
(do
(set! dl-at-pass 0)
(set! dl-at-fail 0)
(set! dl-at-failures (list))
(dl-at-run-all!)
{:passed dl-at-pass
:failed dl-at-fail
:total (+ dl-at-pass dl-at-fail)
:failures dl-at-failures})))

View File

@@ -1,350 +0,0 @@
;; lib/datalog/tests/api.sx — SX-data embedding API.
(define dl-api-pass 0)
(define dl-api-fail 0)
(define dl-api-failures (list))
(define
dl-api-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-api-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let ((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-api-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-api-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-api-deep=? (nth a i) (nth b i))) false)
(else (dl-api-deq-l? a b (+ i 1))))))
(define
dl-api-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i)))
(not (dl-api-deep=? (get a k) (get b k))))
false)
(else (dl-api-deq-d? a b ka (+ i 1))))))
(define
dl-api-set=?
(fn
(a b)
(and
(= (len a) (len b))
(dl-api-subset? a b)
(dl-api-subset? b a))))
(define
dl-api-subset?
(fn
(xs ys)
(cond
((= (len xs) 0) true)
((not (dl-api-contains? ys (first xs))) false)
(else (dl-api-subset? (rest xs) ys)))))
(define
dl-api-contains?
(fn
(xs target)
(cond
((= (len xs) 0) false)
((dl-api-deep=? (first xs) target) true)
(else (dl-api-contains? (rest xs) target)))))
(define
dl-api-test!
(fn
(name got expected)
(if
(dl-api-deep=? got expected)
(set! dl-api-pass (+ dl-api-pass 1))
(do
(set! dl-api-fail (+ dl-api-fail 1))
(append!
dl-api-failures
(str
name
"\n expected: " expected
"\n got: " got))))))
(define
dl-api-test-set!
(fn
(name got expected)
(if
(dl-api-set=? got expected)
(set! dl-api-pass (+ dl-api-pass 1))
(do
(set! dl-api-fail (+ dl-api-fail 1))
(append!
dl-api-failures
(str
name
"\n expected (set): " expected
"\n got: " got))))))
(define
dl-api-run-all!
(fn
()
(do
;; dl-program-data with arrow form.
(dl-api-test-set! "data API ancestor closure"
(dl-query
(dl-program-data
(quote ((parent tom bob) (parent bob ann) (parent ann pat)))
(quote
((ancestor X Y <- (parent X Y))
(ancestor X Z <- (parent X Y) (ancestor Y Z)))))
(quote (ancestor tom X)))
(list {:X (quote bob)} {:X (quote ann)} {:X (quote pat)}))
;; dl-program-data with dict rules.
(dl-api-test-set! "data API with dict rules"
(dl-query
(dl-program-data
(quote ((p a) (p b) (p c)))
(list
{:head (quote (q X)) :body (quote ((p X)))}))
(quote (q X)))
(list {:X (quote a)} {:X (quote b)} {:X (quote c)}))
;; dl-rule helper.
(dl-api-test-set! "dl-rule constructor"
(dl-query
(dl-program-data
(quote ((p 1) (p 2)))
(list (dl-rule (quote (q X)) (quote ((p X))))))
(quote (q X)))
(list {:X 1} {:X 2}))
;; dl-assert! adds and re-derives.
(dl-api-test-set! "dl-assert! incremental"
(let
((db (dl-program-data
(quote ((parent tom bob) (parent bob ann)))
(quote
((ancestor X Y <- (parent X Y))
(ancestor X Z <- (parent X Y) (ancestor Y Z)))))))
(do
(dl-saturate! db)
(dl-assert! db (quote (parent ann pat)))
(dl-query db (quote (ancestor tom X)))))
(list {:X (quote bob)} {:X (quote ann)} {:X (quote pat)}))
;; dl-retract! removes a fact and recomputes IDB.
(dl-api-test-set! "dl-retract! removes derived"
(let
((db (dl-program-data
(quote ((parent tom bob) (parent bob ann) (parent ann pat)))
(quote
((ancestor X Y <- (parent X Y))
(ancestor X Z <- (parent X Y) (ancestor Y Z)))))))
(do
(dl-saturate! db)
(dl-retract! db (quote (parent bob ann)))
(dl-query db (quote (ancestor tom X)))))
(list {:X (quote bob)}))
;; dl-retract! on a relation with BOTH explicit facts AND a rule
;; (a "mixed" relation) used to wipe the EDB portion when the IDB
;; was re-derived, even when the retract didn't match anything.
;; :edb-keys provenance now preserves user-asserted facts.
(dl-api-test-set! "dl-retract! preserves EDB in mixed relation"
(let
((db (dl-program-data
(quote ((p a) (p b) (q c)))
(quote ((p X <- (q X)))))))
(do
(dl-saturate! db)
;; Retract a non-existent tuple — should be a no-op.
(dl-retract! db (quote (p z)))
(dl-query db (quote (p X)))))
(list {:X (quote a)} {:X (quote b)} {:X (quote c)}))
;; And retracting an actual EDB fact in a mixed relation drops
;; only that fact; the derived portion stays.
(dl-api-test-set! "dl-retract! mixed: drop EDB, keep IDB"
(let
((db (dl-program-data
(quote ((p a) (p b) (q c)))
(quote ((p X <- (q X)))))))
(do
(dl-saturate! db)
(dl-retract! db (quote (p a)))
(dl-query db (quote (p X)))))
(list {:X (quote b)} {:X (quote c)}))
;; dl-program-data + dl-query with constants in head.
(dl-api-test-set! "constant-in-head data"
(dl-query
(dl-program-data
(quote ((edge a b) (edge b c) (edge c a)))
(quote
((reach X Y <- (edge X Y))
(reach X Z <- (edge X Y) (reach Y Z)))))
(quote (reach a X)))
(list {:X (quote a)} {:X (quote b)} {:X (quote c)}))
;; Assert into empty db.
(dl-api-test-set! "assert into empty"
(let
((db (dl-program-data (list) (list))))
(do
(dl-assert! db (quote (p 1)))
(dl-assert! db (quote (p 2)))
(dl-query db (quote (p X)))))
(list {:X 1} {:X 2}))
;; Multi-goal query: pass list of literals.
(dl-api-test-set! "multi-goal query"
(dl-query
(dl-program-data
(quote ((p 1) (p 2) (p 3) (q 2) (q 3)))
(list))
(list (quote (p X)) (quote (q X))))
(list {:X 2} {:X 3}))
;; Multi-goal with comparison.
(dl-api-test-set! "multi-goal with comparison"
(dl-query
(dl-program-data
(quote ((n 1) (n 2) (n 3) (n 4) (n 5)))
(list))
(list (quote (n X)) (list (string->symbol ">") (quote X) 2)))
(list {:X 3} {:X 4} {:X 5}))
;; dl-eval: single-call source + query.
(dl-api-test-set! "dl-eval ancestor"
(dl-eval
"parent(a, b). parent(b, c).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)."
"?- ancestor(a, X).")
(list {:X (quote b)} {:X (quote c)}))
(dl-api-test-set! "dl-eval multi-goal"
(dl-eval
"p(1). p(2). p(3). q(2). q(3)."
"?- p(X), q(X).")
(list {:X 2} {:X 3}))
;; dl-rules-of: rules with head matching a relation name.
(dl-api-test! "dl-rules-of count"
(let
((db (dl-program
"p(1). q(X) :- p(X). r(X) :- p(X). q(2).")))
(len (dl-rules-of db "q")))
1)
(dl-api-test! "dl-rules-of empty"
(let
((db (dl-program "p(1). p(2).")))
(len (dl-rules-of db "q")))
0)
;; dl-clear-idb!: wipe rule-headed relations.
(dl-api-test! "dl-clear-idb! wipes IDB"
(let
((db (dl-program
"parent(a, b). parent(b, c).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(do
(dl-saturate! db)
(dl-clear-idb! db)
(len (dl-relation db "ancestor"))))
0)
(dl-api-test! "dl-clear-idb! preserves EDB"
(let
((db (dl-program
"parent(a, b). parent(b, c).
ancestor(X, Y) :- parent(X, Y).")))
(do
(dl-saturate! db)
(dl-clear-idb! db)
(len (dl-relation db "parent"))))
2)
;; dl-eval-magic — routes single-goal queries through
;; magic-sets evaluation.
(dl-api-test-set! "dl-eval-magic ancestor"
(dl-eval-magic
"parent(a, b). parent(b, c).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)."
"?- ancestor(a, X).")
(list {:X (quote b)} {:X (quote c)}))
;; Equivalence: dl-eval and dl-eval-magic produce the same
;; answers for any well-formed query (magic-sets is a perf
;; alternative, not a semantic change).
(dl-api-test! "dl-eval ≡ dl-eval-magic on ancestor"
(let
((source "parent(a, b). parent(b, c). parent(c, d).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)."))
(let
((semi (dl-eval source "?- ancestor(a, X)."))
(magic (dl-eval-magic source "?- ancestor(a, X).")))
(= (len semi) (len magic))))
true)
;; Comprehensive integration: recursion + stratified negation
;; + aggregation + comparison composed in a single program.
;; (Uses _Anything as a regular var instead of `_` so the
;; outer rule binds via the reach lit.)
(dl-api-test-set! "integration"
(dl-eval
(str
"edge(a, b). edge(b, c). edge(c, d). edge(a, d). "
"banned(c). "
"reach(X, Y) :- edge(X, Y). "
"reach(X, Z) :- edge(X, Y), reach(Y, Z). "
"safe(X, Y) :- reach(X, Y), not(banned(Y)). "
"reach_count(X, N) :- reach(X, Z), count(N, Y, safe(X, Y)). "
"popular(X) :- reach_count(X, N), >=(N, 2).")
"?- popular(X).")
(list {:X (quote a)}))
;; dl-rule-from-list with no arrow → fact-style.
(dl-api-test-set! "no arrow → fact-like rule"
(let
((rule (dl-rule-from-list (quote (foo X Y)))))
(list rule))
(list {:head (quote (foo X Y)) :body (list)}))
;; dl-coerce-rule on dict passes through.
(dl-api-test-set! "coerce dict rule"
(let
((d {:head (quote (h X)) :body (quote ((b X)))}))
(list (dl-coerce-rule d)))
(list {:head (quote (h X)) :body (quote ((b X)))})))))
(define
dl-api-tests-run!
(fn
()
(do
(set! dl-api-pass 0)
(set! dl-api-fail 0)
(set! dl-api-failures (list))
(dl-api-run-all!)
{:passed dl-api-pass
:failed dl-api-fail
:total (+ dl-api-pass dl-api-fail)
:failures dl-api-failures})))

View File

@@ -1,285 +0,0 @@
;; lib/datalog/tests/builtins.sx — comparison + arithmetic body literals.
(define dl-bt-pass 0)
(define dl-bt-fail 0)
(define dl-bt-failures (list))
(define
dl-bt-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-bt-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let
((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-bt-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-bt-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-bt-deep=? (nth a i) (nth b i))) false)
(else (dl-bt-deq-l? a b (+ i 1))))))
(define
dl-bt-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i))) (not (dl-bt-deep=? (get a k) (get b k))))
false)
(else (dl-bt-deq-d? a b ka (+ i 1))))))
(define
dl-bt-set=?
(fn
(a b)
(and (= (len a) (len b)) (dl-bt-subset? a b) (dl-bt-subset? b a))))
(define
dl-bt-subset?
(fn
(xs ys)
(cond
((= (len xs) 0) true)
((not (dl-bt-contains? ys (first xs))) false)
(else (dl-bt-subset? (rest xs) ys)))))
(define
dl-bt-contains?
(fn
(xs target)
(cond
((= (len xs) 0) false)
((dl-bt-deep=? (first xs) target) true)
(else (dl-bt-contains? (rest xs) target)))))
(define
dl-bt-test-set!
(fn
(name got expected)
(if
(dl-bt-set=? got expected)
(set! dl-bt-pass (+ dl-bt-pass 1))
(do
(set! dl-bt-fail (+ dl-bt-fail 1))
(append!
dl-bt-failures
(str
name
"\n expected (set): "
expected
"\n got: "
got))))))
(define
dl-bt-test!
(fn
(name got expected)
(if
(dl-bt-deep=? got expected)
(set! dl-bt-pass (+ dl-bt-pass 1))
(do
(set! dl-bt-fail (+ dl-bt-fail 1))
(append!
dl-bt-failures
(str name "\n expected: " expected "\n got: " got))))))
(define
dl-bt-throws?
(fn
(thunk)
(let
((threw false))
(do (guard (e (#t (set! threw true))) (thunk)) threw))))
(define
dl-bt-run-all!
(fn
()
(do
(dl-bt-test-set!
"less than filter"
(dl-query
(dl-program
"age(alice, 30). age(bob, 17). age(carol, 22).\n adult(X) :- age(X, A), >=(A, 18).")
(list (quote adult) (quote X)))
(list {:X (quote alice)} {:X (quote carol)}))
(dl-bt-test-set!
"less-equal filter"
(dl-query
(dl-program
"n(1). n(2). n(3). n(4). n(5).\n small(X) :- n(X), <=(X, 3).")
(list (quote small) (quote X)))
(list {:X 1} {:X 2} {:X 3}))
(dl-bt-test-set!
"not-equal filter"
(dl-query
(dl-program
"p(1, 2). p(2, 2). p(3, 4).\n diff(X, Y) :- p(X, Y), !=(X, Y).")
(list (quote diff) (quote X) (quote Y)))
(list {:X 1 :Y 2} {:X 3 :Y 4}))
(dl-bt-test-set!
"is plus"
(dl-query
(dl-program
"n(1). n(2). n(3).\n succ(X, Y) :- n(X), is(Y, +(X, 1)).")
(list (quote succ) (quote X) (quote Y)))
(list {:X 1 :Y 2} {:X 2 :Y 3} {:X 3 :Y 4}))
(dl-bt-test-set!
"is multiply"
(dl-query
(dl-program
"n(2). n(3). n(4).\n square(X, Y) :- n(X), is(Y, *(X, X)).")
(list (quote square) (quote X) (quote Y)))
(list {:X 2 :Y 4} {:X 3 :Y 9} {:X 4 :Y 16}))
(dl-bt-test-set!
"is nested expr"
(dl-query
(dl-program
"n(1). n(2). n(3).\n f(X, Y) :- n(X), is(Y, *(+(X, 1), 2)).")
(list (quote f) (quote X) (quote Y)))
(list {:X 1 :Y 4} {:X 2 :Y 6} {:X 3 :Y 8}))
(dl-bt-test-set!
"is bound LHS — equality"
(dl-query
(dl-program
"n(1, 2). n(2, 5). n(3, 4).\n succ(X, Y) :- n(X, Y), is(Y, +(X, 1)).")
(list (quote succ) (quote X) (quote Y)))
(list {:X 1 :Y 2} {:X 3 :Y 4}))
(dl-bt-test-set!
"triple via is"
(dl-query
(dl-program
"n(1). n(2). n(3).\n triple(X, Y) :- n(X), is(Y, *(X, 3)).")
(list (quote triple) (quote X) (quote Y)))
(list {:X 1 :Y 3} {:X 2 :Y 6} {:X 3 :Y 9}))
(dl-bt-test-set!
"= unifies var with constant"
(dl-query
(dl-program "p(a). p(b).\n qual(X) :- p(X), =(X, a).")
(list (quote qual) (quote X)))
(list {:X (quote a)}))
(dl-bt-test-set!
"= unifies two vars (one bound)"
(dl-query
(dl-program "p(a). p(b).\n twin(X, Y) :- p(X), =(Y, X).")
(list (quote twin) (quote X) (quote Y)))
(list {:X (quote a) :Y (quote a)} {:X (quote b) :Y (quote b)}))
(dl-bt-test!
"big count"
(let
((db (dl-program "n(0). n(1). n(2). n(3). n(4). n(5). n(6). n(7). n(8). n(9).\n big(X) :- n(X), >=(X, 5).")))
(do (dl-saturate! db) (len (dl-relation db "big"))))
5)
;; Built-in / arithmetic literals work as standalone query goals
;; (without needing a wrapper rule).
(dl-bt-test-set! "comparison-only goal true"
(dl-eval "" "?- <(1, 2).")
(list {}))
(dl-bt-test-set! "comparison-only goal false"
(dl-eval "" "?- <(2, 1).")
(list))
(dl-bt-test-set! "is goal binds"
(dl-eval "" "?- is(N, +(2, 3)).")
(list {:N 5}))
;; Bounded successor: a recursive rule with a comparison
;; guard terminates because the Herbrand base is effectively
;; bounded.
(dl-bt-test-set! "bounded successor"
(dl-query
(dl-program
"nat(0).
nat(Y) :- nat(X), is(Y, +(X, 1)), <(Y, 5).")
(list (quote nat) (quote X)))
(list {:X 0} {:X 1} {:X 2} {:X 3} {:X 4}))
(dl-bt-test!
"unsafe — comparison without binder"
(dl-bt-throws? (fn () (dl-program "p(X) :- <(X, 5).")))
true)
(dl-bt-test!
"unsafe — comparison both unbound"
(dl-bt-throws? (fn () (dl-program "p(X, Y) :- <(X, Y), q(X).")))
true)
(dl-bt-test!
"unsafe — is uses unbound RHS var"
(dl-bt-throws?
(fn () (dl-program "p(X, Y) :- q(X), is(Y, +(X, Z)).")))
true)
(dl-bt-test!
"unsafe — is on its own"
(dl-bt-throws? (fn () (dl-program "p(Y) :- is(Y, +(X, 1)).")))
true)
(dl-bt-test!
"unsafe — = between two unbound"
(dl-bt-throws? (fn () (dl-program "p(X, Y) :- =(X, Y).")))
true)
(dl-bt-test!
"safe — is binds head var"
(dl-bt-throws?
(fn () (dl-program "n(1). p(Y) :- n(X), is(Y, +(X, 1)).")))
false)
(dl-bt-test!
"safe — comparison after binder"
(dl-bt-throws?
(fn () (dl-program "n(1). big(X) :- n(X), >=(X, 0).")))
false)
(dl-bt-test!
"safe — = binds head var"
(dl-bt-throws?
(fn () (dl-program "p(a). p(b). x(Y) :- p(X), =(Y, X).")))
false)
;; Division by zero raises with a clear error. Without this guard
;; SX's `/` returned IEEE infinity, which then silently flowed
;; through comparisons and aggregations.
(dl-bt-test!
"is — division by zero raises"
(dl-bt-throws?
(fn ()
(dl-eval "p(10). q(R) :- p(X), is(R, /(X, 0))." "?- q(R).")))
true)
;; Comparison ops `<`, `<=`, `>`, `>=` require both operands to
;; have the same primitive type. Cross-type comparisons used to
;; silently return false (for some pairs) or raise a confusing
;; host-level error (for others) — now they all raise with a
;; message that names the offending values.
(dl-bt-test!
"comparison — string vs number raises"
(dl-bt-throws?
(fn ()
(dl-eval "p(\"hello\"). q(X) :- p(X), <(X, 5)." "?- q(X).")))
true)
;; `!=` is the exception — it's a polymorphic inequality test
;; (uses dl-tuple-equal? underneath) so cross-type pairs are
;; legitimate (and trivially unequal).
(dl-bt-test-set! "!= works across types"
(dl-query
(dl-program
"p(1). p(\"1\"). q(X) :- p(X), !=(X, 1).")
(quote (q X)))
(list {:X "1"})))))
(define
dl-builtins-tests-run!
(fn
()
(do
(set! dl-bt-pass 0)
(set! dl-bt-fail 0)
(set! dl-bt-failures (list))
(dl-bt-run-all!)
{:failures dl-bt-failures :total (+ dl-bt-pass dl-bt-fail) :passed dl-bt-pass :failed dl-bt-fail})))

View File

@@ -1,321 +0,0 @@
;; lib/datalog/tests/demo.sx — Phase 10 demo programs.
(define dl-demo-pass 0)
(define dl-demo-fail 0)
(define dl-demo-failures (list))
(define
dl-demo-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-demo-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let ((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-demo-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-demo-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-demo-deep=? (nth a i) (nth b i))) false)
(else (dl-demo-deq-l? a b (+ i 1))))))
(define
dl-demo-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i)))
(not (dl-demo-deep=? (get a k) (get b k))))
false)
(else (dl-demo-deq-d? a b ka (+ i 1))))))
(define
dl-demo-set=?
(fn
(a b)
(and
(= (len a) (len b))
(dl-demo-subset? a b)
(dl-demo-subset? b a))))
(define
dl-demo-subset?
(fn
(xs ys)
(cond
((= (len xs) 0) true)
((not (dl-demo-contains? ys (first xs))) false)
(else (dl-demo-subset? (rest xs) ys)))))
(define
dl-demo-contains?
(fn
(xs target)
(cond
((= (len xs) 0) false)
((dl-demo-deep=? (first xs) target) true)
(else (dl-demo-contains? (rest xs) target)))))
(define
dl-demo-test-set!
(fn
(name got expected)
(if
(dl-demo-set=? got expected)
(set! dl-demo-pass (+ dl-demo-pass 1))
(do
(set! dl-demo-fail (+ dl-demo-fail 1))
(append!
dl-demo-failures
(str
name
"\n expected (set): " expected
"\n got: " got))))))
(define
dl-demo-run-all!
(fn
()
(do
;; ── Federation ──────────────────────────────────────────
(dl-demo-test-set! "mutuals"
(dl-query
(dl-demo-make
(quote ((follows alice bob) (follows bob alice)
(follows bob carol) (follows carol dave)))
dl-demo-federation-rules)
(quote (mutual alice X)))
(list {:X (quote bob)}))
(dl-demo-test-set! "reachable transitive"
(dl-query
(dl-demo-make
(quote ((follows alice bob) (follows bob carol) (follows carol dave)))
dl-demo-federation-rules)
(quote (reachable alice X)))
(list {:X (quote bob)} {:X (quote carol)} {:X (quote dave)}))
(dl-demo-test-set! "foaf"
(dl-query
(dl-demo-make
(quote ((follows alice bob) (follows bob carol) (follows alice dave)))
dl-demo-federation-rules)
(quote (foaf alice X)))
(list {:X (quote carol)}))
;; ── Content ─────────────────────────────────────────────
(dl-demo-test-set! "popular posts"
(dl-query
(dl-demo-make
(quote
((authored alice p1) (authored bob p2) (authored carol p3)
(liked u1 p1) (liked u2 p1) (liked u3 p1)
(liked u1 p2)))
dl-demo-content-rules)
(quote (popular P)))
(list {:P (quote p1)}))
(dl-demo-test-set! "interesting feed"
(dl-query
(dl-demo-make
(quote
((follows me alice) (follows me bob)
(authored alice p1) (authored bob p2)
(liked u1 p1) (liked u2 p1) (liked u3 p1)
(liked u4 p2)))
dl-demo-content-rules)
(quote (interesting me P)))
(list {:P (quote p1)}))
(dl-demo-test-set! "post likes count"
(dl-query
(dl-demo-make
(quote
((authored alice p1)
(liked u1 p1) (liked u2 p1) (liked u3 p1)))
dl-demo-content-rules)
(quote (post-likes p1 N)))
(list {:N 3}))
;; ── Permissions ─────────────────────────────────────────
(dl-demo-test-set! "direct group access"
(dl-query
(dl-demo-make
(quote
((member alice editors)
(allowed editors blog)))
dl-demo-perm-rules)
(quote (can-access X blog)))
(list {:X (quote alice)}))
(dl-demo-test-set! "subgroup access"
(dl-query
(dl-demo-make
(quote
((member bob writers)
(subgroup writers editors)
(allowed editors blog)))
dl-demo-perm-rules)
(quote (can-access X blog)))
(list {:X (quote bob)}))
(dl-demo-test-set! "transitive subgroup"
(dl-query
(dl-demo-make
(quote
((member carol drafters)
(subgroup drafters writers)
(subgroup writers editors)
(allowed editors blog)))
dl-demo-perm-rules)
(quote (can-access X blog)))
(list {:X (quote carol)}))
;; ── Cooking posts (canonical Phase 10 example) ─────────
(dl-demo-test-set! "cooking posts by network"
(dl-query
(dl-demo-make
(quote
((follows me alice) (follows alice bob) (follows alice carol)
(authored alice p1) (authored bob p2)
(authored carol p3) (authored carol p4)
(tagged p1 travel) (tagged p2 cooking)
(tagged p3 cooking) (tagged p4 books)))
dl-demo-cooking-rules)
(quote (cooking-post-by-network me P)))
(list {:P (quote p2)} {:P (quote p3)}))
(dl-demo-test-set! "cooking — direct follow only"
(dl-query
(dl-demo-make
(quote
((follows me bob)
(authored bob p1) (authored bob p2)
(tagged p1 cooking) (tagged p2 books)))
dl-demo-cooking-rules)
(quote (cooking-post-by-network me P)))
(list {:P (quote p1)}))
(dl-demo-test-set! "cooking — none in network"
(dl-query
(dl-demo-make
(quote
((follows me bob)
(authored bob p1) (tagged p1 books)))
dl-demo-cooking-rules)
(quote (cooking-post-by-network me P)))
(list))
;; ── Tag co-occurrence ──────────────────────────────────
(dl-demo-test-set! "cotagged posts"
(dl-query
(dl-demo-make
(quote
((tagged p1 cooking) (tagged p1 vegetarian)
(tagged p2 cooking) (tagged p2 quick)
(tagged p3 vegetarian)))
dl-demo-tag-cooccur-rules)
(quote (cotagged P cooking vegetarian)))
(list {:P (quote p1)}))
(dl-demo-test-set! "tag pair count"
(dl-query
(dl-demo-make
(quote
((tagged p1 cooking) (tagged p1 vegetarian)
(tagged p2 cooking) (tagged p2 quick)
(tagged p3 cooking) (tagged p3 vegetarian)))
dl-demo-tag-cooccur-rules)
(quote (tag-pair-count cooking vegetarian N)))
(list {:N 2}))
;; ── Shortest path on a weighted DAG ──────────────────
(dl-demo-test-set! "shortest a→d via DAG"
(dl-query
(dl-demo-make
(quote ((edge a b 5) (edge b c 3) (edge a c 10) (edge c d 2)))
dl-demo-shortest-path-rules)
(quote (shortest a d W)))
(list {:W 10}))
(dl-demo-test-set! "shortest a→c picks 2-hop"
(dl-query
(dl-demo-make
(quote ((edge a b 5) (edge b c 3) (edge a c 10)))
dl-demo-shortest-path-rules)
(quote (shortest a c W)))
(list {:W 8}))
(dl-demo-test-set! "shortest unreachable empty"
(dl-query
(dl-demo-make
(quote ((edge a b 5) (edge b c 3)))
dl-demo-shortest-path-rules)
(quote (shortest a d W)))
(list))
;; ── Org chart + headcount ─────────────────────────────
(dl-demo-test-set! "ceo subordinate transitive"
(dl-query
(dl-demo-make
(quote
((manager ic1 mgr1) (manager ic2 mgr1)
(manager mgr1 vp1) (manager ic3 vp1)
(manager vp1 ceo)))
dl-demo-org-rules)
(quote (subordinate ceo X)))
(list
{:X (quote vp1)} {:X (quote mgr1)} {:X (quote ic1)}
{:X (quote ic2)} {:X (quote ic3)}))
(dl-demo-test-set! "ceo headcount = 5"
(dl-query
(dl-demo-make
(quote
((manager ic1 mgr1) (manager ic2 mgr1)
(manager mgr1 vp1) (manager ic3 vp1)
(manager vp1 ceo)))
dl-demo-org-rules)
(quote (headcount ceo N)))
(list {:N 5}))
(dl-demo-test-set! "mgr1 headcount = 2"
(dl-query
(dl-demo-make
(quote
((manager ic1 mgr1) (manager ic2 mgr1)
(manager mgr1 vp1) (manager ic3 vp1)
(manager vp1 ceo)))
dl-demo-org-rules)
(quote (headcount mgr1 N)))
(list {:N 2}))
(dl-demo-test-set! "no access without grant"
(dl-query
(dl-demo-make
(quote ((member dave outsiders) (allowed editors blog)))
dl-demo-perm-rules)
(quote (can-access X blog)))
(list)))))
(define
dl-demo-tests-run!
(fn
()
(do
(set! dl-demo-pass 0)
(set! dl-demo-fail 0)
(set! dl-demo-failures (list))
(dl-demo-run-all!)
{:passed dl-demo-pass
:failed dl-demo-fail
:total (+ dl-demo-pass dl-demo-fail)
:failures dl-demo-failures})))

View File

@@ -1,463 +0,0 @@
;; lib/datalog/tests/eval.sx — naive evaluation + safety analysis tests.
(define dl-et-pass 0)
(define dl-et-fail 0)
(define dl-et-failures (list))
;; Same deep-equal helper used in other suites.
(define
dl-et-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-et-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let
((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-et-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-et-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-et-deep=? (nth a i) (nth b i))) false)
(else (dl-et-deq-l? a b (+ i 1))))))
(define
dl-et-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i))) (not (dl-et-deep=? (get a k) (get b k))))
false)
(else (dl-et-deq-d? a b ka (+ i 1))))))
;; Set-equality on lists (order-independent, uses dl-et-deep=?).
(define
dl-et-set=?
(fn
(a b)
(and (= (len a) (len b)) (dl-et-subset? a b) (dl-et-subset? b a))))
(define
dl-et-subset?
(fn
(xs ys)
(cond
((= (len xs) 0) true)
((not (dl-et-contains? ys (first xs))) false)
(else (dl-et-subset? (rest xs) ys)))))
(define
dl-et-contains?
(fn
(xs target)
(cond
((= (len xs) 0) false)
((dl-et-deep=? (first xs) target) true)
(else (dl-et-contains? (rest xs) target)))))
(define
dl-et-test!
(fn
(name got expected)
(if
(dl-et-deep=? got expected)
(set! dl-et-pass (+ dl-et-pass 1))
(do
(set! dl-et-fail (+ dl-et-fail 1))
(append!
dl-et-failures
(str name "\n expected: " expected "\n got: " got))))))
(define
dl-et-test-set!
(fn
(name got expected)
(if
(dl-et-set=? got expected)
(set! dl-et-pass (+ dl-et-pass 1))
(do
(set! dl-et-fail (+ dl-et-fail 1))
(append!
dl-et-failures
(str
name
"\n expected (set): "
expected
"\n got: "
got))))))
(define
dl-et-throws?
(fn
(thunk)
(let
((threw false))
(do (guard (e (#t (set! threw true))) (thunk)) threw))))
(define
dl-et-run-all!
(fn
()
(do
(dl-et-test-set!
"fact lookup any"
(dl-query
(dl-program "parent(tom, bob). parent(bob, ann).")
(list (quote parent) (quote X) (quote Y)))
(list {:X (quote tom) :Y (quote bob)} {:X (quote bob) :Y (quote ann)}))
(dl-et-test-set!
"fact lookup constant arg"
(dl-query
(dl-program "parent(tom, bob). parent(tom, liz). parent(bob, ann).")
(list (quote parent) (quote tom) (quote Y)))
(list {:Y (quote bob)} {:Y (quote liz)}))
(dl-et-test-set!
"no match"
(dl-query
(dl-program "parent(tom, bob).")
(list (quote parent) (quote nobody) (quote X)))
(list))
(dl-et-test-set!
"ancestor closure"
(dl-query
(dl-program
"parent(tom, bob). parent(bob, ann). parent(ann, pat).\n ancestor(X, Y) :- parent(X, Y).\n ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")
(list (quote ancestor) (quote tom) (quote X)))
(list {:X (quote bob)} {:X (quote ann)} {:X (quote pat)}))
(dl-et-test-set!
"sibling"
(dl-query
(dl-program
"parent(tom, bob). parent(tom, liz). parent(jane, bob). parent(jane, liz).\n sibling(X, Y) :- parent(P, X), parent(P, Y).")
(list (quote sibling) (quote bob) (quote Y)))
(list {:Y (quote bob)} {:Y (quote liz)}))
(dl-et-test-set!
"same-generation"
(dl-query
(dl-program
"parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(liz, joe).\n person(tom). person(bob). person(liz). person(ann). person(joe).\n sg(X, X) :- person(X).\n sg(X, Y) :- parent(P1, X), sg(P1, P2), parent(P2, Y).")
(list (quote sg) (quote ann) (quote X)))
(list {:X (quote ann)} {:X (quote joe)}))
(dl-et-test!
"ancestor count"
(let
((db (dl-program "parent(a, b). parent(b, c). parent(c, d).\n ancestor(X, Y) :- parent(X, Y).\n ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(do (dl-saturate! db) (len (dl-relation db "ancestor"))))
6)
(dl-et-test-set!
"grandparent"
(dl-query
(dl-program
"parent(a, b). parent(b, c). parent(c, d).\n grandparent(X, Z) :- parent(X, Y), parent(Y, Z).")
(list (quote grandparent) (quote X) (quote Y)))
(list {:X (quote a) :Y (quote c)} {:X (quote b) :Y (quote d)}))
(dl-et-test!
"no recursion infinite loop"
(let
((db (dl-program "edge(1, 2). edge(2, 3). edge(3, 1).\n reach(X, Y) :- edge(X, Y).\n reach(X, Z) :- edge(X, Y), reach(Y, Z).")))
(do (dl-saturate! db) (len (dl-relation db "reach"))))
9)
;; Rule-shape sanity: empty-list head and non-list body raise
;; clear errors rather than crashing inside the saturator.
(dl-et-test! "empty head rejected"
(dl-et-throws?
(fn ()
(dl-add-rule! (dl-make-db)
{:head (list) :body (list)})))
true)
(dl-et-test! "non-list body rejected"
(dl-et-throws?
(fn ()
(dl-add-rule! (dl-make-db)
{:head (list (quote p) (quote X)) :body 42})))
true)
;; Reserved relation names rejected as rule/fact heads.
(dl-et-test!
"reserved name `not` as head rejected"
(dl-et-throws? (fn () (dl-program "not(X) :- p(X).")))
true)
(dl-et-test!
"reserved name `count` as head rejected"
(dl-et-throws?
(fn () (dl-program "count(N, X, p(X)) :- p(X).")))
true)
(dl-et-test!
"reserved name `<` as head rejected"
(dl-et-throws? (fn () (dl-program "<(X, 5) :- p(X).")))
true)
(dl-et-test!
"reserved name `is` as head rejected"
(dl-et-throws? (fn () (dl-program "is(N, +(1, 2)) :- p(N).")))
true)
;; Body literal with a reserved-name positive head is rejected.
;; The parser only treats outer-level `not(P)` as negation; nested
;; `not(not(P))` would otherwise silently parse as a positive call
;; to a relation named `not` and succeed vacuously. The safety
;; checker now flags this so the user gets a clear error.
;; Body literal with a reserved-name positive head is rejected.
;; The parser only treats outer-level `not(P)` as negation; nested
;; `not(not(P))` would otherwise silently parse as a positive call
;; to a relation named `not` and succeed vacuously — so the safety
;; checker now flags this to give the user a clear error.
(dl-et-test!
"nested not(not(...)) rejected"
(dl-et-throws?
(fn ()
(dl-program
"banned(a). u(a). vip(X) :- u(X), not(not(banned(X))).")))
true)
;; A dict body literal that isn't `{:neg ...}` is almost always a
;; typo — it would otherwise silently fall through to a confusing
;; head-var-unbound safety error. Now caught with a clear message.
(dl-et-test!
"dict body lit without :neg rejected"
(dl-et-throws?
(fn ()
(let ((db (dl-make-db)))
(dl-add-rule! db
{:head (list (quote p) (quote X))
:body (list {:weird "stuff"})}))))
true)
;; Facts may only have simple-term args (number / string / symbol).
;; A compound arg like `+(1, 2)` would otherwise be silently
;; stored as the unreduced expression `(+ 1 2)` because dl-ground?
;; sees no free variables.
(dl-et-test!
"compound arg in fact rejected"
(dl-et-throws? (fn () (dl-program "p(+(1, 2)).")))
true)
;; Rule heads may only have variable or constant args — no
;; compounds. Compound heads would be saturated as unreduced
;; tuples rather than the arithmetic result the user expected.
(dl-et-test!
"compound arg in rule head rejected"
(dl-et-throws?
(fn () (dl-program "n(3). double(*(X, 2)) :- n(X).")))
true)
;; The anonymous-variable renamer used to start at `_anon1`
;; unconditionally; a rule that wrote `q(_anon1) :- p(_anon1, _)`
;; (the user picking the same name the renamer would generate)
;; would see the `_` renamed to `_anon1` too, collapsing the
;; two positions in `p(_anon1, _)` to a single var. Now the
;; renamer scans the rule for the max `_anon<N>` and starts past
;; it, so user-written names of that form are preserved.
(dl-et-test-set! "anonymous-rename avoids user `_anon` collision"
(dl-query
(dl-program
"p(a, b). p(c, d). q(_anon1) :- p(_anon1, _).")
(quote (q X)))
(list {:X (quote a)} {:X (quote c)}))
(dl-et-test!
"unsafe head var"
(dl-et-throws? (fn () (dl-program "p(X, Y) :- q(X).")))
true)
(dl-et-test!
"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 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"
(dl-et-throws? (fn () (dl-program "p(X, Y) :- q(Z).")))
true)
(dl-et-test!
"head var bound by body"
(dl-et-throws? (fn () (dl-program "p(X) :- q(X).")))
false)
(dl-et-test!
"head subset of body"
(dl-et-throws?
(fn
()
(dl-program
"edge(a,b). edge(b,c). reach(X, Z) :- edge(X, Y), edge(Y, Z).")))
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}))
;; dl-summary: relation -> tuple-count for inspection.
(dl-et-test! "dl-summary basic"
(dl-summary
(let
((db (dl-program "p(1). p(2). q(3).")))
(do (dl-saturate! db) db)))
{:p 2 :q 1})
(dl-et-test! "dl-summary empty IDB shown"
(dl-summary
(let
((db (dl-program "r(X) :- s(X).")))
(do (dl-saturate! db) db)))
{:r 0})
(dl-et-test! "dl-summary mixed EDB and IDB"
(dl-summary
(let
((db (dl-program
"parent(a, b).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(do (dl-saturate! db) db)))
{:parent 1 :ancestor 1})
(dl-et-test! "dl-summary empty db"
(dl-summary (dl-make-db))
{})
;; Strategy hook: default semi-naive; :magic accepted but
;; falls back to semi-naive (the transformation itself is
;; deferred — Phase 6 in plan).
(dl-et-test! "default strategy"
(dl-get-strategy (dl-make-db))
:semi-naive)
(dl-et-test! "set strategy"
(let ((db (dl-make-db)))
(do (dl-set-strategy! db :magic) (dl-get-strategy db)))
:magic)
;; Unknown strategy values are rejected so typos don't silently
;; fall back to the default.
(dl-et-test!
"unknown strategy rejected"
(dl-et-throws?
(fn ()
(let ((db (dl-make-db)))
(dl-set-strategy! db :semi_naive))))
true)
;; dl-saturated?: no-work-left predicate.
(dl-et-test! "saturated? after saturation"
(let ((db (dl-program
"parent(a, b).
ancestor(X, Y) :- parent(X, Y).")))
(do (dl-saturate! db) (dl-saturated? db)))
true)
(dl-et-test! "saturated? before saturation"
(let ((db (dl-program
"parent(a, b).
ancestor(X, Y) :- parent(X, Y).")))
(dl-saturated? db))
false)
;; Disjunction via multiple rules — Datalog has no `;` in
;; body, so disjunction is expressed as separate rules with
;; the same head. Here plant_based(X) is satisfied by either
;; vegan(X) or vegetarian(X).
(dl-et-test-set! "disjunction via multiple rules"
(dl-query
(dl-program
"vegan(alice). vegetarian(bob). meat_eater(carol).
plant_based(X) :- vegan(X).
plant_based(X) :- vegetarian(X).")
(list (quote plant_based) (quote X)))
(list {:X (quote alice)} {:X (quote bob)}))
;; Bipartite-style join: pair-of-friends who share a hobby.
;; Three-relation join exercising the planner's join order.
(dl-et-test-set! "bipartite friends-with-hobby"
(dl-query
(dl-program
"hobby(alice, climb). hobby(bob, paint).
hobby(carol, climb).
friend(alice, carol). friend(bob, alice).
match(A, B, H) :- friend(A, B), hobby(A, H), hobby(B, H).")
(list (quote match) (quote A) (quote B) (quote H)))
(list {:A (quote alice) :B (quote carol) :H (quote climb)}))
;; Repeated variable (diagonal): p(X, X) only matches tuples
;; whose two args are equal. The unifier handles this via the
;; subst chain — first occurrence binds X, second occurrence
;; checks against the binding.
(dl-et-test-set! "diagonal query"
(dl-query
(dl-program "p(1, 1). p(2, 3). p(4, 4). p(5, 5).")
(list (quote p) (quote X) (quote X)))
(list {:X 1} {:X 4} {:X 5}))
;; A relation can be both EDB-seeded and rule-derived;
;; saturate combines facts + derivations.
(dl-et-test-set! "mixed EDB + IDB same relation"
(dl-query
(dl-program
"link(a, b). link(c, d). link(e, c).
via(a, e).
link(X, Y) :- via(X, M), link(M, Y).")
(list (quote link) (quote a) (quote X)))
(list {:X (quote b)} {:X (quote c)}))
(dl-et-test! "saturated? after assert"
(let ((db (dl-program
"parent(a, b).
ancestor(X, Y) :- parent(X, Y).")))
(do
(dl-saturate! db)
(dl-add-fact! db (list (quote parent) (quote b) (quote c)))
(dl-saturated? db)))
false)
(dl-et-test-set! "magic-set still derives correctly"
(let
((db (dl-program
"parent(a, b). parent(b, c).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(do
(dl-set-strategy! db :magic)
(dl-query db (list (quote ancestor) (quote a) (quote X)))))
(list {:X (quote b)} {:X (quote c)})))))
(define
dl-eval-tests-run!
(fn
()
(do
(set! dl-et-pass 0)
(set! dl-et-fail 0)
(set! dl-et-failures (list))
(dl-et-run-all!)
{:failures dl-et-failures :total (+ dl-et-pass dl-et-fail) :passed dl-et-pass :failed dl-et-fail})))

View File

@@ -1,528 +0,0 @@
;; lib/datalog/tests/magic.sx — adornment + SIPS analysis tests.
(define dl-mt-pass 0)
(define dl-mt-fail 0)
(define dl-mt-failures (list))
(define
dl-mt-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-mt-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let ((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-mt-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-mt-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-mt-deep=? (nth a i) (nth b i))) false)
(else (dl-mt-deq-l? a b (+ i 1))))))
(define
dl-mt-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i)))
(not (dl-mt-deep=? (get a k) (get b k))))
false)
(else (dl-mt-deq-d? a b ka (+ i 1))))))
(define
dl-mt-test!
(fn
(name got expected)
(if
(dl-mt-deep=? got expected)
(set! dl-mt-pass (+ dl-mt-pass 1))
(do
(set! dl-mt-fail (+ dl-mt-fail 1))
(append!
dl-mt-failures
(str
name
"\n expected: " expected
"\n got: " got))))))
(define
dl-mt-run-all!
(fn
()
(do
;; Goal adornment.
(dl-mt-test! "adorn 0-ary"
(dl-adorn-goal (list (quote ready)))
"")
(dl-mt-test! "adorn all bound"
(dl-adorn-goal (list (quote p) 1 2 3))
"bbb")
(dl-mt-test! "adorn all free"
(dl-adorn-goal (list (quote p) (quote X) (quote Y)))
"ff")
(dl-mt-test! "adorn mixed"
(dl-adorn-goal (list (quote ancestor) (quote tom) (quote X)))
"bf")
(dl-mt-test! "adorn const var const"
(dl-adorn-goal (list (quote p) (quote a) (quote X) (quote b)))
"bfb")
;; dl-adorn-lit with explicit bound set.
(dl-mt-test! "adorn lit with bound"
(dl-adorn-lit (list (quote p) (quote X) (quote Y)) (list "X"))
"bf")
;; Rule SIPS — chain ancestor.
(dl-mt-test! "sips chain ancestor bf"
(dl-rule-sips
{:head (list (quote ancestor) (quote X) (quote Z))
:body (list (list (quote parent) (quote X) (quote Y))
(list (quote ancestor) (quote Y) (quote Z)))}
"bf")
(list
{:lit (list (quote parent) (quote X) (quote Y)) :adornment "bf"}
{:lit (list (quote ancestor) (quote Y) (quote Z)) :adornment "bf"}))
;; SIPS — head fully bound.
(dl-mt-test! "sips head bb"
(dl-rule-sips
{:head (list (quote q) (quote X) (quote Y))
:body (list (list (quote p) (quote X) (quote Z))
(list (quote r) (quote Z) (quote Y)))}
"bb")
(list
{:lit (list (quote p) (quote X) (quote Z)) :adornment "bf"}
{:lit (list (quote r) (quote Z) (quote Y)) :adornment "bb"}))
;; SIPS — comparison; vars must be bound by prior body lit.
(dl-mt-test! "sips with comparison"
(dl-rule-sips
{:head (list (quote q) (quote X))
:body (list (list (quote p) (quote X))
(list (string->symbol "<") (quote X) 5))}
"f")
(list
{:lit (list (quote p) (quote X)) :adornment "f"}
{:lit (list (string->symbol "<") (quote X) 5) :adornment "bb"}))
;; SIPS — `is` binds its left arg.
(dl-mt-test! "sips with is"
(dl-rule-sips
{:head (list (quote q) (quote X) (quote Y))
:body (list (list (quote p) (quote X))
(list (quote is) (quote Y) (list (string->symbol "+") (quote X) 1)))}
"ff")
(list
{:lit (list (quote p) (quote X)) :adornment "f"}
{:lit (list (quote is) (quote Y)
(list (string->symbol "+") (quote X) 1))
:adornment "fb"}))
;; Magic predicate naming.
(dl-mt-test! "magic-rel-name"
(dl-magic-rel-name "ancestor" "bf")
"magic_ancestor^bf")
;; Bound-args extraction.
(dl-mt-test! "bound-args bf"
(dl-bound-args (list (quote ancestor) (quote tom) (quote X)) "bf")
(list (quote tom)))
(dl-mt-test! "bound-args mixed"
(dl-bound-args (list (quote p) 1 (quote Y) 3) "bfb")
(list 1 3))
(dl-mt-test! "bound-args all-free"
(dl-bound-args (list (quote p) (quote X) (quote Y)) "ff")
(list))
;; Magic literal construction.
(dl-mt-test! "magic-lit"
(dl-magic-lit "ancestor" "bf" (list (quote tom)))
(list (string->symbol "magic_ancestor^bf") (quote tom)))
;; Magic-sets rewriter: structural sanity.
(dl-mt-test! "rewrite ancestor produces seed"
(let
((rules
(list
{:head (list (quote ancestor) (quote X) (quote Y))
:body (list (list (quote parent) (quote X) (quote Y)))}
{:head (list (quote ancestor) (quote X) (quote Z))
:body
(list (list (quote parent) (quote X) (quote Y))
(list (quote ancestor) (quote Y) (quote Z)))})))
(get
(dl-magic-rewrite rules "ancestor" "bf" (list (quote a)))
:seed))
(list (string->symbol "magic_ancestor^bf") (quote a)))
;; Equivalence: rewritten program derives same ancestor tuples.
;; In a chain a→b→c→d, magic-rewritten run still derives all
;; ancestor pairs reachable from any node a/b/c/d propagated via
;; magic_ancestor^bf — i.e. the full closure (6 tuples). Magic
;; saves work only when the EDB has irrelevant nodes outside
;; the seed's transitive cone.
(dl-mt-test! "magic-rewritten ancestor count"
(let
((rules
(list
{:head (list (quote ancestor) (quote X) (quote Y))
:body (list (list (quote parent) (quote X) (quote Y)))}
{:head (list (quote ancestor) (quote X) (quote Z))
:body
(list (list (quote parent) (quote X) (quote Y))
(list (quote ancestor) (quote Y) (quote Z)))}))
(edb (list
(list (quote parent) (quote a) (quote b))
(list (quote parent) (quote b) (quote c))
(list (quote parent) (quote c) (quote d)))))
(let
((rewritten (dl-magic-rewrite rules "ancestor" "bf" (list (quote a))))
(db (dl-make-db)))
(do
(for-each (fn (f) (dl-add-fact! db f)) edb)
(dl-add-fact! db (get rewritten :seed))
(for-each (fn (r) (dl-add-rule! db r)) (get rewritten :rules))
(dl-saturate! db)
(len (dl-relation db "ancestor")))))
6)
;; dl-magic-query: end-to-end driver, doesn't mutate caller's db.
;; Magic over a rule with negated body literal — propagation
;; rules generated only for positive lits; negated lits pass
;; through unchanged.
(dl-mt-test! "magic over rule with negation"
(let
((db (dl-program
"u(a). u(b). u(c). banned(b).
active(X) :- u(X), not(banned(X)).")))
(let
((semi (dl-query db (list (quote active) (quote X))))
(magic (dl-magic-query db (list (quote active) (quote X)))))
(= (len semi) (len magic))))
true)
;; All-bound query (existence check) generates an "bb"
;; adornment chain. Verifies the rewriter walks multiple
;; (rel, adn) pairs through the worklist.
(dl-mt-test! "magic existence check via bb"
(let
((db (dl-program
"parent(a, b). parent(b, c). parent(c, d).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(let
((found (dl-magic-query
db (list (quote ancestor) (quote a) (quote c))))
(missing (dl-magic-query
db (list (quote ancestor) (quote a) (quote z)))))
(and (= (len found) 1) (= (len missing) 0))))
true)
;; Magic equivalence on the federation demo.
(dl-mt-test! "magic ≡ semi on foaf demo"
(let
((db (dl-program-data
(quote ((follows alice bob)
(follows bob carol)
(follows alice dave)))
dl-demo-federation-rules)))
(let
((semi (dl-query db (quote (foaf alice X))))
(magic (dl-magic-query db (quote (foaf alice X)))))
(= (len semi) (len magic))))
true)
;; Shape validation: dl-magic-query rejects non-list / non-
;; dict goal shapes cleanly rather than crashing in `rest`.
(dl-mt-test! "magic rejects string goal"
(let ((threw false))
(do
(guard (e (#t (set! threw true)))
(dl-magic-query (dl-make-db) "foo"))
threw))
true)
(dl-mt-test! "magic rejects bare dict goal"
(let ((threw false))
(do
(guard (e (#t (set! threw true)))
(dl-magic-query (dl-make-db) {:foo "bar"}))
threw))
true)
;; 3-stratum program under magic — distinct rule heads at
;; strata 0/1/2 must all rewrite via the worklist.
(dl-mt-test! "magic 3-stratum program"
(let
((db (dl-program
"a(1). a(2). a(3). b(2).
c(X) :- a(X), not(b(X)).
d(X) :- c(X), not(banned(X)).
banned(3).")))
(let
((semi (dl-query db (list (quote d) (quote X))))
(magic (dl-magic-query db (list (quote d) (quote X)))))
(= (len semi) (len magic))))
true)
;; Aggregate -> derived -> threshold chain via magic.
(dl-mt-test! "magic aggregate-derived chain"
(let
((db (dl-program
"src(1). src(2). src(3).
cnt(N) :- count(N, X, src(X)).
active(N) :- cnt(N), >=(N, 2).")))
(let
((semi (dl-query db (list (quote active) (quote N))))
(magic (dl-magic-query db (list (quote active) (quote N)))))
(= (len semi) (len magic))))
true)
;; Multi-relation rewrite chain: query r4 → propagate to r3,
;; r2, r1, a. The worklist must process all of them; an
;; earlier bug stopped after only the head pair.
(dl-mt-test! "magic chain through 4 rule levels"
(let
((db (dl-program
"a(1). a(2). r1(X) :- a(X). r2(X) :- r1(X).
r3(X) :- r2(X). r4(X) :- r3(X).")))
(= 2 (len (dl-magic-query db (list (quote r4) (quote X))))))
true)
;; Shortest-path demo via magic — exercises the rewriter
;; against rules that mix recursive positive lits with an
;; aggregate body literal.
(dl-mt-test! "magic on shortest-path demo"
(let
((db (dl-program-data
(quote ((edge a b 5) (edge b c 3) (edge a c 10)))
dl-demo-shortest-path-rules)))
(let
((semi (dl-query db (quote (shortest a c W))))
(magic (dl-magic-query db (quote (shortest a c W)))))
(and (= (len semi) (len magic))
(= (len semi) 1))))
true)
;; Same relation called with different adornment patterns
;; in different rules. The worklist must enqueue and process
;; each (rel, adornment) pair.
(dl-mt-test! "magic with multi-adornment same relation"
(let
((db (dl-program
"parent(p1, alice). parent(p2, bob).
parent(g, p1). parent(g, p2).
sibling(P1, P2) :- parent(G, P1), parent(G, P2),
!=(P1, P2).
cousin(X, Y) :- parent(P1, X), parent(P2, Y),
sibling(P1, P2).")))
(let
((semi (dl-query db (list (quote cousin) (quote alice) (quote Y))))
(magic (dl-magic-query db (list (quote cousin) (quote alice) (quote Y)))))
(= (len semi) (len magic))))
true)
;; Magic over a rule whose body contains an aggregate.
;; The rewriter passes aggregate body lits through unchanged
;; (no propagation generated for them), so semi-naive's count
;; logic still fires correctly under the rewritten program.
(dl-mt-test! "magic over rule with aggregate body"
(let
((db (dl-program
"post(p1). post(p2). post(p3).
liked(u1, p1). liked(u2, p1). liked(u3, p1).
liked(u1, p2).
rich(P) :- post(P), count(N, U, liked(U, P)),
>=(N, 2).")))
(let
((semi (dl-query db (list (quote rich) (quote P))))
(magic (dl-magic-query db (list (quote rich) (quote P)))))
(= (len semi) (len magic))))
true)
;; Mixed EDB + IDB: a relation can be both EDB-seeded and
;; rule-derived. dl-magic-query must include the EDB portion
;; even though the relation has rules.
(dl-mt-test! "magic mixed EDB+IDB"
(len
(dl-magic-query
(dl-program
"link(a, b). link(c, d). link(e, c).
via(a, e).
link(X, Y) :- via(X, M), link(M, Y).")
(list (quote link) (quote a) (quote X))))
2)
;; dl-magic-query falls back to dl-query for built-in,
;; aggregate, and negation goals (the magic seed would
;; otherwise be non-ground).
(dl-mt-test! "magic-query falls back on aggregate"
(let
((r (dl-magic-query
(dl-program "p(1). p(2). p(3).")
(list (quote count) (quote N) (quote X)
(list (quote p) (quote X))))))
(and (= (len r) 1) (= (get (first r) "N") 3)))
true)
(dl-mt-test! "magic-query equivalent to dl-query"
(let
((db (dl-program
"parent(a, b). parent(b, c). parent(c, d).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(let
((semi (dl-query db (list (quote ancestor) (quote a) (quote X))))
(magic (dl-magic-query
db (list (quote ancestor) (quote a) (quote X)))))
(= (len semi) (len magic))))
true)
;; The magic rewriter passes aggregate body lits through
;; unchanged, so an aggregate over an IDB relation would see an
;; empty inner-goal in the magic db unless the IDB is already
;; materialised. dl-magic-query now pre-saturates the source db
;; to guarantee equivalence with dl-query for every stratified
;; program. Previously this returned `({:N 0})` because `active`
;; (IDB, derived through negation) was never derived in the
;; magic db.
(dl-mt-test! "magic over aggregate-of-IDB matches vanilla"
(let
((src
"u(a). u(b). u(c). u(d). banned(b). banned(d).
active(X) :- u(X), not(banned(X)).
n(N) :- count(N, X, active(X))."))
(let
((vanilla (dl-eval src "?- n(N)."))
(magic (dl-eval-magic src "?- n(N).")))
(and (= (len vanilla) 1)
(= (len magic) 1)
(= (get (first vanilla) "N")
(get (first magic) "N")))))
true)
;; magic-query doesn't mutate caller db.
(dl-mt-test! "magic-query preserves caller db"
(let
((db (dl-program
"parent(a, b). parent(b, c).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(let
((rules-before (len (dl-rules db))))
(do
(dl-magic-query db (list (quote ancestor) (quote a) (quote X)))
(= rules-before (len (dl-rules db))))))
true)
;; Magic-sets benefit: query touches only one cluster of a
;; multi-component graph. Semi-naive derives the full closure
;; over both clusters; magic only the seeded one.
;; Magic-vs-semi work shape: chain of 12. Semi-naive
;; derives the full closure (78 = 12·13/2). A magic query
;; rooted at node 0 returns the 12 descendants only —
;; demonstrating that magic limits derivation to the
;; query's transitive cone.
(dl-mt-test! "magic vs semi work-shape on chain-12"
(let
((source (str
"parent(0, 1). parent(1, 2). parent(2, 3). "
"parent(3, 4). parent(4, 5). parent(5, 6). "
"parent(6, 7). parent(7, 8). parent(8, 9). "
"parent(9, 10). parent(10, 11). parent(11, 12). "
"ancestor(X, Y) :- parent(X, Y). "
"ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(let
((db1 (dl-make-db)) (db2 (dl-make-db)))
(do
(dl-load-program! db1 source)
(dl-saturate! db1)
(dl-load-program! db2 source)
(let
((semi-count (len (dl-relation db1 "ancestor")))
(magic-count
(len (dl-magic-query
db2 (list (quote ancestor) 0 (quote X))))))
;; Magic returns only descendants of 0 (12 of them).
(and (= semi-count 78) (= magic-count 12))))))
true)
;; Magic + arithmetic: rules with `is` clauses pass through
;; the rewriter unchanged (built-ins aren't propagated).
(dl-mt-test! "magic preserves arithmetic"
(let
((source "n(1). n(2). n(3).
doubled(X, Y) :- n(X), is(Y, *(X, 2))."))
(let
((semi (dl-eval source "?- doubled(2, Y)."))
(magic (dl-eval-magic source "?- doubled(2, Y).")))
(= (len semi) (len magic))))
true)
(dl-mt-test! "magic skips irrelevant clusters"
(let
;; Two disjoint chains. Query is rooted in cluster 1.
((db (dl-program
"parent(a, b). parent(b, c).
parent(x, y). parent(y, z).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(do
(dl-saturate! db)
(let
((semi-count (len (dl-relation db "ancestor")))
(magic-results
(dl-magic-query
db (list (quote ancestor) (quote a) (quote X)))))
;; Semi-naive derives 6 (3 in each cluster). Magic
;; gives 3 query results (a's reachable: b, c).
(and (= semi-count 6) (= (len magic-results) 2)))))
true)
(dl-mt-test! "magic-rewritten finds same answers"
(let
((rules
(list
{:head (list (quote ancestor) (quote X) (quote Y))
:body (list (list (quote parent) (quote X) (quote Y)))}
{:head (list (quote ancestor) (quote X) (quote Z))
:body
(list (list (quote parent) (quote X) (quote Y))
(list (quote ancestor) (quote Y) (quote Z)))}))
(edb (list
(list (quote parent) (quote a) (quote b))
(list (quote parent) (quote b) (quote c)))))
(let
((rewritten (dl-magic-rewrite rules "ancestor" "bf" (list (quote a))))
(db (dl-make-db)))
(do
(for-each (fn (f) (dl-add-fact! db f)) edb)
(dl-add-fact! db (get rewritten :seed))
(for-each (fn (r) (dl-add-rule! db r)) (get rewritten :rules))
(dl-saturate! db)
(len (dl-query db (list (quote ancestor) (quote a) (quote X)))))))
2))))
(define
dl-magic-tests-run!
(fn
()
(do
(set! dl-mt-pass 0)
(set! dl-mt-fail 0)
(set! dl-mt-failures (list))
(dl-mt-run-all!)
{:passed dl-mt-pass
:failed dl-mt-fail
:total (+ dl-mt-pass dl-mt-fail)
:failures dl-mt-failures})))

View File

@@ -1,252 +0,0 @@
;; lib/datalog/tests/negation.sx — stratified negation tests.
(define dl-nt-pass 0)
(define dl-nt-fail 0)
(define dl-nt-failures (list))
(define
dl-nt-deep=?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-nt-deq-l? a b 0)))
((and (dict? a) (dict? b))
(let ((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-nt-deq-d? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-nt-deq-l?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-nt-deep=? (nth a i) (nth b i))) false)
(else (dl-nt-deq-l? a b (+ i 1))))))
(define
dl-nt-deq-d?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i)))
(not (dl-nt-deep=? (get a k) (get b k))))
false)
(else (dl-nt-deq-d? a b ka (+ i 1))))))
(define
dl-nt-set=?
(fn
(a b)
(and
(= (len a) (len b))
(dl-nt-subset? a b)
(dl-nt-subset? b a))))
(define
dl-nt-subset?
(fn
(xs ys)
(cond
((= (len xs) 0) true)
((not (dl-nt-contains? ys (first xs))) false)
(else (dl-nt-subset? (rest xs) ys)))))
(define
dl-nt-contains?
(fn
(xs target)
(cond
((= (len xs) 0) false)
((dl-nt-deep=? (first xs) target) true)
(else (dl-nt-contains? (rest xs) target)))))
(define
dl-nt-test!
(fn
(name got expected)
(if
(dl-nt-deep=? got expected)
(set! dl-nt-pass (+ dl-nt-pass 1))
(do
(set! dl-nt-fail (+ dl-nt-fail 1))
(append!
dl-nt-failures
(str
name
"\n expected: " expected
"\n got: " got))))))
(define
dl-nt-test-set!
(fn
(name got expected)
(if
(dl-nt-set=? got expected)
(set! dl-nt-pass (+ dl-nt-pass 1))
(do
(set! dl-nt-fail (+ dl-nt-fail 1))
(append!
dl-nt-failures
(str
name
"\n expected (set): " expected
"\n got: " got))))))
(define
dl-nt-throws?
(fn
(thunk)
(let
((threw false))
(do
(guard
(e (#t (set! threw true)))
(thunk))
threw))))
(define
dl-nt-run-all!
(fn
()
(do
;; Negation against EDB-only relation.
(dl-nt-test-set! "not against EDB"
(dl-query
(dl-program
"p(1). p(2). p(3). r(2).
q(X) :- p(X), not(r(X)).")
(list (quote q) (quote X)))
(list {:X 1} {:X 3}))
;; Negation against derived relation — needs stratification.
(dl-nt-test-set! "not against derived rel"
(dl-query
(dl-program
"p(1). p(2). p(3). s(2).
r(X) :- s(X).
q(X) :- p(X), not(r(X)).")
(list (quote q) (quote X)))
(list {:X 1} {:X 3}))
;; Two-step strata: r derives via s; q derives via not r.
(dl-nt-test-set! "two-step strata"
(dl-query
(dl-program
"node(a). node(b). node(c). node(d).
edge(a, b). edge(b, c). edge(c, a).
reach(X, Y) :- edge(X, Y).
reach(X, Z) :- edge(X, Y), reach(Y, Z).
unreachable(X) :- node(X), not(reach(a, X)).")
(list (quote unreachable) (quote X)))
(list {:X (quote d)}))
;; Combine negation with arithmetic and comparison.
(dl-nt-test-set! "negation with arithmetic"
(dl-query
(dl-program
"n(1). n(2). n(3). n(4). n(5). odd(1). odd(3). odd(5).
even(X) :- n(X), not(odd(X)).")
(list (quote even) (quote X)))
(list {:X 2} {:X 4}))
;; Empty negation result.
(dl-nt-test-set! "negation always succeeds"
(dl-query
(dl-program
"p(1). p(2). q(X) :- p(X), not(r(X)).")
(list (quote q) (quote X)))
(list {:X 1} {:X 2}))
;; Negation always fails.
(dl-nt-test-set! "negation always fails"
(dl-query
(dl-program
"p(1). p(2). r(1). r(2). q(X) :- p(X), not(r(X)).")
(list (quote q) (quote X)))
(list))
;; Anonymous `_` in a negated literal is existentially quantified
;; — it doesn't need to be bound by an earlier body lit. Without
;; this exemption the safety check would reject the common idiom
;; `orphan(X) :- person(X), not(parent(X, _))`.
(dl-nt-test-set! "negation with anonymous var — orphan idiom"
(dl-query
(dl-program
"person(a). person(b). person(c). parent(a, b).
orphan(X) :- person(X), not(parent(X, _)).")
(list (quote orphan) (quote X)))
(list {:X (quote b)} {:X (quote c)}))
;; Multiple anonymous vars are each independently existential.
(dl-nt-test-set! "negation with multiple anonymous vars"
(dl-query
(dl-program
"u(a). u(b). u(c). edge(a, x). edge(b, y).
solo(X) :- u(X), not(edge(X, _)).")
(list (quote solo) (quote X)))
(list {:X (quote c)}))
;; Stratifiability checks.
(dl-nt-test! "non-stratifiable rejected"
(dl-nt-throws?
(fn ()
(let ((db (dl-make-db)))
(do
(dl-add-rule!
db
{:head (list (quote p) (quote X))
:body (list (list (quote q) (quote X))
{:neg (list (quote r) (quote X))})})
(dl-add-rule!
db
{:head (list (quote r) (quote X))
:body (list (list (quote p) (quote X)))})
(dl-add-fact! db (list (quote q) 1))
(dl-saturate! db)))))
true)
(dl-nt-test! "stratifiable accepted"
(dl-nt-throws?
(fn ()
(dl-program
"p(1). p(2). r(2).
q(X) :- p(X), not(r(X)).")))
false)
;; Multi-stratum chain.
(dl-nt-test-set! "three-level strata"
(dl-query
(dl-program
"a(1). a(2). a(3). a(4).
b(X) :- a(X), not(c(X)).
c(X) :- d(X).
d(2).
d(4).")
(list (quote b) (quote X)))
(list {:X 1} {:X 3}))
;; Safety violation: negation refers to unbound var.
(dl-nt-test! "negation safety violation"
(dl-nt-throws?
(fn ()
(dl-program
"p(1). q(X) :- p(X), not(r(Y)).")))
true))))
(define
dl-negation-tests-run!
(fn
()
(do
(set! dl-nt-pass 0)
(set! dl-nt-fail 0)
(set! dl-nt-failures (list))
(dl-nt-run-all!)
{:passed dl-nt-pass
:failed dl-nt-fail
:total (+ dl-nt-pass dl-nt-fail)
:failures dl-nt-failures})))

View File

@@ -1,179 +0,0 @@
;; lib/datalog/tests/parse.sx — parser unit tests
;;
;; Run via: bash lib/datalog/conformance.sh
;; Or: (load "lib/datalog/tokenizer.sx") (load "lib/datalog/parser.sx")
;; (load "lib/datalog/tests/parse.sx") (dl-parse-tests-run!)
(define dl-pt-pass 0)
(define dl-pt-fail 0)
(define dl-pt-failures (list))
;; Order-independent structural equality. Lists compared positionally,
;; dicts as sets of (key, value) pairs. Numbers via = (so 30.0 = 30).
(define
dl-deep-equal?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-deep-equal-list? a b 0)))
((and (dict? a) (dict? b))
(let
((ka (keys a)) (kb (keys b)))
(and
(= (len ka) (len kb))
(dl-deep-equal-dict? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-deep-equal-list?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-deep-equal? (nth a i) (nth b i))) false)
(else (dl-deep-equal-list? a b (+ i 1))))))
(define
dl-deep-equal-dict?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i))) (not (dl-deep-equal? (get a k) (get b k))))
false)
(else (dl-deep-equal-dict? a b ka (+ i 1))))))
(define
dl-pt-test!
(fn
(name got expected)
(if
(dl-deep-equal? got expected)
(set! dl-pt-pass (+ dl-pt-pass 1))
(do
(set! dl-pt-fail (+ dl-pt-fail 1))
(append!
dl-pt-failures
(str name "\n expected: " expected "\n got: " got))))))
(define
dl-pt-throws?
(fn
(thunk)
(let
((threw false))
(do (guard (e (#t (set! threw true))) (thunk)) threw))))
(define
dl-pt-run-all!
(fn
()
(do
(dl-pt-test! "empty program" (dl-parse "") (list))
(dl-pt-test! "fact" (dl-parse "parent(tom, bob).") (list {:body (list) :head (list (quote parent) (quote tom) (quote bob))}))
(dl-pt-test!
"two facts"
(dl-parse "parent(tom, bob). parent(bob, ann).")
(list {:body (list) :head (list (quote parent) (quote tom) (quote bob))} {:body (list) :head (list (quote parent) (quote bob) (quote ann))}))
(dl-pt-test! "zero-ary fact" (dl-parse "ready.") (list {:body (list) :head (list (quote ready))}))
(dl-pt-test!
"rule one body lit"
(dl-parse "ancestor(X, Y) :- parent(X, Y).")
(list {:body (list (list (quote parent) (quote X) (quote Y))) :head (list (quote ancestor) (quote X) (quote Y))}))
(dl-pt-test!
"recursive rule"
(dl-parse "ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")
(list {:body (list (list (quote parent) (quote X) (quote Y)) (list (quote ancestor) (quote Y) (quote Z))) :head (list (quote ancestor) (quote X) (quote Z))}))
(dl-pt-test!
"query single"
(dl-parse "?- ancestor(tom, X).")
(list {:query (list (list (quote ancestor) (quote tom) (quote X)))}))
(dl-pt-test!
"query multi"
(dl-parse "?- p(X), q(X).")
(list {:query (list (list (quote p) (quote X)) (list (quote q) (quote X)))}))
(dl-pt-test!
"negation"
(dl-parse "safe(X) :- person(X), not(parent(X, _)).")
(list {:body (list (list (quote person) (quote X)) {:neg (list (quote parent) (quote X) (quote _))}) :head (list (quote safe) (quote X))}))
(dl-pt-test!
"number arg"
(dl-parse "age(alice, 30).")
(list {:body (list) :head (list (quote age) (quote alice) 30)}))
(dl-pt-test!
"string arg"
(dl-parse "label(x, \"hi\").")
(list {:body (list) :head (list (quote label) (quote x) "hi")}))
;; Quoted 'atoms' parse as strings — a uppercase-starting name
;; in quotes used to misclassify as a variable and reject the
;; fact as non-ground.
(dl-pt-test!
"quoted atom arg parses as string"
(dl-parse "p('Hello World').")
(list {:body (list) :head (list (quote p) "Hello World")}))
(dl-pt-test!
"comparison literal"
(dl-parse "p(X) :- <(X, 5).")
(list {:body (list (list (string->symbol "<") (quote X) 5)) :head (list (quote p) (quote X))}))
(dl-pt-test!
"is with arith"
(dl-parse "succ(X, Y) :- nat(X), is(Y, +(X, 1)).")
(list {:body (list (list (quote nat) (quote X)) (list (quote is) (quote Y) (list (string->symbol "+") (quote X) 1))) :head (list (quote succ) (quote X) (quote Y))}))
(dl-pt-test!
"mixed program"
(dl-parse "p(a). p(b). q(X) :- p(X). ?- q(Y).")
(list {:body (list) :head (list (quote p) (quote a))} {:body (list) :head (list (quote p) (quote b))} {:body (list (list (quote p) (quote X))) :head (list (quote q) (quote X))} {:query (list (list (quote q) (quote Y)))}))
(dl-pt-test!
"comments skipped"
(dl-parse "% comment\nfoo(a).\n/* block */ bar(b).")
(list {:body (list) :head (list (quote foo) (quote a))} {:body (list) :head (list (quote bar) (quote b))}))
(dl-pt-test!
"underscore var"
(dl-parse "p(X) :- q(X, _).")
(list {:body (list (list (quote q) (quote X) (quote _))) :head (list (quote p) (quote X))}))
;; Negative number literals parse as one negative number,
;; while subtraction (`-(X, Y)`) compound is preserved.
(dl-pt-test!
"negative integer literal"
(dl-parse "n(-3).")
(list {:head (list (quote n) -3) :body (list)}))
(dl-pt-test!
"subtraction compound preserved"
(dl-parse "r(X) :- is(X, -(10, 3)).")
(list
{:head (list (quote r) (quote X))
:body (list (list (quote is) (quote X)
(list (string->symbol "-") 10 3)))}))
(dl-pt-test!
"number as relation name raises"
(dl-pt-throws? (fn () (dl-parse "1(X) :- p(X).")))
true)
(dl-pt-test!
"var as relation name raises"
(dl-pt-throws? (fn () (dl-parse "P(X).")))
true)
(dl-pt-test!
"missing dot raises"
(dl-pt-throws? (fn () (dl-parse "p(a)")))
true)
(dl-pt-test!
"trailing comma raises"
(dl-pt-throws? (fn () (dl-parse "p(a,).")))
true))))
(define
dl-parse-tests-run!
(fn
()
(do
(set! dl-pt-pass 0)
(set! dl-pt-fail 0)
(set! dl-pt-failures (list))
(dl-pt-run-all!)
{:failures dl-pt-failures :total (+ dl-pt-pass dl-pt-fail) :passed dl-pt-pass :failed dl-pt-fail})))

View File

@@ -1,153 +0,0 @@
;; lib/datalog/tests/semi_naive.sx — semi-naive correctness vs naive.
;;
;; Strategy: differential — run both saturators on each program and
;; compare the resulting per-relation tuple counts. Counting (not
;; element-wise set equality) keeps the suite fast under the bundled
;; conformance session; correctness on the inhabitants is covered by
;; eval.sx and builtins.sx (which use dl-saturate! by default — the
;; semi-naive saturator).
(define dl-sn-pass 0)
(define dl-sn-fail 0)
(define dl-sn-failures (list))
(define
dl-sn-test!
(fn
(name got expected)
(if
(equal? got expected)
(set! dl-sn-pass (+ dl-sn-pass 1))
(do
(set! dl-sn-fail (+ dl-sn-fail 1))
(append!
dl-sn-failures
(str name "\n expected: " expected "\n got: " got))))))
;; Load `source` into both a semi-naive and a naive db and return a
;; list of (rel-name semi-count naive-count) triples. Both sets must
;; have the same union of relation names.
(define
dl-sn-counts
(fn
(source)
(let
((db-s (dl-program source)) (db-n (dl-program source)))
(do
(dl-saturate! db-s)
(dl-saturate-naive! db-n)
(let
((out (list)))
(do
(for-each
(fn
(k)
(append!
out
(list
k
(len (dl-relation db-s k))
(len (dl-relation db-n k)))))
(keys (get db-s :facts)))
out))))))
(define
dl-sn-counts-agree?
(fn
(counts)
(cond
((= (len counts) 0) true)
(else
(let
((row (first counts)))
(and
(= (nth row 1) (nth row 2))
(dl-sn-counts-agree? (rest counts))))))))
(define
dl-sn-chain-source
(fn
(n)
(let
((parts (list "")))
(do
(define
dl-sn-loop
(fn
(i)
(when
(< i n)
(do
(append! parts (str "parent(" i ", " (+ i 1) "). "))
(dl-sn-loop (+ i 1))))))
(dl-sn-loop 0)
(str
(join "" parts)
"ancestor(X, Y) :- parent(X, Y). "
"ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))))
(define
dl-sn-run-all!
(fn
()
(do
(dl-sn-test!
"ancestor closure counts match"
(dl-sn-counts-agree?
(dl-sn-counts
"parent(a, b). parent(b, c). parent(c, d).\n ancestor(X, Y) :- parent(X, Y).\n ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)."))
true)
(dl-sn-test!
"cyclic reach counts match"
(dl-sn-counts-agree?
(dl-sn-counts
"edge(1, 2). edge(2, 3). edge(3, 1). edge(3, 4).\n reach(X, Y) :- edge(X, Y).\n reach(X, Z) :- edge(X, Y), reach(Y, Z)."))
true)
(dl-sn-test!
"same-gen counts match"
(dl-sn-counts-agree?
(dl-sn-counts
"parent(a, b). parent(a, c). parent(b, d). parent(c, e).\n person(a). person(b). person(c). person(d). person(e).\n sg(X, X) :- person(X).\n sg(X, Y) :- parent(P1, X), sg(P1, P2), parent(P2, Y)."))
true)
(dl-sn-test!
"rules with builtins counts match"
(dl-sn-counts-agree?
(dl-sn-counts
"n(1). n(2). n(3). n(4). n(5).\n small(X) :- n(X), <(X, 5).\n succ(X, Y) :- n(X), <(X, 5), is(Y, +(X, 1))."))
true)
(dl-sn-test!
"static rule fires under semi-naive"
(dl-sn-counts-agree?
(dl-sn-counts "p(a). p(b). q(X) :- p(X), =(X, a)."))
true)
;; Chain length 12 — multiple semi-naive iterations against
;; the recursive ancestor rule (differential vs naive).
(dl-sn-test!
"chain-12 ancestor counts match"
(dl-sn-counts-agree? (dl-sn-counts (dl-sn-chain-source 12)))
true)
;; Chain length 25 — semi-naive only — first-arg index makes
;; this tractable in conformance budget.
(dl-sn-test!
"chain-25 ancestor count value (semi only)"
(let
((db (dl-program (dl-sn-chain-source 25))))
(do (dl-saturate! db) (len (dl-relation db "ancestor"))))
325)
(dl-sn-test!
"query through semi saturate"
(let
((db (dl-program "parent(a, b). parent(b, c).\n ancestor(X, Y) :- parent(X, Y).\n ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).")))
(len (dl-query db (list (quote ancestor) (quote a) (quote X)))))
2))))
(define
dl-semi-naive-tests-run!
(fn
()
(do
(set! dl-sn-pass 0)
(set! dl-sn-fail 0)
(set! dl-sn-failures (list))
(dl-sn-run-all!)
{:failures dl-sn-failures :total (+ dl-sn-pass dl-sn-fail) :passed dl-sn-pass :failed dl-sn-fail})))

View File

@@ -1,189 +0,0 @@
;; lib/datalog/tests/tokenize.sx — tokenizer unit tests
;;
;; Run via: bash lib/datalog/conformance.sh
;; Or: (load "lib/datalog/tokenizer.sx") (load "lib/datalog/tests/tokenize.sx")
;; (dl-tokenize-tests-run!)
(define dl-tk-pass 0)
(define dl-tk-fail 0)
(define dl-tk-failures (list))
(define
dl-tk-test!
(fn
(name got expected)
(if
(= got expected)
(set! dl-tk-pass (+ dl-tk-pass 1))
(do
(set! dl-tk-fail (+ dl-tk-fail 1))
(append!
dl-tk-failures
(str name "\n expected: " expected "\n got: " got))))))
(define dl-tk-types (fn (toks) (map (fn (t) (get t :type)) toks)))
(define dl-tk-values (fn (toks) (map (fn (t) (get t :value)) toks)))
(define
dl-tk-run-all!
(fn
()
(do
(dl-tk-test! "empty" (dl-tk-types (dl-tokenize "")) (list "eof"))
(dl-tk-test!
"atom dot"
(dl-tk-types (dl-tokenize "foo."))
(list "atom" "punct" "eof"))
(dl-tk-test!
"atom dot value"
(dl-tk-values (dl-tokenize "foo."))
(list "foo" "." nil))
(dl-tk-test!
"var"
(dl-tk-types (dl-tokenize "X."))
(list "var" "punct" "eof"))
(dl-tk-test!
"underscore var"
(dl-tk-types (dl-tokenize "_x."))
(list "var" "punct" "eof"))
(dl-tk-test!
"integer"
(dl-tk-values (dl-tokenize "42"))
(list 42 nil))
(dl-tk-test!
"decimal"
(dl-tk-values (dl-tokenize "3.14"))
(list 3.14 nil))
(dl-tk-test!
"string"
(dl-tk-values (dl-tokenize "\"hello\""))
(list "hello" nil))
;; Quoted 'atoms' tokenize as strings — see the type-table
;; comment in lib/datalog/tokenizer.sx for the rationale.
(dl-tk-test!
"quoted atom as string"
(dl-tk-types (dl-tokenize "'two words'"))
(list "string" "eof"))
(dl-tk-test!
"quoted atom value"
(dl-tk-values (dl-tokenize "'two words'"))
(list "two words" nil))
;; A quoted atom whose name would otherwise be a variable
;; (uppercase / leading underscore) is now safely a string —
;; this was the bug that motivated the type change.
(dl-tk-test!
"quoted Uppercase as string"
(dl-tk-types (dl-tokenize "'Hello'"))
(list "string" "eof"))
(dl-tk-test! ":-" (dl-tk-values (dl-tokenize ":-")) (list ":-" nil))
(dl-tk-test! "?-" (dl-tk-values (dl-tokenize "?-")) (list "?-" nil))
(dl-tk-test! "<=" (dl-tk-values (dl-tokenize "<=")) (list "<=" nil))
(dl-tk-test! ">=" (dl-tk-values (dl-tokenize ">=")) (list ">=" nil))
(dl-tk-test! "!=" (dl-tk-values (dl-tokenize "!=")) (list "!=" nil))
(dl-tk-test!
"single op values"
(dl-tk-values (dl-tokenize "< > = + - * /"))
(list "<" ">" "=" "+" "-" "*" "/" nil))
(dl-tk-test!
"single op types"
(dl-tk-types (dl-tokenize "< > = + - * /"))
(list "op" "op" "op" "op" "op" "op" "op" "eof"))
(dl-tk-test!
"punct"
(dl-tk-values (dl-tokenize "( ) , ."))
(list "(" ")" "," "." nil))
(dl-tk-test!
"fact tokens"
(dl-tk-types (dl-tokenize "parent(tom, bob)."))
(list "atom" "punct" "atom" "punct" "atom" "punct" "punct" "eof"))
(dl-tk-test!
"rule shape"
(dl-tk-types (dl-tokenize "p(X) :- q(X)."))
(list
"atom"
"punct"
"var"
"punct"
"op"
"atom"
"punct"
"var"
"punct"
"punct"
"eof"))
(dl-tk-test!
"comparison literal"
(dl-tk-values (dl-tokenize "<(X, 5)"))
(list "<" "(" "X" "," 5 ")" nil))
(dl-tk-test!
"is form"
(dl-tk-values (dl-tokenize "is(Y, +(X, 1))"))
(list "is" "(" "Y" "," "+" "(" "X" "," 1 ")" ")" nil))
(dl-tk-test!
"line comment"
(dl-tk-types (dl-tokenize "% comment line\nfoo."))
(list "atom" "punct" "eof"))
(dl-tk-test!
"block comment"
(dl-tk-types (dl-tokenize "/* a\nb */ x."))
(list "atom" "punct" "eof"))
;; Unexpected characters surface at tokenize time rather
;; than being silently consumed (previously `?(X)` parsed as
;; if the leading `?` weren't there).
(dl-tk-test!
"unexpected char raises"
(let ((threw false))
(do
(guard (e (#t (set! threw true)))
(dl-tokenize "?(X)"))
threw))
true)
;; Unterminated string / quoted-atom must raise.
(dl-tk-test!
"unterminated string raises"
(let ((threw false))
(do
(guard (e (#t (set! threw true)))
(dl-tokenize "\"unclosed"))
threw))
true)
(dl-tk-test!
"unterminated quoted atom raises"
(let ((threw false))
(do
(guard (e (#t (set! threw true)))
(dl-tokenize "'unclosed"))
threw))
true)
;; Unterminated block comment must raise — previously it was
;; silently consumed to EOF.
(dl-tk-test!
"unterminated block comment raises"
(let ((threw false))
(do
(guard (e (#t (set! threw true)))
(dl-tokenize "/* unclosed comment"))
threw))
true)
(dl-tk-test!
"whitespace"
(dl-tk-types (dl-tokenize " foo ,\t bar ."))
(list "atom" "punct" "atom" "punct" "eof"))
(dl-tk-test!
"positions"
(map (fn (t) (get t :pos)) (dl-tokenize "foo bar"))
(list 0 4 7)))))
(define
dl-tokenize-tests-run!
(fn
()
(do
(set! dl-tk-pass 0)
(set! dl-tk-fail 0)
(set! dl-tk-failures (list))
(dl-tk-run-all!)
{:failures dl-tk-failures :total (+ dl-tk-pass dl-tk-fail) :passed dl-tk-pass :failed dl-tk-fail})))

View File

@@ -1,194 +0,0 @@
;; lib/datalog/tests/unify.sx — unification + substitution tests.
(define dl-ut-pass 0)
(define dl-ut-fail 0)
(define dl-ut-failures (list))
(define
dl-ut-deep-equal?
(fn
(a b)
(cond
((and (list? a) (list? b))
(and (= (len a) (len b)) (dl-ut-deq-list? a b 0)))
((and (dict? a) (dict? b))
(let
((ka (keys a)) (kb (keys b)))
(and (= (len ka) (len kb)) (dl-ut-deq-dict? a b ka 0))))
((and (number? a) (number? b)) (= a b))
(else (equal? a b)))))
(define
dl-ut-deq-list?
(fn
(a b i)
(cond
((>= i (len a)) true)
((not (dl-ut-deep-equal? (nth a i) (nth b i))) false)
(else (dl-ut-deq-list? a b (+ i 1))))))
(define
dl-ut-deq-dict?
(fn
(a b ka i)
(cond
((>= i (len ka)) true)
((let ((k (nth ka i))) (not (dl-ut-deep-equal? (get a k) (get b k))))
false)
(else (dl-ut-deq-dict? a b ka (+ i 1))))))
(define
dl-ut-test!
(fn
(name got expected)
(if
(dl-ut-deep-equal? got expected)
(set! dl-ut-pass (+ dl-ut-pass 1))
(do
(set! dl-ut-fail (+ dl-ut-fail 1))
(append!
dl-ut-failures
(str name "\n expected: " expected "\n got: " got))))))
(define
dl-ut-run-all!
(fn
()
(do
(dl-ut-test! "var? uppercase" (dl-var? (quote X)) true)
(dl-ut-test! "var? underscore" (dl-var? (quote _foo)) true)
(dl-ut-test! "var? lowercase" (dl-var? (quote tom)) false)
(dl-ut-test! "var? number" (dl-var? 5) false)
(dl-ut-test! "var? string" (dl-var? "hi") false)
(dl-ut-test! "var? list" (dl-var? (list 1)) false)
(dl-ut-test!
"atom-atom match"
(dl-unify (quote tom) (quote tom) (dl-empty-subst))
{})
(dl-ut-test!
"atom-atom fail"
(dl-unify (quote tom) (quote bob) (dl-empty-subst))
nil)
(dl-ut-test!
"num-num match"
(dl-unify 5 5 (dl-empty-subst))
{})
(dl-ut-test!
"num-num fail"
(dl-unify 5 6 (dl-empty-subst))
nil)
(dl-ut-test!
"string match"
(dl-unify "hi" "hi" (dl-empty-subst))
{})
(dl-ut-test! "string fail" (dl-unify "hi" "bye" (dl-empty-subst)) nil)
(dl-ut-test!
"var-atom binds"
(dl-unify (quote X) (quote tom) (dl-empty-subst))
{:X (quote tom)})
(dl-ut-test!
"atom-var binds"
(dl-unify (quote tom) (quote X) (dl-empty-subst))
{:X (quote tom)})
(dl-ut-test!
"var-var same"
(dl-unify (quote X) (quote X) (dl-empty-subst))
{})
(dl-ut-test!
"var-var bind"
(let
((s (dl-unify (quote X) (quote Y) (dl-empty-subst))))
(dl-walk (quote X) s))
(quote Y))
(dl-ut-test!
"tuple match"
(dl-unify
(list (quote parent) (quote X) (quote bob))
(list (quote parent) (quote tom) (quote Y))
(dl-empty-subst))
{:X (quote tom) :Y (quote bob)})
(dl-ut-test!
"tuple arity mismatch"
(dl-unify
(list (quote p) (quote X))
(list (quote p) (quote a) (quote b))
(dl-empty-subst))
nil)
(dl-ut-test!
"tuple head mismatch"
(dl-unify
(list (quote p) (quote X))
(list (quote q) (quote X))
(dl-empty-subst))
nil)
(dl-ut-test!
"walk chain"
(let
((s1 (dl-unify (quote X) (quote Y) (dl-empty-subst))))
(let
((s2 (dl-unify (quote Y) (quote tom) s1)))
(dl-walk (quote X) s2)))
(quote tom))
;; Walk with circular substitution must not infinite-loop.
;; Cycles return the current term unchanged.
(dl-ut-test!
"walk circular subst no hang"
(let ((s (dl-bind (quote B) (quote A)
(dl-bind (quote A) (quote B) (dl-empty-subst)))))
(dl-walk (quote A) s))
(quote A))
(dl-ut-test!
"apply subst on tuple"
(let
((s (dl-bind (quote X) (quote tom) (dl-empty-subst))))
(dl-apply-subst (list (quote parent) (quote X) (quote Y)) s))
(list (quote parent) (quote tom) (quote Y)))
(dl-ut-test!
"ground? all const"
(dl-ground?
(list (quote p) (quote tom) 5)
(dl-empty-subst))
true)
(dl-ut-test!
"ground? unbound var"
(dl-ground? (list (quote p) (quote X)) (dl-empty-subst))
false)
(dl-ut-test!
"ground? bound var"
(let
((s (dl-bind (quote X) (quote tom) (dl-empty-subst))))
(dl-ground? (list (quote p) (quote X)) s))
true)
(dl-ut-test!
"ground? bare var"
(dl-ground? (quote X) (dl-empty-subst))
false)
(dl-ut-test!
"vars-of basic"
(dl-vars-of
(list (quote p) (quote X) (quote tom) (quote Y) (quote X)))
(list "X" "Y"))
(dl-ut-test!
"vars-of ground"
(dl-vars-of (list (quote p) (quote tom) (quote bob)))
(list))
(dl-ut-test!
"vars-of nested compound"
(dl-vars-of
(list
(quote is)
(quote Z)
(list (string->symbol "+") (quote X) 1)))
(list "Z" "X")))))
(define
dl-unify-tests-run!
(fn
()
(do
(set! dl-ut-pass 0)
(set! dl-ut-fail 0)
(set! dl-ut-failures (list))
(dl-ut-run-all!)
{:failures dl-ut-failures :total (+ dl-ut-pass dl-ut-fail) :passed dl-ut-pass :failed dl-ut-fail})))

View File

@@ -1,269 +0,0 @@
;; lib/datalog/tokenizer.sx — Datalog source → token stream
;;
;; Tokens: {:type T :value V :pos P}
;; Types:
;; "atom" — lowercase-start bare identifier
;; "var" — uppercase-start or _-start ident (value is the name)
;; "number" — numeric literal (decoded to number)
;; "string" — "..." string literal OR quoted 'atom' (treated as a
;; string value to avoid the var-vs-atom ambiguity that
;; would arise from a quoted atom whose name starts with
;; an uppercase letter or underscore)
;; "punct" — ( ) , .
;; "op" — :- ?- <= >= != < > = + - * /
;; "eof"
;;
;; Datalog has no function symbols in arg position; the parser still
;; accepts nested compounds for arithmetic ((is X (+ A B))) but safety
;; analysis rejects non-arithmetic nesting at rule-load time.
(define dl-make-token (fn (type value pos) {:type type :value value :pos pos}))
(define dl-digit? (fn (c) (and (>= c "0") (<= c "9"))))
(define dl-lower? (fn (c) (and (>= c "a") (<= c "z"))))
(define dl-upper? (fn (c) (and (>= c "A") (<= c "Z"))))
(define
dl-ident-char?
(fn (c) (or (dl-lower? c) (dl-upper? c) (dl-digit? c) (= c "_"))))
(define dl-ws? (fn (c) (or (= c " ") (= c "\t") (= c "\n") (= c "\r"))))
(define
dl-tokenize
(fn
(src)
(let
((tokens (list)) (pos 0) (src-len (len src)))
(define
dl-peek
(fn
(offset)
(if (< (+ pos offset) src-len) (nth src (+ pos offset)) nil)))
(define cur (fn () (dl-peek 0)))
(define advance! (fn (n) (set! pos (+ pos n))))
(define
at?
(fn
(s)
(let
((sl (len s)))
(and (<= (+ pos sl) src-len) (= (slice src pos (+ pos sl)) s)))))
(define
dl-emit!
(fn
(type value start)
(append! tokens (dl-make-token type value start))))
(define
skip-line-comment!
(fn
()
(when
(and (< pos src-len) (not (= (cur) "\n")))
(do (advance! 1) (skip-line-comment!)))))
(define
skip-block-comment!
(fn
()
(cond
((>= pos src-len)
(error (str "Tokenizer: unterminated block comment "
"(started at position " pos ")")))
((and (= (cur) "*") (< (+ pos 1) src-len) (= (dl-peek 1) "/"))
(advance! 2))
(else (do (advance! 1) (skip-block-comment!))))))
(define
skip-ws!
(fn
()
(cond
((>= pos src-len) nil)
((dl-ws? (cur)) (do (advance! 1) (skip-ws!)))
((= (cur) "%")
(do (advance! 1) (skip-line-comment!) (skip-ws!)))
((and (= (cur) "/") (< (+ pos 1) src-len) (= (dl-peek 1) "*"))
(do (advance! 2) (skip-block-comment!) (skip-ws!)))
(else nil))))
(define
read-ident
(fn
(start)
(do
(when
(and (< pos src-len) (dl-ident-char? (cur)))
(do (advance! 1) (read-ident start)))
(slice src start pos))))
(define
read-decimal-digits!
(fn
()
(when
(and (< pos src-len) (dl-digit? (cur)))
(do (advance! 1) (read-decimal-digits!)))))
(define
read-number
(fn
(start)
(do
(read-decimal-digits!)
(when
(and
(< pos src-len)
(= (cur) ".")
(< (+ pos 1) src-len)
(dl-digit? (dl-peek 1)))
(do (advance! 1) (read-decimal-digits!)))
(parse-number (slice src start pos)))))
(define
read-quoted
(fn
(quote-char)
(let
((chars (list)))
(advance! 1)
(define
loop
(fn
()
(cond
((>= pos src-len)
(error
(str "Tokenizer: unterminated "
(if (= quote-char "'") "quoted atom" "string")
" (started near position " pos ")")))
((= (cur) "\\")
(do
(advance! 1)
(when
(< pos src-len)
(let
((ch (cur)))
(do
(cond
((= ch "n") (append! chars "\n"))
((= ch "t") (append! chars "\t"))
((= ch "r") (append! chars "\r"))
((= ch "\\") (append! chars "\\"))
((= ch "'") (append! chars "'"))
((= ch "\"") (append! chars "\""))
(else (append! chars ch)))
(advance! 1))))
(loop)))
((= (cur) quote-char) (advance! 1))
(else
(do (append! chars (cur)) (advance! 1) (loop))))))
(loop)
(join "" chars))))
(define
scan!
(fn
()
(do
(skip-ws!)
(when
(< pos src-len)
(let
((ch (cur)) (start pos))
(cond
((at? ":-")
(do
(dl-emit! "op" ":-" start)
(advance! 2)
(scan!)))
((at? "?-")
(do
(dl-emit! "op" "?-" start)
(advance! 2)
(scan!)))
((at? "<=")
(do
(dl-emit! "op" "<=" start)
(advance! 2)
(scan!)))
((at? ">=")
(do
(dl-emit! "op" ">=" start)
(advance! 2)
(scan!)))
((at? "!=")
(do
(dl-emit! "op" "!=" start)
(advance! 2)
(scan!)))
((dl-digit? ch)
(do
(dl-emit! "number" (read-number start) start)
(scan!)))
((= ch "'")
;; Quoted 'atoms' tokenize as strings so a name
;; like 'Hello World' doesn't get misclassified
;; as a variable by dl-var? (which inspects the
;; symbol's first character).
(do (dl-emit! "string" (read-quoted "'") start) (scan!)))
((= ch "\"")
(do (dl-emit! "string" (read-quoted "\"") start) (scan!)))
((dl-lower? ch)
(do (dl-emit! "atom" (read-ident start) start) (scan!)))
((or (dl-upper? ch) (= ch "_"))
(do (dl-emit! "var" (read-ident start) start) (scan!)))
((= ch "(")
(do
(dl-emit! "punct" "(" start)
(advance! 1)
(scan!)))
((= ch ")")
(do
(dl-emit! "punct" ")" start)
(advance! 1)
(scan!)))
((= ch ",")
(do
(dl-emit! "punct" "," start)
(advance! 1)
(scan!)))
((= ch ".")
(do
(dl-emit! "punct" "." start)
(advance! 1)
(scan!)))
((= ch "<")
(do
(dl-emit! "op" "<" start)
(advance! 1)
(scan!)))
((= ch ">")
(do
(dl-emit! "op" ">" start)
(advance! 1)
(scan!)))
((= ch "=")
(do
(dl-emit! "op" "=" start)
(advance! 1)
(scan!)))
((= ch "+")
(do
(dl-emit! "op" "+" start)
(advance! 1)
(scan!)))
((= ch "-")
(do
(dl-emit! "op" "-" start)
(advance! 1)
(scan!)))
((= ch "*")
(do
(dl-emit! "op" "*" start)
(advance! 1)
(scan!)))
((= ch "/")
(do
(dl-emit! "op" "/" start)
(advance! 1)
(scan!)))
(else (error
(str "Tokenizer: unexpected character '" ch
"' at position " start)))))))))
(scan!)
(dl-emit! "eof" nil pos)
tokens)))

View File

@@ -1,171 +0,0 @@
;; lib/datalog/unify.sx — unification + substitution for Datalog terms.
;;
;; Term taxonomy (after parsing):
;; variable — SX symbol whose first char is uppercase AZ or '_'.
;; constant — SX symbol whose first char is lowercase az (atom name).
;; number — numeric literal.
;; string — string literal.
;; compound — SX list (functor arg ... arg). In core Datalog these
;; only appear as arithmetic expressions (see Phase 4
;; safety analysis); compound-against-compound unification
;; is supported anyway for completeness.
;;
;; Substitutions are immutable dicts keyed by variable name (string).
;; A failed unification returns nil; success returns the extended subst.
(define dl-empty-subst (fn () {}))
(define
dl-var?
(fn
(term)
(and
(symbol? term)
(let
((name (symbol->string term)))
(and
(> (len name) 0)
(let
((c (slice name 0 1)))
(or (and (>= c "A") (<= c "Z")) (= c "_"))))))))
;; Walk: chase variable bindings until we hit a non-variable or an unbound
;; variable. The result is either a non-variable term or an unbound var.
(define
dl-walk
(fn (term subst) (dl-walk-aux term subst (list))))
;; Internal: walk with a visited-var set so circular substitutions
;; (from raw dl-bind misuse) don't infinite-loop. Cycles return the
;; current term unchanged.
(define
dl-walk-aux
(fn
(term subst visited)
(if
(dl-var? term)
(let
((name (symbol->string term)))
(cond
((dl-member? name visited) term)
((and (dict? subst) (has-key? subst name))
(let ((seen (list)))
(do
(for-each (fn (v) (append! seen v)) visited)
(append! seen name)
(dl-walk-aux (get subst name) subst seen))))
(else term)))
term)))
;; Bind a variable symbol to a value in subst, returning a new subst.
(define
dl-bind
(fn (var-sym value subst) (assoc subst (symbol->string var-sym) value)))
(define
dl-unify
(fn
(t1 t2 subst)
(if
(nil? subst)
nil
(let
((u1 (dl-walk t1 subst)) (u2 (dl-walk t2 subst)))
(cond
((dl-var? u1)
(cond
((and (dl-var? u2) (= (symbol->string u1) (symbol->string u2)))
subst)
(else (dl-bind u1 u2 subst))))
((dl-var? u2) (dl-bind u2 u1 subst))
((and (list? u1) (list? u2))
(if
(= (len u1) (len u2))
(dl-unify-list u1 u2 subst 0)
nil))
((and (number? u1) (number? u2)) (if (= u1 u2) subst nil))
((and (string? u1) (string? u2)) (if (= u1 u2) subst nil))
((and (symbol? u1) (symbol? u2))
(if (= (symbol->string u1) (symbol->string u2)) subst nil))
(else nil))))))
(define
dl-unify-list
(fn
(a b subst i)
(cond
((nil? subst) nil)
((>= i (len a)) subst)
(else
(dl-unify-list
a
b
(dl-unify (nth a i) (nth b i) subst)
(+ i 1))))))
;; Apply substitution: walk the term and recurse into lists.
(define
dl-apply-subst
(fn
(term subst)
(let
((w (dl-walk term subst)))
(if (list? w) (map (fn (x) (dl-apply-subst x subst)) w) w))))
;; Ground? — true iff no free variables remain after walking.
(define
dl-ground?
(fn
(term subst)
(let
((w (dl-walk term subst)))
(cond
((dl-var? w) false)
((list? w) (dl-ground-list? w subst 0))
(else true)))))
(define
dl-ground-list?
(fn
(xs subst i)
(cond
((>= i (len xs)) true)
((not (dl-ground? (nth xs i) subst)) false)
(else (dl-ground-list? xs subst (+ i 1))))))
;; Return the list of variable names appearing in a term (deduped, in
;; left-to-right order). Useful for safety analysis later.
(define
dl-vars-of
(fn (term) (let ((seen (list))) (do (dl-vars-of-aux term seen) seen))))
(define
dl-vars-of-aux
(fn
(term acc)
(cond
((dl-var? term)
(let
((name (symbol->string term)))
(when (not (dl-member? name acc)) (append! acc name))))
((list? term) (dl-vars-of-list term acc 0))
(else nil))))
(define
dl-vars-of-list
(fn
(xs acc i)
(when
(< i (len xs))
(do
(dl-vars-of-aux (nth xs i) acc)
(dl-vars-of-list xs acc (+ i 1))))))
(define
dl-member?
(fn
(x xs)
(cond
((= (len xs) 0) false)
((= (first xs) x) true)
(else (dl-member? x (rest xs))))))

View File

@@ -0,0 +1,23 @@
let div_sum n =
let s = ref 1 in
let i = ref 2 in
while !i * !i <= n do
if n mod !i = 0 then begin
s := !s + !i;
let q = n / !i in
if q <> !i then s := !s + q
end;
i := !i + 1
done;
if n = 1 then 0 else !s
let count_abundant n =
let c = ref 0 in
for i = 12 to n - 1 do
if div_sum i > i then c := !c + 1
done;
!c
;;
count_abundant 100

View File

@@ -0,0 +1,8 @@
let rec ack m n =
if m = 0 then n + 1
else if n = 0 then ack (m - 1) 1
else ack (m - 1) (ack m (n - 1))
;;
ack 3 4

View File

@@ -0,0 +1,31 @@
let max_nonoverlap intervals =
let arr = Array.of_list intervals in
let n = Array.length arr in
let sorted = Array.make n (0, 0) in
for i = 0 to n - 1 do
sorted.(i) <- arr.(i)
done;
for i = 0 to n - 1 do
for j = 0 to n - 2 - i do
let (_, e1) = sorted.(j) in
let (_, e2) = sorted.(j + 1) in
if e1 > e2 then begin
let t = sorted.(j) in
sorted.(j) <- sorted.(j + 1);
sorted.(j + 1) <- t
end
done
done;
let count = ref 0 in
let last_end = ref (-1000000) in
for i = 0 to n - 1 do
let (s, e) = sorted.(i) in
if s >= !last_end then begin
count := !count + 1;
last_end := e
end
done;
!count
;;
max_nonoverlap [(1, 4); (3, 5); (0, 6); (5, 7); (3, 8); (5, 9); (6, 10); (8, 11); (8, 12); (2, 13); (12, 14)]

View File

@@ -0,0 +1,13 @@
let adler32 s =
let a = ref 1 in
let b = ref 0 in
let m = 65521 in
for i = 0 to String.length s - 1 do
a := (!a + Char.code s.[i]) mod m;
b := (!b + !a) mod m
done;
!b * 65536 + !a
;;
adler32 "Wikipedia"

View File

@@ -0,0 +1,23 @@
let to_counts s =
let counts = Array.make 256 0 in
for i = 0 to String.length s - 1 do
let c = Char.code s.[i] in
counts.(c) <- counts.(c) + 1
done;
counts
let same_counts a b =
let result = ref true in
for i = 0 to 255 do
if a.(i) <> b.(i) then result := false
done;
!result
let is_anagram s t = same_counts (to_counts s) (to_counts t)
;;
(if is_anagram "listen" "silent" then 1 else 0) +
(if is_anagram "hello" "world" then 1 else 0) +
(if is_anagram "anagram" "nagaram" then 1 else 0) +
(if is_anagram "abc" "abcd" then 1 else 0)

View File

@@ -0,0 +1,29 @@
let canonical s =
let chars = Array.make 26 0 in
for i = 0 to String.length s - 1 do
let k = Char.code s.[i] - Char.code 'a' in
if k >= 0 && k < 26 then chars.(k) <- chars.(k) + 1
done;
let buf = Buffer.create 26 in
for i = 0 to 25 do
for _ = 1 to chars.(i) do
Buffer.add_string buf (String.make 1 (Char.chr (i + Char.code 'a')))
done
done;
Buffer.contents buf
let group_anagrams xs =
let h = Hashtbl.create 8 in
List.iter (fun s ->
let k = canonical s in
let cur = match Hashtbl.find_opt h k with
| Some xs -> xs
| None -> []
in
Hashtbl.replace h k (s :: cur)
) xs;
Hashtbl.length h
;;
group_anagrams ["eat"; "tea"; "tan"; "ate"; "nat"; "bat"]

View File

@@ -0,0 +1,26 @@
(* Baseline: count anagram groups using Hashtbl + sort *)
(* Sort the chars in a string to get its anagram-equivalence key *)
let canonical s =
let n = String.length s in
let chars = ref [] in
for i = 0 to n - 1 do
chars := (String.get s i) :: !chars
done ;
let sorted = List.sort compare !chars in
String.concat "" sorted
;;
let count_groups words =
let counts = Hashtbl.create 16 in
List.iter
(fun w ->
let k = canonical w in
match Hashtbl.find_opt counts k with
| None -> Hashtbl.add counts k 1
| Some n -> Hashtbl.replace counts k (n + 1))
words ;
Hashtbl.length counts
;;
count_groups ["eat"; "tea"; "tan"; "ate"; "nat"; "bat"]

17
lib/ocaml/baseline/atm.ml Normal file
View File

@@ -0,0 +1,17 @@
type account = { mutable balance : int }
exception Insufficient
let withdraw acct amt =
if amt > acct.balance then raise Insufficient
else acct.balance <- acct.balance - amt
let deposit acct amt = acct.balance <- acct.balance + amt
;;
let a = { balance = 100 } in
deposit a 50;
withdraw a 30;
try (withdraw a 200; -1)
with Insufficient -> a.balance

18
lib/ocaml/baseline/bag.ml Normal file
View File

@@ -0,0 +1,18 @@
let count_words text =
let words = String.split_on_char ' ' text in
let counts = Hashtbl.create 8 in
List.iter (fun w ->
let n = match Hashtbl.find_opt counts w with
| Some n -> n + 1
| None -> 1
in
Hashtbl.replace counts w n
) words;
counts
let max_count counts =
Hashtbl.fold (fun _ v acc -> if v > acc then v else acc) counts 0
;;
max_count (count_words "the quick brown fox jumps over the lazy dog the fox")

View File

@@ -0,0 +1,25 @@
let is_balanced s =
let stack = Stack.create () in
let n = String.length s in
let ok = ref true in
let i = ref 0 in
while !i < n && !ok do
let c = s.[!i] in
(if c = '(' || c = '[' || c = '{' then Stack.push c stack
else if c = ')' then
(if Stack.is_empty stack || Stack.pop stack <> '(' then ok := false)
else if c = ']' then
(if Stack.is_empty stack || Stack.pop stack <> '[' then ok := false)
else if c = '}' then
(if Stack.is_empty stack || Stack.pop stack <> '{' then ok := false));
i := !i + 1
done;
!ok && Stack.is_empty stack
;;
(if is_balanced "({[abc]d}e)" then 1 else 0) +
(if is_balanced "(a]" then 1 else 0) +
(if is_balanced "{[}]" then 1 else 0) +
(if is_balanced "(())" then 1 else 0) +
(if is_balanced "" then 1 else 0)

View File

@@ -0,0 +1,19 @@
let to_base_n n base =
let digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" in
if n = 0 then "0"
else begin
let m = ref (abs n) in
let acc = ref "" in
while !m > 0 do
acc := String.make 1 digits.[!m mod base] ^ !acc;
m := !m / base
done;
if n < 0 then "-" ^ !acc else !acc
end
;;
String.length (to_base_n 255 16) +
String.length (to_base_n 1024 2) +
String.length (to_base_n 100 10) +
String.length (to_base_n 0 16)

View File

@@ -0,0 +1,42 @@
let interpret prog =
let mem = Array.make 256 0 in
let ptr = ref 0 in
let pc = ref 0 in
let n = String.length prog in
let acc = ref 0 in
while !pc < n do
let c = prog.[!pc] in
(if c = '>' then ptr := !ptr + 1
else if c = '<' then ptr := !ptr - 1
else if c = '+' then mem.(!ptr) <- mem.(!ptr) + 1
else if c = '-' then mem.(!ptr) <- mem.(!ptr) - 1
else if c = '.' then acc := !acc + mem.(!ptr)
else if c = '[' then begin
if mem.(!ptr) = 0 then begin
let depth = ref 1 in
while !depth > 0 do
pc := !pc + 1;
let c = prog.[!pc] in
if c = '[' then depth := !depth + 1
else if c = ']' then depth := !depth - 1
done
end
end
else if c = ']' then begin
if mem.(!ptr) <> 0 then begin
let depth = ref 1 in
while !depth > 0 do
pc := !pc - 1;
let c = prog.[!pc] in
if c = ']' then depth := !depth + 1
else if c = '[' then depth := !depth - 1
done
end
end);
pc := !pc + 1
done;
!acc
;;
interpret "+++[.-]"

43
lib/ocaml/baseline/bfs.ml Normal file
View File

@@ -0,0 +1,43 @@
(* Baseline: graph BFS using Queue + Hashtbl visited set.
Returns the count of reachable nodes. *)
(* Adjacency as an assoc list of (node, neighbors). *)
let graph =
[ ("A", ["B"; "C"])
; ("B", ["D"])
; ("C", ["D"; "E"])
; ("D", ["F"])
; ("E", ["F"])
; ("F", [])
]
;;
let neighbors n =
match List.assoc_opt n graph with
| None -> []
| Some ns -> ns
;;
let bfs start =
let visited = Hashtbl.create 16 in
let q = Queue.create () in
Queue.push start q ;
Hashtbl.add visited start true ;
let rec loop () =
if Queue.is_empty q then ()
else
let v = Queue.pop q in
List.iter
(fun n ->
if not (Hashtbl.mem visited n) then begin
Hashtbl.add visited n true ;
Queue.push n q
end)
(neighbors v) ;
loop ()
in
loop () ;
Hashtbl.length visited
;;
bfs "A"

View File

@@ -0,0 +1,42 @@
let h = 5
let w = 5
let grid = [|
[| 0; 0; 1; 0; 0 |];
[| 1; 0; 1; 0; 1 |];
[| 0; 0; 0; 0; 0 |];
[| 0; 1; 1; 1; 0 |];
[| 0; 0; 0; 0; 0 |]
|]
let step dist q r c nr nc =
if nr >= 0 && nr < h && nc >= 0 && nc < w
&& grid.(nr).(nc) = 0 && dist.(nr).(nc) = -1 then begin
dist.(nr).(nc) <- dist.(r).(c) + 1;
Queue.push (nr * 10 + nc) q
end
let bfs sr sc tr tc =
let dist = Array.init h (fun _ -> Array.make w (-1)) in
let q = Queue.create () in
dist.(sr).(sc) <- 0;
Queue.push (sr * 10 + sc) q;
let go = ref true in
while !go do
if Queue.is_empty q then go := false
else if dist.(tr).(tc) <> -1 then go := false
else begin
let rc = Queue.pop q in
let r = rc / 10 in
let c = rc mod 10 in
step dist q r c (r - 1) c;
step dist q r c (r + 1) c;
step dist q r c r (c - 1);
step dist q r c r (c + 1)
end
done;
dist.(tr).(tc)
;;
bfs 0 0 4 4

View File

@@ -0,0 +1,24 @@
let bigint_add a b =
let rec aux a b carry =
match (a, b) with
| ([], []) -> if carry = 0 then [] else [carry]
| (x :: xs, []) ->
let s = x + carry in
(s mod 10) :: aux xs [] (s / 10)
| ([], y :: ys) ->
let s = y + carry in
(s mod 10) :: aux [] ys (s / 10)
| (x :: xs, y :: ys) ->
let s = x + y + carry in
(s mod 10) :: aux xs ys (s / 10)
in
aux a b 0
;;
let r1 = bigint_add [9;9;9] [1] in
let r2 = bigint_add [5;6;7] [8;9;1] in
let r3 = bigint_add [9;9;9;9;9;9;9;9] [1] in
List.fold_left (+) 0 r1
+ List.fold_left (+) 0 r2
+ List.length r3

View File

@@ -0,0 +1,47 @@
let parent i = (i - 1) / 2
let lchild i = 2 * i + 1
let rchild i = 2 * i + 2
let swap a i j =
let t = a.(i) in
a.(i) <- a.(j);
a.(j) <- t
let rec sift_up a i =
if i > 0 && a.(parent i) > a.(i) then begin
swap a i (parent i);
sift_up a (parent i)
end
let rec sift_down a n i =
let l = lchild i and r = rchild i in
let smallest = ref i in
if l < n && a.(l) < a.(!smallest) then smallest := l;
if r < n && a.(r) < a.(!smallest) then smallest := r;
if !smallest <> i then begin
swap a i !smallest;
sift_down a n !smallest
end
let push a size x =
a.(!size) <- x;
size := !size + 1;
sift_up a (!size - 1)
let pop a size =
let m = a.(0) in
size := !size - 1;
a.(0) <- a.(!size);
sift_down a !size 0;
m
;;
let a = Array.make 20 0 in
let s = ref 0 in
List.iter (fun x -> push a s x) [9; 4; 7; 1; 8; 3; 5; 2; 6];
let total = ref 0 in
for _ = 1 to 9 do
total := !total * 10 + pop a s
done;
!total

View File

@@ -0,0 +1,39 @@
let is_bipartite n adj =
let color = Array.make n (-1) in
let ok = ref true in
let q = Queue.create () in
for src = 0 to n - 1 do
if color.(src) = -1 then begin
color.(src) <- 0;
Queue.push src q;
while not (Queue.is_empty q) do
let u = Queue.pop q in
List.iter (fun v ->
if color.(v) = -1 then begin
color.(v) <- 1 - color.(u);
Queue.push v q
end else if color.(v) = color.(u) then
ok := false
) adj.(u)
done
end
done;
let zeros = ref 0 in
for i = 0 to n - 1 do
if color.(i) = 0 then zeros := !zeros + 1
done;
if !ok then !zeros else -1
;;
let n = 7 in
let adj = [|
[1; 3];
[0; 2; 4];
[1; 5];
[0; 4; 6];
[1; 3];
[2; 6];
[3; 5]
|] in
is_bipartite n adj

View File

@@ -0,0 +1,13 @@
let bisect f lo hi =
let lo = ref lo and hi = ref hi in
for _ = 1 to 50 do
let mid = (!lo +. !hi) /. 2.0 in
if f mid = 0.0 || f !lo *. f mid < 0.0 then hi := mid
else lo := mid
done;
!lo
;;
let r = bisect (fun x -> x *. x -. 2.0) 1.0 2.0 in
int_of_float (r *. 100.0)

View File

@@ -0,0 +1,12 @@
let popcount n =
let count = ref 0 in
let m = ref n in
while !m > 0 do
if !m land 1 = 1 then count := !count + 1;
m := !m lsr 1
done;
!count
;;
popcount 1023 + popcount 5 + popcount 1024 + popcount 0xff

View File

@@ -0,0 +1,24 @@
let bowling_score frames =
let arr = Array.of_list frames in
let n = Array.length arr in
let total = ref 0 in
let i = ref 0 in
let frame = ref 1 in
while !frame <= 10 && !i < n do
if arr.(!i) = 10 then begin
total := !total + 10 + arr.(!i + 1) + arr.(!i + 2);
i := !i + 1
end else if !i + 1 < n && arr.(!i) + arr.(!i + 1) = 10 then begin
total := !total + 10 + arr.(!i + 2);
i := !i + 2
end else begin
total := !total + arr.(!i) + arr.(!i + 1);
i := !i + 2
end;
frame := !frame + 1
done;
!total
;;
bowling_score [10; 7; 3; 9; 0; 10; 0; 8; 8; 2; 0; 6; 10; 10; 10; 8; 1]

View File

@@ -0,0 +1,32 @@
let bracket_match s =
let n = String.length s in
let stack = ref [] in
let ok = ref true in
let i = ref 0 in
while !ok && !i < n do
let c = s.[!i] in
if c = '(' || c = '[' || c = '{' then
stack := c :: !stack
else if c = ')' || c = ']' || c = '}' then begin
match !stack with
| [] -> ok := false
| top :: rest ->
let pair =
(c = ')' && top = '(') ||
(c = ']' && top = '[') ||
(c = '}' && top = '{')
in
if pair then stack := rest else ok := false
end;
i := !i + 1
done;
if !ok && !stack = [] then 1 else 0
;;
let strings = ["()"; "[{()}]"; "({[}])"; ""; "(("; "()[](){}"; "(a(b)c)"; "(()"; "])"] in
let count = ref 0 in
List.iter (fun s ->
count := !count + bracket_match s
) strings;
!count

View File

@@ -0,0 +1,20 @@
let interpret prog =
let mem = Array.make 256 0 in
let ptr = ref 0 in
let pc = ref 0 in
let n = String.length prog in
let acc = ref 0 in
while !pc < n do
let c = prog.[!pc] in
(if c = '>' then ptr := !ptr + 1
else if c = '<' then ptr := !ptr - 1
else if c = '+' then mem.(!ptr) <- mem.(!ptr) + 1
else if c = '-' then mem.(!ptr) <- mem.(!ptr) - 1
else if c = '.' then acc := !acc + mem.(!ptr));
pc := !pc + 1
done;
!acc
;;
interpret "+++++.+++++.+++++.+++++.+++++."

View File

@@ -0,0 +1,27 @@
let lower_bound arr x =
let lo = ref 0 and hi = ref (Array.length arr) in
while !lo < !hi do
let mid = (!lo + !hi) / 2 in
if arr.(mid) < x then lo := mid + 1
else hi := mid
done;
!lo
let upper_bound arr x =
let lo = ref 0 and hi = ref (Array.length arr) in
while !lo < !hi do
let mid = (!lo + !hi) / 2 in
if arr.(mid) <= x then lo := mid + 1
else hi := mid
done;
!lo
;;
let a = [| 1; 2; 2; 3; 3; 3; 5; 7; 9 |] in
let cnt3 = upper_bound a 3 - lower_bound a 3 in
let cnt2 = upper_bound a 2 - lower_bound a 2 in
let cnt5 = upper_bound a 5 - lower_bound a 5 in
let cnt9 = upper_bound a 9 - lower_bound a 9 in
let cnt4 = upper_bound a 4 - lower_bound a 4 in
cnt3 * 1000 + cnt2 * 100 + cnt5 * 10 + cnt9 + cnt4

View File

@@ -0,0 +1,25 @@
let bs_rotated arr target =
let lo = ref 0 in
let hi = ref (Array.length arr - 1) in
let result = ref (-1) in
while !lo <= !hi && !result = -1 do
let mid = (!lo + !hi) / 2 in
if arr.(mid) = target then result := mid
else if arr.(!lo) <= arr.(mid) then begin
if target >= arr.(!lo) && target < arr.(mid) then
hi := mid - 1
else
lo := mid + 1
end else begin
if target > arr.(mid) && target <= arr.(!hi) then
lo := mid + 1
else
hi := mid - 1
end
done;
!result
;;
let a = [| 4; 5; 6; 7; 0; 1; 2 |] in
bs_rotated a 0 + bs_rotated a 7 * 10 + bs_rotated a 3 * 100

View File

@@ -0,0 +1,16 @@
let bsearch arr target =
let n = Array.length arr in
let lo = ref 0 and hi = ref (n - 1) in
let found = ref (-1) in
while !lo <= !hi && !found = -1 do
let mid = (!lo + !hi) / 2 in
if arr.(mid) = target then found := mid
else if arr.(mid) < target then lo := mid + 1
else hi := mid - 1
done;
!found
;;
let a = Array.of_list [1;3;5;7;9;11;13;15;17;19;21] in
bsearch a 13 + bsearch a 5 + bsearch a 100

View File

@@ -0,0 +1,25 @@
(* Baseline: binary search tree with insert + in-order traversal *)
type 'a tree =
| Leaf
| Node of 'a * 'a tree * 'a tree
;;
let rec insert x t =
match t with
| Leaf -> Node (x, Leaf, Leaf)
| Node (v, l, r) ->
if x < v then Node (v, insert x l, r)
else if x > v then Node (v, l, insert x r)
else t
;;
let rec inorder t =
match t with
| Leaf -> []
| Node (v, l, r) -> List.append (inorder l) (v :: inorder r)
;;
let from_list xs = List.fold_left (fun t x -> insert x t) Leaf xs ;;
let t = from_list [5; 3; 8; 1; 4; 7; 9; 2] ;;
List.fold_left (fun a b -> a + b) 0 (inorder t)

View File

@@ -0,0 +1,14 @@
let shift_char c k =
let n = Char.code c in
if n >= 97 && n <= 122 then
Char.chr (((n - 97 + k) mod 26 + 26) mod 26 + 97)
else c
let encode s k =
String.init (String.length s) (fun i -> shift_char s.[i] k)
;;
(* ROT13 round-trip: encode (encode "hello" 13) 13 = "hello".
Sum the codes of two chars to give a deterministic integer check. *)
let r = encode (encode "hello" 13) 13 in
Char.code r.[0] + Char.code r.[4]

View File

@@ -0,0 +1,76 @@
(* Baseline: recursive-descent calculator for "+", "*", parens, ints. *)
type expr =
| Lit of int
| Add of expr * expr
| Mul of expr * expr
;;
let parse_input src =
let pos = ref 0 in
let peek () = if !pos < String.length src then String.get src !pos else "" in
let advance () = pos := !pos + 1 in
let skip_ws () =
while !pos < String.length src && peek () = " " do advance () done
in
let rec parse_atom () =
skip_ws () ;
if peek () = "(" then begin
advance () ;
let e = parse_expr () in
skip_ws () ;
advance () ; (* consume ')' *)
e
end
else
let start = !pos in
let rec digits () =
if !pos < String.length src then
let c = peek () in
if c >= "0" && c <= "9" then begin advance () ; digits () end
else ()
in
digits () ;
let n = Int.of_string (String.sub src start (!pos - start)) in
Lit n
and parse_term () =
skip_ws () ;
let lhs = ref (parse_atom ()) in
let rec loop () =
skip_ws () ;
if peek () = "*" then begin
advance () ;
lhs := Mul (!lhs, parse_atom ()) ;
loop ()
end
in
loop () ;
!lhs
and parse_expr () =
skip_ws () ;
let lhs = ref (parse_term ()) in
let rec loop () =
skip_ws () ;
if peek () = "+" then begin
advance () ;
lhs := Add (!lhs, parse_term ()) ;
loop ()
end
in
loop () ;
!lhs
in
parse_expr ()
;;
let rec eval e =
match e with
| Lit n -> n
| Add (a, b) -> eval a + eval b
| Mul (a, b) -> eval a * eval b
;;
(* (1 + 2) * 3 + 4 = 9 + 4 = 13 *)
eval (parse_input "(1 + 2) * 3 + 4")

View File

@@ -0,0 +1,13 @@
let catalan n =
let dp = Array.make (n + 1) 0 in
dp.(0) <- 1;
for i = 1 to n do
for j = 0 to i - 1 do
dp.(i) <- dp.(i) + dp.(j) * dp.(i - 1 - j)
done
done;
dp.(n)
;;
catalan 5

View File

@@ -0,0 +1,5 @@
(* Baseline: closures + curried application *)
let make_adder n = fun x -> n + x ;;
let add5 = make_adder 5 ;;
let add10 = make_adder 10 ;;
add5 100 + add10 200

View File

@@ -0,0 +1,15 @@
let coin_change coins target =
let dp = Array.make (target + 1) (target + 1) in
dp.(0) <- 0;
for i = 1 to target do
List.iter (fun c ->
if c <= i && dp.(i - c) + 1 < dp.(i) then
dp.(i) <- dp.(i - c) + 1
) coins
done;
if dp.(target) > target then -1
else dp.(target)
;;
coin_change [1; 5; 10; 25] 67

View File

@@ -0,0 +1,16 @@
let coin_min coins amount =
let dp = Array.make (amount + 1) (-1) in
dp.(0) <- 0;
for i = 1 to amount do
List.iter (fun c ->
if c <= i && dp.(i - c) >= 0 then begin
let cand = dp.(i - c) + 1 in
if dp.(i) < 0 || cand < dp.(i) then dp.(i) <- cand
end
) coins
done;
dp.(amount)
;;
coin_min [1; 5; 10; 25] 67

View File

@@ -0,0 +1,12 @@
let rec choose k xs =
if k = 0 then [[]]
else
match xs with
| [] -> []
| h :: rest ->
List.map (fun c -> h :: c) (choose (k - 1) rest)
@ choose k rest
;;
List.length (choose 4 [1; 2; 3; 4; 5; 6; 7; 8; 9])

View File

@@ -0,0 +1,43 @@
let cross ox oy ax ay bx by =
(ax - ox) * (by - oy) - (ay - oy) * (bx - ox)
let hull_size pts =
let n = List.length pts in
if n < 3 then n
else begin
let sorted = List.sort (fun (a, b) (c, d) ->
if a <> c then compare a c else compare b d) pts in
let arr = Array.of_list sorted in
let h = Array.make (2 * n) (0, 0) in
let k = ref 0 in
for i = 0 to n - 1 do
let (xi, yi) = arr.(i) in
let cont = ref true in
while !cont && !k >= 2 do
let (ox, oy) = h.(!k - 2) in
let (ax, ay) = h.(!k - 1) in
if cross ox oy ax ay xi yi <= 0 then k := !k - 1
else cont := false
done;
h.(!k) <- (xi, yi);
k := !k + 1
done;
let lo = !k + 1 in
for i = n - 2 downto 0 do
let (xi, yi) = arr.(i) in
let cont = ref true in
while !cont && !k >= lo do
let (ox, oy) = h.(!k - 2) in
let (ax, ay) = h.(!k - 1) in
if cross ox oy ax ay xi yi <= 0 then k := !k - 1
else cont := false
done;
h.(!k) <- (xi, yi);
k := !k + 1
done;
!k - 1
end
;;
hull_size [(0, 0); (1, 1); (2, 0); (2, 2); (0, 2); (1, 0); (3, 3); (5, 1)]

View File

@@ -0,0 +1,14 @@
let count_bits n =
let result = Array.make (n + 1) 0 in
for i = 1 to n do
result.(i) <- result.(i / 2) + (i mod 2)
done;
let sum = ref 0 in
for i = 0 to n do
sum := !sum + result.(i)
done;
!sum
;;
count_bits 100

View File

@@ -0,0 +1,13 @@
let count_ways coins target =
let dp = Array.make (target + 1) 0 in
dp.(0) <- 1;
List.iter (fun c ->
for i = c to target do
dp.(i) <- dp.(i) + dp.(i - c)
done
) coins;
dp.(target)
;;
count_ways [1; 2; 5; 10; 25] 50

View File

@@ -0,0 +1,42 @@
let count_inv arr =
let n = Array.length arr in
let temp = Array.make n 0 in
let count = ref 0 in
let rec merge lo mid hi =
let i = ref lo and j = ref (mid + 1) and k = ref lo in
while !i <= mid && !j <= hi do
if arr.(!i) <= arr.(!j) then begin
temp.(!k) <- arr.(!i);
i := !i + 1
end else begin
temp.(!k) <- arr.(!j);
count := !count + (mid - !i + 1);
j := !j + 1
end;
k := !k + 1
done;
while !i <= mid do
temp.(!k) <- arr.(!i);
i := !i + 1; k := !k + 1
done;
while !j <= hi do
temp.(!k) <- arr.(!j);
j := !j + 1; k := !k + 1
done;
for x = lo to hi do
arr.(x) <- temp.(x)
done
and sort lo hi =
if lo < hi then begin
let mid = (lo + hi) / 2 in
sort lo mid;
sort (mid + 1) hi;
merge lo mid hi
end
in
sort 0 (n - 1);
!count
;;
count_inv [|8; 4; 2; 1; 3; 5; 7; 6|]

View File

@@ -0,0 +1,17 @@
let count_pal s =
let n = String.length s in
let count = ref 0 in
for c = 0 to 2 * n - 2 do
let l = ref (c / 2) in
let r = ref ((c + 1) / 2) in
while !l >= 0 && !r < n && s.[!l] = s.[!r] do
count := !count + 1;
l := !l - 1;
r := !r + 1
done
done;
!count
;;
count_pal "aabaa"

View File

@@ -0,0 +1,42 @@
let n = 6
let adj = [|
[1; 2];
[3];
[3; 4];
[5];
[5];
[]
|]
let in_deg = Array.make n 0
let paths = Array.make n 0
let count_paths () =
for u = 0 to n - 1 do
List.iter (fun v -> in_deg.(v) <- in_deg.(v) + 1) adj.(u)
done;
let order = ref [] in
let q = Queue.create () in
for v = 0 to n - 1 do
if in_deg.(v) = 0 then Queue.push v q
done;
while not (Queue.is_empty q) do
let u = Queue.pop q in
order := u :: !order;
List.iter (fun v ->
in_deg.(v) <- in_deg.(v) - 1;
if in_deg.(v) = 0 then Queue.push v q
) adj.(u)
done;
paths.(0) <- 1;
let topo = List.rev !order in
List.iter (fun u ->
List.iter (fun v ->
paths.(v) <- paths.(v) + paths.(u)
) adj.(u)
) topo;
paths.(n - 1)
;;
count_paths ()

View File

@@ -0,0 +1,16 @@
let count_subarr_sum_k arr k =
let n = Array.length arr in
let prefix = Array.make (n + 1) 0 in
for i = 0 to n - 1 do
prefix.(i + 1) <- prefix.(i) + arr.(i)
done;
let count = ref 0 in
for i = 0 to n - 1 do
for j = i + 1 to n do
if prefix.(j) - prefix.(i) = k then count := !count + 1
done
done;
!count
;;
count_subarr_sum_k [| 1; 1; 1; 2; -1; 3; 1; -2; 4 |] 3

12
lib/ocaml/baseline/csv.ml Normal file
View File

@@ -0,0 +1,12 @@
let sum_second_col text =
let lines = String.split_on_char '\n' text in
List.fold_left (fun acc line ->
let fields = String.split_on_char ',' line in
if List.length fields >= 2 then
acc + int_of_string (List.nth fields 1)
else acc
) 0 lines
;;
sum_second_col "a,1,extra\nb,2,extra\nc,3,extra\nd,4,extra"

View File

@@ -0,0 +1,24 @@
let daily_temperatures temps =
let n = Array.length temps in
let answer = Array.make n 0 in
let stack = ref [] in
for i = 0 to n - 1 do
let cont = ref true in
while !cont do
match !stack with
| top :: rest when temps.(top) < temps.(i) ->
answer.(top) <- i - top;
stack := rest
| _ -> cont := false
done;
stack := i :: !stack
done;
let sum = ref 0 in
for i = 0 to n - 1 do
sum := !sum + answer.(i)
done;
!sum
;;
daily_temperatures [| 73; 74; 75; 71; 69; 72; 76; 73 |]

View File

@@ -0,0 +1,37 @@
let n = 5
let edges = [|
[(1, 4); (2, 1)];
[(3, 1)];
[(1, 2); (3, 5)];
[(4, 3)];
[]
|]
let dijkstra src =
let dist = Array.make n 1000000 in
dist.(src) <- 0;
let visited = Array.make n false in
for _ = 0 to n - 1 do
let u = ref (-1) in
let best = ref 1000000 in
for v = 0 to n - 1 do
if (not visited.(v)) && dist.(v) < !best then begin
best := dist.(v);
u := v
end
done;
if !u >= 0 then begin
visited.(!u) <- true;
List.iter (fun (v, w) ->
if dist.(!u) + w < dist.(v) then
dist.(v) <- dist.(!u) + w
) edges.(!u)
end
done;
dist
;;
let d = dijkstra 0 in
d.(4)

View File

@@ -0,0 +1,20 @@
let count_subseq s t =
let m = String.length s in
let n = String.length t in
let dp = Array.init (m + 1) (fun _ -> Array.make (n + 1) 0) in
for i = 0 to m do
dp.(i).(0) <- 1
done;
for i = 1 to m do
for j = 1 to n do
if s.[i - 1] = t.[j - 1] then
dp.(i).(j) <- dp.(i - 1).(j) + dp.(i - 1).(j - 1)
else
dp.(i).(j) <- dp.(i - 1).(j)
done
done;
dp.(m).(n)
;;
count_subseq "rabbbit" "rabbit"

View File

@@ -0,0 +1,27 @@
let word_break s words =
let n = String.length s in
let dp = Array.make (n + 1) false in
dp.(0) <- true;
for i = 1 to n do
List.iter (fun w ->
let wl = String.length w in
if i >= wl && dp.(i - wl) then begin
let prefix = String.sub s (i - wl) wl in
if prefix = w then dp.(i) <- true
end
) words
done;
if dp.(n) then 1 else 0
let count_ok strings words =
let count = ref 0 in
List.iter (fun s ->
count := !count + word_break s words
) strings;
!count
;;
let dict = ["apple"; "pen"; "pine"; "pineapple"; "cats"; "cat"; "and"; "sand"; "dog"] in
let inputs = ["applepenapple"; "pineapplepenapple"; "catsanddog"; "catsandog"; "applesand"] in
count_ok inputs dict

View File

@@ -0,0 +1,26 @@
let egg_drop eggs floors =
let dp = Array.init (eggs + 1) (fun _ -> Array.make (floors + 1) 0) in
for f = 1 to floors do
dp.(1).(f) <- f
done;
for e = 1 to eggs do
dp.(e).(0) <- 0;
dp.(e).(1) <- 1
done;
for e = 2 to eggs do
for f = 2 to floors do
let best = ref 100000000 in
for k = 1 to f do
let bre = dp.(e - 1).(k - 1) in
let sur = dp.(e).(f - k) in
let cand = 1 + (if bre > sur then bre else sur) in
if cand < !best then best := cand
done;
dp.(e).(f) <- !best
done
done;
dp.(eggs).(floors)
;;
egg_drop 2 36

View File

@@ -0,0 +1,10 @@
let euler1 limit =
let sum = ref 0 in
for i = 1 to limit - 1 do
if i mod 3 = 0 || i mod 5 = 0 then sum := !sum + i
done;
!sum
;;
euler1 1000

View File

@@ -0,0 +1,22 @@
let sieve_sum n =
let s = Array.make (n + 1) true in
s.(0) <- false;
s.(1) <- false;
for i = 2 to n do
if s.(i) then begin
let j = ref (i * i) in
while !j <= n do
s.(!j) <- false;
j := !j + i
done
end
done;
let total = ref 0 in
for i = 2 to n do
if s.(i) then total := !total + i
done;
!total
;;
sieve_sum 100

View File

@@ -0,0 +1,25 @@
let collatz_len n =
let m = ref n in
let c = ref 0 in
while !m > 1 do
if !m mod 2 = 0 then m := !m / 2
else m := 3 * !m + 1;
c := !c + 1
done;
!c
let euler14 limit =
let best = ref 0 in
let best_n = ref 0 in
for n = 2 to limit do
let l = collatz_len n in
if l > !best then begin
best := l;
best_n := n
end
done;
!best_n
;;
euler14 100

View File

@@ -0,0 +1,14 @@
let euler16 n =
let p = ref 1 in
for _ = 1 to n do p := !p * 2 done;
let sum = ref 0 in
let m = ref !p in
while !m > 0 do
sum := !sum + !m mod 10;
m := !m / 10
done;
!sum
;;
euler16 15

View File

@@ -0,0 +1,15 @@
let euler2 limit =
let a = ref 1 in
let b = ref 2 in
let sum = ref 0 in
while !a <= limit do
if !a mod 2 = 0 then sum := !sum + !a;
let c = !a + !b in
a := !b;
b := c
done;
!sum
;;
euler2 4000000

View File

@@ -0,0 +1,25 @@
let div_sum n =
let s = ref 1 in
let i = ref 2 in
while !i * !i <= n do
if n mod !i = 0 then begin
s := !s + !i;
let q = n / !i in
if q <> !i then s := !s + q
end;
i := !i + 1
done;
if n = 1 then 0 else !s
let euler21 limit =
let total = ref 0 in
for a = 2 to limit do
let b = div_sum a in
if b <> a && b > a && b <= limit && div_sum b = a then
total := !total + a + b
done;
!total
;;
euler21 300

View File

@@ -0,0 +1,17 @@
let euler25 n =
let a = ref 1 in
let b = ref 1 in
let i = ref 2 in
let target = ref 1 in
for _ = 1 to n - 1 do target := !target * 10 done;
while !b < !target do
let c = !a + !b in
a := !b;
b := c;
i := !i + 1
done;
!i
;;
euler25 12

View File

@@ -0,0 +1,15 @@
let euler28 n =
let s = ref 1 in
let k = ref 1 in
for layer = 1 to (n - 1) / 2 do
let step = 2 * layer in
for _ = 1 to 4 do
k := !k + step;
s := !s + !k
done
done;
!s
;;
euler28 7

View File

@@ -0,0 +1,14 @@
let euler29 n =
let h = Hashtbl.create 64 in
for a = 2 to n do
for b = 2 to n do
let p = ref 1 in
for _ = 1 to b do p := !p * a done;
Hashtbl.replace h !p ()
done
done;
Hashtbl.length h
;;
euler29 5

View File

@@ -0,0 +1,15 @@
let largest_prime_factor n =
let m = ref n in
let factor = ref 2 in
let largest = ref 0 in
while !m > 1 do
if !m mod !factor = 0 then begin
largest := !factor;
m := !m / !factor
end else factor := !factor + 1
done;
!largest
;;
largest_prime_factor 13195

View File

@@ -0,0 +1,22 @@
let pow_digit_sum n p =
let m = ref n in
let s = ref 0 in
while !m > 0 do
let d = !m mod 10 in
let pd = ref 1 in
for _ = 1 to p do pd := !pd * d done;
s := !s + !pd;
m := !m / 10
done;
!s
let euler30 p limit =
let total = ref 0 in
for n = 2 to limit do
if pow_digit_sum n p = n then total := !total + n
done;
!total
;;
euler30 3 999

View File

@@ -0,0 +1,24 @@
let fact n =
let r = ref 1 in
for i = 2 to n do r := !r * i done;
!r
let digit_fact_sum n =
let m = ref n in
let s = ref 0 in
while !m > 0 do
s := !s + fact (!m mod 10);
m := !m / 10
done;
!s
let euler34 limit =
let total = ref 0 in
for n = 3 to limit do
if digit_fact_sum n = n then total := !total + n
done;
!total
;;
euler34 2000

View File

@@ -0,0 +1,41 @@
let pal_dec n =
let s = string_of_int n in
let len = String.length s in
let p = ref true in
for i = 0 to len / 2 - 1 do
if s.[i] <> s.[len - 1 - i] then p := false
done;
!p
let to_binary n =
if n = 0 then "0"
else
let buf = Buffer.create 32 in
let m = ref n in
let stack = ref [] in
while !m > 0 do
stack := (!m mod 2) :: !stack;
m := !m / 2
done;
List.iter (fun d -> Buffer.add_string buf (string_of_int d)) !stack;
Buffer.contents buf
let pal_bin n =
let s = to_binary n in
let len = String.length s in
let p = ref true in
for i = 0 to len / 2 - 1 do
if s.[i] <> s.[len - 1 - i] then p := false
done;
!p
let euler36 limit =
let sum = ref 0 in
for n = 1 to limit - 1 do
if pal_dec n && pal_bin n then sum := !sum + n
done;
!sum
;;
euler36 1000

View File

@@ -0,0 +1,22 @@
let euler40 () =
let buf = Buffer.create 4096 in
let len = ref 0 in
let i = ref 1 in
while !len < 1500 do
let s = string_of_int !i in
Buffer.add_string buf s;
len := !len + String.length s;
i := !i + 1
done;
let s = Buffer.contents buf in
let prod = ref 1 in
let positions = [1; 10; 100; 1000] in
List.iter (fun p ->
let c = s.[p - 1] in
prod := !prod * (Char.code c - Char.code '0')
) positions;
!prod
;;
euler40 ()

View File

@@ -0,0 +1,21 @@
let is_pal n =
let s = string_of_int n in
let len = String.length s in
let p = ref true in
for i = 0 to len / 2 - 1 do
if s.[i] <> s.[len - 1 - i] then p := false
done;
!p
let euler4 lo hi =
let m = ref 0 in
for a = lo to hi do
for b = a to hi do
let p = a * b in
if p > !m && is_pal p then m := p
done
done;
!m
;;
euler4 10 99

View File

@@ -0,0 +1,11 @@
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let lcm a b = a * b / gcd a b
let euler5 n =
let r = ref 1 in
for i = 2 to n do
r := lcm !r i
done;
!r
;;
euler5 20

View File

@@ -0,0 +1,12 @@
let euler6 n =
let sum = ref 0 in
let sum_sq = ref 0 in
for i = 1 to n do
sum := !sum + i;
sum_sq := !sum_sq + i * i
done;
!sum * !sum - !sum_sq
;;
euler6 100

View File

@@ -0,0 +1,22 @@
let nth_prime n =
let count = ref 0 in
let i = ref 1 in
let result = ref 0 in
while !count < n do
i := !i + 1;
let p = ref true in
let j = ref 2 in
while !j * !j <= !i && !p do
if !i mod !j = 0 then p := false;
j := !j + 1
done;
if !p then begin
count := !count + 1;
if !count = n then result := !i
end
done;
!result
;;
nth_prime 100

View File

@@ -0,0 +1,17 @@
let euler9 () =
let result = ref 0 in
for a = 1 to 333 do
let num = 500000 - 1000 * a in
let den = 1000 - a in
if num mod den = 0 then begin
let b = num / den in
if b > a then
let c = 1000 - a - b in
if c > b then result := a * b * c
end
done;
!result
;;
euler9 ()

View File

@@ -0,0 +1,17 @@
(* Baseline: exception declaration + raise + try-with *)
exception NegArg of int ;;
let safe_sqrt n =
if n < 0 then raise (NegArg n)
else
begin
let rec find_sqrt i =
if i * i > n then i - 1
else find_sqrt (i + 1)
in find_sqrt 0
end ;;
let result =
try
safe_sqrt 16
with
| NegArg _ -> 0 ;;
result

View File

@@ -0,0 +1,16 @@
exception Negative of int
let safe_sqrt n =
if n < 0 then raise (Negative n)
else
let g = ref 1 in
while !g * !g < n do g := !g + 1 done;
!g
let try_sqrt n =
try safe_sqrt n with
| Negative x -> -x
;;
try_sqrt 16 + try_sqrt 25 + try_sqrt (-7) + try_sqrt 100

View File

@@ -0,0 +1,207 @@
{
"abundant.ml": 21,
"activity_select.ml": 4,
"ackermann.ml": 125,
"adler32.ml": 300286872,
"anagram_check.ml": 2,
"anagram_groups.ml": 3,
"anagrams.ml": 3,
"atm.ml": 120,
"bag.ml": 3,
"bowling.ml": 167,
"bf_full.ml": 6,
"bisect.ml": 141,
"bigint_add.ml": 28,
"binary_heap.ml": 123456789,
"bipartite.ml": 4,
"bits.ml": 21,
"balance.ml": 3,
"bracket_match.ml": 5,
"base_n.ml": 17,
"bfs.ml": 6,
"bfs_grid.ml": 8,
"btree.ml": 39,
"brainfuck.ml": 75,
"bs_bounds.ml": 3211,
"bs_rotated.ml": -66,
"bsearch.ml": 7,
"caesar.ml": 215,
"calc.ml": 13,
"catalan.ml": 42,
"closures.ml": 315,
"combinations.ml": 126,
"convex_hull.ml": 5,
"coin_change.ml": 6,
"coin_min.ml": 6,
"count_bits.ml": 319,
"count_change.ml": 406,
"count_paths_dag.ml": 3,
"count_inversions.ml": 12,
"count_palindromes.ml": 9,
"count_subarrays_k.ml": 7,
"csv.ml": 10,
"daily_temperatures.ml": 10,
"egg_drop.ml": 8,
"dijkstra.ml": 7,
"dp_word_break.ml": 4,
"distinct_subseq.ml": 3,
"exception_handle.ml": 4,
"exception_user.ml": 26,
"euler1.ml": 233168,
"euler16.ml": 26,
"euler10.ml": 1060,
"euler14.ml": 97,
"euler2.ml": 4613732,
"euler21_small.ml": 504,
"euler25.ml": 55,
"euler28.ml": 261,
"euler29_small.ml": 15,
"euler30_cube.ml": 1301,
"euler34_small.ml": 145,
"euler36.ml": 1772,
"euler40_small.ml": 15,
"euler3.ml": 29,
"euler4_small.ml": 9009,
"euler5.ml": 232792560,
"euler6.ml": 25164150,
"euler7.ml": 541,
"euler9.ml": 31875000,
"expr_eval.ml": 16,
"expr_simp.ml": 22,
"factorial.ml": 3628800,
"fenwick_tree.ml": 228,
"fib_doubling.ml": 102334155,
"fib_mod.ml": 391360,
"fraction.ml": 7,
"frequency.ml": 5,
"gas_station.ml": 3,
"gcd_lcm.ml": 60,
"gray_code.ml": 136,
"grep_count.ml": 3,
"grid_paths.ml": 210,
"group_consec.ml": 53,
"hailstone.ml": 111,
"harshad.ml": 33,
"hamming.ml": 4,
"hanoi.ml": 1023,
"hist.ml": 75,
"house_robber.ml": 22,
"histogram_area.ml": 10,
"huffman.ml": 224,
"int_sqrt.ml": 1027,
"interval_overlap.ml": 6,
"is_prime.ml": 25,
"island_count.ml": 5,
"fizz_classifier.ml": 540,
"fizzbuzz.ml": 57,
"flatten_tree.ml": 28,
"flood_fill.ml": 7,
"floyd_cycle.ml": 8,
"floyd_warshall.ml": 9,
"lis.ml": 6,
"list_ops.ml": 30,
"lps_dp.ml": 7,
"lru_cache.ml": 499,
"luhn.ml": 2,
"magic_square.ml": 65,
"mat_mul.ml": 621,
"matrix_power.ml": 832040,
"max_path_tree.ml": 11,
"max_product3.ml": 300,
"max_run.ml": 5,
"mod_inverse.ml": 27,
"josephus.ml": 11,
"json_pretty.ml": 24,
"kadane.ml": 6,
"kmp.ml": 5,
"kth_two.ml": 8,
"knapsack.ml": 36,
"lambda_calc.ml": 7,
"lcs.ml": 4,
"majority_vote.ml": 4,
"manacher.ml": 7,
"lev_iter.ml": 16,
"levenshtein.ml": 11,
"memo_fib.ml": 75025,
"mortgage.ml": 1073,
"mst_kruskal.ml": 11,
"merge_intervals.ml": 12,
"min_meeting_rooms.ml": 4,
"merge_sort.ml": 44,
"merge_two.ml": 441,
"min_cost_path.ml": 12,
"min_jumps.ml": 4,
"min_subarr_target.ml": 2,
"module_use.ml": 3,
"monotonic.ml": 4,
"newton_sqrt.ml": 1414,
"next_greater.ml": 153,
"next_permutation.ml": 119,
"number_words.ml": 106,
"mutable_record.ml": 10,
"option_match.ml": 5,
"palindrome.ml": 4,
"palindrome_part.ml": 1,
"palindrome_sum.ml": 49500,
"paren_depth.ml": 7,
"partition.ml": 3025,
"partition_count.ml": 176,
"pancake_sort.ml": 910,
"pascal.ml": 252,
"peano.ml": 30,
"perfect.ml": 3,
"permutations_gen.ml": 12,
"pi_leibniz.ml": 314,
"prefix_sum.ml": 66,
"pretty_table.ml": 64,
"poly_stack.ml": 5,
"polygon_area.ml": 32,
"pow_mod.ml": 738639,
"powerset_target.ml": 20,
"prime_factors.ml": 17,
"pythagorean.ml": 16,
"queens.ml": 2,
"quickselect.ml": 5,
"quicksort.ml": 44,
"radix_sort.ml": 802002,
"roman.ml": 44,
"rolling_hash.ml": 6,
"regex_simple.ml": 7,
"reverse_int.ml": 54329,
"rpn.ml": 9,
"run_decode.ml": 21,
"run_length.ml": 11,
"safe_div.ml": 20,
"segment_tree.ml": 4232,
"shuffle.ml": 55,
"simpson_int.ml": 10000,
"stable_unique.ml": 46,
"stock_two.ml": 6,
"subseq_check.ml": 3,
"tail_factorial.ml": 479001600,
"task_scheduler.ml": 7,
"tarjan_scc.ml": 4,
"subset_sum.ml": 8,
"tic_tac_toe.ml": 1,
"topo_dfs.ml": 24135,
"topo_sort.ml": 6,
"wildcard_match.ml": 6,
"word_freq.ml": 8,
"xor_cipher.ml": 601,
"zerosafe.ml": 28,
"zigzag.ml": 55,
"zip_unzip.ml": 1000,
"sieve.ml": 15,
"sum_squares.ml": 385,
"tree_depth.ml": 4,
"trapping_rain.ml": 6,
"triangle.ml": 11,
"trie.ml": 6,
"triangle_div.ml": 120,
"twosum.ml": 5,
"union_find.ml": 4,
"unique_paths_obs.ml": 3,
"unique_set.ml": 9,
"validate.ml": 417,
"word_count.ml": 3
}

View File

@@ -0,0 +1,19 @@
(* Baseline: a tiny expression evaluator using ADTs + match *)
type expr =
| Lit of int
| Add of expr * expr
| Mul of expr * expr
| Neg of expr
;;
let rec eval e =
match e with
| Lit n -> n
| Add (a, b) -> eval a + eval b
| Mul (a, b) -> eval a * eval b
| Neg x -> 0 - eval x
;;
(* (1 + 2) * (3 + 4) - 5 = 21 - 5 = 16 *)
eval
(Add (Mul (Add (Lit 1, Lit 2), Add (Lit 3, Lit 4)), Neg (Lit 5)))

View File

@@ -0,0 +1,33 @@
type expr =
| Num of int
| Add of expr * expr
| Mul of expr * expr
let rec simp e =
match e with
| Num n -> Num n
| Add (a, b) ->
(match (simp a, simp b) with
| (Num 0, x) -> x
| (x, Num 0) -> x
| (Num n, Num m) -> Num (n + m)
| (a', b') -> Add (a', b'))
| Mul (a, b) ->
(match (simp a, simp b) with
| (Num 0, _) -> Num 0
| (_, Num 0) -> Num 0
| (Num 1, x) -> x
| (x, Num 1) -> x
| (Num n, Num m) -> Num (n * m)
| (a', b') -> Mul (a', b'))
let rec eval e =
match e with
| Num n -> n
| Add (a, b) -> eval a + eval b
| Mul (a, b) -> eval a * eval b
;;
let e = Add (Mul (Num 3, Num 5), Add (Num 0, Mul (Num 1, Num 7))) in
eval (simp e)

View File

@@ -0,0 +1,4 @@
(* Baseline: factorial via let-rec *)
let rec fact n =
if n = 0 then 1 else n * fact (n - 1) ;;
fact 10

View File

@@ -0,0 +1,33 @@
let n = 8
let bit = Array.make (n + 1) 0
let lowbit i =
let r = ref 1 in
while !r * 2 <= i && i mod (!r * 2) = 0 do
r := !r * 2
done;
!r
let rec update i delta =
if i <= n then begin
bit.(i) <- bit.(i) + delta;
update (i + lowbit i) delta
end
let rec prefix_sum i =
if i <= 0 then 0
else bit.(i) + prefix_sum (i - lowbit i)
let range_sum l r = prefix_sum r - prefix_sum (l - 1)
;;
let a = [| 1; 3; 5; 7; 9; 11; 13; 15 |] in
for i = 0 to n - 1 do
update (i + 1) a.(i)
done;
let total = prefix_sum n in
update 1 100;
let after = prefix_sum n in
total + after

View File

@@ -0,0 +1,14 @@
let rec fib_pair n =
if n = 0 then (0, 1)
else
let (a, b) = fib_pair (n / 2) in
let c = a * (2 * b - a) in
let d = a * a + b * b in
if n mod 2 = 0 then (c, d)
else (d, c + d)
let fib n = let (f, _) = fib_pair n in f
;;
fib 40

Some files were not shown because too many files have changed in this diff Show More