Expand description
Automatically derive variable binding and alpha equivalence for abstract syntax trees.
§Example
Here is an example of how you might use moniker
to define the AST for the
simply typed lambda calculus:
#[macro_use]
extern crate moniker;
use std::rc::Rc;
use moniker::{Embed, Nest, Binder, Scope, Var};
#[derive(Debug, Clone, BoundTerm)]
pub enum Type {
Base,
Arrow(Rc<Type>, Rc<Type>),
}
#[derive(Debug, Clone, BoundTerm)]
pub enum Expr {
Var(Var<String>),
Lam(Scope<(Binder<String>, Embed<Rc<Type>>), Rc<Expr>>),
Let(Scope<Nest<(Binder<String>, Embed<(Rc<Type>, Rc<Expr>)>)>, Rc<Expr>>),
App(Rc<Expr>, Rc<Expr>),
}
§Overview of traits and data types
We separate data types into terms and patterns:
§Terms
Terms are data types that implement the BoundTerm
trait.
Var<N>
: A variable that is either free or boundScope<P: BoundPattern<N>, T: BoundTerm<N>>
: bind the termT
using the patternP
Ignore<T>
: IgnoresT
when comparing for alpha equality
Implementations for tuples, strings, numbers, slices, vectors, and mart pointers are also provided for convenience.
§Patterns
Patterns are data types that implement the BoundPattern
trait.
Binder<N>
: Captures a free variables within a term, but is ignored for alpha equalityIgnore<T>
: IgnoresT
when comparing for alpha equalityEmbed<T: BoundTerm<N>>
: Embed a termT
in a patternNest<P: BoundPattern<N>>
: Multiple nested binding patternsRec<P: BoundPattern<N>>
: Recursively bind a pattern in itself
Implementations for tuples, strings, numbers, slices, vectors, and mart pointers are also provided for convenience.
Macros§
- assert_
pattern_ eq - Assert that two expressions are alpha equivalent to each other (using
BoundPattern::pattern_eq
). - assert_
term_ eq - Assert that two expressions are alpha equivalent to each other (using
BoundTerm::term_eq
).
Structs§
- Binder
- Binder
Index - Bound
Var - Embed
- Embed a term in a pattern
- FreeVar
- A free variable
- Ignore
- Data that does not participate in name binding
- Nest
- Nested binding patterns
- Rec
- Recursively bind a pattern in itself
- Scope
- A bound scope
- Scope
Offset - The Debruijn index of the binder that introduced the variable
- Scope
State - Unique
Id - A generated id
Enums§
- Var
- A variable that can either be free or bound
Traits§
- Bound
Pattern - Patterns that bind variables in terms
- Bound
Term - Terms that may contain variables that can be bound by patterns
- OnBound
Fn - OnFree
Fn