Skip to content

Commit

Permalink
Clean up macros for expression building
Browse files Browse the repository at this point in the history
  • Loading branch information
Cypher1 committed Feb 18, 2024
1 parent cf520bd commit e6956ff
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions llamada/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ include = ["src/**/*", "LICENSE.md", "README.md"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pretty_assertions = "1.0"
better-std = { path = "../better-std", features = [ ] }
20 changes: 9 additions & 11 deletions llamada/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
#[macro_export]
macro_rules! expr(
{ $ctx: expr, $final: ident, $( $name: ident = $ex: expr ),* $(,)? } => {
{ $ctx: expr, $( $name: ident = $ex: expr ),* $(,)? } => {
{
#[allow(unused_imports)]
use $crate::Term::*;
use $crate::{expr, Term::*};
$( let $name = $ctx.add($ex); )*
$final
}
}
);
#[macro_export]
macro_rules! new_expr(
{ $ty: ty, $final: ident, $first: ident = $first_ex: expr, $( $name: ident = $ex: expr ),* $(,)? } => {
{ $ty: ty, $first: ident = $first_ex: expr, $( $name: ident = $ex: expr ),* $(,)? } => {
{
#[allow(unused_imports)]
use $crate::Term::*;
use $crate::expr;
let mut e = <$ty>::new($first_ex, Empty {});
let $first = e.get_last_id();
let f = expr!(e, $final, $( $name = $ex, )*);
*e.root_mut() = f;
e
use $crate::{expr, Term::*};
let mut expr = <$ty>::new($first_ex, Empty {});
let $first = expr.get_last_id();
expr!(expr, $( $name = $ex, )*);
*expr.root_mut() = expr.get_last_id();
expr
}
}
);
Expand Down
6 changes: 5 additions & 1 deletion llamada/src/reprs/ref_counted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl<
}
}
fn get_last_id(&self) -> Self::Index {
// This clone is always cheap thanks to reference counting.
self.terms.last().unwrap().clone()
}
fn get<'a>(&'a self, id: &'a Self::Index) -> &'a Term<Self::Value, Self::Index> {
Expand Down Expand Up @@ -93,7 +94,9 @@ impl<
term: Term<Self::Value, Self::Index>,
meta: Self::Meta,
) -> Self::Index {
Rc::new(Ptr::new(term, meta))
let node = Rc::new(Ptr::new(term, meta));
self.terms.push(node.clone());
node
}
fn print_meta(&self) -> bool {
self.print_meta
Expand All @@ -107,5 +110,6 @@ pub type LambdaCalc = RcRepr<Never, Empty>;
#[cfg(test)]
mod tests {
use super::*;
use better_std::assert_eq;
tests!(LambdaCalc);
}
6 changes: 5 additions & 1 deletion llamada/src/reprs/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ impl<
term: Term<Self::Value, Self::Index>,
meta: Self::Meta,
) -> Self::Index {
Ptr::new(term, meta)
let node = Ptr::new(term, meta);
// TODO: Consider a way to avoid this copy.
self.terms.push(node.clone());
node
}
fn print_meta(&self) -> bool {
self.print_meta
Expand All @@ -109,5 +112,6 @@ pub type LambdaCalc = SparseRepr<Never, Empty>;
#[cfg(test)]
mod tests {
use super::*;
use better_std::assert_eq;
tests!(LambdaCalc);
}
41 changes: 41 additions & 0 deletions llamada/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,47 @@ macro_rules! tests {
assert_eq!(expr.as_church(expr.root()), Some(2));
}

#[test]
fn plus_expr_using_macros() {
let mut expr = $crate::new_expr!(
$ty,
x = Var(1),
f = Var(2),
m = Var(3),
n = Var(4),
nf = App(n.clone(), f.clone()),
mf = App(m.clone(), f.clone()),
mfx = App(mf, x.clone()),
nfmfx = App(nf.clone(), mfx.clone()),
abs1_nfmfx = Term::abs(nfmfx),
abs2_nfmfx = Term::abs(abs1_nfmfx),
abs3_nfmfx = Term::abs(abs2_nfmfx),
_plus = Term::abs(abs3_nfmfx)
);
let plus = expr.root().clone();

assert_eq!(
format!("{}", &expr),
"(a => (b => (c => (d => ((a c) ((b c) d))))))"
);
for n in 0..10 {
for m in 0..10 {
let church_n = expr.to_church(n);
let church_m = expr.to_church(m);

$crate::expr!(
&mut expr,
plus_m = App(plus.clone(), church_m),
_plus_n_m = App(plus_m, church_n)
);
expr.reduce();
let result = expr.as_church(expr.root());
eprintln!("{n:?} + {m:?} = {result:?}");
assert_eq!(result, Some(n + m));
}
}
}

#[test]
fn plus_expr() {
let mut expr = <$ty>::new(Term::Var(1), Empty);
Expand Down
2 changes: 0 additions & 2 deletions llamada/src/type_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ mod test {
fn simple_type_system_with_no_collapsing() {
let expr = new_expr!(
LambdaCalc,
p_a_b,
a = ext(3),
b = ext(4),
plus = ext(NumOp::Mul),
Expand All @@ -352,7 +351,6 @@ mod test {
fn simply_typed() {
let expr = new_expr!(
LambdaCalc,
p_a_b,
a = ext(3),
b = ext(4),
plus = ext(NumOp::Mul),
Expand Down
14 changes: 6 additions & 8 deletions llamada/src/visitors/compact_to_church.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ impl Visitor<Never, LambdaCalc> for FromCompactToChurch {
NumOp::Mul => {
expr!(
self.output,
plus,
x = Var(1),
f = Var(2),
n = Var(3),
Expand All @@ -42,13 +41,13 @@ impl Visitor<Never, LambdaCalc> for FromCompactToChurch {
mnfx_1 = Term::abs(mnfx),
mnfx_2 = Term::abs(mnfx_1),
mnfx_3 = Term::abs(mnfx_2),
plus = Term::abs(mnfx_3),
)
_plus = Term::abs(mnfx_3),
);
*self.output.root()
}
NumOp::Add => {
expr!(
self.output,
mul,
x = Var(1),
f = Var(2),
n = Var(3),
Expand All @@ -60,8 +59,9 @@ impl Visitor<Never, LambdaCalc> for FromCompactToChurch {
mfnfx_1 = Term::abs(mfnfx),
mfnfx_2 = Term::abs(mfnfx_1),
mfnfx_3 = Term::abs(mfnfx_2),
mul = Term::abs(mfnfx_3),
)
_mul = Term::abs(mfnfx_3),
);
*self.output.root()
}
_ => todo!(),
},
Expand Down Expand Up @@ -194,7 +194,6 @@ mod test {

let expr = new_expr!(
LambdaCalc,
p_a_b,
a = Ext(3.into()),
b = Ext(4.into()),
plus = Ext(NumOp::Mul.into()),
Expand All @@ -218,7 +217,6 @@ mod test {
fn arity_checker() {
let expr = new_expr!(
LambdaCalc,
p_a_b,
a = ext(3),
b = ext(4),
plus = ext(NumOp::Mul),
Expand Down
13 changes: 6 additions & 7 deletions llamada/src/visitors/transform_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ impl Visitor<Never, LambdaCalc> for FromCompactToChurch {
NumOp::Mul => {
expr!(
self.output,
plus,
x = Var(1),
f = Var(2),
n = Var(3),
Expand All @@ -119,13 +118,13 @@ impl Visitor<Never, LambdaCalc> for FromCompactToChurch {
mnfx_1 = Term::abs(mnfx),
mnfx_2 = Term::abs(mnfx_1),
mnfx_3 = Term::abs(mnfx_2),
plus = Term::abs(mnfx_3),
)
_plus = Term::abs(mnfx_3),
);
*self.output.root()
}
NumOp::Add => {
expr!(
self.output,
mul,
x = Var(1),
f = Var(2),
n = Var(3),
Expand All @@ -137,8 +136,9 @@ impl Visitor<Never, LambdaCalc> for FromCompactToChurch {
mfnfx_1 = Term::abs(mfnfx),
mfnfx_2 = Term::abs(mfnfx_1),
mfnfx_3 = Term::abs(mfnfx_2),
mul = Term::abs(mfnfx_3),
)
_mul = Term::abs(mfnfx_3),
);
*self.output.root()
}
_ => todo!(),
},
Expand Down Expand Up @@ -242,7 +242,6 @@ mod test {
fn arity_checker() {
let expr = new_expr!(
LambdaCalc,
p_a_b,
a = ext(3),
b = ext(4),
plus = ext(NumOp::Mul),
Expand Down

0 comments on commit e6956ff

Please sign in to comment.