src/HOL/Tools/Nitpick/nitpick_isar.ML
author blanchet
Thu, 17 Dec 2009 15:22:11 +0100
changeset 34121 c4628a1dcf75
parent 34118 5e831d805118
child 34923 c4f04bee79f3
permissions -rw-r--r--
added support for binary nat/int representation to Nitpick
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_isar.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@33192
     3
    Copyright   2008, 2009
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@33192
    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
wenzelm@33601
    31
    (Preferences.bool_pref auto
wenzelm@33601
    32
      "auto-nitpick"
wenzelm@33601
    33
      "Whether to run Nitpick automatically.")
blanchet@33552
    34
blanchet@33192
    35
type raw_param = string * string list
blanchet@33192
    36
blanchet@33192
    37
val default_default_params =
blanchet@33192
    38
  [("card", ["1\<midarrow>8"]),
blanchet@33192
    39
   ("iter", ["0,1,2,4,8,12,16,24"]),
blanchet@34121
    40
   ("bits", ["1,2,3,4,6,8,10,12"]),
blanchet@33192
    41
   ("bisim_depth", ["7"]),
blanchet@33192
    42
   ("box", ["smart"]),
blanchet@33192
    43
   ("mono", ["smart"]),
blanchet@33192
    44
   ("wf", ["smart"]),
blanchet@33192
    45
   ("sat_solver", ["smart"]),
blanchet@33192
    46
   ("batch_size", ["smart"]),
blanchet@33192
    47
   ("blocking", ["true"]),
blanchet@33192
    48
   ("falsify", ["true"]),
blanchet@33192
    49
   ("user_axioms", ["smart"]),
blanchet@33192
    50
   ("assms", ["true"]),
blanchet@33547
    51
   ("merge_type_vars", ["false"]),
blanchet@34121
    52
   ("binary_ints", ["smart"]),
blanchet@33192
    53
   ("destroy_constrs", ["true"]),
blanchet@33192
    54
   ("specialize", ["true"]),
blanchet@33192
    55
   ("skolemize", ["true"]),
blanchet@33192
    56
   ("star_linear_preds", ["true"]),
blanchet@33192
    57
   ("uncurry", ["true"]),
blanchet@33192
    58
   ("fast_descrs", ["true"]),
blanchet@33192
    59
   ("peephole_optim", ["true"]),
blanchet@33192
    60
   ("timeout", ["30 s"]),
blanchet@33192
    61
   ("tac_timeout", ["500 ms"]),
blanchet@33192
    62
   ("sym_break", ["20"]),
blanchet@33192
    63
   ("sharing_depth", ["3"]),
blanchet@33192
    64
   ("flatten_props", ["false"]),
blanchet@33192
    65
   ("max_threads", ["0"]),
blanchet@33192
    66
   ("verbose", ["false"]),
blanchet@33192
    67
   ("debug", ["false"]),
blanchet@33197
    68
   ("overlord", ["false"]),
blanchet@33192
    69
   ("show_all", ["false"]),
blanchet@33192
    70
   ("show_skolems", ["true"]),
blanchet@33192
    71
   ("show_datatypes", ["false"]),
blanchet@33192
    72
   ("show_consts", ["false"]),
blanchet@33192
    73
   ("format", ["1"]),
blanchet@33192
    74
   ("max_potential", ["1"]),
blanchet@33192
    75
   ("max_genuine", ["1"]),
blanchet@33192
    76
   ("check_potential", ["false"]),
blanchet@33192
    77
   ("check_genuine", ["false"])]
blanchet@33192
    78
blanchet@33192
    79
