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
Polymorphic types (e.g. ∀α. α → α, ∀α. ∀β. α → β)
Implementations§
Source§impl<N: Name> TypeScheme<N>
impl<N: Name> TypeScheme<N>
Sourcepub fn is_bound(&self, v: Variable) -> bool
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));Sourcepub fn bound_vars(&self) -> Vec<Variable> ⓘ
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]);Sourcepub fn free_vars(&self) -> Vec<Variable> ⓘ
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]);Sourcepub fn instantiate(&self, ctx: &mut Context<N>) -> Type<N>
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)");Sourcepub fn instantiate_owned(self, ctx: &mut Context<N>) -> Type<N>
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>
impl<N: Clone + Name> Clone for TypeScheme<N>
Source§fn clone(&self) -> TypeScheme<N>
fn clone(&self) -> TypeScheme<N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<N: Name> Display for TypeScheme<N>
impl<N: Name> Display for TypeScheme<N>
Source§impl<N: Name> FromStr for TypeScheme<N>
impl<N: Name> FromStr for TypeScheme<N>
Source§fn from_str(s: &str) -> Result<TypeScheme<N>, ParseError>
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);Source§type Err = ParseError
type Err = ParseError
impl<N: Eq + Name> Eq for TypeScheme<N>
impl<N: Name> StructuralPartialEq for TypeScheme<N>
Auto Trait Implementations§
impl<N> Freeze for TypeScheme<N>where
N: Freeze,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more