-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathp48.re
26 lines (23 loc) · 793 Bytes
/
p48.re
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
/* Truth tables for logical expressions. */
type bool_expr =
| Var(string)
| Not(bool_expr)
| And(bool_expr, bool_expr)
| Or(bool_expr, bool_expr);
let rec eval = (val_vars, exp) =>
switch exp {
| Var(x) => List.assoc(x, val_vars)
| Not(e) => ! eval(val_vars, e)
| And(e1, e2) => eval(val_vars, e1) && eval(val_vars, e2)
| Or(e1, e2) => eval(val_vars, e1) || eval(val_vars, e2)
};
/* Again, this is an easy and short implementation rather than an
efficient one. */
let rec table_make = (val_vars, vars, expr) =>
switch vars {
| [] => [(List.rev(val_vars), eval(val_vars, expr))]
| [v, ...tl] =>
table_make([(v, true), ...val_vars], tl, expr)
@ table_make([(v, false), ...val_vars], tl, expr)
};
let table = (vars, expr) => table_make([], vars, expr);