forked from charliermarsh/OCaml-SAT-Solvers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnf.ml
57 lines (50 loc) · 1.68 KB
/
cnf.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
type symbol = string ;;
type literal = symbol * bool ;;
type clause = literal list ;;
type cnf = clause list ;;
type assignment = symbol * bool ;;
type model = assignment list ;;
open List ;;
(* utility method for finding the assignment of a symbol within a model *)
let rec find_assignment (sym:symbol) (ml:model) : bool option =
match ml with
[] -> None
| (sym', b)::tl -> if (sym = sym') then Some b else find_assignment sym tl
;;
(* utility method for checking if a model satisfies a clause *)
let is_clause_sat (cl:clause) (ml:model) : bool =
List.fold_left (fun acc lit -> let (sym, b) = lit in
acc || (Some b = find_assignment sym ml))
false cl
;;
(* utility method for checking if a model satisfies a cnf *)
let is_cnf_sat (cnf:cnf) (ml:model) : bool =
List.fold_left (fun acc cl -> acc && is_clause_sat cl ml) true cnf
;;
(* utility method for extracting set of symbols in a cnf *)
let symbols_in_cnf (cnf:cnf) : symbol list =
let contains (sym:symbol) (syms:symbol list) : bool =
List.fold_left (fun acc nxt -> acc || nxt = sym) false syms in
List.fold_left (fun all_syms nxt ->
List.fold_left (fun acc nxt ->
let (sym, _) = nxt in if not (contains sym acc) then sym::acc else acc)
all_syms nxt)
[] cnf
;;
(* method to print model *)
let print_model (ml:model) =
List.fold_left
(fun _ (sym, b) ->
print_string sym;
print_string " = ";
if b then print_string "true" else print_string "false";
print_newline ()) () ml
;;
(* method to print cnf *)
let print_cnf (cnf:cnf) =
List.fold_left
(fun _ clause ->
print_string "---CLAUSE---";
print_newline ();
print_model clause) () cnf
;;