From 689438d12e1f532b68e7402f1ce890ad6b257236 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 23:18:26 +0000 Subject: [PATCH] ocaml: phase 5.1 matrix_power.ml baseline (F(30) = 832040 via 2x2 matrix pow) Fibonacci via repeated-squaring matrix exponentiation: [[1, 1], [1, 0]] ^ n = [[F(n+1), F(n)], [F(n), F(n-1)]] Recursive O(log n) power: let rec mpow m n = if n = 0 then identity else if n mod 2 = 0 then let h = mpow m (n / 2) in mul h h else mul m (mpow m (n - 1)) Returns the .b cell after raising to the 30th power -> 832040 = F(30). Tests record literal construction inside recursive function returns, record field access (x.a etc), and pure integer arithmetic in the matrix multiply. 165 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/matrix_power.ml | 20 ++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 29 insertions(+) create mode 100644 lib/ocaml/baseline/matrix_power.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 7e246220..bb4040eb 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -82,6 +82,7 @@ "list_ops.ml": 30, "luhn.ml": 2, "mat_mul.ml": 621, + "matrix_power.ml": 832040, "max_path_tree.ml": 11, "max_product3.ml": 300, "max_run.ml": 5, diff --git a/lib/ocaml/baseline/matrix_power.ml b/lib/ocaml/baseline/matrix_power.ml new file mode 100644 index 00000000..a6b630b3 --- /dev/null +++ b/lib/ocaml/baseline/matrix_power.ml @@ -0,0 +1,20 @@ +type m22 = { a : int; b : int; c : int; d : int } + +let mul x y = + { a = x.a * y.a + x.b * y.c; + b = x.a * y.b + x.b * y.d; + c = x.c * y.a + x.d * y.c; + d = x.c * y.b + x.d * y.d } + +let rec mpow m n = + if n = 0 then { a = 1; b = 0; c = 0; d = 1 } + else if n mod 2 = 0 then + let h = mpow m (n / 2) in mul h h + else + mul m (mpow m (n - 1)) + +;; + +let fib_matrix = { a = 1; b = 1; c = 1; d = 0 } in +let r = mpow fib_matrix 30 in +r.b diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index e79e89ab..1f01de16 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _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 — matrix_power.ml baseline (Fibonacci via + 2×2 matrix fast exponentiation, F(30) = 832040). [[1,1],[1,0]]^n + has Fibonacci numbers in the top row; recursive O(log n) power + via repeated squaring on even n, multiply by base on odd n. + Records `{a; b; c; d}` standing in for matrix entries. F(30) = + 832040 matches the closed form. Tests record literal construction + inside recursive function returns, record field access (x.a etc), + arithmetic on integers (no float). 165 baseline programs total. - 2026-05-10 Phase 5.1 — bipartite.ml baseline (BFS 2-coloring on 7-node cycle-rich graph → bipartite with 4 vertices in color 0). Edges: 0-1, 0-3, 1-2, 1-4, 2-5, 3-4, 3-6, 5-6. This forms a