2 * In def 3.1 on p. 14, GS&V define `s` updated with \[[not φ]] as:
4 > { i ∈ s | i does not subsist in s[φ] }
6 where `i` *subsists* in <code>s[φ]</code> if there are any `i'` that *extend* `i` in <code>s[φ]</code>.
8 Here's how to do that in our framework:
10 type clause_op = bool dpm -> bool dpm set;;
12 let negate_op (phi : clause_op) : clause_op =
13 fun one_dpm -> unit_set (
15 let extensions set = List.filter (fun one_dpm -> let (truth_value, _, _) = one_dpm (r, h) in truth_value) set
16 in let truth_value' = extensions (phi one_dpm) = []
17 in (truth_value', r, h)
21 * Representing \[[and φ ψ]] is simple:
23 let and_op (phi : clause_op) (psi : clause_op) : clause_op =
24 fun one_dpm -> bind_set (phi one_dpm) psi;;
26 * Here are `or` and `if`:
28 let or_op (phi : clause_op) (psi : clause_op) =
29 (* NOT: negate_op (and_op (negate_op phi) (negate_op psi)) *)
30 fun one_dpm -> unit_set (
32 let extensions set = List.filter (fun one_dpm -> let (truth_value, _, _) = one_dpm (r, h) in truth_value) set
33 in let truth_value' = extensions (phi one_dpm) <> [] || extensions (bind_set (negate_op phi one_dpm) psi) <> []
34 in (truth_value', r, h))
36 let if_op (phi : clause_op) (psi : clause_op) : clause_op =
37 (* NOT: negate_op (and_op phi (negate_op psi)) *)
38 fun one_dpm -> unit_set (
40 let extensions set = List.filter (fun one_dpm -> let (truth_value, _, _) = one_dpm (r, h) in truth_value) set
41 in let truth_value' = List.for_all (fun one_dpm -> let (truth_value, _, _) = one_dpm (r, h) in truth_value = false || extensions (psi one_dpm) <> []) (phi one_dpm)
42 in (truth_value', r, h));;
45 * Now let's test everything we've developed:
47 type entity = Bob | Carol | Ted | Alice;;
48 let domain = [Bob; Carol; Ted; Alice];;
49 type assignment = char -> int;;
50 type store = entity list;;
51 type 'a dpm = assignment * store -> 'a * assignment * store;;
52 let unit_dpm (x : 'a) : 'a dpm = fun (r, h) -> (x, r, h);;
53 let bind_dpm (u: 'a dpm) (f : 'a -> 'b dpm) : 'b dpm =
55 let (a, r', h') = u (r, h)
59 type 'a set = 'a list;;
60 let empty_set : 'a set = [];;
61 let unit_set (x : 'a) : 'a set = [x];;
62 let bind_set (u : 'a set) (f : 'a -> 'b set) : 'b set =
63 List.concat (List.map f u);;
65 type clause_op = bool dpm -> bool dpm set;;
67 let get (var : char) : entity dpm =
69 let obj = List.nth h (r var)
72 let lift_predicate (f : entity -> bool) : entity dpm -> clause_op =
74 let eliminator = fun (truth_value : bool) ->
75 if truth_value = false
77 else bind_dpm entity_dpm (fun e -> unit_dpm (f e))
78 in fun one_dpm -> unit_set (bind_dpm one_dpm eliminator);;
80 let lift_predicate2 (f : entity -> entity -> bool) : entity dpm -> entity dpm -> clause_op =
81 fun entity1_dpm entity2_dpm ->
82 let eliminator = fun (truth_value : bool) ->
83 if truth_value = false
85 else bind_dpm entity1_dpm (fun e1 -> bind_dpm entity2_dpm (fun e2 -> unit_dpm (f e1 e2)))
86 in fun one_dpm -> unit_set (bind_dpm one_dpm eliminator);;
88 let new_peg_and_assign (var_to_bind : char) (d : entity) : bool -> bool dpm =
91 let new_index = List.length h
92 in let h' = List.append h [d]
93 in let r' = fun var ->
94 if var = var_to_bind then new_index else r var
95 in (truth_value, r', h')
97 let exists var : clause_op = fun one_dpm ->
98 List.map (fun d -> bind_dpm one_dpm (new_peg_and_assign var d)) domain
100 (* negate_op, and_op, or_op, and if_op as above *)
102 let (>>=) = bind_set;;
103 let initial_set = [fun (r,h) -> (true,r,h)];;
105 let initial_r = fun var -> failwith ("no value for " ^ (Char.escaped var));;
107 let bool_set = List.map (fun one_dpm -> let (value, r, h) = one_dpm (initial_r, []) in value) dpm_set
108 in List.exists (fun truth_value -> truth_value) bool_set;;
110 let male obj = obj = Bob || obj = Ted;;
111 let wife_of x y = (x,y) = (Bob, Carol) || (x,y) = (Ted, Alice);;
112 let kisses x y = (x,y) = (Bob, Carol) || (x,y) = (Ted, Alice);;
113 let misses x y = (x,y) = (Bob, Carol) || (x,y) = (Ted, Carol);;
117 (* "a man x has a wife y" *)
118 let antecedent = fun one_dpm -> exists 'x' one_dpm >>= lift_predicate male getx >>= exists 'y' >>= lift_predicate2 wife_of getx gety;;
120 (* "if a man x has a wife y, x kisses y" *)
121 run (initial_set >>= if_op antecedent (lift_predicate2 kisses getx gety));;
122 (* Bob has wife Carol, and kisses her; and Ted has wife Alice and kisses her; so this is true! *)
124 (* "if a man x has a wife y, x misses y" *)
125 run (initial_set >>= if_op antecedent (lift_predicate2 misses getx gety));;
126 (* Bob has wife Carol, and misses her; but Ted misses only Carol, not his wife Alice; so this is false! *)