1
//! The per-head lemma over the whitelist and the macroexpand heads.
2

            
3
use super::*;
4

            
5
/// The per-head lemma: with constant operands — well-formed and malformed
6
/// alike — a whitelisted head's eval answers a datum or an error, never a
7
/// placeholder, and the table is unchanged either way.
8
#[test]
9
1
fn whitelisted_heads_answer_a_datum_or_an_error_and_keep_the_table() {
10
1
    let mut symbols = with_constant("C", "'(1 2)");
11
1
    for source in [
12
1
        "(block b 1)",
13
1
        "(block)",
14
1
        "(if t 1 2)",
15
1
        "(if)",
16
1
        "(progn)",
17
1
        "(progn 1 2)",
18
1
        "(let ((x 1)) x)",
19
1
        "(let x)",
20
1
        "(let* ((x 1) (y x)) y)",
21
1
        "(dolist (x c) x)",
22
1
        "(dolist (x 1) x)",
23
1
        "(dolist x)",
24
1
        "(and 1 nil)",
25
1
        "(or nil 2)",
26
1
        "(quote (1 . 2))",
27
1
        "(quote)",
28
1
        "(list 1 'a \"s\")",
29
1
        "(cons 1 c)",
30
1
        "(cons 1)",
31
1
        "(length c)",
32
1
        "(length 1)",
33
1
        "(car c)",
34
1
        "(car nil)",
35
1
        "(car 1)",
36
1
        "(car)",
37
1
        "(cdr c)",
38
1
        "(cdr (quote (1 . 2)))",
39
1
        "(cdr 1)",
40
1
        "(cdr c c)",
41
1
        "(reverse c)",
42
1
        "(reverse nil)",
43
1
        "(reverse 1)",
44
1
        "(reverse)",
45
1
        "(append c c)",
46
1
        "(append)",
47
1
        "(append c 1)",
48
1
        "(append 1 c)",
49
1
        "(pair? c)",
50
1
        "(pair? nil)",
51
1
        "(pair? 1)",
52
1
        "(pair?)",
53
1
        "(not nil)",
54
1
        "(not)",
55
1
        "(null? nil)",
56
1
        "(null? c)",
57
1
        "(null?)",
58
1
        "(= 1 1)",
59
1
        "(= 1 \"a\")",
60
1
        "(/= 1 2)",
61
1
        "(< 1 2 3)",
62
1
        "(< 'a 1)",
63
1
        "(> 2 1)",
64
1
        "(<= 1 1)",
65
1
        "(>= 1 2)",
66
1
        "(+ 1 1/2)",
67
1
        "(+ 1 \"a\")",
68
1
        "(- 1)",
69
1
        "(-)",
70
1
        "(* 2 3)",
71
1
        "(* 1000000000 1000000000 1000000000)",
72
1
        "(/ 3 2)",
73
1
        "(/ 1 0)",
74
1
        "(/)",
75
1
        "(mod 7 2)",
76
1
        "(mod 1 0)",
77
1
        "(mod 1)",
78
1
        "(upcase-string \"a\")",
79
1
        "(upcase-string 1)",
80
1
    ] {
81
68
        let before = strip_charge(&symbols.state_digest());
82
68
        let answer = eval_value(&mut symbols, &form(source));
83
68
        assert_eq!(before, strip_charge(&symbols.state_digest()), "{source}");
84
68
        match answer {
85
38
            Ok(value) => assert!(is_constant_datum(&value), "{source}: {value:?}"),
86
            Err(Error::Resource(_)) => panic!("{source}: resource error"),
87
30
            Err(_) => {}
88
        }
89
    }
90
1
}
91

            
92
/// A macro body's store to anything outside its private frame is refused
93
/// (ADR-0034:233); a store to its own parameter or LET variable is allowed.
94
#[test]
95
1
fn a_macro_body_may_not_mutate_a_global() {
96
1
    let mut symbols = with_constant("G", "0");
97
1
    symbols.define(
98
1
        Symbol::new("BUMP", SymbolKind::Macro).with_function(Expr::Lambda(
99
1
            crate::ast::LambdaParams::simple(vec!["V".to_string()]),
100
1
            Box::new(form(
101
1
                "(progn (setf v 1) (let ((w 0)) (setf w 2)) (setf g 1))",
102
1
            )),
103
1
        )),
104
1
    );
105
1
    let err = eval_value(&mut symbols, &form("(bump 0)")).expect_err("the global store is refused");
106
1
    assert!(err.to_string().contains("may not mutate a global"), "{err}");
107
1
    symbols.define(
108
1
        Symbol::new("LOCAL", SymbolKind::Macro).with_function(Expr::Lambda(
109
1
            crate::ast::LambdaParams::simple(vec!["V".to_string()]),
110
1
            Box::new(form("(progn (setf v 1) (let ((w 0)) (setf w 2) w))")),
111
1
        )),
112
1
    );
113
1
    assert_eq!(
114
1
        eval_value(&mut symbols, &form("(local 0)")).ok(),
115
1
        Some(form("2"))
116
    );
117
1
}
118

            
119
/// `MACROEXPAND-1` / `MACROEXPAND` expand in a private clone: the answer is a
120
/// constant datum or an error and the table's bindings are untouched, even
121
/// for a macro body that prints, stores or reads a global.
122
#[test]
123
1
fn macroexpand_heads_answer_a_datum_or_an_error_and_keep_the_bindings() {
124
1
    let mut symbols = with_constant("G", "0");
125
1
    for (name, params, body) in [
126
1
        ("TWICE", vec!["X"], "`(+ ,x ,x)"),
127
1
        ("NOISY", vec!["X"], "(progn (print 99) `(+ ,x 1))"),
128
1
        ("LOCAL", vec!["V"], "(progn (setf v 1) `(quote ,v))"),
129
1
        ("READG", vec![], "`(+ ,g 1)"),
130
1
    ] {
131
4
        symbols.define(
132
4
            Symbol::new(name, SymbolKind::Macro).with_function(Expr::Lambda(
133
4
                crate::ast::LambdaParams::simple(params.into_iter().map(String::from).collect()),
134
4
                Box::new(form(body)),
135
4
            )),
136
4
        );
137
    }
138
1
    for source in [
139
1
        "(macroexpand-1 '(when t 1))",
140
1
        "(macroexpand '(when t 1))",
141
1
        "(car (macroexpand-1 '(when t 1)))",
142
1
        "(car (macroexpand '(unless t 1)))",
143
1
        "(macroexpand-1 '(twice 3))",
144
1
        "(macroexpand '(twice 3))",
145
1
        "(macroexpand-1 '(noisy 3))",
146
1
        "(macroexpand '(local 3))",
147
1
        "(macroexpand-1 '(readg))",
148
1
        "(macroexpand-1 '(twice))",
149
1
        "(macroexpand-1)",
150
1
        "(macroexpand 1 2)",
151
1
        "(macroexpand-1 1)",
152
1
        "(macroexpand '(car '(1)))",
153
1
    ] {
154
14
        let before = strip_charge(&symbols.state_digest());
155
14
        let folded = fold_value(&mut symbols, &form(source));
156
14
        assert_eq!(before, strip_charge(&symbols.state_digest()), "{source}");
157
14
        if let Ok(Some(value)) = folded {
158
10
            assert!(is_constant_datum(&value), "{source}: {value:?}")
159
4
        }
160
    }
161
1
    assert_eq!(
162
1
        fold_value(&mut symbols, &form("(car (macroexpand-1 '(when t 1)))")).ok(),
163
1
        Some(Some(datum("'if")))
164
    );
165
1
}