pub enum Term {
    Var(usize),
    Abs(Box<Term>),
    App(Box<(Term, Term)>),
}
Expand description

A lambda term that is either a variable with a De Bruijn index, an abstraction over a term or an applicaction of one term to another.

Variants§

§

Var(usize)

a variable

§

Abs(Box<Term>)

an abstraction

§

App(Box<(Term, Term)>)

an application

Implementations§

source§

impl Term

source

pub fn unvar(self) -> Result<usize, TermError>

Returns a variable’s De Bruijn index, consuming it in the process.

Example
use lambda_calculus::*;

assert_eq!(Var(1).unvar(), Ok(1));
Errors

Returns a TermError if self is not a Variable.

source

pub fn unvar_ref(&self) -> Result<&usize, TermError>

Returns a reference to a variable’s De Bruijn index.

Example
use lambda_calculus::*;

assert_eq!(Var(1).unvar_ref(), Ok(&1));
Errors

Returns a TermError if self is not a Variable.

source

pub fn unvar_mut(&mut self) -> Result<&mut usize, TermError>

Returns a mutable reference to a variable’s De Bruijn index.

Example
use lambda_calculus::*;

assert_eq!(Var(1).unvar_mut(), Ok(&mut 1));
Errors

Returns a TermError if self is not a Variable.

source

pub fn unabs(self) -> Result<Term, TermError>

Returns an abstraction’s underlying term, consuming it in the process.

Example
use lambda_calculus::*;

assert_eq!(abs(Var(1)).unabs(), Ok(Var(1)));
Errors

Returns a TermError if self is not an Abstraction.

source

pub fn unabs_ref(&self) -> Result<&Term, TermError>

Returns a reference to an abstraction’s underlying term.

Example
use lambda_calculus::*;

assert_eq!(abs(Var(1)).unabs_ref(), Ok(&Var(1)));
Errors

Returns a TermError if self is not an Abstraction.

source

pub fn unabs_mut(&mut self) -> Result<&mut Term, TermError>

Returns a mutable reference to an abstraction’s underlying term.

Example
use lambda_calculus::*;

assert_eq!(abs(Var(1)).unabs_mut(), Ok(&mut Var(1)));
Errors

Returns a TermError if self is not an Abstraction.

source

pub fn unapp(self) -> Result<(Term, Term), TermError>

Returns a pair containing an application’s underlying terms, consuming it in the process.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).unapp(), Ok((Var(1), Var(2))));
Errors

Returns a TermError if self is not an Application.

source

pub fn unapp_ref(&self) -> Result<(&Term, &Term), TermError>

Returns a pair containing references to an application’s underlying terms.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).unapp_ref(), Ok((&Var(1), &Var(2))));
Errors

Returns a TermError if self is not an Application.

source

pub fn unapp_mut(&mut self) -> Result<(&mut Term, &mut Term), TermError>

Returns a pair containing mutable references to an application’s underlying terms.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).unapp_mut(), Ok((&mut Var(1), &mut Var(2))));
Errors

Returns a TermError if self is not an Application.

source

pub fn lhs(self) -> Result<Term, TermError>

Returns the left-hand side term of an application. Consumes self.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).lhs(), Ok(Var(1)));
Errors

Returns a TermError if self is not an Application.

source

pub fn lhs_ref(&self) -> Result<&Term, TermError>

Returns a reference to the left-hand side term of an application.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).lhs_ref(), Ok(&Var(1)));
Errors

Returns a TermError if self is not an Application.

source

pub fn lhs_mut(&mut self) -> Result<&mut Term, TermError>

Returns a mutable reference to the left-hand side term of an application.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).lhs_mut(), Ok(&mut Var(1)));
source

pub fn rhs(self) -> Result<Term, TermError>

Returns the right-hand side term of an application. Consumes self.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).rhs(), Ok(Var(2)));
Errors

Returns a TermError if self is not an Application.

source

pub fn rhs_ref(&self) -> Result<&Term, TermError>

Returns a reference to the right-hand side term of an application.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).rhs_ref(), Ok(&Var(2)));
Errors

Returns a TermError if self is not an Application.

source

pub fn rhs_mut(&mut self) -> Result<&mut Term, TermError>

Returns a mutable reference to the right-hand side term of an application.

Example
use lambda_calculus::*;

assert_eq!(app(Var(1), Var(2)).rhs_mut(), Ok(&mut Var(2)));
Errors

Returns a TermError if self is not an Application.

source

pub fn is_supercombinator(&self) -> bool

Returns true if self is a supercombinator.

Example
use lambda_calculus::*;

let term1 = abs(app(Var(1), abs(Var(1)))); // λ 1 (λ 1)
let term2 = app(abs(Var(2)), abs(Var(1))); // (λ 2) (λ 1)

assert_eq!(term1.is_supercombinator(), true);
assert_eq!(term2.is_supercombinator(), false);
source

pub fn max_depth(&self) -> u32

Returns the maximum depth of lambda abstractions in the given Term.

Example
use lambda_calculus::*;

assert_eq!(abs(Var(1)).max_depth(), 1);
source§

impl Term

source

pub fn apply(&mut self, rhs: &Term) -> Result<(), TermError>

Applies a Term to self via substitution and variable update.

Example
use lambda_calculus::*;

let mut term1  = parse(&"λλ42(λ13)", DeBruijn).unwrap();
let term2      = parse(&"λ51", DeBruijn).unwrap();
let result     = parse(&"λ3(λ61)(λ1(λ71))", DeBruijn).unwrap();

term1.apply(&term2);

assert_eq!(term1, result);
Errors

Returns a TermError if self is not an Abstraction.

source

pub fn reduce(&mut self, order: Order, limit: usize) -> usize

Performs β-reduction on a Term with the specified evaluation Order and an optional limit on the number of reductions (0 means no limit) and returns the number of performed reductions.

Example
use lambda_calculus::*;

let mut expression = parse(&"(λa.λb.λc.b (a b c)) (λa.λb.b)", Classic).unwrap();
let reduced        = parse(&"λa.λb.a b", Classic).unwrap();

expression.reduce(NOR, 0);

assert_eq!(expression, reduced);

Trait Implementations§

source§

impl Clone for Term

source§

fn clone(&self) -> Term

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Term

source§

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

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

impl Display for Term

source§

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

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

impl From<(Term, Term)> for Term

source§

fn from((a, b): (Term, Term)) -> Term

Converts to this type from the input type.
source§

impl From<Option<Term>> for Term

source§

fn from(option: Option<Term>) -> Term

Converts to this type from the input type.
source§

impl From<Result<Term, Term>> for Term

source§

fn from(result: Result<Term, Term>) -> Term

Converts to this type from the input type.
source§

impl From<Vec<Term, Global>> for Term

source§

fn from(vec: Vec<Term>) -> Term

Converts to this type from the input type.
source§

impl From<bool> for Term

source§

fn from(b: bool) -> Term

Converts to this type from the input type.
source§

impl Hash for Term

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<Term> for Term

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Term

source§

impl StructuralEq for Term

source§

impl StructuralPartialEq for Term

Auto Trait Implementations§

§

impl RefUnwindSafe for Term

§

impl Send for Term

§

impl Sync for Term

§

impl Unpin for Term

§

impl UnwindSafe for Term

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.