diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 90b61eb0..d2b6abc9 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -68,6 +68,7 @@ "fizz_classifier.ml": 540, "fizzbuzz.ml": 57, "flatten_tree.ml": 28, + "lis.ml": 6, "list_ops.ml": 30, "luhn.ml": 2, "mat_mul.ml": 621, diff --git a/lib/ocaml/baseline/lis.ml b/lib/ocaml/baseline/lis.ml new file mode 100644 index 00000000..3da1c520 --- /dev/null +++ b/lib/ocaml/baseline/lis.ml @@ -0,0 +1,18 @@ +let lis arr = + let n = Array.length arr in + let dp = Array.make n 1 in + for i = 1 to n - 1 do + for j = 0 to i - 1 do + if arr.(j) < arr.(i) && dp.(j) + 1 > dp.(i) then + dp.(i) <- dp.(j) + 1 + done + done; + let best = ref 0 in + for i = 0 to n - 1 do + if dp.(i) > !best then best := dp.(i) + done; + !best + +;; + +lis [|10; 22; 9; 33; 21; 50; 41; 60; 80|] diff --git a/lib/ocaml/parser.sx b/lib/ocaml/parser.sx index bd2b77d8..92773071 100644 --- a/lib/ocaml/parser.sx +++ b/lib/ocaml/parser.sx @@ -468,6 +468,47 @@ (advance-tok!) (cond ((at-op? "]") (begin (advance-tok!) (list :list))) + ;; Array literal `[| e1; e2; ...; en |]` desugars to + ;; `Array.of_list [e1; e2; ...; en]`. Empty `[||]` + ;; → `Array.of_list []`. + ((at-op? "|") + (begin + (advance-tok!) + (cond + ((at-op? "|") + (begin + (advance-tok!) + (consume! "op" "]") + (list :app + (list :field (list :con "Array") + "of_list") + (list :list)))) + (else + (let + ((items (list))) + (begin + (append! items (parse-expr-no-seq)) + (define + aloop + (fn + () + (when + (at-op? ";") + (begin + (advance-tok!) + (when + (not (at-op? "|")) + (begin + (append! items + (parse-expr-no-seq)) + (aloop))))))) + (aloop) + (consume! "op" "|") + (consume! "op" "]") + (list :app + (list :field (list :con "Array") + "of_list") + (cons :list items)))))))) (else (let ((items (list))) diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index bb56d80e..ba8e479f 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,16 @@ _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 — array literals + lis.ml baseline (longest + increasing subsequence on a 9-element array = 6). Added parser + support for `[| e1; e2; ...; en |]` syntax: desugars to + `Array.of_list [e1; e2; ...; en]` (which is `ref [...]`). Empty + `[||]` → `Array.of_list []`. Tokenizer leaves `[`, `|`, `|`, `]` + as separate ops; parser detects `[` followed by `|` to switch to + array-literal mode and consumes `|` then `]` at the end. + Used in `lis [|10; 22; 9; 33; 21; 50; 41; 60; 80|] = 6`. + Tests Array.length, .(i)<-/.(i), nested for + ref. + 137 baseline programs total. - 2026-05-10 Phase 5.1 — josephus.ml baseline (Josephus problem, n=50 k=3 → survivor at position 11, 1-indexed). Uses the classic recursive formula: J(1, k) = 0; J(n, k) = (J(n-1, k) + k) mod n.