From b771ea306c0211514710db7fb1f875f6e9e5ac9f Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 01:02:59 +0000 Subject: [PATCH] ocaml: phase 5.1 bracket_match.ml baseline (5/9 balanced strings) Stack-based multi-bracket parenthesis matching for ( [ { ) ] }. Non-bracket chars are skipped (treated as content). Tests: () yes [{()}] yes ({[}]) no (mismatched closer) "" yes (( no (unclosed) ()[](){} yes (a(b)c) yes (a/b/c skipped) (() no ]) no 5 balanced Body uses begin/end-wrapped match inside while: else if c = ')' || c = ']' || c = '}' then begin match !stack with | [] -> ok := false | top :: rest -> let pair = (c = ')' && top = '(') || (c = ']' && top = '[') || (c = '}' && top = '{') in if pair then stack := rest else ok := false end Tests side-effecting match arms inside while body, ref-of-list as stack, multi-char pairing dispatch. 174 baseline programs total. --- lib/ocaml/baseline/bracket_match.ml | 32 +++++++++++++++++++++++++++++ lib/ocaml/baseline/expected.json | 1 + plans/ocaml-on-sx.md | 9 ++++++++ 3 files changed, 42 insertions(+) create mode 100644 lib/ocaml/baseline/bracket_match.ml diff --git a/lib/ocaml/baseline/bracket_match.ml b/lib/ocaml/baseline/bracket_match.ml new file mode 100644 index 00000000..6e77e74e --- /dev/null +++ b/lib/ocaml/baseline/bracket_match.ml @@ -0,0 +1,32 @@ +let bracket_match s = + let n = String.length s in + let stack = ref [] in + let ok = ref true in + let i = ref 0 in + while !ok && !i < n do + let c = s.[!i] in + if c = '(' || c = '[' || c = '{' then + stack := c :: !stack + else if c = ')' || c = ']' || c = '}' then begin + match !stack with + | [] -> ok := false + | top :: rest -> + let pair = + (c = ')' && top = '(') || + (c = ']' && top = '[') || + (c = '}' && top = '{') + in + if pair then stack := rest else ok := false + end; + i := !i + 1 + done; + if !ok && !stack = [] then 1 else 0 + +;; + +let strings = ["()"; "[{()}]"; "({[}])"; ""; "(("; "()[](){}"; "(a(b)c)"; "(()"; "])"] in +let count = ref 0 in +List.iter (fun s -> + count := !count + bracket_match s +) strings; +!count diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 1980bab3..bf25a668 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -15,6 +15,7 @@ "bipartite.ml": 4, "bits.ml": 21, "balance.ml": 3, + "bracket_match.ml": 5, "base_n.ml": 17, "bfs.ml": 6, "bfs_grid.ml": 8, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index ffe73ce7..ffadc782 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -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-11 Phase 5.1 — bracket_match.ml baseline (multi-bracket + parenthesis matching over 9 test strings, 5 balanced). Stack- + based scan: push openers `( [ {`, pop and pair-check closers. + Sequence: `() [{()}] ({[}]) "" (( ()[](){} (a(b)c) (() ])` → + yes/yes/no/yes/no/yes/yes/no/no = 5. + Tests `begin … match !stack with [] -> … | top :: rest -> … end` + with side-effecting match arms inside a `while` body, ref-of-list + used as a stack, three-way char dispatch via short-circuited + comparisons. 174 baseline programs total. - 2026-05-11 Phase 5.1 — wildcard_match.ml baseline (recursive `*` /`?` wildcard matcher; over 3×6 = 18 (pattern, text) combos, 6 match). Cases: