src/HOL/Tools/Nitpick/nitpick_isar.ML
author wenzelm
Mon, 17 May 2010 23:54:15 +0200
changeset 36970 01594f816e3a
parent 36633 bafd82950e24
child 37163 f69efa106feb
permissions -rw-r--r--
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
eliminated old-style structure aliases K = Keyword, P = Parse;
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_isar.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34969
     3
    Copyright   2008, 2009, 2010
blanchet@33192
     4
blanchet@33192
     5
Adds the "nitpick" and "nitpick_params" commands to Isabelle/Isar's outer
blanchet@33192
     6
syntax.
blanchet@33192
     7
*)
blanchet@33192
     8
blanchet@33192
     9
signature NITPICK_ISAR =
blanchet@33192
    10
sig
blanchet@33192
    11
  type params = Nitpick.params
blanchet@33192
    12
blanchet@33552
    13
  val auto: bool Unsynchronized.ref
blanchet@33192
    14
  val default_params : theory -> (string * string) list -> params
blanchet@33552
    15
  val setup : theory -> theory
blanchet@35866
    16
end;
blanchet@33192
    17
blanchet@33224
    18
structure Nitpick_Isar : NITPICK_ISAR =
blanchet@33192
    19
struct
blanchet@33192
    20
blanchet@33224
    21
open Nitpick_Util
blanchet@33224
    22
open Nitpick_HOL
blanchet@33224
    23
open Nitpick_Rep
blanchet@33224
    24
open Nitpick_Nut
blanchet@33192
    25
open Nitpick
blanchet@33192
    26
blanchet@33552
    27
val auto = Unsynchronized.ref false;
blanchet@33552
    28
wenzelm@33601
    29
val _ =
wenzelm@33601
    30
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@34969
    31
      (Preferences.bool_pref auto "auto-nitpick"
blanchet@34969
    32
           "Whether to run Nitpick automatically.")
blanchet@33552
    33
blanchet@33192
    34
type raw_param = string * string list
blanchet@33192
    35
blanchet@33192
    36
val default_default_params =
blanchet@35962
    37
  [("card", "1\<midarrow>8"),
blanchet@35962
    38
   ("iter", "0,1,2,4,8,12,16,24"),
blanchet@35962
    39
   ("bits", "1,2,3,4,6,8,10,12"),
blanchet@35962
    40
   ("bisim_depth", "7"),
blanchet@35962
    41
   ("box", "smart"),
blanchet@35962
    42
   ("finitize", "smart"),
blanchet@35962
    43
   ("mono", "smart"),
blanchet@35962
    44
   ("std", "true"),
blanchet@35962
    45
   ("wf", "smart"),
blanchet@35962
    46
   ("sat_solver", "smart"),
blanchet@35962
    47
   ("batch_size", "smart"),
blanchet@35962
    48
   ("blocking", "true"),
blanchet@35962
    49
   ("falsify", "true"),
blanchet@35962
    50
   ("user_axioms", "smart"),
blanchet@35962
    51
   ("assms", "true"),
blanchet@35962
    52
   ("merge_type_vars", "false"),
blanchet@35962
    53
   ("binary_ints", "smart"),
blanchet@35962
    54
   ("destroy_constrs", "true"),
blanchet@35962
    55
   ("specialize", "true"),
blanchet@35962
    56
   ("star_linear_preds", "true"),
blanchet@35962
    57
   ("fast_descrs", "true"),
blanchet@35962
    58
   ("peephole_optim", "true"),
blanchet@35962
    59
   ("timeout", "30 s"),
blanchet@35962
    60
   ("tac_timeout", "500 ms"),
blanchet@35962
    61
   ("max_threads", "0"),
blanchet@35962
    62
   ("debug", "false"),
blanchet@35962
    63
   ("verbose", "false"),
blanchet@35962
    64
   ("overlord", "false"),
blanchet@35962
    65
   ("show_all", "false"),
blanchet@35962
    66
   ("show_datatypes", "false"),
blanchet@35962
    67
   ("show_consts", "false"),
blanchet@35962
    68
   ("format", "1"),
blanchet@35962
    69
   ("max_potential", "1"),
blanchet@35962
    70
   ("max_genuine", "1"),
blanchet@35962
    71
   ("check_potential", "false"),
blanchet@35962
    72
   ("check_genuine", "false")]
