src/HOL/Tools/Nitpick/nitpick_isar.ML
author wenzelm
Tue, 27 Oct 2009 11:25:56 +0100
changeset 33220 11a1af478dac
parent 33197 de6285ebcc05
child 33224 f93390060bbe
permissions -rw-r--r--
SimpleThread.fork: uniform handling of outermost Interrupt, which is not an error and should not produce exception trace;
blanchet@33192
     1
(*  Title:      HOL/Nitpick/Tools/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@33192
    13
  val default_params : theory -> (string * string) list -> params
blanchet@33192
    14
end
blanchet@33192
    15
blanchet@33192
    16
structure NitpickIsar : NITPICK_ISAR =
blanchet@33192
    17
struct
blanchet@33192
    18
blanchet@33192
    19
open NitpickUtil
blanchet@33192
    20
open NitpickHOL
blanchet@33192
    21
open NitpickRep
blanchet@33192
    22
open NitpickNut
blanchet@33192
    23
open Nitpick
blanchet@33192
    24
blanchet@33192
    25
type raw_param = string * string list
blanchet@33192
    26
blanchet@33192
    27
val default_default_params =
blanchet@33192
    28
  [("card", ["1\<midarrow>8"]),
blanchet@33192
    29
   ("iter", ["0,1,2,4,8,12,16,24"]),
blanchet@33192
    30
   ("bisim_depth", ["7"]),
blanchet@33192
    31
   ("box", ["smart"]),
blanchet@33192
    32
   ("mono", ["smart"]),
blanchet@33192
    33
   ("wf", ["smart"]),
blanchet@33192
    34
   ("sat_solver", ["smart"]),
blanchet@33192
    35
   ("batch_size", ["smart"]),
blanchet@33192
    36
   ("auto", ["false"]),
blanchet@33192
    37
   ("blocking", ["true"]),
blanchet@33192
    38
   ("falsify", ["true"]),
blanchet@33192
    39
   ("user_axioms", ["smart"]),
blanchet@33192
    40
   ("assms", ["true"]),
blanchet@33192
    41
   ("coalesce_type_vars", ["false"]),
blanchet@33192
    42
   ("destroy_constrs", ["true"]),
blanchet@33192
    43
   ("specialize", ["true"]),
blanchet@33192
    44
   ("skolemize", ["true"]),
blanchet@33192
    45
   ("star_linear_preds", ["true"]),
blanchet@33192
    46
   ("uncurry", ["true"]),
blanchet@33192
    47
   ("fast_descrs", ["true"]),
blanchet@33192
    48
   ("peephole_optim", ["true"]),
blanchet@33192
    49
   ("timeout", ["30 s"]),
blanchet@33192
    50
   ("auto_timeout", ["5 s"]),
blanchet@33192
    51
   ("tac_timeout", ["500 ms"]),
blanchet@33192
    52
   ("sym_break", ["20"]),
blanchet@33192
    53
   ("sharing_depth", ["3"]),
blanchet@33192
    54
   ("flatten_props", ["false"]),
blanchet@33192
    55
   ("max_threads", ["0"]),
blanchet@33192
    56
   ("verbose", ["false"]),
blanchet@33192
    57
   ("debug", ["false"]),
blanchet@33197
    58
   ("overlord", ["false"]),
blanchet@33192
    59
   ("show_all", ["false"]),
blanchet@33192
    60
   ("show_skolems", ["true"]),
blanchet@33192
    61
   ("show_datatypes", ["false"]),
blanchet@33192
    62
   ("show_consts", ["false"]),
blanchet@33192
    63
   ("format", ["1"]),
blanchet@33192
    64
   ("max_potential", ["1"]),
blanchet@33192
    65
   ("max_genuine", ["1"]),
blanchet@33192
    66
   ("check_potential", ["false"]),
blanchet@33192
    67
   ("check_genuine", ["false"])]
blanchet@33192
    68
blanchet@33192
    69
val negated_params =
blanchet@33192
    70
  [("dont_box", "box"),
blanchet@33192
    71
   ("non_mono", "mono"),
blanchet@33192
    72
   ("non_wf", "wf"),
blanchet@33192
    73
   ("no_auto", "auto"),
blanchet@33192
    74
   ("non_blocking", "blocking"),
blanchet@33192
    75
   ("satisfy", "falsify"),
blanchet@33192
    76
   ("no_user_axioms", "user_axioms"),
blanchet@33192
    77
   ("no_assms", "assms"),
blanchet@33192
    78
   ("dont_coalesce_type_vars", "coalesce_type_vars"),
blanchet@33192
    79
   ("dont_destroy_constrs", "destroy_constrs"),
blanchet@33192
    80
   ("dont_specialize", "specialize"),
blanchet@33192
    81
   ("dont_skolemize", "skolemize"),
blanchet@33192
    82
   ("dont_star_linear_preds", "star_linear_preds"),
blanchet@33192
    83
   ("dont_uncurry", "uncurry"),
blanchet@33192
    84
   ("full_descrs", "fast_descrs"),
blanchet@33192
    85
   ("no_peephole_optim", "peephole_optim"),
blanchet@33192
    86
   ("dont_flatten_props", "flatten_props"),
blanchet@33192
    87
   ("quiet", "verbose"),
blanchet@33192
    88
   ("no_debug", "debug"),
blanchet@33192
    89
   ("no_overlord", "overlord"),
blanchet@33192
    90
   ("dont_show_all", "show_all"),
blanchet@33192
    91
   ("hide_skolems", "show_skolems"),
blanchet@33192
    92
   ("hide_datatypes", "show_datatypes"),
blanchet@33192
    93
   ("hide_consts", "show_consts"),
blanchet@33192
    94
   ("trust_potential", "check_potential"),
blanchet@33192
    95
   ("trust_genuine", "check_genuine")]
blanchet@33192
    96
blanchet@33192
    97
(* string -> bool *)
blanchet@33192
    98
fun is_known_raw_param s =
blanchet@33192
    99
  AList.defined (op =) default_default_params s
blanchet@33192
   100
  orelse AList.defined (op =) negated_params s
blanchet@33192
   101
  orelse s mem ["max", "eval", "expect"]
blanchet@33192
   102
  orelse exists (fn p => String.isPrefix (p ^ " ") s)
blanchet@33192
   103
                ["card", "max", "iter", "box", "dont_box", "mono", "non_mono",
blanchet@33192
   104
                 "wf", "non_wf", "format"]
blanchet@33192
   105
blanchet@33192
   106
(* string * 'a -> unit *)
blanchet@33192
   107
fun check_raw_param (s, _) =
blanchet@33192
   108
  if is_known_raw_param s then ()
blanchet@33192
   109
  else error ("Unknown parameter " ^ quote s ^ ".")  
blanchet@33192
   110
blanchet@33192
   111
(* string -> string option *)
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
(* raw_param -> raw_param *)
blanchet@33192
   119
fun unnegate_raw_param (name, value) =
blanchet@33192
   120
  case unnegate_param_name name of
blanchet@33192
   121
    SOME name' => (name', case value of
blanchet@33192
   122
                            ["false"] => ["true"]
blanchet@33192
   123
                          | ["true"] => ["false"]
blanchet@33192
   124
                          | [] => ["false"]
blanchet@33192
   125
                          | _ => value)
blanchet@33192
   126
  | NONE => (name, value)
blanchet@33192
   127
blanchet@33192
   128
structure TheoryData = TheoryDataFun(
blanchet@33192
   129
  type T = {params: raw_param list, registered_auto: bool}
blanchet@33192
   130
  val empty = {params = rev default_default_params, registered_auto = false}
blanchet@33192
   131
  val copy = I
blanchet@33192
   132
  val extend = I
blanchet@33192
   133
  fun merge _ ({params = ps1, registered_auto = a1},
blanchet@33192
   134
               {params = ps2, registered_auto = a2}) =
blanchet@33192
   135
    {params = AList.merge (op =) (op =) (ps1, ps2),
blanchet@33192
   136
     registered_auto = a1 orelse a2})
blanchet@33192
   137
blanchet@33192
   138
(* raw_param -> theory -> theory *)
blanchet@33192
   139
fun set_default_raw_param param thy =
blanchet@33192
   140
  let val {params, registered_auto} = TheoryData.get thy in
blanchet@33192
   141
    TheoryData.put
blanchet@33192
   142
      {params = AList.update (op =) (unnegate_raw_param param) params,
blanchet@33192
   143
       registered_auto = registered_auto} thy
blanchet@33192
   144
  end
blanchet@33192
   145
(* theory -> raw_param list *)
blanchet@33192
   146
val default_raw_params = #params o TheoryData.get
blanchet@33192
   147
blanchet@33192
   148
(* theory -> theory *)
blanchet@33192
   149
fun set_registered_auto thy =
blanchet@33192
   150
  TheoryData.put {params = default_raw_params thy, registered_auto = true} thy
blanchet@33192
   151
(* theory -> bool *)
blanchet@33192
   152
val is_registered_auto = #registered_auto o TheoryData.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@33192
   289
    val bisim_depths = lookup_int_seq "bisim_depth" ~1
blanchet@33192
   290
    val boxes =
blanchet@33192
   291
      lookup_bool_option_assigns read_type_polymorphic "box" @
blanchet@33192
   292
      map_filter (fn (SOME T, _) =>
blanchet@33192
   293
                     if is_fun_type T orelse is_pair_type T then
blanchet@33192
   294
                       SOME (SOME T, SOME true)
blanchet@33192
   295
                     else
blanchet@33192
   296
                       NONE
blanchet@33192
   297
                   | (NONE, _) => NONE) cards_assigns
blanchet@33192
   298
    val monos = lookup_bool_option_assigns read_type_polymorphic "mono"
blanchet@33192
   299
    val wfs = lookup_bool_option_assigns read_const_polymorphic "wf"
blanchet@33192
   300
    val sat_solver = lookup_string "sat_solver"
blanchet@33192
   301
    val blocking = not auto andalso lookup_bool "blocking"
blanchet@33192
   302
    val falsify = lookup_bool "falsify"
blanchet@33192
   303
    val debug = not auto andalso lookup_bool "debug"
blanchet@33192
   304
    val verbose = debug orelse (not auto andalso lookup_bool "verbose")
blanchet@33192
   305
    val overlord = lookup_bool "overlord"
blanchet@33192
   306
    val user_axioms = lookup_bool_option "user_axioms"
blanchet@33192
   307
    val assms = lookup_bool "assms"
blanchet@33192
   308
    val coalesce_type_vars = lookup_bool "coalesce_type_vars"
blanchet@33192
   309
    val destroy_constrs = lookup_bool "destroy_constrs"
blanchet@33192
   310
    val specialize = lookup_bool "specialize"
blanchet@33192
   311
    val skolemize = lookup_bool "skolemize"
blanchet@33192
   312
    val star_linear_preds = lookup_bool "star_linear_preds"
blanchet@33192
   313
    val uncurry = lookup_bool "uncurry"
blanchet@33192
   314
    val fast_descrs = lookup_bool "fast_descrs"
blanchet@33192
   315
    val peephole_optim = lookup_bool "peephole_optim"
blanchet@33192
   316
    val timeout = if auto then lookup_time "auto_timeout"
blanchet@33192
   317
                  else lookup_time "timeout"
blanchet@33192
   318
    val tac_timeout = lookup_time "tac_timeout"
blanchet@33192
   319
    val sym_break = Int.max (0, lookup_int "sym_break")
blanchet@33192
   320
    val sharing_depth = Int.max (1, lookup_int "sharing_depth")
blanchet@33192
   321
    val flatten_props = lookup_bool "flatten_props"
blanchet@33192
   322
    val max_threads = Int.max (0, lookup_int "max_threads")
blanchet@33192
   323
    val show_all = debug orelse lookup_bool "show_all"
blanchet@33192
   324
    val show_skolems = show_all orelse lookup_bool "show_skolems"
blanchet@33192
   325
    val show_datatypes = show_all orelse lookup_bool "show_datatypes"
blanchet@33192
   326
    val show_consts = show_all orelse lookup_bool "show_consts"
blanchet@33192
   327
    val formats = lookup_ints_assigns read_term_polymorphic "format" 0
blanchet@33192
   328
    val evals = lookup_term_list "eval"
blanchet@33192
   329
    val max_potential = if auto then 0
blanchet@33192
   330
                        else Int.max (0, lookup_int "max_potential")
blanchet@33192
   331
    val max_genuine = Int.max (0, lookup_int "max_genuine")
blanchet@33192
   332
    val check_potential = lookup_bool "check_potential"
blanchet@33192
   333
    val check_genuine = lookup_bool "check_genuine"
blanchet@33192
   334
    val batch_size = case lookup_int_option "batch_size" of
blanchet@33192
   335
                       SOME n => Int.max (1, n)
blanchet@33192
   336
                     | NONE => if debug then 1 else 64
blanchet@33192
   337
    val expect = lookup_string "expect"
blanchet@33192
   338
  in
blanchet@33192
   339
    {cards_assigns = cards_assigns, maxes_assigns = maxes_assigns,
blanchet@33192
   340
     iters_assigns = iters_assigns, bisim_depths = bisim_depths, boxes = boxes,
blanchet@33192
   341
     monos = monos, wfs = wfs, sat_solver = sat_solver, blocking = blocking,
blanchet@33192
   342
     falsify = falsify, debug = debug, verbose = verbose, overlord = overlord,
blanchet@33192
   343
     user_axioms = user_axioms, assms = assms,
blanchet@33192
   344
     coalesce_type_vars = coalesce_type_vars, destroy_constrs = destroy_constrs,
blanchet@33192
   345
     specialize = specialize, skolemize = skolemize,
blanchet@33192
   346
     star_linear_preds = star_linear_preds, uncurry = uncurry,
blanchet@33192
   347
     fast_descrs = fast_descrs, peephole_optim = peephole_optim,
blanchet@33192
   348
     timeout = timeout, tac_timeout = tac_timeout, sym_break = sym_break,
blanchet@33192
   349
     sharing_depth = sharing_depth, flatten_props = flatten_props,
blanchet@33192
   350
     max_threads = max_threads, show_skolems = show_skolems,
blanchet@33192
   351
     show_datatypes = show_datatypes, show_consts = show_consts,
blanchet@33192
   352
     formats = formats, evals = evals, max_potential = max_potential,
blanchet@33192
   353
     max_genuine = max_genuine, check_potential = check_potential,
blanchet@33192
   354
     check_genuine = check_genuine, batch_size = batch_size, expect = expect}
blanchet@33192
   355
  end
blanchet@33192
   356
blanchet@33192
   357
(* theory -> (string * string) list -> params *)
blanchet@33192
   358
fun default_params thy =
blanchet@33192
   359
  extract_params (ProofContext.init thy) false (default_raw_params thy)
blanchet@33192
   360
  o map (apsnd single)
blanchet@33192
   361
blanchet@33192
   362
(* OuterParse.token list -> string * OuterParse.token list *)
blanchet@33192
   363
val scan_key = Scan.repeat1 OuterParse.typ_group >> space_implode " "
blanchet@33192
   364
blanchet@33192
   365
(* OuterParse.token list -> string list * OuterParse.token list *)
blanchet@33192
   366
val scan_value =
blanchet@33192
   367
  Scan.repeat1 (OuterParse.minus >> single
blanchet@33192
   368
                || Scan.repeat1 (Scan.unless OuterParse.minus OuterParse.name)
blanchet@33192
   369
                || OuterParse.$$$ "," |-- OuterParse.number >> prefix ","
blanchet@33192
   370
                   >> single) >> flat
blanchet@33192
   371
blanchet@33192
   372
(* OuterParse.token list -> raw_param * OuterParse.token list *)
blanchet@33192
   373
val scan_param =
blanchet@33192
   374
  scan_key -- (Scan.option (OuterParse.$$$ "=" |-- scan_value) >> these)
blanchet@33192
   375
(* OuterParse.token list -> raw_param list option * OuterParse.token list *)
blanchet@33192
   376
val scan_params = Scan.option (OuterParse.$$$ "[" |-- OuterParse.list scan_param
blanchet@33192
   377
                               --| OuterParse.$$$ "]")
blanchet@33192
   378
blanchet@33192
   379
(* Proof.context -> ('a -> 'a) -> 'a -> 'a *)
blanchet@33192
   380
fun handle_exceptions ctxt f x =
blanchet@33192
   381
  f x
blanchet@33192
   382
  handle ARG (loc, details) =>
blanchet@33192
   383
         error ("Bad argument(s) to " ^ quote loc ^ ": " ^ details ^ ".")
blanchet@33192
   384
       | BAD (loc, details) =>
blanchet@33192
   385
         error ("Internal error (" ^ quote loc ^ "): " ^ details ^ ".")
blanchet@33192
   386
       | LIMIT (_, details) =>
blanchet@33192
   387
         (warning ("Limit reached: " ^ details ^ "."); x)
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@33192
   401
       | TYPE (loc, Ts, ts) =>
blanchet@33192
   402
         error ("Invalid type" ^ plural_s_for_list Ts ^
blanchet@33192
   403
                (if null ts then
blanchet@33192
   404
                   ""
blanchet@33192
   405
                 else
blanchet@33192
   406
                   " for term" ^ plural_s_for_list ts ^ " " ^
blanchet@33192
   407
                   commas (map (quote o Syntax.string_of_term ctxt) ts)) ^
blanchet@33192
   408
                " (" ^ quote loc ^ "): " ^
blanchet@33192
   409
                commas (map (Syntax.string_of_typ ctxt) Ts) ^ ".")
blanchet@33192
   410
       | Kodkod.SYNTAX (_, details) =>
blanchet@33192
   411
         (warning ("Ill-formed Kodkodi output: " ^ details ^ "."); x)
blanchet@33192
   412
       | Refute.REFUTE (loc, details) =>
blanchet@33192
   413
         error ("Unhandled Refute error (" ^ quote loc ^ "): " ^ details ^ ".")
blanchet@33192
   414
blanchet@33192
   415
(* raw_param list -> bool -> int -> Proof.state -> Proof.state *)
blanchet@33192
   416
fun pick_nits override_params auto subgoal state =
blanchet@33192
   417
  let
blanchet@33192
   418
    val thy = Proof.theory_of state
blanchet@33192
   419
    val ctxt = Proof.context_of state
blanchet@33192
   420
    val thm = snd (snd (Proof.get_goal state))
blanchet@33192
   421
    val _ = List.app check_raw_param override_params
blanchet@33192
   422
    val params as {blocking, debug, ...} =
blanchet@33192
   423
      extract_params ctxt auto (default_raw_params thy) override_params
blanchet@33192
   424
    (* unit -> Proof.state *)
blanchet@33192
   425
    fun go () =
blanchet@33192
   426
      (if auto then perhaps o try
blanchet@33192
   427
       else if debug then fn f => fn x => f x
blanchet@33192
   428
       else handle_exceptions ctxt)
blanchet@33192
   429
      (fn state => pick_nits_in_subgoal state params auto subgoal |> snd)
blanchet@33192
   430
      state
blanchet@33192
   431
  in
blanchet@33192
   432
    if auto orelse blocking then
blanchet@33192
   433
      go ()
blanchet@33192
   434
    else
wenzelm@33220
   435
      (SimpleThread.fork true (fn () => (go (); ()));
blanchet@33192
   436
       state)
blanchet@33192
   437
  end
blanchet@33192
   438
blanchet@33192
   439
(* (TableFun().key * string list) list option * int option
blanchet@33192
   440
   -> Toplevel.transition -> Toplevel.transition *)
blanchet@33192
   441
fun nitpick_trans (opt_params, opt_subgoal) =
blanchet@33192
   442
  Toplevel.keep (K ()
blanchet@33192
   443
      o pick_nits (these opt_params) false (the_default 1 opt_subgoal)
blanchet@33192
   444
      o Toplevel.proof_of)
blanchet@33192
   445
blanchet@33192
   446
(* raw_param -> string *)
blanchet@33192
   447
fun string_for_raw_param (name, value) =
blanchet@33192
   448
  name ^ " = " ^ stringify_raw_param_value value
blanchet@33192
   449
blanchet@33192
   450
(* bool -> Proof.state -> Proof.state *)
blanchet@33192
   451
fun pick_nits_auto interactive state =
blanchet@33192
   452
  let val thy = Proof.theory_of state in
blanchet@33192
   453
    ((interactive andalso not (!Toplevel.quiet)
blanchet@33192
   454
      andalso the (general_lookup_bool false (default_raw_params thy)
blanchet@33192
   455
                  (SOME false) "auto"))
blanchet@33192
   456
     ? pick_nits [] true 0) state
blanchet@33192
   457
  end
blanchet@33192
   458
blanchet@33192
   459
(* theory -> theory *)
blanchet@33192
   460
fun register_auto thy =
blanchet@33192
   461
  (not (is_registered_auto thy)
blanchet@33192
   462
   ? (set_registered_auto
blanchet@33192
   463
      #> Context.theory_map (Specification.add_theorem_hook pick_nits_auto)))
blanchet@33192
   464
  thy
blanchet@33192
   465
blanchet@33192
   466
(* (TableFun().key * string) list option -> Toplevel.transition
blanchet@33192
   467
   -> Toplevel.transition *)
blanchet@33192
   468
fun nitpick_params_trans opt_params =
blanchet@33192
   469
  Toplevel.theory
blanchet@33192
   470
      (fn thy =>
blanchet@33192
   471
          let val thy = fold set_default_raw_param (these opt_params) thy in
blanchet@33192
   472
            writeln ("Default parameters for Nitpick:\n" ^
blanchet@33192
   473
                     (case rev (default_raw_params thy) of
blanchet@33192
   474
                        [] => "none"
blanchet@33192
   475
                      | params =>
blanchet@33192
   476
                        (map check_raw_param params;
blanchet@33192
   477
                         params |> map string_for_raw_param |> sort_strings
blanchet@33192
   478
                                |> cat_lines)));
blanchet@33192
   479
            register_auto thy
blanchet@33192
   480
          end)
blanchet@33192
   481
blanchet@33192
   482
(* OuterParse.token list
blanchet@33192
   483
   -> (Toplevel.transition -> Toplevel.transition) * OuterParse.token list *)
blanchet@33192
   484
fun scan_nitpick_command tokens =
blanchet@33192
   485
  (scan_params -- Scan.option OuterParse.nat) tokens |>> nitpick_trans
blanchet@33192
   486
fun scan_nitpick_params_command tokens =
blanchet@33192
   487
  scan_params tokens |>> nitpick_params_trans
blanchet@33192
   488
blanchet@33192
   489
val _ = OuterSyntax.improper_command "nitpick"
blanchet@33192
   490
            "try to find a counterexample for a given subgoal using Kodkod"
blanchet@33192
   491
            OuterKeyword.diag scan_nitpick_command
blanchet@33192
   492
val _ = OuterSyntax.command "nitpick_params"
blanchet@33192
   493
            "set and display the default parameters for Nitpick"
blanchet@33192
   494
            OuterKeyword.thy_decl scan_nitpick_params_command
blanchet@33192
   495
blanchet@33192
   496
end;