val negated_params =
blanchet@33192
    80
  [("dont_box", "box"),
blanchet@33192
    81
   ("non_mono", "mono"),
blanchet@33192
    82
   ("non_wf", "wf"),
blanchet@33192
    83
   ("non_blocking", "blocking"),
blanchet@33192
    84
   ("satisfy", "falsify"),
blanchet@33192
    85
   ("no_user_axioms", "user_axioms"),
blanchet@33192
    86
   ("no_assms", "assms"),
blanchet@33547
    87
   ("dont_merge_type_vars", "merge_type_vars"),
blanchet@34121
    88
   ("unary_ints", "binary_ints"),
blanchet@33192
    89
   ("dont_destroy_constrs", "destroy_constrs"),
blanchet@33192
    90
   ("dont_specialize", "specialize"),
blanchet@33192
    91
   ("dont_skolemize", "skolemize"),
blanchet@33192
    92
   ("dont_star_linear_preds", "star_linear_preds"),
blanchet@33192
    93
   ("dont_uncurry", "uncurry"),
blanchet@33192
    94
   ("full_descrs", "fast_descrs"),
blanchet@33192
    95
   ("no_peephole_optim", "peephole_optim"),
blanchet@33192
    96
   ("dont_flatten_props", "flatten_props"),
blanchet@33192
    97
   ("quiet", "verbose"),
blanchet@33192
    98
   ("no_debug", "debug"),
blanchet@33192
    99
   ("no_overlord", "overlord"),
blanchet@33192
   100
   ("dont_show_all", "show_all"),
blanchet@33192
   101
   ("hide_skolems", "show_skolems"),
blanchet@33192
   102
   ("hide_datatypes", "show_datatypes"),
blanchet@33192
   103
   ("hide_consts", "show_consts"),
blanchet@33192
   104
   ("trust_potential", "check_potential"),
blanchet@33192
   105
   ("trust_genuine", "check_genuine")]
blanchet@33192
   106
blanchet@33192
   107
(* string -> bool *)
blanchet@33192
   108
fun is_known_raw_param s =
blanchet@33192
   109
  AList.defined (op =) default_default_params s
blanchet@33192
   110
  orelse AList.defined (op =) negated_params s
blanchet@34118
   111
  orelse s = "max" orelse s = "eval" orelse s = "expect"
blanchet@33192
   112
  orelse exists (fn p => String.isPrefix (p ^ " ") s)
blanchet@33192
   113
                ["card", "max", "iter", "box", "dont_box", "mono", "non_mono",
blanchet@33192
   114
                 "wf", "non_wf", "format"]
blanchet@33192
   115
blanchet@33192
   116
(* string * 'a -> unit *)
blanchet@33192
   117
fun check_raw_param (s, _) =
blanchet@33192
   118
  if is_known_raw_param s then ()
blanchet@33192
   119
  else error ("Unknown parameter " ^ quote s ^ ".")  
blanchet@33192
   120
blanchet@33192
   121
(* string -> string option *)
blanchet@33192
   122
fun unnegate_param_name name =
blanchet@33192
   123
  case AList.lookup (op =) negated_params name of
blanchet@33192
   124
    NONE => if String.isPrefix "dont_" name then SOME (unprefix "dont_" name)
blanchet@33192
   125
            else if String.isPrefix "non_" name then SOME (unprefix "non_" name)
blanchet@33192
   126
            else NONE
blanchet@33192
   127
  | some_name => some_name
blanchet@33192
   128
(* raw_param -> raw_param *)
blanchet@33192
   129
fun unnegate_raw_param (name, value) =
blanchet@33192
   130
  case unnegate_param_name name of
blanchet@33192
   131
    SOME name' => (name', case value of
blanchet@33192
   132
                            ["false"] => ["true"]
blanchet@33192
   133
                          | ["true"] => ["false"]
blanchet@33192
   134
                          | [] => ["false"]
blanchet@33192
   135
                          | _ => value)
blanchet@33192
   136
  | NONE => (name, value)
blanchet@33192
   137
blanchet@33574
   138
structure Data = Theory_Data(
blanchet@33552
   139
  type T = {params: raw_param list}
blanchet@33552
   140
  val empty = {params = rev default_default_params}
blanchet@33192
   141
  val extend = I
wenzelm@33599
   142
  fun merge ({params = ps1}, {params = ps2}) : T =
wenzelm@33699
   143
    {params = AList.merge (op =) (K true) (ps1, ps2)})
blanchet@33192
   144
blanchet@33192
   145
(* raw_param -> theory -> theory *)
blanchet@33192
   146
fun set_default_raw_param param thy =
blanchet@33574
   147
  let val {params} = Data.get thy in
blanchet@33574
   148
    Data.put {params = AList.update (op =) (unnegate_raw_param param) params}
