Enum polytype::TypeSchema[][src]

pub enum TypeSchema<N: Name = &'static str> {
    Monotype(Type<N>),
    Polytype {
        variable: Variable,
        body: Box<TypeSchema<N>>,
    },
}
Expand description

Represents polytypes (uninstantiated, universally quantified types).

The primary ways of creating a TypeSchema are with the ptp! macro or with Type::generalize.

Variants

Monotype(Type<N>)

Tuple Fields

0: Type<N>

Non-polymorphic types (e.g. α → β, int → bool)

Polytype

Fields

variable: Variable

The Variable being bound

body: Box<TypeSchema<N>>

The type in which variable is bound

Polymorphic types (e.g. ∀α. α → α, ∀α. ∀β. α → β)

Implementations

Parse a TypeSchema 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.

The “for-all” is optional.

Examples
let t_par = TypeSchema::parse("∀t0. t0 -> t0").expect("valid type");
let t_lit = ptp!(0; @arrow[tp!(0), tp!(0)]);
assert_eq!(t_par, t_lit);

let s = "∀t0. ∀t1. (t1 → t0 → t1) → t1 → list(t0) → t1";
let t: TypeSchema<&'static str> = TypeSchema::parse(s).expect("valid type");
let round_trip = t.to_string();
assert_eq!(s, round_trip);

Checks whether a variable is bound in the quantification of a polytype.

Examples
let t = ptp!(0; @arrow[tp!(0), tp!(1)]); // ∀α. α → β
assert!(t.is_bound(0));
assert!(!t.is_bound(1));

Returns a set of each Variable bound by the TypeSchema.

Examples
let t = ptp!(0, 1; @arrow[tp!(1), tp!(2), tp!(3)]); // ∀α. ∀β. β → ɣ → δ
assert_eq!(t.bound_vars(), vec![0, 1]);

Returns a set of each free Variable in the TypeSchema.

Examples
let t = ptp!(0, 1; @arrow[tp!(1), tp!(2), tp!(3)]); // ∀α. ∀β. β → ɣ → δ
let mut free = t.free_vars();
free.sort();
assert_eq!(free, vec![2, 3]);

Instantiate a TypeSchema in the context by removing quantifiers.

All type variables will be replaced with fresh type variables.

Examples
let mut ctx = Context::default();

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

let t1 = t1.instantiate(&mut ctx);
let t2 = t2.instantiate(&mut ctx);
assert_eq!(t1.to_string(), "list(t0)");
assert_eq!(t2.to_string(), "list(t1)");

Like instantiate, but works in-place.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

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

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

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.