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.
This commit is contained in:
2026-05-08 13:44:28 +00:00
parent de7be332c8
commit 46d0eb258e
6 changed files with 53 additions and 22 deletions

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,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

@@ -1,7 +1,10 @@
{
"closures.ml": 315,
"exception_handle.ml": 4,
"factorial.ml": 3628800,
"list_ops.ml": 30,
"option_match.ml": 5,
"module_use.ml": 3,
"option_match.ml": 5,
"quicksort.ml": 44,
"sum_squares.ml": 385
}

View File

@@ -0,0 +1,10 @@
(* Baseline: quicksort over a list, returns sum of sorted result *)
let rec quicksort lst =
match lst with
| [] -> []
| pivot :: rest ->
let smaller = List.filter (fun x -> x < pivot) rest in
let larger = List.filter (fun x -> x >= pivot) rest in
List.append (quicksort smaller) (pivot :: quicksort larger) ;;
let sorted = quicksort [3; 1; 4; 1; 5; 9; 2; 6; 5; 3; 5] ;;
List.fold_left (fun a b -> a + b) 0 sorted

View File

@@ -22,25 +22,7 @@ for f in lib/ocaml/baseline/*.ml; do
fi
TMP=$(mktemp)
cat > "$TMP" << EPOCHS
(epoch 1)
(load "lib/guest/lex.sx")
(load "lib/guest/prefix.sx")
(load "lib/guest/pratt.sx")
(load "lib/ocaml/tokenizer.sx")
(load "lib/ocaml/parser.sx")
(load "lib/ocaml/eval.sx")
(load "lib/ocaml/runtime.sx")
(eval "(ocaml-load-stdlib!)")
(epoch 2)
(eval "(ocaml-run-program (file-read \\"$f\\"))")
EPOCHS
output=$(timeout 60 "$SX_SERVER" < "$TMP" 2>/dev/null | grep -E '^\(ok-len 2|^\(ok 2' | head -1)
rm -f "$TMP"
# Pull the next line which has the value
result=$(timeout 60 "$SX_SERVER" < <(cat <<EPOCHS
cat > "$TMP" << EOF
(epoch 1)
(load "lib/guest/lex.sx")
(load "lib/guest/prefix.sx")
@@ -52,8 +34,15 @@ EPOCHS
(eval "(ocaml-load-stdlib!)")
(epoch 2)
(eval "(ocaml-run-program (file-read \"$f\"))")
EPOCHS
) 2>/dev/null | awk '/^\(ok-len 2 / {getline; print; exit} /^\(ok 2 / {sub(/^\(ok 2 /, ""); sub(/\)$/, ""); print; exit}')
EOF
output=$(timeout 60 "$SX_SERVER" < "$TMP" 2>/dev/null)
rm -f "$TMP"
result=$(echo "$output" | awk '
/^\(ok-len 2 / { getline; print; exit }
/^\(ok 2 [^)]+\)$/ { sub(/^\(ok 2 /, ""); sub(/\)$/, ""); print; exit }
')
if [ "$result" = "$expected" ]; then
PASS=$((PASS + 1))