From 5384ff6c42a8e11f06d0fdfb36c34b32a89ab965 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 22:27:18 +0000 Subject: [PATCH] ocaml: phase 5.1 topo_dfs.ml baseline (DFS topo sort, fingerprint 24135) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DFS topological sort — recurse on out-edges first, prepend after: let rec dfs v = if not visited.(v) then begin visited.(v) <- true; List.iter dfs adj.(v); order := v :: !order end Same 6-node DAG as iter 230's Kahn's-algorithm baseline: 0 -> {1, 2} 1 -> {3} 2 -> {3, 4} 3 -> {5} 4 -> {5} 5 DFS order: [0; 2; 4; 1; 3; 5] Horner fold: 0->0->2->24->241->2413->24135. Complementary to topo_sort.ml (Kahn's BFS); tests recursive DFS with no explicit stack + List.fold_left as a horner reduction. 160 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/topo_dfs.ml | 26 ++++++++++++++++++++++++++ plans/ocaml-on-sx.md | 9 +++++++++ 3 files changed, 36 insertions(+) create mode 100644 lib/ocaml/baseline/topo_dfs.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index f980e5bf..f83a769d 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -143,6 +143,7 @@ "tarjan_scc.ml": 4, "subset_sum.ml": 8, "tic_tac_toe.ml": 1, + "topo_dfs.ml": 24135, "topo_sort.ml": 6, "word_freq.ml": 8, "xor_cipher.ml": 601, diff --git a/lib/ocaml/baseline/topo_dfs.ml b/lib/ocaml/baseline/topo_dfs.ml new file mode 100644 index 00000000..172b2b78 --- /dev/null +++ b/lib/ocaml/baseline/topo_dfs.ml @@ -0,0 +1,26 @@ +let n = 6 +let adj = [| + [1; 2]; + [3]; + [3; 4]; + [5]; + [5]; + [] +|] + +let visited = Array.make n false +let order = ref [] + +let rec dfs v = + if not visited.(v) then begin + visited.(v) <- true; + List.iter dfs adj.(v); + order := v :: !order + end + +;; + +for v = 0 to n - 1 do + dfs v +done; +List.fold_left (fun acc v -> acc * 10 + v) 0 !order diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 253ec778..935dd1d9 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-10 Phase 5.1 — topo_dfs.ml baseline (DFS-based topo sort + on the same 6-node DAG as topo_sort.ml, digit-fingerprint 24135). + Cons each node onto the order list AFTER recursing on all its + out-edges, giving a reverse topological order. Same graph + (0→{1,2}; 1→{3}; 2→{3,4}; 3→{5}; 4→{5}; 5) produces order + [0;2;4;1;3;5]. Reduce: 0→0→2→24→241→2413→24135. Complementary + to Kahn's (iter 230); tests recursive DFS without an explicit + stack and List.fold_left as a horner reduction. 160 baseline + programs total. - 2026-05-10 Phase 5.1 — radix_sort.ml baseline (LSD radix sort, fingerprint a.(0) + a.(7)*1000 = 802002). 8-element array [170;45;75;90;802;24;2;66] sorts to [2;24;45;66;75;90;170;802].