blanchet@33574
   149
             thy
blanchet@33192
   150
  end
blanchet@33192
   151
(* theory -> raw_param list *)
blanchet@33574
   152
val default_raw_params = #params o Data.get
blanchet@33192
   153
blanchet@33192
   154
(* string -> bool *)
blanchet@33192
   155
fun is_punctuation s = (s = "," orelse s = "-" orelse s = "\<midarrow>")
blanchet@33192
   156
blanchet@33192
   157
(* string list -> string *)
blanchet@33192
   158
fun stringify_raw_param_value [] = ""
blanchet@33192
   159
  | stringify_raw_param_value [s] = s
blanchet@33192
   160
  | stringify_raw_param_value (s1 :: s2 :: ss) =
blanchet@33192
   161
    s1 ^ (if is_punctuation s1 orelse is_punctuation s2 then "" else " ") ^
blanchet@33192
   162
    stringify_raw_param_value (s2 :: ss)
blanchet@33192
   163
blanchet@33192
   164
(* bool -> string -> string -> bool option *)
blanchet@33192
   165
fun bool_option_from_string option name s =
blanchet@33192
   166
  (case s of
blanchet@33192
   167
     "smart" => if option then NONE else raise Option
blanchet@33192
   168
   | "false" => SOME false
blanchet@33192
   169
   | "true" => SOME true
blanchet@33192
   170
   | "" => SOME true
blanchet@33192
   171
   | s => raise Option)
blanchet@33192
   172
  handle Option.Option =>
blanchet@33192
   173
         let val ss = map quote ((option ? cons "smart") ["true", "false"]) in
blanchet@33192
   174
           error ("Parameter " ^ quote name ^ " must be assigned " ^
blanchet@33192
   175
                  space_implode " " (serial_commas "or" ss) ^ ".")
blanchet@33192
   176
         end
blanchet@33192
   177
(* bool -> raw_param list -> bool option -> string -> bool option *)
blanchet@33192
   178
fun general_lookup_bool option raw_params default_value name =
blanchet@33192
   179
  case AList.lookup (op =) raw_params name of
blanchet@33192
   180
    SOME s => s |> stringify_raw_param_value
blanchet@33192
   181
                |> bool_option_from_string option name
blanchet@33192
   182
  | NONE => default_value
blanchet@33192
   183
blanchet@33192
   184
(* int -> string -> int *)
blanchet@33192
   185
fun maxed_int_from_string min_int s = Int.max (min_int, the (Int.fromString s))
blanchet@33192
   186
blanchet@33192
   187
(* Proof.context -> bool -> raw_param list -> raw_param list -> params *)
blanchet@33192
   188
fun extract_params ctxt auto default_params override_params =
blanchet@33192
   189
  let
blanchet@33192
   190
    val override_params = map unnegate_raw_param override_params
blanchet@33192
   191
    val raw_params = rev override_params @ rev default_params
blanchet@33192
   192
    val lookup =
blanchet@33192
   193
      Option.map stringify_raw_param_value o AList.lookup (op =) raw_params
blanchet@33192
   194
    (* string -> string *)
blanchet@33192
   195
    fun lookup_string name = the_default "" (lookup name)
blanchet@33192
   196
    (* string -> bool *)
blanchet@33192
   197
    val lookup_bool = the o general_lookup_bool false raw_params (SOME false)
blanchet@33192
   198
    (* string -> bool option *)
blanchet@33192
   199
    val lookup_bool_option = general_lookup_bool true raw_params NONE
blanchet@33192
   200
    (* string -> string option -> int *)
blanchet@33192
   201
    fun do_int name value =
blanchet@33192
   202
      case value of
blanchet@33192
   203
        SOME s => (case Int.fromString s of
blanchet@33192
   204
                     SOME i => i
blanchet@33192
   205
                   | NONE => error ("Parameter " ^ quote name ^
blanchet@33192
   206
                                    " must be assigned an integer value."))
blanchet@33192
   207
      | NONE => 0
blanchet@33192
   208
    (* string -> int *)
blanchet@33192
   209
    fun lookup_int name = do_int name (lookup name)
blanchet@33192
   210
    (* string -> int option *)
