assignment7 tweaks
[lambda.git] / hints / assignment_7_hint_5.mdwn
index 07cbd64..1b2ee6a 100644 (file)
        Deferring the "property P" part, this corresponds to:
 
        <pre><code>u updated with \[[&exist;x]] &equiv;
-               let extend_one = fun one_dpm ->
-                       fun truth_value ->
-                               if truth_value = false
-                               then empty_set
-                               else List.map (fun d -> new_peg_and_assign 'x' d) domain
+               let extend_one = fun (one_dpm : bool dpm) ->
+                       List.map (fun d -> bind_dpm one_dpm (new_peg_and_assign 'x' d)) domain
                in bind_set u extend_one
        </code></pre>
 
        where `new_peg_and_assign` is the operation we defined in [hint 3](/hints/assignment_7_hint_3):
 
                let new_peg_and_assign (var_to_bind : char) (d : entity) =
-                       fun ((r, h) : assignment * store) ->
-                               (* first we calculate an unused index *)
-                               let newindex = List.length h
-                               (* next we store d at h[newindex], which is at the very end of h *)
-                               (* the following line achieves that in a simple but inefficient way *)
-                               in let h' = List.append h [d]
-                               (* next we assign 'x' to location newindex *)
-                               in let r' = fun v ->
-                                       if v = var_to_bind then newindex else r v
-                               (* the reason for returning true as an initial element should now be apparent *)
-                               in (true, r',h')
+                       (* we want to return not a function that we can bind to a bool dpm *)
+                       fun (truth_value : bool) : bool dpm ->
+                               fun ((r, h) : assignment * store) ->
+                                       (* first we calculate an unused index *)
+                                       let new_index = List.length h
+                                       (* next we store d at h[new_index], which is at the very end of h *)
+                                       (* the following line achieves that in a simple but inefficient way *)
+                                       in let h' = List.append h [d]
+                                       (* next we assign 'x' to location new_index *)
+                                       in let r' = fun var ->
+                                               if var = var_to_bind then new_index else r var
+                                       (* we pass through the same truth_value that we started with *)
+                                       in (truth_value, r', h')
        
-       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)`.
+       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.
 
-       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.
+       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.
 
        So if we just call the function `extend_one` defined above \[[&exist;x]], then `u` updated with \[[&exist;x]] updated with \[[Px]] is just:
 
@@ -53,4 +51,103 @@ entity `d` we did that with doesn't have property P.
        <pre><code>bind_set (bind_set u \[[&exist;x]]) \[[Px]]
        </code></pre>
 
-*      Can you figure out how to handle \[[not &phi;]] on your own? If not, here are some [more hints](/hints/assignment_7_hint_6).
+*      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](
+/reader_monad_for_variable_binding).)
+               type assignment = char -> entity;;
+               type 'a reader = assignment -> 'a;;
+
+               let unit_reader (x : 'a) = fun r -> x;;
+
+               let bind_reader (u : 'a reader) (f : 'a -> 'b reader) =
+                       fun r ->
+                               let a = u r
+                               in let u' = f a
+                               in u' r;;
+
+               let getx = fun r -> r 'x';;
+
+               let lift (predicate : entity -> bool) =
+                       fun entity_reader ->
+                               fun r ->
+                                       let obj = entity_reader r
+                                       in unit_reader (predicate obj)
+
+       `lift predicate` converts a function of type `entity -> bool` into one of type `entity reader -> bool reader`. The meaning of \[[Qx]] would then be:
+
+       <pre><code>\[[Q]] &equiv; lift q
+       \[[x]] & equiv; getx
+       \[[Qx]] &equiv; \[[Q]] \[[x]] &equiv;
+               fun r ->
+                       let obj = getx r
+                       in unit_reader (q obj)
+       </code></pre>
+
+       Recall also how we defined \[[lambda x]], or as [we called it before](/reader_monad_for_variable_binding), \\[[who(x)]]:
+
+               let shift (var_to_bind : char) entity_reader (v : 'a reader) =
+               fun (r : assignment) ->
+                       let new_value = entity_reader r
+                       (* remember here we're implementing assignments as functions rather than as lists of pairs *)
+                       in let r' = fun var -> if var = var_to_bind then new_value else r var
+                       in v r'
+
+       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:
+
+               fun (lifted_predicate : entity reader -> bool reader) : bool reader ->
+                       fun r -> exists (fun (obj : entity) -> lifted_predicate (unit_reader obj) r)
+                       
+       That would be the meaning of \[[&exist;]], which we'd use like this:
+
+       <pre><code>\[[&exist;]] \[[Q]]
+       </code></pre>
+
+       or this:
+
+       <pre><code>\[[&exist;]] ( \[[lambda x]] \[[Qx]] )
+       </code></pre>
+
+       If we wanted to compose \[[&exist;]] with \[[lambda x]], we'd get:
+
+               let shift var_to_bind entity_reader v =
+                       fun r ->
+                               let new_value = entity_reader r
+                               in let r' = fun var -> if var = var_to_bind then new_value else r var
+                               in v r'
+               in let lifted_exists =
+                       fun lifted_predicate ->
+                               fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
+               in fun bool_reader -> lifted_exists (shift 'x' getx bool_reader)
+
+       which we can simplify to:
+
+               let shifted v =
+                       fun r ->
+                               let new_value = r 'x'
+                               in let r' = fun var -> if var = 'x' then new_value else r var
+                               in v r'
+               in let lifted_exists =
+                       fun lifted_predicate ->
+                               fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
+               in fun bool_reader -> lifted_exists (shifted bool_reader)
+
+       and simplifying further:
+
+               fun bool_reader ->
+                       let shifted v =
+                               fun r ->
+                                       let new_value = r 'x'
+                                       in let r' = fun var -> if var = 'x' then new_value else r var
+                                       in v r'
+                       let lifted_predicate = shifted bool_reader
+                       in fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
+
+               fun bool_reader ->
+                       let lifted_predicate = fun r ->
+                                       let new_value = r 'x'
+                                       in let r' = fun var -> if var = 'x' then new_value else r var
+                                       in bool_reader r'
+                       in fun r -> exists (fun obj -> lifted_predicate (unit_reader obj) r)
+
+
+*      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.