src/HOL/Tools/Predicate_Compile/predicate_compile_quickcheck.ML
changeset 43886 bf068e758783
parent 42361 23f352990944
child 44241 7943b69f0188
equal deleted inserted replaced
43885:7caa1139b4e5 43886:bf068e758783
    19   val put_new_dseq_result : (unit -> int -> term list Lazy_Sequence.lazy_sequence) ->
    19   val put_new_dseq_result : (unit -> int -> term list Lazy_Sequence.lazy_sequence) ->
    20     Proof.context -> Proof.context
    20     Proof.context -> Proof.context
    21 
    21 
    22   val tracing : bool Unsynchronized.ref;
    22   val tracing : bool Unsynchronized.ref;
    23   val quiet : bool Unsynchronized.ref;
    23   val quiet : bool Unsynchronized.ref;
    24   val quickcheck_compile_term : Predicate_Compile_Aux.compilation -> bool -> bool -> int ->
    24   val test_goals : (Predicate_Compile_Aux.compilation * bool * bool * int) ->
    25     Proof.context -> (term * term list) list -> int list -> term list option * Quickcheck.report option;
    25     Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list
    26 (*  val test_term : Proof.context -> bool -> int -> int -> int -> int -> term -> *)
    26       -> Quickcheck.result list
    27   val nrandom : int Unsynchronized.ref;
    27   val nrandom : int Unsynchronized.ref;
    28   val debug : bool Unsynchronized.ref;
    28   val debug : bool Unsynchronized.ref;
    29   val function_flattening : bool Unsynchronized.ref;
    29   val function_flattening : bool Unsynchronized.ref;
    30   val no_higher_order_predicate : string list Unsynchronized.ref;
    30   val no_higher_order_predicate : string list Unsynchronized.ref;
       
    31   val setup : theory -> theory
    31 end;
    32 end;
    32 
    33 
    33 structure Predicate_Compile_Quickcheck : PREDICATE_COMPILE_QUICKCHECK =
    34 structure Predicate_Compile_Quickcheck : PREDICATE_COMPILE_QUICKCHECK =
    34 struct
    35 struct
    35 
    36 
   213 
   214 
   214 fun cpu_time description e =
   215 fun cpu_time description e =
   215   let val ({cpu, ...}, result) = Timing.timing e ()
   216   let val ({cpu, ...}, result) = Timing.timing e ()
   216   in (result, (description, Time.toMilliseconds cpu)) end
   217   in (result, (description, Time.toMilliseconds cpu)) end
   217 
   218 
   218 fun compile_term compilation options ctxt [(t, eval_terms)] =
   219 fun compile_term compilation options ctxt t =
   219   let
   220   let
   220     val t' = list_abs_free (Term.add_frees t [], t)
   221     val t' = list_abs_free (Term.add_frees t [], t)
   221     val thy = Theory.copy (Proof_Context.theory_of ctxt)
   222     val thy = Theory.copy (Proof_Context.theory_of ctxt)
   222     val ((((full_constname, constT), vs'), intro), thy1) =
   223     val ((((full_constname, constT), vs'), intro), thy1) =
   223       Predicate_Compile_Aux.define_quickcheck_predicate t' thy
   224       Predicate_Compile_Aux.define_quickcheck_predicate t' thy
   347     try' 0
   348     try' 0
   348   end
   349   end
   349 
   350 
   350 (* quickcheck interface functions *)
   351 (* quickcheck interface functions *)
   351 
   352 
   352 fun compile_term' compilation options depth ctxt t =
   353 fun compile_term' compilation options depth ctxt (t, eval_terms) =
   353   let
   354   let
       
   355     val size = Config.get ctxt Quickcheck.size
   354     val c = compile_term compilation options ctxt t
   356     val c = compile_term compilation options ctxt t
   355   in
   357     val counterexample = try_upto (!quiet) (c size (!nrandom)) depth
   356     fn [card, size] => (try_upto (!quiet) (c size (!nrandom)) depth, NONE)
   358   in
       
   359     Quickcheck.Result
       
   360       {counterexample = Option.map ((curry (op ~~)) (Term.add_free_names t [])) counterexample,
       
   361        evaluation_terms = Option.map (K []) counterexample, timings = [], reports = []}
   357   end
   362   end
   358 
   363 
   359 fun quickcheck_compile_term compilation function_flattening fail_safe_function_flattening depth =
   364 fun quickcheck_compile_term compilation function_flattening fail_safe_function_flattening depth =
   360   let
   365   let
   361      val options =
   366      val options =
   363          (set_function_flattening function_flattening (get_options ()))
   368          (set_function_flattening function_flattening (get_options ()))
   364   in
   369   in
   365     compile_term' compilation options depth
   370     compile_term' compilation options depth
   366   end
   371   end
   367 
   372 
       
   373 
       
   374 fun test_goals options ctxt (limit_time, is_interactive) insts goals =
       
   375   let
       
   376     val (compilation, function_flattening, fail_safe_function_flattening, depth) = options
       
   377     val correct_inst_goals = Quickcheck.instantiate_goals ctxt insts goals
       
   378     val test_term =
       
   379       quickcheck_compile_term compilation function_flattening fail_safe_function_flattening depth
       
   380   in
       
   381     Quickcheck.collect_results (test_term ctxt)
       
   382       (maps (map snd) correct_inst_goals) []
       
   383   end
       
   384   
       
   385 val wo_ff_active = Attrib.setup_config_bool @{binding quickcheck_pc_wo_ff_active} (K false);
       
   386 val ff_active = Attrib.setup_config_bool @{binding quickcheck_pc_ff_active} (K false);
       
   387 val ff_nofs_active = Attrib.setup_config_bool @{binding quickcheck_pc_ff_nofs_active} (K false);
       
   388 
       
   389 val setup = 
       
   390   Context.theory_map (Quickcheck.add_tester ("predicate_compile_wo_ff",
       
   391     (wo_ff_active, test_goals (Predicate_Compile_Aux.New_Pos_Random_DSeq, false, true, 4))))
       
   392   #> Context.theory_map (Quickcheck.add_tester ("predicate_compile_ff_fs",
       
   393     (ff_active, test_goals (Predicate_Compile_Aux.New_Pos_Random_DSeq, true, true, 4))))
       
   394   #> Context.theory_map (Quickcheck.add_tester ("predicate_compile_ff_nofs",
       
   395     (wo_ff_active, test_goals (Predicate_Compile_Aux.New_Pos_Random_DSeq, true, true, 4))))
       
   396 
       
   397 
   368 end;
   398 end;