Skip to content

feat(typing): support approximating modular explicits in let rec#14537

Open
johnyob wants to merge 13 commits intoocaml:trunkfrom
johnyob:approx-moddep
Open

feat(typing): support approximating modular explicits in let rec#14537
johnyob wants to merge 13 commits intoocaml:trunkfrom
johnyob:approx-moddep

Conversation

@johnyob
Copy link
Contributor

@johnyob johnyob commented Feb 10, 2026

Context

This PR builds upon #14394, #14538 to support approximating module dependent function types when typing let rec.

Description

This PR extends type_approx to handle module dependent functions fun (module M : S) -> e.

The approximation is

type_approx env (fun (module M : S) -> e) = 
  with_moddep_param env M S (fun env ->
    type_approx env e) 

The cheeky helper with_moddep_param does the following:

  1. Enter a new scope with a freshly created scoped ident that is used to bind the module type from the package type S
  2. Call the function passed to it with the extended environment
  3. Uses Ctype.instance_funct_opt to replace the scoped ident with an unscoped one
  4. Profit :)

Reviewing

Warning

#14394, #14538 should be reviewed first -- this PR builds directly on top of it

This PR is best reviewed commit-by-commit:

  1. The first commit extracts with_moddep_param from type_moddep_fun
  2. And the second implements the approximation of module dependent types in Type_approx

Testing

make -C testsuite one TEST=tests/typing-modular-explicits/general.ml

@johnyob johnyob changed the title feat(typing): support approximating modular explicits feat(typing): support approximating modular explicits in let rec Feb 10, 2026
@johnyob johnyob force-pushed the approx-moddep branch 2 times, most recently from a24b371 to 36dd00c Compare February 10, 2026 22:57
@johnyob johnyob marked this pull request as ready for review February 10, 2026 22:57
@gasche gasche added the typing label Mar 13, 2026
johnyob added 13 commits March 18, 2026 19:05
This commit also improves the code within `type_approx`.
Making it more robust to errors and using `Typetexp.transl_`
functions to translate `core_type`s
This commit shouldn't change the behaviour
of typechecking. It simply adds some plumbing
for future commits.
…rec`

Previously, pattern inference and approximation were interleaved;
now they are distinct phases:
1. Type the patterns to get the expected types (`new_env`)
2. Build an approximation environment with approximated
   expression types and *generalize* (`approx_env`)
3. Unify approximated types with expected types for compatibility
4. Typecheck expressions using `approx_env`

This separation enables `Type_approx` to produce *polymorphic* approximations
for recursive values with polymorphic type annotations.  Notably, the
`vb_pat_constraint` elaboration is no-longer used in `type_let_rec`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants