From a3a93c20b8d0e714b63eb43a0ef60814684b7171 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 15:22:28 +0000 Subject: [PATCH] ocaml: phase 5.1 max_path_tree.ml baseline (max root-to-leaf sum, 1+3+7 = 11) Recursive ADT for binary trees: type tree = Leaf | Node of int * tree * tree let rec max_path t = match t with | Leaf -> 0 | Node (v, l, r) -> let lp = max_path l in let rp = max_path r in v + (if lp > rp then lp else rp) For the test tree: 1 / 2 3 / \ \ 4 5 7 paths sum: 1+2+4=7, 1+2+5=8, 1+3+7=11. max = 11. Tests 3-arg Node constructor with positional arg destructuring, two nested let-bindings, and if-then-else as an inline expression. 73 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/max_path_tree.ml | 21 +++++++++++++++++++++ plans/ocaml-on-sx.md | 7 +++++++ 3 files changed, 29 insertions(+) create mode 100644 lib/ocaml/baseline/max_path_tree.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 6a323ea1..818e582e 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -36,6 +36,7 @@ "flatten_tree.ml": 28, "list_ops.ml": 30, "mat_mul.ml": 621, + "max_path_tree.ml": 11, "mod_inverse.ml": 27, "json_pretty.ml": 24, "kadane.ml": 6, diff --git a/lib/ocaml/baseline/max_path_tree.ml b/lib/ocaml/baseline/max_path_tree.ml new file mode 100644 index 00000000..84d4af37 --- /dev/null +++ b/lib/ocaml/baseline/max_path_tree.ml @@ -0,0 +1,21 @@ +type tree = Leaf | Node of int * tree * tree + +let rec max_path t = + match t with + | Leaf -> 0 + | Node (v, l, r) -> + let lp = max_path l in + let rp = max_path r in + v + (if lp > rp then lp else rp) + +;; + +let t = Node (1, + Node (2, + Node (4, Leaf, Leaf), + Node (5, Leaf, Leaf)), + Node (3, + Leaf, + Node (7, Leaf, Leaf))) +in +max_path t diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 3398aab6..9fee206a 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _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 — max_path_tree.ml baseline (max root-to-leaf + sum in a binary tree, 1+3+7 = 11). Recursive ADT `tree = Leaf | + Node of int * tree * tree`. max_path returns 0 at Leaf, else + v + max(left subtree, right subtree). Tree has 6 nodes; the + rightmost path 1→3→7 maximises at 11. Tests 3-arg `Node` + constructor with positional arg destructuring + nested let-binding + + if-then-else as expression. 73 baseline programs total. - 2026-05-09 Phase 5.1 — mod_inverse.ml baseline (extended Euclidean + modular inverse, sum 4+21+2 = 27). ext_gcd returns a triple (gcd, x, y) such that ax + by = gcd. mod_inverse extracts x and