[−][src]Enum polytype::TypeSchema
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>)
Non-polymorphic types (e.g. α → β
, int → bool
)
Polytype
Polymorphic types (e.g. ∀α. α → α
, ∀α. ∀β. α → β
)
Fields of Polytype
variable: Variable
The Variable
being bound
body: Box<TypeSchema<N>>
The type in which variable
is bound
Methods
impl<N: Name> TypeSchema<N>
[src]
pub fn is_bound(&self, v: Variable) -> bool
[src]
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));
pub fn bound_vars(&self) -> Vec<Variable>
[src]
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]);
pub fn free_vars(&self) -> Vec<Variable>
[src]
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]);
pub fn instantiate(&self, ctx: &mut Context<N>) -> Type<N>
[src]
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)");
pub fn instantiate_owned(self, ctx: &mut Context<N>) -> Type<N>
[src]
Like instantiate
, but works in-place.
pub fn parse(s: &str) -> Result<TypeSchema<N>, ()>
[src]
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);
Trait Implementations
impl<N: PartialEq + Name> PartialEq<TypeSchema<N>> for TypeSchema<N>
[src]
fn eq(&self, other: &TypeSchema<N>) -> bool
[src]
fn ne(&self, other: &TypeSchema<N>) -> bool
[src]
impl<N: Clone + Name> Clone for TypeSchema<N>
[src]
fn clone(&self) -> TypeSchema<N>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<N: Eq + Name> Eq for TypeSchema<N>
[src]
impl<N: Name> Display for TypeSchema<N>
[src]
impl<N: Debug + Name> Debug for TypeSchema<N>
[src]
impl<N: Hash + Name> Hash for TypeSchema<N>
[src]
Auto Trait Implementations
impl<N> Send for TypeSchema<N> where
N: Send,
N: Send,
impl<N> Sync for TypeSchema<N> where
N: Sync,
N: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,