| author | wenzelm | 
| Wed, 07 Nov 2012 16:45:33 +0100 | |
| changeset 50074 | 0b02aaf7c7c5 | 
| parent 50046 | 0051dc4f301f | 
| child 50723 | 07ecb6716df2 | 
| permissions | -rw-r--r-- | 
| 41923 
f05fc0711bc7
renaming signatures and structures; correcting header
 bulwahn parents: 
41921diff
changeset | 1 | (* Title: HOL/Tools/Quickcheck/random_generators.ML | 
| 37744 | 2 | Author: Florian Haftmann, TU Muenchen | 
| 31260 | 3 | |
| 41923 
f05fc0711bc7
renaming signatures and structures; correcting header
 bulwahn parents: 
41921diff
changeset | 4 | Random generators for various types. | 
| 31260 | 5 | *) | 
| 6 | ||
| 41923 
f05fc0711bc7
renaming signatures and structures; correcting header
 bulwahn parents: 
41921diff
changeset | 7 | signature RANDOM_GENERATORS = | 
| 31260 | 8 | sig | 
| 9 | type seed = Random_Engine.seed | |
| 10 |   val random_fun: typ -> typ -> ('a -> 'a -> bool) -> ('a -> term)
 | |
| 11 |     -> (seed -> ('b * (unit -> term)) * seed) -> (seed -> seed * seed)
 | |
| 33243 | 12 |     -> seed -> (('a -> 'b) * (unit -> term)) * seed
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 13 | val compile_generator_expr: | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 14 | Proof.context -> (term * term list) list -> bool -> int list -> (bool * term list) option * Quickcheck.report option | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 15 | val put_counterexample: (unit -> int -> bool -> int -> seed -> (bool * term list) option * seed) | 
| 39388 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 16 | -> Proof.context -> Proof.context | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 17 | val put_counterexample_report: (unit -> int -> bool -> int -> seed -> ((bool * term list) option * (bool list * bool)) * seed) | 
| 39388 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 18 | -> Proof.context -> Proof.context | 
| 45940 
71970a26a269
quickcheck_generator command also creates random generators
 bulwahn parents: 
45923diff
changeset | 19 | val instantiate_random_datatype : Datatype_Aux.config -> Datatype_Aux.descr -> | 
| 
71970a26a269
quickcheck_generator command also creates random generators
 bulwahn parents: 
45923diff
changeset | 20 | (string * sort) list -> string list -> string -> string list * string list -> typ list * typ list -> theory -> theory | 
| 31260 | 21 | val setup: theory -> theory | 
| 22 | end; | |
| 23 | ||
| 41923 
f05fc0711bc7
renaming signatures and structures; correcting header
 bulwahn parents: 
41921diff
changeset | 24 | structure Random_Generators : RANDOM_GENERATORS = | 
| 31260 | 25 | struct | 
| 26 | ||
| 31950 | 27 | (** abstract syntax **) | 
| 31260 | 28 | |
| 31950 | 29 | fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
 | 
| 30 | val size = @{term "i::code_numeral"};
 | |
| 31984 | 31 | val size_pred = @{term "(i::code_numeral) - 1"};
 | 
| 31950 | 32 | val size' = @{term "j::code_numeral"};
 | 
| 33 | val seed = @{term "s::Random.seed"};
 | |
| 31260 | 34 | |
| 45723 
75691bcc2c0f
quickcheck-random compilation also indicates if the counterexample is potentially spurious or not
 bulwahn parents: 
45721diff
changeset | 35 | val resultT =  @{typ "(bool * term list) option"};
 | 
| 45721 
d1fb55c2ed65
quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
 bulwahn parents: 
45719diff
changeset | 36 | |
| 31260 | 37 | (** typ "'a => 'b" **) | 
| 38 | ||
| 39 | type seed = Random_Engine.seed; | |
| 40 | ||
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 41 | fun random_fun T1 T2 eq term_of random random_split seed = | 
| 31260 | 42 | let | 
| 43 |     val fun_upd = Const (@{const_name fun_upd},
 | |
| 44 | (T1 --> T2) --> T1 --> T2 --> T1 --> T2); | |
| 50046 | 45 | val ((_, t2), seed') = random seed; | 
| 32344 | 46 | val (seed'', seed''') = random_split seed'; | 
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 47 | |
| 32740 | 48 |     val state = Unsynchronized.ref (seed'', [], fn () => Abs ("x", T1, t2 ()));
 | 
| 31260 | 49 | fun random_fun' x = | 
| 50 | let | |
| 51 | val (seed, fun_map, f_t) = ! state; | |
| 52 | in case AList.lookup (uncurry eq) fun_map x | |
| 53 | of SOME y => y | |
| 54 | | NONE => let | |
| 55 | val t1 = term_of x; | |
| 56 | val ((y, t2), seed') = random seed; | |
| 57 | val fun_map' = (x, y) :: fun_map; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 58 | val f_t' = fn () => fun_upd $ f_t () $ t1 $ t2 (); | 
| 31260 | 59 | val _ = state := (seed', fun_map', f_t'); | 
| 60 | in y end | |
| 61 | end; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 62 | fun term_fun' () = #3 (! state) (); | 
| 32344 | 63 | in ((random_fun', term_fun'), seed''') end; | 
| 31260 | 64 | |
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 65 | |
| 31260 | 66 | (** datatypes **) | 
| 67 | ||
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 68 | (* definitional scheme for random instances on datatypes *) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 69 | |
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 70 | local | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 71 | |
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 72 | fun dest_ctyp_nth k cT = nth (Thm.dest_ctyp cT) k; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 73 | val eq = Thm.cprop_of @{thm random_aux_rec} |> Thm.dest_arg |> Thm.dest_arg |> Thm.dest_arg;
 | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 74 | val lhs = eq |> Thm.dest_arg1; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 75 | val pt_random_aux = lhs |> Thm.dest_fun; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 76 | val pt_rhs = eq |> Thm.dest_arg |> Thm.dest_fun; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 77 | val aT = pt_random_aux |> Thm.ctyp_of_term |> dest_ctyp_nth 1; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 78 | |
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 79 | val rew_thms = map mk_meta_eq [@{thm code_numeral_zero_minus_one},
 | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 80 |   @{thm Suc_code_numeral_minus_one}, @{thm select_weight_cons_zero}, @{thm beyond_zero}];
 | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 81 | val rew_ts = map (Logic.dest_equals o Thm.prop_of) rew_thms; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 82 | val rew_ss = HOL_ss addsimps rew_thms; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 83 | |
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 84 | in | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 85 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 86 | fun random_aux_primrec eq lthy = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 87 | let | 
| 42361 | 88 | val thy = Proof_Context.theory_of lthy; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 89 | val ((t_random_aux as Free (random_aux, T)) $ (t_k as Free (v, _)), proto_t_rhs) = | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 90 | (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 91 | val Type (_, [_, iT]) = T; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 92 | val icT = Thm.ctyp_of thy iT; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 93 | val cert = Thm.cterm_of thy; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 94 | val inst = Thm.instantiate_cterm ([(aT, icT)], []); | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 95 | fun subst_v t' = map_aterms (fn t as Free (w, _) => if v = w then t' else t | t => t); | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 96 | val t_rhs = lambda t_k proto_t_rhs; | 
| 31785 | 97 |     val eqs0 = [subst_v @{term "0::code_numeral"} eq,
 | 
| 46547 
d1dcb91a512e
use qualified constant names instead of suffixes (from Florian Haftmann)
 huffman parents: 
46331diff
changeset | 98 |       subst_v (@{const Code_Numeral.Suc} $ t_k) eq];
 | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 99 | val eqs1 = map (Pattern.rewrite_term thy rew_ts []) eqs0; | 
| 35166 | 100 | val ((_, (_, eqs2)), lthy') = Primrec.add_primrec_simple | 
| 33205 | 101 | [((Binding.conceal (Binding.name random_aux), T), NoSyn)] eqs1 lthy; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 102 | val cT_random_aux = inst pt_random_aux; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 103 | val cT_rhs = inst pt_rhs; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 104 |     val rule = @{thm random_aux_rec}
 | 
| 43333 
2bdec7f430d3
renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
 wenzelm parents: 
43329diff
changeset | 105 | |> Drule.instantiate_normalize ([(aT, icT)], | 
| 31785 | 106 | [(cT_random_aux, cert t_random_aux), (cT_rhs, cert t_rhs)]); | 
| 107 | val tac = ALLGOALS (rtac rule) | |
| 108 | THEN ALLGOALS (simp_tac rew_ss) | |
| 42361 | 109 | THEN (ALLGOALS (Proof_Context.fact_tac eqs2)) | 
| 32970 
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
 wenzelm parents: 
32740diff
changeset | 110 | val simp = Skip_Proof.prove lthy' [v] [] eq (K tac); | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 111 | in (simp, lthy') end; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 112 | |
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 113 | end; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 114 | |
| 31868 | 115 | fun random_aux_primrec_multi auxname [eq] lthy = | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 116 | lthy | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 117 | |> random_aux_primrec eq | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 118 | |>> (fn simp => [simp]) | 
| 31868 | 119 | | random_aux_primrec_multi auxname (eqs as _ :: _ :: _) lthy = | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 120 | let | 
| 42361 | 121 | val thy = Proof_Context.theory_of lthy; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 122 | val (lhss, rhss) = map_split (HOLogic.dest_eq o HOLogic.dest_Trueprop) eqs; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 123 | val (vs, (arg as Free (v, _)) :: _) = map_split (fn (t1 $ t2) => (t1, t2)) lhss; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 124 | val Ts = map fastype_of lhss; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 125 | val tupleT = foldr1 HOLogic.mk_prodT Ts; | 
| 31868 | 126 |         val aux_lhs = Free ("mutual_" ^ auxname, fastype_of arg --> tupleT) $ arg;
 | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 127 | val aux_eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 128 | (aux_lhs, foldr1 HOLogic.mk_prod rhss); | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 129 | fun mk_proj t [T] = [t] | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 130 | | mk_proj t (Ts as T :: (Ts' as _ :: _)) = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 131 |               Const (@{const_name fst}, foldr1 HOLogic.mk_prodT Ts --> T) $ t
 | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 132 |                 :: mk_proj (Const (@{const_name snd},
 | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 133 | foldr1 HOLogic.mk_prodT Ts --> foldr1 HOLogic.mk_prodT Ts') $ t) Ts'; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 134 | val projs = mk_proj (aux_lhs) Ts; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 135 | val proj_eqs = map2 (fn v => fn proj => (v, lambda arg proj)) vs projs; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 136 | val proj_defs = map2 (fn Free (name, _) => fn (_, rhs) => | 
| 33205 | 137 | ((Binding.conceal (Binding.name name), NoSyn), | 
| 33280 | 138 | (apfst Binding.conceal Attrib.empty_binding, rhs))) vs proj_eqs; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 139 | val aux_eq' = Pattern.rewrite_term thy proj_eqs [] aux_eq; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 140 | fun prove_eqs aux_simp proj_defs lthy = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 141 | let | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 142 | val proj_simps = map (snd o snd) proj_defs; | 
| 31645 | 143 |             fun tac { context = ctxt, prems = _ } =
 | 
| 31625 | 144 | ALLGOALS (simp_tac (HOL_ss addsimps proj_simps)) | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 145 | THEN ALLGOALS (EqSubst.eqsubst_tac ctxt [0] [aux_simp]) | 
| 37136 | 146 |               THEN ALLGOALS (simp_tac (HOL_ss addsimps [@{thm fst_conv}, @{thm snd_conv}]));
 | 
| 32970 
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
 wenzelm parents: 
32740diff
changeset | 147 | in (map (fn prop => Skip_Proof.prove lthy [v] [] prop tac) eqs, lthy) end; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 148 | in | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 149 | lthy | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 150 | |> random_aux_primrec aux_eq' | 
| 33766 
c679f05600cd
adapted Local_Theory.define -- eliminated odd thm kind;
 wenzelm parents: 
33671diff
changeset | 151 | ||>> fold_map Local_Theory.define proj_defs | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 152 | |-> (fn (aux_simp, proj_defs) => prove_eqs aux_simp proj_defs) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 153 | end; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 154 | |
| 31868 | 155 | fun random_aux_specification prfx name eqs lthy = | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 156 | let | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 157 | val vs = fold Term.add_free_names ((snd o strip_comb o fst o HOLogic.dest_eq | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 158 | o HOLogic.dest_Trueprop o hd) eqs) []; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 159 | fun mk_proto_eq eq = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 160 | let | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 161 | val (head $ t $ u, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 162 | in ((HOLogic.mk_Trueprop o HOLogic.mk_eq) (head, lambda t (lambda u rhs))) end; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 163 | val proto_eqs = map mk_proto_eq eqs; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 164 | fun prove_simps proto_simps lthy = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 165 | let | 
| 31625 | 166 | val ext_simps = map (fn thm => fun_cong OF [fun_cong OF [thm]]) proto_simps; | 
| 42361 | 167 | val tac = ALLGOALS (Proof_Context.fact_tac ext_simps); | 
| 32970 
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
 wenzelm parents: 
32740diff
changeset | 168 | in (map (fn prop => Skip_Proof.prove lthy vs [] prop (K tac)) eqs, lthy) end; | 
| 33205 | 169 | val b = Binding.conceal (Binding.qualify true prfx | 
| 170 | (Binding.qualify true name (Binding.name "simps"))); | |
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 171 | in | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 172 | lthy | 
| 31868 | 173 | |> random_aux_primrec_multi (name ^ prfx) proto_eqs | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 174 | |-> (fn proto_simps => prove_simps proto_simps) | 
| 33671 | 175 | |-> (fn simps => Local_Theory.note | 
| 45592 | 176 |       ((b, Code.add_default_eqn_attrib :: @{attributes [simp, nitpick_simp]}), simps))
 | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 177 | |> snd | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 178 | end | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 179 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 180 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 181 | (* constructing random instances on datatypes *) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 182 | |
| 31868 | 183 | val random_auxN = "random_aux"; | 
| 184 | ||
| 50046 | 185 | fun mk_random_aux_eqs thy descr vs (names, auxnames) (Ts, Us) = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 186 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 187 | val mk_const = curry (Sign.mk_const thy); | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 188 | val random_auxsN = map (prefix (random_auxN ^ "_")) (names @ auxnames); | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 189 | val rTs = Ts @ Us; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 190 |     fun random_resultT T = @{typ Random.seed}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 191 |       --> HOLogic.mk_prodT (termifyT T,@{typ Random.seed});
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 192 |     fun sizeT T = @{typ code_numeral} --> @{typ code_numeral} --> T;
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 193 | val random_auxT = sizeT o random_resultT; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 194 | val random_auxs = map2 (fn s => fn rT => Free (s, random_auxT rT)) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 195 | random_auxsN rTs; | 
| 31950 | 196 | fun mk_random_call T = (NONE, (HOLogic.mk_random T size', T)); | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 197 | fun mk_random_aux_call fTs (k, _) (tyco, Ts) = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 198 | let | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 199 | val T = Type (tyco, Ts); | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 200 | fun mk_random_fun_lift [] t = t | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 201 | | mk_random_fun_lift (fT :: fTs) t = | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 202 |               mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
 | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 203 | mk_random_fun_lift fTs t; | 
| 31984 | 204 | val t = mk_random_fun_lift fTs (nth random_auxs k $ size_pred $ size'); | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33766diff
changeset | 205 | val size = Option.map snd (Datatype_Aux.find_shortest_path descr k) | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 206 | |> the_default 0; | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 207 | in (SOME size, (t, fTs ---> T)) end; | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33766diff
changeset | 208 | val tss = Datatype_Aux.interpret_construction descr vs | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 209 |       { atyp = mk_random_call, dtyp = mk_random_aux_call };
 | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 210 | fun mk_consexpr simpleT (c, xs) = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 211 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 212 | val (ks, simple_tTs) = split_list xs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 213 | val T = termifyT simpleT; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 214 | val tTs = (map o apsnd) termifyT simple_tTs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 215 | val is_rec = exists is_some ks; | 
| 33029 | 216 | val k = fold (fn NONE => I | SOME k => Integer.max k) ks 0; | 
| 43329 
84472e198515
tuned signature: Name.invent and Name.invent_names;
 wenzelm parents: 
42361diff
changeset | 217 | val vs = Name.invent_names Name.context "x" (map snd simple_tTs); | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 218 |         val tc = HOLogic.mk_return T @{typ Random.seed}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 219 | (HOLogic.mk_valtermify_app c vs simpleT); | 
| 38543 | 220 | val t = HOLogic.mk_ST | 
| 221 |           (map2 (fn (t, _) => fn (v, T') => ((t, @{typ Random.seed}), SOME ((v, termifyT T')))) tTs vs)
 | |
| 222 |             tc @{typ Random.seed} (SOME T, @{typ Random.seed});
 | |
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 223 | val tk = if is_rec | 
| 31950 | 224 | then if k = 0 then size | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 225 |             else @{term "Quickcheck.beyond :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
 | 
| 31950 | 226 |              $ HOLogic.mk_number @{typ code_numeral} k $ size
 | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 227 |           else @{term "1::code_numeral"}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 228 | in (is_rec, HOLogic.mk_prod (tk, t)) end; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 229 | fun sort_rec xs = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 230 | map_filter (fn (true, t) => SOME t | _ => NONE) xs | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 231 | @ map_filter (fn (false, t) => SOME t | _ => NONE) xs; | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 232 | val gen_exprss = tss | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 233 | |> (map o apfst) Type | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 234 | |> map (fn (T, cs) => (T, (sort_rec o map (mk_consexpr T)) cs)); | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 235 | fun mk_select (rT, xs) = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 236 |       mk_const @{const_name Quickcheck.collapse} [@{typ "Random.seed"}, termifyT rT]
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 237 |       $ (mk_const @{const_name Random.select_weight} [random_resultT rT]
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 238 |         $ HOLogic.mk_list (HOLogic.mk_prodT (@{typ code_numeral}, random_resultT rT)) xs)
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 239 | $ seed; | 
| 31950 | 240 | val auxs_lhss = map (fn t => t $ size $ size' $ seed) random_auxs; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 241 | val auxs_rhss = map mk_select gen_exprss; | 
| 31868 | 242 | in (random_auxs, auxs_lhss ~~ auxs_rhss) end; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 243 | |
| 38543 | 244 | fun instantiate_random_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 245 | let | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33766diff
changeset | 246 | val _ = Datatype_Aux.message config "Creating quickcheck generators ..."; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 247 | val mk_prop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq; | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33766diff
changeset | 248 | fun mk_size_arg k = case Datatype_Aux.find_shortest_path descr k | 
| 31950 | 249 | of SOME (_, l) => if l = 0 then size | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 250 |           else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
 | 
| 31950 | 251 |             $ HOLogic.mk_number @{typ code_numeral} l $ size
 | 
| 252 | | NONE => size; | |
| 31868 | 253 | val (random_auxs, auxs_eqs) = (apsnd o map) mk_prop_eq | 
| 50046 | 254 | (mk_random_aux_eqs thy descr vs (names, auxnames) (Ts, Us)); | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 255 | val random_defs = map_index (fn (k, T) => mk_prop_eq | 
| 31950 | 256 | (HOLogic.mk_random T size, nth random_auxs k $ mk_size_arg k $ size)) Ts; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 257 | in | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 258 | thy | 
| 38348 
cf7b2121ad9d
moved instantiation target formally to class_target.ML
 haftmann parents: 
37744diff
changeset | 259 |     |> Class.instantiation (tycos, vs, @{sort random})
 | 
| 31868 | 260 | |> random_aux_specification prfx random_auxN auxs_eqs | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 261 | |> `(fn lthy => map (Syntax.check_term lthy) random_defs) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 262 | |-> (fn random_defs' => fold_map (fn random_def => | 
| 33280 | 263 | Specification.definition (NONE, (apfst Binding.conceal | 
| 33205 | 264 | Attrib.empty_binding, random_def))) random_defs') | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 265 | |> snd | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 266 | |> Class.prove_instantiation_exit (K (Class.intro_classes_tac [])) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 267 | end; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 268 | |
| 31950 | 269 | (** building and compiling generator expressions **) | 
| 270 | ||
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 271 | (* FIXME just one data slot (record) per program unit *) | 
| 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 272 | |
| 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 273 | structure Counterexample = Proof_Data | 
| 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 274 | ( | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 275 | type T = unit -> int -> bool -> int -> int * int -> (bool * term list) option * (int * int) | 
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 276 | (* FIXME avoid user error with non-user text *) | 
| 39388 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 277 | fun init _ () = error "Counterexample" | 
| 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 278 | ); | 
| 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 279 | val put_counterexample = Counterexample.put; | 
| 31950 | 280 | |
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 281 | structure Counterexample_Report = Proof_Data | 
| 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 282 | ( | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 283 | type T = unit -> int -> bool -> int -> seed -> ((bool * term list) option * (bool list * bool)) * seed | 
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
40911diff
changeset | 284 | (* FIXME avoid user error with non-user text *) | 
| 39388 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 285 | fun init _ () = error "Counterexample_Report" | 
| 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 286 | ); | 
| 
fdbb2c55ffc2
replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
 haftmann parents: 
39253diff
changeset | 287 | val put_counterexample_report = Counterexample_Report.put; | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 288 | |
| 31950 | 289 | val target = "Quickcheck"; | 
| 290 | ||
| 50046 | 291 | fun mk_generator_expr ctxt (t, _) = | 
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 292 | let | 
| 42361 | 293 | val thy = Proof_Context.theory_of ctxt | 
| 44241 | 294 | val prop = fold_rev absfree (Term.add_frees t []) t | 
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 295 | val Ts = (map snd o fst o strip_abs) prop | 
| 31950 | 296 | val bound_max = length Ts - 1; | 
| 297 | val bounds = map_index (fn (i, ty) => | |
| 298 | (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts; | |
| 299 | val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds); | |
| 300 |     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
 | |
| 50046 | 301 | val ([genuine_only_name], _) = Variable.variant_fixes ["genuine_only"] ctxt | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 302 |     val genuine_only = Free (genuine_only_name, @{typ bool})
 | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 303 |     val none_t = Const (@{const_name "None"}, resultT)
 | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 304 | val check = Quickcheck_Common.mk_safe_if genuine_only none_t (result, none_t, | 
| 45721 
d1fb55c2ed65
quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
 bulwahn parents: 
45719diff
changeset | 305 |       fn genuine => @{term "Some :: bool * term list => (bool * term list) option"} $
 | 
| 
d1fb55c2ed65
quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
 bulwahn parents: 
45719diff
changeset | 306 | HOLogic.mk_prod (Quickcheck_Common.reflect_bool genuine, terms)) | 
| 45723 
75691bcc2c0f
quickcheck-random compilation also indicates if the counterexample is potentially spurious or not
 bulwahn parents: 
45721diff
changeset | 307 |     val return = HOLogic.pair_const resultT @{typ Random.seed};
 | 
| 31950 | 308 | fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT); | 
| 309 |     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
 | |
| 310 |     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
 | |
| 311 | liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g; | |
| 312 | fun mk_split T = Sign.mk_const thy | |
| 45723 
75691bcc2c0f
quickcheck-random compilation also indicates if the counterexample is potentially spurious or not
 bulwahn parents: 
45721diff
changeset | 313 |       (@{const_name prod_case}, [T, @{typ "unit => term"}, liftT resultT @{typ Random.seed}]);
 | 
| 31950 | 314 | fun mk_scomp_split T t t' = | 
| 45723 
75691bcc2c0f
quickcheck-random compilation also indicates if the counterexample is potentially spurious or not
 bulwahn parents: 
45721diff
changeset | 315 |       mk_scomp (mk_termtyp T) resultT @{typ Random.seed} t
 | 
| 31950 | 316 |         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
 | 
| 317 | fun mk_bindclause (_, _, i, T) = mk_scomp_split T | |
| 318 |       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
 | |
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 319 | in | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 320 | lambda genuine_only | 
| 45761 
90028fd2f1fa
exhaustive returns if a counterexample is genuine or potentially spurious in the presence of assumptions more correctly
 bulwahn parents: 
45759diff
changeset | 321 |       (Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check true)))
 | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 322 | end; | 
| 31950 | 323 | |
| 50046 | 324 | fun mk_reporting_generator_expr ctxt (t, _) = | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 325 | let | 
| 42361 | 326 | val thy = Proof_Context.theory_of ctxt | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 327 |     val resultT = @{typ "(bool * term list) option * (bool list * bool)"}
 | 
| 44241 | 328 | val prop = fold_rev absfree (Term.add_frees t []) t | 
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 329 | val Ts = (map snd o fst o strip_abs) prop | 
| 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 330 | val bound_max = length Ts - 1 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 331 | val bounds = map_index (fn (i, ty) => | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 332 | (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts; | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 333 | val prop' = betapplys (prop, map (fn (i, _, _, _) => Bound i) bounds); | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 334 |     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds)
 | 
| 42195 
1e7b62c93f5d
adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
 bulwahn parents: 
42159diff
changeset | 335 | val (assms, concl) = Quickcheck_Common.strip_imp prop' | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 336 |     val return = HOLogic.pair_const resultT @{typ "Random.seed"};
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 337 | fun mk_assms_report i = | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 338 |       HOLogic.mk_prod (@{term "None :: (bool * term list) option"},
 | 
| 38553 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 339 | HOLogic.mk_prod (HOLogic.mk_list HOLogic.boolT | 
| 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 340 |           (replicate i @{term True} @ replicate (length assms - i) @{term False}),
 | 
| 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 341 |         @{term False}))
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 342 | fun mk_concl_report b = | 
| 38553 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 343 |       HOLogic.mk_prod (HOLogic.mk_list HOLogic.boolT (replicate (length assms) @{term True}),
 | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 344 | Quickcheck_Common.reflect_bool b) | 
| 50046 | 345 | val ([genuine_only_name], _) = Variable.variant_fixes ["genuine_only"] ctxt | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 346 |     val genuine_only = Free (genuine_only_name, @{typ bool})
 | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 347 |     val none_t = HOLogic.mk_prod (@{term "None :: (bool * term list) option"}, mk_concl_report true)
 | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 348 | val concl_check = Quickcheck_Common.mk_safe_if genuine_only none_t (concl, none_t, | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 349 |       fn genuine => HOLogic.mk_prod (@{term "Some :: bool * term list => (bool * term list) option"} $
 | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 350 | HOLogic.mk_prod (Quickcheck_Common.reflect_bool genuine, terms), mk_concl_report false)) | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 351 | val check = fold_rev (fn (i, assm) => fn t => Quickcheck_Common.mk_safe_if genuine_only | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 352 | (mk_assms_report i) (HOLogic.mk_not assm, mk_assms_report i, t)) | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 353 | (map_index I assms) concl_check | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 354 | fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT); | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 355 |     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 356 |     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 357 | liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g; | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 358 | fun mk_split T = Sign.mk_const thy | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 359 |       (@{const_name prod_case}, [T, @{typ "unit => term"}, liftT resultT @{typ Random.seed}]);
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 360 | fun mk_scomp_split T t t' = | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 361 |       mk_scomp (mk_termtyp T) resultT @{typ Random.seed} t
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 362 |         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 363 | fun mk_bindclause (_, _, i, T) = mk_scomp_split T | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 364 |       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 365 | in | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 366 | lambda genuine_only | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 367 |       (Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check true)))
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 368 | end | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 369 | |
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 370 | val mk_parametric_generator_expr = Quickcheck_Common.gen_mk_parametric_generator_expr | 
| 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 371 | ((mk_generator_expr, | 
| 45759 | 372 |     absdummy @{typ bool} (absdummy @{typ code_numeral}
 | 
| 373 |       @{term "Pair None :: Random.seed => (bool * term list) option * Random.seed"})),
 | |
| 374 |     @{typ "bool => code_numeral => Random.seed => (bool * term list) option * Random.seed"})
 | |
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 375 | |
| 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 376 | val mk_parametric_reporting_generator_expr = Quickcheck_Common.gen_mk_parametric_generator_expr | 
| 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 377 | ((mk_reporting_generator_expr, | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 378 |     absdummy @{typ bool} (absdummy @{typ code_numeral}
 | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 379 |       @{term "Pair (None, ([], False)) :: Random.seed =>
 | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 380 | ((bool * term list) option * (bool list * bool)) * Random.seed"})), | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 381 |     @{typ "bool => code_numeral => Random.seed => ((bool * term list) option * (bool list * bool)) * Random.seed"})
 | 
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 382 | |
| 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 383 | |
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 384 | (* single quickcheck report *) | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 385 | |
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 386 | datatype single_report = Run of bool list * bool | MatchExc | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 387 | |
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 388 | fun collect_single_report single_report | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 389 |     (Quickcheck.Report {iterations = iterations, raised_match_errors = raised_match_errors,
 | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 390 | satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) = | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 391 | case single_report | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 392 | of MatchExc => | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 393 |     Quickcheck.Report {iterations = iterations + 1, raised_match_errors = raised_match_errors + 1,
 | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 394 | satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests} | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 395 | | Run (assms, concl) => | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 396 |     Quickcheck.Report {iterations = iterations + 1, raised_match_errors = raised_match_errors,
 | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 397 | satisfied_assms = | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 398 | map2 (fn b => fn s => if b then s + 1 else s) assms | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 399 | (if null satisfied_assms then replicate (length assms) 0 else satisfied_assms), | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 400 | positive_concl_tests = if concl then positive_concl_tests + 1 else positive_concl_tests} | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 401 | |
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 402 | val empty_report = Quickcheck.Report { iterations = 0, raised_match_errors = 0,
 | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 403 | satisfied_assms = [], positive_concl_tests = 0 } | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 404 | |
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 405 | fun compile_generator_expr ctxt ts = | 
| 31950 | 406 | let | 
| 42361 | 407 | val thy = Proof_Context.theory_of ctxt | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 408 | val iterations = Config.get ctxt Quickcheck.iterations | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 409 | in | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 410 | if Config.get ctxt Quickcheck.report then | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 411 | let | 
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 412 | val t' = mk_parametric_reporting_generator_expr ctxt ts; | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 413 | val compile = Code_Runtime.dynamic_value_strict | 
| 41923 
f05fc0711bc7
renaming signatures and structures; correcting header
 bulwahn parents: 
41921diff
changeset | 414 | (Counterexample_Report.get, put_counterexample_report, "Random_Generators.put_counterexample_report") | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 415 | thy (SOME target) | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 416 | (fn proc => fn g => fn c => fn b => fn s => g c b s | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 417 | #>> (apfst o Option.map o apsnd o map) proc) t' []; | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 418 | fun single_tester c b s = compile c b s |> Random_Engine.run | 
| 50046 | 419 | fun iterate_and_collect _ _ 0 report = (NONE, report) | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 420 | | iterate_and_collect genuine_only (card, size) j report = | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 421 | let | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 422 | val (test_result, single_report) = apsnd Run (single_tester card genuine_only size) | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 423 | val report = collect_single_report single_report report | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 424 | in | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 425 | case test_result of NONE => iterate_and_collect genuine_only (card, size) (j - 1) report | 
| 45762 
daf57640d4df
the reporting random testing also returns if the counterexample is genuine or potentially spurious
 bulwahn parents: 
45761diff
changeset | 426 | | SOME q => (SOME q, report) | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 427 | end | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 428 | in | 
| 45763 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 429 | fn genuine_only => fn [card, size] => | 
| 
3bb2bdf654f7
random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
 bulwahn parents: 
45762diff
changeset | 430 | apsnd SOME (iterate_and_collect genuine_only (card, size) iterations empty_report) | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 431 | end | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 432 | else | 
| 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 433 | let | 
| 42159 
234ec7011e5d
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
 bulwahn parents: 
42028diff
changeset | 434 | val t' = mk_parametric_generator_expr ctxt ts; | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 435 | val compile = Code_Runtime.dynamic_value_strict | 
| 41923 
f05fc0711bc7
renaming signatures and structures; correcting header
 bulwahn parents: 
41921diff
changeset | 436 | (Counterexample.get, put_counterexample, "Random_Generators.put_counterexample") | 
| 45723 
75691bcc2c0f
quickcheck-random compilation also indicates if the counterexample is potentially spurious or not
 bulwahn parents: 
45721diff
changeset | 437 | thy (SOME target) | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 438 | (fn proc => fn g => fn c => fn b => fn s => g c b s | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 439 | #>> (Option.map o apsnd o map) proc) t' []; | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 440 | fun single_tester c b s = compile c b s |> Random_Engine.run | 
| 50046 | 441 | fun iterate _ _ 0 = NONE | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 442 | | iterate genuine_only (card, size) j = | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 443 | case single_tester card genuine_only size of | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 444 | NONE => iterate genuine_only (card, size) (j - 1) | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 445 | | SOME q => SOME q | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 446 | in | 
| 45754 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 447 | fn genuine_only => fn [card, size] => | 
| 
394ecd91434a
dynamic genuine_flag in compilation of random and exhaustive
 bulwahn parents: 
45728diff
changeset | 448 | (rpair NONE (iterate genuine_only (card, size) iterations)) | 
| 40911 
7febf76e0a69
moving iteration of tests to the testers in quickcheck
 bulwahn parents: 
40644diff
changeset | 449 | end | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 450 | end; | 
| 31950 | 451 | |
| 46331 
f5598b604a54
generalizing check if size matters because it is different for random and exhaustive testing
 bulwahn parents: 
45940diff
changeset | 452 | fun size_matters_for _ Ts = not (forall | 
| 
f5598b604a54
generalizing check if size matters because it is different for random and exhaustive testing
 bulwahn parents: 
45940diff
changeset | 453 | (fn Type (tyco, []) => is_some (try (unprefix "Enum.finite") tyco) | _ => false) Ts) | 
| 
f5598b604a54
generalizing check if size matters because it is different for random and exhaustive testing
 bulwahn parents: 
45940diff
changeset | 454 | |
| 
f5598b604a54
generalizing check if size matters because it is different for random and exhaustive testing
 bulwahn parents: 
45940diff
changeset | 455 | val test_goals = | 
| 
f5598b604a54
generalizing check if size matters because it is different for random and exhaustive testing
 bulwahn parents: 
45940diff
changeset | 456 |   Quickcheck_Common.generator_test_goal_terms ("random", (size_matters_for, compile_generator_expr));
 | 
| 43875 
485d2ad43528
adding random, exhaustive and SML quickcheck as testers
 bulwahn parents: 
43333diff
changeset | 457 | |
| 31260 | 458 | (** setup **) | 
| 459 | ||
| 43878 
eeb10fdd9535
changed every tester to have a configuration in quickcheck; enabling parallel testing of different testers in quickcheck
 bulwahn parents: 
43877diff
changeset | 460 | val active = Attrib.setup_config_bool @{binding quickcheck_random_active} (K false);
 | 
| 
eeb10fdd9535
changed every tester to have a configuration in quickcheck; enabling parallel testing of different testers in quickcheck
 bulwahn parents: 
43877diff
changeset | 461 | |
| 38393 | 462 | val setup = | 
| 45923 
473b744c23f2
generalize ensure_sort_datatype to ensure_sort  in quickcheck_common to allow generators for abstract types;
 bulwahn parents: 
45763diff
changeset | 463 |   Quickcheck_Common.datatype_interpretation (@{sort random}, instantiate_random_datatype)
 | 
| 43878 
eeb10fdd9535
changed every tester to have a configuration in quickcheck; enabling parallel testing of different testers in quickcheck
 bulwahn parents: 
43877diff
changeset | 464 |   #> Context.theory_map (Quickcheck.add_tester ("random", (active, test_goals)));
 | 
| 31260 | 465 | |
| 466 | end; |