Let's say we have a function that takes as input a set of Boolean variables: bol1,bol2...boln. How can we iterate through all possible Boolean assignments using minimum codes as possible? I need to implement a function that takes input a set of two Boolean variables together with a Boolean expression involving the variables and create a truth table. If you look at my code, It is long. SO I want to reduce it. Furthermore, the way I did it seem to be redudndant as the compiler gives warning saying the match case |Var v2 is unused for all match case |Var v2 in the code.
This is exercises 46/47 in this link : "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)."
type bool_expr =
| Var of string
| Not of bool_expr
| And of bool_expr * bool_expr
| Or of bool_expr * bool_expr
let table2 (v1 : string) (v2 : string ) (exp : bool_expr)=
let rec evaluate (bol1 : bool) (bol2 : bool) (expp : bool_expr) =
match bol1, bol2 with
|true, true -> (match expp with
|Var v1 -> true
|Var v2 -> true
|Not q -> not (evaluate bol1 bol2 q )
|And (q,w) -> (evaluate bol1 bol2 q) && (evaluate bol1 bol2 w)
|Or (q, w) -> (evaluate bol1 bol2 q) || (evaluate bol1 bol2 w))
|true, false -> (match expp with
|Var v1 -> true
|Var v2 -> false
|Not q -> not (evaluate bol1 bol2 q )
|And (q,w) -> (evaluate bol1 bol2 q) && (evaluate bol1 bol2 w)
|Or (q, w) -> (evaluate bol1 bol2 q) || (evaluate bol1 bol2 w))
|false, true -> (match expp with
|Var v1 -> false
|Var v2 -> true
|Not q -> not (evaluate bol1 bol2 q )
|And (q,w) -> (evaluate bol1 bol2 q) && (evaluate bol1 bol2 w)
|Or (q, w) -> (evaluate bol1 bol2 q) || (evaluate bol1 bol2 w))
|false, false -> (match expp with
|Var v1 -> false
|Var v2 -> false
|Not q -> not (evaluate bol1 bol2 q )
|And (q,w) -> (evaluate bol1 bol2 q) && (evaluate bol1 bol2 w)
|Or (q, w) -> (evaluate bol1 bol2 q) || (evaluate bol1 bol2 w))
in [(true,true, (evaluate true true exp));(true,false, (evaluate true false exp));(false,true, (evaluate false true exp));(false,false, (evaluate false false exp))]
Here is an example of the expected output:
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)]