-
Notifications
You must be signed in to change notification settings - Fork 0
/
40.ml
73 lines (63 loc) · 2.43 KB
/
40.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(* === Logic and Codes === *)
(*
Let us define a small "language" for boolean expressions containing variables:
# type bool_expr =
| Var of string
| Not of bool_expr
| And of bool_expr * bool_expr
| Or of bool_expr * bool_expr;;
type bool_expr =
Var of string
| Not of bool_expr
| And of bool_expr * bool_expr
| Or of bool_expr * bool_expr
A logical expression in two variables can then be written in prefix notation. For example, (a ∨ b) ∧ (a ∧ b) is written:
# And(Or(Var "a", Var "b"), And(Var "a", Var "b"));;
- : bool_expr = And (Or (Var "a", Var "b"), And (Var "a", Var "b")) *)
(* Truth tables for logical expressions (2 variables). (medium)
Define a function, table2 which returns the truth table of a given logical expression in two variables (specified as arguments). The return value must be a list of triples containing (value_of_a, balue_of_b, value_of_expr). *)
(*
# table2 "a" "b" (And(Var "a", Or(Var "a", Var "b")));;
- : (bool * bool * bool) list =
[(true, true, true); (true, false, true); (false, true, false);
(false, false, false)]
*)
type bool_expr =
| Var of string
| Not of bool_expr
| And of bool_expr * bool_expr
| Or of bool_expr * bool_expr;;
let rec eval vars = function
| Var s -> Hashtbl.find vars s
| Not expr -> not (eval vars expr)
| And (lhs, rhs) -> (eval vars lhs) && (eval vars rhs)
| Or (lhs, rhs) -> (eval vars lhs) || (eval vars rhs);;
let table2 a b expr =
let aux a b aval bval expr =
let vars = Hashtbl.create 2 in
let _ =
Hashtbl.add vars a aval;
Hashtbl.add vars b bval; () in
(aval, bval, eval vars expr) in
(aux a b true true expr) ::
(aux a b true false expr) ::
(aux a b false true expr) ::
(aux a b false false expr) ::
[];;
table2 "a" "b" (And (Var "a", Var "b"));;
table2 "a" "b" (And (Not (Var "a"), Var "b"));;
table2 "a" "b" (Or (And (Var "a", Var "b"), And (Not (Var "a"), Not (Var "b"))));;
(*
# table2 "a" "b" (And (Var "a", Var "b"));;
- : (bool * bool * bool) list =
[(true, true, true); (true, false, false); (false, true, false);
(false, false, false)]
# table2 "a" "b" (And (Not (Var "a"), Var "b"));;
- : (bool * bool * bool) list =
[(true, true, false); (true, false, false); (false, true, true);
(false, false, false)]
# table2 "a" "b" (Or (And (Var "a", Var "b"), And (Not (Var "a"), Not (Var "b"))));;
- : (bool * bool * bool) list =
[(true, true, true); (true, false, false); (false, true, false);
(false, false, true)]
*)