src/HOL/Tools/sat_solver.ML
author wenzelm
Sat Jan 21 23:02:14 2006 +0100 (2006-01-21)
changeset 18728 6790126ab5f6
parent 18678 dd0c569fa43d
child 19190 7c311c513bae
permissions -rw-r--r--
simplified type attribute;
webertj@14453
     1
(*  Title:      HOL/Tools/sat_solver.ML
webertj@14453
     2
    ID:         $Id$
webertj@14453
     3
    Author:     Tjark Weber
webertj@16911
     4
    Copyright   2004-2005
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@14965
    11
	exception NOT_CONFIGURED
webertj@14453
    12
webertj@14965
    13
	type assignment = int -> bool option
webertj@17493
    14
	type proof      = int list Inttab.table * int
webertj@14965
    15
	datatype result = SATISFIABLE of assignment
webertj@17493
    16
	                | UNSATISFIABLE of proof option
webertj@14965
    17
	                | UNKNOWN
webertj@14965
    18
	type solver     = PropLogic.prop_formula -> result
webertj@14965
    19
webertj@14965
    20
	(* auxiliary functions to create external SAT solvers *)
webertj@14453
    21
	val write_dimacs_cnf_file : Path.T -> PropLogic.prop_formula -> unit
webertj@14453
    22
	val write_dimacs_sat_file : Path.T -> PropLogic.prop_formula -> unit
webertj@17620
    23
	val read_std_result_file  : Path.T -> string * string * string -> result
webertj@14965
    24
	val make_external_solver  : string -> (PropLogic.prop_formula -> unit) -> (unit -> result) -> solver
webertj@14453
    25
webertj@15040
    26
	val read_dimacs_cnf_file : Path.T -> PropLogic.prop_formula
webertj@15040
    27
webertj@16899
    28
	(* generic solver interface *)
webertj@14487
    29
	val solvers       : (string * solver) list ref
webertj@14487
    30
	val add_solver    : string * solver -> unit
webertj@15605
    31
	val invoke_solver : string -> solver  (* exception Option *)
webertj@14453
    32
end;
webertj@14453
    33
webertj@14453
    34
structure SatSolver : SAT_SOLVER =
webertj@14453
    35
struct
webertj@14453
    36
webertj@14453
    37
	open PropLogic;
webertj@14453
    38
webertj@14453
    39
(* ------------------------------------------------------------------------- *)
webertj@14965
    40
(* should be raised by an external SAT solver to indicate that the solver is *)
webertj@14965
    41
(* not configured properly                                                   *)
webertj@14965
    42
(* ------------------------------------------------------------------------- *)
webertj@14965
    43
webertj@14965
    44
	exception NOT_CONFIGURED;
webertj@14965
    45
webertj@14965
    46
(* ------------------------------------------------------------------------- *)
skalberg@15531
    47
(* type of partial (satisfying) assignments: 'a i = NONE' means that 'a' is  *)
webertj@14965
    48
(*      a satisfying assigment regardless of the value of variable 'i'       *)
webertj@14453
    49
(* ------------------------------------------------------------------------- *)
webertj@14453
    50
webertj@14965
    51
	type assignment = int -> bool option;
webertj@14965
    52
webertj@14965
    53
(* ------------------------------------------------------------------------- *)
webertj@17493
    54
(* a proof of unsatisfiability, to be interpreted as follows: each integer   *)
webertj@17493
    55
(*      is a clause ID, each list 'xs' stored under the key 'x' in the table *)
webertj@17493
    56
(*      contains the IDs of clauses that must be resolved (in the given      *)
webertj@17493
    57
(*      order) to obtain the new clause 'x'.  Each list 'xs' must be         *)
webertj@17493
    58
(*      non-empty, and the literal to be resolved upon must always be unique *)
webertj@17494
    59
(*      (e.g. "A | ~B" must not be resolved with "~A | B").  Circular        *)
webertj@17493
    60
(*      dependencies of clauses are not allowed.  (At least) one of the      *)
webertj@17493
    61
(*      clauses in the table must be the empty clause (i.e. contain no       *)
webertj@17493
    62
(*      literals); its ID is given by the second component of the proof.     *)
webertj@17493
    63
(*      The clauses of the original problem passed to the SAT solver have    *)
webertj@17493
    64
(*      consecutive IDs starting with 0.  Clause IDs must be non-negative,   *)
webertj@17493
    65
(*      but do not need to be consecutive.                                   *)
webertj@17493
    66
(* ------------------------------------------------------------------------- *)
webertj@17493
    67
webertj@17493
    68
	type proof = int list Inttab.table * int;
webertj@17493
    69
webertj@17493
    70
(* ------------------------------------------------------------------------- *)
webertj@14965
    71
(* return type of SAT solvers: if the result is 'SATISFIABLE', a satisfying  *)
webertj@17493
    72
(*      assignment must be returned as well; if the result is                *)
webertj@17493
    73
(*      'UNSATISFIABLE', a proof of unsatisfiability may be returned         *)
webertj@14965
    74
(* ------------------------------------------------------------------------- *)
webertj@14965
    75
webertj@14965
    76
	datatype result = SATISFIABLE of assignment
webertj@17493
    77
	                | UNSATISFIABLE of proof option
webertj@14965
    78
	                | UNKNOWN;
webertj@14965
    79
webertj@14965
    80
(* ------------------------------------------------------------------------- *)
webertj@14965
    81
(* type of SAT solvers: given a propositional formula, a satisfying          *)
webertj@14965
    82
(*      assignment may be returned                                           *)
webertj@14965
    83
(* ------------------------------------------------------------------------- *)
webertj@14965
    84
webertj@14965
    85
	type solver = prop_formula -> result;
webertj@14453
    86
webertj@14453
    87
(* ------------------------------------------------------------------------- *)
webertj@14453
    88
(* write_dimacs_cnf_file: serializes a formula 'fm' of propositional logic   *)
webertj@14453
    89
(*      to a file in DIMACS CNF format (see "Satisfiability Suggested        *)
webertj@14453
    90
(*      Format", May 8 1993, Section 2.1)                                    *)
webertj@14453
    91
(* Note: 'fm' must not contain a variable index less than 1.                 *)
webertj@14965
    92
(* Note: 'fm' must be given in CNF.                                          *)
webertj@14453
    93
(* ------------------------------------------------------------------------- *)
webertj@14453
    94
webertj@14453
    95
	(* Path.T -> prop_formula -> unit *)
webertj@14453
    96
webertj@14453
    97
	fun write_dimacs_cnf_file path fm =
webertj@14453
    98
	let
webertj@14453
    99
		(* prop_formula -> prop_formula *)
webertj@14453
   100
		fun cnf_True_False_elim True =
webertj@14453
   101
			Or (BoolVar 1, Not (BoolVar 1))
webertj@14453
   102
		  | cnf_True_False_elim False =
webertj@14453
   103
			And (BoolVar 1, Not (BoolVar 1))
webertj@14453
   104
		  | cnf_True_False_elim fm =
webertj@14453
   105
			fm  (* since 'fm' is in CNF, either 'fm'='True'/'False', or 'fm' does not contain 'True'/'False' at all *)
webertj@14453
   106
		(* prop_formula -> int *)
webertj@14453
   107
		fun cnf_number_of_clauses (And (fm1,fm2)) =
webertj@14453
   108
			(cnf_number_of_clauses fm1) + (cnf_number_of_clauses fm2)
webertj@14453
   109
		  | cnf_number_of_clauses _ =
webertj@14453
   110
			1
webertj@16915
   111
		(* prop_formula -> string list *)
webertj@14999
   112
		fun cnf_string fm =
webertj@14999
   113
		let
webertj@14999
   114
			(* prop_formula -> string list -> string list *)
webertj@14999
   115
			fun cnf_string_acc True acc =
webertj@14999
   116
				error "formula is not in CNF"
webertj@14999
   117
			  | cnf_string_acc False acc =
webertj@14999
   118
				error "formula is not in CNF"
webertj@14999
   119
			  | cnf_string_acc (BoolVar i) acc =
webertj@14999
   120
				(assert (i>=1) "formula contains a variable index less than 1";
webertj@14999
   121
				string_of_int i :: acc)
webertj@14999
   122
			  | cnf_string_acc (Not (BoolVar i)) acc =
webertj@14999
   123
				"-" :: cnf_string_acc (BoolVar i) acc
webertj@14999
   124
			  | cnf_string_acc (Not _) acc =
webertj@14999
   125
				error "formula is not in CNF"
webertj@14999
   126
			  | cnf_string_acc (Or (fm1,fm2)) acc =
webertj@14999
   127
				cnf_string_acc fm1 (" " :: cnf_string_acc fm2 acc)
webertj@14999
   128
			  | cnf_string_acc (And (fm1,fm2)) acc =
webertj@14999
   129
				cnf_string_acc fm1 (" 0\n" :: cnf_string_acc fm2 acc)
webertj@14999
   130
		in
webertj@16915
   131
			cnf_string_acc fm []
webertj@14999
   132
		end
webertj@16915
   133
		val fm'               = cnf_True_False_elim fm
webertj@16915
   134
		val number_of_vars    = maxidx fm'
webertj@16915
   135
		val number_of_clauses = cnf_number_of_clauses fm'
webertj@14453
   136
	in
webertj@14453
   137
		File.write path
webertj@16915
   138
			("c This file was generated by SatSolver.write_dimacs_cnf_file\n" ^
webertj@16915
   139
			 "p cnf " ^ string_of_int number_of_vars ^ " " ^ string_of_int number_of_clauses ^ "\n");
webertj@16915
   140
		File.append_list path
webertj@16915
   141
			(cnf_string fm');
webertj@16915
   142
		File.append path
webertj@16915
   143
			" 0\n"
webertj@14453
   144
	end;
webertj@14453
   145
webertj@14453
   146
(* ------------------------------------------------------------------------- *)
webertj@14453
   147
(* write_dimacs_sat_file: serializes a formula 'fm' of propositional logic   *)
webertj@14453
   148
(*      to a file in DIMACS SAT format (see "Satisfiability Suggested        *)
webertj@14453
   149
(*      Format", May 8 1993, Section 2.2)                                    *)
webertj@14453
   150
(* Note: 'fm' must not contain a variable index less than 1.                 *)
webertj@14453
   151
(* ------------------------------------------------------------------------- *)
webertj@14453
   152
webertj@14453
   153
	(* Path.T -> prop_formula -> unit *)
webertj@14453
   154
webertj@14453
   155
	fun write_dimacs_sat_file path fm =
webertj@14453
   156
	let
webertj@16915
   157
		(* prop_formula -> string list *)
webertj@14999
   158
		fun sat_string fm =
webertj@14999
   159
		let
webertj@14999
   160
			(* prop_formula -> string list -> string list *)
webertj@14999
   161
			fun sat_string_acc True acc =
webertj@14999
   162
				"*()" :: acc
webertj@14999
   163
			  | sat_string_acc False acc =
webertj@14999
   164
				"+()" :: acc
webertj@14999
   165
			  | sat_string_acc (BoolVar i) acc =
webertj@14999
   166
				(assert (i>=1) "formula contains a variable index less than 1";
webertj@14999
   167
				string_of_int i :: acc)
webertj@16899
   168
			  | sat_string_acc (Not (BoolVar i)) acc =
webertj@16899
   169
				"-" :: sat_string_acc (BoolVar i) acc
webertj@14999
   170
			  | sat_string_acc (Not fm) acc =
webertj@14999
   171
				"-(" :: sat_string_acc fm (")" :: acc)
webertj@14999
   172
			  | sat_string_acc (Or (fm1,fm2)) acc =
webertj@16899
   173
				"+(" :: sat_string_acc_or fm1 (" " :: sat_string_acc_or fm2 (")" :: acc))
webertj@14999
   174
			  | sat_string_acc (And (fm1,fm2)) acc =
webertj@16899
   175
				"*(" :: sat_string_acc_and fm1 (" " :: sat_string_acc_and fm2 (")" :: acc))
webertj@16899
   176
			(* optimization to make use of n-ary disjunction/conjunction *)
webertj@16899
   177
			(* prop_formula -> string list -> string list *)
webertj@16899
   178
			and sat_string_acc_or (Or (fm1,fm2)) acc =
webertj@16899
   179
				sat_string_acc_or fm1 (" " :: sat_string_acc_or fm2 acc)
webertj@16899
   180
			  | sat_string_acc_or fm acc =
webertj@16899
   181
				sat_string_acc fm acc
webertj@16899
   182
			(* prop_formula -> string list -> string list *)
webertj@16899
   183
			and sat_string_acc_and (And (fm1,fm2)) acc =
webertj@16899
   184
				sat_string_acc_and fm1 (" " :: sat_string_acc_and fm2 acc)
webertj@16899
   185
			  | sat_string_acc_and fm acc =
webertj@16899
   186
				sat_string_acc fm acc
webertj@14999
   187
		in
webertj@16915
   188
			sat_string_acc fm []
webertj@14999
   189
		end
webertj@16915
   190
		val number_of_vars = Int.max (maxidx fm, 1)
webertj@14453
   191
	in
webertj@14453
   192
		File.write path
webertj@16915
   193
			("c This file was generated by SatSolver.write_dimacs_sat_file\n" ^
webertj@16915
   194
			 "p sat " ^ string_of_int number_of_vars ^ "\n" ^
webertj@16915
   195
			 "(");
webertj@16915
   196
		File.append_list path
webertj@16915
   197
			(sat_string fm);
webertj@16915
   198
		File.append path
webertj@16915
   199
			")\n"
webertj@14453
   200
	end;
webertj@14453
   201
webertj@14453
   202
(* ------------------------------------------------------------------------- *)
webertj@17620
   203
(* read_std_result_file: scans a SAT solver's output file for a satisfying   *)
webertj@14965
   204
(*      variable assignment.  Returns the assignment, or 'UNSATISFIABLE' if  *)
webertj@14965
   205
(*      the file contains 'unsatisfiable', or 'UNKNOWN' if the file contains *)
webertj@14965
   206
(*      neither 'satisfiable' nor 'unsatisfiable'.  Empty lines are ignored. *)
webertj@14965
   207
(*      The assignment must be given in one or more lines immediately after  *)
webertj@14965
   208
(*      the line that contains 'satisfiable'.  These lines must begin with   *)
webertj@14965
   209
(*      'assignment_prefix'.  Variables must be separated by " ".  Non-      *)
webertj@14965
   210
(*      integer strings are ignored.  If variable i is contained in the      *)
webertj@14965
   211
(*      assignment, then i is interpreted as 'true'.  If ~i is contained in  *)
webertj@14965
   212
(*      the assignment, then i is interpreted as 'false'.  Otherwise the     *)
webertj@14965
   213
(*      value of i is taken to be unspecified.                               *)
webertj@14453
   214
(* ------------------------------------------------------------------------- *)
webertj@14453
   215
webertj@14965
   216
	(* Path.T -> string * string * string -> result *)
webertj@14453
   217
webertj@17620
   218
	fun read_std_result_file path (satisfiable, assignment_prefix, unsatisfiable) =
webertj@14453
   219
	let
webertj@14453
   220
		(* string -> int list *)
webertj@14453
   221
		fun int_list_from_string s =
webertj@15605
   222
			List.mapPartial Int.fromString (space_explode " " s)
webertj@14965
   223
		(* int list -> assignment *)
webertj@14453
   224
		fun assignment_from_list [] i =
skalberg@15531
   225
			NONE  (* the SAT solver didn't provide a value for this variable *)
webertj@14453
   226
		  | assignment_from_list (x::xs) i =
skalberg@15531
   227
			if x=i then (SOME true)
skalberg@15531
   228
			else if x=(~i) then (SOME false)
webertj@14453
   229
			else assignment_from_list xs i
webertj@14965
   230
		(* int list -> string list -> assignment *)
webertj@14965
   231
		fun parse_assignment xs [] =
webertj@14965
   232
			assignment_from_list xs
webertj@14965
   233
		  | parse_assignment xs (line::lines) =
webertj@14965
   234
			if String.isPrefix assignment_prefix line then
webertj@14965
   235
				parse_assignment (xs @ int_list_from_string line) lines
webertj@14965
   236
			else
webertj@14965
   237
				assignment_from_list xs
webertj@14453
   238
		(* string -> string -> bool *)
webertj@14453
   239
		fun is_substring needle haystack =
webertj@14453
   240
		let
webertj@14453
   241
			val length1 = String.size needle
webertj@14453
   242
			val length2 = String.size haystack
webertj@14453
   243
		in
webertj@14453
   244
			if length2 < length1 then
webertj@14453
   245
				false
webertj@14453
   246
			else if needle = String.substring (haystack, 0, length1) then
webertj@14453
   247
				true
webertj@14453
   248
			else is_substring needle (String.substring (haystack, 1, length2-1))
webertj@14453
   249
		end
webertj@14965
   250
		(* string list -> result *)
webertj@14453
   251
		fun parse_lines [] =
webertj@14965
   252
			UNKNOWN
webertj@14453
   253
		  | parse_lines (line::lines) =
webertj@14965
   254
			if is_substring satisfiable line then
webertj@14965
   255
				SATISFIABLE (parse_assignment [] lines)
webertj@14965
   256
			else if is_substring unsatisfiable line then
webertj@17493
   257
				UNSATISFIABLE NONE
webertj@14453
   258
			else
webertj@14453
   259
				parse_lines lines
webertj@14453
   260
	in
skalberg@15570
   261
		(parse_lines o (List.filter (fn l => l <> "")) o split_lines o File.read) path
webertj@14453
   262
	end;
webertj@14453
   263
webertj@14453
   264
(* ------------------------------------------------------------------------- *)
webertj@14453
   265
(* make_external_solver: call 'writefn', execute 'cmd', call 'readfn'        *)
webertj@14453
   266
(* ------------------------------------------------------------------------- *)
webertj@14453
   267
webertj@14965
   268
	(* string -> (PropLogic.prop_formula -> unit) -> (unit -> result) -> solver *)
webertj@14453
   269
webertj@14453
   270
	fun make_external_solver cmd writefn readfn fm =
webertj@14965
   271
		(writefn fm; system cmd; readfn ());
webertj@14453
   272
webertj@14453
   273
(* ------------------------------------------------------------------------- *)
webertj@15040
   274
(* read_dimacs_cnf_file: returns a propositional formula that corresponds to *)
webertj@15040
   275
(*      a SAT problem given in DIMACS CNF format                             *)
webertj@15040
   276
(* ------------------------------------------------------------------------- *)
webertj@15040
   277
webertj@15040
   278
	(* Path.T -> PropLogic.prop_formula *)
webertj@15040
   279
webertj@15040
   280
	fun read_dimacs_cnf_file path =
webertj@15040
   281
	let
webertj@15040
   282
		(* string list -> string list *)
webertj@15040
   283
		fun filter_preamble [] =
webertj@15040
   284
			error "problem line not found in DIMACS CNF file"
webertj@15040
   285
		  | filter_preamble (line::lines) =
webertj@15040
   286
			if String.isPrefix "c " line then
webertj@15040
   287
				(* ignore comments *)
webertj@15040
   288
				filter_preamble lines
webertj@15040
   289
			else if String.isPrefix "p " line then
webertj@15040
   290
				(* ignore the problem line (which must be the last line of the preamble) *)
webertj@15040
   291
				(* Ignoring the problem line implies that if the file contains more clauses *)
webertj@15040
   292
				(* or variables than specified in its preamble, we will accept it anyway.   *)
webertj@15040
   293
				lines
webertj@15040
   294
			else
webertj@15040
   295
				error "preamble in DIMACS CNF file contains a line that does not begin with \"c \" or \"p \""
webertj@15040
   296
		(* string -> int *)
webertj@15040
   297
		fun int_from_string s =
webertj@15040
   298
			case Int.fromString s of
webertj@15040
   299
			  SOME i => i
webertj@15040
   300
			| NONE   => error ("token " ^ quote s ^ "in DIMACS CNF file is not a number")
webertj@15040
   301
		(* int list -> int list list *)
webertj@15040
   302
		fun clauses xs =
webertj@15040
   303
			let
webertj@15040
   304
				val (xs1, xs2) = take_prefix (fn i => i <> 0) xs
webertj@15040
   305
			in
webertj@15128
   306
				case xs2 of
webertj@15128
   307
				  []      => [xs1]
webertj@15128
   308
				| (0::[]) => [xs1]
webertj@15128
   309
				| (0::tl) => xs1 :: clauses tl
wenzelm@18678
   310
				| _       => sys_error "this cannot happen"
webertj@15040
   311
			end
webertj@15040
   312
		(* int -> PropLogic.prop_formula *)
webertj@15040
   313
		fun literal_from_int i =
webertj@15040
   314
			(assert (i<>0) "variable index in DIMACS CNF file is 0";
webertj@15040
   315
			if i>0 then
webertj@15040
   316
				PropLogic.BoolVar i
webertj@15040
   317
			else
webertj@15040
   318
				PropLogic.Not (PropLogic.BoolVar (~i)))
webertj@15040
   319
		(* PropLogic.prop_formula list -> PropLogic.prop_formula *)
webertj@15040
   320
		fun disjunction [] =
webertj@15040
   321
			error "empty clause in DIMACS CNF file"
webertj@15040
   322
		  | disjunction (x::xs) =
webertj@15040
   323
			(case xs of
webertj@15040
   324
			  [] => x
webertj@15040
   325
			| _  => PropLogic.Or (x, disjunction xs))
webertj@15040
   326
		(* PropLogic.prop_formula list -> PropLogic.prop_formula *)
webertj@15040
   327
		fun conjunction [] =
webertj@15040
   328
			error "no clause in DIMACS CNF file"
webertj@15040
   329
		  | conjunction (x::xs) =
webertj@15040
   330
			(case xs of
webertj@15040
   331
			  [] => x
webertj@15040
   332
			| _  => PropLogic.And (x, conjunction xs))
webertj@15040
   333
	in
webertj@15040
   334
		(conjunction
webertj@15040
   335
		o (map disjunction)
webertj@15040
   336
		o (map (map literal_from_int))
webertj@15040
   337
		o clauses
webertj@15040
   338
		o (map int_from_string)
skalberg@15570
   339
		o List.concat
webertj@15040
   340
		o (map (String.fields (fn c => c mem [#" ", #"\t", #"\n"])))
webertj@15040
   341
		o filter_preamble
skalberg@15570
   342
		o (List.filter (fn l => l <> ""))
webertj@15040
   343
		o split_lines
webertj@15040
   344
		o File.read)
webertj@15040
   345
			path
webertj@15040
   346
	end;
webertj@15040
   347
webertj@15040
   348
(* ------------------------------------------------------------------------- *)
webertj@14453
   349
(* solvers: a (reference to a) table of all registered SAT solvers           *)
webertj@14453
   350
(* ------------------------------------------------------------------------- *)
webertj@14453
   351
webertj@14487
   352
	val solvers = ref ([] : (string * solver) list);
webertj@14453
   353
webertj@14453
   354
(* ------------------------------------------------------------------------- *)
webertj@14453
   355
(* add_solver: updates 'solvers' by adding a new solver                      *)
webertj@14453
   356
(* ------------------------------------------------------------------------- *)
webertj@14453
   357
webertj@14453
   358
	(* string * solver -> unit *)
webertj@14453
   359
webertj@14453
   360
	fun add_solver (name, new_solver) =
haftmann@17541
   361
		(solvers := update_warn (op =) ("SAT solver " ^ quote name ^ " was defined before") (name, new_solver) (!solvers));
webertj@14453
   362
webertj@14453
   363
(* ------------------------------------------------------------------------- *)
webertj@14453
   364
(* invoke_solver: returns the solver associated with the given 'name'        *)
webertj@15605
   365
(* Note: If no solver is associated with 'name', exception 'Option' will be  *)
webertj@14453
   366
(*       raised.                                                             *)
webertj@14453
   367
(* ------------------------------------------------------------------------- *)
webertj@14453
   368
webertj@14453
   369
	(* string -> solver *)
webertj@14453
   370
webertj@14453
   371
	fun invoke_solver name =
haftmann@17541
   372
		(the o AList.lookup (op =) (!solvers)) name;
webertj@14453
   373
webertj@14453
   374
end;  (* SatSolver *)
webertj@14453
   375
webertj@14453
   376
webertj@14453
   377
(* ------------------------------------------------------------------------- *)
webertj@14453
   378
(* Predefined SAT solvers                                                    *)
webertj@14453
   379
(* ------------------------------------------------------------------------- *)
webertj@14453
   380
webertj@14453
   381
(* ------------------------------------------------------------------------- *)
webertj@14753
   382
(* Internal SAT solver, available as 'SatSolver.invoke_solver "enumerate"'   *)
webertj@14753
   383
(* -- simply enumerates assignments until a satisfying assignment is found   *)
webertj@14453
   384
(* ------------------------------------------------------------------------- *)
webertj@14453
   385
webertj@14453
   386
let
webertj@14453
   387
	fun enum_solver fm =
webertj@14453
   388
	let
webertj@14453
   389
		(* int list *)
webertj@14453
   390
		val indices = PropLogic.indices fm
webertj@14453
   391
		(* int list -> int list -> int list option *)
webertj@14453
   392
		(* binary increment: list 'xs' of current bits, list 'ys' of all bits (lower bits first) *)
webertj@14453
   393
		fun next_list _ ([]:int list) =
skalberg@15531
   394
			NONE
webertj@14453
   395
		  | next_list [] (y::ys) =
skalberg@15531
   396
			SOME [y]
webertj@14453
   397
		  | next_list (x::xs) (y::ys) =
webertj@14453
   398
			if x=y then
webertj@14453
   399
				(* reset the bit, continue *)
webertj@14453
   400
				next_list xs ys
webertj@14453
   401
			else
webertj@14453
   402
				(* set the lowest bit that wasn't set before, keep the higher bits *)
skalberg@15531
   403
				SOME (y::x::xs)
webertj@14453
   404
		(* int list -> int -> bool *)
webertj@14453
   405
		fun assignment_from_list xs i =
webertj@14453
   406
			i mem xs
webertj@14965
   407
		(* int list -> SatSolver.result *)
webertj@14453
   408
		fun solver_loop xs =
webertj@14453
   409
			if PropLogic.eval (assignment_from_list xs) fm then
skalberg@15531
   410
				SatSolver.SATISFIABLE (SOME o (assignment_from_list xs))
webertj@14453
   411
			else
webertj@14453
   412
				(case next_list xs indices of
skalberg@15531
   413
				  SOME xs' => solver_loop xs'
webertj@17493
   414
				| NONE     => SatSolver.UNSATISFIABLE NONE)
webertj@14453
   415
	in
webertj@14965
   416
		(* start with the "first" assignment (all variables are interpreted as 'false') *)
webertj@14453
   417
		solver_loop []
webertj@14453
   418
	end
webertj@14453
   419
in
webertj@14965
   420
	SatSolver.add_solver ("enumerate", enum_solver)
webertj@14453
   421
end;
webertj@14453
   422
webertj@14453
   423
(* ------------------------------------------------------------------------- *)
webertj@14753
   424
(* Internal SAT solver, available as 'SatSolver.invoke_solver "dpll"' -- a   *)
webertj@14753
   425
(* simple implementation of the DPLL algorithm (cf. L. Zhang, S. Malik: "The *)
webertj@14753
   426
(* Quest for Efficient Boolean Satisfiability Solvers", July 2002, Fig. 1).  *)
webertj@14453
   427
(* ------------------------------------------------------------------------- *)
webertj@14453
   428
webertj@14453
   429
let
webertj@14453
   430
	local
webertj@14453
   431
		open PropLogic
webertj@14453
   432
	in
webertj@14453
   433
		fun dpll_solver fm =
webertj@14453
   434
		let
webertj@16914
   435
			(* We could use 'PropLogic.defcnf fm' instead of 'PropLogic.nnf fm' *)
webertj@17493
   436
			(* but that sometimes leads to worse performance due to the         *)
webertj@17493
   437
			(* introduction of additional variables.                            *)
webertj@14514
   438
			val fm' = PropLogic.nnf fm
webertj@14453
   439
			(* int list *)
webertj@14514
   440
			val indices = PropLogic.indices fm'
webertj@14453
   441
			(* int list -> int -> prop_formula *)
webertj@14453
   442
			fun partial_var_eval []      i = BoolVar i
webertj@14453
   443
			  | 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
   444
			(* int list -> prop_formula -> prop_formula *)
webertj@14453
   445
			fun partial_eval xs True             = True
webertj@14453
   446
			  | partial_eval xs False            = False
webertj@14453
   447
			  | partial_eval xs (BoolVar i)      = partial_var_eval xs i
webertj@14453
   448
			  | partial_eval xs (Not fm)         = SNot (partial_eval xs fm)
webertj@14453
   449
			  | partial_eval xs (Or (fm1, fm2))  = SOr (partial_eval xs fm1, partial_eval xs fm2)
webertj@14453
   450
			  | partial_eval xs (And (fm1, fm2)) = SAnd (partial_eval xs fm1, partial_eval xs fm2)
webertj@14453
   451
			(* prop_formula -> int list *)
webertj@14453
   452
			fun forced_vars True              = []
webertj@14453
   453
			  | forced_vars False             = []
webertj@14453
   454
			  | forced_vars (BoolVar i)       = [i]
webertj@14453
   455
			  | forced_vars (Not (BoolVar i)) = [~i]
webertj@14453
   456
			  | forced_vars (Or (fm1, fm2))   = (forced_vars fm1) inter_int (forced_vars fm2)
webertj@14453
   457
			  | forced_vars (And (fm1, fm2))  = (forced_vars fm1) union_int (forced_vars fm2)
webertj@14453
   458
			  (* Above, i *and* ~i may be forced.  In this case the first occurrence takes   *)
webertj@14453
   459
			  (* precedence, and the next partial evaluation of the formula returns 'False'. *)
wenzelm@18678
   460
			  | forced_vars _                 = error "formula is not in negation normal form"
webertj@14453
   461
			(* int list -> prop_formula -> (int list * prop_formula) *)
webertj@14453
   462
			fun eval_and_force xs fm =
webertj@14453
   463
			let
webertj@14453
   464
				val fm' = partial_eval xs fm
webertj@14453
   465
				val xs' = forced_vars fm'
webertj@14453
   466
			in
webertj@14453
   467
				if null xs' then
webertj@14453
   468
					(xs, fm')
webertj@14453
   469
				else
webertj@14453
   470
					eval_and_force (xs@xs') fm'  (* xs and xs' should be distinct, so '@' here should have *)
webertj@14453
   471
					                             (* the same effect as 'union_int'                         *)
webertj@14453
   472
			end
webertj@14453
   473
			(* int list -> int option *)
webertj@14453
   474
			fun fresh_var xs =
webertj@14453
   475
				Library.find_first (fn i => not (i mem_int xs) andalso not ((~i) mem_int xs)) indices
webertj@14453
   476
			(* int list -> prop_formula -> int list option *)
webertj@14453
   477
			(* partial assignment 'xs' *)
webertj@14453
   478
			fun dpll xs fm =
webertj@14453
   479
			let
webertj@14453
   480
				val (xs', fm') = eval_and_force xs fm
webertj@14453
   481
			in
webertj@14453
   482
				case fm' of
skalberg@15531
   483
				  True  => SOME xs'
skalberg@15531
   484
				| False => NONE
webertj@14453
   485
				| _     =>
webertj@14453
   486
					let
skalberg@15570
   487
						val x = valOf (fresh_var xs')  (* a fresh variable must exist since 'fm' did not evaluate to 'True'/'False' *)
webertj@14453
   488
					in
webertj@14453
   489
						case dpll (x::xs') fm' of  (* passing fm' rather than fm should save some simplification work *)
skalberg@15531
   490
						  SOME xs'' => SOME xs''
skalberg@15531
   491
						| NONE      => dpll ((~x)::xs') fm'  (* now try interpreting 'x' as 'False' *)
webertj@14453
   492
					end
webertj@14453
   493
			end
webertj@14965
   494
			(* int list -> assignment *)
webertj@14453
   495
			fun assignment_from_list [] i =
skalberg@15531
   496
				NONE  (* the DPLL procedure didn't provide a value for this variable *)
webertj@14453
   497
			  | assignment_from_list (x::xs) i =
skalberg@15531
   498
				if x=i then (SOME true)
skalberg@15531
   499
				else if x=(~i) then (SOME false)
webertj@14453
   500
				else assignment_from_list xs i
webertj@14453
   501
		in
webertj@14453
   502
			(* initially, no variable is interpreted yet *)
webertj@14965
   503
			case dpll [] fm' of
skalberg@15531
   504
			  SOME assignment => SatSolver.SATISFIABLE (assignment_from_list assignment)
webertj@17493
   505
			| NONE            => SatSolver.UNSATISFIABLE NONE
webertj@14453
   506
		end
webertj@14453
   507
	end  (* local *)
webertj@14453
   508
in
webertj@14965
   509
	SatSolver.add_solver ("dpll", dpll_solver)
webertj@14453
   510
end;
webertj@14453
   511
webertj@14453
   512
(* ------------------------------------------------------------------------- *)
webertj@14753
   513
(* Internal SAT solver, available as 'SatSolver.invoke_solver "auto"': uses  *)
webertj@17577
   514
(* the last installed solver (other than "auto" itself) that does not raise  *)
webertj@15299
   515
(* 'NOT_CONFIGURED'.  (However, the solver may return 'UNKNOWN'.)            *)
webertj@14453
   516
(* ------------------------------------------------------------------------- *)
webertj@14453
   517
webertj@14453
   518
let
webertj@14453
   519
	fun auto_solver fm =
webertj@14965
   520
	let
webertj@14965
   521
		fun loop [] =
webertj@14965
   522
			SatSolver.UNKNOWN
webertj@14965
   523
		  | loop ((name, solver)::solvers) =
webertj@14487
   524
			if name="auto" then
webertj@14965
   525
				(* do not call solver "auto" from within "auto" *)
webertj@14965
   526
				loop solvers
webertj@15299
   527
			else (
webertj@14965
   528
				(if name="dpll" orelse name="enumerate" then
webertj@14805
   529
					warning ("Using SAT solver " ^ quote name ^ "; for better performance, consider using an external solver.")
webertj@14487
   530
				else
wenzelm@18678
   531
					Output.debug ("Using SAT solver " ^ quote name ^ "."));
webertj@14965
   532
				(* apply 'solver' to 'fm' *)
webertj@15299
   533
				solver fm
webertj@15299
   534
					handle SatSolver.NOT_CONFIGURED => loop solvers
webertj@14965
   535
			)
webertj@14965
   536
	in
webertj@17581
   537
		loop (!SatSolver.solvers)
webertj@14965
   538
	end
webertj@14453
   539
in
webertj@14453
   540
	SatSolver.add_solver ("auto", auto_solver)
webertj@14453
   541
end;
webertj@14453
   542
webertj@14453
   543
(* ------------------------------------------------------------------------- *)
webertj@15332
   544
(* zChaff (http://www.princeton.edu/~chaff/zchaff.html)                      *)
webertj@14453
   545
(* ------------------------------------------------------------------------- *)
webertj@14453
   546
webertj@17493
   547
(* ------------------------------------------------------------------------- *)
webertj@17493
   548
(* 'zchaff_with_proofs' applies the "zchaff" prover to a formula, and if     *)
webertj@17493
   549
(* zChaff finds that the formula is unsatisfiable, a proof of this is read   *)
webertj@17493
   550
(* from a file "resolve_trace" that was generated by zChaff.  See the code   *)
webertj@17493
   551
(* below for the expected format of the "resolve_trace" file.  Aside from    *)
webertj@17493
   552
(* some basic syntactic checks, no verification of the proof is performed.   *)
webertj@17493
   553
(* ------------------------------------------------------------------------- *)
webertj@17493
   554
webertj@17493
   555
(* add "zchaff_with_proofs" _before_ "zchaff" to the available solvers, so   *)
webertj@17493
   556
(* that the latter is preferred by the "auto" solver                         *)
webertj@17493
   557
webertj@17493
   558
let
webertj@17493
   559
	exception INVALID_PROOF of string
webertj@17493
   560
	fun zchaff_with_proofs fm =
webertj@17493
   561
	case SatSolver.invoke_solver "zchaff" fm of
webertj@17493
   562
	  SatSolver.UNSATISFIABLE NONE =>
webertj@17493
   563
		(let
webertj@17493
   564
			(* string list *)
webertj@17493
   565
			val proof_lines = ((split_lines o File.read) (Path.unpack "resolve_trace"))
webertj@17493
   566
				handle _ => raise INVALID_PROOF "Could not read file \"resolve_trace\""
webertj@17493
   567
			(* PropLogic.prop_formula -> int *)
webertj@17493
   568
			fun cnf_number_of_clauses (PropLogic.And (fm1, fm2)) = cnf_number_of_clauses fm1 + cnf_number_of_clauses fm2
webertj@17493
   569
			  | cnf_number_of_clauses _                          = 1
webertj@17493
   570
			(* string -> int *)
webertj@17493
   571
			fun int_from_string s = (
webertj@17493
   572
				case Int.fromString s of
webertj@17493
   573
				  SOME i => i
webertj@17493
   574
				| NONE   => raise INVALID_PROOF ("File format error: number expected (" ^ quote s ^ " encountered).")
webertj@17493
   575
			)
webertj@17493
   576
			(* parse the "resolve_trace" file *)
webertj@17493
   577
			(* int ref *)
webertj@17493
   578
			val clause_offset = ref ~1
webertj@17493
   579
			(* string list -> proof -> proof *)
webertj@17493
   580
			fun process_tokens [] proof =
webertj@17493
   581
				proof
webertj@17621
   582
			  | process_tokens (tok::toks) (clause_table, empty_id) =
webertj@17493
   583
				if tok="CL:" then (
webertj@17493
   584
					case toks of
webertj@17493
   585
					  id::sep::ids =>
webertj@17493
   586
						let
webertj@17493
   587
							val _   = if !clause_offset = ~1 then () else raise INVALID_PROOF ("File format error: \"CL:\" disallowed after \"VAR:\".")
webertj@17621
   588
							val _   = if empty_id = ~1 then () else raise INVALID_PROOF ("File format error: \"CL:\" disallowed after \"CONF:\".")
webertj@17493
   589
							val cid = int_from_string id
webertj@17493
   590
							val _   = if sep = "<=" then () else raise INVALID_PROOF ("File format error: \"<=\" expected (" ^ quote sep ^ " encountered).")
webertj@17493
   591
							val rs  = map int_from_string ids
webertj@17493
   592
						in
webertj@17493
   593
							(* update clause table *)
webertj@17621
   594
							(Inttab.update_new (cid, rs) clause_table, empty_id)
webertj@17493
   595
								handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ id ^ " defined more than once.")
webertj@17493
   596
						end
webertj@17493
   597
					| _ =>
webertj@17493
   598
						raise INVALID_PROOF "File format error: \"CL:\" followed by an insufficient number of tokens."
webertj@17493
   599
				) else if tok="VAR:" then (
webertj@17493
   600
					case toks of
webertj@17493
   601
					  id::levsep::levid::valsep::valid::antesep::anteid::litsep::lits =>
webertj@17493
   602
						let
webertj@17621
   603
							val _   = if empty_id = ~1 then () else raise INVALID_PROOF ("File format error: \"VAR:\" disallowed after \"CONF:\".")
webertj@17493
   604
							(* set 'clause_offset' to the largest used clause ID *)
webertj@17493
   605
							val _   = if !clause_offset = ~1 then clause_offset :=
webertj@17493
   606
								(case Inttab.max_key clause_table of
webertj@17493
   607
								  SOME id => id
webertj@17527
   608
								| NONE    => cnf_number_of_clauses (PropLogic.defcnf fm) - 1  (* the first clause ID is 0, not 1 *))
webertj@17493
   609
								else
webertj@17493
   610
									()
webertj@17493
   611
							val vid = int_from_string id
webertj@17493
   612
							val _   = if levsep = "L:" then () else raise INVALID_PROOF ("File format error: \"L:\" expected (" ^ quote levsep ^ " encountered).")
webertj@17493
   613
							val _   = int_from_string levid
webertj@17493
   614
							val _   = if valsep = "V:" then () else raise INVALID_PROOF ("File format error: \"V:\" expected (" ^ quote valsep ^ " encountered).")
webertj@17493
   615
							val _   = int_from_string valid
webertj@17493
   616
							val _   = if antesep = "A:" then () else raise INVALID_PROOF ("File format error: \"A:\" expected (" ^ quote antesep ^ " encountered).")
webertj@17493
   617
							val aid = int_from_string anteid
webertj@17493
   618
							val _   = if litsep = "Lits:" then () else raise INVALID_PROOF ("File format error: \"Lits:\" expected (" ^ quote litsep ^ " encountered).")
webertj@17493
   619
							val ls  = map int_from_string lits
webertj@17493
   620
							(* convert the data provided by zChaff to our resolution-style proof format *)
webertj@17493
   621
							(* each "VAR:" line defines a unit clause, the resolvents are implicitly    *)
webertj@17493
   622
							(* given by the literals in the antecedent clause                           *)
webertj@17493
   623
							(* we use the sum of '!clause_offset' and the variable ID as clause ID for the unit clause *)
webertj@17493
   624
							val cid = !clause_offset + vid
webertj@17493
   625
							(* the low bit of each literal gives its sign (positive/negative), therefore  *)
webertj@17493
   626
							(* we have to divide each literal by 2 to obtain the proper variable ID; then *)
webertj@17493
   627
							(* we add '!clause_offset' to obtain the ID of the corresponding unit clause  *)
webertj@17493
   628
							val vids = filter (not_equal vid) (map (fn l => l div 2) ls)
webertj@17493
   629
							val rs   = aid :: map (fn v => !clause_offset + v) vids
webertj@17493
   630
						in
webertj@17493
   631
							(* update clause table *)
webertj@17621
   632
							(Inttab.update_new (cid, rs) clause_table, empty_id)
webertj@17527
   633
								handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ string_of_int cid ^ " (derived from antecedent for variable " ^ id ^ ") already defined.")
webertj@17493
   634
						end
webertj@17493
   635
					| _ =>
webertj@17493
   636
						raise INVALID_PROOF "File format error: \"VAR:\" followed by an insufficient number of tokens."
webertj@17493
   637
				) else if tok="CONF:" then (
webertj@17493
   638
					case toks of
webertj@17493
   639
					  id::sep::ids =>
webertj@17493
   640
						let
webertj@17621
   641
							val _   = if empty_id = ~1 then () else raise INVALID_PROOF "File format error: more than one conflicting clause specified."
webertj@17493
   642
							val cid = int_from_string id
webertj@17493
   643
							val _   = if sep = "==" then () else raise INVALID_PROOF ("File format error: \"==\" expected (" ^ quote sep ^ " encountered).")
webertj@17527
   644
							val ls  = map int_from_string ids
webertj@17527
   645
							(* the conflict clause must be resolved with the unit clauses *)
webertj@17527
   646
							(* for its literals to obtain the empty clause                *)
webertj@17621
   647
							val vids         = map (fn l => l div 2) ls
webertj@17621
   648
							val rs           = cid :: map (fn v => !clause_offset + v) vids
webertj@17621
   649
							val new_empty_id = getOpt (Inttab.max_key clause_table, !clause_offset) + 1
webertj@17493
   650
						in
webertj@17527
   651
							(* update clause table and conflict id *)
webertj@17621
   652
							(Inttab.update_new (new_empty_id, rs) clause_table, new_empty_id)
webertj@17621
   653
								handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ string_of_int new_empty_id ^ " (empty clause derived from clause " ^ id ^ ") already defined.")
webertj@17493
   654
						end
webertj@17493
   655
					| _ =>
webertj@17493
   656
						raise INVALID_PROOF "File format error: \"CONF:\" followed by an insufficient number of tokens."
webertj@17493
   657
				) else
webertj@17493
   658
					raise INVALID_PROOF ("File format error: unknown token " ^ quote tok ^ " encountered.")
webertj@17493
   659
			(* string list -> proof -> proof *)
webertj@17493
   660
			fun process_lines [] proof =
webertj@17493
   661
				proof
webertj@17493
   662
			  | process_lines (l::ls) proof =
webertj@17493
   663
				process_lines ls (process_tokens (String.tokens (fn c => c = #" " orelse c = #"\t") l) proof)
webertj@17493
   664
			(* proof *)
webertj@17621
   665
			val (clause_table, empty_id) = process_lines proof_lines (Inttab.empty, ~1)
webertj@17621
   666
			val _                        = if empty_id <> ~1 then () else raise INVALID_PROOF "File format error: no conflicting clause specified."
webertj@17493
   667
		in
webertj@17621
   668
			SatSolver.UNSATISFIABLE (SOME (clause_table, empty_id))
webertj@17493
   669
		end handle INVALID_PROOF reason => (warning reason; SatSolver.UNSATISFIABLE NONE))
webertj@17493
   670
	| result =>
webertj@17493
   671
		result
webertj@17493
   672
in
webertj@17493
   673
	SatSolver.add_solver ("zchaff_with_proofs", zchaff_with_proofs)
webertj@17493
   674
end;
webertj@17493
   675
webertj@14487
   676
let
webertj@14487
   677
	fun zchaff fm =
webertj@14453
   678
	let
webertj@14965
   679
		val _          = if (getenv "ZCHAFF_HOME") = "" then raise SatSolver.NOT_CONFIGURED else ()
webertj@15332
   680
		val _          = if (getenv "ZCHAFF_VERSION") <> "2004.5.13" andalso
webertj@15332
   681
		                    (getenv "ZCHAFF_VERSION") <> "2004.11.15" then raise SatSolver.NOT_CONFIGURED else ()
webertj@15332
   682
			(* both versions of zChaff appear to have the same interface, so we do *)
webertj@15332
   683
			(* not actually need to distinguish between them in the following code *)
webertj@14965
   684
		val inpath     = File.tmp_path (Path.unpack "isabelle.cnf")
webertj@14965
   685
		val outpath    = File.tmp_path (Path.unpack "result")
webertj@14453
   686
		val cmd        = (getenv "ZCHAFF_HOME") ^ "/zchaff " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
webertj@16914
   687
		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm)
webertj@17620
   688
		fun readfn ()  = SatSolver.read_std_result_file outpath ("Instance Satisfiable", "", "Instance Unsatisfiable")
wenzelm@16270
   689
		val _          = if File.exists inpath then warning ("overwriting existing file " ^ quote (Path.pack inpath)) else ()
wenzelm@16270
   690
		val _          = if File.exists outpath then warning ("overwriting existing file " ^ quote (Path.pack outpath)) else ()
webertj@14965
   691
		val result     = SatSolver.make_external_solver cmd writefn readfn fm
wenzelm@16270
   692
		val _          = try File.rm inpath
wenzelm@16270
   693
		val _          = try File.rm outpath
webertj@14453
   694
	in
webertj@14965
   695
		result
webertj@14453
   696
	end
webertj@14487
   697
in
webertj@14965
   698
	SatSolver.add_solver ("zchaff", zchaff)
webertj@14965
   699
end;
webertj@14965
   700
webertj@14965
   701
(* ------------------------------------------------------------------------- *)
webertj@14965
   702
(* BerkMin 561 (http://eigold.tripod.com/BerkMin.html)                       *)
webertj@14965
   703
(* ------------------------------------------------------------------------- *)
webertj@14965
   704
webertj@14965
   705
let
webertj@14965
   706
	fun berkmin fm =
webertj@14965
   707
	let
webertj@14965
   708
		val _          = if (getenv "BERKMIN_HOME") = "" orelse (getenv "BERKMIN_EXE") = "" then raise SatSolver.NOT_CONFIGURED else ()
webertj@14965
   709
		val inpath     = File.tmp_path (Path.unpack "isabelle.cnf")
webertj@14965
   710
		val outpath    = File.tmp_path (Path.unpack "result")
webertj@14965
   711
		val cmd        = (getenv "BERKMIN_HOME") ^ "/" ^ (getenv "BERKMIN_EXE") ^ " " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
webertj@16914
   712
		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm)
webertj@17620
   713
		fun readfn ()  = SatSolver.read_std_result_file outpath ("Satisfiable          !!", "solution =", "UNSATISFIABLE          !!")
wenzelm@16270
   714
		val _          = if File.exists inpath then warning ("overwriting existing file " ^ quote (Path.pack inpath)) else ()
wenzelm@16270
   715
		val _          = if File.exists outpath then warning ("overwriting existing file " ^ quote (Path.pack outpath)) else ()
webertj@14965
   716
		val result     = SatSolver.make_external_solver cmd writefn readfn fm
wenzelm@16270
   717
		val _          = try File.rm inpath
wenzelm@16270
   718
		val _          = try File.rm outpath
webertj@14965
   719
	in
webertj@14965
   720
		result
webertj@14965
   721
	end
webertj@14965
   722
in
webertj@14965
   723
	SatSolver.add_solver ("berkmin", berkmin)
webertj@14965
   724
end;
webertj@14965
   725
webertj@14965
   726
(* ------------------------------------------------------------------------- *)
webertj@14965
   727
(* Jerusat 1.3 (http://www.cs.tau.ac.il/~ale1/)                              *)
webertj@14965
   728
(* ------------------------------------------------------------------------- *)
webertj@14965
   729
webertj@14965
   730
let
webertj@14965
   731
	fun jerusat fm =
webertj@14965
   732
	let
webertj@14965
   733
		val _          = if (getenv "JERUSAT_HOME") = "" then raise SatSolver.NOT_CONFIGURED else ()
webertj@14965
   734
		val inpath     = File.tmp_path (Path.unpack "isabelle.cnf")
webertj@14965
   735
		val outpath    = File.tmp_path (Path.unpack "result")
webertj@14965
   736
		val cmd        = (getenv "JERUSAT_HOME") ^ "/Jerusat1.3 " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath)
webertj@16914
   737
		fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm)
webertj@17620
   738
		fun readfn ()  = SatSolver.read_std_result_file outpath ("s SATISFIABLE", "v ", "s UNSATISFIABLE")
wenzelm@16270
   739
		val _          = if File.exists inpath then warning ("overwriting existing file " ^ quote (Path.pack inpath)) else ()
wenzelm@16270
   740
		val _          = if File.exists outpath then warning ("overwriting existing file " ^ quote (Path.pack outpath)) else ()
webertj@14965
   741
		val result     = SatSolver.make_external_solver cmd writefn readfn fm
wenzelm@16270
   742
		val _          = try File.rm inpath
wenzelm@16270
   743
		val _          = try File.rm outpath
webertj@14965
   744
	in
webertj@14965
   745
		result
webertj@14965
   746
	end
webertj@14965
   747
in
webertj@14965
   748
	SatSolver.add_solver ("jerusat", jerusat)
webertj@14487
   749
end;
webertj@14453
   750
webertj@14453
   751
(* ------------------------------------------------------------------------- *)
webertj@14453
   752
(* Add code for other SAT solvers below.                                     *)
webertj@14453
   753
(* ------------------------------------------------------------------------- *)
webertj@14453
   754
webertj@14453
   755
(*
webertj@14487
   756
let
webertj@14487
   757
	fun mysolver fm = ...
webertj@14487
   758
in
webertj@14965
   759
	SatSolver.add_solver ("myname", (fn fm => if mysolver_is_configured then mysolver fm else raise SatSolver.NOT_CONFIGURED));  -- register the solver
webertj@14487
   760
end;
webertj@14453
   761
webertj@14453
   762
-- the solver is now available as SatSolver.invoke_solver "myname"
webertj@14453
   763
*)