-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathworld-model.scm
284 lines (226 loc) · 9.44 KB
/
world-model.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
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
;; -- Kinship in Church --
;; Author: Gabe Grand ([email protected])
;; With help from Alex Lew and Lio Wong.
;; -- GENERAL UTILITIES --
;; Membership test that returns true instead of literal list
(define (member? a b)
(if (member a b) true false))
;; Shuffle a list. Relies on items in the list being unique.
(define (shuffle-unique lst)
(if (null? lst)
()
(let* ((n (random-integer (length lst)))
(x (list-ref lst n)))
(cons x (shuffle-unique (difference lst (list x)))))))
;; Convenience method for accessing properties in association lists
(define (lookup obj key)
(if (assoc key obj) (rest (assoc key obj)) ()))
;; Geometric distribution
(define (bounded-geometric p n max-n)
(if (>= n max-n)
n
(if (flip p)
n
(bounded-geometric p (+ 1 n) max-n))))
;; Shallow flatten
(define (shallow-flatten x)
(cond ((null? x) '())
((pair? x) (append (car x) (shallow-flatten (cdr x))))
(else (list x))))
;; -- NAMING --
;; All the names that can be used in the conversational context.
(define ALL-NAMES '(avery blake charlie dana))
;; Replace unknown names with "other" (for histograms)
(define (mask-other names)
(map (lambda (name)
(cond
((null? name) name)
((member? name ALL-NAMES) name)
(else "other")))
names))
;; -- WORLD MODEL --
(define (run-world-model)
(rejection-query
;; Generates unique person ids of the format (person-0, person-1, ...)
(define PERSON-PREFIX "person-")
(define new-person-id (make-gensym PERSON-PREFIX))
(define (id->idx person-id)
(string->number (string-slice (stringify person-id) (string-length PERSON-PREFIX))))
;; Randomly assign a gender
(define person->gender (mem (lambda (person-id)
(uniform-draw '(male female)))))
;; Randomly-ordered list of person names
(define NAMES (shuffle-unique ALL-NAMES))
(define person->name (mem (lambda (person-id)
(list-ref NAMES (id->idx person-id)))))
;; Person node in tree
(define (person person-id parent-1-id parent-2-id) (list
(pair 'person-id person-id)
(pair 'name person-id)
(pair 'gender (person->gender person-id))
(pair 'parent-1-id parent-1-id)
(pair 'parent-2-id parent-2-id)))
;; Generate the full tree
;; Max tree size is 1 + (sum_{n=0}^{n=MAX-DEPTH} 2 * MAX-WIDTH^n)
(define MAX-WIDTH 3)
(define MAX-DEPTH 2)
(define PARTNER-PROBABILITY 0.5)
(define (generate-tree root-primary-id root-secondary-id depth)
(let* (
;; Create the primary parent
(parent-1-id (new-person-id))
(parent-1 (person parent-1-id root-primary-id root-secondary-id)))
(if (flip PARTNER-PROBABILITY)
;; Case: parent-1 has partner
(let* (
;; Create the secondary parent
(parent-2-id (new-person-id))
(parent-2 (person parent-2-id () ()))
;; Link the parents with a partner relation
(parent-1 (append parent-1 (list (pair 'partner-id parent-2-id))))
(parent-2 (append parent-2 (list (pair 'partner-id parent-1-id))))
;; Generate children
(n-children (if (>= depth MAX-DEPTH) 0 (bounded-geometric 0.5 0 MAX-WIDTH)))
(child-trees (repeat n-children (lambda () (generate-tree parent-1-id parent-2-id (+ depth 1)))))
;; Update the parents to point to the children
(child-ids (map (lambda (t) (lookup (first t) 'person-id)) child-trees))
(parent-1 (append parent-1 (list (pair 'child-ids child-ids))))
(parent-2 (append parent-2 (list (pair 'child-ids child-ids)))))
(append (list parent-1) (list parent-2) (shallow-flatten child-trees)))
;; Case: parent-1 has no partner
(list parent-1))))
;; Generate the global tree.
(define T (generate-tree () () 0))
;; Assign names randomly to (some of) the people in the tree.
(define (add-names-to-tree tree names)
(if (null? tree) ()
(let*
;; Probability of addding a name to the first person
((p (min 1.0 (/ (length names) (length tree))))
(person (first tree)))
(if (flip p)
;; Name the person
(let
((named-person (update-list person 1 (pair 'name (first names)))))
(cons named-person (add-names-to-tree (rest tree) (rest names))))
;; Don't name the person
(cons person (add-names-to-tree (rest tree) names))))))
;; Update the tree with the name information.
(define T (add-names-to-tree T NAMES))
;; -- CORE TREE UTILITIES --
;; Returns all instances of person with property `key` equal to `value`
(define filter-by-property
(mem (lambda (key value)
(filter (lambda (p) (equal? (lookup p key) value)) T))))
;; Returns the unique instance of person with name.
(define get-person-by-name
(mem (lambda (name)
(let
((results (filter-by-property 'name name)))
(if (null? results) () (first results))))))
;; People without a name can be referenced directly by person-id.
(define get-person-by-id
(mem (lambda (person-id)
(if (null? person-id)
()
(let ((idx (id->idx person-id)))
(if (>= idx (length T)) () (list-ref T idx)))))))
;; Get a person object either by name or person-id.
(define get-person
(mem (lambda (person-ref)
(cond
((null? person-ref) ())
((member? person-ref NAMES) (get-person-by-name person-ref))
(else (get-person-by-id person-ref))))))
;; Get a property of a person.
(define get-property
(mem (lambda (name key)
(lookup (get-person name) key))))
;; List of all the people in the tree with names.
(define named-people (filter (lambda (person) (not (null? person))) (map get-person NAMES)))
;; -- CONCEPTUAL SYSTEM --
;; Gets the partner of a person.
(define (partner-of name)
(get-property (get-property name 'partner-id) 'name))
;; Gets the parents of a person.
(define (parents-of name)
(let* ((parent-1-id (get-property name 'parent-1-id))
(parent-1-name (get-property parent-1-id 'name))
(parent-2-id (get-property name 'parent-2-id))
(parent-2-name (get-property parent-2-id 'name)))
(list parent-1-name parent-2-name)))
;; Gets the grandparents of a person.
(define (grandparents-of name)
(let ((parent-1 (first (parents-of name))))
(parents-of parent-1)))
;; Gets the children of a person.
(define (children-of name)
(let ((child-ids (get-property name 'child-ids)))
(map (lambda (child-id) (get-property child-id 'name)) child-ids)))
;; Gets the siblings of a person.
(define (siblings-of name)
(let* ((parent-1-id (get-property name 'parent-1-id))
(child-ids (get-property parent-1-id 'child-ids))
(child-names (map (lambda (child-id) (get-property child-id 'name)) child-ids)))
(filter (lambda (child-name) (not (equal? child-name name))) child-names)))
;; -- QUANTIFIERS --
;; predicate :: name -> boolean
(define (map-tree predicate)
(map (lambda (x) (predicate (lookup x 'name))) T))
(define (filter-tree predicate)
(filter (lambda (x) (predicate (lookup x 'name))) T))
(define (exists predicate)
(some (map-tree predicate)))
;; -- BOOLEAN RELATIONS --
(define (partner-of? name_a name_b)
(equal? name_a (partner-of name_b)))
(define (parent-of? name_a name_b)
(member? name_a (parents-of name_b)))
(define (father-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'male)
(parent-of? name_a name_b)))
(define (mother-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'female)
(parent-of? name_a name_b)))
(define (grandparent-of? name_a name_b)
(member? name_a (grandparents-of name_b)))
(define (grandfather-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'male)
(grandparent-of? name_a name_b)))
(define (grandmother-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'female)
(grandparent-of? name_a name_b)))
(define (child-of? name_a name_b)
(member? name_a (children-of name_b)))
(define (son-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'male)
(child-of? name_a name_b)))
(define (daughter-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'female)
(child-of? name_a name_b)))
(define (sibling-of? name_a name_b)
(member? name_a (siblings-of name_b)))
(define (brother-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'male)
(sibling-of? name_a name_b)))
(define (sister-of? name_a name_b)
(and (equal? (get-property name_a 'gender) 'female)
(sibling-of? name_a name_b)))
;; -- CONDITIONING STATEMENTS --
(condition
(and
;; Condition: Avery has a sister named Blake.
(sister-of? 'blake 'avery)
;; Condition: Charlie is Dana's grandfather.
(grandfather-of? 'charlie 'dana)
))
;; -- QUERY STATEMENT --
;; Query: Who are Dana's parents?
(parents-of 'dana)
;; Uncomment to return tree object (useful for debugging)
;; T
))
;; -- VISUALIZE QUERY --
(hist (mask-other (flatten (repeat 10 run-world-model))))
;; Compute a single output (useful for debugging)
;; (run-world-model)