From e4af8559784e851047702045ff677c9a91897fdd Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Wed, 9 Oct 2024 09:38:52 +0200 Subject: [wip] client generates applications with more than 2 terms --- rust/src/ast.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'rust/src/ast.rs') diff --git a/rust/src/ast.rs b/rust/src/ast.rs index e70f323..29d548f 100644 --- a/rust/src/ast.rs +++ b/rust/src/ast.rs @@ -22,7 +22,13 @@ impl Display for Value { Value::Num(i) => write!(f, "{}", i), Value::Bool(b) => write!(f, "{}", b), Value::Sym(s) => write!(f, "{}", s), - Value::App(l, r) => write!(f, "({} {})", l, r), + Value::App(l, r) => { + if let App(l1, l2) = *l.clone() { + write!(f, "({} {} {})", l1, l2, r) + } else { + write!(f, "({} {})", l, r) + } + } Value::Lam(var, body) => write!(f, "(lam {} {})", var, body), Value::Def(var, value) => write!(f, "(def {} {})", var, value), Value::Let(var, value, body) => write!(f, "(let ({} {}) {})", var, value, body), @@ -71,3 +77,32 @@ impl Arbitrary for Value { .boxed() } } + +#[cfg(test)] +mod ast_tests { + + use super::{ + identifier, + Value::{self, *}, + }; + use proptest::collection::vec; + use proptest::prelude::*; + + fn any_sym() -> impl Strategy { + identifier().prop_map(Sym) + } + + proptest! { + + #[test] + fn display_multiple_applications_as_a_sequence(atoms in vec("[a-z]".prop_map(Sym), 2..10)) { + let init = atoms.first().unwrap().clone(); + let value = atoms.iter().skip(1).fold(init, |acc, expr| { + Value::App(Box::new(acc.clone()), Box::new(expr.clone())) + }); + assert_eq!(value.to_string(), + format!("({})", + atoms.iter().map(|v| v.to_string()).collect::>().join(" "))); + } + } +} -- cgit v1.2.3