blanchet@33192
   211
    fun lookup_int_option name =
blanchet@33192
   212
      case lookup name of
blanchet@33192
   213
        SOME "smart" => NONE
blanchet@33192
   214
      | value => SOME (do_int name value)
blanchet@33192
   215
    (* string -> int -> string -> int list *)
blanchet@33192
   216
    fun int_range_from_string name min_int s =
blanchet@33192
   217
      let
blanchet@33192
   218
        val (k1, k2) =
blanchet@33192
   219
          (case space_explode "-" s of
blanchet@33192
   220
             [s] => the_default (s, s) (first_field "\<midarrow>" s)
blanchet@33192
   221
           | ["", s2] => ("-" ^ s2, "-" ^ s2)
blanchet@33192
   222
           | [s1, s2] => (s1, s2)
blanchet@33192
   223
           | _ => raise Option)
blanchet@33192
   224
          |> pairself (maxed_int_from_string min_int)
blanchet@33192
   225
      in if k1 <= k2 then k1 upto k2 else k1 downto k2 end
blanchet@33192
   226
      handle Option.Option =>
blanchet@33192
   227
             error ("Parameter " ^ quote name ^
blanchet@33192
   228
                    " must be assigned a sequence of integers.")
blanchet@33192
   229
    (* string -> int -> string -> int list *)
blanchet@33192
   230
    fun int_seq_from_string name min_int s =
blanchet@33192
   231
      maps (int_range_from_string name min_int) (space_explode "," s)
blanchet@33192
   232
    (* string -> int -> int list *)
blanchet@33192
   233
    fun lookup_int_seq name min_int =
blanchet@33192
   234
      case lookup name of
blanchet@33192
   235
        SOME s => (case int_seq_from_string name min_int s of
blanchet@33192
   236
                     [] => [min_int]
blanchet@33192
   237
                   | value => value)
blanchet@33192
   238
      | NONE => [min_int]
blanchet@33192
   239
    (* (string -> 'a) -> int -> string -> ('a option * int list) list *)
blanchet@33192
   240
    fun lookup_ints_assigns read prefix min_int =
blanchet@33192
   241
      (NONE, lookup_int_seq prefix min_int)
blanchet@33192
   242
      :: map (fn (name, value) =>
blanchet@33192
   243
                 (SOME (read (String.extract (name, size prefix + 1, NONE))),
blanchet@33192
   244
                  value |> stringify_raw_param_value
blanchet@33192
   245
                        |> int_seq_from_string name min_int))
blanchet@33192
   246
             (filter (String.isPrefix (prefix ^ " ") o fst) raw_params)
blanchet@33192
   247
    (* (string -> 'a) -> string -> ('a option * bool option) list *)
blanchet@33192
   248
    fun lookup_bool_option_assigns read prefix =
blanchet@33192
   249
      (NONE, lookup_bool_option prefix)
blanchet@33192
   250
      :: map (fn (name, value) =>
blanchet@33192
   251
                 (SOME (read (String.extract (name, size prefix + 1, NONE))),
blanchet@33192
   252
                  value |> stringify_raw_param_value
blanchet@33192
   253
                        |> bool_option_from_string true name))
blanchet@33192
   254
             (filter (String.isPrefix (prefix ^ " ") o fst) raw_params)
blanchet@33192
   255
    (* string -> Time.time option *)
blanchet@33192
   256
    fun lookup_time name =
blanchet@33192
   257
      case lookup name of
blanchet@33192
   258
        NONE => NONE
blanchet@33192
   259
      | SOME "none" => NONE
blanchet@33192
   260
      | SOME s =>
blanchet@33192
   261
        let
blanchet@33192
   262
          val msecs =
blanchet@33192
   263
            case space_explode " " s of
blanchet@33192
   264
              [s1, "min"] => 60000 * the (Int.fromString s1)
blanchet@33192
   265
            | [s1, "s"] => 1000 * the (Int.fromString s1)
blanchet@33192
   266
            | [s1, "ms"] => the (Int.fromString s1)
blanchet@33192
   267
            | _ => 0
blanchet@33192
   268
        in
blanchet@33192
   269
          if msecs <= 0 then
