lamcalc

A library for creating, parsing and evaluating lambda calculus

14 releases (3 stable)

1.2.0 Jun 25, 2023
1.1.0 May 29, 2023
1.0.0 Apr 8, 2023
0.4.5 Mar 23, 2023
0.1.0 Mar 14, 2023

#1284 in Parser implementations

Download history 17/week @ 2024-09-18 31/week @ 2024-09-25

54 downloads per month

MIT license

60KB
1.5K SLoC

LamCalc: An implementation for Lambda Calculus

docs.rs Crates.io tutorial

LamCalc implements untyped Lambda Calculus, Inspired by Lambda Calculus: Basic Interpreter in Rust (Part 2).

Current status: stabalized v1.

Features

  • lambda! macro for convenient definition.
  • Implemented using De Bruijn index.
  • Parser for expressions/definitions/files.
  • WASM package for web application.

Quick View

use lamcalc::{lambda, Error, parser::parse_exp};

fn main () -> Result<(), Error> {
    // define using macro
    let tt = lambda!(x. y. x); // use macro to define lambda
    let ff = lambda!(x. (y. y)); // add parentheses for clarity
    let and = lambda!(x.y.x y x); // space between dots are not necessary

    // multiple printing format
    println!("and = {}", and);   // print lambda
    println!("and = {:#}", and); // lambda with De Bruijn index
    println!("and = {}", and.purify()); // De Bruijn encoding

    // use braces to refer to previously defined lambda
    let mut and_f_t = lambda!({and} {ff} {tt}); 
    and_f_t.simplify()?; // get simplified result
    assert_eq!(and_f_t, ff);

    // parse lambda expression string
    let y_combinator = lambda!(f.(x. f (x x)) (x. f (x x)));
    let y_str = r#"\f.(\x. f (x x)) (\x. f (x x))"#;
    let (y2, _) = parse_exp(y_str)?;
    
    assert_eq!(y2, y_combinator);

    Ok(())
}

See examples/ for more.

Dependencies

~2–5MB
~98K SLoC