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