src/HOL/Tools/Nitpick/kodkod_sat.ML
author blanchet
Thu, 04 Feb 2010 13:36:52 +0100
changeset 34998 5e492a862b34
parent 34982 7b8c366e34a2
child 35078 6fd1052fe463
permissions -rw-r--r--
four changes to Nitpick: 1. avoid writing absolute paths in Kodkodi files for input/output files of external SAT solvers (e.g. MiniSat), to dodge Cygwin problems 2. do eta-contraction in the monotonicity check 3. improved quantifier massaging algorithms using ideas from Paradox 4. repaired "check_potential" and "check_genuine"
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
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    12
  val sat_solver_spec : bool -> 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
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    29
val berkmin_exec = getenv "BERKMIN_EXE"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    30
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    31
(* (string * sat_solver_info) list *)
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    32
val static_list =
33731
040852c71779 change the order in which Nitpick tries SAT solvers;
blanchet
parents: 33232
diff changeset
    33
  [("MiniSat", ExternalV2 (ToFile, "MINISAT_HOME", "minisat", [], "SAT", "",
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    34
                           "UNSAT")),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    35
   ("PicoSAT", External (ToStdout, "PICOSAT_HOME", "picosat", [])),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    36
   ("zChaff", ExternalV2 (ToStdout, "ZCHAFF_HOME", "zchaff", [],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    37
                          "Instance Satisfiable", "",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    38
                          "Instance Unsatisfiable")),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    39
   ("RSat", ExternalV2 (ToStdout, "RSAT_HOME", "rsat", ["-s"],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    40
                        "s SATISFIABLE", "v ", "s UNSATISFIABLE")),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    41
   ("BerkMin", ExternalV2 (ToStdout, "BERKMIN_HOME",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    42
                           if berkmin_exec = "" then "BerkMin561"
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    43
                           else berkmin_exec, [], "Satisfiable          !!",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    44
                           "solution =", "UNSATISFIABLE          !!")),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    45
   ("BerkMinAlloy", External (ToStdout, "BERKMINALLOY_HOME", "berkmin", [])),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    46
   ("Jerusat", External (ToStdout, "JERUSAT_HOME", "Jerusat1.3", [])),
33731
040852c71779 change the order in which Nitpick tries SAT solvers;
blanchet
parents: 33232
diff changeset
    47
   ("MiniSatJNI", Internal (JNI, Incremental, ["MiniSat"])),
040852c71779 change the order in which Nitpick tries SAT solvers;
blanchet
parents: 33232
diff changeset
    48
   ("zChaffJNI", Internal (JNI, Batch, ["zChaff"])),
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
    49
   ("SAT4J", Internal (Java, Incremental, ["DefaultSAT4J"])),
fba7527c3ef1 made Nitpick aware of the KODKODI_JAVA_LIBRARY_PATH, for detecting and properly invoking JNI-based SAT solvers
blanchet
parents: 33192
diff changeset
    50
   ("SAT4JLight", Internal (Java, Incremental, ["LightSAT4J"])),
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    51
   ("HaifaSat", ExternalV2 (ToStdout, "HAIFASAT_HOME", "HaifaSat", ["-p", "1"],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    52
                            "s SATISFIABLE", "v ", "s UNSATISFIABLE"))]
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    53
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    54
(* bool -> string -> sink -> string -> string -> string list -> string list
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    55
   -> (string * (unit -> string list)) option *)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    56
fun dynamic_entry_for_external overlord name dev home exec args markers =
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    57
  case getenv home of
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    58
    "" => NONE
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    59
  | dir =>
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    60
    SOME (name, fn () =>
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    61
                   let
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    62
                     val serial_str = if overlord then "" else serial_string ()
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    63
                     val base = name ^ serial_str
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    64
                     val out_file = base ^ ".out"
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    65
                     val dir_sep =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    66
                       if String.isSubstring "\\" dir andalso
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    67
                          not (String.isSubstring "/" dir) then
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    68
                         "\\"
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    69
                       else
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    70
                         "/"
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    71
                   in
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    72
                     [if null markers then "External" else "ExternalV2",
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    73
                      dir ^ dir_sep ^ exec, base ^ ".cnf",
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    74
                      if dev = ToFile then out_file else ""] @ markers @
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    75
                      (if dev = ToFile then [out_file] else []) @ args
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    76
                   end)
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    77
(* bool -> bool -> string * sat_solver_info
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    78
   -> (string * (unit -> string list)) option *)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    79
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
    80
    if incremental andalso mode = Batch then NONE else SOME (name, K ss)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    81
  | 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
    82
    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
    83
      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
    84
    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
    85
      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
    86
        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
    87
                        |> 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
    88
      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
    89
        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
    90
                  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
    91
          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
    92
        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
    93
          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
    94
      end
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    95
  | dynamic_entry_for_info overlord false
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    96
    (name, External (dev, home, exec, args)) =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    97
    dynamic_entry_for_external overlord name dev home exec args []
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    98
  | dynamic_entry_for_info overlord false
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    99
        (name, ExternalV2 (dev, home, exec, args, m1, m2, m3)) =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   100
    dynamic_entry_for_external overlord name dev home exec args [m1, m2, m3]
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   101
  | dynamic_entry_for_info _ true _ = NONE
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   102
(* bool -> bool -> (string * (unit -> string list)) list *)
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   103
fun dynamic_list overlord incremental =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   104
  map_filter (dynamic_entry_for_info overlord incremental) static_list
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   105
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   106
(* bool -> string list *)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   107
val configured_sat_solvers = map fst o dynamic_list false
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   108
(* bool -> string *)
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   109
val smart_sat_solver_name = fst o hd o dynamic_list false
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   110
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   111
(* bool -> string -> string * string list *)
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   112
fun sat_solver_spec overlord name =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   113
  let
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   114
    val dyn_list = dynamic_list overlord false
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   115
    (* (string * 'a) list -> string *)
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   116
    fun enum_solvers solvers =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   117
      commas (distinct (op =) (map (quote o fst) solvers))
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   118
  in
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   119
    (name, the (AList.lookup (op =) dyn_list name) ())
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   120
    handle Option.Option =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   121
           error (if AList.defined (op =) static_list name then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   122
                    "The SAT solver " ^ quote name ^ " is not configured. The \
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   123
                    \following solvers are configured:\n" ^
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
   124
                    enum_solvers dyn_list ^ "."
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   125
                  else
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   126
                    "Unknown SAT solver " ^ quote name ^ ". The following \
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   127
                    \solvers are supported:\n" ^ enum_solvers static_list ^ ".")
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   128
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   129
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
   130
end;