Enum polytype::TypeScheme

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

Represents polytypes (uninstantiated, universally quantified types).

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

Variants§

§

Monotype(Type<N>)

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

§

Polytype

Fields

§variable: Variable

The Variable being bound

§body: Box<TypeScheme<N>>

The type in which variable is bound

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

Implementations§

source§

impl<N: Name> TypeScheme<N>

source

pub fn is_bound(&self, v: Variable) -> bool

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));
source

pub fn bound_vars(&self) -> Vec<Variable>

Returns a set of each Variable bound by the TypeScheme.

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

pub fn free_vars(&self) -> Vec<Variable>

Returns a set of each free Variable in the TypeScheme.

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]);
source

pub fn instantiate(&self, ctx: &mut Context<N>) -> Type<N>

Instantiate a TypeScheme 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)");
source

pub fn instantiate_owned(self, ctx: &mut Context<N>) -> Type<N>

Like instantiate, but works in-place.

Trait Implementations§

source§

impl<N: Clone + Name> Clone for TypeScheme<N>

source§

fn clone(&self) -> TypeScheme<N>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<N: Debug + Name> Debug for TypeScheme<N>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<N: Name> Display for TypeScheme<N>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<N: Name> FromStr for TypeScheme<N>

source§

fn from_str(s: &str) -> Result<TypeScheme<N>, ParseError>

Parse a TypeScheme 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: TypeScheme = "∀t0. t0 -> t0".parse().expect("invalid 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: TypeScheme<&'static str> = s.parse().expect("invalid type");
let round_trip = t.to_string();
assert_eq!(s, round_trip);
§

type Err = ParseError

The associated error which can be returned from parsing.
source§

impl<N: Hash + Name> Hash for TypeScheme<N>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<N: PartialEq + Name> PartialEq for TypeScheme<N>

source§

fn eq(&self, other: &TypeScheme<N>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<N: Eq + Name> Eq for TypeScheme<N>

source§

impl<N: Name> StructuralEq for TypeScheme<N>

source§

impl<N: Name> StructuralPartialEq for TypeScheme<N>

Auto Trait Implementations§

§

impl<N> RefUnwindSafe for TypeScheme<N>
where N: RefUnwindSafe,

§

impl<N> Send for TypeScheme<N>
where N: Send,

§

impl<N> Sync for TypeScheme<N>
where N: Sync,

§

impl<N> Unpin for TypeScheme<N>
where N: Unpin,

§

impl<N> UnwindSafe for TypeScheme<N>
where N: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

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

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.