diff --git a/lib/ocaml/runtime.sx b/lib/ocaml/runtime.sx index 546afabb..b0ec999e 100644 --- a/lib/ocaml/runtime.sx +++ b/lib/ocaml/runtime.sx @@ -560,6 +560,31 @@ let to_list a = !a let of_list xs = ref xs let copy a = ref !a + let sort cmp a = a := List.sort cmp !a + let stable_sort = sort + let fast_sort = sort + + let append a b = ref (List.append !a !b) + + let sub a pos n = + let rec take xs k = + if k = 0 then [] + else match xs with + | [] -> [] + | h :: t -> h :: take t (k - 1) + in + let rec drop xs k = + if k = 0 then xs + else match xs with + | [] -> [] + | _ :: t -> drop t (k - 1) + in + ref (take (drop !a pos) n) + + let exists p a = List.exists p !a + let for_all p a = List.for_all p !a + let mem x a = List.mem x !a + let blit src si dst di n = for k = 0 to n - 1 do set dst (di + k) (get src (si + k)) diff --git a/lib/ocaml/test.sh b/lib/ocaml/test.sh index 52a026eb..4cf26cf0 100755 --- a/lib/ocaml/test.sh +++ b/lib/ocaml/test.sh @@ -1286,6 +1286,18 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 5032) (eval "(ocaml-run \"let a = Array.make 5 0 in for i = 0 to 4 do a.(i) <- i * i done; a.(3) + a.(4)\")") +;; ── Array.sort / sub / append / exists / mem ───────────────── +(epoch 5040) +(eval "(ocaml-run \"let a = Array.of_list [3;1;4;1;5;9;2;6] in Array.sort compare a; Array.to_list a\")") +(epoch 5041) +(eval "(ocaml-run \"let a = Array.of_list [10;20;30;40;50] in let b = Array.sub a 1 3 in Array.fold_left (+) 0 b\")") +(epoch 5042) +(eval "(ocaml-run \"let a = Array.of_list [1;2;3] in let b = Array.of_list [4;5;6] in Array.fold_left (+) 0 (Array.append a b)\")") +(epoch 5043) +(eval "(ocaml-run \"let a = Array.init 5 (fun i -> i * 2) in Array.exists (fun x -> x = 6) a\")") +(epoch 5044) +(eval "(ocaml-run \"let a = Array.of_list [1;2;3;4;5] in Array.mem 3 a\")") + EPOCHS OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null) @@ -2043,6 +2055,13 @@ check 5030 "a.(2) <- 99; a.(2) + a.(0)" '106' check 5031 "Array.init 4 + sum a.(0..3)" '10' check 5032 "for + a.(i) <- i*i + sum" '25' +# ── Array.sort / sub / append / exists / mem ──────────────────── +check 5040 "Array.sort compare" '(1 1 2 3 4 5 6 9)' +check 5041 "Array.sub 1 3 sum" '90' +check 5042 "Array.append 6-len sum" '21' +check 5043 "Array.exists = 6" 'true' +check 5044 "Array.mem 3 [1..5]" 'true' + TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then echo "ok $PASS/$TOTAL OCaml-on-SX tests passed" diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 6668bcc8..90a1154f 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 6 — Array.sort/stable_sort/fast_sort + sub + + append + exists + for_all + mem (+5 tests, 520 total). All + delegate to the corresponding List operation on the cell's + underlying list (sort mutates by replacing the cell, the rest are + pure observers). Array round-trip via of_list → sort → to_list + works as expected. - 2026-05-09 Phase 5.1 — brainfuck.ml baseline (subset interpreter, five `+++++.` groups → cumulative 5+10+15+20+25 = 75). No loop brackets — the interpreter only handles `> < + - .`, but that's