1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
//!
//! ## Example: Church Encoding
//!
//! ```rust
#![doc = include_str!("../examples/church_encoding.rs")]
//! ```
//!
//! ## Example: Parser
//!
//! ```rust
#![doc = include_str!("../examples/parser.rs")]
//! ```
//!
//! ## Example: Y Combinator
//!
//! ```rust
#![doc = include_str!("../examples/y_combinator.rs")]
//! ```

#[doc(hidden)]
pub mod builder;
mod error;
mod eval;
mod exp;
pub mod parser;
#[cfg(feature = "wasm")]
pub mod wasm;

pub use error::Error;
pub use eval::SIMPLIFY_LIMIT;
pub use exp::Exp;
pub use exp::Ident;

#[cfg(test)]
mod tests {
    use super::{Exp, Ident};
    use crate::{lambda, Error};

    #[test]
    fn test_display() {
        println!(
            "{}",
            Exp::Abs(
                Ident(String::from("x"), 0),
                Box::new(Exp::App(
                    Box::new(Exp::Var(Ident(String::from("y"), 0))),
                    Box::new(Exp::Var(Ident(String::from("x"), 2)))
                ))
            )
        )
    }
    #[test]
    fn test_clone() {
        let mut tt = lambda!(x.y.x).purify();
        let mut ff = tt.clone();
        ff.into_abs()
            .unwrap()
            .1
            .into_abs()
            .unwrap()
            .1
            .into_ident()
            .unwrap()
            .1 = 1;
        assert_eq!(
            tt.into_abs()
                .unwrap()
                .1
                .into_abs()
                .unwrap()
                .1
                .into_ident()
                .unwrap()
                .1,
            2
        );
        println!("tt = {}, ff = {}", tt, ff);
    }
    #[test]
    fn test_macros() {
        let l_true = lambda!(x.y.x);
        let l_false = lambda!(x.y.y);
        let y_comb = lambda!(f.(x. f (x x)) (x. f (x x)));
        assert_eq!(format!("{}", y_comb), "λf. (λx. f (x x)) λx. f (x x)");
        assert_eq!(
            format!("{:#}", y_comb),
            "λf. (λx. f<2> (x<1> x<1>)) λx. f<2> (x<1> x<1>)"
        );

        println!("{}\n{}\n{}\n", l_true, l_false, y_comb);
        println!("{:#}\n{:#}\n{:#}", l_true, l_false, y_comb);

        let test_app = lambda!(x. y. z. x y z);
        let test_app2 = lambda!(x. y. z. (x (y z)) (x z));

        println!("{}\n{}", test_app, test_app2);
    }
    #[test]
    fn test_eval() {
        let tt = lambda!(x. (y. x));
        let and = lambda!(x. y. x y x);
        // let or = lambda!(x. y. x y [tt]);
        // let neg = lambda!(x. x [ff] [tt]);

        let mut res = lambda!({and} {tt} {tt});

        println!("res = {}", res);
        while res.eval_normal_order(false) {
            println!("res = {}", res);
        }
        assert_eq!(res.to_string(), "λx. λy. x");
    }
    #[test]
    fn test_nat() -> Result<(), Error> {
        let zero = lambda!(s. (z. z));
        let suc = lambda!(n. s. z. s (n s z));
        let mut plus = lambda!(n. m. n {suc} m);
        plus.simplify()?;

        let mut nats = vec![zero];
        for i in 1..10 {
            let x = nats.last().unwrap();
            let mut sx = lambda!({suc} {x});
            sx.simplify()?;
            eprintln!("{} = {}", i, sx.purify());
            nats.push(sx);
        }
        let mut test = lambda!({plus} {nats[4]} {nats[3]});
        test.simplify()?;
        println!("test = {:#}", test);

        assert_eq!(test.to_string(), nats[7].to_string());
        Ok(())
    }
}