use crate::{lambda, Error, Term}; use std::collections::HashMap; pub fn substitute_definitions(mut term: Term, definitions: &HashMap) -> Term { loop { match term { Term::Var(v) => { if let Some(def) = definitions.get(&v) { term = (*def).clone(); } else { return Term::Var(v); } } Term::App(l, r) => { let l_sub = substitute_definitions(*l, definitions); let r_sub = substitute_definitions(*r, definitions); return Term::App(Box::new(l_sub), Box::new(r_sub)); } _ => return term, } } } pub fn reduce(term: Term, definitions: &HashMap, max: usize) -> Result { let mut term = term; for _ in 0..max { let next = step(term.clone(), definitions); if next == term { return Ok(term); } term = next; } Err(Error::TooManyReductions( "reduction limit reached".to_string(), )) } pub fn to_lambda_display(term: &Term) -> Result { let db = lambda::term_to_debruijn(term); match lambda::beta_reduce(db, 1000) { Ok(reduced) => Ok(lambda::debruijn_show(&reduced)), Err(_) => Ok("[large term]".to_string()), } } fn safe_exp(c: crate::C) -> crate::C { let r = c.exp(); if r.re.is_infinite() && r.im.is_infinite() { crate::C::new(r.re, 0.0) } else { r } } fn iota_to_sk(x: Term) -> Term { Term::App( Box::new(Term::App(Box::new(x), Box::new(Term::S))), Box::new(Term::K), ) } fn step(term: Term, definitions: &HashMap) -> Term { match term { Term::App(l, r) => { if let (Term::Num(a), Term::Num(b)) = (l.as_ref(), r.as_ref()) { let exp_a = safe_exp(*a); let ln_b = b.ln(); return Term::Num(exp_a - ln_b); } if let Term::Iota = l.as_ref() { return iota_to_sk(*r); } if let Term::App(ll, lr) = l.as_ref() { if let Term::Iota = ll.as_ref() { return Term::App(Box::new(iota_to_sk((**lr).clone())), r); } if let Term::K = ll.as_ref() { return *lr.clone(); } if let Term::App(l3, r3) = ll.as_ref() { if let Term::S = l3.as_ref() { let x = r3.as_ref().clone(); let y = lr.as_ref().clone(); let z = r.as_ref().clone(); return Term::App( Box::new(Term::App(Box::new(x), Box::new(z.clone()))), Box::new(Term::App(Box::new(y), Box::new(z))), ); } } } let l = *l; let r = *r; Term::App( Box::new(step(l, definitions)), Box::new(step(r, definitions)), ) } Term::Var(v) => { if let Some(def) = definitions.get(&v) { (*def).clone() } else { Term::Var(v) } } _ => term, } } #[cfg(test)] mod tests { use super::*; use crate::Term; use std::collections::HashMap; fn s() -> Term { Term::S } fn k() -> Term { Term::K } fn iota() -> Term { Term::Iota } fn var(s: &str) -> Term { Term::Var(s.to_string()) } fn app(l: Term, r: Term) -> Term { Term::App(Box::new(l), Box::new(r)) } fn num(re: f64, im: f64) -> Term { Term::Num(crate::C::new(re, im)) } fn empty_defs() -> HashMap { HashMap::new() } fn reduce_term(term: Term, max: usize) -> Term { let defs = empty_defs(); let substituted = substitute_definitions(term, &defs); reduce(substituted, &defs, max).unwrap() } fn approx_eq(a: crate::C, b: crate::C, eps: f64) -> bool { (a - b).norm() < eps } fn one() -> Term { num(1.0, 0.0) } fn exp_t(t: Term) -> Term { app(t, one()) } fn ln_t(t: Term) -> Term { app(one(), app(app(one(), t), one())) } fn sub_t(x: Term, y: Term) -> Term { app(ln_t(x), exp_t(y)) } fn neg_t(t: Term) -> Term { app(ln_t(ln_t(one())), exp_t(t)) } fn add_t(x: Term, y: Term) -> Term { sub_t(x, neg_t(y)) } fn mul_t(x: Term, y: Term) -> Term { app(sub_t(ln_t(x), neg_t(ln_t(y))), one()) } fn inv_t(t: Term) -> Term { app(neg_t(ln_t(t)), one()) } fn div_t(x: Term, y: Term) -> Term { mul_t(x, inv_t(y)) } fn two() -> Term { sub_t(one(), neg_t(one())) } fn half_t(t: Term) -> Term { div_t(t, two()) } fn sqr_t(t: Term) -> Term { mul_t(t.clone(), t) } fn sqrt_t(t: Term) -> Term { app(half_t(ln_t(t)), one()) } fn cosh_t(t: Term) -> Term { half_t(sub_t(exp_t(t.clone()), neg_t(exp_t(neg_t(t))))) } fn sinh_t(t: Term) -> Term { app(t.clone(), exp_t(cosh_t(t))) } fn pi_t() -> Term { sqrt_t(neg_t(sqr_t(ln_t(neg_t(one()))))) } fn cos_t(t: Term) -> Term { cosh_t(sqrt_t(neg_t(sqr_t(t)))) } #[test] fn test_iota_x_reduces_to_x_s_k() { let term = app(iota(), var("x")); let reduced = reduce_term(term, 100); let expected = app(app(var("x"), s()), k()); assert_eq!(reduced, expected); } #[test] fn test_iota_iota() { let reduced = reduce_term(app(iota(), iota()), 200); assert!(!matches!(reduced, Term::Iota)); } #[test] fn test_iota_iota_iota() { let reduced = reduce_term(app(app(iota(), iota()), iota()), 200); assert_eq!(reduced, iota()); } #[test] fn test_iota_app_to_iota() { let reduced = reduce_term(app(iota(), app(iota(), var("x"))), 200); assert!(!matches!(reduced, Term::Iota)); } #[test] fn test_k_k_k_reduces() { let reduced = reduce_term(app(app(k(), k()), k()), 100); assert!(matches!(reduced, Term::K)); } #[test] fn test_k_x_y_returns_x() { let reduced = reduce_term(app(app(k(), var("x")), var("y")), 100); assert_eq!(reduced, var("x")); } #[test] fn test_s_x_y_z() { let reduced = reduce_term(app(app(app(s(), var("x")), var("y")), var("z")), 100); match reduced { Term::App(l, r) => { match *l { Term::App(l2, r2) => { assert_eq!(*l2, var("x")); assert_eq!(*r2, var("z")); } _ => panic!("Expected x z"), } assert_eq!(*r, app(var("y"), var("z"))); } _ => panic!("Expected application"), } } #[test] fn test_k_continues_as_function() { assert_eq!(reduce_term(k(), 100), k()); } #[test] fn test_k_x_does_not_reduce() { let term = app(k(), var("z")); assert_eq!(reduce_term(term.clone(), 100), term); } #[test] fn test_i_does_not_reduce_alone() { assert_eq!(reduce_term(iota(), 100), iota()); } #[test] fn test_e_is_one() { assert_eq!(reduce_term(num(1.0, 0.0), 100), num(1.0, 0.0)); } #[test] fn test_eml_e_e_is_euler() { match reduce_term(app(num(1.0, 0.0), num(1.0, 0.0)), 100) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(std::f64::consts::E, 0.0), 1e-12)), _ => panic!("Expected Num"), } } #[test] fn test_eml_left_associative() { match reduce_term(app(app(num(1.0, 0.0), num(1.0, 0.0)), num(1.0, 0.0)), 100) { Term::Num(c) => { let expected = crate::C::new(std::f64::consts::E, 0.0).exp(); assert!(approx_eq(c, expected, 1e-10)); } _ => panic!("Expected Num"), } } #[test] fn test_eml_right_nested() { match reduce_term(app(num(1.0, 0.0), app(num(1.0, 0.0), num(1.0, 0.0))), 100) { Term::Num(c) => { let expected = crate::C::new(std::f64::consts::E - 1.0, 0.0); assert!(approx_eq(c, expected, 1e-12)); } _ => panic!("Expected Num"), } } #[test] fn test_eml_stuck_num_var() { let term = app(num(1.0, 0.0), var("x")); assert_eq!(reduce_term(term.clone(), 100), term); } #[test] fn test_eml_stuck_var_num() { let term = app(var("x"), num(1.0, 0.0)); assert_eq!(reduce_term(term.clone(), 100), term); } #[test] fn test_eml_stuck_num_combinator() { let term = app(num(1.0, 0.0), var("s")); assert_eq!(reduce_term(term.clone(), 100), term); } #[test] fn test_eml_with_k_combinator() { let term = app( app(Term::K, num(1.0, 0.0)), app(num(1.0, 0.0), num(1.0, 0.0)), ); assert_eq!(reduce_term(term, 100), num(1.0, 0.0)); } #[test] fn test_eml_complex_ln_negative_one() { match reduce_term(app(num(1.0, 0.0), num(-1.0, 0.0)), 100) { Term::Num(c) => { let expected = crate::C::new(std::f64::consts::E, -std::f64::consts::PI); assert!(approx_eq(c, expected, 1e-12)); } _ => panic!("Expected Num"), } } #[test] fn test_eml_complex_exp_pure_imaginary() { match reduce_term(app(num(0.0, std::f64::consts::PI), num(1.0, 0.0)), 100) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(-1.0, 0.0), 1e-12)), _ => panic!("Expected Num"), } } #[test] fn test_eml_exp_of_ln_is_identity() { match reduce_term(app(ln_t(num(2.0, 0.0)), one()), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(2.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_ln_of_exp_is_identity() { match reduce_term(ln_t(app(num(2.0, 0.0), one())), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(2.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_subtraction() { match reduce_term(sub_t(num(5.0, 0.0), num(3.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(2.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_negation() { match reduce_term(neg_t(num(2.5, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(-2.5, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_addition() { match reduce_term(add_t(num(2.0, 0.0), num(3.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(5.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_multiplication() { match reduce_term(mul_t(num(2.0, 0.0), num(3.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(6.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_inverse() { match reduce_term(inv_t(num(4.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(0.25, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_division() { match reduce_term(div_t(num(7.0, 0.0), num(2.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(3.5, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_square() { match reduce_term(sqr_t(num(3.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(9.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_sqrt() { match reduce_term(sqrt_t(num(9.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(3.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_sqrt_negative_one() { match reduce_term(sqrt_t(num(-1.0, 0.0)), 10000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(0.0, 1.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_pi() { match reduce_term(pi_t(), 100000) { Term::Num(c) => assert!(approx_eq( c, crate::C::new(std::f64::consts::PI, 0.0), 1e-10 )), _ => panic!("Expected Num"), } } #[test] fn test_eml_cosh_zero() { match reduce_term(cosh_t(num(0.0, 0.0)), 100000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(1.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_sinh_zero() { match reduce_term(sinh_t(num(0.0, 0.0)), 100000) { Term::Num(c) => assert!(approx_eq(c, crate::C::new(0.0, 0.0), 1e-10)), _ => panic!("Expected Num"), } } #[test] fn test_eml_cos_one() { match reduce_term(cos_t(num(1.0, 0.0)), 100000) { Term::Num(c) => { assert!(approx_eq(c, crate::C::new(0.5403023058681398, 0.0), 1e-8)); } _ => panic!("Expected Num"), } } #[test] fn test_substitute_preserves_structure() { let defs = empty_defs(); let result = substitute_definitions(app(s(), k()), &defs); match result { Term::App(l, r) => { assert!(matches!(*l, Term::S)); assert!(matches!(*r, Term::K)); } _ => panic!("Expected App(S, K)"), } } #[test] fn test_substitute_with_def() { let mut defs = HashMap::new(); let k_term = k(); defs.insert("x".to_string(), &k_term); assert_eq!(substitute_definitions(var("x"), &defs), k()); } #[test] fn test_substitute_nested() { let mut defs = HashMap::new(); let k_term = k(); defs.insert("a".to_string(), &k_term); let result = substitute_definitions(app(var("a"), var("b")), &defs); match result { Term::App(l, r) => { assert!(matches!(*l, Term::K)); assert_eq!(*r, var("b")); } _ => panic!("Expected K b"), } } #[test] fn test_substitute_iota_unchanged() { let defs = empty_defs(); assert_eq!(substitute_definitions(iota(), &defs), iota()); } }