ocaml: phase 5 HM with user type declarations (+6 tests, 363 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s

ocaml-hm-ctors is now a mutable list cell; user type-defs register
their constructors via ocaml-hm-register-type-def!. New
ocaml-type-of-program processes top-level decls in order:
- type-def: register ctors with the scheme inferred from PARAMS+CTORS
- def/def-rec: generalize and bind in the type env
- exception-def: no-op for typing
- expr: return inferred type

Examples:
  type color = Red | Green | Blue;; Red : color
  type shape = Circle of int | Square of int;;
  let area s = match s with
    | Circle r -> r * r
    | Square s -> s * s;;
  area : shape -> Int

Caveat: ctor arg types parsed as raw source strings; registry defaults
to int for any single-arg ctor. Proper type-source parsing pending.
This commit is contained in:
2026-05-08 13:12:07 +00:00
parent 5bc7895ce0
commit 756d5fba64
3 changed files with 141 additions and 11 deletions

View File

@@ -227,10 +227,12 @@ SX CEK evaluator (both JS and OCaml hosts)
pattern matching, let-rec, modules.)_
- [x] Type variables: `'a`, `'b`; unification with occur-check (kit).
- [x] Let-polymorphism: generalise at let-bindings (kit `hm-generalize`).
- [~] ADT types: `option`/`result` ctors hardcoded in
`ocaml-hm-ctor-env`; `:con NAME` and `:pcon NAME …` look up the
scheme and instantiate. User type-defs would extend the registry.
Format: `Int option`, `(Int, 'b) result`.
- [x] ADT types: `option`/`result` ctors seeded;
`ocaml-hm-register-type-def!` registers user types from `:type-def`.
`ocaml-type-of-program` threads decls through the env, registering
types and binding `let` schemes. `:con NAME` / `:pcon NAME …`
instantiate from the registry. _(Caveat: ctor arg types currently
default to `int` — proper type parsing pending.)_
- [~] Function types `T1 -> T2` work; tuples (`'a * 'b`) and lists
(`'a list`) supported. Records pending.
- [ ] Type signatures: `val f : int -> int` — verify against inferred type.
@@ -368,6 +370,18 @@ the "mother tongue" closure: OCaml → SX → OCaml. This means:
_Newest first._
- 2026-05-08 Phase 5 — HM with user `type` declarations (+6 tests, 363
total). `ocaml-hm-ctors` is now a mutable list cell; user type-defs
register their constructors via `ocaml-hm-register-type-def!`. New
`ocaml-type-of-program` processes top-level decls in order: type-def
registers ctors, def/def-rec generalize, exception-def is a no-op,
expr returns its inferred type. Examples:
`type color = Red | Green | Blue;; Red : color`
`type shape = Circle of int | Square of int;; let area s = match s
with | Circle r -> r * r | Square s -> s * s;; area : shape -> Int`
Caveat: ctor arg types parsed as raw source strings; the registry
defaults to `int` for any single-arg ctor. Proper type-source parsing
is pending.
- 2026-05-08 Phase 5 — HM let-rec inference + `::`/`@` operator types
(+6 tests, 357 total). `ocaml-infer-let-rec` pre-binds the function
name to a fresh tv, infers rhs (which can recursively call name),