assignment7 tweaks
[lambda.git] / hints / assignment_7_hint_5.mdwn
1
2 *       How shall we handle \[[∃x]]? As we said, GS&V really tell us how to interpret \[[∃xPx]], but what they say about this breaks naturally into two pieces, such that we can represent the update of our starting set `u` with \[[∃xPx]] as:
3
4         <pre><code>u >>=<sub>set</sub> \[[&exist;x]] >>=<sub>set</sub> \[[Px]]
5         </code></pre>
6
7         What does \[[&exist;x]] need to be here? Here's what they say, on the top of p. 13:
8
9         >       Suppose an information state `s` is updated with the sentence &exist;xPx. Possibilities in `s` in which no entity has the property P will be eliminated.
10
11         We can defer that to a later step, where we do `... >>= \[[Px]]`.
12  
13         >       The referent system of the remaining possibilities will be extended with a new peg, which is associated with `x`. And for each old possibility `i` in `s`, there will be just as many extensions `i[x/d]` in the new state `s'` and there are entities `d` which in the possible world of `i` have the property P.
14
15         Deferring the "property P" part, this corresponds to:
16
17         <pre><code>u updated with \[[&exist;x]] &equiv;
18                 let extend_one = fun (one_dpm : bool dpm) ->
19                         List.map (fun d -> bind_dpm one_dpm (new_peg_and_assign 'x' d)) domain
20                 in bind_set u extend_one
21         </code></pre>
22
23         where `new_peg_and_assign` is the operation we defined in [hint 3](/hints/assignment_7_hint_3):
24
25                 let new_peg_and_assign (var_to_bind : char) (d : entity) =
26                         (* we want to return not a function that we can bind to a bool dpm *)
27                         fun (truth_value : bool) : bool dpm ->
28                                 fun ((r, h) : assignment * store) ->
29                                         (* first we calculate an unused index *)
30                                         let new_index = List.length h
31                                         (* next we store d at h[new_index], which is at the very end of h *)
32                                         (* the following line achieves that in a simple but inefficient way *)
33                                         in let h' = List.append h [d]
34                                         (* next we assign 'x' to location new_index *)
35                                         in let r' = fun var ->
36                                                 if var = var_to_bind then new_index else r var
37                                         (* we pass through the same truth_value that we started with *)
38                                         in (truth_value, r', h')
39         
40         What's going on in this representation of `u` updated with \[[&exist;x]]? For each `bool dpm` in `u`, we collect `dpm`s that are the result of passing through their `bool`, but extending their input `(r, h)` by allocating a new peg for entity `d`, for each `d` in our whole domain of entities, and binding the variable `x` to the index of that peg.
41
42         A later step can then filter out all the `dpm`s according to which the entity `d` we did that with doesn't have property P.
43
44         So if we just call the function `extend_one` defined above \[[&exist;x]], then `u` updated with \[[&exist;x]] updated with \[[Px]] is just:
45
46         <pre><code>u >>= \[[&exist;x]] >>= \[[Px]]
47         </code></pre>
48
49         or, being explicit about which "bind" operation we're representing here with `>>=`, that is:
50
51         <pre><code>bind_set (bind_set u \[[&exist;x]]) \[[Px]]
52         </code></pre>
53
54 *       Let's compare this to what \[[&exist;xPx]] would look like on a non-dynamic semantics, for example, where we use a simple reader monad to implement variable binding. Reminding ourselves, we'd be working in a framework like this. (Here we implement environments or assignments as functions from variables to entities, instead of as lists of pairs of variables and entities. An assignment `r` here is what `fun c -> List.assoc c r` would have been in [week6](
55 /reader_monad_for_variable_binding).)
56  
57                 type assignment = char -> entity;;
58                 type 'a reader = assignment -> 'a;;
59
60                 let unit_reader (x : 'a) = fun r -> x;;
61
62                 let bind_reader (u : 'a reader) (f : 'a -> 'b reader) =
63                         fun r ->
64                                 let a = u r
65                                 in let u' = f a
66                                 in u' r;;
67
68                 let getx = fun r -> r 'x';;
69
70                 let lift (predicate : entity -> bool) =
71                         fun entity_reader ->
72                                 fun r ->
73                                         let obj = entity_reader r
74                                         in unit_reader (predicate obj)
75
76         `lift predicate` converts a function of type `entity -> bool` into one of type `entity reader -> bool reader`. The meaning of \[[Qx]] would then be:
77
78         <pre><code>\[[Q]] &equiv; lift q
79         \[[x]] & equiv; getx
80         \[[Qx]] &equiv; \[[Q]] \[[x]] &equiv;
81                 fun r ->
82                         let obj = getx r
83                         in unit_reader (q obj)
84         </code></pre>
85
86         Recall also how we defined \[[lambda x]], or as [we called it before](/reader_monad_for_variable_binding), \\[[who(x)]]:
87
88                 let shift (var_to_bind : char) entity_reader (v : 'a reader) =
89                 fun (r : assignment) ->
90                         let new_value = entity_reader r
91                         (* remember here we're implementing assignments as functions rather than as lists of pairs *)
92                         in let r' = fun var -> if var = var_to_bind then new_value else r var
93                         in v r'
94
95         Now, how would we implement quantifiers in this setting? I'll assume we have a function `exists` of type `(entity -> bool) -> bool`. That is, it accepts a predicate as argument and returns `true` if any element in the domain satisfies that predicate. We could implement the reader-monad version of that like this:
96
97                 fun (lifted_predicate : entity reader -> bool reader) : bool reader ->
98                         fun r -> exists (fun (obj : entity) -> lifted_predicate (unit_reader obj) r)
99                         
100         That would be the meaning of \[[&exist;]], which we'd use like this:
101
102         <pre><code>\[[&exist;]] \[[Q]]
103         </code></pre>
104
105         or this:
106
107         <pre><code>\[[&exist;]] ( \[[lambda x]] \[[Qx]] )
108         </code></pre>
109
110         If we wanted to compose \[[&exist;]] with \[[lambda x]], we'd get:
111
112                 let shift var_to_bind entity_reader v =
113                         fun r ->
114                                 let new_value = entity_reader r
115                                 in let r' = fun var -> if var = var_to_bind then new_value else r var
116                                 in v r'
117                 in let lifted_exists =
118                         fun lifted_predicate ->
119                                 fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
120                 in fun bool_reader -> lifted_exists (shift 'x' getx bool_reader)
121
122         which we can simplify to:
123
124                 let shifted v =
125                         fun r ->
126                                 let new_value = r 'x'
127                                 in let r' = fun var -> if var = 'x' then new_value else r var
128                                 in v r'
129                 in let lifted_exists =
130                         fun lifted_predicate ->
131                                 fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
132                 in fun bool_reader -> lifted_exists (shifted bool_reader)
133
134         and simplifying further:
135
136                 fun bool_reader ->
137                         let shifted v =
138                                 fun r ->
139                                         let new_value = r 'x'
140                                         in let r' = fun var -> if var = 'x' then new_value else r var
141                                         in v r'
142                         let lifted_predicate = shifted bool_reader
143                         in fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
144
145                 fun bool_reader ->
146                         let lifted_predicate = fun r ->
147                                         let new_value = r 'x'
148                                         in let r' = fun var -> if var = 'x' then new_value else r var
149                                         in bool_reader r'
150                         in fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
151
152
153 *       Can you figure out how to handle \[[not &phi;]] on your own? If not, here are some [more hints](/hints/assignment_7_hint_6). But try to get as far as you can on your own.