X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=hints%2Fassignment_7_hint_4.mdwn;h=e4f0a7907c95abdd0cb87daa6fe5ae3b1511a269;hp=017a0d3be8166824cfe7de503a1b8ecf53c4bcb6;hb=1d5249432378066bbe948bfe0b303704e18468a5;hpb=824a638b7070e9ae64a5124c4467a03fd0f18b00 diff --git a/hints/assignment_7_hint_4.mdwn b/hints/assignment_7_hint_4.mdwn index 017a0d3b..e4f0a790 100644 --- a/hints/assignment_7_hint_4.mdwn +++ b/hints/assignment_7_hint_4.mdwn @@ -18,7 +18,7 @@ in (truth_value', r, h)) in bind_set u (fun one_dpm -> unit_set (bind_dpm one_dpm eliminate_non_Qxs)) - The first seven lines here just perfom the operation we described: return a `bool dpm` computation that only yields `true` whether its input `(r, h)` associates variable `x` with the right sort of entity. The last line performs the `bind_set` operation. This works by taking each `dpm` in the set and returning a `unit_set` of a filtered `dpm`. The definition of `bind_set` takes care of collecting together all of the `unit_set`s that result for each different set element we started with. + The first seven lines here just perfom the operation we described: return a `bool dpm` computation that only yields `true` when its input `(r, h)` associates variable `x` with the right sort of entity. The last line performs the `bind_set` operation. This works by taking each `dpm` in the set and returning a `unit_set` of a filtered `dpm`. The definition of `bind_set` takes care of collecting together all of the `unit_set`s that result for each different set element we started with. We can call the `(fun one_dpm -> ...)` part \[[Qx]] and then updating `u` with \[[Qx]] will be: @@ -46,15 +46,18 @@ Finally, we realize that we're going to have a set of `bool dpm`s to start with, and we need to compose \[[Qx]] with them. We don't want any of the monadic values in the set that wrap `false` to become `true`; instead, we want to apply a filter that checks whether values that formerly wrapped `true` should still continue to do so. - This is most easily done like this: + This could be handled like this: fun entity_dpm -> - fun truth_value -> + let eliminate_non_Qxs = fun truth_value -> if truth_value = false then empty_set - else unit_set (bind dpm entity_dpm (fun e -> unit_dpm (Q e))) + else unit_set (bind_dpm entity_dpm (fun e -> unit_dpm (Q e))) + in fun one_dpm -> (bind_dpm one_dpm eliminate_non_Qxs) - Doing things this way will discard `bool dpm`s that start out wrapping `false`, and will pass through other `bool dpm`s that start out wrapping `true` but which our current filter transforms to a wrapped `false`. You might instead aim for consistency, and always pass through wrapped `false`s, whether they started out that way or are only now being generated; or instead always discard such, and only pass through wrapped `true`s. But what we have here will work fine too. + Applied to an `entity_dpm`, that yields a function that we can bind to a `bool dpm set` and that will transform the doubly-wrapped `bool` into a new `bool dpm set`. + + Doing things this way will discard `bool dpm`s from the set that started out wrapping `false`, and will pass through other `bool dpm`s that start out wrapping `true` but which our current filter transforms to a wrapped `false`. You might instead aim for consistency, and always pass through wrapped `false`s, whether they started out that way or are only now being generated; or instead always discard such, and only pass through wrapped `true`s. But what we have here will work fine too. If we let that be \[[Q]], then \[[Q]] \[[x]] would be: @@ -62,27 +65,29 @@ let obj = List.nth h (r 'x') in (obj, r, h) in let entity_dpm = getx - in fun truth_value -> + in let eliminate_non_Qxs = fun truth_value -> if truth_value = false then empty_set else unit_set (bind_dpm entity_dpm (fun e -> unit_dpm (Q e))) + in fun one_dpm -> (bind_dpm one_dpm eliminate_non_Qxs) or, simplifying: let getx = fun (r, h) -> let obj = List.nth h (r 'x') in (obj, r, h) - in fun truth_value -> + in let eliminate_non_Qxs = fun truth_value -> if truth_value then unit_set (bind_dpm getx (fun e -> unit_dpm (Q e))) else empty_set + in fun one_dpm -> (bind_dpm one_dpm eliminate_non_Qxs) - which is: + unpacking the definition of `bind_dpm`, that is: let getx = fun (r, h) -> let obj = List.nth h (r 'x') in (obj, r, h) - in fun truth_value -> + in let eliminate_non_Qxs = fun truth_value -> if truth_value then unit_set ( fun (r, h) -> @@ -90,30 +95,35 @@ in let u' = (fun e -> unit_dpm (Q e)) a in u' (r', h') ) else empty_set - + in fun one_dpm -> (bind_dpm one_dpm eliminate_non_Qxs) + which is: - in fun truth_value -> + fun truth_value -> if truth_value then unit_set ( - fun (r, h) -> + let eliminate_non_Qxs = fun (r, h) -> let obj = List.nth h (r 'x') let (a, r', h') = (obj, r, h) in let u' = (fun e -> unit_dpm (Q e)) a in u' (r', h') ) else empty_set - + in fun one_dpm -> (bind_dpm one_dpm eliminate_non_Qxs) + which is: - in fun truth_value -> + let eliminate_non_Qxs = fun truth_value -> if truth_value then unit_set ( fun (r, h) -> let obj = List.nth h (r 'x') in let u' = unit_dpm (Q obj) - in u' (r', h') + in u' (r, h) ) else empty_set - + in fun one_dpm -> (bind_dpm one_dpm eliminate_non_Qxs) + + This is a function that takes a `bool dpm` as input and returns a `bool dpm set` as output. + This is a bit different than the \[[Qx]] we had before: let eliminate_non_Qxs = (fun truth_value -> @@ -123,7 +133,7 @@ then let obj = List.nth h (r 'x') in Q obj else false in (truth_value', r, h)) - in (fun one_dpm -> unit_set (bind_dpm one_dpm eliminate_non_Qxs)) + in fun one_dpm -> unit_set (bind_dpm one_dpm eliminate_non_Qxs) because that one passed through every `bool dpm` that wrapped a `false`; whereas now we're discarding some of them. But these will work equally well. We can implement either behavior (or, as we said before, the behavior of never passing through a wrapped `false`).