Skip to content

A λ-calculus grammar/interpretor written using LALRPOP and `λ!`.

License

Notifications You must be signed in to change notification settings

0x7fff5238100b/lalrpop-lambda

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

λ-calculus Parser (using LALRPOP)

Build Status Crates.io Version docs.rs

Write lambda calculus with ease, and evaluate it. There are a number of ways to use this library (each interchangeable with another):

  • Expression AST variants Abs, App, and Var
  • Macros abs!/λ!, app!/γ!, and var!
  • Parsed λ-calculus strings λx.x (\a.\b.a)
  • Native types: u64, bool, fn (WIP)
let id = λ!{x.x};
let one = λ!{f.λ!{x.γ!(f,x)}};

println!("{}", one.normalize(false));
assert_eq!(1u64, u64::from(app!({id},{one})));

Usage (JS)

import("./node_modules/lalrpop-lambda/lalrpop_lambda.js").then(lambda => {
    console.log([
        new lambda.Exp("x"),
        new lambda.Exp(5),
        new lambda.Exp(false),
        new lambda.Exp("(\\x.x) y").normalize(true)
    ])
});

Usage (Rust)

use lalrpop_lambda::lambda::ExpressionParser;
let parser = ExpressionParser::new();

// Parse a single free variable.
let x = parser.parse("x");

// Parse the identity function.
let id = parser.parse(r"\x.x");

// f ∘ g
let compose = parser.parse(r"\f.\g.\x.(f (g x))"));

// Print the free variable in this expression.
let unbound_y = parser.parse(r"\x.x y");
println!("{}", unbound_y.free_variables());

// No need for parsing strings at all.
let id = λ!{x.x};
let one = λ!{f.λ!{x.γ!(f, x)}};

// Identity application.
let id = λ!{x.x};
println!("(id one): {} -> {}",
         app!({&id}, {&one}),
         app!({&id}, {&one}).normalize(false));

// Make the Y combinator.
let ω = parser.parse(r"λx.(x x)");
let Ω = parser.parse(r"(λx.(x x)) (λx.(x x))");
let W = parser.parse(r"λf.λx. f x x");
let Y = parser.parse(r"λf.(λx.f (x x)) (λx.f (x x))");

Development

This assumes you have an updated and working copy of [rustup][rustup].

cargo +nightly [build | test | bench | doc | run --example <>]
WASM

First make sure you have wasm-pack installed. Then:

wasm-pack build

About

A λ-calculus grammar/interpretor written using LALRPOP and `λ!`.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 98.4%
  • Shell 1.6%