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