src/HOL/Tools/sat_solver.ML
author webertj
Wed Mar 10 20:28:18 2004 +0100 (2004-03-10)
changeset 14453 3397a69dfa4e
child 14460 04e787a4f17a
permissions -rw-r--r--
Internal and external SAT solvers
     1 (*  Title:      HOL/Tools/sat_solver.ML
     2     ID:         $Id$
     3     Author:     Tjark Weber
     4     Copyright   2004
     5 
     6 Interface to external SAT solvers, and (simple) built-in SAT solvers.
     7 *)
     8 
     9 signature SAT_SOLVER =
    10 sig
    11 	type solver  (* PropLogic.prop_formula -> (int -> bool) option *)
    12 
    13 	(* external SAT solvers *)
    14 	val write_dimacs_cnf_file : Path.T -> PropLogic.prop_formula -> unit
    15 	val write_dimacs_sat_file : Path.T -> PropLogic.prop_formula -> unit
    16 	val parse_std_result_file : Path.T -> string -> (int -> bool) option
    17 	val make_external_solver  : string -> (PropLogic.prop_formula -> unit) -> (unit -> (int -> bool) option) -> solver
    18 
    19 	(* generic interface *)
    20 	val solvers       : (solver Symtab.table) ref
    21 	val add_solver    : string * solver -> unit  (* exception DUP *)
    22 	val invoke_solver : string -> solver         (* exception OPTION *)
    23 	val preferred     : string ref
    24 end;
    25 
    26 structure SatSolver : SAT_SOLVER =
    27 struct
    28 
    29 	open PropLogic;
    30 
    31 (* ------------------------------------------------------------------------- *)
    32 (* Type of SAT solvers: given a propositional formula, a satisfying          *)
    33 (*      assignment may be returned                                           *)
    34 (* ------------------------------------------------------------------------- *)
    35 
    36 	type solver = prop_formula -> (int -> bool) option;
    37 
    38 (* ------------------------------------------------------------------------- *)
    39 (* write_dimacs_cnf_file: serializes a formula 'fm' of propositional logic   *)
    40 (*      to a file in DIMACS CNF format (see "Satisfiability Suggested        *)
    41 (*      Format", May 8 1993, Section 2.1)                                    *)
    42 (* Note: 'fm' must not contain a variable index less than 1.                 *)
    43 (* Note: 'fm' is converted into (definitional) CNF.                          *)
    44 (* ------------------------------------------------------------------------- *)
    45 
    46 	(* Path.T -> prop_formula -> unit *)
    47 
    48 	fun write_dimacs_cnf_file path fm =
    49 	let
    50 		(* prop_formula -> prop_formula *)
    51 		fun cnf_True_False_elim True =
    52 			Or (BoolVar 1, Not (BoolVar 1))
    53 		  | cnf_True_False_elim False =
    54 			And (BoolVar 1, Not (BoolVar 1))
    55 		  | cnf_True_False_elim fm =
    56 			fm  (* since 'fm' is in CNF, either 'fm'='True'/'False', or 'fm' does not contain 'True'/'False' at all *)
    57 		(* prop_formula -> int *)
    58 		fun cnf_number_of_clauses (And (fm1,fm2)) =
    59 			(cnf_number_of_clauses fm1) + (cnf_number_of_clauses fm2)
    60 		  | cnf_number_of_clauses _ =
    61 			1
    62 		(* prop_formula -> string *)
    63 		fun cnf_string True =
    64 			error "formula is not in CNF"
    65 		  | cnf_string False =
    66 			error "formula is not in CNF"
    67 		  | cnf_string (BoolVar i) =
    68 			(assert (i>=1) "formula contains a variable index less than 1";
    69 			string_of_int i)
    70 		  | cnf_string (Not fm) =
    71 			"-" ^ (cnf_string fm)
    72 		  | cnf_string (Or (fm1,fm2)) =
    73 			(cnf_string fm1) ^ " " ^ (cnf_string fm2)
    74 		  | cnf_string (And (fm1,fm2)) =
    75 			(cnf_string fm1) ^ " 0\n" ^ (cnf_string fm2)
    76 	in
    77 		File.write path
    78 			(let
    79 				val cnf               = (cnf_True_False_elim o defcnf) fm  (* conversion into def. CNF *)
    80 				val number_of_vars    = maxidx cnf
    81 				val number_of_clauses = cnf_number_of_clauses cnf
    82 			in
    83 				"c This file was generated by SatSolver.write_dimacs_cnf_file\n" ^
    84 				"c (c) Tjark Weber\n" ^
    85 				"p cnf " ^ (string_of_int number_of_vars) ^ " " ^ (string_of_int number_of_clauses) ^ "\n" ^
    86 				(cnf_string cnf) ^ "\n"
    87 			end)
    88 	end;
    89 
    90 (* ------------------------------------------------------------------------- *)
    91 (* write_dimacs_sat_file: serializes a formula 'fm' of propositional logic   *)
    92 (*      to a file in DIMACS SAT format (see "Satisfiability Suggested        *)
    93 (*      Format", May 8 1993, Section 2.2)                                    *)
    94 (* Note: 'fm' must not contain a variable index less than 1.                 *)
    95 (* ------------------------------------------------------------------------- *)
    96 
    97 	(* Path.T -> prop_formula -> unit *)
    98 
    99 	fun write_dimacs_sat_file path fm =
   100 	let
   101 		(* prop_formula -> string *)
   102 		fun sat_string True =
   103 			"*()"
   104 		  | sat_string False =
   105 			"+()"
   106 		  | sat_string (BoolVar i) =
   107 			(assert (i>=1) "formula contains a variable index less than 1";
   108 			string_of_int i)
   109 		  | sat_string (Not fm) =
   110 			"-(" ^ (sat_string fm) ^ ")"
   111 		  | sat_string (Or (fm1,fm2)) =
   112 			"+(" ^ (sat_string fm1) ^ " " ^ (sat_string fm2) ^ ")"
   113 		  | sat_string (And (fm1,fm2)) =
   114 			"*(" ^ (sat_string fm1) ^ " " ^ (sat_string fm2) ^ ")"
   115 	in
   116 		File.write path
   117 			(let
   118 				val number_of_vars = Int.max (maxidx fm, 1)
   119 			in
   120 				"c This file was generated by SatSolver.write_dimacs_sat_file\n" ^
   121 				"c (c) Tjark Weber\n" ^
   122 				"p sat " ^ (string_of_int number_of_vars) ^ "\n" ^
   123 				"(" ^ (sat_string fm) ^ ")\n"
   124 			end)
   125 	end;
   126 
   127 (* ------------------------------------------------------------------------- *)
   128 (* parse_std_result_file: scans a SAT solver's output file for a satisfying  *)
   129 (*      variable assignment.  Returns the assignment, or 'None' if the SAT   *)
   130 (*      solver did not find one.  The file format must be as follows:        *)
   131 (*      ,-----                                                               *)
   132 (*      | 0 or more lines not containing 'success'                           *)
   133 (*      | A line containing 'success' as a substring                         *)
   134 (*      | A line ASSIGNMENT containing integers, separated by " "            *)
   135 (*      | 0 or more lines                                                    *)
   136 (*      `-----                                                               *)
   137 (*      If variable i is contained in ASSIGNMENT, then i is interpreted as   *)
   138 (*      'true'.  If ~i is contained in ASSIGNMENT, then i is interpreted as  *)
   139 (*      'false'.                                                             *)
   140 (* ------------------------------------------------------------------------- *)
   141 
   142 	(* Path.T -> string -> (int -> bool) option *)
   143 
   144 	fun parse_std_result_file path success =
   145 	let
   146 		(* 'a option -> 'a Library.option *)
   147 		fun option (SOME a) =
   148 			Some a
   149 		  | option NONE =
   150 			None
   151 		(* string -> int list *)
   152 		fun int_list_from_string s =
   153 			mapfilter (option o Int.fromString) (space_explode " " s)
   154 		(* int list -> int -> bool *)
   155 		fun assignment_from_list [] i =
   156 			false  (* could be 'true' just as well; the SAT solver didn't provide a value for this variable *)
   157 		  | assignment_from_list (x::xs) i =
   158 			if x=i then true
   159 			else if x=(~i) then false
   160 			else assignment_from_list xs i
   161 		(* string -> string -> bool *)
   162 		fun is_substring needle haystack =
   163 		let
   164 			val length1 = String.size needle
   165 			val length2 = String.size haystack
   166 		in
   167 			if length2 < length1 then
   168 				false
   169 			else if needle = String.substring (haystack, 0, length1) then
   170 				true
   171 			else is_substring needle (String.substring (haystack, 1, length2-1))
   172 		end
   173 		(* string list -> int list option *)
   174 		fun parse_lines [] =
   175 			None
   176 		  | parse_lines (line::lines) =
   177 			if is_substring success line then
   178 				(* the next line must be a list of integers *)
   179 				(Some o assignment_from_list o int_list_from_string o hd) lines
   180 			else
   181 				parse_lines lines
   182 	in
   183 		(parse_lines o split_lines o File.read) path
   184 	end;
   185 
   186 (* ------------------------------------------------------------------------- *)
   187 (* make_external_solver: call 'writefn', execute 'cmd', call 'readfn'        *)
   188 (* ------------------------------------------------------------------------- *)
   189 
   190 	(* string -> (prop_formula -> unit) -> (unit -> (int -> bool) option) -> solver *)
   191 
   192 	fun make_external_solver cmd writefn readfn fm =
   193 		(writefn fm;
   194 		assert ((system cmd)=0) ("system command " ^ quote cmd ^ " failed (make sure the SAT solver is installed)");
   195 		readfn ());
   196 
   197 (* ------------------------------------------------------------------------- *)
   198 (* solvers: a (reference to a) table of all registered SAT solvers           *)
   199 (* ------------------------------------------------------------------------- *)
   200 
   201 	val solvers = ref Symtab.empty;
   202 
   203 (* ------------------------------------------------------------------------- *)
   204 (* add_solver: updates 'solvers' by adding a new solver                      *)
   205 (* Note: No two solvers may have the same 'name'; otherwise exception 'DUP'  *)
   206 (*       will be raised.                                                     *)
   207 (* ------------------------------------------------------------------------- *)
   208 
   209 	(* string * solver -> unit *)
   210 
   211 	fun add_solver (name, new_solver) =
   212 		solvers := Symtab.update_new ((name, new_solver), !solvers);
   213 
   214 (* ------------------------------------------------------------------------- *)
   215 (* invoke_solver: returns the solver associated with the given 'name'        *)
   216 (* Note: If no solver is associated with 'name', exception 'OPTION' will be  *)
   217 (*       raised.                                                             *)
   218 (* ------------------------------------------------------------------------- *)
   219 
   220 	(* string -> solver *)
   221 
   222 	fun invoke_solver name =
   223 		(the o Symtab.lookup) (!solvers, name);
   224 
   225 (* ------------------------------------------------------------------------- *)
   226 (* preferred: the name of the preferred SAT solver                           *)
   227 (* ------------------------------------------------------------------------- *)
   228 
   229 	val preferred = ref "";
   230 
   231 end;  (* SatSolver *)
   232 
   233 
   234 (* ------------------------------------------------------------------------- *)
   235 (* Predefined SAT solvers                                                    *)
   236 (* ------------------------------------------------------------------------- *)
   237 
   238 (* ------------------------------------------------------------------------- *)
   239 (* Internal SAT solver, available as 'SatSolver.solve "enumerate"' -- simply *)
   240 (* enumerates assignments until a satisfying assignment is found             *)
   241 (* ------------------------------------------------------------------------- *)
   242 
   243 let
   244 	fun enum_solver fm =
   245 	let
   246 		(* int list *)
   247 		val indices = PropLogic.indices fm
   248 		(* int list -> int list -> int list option *)
   249 		(* binary increment: list 'xs' of current bits, list 'ys' of all bits (lower bits first) *)
   250 		fun next_list _ ([]:int list) =
   251 			None
   252 		  | next_list [] (y::ys) =
   253 			Some [y]
   254 		  | next_list (x::xs) (y::ys) =
   255 			if x=y then
   256 				(* reset the bit, continue *)
   257 				next_list xs ys
   258 			else
   259 				(* set the lowest bit that wasn't set before, keep the higher bits *)
   260 				Some (y::x::xs)
   261 		(* int list -> int -> bool *)
   262 		fun assignment_from_list xs i =
   263 			i mem xs
   264 		(* int list -> (int -> bool) option *)
   265 		fun solver_loop xs =
   266 			if PropLogic.eval (assignment_from_list xs) fm then
   267 				Some (assignment_from_list xs)
   268 			else
   269 				(case next_list xs indices of
   270 				  Some xs' => solver_loop xs'
   271 				| None     => None)
   272 	in
   273 		(* start with the "first" assignment (all variables are interpreted as 'False') *)
   274 		solver_loop []
   275 	end
   276 in
   277 	SatSolver.add_solver ("enumerate", enum_solver)
   278 end;
   279 
   280 (* ------------------------------------------------------------------------- *)
   281 (* Internal SAT solver, available as 'SatSolver.solve "dpll"' -- a simple    *)
   282 (* implementation of the DPLL algorithm (cf. L. Zhang, S. Malik: "The Quest  *)
   283 (* for Efficient Boolean Satisfiability Solvers", July 2002, Fig. 1).        *)
   284 (* ------------------------------------------------------------------------- *)
   285 
   286 let
   287 	local
   288 		open PropLogic
   289 	in
   290 		fun dpll_solver fm =
   291 		let
   292 			(* prop_formula *)
   293 			val defcnf = PropLogic.defcnf fm
   294 			(* int list *)
   295 			val indices = PropLogic.indices defcnf
   296 			(* int list -> int -> prop_formula *)
   297 			fun partial_var_eval []      i = BoolVar i
   298 			  | partial_var_eval (x::xs) i = if x=i then True else if x=(~i) then False else partial_var_eval xs i
   299 			(* int list -> prop_formula -> prop_formula *)
   300 			fun partial_eval xs True             = True
   301 			  | partial_eval xs False            = False
   302 			  | partial_eval xs (BoolVar i)      = partial_var_eval xs i
   303 			  | partial_eval xs (Not fm)         = SNot (partial_eval xs fm)
   304 			  | partial_eval xs (Or (fm1, fm2))  = SOr (partial_eval xs fm1, partial_eval xs fm2)
   305 			  | partial_eval xs (And (fm1, fm2)) = SAnd (partial_eval xs fm1, partial_eval xs fm2)
   306 			(* prop_formula -> int list *)
   307 			fun forced_vars True              = []
   308 			  | forced_vars False             = []
   309 			  | forced_vars (BoolVar i)       = [i]
   310 			  | forced_vars (Not (BoolVar i)) = [~i]
   311 			  | forced_vars (Or (fm1, fm2))   = (forced_vars fm1) inter_int (forced_vars fm2)
   312 			  | forced_vars (And (fm1, fm2))  = (forced_vars fm1) union_int (forced_vars fm2)
   313 			  (* Above, i *and* ~i may be forced.  In this case the first occurrence takes   *)
   314 			  (* precedence, and the next partial evaluation of the formula returns 'False'. *)
   315 			  | forced_vars _                 = raise ERROR  (* formula is not in negation normal form *)
   316 			(* int list -> prop_formula -> (int list * prop_formula) *)
   317 			fun eval_and_force xs fm =
   318 			let
   319 				val fm' = partial_eval xs fm
   320 				val xs' = forced_vars fm'
   321 			in
   322 				if null xs' then
   323 					(xs, fm')
   324 				else
   325 					eval_and_force (xs@xs') fm'  (* xs and xs' should be distinct, so '@' here should have *)
   326 					                             (* the same effect as 'union_int'                         *)
   327 			end
   328 			(* int list -> int option *)
   329 			fun fresh_var xs =
   330 				Library.find_first (fn i => not (i mem_int xs) andalso not ((~i) mem_int xs)) indices
   331 			(* int list -> prop_formula -> int list option *)
   332 			(* partial assignment 'xs' *)
   333 			fun dpll xs fm =
   334 			let
   335 				val (xs', fm') = eval_and_force xs fm
   336 			in
   337 				case fm' of
   338 				  True  => Some xs'
   339 				| False => None
   340 				| _     =>
   341 					let
   342 						val x = the (fresh_var xs')  (* a fresh variable must exist since 'fm' did not evaluate to 'True'/'False' *)
   343 					in
   344 						case dpll (x::xs') fm' of  (* passing fm' rather than fm should save some simplification work *)
   345 						  Some xs'' => Some xs''
   346 						| None      => dpll ((~x)::xs') fm'  (* now try interpreting 'x' as 'False' *)
   347 					end
   348 			end
   349 			(* int list -> int -> bool *)
   350 			fun assignment_from_list [] i =
   351 				false  (* could be 'true' just as well; the DPLL procedure didn't provide a value for this variable *)
   352 			  | assignment_from_list (x::xs) i =
   353 				if x=i then true
   354 				else if x=(~i) then false
   355 				else assignment_from_list xs i
   356 		in
   357 			(* initially, no variable is interpreted yet *)
   358 			apsome assignment_from_list (dpll [] defcnf)
   359 		end
   360 	end  (* local *)
   361 in
   362 	SatSolver.add_solver ("dpll", dpll_solver);
   363 	SatSolver.preferred := "dpll"
   364 end;
   365 
   366 (* ------------------------------------------------------------------------- *)
   367 (* Internal SAT solver, available as 'SatSolver.solve "auto"': uses the      *)
   368 (* preferred solver, or "dpll" if the preferred solver isn't present         *)
   369 (* ------------------------------------------------------------------------- *)
   370 
   371 let
   372 	fun auto_solver fm =
   373 	let
   374 		val preferred = !SatSolver.preferred
   375 		val fallback  = "dpll"
   376 	in
   377 		if preferred="auto" then  (* prevent infinite recursion *)
   378 			(warning ("Preferred SAT solver \"auto\": using solver " ^ quote fallback ^ " instead.");
   379 			SatSolver.invoke_solver fallback fm)
   380 		else if preferred=fallback then
   381 			(warning ("Preferred SAT solver is " ^ quote fallback ^ "; for better performance, consider using an external solver.");
   382 			SatSolver.invoke_solver fallback fm)
   383 		else
   384 			(SatSolver.invoke_solver preferred fm
   385 			handle OPTION =>
   386 				(warning ("Preferred SAT solver " ^ quote preferred ^ " not installed; using solver " ^ quote fallback ^ " instead.");
   387 				SatSolver.invoke_solver fallback fm))
   388 	end
   389 in
   390 	SatSolver.add_solver ("auto", auto_solver)
   391 end;
   392 
   393 (* ------------------------------------------------------------------------- *)
   394 (* ZChaff, Version 2003.12.04                                                *)
   395 (* ------------------------------------------------------------------------- *)
   396 
   397 if getenv "ZCHAFF_HOME" <> "" then
   398 	let
   399 		val inname     = "isabelle.cnf"
   400 		val outname    = "result"
   401 		val inpath     = File.tmp_path (Path.unpack inname)
   402 		val outpath    = File.tmp_path (Path.unpack outname)
   403 		val cmd        = (getenv "ZCHAFF_HOME") ^ "/zchaff " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
   404 		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath fm
   405 		fun readfn ()  = SatSolver.parse_std_result_file outpath "Verify Solution successful. Instance satisfiable"
   406 		fun zchaff fm =
   407 		let
   408 			val _          = assert (not (File.exists inpath)) ("file " ^ quote (Path.pack inpath) ^ " exists, please delete (will not overwrite)")
   409 			val _          = assert (not (File.exists outpath)) ("file " ^ quote (Path.pack outpath) ^ " exists, please delete (will not overwrite)")
   410 			val assignment = SatSolver.make_external_solver cmd writefn readfn fm
   411 			val _          = (File.rm inpath handle _ => ())
   412 			val _          = (File.rm outpath handle _ => ())
   413 		in
   414 			assignment
   415 		end
   416 	in
   417 		SatSolver.add_solver ("zchaff", zchaff);
   418 		SatSolver.preferred := "zchaff"
   419 	end
   420 else
   421 	();
   422 
   423 (* ------------------------------------------------------------------------- *)
   424 (* Add code for other SAT solvers below.                                     *)
   425 (* ------------------------------------------------------------------------- *)
   426 
   427 (*
   428 if mysolver_is_installed then
   429 	let
   430 		fun mysolver fm = ...
   431 	in
   432 		SatSolver.add_solver ("myname", mysolver);  -- register the solver
   433 		SatSolver.preferred := "myname"             -- make it the preferred solver (optional)
   434 	end
   435 else
   436 	();
   437 
   438 -- the solver is now available as SatSolver.invoke_solver "myname"
   439 *)