From bb16c10c4c851018ee5aa905a719abdfd8fdc861 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Wed, 25 Sep 2024 08:17:09 +0200 Subject: Evaluate terms recursively on application --- rust/src/lambda.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs index b05bd22..9b2a6f2 100644 --- a/rust/src/lambda.rs +++ b/rust/src/lambda.rs @@ -17,7 +17,14 @@ pub fn run(arg: &str) -> String { fn interpret(arg: &Value) -> Value { match arg { - Value::App(l, r) => apply(&interpret(l), &interpret(r)), + Value::App(l, r) => { + let result = apply(&interpret(l), &interpret(r)); + if result == *arg { + result + } else { + interpret(&result) + } + } other => other.clone(), } } @@ -94,4 +101,10 @@ mod lambda_test { assert_eq!(Value::Num(12), interpret(&value)); } + #[test] + fn reduction_is_applied_until_normal_form_is_reached() { + let value = parse("((((lam y (lam x (lam y (x y)))) 13) (lam x x)) 11)"); + assert_eq!(Value::Num(11), interpret(&value)); + } + } -- cgit v1.2.3