Crates.io | polytype |
lib.rs | polytype |
version | 7.0.1 |
source | src |
created_at | 2018-02-15 00:59:04.58611 |
updated_at | 2023-12-15 21:22:10.912368 |
description | A Hindley-Milner polymorphic typing system. |
homepage | https://github.com/lorepozo/polytype-rs |
repository | https://github.com/lorepozo/polytype-rs |
max_upload_size | |
id | 51223 |
size | 76,963 |
A Hindley-Milner polymorphic typing system. Implements type inference via unification.
[dependencies]
polytype = "7.0"
polytype
provides the
TypeScheme
and
Type
enums, the
Context
struct, and the
tp!
and
ptp!
macros which
help to concisely create types and type schemes.
Unification:
let mut ctx = Context::default();
// t1: list(int → α) ; t2: list(β → bool)
let t1 = tp!(list(tp!(@arrow[tp!(int), tp!(0)])));
let t2 = tp!(list(tp!(@arrow[tp!(1), tp!(bool)])));
ctx.unify(&t1, &t2).expect("unifies");
let t1 = t1.apply(&ctx);
let t2 = t2.apply(&ctx);
assert_eq!(t1, t2); // list(int → bool)
Apply a type context:
let mut ctx = Context::default();
// assign t0 to int
ctx.extend(0, tp!(int));
let t = tp!(list(tp!(0)));
assert_eq!(t.to_string(), "list(t0)");
let t = t.apply(&ctx);
assert_eq!(t.to_string(), "list(int)");
Instantiate a TypeScheme
:
let mut ctx = Context::default();
// ∀α. list(α)
let scheme = ptp!(3; tp!(list(tp!(3))));
// They instantiate to new fresh type variables
let t1 = scheme.instantiate(&mut ctx);
let t2 = scheme.instantiate(&mut ctx);
assert_eq!(t1.to_string(), "list(t0)");
assert_eq!(t2.to_string(), "list(t1)");
See the documentation for more details.
By default polytype
includes a type parser that can be invoked with
str::parse.
This can be disabled with default-features = false
.