diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 37132b62..7a5bb436 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -59,6 +59,7 @@ "merge_sort.ml": 44, "merge_two.ml": 441, "module_use.ml": 3, + "monotonic.ml": 4, "newton_sqrt.ml": 1414, "mutable_record.ml": 10, "option_match.ml": 5, diff --git a/lib/ocaml/baseline/monotonic.ml b/lib/ocaml/baseline/monotonic.ml new file mode 100644 index 00000000..50020301 --- /dev/null +++ b/lib/ocaml/baseline/monotonic.ml @@ -0,0 +1,25 @@ +let is_monotonic xs = + match xs with + | [] -> true + | [_] -> true + | _ -> + let inc = ref true in + let dec = ref true in + let rec walk prev rest = + match rest with + | [] -> () + | h :: t -> + if h < prev then inc := false; + if h > prev then dec := false; + walk h t + in + (match xs with h :: t -> walk h t | [] -> ()); + !inc || !dec + +;; + +(if is_monotonic [1;2;3;4] then 1 else 0) + +(if is_monotonic [4;3;2;1] then 1 else 0) + +(if is_monotonic [1;2;1] then 1 else 0) + +(if is_monotonic [5;5;5] then 1 else 0) + +(if is_monotonic [] then 1 else 0) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 9bd0f9ae..e73aeb8b 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,17 @@ _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 5.1 — monotonic.ml baseline (monotonicity check, + 4/5 inputs monotonic). Tracks two bool refs (inc, dec). Each pair + of consecutive elements: if `h < prev` clear `inc`, if `h > prev` + clear `dec`. Empty list and singleton are vacuously true. Five + test cases: + [1;2;3;4] inc only true + [4;3;2;1] dec only true + [1;2;1] neither false + [5;5;5] both (constant) true + [] empty true (vacuous) + Sum = 4. 98 baseline programs total. - 2026-05-09 Phase 5.1 — majority_vote.ml baseline (Boyer-Moore majority, [3;3;4;2;4;4;2;4;4] → 4). O(n) time / O(1) space: candidate-and-count refs; on match increment, on mismatch