pub enum Type {
Variable(Variable),
Operator(Operator),
}
Expand description
A Type::Variable or Type::Operator:
- A Type::Variable can be either bound or unbound (instance: Some(_) | None)
- A Type::Operator has a name (used to identify the operator) and a list of types.
A type which contains unbound variables is considered “generic” (see
Type::is_generic()
).
Variants§
Implementations§
source§impl Type
impl Type
sourcepub fn new_var() -> RcType
pub fn new_var() -> RcType
Creates a new unbound type variable
sourcepub fn new_inst(of: &RcType) -> RcType
pub fn new_inst(of: &RcType) -> RcType
Creates a variable that is a new instance of another Type
sourcepub fn new_fn(takes: &RcType, returns: &RcType) -> RcType
pub fn new_fn(takes: &RcType, returns: &RcType) -> RcType
Creates a new type operator representing a lambda
sourcepub fn new_prim(name: Sym) -> RcType
pub fn new_prim(name: Sym) -> RcType
Creates a new type operator representing a primitive type
sourcepub fn new_tuple(members: &[RcType]) -> RcType
pub fn new_tuple(members: &[RcType]) -> RcType
Creates a new type operator representing a tuple
sourcepub fn set_instance(self: &RcType, of: &RcType)
pub fn set_instance(self: &RcType, of: &RcType)
Sets this type variable to be an instance of
the other
§Panics
Panics if self
is not a type variable
sourcepub fn is_generic(self: &RcType) -> bool
pub fn is_generic(self: &RcType) -> bool
Checks whether there are any unbound type variables in this type.
let bool = Type::new_op("bool".into(), &[]);
let true_v = Type::new_inst(&bool);
let unbound = Type::new_var();
let id_fun = Type::new_fn(&unbound, &unbound);
let truthy = Type::new_fn(&unbound, &bool);
assert!(!bool.is_generic()); // bool contains no unbound type variables
assert!(!true_v.is_generic()); // true_v is bound to `bool`
assert!(unbound.is_generic()); // unbound is an unbound type variable
assert!(id_fun.is_generic()); // id_fun is a function with unbound type variables
assert!(truthy.is_generic()); // truthy is a function with one unbound type variable
sourcepub fn deep_clone(self: &RcType) -> RcType
pub fn deep_clone(self: &RcType) -> RcType
Makes a deep copy of a type expression.
Bound variables are shared, unbound variables are duplicated.
sourcepub fn prune(self: &RcType) -> RcType
pub fn prune(self: &RcType) -> RcType
Returns the defining instance of self
,
collapsing type instances along the way.
§May panic
Panics if this type variable’s instance field is already borrowed.
§Examples
let t_bool = Type::new_op("bool".into(), &[]);
let t_nest = Type::new_inst(&Type::new_inst(&Type::new_inst(&t_bool)));
let pruned = t_nest.prune();
assert_eq!(pruned, t_bool);
assert_eq!(t_nest, Type::new_inst(&t_bool));