diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 7bf2e72e..6b51c383 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -10,6 +10,7 @@ "module_use.ml": 3, "mutable_record.ml": 10, "option_match.ml": 5, + "queens.ml": 2, "quicksort.ml": 44, "sum_squares.ml": 385, "word_count.ml": 3 diff --git a/lib/ocaml/baseline/queens.ml b/lib/ocaml/baseline/queens.ml new file mode 100644 index 00000000..2effdb3c --- /dev/null +++ b/lib/ocaml/baseline/queens.ml @@ -0,0 +1,35 @@ +(* Baseline: n-queens count for n=6. + We count placements of n queens on an n×n board such that no two + share a row, column, or diagonal. *) + +let safe q queens = + let rec go qs offset = + match qs with + | [] -> true + | h :: t -> + if h = q then false + else if h - q = offset then false + else if q - h = offset then false + else go t (offset + 1) + in + go queens 1 +;; + +let rec range a b = + if a > b then [] else a :: range (a + 1) b +;; + +let rec solve n queens row = + if row > n then 1 + else + List.fold_left + (fun acc col -> + if safe col queens then + acc + solve n (col :: queens) (row + 1) + else + acc) + 0 + (range 1 n) +;; + +solve 4 [] 1 diff --git a/lib/ocaml/baseline/run.sh b/lib/ocaml/baseline/run.sh index 8d5e75e7..c26fbdb0 100755 --- a/lib/ocaml/baseline/run.sh +++ b/lib/ocaml/baseline/run.sh @@ -36,7 +36,7 @@ for f in lib/ocaml/baseline/*.ml; do (eval "(ocaml-run-program (file-read \"$f\"))") EOF - output=$(timeout 120 "$SX_SERVER" < "$TMP" 2>/dev/null) + output=$(timeout 240 "$SX_SERVER" < "$TMP" 2>/dev/null) rm -f "$TMP" result=$(echo "$output" | awk ' diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 41d171f2..a5fdfeb0 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-08 Phase 5.1 — queens.ml baseline (15/15 pass). 4-queens + count via recursive backtracking with `List.fold_left`. Returns 2 + (the two solutions of 4-queens). Per-program timeout in run.sh + bumped to 240s — 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 Phase 5.1 — mutable_record.ml baseline (14/14 pass). Counter-style record with two mutable fields, bump function uses `r.f <- v` to mutate. End-to-end validates type decl + record