-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.scm
83 lines (62 loc) · 1.86 KB
/
examples.scm
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
; Let's allow Scheme expressions to have multiple values.
; Infintely many values, even.
; We can provide special forms to average multi-valued expressions or
; display the distribution of values.
(amb-range 0 1)
(amb)
(amb)
(amb)
; Suppose we have a primitive called amb-range
(define amb-unit-sphere
(let ((x (amb-range -1 1))
(y (amb-range -1 1))
(z (amb-range -1 1)))
(if (< (+ (square x) (square y) (square z)) 1) (amb))
(list x y z)))
; How many random iterations to average
(amb-set-precision 10)
; Monte Carlo integration of a function f over the unit sphere
(f (amb-unit-sphere))
*ambiguous-distribution*
; The distribution object contains a continuation: we can evaluate it
; on-demand using several different strategies.
(amb-average-with-precision (f (amb-unit-sphere)) 10)
; (amb-cont (regular-amb . options)) === (amb . options)
(define (regular-amb . options)
(lambda (succeed fail)
(if (null? options)
(fail)
(let ((result (car options)))
(set! options (cdr options))
(succeed result)))))
#|
;;; Amb-Eval input:
(amb-cont (regular-amb 1 2 3))
;;; Starting a new problem
;;; Amb-Eval value:
1
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
2
;;; Amb-Eval input:
try-again
;;; Amb-Eval value:
3
;;; Amb-Eval input:
try-again
;;; There are no more values of (amb-cont (regular-amb 1 2 3))
;;; Amb-Eval input:
try-again
;;; There is no current problem
|#
(define (amb-range low high)
(lambda (succeed fail)
(succeed (rand low high))))
(define ((ambify func) . args) (amb (func args)))
; (ambify regular-amb) === amb
Multiple evaluation strategies:
stop at the first non-failing branch (current amb's strategy)
aggregate over all non-failing branches (only good for discrete)
aggregate over predetermined number of non-failing branches (monte carlo integration, etc.)
automatically select an evaluation strategy?