blanchet@33192
   270
            error ("Parameter " ^ quote name ^ " must be assigned a positive \
blanchet@33192
   271
                   \time value (e.g., \"60 s\", \"200 ms\") or \"none\".")
blanchet@33192
   272
          else
blanchet@33192
   273
            SOME (Time.fromMilliseconds msecs)
blanchet@33192
   274
        end
blanchet@33192
   275
    (* string -> term list *)
blanchet@33192
   276
    val lookup_term_list =
blanchet@33192
   277
      AList.lookup (op =) raw_params #> these #> Syntax.read_terms ctxt
blanchet@33192
   278
    val read_type_polymorphic =
blanchet@33192
   279
      Syntax.read_typ ctxt #> Logic.mk_type
blanchet@33192
   280
      #> singleton (Variable.polymorphic ctxt) #> Logic.dest_type
blanchet@33192
   281
    (* string -> term *)
blanchet@33192
   282
    val read_term_polymorphic =
blanchet@33192
   283
      Syntax.read_term ctxt #> singleton (Variable.polymorphic ctxt)
blanchet@33192
   284
    (* string -> styp *)
blanchet@33192
   285
    val read_const_polymorphic = read_term_polymorphic #> dest_Const
blanchet@33192
   286
    val cards_assigns = lookup_ints_assigns read_type_polymorphic "card" 1
blanchet@33192
   287
    val maxes_assigns = lookup_ints_assigns read_const_polymorphic "max" ~1
blanchet@33192
   288
    val iters_assigns = lookup_ints_assigns read_const_polymorphic "iter" 0
blanchet@34121
   289
    val bitss = lookup_int_seq "bits" 1
blanchet@33192
   290
    val bisim_depths = lookup_int_seq "bisim_depth" ~1
blanchet@33192
   291
    val boxes =
blanchet@33192
   292
      lookup_bool_option_assigns read_type_polymorphic "box" @
blanchet@33192
   293
      map_filter (fn (SOME T, _) =>
blanchet@33192
   294
                     if is_fun_type T orelse is_pair_type T then
blanchet@33192
   295
                       SOME (SOME T, SOME true)
blanchet@33192
   296
                     else
blanchet@33192
   297
                       NONE
blanchet@33192
   298
                   | (NONE, _) => NONE) cards_assigns
blanchet@33192
   299
    val monos = lookup_bool_option_assigns read_type_polymorphic "mono"
blanchet@33192
   300
    val wfs = lookup_bool_option_assigns read_const_polymorphic "wf"
blanchet@33192
   301
    val sat_solver = lookup_string "sat_solver"
blanchet@33192
   302
    val blocking = not auto andalso lookup_bool "blocking"
blanchet@33192
   303
    val falsify = lookup_bool "falsify"
blanchet@33192
   304
    val debug = not auto andalso lookup_bool "debug"
blanchet@33192
   305
    val verbose = debug orelse (not auto andalso lookup_bool "verbose")
blanchet@33192
   306
    val overlord = lookup_bool "overlord"
blanchet@33192
   307
    val user_axioms = lookup_bool_option "user_axioms"
blanchet@33192
   308
    val assms = lookup_bool "assms"
blanchet@33547
   309
    val merge_type_vars = lookup_bool "merge_type_vars"
blanchet@34121
   310
    val binary_ints = lookup_bool_option "binary_ints"
blanchet@33192
   311
    val destroy_constrs = lookup_bool "destroy_constrs"
blanchet@33192
   312
    val specialize = lookup_bool "specialize"
blanchet@33192
   313
    val skolemize = lookup_bool "skolemize"
blanchet@33192
   314
    val star_linear_preds = lookup_bool "star_linear_preds"
blanchet@33192
   315
    val uncurry = lookup_bool "uncurry"
blanchet@33192
   316
    val fast_descrs = lookup_bool "fast_descrs"
blanchet@33192
   317
    val peephole_optim = lookup_bool "peephole_optim"
blanchet@33552
   318
    val timeout = if auto then NONE else lookup_time "timeout"
blanchet@33192
   319
    val tac_timeout = lookup_time "tac_timeout"
blanchet@33192
   320
    val sym_break = Int.max (0, lookup_int "sym_break")
blanchet@33192
   321
    val sharing_depth = Int.max (1, lookup_int "sharing_depth")
blanchet@33192
   322
    val flatten_props = lookup_bool "flatten_props"
blanchet@33192
   323
    val max_threads = Int.max (0, lookup_int "max_threads")
blanchet@33192
   324
    val show_all = debug orelse lookup_bool "show_all"
blanchet@33192
   325
    val show_skolems = show_all orelse lookup_bool "show_skolems"
blanchet@33192
   326
    val show_datatypes = show_all orelse lookup_bool "show_datatypes"
blanchet@33192
   327
    val show_consts = show_all orelse lookup_bool "show_consts"
blanchet@33192
   328
    val formats = lookup_ints_assigns read_term_polymorphic "format" 0
blanchet@33192
   329
    val evals = lookup_term_list "eval"
blanchet@33552
   330
    val max_potential =
blanchet@33552
   331
      if auto then 0 else Int.max (0, lookup_int "max_potential")
blanchet@33192
   332
    val max_genuine = Int.max (0, lookup_int "max_genuine")
blanchet@33192
   333
    val check_potential = lookup_bool "check_potential"
blanchet@33192
   334
    val check_genuine = lookup_bool "check_genuine"
blanchet@33192
   335
    val batch_size = case lookup_int_option "batch_size" of
blanchet@33192
   336
                       SOME n => Int.max (1, n)
blanchet@33192
   337
                     | NONE => if debug then 1 else 64
blanchet@33192
   338
    val expect = lookup_string "expect"
blanchet@33192
   339
  in
blanchet@33192
   340
    {cards_assigns = cards_assigns, maxes_assigns = maxes_assigns,
blanchet@34121
   341
     iters_assigns = iters_assigns, bitss = bitss, bisim_depths = bisim_depths,
blanchet@34121
   342
     boxes = boxes, monos = monos, wfs = wfs, sat_solver = sat_solver,
blanchet@34121
   343
     blocking = blocking, falsify = falsify, debug = debug, verbose = verbose,
blanchet@34121
   344
     overlord = overlord, user_axioms = user_axioms, assms = assms,
blanchet@34121
   345
     merge_type_vars = merge_type_vars, binary_ints = binary_ints,
blanchet@34121
   346
     destroy_constrs = destroy_constrs, specialize = specialize,
blanchet@34121
   347
     skolemize = skolemize, star_linear_preds = star_linear_preds,
blanchet@34121
   348
     uncurry = uncurry, fast_descrs = fast_descrs,
blanchet@34121
   349
     peephole_optim = peephole_optim, timeout = timeout,
blanchet@34121
   350
     tac_timeout = tac_timeout, sym_break = sym_break,
blanchet@33192
   351
     sharing_depth = sharing_depth, flatten_props = flatten_props,
blanchet@33192
   352
     max_threads = max_threads, show_skolems = show_skolems,
blanchet@33192
   353
     show_datatypes = show_datatypes, show_consts = show_consts,
blanchet@33192
   354
     formats = formats, evals = evals, max_potential = max_potential,
blanchet@33192
   355
     max_genuine = max_genuine, check_potential = check_potential,
blanchet@33192
   356
     check_genuine = check_genuine, batch_size = batch_size, expect = expect}
blanchet@33192
   357
  end
blanchet@33192
   358
blanchet@33192
   359
(* theory -> (string * string) list -> params *)
blanchet@33192
   360
fun default_params thy =
blanchet@33192
   361
  extract_params (ProofContext.init thy) false (default_raw_params thy)
blanchet@33192
   362
  o map (apsnd single)
blanchet@33192
   363
blanchet@33192
   364
(* OuterParse.token list -> string * OuterParse.token list *)
blanchet@33192
   365
val scan_key = Scan.repeat1 OuterParse.typ_group >> space_implode " "
blanchet@33192
   366
blanchet@33192
   367
(* OuterParse.token list -> string list * OuterParse.token list *)
blanchet@33192
   368
val scan_value =
blanchet@33192
   369
  Scan.repeat1 (OuterParse.minus >> single
blanchet@33192
   370
                || Scan.repeat1 (Scan.unless OuterParse.minus OuterParse.name)
blanchet@33192
   371
                || OuterParse.$$$ "," |-- OuterParse.number >> prefix ","
blanchet@33192
   372
                   >> single) >> flat
blanchet@33192
   373
blanchet@33192
   374
(* OuterParse.token list -> raw_param * OuterParse.token list *)
blanchet@33192
   375
val scan_param =
blanchet@33192
   376
  scan_key -- (Scan.option (OuterParse.$$$ "=" |-- scan_value) >> these)
blanchet@33192
   377
(* OuterParse.token list -> raw_param list option * OuterParse.token list *)
blanchet@33192
   378
val scan_params = Scan.option (OuterParse.$$$ "[" |-- OuterParse.list scan_param
blanchet@33192
   379
                               --| OuterParse.$$$ "]")
blanchet@33192
   380
blanchet@33192
   381
(* Proof.context -> ('a -> 'a) -> 'a -> 'a *)
blanchet@33192
   382
fun handle_exceptions ctxt f x =
blanchet@33192
   383
  f x
blanchet@33192
   384
  handle ARG (loc, details) =>
blanchet@33192
   385
         error ("Bad argument(s) to " ^ quote loc ^ ": " ^ details ^ ".")
blanchet@33192
   386
       | BAD (loc, details) =>
blanchet@33192
   387
         error ("Internal error (" ^ quote loc ^ "): " ^ details ^ ".")
blanchet@33192
   388
       | NOT_SUPPORTED details =>
blanchet@33192
   389
         (warning ("Unsupported case: " ^ details ^ "."); x)
blanchet@33192
   390
       | NUT (loc, us) =>
blanchet@33192
   391
         error ("Invalid intermediate term" ^ plural_s_for_list us ^
blanchet@33192
   392
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   393
                commas (map (string_for_nut ctxt) us) ^ ".")
blanchet@33192
   394
       | REP (loc, Rs) =>
blanchet@33192
   395
         error ("Invalid representation" ^ plural_s_for_list Rs ^
blanchet@33192
   396
                " (" ^ quote loc ^ "): " ^ commas (map string_for_rep Rs) ^ ".")
blanchet@33192
   397
       | TERM (loc, ts) =>
blanchet@33192
   398
         error ("Invalid term" ^ plural_s_for_list ts ^
blanchet@33192
   399
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   400
                commas (map (Syntax.string_of_term ctxt) ts) ^ ".")
blanchet@34121
   401
       | TOO_LARGE (_, details) =>
blanchet@34121
   402
         (warning ("Limit reached: " ^ details ^ "."); x)
blanchet@34121
   403
       | TOO_SMALL (_, details) =>
blanchet@34121
   404
         (warning ("Limit reached: " ^ details ^ "."); x)
blanchet@33192
   405
       | TYPE (loc, Ts, ts) =>
blanchet@33192
   406
         error ("Invalid type" ^ plural_s_for_list Ts ^
blanchet@33192
   407
                (if null ts then
blanchet@33192
   408
                   ""
blanchet@33192
   409
                 else
blanchet@33192
   410
                   " for term" ^ plural_s_for_list ts ^ " " ^
blanchet@33192
   411
                   commas (map (quote o Syntax.string_of_term ctxt) ts)) ^
blanchet@33192
   412
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   413
                commas (map (Syntax.string_of_typ ctxt) Ts) ^ ".")
blanchet@33192
   414
       | Kodkod.SYNTAX (_, details) =>
blanchet@33192
   415
         (warning ("Ill-formed Kodkodi output: " ^ details ^ "."); x)
blanchet@33192
   416
       | Refute.REFUTE (loc, details) =>
blanchet@33192
   417
         error ("Unhandled Refute error (" ^ quote loc ^ "): " ^ details ^ ".")
blanchet@33192
   418
blanchet@33552
   419
(* raw_param list -> bool -> int -> Proof.state -> bool * Proof.state *)
blanchet@34118
   420
fun pick_nits override_params auto i state =
blanchet@33192
   421
  let
blanchet@33192
   422
    val thy = Proof.theory_of state
blanchet@33192
   423
    val ctxt = Proof.context_of state
wenzelm@33292
   424
    val thm = #goal (Proof.raw_goal state)
blanchet@33192
   425
    val _ = List.app check_raw_param override_params
blanchet@33192
   426
    val params as {blocking, debug, ...} =
blanchet@33192
   427
      extract_params ctxt auto (default_raw_params thy) override_params
blanchet@33552
   428
    (* unit -> bool * Proof.state *)
blanchet@33192
   429
    fun go () =
blanchet@33552
   430
      (false, state)
blanchet@33552
   431
      |> (if auto then perhaps o try
blanchet@33552
   432
          else if debug then fn f => fn x => f x
blanchet@33552
   433
          else handle_exceptions ctxt)
blanchet@34118
   434
         (fn (_, state) => pick_nits_in_subgoal state params auto i
blanchet@34118
   435
                           |>> curry (op =) "genuine")
blanchet@33192
   436
  in
blanchet@33552
   437
    if auto orelse blocking then go ()
wenzelm@33604
   438
    else (Toplevel.thread true (fn () => (go (); ())); (false, state))
blanchet@33192
   439
  end
blanchet@33192
   440
blanchet@34118
   441
(* raw_param list option * int option -> Toplevel.transition
blanchet@34118
   442
   -> Toplevel.transition *)
blanchet@34118
   443
fun nitpick_trans (opt_params, opt_i) =
blanchet@33192
   444
  Toplevel.keep (K ()
blanchet@34118
   445
      o snd o pick_nits (these opt_params) false (the_default 1 opt_i)
blanchet@33192
   446
      o Toplevel.proof_of)
blanchet@33192
   447
blanchet@33192
   448
(* raw_param -> string *)
blanchet@33192
   449
fun string_for_raw_param (name, value) =
blanchet@33192
   450
  name ^ " = " ^ stringify_raw_param_value value
blanchet@33192
   451
blanchet@34118
   452
(* raw_param list option -> Toplevel.transition -> Toplevel.transition *)
blanchet@33192
   453
fun nitpick_params_trans opt_params =
blanchet@33192
   454
  Toplevel.theory
blanchet@33552
   455
      (fold set_default_raw_param (these opt_params)
blanchet@33552
   456
       #> tap (fn thy => 
blanchet@33552
   457
                  writeln ("Default parameters for Nitpick:\n" ^
blanchet@33552
   458
                           (case rev (default_raw_params thy) of
blanchet@33552
   459
                              [] => "none"
blanchet@33552
   460
                            | params =>
blanchet@33552
   461
                              (map check_raw_param params;
blanchet@33552
   462
                               params |> map string_for_raw_param
blanchet@33552
   463
                                      |> sort_strings |> cat_lines)))))
