From 63901931c41c55419ccbc390997e8b96469deef8 Mon Sep 17 00:00:00 2001 From: giles Date: Sun, 10 May 2026 02:05:09 +0000 Subject: [PATCH] ocaml: phase 5.1 tail_factorial.ml baseline (12! via tail-recursion = 479001600) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Single-helper tail-recursive loop threading an accumulator: let factorial n = let rec go n acc = if n <= 1 then acc else go (n - 1) (n * acc) in go n 1 factorial 12 = 479_001_600 Companion to factorial.ml (10! = 3628800 via doubly-recursive style); same answer-shape, different evaluator stress: this version has constant stack depth. 130 baseline programs total — milestone. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/tail_factorial.ml | 10 ++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 17 insertions(+) create mode 100644 lib/ocaml/baseline/tail_factorial.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 7fa0f1b5..3d81952e 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -114,6 +114,7 @@ "simpson_int.ml": 10000, "stable_unique.ml": 46, "subseq_check.ml": 3, + "tail_factorial.ml": 479001600, "subset_sum.ml": 8, "tic_tac_toe.ml": 1, "word_freq.ml": 8, diff --git a/lib/ocaml/baseline/tail_factorial.ml b/lib/ocaml/baseline/tail_factorial.ml new file mode 100644 index 00000000..3526cbcc --- /dev/null +++ b/lib/ocaml/baseline/tail_factorial.ml @@ -0,0 +1,10 @@ +let factorial n = + let rec go n acc = + if n <= 1 then acc + else go (n - 1) (n * acc) + in + go n 1 + +;; + +factorial 12 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index cfebefa3..969de697 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _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 — tail_factorial.ml baseline (12! via tail + recursion = 479001600). Single-helper tail-recursive loop + threading an accumulator. Companion to factorial.ml (10! via + doubly-recursive style); same answer-shape, different evaluator + stress (tail-call optimisation if any, or pure constant stack + depth otherwise). 130 baseline programs total — milestone. - 2026-05-10 Phase 5.1 — zerosafe.ml baseline (Option-chained safe division, sum 10 + (-1) + (-1) + 20 = 28). safe_div returns None on division by zero; safe_chain stitches two divisions, propagating