src/HOL/Tools/Nitpick/kodkod_sat.ML
author blanchet
Mon, 11 Jul 2022 15:04:04 +0200
changeset 75665 707748d3d186
parent 74640 85d8d9eeb4e1
child 76501 7956b822f239
permissions -rw-r--r--
prefer non-JNI SAT solvers by default in Nitpick
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
74640
85d8d9eeb4e1 discontinued pointless check of kodkodi_version, it is implicit in the bundled component;
wenzelm
parents: 74489
diff changeset
    21
datatype availability = Java | JNI
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
    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 |
45077
3cb902212af5 update list of SAT solvers reflecting Kodkod 1.5
blanchet
parents: 39221
diff changeset
    26
  External of string * string * string list |
33192
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
56853
a265e41cc33b tuned structure name
blanchet
parents: 55889
diff changeset
    29
(* for compatibility with "SAT_Solver" *)
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
val static_list =
75665
707748d3d186 prefer non-JNI SAT solvers by default in Nitpick
blanchet
parents: 74640
diff changeset
    33
  [("CryptoMiniSat", External ("CRYPTOMINISAT_HOME", "cryptominisat", [])),
74478
3984b1e91df6 tuned whitespace;
wenzelm
parents: 67560
diff changeset
    34
   ("MiniSat", ExternalV2 (ToFile, "MINISAT_HOME", "minisat", [], "SAT", "", "UNSAT")),
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    35
   ("zChaff", ExternalV2 (ToStdout, "ZCHAFF_HOME", "zchaff", [],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    36
                          "Instance Satisfiable", "",
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    37
                          "Instance Unsatisfiable")),
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    38
   ("RSat", ExternalV2 (ToStdout, "RSAT_HOME", "rsat", ["-s"],
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    39
                        "s SATISFIABLE", "v ", "s UNSATISFIABLE")),
54608
48dadf69c44d added Riss3g
blanchet
parents: 50489
diff changeset
    40
   ("Riss3g", External ("RISS3G_HOME", "riss3g", [])),
33192
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          !!")),
45077
3cb902212af5 update list of SAT solvers reflecting Kodkod 1.5
blanchet
parents: 39221
diff changeset
    45
   ("BerkMin_Alloy", External ("BERKMINALLOY_HOME", "berkmin", [])),
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
    46
   ("SAT4J", Internal (Java, Incremental, ["DefaultSAT4J"])),
75665
707748d3d186 prefer non-JNI SAT solvers by default in Nitpick
blanchet
parents: 74640
diff changeset
    47
   ("SAT4J_Light", Internal (Java, Incremental, ["LightSAT4J"])),
707748d3d186 prefer non-JNI SAT solvers by default in Nitpick
blanchet
parents: 74640
diff changeset
    48
   ("Lingeling_JNI", Internal (JNI, Batch, ["Lingeling"])),
707748d3d186 prefer non-JNI SAT solvers by default in Nitpick
blanchet
parents: 74640
diff changeset
    49
   ("CryptoMiniSat_JNI", Internal (JNI, Batch, ["CryptoMiniSat"])),
707748d3d186 prefer non-JNI SAT solvers by default in Nitpick
blanchet
parents: 74640
diff changeset
    50
   ("MiniSat_JNI", Internal (JNI, Incremental, ["MiniSat"]))]
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    51
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    52
fun dynamic_entry_for_external name dev home exec args markers =
74479
wenzelm
parents: 74478
diff changeset
    53
  let
74481
9333a6ee57ba proper platform_path for executables run from Java;
wenzelm
parents: 74479
diff changeset
    54
    fun make_args () =
74486
74a36aae067a updated to kodkodi-1.5.7. with more robust/portable management of files and processes;
wenzelm
parents: 74481
diff changeset
    55
      let val inpath = name ^ serial_string () ^ ".cnf" in
74a36aae067a updated to kodkodi-1.5.7. with more robust/portable management of files and processes;
wenzelm
parents: 74481
diff changeset
    56
        [if null markers then "External" else "ExternalV2"] @
74a36aae067a updated to kodkodi-1.5.7. with more robust/portable management of files and processes;
wenzelm
parents: 74481
diff changeset
    57
        [File.platform_path (Path.variable home + Path.platform_exe (Path.basic exec))] @
