src/Tools/quickcheck.ML
author bulwahn
Wed, 21 Jul 2010 18:11:51 +0200
changeset 38153 e85f5ad02a8f
parent 38152 247042107f93
child 38169 22e0797857e6
permissions -rw-r--r--
correcting wellsortedness check and improving error message
wenzelm@30826
     1
(*  Title:      Tools/quickcheck.ML
haftmann@28256
     2
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
haftmann@28256
     3
haftmann@28256
     4
Generic counterexample search engine.
haftmann@28256
     5
*)
haftmann@28256
     6
haftmann@28256
     7
signature QUICKCHECK =
haftmann@28256
     8
sig
bulwahn@38150
     9
  val setup: theory -> theory
bulwahn@38150
    10
  (* configuration *)
wenzelm@32740
    11
  val auto: bool Unsynchronized.ref
bulwahn@34935
    12
  val timing : bool Unsynchronized.ref
bulwahn@35378
    13
  datatype report = Report of
bulwahn@35378
    14
    { iterations : int, raised_match_errors : int,
bulwahn@35378
    15
      satisfied_assms : int list, positive_concl_tests : int }
bulwahn@38150
    16
  datatype test_params = Test_Params of
bulwahn@38150
    17
  { size: int, iterations: int, default_type: typ list, no_assms: bool, report: bool, quiet : bool};
bulwahn@38150
    18
  val test_params_of: theory -> test_params 
bulwahn@38150
    19
  val add_generator:
bulwahn@38150
    20
    string * (Proof.context -> bool -> term -> int -> term list option * (bool list * bool))
bulwahn@38150
    21
      -> theory -> theory
bulwahn@38150
    22
  (* testing terms and proof states *)
bulwahn@35378
    23
  val gen_test_term: Proof.context -> bool -> bool -> string option -> int -> int -> term ->
bulwahn@35380
    24
    (string * term) list option * ((string * int) list * ((int * report list) list) option)
wenzelm@30981
    25
  val test_term: Proof.context -> bool -> string option -> int -> int -> term ->
wenzelm@30981
    26
    (string * term) list option
bulwahn@38149
    27
  val quickcheck: (string * string list) list -> int -> Proof.state -> (string * term) list option
haftmann@28256
    28
end;
haftmann@28256
    29
haftmann@28256
    30
structure Quickcheck : QUICKCHECK =
haftmann@28256
    31
struct
haftmann@28256
    32
wenzelm@30981
    33
(* preferences *)
wenzelm@30981
    34
wenzelm@32740
    35
val auto = Unsynchronized.ref false;
wenzelm@30981
    36
bulwahn@34935
    37
val timing = Unsynchronized.ref false;
bulwahn@34935
    38
wenzelm@30981
    39
val _ =
wenzelm@30981
    40
  ProofGeneralPgip.add_preference Preferences.category_tracing
wenzelm@32966
    41
  (setmp_CRITICAL auto true (fn () =>
wenzelm@30981
    42
    Preferences.bool_pref auto
wenzelm@30981
    43
      "auto-quickcheck"
blanchet@33552
    44
      "Whether to run Quickcheck automatically.") ());
wenzelm@30981
    45
bulwahn@35378
    46
(* quickcheck report *)
bulwahn@35378
    47
bulwahn@35378
    48
datatype single_report = Run of bool list * bool | MatchExc
bulwahn@35378
    49
bulwahn@35378
    50
datatype report = Report of
bulwahn@35378
    51
  { iterations : int, raised_match_errors : int,
bulwahn@35378
    52
    satisfied_assms : int list, positive_concl_tests : int }
bulwahn@35378
    53
bulwahn@35378
    54
