ocaml: phase 5.1 expr_simp.ml baseline (symbolic simplifier, eval(simp e) = 22)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s

Recursive ADT with three constructors (Num/Add/Mul). simp does
bottom-up rewrite using algebraic identities:

  x + 0  -> x
  0 + x  -> x
  x * 0  -> 0
  0 * x  -> 0
  x * 1  -> x
  1 * x  -> x
  constant folding for Num + Num and Num * Num

Uses tuple pattern in nested match: 'match (simp a, simp b) with'.

  Add (Mul (Num 3, Num 5), Add (Num 0, Mul (Num 1, Num 7)))
   -> simp ->  Add (Num 15, Num 7)
   -> eval ->  22

51 baseline programs total.
This commit is contained in:
2026-05-09 11:09:23 +00:00
parent bafa2410e4
commit aa0a7fa1a2
3 changed files with 42 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
"csv.ml": 10,
"exception_handle.ml": 4,
"expr_eval.ml": 16,
"expr_simp.ml": 22,
"factorial.ml": 3628800,
"fraction.ml": 7,
"frequency.ml": 5,

View File

@@ -0,0 +1,33 @@
type expr =
| Num of int
| Add of expr * expr
| Mul of expr * expr
let rec simp e =
match e with
| Num n -> Num n
| Add (a, b) ->
(match (simp a, simp b) with
| (Num 0, x) -> x
| (x, Num 0) -> x
| (Num n, Num m) -> Num (n + m)
| (a', b') -> Add (a', b'))
| Mul (a, b) ->
(match (simp a, simp b) with
| (Num 0, _) -> Num 0
| (_, Num 0) -> Num 0
| (Num 1, x) -> x
| (x, Num 1) -> x
| (Num n, Num m) -> Num (n * m)
| (a', b') -> Mul (a', b'))
let rec eval e =
match e with
| Num n -> n
| Add (a, b) -> eval a + eval b
| Mul (a, b) -> eval a * eval b
;;
let e = Add (Mul (Num 3, Num 5), Add (Num 0, Mul (Num 1, Num 7))) in
eval (simp e)

View File

@@ -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 — expr_simp.ml baseline (symbolic expression
simplifier, eval (simp e) = 22). Recursive ADT with three
constructors (Num/Add/Mul). simp does bottom-up rewrite using
algebraic identities: x+0 → x, 0+x → x, x*0 → 0, 0*x → 0, x*1 → x,
1*x → x, constant folding both. Uses tuple pattern in nested match
(`match (simp a, simp b) with`). For `Add (Mul (Num 3, Num 5),
Add (Num 0, Mul (Num 1, Num 7)))` → simp → `Add (Num 15, Num 7)`
→ eval → 22. 51 baseline programs total.
- 2026-05-09 Phase 5.1 — mat_mul.ml baseline (3x3 row-major matrix
multiply, sum of result = 621). Triple-nested for loop over
i / j / k with row-major indexing `c.(i * n + j) <- c.(i * n + j) +