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