grift_eval 1.4.0

Lisp evaluator for the Grift Scheme language
Documentation
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
mod common;

use grift_eval::*;
use common::{eval_to_string, eval_to_num, eval_is_false};

// ============================================================
// Section 5.6: Continuations
// ============================================================

/// member using call/cc for nonlocal exit from a do loop
#[test]
fn test_member_callcc_not_found() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define member-cc
          (lambda (x ls)
            (call/cc
              (lambda (break)
                (do ((ls ls (cdr ls)))
                    ((null? ls) #f)
                    (if (equal? x (car ls))
                        (break ls)))))))",
    )
    .unwrap();

    // (member-cc 'd '(a b c)) => #f
    assert!(eval_is_false(&lisp, &mut eval, "(member-cc 'd '(a b c))"));
}

#[test]
fn test_member_callcc_found() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define member-cc
          (lambda (x ls)
            (call/cc
              (lambda (break)
                (do ((ls ls (cdr ls)))
                    ((null? ls) #f)
                    (if (equal? x (car ls))
                        (break ls)))))))",
    )
    .unwrap();

    // (member-cc 'b '(a b c)) => (b c)
    assert_eq!(
        eval_to_string(&lisp, &mut eval, "(member-cc 'b '(a b c))"),
        "(b c)"
    );
}

/// dynamic-wind basic: in/body/out thunks called in order
#[test]
fn test_dynamic_wind_basic() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str("(define log '())").unwrap();

    let result = eval
        .eval_str(
            "(dynamic-wind
              (lambda () (set! log (cons 'in log)))
              (lambda () (set! log (cons 'body log)) 42)
              (lambda () (set! log (cons 'out log))))",
        )
        .unwrap();

    // Body returns 42
    assert_eq!(lisp.get(result).unwrap().as_number(), Some(42));

    // log should be (out body in) — most recent first
    assert_eq!(eval_to_string(&lisp, &mut eval, "log"), "(out body in)");
}

/// unwind-protect macro: cleanup runs even on continuation escape
#[test]
fn test_unwind_protect() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define-syntax unwind-protect
          (syntax-rules ()
            ((_ body cleanup ...)
             (dynamic-wind
               (lambda () #f)
               (lambda () body)
               (lambda () cleanup ...)))))",
    )
    .unwrap();

    // ((call/cc
    //    (let ((x 'a))
    //      (lambda (k)
    //        (unwind-protect
    //          (k (lambda () x))
    //          (set! x 'b)))))) => b
    let result = eval
        .eval_str(
            "((call/cc
               (let ((x 'a))
                 (lambda (k)
                   (unwind-protect
                     (k (lambda () x))
                     (set! x 'b))))))",
        )
        .unwrap();
    assert!(lisp.symbol_matches(result, "b").unwrap());
}

/// fluid-let basic: temporary binding restored after exit
#[test]
fn test_fluid_let_basic() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define-syntax fluid-let
          (syntax-rules ()
            ((_ ((x v)) e1 e2 ...)
             (let ((y v))
               (let ((swap (lambda ()
                             (let ((t x))
                               (set! x y)
                               (set! y t)))))
                 (dynamic-wind
                   swap
                   (lambda () e1 e2 ...)
                   swap))))))",
    )
    .unwrap();

    // (let ((x 3))
    //   (+ (fluid-let ((x 5)) x) x)) => 8
    assert_eq!(
        eval_to_num(
            &lisp,
            &mut eval,
            "(let ((x 3))
              (+ (fluid-let ((x 5))
                   x)
                 x))"
        ),
        8
    );
}

/// fluid-let with call/cc: variable reverts on continuation escape
#[test]
fn test_fluid_let_callcc_revert() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define-syntax fluid-let
          (syntax-rules ()
            ((_ ((x v)) e1 e2 ...)
             (let ((y v))
               (let ((swap (lambda ()
                             (let ((t x))
                               (set! x y)
                               (set! y t)))))
                 (dynamic-wind
                   swap
                   (lambda () e1 e2 ...)
                   swap))))))",
    )
    .unwrap();

    // (let ((x 'a))
    //   (let ((f (lambda () x)))
    //     (cons (call/cc
    //             (lambda (k)
    //               (fluid-let ((x 'b))
    //                 (f))))
    //           (f)))) => (b . a)
    assert_eq!(
        eval_to_string(
            &lisp,
            &mut eval,
            "(let ((x 'a))
              (let ((f (lambda () x)))
                (cons (call/cc
                        (lambda (k)
                          (fluid-let ((x 'b))
                            (f))))
                      (f))))"
        ),
        "(b . a)"
    );
}

/// fluid-let with reenter: continuation re-invocation reinstates temporary value
#[test]
fn test_fluid_let_reenter() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define-syntax fluid-let
          (syntax-rules ()
            ((_ ((x v)) e1 e2 ...)
             (let ((y v))
               (let ((swap (lambda ()
                             (let ((t x))
                               (set! x y)
                               (set! y t)))))
                 (dynamic-wind
                   swap
                   (lambda () e1 e2 ...)
                   swap))))))",
    )
    .unwrap();

    eval.eval_str("(define reenter #f)").unwrap();
    eval.eval_str("(define x 0)").unwrap();

    // First invocation returns 2
    assert_eq!(
        eval_to_num(
            &lisp,
            &mut eval,
            "(fluid-let ((x 1))
              (call/cc (lambda (k) (set! reenter k)))
              (set! x (+ x 1))
              x)"
        ),
        2
    );

    // x should be 0 after fluid-let exits
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 0);

    // Re-entering: x was 2 inside, so now becomes 3
    assert_eq!(eval_to_num(&lisp, &mut eval, "(reenter '*)"), 3);

    // Re-entering again: x was 3 inside, so now becomes 4
    assert_eq!(eval_to_num(&lisp, &mut eval, "(reenter '*)"), 4);

    // x is still 0 outside
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 0);
}

