https://github.com/bynect/constraint-inference
OCaml implementation of a constraint-based bottom-up type inference algorithm
https://github.com/bynect/constraint-inference
constraints type-checking type-inference type-system
Last synced: 9 months ago
JSON representation
OCaml implementation of a constraint-based bottom-up type inference algorithm
- Host: GitHub
- URL: https://github.com/bynect/constraint-inference
- Owner: bynect
- License: cc0-1.0
- Created: 2021-08-27T14:41:11.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-15T19:57:23.000Z (over 4 years ago)
- Last Synced: 2025-06-20T16:11:27.163Z (9 months ago)
- Topics: constraints, type-checking, type-inference, type-system
- Language: OCaml
- Homepage:
- Size: 129 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Constraint inference
This repository contains an indipendent implementation of the type
inference algorithm described in the paper
[Generalizing Hindley-Milner Type Inference Algorithms](#ref1).
The paper [Constraint Based Type Inferencing in Helium](#ref2) describes
similar forms of type constraint.
## Implementation
All the code contained in this repository is public domain.
There may be small divergences between this implementation and
the paper pseudo-implementation.
This repo contains the following branches:
- [main](https://github.com/bynect/constraint-inference/tree/main) Original algorithm implementation with some additions, unstable.
- [original](https://github.com/bynect/constraint-inference/tree/original) Original algorithm implementation with detailed comments.
- [clean](https://github.com/bynect/constraint-inference/tree/clean) A copy of the original branch without the comments.
- [parser](https://github.com/bynect/constraint-inference/tree/parser) A copy of the original branch with the addition of a parser and repl.
- [if-expr](https://github.com/bynect/constraint-inference/tree/if-expr) Same as the parser branch with the addition of n-tuples and if expressions.
## Grammar
The `main` branch contains a parser and a little repl to check what types is inferred to an expression.
Additionally, the `main` branch extends the algorithm described in the paper with n-tuples and if expressions.
There is also the addition of a limited form of recursive bindings.
```txt
E ::=
| x
| E1 E2
| \x -> E
| let x = E1 in E2
| rec x = E1 in E2
| E1, ..., En
| lit
```
### Example
```txt
rec fact = \n -> if eq n 1
then 1
else mul n (fact (sub n 1))
in fact
```
```txt
rec fib = \n -> if eq n 1
then n
else fib (add ((sub n 1)) (fib (sub n 2)))
in fib
```
## References
[[1][paper-1]] Bastiaan Heeren, Jurriaan Hage, and Doaitse Swierstra.
Generalizing Hindley-Milner Type Inference Algorithms. Institute of Information and Computing Sciences,
Utrecht University, Netherlands, 2002.
[[2][paper-2]] Bastiaan Heeren, Jurriaan Hage, and S. Doaitse Swierstra.
Constraint Based Type Inferencing in Helium. Institute of Information and Computing Science,
Universiteit Utrecht, Netherlands, 2003.
[paper-1]: http://www.cs.uu.nl/research/techreps/repo/CS-2002/2002-031.pdf
[paper-2]: http://www.open.ou.nl/bhr/heeren-cp03.pdf