src/HOL/Tools/sat_solver.ML
author webertj
Tue, 26 Jul 2005 14:31:42 +0200
changeset 16914 e68528b4fc0b
parent 16912 35b01ba73625
child 16915 bca4c3b1afca
permissions -rw-r--r--
replaced calls to PropLogic.auxcnf by PropLogic.defcnf again
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     1
(*  Title:      HOL/Tools/sat_solver.ML
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     2
    ID:         $Id$
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     3
    Author:     Tjark Weber
16911
20a139ca2f62 replaced calls to PropLogic.defcnf by PropLogic.auxcnf
webertj
parents: 16899
diff changeset
     4
    Copyright   2004-2005
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     5
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     6
Interface to external SAT solvers, and (simple) built-in SAT solvers.
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     7
*)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     8
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
     9
signature SAT_SOLVER =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    10
sig
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    11
	exception NOT_CONFIGURED
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    12
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    13
	type assignment = int -> bool option
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    14
	datatype result = SATISFIABLE of assignment
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    15
	                | UNSATISFIABLE
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    16
	                | UNKNOWN
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    17
	type solver     = PropLogic.prop_formula -> result
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    18
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    19
	(* auxiliary functions to create external SAT solvers *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    20
	val write_dimacs_cnf_file : Path.T -> PropLogic.prop_formula -> unit
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    21
	val write_dimacs_sat_file : Path.T -> PropLogic.prop_formula -> unit
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    22
	val parse_std_result_file : Path.T -> string * string * string -> result
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    23
	val make_external_solver  : string -> (PropLogic.prop_formula -> unit) -> (unit -> result) -> solver
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    24
15040
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
    25
	val read_dimacs_cnf_file : Path.T -> PropLogic.prop_formula
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
    26
16899
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
    27
	(* generic solver interface *)
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
    28
	val solvers       : (string * solver) list ref
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
    29
	val add_solver    : string * solver -> unit
15605
0c544d8b521f minor Library.option related modifications
webertj
parents: 15570
diff changeset
    30
	val invoke_solver : string -> solver  (* exception Option *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    31
end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    32
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    33
structure SatSolver : SAT_SOLVER =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    34
struct
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    35
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    36
	open PropLogic;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    37
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    38
(* ------------------------------------------------------------------------- *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    39
(* should be raised by an external SAT solver to indicate that the solver is *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    40
(* not configured properly                                                   *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    41
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    42
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    43
	exception NOT_CONFIGURED;
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    44
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    45
(* ------------------------------------------------------------------------- *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
    46
(* type of partial (satisfying) assignments: 'a i = NONE' means that 'a' is  *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    47
(*      a satisfying assigment regardless of the value of variable 'i'       *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    48
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    49
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    50
	type assignment = int -> bool option;
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    51
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    52
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    53
(* return type of SAT solvers: if the result is 'SATISFIABLE', a satisfying  *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    54
(*      assignment must be returned as well                                  *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    55
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    56
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    57
	datatype result = SATISFIABLE of assignment
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    58
	                | UNSATISFIABLE
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    59
	                | UNKNOWN;
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    60
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    61
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    62
(* type of SAT solvers: given a propositional formula, a satisfying          *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    63
(*      assignment may be returned                                           *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    64
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    65
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    66
	type solver = prop_formula -> result;
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    67
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    68
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    69
(* write_dimacs_cnf_file: serializes a formula 'fm' of propositional logic   *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    70
(*      to a file in DIMACS CNF format (see "Satisfiability Suggested        *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    71
(*      Format", May 8 1993, Section 2.1)                                    *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    72
(* Note: 'fm' must not contain a variable index less than 1.                 *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
    73
(* Note: 'fm' must be given in CNF.                                          *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    74
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    75
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    76
	(* Path.T -> prop_formula -> unit *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    77
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    78
	fun write_dimacs_cnf_file path fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    79
	let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    80
		(* prop_formula -> prop_formula *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    81
		fun cnf_True_False_elim True =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    82
			Or (BoolVar 1, Not (BoolVar 1))
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    83
		  | cnf_True_False_elim False =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    84
			And (BoolVar 1, Not (BoolVar 1))
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    85
		  | cnf_True_False_elim fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    86
			fm  (* since 'fm' is in CNF, either 'fm'='True'/'False', or 'fm' does not contain 'True'/'False' at all *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    87
		(* prop_formula -> int *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    88
		fun cnf_number_of_clauses (And (fm1,fm2)) =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    89
			(cnf_number_of_clauses fm1) + (cnf_number_of_clauses fm2)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    90
		  | cnf_number_of_clauses _ =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    91
			1
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
    92
		(* prop_formula -> string *)
14999
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    93
		fun cnf_string fm =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    94
		let
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    95
			(* prop_formula -> string list -> string list *)
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    96
			fun cnf_string_acc True acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    97
				error "formula is not in CNF"
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    98
			  | cnf_string_acc False acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
    99
				error "formula is not in CNF"
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   100
			  | cnf_string_acc (BoolVar i) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   101
				(assert (i>=1) "formula contains a variable index less than 1";
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   102
				string_of_int i :: acc)
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   103
			  | cnf_string_acc (Not (BoolVar i)) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   104
				"-" :: cnf_string_acc (BoolVar i) acc
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   105
			  | cnf_string_acc (Not _) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   106
				error "formula is not in CNF"
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   107
			  | cnf_string_acc (Or (fm1,fm2)) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   108
				cnf_string_acc fm1 (" " :: cnf_string_acc fm2 acc)
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   109
			  | cnf_string_acc (And (fm1,fm2)) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   110
				cnf_string_acc fm1 (" 0\n" :: cnf_string_acc fm2 acc)
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   111
		in
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   112
			concat (cnf_string_acc fm [])
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   113
		end
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   114
	in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   115
		File.write path
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   116
			(let
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   117
				val fm'               = cnf_True_False_elim fm
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   118
				val number_of_vars    = maxidx fm'
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   119
				val number_of_clauses = cnf_number_of_clauses fm'
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   120
			in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   121
				"c This file was generated by SatSolver.write_dimacs_cnf_file\n" ^
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   122
				"c (c) Tjark Weber\n" ^
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   123
				"p cnf " ^ string_of_int number_of_vars ^ " " ^ string_of_int number_of_clauses ^ "\n" ^
14999
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   124
				cnf_string fm' ^ " 0\n"
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   125
			end)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   126
	end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   127
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   128
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   129
(* write_dimacs_sat_file: serializes a formula 'fm' of propositional logic   *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   130
(*      to a file in DIMACS SAT format (see "Satisfiability Suggested        *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   131
(*      Format", May 8 1993, Section 2.2)                                    *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   132
(* Note: 'fm' must not contain a variable index less than 1.                 *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   133
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   134
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   135
	(* Path.T -> prop_formula -> unit *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   136
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   137
	fun write_dimacs_sat_file path fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   138
	let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   139
		(* prop_formula -> string *)
14999
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   140
		fun sat_string fm =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   141
		let
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   142
			(* prop_formula -> string list -> string list *)
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   143
			fun sat_string_acc True acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   144
				"*()" :: acc
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   145
			  | sat_string_acc False acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   146
				"+()" :: acc
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   147
			  | sat_string_acc (BoolVar i) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   148
				(assert (i>=1) "formula contains a variable index less than 1";
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   149
				string_of_int i :: acc)
16899
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   150
			  | sat_string_acc (Not (BoolVar i)) acc =
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   151
				"-" :: sat_string_acc (BoolVar i) acc
14999
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   152
			  | sat_string_acc (Not fm) acc =
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   153
				"-(" :: sat_string_acc fm (")" :: acc)
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   154
			  | sat_string_acc (Or (fm1,fm2)) acc =
16899
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   155
				"+(" :: sat_string_acc_or fm1 (" " :: sat_string_acc_or fm2 (")" :: acc))
14999
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   156
			  | sat_string_acc (And (fm1,fm2)) acc =
16899
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   157
				"*(" :: sat_string_acc_and fm1 (" " :: sat_string_acc_and fm2 (")" :: acc))
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   158
			(* optimization to make use of n-ary disjunction/conjunction *)
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   159
			(* prop_formula -> string list -> string list *)
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   160
			and sat_string_acc_or (Or (fm1,fm2)) acc =
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   161
				sat_string_acc_or fm1 (" " :: sat_string_acc_or fm2 acc)
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   162
			  | sat_string_acc_or fm acc =
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   163
				sat_string_acc fm acc
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   164
			(* prop_formula -> string list -> string list *)
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   165
			and sat_string_acc_and (And (fm1,fm2)) acc =
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   166
				sat_string_acc_and fm1 (" " :: sat_string_acc_and fm2 acc)
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   167
			  | sat_string_acc_and fm acc =
ee4d620bcc1c write_dimacs_sat_file now generates slightly smaller files
webertj
parents: 16270
diff changeset
   168
				sat_string_acc fm acc
14999
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   169
		in
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   170
			concat (sat_string_acc fm [])
2c39efba8f67 faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents: 14965
diff changeset
   171
		end
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   172
	in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   173
		File.write path
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   174
			(let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   175
				val number_of_vars = Int.max (maxidx fm, 1)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   176
			in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   177
				"c This file was generated by SatSolver.write_dimacs_sat_file\n" ^
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   178
				"c (c) Tjark Weber\n" ^
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   179
				"p sat " ^ string_of_int number_of_vars ^ "\n" ^
16912
35b01ba73625 write_dimacs_sat_file writes outer parentheses again
webertj
parents: 16911
diff changeset
   180
				"(" ^ sat_string fm ^ ")\n"
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   181
			end)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   182
	end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   183
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   184
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   185
(* parse_std_result_file: scans a SAT solver's output file for a satisfying  *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   186
(*      variable assignment.  Returns the assignment, or 'UNSATISFIABLE' if  *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   187
(*      the file contains 'unsatisfiable', or 'UNKNOWN' if the file contains *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   188
(*      neither 'satisfiable' nor 'unsatisfiable'.  Empty lines are ignored. *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   189
(*      The assignment must be given in one or more lines immediately after  *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   190
(*      the line that contains 'satisfiable'.  These lines must begin with   *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   191
(*      'assignment_prefix'.  Variables must be separated by " ".  Non-      *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   192
(*      integer strings are ignored.  If variable i is contained in the      *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   193
(*      assignment, then i is interpreted as 'true'.  If ~i is contained in  *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   194
(*      the assignment, then i is interpreted as 'false'.  Otherwise the     *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   195
(*      value of i is taken to be unspecified.                               *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   196
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   197
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   198
	(* Path.T -> string * string * string -> result *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   199
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   200
	fun parse_std_result_file path (satisfiable, assignment_prefix, unsatisfiable) =
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   201
	let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   202
		(* string -> int list *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   203
		fun int_list_from_string s =
15605
0c544d8b521f minor Library.option related modifications
webertj
parents: 15570
diff changeset
   204
			List.mapPartial Int.fromString (space_explode " " s)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   205
		(* int list -> assignment *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   206
		fun assignment_from_list [] i =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   207
			NONE  (* the SAT solver didn't provide a value for this variable *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   208
		  | assignment_from_list (x::xs) i =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   209
			if x=i then (SOME true)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   210
			else if x=(~i) then (SOME false)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   211
			else assignment_from_list xs i
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   212
		(* int list -> string list -> assignment *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   213
		fun parse_assignment xs [] =
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   214
			assignment_from_list xs
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   215
		  | parse_assignment xs (line::lines) =
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   216
			if String.isPrefix assignment_prefix line then
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   217
				parse_assignment (xs @ int_list_from_string line) lines
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   218
			else
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   219
				assignment_from_list xs
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   220
		(* string -> string -> bool *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   221
		fun is_substring needle haystack =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   222
		let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   223
			val length1 = String.size needle
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   224
			val length2 = String.size haystack
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   225
		in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   226
			if length2 < length1 then
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   227
				false
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   228
			else if needle = String.substring (haystack, 0, length1) then
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   229
				true
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   230
			else is_substring needle (String.substring (haystack, 1, length2-1))
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   231
		end
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   232
		(* string list -> result *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   233
		fun parse_lines [] =
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   234
			UNKNOWN
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   235
		  | parse_lines (line::lines) =
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   236
			if is_substring satisfiable line then
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   237
				SATISFIABLE (parse_assignment [] lines)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   238
			else if is_substring unsatisfiable line then
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   239
				UNSATISFIABLE
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   240
			else
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   241
				parse_lines lines
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   242
	in
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   243
		(parse_lines o (List.filter (fn l => l <> "")) o split_lines o File.read) path
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   244
	end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   245
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   246
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   247
(* make_external_solver: call 'writefn', execute 'cmd', call 'readfn'        *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   248
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   249
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   250
	(* string -> (PropLogic.prop_formula -> unit) -> (unit -> result) -> solver *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   251
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   252
	fun make_external_solver cmd writefn readfn fm =
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   253
		(writefn fm; system cmd; readfn ());
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   254
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   255
(* ------------------------------------------------------------------------- *)
15040
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   256
(* read_dimacs_cnf_file: returns a propositional formula that corresponds to *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   257
(*      a SAT problem given in DIMACS CNF format                             *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   258
(* ------------------------------------------------------------------------- *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   259
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   260
	(* Path.T -> PropLogic.prop_formula *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   261
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   262
	fun read_dimacs_cnf_file path =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   263
	let
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   264
		(* string list -> string list *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   265
		fun filter_preamble [] =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   266
			error "problem line not found in DIMACS CNF file"
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   267
		  | filter_preamble (line::lines) =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   268
			if String.isPrefix "c " line then
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   269
				(* ignore comments *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   270
				filter_preamble lines
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   271
			else if String.isPrefix "p " line then
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   272
				(* ignore the problem line (which must be the last line of the preamble) *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   273
				(* Ignoring the problem line implies that if the file contains more clauses *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   274
				(* or variables than specified in its preamble, we will accept it anyway.   *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   275
				lines
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   276
			else
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   277
				error "preamble in DIMACS CNF file contains a line that does not begin with \"c \" or \"p \""
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   278
		(* string -> int *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   279
		fun int_from_string s =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   280
			case Int.fromString s of
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   281
			  SOME i => i
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   282
			| NONE   => error ("token " ^ quote s ^ "in DIMACS CNF file is not a number")
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   283
		(* int list -> int list list *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   284
		fun clauses xs =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   285
			let
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   286
				val (xs1, xs2) = take_prefix (fn i => i <> 0) xs
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   287
			in
15128
da03f05815b0 bugfix in read_dimacs_cnf_file
webertj
parents: 15040
diff changeset
   288
				case xs2 of
da03f05815b0 bugfix in read_dimacs_cnf_file
webertj
parents: 15040
diff changeset
   289
				  []      => [xs1]
da03f05815b0 bugfix in read_dimacs_cnf_file
webertj
parents: 15040
diff changeset
   290
				| (0::[]) => [xs1]
da03f05815b0 bugfix in read_dimacs_cnf_file
webertj
parents: 15040
diff changeset
   291
				| (0::tl) => xs1 :: clauses tl
15329
bd94b0a71dd2 Removed a "Matches are not exhaustive" warning
webertj
parents: 15313
diff changeset
   292
				| _       => raise ERROR  (* this cannot happen *)
15040
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   293
			end
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   294
		(* int -> PropLogic.prop_formula *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   295
		fun literal_from_int i =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   296
			(assert (i<>0) "variable index in DIMACS CNF file is 0";
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   297
			if i>0 then
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   298
				PropLogic.BoolVar i
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   299
			else
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   300
				PropLogic.Not (PropLogic.BoolVar (~i)))
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   301
		(* PropLogic.prop_formula list -> PropLogic.prop_formula *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   302
		fun disjunction [] =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   303
			error "empty clause in DIMACS CNF file"
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   304
		  | disjunction (x::xs) =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   305
			(case xs of
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   306
			  [] => x
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   307
			| _  => PropLogic.Or (x, disjunction xs))
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   308
		(* PropLogic.prop_formula list -> PropLogic.prop_formula *)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   309
		fun conjunction [] =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   310
			error "no clause in DIMACS CNF file"
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   311
		  | conjunction (x::xs) =
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   312
			(case xs of
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   313
			  [] => x
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   314
			| _  => PropLogic.And (x, conjunction xs))
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   315
	in
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   316
		(conjunction
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   317
		o (map disjunction)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   318
		o (map (map literal_from_int))
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   319
		o clauses
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   320
		o (map int_from_string)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   321
		o List.concat
15040
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   322
		o (map (String.fields (fn c => c mem [#" ", #"\t", #"\n"])))
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   323
		o filter_preamble
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   324
		o (List.filter (fn l => l <> ""))
15040
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   325
		o split_lines
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   326
		o File.read)
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   327
			path
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   328
	end;
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   329
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   330
(* ------------------------------------------------------------------------- *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   331
(* solvers: a (reference to a) table of all registered SAT solvers           *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   332
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   333
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   334
	val solvers = ref ([] : (string * solver) list);
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   335
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   336
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   337
(* add_solver: updates 'solvers' by adding a new solver                      *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   338
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   339
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   340
	(* string * solver -> unit *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   341
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   342
	fun add_solver (name, new_solver) =
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   343
		(solvers := overwrite_warn (!solvers, (name, new_solver)) ("SAT solver " ^ quote name ^ " was defined before"));
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   344
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   345
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   346
(* invoke_solver: returns the solver associated with the given 'name'        *)
15605
0c544d8b521f minor Library.option related modifications
webertj
parents: 15570
diff changeset
   347
(* Note: If no solver is associated with 'name', exception 'Option' will be  *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   348
(*       raised.                                                             *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   349
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   350
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   351
	(* string -> solver *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   352
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   353
	fun invoke_solver name =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   354
		(valOf o assoc) (!solvers, name);
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   355
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   356
end;  (* SatSolver *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   357
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   358
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   359
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   360
(* Predefined SAT solvers                                                    *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   361
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   362
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   363
(* ------------------------------------------------------------------------- *)
14753
f40b45db8cf0 Comments fixed
webertj
parents: 14703
diff changeset
   364
(* Internal SAT solver, available as 'SatSolver.invoke_solver "enumerate"'   *)
f40b45db8cf0 Comments fixed
webertj
parents: 14703
diff changeset
   365
(* -- simply enumerates assignments until a satisfying assignment is found   *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   366
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   367
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   368
let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   369
	fun enum_solver fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   370
	let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   371
		(* int list *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   372
		val indices = PropLogic.indices fm
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   373
		(* int list -> int list -> int list option *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   374
		(* binary increment: list 'xs' of current bits, list 'ys' of all bits (lower bits first) *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   375
		fun next_list _ ([]:int list) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   376
			NONE
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   377
		  | next_list [] (y::ys) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   378
			SOME [y]
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   379
		  | next_list (x::xs) (y::ys) =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   380
			if x=y then
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   381
				(* reset the bit, continue *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   382
				next_list xs ys
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   383
			else
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   384
				(* set the lowest bit that wasn't set before, keep the higher bits *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   385
				SOME (y::x::xs)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   386
		(* int list -> int -> bool *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   387
		fun assignment_from_list xs i =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   388
			i mem xs
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   389
		(* int list -> SatSolver.result *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   390
		fun solver_loop xs =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   391
			if PropLogic.eval (assignment_from_list xs) fm then
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   392
				SatSolver.SATISFIABLE (SOME o (assignment_from_list xs))
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   393
			else
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   394
				(case next_list xs indices of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   395
				  SOME xs' => solver_loop xs'
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   396
				| NONE     => SatSolver.UNSATISFIABLE)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   397
	in
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   398
		(* start with the "first" assignment (all variables are interpreted as 'false') *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   399
		solver_loop []
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   400
	end
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   401
in
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   402
	SatSolver.add_solver ("enumerate", enum_solver)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   403
end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   404
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   405
(* ------------------------------------------------------------------------- *)
14753
f40b45db8cf0 Comments fixed
webertj
parents: 14703
diff changeset
   406
(* Internal SAT solver, available as 'SatSolver.invoke_solver "dpll"' -- a   *)
f40b45db8cf0 Comments fixed
webertj
parents: 14703
diff changeset
   407
(* simple implementation of the DPLL algorithm (cf. L. Zhang, S. Malik: "The *)
f40b45db8cf0 Comments fixed
webertj
parents: 14703
diff changeset
   408
(* Quest for Efficient Boolean Satisfiability Solvers", July 2002, Fig. 1).  *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   409
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   410
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   411
let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   412
	local
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   413
		open PropLogic
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   414
	in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   415
		fun dpll_solver fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   416
		let
16914
e68528b4fc0b replaced calls to PropLogic.auxcnf by PropLogic.defcnf again
webertj
parents: 16912
diff changeset
   417
			(* We could use 'PropLogic.defcnf fm' instead of 'PropLogic.nnf fm' *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   418
			(* but that sometimes introduces more boolean variables than we can *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   419
			(* handle efficiently.                                              *)
14514
15abb7d42e2e fixed dpll solver (now uses NNF)
webertj
parents: 14489
diff changeset
   420
			val fm' = PropLogic.nnf fm
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   421
			(* int list *)
14514
15abb7d42e2e fixed dpll solver (now uses NNF)
webertj
parents: 14489
diff changeset
   422
			val indices = PropLogic.indices fm'
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   423
			(* int list -> int -> prop_formula *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   424
			fun partial_var_eval []      i = BoolVar i
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   425
			  | partial_var_eval (x::xs) i = if x=i then True else if x=(~i) then False else partial_var_eval xs i
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   426
			(* int list -> prop_formula -> prop_formula *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   427
			fun partial_eval xs True             = True
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   428
			  | partial_eval xs False            = False
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   429
			  | partial_eval xs (BoolVar i)      = partial_var_eval xs i
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   430
			  | partial_eval xs (Not fm)         = SNot (partial_eval xs fm)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   431
			  | partial_eval xs (Or (fm1, fm2))  = SOr (partial_eval xs fm1, partial_eval xs fm2)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   432
			  | partial_eval xs (And (fm1, fm2)) = SAnd (partial_eval xs fm1, partial_eval xs fm2)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   433
			(* prop_formula -> int list *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   434
			fun forced_vars True              = []
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   435
			  | forced_vars False             = []
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   436
			  | forced_vars (BoolVar i)       = [i]
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   437
			  | forced_vars (Not (BoolVar i)) = [~i]
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   438
			  | forced_vars (Or (fm1, fm2))   = (forced_vars fm1) inter_int (forced_vars fm2)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   439
			  | forced_vars (And (fm1, fm2))  = (forced_vars fm1) union_int (forced_vars fm2)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   440
			  (* Above, i *and* ~i may be forced.  In this case the first occurrence takes   *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   441
			  (* precedence, and the next partial evaluation of the formula returns 'False'. *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   442
			  | forced_vars _                 = raise ERROR  (* formula is not in negation normal form *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   443
			(* int list -> prop_formula -> (int list * prop_formula) *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   444
			fun eval_and_force xs fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   445
			let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   446
				val fm' = partial_eval xs fm
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   447
				val xs' = forced_vars fm'
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   448
			in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   449
				if null xs' then
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   450
					(xs, fm')
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   451
				else
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   452
					eval_and_force (xs@xs') fm'  (* xs and xs' should be distinct, so '@' here should have *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   453
					                             (* the same effect as 'union_int'                         *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   454
			end
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   455
			(* int list -> int option *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   456
			fun fresh_var xs =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   457
				Library.find_first (fn i => not (i mem_int xs) andalso not ((~i) mem_int xs)) indices
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   458
			(* int list -> prop_formula -> int list option *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   459
			(* partial assignment 'xs' *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   460
			fun dpll xs fm =
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   461
			let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   462
				val (xs', fm') = eval_and_force xs fm
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   463
			in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   464
				case fm' of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   465
				  True  => SOME xs'
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   466
				| False => NONE
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   467
				| _     =>
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   468
					let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   469
						val x = valOf (fresh_var xs')  (* a fresh variable must exist since 'fm' did not evaluate to 'True'/'False' *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   470
					in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   471
						case dpll (x::xs') fm' of  (* passing fm' rather than fm should save some simplification work *)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   472
						  SOME xs'' => SOME xs''
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   473
						| NONE      => dpll ((~x)::xs') fm'  (* now try interpreting 'x' as 'False' *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   474
					end
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   475
			end
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   476
			(* int list -> assignment *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   477
			fun assignment_from_list [] i =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   478
				NONE  (* the DPLL procedure didn't provide a value for this variable *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   479
			  | assignment_from_list (x::xs) i =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   480
				if x=i then (SOME true)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   481
				else if x=(~i) then (SOME false)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   482
				else assignment_from_list xs i
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   483
		in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   484
			(* initially, no variable is interpreted yet *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   485
			case dpll [] fm' of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   486
			  SOME assignment => SatSolver.SATISFIABLE (assignment_from_list assignment)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15332
diff changeset
   487
			| NONE            => SatSolver.UNSATISFIABLE
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   488
		end
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   489
	end  (* local *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   490
in
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   491
	SatSolver.add_solver ("dpll", dpll_solver)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   492
end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   493
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   494
(* ------------------------------------------------------------------------- *)
14753
f40b45db8cf0 Comments fixed
webertj
parents: 14703
diff changeset
   495
(* Internal SAT solver, available as 'SatSolver.invoke_solver "auto"': uses  *)
15299
576fd0b65ed8 solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents: 15128
diff changeset
   496
(* the first installed solver (other than "auto" itself) that does not raise *)
576fd0b65ed8 solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents: 15128
diff changeset
   497
(* 'NOT_CONFIGURED'.  (However, the solver may return 'UNKNOWN'.)            *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   498
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   499
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   500
let
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   501
	fun auto_solver fm =
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   502
	let
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   503
		fun loop [] =
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   504
			SatSolver.UNKNOWN
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   505
		  | loop ((name, solver)::solvers) =
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   506
			if name="auto" then
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   507
				(* do not call solver "auto" from within "auto" *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   508
				loop solvers
15299
576fd0b65ed8 solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents: 15128
diff changeset
   509
			else (
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   510
				(if name="dpll" orelse name="enumerate" then
14805
eff7b9df27e9 solver "auto" now issues a warning when it uses solver "enumerate"
webertj
parents: 14753
diff changeset
   511
					warning ("Using SAT solver " ^ quote name ^ "; for better performance, consider using an external solver.")
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   512
				else
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   513
					());
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   514
				(* apply 'solver' to 'fm' *)
15299
576fd0b65ed8 solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents: 15128
diff changeset
   515
				solver fm
576fd0b65ed8 solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents: 15128
diff changeset
   516
					handle SatSolver.NOT_CONFIGURED => loop solvers
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   517
			)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   518
	in
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   519
		loop (rev (!SatSolver.solvers))
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   520
	end
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   521
in
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   522
	SatSolver.add_solver ("auto", auto_solver)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   523
end;
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   524
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   525
(* ------------------------------------------------------------------------- *)
15332
0dc05858a862 added ZCHAFF_VERSION
webertj
parents: 15329
diff changeset
   526
(* zChaff (http://www.princeton.edu/~chaff/zchaff.html)                      *)
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   527
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   528
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   529
let
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   530
	fun zchaff fm =
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   531
	let
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   532
		val _          = if (getenv "ZCHAFF_HOME") = "" then raise SatSolver.NOT_CONFIGURED else ()
15332
0dc05858a862 added ZCHAFF_VERSION
webertj
parents: 15329
diff changeset
   533
		val _          = if (getenv "ZCHAFF_VERSION") <> "2004.5.13" andalso
0dc05858a862 added ZCHAFF_VERSION
webertj
parents: 15329
diff changeset
   534
		                    (getenv "ZCHAFF_VERSION") <> "2004.11.15" then raise SatSolver.NOT_CONFIGURED else ()
0dc05858a862 added ZCHAFF_VERSION
webertj
parents: 15329
diff changeset
   535
			(* both versions of zChaff appear to have the same interface, so we do *)
0dc05858a862 added ZCHAFF_VERSION
webertj
parents: 15329
diff changeset
   536
			(* not actually need to distinguish between them in the following code *)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   537
		val inpath     = File.tmp_path (Path.unpack "isabelle.cnf")
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   538
		val outpath    = File.tmp_path (Path.unpack "result")
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   539
		val cmd        = (getenv "ZCHAFF_HOME") ^ "/zchaff " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
16914
e68528b4fc0b replaced calls to PropLogic.auxcnf by PropLogic.defcnf again
webertj
parents: 16912
diff changeset
   540
		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm)
15040
ed574b4f7e70 read_dimacs_cnf_file added
webertj
parents: 14999
diff changeset
   541
		fun readfn ()  = SatSolver.parse_std_result_file outpath ("Instance Satisfiable", "", "Instance Unsatisfiable")
16270
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   542
		val _          = if File.exists inpath then warning ("overwriting existing file " ^ quote (Path.pack inpath)) else ()
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   543
		val _          = if File.exists outpath then warning ("overwriting existing file " ^ quote (Path.pack outpath)) else ()
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   544
		val result     = SatSolver.make_external_solver cmd writefn readfn fm
16270
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   545
		val _          = try File.rm inpath
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   546
		val _          = try File.rm outpath
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   547
	in
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   548
		result
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   549
	end
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   550
in
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   551
	SatSolver.add_solver ("zchaff", zchaff)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   552
end;
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   553
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   554
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   555
(* BerkMin 561 (http://eigold.tripod.com/BerkMin.html)                       *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   556
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   557
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   558
let
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   559
	fun berkmin fm =
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   560
	let
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   561
		val _          = if (getenv "BERKMIN_HOME") = "" orelse (getenv "BERKMIN_EXE") = "" then raise SatSolver.NOT_CONFIGURED else ()
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   562
		val inpath     = File.tmp_path (Path.unpack "isabelle.cnf")
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   563
		val outpath    = File.tmp_path (Path.unpack "result")
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   564
		val cmd        = (getenv "BERKMIN_HOME") ^ "/" ^ (getenv "BERKMIN_EXE") ^ " " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
16914
e68528b4fc0b replaced calls to PropLogic.auxcnf by PropLogic.defcnf again
webertj
parents: 16912
diff changeset
   565
		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   566
		fun readfn ()  = SatSolver.parse_std_result_file outpath ("Satisfiable          !!", "solution =", "UNSATISFIABLE          !!")
16270
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   567
		val _          = if File.exists inpath then warning ("overwriting existing file " ^ quote (Path.pack inpath)) else ()
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   568
		val _          = if File.exists outpath then warning ("overwriting existing file " ^ quote (Path.pack outpath)) else ()
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   569
		val result     = SatSolver.make_external_solver cmd writefn readfn fm
16270
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   570
		val _          = try File.rm inpath
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   571
		val _          = try File.rm outpath
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   572
	in
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   573
		result
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   574
	end
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   575
in
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   576
	SatSolver.add_solver ("berkmin", berkmin)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   577
end;
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   578
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   579
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   580
(* Jerusat 1.3 (http://www.cs.tau.ac.il/~ale1/)                              *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   581
(* ------------------------------------------------------------------------- *)
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   582
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   583
let
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   584
	fun jerusat fm =
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   585
	let
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   586
		val _          = if (getenv "JERUSAT_HOME") = "" then raise SatSolver.NOT_CONFIGURED else ()
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   587
		val inpath     = File.tmp_path (Path.unpack "isabelle.cnf")
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   588
		val outpath    = File.tmp_path (Path.unpack "result")
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   589
		val cmd        = (getenv "JERUSAT_HOME") ^ "/Jerusat1.3 " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
16914
e68528b4fc0b replaced calls to PropLogic.auxcnf by PropLogic.defcnf again
webertj
parents: 16912
diff changeset
   590
		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm)
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   591
		fun readfn ()  = SatSolver.parse_std_result_file outpath ("s SATISFIABLE", "v ", "s UNSATISFIABLE")
16270
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   592
		val _          = if File.exists inpath then warning ("overwriting existing file " ^ quote (Path.pack inpath)) else ()
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   593
		val _          = if File.exists outpath then warning ("overwriting existing file " ^ quote (Path.pack outpath)) else ()
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   594
		val result     = SatSolver.make_external_solver cmd writefn readfn fm
16270
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   595
		val _          = try File.rm inpath
4280d6bbc1bb replaced File.sysify_path by Path.pack;
wenzelm
parents: 15605
diff changeset
   596
		val _          = try File.rm outpath
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   597
	in
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   598
		result
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   599
	end
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   600
in
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   601
	SatSolver.add_solver ("jerusat", jerusat)
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   602
end;
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   603
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   604
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   605
(* Add code for other SAT solvers below.                                     *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   606
(* ------------------------------------------------------------------------- *)
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   607
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   608
(*
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   609
let
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   610
	fun mysolver fm = ...
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   611
in
14965
7155b319eafa new SAT solver interface
webertj
parents: 14805
diff changeset
   612
	SatSolver.add_solver ("myname", (fn fm => if mysolver_is_configured then mysolver fm else raise SatSolver.NOT_CONFIGURED));  -- register the solver
14487
157d0ea7b2da Installed solvers now determined at call time (as opposed to compile time)
webertj
parents: 14460
diff changeset
   613
end;
14453
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   614
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   615
-- the solver is now available as SatSolver.invoke_solver "myname"
3397a69dfa4e Internal and external SAT solvers
webertj
parents:
diff changeset
   616
*)