Enum polytype::Type
[−]
[src]
pub enum Type { Arrow(Box<Arrow>), Constructed(&'static str, Vec<Type>), Variable(u32), }
Represents a type in the Hindley-Milner polymorphic typing system.
Variants
Arrow(Box<Arrow>)
For functions α → β
.
If a function has many arguments, use currying.
Examples
let t = Type::Arrow(Box::new(Arrow { arg: Type::Variable(0), ret: Type::Variable(1), })); assert_eq!(format!("{}", &t), "t0 → t1");
With the macros:
let t = arrow![tp!(0), tp!(1), tp!(int), tp!(bool)]; assert_eq!(format!("{}", &t), "t0 → t1 → int → bool");
Constructed(&'static str, Vec<Type>)
For primitive or composite types.
Examples
Primitives have no associated types:
let tint = Type::Constructed("int", vec![]); assert_eq!(format!("{}", &tint), "int")
Composites have associated types:
let tint = Type::Constructed("int", vec![]); let tlist_of_ints = Type::Constructed("list", vec![tint]); assert_eq!(format!("{}", &tlist_of_ints), "list(int)");
With the macros:
let t = tp!(list(tp!(int))); assert_eq!(format!("{}", &t), "list(int)");
Variable(u32)
For type variables.
Examples
// any function: α → β let t = arrow![Type::Variable(0), Type::Variable(1)]; assert_eq!(format!("{}", &t), "t0 → t1");
With the macros:
// map: (α → β) → [α] → [β] let t = arrow![ arrow![tp!(0), tp!(1)], tp!(list(tp!(0))), tp!(list(tp!(1))), ]; assert_eq!(format!("{}", &t), "(t0 → t1) → list(t0) → list(t1)");
Methods
impl Type
[src]
pub fn is_polymorphic(&self) -> bool
[src]
Whether a type has any type variables.
pub fn apply(&self, ctx: &Context) -> Type
[src]
Applies the type in a context.
This will replace any type variables that have substitutions defined in the context.
Examples
let mut ctx = Context::default(); ctx.unify(&tp!(0), &tp!(int)).expect("unifies"); let t = tp!(list(tp!(0))); assert_eq!(format!("{}", &t), "list(t0)"); let t = t.apply(&ctx); assert_eq!(format!("{}", &t), "list(int)");
pub fn instantiate_indep(&self, ctx: &mut Context) -> Type
[src]
Independently instantiates a type in the context.
All type variables will be replaced with new type variables that the context has not seen.
Equivalent to calling Type::instantiate
with an empty map.
Examples
let mut ctx = Context::default(); let t1 = tp!(list(tp!(3))); let t2 = tp!(list(tp!(3))); let t1 = t1.instantiate_indep(&mut ctx); let t2 = t2.instantiate_indep(&mut ctx); assert_eq!(format!("{}", &t1), "list(t0)"); assert_eq!(format!("{}", &t2), "list(t1)");
pub fn instantiate(
&self,
ctx: &mut Context,
bindings: &mut HashMap<u32, Type>
) -> Type
[src]
&self,
ctx: &mut Context,
bindings: &mut HashMap<u32, Type>
) -> Type
Dependently instantiates a type in the context.
All type variables will be replaced with new type variables that the context has not seen, unless specified by bindings. Mutates bindings for use with other instantiations, so their type variables are consistent with one another.
Examples
use std::collections::HashMap; let mut ctx = Context::default(); let t1 = tp!(list(tp!(3))); let t2 = tp!(list(tp!(3))); let mut bindings = HashMap::new(); let t1 = t1.instantiate(&mut ctx, &mut bindings); let t2 = t2.instantiate(&mut ctx, &mut bindings); assert_eq!(format!("{}", &t1), "list(t0)"); assert_eq!(format!("{}", &t2), "list(t0)");
pub fn canonical(&self, bindings: &mut HashMap<u32, Type>) -> Type
[src]
Canonicalizes the type by instantiating in an empty context.
Replaces type variables according to bindings.
pub fn parse(s: &str) -> Result<Type, ()>
[src]
Parse a type 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.
Examples
let t_par = Type::parse("int -> hashmap(str, list(bool))").expect("valid type"); let t_lit = arrow![tp!(int), tp!(hashmap(tp!(str), tp!(list(tp!(bool)))))]; assert_eq!(t_par, t_lit); let s = "(t1 → t0 → t1) → t1 → list(t0) → t1"; let t = Type::parse(s).expect("valid type"); let round_trip = format!("{}", &t); assert_eq!(s, round_trip);
Trait Implementations
impl Debug for Type
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl Clone for Type
[src]
fn clone(&self) -> Type
[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 Type
[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 Type
[src]
fn eq(&self, __arg_0: &Type) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Type) -> bool
[src]
This method tests for !=
.
impl Eq for Type
[src]
impl Display for Type
[src]
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
[src]
Formats the value using the given formatter. Read more