| author | blanchet | 
| Fri, 10 Jan 2014 16:18:18 +0100 | |
| changeset 54979 | d7593bfccf25 | 
| parent 52049 | 156e12d5cb92 | 
| child 55436 | 9781e17dcc23 | 
| permissions | -rw-r--r-- | 
| 14453 | 1 | (* Title: HOL/Tools/sat_solver.ML | 
| 2 | Author: Tjark Weber | |
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 3 | Copyright 2004-2009 | 
| 14453 | 4 | |
| 5 | Interface to external SAT solvers, and (simple) built-in SAT solvers. | |
| 51940 | 6 | |
| 7 | Relevant Isabelle environment settings: | |
| 8 | ||
| 9 | # MiniSat 1.14 | |
| 10 | #MINISAT_HOME=/usr/local/bin | |
| 11 | ||
| 12 | # zChaff | |
| 13 | #ZCHAFF_HOME=/usr/local/bin | |
| 14 | ||
| 15 | # BerkMin561 | |
| 16 | #BERKMIN_HOME=/usr/local/bin | |
| 17 | #BERKMIN_EXE=BerkMin561-linux | |
| 18 | #BERKMIN_EXE=BerkMin561-solaris | |
| 19 | ||
| 20 | # Jerusat 1.3 | |
| 21 | #JERUSAT_HOME=/usr/local/bin | |
| 14453 | 22 | *) | 
| 23 | ||
| 24 | signature SAT_SOLVER = | |
| 25 | sig | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 26 | exception NOT_CONFIGURED | 
| 14453 | 27 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 28 | type assignment = int -> bool option | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 29 | type proof = int list Inttab.table * int | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 30 | datatype result = SATISFIABLE of assignment | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 31 | | UNSATISFIABLE of proof option | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 32 | | UNKNOWN | 
| 41471 | 33 | type solver = Prop_Logic.prop_formula -> result | 
| 14965 | 34 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 35 | (* auxiliary functions to create external SAT solvers *) | 
| 41471 | 36 | val write_dimacs_cnf_file : Path.T -> Prop_Logic.prop_formula -> unit | 
| 37 | val write_dimacs_sat_file : Path.T -> Prop_Logic.prop_formula -> unit | |
| 38 | val read_std_result_file : Path.T -> string * string * string -> result | |
| 39 | val make_external_solver : string -> (Prop_Logic.prop_formula -> unit) -> | |
| 40 | (unit -> result) -> solver | |
| 14453 | 41 | |
| 41471 | 42 | val read_dimacs_cnf_file : Path.T -> Prop_Logic.prop_formula | 
| 15040 | 43 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 44 | (* generic solver interface *) | 
| 32740 | 45 | val solvers : (string * solver) list Unsynchronized.ref | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 46 | val add_solver : string * solver -> unit | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 47 | val invoke_solver : string -> solver (* exception Option *) | 
| 14453 | 48 | end; | 
| 49 | ||
| 50 | structure SatSolver : SAT_SOLVER = | |
| 51 | struct | |
| 52 | ||
| 41471 | 53 | open Prop_Logic; | 
| 14453 | 54 | |
| 55 | (* ------------------------------------------------------------------------- *) | |
| 14965 | 56 | (* should be raised by an external SAT solver to indicate that the solver is *) | 
| 57 | (* not configured properly *) | |
| 58 | (* ------------------------------------------------------------------------- *) | |
| 59 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 60 | exception NOT_CONFIGURED; | 
| 14965 | 61 | |
| 62 | (* ------------------------------------------------------------------------- *) | |
| 15531 | 63 | (* type of partial (satisfying) assignments: 'a i = NONE' means that 'a' is *) | 
| 19190 | 64 | (* a satisfying assignment regardless of the value of variable 'i' *) | 
| 14453 | 65 | (* ------------------------------------------------------------------------- *) | 
| 66 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 67 | type assignment = int -> bool option; | 
| 14965 | 68 | |
| 69 | (* ------------------------------------------------------------------------- *) | |
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 70 | (* a proof of unsatisfiability, to be interpreted as follows: each integer *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 71 | (* is a clause ID, each list 'xs' stored under the key 'x' in the table *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 72 | (* contains the IDs of clauses that must be resolved (in the given *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 73 | (* order) to obtain the new clause 'x'. Each list 'xs' must be *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 74 | (* non-empty, and the literal to be resolved upon must always be unique *) | 
| 17494 | 75 | (* (e.g. "A | ~B" must not be resolved with "~A | B"). Circular *) | 
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 76 | (* dependencies of clauses are not allowed. (At least) one of the *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 77 | (* clauses in the table must be the empty clause (i.e. contain no *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 78 | (* literals); its ID is given by the second component of the proof. *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 79 | (* The clauses of the original problem passed to the SAT solver have *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 80 | (* consecutive IDs starting with 0. Clause IDs must be non-negative, *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 81 | (* but do not need to be consecutive. *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 82 | (* ------------------------------------------------------------------------- *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 83 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 84 | type proof = int list Inttab.table * int; | 
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 85 | |
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 86 | (* ------------------------------------------------------------------------- *) | 
| 14965 | 87 | (* return type of SAT solvers: if the result is 'SATISFIABLE', a satisfying *) | 
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 88 | (* assignment must be returned as well; if the result is *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 89 | (* 'UNSATISFIABLE', a proof of unsatisfiability may be returned *) | 
| 14965 | 90 | (* ------------------------------------------------------------------------- *) | 
| 91 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 92 | datatype result = SATISFIABLE of assignment | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 93 | | UNSATISFIABLE of proof option | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 94 | | UNKNOWN; | 
| 14965 | 95 | |
| 96 | (* ------------------------------------------------------------------------- *) | |
| 97 | (* type of SAT solvers: given a propositional formula, a satisfying *) | |
| 98 | (* assignment may be returned *) | |
| 99 | (* ------------------------------------------------------------------------- *) | |
| 100 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 101 | type solver = prop_formula -> result; | 
| 14453 | 102 | |
| 103 | (* ------------------------------------------------------------------------- *) | |
| 104 | (* write_dimacs_cnf_file: serializes a formula 'fm' of propositional logic *) | |
| 105 | (* to a file in DIMACS CNF format (see "Satisfiability Suggested *) | |
| 106 | (* Format", May 8 1993, Section 2.1) *) | |
| 107 | (* Note: 'fm' must not contain a variable index less than 1. *) | |
| 14965 | 108 | (* Note: 'fm' must be given in CNF. *) | 
| 14453 | 109 | (* ------------------------------------------------------------------------- *) | 
| 110 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 111 | (* Path.T -> prop_formula -> unit *) | 
| 14453 | 112 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 113 | fun write_dimacs_cnf_file path fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 114 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 115 | (* prop_formula -> prop_formula *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 116 | fun cnf_True_False_elim True = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 117 | Or (BoolVar 1, Not (BoolVar 1)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 118 | | cnf_True_False_elim False = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 119 | And (BoolVar 1, Not (BoolVar 1)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 120 | | cnf_True_False_elim fm = | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 121 | fm (* since 'fm' is in CNF, either 'fm'='True'/'False', | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 122 | or 'fm' does not contain 'True'/'False' at all *) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 123 | (* prop_formula -> int *) | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 124 | fun cnf_number_of_clauses (And (fm1, fm2)) = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 125 | (cnf_number_of_clauses fm1) + (cnf_number_of_clauses fm2) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 126 | | cnf_number_of_clauses _ = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 127 | 1 | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 128 | (* TextIO.outstream -> unit *) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 129 | fun write_cnf_file out = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 130 | let | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 131 | (* prop_formula -> unit *) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 132 | fun write_formula True = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 133 | error "formula is not in CNF" | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 134 | | write_formula False = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 135 | error "formula is not in CNF" | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 136 | | write_formula (BoolVar i) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 137 | (i>=1 orelse error "formula contains a variable index less than 1"; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 138 | TextIO.output (out, string_of_int i)) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 139 | | write_formula (Not (BoolVar i)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 140 | (TextIO.output (out, "-"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 141 | write_formula (BoolVar i)) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 142 | | write_formula (Not _) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 143 | error "formula is not in CNF" | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 144 | | write_formula (Or (fm1, fm2)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 145 | (write_formula fm1; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 146 | TextIO.output (out, " "); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 147 | write_formula fm2) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 148 | | write_formula (And (fm1, fm2)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 149 | (write_formula fm1; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 150 | TextIO.output (out, " 0\n"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 151 | write_formula fm2) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 152 | val fm' = cnf_True_False_elim fm | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 153 | val number_of_vars = maxidx fm' | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 154 | val number_of_clauses = cnf_number_of_clauses fm' | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 155 | in | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 156 | TextIO.output (out, "c This file was generated by SatSolver.write_dimacs_cnf_file\n"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 157 | TextIO.output (out, "p cnf " ^ string_of_int number_of_vars ^ " " ^ | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 158 | string_of_int number_of_clauses ^ "\n"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 159 | write_formula fm'; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 160 | TextIO.output (out, " 0\n") | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 161 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 162 | in | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 163 | File.open_output write_cnf_file path | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 164 | end; | 
| 14453 | 165 | |
| 166 | (* ------------------------------------------------------------------------- *) | |
| 167 | (* write_dimacs_sat_file: serializes a formula 'fm' of propositional logic *) | |
| 168 | (* to a file in DIMACS SAT format (see "Satisfiability Suggested *) | |
| 169 | (* Format", May 8 1993, Section 2.2) *) | |
| 170 | (* Note: 'fm' must not contain a variable index less than 1. *) | |
| 171 | (* ------------------------------------------------------------------------- *) | |
| 172 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 173 | (* Path.T -> prop_formula -> unit *) | 
| 14453 | 174 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 175 | fun write_dimacs_sat_file path fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 176 | let | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 177 | (* TextIO.outstream -> unit *) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 178 | fun write_sat_file out = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 179 | let | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 180 | (* prop_formula -> unit *) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 181 | fun write_formula True = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 182 | TextIO.output (out, "*()") | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 183 | | write_formula False = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 184 | TextIO.output (out, "+()") | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 185 | | write_formula (BoolVar i) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 186 | (i>=1 orelse error "formula contains a variable index less than 1"; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 187 | TextIO.output (out, string_of_int i)) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 188 | | write_formula (Not (BoolVar i)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 189 | (TextIO.output (out, "-"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 190 | write_formula (BoolVar i)) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 191 | | write_formula (Not fm) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 192 |           (TextIO.output (out, "-(");
 | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 193 | write_formula fm; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 194 | TextIO.output (out, ")")) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 195 | | write_formula (Or (fm1, fm2)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 196 |           (TextIO.output (out, "+(");
 | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 197 | write_formula_or fm1; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 198 | TextIO.output (out, " "); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 199 | write_formula_or fm2; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 200 | TextIO.output (out, ")")) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 201 | | write_formula (And (fm1, fm2)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 202 |           (TextIO.output (out, "*(");
 | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 203 | write_formula_and fm1; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 204 | TextIO.output (out, " "); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 205 | write_formula_and fm2; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 206 | TextIO.output (out, ")")) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 207 | (* optimization to make use of n-ary disjunction/conjunction *) | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 208 | and write_formula_or (Or (fm1, fm2)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 209 | (write_formula_or fm1; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 210 | TextIO.output (out, " "); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 211 | write_formula_or fm2) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 212 | | write_formula_or fm = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 213 | write_formula fm | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 214 | and write_formula_and (And (fm1, fm2)) = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 215 | (write_formula_and fm1; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 216 | TextIO.output (out, " "); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 217 | write_formula_and fm2) | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 218 | | write_formula_and fm = | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 219 | write_formula fm | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 220 | val number_of_vars = Int.max (maxidx fm, 1) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 221 | in | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 222 | TextIO.output (out, "c This file was generated by SatSolver.write_dimacs_sat_file\n"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 223 | TextIO.output (out, "p sat " ^ string_of_int number_of_vars ^ "\n"); | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 224 |       TextIO.output (out, "(");
 | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 225 | write_formula fm; | 
| 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 226 | TextIO.output (out, ")\n") | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 227 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 228 | in | 
| 31219 
034f23104635
write_dimacs_{sat,cnf}_file now write the DIMACS file on the fly, without building it in memory first
 webertj parents: 
30275diff
changeset | 229 | File.open_output write_sat_file path | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 230 | end; | 
| 14453 | 231 | |
| 232 | (* ------------------------------------------------------------------------- *) | |
| 17620 
49568e5e0450
parse_std_result_file renamed to read_std_result_file
 webertj parents: 
17581diff
changeset | 233 | (* read_std_result_file: scans a SAT solver's output file for a satisfying *) | 
| 14965 | 234 | (* variable assignment. Returns the assignment, or 'UNSATISFIABLE' if *) | 
| 235 | (* the file contains 'unsatisfiable', or 'UNKNOWN' if the file contains *) | |
| 236 | (* neither 'satisfiable' nor 'unsatisfiable'. Empty lines are ignored. *) | |
| 237 | (* The assignment must be given in one or more lines immediately after *) | |
| 238 | (* the line that contains 'satisfiable'. These lines must begin with *) | |
| 239 | (* 'assignment_prefix'. Variables must be separated by " ". Non- *) | |
| 240 | (* integer strings are ignored. If variable i is contained in the *) | |
| 241 | (* assignment, then i is interpreted as 'true'. If ~i is contained in *) | |
| 242 | (* the assignment, then i is interpreted as 'false'. Otherwise the *) | |
| 243 | (* value of i is taken to be unspecified. *) | |
| 14453 | 244 | (* ------------------------------------------------------------------------- *) | 
| 245 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 246 | (* Path.T -> string * string * string -> result *) | 
| 14453 | 247 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 248 | fun read_std_result_file path (satisfiable, assignment_prefix, unsatisfiable) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 249 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 250 | (* string -> int list *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 251 | fun int_list_from_string s = | 
| 32952 | 252 | map_filter Int.fromString (space_explode " " s) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 253 | (* int list -> assignment *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 254 | fun assignment_from_list [] i = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 255 | NONE (* the SAT solver didn't provide a value for this variable *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 256 | | assignment_from_list (x::xs) i = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 257 | if x=i then (SOME true) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 258 | else if x=(~i) then (SOME false) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 259 | else assignment_from_list xs i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 260 | (* int list -> string list -> assignment *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 261 | fun parse_assignment xs [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 262 | assignment_from_list xs | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 263 | | parse_assignment xs (line::lines) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 264 | if String.isPrefix assignment_prefix line then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 265 | parse_assignment (xs @ int_list_from_string line) lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 266 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 267 | assignment_from_list xs | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 268 | (* string -> string -> bool *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 269 | fun is_substring needle haystack = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 270 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 271 | val length1 = String.size needle | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 272 | val length2 = String.size haystack | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 273 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 274 | if length2 < length1 then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 275 | false | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 276 | else if needle = String.substring (haystack, 0, length1) then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 277 | true | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 278 | else is_substring needle (String.substring (haystack, 1, length2-1)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 279 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 280 | (* string list -> result *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 281 | fun parse_lines [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 282 | UNKNOWN | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 283 | | parse_lines (line::lines) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 284 | if is_substring unsatisfiable line then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 285 | UNSATISFIABLE NONE | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 286 | else if is_substring satisfiable line then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 287 | SATISFIABLE (parse_assignment [] lines) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 288 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 289 | parse_lines lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 290 | in | 
| 33317 | 291 | (parse_lines o filter (fn l => l <> "") o split_lines o File.read) path | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 292 | end; | 
| 14453 | 293 | |
| 294 | (* ------------------------------------------------------------------------- *) | |
| 295 | (* make_external_solver: call 'writefn', execute 'cmd', call 'readfn' *) | |
| 296 | (* ------------------------------------------------------------------------- *) | |
| 297 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 298 | fun make_external_solver cmd writefn readfn fm = | 
| 43850 
7f2cbc713344
moved bash operations to Isabelle_System (cf. Scala version);
 wenzelm parents: 
43701diff
changeset | 299 | (writefn fm; Isabelle_System.bash cmd; readfn ()); | 
| 14453 | 300 | |
| 301 | (* ------------------------------------------------------------------------- *) | |
| 15040 | 302 | (* read_dimacs_cnf_file: returns a propositional formula that corresponds to *) | 
| 303 | (* a SAT problem given in DIMACS CNF format *) | |
| 304 | (* ------------------------------------------------------------------------- *) | |
| 305 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 306 | fun read_dimacs_cnf_file path = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 307 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 308 | (* string list -> string list *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 309 | fun filter_preamble [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 310 | error "problem line not found in DIMACS CNF file" | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 311 | | filter_preamble (line::lines) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 312 | if String.isPrefix "c " line orelse line = "c" then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 313 | (* ignore comments *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 314 | filter_preamble lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 315 | else if String.isPrefix "p " line then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 316 | (* ignore the problem line (which must be the last line of the preamble) *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 317 | (* Ignoring the problem line implies that if the file contains more clauses *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 318 | (* or variables than specified in its preamble, we will accept it anyway. *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 319 | lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 320 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 321 | error "preamble in DIMACS CNF file contains a line that does not begin with \"c \" or \"p \"" | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 322 | (* string -> int *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 323 | fun int_from_string s = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 324 | case Int.fromString s of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 325 | SOME i => i | 
| 33937 
b5ca587d0885
read_dimacs_cnf_file can now read DIMACS files that contain successive
 webertj parents: 
33576diff
changeset | 326 |       | NONE   => error ("token " ^ quote s ^ " in DIMACS CNF file is not a number")
 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 327 | (* int list -> int list list *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 328 | fun clauses xs = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 329 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 330 | val (xs1, xs2) = take_prefix (fn i => i <> 0) xs | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 331 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 332 | case xs2 of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 333 | [] => [xs1] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 334 | | (0::[]) => [xs1] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 335 | | (0::tl) => xs1 :: clauses tl | 
| 40314 
b5ec88d9ac03
replaced ancient sys_error by raise Fail, assuming that the latter is not handled specifically by the environment;
 wenzelm parents: 
39687diff
changeset | 336 | | _ => raise Fail "SatSolver.clauses" | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 337 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 338 | fun literal_from_int i = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 339 | (i<>0 orelse error "variable index in DIMACS CNF file is 0"; | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 340 | if i>0 then | 
| 41471 | 341 | Prop_Logic.BoolVar i | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 342 | else | 
| 41471 | 343 | Prop_Logic.Not (Prop_Logic.BoolVar (~i))) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 344 | fun disjunction [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 345 | error "empty clause in DIMACS CNF file" | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 346 | | disjunction (x::xs) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 347 | (case xs of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 348 | [] => x | 
| 41471 | 349 | | _ => Prop_Logic.Or (x, disjunction xs)) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 350 | fun conjunction [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 351 | error "no clause in DIMACS CNF file" | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 352 | | conjunction (x::xs) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 353 | (case xs of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 354 | [] => x | 
| 41471 | 355 | | _ => Prop_Logic.And (x, conjunction xs)) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 356 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 357 | (conjunction | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 358 | o (map disjunction) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 359 | o (map (map literal_from_int)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 360 | o clauses | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 361 | o (map int_from_string) | 
| 36692 
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
 haftmann parents: 
35011diff
changeset | 362 | o (maps (String.tokens (member (op =) [#" ", #"\t", #"\n"]))) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 363 | o filter_preamble | 
| 33317 | 364 | o filter (fn l => l <> "") | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 365 | o split_lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 366 | o File.read) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 367 | path | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 368 | end; | 
| 15040 | 369 | |
| 370 | (* ------------------------------------------------------------------------- *) | |
| 14453 | 371 | (* solvers: a (reference to a) table of all registered SAT solvers *) | 
| 372 | (* ------------------------------------------------------------------------- *) | |
| 373 | ||
| 32740 | 374 | val solvers = Unsynchronized.ref ([] : (string * solver) list); | 
| 14453 | 375 | |
| 376 | (* ------------------------------------------------------------------------- *) | |
| 377 | (* add_solver: updates 'solvers' by adding a new solver *) | |
| 378 | (* ------------------------------------------------------------------------- *) | |
| 379 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 380 | (* string * solver -> unit *) | 
| 14453 | 381 | |
| 33228 | 382 | fun add_solver (name, new_solver) = CRITICAL (fn () => | 
| 22220 | 383 | let | 
| 384 | val the_solvers = !solvers; | |
| 385 | val _ = if AList.defined (op =) the_solvers name | |
| 386 |           then warning ("SAT solver " ^ quote name ^ " was defined before")
 | |
| 387 | else (); | |
| 33228 | 388 | in solvers := AList.update (op =) (name, new_solver) the_solvers end); | 
| 14453 | 389 | |
| 390 | (* ------------------------------------------------------------------------- *) | |
| 391 | (* invoke_solver: returns the solver associated with the given 'name' *) | |
| 15605 | 392 | (* Note: If no solver is associated with 'name', exception 'Option' will be *) | 
| 14453 | 393 | (* raised. *) | 
| 394 | (* ------------------------------------------------------------------------- *) | |
| 395 | ||
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 396 | (* string -> solver *) | 
| 14453 | 397 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 398 | fun invoke_solver name = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 399 | (the o AList.lookup (op =) (!solvers)) name; | 
| 14453 | 400 | |
| 401 | end; (* SatSolver *) | |
| 402 | ||
| 403 | ||
| 404 | (* ------------------------------------------------------------------------- *) | |
| 405 | (* Predefined SAT solvers *) | |
| 406 | (* ------------------------------------------------------------------------- *) | |
| 407 | ||
| 408 | (* ------------------------------------------------------------------------- *) | |
| 14753 | 409 | (* Internal SAT solver, available as 'SatSolver.invoke_solver "enumerate"' *) | 
| 410 | (* -- simply enumerates assignments until a satisfying assignment is found *) | |
| 14453 | 411 | (* ------------------------------------------------------------------------- *) | 
| 412 | ||
| 413 | let | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 414 | fun enum_solver fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 415 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 416 | (* int list *) | 
| 41471 | 417 | val indices = Prop_Logic.indices fm | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 418 | (* int list -> int list -> int list option *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 419 | (* binary increment: list 'xs' of current bits, list 'ys' of all bits (lower bits first) *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 420 | fun next_list _ ([]:int list) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 421 | NONE | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 422 | | next_list [] (y::ys) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 423 | SOME [y] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 424 | | next_list (x::xs) (y::ys) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 425 | if x=y then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 426 | (* reset the bit, continue *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 427 | next_list xs ys | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 428 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 429 | (* set the lowest bit that wasn't set before, keep the higher bits *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 430 | SOME (y::x::xs) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 431 | (* int list -> int -> bool *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 432 | fun assignment_from_list xs i = | 
| 36692 
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
 haftmann parents: 
35011diff
changeset | 433 | member (op =) xs i | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 434 | (* int list -> SatSolver.result *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 435 | fun solver_loop xs = | 
| 41471 | 436 | if Prop_Logic.eval (assignment_from_list xs) fm then | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 437 | SatSolver.SATISFIABLE (SOME o (assignment_from_list xs)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 438 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 439 | (case next_list xs indices of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 440 | SOME xs' => solver_loop xs' | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 441 | | NONE => SatSolver.UNSATISFIABLE NONE) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 442 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 443 | (* start with the "first" assignment (all variables are interpreted as 'false') *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 444 | solver_loop [] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 445 | end | 
| 14453 | 446 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 447 |   SatSolver.add_solver ("enumerate", enum_solver)
 | 
| 14453 | 448 | end; | 
| 449 | ||
| 450 | (* ------------------------------------------------------------------------- *) | |
| 14753 | 451 | (* Internal SAT solver, available as 'SatSolver.invoke_solver "dpll"' -- a *) | 
| 452 | (* simple implementation of the DPLL algorithm (cf. L. Zhang, S. Malik: "The *) | |
| 453 | (* Quest for Efficient Boolean Satisfiability Solvers", July 2002, Fig. 1). *) | |
| 14453 | 454 | (* ------------------------------------------------------------------------- *) | 
| 455 | ||
| 456 | let | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 457 | local | 
| 41471 | 458 | open Prop_Logic | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 459 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 460 | fun dpll_solver fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 461 | let | 
| 41471 | 462 | (* We could use 'Prop_Logic.defcnf fm' instead of 'Prop_Logic.nnf fm' *) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 463 | (* but that sometimes leads to worse performance due to the *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 464 | (* introduction of additional variables. *) | 
| 41471 | 465 | val fm' = Prop_Logic.nnf fm | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 466 | (* int list *) | 
| 41471 | 467 | val indices = Prop_Logic.indices fm' | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 468 | (* int list -> int -> prop_formula *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 469 | fun partial_var_eval [] i = BoolVar i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 470 | | partial_var_eval (x::xs) i = if x=i then True else if x=(~i) then False else partial_var_eval xs i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 471 | (* int list -> prop_formula -> prop_formula *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 472 | fun partial_eval xs True = True | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 473 | | partial_eval xs False = False | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 474 | | partial_eval xs (BoolVar i) = partial_var_eval xs i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 475 | | partial_eval xs (Not fm) = SNot (partial_eval xs fm) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 476 | | partial_eval xs (Or (fm1, fm2)) = SOr (partial_eval xs fm1, partial_eval xs fm2) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 477 | | partial_eval xs (And (fm1, fm2)) = SAnd (partial_eval xs fm1, partial_eval xs fm2) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 478 | (* prop_formula -> int list *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 479 | fun forced_vars True = [] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 480 | | forced_vars False = [] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 481 | | forced_vars (BoolVar i) = [i] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 482 | | forced_vars (Not (BoolVar i)) = [~i] | 
| 33049 
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
 haftmann parents: 
33039diff
changeset | 483 | | forced_vars (Or (fm1, fm2)) = inter (op =) (forced_vars fm1) (forced_vars fm2) | 
| 33042 | 484 | | forced_vars (And (fm1, fm2)) = union (op =) (forced_vars fm1) (forced_vars fm2) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 485 | (* Above, i *and* ~i may be forced. In this case the first occurrence takes *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 486 | (* precedence, and the next partial evaluation of the formula returns 'False'. *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 487 | | forced_vars _ = error "formula is not in negation normal form" | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 488 | (* int list -> prop_formula -> (int list * prop_formula) *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 489 | fun eval_and_force xs fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 490 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 491 | val fm' = partial_eval xs fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 492 | val xs' = forced_vars fm' | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 493 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 494 | if null xs' then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 495 | (xs, fm') | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 496 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 497 | eval_and_force (xs@xs') fm' (* xs and xs' should be distinct, so '@' here should have *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 498 | (* the same effect as 'union_int' *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 499 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 500 | (* int list -> int option *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 501 | fun fresh_var xs = | 
| 36692 
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
 haftmann parents: 
35011diff
changeset | 502 | find_first (fn i => not (member (op =) xs i) andalso not (member (op =) xs (~i))) indices | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 503 | (* int list -> prop_formula -> int list option *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 504 | (* partial assignment 'xs' *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 505 | fun dpll xs fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 506 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 507 | val (xs', fm') = eval_and_force xs fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 508 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 509 | case fm' of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 510 | True => SOME xs' | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 511 | | False => NONE | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 512 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 513 | let | 
| 33035 | 514 | val x = the (fresh_var xs') (* a fresh variable must exist since 'fm' did not evaluate to 'True'/'False' *) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 515 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 516 | case dpll (x::xs') fm' of (* passing fm' rather than fm should save some simplification work *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 517 | SOME xs'' => SOME xs'' | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 518 | | NONE => dpll ((~x)::xs') fm' (* now try interpreting 'x' as 'False' *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 519 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 520 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 521 | (* int list -> assignment *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 522 | fun assignment_from_list [] i = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 523 | NONE (* the DPLL procedure didn't provide a value for this variable *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 524 | | assignment_from_list (x::xs) i = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 525 | if x=i then (SOME true) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 526 | else if x=(~i) then (SOME false) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 527 | else assignment_from_list xs i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 528 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 529 | (* initially, no variable is interpreted yet *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 530 | case dpll [] fm' of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 531 | SOME assignment => SatSolver.SATISFIABLE (assignment_from_list assignment) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 532 | | NONE => SatSolver.UNSATISFIABLE NONE | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 533 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 534 | end (* local *) | 
| 14453 | 535 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 536 |   SatSolver.add_solver ("dpll", dpll_solver)
 | 
| 14453 | 537 | end; | 
| 538 | ||
| 539 | (* ------------------------------------------------------------------------- *) | |
| 14753 | 540 | (* Internal SAT solver, available as 'SatSolver.invoke_solver "auto"': uses *) | 
| 17577 
e87bf1d8f17a
zchaff_with_proofs does not delete zChaff\s resolve_trace file anymore
 webertj parents: 
17541diff
changeset | 541 | (* the last installed solver (other than "auto" itself) that does not raise *) | 
| 15299 
576fd0b65ed8
solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
 webertj parents: 
15128diff
changeset | 542 | (* 'NOT_CONFIGURED'. (However, the solver may return 'UNKNOWN'.) *) | 
| 14453 | 543 | (* ------------------------------------------------------------------------- *) | 
| 544 | ||
| 545 | let | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 546 | fun auto_solver fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 547 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 548 | fun loop [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 549 | SatSolver.UNKNOWN | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 550 | | loop ((name, solver)::solvers) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 551 | if name="auto" then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 552 | (* do not call solver "auto" from within "auto" *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 553 | loop solvers | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 554 | else ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 555 | (* apply 'solver' to 'fm' *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 556 | solver fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 557 | handle SatSolver.NOT_CONFIGURED => loop solvers | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 558 | ) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 559 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 560 | loop (!SatSolver.solvers) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 561 | end | 
| 14453 | 562 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 563 |   SatSolver.add_solver ("auto", auto_solver)
 | 
| 14453 | 564 | end; | 
| 565 | ||
| 566 | (* ------------------------------------------------------------------------- *) | |
| 20033 | 567 | (* MiniSat 1.14 *) | 
| 568 | (* (http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/) *) | |
| 569 | (* ------------------------------------------------------------------------- *) | |
| 570 | ||
| 20135 | 571 | (* ------------------------------------------------------------------------- *) | 
| 572 | (* "minisat_with_proofs" requires a modified version of MiniSat 1.14 by John *) | |
| 573 | (* Matthews, which can output ASCII proof traces. Replaying binary proof *) | |
| 574 | (* traces generated by MiniSat-p_v1.14 has _not_ been implemented. *) | |
| 575 | (* ------------------------------------------------------------------------- *) | |
| 576 | ||
| 577 | (* add "minisat_with_proofs" _before_ "minisat" to the available solvers, so *) | |
| 578 | (* that the latter is preferred by the "auto" solver *) | |
| 579 | ||
| 20152 
b6373fe199e1
MiniSat proof trace format changed; MiniSat is now expected to produce a proof also for "trivial" problems
 webertj parents: 
20137diff
changeset | 580 | (* There is a complication that is dealt with in the code below: MiniSat *) | 
| 
b6373fe199e1
MiniSat proof trace format changed; MiniSat is now expected to produce a proof also for "trivial" problems
 webertj parents: 
20137diff
changeset | 581 | (* introduces IDs for original clauses in the proof trace. It does not (in *) | 
| 
b6373fe199e1
MiniSat proof trace format changed; MiniSat is now expected to produce a proof also for "trivial" problems
 webertj parents: 
20137diff
changeset | 582 | (* general) follow the convention that the original clauses are numbered *) | 
| 
b6373fe199e1
MiniSat proof trace format changed; MiniSat is now expected to produce a proof also for "trivial" problems
 webertj parents: 
20137diff
changeset | 583 | (* from 0 to n-1 (where n is the number of clauses in the formula). *) | 
| 20135 | 584 | |
| 585 | let | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 586 | exception INVALID_PROOF of string | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 587 | fun minisat_with_proofs fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 588 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 589 | val _ = if (getenv "MINISAT_HOME") = "" then raise SatSolver.NOT_CONFIGURED else () | 
| 29872 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 590 | val serial_str = serial_string () | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 591 |     val inpath     = File.tmp_path (Path.explode ("isabelle" ^ serial_str ^ ".cnf"))
 | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 592 |     val outpath    = File.tmp_path (Path.explode ("result" ^ serial_str))
 | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 593 |     val proofpath  = File.tmp_path (Path.explode ("result" ^ serial_str ^ ".prf"))
 | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 594 | val cmd = getenv "MINISAT_HOME" ^ "/minisat " ^ File.shell_path inpath ^ " -r " ^ File.shell_path outpath ^ " -t " ^ File.shell_path proofpath ^ "> /dev/null" | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 595 | fun writefn fm = SatSolver.write_dimacs_cnf_file inpath fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 596 |     fun readfn ()  = SatSolver.read_std_result_file outpath ("SAT", "", "UNSAT")
 | 
| 41944 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 597 |     val _ = if File.exists inpath then warning ("overwriting existing file " ^ Path.print inpath) else ()
 | 
| 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 598 |     val _ = if File.exists outpath then warning ("overwriting existing file " ^ Path.print outpath) else ()
 | 
| 41471 | 599 | val cnf = Prop_Logic.defcnf fm | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 600 | val result = SatSolver.make_external_solver cmd writefn readfn cnf | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 601 | val _ = try File.rm inpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 602 | val _ = try File.rm outpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 603 | in case result of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 604 | SatSolver.UNSATISFIABLE NONE => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 605 | (let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 606 | (* string list *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 607 | val proof_lines = (split_lines o File.read) proofpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 608 | handle IO.Io _ => raise INVALID_PROOF "Could not read file \"result.prf\"" | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 609 | (* representation of clauses as ordered lists of literals (with duplicates removed) *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 610 | (* prop_formula -> int list *) | 
| 41471 | 611 | fun clause_to_lit_list (Prop_Logic.Or (fm1, fm2)) = | 
| 39687 | 612 | Ord_List.union int_ord (clause_to_lit_list fm1) (clause_to_lit_list fm2) | 
| 41471 | 613 | | clause_to_lit_list (Prop_Logic.BoolVar i) = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 614 | [i] | 
| 41471 | 615 | | clause_to_lit_list (Prop_Logic.Not (Prop_Logic.BoolVar i)) = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 616 | [~i] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 617 | | clause_to_lit_list _ = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 618 | raise INVALID_PROOF "Error: invalid clause in CNF formula." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 619 | (* prop_formula -> int *) | 
| 41471 | 620 | fun cnf_number_of_clauses (Prop_Logic.And (fm1, fm2)) = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 621 | cnf_number_of_clauses fm1 + cnf_number_of_clauses fm2 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 622 | | cnf_number_of_clauses _ = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 623 | 1 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 624 | val number_of_clauses = cnf_number_of_clauses cnf | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 625 | (* int list array *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 626 | val clauses = Array.array (number_of_clauses, []) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 627 | (* initialize the 'clauses' array *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 628 | (* prop_formula * int -> int *) | 
| 41471 | 629 | fun init_array (Prop_Logic.And (fm1, fm2), n) = | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 630 | init_array (fm2, init_array (fm1, n)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 631 | | init_array (fm, n) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 632 | (Array.update (clauses, n, clause_to_lit_list fm); n+1) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 633 | val _ = init_array (cnf, 0) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 634 | (* optimization for the common case where MiniSat "R"s clauses in their *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 635 | (* original order: *) | 
| 32740 | 636 | val last_ref_clause = Unsynchronized.ref (number_of_clauses - 1) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 637 | (* search the 'clauses' array for the given list of literals 'lits', *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 638 | (* starting at index '!last_ref_clause + 1' *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 639 | (* int list -> int option *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 640 | fun original_clause_id lits = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 641 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 642 | fun original_clause_id_from index = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 643 | if index = number_of_clauses then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 644 | (* search from beginning again *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 645 | original_clause_id_from 0 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 646 | (* both 'lits' and the list of literals used in 'clauses' are sorted, so *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 647 | (* testing for equality should suffice -- barring duplicate literals *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 648 | else if Array.sub (clauses, index) = lits then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 649 | (* success *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 650 | last_ref_clause := index; | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 651 | SOME index | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 652 | ) else if index = !last_ref_clause then | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 653 | (* failure *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 654 | NONE | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 655 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 656 | (* continue search *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 657 | original_clause_id_from (index + 1) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 658 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 659 | original_clause_id_from (!last_ref_clause + 1) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 660 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 661 | (* string -> int *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 662 | fun int_from_string s = ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 663 | case Int.fromString s of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 664 | SOME i => i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 665 |         | NONE   => raise INVALID_PROOF ("File format error: number expected (" ^ quote s ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 666 | ) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 667 | (* parse the proof file *) | 
| 32740 | 668 | val clause_table = Unsynchronized.ref (Inttab.empty : int list Inttab.table) | 
| 669 | val empty_id = Unsynchronized.ref ~1 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 670 | (* contains a mapping from clause IDs as used by MiniSat to clause IDs in *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 671 | (* our proof format, where original clauses are numbered starting from 0 *) | 
| 32740 | 672 | val clause_id_map = Unsynchronized.ref (Inttab.empty : int Inttab.table) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 673 | fun sat_to_proof id = ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 674 | case Inttab.lookup (!clause_id_map) id of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 675 | SOME id' => id' | 
| 41491 | 676 |         | NONE     => raise INVALID_PROOF ("Clause ID " ^ string_of_int id ^ " used, but not defined.")
 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 677 | ) | 
| 32740 | 678 | val next_id = Unsynchronized.ref (number_of_clauses - 1) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 679 | (* string list -> unit *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 680 | fun process_tokens [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 681 | () | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 682 | | process_tokens (tok::toks) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 683 | if tok="R" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 684 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 685 | id::sep::lits => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 686 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 687 | val _ = if !empty_id = ~1 then () else raise INVALID_PROOF "File format error: \"R\" disallowed after \"X\"." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 688 | val cid = int_from_string id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 689 |               val _        = if sep = "<=" then () else raise INVALID_PROOF ("File format error: \"<=\" expected (" ^ quote sep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 690 | val ls = sort int_ord (map int_from_string lits) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 691 | val proof_id = case original_clause_id ls of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 692 | SOME orig_id => orig_id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 693 |                              | NONE         => raise INVALID_PROOF ("Original clause (new ID is " ^ id ^ ") not found.")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 694 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 695 | (* extend the mapping of clause IDs with this newly defined ID *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 696 | clause_id_map := Inttab.update_new (cid, proof_id) (!clause_id_map) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 697 |                 handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ id ^ " defined more than once (in \"R\").")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 698 | (* the proof itself doesn't change *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 699 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 700 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 701 | raise INVALID_PROOF "File format error: \"R\" followed by an insufficient number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 702 | ) else if tok="C" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 703 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 704 | id::sep::ids => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 705 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 706 | val _ = if !empty_id = ~1 then () else raise INVALID_PROOF "File format error: \"C\" disallowed after \"X\"." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 707 | val cid = int_from_string id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 708 |               val _        = if sep = "<=" then () else raise INVALID_PROOF ("File format error: \"<=\" expected (" ^ quote sep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 709 | (* ignore the pivot literals in MiniSat's trace *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 710 | fun unevens [] = raise INVALID_PROOF "File format error: \"C\" followed by an even number of IDs." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 711 | | unevens (x :: []) = x :: [] | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 712 | | unevens (x :: _ :: xs) = x :: unevens xs | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 713 | val rs = (map sat_to_proof o unevens o map int_from_string) ids | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 714 | (* extend the mapping of clause IDs with this newly defined ID *) | 
| 32740 | 715 | val proof_id = Unsynchronized.inc next_id | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 716 | val _ = clause_id_map := Inttab.update_new (cid, proof_id) (!clause_id_map) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 717 |                                handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ id ^ " defined more than once (in \"C\").")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 718 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 719 | (* update clause table *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 720 | clause_table := Inttab.update_new (proof_id, rs) (!clause_table) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 721 |                 handle Inttab.DUP _ => raise INVALID_PROOF ("Error: internal ID for clause " ^ id ^ " already used.")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 722 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 723 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 724 | raise INVALID_PROOF "File format error: \"C\" followed by an insufficient number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 725 | ) else if tok="D" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 726 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 727 | [id] => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 728 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 729 | val _ = if !empty_id = ~1 then () else raise INVALID_PROOF "File format error: \"D\" disallowed after \"X\"." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 730 | val _ = sat_to_proof (int_from_string id) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 731 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 732 | (* simply ignore "D" *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 733 | () | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 734 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 735 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 736 | raise INVALID_PROOF "File format error: \"D\" followed by an illegal number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 737 | ) else if tok="X" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 738 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 739 | [id1, id2] => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 740 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 741 | val _ = if !empty_id = ~1 then () else raise INVALID_PROOF "File format error: more than one end-of-proof statement." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 742 | val _ = sat_to_proof (int_from_string id1) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 743 | val new_empty_id = sat_to_proof (int_from_string id2) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 744 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 745 | (* update conflict id *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 746 | empty_id := new_empty_id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 747 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 748 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 749 | raise INVALID_PROOF "File format error: \"X\" followed by an illegal number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 750 | ) else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 751 |           raise INVALID_PROOF ("File format error: unknown token " ^ quote tok ^ " encountered.")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 752 | (* string list -> unit *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 753 | fun process_lines [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 754 | () | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 755 | | process_lines (l::ls) = ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 756 | process_tokens (String.tokens (fn c => c = #" " orelse c = #"\t") l); | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 757 | process_lines ls | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 758 | ) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 759 | (* proof *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 760 | val _ = process_lines proof_lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 761 | val _ = if !empty_id <> ~1 then () else raise INVALID_PROOF "File format error: no conflicting clause specified." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 762 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 763 | SatSolver.UNSATISFIABLE (SOME (!clause_table, !empty_id)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 764 | end handle INVALID_PROOF reason => (warning reason; SatSolver.UNSATISFIABLE NONE)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 765 | | result => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 766 | result | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 767 | end | 
| 20135 | 768 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 769 |   SatSolver.add_solver ("minisat_with_proofs", minisat_with_proofs)
 | 
| 20135 | 770 | end; | 
| 771 | ||
| 20033 | 772 | let | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 773 | fun minisat fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 774 | let | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 775 | val _ = if getenv "MINISAT_HOME" = "" then raise SatSolver.NOT_CONFIGURED else () | 
| 29872 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 776 | val serial_str = serial_string () | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 777 |     val inpath     = File.tmp_path (Path.explode ("isabelle" ^ serial_str ^ ".cnf"))
 | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 778 |     val outpath    = File.tmp_path (Path.explode ("result" ^ serial_str))
 | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 779 | val cmd = getenv "MINISAT_HOME" ^ "/minisat " ^ File.shell_path inpath ^ " -r " ^ File.shell_path outpath ^ " > /dev/null" | 
| 41471 | 780 | fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (Prop_Logic.defcnf fm) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 781 |     fun readfn ()  = SatSolver.read_std_result_file outpath ("SAT", "", "UNSAT")
 | 
| 41944 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 782 |     val _ = if File.exists inpath then warning ("overwriting existing file " ^ Path.print inpath) else ()
 | 
| 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 783 |     val _ = if File.exists outpath then warning ("overwriting existing file " ^ Path.print outpath) else ()
 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 784 | val result = SatSolver.make_external_solver cmd writefn readfn fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 785 | val _ = try File.rm inpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 786 | val _ = try File.rm outpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 787 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 788 | result | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 789 | end | 
| 20033 | 790 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 791 |   SatSolver.add_solver ("minisat", minisat)
 | 
| 20033 | 792 | end; | 
| 793 | ||
| 794 | (* ------------------------------------------------------------------------- *) | |
| 15332 | 795 | (* zChaff (http://www.princeton.edu/~chaff/zchaff.html) *) | 
| 14453 | 796 | (* ------------------------------------------------------------------------- *) | 
| 797 | ||
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 798 | (* ------------------------------------------------------------------------- *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 799 | (* 'zchaff_with_proofs' applies the "zchaff" prover to a formula, and if *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 800 | (* zChaff finds that the formula is unsatisfiable, a proof of this is read *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 801 | (* from a file "resolve_trace" that was generated by zChaff. See the code *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 802 | (* below for the expected format of the "resolve_trace" file. Aside from *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 803 | (* some basic syntactic checks, no verification of the proof is performed. *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 804 | (* ------------------------------------------------------------------------- *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 805 | |
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 806 | (* add "zchaff_with_proofs" _before_ "zchaff" to the available solvers, so *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 807 | (* that the latter is preferred by the "auto" solver *) | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 808 | |
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 809 | let | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 810 | exception INVALID_PROOF of string | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 811 | fun zchaff_with_proofs fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 812 | case SatSolver.invoke_solver "zchaff" fm of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 813 | SatSolver.UNSATISFIABLE NONE => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 814 | (let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 815 | (* string list *) | 
| 43701 | 816 | (* FIXME File.tmp_path (!?) *) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 817 | val proof_lines = ((split_lines o File.read) (Path.explode "resolve_trace")) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 818 | handle IO.Io _ => raise INVALID_PROOF "Could not read file \"resolve_trace\"" | 
| 41471 | 819 | fun cnf_number_of_clauses (Prop_Logic.And (fm1, fm2)) = | 
| 820 | cnf_number_of_clauses fm1 + cnf_number_of_clauses fm2 | |
| 821 | | cnf_number_of_clauses _ = 1 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 822 | (* string -> int *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 823 | fun int_from_string s = ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 824 | case Int.fromString s of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 825 | SOME i => i | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 826 |         | NONE   => raise INVALID_PROOF ("File format error: number expected (" ^ quote s ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 827 | ) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 828 | (* parse the "resolve_trace" file *) | 
| 32740 | 829 | val clause_offset = Unsynchronized.ref ~1 | 
| 830 | val clause_table = Unsynchronized.ref (Inttab.empty : int list Inttab.table) | |
| 831 | val empty_id = Unsynchronized.ref ~1 | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 832 | (* string list -> unit *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 833 | fun process_tokens [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 834 | () | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 835 | | process_tokens (tok::toks) = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 836 | if tok="CL:" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 837 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 838 | id::sep::ids => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 839 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 840 |               val _   = if !clause_offset = ~1 then () else raise INVALID_PROOF ("File format error: \"CL:\" disallowed after \"VAR:\".")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 841 |               val _   = if !empty_id = ~1 then () else raise INVALID_PROOF ("File format error: \"CL:\" disallowed after \"CONF:\".")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 842 | val cid = int_from_string id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 843 |               val _   = if sep = "<=" then () else raise INVALID_PROOF ("File format error: \"<=\" expected (" ^ quote sep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 844 | val rs = map int_from_string ids | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 845 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 846 | (* update clause table *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 847 | clause_table := Inttab.update_new (cid, rs) (!clause_table) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 848 |                 handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ id ^ " defined more than once.")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 849 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 850 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 851 | raise INVALID_PROOF "File format error: \"CL:\" followed by an insufficient number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 852 | ) else if tok="VAR:" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 853 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 854 | id::levsep::levid::valsep::valid::antesep::anteid::litsep::lits => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 855 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 856 |               val _   = if !empty_id = ~1 then () else raise INVALID_PROOF ("File format error: \"VAR:\" disallowed after \"CONF:\".")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 857 | (* set 'clause_offset' to the largest used clause ID *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 858 | val _ = if !clause_offset = ~1 then clause_offset := | 
| 52049 | 859 | (case Inttab.max (!clause_table) of | 
| 860 | SOME (id, _) => id | |
| 41471 | 861 | | NONE => cnf_number_of_clauses (Prop_Logic.defcnf fm) - 1 (* the first clause ID is 0, not 1 *)) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 862 | else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 863 | () | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 864 | val vid = int_from_string id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 865 |               val _   = if levsep = "L:" then () else raise INVALID_PROOF ("File format error: \"L:\" expected (" ^ quote levsep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 866 | val _ = int_from_string levid | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 867 |               val _   = if valsep = "V:" then () else raise INVALID_PROOF ("File format error: \"V:\" expected (" ^ quote valsep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 868 | val _ = int_from_string valid | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 869 |               val _   = if antesep = "A:" then () else raise INVALID_PROOF ("File format error: \"A:\" expected (" ^ quote antesep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 870 | val aid = int_from_string anteid | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 871 |               val _   = if litsep = "Lits:" then () else raise INVALID_PROOF ("File format error: \"Lits:\" expected (" ^ quote litsep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 872 | val ls = map int_from_string lits | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 873 | (* convert the data provided by zChaff to our resolution-style proof format *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 874 | (* each "VAR:" line defines a unit clause, the resolvents are implicitly *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 875 | (* given by the literals in the antecedent clause *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 876 | (* we use the sum of '!clause_offset' and the variable ID as clause ID for the unit clause *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 877 | val cid = !clause_offset + vid | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 878 | (* the low bit of each literal gives its sign (positive/negative), therefore *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 879 | (* we have to divide each literal by 2 to obtain the proper variable ID; then *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 880 | (* we add '!clause_offset' to obtain the ID of the corresponding unit clause *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 881 | val vids = filter (not_equal vid) (map (fn l => l div 2) ls) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 882 | val rs = aid :: map (fn v => !clause_offset + v) vids | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 883 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 884 | (* update clause table *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 885 | clause_table := Inttab.update_new (cid, rs) (!clause_table) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 886 |                 handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ string_of_int cid ^ " (derived from antecedent for variable " ^ id ^ ") already defined.")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 887 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 888 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 889 | raise INVALID_PROOF "File format error: \"VAR:\" followed by an insufficient number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 890 | ) else if tok="CONF:" then ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 891 | case toks of | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 892 | id::sep::ids => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 893 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 894 | val _ = if !empty_id = ~1 then () else raise INVALID_PROOF "File format error: more than one conflicting clause specified." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 895 | val cid = int_from_string id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 896 |               val _   = if sep = "==" then () else raise INVALID_PROOF ("File format error: \"==\" expected (" ^ quote sep ^ " encountered).")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 897 | val ls = map int_from_string ids | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 898 | (* the conflict clause must be resolved with the unit clauses *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 899 | (* for its literals to obtain the empty clause *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 900 | val vids = map (fn l => l div 2) ls | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 901 | val rs = cid :: map (fn v => !clause_offset + v) vids | 
| 52049 | 902 | val new_empty_id = the_default (!clause_offset) (Option.map fst (Inttab.max (!clause_table))) + 1 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 903 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 904 | (* update clause table and conflict id *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 905 | clause_table := Inttab.update_new (new_empty_id, rs) (!clause_table) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 906 |                 handle Inttab.DUP _ => raise INVALID_PROOF ("File format error: clause " ^ string_of_int new_empty_id ^ " (empty clause derived from clause " ^ id ^ ") already defined.");
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 907 | empty_id := new_empty_id | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 908 | end | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 909 | | _ => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 910 | raise INVALID_PROOF "File format error: \"CONF:\" followed by an insufficient number of tokens." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 911 | ) else | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 912 |           raise INVALID_PROOF ("File format error: unknown token " ^ quote tok ^ " encountered.")
 | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 913 | (* string list -> unit *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 914 | fun process_lines [] = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 915 | () | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 916 | | process_lines (l::ls) = ( | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 917 | process_tokens (String.tokens (fn c => c = #" " orelse c = #"\t") l); | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 918 | process_lines ls | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 919 | ) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 920 | (* proof *) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 921 | val _ = process_lines proof_lines | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 922 | val _ = if !empty_id <> ~1 then () else raise INVALID_PROOF "File format error: no conflicting clause specified." | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 923 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 924 | SatSolver.UNSATISFIABLE (SOME (!clause_table, !empty_id)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 925 | end handle INVALID_PROOF reason => (warning reason; SatSolver.UNSATISFIABLE NONE)) | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 926 | | result => | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 927 | result | 
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 928 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 929 |   SatSolver.add_solver ("zchaff_with_proofs", zchaff_with_proofs)
 | 
| 17493 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 930 | end; | 
| 
cf8713d880b1
SAT solver interface modified to support proofs of unsatisfiability
 webertj parents: 
16915diff
changeset | 931 | |
| 14487 
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
 webertj parents: 
14460diff
changeset | 932 | let | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 933 | fun zchaff fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 934 | let | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 935 | val _ = if getenv "ZCHAFF_HOME" = "" then raise SatSolver.NOT_CONFIGURED else () | 
| 29872 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 936 | val serial_str = serial_string () | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 937 |     val inpath     = File.tmp_path (Path.explode ("isabelle" ^ serial_str ^ ".cnf"))
 | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 938 |     val outpath    = File.tmp_path (Path.explode ("result" ^ serial_str))
 | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 939 | val cmd = getenv "ZCHAFF_HOME" ^ "/zchaff " ^ File.shell_path inpath ^ " > " ^ File.shell_path outpath | 
| 41471 | 940 | fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (Prop_Logic.defcnf fm) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 941 |     fun readfn ()  = SatSolver.read_std_result_file outpath ("Instance Satisfiable", "", "Instance Unsatisfiable")
 | 
| 41944 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 942 |     val _ = if File.exists inpath then warning ("overwriting existing file " ^ Path.print inpath) else ()
 | 
| 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 943 |     val _ = if File.exists outpath then warning ("overwriting existing file " ^ Path.print outpath) else ()
 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 944 | val result = SatSolver.make_external_solver cmd writefn readfn fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 945 | val _ = try File.rm inpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 946 | val _ = try File.rm outpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 947 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 948 | result | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 949 | end | 
| 14487 
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
 webertj parents: 
14460diff
changeset | 950 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 951 |   SatSolver.add_solver ("zchaff", zchaff)
 | 
| 14965 | 952 | end; | 
| 953 | ||
| 954 | (* ------------------------------------------------------------------------- *) | |
| 955 | (* BerkMin 561 (http://eigold.tripod.com/BerkMin.html) *) | |
| 956 | (* ------------------------------------------------------------------------- *) | |
| 957 | ||
| 958 | let | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 959 | fun berkmin fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 960 | let | 
| 30275 
381ce8d88cb8
Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
 blanchet parents: 
30240diff
changeset | 961 | val _ = if (getenv "BERKMIN_HOME") = "" then raise SatSolver.NOT_CONFIGURED else () | 
| 29872 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 962 | val serial_str = serial_string () | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 963 |     val inpath     = File.tmp_path (Path.explode ("isabelle" ^ serial_str ^ ".cnf"))
 | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 964 |     val outpath    = File.tmp_path (Path.explode ("result" ^ serial_str))
 | 
| 30275 
381ce8d88cb8
Reintroduced previous changes: Made "Refute.norm_rhs" public and simplified the configuration of the BerkMin and zChaff SAT solvers.
 blanchet parents: 
30240diff
changeset | 965 | val exec = getenv "BERKMIN_EXE" | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 966 | val cmd = getenv "BERKMIN_HOME" ^ "/" ^ (if exec = "" then "BerkMin561" else exec) ^ " " ^ File.shell_path inpath ^ " > " ^ File.shell_path outpath | 
| 41471 | 967 | fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (Prop_Logic.defcnf fm) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 968 |     fun readfn ()  = SatSolver.read_std_result_file outpath ("Satisfiable          !!", "solution =", "UNSATISFIABLE          !!")
 | 
| 41944 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 969 |     val _ = if File.exists inpath then warning ("overwriting existing file " ^ Path.print inpath) else ()
 | 
| 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 970 |     val _ = if File.exists outpath then warning ("overwriting existing file " ^ Path.print outpath) else ()
 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 971 | val result = SatSolver.make_external_solver cmd writefn readfn fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 972 | val _ = try File.rm inpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 973 | val _ = try File.rm outpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 974 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 975 | result | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 976 | end | 
| 14965 | 977 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 978 |   SatSolver.add_solver ("berkmin", berkmin)
 | 
| 14965 | 979 | end; | 
| 980 | ||
| 981 | (* ------------------------------------------------------------------------- *) | |
| 982 | (* Jerusat 1.3 (http://www.cs.tau.ac.il/~ale1/) *) | |
| 983 | (* ------------------------------------------------------------------------- *) | |
| 984 | ||
| 985 | let | |
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 986 | fun jerusat fm = | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 987 | let | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 988 | val _ = if (getenv "JERUSAT_HOME") = "" then raise SatSolver.NOT_CONFIGURED else () | 
| 29872 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 989 | val serial_str = serial_string () | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 990 |     val inpath     = File.tmp_path (Path.explode ("isabelle" ^ serial_str ^ ".cnf"))
 | 
| 
14e208d607af
Added serial_string to SAT solver input and output files, to prevent multithreading chaos.
 blanchet parents: 
22567diff
changeset | 991 |     val outpath    = File.tmp_path (Path.explode ("result" ^ serial_str))
 | 
| 35011 
9e55e87434ff
proper treatment of paths passed to the shell -- to allow spaces in file names as usual;
 wenzelm parents: 
35010diff
changeset | 992 | val cmd = getenv "JERUSAT_HOME" ^ "/Jerusat1.3 " ^ File.shell_path inpath ^ " > " ^ File.shell_path outpath | 
| 41471 | 993 | fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (Prop_Logic.defcnf fm) | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 994 |     fun readfn ()  = SatSolver.read_std_result_file outpath ("s SATISFIABLE", "v ", "s UNSATISFIABLE")
 | 
| 41944 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 995 |     val _ = if File.exists inpath then warning ("overwriting existing file " ^ Path.print inpath) else ()
 | 
| 
b97091ae583a
Path.print is the official way to show file-system paths to users -- note that Path.implode often indicates violation of the abstract datatype;
 wenzelm parents: 
41491diff
changeset | 996 |     val _ = if File.exists outpath then warning ("overwriting existing file " ^ Path.print outpath) else ()
 | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 997 | val result = SatSolver.make_external_solver cmd writefn readfn fm | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 998 | val _ = try File.rm inpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 999 | val _ = try File.rm outpath | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 1000 | in | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 1001 | result | 
| 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 1002 | end | 
| 14965 | 1003 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 1004 |   SatSolver.add_solver ("jerusat", jerusat)
 | 
| 14487 
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
 webertj parents: 
14460diff
changeset | 1005 | end; | 
| 14453 | 1006 | |
| 1007 | (* ------------------------------------------------------------------------- *) | |
| 1008 | (* Add code for other SAT solvers below. *) | |
| 1009 | (* ------------------------------------------------------------------------- *) | |
| 1010 | ||
| 1011 | (* | |
| 14487 
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
 webertj parents: 
14460diff
changeset | 1012 | let | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 1013 | fun mysolver fm = ... | 
| 14487 
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
 webertj parents: 
14460diff
changeset | 1014 | in | 
| 22567 
1565d476a9e2
removed assert/deny (avoid clash with Alice keywords and confusion due to strict evaluation);
 wenzelm parents: 
22220diff
changeset | 1015 |   SatSolver.add_solver ("myname", (fn fm => if mysolver_is_configured then mysolver fm else raise SatSolver.NOT_CONFIGURED));  -- register the solver
 | 
| 14487 
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
 webertj parents: 
14460diff
changeset | 1016 | end; | 
| 14453 | 1017 | |
| 1018 | -- the solver is now available as SatSolver.invoke_solver "myname" | |
| 1019 | *) |