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