// ============================================================
// Section 5.7: Delayed Evaluation
// ============================================================

/// Basic delay and force
#[test]
fn test_delay_force_basic() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    assert_eq!(
        eval_to_num(&lisp, &mut eval, "(force (delay (+ 1 2)))"),
        3
    );
}

/// delay memoizes: expression evaluated only once
#[test]
fn test_delay_memoization() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str("(define count 0)").unwrap();
    eval.eval_str(
        "(define p (delay (begin (set! count (+ count 1)) count)))",
    )
    .unwrap();

    assert_eq!(eval_to_num(&lisp, &mut eval, "(force p)"), 1);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(force p)"), 1);
    // count should be 1 — only evaluated once
    assert_eq!(eval_to_num(&lisp, &mut eval, "count"), 1);
}

/// Stream abstraction: stream-car, stream-cdr, counters
#[test]
fn test_streams_basic() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define stream-car
          (lambda (s)
            (car (force s))))",
    )
    .unwrap();

    eval.eval_str(
        "(define stream-cdr
          (lambda (s)
            (cdr (force s))))",
    )
    .unwrap();

    eval.eval_str(
        "(define counters
          (let next ((n 1))
            (delay (cons n (next (+ n 1))))))",
    )
    .unwrap();

    // (stream-car counters) => 1
    assert_eq!(eval_to_num(&lisp, &mut eval, "(stream-car counters)"), 1);

    // (stream-car (stream-cdr counters)) => 2
    assert_eq!(
        eval_to_num(&lisp, &mut eval, "(stream-car (stream-cdr counters))"),
        2
    );
}

/// stream-add and even-counters
#[test]
fn test_stream_add() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    eval.eval_str(
        "(define stream-car
          (lambda (s)
            (car (force s))))",
    )
    .unwrap();

    eval.eval_str(
        "(define stream-cdr
          (lambda (s)
            (cdr (force s))))",
    )
    .unwrap();

    eval.eval_str(
        "(define counters
          (let next ((n 1))
            (delay (cons n (next (+ n 1))))))",
    )
    .unwrap();

    eval.eval_str(
        "(define stream-add
          (lambda (s1 s2)
            (delay (cons
                     (+ (stream-car s1) (stream-car s2))
                     (stream-add (stream-cdr s1) (stream-cdr s2))))))",
    )
    .unwrap();

    eval.eval_str("(define even-counters (stream-add counters counters))")
        .unwrap();

    // (stream-car even-counters) => 2
    assert_eq!(
        eval_to_num(&lisp, &mut eval, "(stream-car even-counters)"),
        2
    );

    // (stream-car (stream-cdr even-counters)) => 4
    assert_eq!(
        eval_to_num(
            &lisp,
            &mut eval,
            "(stream-car (stream-cdr even-counters))"
        ),
        4
    );
}

/// make-promise and force definitions from the spec
#[test]
fn test_make_promise_force() {
    let lisp: Lisp<30000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();

    // Use the spec's definitions
    eval.eval_str(
        "(define make-promise
          (lambda (p)
            (let ((val #f) (set? #f))
              (lambda ()
                (if (not set?)
                    (let ((x (p)))
                      (if (not set?)
                          (begin (set! val x)
                                 (set! set? #t)))))
                val))))",
    )
    .unwrap();

    eval.eval_str(
        "(define my-force
          (lambda (promise)
            (promise)))",
    )
    .unwrap();

    eval.eval_str(
        "(define my-delay-val (make-promise (lambda () (+ 10 20))))",
    )
    .unwrap();

    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-force my-delay-val)"), 30);
    // Second force returns memoized value
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-force my-delay-val)"), 30);
}