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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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<Value>, Box<Value>),
Lam(String, Box<Value>),
Def(String, Box<Value>),
Let(String, Box<Value>, Box<Value>),
}
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(l, r) => 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),
}
}
}
use Value::*;
pub const IDENTIFIER: &str = "\\pL(\\pL|\\pN)*";
pub fn identifier() -> RegexGeneratorStrategy<String> {
string_regex(IDENTIFIER).unwrap()
}
pub fn ascii_identifier() -> RegexGeneratorStrategy<String> {
string_regex("[a-zA-Z][a-zA-Z0-9]*").unwrap()
}
impl Arbitrary for Value {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(_args: ()) -> Self::Strategy {
let any_num = any::<i32>().prop_map(Num);
let any_bool = any::<bool>().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()
}
}
|