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 ->
19                         fun truth_value ->
20                                 if truth_value = false
21                                 then empty_set
22                                 else List.map (fun d -> new_peg_and_assign 'x' d) domain
23                 in bind_set u extend_one
24         </code></pre>
25
26         where `new_peg_and_assign` is the operation we defined in [hint 3](/hints/assignment_7_hint_3):
27
28                 let new_peg_and_assign (var_to_bind : char) (d : entity) =
29                         fun ((r, h) : assignment * store) ->
30                                 (* first we calculate an unused index *)
31                                 let new_index = List.length h
32                                 (* next we store d at h[new_index], which is at the very end of h *)
33                                 (* the following line achieves that in a simple but inefficient way *)
34                                 in let h' = List.append h [d]
35                                 (* next we assign 'x' to location new_index *)
36                                 in let r' = fun var ->
37                                         if var = var_to_bind then new_index else r var
38                                 (* the reason for returning true as an initial element should now be apparent *)
39                                 in (true, r', h')
40         
41         What's going on in this representation of `u` updated with \[[&exist;x]]? For each `bool dpm` in `u` that wraps a `true`, we collect `dpm`s that are the result of 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. For `bool dpm`s in `u` that wrap `false`, we just discard them. We could if we wanted instead return `unit_set (unit_dpm false)`.
42
43         A later step can then filter out all the `dpm`s according to which the 
44 entity `d` we did that with doesn't have property P.
45
46         So if we just call the function `extend_one` defined above \[[&exist;x]], then `u` updated with \[[&exist;x]] updated with \[[Px]] is just:
47
48         <pre><code>u >>= \[[&exist;x]] >>= \[[Px]]
49         </code></pre>
50
51         or, being explicit about which "bind" operation we're representing here with `>>=`, that is:
52
53         <pre><code>bind_set (bind_set u \[[&exist;x]]) \[[Px]]
54         </code></pre>
55
56 *       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](
57 /reader_monad_for_variable_binding).)
58  
59                 type assignment = char -> entity;;
60                 type 'a reader = assignment -> 'a;;
61
62                 let unit_reader (x : 'a) = fun r -> x;;
63
64                 let bind_reader (u : 'a reader) (f : 'a -> 'b reader) =
65                         fun r ->
66                                 let a = u r
67                                 in let u' = f a
68                                 in u' r;;
69
70                 let getx = fun r -> r 'x';;
71
72                 let lift (predicate : entity -> bool) =
73                         fun entity_reader ->
74                                 fun r ->
75                                         let obj = entity_reader r
76                                         in unit_reader (predicate obj)
77
78         `lift predicate` converts a function of type `entity -> bool` into one of type `entity reader -> bool reader`. The meaning of \[[Qx]] would then be:
79
80         <pre><code>\[[Q]] &equiv; lift q
81         \[[x]] & equiv; getx
82         \[[Qx]] &equiv; \[[Q]] \[[x]] &equiv;
83                 fun r ->
84                         let obj = getx r
85                         in unit_reader (q obj)
86         </code></pre>
87
88         Recall also how we defined \[[lambda x]], or as [we called it before](/reader_monad_for_variable_binding), \\[[who(x)]]:
89
90                 let shift (var_to_bind : char) entity_reader (v : 'a reader) =
91                 fun (r : assignment) ->
92                         let new_value = entity_reader r
93                         (* remember here we're implementing assignments as functions rather than as lists of pairs *)
94                         in let r' = fun var -> if var = var_to_bind then new_value else r var
95                         in v r'
96
97         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:
98
99                 fun (lifted_predicate : entity reader -> bool reader) : bool reader ->
100                         fun r -> exists (fun (obj : entity) -> lifted_predicate (unit_reader obj) r)
101                         
102         That would be the meaning of \[[&exist;]], which we'd use like this:
103
104         <pre><code>\[[&exist;]] \[[Q]]
105         </code></pre>
106
107         or this:
108
109         <pre><code>\[[&exist;]] ( \[[lambda x]] \[[Qx]] )
110         </code></pre>
111
112         If we wanted to compose \[[&exist;]] with \[[lambda x]], we'd get:
113
114                 let shift var_to_bind entity_reader v =
115                         fun r ->
116                                 let new_value = entity_reader r
117                                 in let r' = fun var -> if var = var_to_bind then new_value else r var
118                                 in v r'
119                 in let lifted_exists =
120                         fun lifted_predicate ->
121                                 fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
122                 in fun bool_reader -> lifted_exists (shift 'x' getx bool_reader)
123
124         which we can simplify to:
125
126                 let shifted v =
127                         fun r ->
128                                 let new_value = r 'x'
129                                 in let r' = fun var -> if var = 'x' then new_value else r var
130                                 in v r'
131                 in let lifted_exists =
132                         fun lifted_predicate ->
133                                 fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
134                 in fun bool_reader -> lifted_exists (shifted bool_reader)
135
136         and simplifying further:
137
138                 fun bool_reader ->
139                         let shifted v =
140                                 fun r ->
141                                         let new_value = r 'x'
142                                         in let r' = fun var -> if var = 'x' then new_value else r var
143                                         in v r'
144                         let lifted_predicate = shifted bool_reader
145                         in fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
146
147                 fun bool_reader ->
148                         let lifted_predicate = fun r ->
149                                         let new_value = r 'x'
150                                         in let r' = fun var -> if var = 'x' then new_value else r var
151                                         in bool_reader r'
152                         in fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
153
154
155
156
157                 
158         
159
160 *       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.