740eba2b637e3d8a27116507b6693d77050d7bab
[lambda.git] / hints / assignment_7_hint_6.mdwn
1
2 *       In def 3.1 on p. 14, GS&V define `s` updated with \[[not φ]] as:
3
4         >       { i ∈ s | i does not subsist in s[φ] }
5
6         where `i` *subsists* in <code>s[&phi;]</code> if there are any `i'` that *extend* `i` in <code>s[&phi;]</code>.
7
8         Here's how to do that in our framework:
9
10                 type clause_op = bool dpm -> bool dpm set;;
11                 
12                 (* filter out which bool dpms in a set are true when receiving (r, h) as input *)
13                 let extensions set (r, h) = List.filter (fun one_dpm -> let (truth_value, _, _) = one_dpm (r, h) in truth_value) set;;
14
15                 let negate_op (phi : clause_op) : clause_op =
16                         fun one_dpm -> unit_set (
17                 fun (r, h) ->
18                                         let truth_value' = extensions (phi one_dpm) (r, h) = []
19                     in (truth_value', r, h)
20             )
21
22
23 *       Representing \[[and &phi; &psi;]] is simple:
24
25                 let and_op (phi : clause_op) (psi : clause_op) : clause_op =
26                         fun one_dpm -> bind_set (phi one_dpm) psi;;
27
28 *       Here are `or` and `if`:
29
30                 let or_op (phi : clause_op) (psi : clause_op) =
31                         (* NOT: negate_op (and_op (negate_op phi) (negate_op psi)) *)
32             fun one_dpm -> unit_set (
33                 fun (r, h) ->
34                                         in let truth_value' = extensions (phi one_dpm) (r, h) <> [] || extensions (bind_set (negate_op phi one_dpm) psi) (r, h) <> []
35                     in (truth_value', r, h))
36
37                 let if_op (phi : clause_op) (psi : clause_op) : clause_op =
38                         (* NOT: negate_op (and_op phi (negate_op psi)) *)
39             fun one_dpm -> unit_set (
40               fun (r, h) ->
41                                         in let truth_value' = List.for_all (fun one_dpm ->
42                                                         let (truth_value, _, _) = one_dpm (r, h)
43                                                         in truth_value = false || extensions (psi one_dpm) (r, h) <> []
44                                                 ) (phi one_dpm)
45                     in (truth_value', r, h));;
46
47
48 *       Now let's test everything we've developed:
49
50                 type entity = Bob | Carol | Ted | Alice;;
51                 let domain = [Bob; Carol; Ted; Alice];;
52                 type assignment = char -> int;;
53                 type store = entity list;;
54                 type 'a dpm = assignment * store -> 'a * assignment * store;;
55                 let unit_dpm (x : 'a) : 'a dpm = fun (r, h) -> (x, r, h);;
56                 let bind_dpm (u: 'a dpm) (f : 'a -> 'b dpm) : 'b dpm =
57                         fun (r, h) ->
58                                 let (a, r', h') = u (r, h)
59                                 in let u' = f a
60                                 in u' (r', h')
61
62                 type 'a set = 'a list;;
63                 let empty_set : 'a set = [];;
64                 let unit_set (x : 'a) : 'a set = [x];;
65                 let bind_set (u : 'a set) (f : 'a -> 'b set) : 'b set =
66                         List.concat (List.map f u);;
67
68                 type clause_op = bool dpm -> bool dpm set;;
69
70                 let get (var : char) : entity dpm =
71                         fun (r, h) ->
72                                 let obj = List.nth h (r var)
73                                 in (obj, r, h);;
74
75                 let lift_predicate (f : entity -> bool) : entity dpm -> clause_op =
76                         fun entity_dpm ->
77                                 let eliminator = fun (truth_value : bool) ->
78                                         if truth_value = false
79                                         then unit_dpm false
80                                         else bind_dpm entity_dpm (fun e -> unit_dpm (f e))
81                                 in fun one_dpm -> unit_set (bind_dpm one_dpm eliminator);;
82
83                 let lift_predicate2 (f : entity -> entity -> bool) : entity dpm -> entity dpm -> clause_op =
84                         fun entity1_dpm entity2_dpm ->
85                                 let eliminator = fun (truth_value : bool) ->
86                                         if truth_value = false
87                                         then unit_dpm false
88                                         else bind_dpm entity1_dpm (fun e1 -> bind_dpm entity2_dpm (fun e2 -> unit_dpm (f e1 e2)))
89                                 in fun one_dpm -> unit_set (bind_dpm one_dpm eliminator);;
90
91                 let new_peg_and_assign (var_to_bind : char) (d : entity) : bool -> bool dpm =
92                   fun truth_value ->
93                           fun (r, h) ->
94                                 let new_index = List.length h
95                                 in let h' = List.append h [d]
96                                 in let r' = fun var ->
97                                   if var = var_to_bind then new_index else r var
98                                 in (truth_value, r', h')
99
100                 let exists var : clause_op = fun one_dpm ->
101                         List.map (fun d -> bind_dpm one_dpm (new_peg_and_assign var d)) domain
102
103                 (* negate_op, and_op, or_op, and if_op as above *)
104
105                 let (>>=) = bind_set;;
106                 let initial_set = [fun (r,h) -> (true,r,h)];;
107
108                 let initial_r = fun var -> failwith ("no value for " ^ (Char.escaped var));;
109                 let run dpm_set =
110                         let bool_set = List.map (fun one_dpm -> let (value, r, h) = one_dpm (initial_r, []) in value) dpm_set
111                         in List.exists (fun truth_value -> truth_value) bool_set;;
112
113                 let male obj = obj = Bob || obj = Ted;;
114                 let wife_of x y = (x,y) = (Bob, Carol) || (x,y) = (Ted, Alice);;
115                 let kisses x y = (x,y) = (Bob, Carol) || (x,y) = (Ted, Alice);;
116                 let misses x y = (x,y) = (Bob, Carol) || (x,y) = (Ted, Carol);;
117                 let getx = get 'x';;
118                 let gety = get 'y';;
119
120                 (* "a man x has a wife y" *)
121                 let antecedent = fun one_dpm -> exists 'x' one_dpm >>= lift_predicate male getx >>= exists 'y' >>= lift_predicate2 wife_of getx gety;;
122                 
123                 (* "if a man x has a wife y, x kisses y" *)
124                 run (initial_set >>= if_op antecedent (lift_predicate2 kisses getx gety));;
125                 (* Bob has wife Carol, and kisses her; and Ted has wife Alice and kisses her; so this is true! *)
126
127                 (* "if a man x has a wife y, x misses y" *)
128                 run (initial_set >>= if_op antecedent (lift_predicate2 misses getx gety));;
129                 (* Bob has wife Carol, and misses her; but Ted misses only Carol, not his wife Alice; so this is false! *)
130