Skip to content

Hindley-milnder type inference#216

Merged
agle merged 22 commits into
mainfrom
hm4
Jul 15, 2026
Merged

Hindley-milnder type inference#216
agle merged 22 commits into
mainfrom
hm4

Conversation

@agle

@agle agle commented Jul 9, 2026

Copy link
Copy Markdown
Owner

This forms the skeleton of the hindley-milner type inference system. This comprises:

  • A minimal hash-consed type expression representation in a union-find structure used for type inference (typeExpr.ml).

This is essentially:

type 'a type_expr = Var of tvar | TypeConstr of 'a list * string

This should be sufficiently general to represent anything we want.

  • Extraction from bincaml expressions to this representation
  • Union-find based HM type unification/inference
  • Elaboration pass over program

I expect the current inference and elaboration to not cover the whole program structure yet.

  • I will write more on the design later to make it reviewable hopefully.
  • I will consider this mergable when we have some tests and a function for local expression type inference which we can use to solve the problem with the boogie backend: Fix the typing of generated functions #208. I've had it floating around a while and want to get it in so I can start the migration without getting conflicts.~~

Similarly it doesn't do anything special to make sure the polymorphic ops get typed, identifying type errors, or let-generalisation. It doesn't do much yet as everything comes in typed already from the frontend; that will need a refactor to not enforce typing immediately.

We still generally use online type propagation from variables to get types, over the explicit typ field in each exprssion node; that will also need to be fixed in order to migrate to this system.

@katrinafyi katrinafyi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first thoughts are that I think the inference.ml should be split up. There's a few separate things happening inside there. Forgive any imprecision but my impression is that it's roughly...

  • expression-level type unification
  • intrinsic-level type scheme definition and instantiation (?)
  • statement-level
  • procedure-level including parameters
  • big everything procedure inference
  • concretisation back to Bincaml types

I won't direct you about details but you get the idea. It would also let you write module-level docs for each idea closer to the code.

Generally, I would also like to see more.. restrained use of single-letter module names and local opens where possible.

Comment thread lib/lang/hm/hm.ml Outdated
Comment thread lib/lang/hm/typeExpr.ml Outdated
@agle agle marked this pull request as ready for review July 14, 2026 04:45

@katrinafyi katrinafyi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important comments/questions

Comment thread lib/lang/hm/inference.ml Outdated
Comment thread lib/lang/hm/solve_bv.ml Outdated
Comment thread lib/lang/hm/solve_bv.ml Outdated
Comment thread lib/lang/hm/unification.ml Outdated
let occurs_in a b =
let check = function
| Var t -> equal_tvar t a
| TypeConstr (ls, _) -> List.fold_left ( || ) false ls

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| TypeConstr (ls, _) -> List.fold_left ( || ) false ls
| TypeConstr (ls, _) -> List.for_all Fun.id ls

Comment thread lib/lang/hm/unification.ml Outdated
Comment thread lib/lang/hm/unification.ml Outdated
agle and others added 2 commits July 14, 2026 16:14
Co-authored-by: Kait <39479354+katrinafyi@users.noreply.github.com>

@katrinafyi katrinafyi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible to review this PR properly, so I am really just making sure that it's documented enough and organised enough.

This will be the last major round of comments.

Comment thread lib/lang/hm/hm.ml Outdated
Comment thread lib/lang/hm/hm_types.ml Outdated
Comment thread lib/lang/hm/solve_bv.ml Outdated
Comment thread lib/lang/hm/unification.ml Outdated
Comment thread lib/lang/hm/unification.ml Outdated
Comment thread lib/lang/hm/unification.ml Outdated
Comment thread lib/lang/hm/hm.ml Outdated
Comment thread lib/lang/hm/elaboration.ml Outdated
Comment thread lib/lang/hm/hm.ml
Comment thread lib/lang/hm/typeExpr.ml
agle and others added 6 commits July 15, 2026 11:38
Co-authored-by: Kait <39479354+katrinafyi@users.noreply.github.com>
Co-authored-by: Kait <39479354+katrinafyi@users.noreply.github.com>
Co-authored-by: Kait <39479354+katrinafyi@users.noreply.github.com>
@agle agle enabled auto-merge (squash) July 15, 2026 03:20
@agle agle disabled auto-merge July 15, 2026 03:32
@agle agle merged commit f8782da into main Jul 15, 2026
9 checks passed
@agle agle deleted the hm4 branch July 15, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants