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