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