74a36aae067a updated to kodkodi-1.5.7. with more robust/portable management of files and processes;
wenzelm
parents: 74481
diff changeset
    58
        [inpath] @ (if null markers then [] else [if dev = ToFile then "out" else ""]) @
74a36aae067a updated to kodkodi-1.5.7. with more robust/portable management of files and processes;
wenzelm
parents: 74481
diff changeset
    59
        markers @ args
74479
wenzelm
parents: 74478
diff changeset
    60
      end
74481
9333a6ee57ba proper platform_path for executables run from Java;
wenzelm
parents: 74479
diff changeset
    61
  in if getenv home = "" then NONE else SOME (name, make_args) end
55889
6bfbec3dff62 tuned code
blanchet
parents: 54608
diff changeset
    62
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    63
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
    64
    if incremental andalso mode = Batch then NONE else SOME (name, K ss)
74640
85d8d9eeb4e1 discontinued pointless check of kodkodi_version, it is implicit in the bundled component;
wenzelm
parents: 74489
diff changeset
    65
  | dynamic_entry_for_info incremental (name, Internal (JNI, mode, ss)) =
85d8d9eeb4e1 discontinued pointless check of kodkodi_version, it is implicit in the bundled component;
wenzelm
parents: 74489
diff changeset
    66
    if incremental andalso mode = Batch then NONE
74489
d219a959b951 clarified test of directories;
wenzelm
parents: 74486
diff changeset
    67
    else if exists File.is_dir (Path.split (getenv "KODKODI_JAVA_LIBRARY_PATH"))
d219a959b951 clarified test of directories;
wenzelm
parents: 74486
diff changeset
    68
    then SOME (name, K ss) else NONE
45077
3cb902212af5 update list of SAT solvers reflecting Kodkod 1.5
blanchet
parents: 39221
diff changeset
    69
  | dynamic_entry_for_info false (name, External (home, exec, args)) =
3cb902212af5 update list of SAT solvers reflecting Kodkod 1.5
blanchet
parents: 39221
diff changeset
    70
    dynamic_entry_for_external name ToStdout home exec args []
74640
85d8d9eeb4e1 discontinued pointless check of kodkodi_version, it is implicit in the bundled component;
wenzelm
parents: 74489
diff changeset
    71
  | dynamic_entry_for_info false (name, ExternalV2 (dev, home, exec, args, m1, m2, m3)) =
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    72
    dynamic_entry_for_external name dev home exec args [m1, m2, m3]
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    73
  | dynamic_entry_for_info true _ = NONE
55889
6bfbec3dff62 tuned code
blanchet
parents: 54608
diff changeset
    74
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    75
fun dynamic_list incremental =
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    76
  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
    77
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    78
val configured_sat_solvers = map fst o dynamic_list
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    79
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
    80
35177
168041f24f80 various cosmetic changes to Nitpick
blanchet
parents: 35078
diff changeset
    81
fun sat_solver_spec name =
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    82
  let
50489
blanchet
parents: 50488
diff changeset
    83
    val dyns = dynamic_list false
34998
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    84
    fun enum_solvers solvers =
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    85
      commas (distinct (op =) (map (quote o fst) solvers))
5e492a862b34 four changes to Nitpick:
blanchet
parents: 34982
diff changeset
    86
  in
50489
blanchet
parents: 50488
diff changeset
    87
    (name, the (AList.lookup (op =) dyns name) ())
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    88
    handle Option.Option =>
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    89
           error (if AList.defined (op =) static_list name then
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    90
                    "The SAT solver " ^ quote name ^ " is not configured. The \
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    91
                    \following solvers are configured:\n" ^
63693
5b02f7757a4c removed trailing final stops in Nitpick messages
blanchet
parents: 56853
diff changeset
    92
                    enum_solvers dyns
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    93
                  else
63693
5b02f7757a4c removed trailing final stops in Nitpick messages
blanchet
parents: 56853
diff changeset
    94
                    "Unknown SAT solver " ^ quote name ^ "\nThe following \
5b02f7757a4c removed trailing final stops in Nitpick messages
blanchet
parents: 56853
diff changeset
    95
                    \solvers are supported:\n" ^ enum_solvers static_list)
33192
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    96
  end
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    97
08a39a957ed7 added Nitpick's theory and ML files to Isabelle/HOL;
blanchet
parents:
diff changeset
    98
end;