src/HOL/Tools/Nunchaku/nunchaku_commands.ML
author blanchet
Fri, 08 Sep 2017 00:02:24 +0200
changeset 66625 2cd22f070929
parent 66619 556e19e43e4d
child 66627 4145169ae609
permissions -rw-r--r--
added Kodkod-specific options to Nunchaku
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Nunchaku/Tools/nunchaku_commands.ML
66614
1f1c5d85d232 moved Nunchaku to Main; the goal is to move Nitpick out in the next 1-2 years
blanchet
parents: 66163
diff changeset
     2
    Author:     Jasmin Blanchette, VU Amsterdam
1f1c5d85d232 moved Nunchaku to Main; the goal is to move Nitpick out in the next 1-2 years
blanchet
parents: 66163
diff changeset
     3
    Copyright   2015, 2016, 2017
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     4
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     5
Adds the "nunchaku" and "nunchaku_params" commands to Isabelle/Isar's outer syntax.
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     6
*)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     7
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     8
signature NUNCHAKU_COMMANDS =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
     9
sig
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    10
  type params = Nunchaku.params
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    11
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    12
  val default_params: theory -> (string * string) list -> params
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    13
end;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    14
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    15
structure Nunchaku_Commands : NUNCHAKU_COMMANDS =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    16
struct
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    17
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    18
open Nunchaku_Util;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    19
open Nunchaku;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    20
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    21
type raw_param = string * string list;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    22
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    23
val default_default_params =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    24
  [("assms", "true"),
66625
2cd22f070929 added Kodkod-specific options to Nunchaku
blanchet
parents: 66619
diff changeset
    25
   ("bound_increment", "2"),
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    26
   ("debug", "false"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    27
   ("falsify", "true"),
66625
2cd22f070929 added Kodkod-specific options to Nunchaku
blanchet
parents: 66619
diff changeset
    28
   ("initial_bound", "2"),
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    29
   ("max_genuine", "1"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    30
   ("max_potential", "1"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    31
   ("overlord", "false"),
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
    32
   ("solvers", "cvc4 kodkod paradox smbc"),
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    33
   ("specialize", "true"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    34
   ("spy", "false"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    35
   ("timeout", "30"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    36
   ("verbose", "false"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    37
   ("wf_timeout", "0.5")];
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    38
66619
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    39
val alias_params =
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    40
  [("solver", "solvers")];
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    41
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    42
val negated_alias_params =
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    43
  [("dont_whack", "whack"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    44
   ("dont_specialize", "specialize"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    45
   ("dont_spy", "spy"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    46
   ("no_assms", "assms"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    47
   ("no_debug", "debug"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    48
   ("no_overlord", "overlord"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    49
   ("non_mono", "mono"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    50
   ("non_wf", "wf"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    51
   ("quiet", "verbose"),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    52
   ("satisfy", "falsify")];
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    53
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    54
fun is_known_raw_param s =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    55
  AList.defined (op =) default_default_params s orelse
66619
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    56
  AList.defined (op =) alias_params s orelse
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    57
  AList.defined (op =) negated_alias_params s orelse
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    58
  member (op =) ["atoms", "card", "eval", "expect"] s orelse
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    59
  exists (fn p => String.isPrefix (p ^ " ") s)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    60
    ["atoms", "card", "dont_whack", "mono", "non_mono", "non_wf", "wf", "whack"];
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    61
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    62
fun check_raw_param (s, _) =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    63
  if is_known_raw_param s then () else error ("Unknown parameter: " ^ quote s);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    64
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    65
fun unnegate_param_name name =
66619
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    66
  (case AList.lookup (op =) negated_alias_params name of
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    67
    NONE =>
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    68
    if String.isPrefix "dont_" name then SOME (unprefix "dont_" name)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    69
    else if String.isPrefix "non_" name then SOME (unprefix "non_" name)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    70
    else NONE
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    71
  | some_name => some_name);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    72
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    73
fun normalize_raw_param (name, value) =
66619
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    74
  (case AList.lookup (op =) alias_params name of
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    75
    SOME alias => [(alias, value)]
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    76
  | NONE =>
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    77
    (case unnegate_param_name name of
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    78
      SOME name' =>
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    79
      [(name',
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    80
        (case value of
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    81
          ["false"] => ["true"]
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    82
        | ["true"] => ["false"]
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    83
        | [] => ["false"]
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    84
        | _ => value))]
556e19e43e4d added singular 'solver' option to Nunchaku
blanchet
parents: 66614
diff changeset
    85
    | NONE => [(name, value)]));
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    86
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    87
structure Data = Theory_Data
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    88
(
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    89
  type T = raw_param list
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    90
  val empty = default_default_params |> map (apsnd single)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    91
  val extend = I
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    92
  fun merge data = AList.merge (op =) (K true) data
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    93
);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    94
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    95
val set_default_raw_param = Data.map o fold (AList.update (op =)) o normalize_raw_param;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    96
val default_raw_params = Data.get;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    97
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    98
fun is_punctuation s = (s = "," orelse s = "-");
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
    99
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   100
fun stringify_raw_param_value [] = ""
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   101
  | stringify_raw_param_value [s] = s
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   102
  | stringify_raw_param_value (s1 :: s2 :: ss) =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   103
    s1 ^ (if is_punctuation s1 orelse is_punctuation s2 then "" else " ") ^
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   104
    stringify_raw_param_value (s2 :: ss);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   105
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   106
fun extract_params ctxt mode default_params override_params =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   107
  let
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   108
    val override_params = maps normalize_raw_param override_params;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   109
    val raw_params = rev override_params @ rev default_params;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   110
    val raw_lookup = AList.lookup (op =) raw_params;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   111
    val lookup = Option.map stringify_raw_param_value o raw_lookup;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   112
    val lookup_string = the_default "" o lookup;
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   113
    val lookup_strings = these o Option.map (space_explode " ") o lookup;
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   114
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   115
    fun general_lookup_bool option default_value name =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   116
      (case lookup name of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   117
        SOME s => parse_bool_option option name s
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   118
      | NONE => default_value);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   119
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   120
    val lookup_bool = the o general_lookup_bool false (SOME false);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   121
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   122
    fun lookup_int name =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   123
      (case lookup name of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   124
        SOME s =>
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   125
        (case Int.fromString s of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   126
          SOME i => i
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   127
        | NONE => error ("Parameter " ^ quote name ^ " must be assigned an integer value"))
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   128
      | NONE => 0);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   129
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   130
    fun int_range_from_string name s =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   131
      (case space_explode "-" s of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   132
         [s] => (s, s)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   133
       | [s1, s2] => (s1, s2)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   134
       | _ => error ("Parameter " ^ quote name ^ " must be assigned a range of integers"))
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   135
      |> apply2 Int.fromString;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   136
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   137
    fun lookup_assigns read pre of_str =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   138
      (case lookup pre of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   139
        SOME s => [(NONE, of_str s)]
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   140
      | NONE => []) @
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   141
      map (fn (name, value) => (SOME (read (String.extract (name, size pre + 1, NONE))),
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   142
          of_str (stringify_raw_param_value value)))
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   143
        (filter (String.isPrefix (pre ^ " ") o fst) raw_params);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   144
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   145
    fun lookup_int_range_assigns read pre =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   146
      lookup_assigns read pre (int_range_from_string pre);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   147
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   148
    fun lookup_bool_assigns read pre =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   149
      lookup_assigns read pre (the o parse_bool_option false pre);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   150
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   151
    fun lookup_bool_option_assigns read pre =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   152
      lookup_assigns read pre (parse_bool_option true pre);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   153
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   154
    fun lookup_strings_assigns read pre =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   155
      lookup_assigns read pre (space_explode " ");
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   156
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   157
    fun lookup_time name =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   158
      (case lookup name of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   159
        SOME s => parse_time name s
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   160
      | NONE => Time.zeroTime);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   161
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   162
    val read_type_polymorphic =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   163
      Syntax.read_typ ctxt #> Logic.mk_type
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   164
      #> singleton (Variable.polymorphic ctxt) #> Logic.dest_type;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   165
    val read_term_polymorphic =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   166
      Syntax.read_term ctxt #> singleton (Variable.polymorphic ctxt);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   167
    val lookup_term_list_option_polymorphic =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   168
      AList.lookup (op =) raw_params #> Option.map (map read_term_polymorphic);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   169
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   170
    fun read_const_polymorphic s =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   171
      (case read_term_polymorphic s of
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   172
        Const x => x
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   173
      | t => error ("Not a constant: " ^ Syntax.string_of_term ctxt t));
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   174
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   175
    val solvers = lookup_strings "solvers";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   176
    val falsify = lookup_bool "falsify";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   177
    val assms = lookup_bool "assms";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   178
    val spy = getenv "NUNCHAKU_SPY" = "yes" orelse lookup_bool "spy";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   179
    val overlord = lookup_bool "overlord";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   180
    val expect = lookup_string "expect";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   181
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   182
    val wfs = lookup_bool_option_assigns read_const_polymorphic "wf";
66625
2cd22f070929 added Kodkod-specific options to Nunchaku
blanchet
parents: 66619
diff changeset
   183
    val initial_bound = lookup_int "initial_bound";
2cd22f070929 added Kodkod-specific options to Nunchaku
blanchet
parents: 66619
diff changeset
   184
    val bound_increment = lookup_int "bound_increment";
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   185
    val whacks = lookup_bool_assigns read_term_polymorphic "whack";
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   186
    val cards = lookup_int_range_assigns read_type_polymorphic "card";
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   187
    val monos = lookup_bool_option_assigns read_type_polymorphic "mono";
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   188
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   189
    val debug = (mode <> Auto_Try andalso lookup_bool "debug");
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   190
    val verbose = debug orelse (mode <> Auto_Try andalso lookup_bool "verbose");
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   191
    val max_potential = if mode = Normal then Int.max (0, lookup_int "max_potential") else 0;
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   192
    val max_genuine = Int.max (0, lookup_int "max_genuine");
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   193
    val evals = these (lookup_term_list_option_polymorphic "eval");
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   194
    val atomss = lookup_strings_assigns read_type_polymorphic "atoms";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   195
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   196
    val specialize = lookup_bool "specialize";
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   197
    val multithread = mode = Normal andalso lookup_bool "multithread";
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   198
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   199
    val timeout = lookup_time "timeout";
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   200
    val wf_timeout = lookup_time "wf_timeout";
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   201
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   202
    val mode_of_operation_params =
66163
45d3d43abee7 added 'solvers' option to Nunchaku
blanchet
parents: 64389
diff changeset
   203
      {solvers = solvers, falsify = falsify, assms = assms, spy = spy, overlord = overlord,
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   204
       expect = expect};
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   205
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   206
    val scope_of_search_params =
66625
2cd22f070929 added Kodkod-specific options to Nunchaku
blanchet
parents: 66619
diff changeset
   207
      {wfs = wfs, whacks = whacks, initial_bound = initial_bound, bound_increment = bound_increment,
2cd22f070929 added Kodkod-specific options to Nunchaku
blanchet
parents: 66619
diff changeset
   208
       cards = cards, monos = monos};
64389
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   209
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   210
    val output_format_params =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   211
      {verbose = verbose, debug = debug, max_potential = max_potential, max_genuine = max_genuine,
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   212
       evals = evals, atomss = atomss};
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   213
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   214
    val optimization_params =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   215
      {specialize = specialize, multithread = multithread};
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   216
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   217
    val timeout_params =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   218
      {timeout = timeout, wf_timeout = wf_timeout};
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   219
  in
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   220
    {mode_of_operation_params = mode_of_operation_params,
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   221
     scope_of_search_params = scope_of_search_params,
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   222
     output_format_params = output_format_params,
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   223
     optimization_params = optimization_params,
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   224
     timeout_params = timeout_params}
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   225
  end;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   226
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   227
fun default_params thy =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   228
  extract_params (Proof_Context.init_global thy) Normal (default_raw_params thy)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   229
  o map (apsnd single);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   230
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   231
val parse_key = Scan.repeat1 Parse.embedded >> space_implode " ";
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   232
val parse_value =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   233
  Scan.repeat1 (Parse.minus >> single
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   234
    || Scan.repeat1 (Scan.unless Parse.minus (Parse.name || Parse.float_number))
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   235
    || @{keyword ","} |-- Parse.number >> prefix "," >> single)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   236
  >> flat;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   237
val parse_param = parse_key -- Scan.optional (@{keyword "="} |-- parse_value) [];
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   238
val parse_params = Scan.optional (@{keyword "["} |-- Parse.list parse_param --| @{keyword "]"}) [];
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   239
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   240
fun run_chaku override_params mode i state0 =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   241
  let
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   242
    val state = Proof.map_contexts (Try0.silence_methods false) state0;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   243
    val thy = Proof.theory_of state;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   244
    val ctxt = Proof.context_of state;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   245
    val _ = List.app check_raw_param override_params;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   246
    val params = extract_params ctxt mode (default_raw_params thy) override_params;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   247
  in
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   248
    (if mode = Auto_Try then perhaps o try else fn f => fn x => f x)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   249
      (fn _ => run_chaku_on_subgoal state params mode i) (unknownN, NONE)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   250
    |> `(fn (outcome_code, _) => outcome_code = genuineN)
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   251
  end;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   252
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   253
fun string_for_raw_param (name, value) =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   254
  name ^ " = " ^ stringify_raw_param_value value;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   255
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   256
fun nunchaku_params_trans params =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   257
  Toplevel.theory (fold set_default_raw_param params
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   258
    #> tap (fn thy =>
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   259
      let val params = rev (default_raw_params thy) in
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   260
        List.app check_raw_param params;
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   261
        writeln ("Default parameters for Nunchaku:\n" ^
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   262
          (params |> map string_for_raw_param |> sort_strings |> cat_lines))
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   263
      end));
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   264
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   265
val _ =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   266
  Outer_Syntax.command @{command_keyword nunchaku}
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   267
    "try to find a countermodel using Nunchaku"
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   268
    (parse_params -- Scan.optional Parse.nat 1 >> (fn (params, i) =>
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   269
       Toplevel.keep_proof (fn state =>
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   270
         ignore (run_chaku params Normal i (Toplevel.proof_of state)))));
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   271
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   272
val _ =
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   273
  Outer_Syntax.command @{command_keyword nunchaku_params}
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   274
    "set and display the default parameters for Nunchaku"
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   275
    (parse_params #>> nunchaku_params_trans);
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   276
6273d4c8325b added Nunchaku integration
blanchet
parents:
diff changeset
   277
end;