From 7752d73216578d5961751b5d0535088d384b4aa6 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Sat, 25 Jan 2025 10:45:41 +0100 Subject: Move λ-calcul workshop code to subdirectory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lambda-calcul/rust/src/ast.rs | 117 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 lambda-calcul/rust/src/ast.rs (limited to 'lambda-calcul/rust/src/ast.rs') diff --git a/lambda-calcul/rust/src/ast.rs b/lambda-calcul/rust/src/ast.rs new file mode 100644 index 0000000..d0f1d6f --- /dev/null +++ b/lambda-calcul/rust/src/ast.rs @@ -0,0 +1,117 @@ +use proptest::{ + prelude::*, + string::{string_regex, RegexGeneratorStrategy}, +}; +use serde::{Deserialize, Serialize}; +use std::fmt::{self, Display}; + +#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] +pub enum Value { + Num(i32), + Bool(bool), + Sym(String), + App(Box, Box), + Lam(String, Box), + Def(String, Box), + Let(String, Box, Box), +} + +use Value::*; + +impl Value { + /// Return the spine of an application + fn spine(&self) -> Vec { + match self { + App(l, r) => { + let mut spine = l.spine(); + spine.push(*r.clone()); + spine + } + _ => vec![self.clone()], + } + } +} + +impl Display for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Value::Num(i) => write!(f, "{}", i), + Value::Bool(b) => write!(f, "{}", b), + Value::Sym(s) => write!(f, "{}", s), + Value::App(_, _) => { + let app = self + .spine() + .iter() + .map(|v| v.to_string()) + .collect::>() + .join(" "); + write!(f, "({})", app) + } + 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), + } + } +} + +pub const IDENTIFIER: &str = "\\pL(\\pL|\\pN)*"; + +pub fn identifier() -> RegexGeneratorStrategy { + string_regex(IDENTIFIER).unwrap() +} + +pub fn ascii_identifier() -> RegexGeneratorStrategy { + string_regex("[a-zA-Z][a-zA-Z0-9]*").unwrap() +} + +impl Arbitrary for Value { + type Parameters = (); + type Strategy = BoxedStrategy; + + fn arbitrary_with(_args: ()) -> Self::Strategy { + let any_num = any::().prop_map(Num); + let any_bool = any::().prop_map(Bool); + let leaf = prop_oneof![ + any_num, + any_bool, + // see https://unicode.org/reports/tr18/#General_Category_Property for one letter unicode categories + identifier().prop_map(Sym), + ]; + let expr = leaf.prop_recursive(4, 128, 5, move |inner| { + prop_oneof![ + (inner.clone(), inner.clone()).prop_map(|(l, r)| App(Box::new(l), Box::new(r))), + (identifier(), inner.clone()).prop_map(|(var, body)| Lam(var, Box::new(body))), + (identifier(), inner.clone(), inner.clone()).prop_map(|(var, body, expr)| { + Value::Let(var, Box::new(body), Box::new(expr)) + }), + ] + }); + prop_oneof![ + expr.clone(), + (identifier(), expr).prop_map(|(var, body)| Def(var, Box::new(body))) + ] + .boxed() + } +} + +#[cfg(test)] +mod ast_tests { + + use super::Value::{self, *}; + use proptest::collection::vec; + use proptest::prelude::*; + + 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