blanchet@33192
   464
blanchet@33192
   465
(* OuterParse.token list
blanchet@33192
   466
   -> (Toplevel.transition -> Toplevel.transition) * OuterParse.token list *)
blanchet@33552
   467
val scan_nitpick_command =
blanchet@33552
   468
  (scan_params -- Scan.option OuterParse.nat) #>> nitpick_trans
blanchet@33552
   469
val scan_nitpick_params_command = scan_params #>> nitpick_params_trans
blanchet@33192
   470
blanchet@33192
   471
val _ = OuterSyntax.improper_command "nitpick"
blanchet@33192
   472
            "try to find a counterexample for a given subgoal using Kodkod"
blanchet@33192
   473
            OuterKeyword.diag scan_nitpick_command
blanchet@33192
   474
val _ = OuterSyntax.command "nitpick_params"
blanchet@33192
   475
            "set and display the default parameters for Nitpick"
blanchet@33192
   476
            OuterKeyword.thy_decl scan_nitpick_params_command
blanchet@33192
   477
blanchet@33552
   478
(* Proof.state -> bool * Proof.state *)
blanchet@33552
   479
fun auto_nitpick state =
blanchet@33552
   480
  if not (!auto) then (false, state) else pick_nits [] true 1 state
blanchet@33552
   481
blanchet@33552
   482
val setup = Auto_Counterexample.register_tool ("nitpick", auto_nitpick)
blanchet@33552
   483
blanchet@33192
   484
end;