blanchet@33192
    73
blanchet@33192
    74
val negated_params =
blanchet@33192
    75
  [("dont_box", "box"),
blanchet@35665
    76
   ("dont_finitize", "finitize"),
blanchet@33192
    77
   ("non_mono", "mono"),
blanchet@34969
    78
   ("non_std", "std"),
blanchet@33192
    79
   ("non_wf", "wf"),
blanchet@33192
    80
   ("non_blocking", "blocking"),
blanchet@33192
    81
   ("satisfy", "falsify"),
blanchet@33192
    82
   ("no_user_axioms", "user_axioms"),
blanchet@33192
    83
   ("no_assms", "assms"),
blanchet@33547
    84
   ("dont_merge_type_vars", "merge_type_vars"),
blanchet@34121
    85
   ("unary_ints", "binary_ints"),
blanchet@33192
    86
   ("dont_destroy_constrs", "destroy_constrs"),
blanchet@33192
    87
   ("dont_specialize", "specialize"),
blanchet@33192
    88
   ("dont_star_linear_preds", "star_linear_preds"),
blanchet@33192
    89
   ("full_descrs", "fast_descrs"),
blanchet@33192
    90
   ("no_peephole_optim", "peephole_optim"),
blanchet@35962
    91
   ("no_debug", "debug"),
blanchet@33192
    92
   ("quiet", "verbose"),
blanchet@33192
    93
   ("no_overlord", "overlord"),
blanchet@33192
    94
   ("dont_show_all", "show_all"),
blanchet@33192
    95
   ("hide_datatypes", "show_datatypes"),
blanchet@33192
    96
   ("hide_consts", "show_consts"),
blanchet@33192
    97
   ("trust_potential", "check_potential"),
blanchet@33192
    98
   ("trust_genuine", "check_genuine")]
blanchet@33192
    99
blanchet@33192
   100
fun is_known_raw_param s =
blanchet@34923
   101
  AList.defined (op =) default_default_params s orelse
blanchet@34923
   102
  AList.defined (op =) negated_params s orelse
blanchet@34923
   103
  s = "max" orelse s = "eval" orelse s = "expect" orelse
blanchet@34923
   104
  exists (fn p => String.isPrefix (p ^ " ") s)
blanchet@35665
   105
         ["card", "max", "iter", "box", "dont_box", "finitize", "dont_finitize",
blanchet@35665
   106
          "mono", "non_mono", "std", "non_std", "wf", "non_wf", "format"]
blanchet@33192
   107
blanchet@33192
   108
fun check_raw_param (s, _) =
blanchet@33192
   109
  if is_known_raw_param s then ()
blanchet@35962
   110
  else error ("Unknown parameter: " ^ quote s ^ ".")  
blanchet@33192
   111
blanchet@33192
   112
fun unnegate_param_name name =
blanchet@33192
   113
  case AList.lookup (op =) negated_params name of
blanchet@33192
   114
    NONE => if String.isPrefix "dont_" name then SOME (unprefix "dont_" name)
blanchet@33192
   115
            else if String.isPrefix "non_" name then SOME (unprefix "non_" name)
blanchet@33192
   116
            else NONE
blanchet@33192
   117
  | some_name => some_name
blanchet@33192
   118
fun unnegate_raw_param (name, value) =
blanchet@33192
   119
  case unnegate_param_name name of
blanchet@33192
   120
    SOME name' => (name', case value of
blanchet@33192
   121
                            ["false"] => ["true"]
blanchet@33192
   122
                          | ["true"] => ["false"]
blanchet@33192
   123
                          | [] => ["false"]
blanchet@33192
   124
                          | _ => value)
blanchet@33192
   125
  | NONE => (name, value)
