src/HOL/Tools/Nitpick/kodkod_sat.ML
author blanchet
Sat, 31 Jul 2010 22:02:54 +0200
changeset 38125 b178a63df952
parent 38122 fb5e5a425948
child 39221 70fd4a3c41ed
permissions -rw-r--r--
change the order of the SAT solvers, from fastest to slowest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33982
1ae222745c4a fixed paths in Nitpick's ML file headers
blanchet
parents: 33731
diff changeset
     1
(*  Title:      HOL/Tools/Nitpick/kodkod_sat.ML
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
34982
7b8c366e34a2 added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents: 33982
diff changeset
     3
    Copyright   2009, 2010
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     4
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     5
Kodkod SAT solver integration.
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     6
*)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     7
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     8
signature KODKOD_SAT =
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
     9
sig
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    10
  val configured_sat_solvers : bool -> string list
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    11
  val smart_sat_solver_name : bool -> string
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    12
  val sat_solver_spec : string -> string * string list
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    13
end;
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    14
33232
f93390060bbe internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents: 33229
diff changeset
    15
structure Kodkod_SAT : KODKOD_SAT =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    16
struct
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    17
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    18
open Kodkod
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    19
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    20
datatype sink = ToStdout | ToFile
33229
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    21
datatype availability = Java | JNI
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    22
datatype mode = Batch | Incremental
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    23
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    24
datatype sat_solver_info =
33229
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    25
  Internal of availability * mode * string list |
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    26
  External of sink * string * string * string list |
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    27
  ExternalV2 of sink * string * string * string list * string * string * string
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    28
38125
b178a63df952 change the order of the SAT solvers, from fastest to slowest
blanchet
parents: 38122
diff changeset
    29
(* for compatibility with "SatSolver" *)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    30
val berkmin_exec = getenv "BERKMIN_EXE"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    31
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    32
(* (string * sat_solver_info) list *)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    33
val static_list =
38125
b178a63df952 change the order of the SAT solvers, from fastest to slowest
blanchet
parents: 38122
diff changeset
    34
  [("MiniSat", ExternalV2 (ToFile, "MINISAT_HOME", "minisat", [], "SAT", "",
b178a63df952 change the order of the SAT solvers, from fastest to slowest
blanchet
parents: 38122
diff changeset
    35
                           "UNSAT")),
b178a63df952 change the order of the SAT solvers, from fastest to slowest
blanchet
parents: 38122
diff changeset
    36
   ("MiniSat_JNI", Internal (JNI, Incremental, ["MiniSat"])),
b178a63df952 change the order of the SAT solvers, from fastest to slowest
blanchet
parents: 38122
diff changeset
    37
   ("CryptoMiniSat", External (ToStdout, "CRYPTOMINISAT_HOME", "cryptominisat",
38122
fb5e5a425948 added support for CryptoMiniSat
blanchet
parents: 36923
diff changeset
    38
                               [])),
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    39
   ("PicoSAT", External (ToStdout, "PICOSAT_HOME", "picosat", [])),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    40
   ("zChaff", ExternalV2 (ToStdout, "ZCHAFF_HOME", "zchaff", [],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    41
                          "Instance Satisfiable", "",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    42
                          "Instance Unsatisfiable")),
38125
b178a63df952 change the order of the SAT solvers, from fastest to slowest
blanchet
parents: 38122
diff changeset
    43
   ("zChaff_JNI", Internal (JNI, Batch, ["zChaff"])),
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    44
   ("RSat", ExternalV2 (ToStdout, "RSAT_HOME", "rsat", ["-s"],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    45
                        "s SATISFIABLE", "v ", "s UNSATISFIABLE")),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    46
   ("BerkMin", ExternalV2 (ToStdout, "BERKMIN_HOME",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    47
                           if berkmin_exec = "" then "BerkMin561"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    48
                           else berkmin_exec, [], "Satisfiable          !!",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    49
                           "solution =", "UNSATISFIABLE          !!")),
35078
6fd1052fe463 optimization to quantifiers in Nitpick's handling of simp rules + renamed some SAT solvers
blanchet
parents: 34998
diff changeset
    50
   ("BerkMin_Alloy", External (ToStdout, "BERKMINALLOY_HOME", "berkmin", [])),
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    51
   ("Jerusat", External (ToStdout, "JERUSAT_HOME", "Jerusat1.3", [])),
33229
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    52
   ("SAT4J", Internal (Java, Incremental, ["DefaultSAT4J"])),
36923
538cf3fdfe4d remove support for crashing beta solver HaifaSat
blanchet
parents: 36385
diff changeset
    53
   ("SAT4J_Light", Internal (Java, Incremental, ["LightSAT4J"]))]
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    54
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    55
fun dynamic_entry_for_external name dev home exec args markers =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    56
  case getenv home of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    57
    "" => NONE
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    58
  | dir =>
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    59
    SOME (name, fn () =>
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    60
                   let
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    61
                     val serial_str = serial_string ()
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    62
                     val base = name ^ serial_str
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    63
                     val out_file = base ^ ".out"
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    64
                     val dir_sep =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    65
                       if String.isSubstring "\\" dir andalso
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    66
                          not (String.isSubstring "/" dir) then
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    67
                         "\\"
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    68
                       else
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    69
                         "/"
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    70
                   in
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    71
                     [if null markers then "External" else "ExternalV2",
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    72
                      dir ^ dir_sep ^ exec, base ^ ".cnf",
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    73
                      if dev = ToFile then out_file else ""] @ markers @
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    74
                      (if dev = ToFile then [out_file] else []) @ args
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    75
                   end)
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    76
fun dynamic_entry_for_info incremental (name, Internal (Java, mode, ss)) =
33229
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    77
    if incremental andalso mode = Batch then NONE else SOME (name, K ss)
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    78
  | dynamic_entry_for_info incremental (name, Internal (JNI, mode, ss)) =
