Enum polytype::TypeSchema
[−]
[src]
pub enum TypeSchema {
Monotype(Type),
Polytype {
variable: Variable,
body: Box<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)Non-polymorphic types (e.g. α → β, int → bool)
PolytypePolymorphic types (e.g. ∀α. α → α, ∀α. ∀β. α → β)
Fields of Polytype
variable: Variable | The |
body: Box<TypeSchema> | The type in which |
Methods
impl TypeSchema[src]
pub fn bound_variables(&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_variables(), vec![0, 1], );
pub fn is_bound(&self, v: Variable) -> bool[src]
pub fn free_vars(&self, ctx: &Context) -> Vec<Variable>[src]
Returns a set of each free Variable in the TypeSchema.
pub fn free_vars_internal(&self, ctx: &Context, s: &mut HashSet<Variable>)[src]
pub fn instantiate(&self, ctx: &mut Context) -> Type[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!(format!("{}", &t1), "list(t0)"); assert_eq!(format!("{}", &t2), "list(t1)");
pub fn instantiate_owned(self, ctx: &mut Context) -> Type[src]
Like instantiate, but works in-place.
pub fn parse(s: &str) -> Result<TypeSchema, ()>[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::parse(s).expect("valid type"); let round_trip = format!("{}", &t); assert_eq!(s, round_trip);
Trait Implementations
impl Debug for TypeSchema[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result[src]
Formats the value using the given formatter. Read more
impl Clone for TypeSchema[src]
fn clone(&self) -> TypeSchema[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
Performs copy-assignment from source. Read more
impl Hash for TypeSchema[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)[src]
Feeds this value into the given [Hasher]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher]. Read more
impl PartialEq for TypeSchema[src]
fn eq(&self, __arg_0: &TypeSchema) -> bool[src]
This method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, __arg_0: &TypeSchema) -> bool[src]
This method tests for !=.