blanchet@33192
   126
blanchet@33574
   127
structure Data = Theory_Data(
blanchet@35962
   128
  type T = raw_param list
blanchet@36391
   129
  val empty = map (apsnd single) default_default_params
blanchet@33192
   130
  val extend = I
krauss@36607
   131
  fun merge (x, y) = AList.merge (op =) (K true) (x, y))
blanchet@33192
   132
blanchet@35962
   133
val set_default_raw_param = Data.map o AList.update (op =) o unnegate_raw_param
blanchet@35962
   134
val default_raw_params = Data.get
blanchet@33192
   135
blanchet@33192
   136
fun is_punctuation s = (s = "," orelse s = "-" orelse s = "\<midarrow>")
blanchet@33192
   137
blanchet@33192
   138
fun stringify_raw_param_value [] = ""
blanchet@33192
   139
  | stringify_raw_param_value [s] = s
blanchet@33192
   140
  | stringify_raw_param_value (s1 :: s2 :: ss) =
blanchet@33192
   141
    s1 ^ (if is_punctuation s1 orelse is_punctuation s2 then "" else " ") ^
blanchet@33192
   142
    stringify_raw_param_value (s2 :: ss)
blanchet@33192
   143
blanchet@33192
   144
fun maxed_int_from_string min_int s = Int.max (min_int, the (Int.fromString s))
blanchet@33192
   145
blanchet@33192
   146
fun extract_params ctxt auto default_params override_params =
blanchet@33192
   147
  let
blanchet@33192
   148
    val override_params = map unnegate_raw_param override_params
blanchet@33192
   149
    val raw_params = rev override_params @ rev default_params
blanchet@33192
   150
    val lookup =
blanchet@33192
   151
      Option.map stringify_raw_param_value o AList.lookup (op =) raw_params
blanchet@35962
   152
    val lookup_string = the_default "" o lookup
blanchet@35962
   153
    fun general_lookup_bool option default_value name =
blanchet@35962
   154
      case lookup name of
blanchet@36380
   155
        SOME s => parse_bool_option option name s
blanchet@35962
   156
      | NONE => default_value
blanchet@35962
   157
    val lookup_bool = the o general_lookup_bool false (SOME false)
blanchet@35962
   158
    val lookup_bool_option = general_lookup_bool true NONE
blanchet@33192
   159
    fun do_int name value =
blanchet@33192
   160
      case value of
blanchet@33192
   161
        SOME s => (case Int.fromString s of
blanchet@33192
   162
                     SOME i => i
blanchet@33192
   163
                   | NONE => error ("Parameter " ^ quote name ^
blanchet@33192
   164
                                    " must be assigned an integer value."))
blanchet@33192
   165
      | NONE => 0
blanchet@33192
   166
    fun lookup_int name = do_int name (lookup name)
blanchet@33192
   167
    fun lookup_int_option name =
blanchet@33192
   168
      case lookup name of
blanchet@33192
   169
        SOME "smart" => NONE
blanchet@33192
   170
      | value => SOME (do_int name value)
blanchet@33192
   171
    fun int_range_from_string name min_int s =
blanchet@33192
   172
      let
blanchet@33192
   173
        val (k1, k2) =
blanchet@33192
   174
          (case space_explode "-" s of
blanchet@33192
   175
             [s] => the_default (s, s) (first_field "\<midarrow>" s)
blanchet@33192
   176
           | ["", s2] => ("-" ^ s2, "-" ^ s2)
blanchet@33192
   177
           | [s1, s2] => (s1, s2)
blanchet@33192
   178
           | _ => raise Option)
blanchet@33192
   179
          |> pairself (maxed_int_from_string min_int)
blanchet@33192
   180
      in if k1 <= k2 then k1 upto k2 else k1 downto k2 end
blanchet@33192
   181
      handle Option.Option =>
blanchet@33192
   182
             error ("Parameter " ^ quote name ^
blanchet@33192
   183
                    " must be assigned a sequence of integers.")
blanchet@33192
   184
    fun int_seq_from_string name min_int s =
