Internal and external SAT solvers
authorwebertj
Wed Mar 10 20:28:18 2004 +0100 (2004-03-10)
changeset 144533397a69dfa4e
parent 14452 c24d90dbf0c9
child 14454 8a8330bef1f8
Internal and external SAT solvers
src/HOL/Tools/sat_solver.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/sat_solver.ML	Wed Mar 10 20:28:18 2004 +0100
     1.3 @@ -0,0 +1,439 @@
     1.4 +(*  Title:      HOL/Tools/sat_solver.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tjark Weber
     1.7 +    Copyright   2004
     1.8 +
     1.9 +Interface to external SAT solvers, and (simple) built-in SAT solvers.
    1.10 +*)
    1.11 +
    1.12 +signature SAT_SOLVER =
    1.13 +sig
    1.14 +	type solver  (* PropLogic.prop_formula -> (int -> bool) option *)
    1.15 +
    1.16 +	(* external SAT solvers *)
    1.17 +	val write_dimacs_cnf_file : Path.T -> PropLogic.prop_formula -> unit
    1.18 +	val write_dimacs_sat_file : Path.T -> PropLogic.prop_formula -> unit
    1.19 +	val parse_std_result_file : Path.T -> string -> (int -> bool) option
    1.20 +	val make_external_solver  : string -> (PropLogic.prop_formula -> unit) -> (unit -> (int -> bool) option) -> solver
    1.21 +
    1.22 +	(* generic interface *)
    1.23 +	val solvers       : (solver Symtab.table) ref
    1.24 +	val add_solver    : string * solver -> unit  (* exception DUP *)
    1.25 +	val invoke_solver : string -> solver         (* exception OPTION *)
    1.26 +	val preferred     : string ref
    1.27 +end;
    1.28 +
    1.29 +structure SatSolver : SAT_SOLVER =
    1.30 +struct
    1.31 +
    1.32 +	open PropLogic;
    1.33 +
    1.34 +(* ------------------------------------------------------------------------- *)
    1.35 +(* Type of SAT solvers: given a propositional formula, a satisfying          *)
    1.36 +(*      assignment may be returned                                           *)
    1.37 +(* ------------------------------------------------------------------------- *)
    1.38 +
    1.39 +	type solver = prop_formula -> (int -> bool) option;
    1.40 +
    1.41 +(* ------------------------------------------------------------------------- *)
    1.42 +(* write_dimacs_cnf_file: serializes a formula 'fm' of propositional logic   *)
    1.43 +(*      to a file in DIMACS CNF format (see "Satisfiability Suggested        *)
    1.44 +(*      Format", May 8 1993, Section 2.1)                                    *)
    1.45 +(* Note: 'fm' must not contain a variable index less than 1.                 *)
    1.46 +(* Note: 'fm' is converted into (definitional) CNF.                          *)
    1.47 +(* ------------------------------------------------------------------------- *)
    1.48 +
    1.49 +	(* Path.T -> prop_formula -> unit *)
    1.50 +
    1.51 +	fun write_dimacs_cnf_file path fm =
    1.52 +	let
    1.53 +		(* prop_formula -> prop_formula *)
    1.54 +		fun cnf_True_False_elim True =
    1.55 +			Or (BoolVar 1, Not (BoolVar 1))
    1.56 +		  | cnf_True_False_elim False =
    1.57 +			And (BoolVar 1, Not (BoolVar 1))
    1.58 +		  | cnf_True_False_elim fm =
    1.59 +			fm  (* since 'fm' is in CNF, either 'fm'='True'/'False', or 'fm' does not contain 'True'/'False' at all *)
    1.60 +		(* prop_formula -> int *)
    1.61 +		fun cnf_number_of_clauses (And (fm1,fm2)) =
    1.62 +			(cnf_number_of_clauses fm1) + (cnf_number_of_clauses fm2)
    1.63 +		  | cnf_number_of_clauses _ =
    1.64 +			1
    1.65 +		(* prop_formula -> string *)
    1.66 +		fun cnf_string True =
    1.67 +			error "formula is not in CNF"
    1.68 +		  | cnf_string False =
    1.69 +			error "formula is not in CNF"
    1.70 +		  | cnf_string (BoolVar i) =
    1.71 +			(assert (i>=1) "formula contains a variable index less than 1";
    1.72 +			string_of_int i)
    1.73 +		  | cnf_string (Not fm) =
    1.74 +			"-" ^ (cnf_string fm)
    1.75 +		  | cnf_string (Or (fm1,fm2)) =
    1.76 +			(cnf_string fm1) ^ " " ^ (cnf_string fm2)
    1.77 +		  | cnf_string (And (fm1,fm2)) =
    1.78 +			(cnf_string fm1) ^ " 0\n" ^ (cnf_string fm2)
    1.79 +	in
    1.80 +		File.write path
    1.81 +			(let
    1.82 +				val cnf               = (cnf_True_False_elim o defcnf) fm  (* conversion into def. CNF *)
    1.83 +				val number_of_vars    = maxidx cnf
    1.84 +				val number_of_clauses = cnf_number_of_clauses cnf
    1.85 +			in
    1.86 +				"c This file was generated by SatSolver.write_dimacs_cnf_file\n" ^
    1.87 +				"c (c) Tjark Weber\n" ^
    1.88 +				"p cnf " ^ (string_of_int number_of_vars) ^ " " ^ (string_of_int number_of_clauses) ^ "\n" ^
    1.89 +				(cnf_string cnf) ^ "\n"
    1.90 +			end)
    1.91 +	end;
    1.92 +
    1.93 +(* ------------------------------------------------------------------------- *)
    1.94 +(* write_dimacs_sat_file: serializes a formula 'fm' of propositional logic   *)
    1.95 +(*      to a file in DIMACS SAT format (see "Satisfiability Suggested        *)
    1.96 +(*      Format", May 8 1993, Section 2.2)                                    *)
    1.97 +(* Note: 'fm' must not contain a variable index less than 1.                 *)
    1.98 +(* ------------------------------------------------------------------------- *)
    1.99 +
   1.100 +	(* Path.T -> prop_formula -> unit *)
   1.101 +
   1.102 +	fun write_dimacs_sat_file path fm =
   1.103 +	let
   1.104 +		(* prop_formula -> string *)
   1.105 +		fun sat_string True =
   1.106 +			"*()"
   1.107 +		  | sat_string False =
   1.108 +			"+()"
   1.109 +		  | sat_string (BoolVar i) =
   1.110 +			(assert (i>=1) "formula contains a variable index less than 1";
   1.111 +			string_of_int i)
   1.112 +		  | sat_string (Not fm) =
   1.113 +			"-(" ^ (sat_string fm) ^ ")"
   1.114 +		  | sat_string (Or (fm1,fm2)) =
   1.115 +			"+(" ^ (sat_string fm1) ^ " " ^ (sat_string fm2) ^ ")"
   1.116 +		  | sat_string (And (fm1,fm2)) =
   1.117 +			"*(" ^ (sat_string fm1) ^ " " ^ (sat_string fm2) ^ ")"
   1.118 +	in
   1.119 +		File.write path
   1.120 +			(let
   1.121 +				val number_of_vars = Int.max (maxidx fm, 1)
   1.122 +			in
   1.123 +				"c This file was generated by SatSolver.write_dimacs_sat_file\n" ^
   1.124 +				"c (c) Tjark Weber\n" ^
   1.125 +				"p sat " ^ (string_of_int number_of_vars) ^ "\n" ^
   1.126 +				"(" ^ (sat_string fm) ^ ")\n"
   1.127 +			end)
   1.128 +	end;
   1.129 +
   1.130 +(* ------------------------------------------------------------------------- *)
   1.131 +(* parse_std_result_file: scans a SAT solver's output file for a satisfying  *)
   1.132 +(*      variable assignment.  Returns the assignment, or 'None' if the SAT   *)
   1.133 +(*      solver did not find one.  The file format must be as follows:        *)
   1.134 +(*      ,-----                                                               *)
   1.135 +(*      | 0 or more lines not containing 'success'                           *)
   1.136 +(*      | A line containing 'success' as a substring                         *)
   1.137 +(*      | A line ASSIGNMENT containing integers, separated by " "            *)
   1.138 +(*      | 0 or more lines                                                    *)
   1.139 +(*      `-----                                                               *)
   1.140 +(*      If variable i is contained in ASSIGNMENT, then i is interpreted as   *)
   1.141 +(*      'true'.  If ~i is contained in ASSIGNMENT, then i is interpreted as  *)
   1.142 +(*      'false'.                                                             *)
   1.143 +(* ------------------------------------------------------------------------- *)
   1.144 +
   1.145 +	(* Path.T -> string -> (int -> bool) option *)
   1.146 +
   1.147 +	fun parse_std_result_file path success =
   1.148 +	let
   1.149 +		(* 'a option -> 'a Library.option *)
   1.150 +		fun option (SOME a) =
   1.151 +			Some a
   1.152 +		  | option NONE =
   1.153 +			None
   1.154 +		(* string -> int list *)
   1.155 +		fun int_list_from_string s =
   1.156 +			mapfilter (option o Int.fromString) (space_explode " " s)
   1.157 +		(* int list -> int -> bool *)
   1.158 +		fun assignment_from_list [] i =
   1.159 +			false  (* could be 'true' just as well; the SAT solver didn't provide a value for this variable *)
   1.160 +		  | assignment_from_list (x::xs) i =
   1.161 +			if x=i then true
   1.162 +			else if x=(~i) then false
   1.163 +			else assignment_from_list xs i
   1.164 +		(* string -> string -> bool *)
   1.165 +		fun is_substring needle haystack =
   1.166 +		let
   1.167 +			val length1 = String.size needle
   1.168 +			val length2 = String.size haystack
   1.169 +		in
   1.170 +			if length2 < length1 then
   1.171 +				false
   1.172 +			else if needle = String.substring (haystack, 0, length1) then
   1.173 +				true
   1.174 +			else is_substring needle (String.substring (haystack, 1, length2-1))
   1.175 +		end
   1.176 +		(* string list -> int list option *)
   1.177 +		fun parse_lines [] =
   1.178 +			None
   1.179 +		  | parse_lines (line::lines) =
   1.180 +			if is_substring success line then
   1.181 +				(* the next line must be a list of integers *)
   1.182 +				(Some o assignment_from_list o int_list_from_string o hd) lines
   1.183 +			else
   1.184 +				parse_lines lines
   1.185 +	in
   1.186 +		(parse_lines o split_lines o File.read) path
   1.187 +	end;
   1.188 +
   1.189 +(* ------------------------------------------------------------------------- *)
   1.190 +(* make_external_solver: call 'writefn', execute 'cmd', call 'readfn'        *)
   1.191 +(* ------------------------------------------------------------------------- *)
   1.192 +
   1.193 +	(* string -> (prop_formula -> unit) -> (unit -> (int -> bool) option) -> solver *)
   1.194 +
   1.195 +	fun make_external_solver cmd writefn readfn fm =
   1.196 +		(writefn fm;
   1.197 +		assert ((system cmd)=0) ("system command " ^ quote cmd ^ " failed (make sure the SAT solver is installed)");
   1.198 +		readfn ());
   1.199 +
   1.200 +(* ------------------------------------------------------------------------- *)
   1.201 +(* solvers: a (reference to a) table of all registered SAT solvers           *)
   1.202 +(* ------------------------------------------------------------------------- *)
   1.203 +
   1.204 +	val solvers = ref Symtab.empty;
   1.205 +
   1.206 +(* ------------------------------------------------------------------------- *)
   1.207 +(* add_solver: updates 'solvers' by adding a new solver                      *)
   1.208 +(* Note: No two solvers may have the same 'name'; otherwise exception 'DUP'  *)
   1.209 +(*       will be raised.                                                     *)
   1.210 +(* ------------------------------------------------------------------------- *)
   1.211 +
   1.212 +	(* string * solver -> unit *)
   1.213 +
   1.214 +	fun add_solver (name, new_solver) =
   1.215 +		solvers := Symtab.update_new ((name, new_solver), !solvers);
   1.216 +
   1.217 +(* ------------------------------------------------------------------------- *)
   1.218 +(* invoke_solver: returns the solver associated with the given 'name'        *)
   1.219 +(* Note: If no solver is associated with 'name', exception 'OPTION' will be  *)
   1.220 +(*       raised.                                                             *)
   1.221 +(* ------------------------------------------------------------------------- *)
   1.222 +
   1.223 +	(* string -> solver *)
   1.224 +
   1.225 +	fun invoke_solver name =
   1.226 +		(the o Symtab.lookup) (!solvers, name);
   1.227 +
   1.228 +(* ------------------------------------------------------------------------- *)
   1.229 +(* preferred: the name of the preferred SAT solver                           *)
   1.230 +(* ------------------------------------------------------------------------- *)
   1.231 +
   1.232 +	val preferred = ref "";
   1.233 +
   1.234 +end;  (* SatSolver *)
   1.235 +
   1.236 +
   1.237 +(* ------------------------------------------------------------------------- *)
   1.238 +(* Predefined SAT solvers                                                    *)
   1.239 +(* ------------------------------------------------------------------------- *)
   1.240 +
   1.241 +(* ------------------------------------------------------------------------- *)
   1.242 +(* Internal SAT solver, available as 'SatSolver.solve "enumerate"' -- simply *)
   1.243 +(* enumerates assignments until a satisfying assignment is found             *)
   1.244 +(* ------------------------------------------------------------------------- *)
   1.245 +
   1.246 +let
   1.247 +	fun enum_solver fm =
   1.248 +	let
   1.249 +		(* int list *)
   1.250 +		val indices = PropLogic.indices fm
   1.251 +		(* int list -> int list -> int list option *)
   1.252 +		(* binary increment: list 'xs' of current bits, list 'ys' of all bits (lower bits first) *)
   1.253 +		fun next_list _ ([]:int list) =
   1.254 +			None
   1.255 +		  | next_list [] (y::ys) =
   1.256 +			Some [y]
   1.257 +		  | next_list (x::xs) (y::ys) =
   1.258 +			if x=y then
   1.259 +				(* reset the bit, continue *)
   1.260 +				next_list xs ys
   1.261 +			else
   1.262 +				(* set the lowest bit that wasn't set before, keep the higher bits *)
   1.263 +				Some (y::x::xs)
   1.264 +		(* int list -> int -> bool *)
   1.265 +		fun assignment_from_list xs i =
   1.266 +			i mem xs
   1.267 +		(* int list -> (int -> bool) option *)
   1.268 +		fun solver_loop xs =
   1.269 +			if PropLogic.eval (assignment_from_list xs) fm then
   1.270 +				Some (assignment_from_list xs)
   1.271 +			else
   1.272 +				(case next_list xs indices of
   1.273 +				  Some xs' => solver_loop xs'
   1.274 +				| None     => None)
   1.275 +	in
   1.276 +		(* start with the "first" assignment (all variables are interpreted as 'False') *)
   1.277 +		solver_loop []
   1.278 +	end
   1.279 +in
   1.280 +	SatSolver.add_solver ("enumerate", enum_solver)
   1.281 +end;
   1.282 +
   1.283 +(* ------------------------------------------------------------------------- *)
   1.284 +(* Internal SAT solver, available as 'SatSolver.solve "dpll"' -- a simple    *)
   1.285 +(* implementation of the DPLL algorithm (cf. L. Zhang, S. Malik: "The Quest  *)
   1.286 +(* for Efficient Boolean Satisfiability Solvers", July 2002, Fig. 1).        *)
   1.287 +(* ------------------------------------------------------------------------- *)
   1.288 +
   1.289 +let
   1.290 +	local
   1.291 +		open PropLogic
   1.292 +	in
   1.293 +		fun dpll_solver fm =
   1.294 +		let
   1.295 +			(* prop_formula *)
   1.296 +			val defcnf = PropLogic.defcnf fm
   1.297 +			(* int list *)
   1.298 +			val indices = PropLogic.indices defcnf
   1.299 +			(* int list -> int -> prop_formula *)
   1.300 +			fun partial_var_eval []      i = BoolVar i
   1.301 +			  | partial_var_eval (x::xs) i = if x=i then True else if x=(~i) then False else partial_var_eval xs i
   1.302 +			(* int list -> prop_formula -> prop_formula *)
   1.303 +			fun partial_eval xs True             = True
   1.304 +			  | partial_eval xs False            = False
   1.305 +			  | partial_eval xs (BoolVar i)      = partial_var_eval xs i
   1.306 +			  | partial_eval xs (Not fm)         = SNot (partial_eval xs fm)
   1.307 +			  | partial_eval xs (Or (fm1, fm2))  = SOr (partial_eval xs fm1, partial_eval xs fm2)
   1.308 +			  | partial_eval xs (And (fm1, fm2)) = SAnd (partial_eval xs fm1, partial_eval xs fm2)
   1.309 +			(* prop_formula -> int list *)
   1.310 +			fun forced_vars True              = []
   1.311 +			  | forced_vars False             = []
   1.312 +			  | forced_vars (BoolVar i)       = [i]
   1.313 +			  | forced_vars (Not (BoolVar i)) = [~i]
   1.314 +			  | forced_vars (Or (fm1, fm2))   = (forced_vars fm1) inter_int (forced_vars fm2)
   1.315 +			  | forced_vars (And (fm1, fm2))  = (forced_vars fm1) union_int (forced_vars fm2)
   1.316 +			  (* Above, i *and* ~i may be forced.  In this case the first occurrence takes   *)
   1.317 +			  (* precedence, and the next partial evaluation of the formula returns 'False'. *)
   1.318 +			  | forced_vars _                 = raise ERROR  (* formula is not in negation normal form *)
   1.319 +			(* int list -> prop_formula -> (int list * prop_formula) *)
   1.320 +			fun eval_and_force xs fm =
   1.321 +			let
   1.322 +				val fm' = partial_eval xs fm
   1.323 +				val xs' = forced_vars fm'
   1.324 +			in
   1.325 +				if null xs' then
   1.326 +					(xs, fm')
   1.327 +				else
   1.328 +					eval_and_force (xs@xs') fm'  (* xs and xs' should be distinct, so '@' here should have *)
   1.329 +					                             (* the same effect as 'union_int'                         *)
   1.330 +			end
   1.331 +			(* int list -> int option *)
   1.332 +			fun fresh_var xs =
   1.333 +				Library.find_first (fn i => not (i mem_int xs) andalso not ((~i) mem_int xs)) indices
   1.334 +			(* int list -> prop_formula -> int list option *)
   1.335 +			(* partial assignment 'xs' *)
   1.336 +			fun dpll xs fm =
   1.337 +			let
   1.338 +				val (xs', fm') = eval_and_force xs fm
   1.339 +			in
   1.340 +				case fm' of
   1.341 +				  True  => Some xs'
   1.342 +				| False => None
   1.343 +				| _     =>
   1.344 +					let
   1.345 +						val x = the (fresh_var xs')  (* a fresh variable must exist since 'fm' did not evaluate to 'True'/'False' *)
   1.346 +					in
   1.347 +						case dpll (x::xs') fm' of  (* passing fm' rather than fm should save some simplification work *)
   1.348 +						  Some xs'' => Some xs''
   1.349 +						| None      => dpll ((~x)::xs') fm'  (* now try interpreting 'x' as 'False' *)
   1.350 +					end
   1.351 +			end
   1.352 +			(* int list -> int -> bool *)
   1.353 +			fun assignment_from_list [] i =
   1.354 +				false  (* could be 'true' just as well; the DPLL procedure didn't provide a value for this variable *)
   1.355 +			  | assignment_from_list (x::xs) i =
   1.356 +				if x=i then true
   1.357 +				else if x=(~i) then false
   1.358 +				else assignment_from_list xs i
   1.359 +		in
   1.360 +			(* initially, no variable is interpreted yet *)
   1.361 +			apsome assignment_from_list (dpll [] defcnf)
   1.362 +		end
   1.363 +	end  (* local *)
   1.364 +in
   1.365 +	SatSolver.add_solver ("dpll", dpll_solver);
   1.366 +	SatSolver.preferred := "dpll"
   1.367 +end;
   1.368 +
   1.369 +(* ------------------------------------------------------------------------- *)
   1.370 +(* Internal SAT solver, available as 'SatSolver.solve "auto"': uses the      *)
   1.371 +(* preferred solver, or "dpll" if the preferred solver isn't present         *)
   1.372 +(* ------------------------------------------------------------------------- *)
   1.373 +
   1.374 +let
   1.375 +	fun auto_solver fm =
   1.376 +	let
   1.377 +		val preferred = !SatSolver.preferred
   1.378 +		val fallback  = "dpll"
   1.379 +	in
   1.380 +		if preferred="auto" then  (* prevent infinite recursion *)
   1.381 +			(warning ("Preferred SAT solver \"auto\": using solver " ^ quote fallback ^ " instead.");
   1.382 +			SatSolver.invoke_solver fallback fm)
   1.383 +		else if preferred=fallback then
   1.384 +			(warning ("Preferred SAT solver is " ^ quote fallback ^ "; for better performance, consider using an external solver.");
   1.385 +			SatSolver.invoke_solver fallback fm)
   1.386 +		else
   1.387 +			(SatSolver.invoke_solver preferred fm
   1.388 +			handle OPTION =>
   1.389 +				(warning ("Preferred SAT solver " ^ quote preferred ^ " not installed; using solver " ^ quote fallback ^ " instead.");
   1.390 +				SatSolver.invoke_solver fallback fm))
   1.391 +	end
   1.392 +in
   1.393 +	SatSolver.add_solver ("auto", auto_solver)
   1.394 +end;
   1.395 +
   1.396 +(* ------------------------------------------------------------------------- *)
   1.397 +(* ZChaff, Version 2003.12.04                                                *)
   1.398 +(* ------------------------------------------------------------------------- *)
   1.399 +
   1.400 +if getenv "ZCHAFF_HOME" <> "" then
   1.401 +	let
   1.402 +		val inname     = "isabelle.cnf"
   1.403 +		val outname    = "result"
   1.404 +		val inpath     = File.tmp_path (Path.unpack inname)
   1.405 +		val outpath    = File.tmp_path (Path.unpack outname)
   1.406 +		val cmd        = (getenv "ZCHAFF_HOME") ^ "/zchaff " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
   1.407 +		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath fm
   1.408 +		fun readfn ()  = SatSolver.parse_std_result_file outpath "Verify Solution successful. Instance satisfiable"
   1.409 +		fun zchaff fm =
   1.410 +		let
   1.411 +			val _          = assert (not (File.exists inpath)) ("file " ^ quote (Path.pack inpath) ^ " exists, please delete (will not overwrite)")
   1.412 +			val _          = assert (not (File.exists outpath)) ("file " ^ quote (Path.pack outpath) ^ " exists, please delete (will not overwrite)")
   1.413 +			val assignment = SatSolver.make_external_solver cmd writefn readfn fm
   1.414 +			val _          = (File.rm inpath handle _ => ())
   1.415 +			val _          = (File.rm outpath handle _ => ())
   1.416 +		in
   1.417 +			assignment
   1.418 +		end
   1.419 +	in
   1.420 +		SatSolver.add_solver ("zchaff", zchaff);
   1.421 +		SatSolver.preferred := "zchaff"
   1.422 +	end
   1.423 +else
   1.424 +	();
   1.425 +
   1.426 +(* ------------------------------------------------------------------------- *)
   1.427 +(* Add code for other SAT solvers below.                                     *)
   1.428 +(* ------------------------------------------------------------------------- *)
   1.429 +
   1.430 +(*
   1.431 +if mysolver_is_installed then
   1.432 +	let
   1.433 +		fun mysolver fm = ...
   1.434 +	in
   1.435 +		SatSolver.add_solver ("myname", mysolver);  -- register the solver
   1.436 +		SatSolver.preferred := "myname"             -- make it the preferred solver (optional)
   1.437 +	end
   1.438 +else
   1.439 +	();
   1.440 +
   1.441 +-- the solver is now available as SatSolver.invoke_solver "myname"
   1.442 +*)