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
body: Box<TypeScheme<N>>
The type in which variable
is bound
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);
§type Err = ParseError
type Err = ParseError
source§impl<N: PartialEq + Name> PartialEq for TypeScheme<N>
impl<N: PartialEq + Name> PartialEq for TypeScheme<N>
source§fn eq(&self, other: &TypeScheme<N>) -> bool
fn eq(&self, other: &TypeScheme<N>) -> bool
self
and other
values to be equal, and is used
by ==
.impl<N: Eq + Name> Eq for TypeScheme<N>
impl<N: Name> StructuralEq for TypeScheme<N>
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> 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
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.