Skip to content

joshrule/polytype-rs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

polytype

Build Status crates.io docs.rs

A Hindley-Milner polymorphic typing system. Implements type inference via unification.

Usage

[dependencies]
polytype = "6.2"

polytype provides the TypeSchema and Type enums, the Context struct, and the tp! and ptp! macros which help to concisely create types and type schemas.

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 TypeSchema:

let mut ctx = Context::default();

// ∀α. list(α)
let schema = ptp!(3; tp!(list(tp!(3))));

// They instantiate to new fresh type variables
let t1 = schema.instantiate(&mut ctx);
let t2 = schema.instantiate(&mut ctx);
assert_eq!(t1.to_string(), "list(t0)");
assert_eq!(t2.to_string(), "list(t1)");

See the documentation for more details.

Features

By default polytype includes a type parser that can be invoked with Type::parse. This can be disabled with default-features = false.

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%