fun collect_single_report single_report
bulwahn@35378
    55
    (Report {iterations = iterations, raised_match_errors = raised_match_errors,
bulwahn@35378
    56
    satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
bulwahn@35378
    57
  case single_report
bulwahn@35378
    58
  of MatchExc =>
bulwahn@35378
    59
    Report {iterations = iterations + 1, raised_match_errors = raised_match_errors + 1,
bulwahn@35378
    60
      satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}
bulwahn@35378
    61
   | Run (assms, concl) =>
bulwahn@35378
    62
    Report {iterations = iterations + 1, raised_match_errors = raised_match_errors,
bulwahn@35378
    63
      satisfied_assms =
bulwahn@35378
    64
        map2 (fn b => fn s => if b then s + 1 else s) assms
bulwahn@35378
    65
         (if null satisfied_assms then replicate (length assms) 0 else satisfied_assms),
bulwahn@35378
    66
      positive_concl_tests = if concl then positive_concl_tests + 1 else positive_concl_tests}
haftmann@30973
    67
haftmann@28315
    68
(* quickcheck configuration -- default parameters, test generators *)
haftmann@28315
    69
haftmann@28309
    70
datatype test_params = Test_Params of
bulwahn@38149
    71
  { size: int, iterations: int, default_type: typ list, no_assms: bool, report: bool, quiet : bool};
haftmann@28309
    72
bulwahn@35379
    73
fun dest_test_params (Test_Params { size, iterations, default_type, no_assms, report, quiet }) =
bulwahn@35379
    74
  ((size, iterations), ((default_type, no_assms), (report, quiet)));
bulwahn@35379
    75
fun make_test_params ((size, iterations), ((default_type, no_assms), (report, quiet))) =
blanchet@34125
    76
  Test_Params { size = size, iterations = iterations, default_type = default_type,
bulwahn@35379
    77
                no_assms = no_assms, report = report, quiet = quiet };
bulwahn@35379
    78
fun map_test_params f (Test_Params { size, iterations, default_type, no_assms, report, quiet }) =
bulwahn@35379
    79
  make_test_params (f ((size, iterations), ((default_type, no_assms), (report, quiet))));
blanchet@34125
    80
fun merge_test_params (Test_Params { size = size1, iterations = iterations1, default_type = default_type1,
bulwahn@35379
    81
                                     no_assms = no_assms1, report = report1, quiet = quiet1 },
blanchet@34125
    82
  Test_Params { size = size2, iterations = iterations2, default_type = default_type2,
bulwahn@35379
    83
                no_assms = no_assms2, report = report2, quiet = quiet2 }) =
haftmann@31599
    84
  make_test_params ((Int.max (size1, size2), Int.max (iterations1, iterations2)),
bulwahn@38151
    85
    ((merge (op =) (default_type1, default_type2), no_assms1 orelse no_assms2),
bulwahn@35379
    86
    (report1 orelse report2, quiet1 orelse quiet2)));
haftmann@28309
    87
wenzelm@33522
    88
structure Data = Theory_Data
wenzelm@33522
    89
(
bulwahn@35378
    90
  type T = (string * (Proof.context -> bool -> term -> int -> term list option * (bool list * bool))) list
haftmann@28309
    91
    * test_params;
bulwahn@35378
    92
  val empty = ([], Test_Params
bulwahn@38149
    93
    { size = 10, iterations = 100, default_type = [], no_assms = false, report = false, quiet = false});
haftmann@28256
    94
  val extend = I;
wenzelm@33522
    95
  fun merge ((generators1, params1), (generators2, params2)) : T =
wenzelm@33522
    96
    (AList.merge (op =) (K true) (generators1, generators2),
haftmann@28309
    97
      merge_test_params (params1, params2));
wenzelm@33522
    98
);
haftmann@28256
    99
bulwahn@38150
   100
val test_params_of = snd o Data.get;
bulwahn@38150
   101
haftmann@28309
   102
val add_generator = Data.map o apfst o AList.update (op =);
haftmann@28256
   103
haftmann@28315
   104
(* generating tests *)
haftmann@28315
   105
haftmann@28309
   106
fun mk_tester_select name ctxt =
haftmann@28309
   107
  case AList.lookup (op =) ((fst o Data.get o ProofContext.theory_of) ctxt) name
haftmann@28309
   108
   of NONE => error ("No such quickcheck generator: " ^ name)
haftmann@28309
   109
    | SOME generator => generator ctxt;
haftmann@28256
   110
bulwahn@35378
   111
fun mk_testers ctxt report t =
haftmann@28309
   112
  (map snd o fst o Data.get o ProofContext.theory_of) ctxt
bulwahn@35378
   113
  |> map_filter (fn generator => try (generator ctxt report) t);
haftmann@28256
   114
bulwahn@35378
   115
fun mk_testers_strict ctxt report t =
haftmann@28309
   116
  let
haftmann@28309
   117
    val generators = ((map snd o fst o Data.get o ProofContext.theory_of) ctxt)
bulwahn@35378
   118
    val testers = map (fn generator => Exn.capture (generator ctxt report) t) generators;
haftmann@28309
   119
  in if forall (is_none o Exn.get_result) testers
haftmann@28309
   120
    then [(Exn.release o snd o split_last) testers]
haftmann@28309
   121
    else map_filter Exn.get_result testers
haftmann@28309
   122
  end;
haftmann@28309
   123
haftmann@28315
   124
haftmann@28315
   125
(* testing propositions *)
haftmann@28315
   126
haftmann@28309
   127
fun prep_test_term t =
haftmann@28309
   128
  let
wenzelm@29266
   129
    val _ = (null (Term.add_tvars t []) andalso null (Term.add_tfrees t [])) orelse
haftmann@28309
   130
      error "Term to be tested contains type variables";
wenzelm@29266
   131
    val _ = null (Term.add_vars t []) orelse
haftmann@28309
   132
      error "Term to be tested contains schematic variables";
haftmann@31138
   133
    val frees = Term.add_frees t [];
haftmann@28309
   134
  in (map fst frees, list_abs_free (frees, t)) end
haftmann@28309
   135
bulwahn@35324
   136
fun cpu_time description f =
bulwahn@35324
   137
  let
bulwahn@35324
   138
    val start = start_timing ()
bulwahn@35324
   139
    val result = Exn.capture f ()
bulwahn@35324
   140
    val time = Time.toMilliseconds (#cpu (end_timing start))
bulwahn@35324
   141
  in (Exn.release result, (description, time)) end
bulwahn@35324
   142
bulwahn@35378
   143
fun gen_test_term ctxt quiet report generator_name size i t =
haftmann@28309
   144
  let
haftmann@28309
   145
    val (names, t') = prep_test_term t;
bulwahn@35324
   146
    val (testers, comp_time) = cpu_time "quickcheck compilation"
bulwahn@35324
   147
      (fn () => (case generator_name
bulwahn@35378
   148
       of NONE => if quiet then mk_testers ctxt report t' else mk_testers_strict ctxt report t'
bulwahn@35378
   149
        | SOME name => [mk_tester_select name ctxt report t']));
bulwahn@35378
   150
    fun iterate f 0 report = (NONE, report)
bulwahn@35378
   151
      | iterate f j report =
bulwahn@35378
   152
        let
bulwahn@35378
   153
          val (test_result, single_report) = apsnd Run (f ()) handle Match => (if quiet then ()
bulwahn@35378
   154
             else warning "Exception Match raised during quickcheck"; (NONE, MatchExc))
bulwahn@35378
   155
          val report = collect_single_report single_report report
bulwahn@35378
   156
        in
bulwahn@35378
   157
          case test_result of NONE => iterate f (j - 1) report | SOME q => (SOME q, report)
bulwahn@35378
   158
        end
bulwahn@35378
   159
    val empty_report = Report { iterations = 0, raised_match_errors = 0,
bulwahn@35378
   160
      satisfied_assms = [], positive_concl_tests = 0 }
bulwahn@35378
   161
    fun with_testers k [] = (NONE, [])
haftmann@28309
   162
      | with_testers k (tester :: testers) =
bulwahn@35378
   163
          case iterate (fn () => tester (k - 1)) i empty_report
bulwahn@35378
   164
           of (NONE, report) => apsnd (cons report) (with_testers k testers)
bulwahn@35378
   165
            | (SOME q, report) => (SOME q, [report]);
bulwahn@35378
   166
    fun with_size k reports = if k > size then (NONE, reports)
haftmann@28309
   167
      else (if quiet then () else priority ("Test data size: " ^ string_of_int k);
bulwahn@35378
   168
        let
bulwahn@35378
   169
          val (result, new_report) = with_testers k testers
bulwahn@35378
   170
          val reports = ((k, new_report) :: reports)
bulwahn@35378
   171
        in case result of NONE => with_size (k + 1) reports | SOME q => (SOME q, reports) end);
bulwahn@35378
   172
    val ((result, reports), exec_time) = cpu_time "quickcheck execution"
bulwahn@35378
   173
      (fn () => apfst
bulwahn@35378
   174
         (fn result => case result of NONE => NONE
bulwahn@35378
   175
        | SOME ts => SOME (names ~~ ts)) (with_size 1 []))
bulwahn@34935
   176
  in
bulwahn@35380
   177
    (result, ([exec_time, comp_time], if report then SOME reports else NONE))
haftmann@28309
   178
  end;
haftmann@28309
   179
bulwahn@35324
   180
fun test_term ctxt quiet generator_name size i t =
bulwahn@35378
   181
  fst (gen_test_term ctxt quiet false generator_name size i t)
bulwahn@35324
   182
bulwahn@38153
   183
exception WELLSORTED of string
bulwahn@38153
   184
haftmann@28309
   185
fun monomorphic_term thy insts default_T = 
haftmann@28309
   186
  let
haftmann@28309
   187
    fun subst (T as TFree (v, S)) =
haftmann@28309
   188
          let
haftmann@28309
   189
            val T' = AList.lookup (op =) insts v
bulwahn@38152
   190
              |> the_default default_T
bulwahn@38153
   191
          in if Sign.of_sort thy (T', S) then T'
bulwahn@38153
   192
            else raise (WELLSORTED ("For instantiation with default_type " ^ Syntax.string_of_typ_global thy default_T ^
bulwahn@38153
   193
              ":\n" ^ Syntax.string_of_typ_global thy T' ^
haftmann@28309
   194
              " to be substituted for variable " ^
bulwahn@38153
   195
              Syntax.string_of_typ_global thy T ^ " does not have sort " ^
bulwahn@38153
   196
              Syntax.string_of_sort_global thy S))
haftmann@28309
   197
          end
haftmann@28309
   198
      | subst T = T;
haftmann@28309
   199
  in (map_types o map_atyps) subst end;
haftmann@28309
   200
bulwahn@38153
   201
datatype wellsorted_error = Wellsorted_Error of string | Term of term
bulwahn@38153
   202
bulwahn@35378
   203
fun test_goal quiet report generator_name size iterations default_T no_assms insts i assms state =
haftmann@28309
   204
  let
haftmann@28309
   205
    val ctxt = Proof.context_of state;
haftmann@28309
   206
    val thy = Proof.theory_of state;
haftmann@28309
   207
    fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
haftmann@28309
   208
      | strip t = t;
wenzelm@33291
   209
    val {goal = st, ...} = Proof.raw_goal state;
haftmann@28309
   210
    val (gi, frees) = Logic.goal_params (prop_of st) i;
bulwahn@38153
   211
    val gi' = Logic.list_implies (if no_assms then [] else assms,
blanchet@34125
   212
                                  subst_bounds (frees, strip gi))
bulwahn@38153
   213
    val inst_goals = map (fn T =>
bulwahn@38153
   214
      Term (monomorphic_term thy insts T gi |> Object_Logic.atomize_term thy)
bulwahn@38153
   215
        handle WELLSORTED s => Wellsorted_Error s) default_T
bulwahn@38153
   216
    val error_msg = cat_lines (map_filter (fn Term t => NONE | Wellsorted_Error s => SOME s) inst_goals)
bulwahn@38153
   217
    val correct_inst_goals =
bulwahn@38153
   218
      case map_filter (fn Term t => SOME t | Wellsorted_Error s => NONE) inst_goals of
bulwahn@38153
   219
        [] => error error_msg
bulwahn@38153
   220
      | xs => xs
bulwahn@38153
   221
    val _ = if quiet then () else warning error_msg
bulwahn@38152
   222
    fun collect_results f reports [] = (NONE, rev reports)
bulwahn@38152
   223
      | collect_results f reports (t :: ts) =
bulwahn@38152
   224
        case f t of
bulwahn@38152
   225
          (SOME res, report) => (SOME res, rev (report :: reports))
bulwahn@38152
   226
        | (NONE, report) => collect_results f (report :: reports) ts
bulwahn@38153
   227
  in collect_results (gen_test_term ctxt quiet report generator_name size iterations) [] correct_inst_goals end;
bulwahn@38152
   228
bulwahn@38152
   229
(* pretty printing *)
haftmann@28315
   230
blanchet@35074
   231
fun pretty_counterex ctxt NONE = Pretty.str "Quickcheck found no counterexample."
haftmann@28315
   232
  | pretty_counterex ctxt (SOME cex) =
blanchet@35074
   233
      Pretty.chunks (Pretty.str "Quickcheck found a counterexample:\n" ::
haftmann@28315
   234
        map (fn (s, t) =>
haftmann@28315
   235
          Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) cex);
haftmann@28315
   236
bulwahn@35378
   237
fun pretty_report (Report {iterations = iterations, raised_match_errors = raised_match_errors,
bulwahn@35378
   238
    satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
bulwahn@35378
   239
  let
bulwahn@35378
   240
    fun pretty_stat s i = Pretty.block ([Pretty.str (s ^ ": " ^ string_of_int i)])
bulwahn@35378
   241
  in
bulwahn@35378
   242
     ([pretty_stat "iterations" iterations,
bulwahn@35378
   243
     pretty_stat "match exceptions" raised_match_errors]
bulwahn@35378
   244
     @ map_index (fn (i, n) => pretty_stat ("satisfied " ^ string_of_int (i + 1) ^ ". assumption") n)
bulwahn@35378
   245
       satisfied_assms
bulwahn@35378
   246
     @ [pretty_stat "positive conclusion tests" positive_concl_tests])
bulwahn@35378
   247
  end
bulwahn@35378
   248
bulwahn@35378
   249
fun pretty_reports' [report] = [Pretty.chunks (pretty_report report)]
bulwahn@35378
   250
  | pretty_reports' reports =
bulwahn@35378
   251
  map_index (fn (i, report) =>
bulwahn@35378
   252
    Pretty.chunks (Pretty.str (string_of_int (i + 1) ^ ". generator:\n") :: pretty_report report))
bulwahn@35378
   253
    reports
bulwahn@35378
   254
bulwahn@35380
   255
fun pretty_reports ctxt (SOME reports) =
bulwahn@35378
   256
  Pretty.chunks (Pretty.str "Quickcheck report:" ::
bulwahn@35378
   257
    maps (fn (size, reports) =>
bulwahn@35378
   258
      Pretty.str ("size " ^ string_of_int size ^ ":") :: pretty_reports' reports @ [Pretty.brk 1])
bulwahn@35378
   259
      (rev reports))
bulwahn@35380
   260
  | pretty_reports ctxt NONE = Pretty.str ""
bulwahn@35378
   261
bulwahn@38152
   262
fun pretty_counterex_and_reports ctxt (cex, timing_and_reports) =
bulwahn@38152
   263
  Pretty.chunks (pretty_counterex ctxt cex :: map (pretty_reports ctxt) (map snd timing_and_reports))
haftmann@28315
   264
haftmann@28315
   265
(* automatic testing *)
haftmann@28309
   266
blanchet@33552
   267
fun auto_quickcheck state =
blanchet@33552
   268
  if not (!auto) then
blanchet@33552
   269
    (false, state)
blanchet@33552
   270
  else
blanchet@33552
   271
    let
blanchet@33552
   272
      val ctxt = Proof.context_of state;
blanchet@33552
   273
      val assms = map term_of (Assumption.all_assms_of ctxt);
bulwahn@35379
   274
      val Test_Params { size, iterations, default_type, no_assms, report, quiet } =
blanchet@33552
   275
        (snd o Data.get o Proof.theory_of) state;
blanchet@33552
   276
      val res =
bulwahn@35378
   277
        try (test_goal true false NONE size iterations default_type no_assms [] 1 assms) state;
blanchet@33552
   278
    in
blanchet@33552
   279
      case res of
blanchet@33552
   280
        NONE => (false, state)
bulwahn@35378
   281
      | SOME (NONE, report) => (false, state)
bulwahn@35378
   282
      | SOME (cex, report) => (true, Proof.goal_message (K (Pretty.chunks [Pretty.str "",
blanchet@33552
   283
          Pretty.mark Markup.hilite (pretty_counterex ctxt cex)])) state)
blanchet@33552
   284
    end
blanchet@33552
   285
blanchet@33552
   286
val setup = Auto_Counterexample.register_tool ("quickcheck", auto_quickcheck)
haftmann@28309
   287
haftmann@28309
   288
wenzelm@30981
   289
(* Isar commands *)
haftmann@28315
   290
haftmann@28336
   291
fun read_nat s = case (Library.read_int o Symbol.explode) s
haftmann@28336
   292
 of (k, []) => if k >= 0 then k
haftmann@28336
   293
      else error ("Not a natural number: " ^ s)
haftmann@28336
   294
  | (_, _ :: _) => error ("Not a natural number: " ^ s);
bulwahn@38149
   295
blanchet@34125
   296
fun read_bool "false" = false
blanchet@34125
   297
  | read_bool "true" = true
blanchet@34125
   298
  | read_bool s = error ("Not a Boolean value: " ^ s)
haftmann@28315
   299
bulwahn@38149
   300
fun parse_test_param ctxt ("size", [arg]) =
haftmann@28336
   301
      (apfst o apfst o K) (read_nat arg)
bulwahn@38149
   302
  | parse_test_param ctxt ("iterations", [arg]) =
haftmann@28336
   303
      (apfst o apsnd o K) (read_nat arg)
haftmann@28336
   304
  | parse_test_param ctxt ("default_type", arg) =
bulwahn@38149
   305
      (apsnd o apfst o apfst o K) (map (ProofContext.read_typ ctxt) arg)
bulwahn@38149
   306
  | parse_test_param ctxt ("no_assms", [arg]) =
bulwahn@35378
   307
      (apsnd o apfst o apsnd o K) (read_bool arg)
bulwahn@38149
   308
  | parse_test_param ctxt ("report", [arg]) =
bulwahn@35379
   309
      (apsnd o apsnd o apfst o K) (read_bool arg)
bulwahn@38149
   310
  | parse_test_param ctxt ("quiet", [arg]) =
bulwahn@35379
   311
      (apsnd o apsnd o apsnd o K) (read_bool arg)
haftmann@28336
   312
  | parse_test_param ctxt (name, _) =
blanchet@34125
   313
      error ("Unknown test parameter: " ^ name);
haftmann@28315
   314
bulwahn@38149
   315
fun parse_test_param_inst ctxt ("generator", [arg]) =
haftmann@28336
   316
      (apsnd o apfst o K o SOME) arg
haftmann@28336
   317
  | parse_test_param_inst ctxt (name, arg) =
haftmann@28336
   318
      case try (ProofContext.read_typ ctxt) name
haftmann@28336
   319
       of SOME (TFree (v, _)) => (apsnd o apsnd o AList.update (op =))
bulwahn@38149
   320
              (v, ProofContext.read_typ ctxt (the_single arg))
haftmann@28336
   321
        | _ => (apfst o parse_test_param ctxt) (name, arg);
haftmann@28315
   322
haftmann@28336
   323
fun quickcheck_params_cmd args thy =
haftmann@28315
   324
  let
wenzelm@36633
   325
    val ctxt = ProofContext.init_global thy;
haftmann@28336
   326
    val f = fold (parse_test_param ctxt) args;
haftmann@28315
   327
  in
haftmann@28315
   328
    thy
haftmann@28336
   329
    |> (Data.map o apsnd o map_test_params) f
haftmann@28315
   330
  end;
haftmann@28315
   331
bulwahn@35378
   332
fun gen_quickcheck args i state =
haftmann@28315
   333
  let
boehmes@32295
   334
    val thy = Proof.theory_of state;
boehmes@32295
   335
    val ctxt = Proof.context_of state;
blanchet@34125
   336
    val assms = map term_of (Assumption.all_assms_of ctxt);
haftmann@28315
   337
    val default_params = (dest_test_params o snd o Data.get) thy;
haftmann@28336
   338
    val f = fold (parse_test_param_inst ctxt) args;
bulwahn@35379
   339
    val (((size, iterations), ((default_type, no_assms), (report, quiet))), (generator_name, insts)) =
haftmann@28336
   340
      f (default_params, (NONE, []));
boehmes@32295
   341
  in
bulwahn@35379
   342
    test_goal quiet report generator_name size iterations default_type no_assms insts i assms state
boehmes@32295
   343
  end;
boehmes@32295
   344
wenzelm@36970
   345
fun quickcheck args i state = fst (gen_quickcheck args i state);
bulwahn@35378
   346
boehmes@32295
   347
fun quickcheck_cmd args i state =
bulwahn@35378
   348
  gen_quickcheck args i (Toplevel.proof_of state)
bulwahn@35378
   349
  |> Pretty.writeln o pretty_counterex_and_reports (Toplevel.context_of state);
haftmann@28309
   350
bulwahn@38149
   351
val parse_arg = Parse.name -- (Scan.optional (Parse.$$$ "=" |-- 
bulwahn@38149
   352
  ((Parse.name >> single) || (Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]"))) ["true"]);
haftmann@28309
   353
wenzelm@36970
   354
val parse_args = Parse.$$$ "[" |-- Parse.list1 parse_arg --| Parse.$$$ "]"
haftmann@28336
   355
  || Scan.succeed [];
haftmann@28336
   356
wenzelm@36970
   357
val _ =
wenzelm@36970
   358
  Outer_Syntax.command "quickcheck_params" "set parameters for random testing" Keyword.thy_decl
wenzelm@36970
   359
    (parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
haftmann@28309
   360
wenzelm@36970
   361
val _ =
wenzelm@36970
   362
  Outer_Syntax.improper_command "quickcheck" "try to find counterexample for subgoal" Keyword.diag
wenzelm@36970
   363
    (parse_args -- Scan.optional Parse.nat 1
wenzelm@36970
   364
      >> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
haftmann@28309
   365
haftmann@28309
   366
end;
haftmann@28256
   367
haftmann@28315
   368
haftmann@28315
   369
val auto_quickcheck = Quickcheck.auto;