Skip to main content

Type

Enum Type 

Source
pub enum Type {
    Var(TyVar),
    App(TyCon, Vec<Type>),
}
Expand description

A type term: either an inference variable or a constructor applied to arguments.

This is the structural core every concrete type is expressed in. A type is one of two shapes:

  • Var — an inference TyVar, possibly still unbound.
  • App — a TyCon applied to zero or more argument types. A primitive like int is the zero-argument case; a List<int> is List applied to int; a function (A) -> B is some -> constructor applied to A and B.

The same two shapes describe primitives, generics, functions, tuples, and any other first-order type former — the meaning of each constructor is the consumer’s to define. Build terms with the var, con, and app constructors.

§Examples

use type_lang::{TyCon, Type, Unifier};

const INT: TyCon = TyCon::new(0);
const LIST: TyCon = TyCon::new(1);

let mut unifier = Unifier::new();
let element = unifier.fresh();

// List<?element>
let list_of = Type::app(LIST, [Type::var(element)]);
// List<int>
let list_int = Type::app(LIST, [Type::con(INT)]);

unifier.unify(&list_of, &list_int).expect("same constructor");
assert_eq!(unifier.resolve(&Type::var(element)), Type::con(INT));

Variants§

§

Var(TyVar)

An inference variable.

§

App(TyCon, Vec<Type>)

A constructor applied to its arguments. The argument list is empty for a nullary constructor such as a primitive.

Implementations§

Source§

impl Type

Source

pub const fn var(var: TyVar) -> Self

Builds a variable type from a TyVar.

§Examples
use type_lang::{Type, Unifier};

let mut unifier = Unifier::new();
let v = unifier.fresh();
let ty = Type::var(v);
assert_eq!(ty.as_var(), Some(v));
Source

pub const fn con(head: TyCon) -> Self

Builds a nullary constructor type, such as a primitive.

This is the zero-argument case of app; Type::con(c) and Type::app(c, []) are the same term.

§Examples
use type_lang::{TyCon, Type};

const UNIT: TyCon = TyCon::new(0);
let ty = Type::con(UNIT);
assert_eq!(ty.head(), Some(UNIT));
assert!(ty.args().is_empty());
Source

pub fn app(head: TyCon, args: impl Into<Vec<Type>>) -> Self

Builds a constructor applied to a list of argument types.

Accepts anything that turns into a Vec<Type> — an array, a Vec, or any iterator’s collect() — so a fixed-arity type reads naturally:

use type_lang::{TyCon, Type, Unifier};

const FUNCTION: TyCon = TyCon::new(0);
const INT: TyCon = TyCon::new(1);

let mut unifier = Unifier::new();
let result = unifier.fresh();

// (int) -> ?result
let signature = Type::app(FUNCTION, [Type::con(INT), Type::var(result)]);
assert_eq!(signature.head(), Some(FUNCTION));
assert_eq!(signature.args().len(), 2);
Source

pub const fn as_var(&self) -> Option<TyVar>

Returns the variable if this is a Var, otherwise None.

§Examples
use type_lang::{TyCon, Type, Unifier};

let mut unifier = Unifier::new();
let v = unifier.fresh();
assert_eq!(Type::var(v).as_var(), Some(v));
assert_eq!(Type::con(TyCon::new(0)).as_var(), None);
Source

pub const fn head(&self) -> Option<TyCon>

Returns the head constructor if this is an App, otherwise None (a variable has no constructor).

§Examples
use type_lang::{TyCon, Type, Unifier};

const INT: TyCon = TyCon::new(0);
let mut unifier = Unifier::new();

assert_eq!(Type::con(INT).head(), Some(INT));
assert_eq!(Type::var(unifier.fresh()).head(), None);
Source

pub fn args(&self) -> &[Type]

Returns the argument types of an App, or an empty slice for a variable or a nullary constructor.

§Examples
use type_lang::{TyCon, Type};

const PAIR: TyCon = TyCon::new(0);
const INT: TyCon = TyCon::new(1);

let pair = Type::app(PAIR, [Type::con(INT), Type::con(INT)]);
assert_eq!(pair.args().len(), 2);
assert!(Type::con(INT).args().is_empty());
Source

pub const fn is_var(&self) -> bool

Returns true if this term is an inference variable.

Trait Implementations§

Source§

impl Clone for Type

Source§

fn clone(&self) -> Type

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Type

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Type

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Type

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Type

Source§

impl Hash for Type

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Type

Source§

fn eq(&self, other: &Type) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Type

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Type

Auto Trait Implementations§

§

impl Freeze for Type

§

impl RefUnwindSafe for Type

§

impl Send for Type

§

impl Sync for Type

§

impl Unpin for Type

§

impl UnsafeUnpin for Type

§

impl UnwindSafe for Type

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.