ocaml: phase 4 basic labeled / optional argument syntax (label dropped) (+3 tests, 562 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s

Three parser changes:

  1. at-app-start? returns true on op '~' or '?' so the app loop
     keeps consuming labeled args.
  2. The app arg parser handles:
       ~name:VAL    drop label, parse VAL as the arg
       ?name:VAL    same
       ~name        punning -- treat as (:var name)
       ?name        same
  3. try-consume-param! drops '~' or '?' and treats the following
     ident as a regular positional param name.

Caveats:
  - Order in the call must match definition order; we don't reorder
    by label name.
  - Optional args don't auto-wrap in Some, so the function body sees
    the raw value for ?x:V.

Lets us write idiomatic-looking OCaml even though the runtime is
positional underneath:

  let f ~x ~y = x + y in f ~x:3 ~y:7         = 10
  let x = 4 in let y = 5 in f ~x ~y          = 20  (punning)
  let f ?x ~y = x + y in f ?x:1 ~y:2         = 3
This commit is contained in:
2026-05-09 05:12:34 +00:00
parent 7c40506571
commit 7773c40337
3 changed files with 53 additions and 0 deletions

View File

@@ -407,6 +407,20 @@ _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 4 — basic labeled / optional argument syntax
(label dropped, positional semantics) (+3 tests, 562 total). Three
parser changes:
(1) `at-app-start?` returns true on op `~` or `?` so the app loop
keeps consuming labeled args;
(2) the app arg parser handles `~name:VAL` (drop label, parse VAL),
`?name:VAL` (same), and `~name` punning (treat as `(:var name)`);
(3) `try-consume-param!` drops `~` / `?` and treats the following
ident as a regular positional param name.
Order in the call must match definition order — we don't reorder
args by label name. Optional args don't auto-wrap in Some, so the
function body sees the raw value for `?x:V`. Lets us write
`let f ~x ~y = x + y in f ~x:3 ~y:7` and `let x = 4 in let y = 5
in f ~x ~y` (punning).
- 2026-05-09 Phase 5.1 — merge_sort.ml baseline (user-implemented
mergesort, sorted sum = 44). Stress-tests `let (a, b) = split rest
in (x :: a, y :: b)` (let-tuple destructuring inside a recursive