From 0a3f02d63601d284af668804122bbbf0ef661456 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 00:42:08 +0000 Subject: [PATCH] ocaml: phase 5.1 powerset_target.ml baseline (subsets of {1..10} summing to 15 = 20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Recursive powerset construction by doubling: let rec gen xs = match xs with | [] -> [[]] | h :: rest -> let sub = gen rest in sub @ List.map (fun s -> h :: s) sub Enumerates all 2^10 = 1024 subsets, filters by sum: count = |{ S ⊆ {1..10} | Σ S = 15 }| = 20 Examples: {1,2,3,4,5}, {2,3,4,6}, {1,4,10}, {7,8}, {6,9}, ... Tests recursive subset construction via List.map + closures, pattern matching with h :: rest, List.fold_left (+) 0 sum reduce, exhaustive O(2^n * n) traversal. 172 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/powerset_target.ml | 16 ++++++++++++++++ plans/ocaml-on-sx.md | 9 +++++++++ 3 files changed, 26 insertions(+) create mode 100644 lib/ocaml/baseline/powerset_target.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 90b74c93..0449c025 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -132,6 +132,7 @@ "poly_stack.ml": 5, "polygon_area.ml": 32, "pow_mod.ml": 738639, + "powerset_target.ml": 20, "prime_factors.ml": 17, "pythagorean.ml": 16, "queens.ml": 2, diff --git a/lib/ocaml/baseline/powerset_target.ml b/lib/ocaml/baseline/powerset_target.ml new file mode 100644 index 00000000..6d7d2895 --- /dev/null +++ b/lib/ocaml/baseline/powerset_target.ml @@ -0,0 +1,16 @@ +let rec gen xs = + match xs with + | [] -> [[]] + | h :: rest -> + let sub = gen rest in + sub @ List.map (fun s -> h :: s) sub + +;; + +let all = gen [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] in +let count = ref 0 in +List.iter (fun sub -> + let sum = List.fold_left (+) 0 sub in + if sum = 15 then count := !count + 1 +) all; +!count diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 24604c06..c7be4450 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _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-11 Phase 5.1 — powerset_target.ml baseline (count subsets + of {1..10} summing to 15 = 20). Generates the full 2^10 = 1024 + powerset by recursive doubling: `gen (h :: rest) = gen rest @ + map (cons h) (gen rest)`, then filters by sum. The 20 valid + subsets include {1,2,3,4,5}, {2,3,4,6}, {1,4,10}, {7,8}, {6,9}, + etc. Tests recursive subset construction via List.map + closures, + pattern matching with `h :: rest`, `List.fold_left (+) 0` + one-line summary, exhaustive O(2^n · n) traversal. 172 baseline + programs total. - 2026-05-11 Phase 5.1 — parser: top-level tuple patterns (`match e1, e2 with | p1, p2 -> …`). `parse-pattern` now collects comma-separated patterns into a `:ptuple`, mirroring the scrutinee