ocaml: phase 5.1 atm.ml baseline (mutable record + exception + try/with, balance = 120)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s

Models a bank account using a mutable record + a user exception:

  type account = { mutable balance : int }
  exception Insufficient

  let withdraw acct amt =
    if amt > acct.balance then raise Insufficient
    else acct.balance <- acct.balance - amt

Sequence:
  start             100
  deposit 50        150
  withdraw 30       120
  withdraw 200      raises Insufficient
  handler returns   acct.balance        (= 120, transaction rolled back)

Combines mutable record fields, user exception declaration,
try-with-bare-pattern, and verifies that a raise in the middle of a
sequence doesn't leave a partial mutation.

59 baseline programs total.
This commit is contained in:
2026-05-09 12:34:36 +00:00
parent 097c7f4590
commit f5122a9a5d
3 changed files with 27 additions and 0 deletions

17
lib/ocaml/baseline/atm.ml Normal file
View File

@@ -0,0 +1,17 @@
type account = { mutable balance : int }
exception Insufficient
let withdraw acct amt =
if amt > acct.balance then raise Insufficient
else acct.balance <- acct.balance - amt
let deposit acct amt = acct.balance <- acct.balance + amt
;;
let a = { balance = 100 } in
deposit a 50;
withdraw a 30;
try (withdraw a 200; -1)
with Insufficient -> a.balance

View File

@@ -2,6 +2,7 @@
"ackermann.ml": 125,
"anagram_check.ml": 2,
"anagrams.ml": 3,
"atm.ml": 120,
"bag.ml": 3,
"bf_full.ml": 6,
"bigint_add.ml": 28,

View File

@@ -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-09 Phase 5.1 — atm.ml baseline (mutable record + custom
exception + try/with, balance 120 after rollback). Models a bank
account: deposit/withdraw mutate the balance field; an over-draw
raises `Insufficient` which the caller catches and falls back to
the unchanged balance. Starting at 100, +50 = 150, -30 = 120,
attempted -200 throws and the handler returns 120. Combines
mutable record fields, user exception declaration, and
try-with-bare-pattern in a realistic micro-pattern. 59 baseline
programs total.
- 2026-05-09 Phase 5.1 — bf_full.ml baseline (Brainfuck interpreter
with `[`/`]` loops, `+++[.-]` → 3+2+1 = 6). Extends the iter-92
brainfuck.ml subset with bracket matching: `[` jumps past matching