33229
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    79
    if incremental andalso mode = Batch then
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    80
      NONE
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    81
    else
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    82
      let
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    83
        val lib_paths = getenv "KODKODI_JAVA_LIBRARY_PATH"
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    84
                        |> space_explode ":"
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    85
      in
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    86
        if exists (fn path => File.exists (Path.explode (path ^ "/")))
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    87
                  lib_paths then
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    88
          SOME (name, K ss)
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    89
        else
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    90
          NONE
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    91
      end
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    92
  | dynamic_entry_for_info false (name, External (dev, home, exec, args)) =
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    93
    dynamic_entry_for_external name dev home exec args []
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    94
  | dynamic_entry_for_info false
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    95
        (name, ExternalV2 (dev, home, exec, args, m1, m2, m3)) =
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    96
    dynamic_entry_for_external name dev home exec args [m1, m2, m3]
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    97
  | dynamic_entry_for_info true _ = NONE
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    98
fun dynamic_list incremental =
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    99
  map_filter (dynamic_entry_for_info incremental) static_list
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   100
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
   101
val configured_sat_solvers = map fst o dynamic_list
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
   102
val smart_sat_solver_name = fst o hd o dynamic_list
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   103
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
   104
fun sat_solver_spec name =
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   105
  let
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
   106
    val dyn_list = dynamic_list false
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   107
    fun enum_solvers solvers =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   108
      commas (distinct (op =) (map (quote o fst) solvers))
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   109
  in
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   110
    (name, the (AList.lookup (op =) dyn_list name) ())
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   111
    handle Option.Option =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   112
           error (if AList.defined (op =) static_list name then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   113
                    "The SAT solver " ^ quote name ^ " is not configured. The \
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   114
                    \following solvers are configured:\n" ^
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   115
                    enum_solvers dyn_list ^ "."
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   116
                  else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   117
                    "Unknown SAT solver " ^ quote name ^ ". The following \
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   118
                    \solvers are supported:\n" ^ enum_solvers static_list ^ ".")
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   119
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   120
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   121
end;