blanchet@33192
   185
      maps (int_range_from_string name min_int) (space_explode "," s)
blanchet@33192
   186
    fun lookup_int_seq name min_int =
blanchet@33192
   187
      case lookup name of
blanchet@33192
   188
        SOME s => (case int_seq_from_string name min_int s of
blanchet@33192
   189
                     [] => [min_int]
blanchet@33192
   190
                   | value => value)
blanchet@33192
   191
      | NONE => [min_int]
blanchet@33192
   192
    fun lookup_ints_assigns read prefix min_int =
blanchet@33192
   193
      (NONE, lookup_int_seq prefix min_int)
blanchet@33192
   194
      :: map (fn (name, value) =>
blanchet@33192
   195
                 (SOME (read (String.extract (name, size prefix + 1, NONE))),
blanchet@33192
   196
                  value |> stringify_raw_param_value
blanchet@33192
   197
                        |> int_seq_from_string name min_int))
blanchet@33192
   198
             (filter (String.isPrefix (prefix ^ " ") o fst) raw_params)
blanchet@34969
   199
    fun lookup_bool_assigns read prefix =
blanchet@34969
   200
      (NONE, lookup_bool prefix)
blanchet@34969
   201
      :: map (fn (name, value) =>
blanchet@34969
   202
                 (SOME (read (String.extract (name, size prefix + 1, NONE))),
blanchet@34969
   203
                  value |> stringify_raw_param_value
blanchet@36380
   204
                        |> parse_bool_option false name |> the))
blanchet@34969
   205
             (filter (String.isPrefix (prefix ^ " ") o fst) raw_params)
blanchet@33192
   206
    fun lookup_bool_option_assigns read prefix =
blanchet@33192
   207
      (NONE, lookup_bool_option prefix)
blanchet@33192
   208
      :: map (fn (name, value) =>
blanchet@33192
   209
                 (SOME (read (String.extract (name, size prefix + 1, NONE))),
blanchet@33192
   210
                  value |> stringify_raw_param_value
blanchet@36380
   211
                        |> parse_bool_option true name))
blanchet@33192
   212
             (filter (String.isPrefix (prefix ^ " ") o fst) raw_params)
blanchet@33192
   213
    fun lookup_time name =
blanchet@33192
   214
      case lookup name of
blanchet@33192
   215
        NONE => NONE
blanchet@36380
   216
      | SOME s => parse_time_option name s
blanchet@33192
   217
    val lookup_term_list =
blanchet@33192
   218
      AList.lookup (op =) raw_params #> these #> Syntax.read_terms ctxt
blanchet@33192
   219
    val read_type_polymorphic =
blanchet@33192
   220
      Syntax.read_typ ctxt #> Logic.mk_type
blanchet@33192
   221
      #> singleton (Variable.polymorphic ctxt) #> Logic.dest_type
blanchet@33192
   222
    val read_term_polymorphic =
blanchet@33192
   223
      Syntax.read_term ctxt #> singleton (Variable.polymorphic ctxt)
blanchet@33192
   224
    val read_const_polymorphic = read_term_polymorphic #> dest_Const
blanchet@33192
   225
    val cards_assigns = lookup_ints_assigns read_type_polymorphic "card" 1
blanchet@33192
   226
    val maxes_assigns = lookup_ints_assigns read_const_polymorphic "max" ~1
blanchet@33192
   227
    val iters_assigns = lookup_ints_assigns read_const_polymorphic "iter" 0
blanchet@34121
   228
    val bitss = lookup_int_seq "bits" 1
blanchet@33192
   229
    val bisim_depths = lookup_int_seq "bisim_depth" ~1
blanchet@35665
   230
    val boxes = lookup_bool_option_assigns read_type_polymorphic "box"
blanchet@35665
   231
    val finitizes = lookup_bool_option_assigns read_type_polymorphic "finitize"
blanchet@33192
   232
    val monos = lookup_bool_option_assigns read_type_polymorphic "mono"
blanchet@34969
   233
    val stds = lookup_bool_assigns read_type_polymorphic "std"
