11
11
#include "parser.h"
12
12
#include "storage.h"
13
13
14
+ static obj is_amb_p (obj args )
15
+ {
16
+ return is_amb (car (args )) ? true_o : false_o ;
17
+ }
18
+
19
+ static obj amb_choices_p (obj args )
20
+ {
21
+ return cdar (args );
22
+ }
23
+
14
24
static obj evalstr (char * e , obj env )
15
25
{
16
26
return eceval (readp (openin_string (e )), env );
@@ -23,6 +33,10 @@ static void init(obj execution_environment)
23
33
24
34
add_primprocs (ambenv );
25
35
36
+ define_variable (of_identifier ("amb?" ), of_function (is_amb_p ), ambenv );
37
+ define_variable (of_identifier ("amb-choices" ),
38
+ of_function (amb_choices_p ), ambenv );
39
+
26
40
evalstr ("(define (map proc . arglists)"
27
41
" (define (smap proc items)"
28
42
" (define (iter items mapped)"
@@ -42,7 +56,6 @@ static void init(obj execution_environment)
42
56
ambenv );
43
57
44
58
evalstr ("(define (analyze exp)"
45
- // " (newline)(display \"Exp:\")(display exp)(newline)"
46
59
" (cond ((self-evaluating? exp) "
47
60
" (analyze-self-evaluating exp))"
48
61
" ((quoted? exp) (analyze-quoted exp))"
@@ -61,6 +74,7 @@ static void init(obj execution_environment)
61
74
" ((delay? exp) (analyze (delay->lambda exp)))"
62
75
" ((cons-stream? exp) (analyze (cons-stream->cons exp)))"
63
76
" ((time? exp) (analyze-time exp))"
77
+ " ((amb? exp) (analyze-amb exp))"
64
78
" ((apply? exp) (analyze-apply exp))"
65
79
" ((application? exp) (analyze-application exp))"
66
80
" (else"
@@ -205,6 +219,18 @@ static void init(obj execution_environment)
205
219
" (lambda (env succeed fail)"
206
220
" (time (proc env succeed fail)))))" ,
207
221
ambenv );
222
+ evalstr ("(define (analyze-amb exp)"
223
+ " (let ((cprocs (map analyze (amb-choices exp))))"
224
+ " (lambda (env succeed fail)"
225
+ " (define (try-next choices)"
226
+ " (if (null? choices)"
227
+ " (fail)"
228
+ " ((car choices) env"
229
+ " succeed"
230
+ " (lambda ()"
231
+ " (try-next (cdr choices))))))"
232
+ " (try-next cprocs))))" ,
233
+ ambenv );
208
234
}
209
235
210
236
static obj ambeval (obj exp , obj exenv , obj succeed , obj fail )
0 commit comments