ocaml: phase 5.1 array literals [|...|] + lis.ml baseline (LIS = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s

Added parser support for OCaml array literal syntax:

  [| e1; e2; ...; en |]  -->  Array.of_list [e1; e2; ...; en]
  [||]                   -->  Array.of_list []

Desugaring keeps the array representation unchanged (ref-of-list)
since Array.of_list is a no-op constructor for that backing.

Tokenizer emits [, |, |, ] as separate ops; parser detects [ followed
by | and enters array-literal mode, terminating on |].

Baseline lis.ml exercises the syntax:

  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|] = 6

137 baseline programs total.
This commit is contained in:
2026-05-10 03:41:19 +00:00
parent 76de0a20f8
commit 551ed44f7f
4 changed files with 70 additions and 0 deletions

View File

@@ -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,

18
lib/ocaml/baseline/lis.ml Normal file
View File

@@ -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|]

View File

@@ -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)))

View File

@@ -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.