From b7b841821ccc78ac7cc01f29330203a307b44109 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 9 May 2026 16:38:09 +0000 Subject: [PATCH] ocaml: phase 5.1 peano.ml baseline (Peano arithmetic, 5*6 = 30) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Defines unary Peano numerals with two recursive functions for arithmetic: type peano = Zero | Succ of peano let rec plus a b = match a with | Zero -> b | Succ a' -> Succ (plus a' b) let rec mul a b = match a with | Zero -> Zero | Succ a' -> plus b (mul a' b) mul is defined inductively: mul Zero _ = Zero; mul (Succ a) b = b + (a * b). to_int (mul (from_int 5) (from_int 6)) = 30 The result is a Peano value with 30 nested Succ wrappers; to_int unrolls them to a host int. Tests recursive ADT with a single-arg constructor + four mutually-defined recursive functions (no rec/and needed since each is defined separately). 80 baseline programs total — milestone. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/peano.ml | 21 +++++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 30 insertions(+) create mode 100644 lib/ocaml/baseline/peano.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 99345527..5bf23786 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -56,6 +56,7 @@ "paren_depth.ml": 7, "pancake_sort.ml": 910, "pascal.ml": 252, + "peano.ml": 30, "pi_leibniz.ml": 314, "prefix_sum.ml": 66, "pretty_table.ml": 64, diff --git a/lib/ocaml/baseline/peano.ml b/lib/ocaml/baseline/peano.ml new file mode 100644 index 00000000..fa39a3cc --- /dev/null +++ b/lib/ocaml/baseline/peano.ml @@ -0,0 +1,21 @@ +type peano = Zero | Succ of peano + +let rec to_int p = match p with + | Zero -> 0 + | Succ p' -> 1 + to_int p' + +let rec from_int n = + if n = 0 then Zero + else Succ (from_int (n - 1)) + +let rec plus a b = match a with + | Zero -> b + | Succ a' -> Succ (plus a' b) + +let rec mul a b = match a with + | Zero -> Zero + | Succ a' -> plus b (mul a' b) + +;; + +to_int (mul (from_int 5) (from_int 6)) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 4c4d3507..cb7d3e01 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-09 Phase 5.1 — peano.ml baseline (Peano arithmetic, 5*6 = + 30). Defines `type peano = Zero | Succ of peano` and four + recursive functions: to_int, from_int, plus, mul. Multiplication + is defined inductively: `mul Zero _ = Zero; mul (Succ a) b = plus + b (mul a b)`. The result of `mul (from_int 5) (from_int 6)` is a + Peano number with 30 nested Succ wrappers. Tests recursive ADT + with single-arg constructor + recursive function bodies. 80 + baseline programs total — milestone. - 2026-05-09 Phase 5.1 — count_change.ml baseline (number of ways to make 50c from [1;2;5;10;25] = 406). Companion to coin_change.ml (min coins): instead of minimising, this counts distinct