blanchet@33192
   234
    val wfs = lookup_bool_option_assigns read_const_polymorphic "wf"
blanchet@33192
   235
    val sat_solver = lookup_string "sat_solver"
blanchet@33192
   236
    val blocking = not auto andalso lookup_bool "blocking"
blanchet@33192
   237
    val falsify = lookup_bool "falsify"
blanchet@33192
   238
    val debug = not auto andalso lookup_bool "debug"
blanchet@33192
   239
    val verbose = debug orelse (not auto andalso lookup_bool "verbose")
blanchet@33192
   240
    val overlord = lookup_bool "overlord"
blanchet@33192
   241
    val user_axioms = lookup_bool_option "user_axioms"
blanchet@33192
   242
    val assms = lookup_bool "assms"
blanchet@33547
   243
    val merge_type_vars = lookup_bool "merge_type_vars"
blanchet@34121
   244
    val binary_ints = lookup_bool_option "binary_ints"
blanchet@33192
   245
    val destroy_constrs = lookup_bool "destroy_constrs"
blanchet@33192
   246
    val specialize = lookup_bool "specialize"
blanchet@33192
   247
    val star_linear_preds = lookup_bool "star_linear_preds"
blanchet@33192
   248
    val fast_descrs = lookup_bool "fast_descrs"
blanchet@33192
   249
    val peephole_optim = lookup_bool "peephole_optim"
blanchet@33552
   250
    val timeout = if auto then NONE else lookup_time "timeout"
blanchet@33192
   251
    val tac_timeout = lookup_time "tac_timeout"
blanchet@33192
   252
    val max_threads = Int.max (0, lookup_int "max_threads")
blanchet@33192
   253
    val show_all = debug orelse lookup_bool "show_all"
blanchet@33192
   254
    val show_datatypes = show_all orelse lookup_bool "show_datatypes"
blanchet@33192
   255
    val show_consts = show_all orelse lookup_bool "show_consts"
blanchet@33192
   256
    val formats = lookup_ints_assigns read_term_polymorphic "format" 0
blanchet@33192
   257
    val evals = lookup_term_list "eval"
blanchet@33552
   258
    val max_potential =
blanchet@33552
   259
      if auto then 0 else Int.max (0, lookup_int "max_potential")
blanchet@33192
   260
    val max_genuine = Int.max (0, lookup_int "max_genuine")
blanchet@33192
   261
    val check_potential = lookup_bool "check_potential"
blanchet@33192
   262
    val check_genuine = lookup_bool "check_genuine"
blanchet@36390
   263
    val batch_size =
blanchet@36390
   264
      case lookup_int_option "batch_size" of
blanchet@36390
   265
        SOME n => Int.max (1, n)
blanchet@36390
   266
      | NONE => if debug then 1 else 64
blanchet@33192
   267
    val expect = lookup_string "expect"
blanchet@33192
   268
  in
blanchet@33192
   269
    {cards_assigns = cards_assigns, maxes_assigns = maxes_assigns,
blanchet@34121
   270
     iters_assigns = iters_assigns, bitss = bitss, bisim_depths = bisim_depths,
blanchet@35665
   271
     boxes = boxes, finitizes = finitizes, monos = monos, stds = stds,
blanchet@35665
   272
     wfs = wfs, sat_solver = sat_solver, blocking = blocking, falsify = falsify,
blanchet@34969
   273
     debug = debug, verbose = verbose, overlord = overlord,
blanchet@34969
   274
     user_axioms = user_axioms, assms = assms,
blanchet@34121
   275
     merge_type_vars = merge_type_vars, binary_ints = binary_ints,
blanchet@34121
   276
     destroy_constrs = destroy_constrs, specialize = specialize,
blanchet@36389
   277
     star_linear_preds = star_linear_preds, fast_descrs = fast_descrs,
blanchet@36389
   278
     peephole_optim = peephole_optim, timeout = timeout,
blanchet@36389
   279
     tac_timeout = tac_timeout, max_threads = max_threads,
blanchet@36390
   280
     show_datatypes = show_datatypes, show_consts = show_consts,
blanchet@36390
   281
     formats = formats, evals = evals, max_potential = max_potential,
blanchet@36390
   282
     max_genuine = max_genuine, check_potential = check_potential,
blanchet@36390
   283
     check_genuine = check_genuine, batch_size = batch_size, expect = expect}
