Enum polytype::Type [] [src]

pub enum Type {
    Arrow(Box<Arrow>),
    Constructed(&'static strVec<Type>),
    Variable(u32),
}

Represents a type in the Hindley-Milner polymorphic typing system.

Variants

For functions α → β.

If a function has many arguments, use currying.

Examples

let t = Type::Arrow(Box::new(Arrow {
    arg: Type::Variable(0),
    ret: Type::Variable(1),
}));
assert_eq!(format!("{}", &t), "t0 → t1");

With the macros:

let t = arrow![tp!(0), tp!(1), tp!(int), tp!(bool)];
assert_eq!(format!("{}", &t), "t0 → t1 → int → bool");

For primitive or composite types.

Examples

Primitives have no associated types:

let tint = Type::Constructed("int", vec![]);
assert_eq!(format!("{}", &tint), "int")

Composites have associated types:

let tint = Type::Constructed("int", vec![]);
let tlist_of_ints = Type::Constructed("list", vec![tint]);
assert_eq!(format!("{}", &tlist_of_ints), "list(int)");

With the macros:

let t = tp!(list(tp!(int)));
assert_eq!(format!("{}", &t), "list(int)");

For type variables.

Examples

// any function: α → β
let t = arrow![Type::Variable(0), Type::Variable(1)];
assert_eq!(format!("{}", &t), "t0 → t1");

With the macros:

// map: (α → β) → [α] → [β]
let t = arrow![
    arrow![tp!(0), tp!(1)],
    tp!(list(tp!(0))),
    tp!(list(tp!(1))),
];
assert_eq!(format!("{}", &t), "(t0 → t1) → list(t0) → list(t1)");

Methods

impl Type
[src]

[src]

Whether a type has any type variables.

[src]

Applies the type in a context.

This will replace any type variables that have substitutions defined in the context.

Examples

let mut ctx = Context::default();
ctx.unify(&tp!(0), &tp!(int)).expect("unifies");

let t = tp!(list(tp!(0)));
assert_eq!(format!("{}", &t), "list(t0)");
let t = t.apply(&ctx);
assert_eq!(format!("{}", &t), "list(int)");

[src]

Independently instantiates a type in the context.

All type variables will be replaced with new type variables that the context has not seen. Equivalent to calling Type::instantiate with an empty map.

Examples

let mut ctx = Context::default();

let t1 = tp!(list(tp!(3)));
let t2 = tp!(list(tp!(3)));

let t1 = t1.instantiate_indep(&mut ctx);
let t2 = t2.instantiate_indep(&mut ctx);
assert_eq!(format!("{}", &t1), "list(t0)");
assert_eq!(format!("{}", &t2), "list(t1)");

[src]

Dependently instantiates a type in the context.

All type variables will be replaced with new type variables that the context has not seen, unless specified by bindings. Mutates bindings for use with other instantiations, so their type variables are consistent with one another.

Examples

use std::collections::HashMap;

let mut ctx = Context::default();

let t1 = tp!(list(tp!(3)));
let t2 = tp!(list(tp!(3)));

let mut bindings = HashMap::new();
let t1 = t1.instantiate(&mut ctx, &mut bindings);
let t2 = t2.instantiate(&mut ctx, &mut bindings);
assert_eq!(format!("{}", &t1), "list(t0)");
assert_eq!(format!("{}", &t2), "list(t0)");

[src]

Canonicalizes the type by instantiating in an empty context.

Replaces type variables according to bindings.

[src]

Parse a type from a string. This round-trips with Display. This is a leaky operation and should be avoided wherever possible: names of constructed types will remain until program termination.

Examples

let t_par = Type::parse("int -> hashmap(str, list(bool))").expect("valid type");
let t_lit = arrow![tp!(int), tp!(hashmap(tp!(str), tp!(list(tp!(bool)))))];
assert_eq!(t_par, t_lit);

let s = "(t1 → t0 → t1) → t1 → list(t0) → t1";
let t = Type::parse(s).expect("valid type");
let round_trip = format!("{}", &t);
assert_eq!(s, round_trip);

Trait Implementations

impl Debug for Type
[src]

[src]

Formats the value using the given formatter. Read more

impl Clone for Type
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Hash for Type
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl PartialEq for Type
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl Eq for Type
[src]

impl Display for Type
[src]

[src]

Formats the value using the given formatter. Read more

impl From<Arrow> for Type
[src]

[src]

Performs the conversion.

impl From<VecDeque<Type>> for Type
[src]

[src]

Performs the conversion.

impl From<Vec<Type>> for Type
[src]

[src]

Performs the conversion.

Auto Trait Implementations

impl Send for Type

impl Sync for Type