Term

Enum Term 

Source
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

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

Returns true if self is structurally isomorphic to other.

§Example
use lambda_calculus::*;

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

assert_eq!(term1.is_isomorphic_to(&term2), false);
assert_eq!(term1.is_isomorphic_to(&term3), true);
Source

pub fn has_free_variables(&self) -> bool

Returns true if self has any free vairables.

§Example
use lambda_calculus::*;

let with_freevar = abs(Var(2));     // λ 2
let without_freevar = abs(Var(1));  // λ 1

assert!(with_freevar.has_free_variables());
assert!(!without_freevar.has_free_variables());
Source

pub fn max_free_index(&self) -> usize

Calculates the maximum index of any free variable in the term.

The result corresponds to the number of names Context must supply to bind them all.

Source

pub fn with_context<'a>(&'a self, ctx: &'a Context) -> impl Display + 'a

Returns a helper struct that allows displaying the term with a given context.

§Example
use lambda_calculus::{*, term::Context};

let term = abs(Var(2)); // λa.b
let ctx = Context::new(&["x"]); // Predefine "x" as a free variable

// The context defines `Var(2)` as "x" instead of the default "b"
assert_eq!(term.with_context(&ctx).to_string(), "λa.x");
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 duplicate 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>> 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 for Term

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Eq for Term

Source§

impl StructuralPartialEq for Term

Auto Trait Implementations§

§

impl Freeze for Term

§

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 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> 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.