blanchet@33192
   284
  end
blanchet@33192
   285
blanchet@33192
   286
fun default_params thy =
wenzelm@36633
   287
  extract_params (ProofContext.init_global thy) false (default_raw_params thy)
blanchet@33192
   288
  o map (apsnd single)
blanchet@33192
   289
wenzelm@36970
   290
val parse_key = Scan.repeat1 Parse.typ_group >> space_implode " "
blanchet@35962
   291
val parse_value =
wenzelm@36970
   292
  Scan.repeat1 (Parse.minus >> single
wenzelm@36970
   293
                || Scan.repeat1 (Scan.unless Parse.minus Parse.name)
wenzelm@36970
   294
                || Parse.$$$ "," |-- Parse.number >> prefix "," >> single) >> flat
wenzelm@36970
   295
val parse_param = parse_key -- Scan.optional (Parse.$$$ "=" |-- parse_value) []
blanchet@35966
   296
val parse_params =
wenzelm@36970
   297
  Scan.optional (Parse.$$$ "[" |-- Parse.list parse_param --| Parse.$$$ "]") []
blanchet@33192
   298
blanchet@33192
   299
fun handle_exceptions ctxt f x =
blanchet@33192
   300
  f x
blanchet@33192
   301
  handle ARG (loc, details) =>
blanchet@33192
   302
         error ("Bad argument(s) to " ^ quote loc ^ ": " ^ details ^ ".")
blanchet@33192
   303
       | BAD (loc, details) =>
blanchet@33192
   304
         error ("Internal error (" ^ quote loc ^ "): " ^ details ^ ".")
blanchet@33192
   305
       | NOT_SUPPORTED details =>
blanchet@33192
   306
         (warning ("Unsupported case: " ^ details ^ "."); x)
blanchet@33192
   307
       | NUT (loc, us) =>
blanchet@33192
   308
         error ("Invalid intermediate term" ^ plural_s_for_list us ^
blanchet@33192
   309
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   310
                commas (map (string_for_nut ctxt) us) ^ ".")
blanchet@33192
   311
       | REP (loc, Rs) =>
blanchet@33192
   312
         error ("Invalid representation" ^ plural_s_for_list Rs ^
blanchet@33192
   313
                " (" ^ quote loc ^ "): " ^ commas (map string_for_rep Rs) ^ ".")
blanchet@33192
   314
       | TERM (loc, ts) =>
blanchet@33192
   315
         error ("Invalid term" ^ plural_s_for_list ts ^
blanchet@33192
   316
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   317
                commas (map (Syntax.string_of_term ctxt) ts) ^ ".")
blanchet@34121
   318
       | TOO_LARGE (_, details) =>
blanchet@34121
   319
         (warning ("Limit reached: " ^ details ^ "."); x)
blanchet@34121
   320
       | TOO_SMALL (_, details) =>
blanchet@34121
   321
         (warning ("Limit reached: " ^ details ^ "."); x)
blanchet@33192
   322
       | TYPE (loc, Ts, ts) =>
blanchet@33192
   323
         error ("Invalid type" ^ plural_s_for_list Ts ^
blanchet@33192
   324
                (if null ts then
blanchet@33192
   325
                   ""
blanchet@33192
   326
                 else
blanchet@33192
   327
                   " for term" ^ plural_s_for_list ts ^ " " ^
blanchet@33192
   328
                   commas (map (quote o Syntax.string_of_term ctxt) ts)) ^
blanchet@33192
   329
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   330
                commas (map (Syntax.string_of_typ ctxt) Ts) ^ ".")
blanchet@33192
   331
       | Kodkod.SYNTAX (_, details) =>
