From e77a2d3a81d711e4e138feb8b98d75d21a0f7dcc Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 01:49:23 +0000 Subject: [PATCH] ocaml: phase 5.1 zerosafe.ml baseline (Option-chained safe division, sum = 28) 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. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/zerosafe.ml | 14 ++++++++++++++ plans/ocaml-on-sx.md | 5 +++++ 3 files changed, 20 insertions(+) create mode 100644 lib/ocaml/baseline/zerosafe.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index f8367cde..7fa0f1b5 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -118,6 +118,7 @@ "tic_tac_toe.ml": 1, "word_freq.ml": 8, "xor_cipher.ml": 601, + "zerosafe.ml": 28, "zigzag.ml": 55, "zip_unzip.ml": 1000, "sieve.ml": 15, diff --git a/lib/ocaml/baseline/zerosafe.ml b/lib/ocaml/baseline/zerosafe.ml new file mode 100644 index 00000000..b4054999 --- /dev/null +++ b/lib/ocaml/baseline/zerosafe.ml @@ -0,0 +1,14 @@ +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 + +;; + +(match safe_chain 100 2 5 with Some x -> x | None -> -1) ++ (match safe_chain 100 0 5 with Some x -> x | None -> -1) ++ (match safe_chain 50 5 0 with Some x -> x | None -> -1) ++ (match safe_chain 1000 10 5 with Some x -> x | None -> -1) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index fee3ee91..cfebefa3 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,11 @@ _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-10 Phase 5.1 — zerosafe.ml baseline (Option-chained safe + division, sum 10 + (-1) + (-1) + 20 = 28). safe_div returns None + on division by zero; safe_chain stitches two divisions, propagating + None on either failure. Tests Option chaining + match-on-result + with sentinel default. 129 baseline programs total. - 2026-05-10 Phase 5.1 — number_words.ml baseline (letter count of numbers 1-19 spelled out = 106). 19-arm match dispatch returning the English word for each number. Sums lengths over 1..19. Real