diff --git a/lib/ocaml/runtime.sx b/lib/ocaml/runtime.sx index 5f21e4f1..a9e204fb 100644 --- a/lib/ocaml/runtime.sx +++ b/lib/ocaml/runtime.sx @@ -321,6 +321,18 @@ | None -> Error none_v | Some x -> Ok x + let equal eq a b = + match a with + | None -> (match b with None -> true | Some _ -> false) + | Some x -> + (match b with None -> false | Some y -> eq x y) + + let compare cmp a b = + match a with + | None -> (match b with None -> 0 | Some _ -> -1) + | Some x -> + (match b with None -> 1 | Some y -> cmp x y) + let some x = Some x let none = None end ;; @@ -464,6 +476,18 @@ let sub s i n = String.sub s i n end ;; + module Bool = struct + let equal a b = a = b + let compare a b = + if a = b then 0 + else if a then 1 + else -1 + let to_string b = if b then \"true\" else \"false\" + let of_string s = s = \"true\" + let not_ b = not b + let to_int b = if b then 1 else 0 + end ;; + module Char = struct let code c = _char_code c let chr n = _char_chr n diff --git a/lib/ocaml/test.sh b/lib/ocaml/test.sh index 9d5d1e24..7d040fe7 100755 --- a/lib/ocaml/test.sh +++ b/lib/ocaml/test.sh @@ -1448,6 +1448,18 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 5202) (eval "(ocaml-run \"String.cat \\\"hello \\\" \\\"world\\\"\")") +;; ── Bool module + Option.equal/compare ─────────────────────── +(epoch 5210) +(eval "(ocaml-run \"Bool.to_string true ^ \\\"-\\\" ^ Bool.to_string false\")") +(epoch 5211) +(eval "(ocaml-run \"Bool.compare true false\")") +(epoch 5212) +(eval "(ocaml-run \"Option.equal (=) (Some 1) (Some 1)\")") +(epoch 5213) +(eval "(ocaml-run \"Option.equal (=) (Some 1) None\")") +(epoch 5214) +(eval "(ocaml-run \"Option.compare compare (Some 5) (Some 3)\")") + EPOCHS OUTPUT=$(timeout 360 "$SX_SERVER" < "$TMPFILE" 2>/dev/null) @@ -2303,6 +2315,13 @@ check 5200 "String.equal abc abc" 'true' check 5201 "String.compare banana apple" '1' check 5202 "String.cat hello world" '"hello world"' +# ── Bool module + Option.equal/compare ───────────────────────── +check 5210 "Bool.to_string true/false" '"true-false"' +check 5211 "Bool.compare true false" '1' +check 5212 "Option.equal Some 1 Some 1" 'true' +check 5213 "Option.equal Some 1 None" 'false' +check 5214 "Option.compare Some 5 Some 3" '1' + 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 cd07e8ac..dc5c66fc 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 — Bool module + Option.equal / Option.compare + (+5 tests, 584 total). Bool: equal, compare (false < true via if + ladder), to_string, of_string, not_, to_int. Option additions + take an `eq` or `cmp` parameter for the inner-value check, mirroring + real OCaml's signature: `Option.equal eq a b`, `Option.compare cmp + a b`. None < Some _ for compare. - 2026-05-09 Phase 5.1 — bag.ml baseline + String.equal/compare/cat/ empty (+3 tests, 579 total). bag.ml: split a sentence on spaces, count word frequency in a Hashtbl, return the maximum count.