Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ readme = "README.md"
license = "CC0-1.0"
keywords = ["lambda", "calculus", "functional", "mathematics", "combinators"]
authors = ["ljedrz <ljedrz@gmail.com>"]
edition = "2021"
edition = "2024"

[lib]
crate-type = ["lib"]
Expand Down
2 changes: 1 addition & 1 deletion src/combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#![allow(non_snake_case)]

use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// I - the identity combinator.
///
Expand Down
8 changes: 2 additions & 6 deletions src/data/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! [Lambda-encoded booleans](https://en.wikipedia.org/wiki/Church_encoding#Church_Booleans)

use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// A lambda-encoded boolean `true`.
///
Expand Down Expand Up @@ -179,10 +179,6 @@ pub fn imply() -> Term {

impl From<bool> for Term {
fn from(b: bool) -> Term {
if b {
tru()
} else {
fls()
}
if b { tru() } else { fls() }
}
}
2 changes: 1 addition & 1 deletion src/data/list/church.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::data::boolean::{fls, tru};
use crate::data::pair::{fst, pair, snd};
use crate::term::Term::*;
use crate::term::{abs, app, Term, UD};
use crate::term::{Term, UD, abs, app};

/// Produces a `nil`, the last link of a Church-encoded list; equivalent to `boolean::tru`.
///
Expand Down
4 changes: 3 additions & 1 deletion src/data/list/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#![allow(missing_docs)]

use alloc::vec::Vec;

use crate::data::num::convert::*;
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

macro_rules! make_trait {
($trait_name:ident, $function_name:ident) => {
Expand Down
4 changes: 3 additions & 1 deletion src/data/list/pair.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! [Single-pair list](https://en.wikipedia.org/wiki/Church_encoding#One_pair_as_a_list_node)

use alloc::vec::Vec;

use crate::combinators::{I, Z};
use crate::data::boolean::{fls, tru};
use crate::data::num::church::{is_zero, pred, succ, zero};
use crate::data::pair::{fst, pair, snd};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Produces a `nil`, the last link of a pair-encoded list; equivalent to `boolean::fls`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/list/parigot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::data::boolean::{fls, tru};
use crate::term::Term::*;
use crate::term::{abs, app, Term, UD};
use crate::term::{Term, UD, abs, app};

/// Produces a `nil`, the last link of a Parigot-encoded list; equivalent to `boolean::tru`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/list/scott.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::data::boolean::{fls, tru};
use crate::term::Term::*;
use crate::term::{abs, app, Term, UD};
use crate::term::{Term, UD, abs, app};

/// Produces a `nil`, the last link of a Scott-encoded list; equivalent to `boolean::tru`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::combinators::I;
use crate::data::boolean::{fls, tru};
use crate::data::pair::{fst, pair, snd};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// A 0 bit; equivalent to `boolean::tru`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/church.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::data::boolean::{and, fls, not, or, tru};
use crate::data::num::{parigot, scott, stumpfu};
use crate::data::pair::pair;
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Produces a Church-encoded number zero; equivalent to `boolean::fls`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use self::Encoding::*;
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// The type of numeric encoding.
#[derive(Debug, Clone, Copy)]
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/parigot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::data::boolean::{fls, tru};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Produces a Parigot-encoded number zero; equivalent to `boolean::fls`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/scott.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::combinators::Z;
use crate::data::boolean::{fls, tru};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Produces a Scott-encoded number zero; equivalent to `boolean::tru`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::data::num::convert::Encoding::*;
use crate::data::num::{church, parigot, scott, stumpfu};
use crate::data::pair::{fst, pair, snd, swap};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Applied to a numeral with a specified encoding it produces a pair representing its signed
/// equivalent.
Expand Down
2 changes: 1 addition & 1 deletion src/data/num/stumpfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::data::boolean::{fls, tru};
use crate::data::num::convert::IntoChurchNum;
use crate::data::num::{church, parigot, scott};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Produces a Stump-Fu-encoded number zero; equivalent to `boolean::fls`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::combinators::I;
use crate::data::boolean::{fls, tru};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Produces a lambda-encoded empty option; equivalent to `boolean::tru`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::data::boolean::{fls, tru};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Applied to two `Term`s it contains them in a lambda-encoded pair.
///
Expand Down
2 changes: 1 addition & 1 deletion src/data/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::combinators::I;
use crate::data::boolean::{fls, tru};
use crate::data::option::{none, some};
use crate::term::Term::*;
use crate::term::{abs, app, Term};
use crate::term::{Term, abs, app};

/// Applied to an argument it consumes it and produces a lambda-encoded `Result::Ok` that contains
/// it.
Expand Down
4 changes: 2 additions & 2 deletions src/data/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// ```
#[macro_export]
macro_rules! tuple {
($first:expr, $($next:expr),+) => {
($first:expr_2021, $($next:expr_2021),+) => {
{
let mut ret = app(Var(1), $first);
$(ret = app(ret, $next);)*
Expand Down Expand Up @@ -57,7 +57,7 @@ macro_rules! tuple {
/// ```
#[macro_export]
macro_rules! pi {
($i:expr, $n:expr) => {{
($i:expr_2021, $n:expr_2021) => {{
let mut ret = Var($n + 1 - $i);

for _ in 0..$n {
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
//! **lambda_calculus** is a simple implementation of the untyped lambda calculus in Rust.

#![no_std]
#![deny(missing_docs)]
#![deny(unsafe_code)]

#[macro_use]
extern crate alloc;
#[macro_use]
pub mod term;
pub mod combinators;
pub mod parser;
pub mod reduction;

pub use self::parser::parse;
pub use self::reduction::beta;
pub use self::reduction::Order::*;
pub use self::reduction::beta;
pub use self::term::Notation::*;
pub use self::term::Term::*;
pub use self::term::{abs, app, Term, UD};
pub use self::term::{Term, UD, abs, app};

#[cfg(feature = "encoding")]
pub mod data;
Expand Down
13 changes: 8 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ use self::ParseError::*;
use self::Token::*;
pub use crate::term::Notation::*;
use crate::term::Term::*;
use crate::term::{abs, app, Notation, Term};
use std::collections::VecDeque;
use std::error::Error;
use std::fmt;
use crate::term::{Notation, Term, abs, app};
use alloc::collections::VecDeque;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::error::Error;
use core::fmt;

/// An error returned by `parse()` when a parsing issue is encountered.
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -231,7 +234,7 @@ fn _get_ast(tokens: &[Token], pos: &mut usize) -> Result<Expression, ParseError>
/// Attempts to parse the input `&str` as a lambda `Term` encoded in the given `Notation`.
///
/// - lambdas can be represented either with the greek letter (λ) or a backslash (\\ -
/// less aesthetic, but only one byte in size)
/// less aesthetic, but only one byte in size)
/// - the identifiers in `Classic` notation are `String`s of alphabetic Unicode characters
/// - `Classic` notation ignores whitespaces where unambiguous
/// - the indices in the `DeBruijn` notation start with 1 and are hexadecimal digits
Expand Down
12 changes: 7 additions & 5 deletions src/reduction.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
//! [β-reduction](https://en.wikipedia.org/wiki/Beta_normal_form) for lambda `Term`s

use alloc::borrow::ToOwned;

pub use self::Order::*;
use crate::term::Term::*;
use crate::term::{Term, TermError};
use std::{cmp, fmt, mem};
use core::{cmp, fmt, mem};

/// The [evaluation order](http://www.cs.cornell.edu/courses/cs6110/2014sp/Handouts/Sestoft.pdf) of
/// β-reductions.
///
/// - the `NOR`, `HNO`, `APP` and `HAP` orders reduce expressions to their normal form
/// - the `APP` order will fail to fully reduce expressions containing terms without a normal form,
/// e.g. the `Y` combinator (they will expand forever)
/// e.g. the `Y` combinator (they will expand forever)
/// - the `CBN` order reduces to weak head normal form
/// - the `CBV` order reduces to weak normal form
/// - the `HSP` order reduces to head normal form
Expand Down Expand Up @@ -92,7 +94,7 @@ impl Term {
}
_ => {}
},
Abs(ref mut abstracted) => abstracted._apply(rhs, depth + 1),
Abs(abstracted) => abstracted._apply(rhs, depth + 1),
App(boxed) => {
let (ref mut lhs_lhs, ref mut lhs_rhs) = **boxed;
lhs_lhs._apply(rhs, depth);
Expand All @@ -103,12 +105,12 @@ impl Term {

fn update_free_variables(&mut self, added_depth: usize, own_depth: usize) {
match self {
Var(ref mut i) => {
Var(i) => {
if *i > own_depth {
*i += added_depth
}
}
Abs(ref mut abstracted) => abstracted.update_free_variables(added_depth, own_depth + 1),
Abs(abstracted) => abstracted.update_free_variables(added_depth, own_depth + 1),
App(boxed) => {
let (ref mut lhs, ref mut rhs) = **boxed;
lhs.update_free_variables(added_depth, own_depth);
Expand Down
Loading
Loading