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