-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.5-ambeval-exp.rkt
909 lines (814 loc) · 26.6 KB
/
5.5-ambeval-exp.rkt
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#lang sicp
;(#%require racket)
(#%require (only racket provide))
(define ambeval-exp '(begin
; Core of Evaluator: eval/apply
(define (ambeval exp env succeed fail)
((analyze exp) env succeed fail))
(define (analyze exp)
(cond ((self-evaluating? exp)
(analyze-self-evaluating exp))
((quoted? exp)
(analyze-quoted exp))
((variable? exp)
(analyze-variable exp))
((assignment? exp)
(analyze-assignment exp))
((permanent-assignment? exp)
(analyze-permanent-assignment exp))
((definition? exp)
(analyze-definition exp))
((unbound? exp)
(analyze-unbound exp))
((if? exp)
(analyze-if exp))
((if-fail? exp)
(analyze-if-fail exp))
((must-fail? exp)
(analyze-must-fail exp))
((lambda? exp)
(analyze-lambda exp))
((begin? exp)
(analyze-sequence
(begin-actions exp)))
((cond? exp)
(analyze (cond->if exp)))
((and? exp)
(analyze (and->if exp)))
((or? exp)
(analyze (or->if exp)))
((let? exp)
(analyze (let->combination exp)))
((let*? exp)
(analyze (let*->nested-lets exp)))
((letrec? exp)
(analyze (letrec->let exp)))
((amb? exp) (analyze-amb exp))
((ramb? exp) (analyze-ramb exp))
((count? exp) (analyze-count exp))
((call/cc? exp) (analyze-call/cc exp))
((application? exp)
(analyze-application exp))
(else
(error "Unknown expression
type: ANALYZE"
exp))))
(define (analyze-self-evaluating exp)
(lambda (env succeed fail)
(succeed exp fail)))
(define (analyze-quoted exp)
(let ((qval (text-of-quotation exp)))
(lambda (env succeed fail)
(succeed qval fail))))
(define (analyze-variable exp)
(lambda (env succeed fail)
(succeed (lookup-variable-value exp env)
fail)))
; 赋值是我们第一次真正使用延续的地方
(define (analyze-assignment exp)
(let ((var (assignment-variable exp))
(vproc (analyze
(assignment-value exp))))
(lambda (env succeed fail)
(vproc env
(lambda (val fail2) ; *1*
(let ((old-value
(lookup-variable-value-ex
var
env)))
(set-variable-value!
var
val
env)
(succeed
'ok
(lambda () ; *2*
(set-variable-value!
var
old-value
env)
(fail2)))))
fail))))
(define (analyze-permanent-assignment exp)
(let ((var (assignment-variable exp))
(vproc (analyze
(assignment-value exp))))
(lambda (env succeed fail)
(vproc env
(lambda (val fail2) ; *1*
(set-variable-value! var val env)
(succeed 'ok fail2))
fail))))
; 我们不用担心撤销定义,因为我们假设内部定义已经被扫描出去了。
(define (analyze-definition exp)
(let ((var (definition-variable exp))
(vproc (analyze
(definition-value exp))))
(lambda (env succeed fail)
(vproc env
(lambda (val fail2)
(define-variable! var val env)
(succeed 'ok fail2))
fail))))
; 只unbound当前帧的,否则在撤销的时候需要知道是在哪个帧
(define (analyze-unbound exp)
(let ((var (unbound-variable exp)))
(lambda (env succeed fail)
(let ((old-value (lookup-variable-value var env)))
(unbound-variable! var env)
(succeed 'ok
(lambda ()
(define-variable! var old-value env)
(fail)))))))
; if 语句里也可能有amb,会失败。
(define (analyze-if exp)
(let ((pproc (analyze (if-predicate exp)))
(cproc (analyze (if-consequent exp)))
(aproc (analyze (if-alternative exp))))
(lambda (env succeed fail)
(pproc env
;; success continuation for evaluating
;; the predicate to obtain pred-value
(lambda (pred-value fail2)
(if (true? pred-value)
(cproc env succeed fail2)
(aproc env succeed fail2)))
;; failure continuation for
;; evaluating the predicate
fail))))
(define (analyze-lambda exp)
(let ((vars (lambda-parameters exp))
(bproc (scan-out-defines
(lambda-body exp))))
(lambda (env succeed fail)
(succeed (make-procedure vars bproc env)
fail))))
(define (analyze-sequence exps)
(define (sequentially a b)
(lambda (env succeed fail)
(a env
;; success continuation for calling a
(lambda (a-value fail2)
(b env succeed fail2))
;; failure continuation for calling a
fail)))
(define (loop first-proc rest-procs)
(if (null? rest-procs)
first-proc
(loop (sequentially first-proc
(car rest-procs))
(cdr rest-procs))))
(let ((procs (map analyze exps)))
(if (null? procs)
(error "Empty sequence: ANALYZE"))
(loop (car procs) (cdr procs))))
(define (analyze-application exp)
(let ((fproc (analyze (operator exp)))
(aprocs (map analyze (operands exp))))
(lambda (env succeed fail)
(fproc env
(lambda (proc fail2)
(get-args aprocs env
(lambda (args fail3)
(execute-application
proc args succeed fail3))
fail2))
fail))))
(define (get-args aprocs env succeed fail)
(if (null? aprocs)
(succeed '() fail)
((car aprocs)
env
;; success continuation for this aproc
(lambda (arg fail2)
(get-args
(cdr aprocs)
env
;; success continuation for
;; recursive call to get-args
(lambda (args fail3)
(succeed (cons arg args)
fail3))
fail2))
fail)))
(define (execute-application
proc args succeed fail)
(cond ((primitive-procedure? proc)
(succeed
(apply-primitive-procedure
proc args)
fail))
((compound-procedure? proc)
((procedure-body proc)
(extend-environment
(procedure-parameters proc)
args
(procedure-environment proc))
succeed
fail))
(else (error "Unknown procedure type:
EXECUTE-APPLICATION"
proc))))
(define (analyze-amb exp)
(let ((cprocs (map analyze (amb-choices exp))))
(lambda (env succeed fail)
(define (try-next choices)
(if (null? choices)
(fail)
((car choices)
env
succeed
(lambda ()
(try-next (cdr choices))))))
(try-next cprocs))))
(define (shuffle lst)
(map cdr
(sort
(map (lambda (x) (cons (random) x)) lst)
(lambda (x y) (< (car x) (car y))))))
(define (analyze-ramb exp)
(let ((cprocs (map analyze (ramb-choices exp))))
(lambda (env succeed fail)
(define (try-next choices)
(if (null? choices)
(fail)
((car choices)
env
succeed
(lambda ()
(try-next (cdr choices))))))
(try-next (shuffle cprocs))))) ; shuffle at runtime
(define (analyze-if-fail exp)
(let ((proc1 (analyze (if-fail-exp1 exp)))
(proc2 (analyze (if-fail-exp2 exp))))
(lambda (env succeed fail)
(proc1 env
succeed
(lambda ()
(proc2 env succeed fail))))))
(define (analyze-must-fail exp)
(let ((proc (analyze (must-fail-exp exp))))
(lambda (env succeed fail)
(proc env
(lambda (val fail2)
(fail))
(lambda ()
(succeed 'ok fail))))))
(define (analyze-count exp)
(let ((num (count-number exp))
(proc (analyze (count-exp exp))))
(lambda (env succeed fail)
(let ((count 0))
(proc env
(lambda (val try-again)
(set! count (+ count 1))
(if (> count num)
(fail) ; count has exceed, fail
(try-again))) ; otherwise try again
(lambda ()
(if (= count num)
(succeed 'ok fail) ; count precisely equals to num, succeed
(fail))))))))
(define (analyze-call/cc exp)
(let ((lproc (analyze (call/cc-lambda exp))))
(lambda (env succeed fail)
(lproc env
(lambda (proc fail2)
(execute-application
; pass the outer succeed/fail by the continuation lambda
proc (list (make-procedure '(v)
(lambda (env s f) ;ignore s f
(succeed (lookup-variable-value 'v env) fail))
env))
succeed fail2))
fail))))
(define (make-combination operator operands)
(cons operator operands))
; Representing Expressions
(define (self-evaluating? exp)
(cond ((number? exp) true)
((string? exp) true)
(else false)))
(define (variable? exp) (symbol? exp))
; (quote <text-of-quotation>>)
(define (quoted? exp)
(tagged-list? exp 'quote))
(define (text-of-quotation exp)
(cadr exp))
(define (tagged-list? exp tag)
(if (pair? exp)
(eq? (car exp) tag)
false))
; (set! <var> <value>)
(define (assignment? exp)
(tagged-list? exp 'set!))
(define (assignment-variable exp)
(cadr exp))
(define (assignment-value exp) (caddr exp))
(define (make-assignment var val)
(list 'set! var val))
(define (permanent-assignment? exp)
(tagged-list? exp 'permanent-set!))
(define (definition? exp)
(tagged-list? exp 'define))
(define (definition-variable exp)
(if (symbol? (cadr exp))
(cadr exp) ; (define <var> <value>)
(caadr exp))) ; (define (<var> <param1> ... <paramn>) <body>)
(define (definition-value exp)
(if (symbol? (cadr exp))
(caddr exp)
(make-lambda
(cdadr exp) ; formal parameters
(cddr exp)))) ; body
(define (make-define name exp)
(list 'define name exp))
(define (unbound? exp)
(tagged-list? exp 'make-unbound!))
(define (unbound-variable exp)
(cadr exp))
(define (lambda? exp)
(tagged-list? exp 'lambda))
(define (lambda-parameters exp) (cadr exp))
(define (lambda-body exp) (cddr exp))
; constructor used by definition-value
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
(define (if? exp) (tagged-list? exp 'if))
(define (if-predicate exp) (cadr exp))
(define (if-consequent exp) (caddr exp))
(define (if-alternative exp)
(if (not (null? (cdddr exp)))
(cadddr exp)
'false))
; constructor used by cond->if
(define (make-if predicate
consequent
alternative)
(list 'if
predicate
consequent
alternative))
(define (begin? exp)
(tagged-list? exp 'begin))
(define (begin-actions exp) (cdr exp))
(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
; constructor used by cond->if
(define (sequence->exp seq)
(cond ((null? seq) seq)
((last-exp? seq) (first-exp seq))
(else (make-begin seq))))
(define (make-begin seq) (cons 'begin seq))
(define (application? exp) (pair? exp))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operands ops) (cdr ops))
(define (cond? exp)
(tagged-list? exp 'cond))
(define (cond-clauses exp) (cdr exp))
(define (cond-else-clause? clause)
(eq? (cond-predicate clause) 'else))
(define (cond-predicate clause)
(car clause))
(define (cond-actions clause)
(cdr clause))
(define (apply-procedure? actions)
(and (pair? actions)
(eq? '=> (car actions))
(not (null? (cdr actions)))
(null? (cddr actions))))
(define (bad-syntax? actions)
(and (pair? actions)
(eq? '=> (car actions))
(or (null? (cdr actions))
(not (null? (cddr actions))))))
(define (actions-procedure actions)
(cadr actions))
(define (cond->if exp)
(expand-clauses (cond-clauses exp)))
(define (expand-clauses clauses)
(if (null? clauses)
'false ; no else clause
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp
(cond-actions first))
(error "ELSE clause isn't
last: COND->IF"
clauses))
(let ((predicate (cond-predicate first))
(actions (cond-actions first)))
(make-if predicate
(cond
((apply-procedure? actions)
(list (actions-procedure actions) predicate)) ; apply the procedure
((bad-syntax? actions)
(error "COND clause syntax error! => must be followed by a procedure of one argument"))
(else
(sequence->exp actions)))
(expand-clauses rest)))))))
(define (and? exp)
(tagged-list? exp 'and))
(define (and-exps exp) (cdr exp))
(define no-exps? null?)
(define (and->if exp)
(expand-and-exps (and-exps exp)))
(define (expand-and-exps exps)
(if (null? exps)
'true ; no expressions
(let ((first (car exps))
(rest (cdr exps)))
(if (no-exps? rest) ; the last expression
first ; return redirectly
(make-if first
(expand-and-exps rest)
'false)))))
(define (or? exp)
(tagged-list? exp 'or))
(define (or-exps exp) (cdr exp))
(define (or->if exp)
(expand-or-exps (or-exps exp)))
(define (expand-or-exps exps)
(if (null? exps)
'false ; no expressions
(let ((first (car exps))
(rest (cdr exps)))
(if (no-exps? rest) ; the last expression
first ; return redirectly
(make-if first
first
(expand-or-exps rest))))))
(define (let? exp)
(tagged-list? exp 'let))
(define (named-let? exp) ; (let <var> <bindings> <body>)
(not (pair? (cadr exp))))
(define (let-name exp)
(if (named-let? exp)
(cadr exp)
false))
(define (let-vars exp)
(if (named-let? exp)
(map car (caddr exp))
(map car (cadr exp))))
(define (let-inits exp)
(if (named-let? exp)
(map cadr (caddr exp))
(map cadr (cadr exp))))
(define (let-body exp)
(if (named-let? exp)
(cdddr exp)
(cddr exp)))
(define (let->combination exp)
(let ((name (let-name exp))
(vars (let-vars exp))
(inits (let-inits exp))
(body (let-body exp)))
(if name
(sequence->exp
(list (make-define (cons name vars) (sequence->exp body))
(make-combination name inits)))
(make-combination (make-lambda vars body)
inits))))
(define (make-let lop body)
(cons 'let (cons lop body))) ;Note: body needs to be sequence of expressions
(define (let*? exp)
(tagged-list? exp 'let*))
(define (let*-lop exp)
(cadr exp))
(define (let*-body exp)
(cddr exp))
(define (let*->nested-lets exp)
(let ((lop (let*-lop exp))
(body (let*-body exp)))
(define (iter lop)
(cond
((null? lop) (sequence->exp body)) ; 统一转成非list,因为make-let返回的不是list
(else (make-let (list (car lop))
(list (iter (cdr lop)))))))
(iter lop)))
(define (letrec? exp)
(tagged-list? exp 'letrec))
(define (letrec-lop exp)
(cadr exp))
(define (letrec-body exp)
(cddr exp))
(define (declare-variables exp)
(map (lambda (x) (list (car x) ''*unassigned*))
(letrec-lop exp)))
(define (set-variables exp)
(map (lambda (x) (make-assignment (car x) (cadr x)))
(letrec-lop exp)))
(define (letrec->let exp)
(make-let (declare-variables exp)
(append (set-variables exp) (letrec-body exp))))
(define (amb? exp) (tagged-list? exp 'amb))
(define (amb-choices exp) (cdr exp))
(define (ramb? exp) (tagged-list? exp 'ramb))
(define (ramb-choices exp) (cdr exp))
(define (if-fail? exp) (tagged-list? exp 'if-fail))
(define (if-fail-exp1 exp) (cadr exp))
(define (if-fail-exp2 exp) (caddr exp))
(define (must-fail? exp) (tagged-list? exp 'must-fail))
(define (must-fail-exp exp) (cadr exp))
(define (count? exp) (tagged-list? exp 'count=))
(define (count-number exp) (cadr exp))
(define (count-exp exp) (caddr exp))
(define (call/cc? exp) (tagged-list? exp 'call/cc))
(define (call/cc-lambda exp) (cadr exp))
; evaluator data structures
(define (true? x)
(not (eq? x false)))
(define (false? x)
(eq? x false))
; primitive procedure objects
(define (primitive-procedure? proc)
(tagged-list? proc 'primitive))
(define (primitive-implementation proc)
(cadr proc))
(define (original-set! var val)
(set! var val))
(define primitive-procedures
(list (list 'car car)
(list 'cdr cdr)
(list 'cadr cadr)
(list 'cddr cddr)
(list 'caddr caddr)
(list 'cons cons)
(list 'null? null?)
(list 'list list)
(list 'list-ref list-ref)
(list 'pair? pair?)
(list 'append append)
(list 'original-set! original-set!)
(list 'not not)
(list 'eq? eq?)
(list 'equal? equal?)
(list '+ +)
(list '- -)
(list '* *)
(list '/ /)
(list '> >)
(list '< <)
(list '= =)
(list '<= <=)
(list '>= >=)
(list 'sqrt sqrt)
(list 'ceiling ceiling)
(list 'floor floor)
(list 'integer? integer?)
(list 'symbol? symbol?)
(list 'number? number?)
(list 'member member)
(list 'memq memq)
(list 'abs abs)
;(list 'range range)
;(list 'sort sort)
(list 'random random)
(list 'even? even?)
(list 'newline newline)
(list 'display display)
(list 'symbol->string symbol->string)
(list 'string->symbol string->symbol)
(list 'string=? string=?)
(list 'substring substring)
(list 'string-length string-length)
(list 'string-append string-append)
(list 'number->string number->string)
(list 'assoc assoc)
;(list 'make-base-namespace make-base-namespace)
))
;⟨more primitives⟩
(define (primitive-procedure-names)
(map car primitive-procedures))
(define (primitive-procedure-objects)
(map (lambda (proc)
(list 'primitive (cadr proc)))
primitive-procedures))
(define (apply-primitive-procedure proc args)
(apply
(primitive-implementation proc) args))
; compound procedures
(define (make-procedure parameters body env)
;(list 'procedure parameters (scan-out-defines body) env))
(list 'procedure parameters body env))
(define (compound-procedure? p)
(tagged-list? p 'procedure))
(define (procedure-parameters p) (cadr p))
(define (procedure-body p) (caddr p))
(define (procedure-environment p) (cadddr p))
(define (scan-out-defines body)
(define (iter seq)
(cond
((null? seq) '(() ()))
(else (let ((exp (car seq))
(res (iter (cdr seq))))
(if (definition? exp)
(let ((var (definition-variable exp))
(val (definition-value exp)))
(list (cons (make-assignment var val)
(car res))
(cons (list var ''*unassigned*) (cadr res))))
(list (cons exp (car res)) (cadr res)))))))
(let ((res (iter body)))
(let ((newbody (car res))
(vvs (cadr res)))
(if (null? vvs)
(analyze-sequence newbody)
(analyze (make-let vvs newbody))))))
; operations on environments
; 为了实现这些操作,我们将环境表示为一个帧的list,一个环境的enclosing环境是这个list的cdr,空环境是空list。
(define (enclosing-environment env) (cdr env))
(define (first-frame env) (car env))
(define the-empty-environment '())
; 每个帧表达式为一个lists的pair,一个变量list和一个关联值的list。
(define (make-frame variables values)
(cons variables values))
(define (frame-variables frame) (car frame))
(define (frame-values frame) (cdr frame))
(define (set-frame-variables! frame vars)
(set-car! frame vars))
(define (set-frame-values! frame vals)
(set-cdr! frame vals))
(define (add-binding-to-frame! var val frame)
(set-car! frame (cons var (frame-variables frame)))
(set-cdr! frame (cons val (frame-values frame))))
(define (first-variable vars) (car vars))
(define (rest-variables vars) (cdr vars))
(define (first-value vals) (car vals))
(define (rest-values vals) (cdr vals))
(define (set-first-value! vals val) (set-car! vals val))
(define (length-variables vars) (length vars))
(define (length-values vals) (length vals))
; 扩展环境,加一帧
(define (extend-environment vars vals base-env)
(if (= (length-variables vars) (length-values vals))
(cons (make-frame vars vals) base-env)
(if (< (length-variables vars) (length-values vals))
(error "Too many arguments supplied"
vars
vals)
(error "Too few arguments supplied"
vars
vals))))
(define (env-loop env var next op)
(define (iter env)
(define (scan vars vals)
(cond ((null? vars)
(if next
(next env)
(iter (enclosing-environment env))))
((eq? var (first-variable vars))
(op vals))
(else (scan (rest-variables vars)
(rest-values vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env)))
(scan (frame-variables frame)
(frame-values frame)))))
(iter env))
(define (lookup-variable-value var env)
(env-loop env var false
(lambda (vals)
(let ((val (first-value vals)))
(if (eq? '*unassigned* val)
(error "use variable before it's defined" var)
val)))))
(define (lookup-variable-value-ex var env)
(env-loop env var false
(lambda (vals)
(first-value vals))))
(define (set-variable-value! var val env)
(env-loop env var false
(lambda (vals) (set-first-value! vals val))))
(define (define-variable! var val env)
(env-loop env var
(lambda (env)
(add-binding-to-frame! var val
(first-frame env)))
(lambda (vals)
(set-first-value! vals val))))
(define (unbound-variable! var env)
(define (unbound vars vals)
(cond ((null? vars) (make-frame '() '()))
((eq? var (first-variable vars))
(make-frame (rest-variables vars) (rest-values vals)))
(else
(let ((res (unbound (rest-variables vars) (rest-values vals))))
(add-binding-to-frame! (first-variable vars)
(first-value vals)
res)
res))))
(if (eq? env the-empty-environment)
(error "variable doesn't exist")
(let* ((frame (first-frame env))
(res (unbound (frame-variables frame)
(frame-values frame))))
(set-frame-variables! frame (frame-variables res))
(set-frame-values! frame (frame-values res)))))
; primitive and global env
(define (setup-environment)
(let ((initial-env
(extend-environment
(primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)))
(define-variable! 'true true initial-env)
(define-variable! 'false false initial-env)
initial-env))
(define the-global-environment
(setup-environment))
; standard procedures
(define standard-procedures
(list
'(define (require p)
(if (not p) (amb)))
'(define (list-amb l)
(if (null? l)
(amb)
(amb (car l) (list-amb (cdr l)))))
'(define (map proc l)
(if (null? l)
'()
(cons (proc (car l))
(map proc (cdr l)))))
'(define (ormap proc l)
(cond
((null? l) false)
(else (let ((res (proc (car l))))
(if res
res
(ormap proc (cdr l)))))))
'(define (filter proc l)
(cond
((null? l) '())
(else (if (proc (car l))
(cons (car l) (filter proc (cdr l)))
(filter proc (cdr l))))))
'(define (for-each proc l)
(if (not null?)
(begin (proc (car l)) (for-each proc (cdr l)))))
))
(map (lambda (exp)
(ambeval exp the-global-environment
(lambda (val fail) 'ok)
(lambda () (error "fail to define"))))
standard-procedures)
; read-eval-print loop
(define input-prompt ";;; Amb-Eval input:")
(define output-prompt ";;; Amb-Eval value:")
(define (driver-loop)
(define (internal-loop try-again)
(prompt-for-input input-prompt)
(let ((input (read)))
(if (eq? input 'try-again)
(try-again)
(begin
(newline)
(display
";;; Starting a new problem ")
(ambeval
input
the-global-environment
;; ambeval success
(lambda (val next-alternative)
(announce-output
output-prompt)
(user-print val)
(internal-loop
next-alternative))
;; ambeval failure
(lambda ()
(announce-output
";;; There are no
more values of")
(user-print input)
(driver-loop)))))))
(internal-loop
(lambda ()
(newline)
(display
";;; There is no current problem")
(driver-loop))))
(define (prompt-for-input string)
(newline) (newline)
(display string) (newline))
(define (announce-output string)
(newline) (display string) (newline))
(define (user-print object)
(if (compound-procedure? object)
(display
(list 'compound-procedure
(procedure-parameters object)
(procedure-body object)
'<procedure-env>))
(display object)))
;(driver-loop)
))
(provide ambeval-exp)