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