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
use crate::{Expression, Abstraction};

/// Function call support for an `Expression`.
///
/// ```
/// # #![feature(box_syntax)]
/// # #[macro_use]
/// # extern crate lalrpop_lambda;
/// # fn main() {
/// assert_eq!(0u64, λ!{x.x}(0).into());
/// assert_eq!(γ!(γ!(a,b),0), γ!(a,b)(0));
/// # }
/// ```
impl<T> FnOnce<(T,)> for Expression
    where T: Into<Expression> +
             From<Expression>
{
    // TODO: Return Option<T> here too.
    type Output = Expression;

    extern "rust-call" fn call_once(self, t: (T,)) -> Expression {
        γ!({self},t.0.into())
    }
}

impl From<Expression> for fn(u64) -> u64 {
    fn from(e: Expression) -> Self {
        match e {
            Expression::Abs(Abstraction(ref lid, box ref e1)) => {
                match e1 {
                    Expression::Var(ref rid) if lid == rid => {
                        |x| x
                    },
                    Expression::Var(_) => {
                        |_| 0
                    },
                    _ => unreachable!(),
                }
            },
            _ => |_| panic!("not a function"),
        }
    }
}

impl From<fn(u64) -> u64> for Expression {
    fn from(_f: fn(u64) -> u64) -> Self {
        // TODO: Since we can't call `f` now, we should bind it to an `env` and
        // add a reference to f here.
        //
        // un-η
        abs!{x.app!(f,x)}
    }
}


#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    #[test]
    fn var() {
        let one = abs!{f.abs!{x.app!(f,x)}};
        assert_eq!(app!(x,{one}), var!(x)(1));
    }

    #[test]
    fn abs() {
        assert_eq!(5u64, abs!{x.x}(5).into());
    }

    #[test]
    fn app() {
        let zero = abs!{f.abs!{x.x}};
        assert_eq!(app!(app!(a,b),{zero}), app!(a,b)(0));
    }
}