summaryrefslogtreecommitdiff
path: root/rust/src/lambda.rs
blob: 99640219f7689a59193a257689f277e1ff2cc1f0 (plain)
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
use std::fs::read_to_string;

mod ast;
use ast::*;

mod parser;
use parser::*;

pub fn run(arg: &str) -> String {
    let content = read_to_string(arg).unwrap();
    let value = parse(&content.to_string());
    let result = interpret(&value);

    result.to_string()
}

fn interpret(arg: &Value) -> Value {
    match arg {
        Value::App(l, r) => apply(l, r),
        other => other.clone(),
    }
}

fn apply(l: &Value, r: &Value) -> Value {
    if let Value::Lam(v, body) = l {
        subst(r, v, body)
    } else {
        Value::App(Box::new(l.clone()), Box::new(r.clone()))
    }
}

fn subst(r: &Value, v: &str, body: &Value) -> Value {
    match r {
        Value::Sym(x) if x == v => body.clone(),
        other => other.clone(),
    }
}

#[cfg(test)]
mod lambda_test {
    use crate::{interpret, parse, Value};

    #[test]
    fn evaluating_a_non_reducible_value_yields_itself() {
        let value = parse("(foo 12)");
        assert_eq!(value, interpret(&value));
    }

    #[test]
    fn evaluating_application_on_an_abstraction_reduces_it() {
        let value = parse("((lam x x) 12)");
        assert_eq!(Value::Num(12), interpret(&value));
    }
}