blanchet@33192
   332
         (warning ("Ill-formed Kodkodi output: " ^ details ^ "."); x)
blanchet@33192
   333
       | Refute.REFUTE (loc, details) =>
blanchet@33192
   334
         error ("Unhandled Refute error (" ^ quote loc ^ "): " ^ details ^ ".")
blanchet@33192
   335
blanchet@34969
   336
fun pick_nits override_params auto i step state =
blanchet@33192
   337
  let
blanchet@33192
   338
    val thy = Proof.theory_of state
blanchet@33192
   339
    val ctxt = Proof.context_of state
blanchet@33192
   340
    val _ = List.app check_raw_param override_params
blanchet@33192
   341
    val params as {blocking, debug, ...} =
blanchet@33192
   342
      extract_params ctxt auto (default_raw_params thy) override_params
blanchet@33192
   343
    fun go () =
blanchet@33552
   344
      (false, state)
blanchet@33552
   345
      |> (if auto then perhaps o try
blanchet@33552
   346
          else if debug then fn f => fn x => f x
blanchet@33552
   347
          else handle_exceptions ctxt)
blanchet@34969
   348
         (fn (_, state) => pick_nits_in_subgoal state params auto i step
blanchet@34118
   349
                           |>> curry (op =) "genuine")
blanchet@33192
   350
  in
blanchet@33552
   351
    if auto orelse blocking then go ()
wenzelm@33604
   352
    else (Toplevel.thread true (fn () => (go (); ())); (false, state))
blanchet@33192
   353
  end
blanchet@33192
   354
blanchet@35966
   355
fun nitpick_trans (params, i) =
blanchet@34969
   356
  Toplevel.keep (fn st =>
blanchet@35966
   357
      (pick_nits params false i (Toplevel.proof_position_of st)
blanchet@35966
   358
                 (Toplevel.proof_of st); ()))
blanchet@33192
   359
blanchet@33192
   360
fun string_for_raw_param (name, value) =
blanchet@33192
   361
  name ^ " = " ^ stringify_raw_param_value value
blanchet@33192
   362
blanchet@35966
   363
fun nitpick_params_trans params =
blanchet@33192
   364
  Toplevel.theory
blanchet@35966
   365
      (fold set_default_raw_param params
blanchet@33552
   366
       #> tap (fn thy => 
blanchet@33552
   367
                  writeln ("Default parameters for Nitpick:\n" ^
blanchet@33552
   368
                           (case rev (default_raw_params thy) of
blanchet@33552
   369
                              [] => "none"
blanchet@33552
   370
                            | params =>
blanchet@33552
   371
                              (map check_raw_param params;
blanchet@33552
   372
                               params |> map string_for_raw_param
blanchet@33552
   373
                                      |> sort_strings |> cat_lines)))))
blanchet@33192
   374
blanchet@35962
   375
val parse_nitpick_command =
wenzelm@36970
   376
  (parse_params -- Scan.optional Parse.nat 1) #>> nitpick_trans
blanchet@35962
   377
val parse_nitpick_params_command = parse_params #>> nitpick_params_trans
blanchet@33192
   378
wenzelm@36970
   379
val _ = Outer_Syntax.improper_command "nitpick"
blanchet@36390
   380
            "try to find a counterexample for a given subgoal using Nitpick"
wenzelm@36970
   381
            Keyword.diag parse_nitpick_command
wenzelm@36970
   382
val _ = Outer_Syntax.command "nitpick_params"
blanchet@33192
   383
            "set and display the default parameters for Nitpick"
wenzelm@36970
   384
            Keyword.thy_decl parse_nitpick_params_command
blanchet@33192
   385
blanchet@33552
   386
fun auto_nitpick state =
blanchet@34969
   387
  if not (!auto) then (false, state) else pick_nits [] true 1 0 state
blanchet@33552
   388
blanchet@33552
   389
val setup = Auto_Counterexample.register_tool ("nitpick", auto_nitpick)
blanchet@33552
   390
blanchet@33192
   391
end;