src/Tools/quickcheck.ML
author bulwahn
Wed, 20 Jul 2011 08:16:41 +0200
changeset 44786 ef347714c5c1
parent 44783 13e6a4e70219
child 46029 3f1d1ce024cb
permissions -rw-r--r--
removing inner time limits in quickcheck
wenzelm@30826
     1
(*  Title:      Tools/quickcheck.ML
bulwahn@40481
     2
    Author:     Stefan Berghofer, Florian Haftmann, Lukas Bulwahn, 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
blanchet@43861
     9
  val quickcheckN: string
blanchet@43861
    10
  val genuineN: string
blanchet@43861
    11
  val noneN: string
blanchet@43861
    12
  val unknownN: string
bulwahn@38150
    13
  val setup: theory -> theory
bulwahn@38150
    14
  (* configuration *)
wenzelm@32740
    15
  val auto: bool Unsynchronized.ref
bulwahn@44753
    16
  val batch_tester : string Config.T
bulwahn@40892
    17
  val size : int Config.T
bulwahn@40892
    18
  val iterations : int Config.T
bulwahn@40892
    19
  val no_assms : bool Config.T
bulwahn@40892
    20
  val report : bool Config.T
bulwahn@42952
    21
  val timing : bool Config.T
bulwahn@40892
    22
  val quiet : bool Config.T
bulwahn@40892
    23
  val timeout : real Config.T
bulwahn@40896
    24
  val finite_types : bool Config.T
bulwahn@40896
    25
  val finite_type_size : int Config.T
bulwahn@44783
    26
  val set_active_testers: string list -> Context.generic -> Context.generic
wenzelm@41765
    27
  datatype expectation = No_Expectation | No_Counterexample | Counterexample;
bulwahn@40892
    28
  datatype test_params = Test_Params of {default_type: typ list, expect : expectation};
bulwahn@40481
    29
  val test_params_of : Proof.context -> test_params
bulwahn@40892
    30
  val map_test_params : (typ list * expectation -> typ list * expectation)
bulwahn@40481
    31
    -> Context.generic -> Context.generic
bulwahn@42953
    32
  datatype report = Report of
bulwahn@42953
    33
    { iterations : int, raised_match_errors : int,
bulwahn@42953
    34
      satisfied_assms : int list, positive_concl_tests : int }
bulwahn@42953
    35
  (* quickcheck's result *)
bulwahn@42952
    36
  datatype result =
bulwahn@42952
    37
    Result of
bulwahn@42952
    38
     {counterexample : (string * term) list option,
bulwahn@42952
    39
      evaluation_terms : (term * term) list option,
bulwahn@42952
    40
      timings : (string * int) list,
bulwahn@42952
    41
      reports : (int * report) list}
bulwahn@44185
    42
  val empty_result : result
bulwahn@44456
    43
  val add_timing : (string * int) -> result Unsynchronized.ref -> unit
bulwahn@42953
    44
  val counterexample_of : result -> (string * term) list option
bulwahn@42953
    45
  val timings_of : result -> (string * int) list
bulwahn@44749
    46
  (* registering testers & generators *) 
bulwahn@44749
    47
  type tester =
bulwahn@44749
    48
    Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list
bulwahn@44749
    49
  val add_tester : string * (bool Config.T * tester) -> Context.generic -> Context.generic
bulwahn@44749
    50
  val add_batch_generator :
bulwahn@43953
    51
    string * (Proof.context -> term list -> (int -> term list option) list)
bulwahn@43953
    52
      -> Context.generic -> Context.generic
bulwahn@44749
    53
  val add_batch_validator :
bulwahn@43953
    54
    string * (Proof.context -> term list -> (int -> bool) list)
bulwahn@43953
    55
      -> Context.generic -> Context.generic
bulwahn@43954
    56
  (* basic operations *)
bulwahn@44747
    57
  type compile_generator =
bulwahn@44747
    58
    Proof.context -> (term * term list) list -> int list -> term list option * report option
bulwahn@44456
    59
  val limit : Time.time -> (bool * bool) -> (unit -> 'a) -> (unit -> 'a) -> unit -> 'a  
bulwahn@43954
    60
  val instantiate_goals: Proof.context -> (string * typ) list -> (term * term list) list
bulwahn@43954
    61
    -> (typ option * (term * term list)) list list
bulwahn@44749
    62
  val collect_results : ('a -> result) -> 'a list -> result list -> result list
bulwahn@44749
    63
  val generator_test_goal_terms : compile_generator ->
bulwahn@44747
    64
    Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list
bulwahn@38150
    65
  (* testing terms and proof states *)
bulwahn@44747
    66
  val test_term: compile_generator -> Proof.context -> bool * bool -> term * term list -> result
bulwahn@44754
    67
  val test_goal_terms :
bulwahn@44754
    68
    Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list option
bulwahn@38149
    69
  val quickcheck: (string * string list) list -> int -> Proof.state -> (string * term) list option
bulwahn@42733
    70
  (* testing a batch of terms *)
bulwahn@43059
    71
  val test_terms:
bulwahn@43059
    72
    Proof.context -> term list -> (string * term) list option list option * (string * int) list
bulwahn@43065
    73
  val validate_terms: Proof.context -> term list -> bool list option * (string * int) list
haftmann@28256
    74
end;
haftmann@28256
    75
haftmann@28256
    76
structure Quickcheck : QUICKCHECK =
haftmann@28256
    77
struct
haftmann@28256
    78
blanchet@43861
    79
val quickcheckN = "quickcheck"
blanchet@43861
    80
val quickcheck_paramsN = "quickcheck_params"
blanchet@43861
    81
blanchet@43861
    82
val genuineN = "genuine"
blanchet@43861
    83
val noneN = "none"
blanchet@43861
    84
val unknownN = "unknown"
blanchet@43861
    85
wenzelm@30981
    86
(* preferences *)
wenzelm@30981
    87
wenzelm@32740
    88
val auto = Unsynchronized.ref false;
wenzelm@30981
    89
wenzelm@30981
    90
val _ =
wenzelm@30981
    91
  ProofGeneralPgip.add_preference Preferences.category_tracing
wenzelm@39862
    92
  (Unsynchronized.setmp auto true (fn () =>
wenzelm@30981
    93
    Preferences.bool_pref auto
wenzelm@30981
    94
      "auto-quickcheck"
blanchet@39575
    95
      "Run Quickcheck automatically.") ());
wenzelm@30981
    96
bulwahn@35378
    97
(* quickcheck report *)
bulwahn@35378
    98
bulwahn@35378
    99
datatype report = Report of
bulwahn@35378
   100
  { iterations : int, raised_match_errors : int,
bulwahn@35378
   101
    satisfied_assms : int list, positive_concl_tests : int }
bulwahn@35378
   102
bulwahn@42952
   103
(* Quickcheck Result *)
bulwahn@42952
   104
bulwahn@42952
   105
datatype result = Result of
bulwahn@42952
   106
  { counterexample : (string * term) list option, evaluation_terms : (term * term) list option,
bulwahn@42952
   107
    timings : (string * int) list, reports : (int * report) list}
bulwahn@42952
   108
bulwahn@42952
   109
val empty_result =
bulwahn@42952
   110
  Result {counterexample = NONE, evaluation_terms = NONE, timings = [], reports = []}
bulwahn@42952
   111
bulwahn@42952
   112
fun counterexample_of (Result r) = #counterexample r
bulwahn@42952
   113
bulwahn@42952
   114
fun found_counterexample (Result r) = is_some (#counterexample r)
bulwahn@42952
   115
bulwahn@42952
   116
fun response_of (Result r) = case (#counterexample r, #evaluation_terms r) of
bulwahn@42952
   117
    (SOME ts, SOME evals) => SOME (ts, evals)
bulwahn@42952
   118
  | (NONE, NONE) => NONE
bulwahn@42952
   119
bulwahn@42952
   120
fun timings_of (Result r) = #timings r
bulwahn@42952
   121
bulwahn@42952
   122
fun set_reponse names eval_terms (SOME ts) (Result r) =
bulwahn@42952
   123
  let
bulwahn@42952
   124
    val (ts1, ts2) = chop (length names) ts
bulwahn@42952
   125
  in
bulwahn@42952
   126
    Result {counterexample = SOME (names ~~ ts1), evaluation_terms = SOME (eval_terms ~~ ts2),
bulwahn@42952
   127
      timings = #timings r, reports = #reports r}
bulwahn@42952
   128
  end
bulwahn@42952
   129
  | set_reponse _ _ NONE result = result
bulwahn@42952
   130
bulwahn@44456
   131
bulwahn@44456
   132
fun set_counterexample counterexample (Result r) =
bulwahn@44456
   133
  Result {counterexample = counterexample,  evaluation_terms = #evaluation_terms r,
bulwahn@44456
   134
    timings = #timings r, reports = #reports r}
bulwahn@44456
   135
bulwahn@44456
   136
fun set_evaluation_terms evaluation_terms (Result r) =
bulwahn@44456
   137
  Result {counterexample = #counterexample r,  evaluation_terms = evaluation_terms,
bulwahn@44456
   138
    timings = #timings r, reports = #reports r}
bulwahn@44456
   139
bulwahn@42952
   140
fun cons_timing timing (Result r) =
bulwahn@42952
   141
  Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r,
bulwahn@42952
   142
    timings = cons timing (#timings r), reports = #reports r}
bulwahn@42952
   143
bulwahn@42952
   144
fun cons_report size (SOME report) (Result r) =
bulwahn@42952
   145
  Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r,
bulwahn@42952
   146
    timings = #timings r, reports = cons (size, report) (#reports r)}
bulwahn@42952
   147
  | cons_report _ NONE result = result
bulwahn@42952
   148
wenzelm@43071
   149
fun add_timing timing result_ref =
wenzelm@43071
   150
  Unsynchronized.change result_ref (cons_timing timing)
bulwahn@42952
   151
wenzelm@43071
   152
fun add_report size report result_ref =
wenzelm@43071
   153
  Unsynchronized.change result_ref (cons_report size report)
bulwahn@42952
   154
bulwahn@42952
   155
fun add_response names eval_terms response result_ref =
wenzelm@43071
   156
  Unsynchronized.change result_ref (set_reponse names eval_terms response)
bulwahn@42952
   157
bulwahn@38169
   158
(* expectation *)
bulwahn@38169
   159
wenzelm@41765
   160
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
bulwahn@38169
   161
bulwahn@38169
   162
fun merge_expectation (expect1, expect2) =
bulwahn@38169
   163
  if expect1 = expect2 then expect1 else No_Expectation
bulwahn@38169
   164
haftmann@28315
   165
(* quickcheck configuration -- default parameters, test generators *)
bulwahn@44753
   166
val batch_tester = Attrib.setup_config_string @{binding quickcheck_batch_tester} (K "")
wenzelm@43487
   167
val size = Attrib.setup_config_int @{binding quickcheck_size} (K 10)
wenzelm@43487
   168
val iterations = Attrib.setup_config_int @{binding quickcheck_iterations} (K 100)
wenzelm@43487
   169
val no_assms = Attrib.setup_config_bool @{binding quickcheck_no_assms} (K false)
wenzelm@43487
   170
val report = Attrib.setup_config_bool @{binding quickcheck_report} (K true)
wenzelm@43487
   171
val timing = Attrib.setup_config_bool @{binding quickcheck_timing} (K false)
wenzelm@43487
   172
val quiet = Attrib.setup_config_bool @{binding quickcheck_quiet} (K false)
wenzelm@43487
   173
val timeout = Attrib.setup_config_real @{binding quickcheck_timeout} (K 30.0)
wenzelm@43487
   174
val finite_types = Attrib.setup_config_bool @{binding quickcheck_finite_types} (K true)
wenzelm@43487
   175
val finite_type_size = Attrib.setup_config_int @{binding quickcheck_finite_type_size} (K 3)
bulwahn@40894
   176
haftmann@28309
   177
datatype test_params = Test_Params of
bulwahn@40892
   178
  {default_type: typ list, expect : expectation};
haftmann@28309
   179
bulwahn@40892
   180
fun dest_test_params (Test_Params {default_type, expect}) = (default_type, expect);
wenzelm@39034
   181
wenzelm@41765
   182
fun make_test_params (default_type, expect) =
wenzelm@41765
   183
  Test_Params {default_type = default_type, expect = expect};
wenzelm@39034
   184
wenzelm@41765
   185
fun map_test_params' f (Test_Params {default_type, expect}) =
wenzelm@41765
   186
  make_test_params (f (default_type, expect));
wenzelm@39034
   187
wenzelm@39034
   188
fun merge_test_params
wenzelm@41720
   189
  (Test_Params {default_type = default_type1, expect = expect1},
wenzelm@41720
   190
    Test_Params {default_type = default_type2, expect = expect2}) =
wenzelm@41720
   191
  make_test_params
wenzelm@41720
   192
    (merge (op =) (default_type1, default_type2), merge_expectation (expect1, expect2));
haftmann@28309
   193
bulwahn@44749
   194
type tester =
bulwahn@44749
   195
  Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list
bulwahn@44749
   196
bulwahn@39479
   197
structure Data = Generic_Data
wenzelm@33522
   198
(
wenzelm@39034
   199
  type T =
bulwahn@44749
   200
    ((string * (bool Config.T * tester)) list
bulwahn@43065
   201
      * ((string * (Proof.context -> term list -> (int -> term list option) list)) list
bulwahn@43065
   202
      * ((string * (Proof.context -> term list -> (int -> bool) list)) list)))
wenzelm@39034
   203
      * test_params;
bulwahn@44748
   204
  val empty = (([], ([], [])), Test_Params {default_type = [], expect = No_Expectation});
haftmann@28256
   205
  val extend = I;
bulwahn@44748
   206
  fun merge (((testers1, (batch_generators1, batch_validators1)), params1),
bulwahn@44748
   207
    ((testers2, (batch_generators2, batch_validators2)), params2)) : T =
bulwahn@44748
   208
    ((AList.merge (op =) (K true) (testers1, testers2),
bulwahn@43953
   209
      (AList.merge (op =) (K true) (batch_generators1, batch_generators2),
bulwahn@43953
   210
       AList.merge (op =) (K true) (batch_validators1, batch_validators2))),
haftmann@28309
   211
      merge_test_params (params1, params2));
wenzelm@33522
   212
);
haftmann@28256
   213
bulwahn@39479
   214
val test_params_of = snd o Data.get o Context.Proof;
bulwahn@38150
   215
bulwahn@40892
   216
val default_type = fst o dest_test_params o test_params_of
bulwahn@40481
   217
bulwahn@40892
   218
val expect = snd o dest_test_params o test_params_of
bulwahn@40481
   219
bulwahn@40481
   220
val map_test_params = Data.map o apsnd o map_test_params'
bulwahn@39480
   221
bulwahn@44748
   222
(*val add_generator = Data.map o apfst o apfst o apfst o AList.update (op =);*)
bulwahn@43953
   223
bulwahn@44748
   224
val add_tester = Data.map o apfst o apfst o AList.update (op =);
bulwahn@42733
   225
bulwahn@43065
   226
val add_batch_generator = Data.map o apfst o apsnd o apfst o AList.update (op =);
bulwahn@43065
   227
bulwahn@43065
   228
val add_batch_validator = Data.map o apfst o apsnd o apsnd o AList.update (op =);
haftmann@28256
   229
bulwahn@44752
   230
fun active_testers ctxt =
bulwahn@44752
   231
  let
bulwahn@44752
   232
    val testers = (map snd o fst o fst o Data.get o Context.Proof) ctxt
bulwahn@44752
   233
  in
bulwahn@44752
   234
    map snd (filter (fn (active, _) => Config.get ctxt active) testers)
bulwahn@44752
   235
  end
bulwahn@44752
   236
  
bulwahn@44755
   237
fun set_active_testers [] gen_ctxt = gen_ctxt
bulwahn@44755
   238
  | set_active_testers testers gen_ctxt =
bulwahn@44752
   239
  let
bulwahn@44752
   240
    val registered_testers = (fst o fst o Data.get) gen_ctxt
bulwahn@44752
   241
  in
bulwahn@44752
   242
    fold (fn (name, (config, _)) => Config.put_generic config (member (op =) testers name))
bulwahn@44752
   243
      registered_testers gen_ctxt
bulwahn@44752
   244
  end
bulwahn@44752
   245
    
haftmann@28315
   246
(* generating tests *)
haftmann@28315
   247
bulwahn@42733
   248
fun gen_mk_tester lookup ctxt v =
haftmann@28309
   249
  let
bulwahn@44753
   250
    val name = Config.get ctxt batch_tester
bulwahn@42733
   251
    val tester = case lookup ctxt name
bulwahn@44753
   252
      of NONE => error ("No such quickcheck batch-tester: " ^ name)
bulwahn@41153
   253
      | SOME tester => tester ctxt;
wenzelm@40491
   254
  in
bulwahn@41153
   255
    if Config.get ctxt quiet then
bulwahn@42733
   256
      try tester v
bulwahn@41153
   257
    else
wenzelm@44633
   258
      let (* FIXME !?!? *)
bulwahn@42733
   259
        val tester = Exn.interruptible_capture tester v
wenzelm@44633
   260
      in case Exn.get_res tester of
bulwahn@41153
   261
          NONE => SOME (Exn.release tester)
bulwahn@41153
   262
        | SOME tester => SOME tester
bulwahn@41153
   263
      end
bulwahn@41153
   264
  end
bulwahn@44753
   265
bulwahn@43065
   266
val mk_batch_tester =
bulwahn@43065
   267
  gen_mk_tester (fn ctxt => AList.lookup (op =) ((fst o snd o fst o Data.get o Context.Proof) ctxt))
bulwahn@43065
   268
val mk_batch_validator =
bulwahn@43065
   269
  gen_mk_tester (fn ctxt => AList.lookup (op =) ((snd o snd o fst o Data.get o Context.Proof) ctxt))
bulwahn@43953
   270
  
bulwahn@44748
   271
fun lookup_tester ctxt = AList.lookup (op =) ((fst o fst o Data.get o Context.Proof) ctxt)
bulwahn@43953
   272
haftmann@28315
   273
(* testing propositions *)
haftmann@28315
   274
bulwahn@44747
   275
type compile_generator =
bulwahn@44747
   276
  Proof.context -> (term * term list) list -> int list -> term list option * report option
bulwahn@44747
   277
bulwahn@42899
   278
fun check_test_term t =
haftmann@28309
   279
  let
wenzelm@29266
   280
    val _ = (null (Term.add_tvars t []) andalso null (Term.add_tfrees t [])) orelse
haftmann@28309
   281
      error "Term to be tested contains type variables";
wenzelm@29266
   282
    val _ = null (Term.add_vars t []) orelse
haftmann@28309
   283
      error "Term to be tested contains schematic variables";
bulwahn@42899
   284
  in () end
haftmann@28309
   285
wenzelm@42885
   286
fun cpu_time description e =
wenzelm@42885
   287
  let val ({cpu, ...}, result) = Timing.timing e ()
wenzelm@42885
   288
  in (result, (description, Time.toMilliseconds cpu)) end
wenzelm@41765
   289
bulwahn@44456
   290
fun limit timeout (limit_time, is_interactive) f exc () =
bulwahn@42625
   291
  if limit_time then
bulwahn@44456
   292
      TimeLimit.timeLimit timeout f ()
bulwahn@42625
   293
    handle TimeLimit.TimeOut =>
bulwahn@42625
   294
      if is_interactive then exc () else raise TimeLimit.TimeOut
bulwahn@42625
   295
  else
bulwahn@42625
   296
    f ()
bulwahn@42624
   297
bulwahn@44747
   298
fun test_term compile ctxt (limit_time, is_interactive) (t, eval_terms) =
bulwahn@42733
   299
  let
bulwahn@42952
   300
    fun message s = if Config.get ctxt quiet then () else Output.urgent_message s
bulwahn@42899
   301
    val _ = check_test_term t
bulwahn@42899
   302
    val names = Term.add_free_names t []
bulwahn@40612
   303
    val current_size = Unsynchronized.ref 0
bulwahn@42952
   304
    val current_result = Unsynchronized.ref empty_result 
bulwahn@42951
   305
    fun excipit () =
bulwahn@42951
   306
      "Quickcheck ran out of time while testing at size " ^ string_of_int (!current_size)
bulwahn@42952
   307
    fun with_size test_fun k =
bulwahn@41155
   308
      if k > Config.get ctxt size then
bulwahn@42952
   309
        NONE
bulwahn@41155
   310
      else
bulwahn@35378
   311
        let
bulwahn@42952
   312
          val _ = message ("Test data size: " ^ string_of_int k)
bulwahn@40896
   313
          val _ = current_size := k
bulwahn@42952
   314
          val ((result, report), timing) =
bulwahn@43030
   315
            cpu_time ("size " ^ string_of_int k) (fn () => test_fun [1, k - 1])
bulwahn@42952
   316
          val _ = add_timing timing current_result
bulwahn@42952
   317
          val _ = add_report k report current_result
wenzelm@41765
   318
        in
bulwahn@42952
   319
          case result of NONE => with_size test_fun (k + 1) | SOME q => SOME q
wenzelm@41765
   320
        end;
bulwahn@34935
   321
  in
bulwahn@44786
   322
    (*limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive) (fn () =>*)(
bulwahn@43033
   323
      let
bulwahn@43033
   324
        val (test_fun, comp_time) = cpu_time "quickcheck compilation"
bulwahn@44747
   325
          (fn () => compile ctxt [(t, eval_terms)]);
bulwahn@43033
   326
        val _ = add_timing comp_time current_result
bulwahn@44747
   327
        val (response, exec_time) =
bulwahn@44747
   328
          cpu_time "quickcheck execution" (fn () => with_size test_fun 1)
bulwahn@44747
   329
        val _ = add_response names eval_terms response current_result
bulwahn@44747
   330
        val _ = add_timing exec_time current_result
bulwahn@44747
   331
      in !current_result end)
bulwahn@44786
   332
(*    (fn () => (message (excipit ()); !current_result)) ()*)
haftmann@28309
   333
  end;
haftmann@28309
   334
bulwahn@43065
   335
fun validate_terms ctxt ts =
bulwahn@43065
   336
  let
bulwahn@43065
   337
    val _ = map check_test_term ts
bulwahn@43065
   338
    val size = Config.get ctxt size
bulwahn@43065
   339
    val (test_funs, comp_time) = cpu_time "quickcheck batch compilation"
bulwahn@43065
   340
      (fn () => mk_batch_validator ctxt ts) 
bulwahn@43065
   341
    fun with_size tester k =
bulwahn@43065
   342
      if k > size then true
bulwahn@43065
   343
      else if tester k then with_size tester (k + 1) else false
bulwahn@43065
   344
    val (results, exec_time) = cpu_time "quickcheck batch execution" (fn () =>
bulwahn@43065
   345
        Option.map (map (fn test_fun => TimeLimit.timeLimit (seconds (Config.get ctxt timeout))
bulwahn@43065
   346
              (fn () => with_size test_fun 1) ()
bulwahn@43065
   347
             handle TimeLimit.TimeOut => true)) test_funs)
bulwahn@43065
   348
  in
bulwahn@43065
   349
    (results, [comp_time, exec_time])
bulwahn@43065
   350
  end
bulwahn@43065
   351
  
bulwahn@42733
   352
fun test_terms ctxt ts =
bulwahn@42733
   353
  let
bulwahn@42899
   354
    val _ = map check_test_term ts
bulwahn@43065
   355
    val size = Config.get ctxt size
bulwahn@42899
   356
    val namess = map (fn t => Term.add_free_names t []) ts
bulwahn@44752
   357
    val (test_funs, comp_time) =
bulwahn@44752
   358
      cpu_time "quickcheck batch compilation" (fn () => mk_batch_tester ctxt ts) 
bulwahn@42733
   359
    fun with_size tester k =
bulwahn@43065
   360
      if k > size then NONE
bulwahn@42733
   361
      else case tester k of SOME ts => SOME ts | NONE => with_size tester (k + 1)
bulwahn@43059
   362
    val (results, exec_time) = cpu_time "quickcheck batch execution" (fn () =>
bulwahn@43059
   363
        Option.map (map (fn test_fun => TimeLimit.timeLimit (seconds (Config.get ctxt timeout))
bulwahn@43065
   364
              (fn () => with_size test_fun 1) ()
bulwahn@43059
   365
             handle TimeLimit.TimeOut => NONE)) test_funs)
bulwahn@42733
   366
  in
bulwahn@43059
   367
    (Option.map (map2 (fn names => Option.map (fn ts => names ~~ ts)) namess) results,
bulwahn@43059
   368
      [comp_time, exec_time])
bulwahn@42733
   369
  end
bulwahn@42733
   370
bulwahn@41334
   371
(* FIXME: this function shows that many assumptions are made upon the generation *)
bulwahn@41334
   372
(* In the end there is probably no generic quickcheck interface left... *)
bulwahn@41334
   373
bulwahn@44747
   374
fun test_term_with_increasing_cardinality compile ctxt (limit_time, is_interactive) ts =
bulwahn@41291
   375
  let
wenzelm@43232
   376
    val thy = Proof_Context.theory_of ctxt
bulwahn@42952
   377
    fun message s = if Config.get ctxt quiet then () else Output.urgent_message s
bulwahn@43030
   378
    val (ts', eval_terms) = split_list ts
bulwahn@43030
   379
    val _ = map check_test_term ts'
bulwahn@43030
   380
    val names = Term.add_free_names (hd ts') []
bulwahn@43030
   381
    val Ts = map snd (Term.add_frees (hd ts') [])
bulwahn@43033
   382
    val current_result = Unsynchronized.ref empty_result
bulwahn@43033
   383
    fun test_card_size test_fun (card, size) =
bulwahn@41291
   384
      (* FIXME: why decrement size by one? *)
bulwahn@42952
   385
      let
bulwahn@44752
   386
        val (ts, timing) =
bulwahn@44752
   387
          cpu_time ("size " ^ string_of_int size ^ " and card " ^ string_of_int card)
bulwahn@44752
   388
            (fn () => fst (test_fun [card, size - 1]))
bulwahn@42952
   389
        val _ = add_timing timing current_result
bulwahn@42952
   390
      in
bulwahn@42952
   391
        Option.map (pair card) ts
bulwahn@42952
   392
      end
bulwahn@41291
   393
    val enumeration_card_size =
bulwahn@41334
   394
      if forall (fn T => Sign.of_sort thy (T,  ["Enum.enum"])) Ts then
bulwahn@41334
   395
        (* size does not matter *)
bulwahn@41334
   396
        map (rpair 0) (1 upto (length ts))
bulwahn@41334
   397
      else
bulwahn@41334
   398
        (* size does matter *)
bulwahn@41334
   399
        map_product pair (1 upto (length ts)) (1 upto (Config.get ctxt size))
bulwahn@41334
   400
        |> sort (fn ((c1, s1), (c2, s2)) => int_ord ((c1 + s1), (c2 + s2)))
bulwahn@43033
   401
  in
bulwahn@44786
   402
    (*limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive) (fn () =>*)(
bulwahn@43033
   403
      let
bulwahn@44747
   404
        val (test_fun, comp_time) = cpu_time "quickcheck compilation" (fn () => compile ctxt ts)
bulwahn@44747
   405
        val _ = add_timing comp_time current_result     
bulwahn@44747
   406
        val _ = case get_first (test_card_size test_fun) enumeration_card_size of
bulwahn@44747
   407
          SOME (card, ts) => add_response names (nth eval_terms (card - 1)) (SOME ts) current_result
bulwahn@44747
   408
        | NONE => ()
bulwahn@44747
   409
      in !current_result end)
bulwahn@44786
   410
      (*(fn () => (message "Quickcheck ran out of time"; !current_result)) ()*)
bulwahn@43033
   411
  end
bulwahn@41291
   412
bulwahn@40895
   413
fun get_finite_types ctxt =
bulwahn@40895
   414
  fst (chop (Config.get ctxt finite_type_size)
bulwahn@40895
   415
    (map (Type o rpair []) ["Enum.finite_1", "Enum.finite_2", "Enum.finite_3",
wenzelm@41765
   416
     "Enum.finite_4", "Enum.finite_5"]))
bulwahn@40895
   417
bulwahn@38153
   418
exception WELLSORTED of string
bulwahn@38153
   419
wenzelm@41765
   420
fun monomorphic_term thy insts default_T =
haftmann@28309
   421
  let
haftmann@28309
   422
    fun subst (T as TFree (v, S)) =
bulwahn@41147
   423
      let
bulwahn@41147
   424
        val T' = AList.lookup (op =) insts v
bulwahn@41147
   425
          |> the_default default_T
bulwahn@41147
   426
      in if Sign.of_sort thy (T', S) then T'
wenzelm@41765
   427
        else raise (WELLSORTED ("For instantiation with default_type " ^
wenzelm@41765
   428
          Syntax.string_of_typ_global thy default_T ^
bulwahn@41147
   429
          ":\n" ^ Syntax.string_of_typ_global thy T' ^
bulwahn@41147
   430
          " to be substituted for variable " ^
bulwahn@41147
   431
          Syntax.string_of_typ_global thy T ^ " does not have sort " ^
bulwahn@41147
   432
          Syntax.string_of_sort_global thy S))
bulwahn@41147
   433
      end
haftmann@28309
   434
      | subst T = T;
haftmann@28309
   435
  in (map_types o map_atyps) subst end;
haftmann@28309
   436
bulwahn@42897
   437
datatype wellsorted_error = Wellsorted_Error of string | Term of term * term list
bulwahn@38153
   438
bulwahn@43954
   439
fun instantiate_goals lthy insts goals =
haftmann@28309
   440
  let
bulwahn@42897
   441
    fun map_goal_and_eval_terms f (check_goal, eval_terms) = (f check_goal, map f eval_terms)
wenzelm@43232
   442
    val thy = Proof_Context.theory_of lthy
bulwahn@41170
   443
    val default_insts =
bulwahn@41170
   444
      if Config.get lthy finite_types then (get_finite_types lthy) else (default_type lthy)
bulwahn@40895
   445
    val inst_goals =
bulwahn@42897
   446
      map (fn (check_goal, eval_terms) =>
bulwahn@41170
   447
        if not (null (Term.add_tfree_names check_goal [])) then
bulwahn@41170
   448
          map (fn T =>
bulwahn@42897
   449
            (pair (SOME T) o Term o apfst (Object_Logic.atomize_term thy))
bulwahn@42897
   450
              (map_goal_and_eval_terms (monomorphic_term thy insts T) (check_goal, eval_terms))
bulwahn@41291
   451
              handle WELLSORTED s => (SOME T, Wellsorted_Error s)) default_insts
bulwahn@41170
   452
        else
bulwahn@43954
   453
          [(NONE, Term (Object_Logic.atomize_term thy check_goal, eval_terms))]) goals
wenzelm@41765
   454
    val error_msg =
wenzelm@41765
   455
      cat_lines
wenzelm@41765
   456
        (maps (map_filter (fn (_, Term t) => NONE | (_, Wellsorted_Error s) => SOME s)) inst_goals)
bulwahn@41291
   457
    fun is_wellsorted_term (T, Term t) = SOME (T, t)
bulwahn@41291
   458
      | is_wellsorted_term (_, Wellsorted_Error s) = NONE
bulwahn@38153
   459
    val correct_inst_goals =
bulwahn@41291
   460
      case map (map_filter is_wellsorted_term) inst_goals of
bulwahn@41291
   461
        [[]] => error error_msg
bulwahn@38153
   462
      | xs => xs
bulwahn@40892
   463
    val _ = if Config.get lthy quiet then () else warning error_msg
bulwahn@43954
   464
  in
bulwahn@43954
   465
    correct_inst_goals
bulwahn@43954
   466
  end
bulwahn@43954
   467
bulwahn@43954
   468
fun collect_results f [] results = results
bulwahn@43954
   469
  | collect_results f (t :: ts) results =
bulwahn@43954
   470
    let
bulwahn@43954
   471
      val result = f t
bulwahn@43954
   472
    in
bulwahn@43954
   473
      if found_counterexample result then
bulwahn@43954
   474
        (result :: results)
bulwahn@43954
   475
      else
bulwahn@43954
   476
        collect_results f ts (result :: results)
bulwahn@43954
   477
    end  
bulwahn@43954
   478
bulwahn@44747
   479
fun generator_test_goal_terms compile ctxt (limit_time, is_interactive) insts goals =
bulwahn@43954
   480
  let
bulwahn@41291
   481
    fun test_term' goal =
bulwahn@41291
   482
      case goal of
bulwahn@44747
   483
        [(NONE, t)] => test_term compile ctxt (limit_time, is_interactive) t
bulwahn@44747
   484
      | ts => test_term_with_increasing_cardinality compile ctxt (limit_time, is_interactive) (map snd ts)
bulwahn@43988
   485
    val correct_inst_goals = instantiate_goals ctxt insts goals
bulwahn@41291
   486
  in
bulwahn@44746
   487
    if Config.get ctxt finite_types then
bulwahn@44746
   488
      collect_results test_term' correct_inst_goals []
bulwahn@44746
   489
    else
bulwahn@44747
   490
      collect_results (test_term compile ctxt (limit_time, is_interactive))
bulwahn@44746
   491
        (maps (map snd) correct_inst_goals) []
bulwahn@41291
   492
  end;
bulwahn@44786
   493
bulwahn@44786
   494
fun message ctxt s = if Config.get ctxt quiet then () else Output.urgent_message s
bulwahn@44749
   495
  
bulwahn@44746
   496
fun test_goal_terms ctxt (limit_time, is_interactive) insts goals =
bulwahn@44749
   497
  case active_testers ctxt of
bulwahn@44752
   498
    [] => error "No active testers for quickcheck"
bulwahn@44786
   499
  | testers => limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive)
bulwahn@44786
   500
      (fn () => Par_List.get_some (fn tester =>
bulwahn@44750
   501
      tester ctxt (limit_time, is_interactive) insts goals |>
bulwahn@44786
   502
      (fn result => if exists found_counterexample result then SOME result else NONE)) testers)
bulwahn@44786
   503
      (fn () => (message ctxt "Quickcheck ran out of time"; NONE)) ();
bulwahn@44786
   504
      
bulwahn@42897
   505
fun test_goal (time_limit, is_interactive) (insts, eval_terms) i state =
bulwahn@40896
   506
  let
bulwahn@40896
   507
    val lthy = Proof.context_of state;
bulwahn@40896
   508
    val thy = Proof.theory_of state;
bulwahn@40896
   509
    fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
bulwahn@40896
   510
      | strip t = t;
bulwahn@40896
   511
    val {goal = st, ...} = Proof.raw_goal state;
bulwahn@40896
   512
    val (gi, frees) = Logic.goal_params (prop_of st) i;
bulwahn@40896
   513
    val some_locale = case (Option.map #target o Named_Target.peek) lthy
bulwahn@40896
   514
     of NONE => NONE
bulwahn@40896
   515
      | SOME "" => NONE
bulwahn@40896
   516
      | SOME locale => SOME locale;
bulwahn@40896
   517
    val assms = if Config.get lthy no_assms then [] else case some_locale
bulwahn@40896
   518
     of NONE => Assumption.all_assms_of lthy
bulwahn@40896
   519
      | SOME locale => Assumption.local_assms_of lthy (Locale.init locale thy);
bulwahn@40896
   520
    val proto_goal = Logic.list_implies (map Thm.term_of assms, subst_bounds (frees, strip gi));
bulwahn@43954
   521
    val goals = case some_locale
bulwahn@42897
   522
     of NONE => [(proto_goal, eval_terms)]
bulwahn@42897
   523
      | SOME locale =>
bulwahn@42897
   524
        map (fn (_, phi) => (Morphism.term phi proto_goal, map (Morphism.term phi) eval_terms))
bulwahn@42897
   525
          (Locale.registrations_of (Context.Theory thy) (*FIXME*) locale);
bulwahn@40896
   526
  in
bulwahn@43988
   527
    test_goal_terms lthy (time_limit, is_interactive) insts goals
bulwahn@40896
   528
  end
bulwahn@40896
   529
bulwahn@38152
   530
(* pretty printing *)
haftmann@28315
   531
blanchet@40466
   532
fun tool_name auto = (if auto then "Auto " else "") ^ "Quickcheck"
blanchet@40466
   533
blanchet@40466
   534
fun pretty_counterex ctxt auto NONE = Pretty.str (tool_name auto ^ " found no counterexample.")
bulwahn@42899
   535
  | pretty_counterex ctxt auto (SOME (cex, eval_terms)) =
bulwahn@42899
   536
      Pretty.chunks ((Pretty.str (tool_name auto ^ " found a counterexample:\n") ::
haftmann@28315
   537
        map (fn (s, t) =>
bulwahn@42899
   538
          Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) (rev cex))
blanchet@43861
   539
        @ (if null eval_terms then []
blanchet@43861
   540
           else (Pretty.str ("Evaluated terms:\n") ::
blanchet@43861
   541
             map (fn (t, u) =>
bulwahn@44752
   542
               Pretty.block [Syntax.pretty_term ctxt t, Pretty.str " =", Pretty.brk 1,
bulwahn@44752
   543
                 Syntax.pretty_term ctxt u]) (rev eval_terms))));
haftmann@28315
   544
bulwahn@35378
   545
fun pretty_report (Report {iterations = iterations, raised_match_errors = raised_match_errors,
bulwahn@35378
   546
    satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
bulwahn@35378
   547
  let
bulwahn@35378
   548
    fun pretty_stat s i = Pretty.block ([Pretty.str (s ^ ": " ^ string_of_int i)])
bulwahn@35378
   549
  in
bulwahn@35378
   550
     ([pretty_stat "iterations" iterations,
bulwahn@35378
   551
     pretty_stat "match exceptions" raised_match_errors]
wenzelm@41765
   552
     @ map_index
wenzelm@41765
   553
       (fn (i, n) => pretty_stat ("satisfied " ^ string_of_int (i + 1) ^ ". assumption") n)
bulwahn@35378
   554
       satisfied_assms
bulwahn@35378
   555
     @ [pretty_stat "positive conclusion tests" positive_concl_tests])
bulwahn@35378
   556
  end
bulwahn@35378
   557
bulwahn@35380
   558
fun pretty_reports ctxt (SOME reports) =
bulwahn@41160
   559
  Pretty.chunks (Pretty.fbrk :: Pretty.str "Quickcheck report:" ::
bulwahn@41153
   560
    maps (fn (size, report) =>
bulwahn@41153
   561
      Pretty.str ("size " ^ string_of_int size ^ ":") :: pretty_report report @ [Pretty.brk 1])
bulwahn@35378
   562
      (rev reports))
bulwahn@35380
   563
  | pretty_reports ctxt NONE = Pretty.str ""
bulwahn@35378
   564
bulwahn@42952
   565
fun pretty_timings timings =
bulwahn@42952
   566
  Pretty.chunks (Pretty.fbrk :: Pretty.str "timings:" ::
bulwahn@42952
   567
    maps (fn (label, time) =>
bulwahn@42952
   568
      Pretty.str (label ^ ": " ^ string_of_int time ^ " ms") :: Pretty.brk 1 :: []) (rev timings))
bulwahn@42952
   569
bulwahn@43299
   570
fun pretty_counterex_and_reports ctxt auto [] =
bulwahn@43299
   571
    Pretty.chunks [Pretty.strs (tool_name auto ::
bulwahn@43299
   572
        space_explode " " "is used in a locale where no interpretation is provided."),
bulwahn@43299
   573
      Pretty.strs (space_explode " " "Please provide an executable interpretation for the locale.")]
bulwahn@43299
   574
  | pretty_counterex_and_reports ctxt auto (result :: _) =
bulwahn@43299
   575
    Pretty.chunks (pretty_counterex ctxt auto (response_of result) ::
bulwahn@43299
   576
      (* map (pretty_reports ctxt) (reports_of result) :: *)
bulwahn@43299
   577
      (if Config.get ctxt timing then cons (pretty_timings (timings_of result)) else I) [])
haftmann@28315
   578
wenzelm@30981
   579
(* Isar commands *)
haftmann@28315
   580
haftmann@28336
   581
fun read_nat s = case (Library.read_int o Symbol.explode) s
haftmann@28336
   582
 of (k, []) => if k >= 0 then k
haftmann@28336
   583
      else error ("Not a natural number: " ^ s)
haftmann@28336
   584
  | (_, _ :: _) => error ("Not a natural number: " ^ s);
bulwahn@38149
   585
blanchet@34125
   586
fun read_bool "false" = false
blanchet@34125
   587
  | read_bool "true" = true
blanchet@34125
   588
  | read_bool s = error ("Not a Boolean value: " ^ s)
haftmann@28315
   589
bulwahn@40612
   590
fun read_real s =
bulwahn@40612
   591
  case (Real.fromString s) of
bulwahn@40612
   592
    SOME s => s
bulwahn@40612
   593
  | NONE => error ("Not a real number: " ^ s)
bulwahn@40612
   594
bulwahn@38169
   595
fun read_expectation "no_expectation" = No_Expectation
wenzelm@41765
   596
  | read_expectation "no_counterexample" = No_Counterexample
bulwahn@38169
   597
  | read_expectation "counterexample" = Counterexample
wenzelm@41765
   598
  | read_expectation s = error ("Not an expectation value: " ^ s)
bulwahn@38169
   599
bulwahn@44748
   600
fun valid_tester_name genctxt name = AList.defined (op =) (fst (fst (Data.get genctxt))) name
bulwahn@44748
   601
  
bulwahn@44752
   602
fun parse_tester name (testers, genctxt) =
bulwahn@41156
   603
  if valid_tester_name genctxt name then
bulwahn@44752
   604
    (insert (op =) name testers, genctxt)  
bulwahn@41156
   605
  else
bulwahn@41156
   606
    error ("Unknown tester: " ^ name)
bulwahn@41156
   607
bulwahn@44752
   608
fun parse_test_param ("tester", args) = fold parse_tester args
bulwahn@44752
   609
  | parse_test_param ("size", [arg]) = apsnd (Config.put_generic size (read_nat arg))
bulwahn@44752
   610
  | parse_test_param ("iterations", [arg]) = apsnd (Config.put_generic iterations (read_nat arg))
bulwahn@44752
   611
  | parse_test_param ("default_type", arg) = (fn (testers, gen_ctxt) =>
bulwahn@44752
   612
    (testers, map_test_params
bulwahn@44752
   613
      ((apfst o K) (map (Proof_Context.read_typ (Context.proof_of gen_ctxt)) arg)) gen_ctxt))
bulwahn@44752
   614
  | parse_test_param ("no_assms", [arg]) = apsnd (Config.put_generic no_assms (read_bool arg))
bulwahn@44752
   615
  | parse_test_param ("expect", [arg]) = apsnd (map_test_params ((apsnd o K) (read_expectation arg)))
bulwahn@44752
   616
  | parse_test_param ("report", [arg]) = apsnd (Config.put_generic report (read_bool arg))
bulwahn@44752
   617
  | parse_test_param ("quiet", [arg]) = apsnd (Config.put_generic quiet (read_bool arg))
bulwahn@44752
   618
  | parse_test_param ("timeout", [arg]) = apsnd (Config.put_generic timeout (read_real arg))
bulwahn@44752
   619
  | parse_test_param ("finite_types", [arg]) = apsnd (Config.put_generic finite_types (read_bool arg))
wenzelm@41765
   620
  | parse_test_param ("finite_type_size", [arg]) =
bulwahn@44752
   621
    apsnd (Config.put_generic finite_type_size (read_nat arg))
bulwahn@44752
   622
  | parse_test_param (name, _) = (fn (testers, genctxt) =>
bulwahn@41156
   623
    if valid_tester_name genctxt name then
bulwahn@44752
   624
      (insert (op =) name testers, genctxt)  
bulwahn@44752
   625
    else error ("Unknown tester or test parameter: " ^ name));
haftmann@28315
   626
bulwahn@44752
   627
fun proof_map_result f = apsnd Context.the_proof o f o Context.Proof;
bulwahn@44752
   628
bulwahn@44752
   629
fun parse_test_param_inst (name, arg) ((insts, eval_terms), (testers, ctxt)) =
wenzelm@43232
   630
      case try (Proof_Context.read_typ ctxt) name
bulwahn@42896
   631
       of SOME (TFree (v, _)) =>
bulwahn@44752
   632
         ((AList.update (op =) (v, Proof_Context.read_typ ctxt (the_single arg)) insts, eval_terms),
bulwahn@44752
   633
           (testers, ctxt))
bulwahn@42896
   634
        | NONE => (case name of
bulwahn@44752
   635
            "eval" => ((insts, eval_terms @ map (Syntax.read_term ctxt) arg), (testers, ctxt))
bulwahn@44752
   636
          | _ => ((insts, eval_terms),
bulwahn@44752
   637
            proof_map_result (fn gen_ctxt => parse_test_param (name, arg) (testers, gen_ctxt)) ctxt));
haftmann@28315
   638
bulwahn@44752
   639
fun quickcheck_params_cmd args = Context.theory_map
bulwahn@44752
   640
  (fn gen_ctxt => uncurry set_active_testers (fold parse_test_param args ([], gen_ctxt)));
wenzelm@41765
   641
bulwahn@42952
   642
fun check_expectation state results =
bulwahn@44750
   643
  (if is_some results andalso expect (Proof.context_of state) = No_Counterexample
bulwahn@42952
   644
  then
bulwahn@42952
   645
    error ("quickcheck expected to find no counterexample but found one")
bulwahn@42952
   646
  else
bulwahn@44750
   647
    (if is_none results andalso expect (Proof.context_of state) = Counterexample
bulwahn@42952
   648
    then
bulwahn@42952
   649
      error ("quickcheck expected to find a counterexample but did not find one")
bulwahn@42952
   650
    else ()))
bulwahn@42952
   651
bulwahn@35378
   652
fun gen_quickcheck args i state =
bulwahn@40892
   653
  state
bulwahn@44752
   654
  |> Proof.map_context_result (fn ctxt =>
bulwahn@44752
   655
    apsnd (fn (testers, ctxt) => Context.proof_map (set_active_testers testers) ctxt)
bulwahn@44752
   656
      (fold parse_test_param_inst args (([], []), ([], ctxt))))
bulwahn@42897
   657
  |> (fn ((insts, eval_terms), state') => test_goal (true, true) (insts, eval_terms) i state'
bulwahn@42952
   658
  |> tap (check_expectation state'))
boehmes@32295
   659
bulwahn@44750
   660
fun quickcheck args i state =
bulwahn@44750
   661
  Option.map (the o get_first counterexample_of) (gen_quickcheck args i state)
bulwahn@35378
   662
boehmes@32295
   663
fun quickcheck_cmd args i state =
bulwahn@35378
   664
  gen_quickcheck args i (Toplevel.proof_of state)
bulwahn@44750
   665
  |> Option.map (the o get_first response_of)
blanchet@43861
   666
  |> Output.urgent_message o Pretty.string_of
bulwahn@44750
   667
     o pretty_counterex (Toplevel.context_of state) false;
haftmann@28309
   668
wenzelm@41765
   669
val parse_arg =
wenzelm@41765
   670
  Parse.name --
wenzelm@41765
   671
    (Scan.optional (Parse.$$$ "=" |--
wenzelm@41765
   672
      (((Parse.name || Parse.float_number) >> single) ||
wenzelm@41765
   673
        (Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]"))) ["true"]);
haftmann@28309
   674
wenzelm@41765
   675
val parse_args =
wenzelm@41765
   676
  Parse.$$$ "[" |-- Parse.list1 parse_arg --| Parse.$$$ "]" || Scan.succeed [];
haftmann@28336
   677
wenzelm@36970
   678
val _ =
blanchet@43861
   679
  Outer_Syntax.command quickcheck_paramsN "set parameters for random testing" Keyword.thy_decl
wenzelm@36970
   680
    (parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
haftmann@28309
   681
wenzelm@36970
   682
val _ =
blanchet@43861
   683
  Outer_Syntax.improper_command quickcheckN "try to find counterexample for subgoal" Keyword.diag
wenzelm@36970
   684
    (parse_args -- Scan.optional Parse.nat 1
wenzelm@36970
   685
      >> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
haftmann@28309
   686
blanchet@43861
   687
(* automatic testing *)
blanchet@43861
   688
blanchet@43861
   689
fun try_quickcheck auto state =
blanchet@43861
   690
  let
blanchet@43861
   691
    val ctxt = Proof.context_of state;
blanchet@43861
   692
    val i = 1;
blanchet@43861
   693
    val res =
blanchet@43861
   694
      state
blanchet@43861
   695
      |> Proof.map_context (Config.put report false #> Config.put quiet true)
blanchet@43861
   696
      |> try (test_goal (false, false) ([], []) i);
blanchet@43861
   697
  in
blanchet@43861
   698
    case res of
blanchet@43861
   699
      NONE => (unknownN, state)
blanchet@43861
   700
    | SOME results =>
blanchet@43861
   701
        let
bulwahn@44750
   702
          val msg = pretty_counterex ctxt auto (Option.map (the o get_first response_of) results)
blanchet@43861
   703
        in
bulwahn@44750
   704
          if is_some results then
blanchet@43861
   705
            (genuineN,
blanchet@43870
   706
             state
blanchet@43870
   707
             |> (if auto then
blanchet@43870
   708
                   Proof.goal_message (K (Pretty.chunks [Pretty.str "",
blanchet@43870
   709
                       Pretty.mark Markup.hilite msg]))
blanchet@43870
   710
                 else
blanchet@43870
   711
                   tap (fn _ => Output.urgent_message (Pretty.string_of msg))))
blanchet@43861
   712
          else
blanchet@43861
   713
            (noneN, state)
blanchet@43861
   714
        end
blanchet@43861
   715
  end
blanchet@43861
   716
  |> `(fn (outcome_code, _) => outcome_code = genuineN)
blanchet@43861
   717
blanchet@43870
   718
val setup = Try.register_tool (quickcheckN, (20, auto, try_quickcheck))
blanchet@43861
   719
haftmann@28309
   720
end;
haftmann@28256
   721
haftmann@28315
   722
val auto_quickcheck = Quickcheck.auto;