| author | blanchet | 
| Mon, 23 Aug 2010 09:04:37 +0200 | |
| changeset 38647 | 5500241da479 | 
| parent 38553 | 56965d8e4e11 | 
| child 38786 | e46e7a9cb622 | 
| permissions | -rw-r--r-- | 
| 37744 | 1 | (* Title: HOL/Tools/quickcheck_generators.ML | 
| 2 | Author: Florian Haftmann, TU Muenchen | |
| 31260 | 3 | |
| 4 | Quickcheck generators for various types. | |
| 5 | *) | |
| 6 | ||
| 7 | signature QUICKCHECK_GENERATORS = | |
| 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
 | 
| 38543 | 13 | val perhaps_constrain: theory -> (typ * sort) list -> (string * sort) list | 
| 14 | -> (string * sort -> string * sort) option | |
| 31737 | 15 | val ensure_random_datatype: Datatype.config -> string list -> theory -> theory | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 16 | val compile_generator_expr: | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 17 | theory -> bool -> term -> int -> term list option * (bool list * bool) | 
| 32740 | 18 | val eval_ref: (unit -> int -> seed -> term list option * seed) option Unsynchronized.ref | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 19 | val eval_report_ref: | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 20 | (unit -> int -> seed -> (term list option * (bool list * bool)) * seed) option Unsynchronized.ref | 
| 31260 | 21 | val setup: theory -> theory | 
| 22 | end; | |
| 23 | ||
| 24 | structure Quickcheck_Generators : QUICKCHECK_GENERATORS = | |
| 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 | |
| 35 | ||
| 36 | (** typ "'a => 'b" **) | |
| 37 | ||
| 38 | type seed = Random_Engine.seed; | |
| 39 | ||
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 40 | fun random_fun T1 T2 eq term_of random random_split seed = | 
| 31260 | 41 | let | 
| 42 |     val fun_upd = Const (@{const_name fun_upd},
 | |
| 43 | (T1 --> T2) --> T1 --> T2 --> T1 --> T2); | |
| 32344 | 44 | val ((y, t2), seed') = random seed; | 
| 45 | val (seed'', seed''') = random_split seed'; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 46 | |
| 32740 | 47 |     val state = Unsynchronized.ref (seed'', [], fn () => Abs ("x", T1, t2 ()));
 | 
| 31260 | 48 | fun random_fun' x = | 
| 49 | let | |
| 50 | val (seed, fun_map, f_t) = ! state; | |
| 51 | in case AList.lookup (uncurry eq) fun_map x | |
| 52 | of SOME y => y | |
| 53 | | NONE => let | |
| 54 | val t1 = term_of x; | |
| 55 | val ((y, t2), seed') = random seed; | |
| 56 | val fun_map' = (x, y) :: fun_map; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 57 | val f_t' = fn () => fun_upd $ f_t () $ t1 $ t2 (); | 
| 31260 | 58 | val _ = state := (seed', fun_map', f_t'); | 
| 59 | in y end | |
| 60 | end; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 61 | fun term_fun' () = #3 (! state) (); | 
| 32344 | 62 | in ((random_fun', term_fun'), seed''') end; | 
| 31260 | 63 | |
| 64 | ||
| 65 | (** datatypes **) | |
| 66 | ||
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 67 | (* definitional scheme for random instances on datatypes *) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 68 | |
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 69 | local | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 70 | |
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 71 | fun dest_ctyp_nth k cT = nth (Thm.dest_ctyp cT) k; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 72 | 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 | 73 | val lhs = eq |> Thm.dest_arg1; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 74 | val pt_random_aux = lhs |> Thm.dest_fun; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 75 | val ct_k = lhs |> Thm.dest_arg; | 
| 
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 | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 88 | val thy = ProofContext.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,
 | 
| 98 |       subst_v (@{term "Suc_code_numeral"} $ 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}
 | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 105 | |> Drule.instantiate ([(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) | |
| 35166 | 109 | THEN (ALLGOALS (ProofContext.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 | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 121 | val thy = ProofContext.theory_of lthy; | 
| 
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; | 
| 167 | val tac = ALLGOALS (ProofContext.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 | 
| 33666 
e49bfeb0d822
eliminated obsolete "generated" kind -- collapsed to unspecific "" (definitely unused according to Lukas Bulwahn);
 wenzelm parents: 
33553diff
changeset | 176 | ((b, Code.add_default_eqn_attrib :: map (Attrib.internal o K) | 
| 33552 | 177 | [Simplifier.simp_add, Nitpick_Simps.add]), simps)) | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 178 | |> snd | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 179 | end | 
| 
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 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 182 | (* constructing random instances on datatypes *) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 183 | |
| 31868 | 184 | val random_auxN = "random_aux"; | 
| 185 | ||
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 186 | fun mk_random_aux_eqs thy descr vs tycos (names, auxnames) (Ts, Us) = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 187 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 188 | val mk_const = curry (Sign.mk_const thy); | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 189 | val random_auxsN = map (prefix (random_auxN ^ "_")) (names @ auxnames); | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 190 | val rTs = Ts @ Us; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 191 |     fun random_resultT T = @{typ Random.seed}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 192 |       --> HOLogic.mk_prodT (termifyT T,@{typ Random.seed});
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 193 | val pTs = map random_resultT rTs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 194 |     fun sizeT T = @{typ code_numeral} --> @{typ code_numeral} --> T;
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 195 | val random_auxT = sizeT o random_resultT; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 196 | 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 | 197 | random_auxsN rTs; | 
| 31950 | 198 | fun mk_random_call T = (NONE, (HOLogic.mk_random T size', T)); | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 199 | fun mk_random_aux_call fTs (k, _) (tyco, Ts) = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 200 | let | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 201 | val T = Type (tyco, Ts); | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 202 | fun mk_random_fun_lift [] t = t | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 203 | | mk_random_fun_lift (fT :: fTs) t = | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 204 |               mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
 | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 205 | mk_random_fun_lift fTs t; | 
| 31984 | 206 | 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 | 207 | 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 | 208 | |> the_default 0; | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 209 | 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 | 210 | val tss = Datatype_Aux.interpret_construction descr vs | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 211 |       { atyp = mk_random_call, dtyp = mk_random_aux_call };
 | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 212 | fun mk_consexpr simpleT (c, xs) = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 213 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 214 | val (ks, simple_tTs) = split_list xs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 215 | val T = termifyT simpleT; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 216 | val tTs = (map o apsnd) termifyT simple_tTs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 217 | val is_rec = exists is_some ks; | 
| 33029 | 218 | val k = fold (fn NONE => I | SOME k => Integer.max k) ks 0; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 219 | val vs = Name.names Name.context "x" (map snd simple_tTs); | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 220 |         val tc = HOLogic.mk_return T @{typ Random.seed}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 221 | (HOLogic.mk_valtermify_app c vs simpleT); | 
| 38543 | 222 | val t = HOLogic.mk_ST | 
| 223 |           (map2 (fn (t, _) => fn (v, T') => ((t, @{typ Random.seed}), SOME ((v, termifyT T')))) tTs vs)
 | |
| 224 |             tc @{typ Random.seed} (SOME T, @{typ Random.seed});
 | |
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 225 | val tk = if is_rec | 
| 31950 | 226 | then if k = 0 then size | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 227 |             else @{term "Quickcheck.beyond :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
 | 
| 31950 | 228 |              $ HOLogic.mk_number @{typ code_numeral} k $ size
 | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 229 |           else @{term "1::code_numeral"}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 230 | in (is_rec, HOLogic.mk_prod (tk, t)) end; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 231 | fun sort_rec xs = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 232 | map_filter (fn (true, t) => SOME t | _ => NONE) xs | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 233 | @ map_filter (fn (false, t) => SOME t | _ => NONE) xs; | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 234 | val gen_exprss = tss | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 235 | |> (map o apfst) Type | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 236 | |> 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 | 237 | fun mk_select (rT, xs) = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 238 |       mk_const @{const_name Quickcheck.collapse} [@{typ "Random.seed"}, termifyT rT]
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 239 |       $ (mk_const @{const_name Random.select_weight} [random_resultT rT]
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 240 |         $ 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 | 241 | $ seed; | 
| 31950 | 242 | 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 | 243 | val auxs_rhss = map mk_select gen_exprss; | 
| 31868 | 244 | in (random_auxs, auxs_lhss ~~ auxs_rhss) end; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 245 | |
| 38543 | 246 | 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 | 247 | 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 | 248 | val _ = Datatype_Aux.message config "Creating quickcheck generators ..."; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 249 | 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 | 250 | fun mk_size_arg k = case Datatype_Aux.find_shortest_path descr k | 
| 31950 | 251 | of SOME (_, l) => if l = 0 then size | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 252 |           else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
 | 
| 31950 | 253 |             $ HOLogic.mk_number @{typ code_numeral} l $ size
 | 
| 254 | | NONE => size; | |
| 31868 | 255 | val (random_auxs, auxs_eqs) = (apsnd o map) mk_prop_eq | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 256 | (mk_random_aux_eqs thy descr vs tycos (names, auxnames) (Ts, Us)); | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 257 | val random_defs = map_index (fn (k, T) => mk_prop_eq | 
| 31950 | 258 | (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 | 259 | in | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 260 | thy | 
| 38348 
cf7b2121ad9d
moved instantiation target formally to class_target.ML
 haftmann parents: 
37744diff
changeset | 261 |     |> Class.instantiation (tycos, vs, @{sort random})
 | 
| 31868 | 262 | |> random_aux_specification prfx random_auxN auxs_eqs | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 263 | |> `(fn lthy => map (Syntax.check_term lthy) random_defs) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 264 | |-> (fn random_defs' => fold_map (fn random_def => | 
| 33280 | 265 | Specification.definition (NONE, (apfst Binding.conceal | 
| 33205 | 266 | Attrib.empty_binding, random_def))) random_defs') | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 267 | |> snd | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 268 | |> Class.prove_instantiation_exit (K (Class.intro_classes_tac [])) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 269 | end; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 270 | |
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 271 | fun perhaps_constrain thy insts raw_vs = | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 272 | let | 
| 38543 | 273 | fun meet (T, sort) = Sorts.meet_sort (Sign.classes_of thy) | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35378diff
changeset | 274 | (Logic.varifyT_global T, sort); | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 275 | val vtab = Vartab.empty | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 276 | |> fold (fn (v, sort) => Vartab.update ((v, 0), sort)) raw_vs | 
| 38543 | 277 | |> fold meet insts; | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 278 | in SOME (fn (v, _) => (v, (the o Vartab.lookup vtab) (v, 0))) | 
| 33087 
e50f948fd6bd
handle Sorts.CLASS_ERROR instead of arbitrary exceptions;
 wenzelm parents: 
33056diff
changeset | 279 | end handle Sorts.CLASS_ERROR _ => NONE; | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 280 | |
| 31668 
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
 haftmann parents: 
31641diff
changeset | 281 | fun ensure_random_datatype config raw_tycos thy = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 282 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 283 | val algebra = Sign.classes_of thy; | 
| 31868 | 284 | val (descr, raw_vs, tycos, prfx, (names, auxnames), raw_TUs) = | 
| 31784 | 285 | Datatype.the_descr thy raw_tycos; | 
| 32378 | 286 | val typerep_vs = (map o apsnd) | 
| 31744 | 287 |       (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs;
 | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 288 |     val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd)
 | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33766diff
changeset | 289 | (Datatype_Aux.interpret_construction descr typerep_vs | 
| 31744 | 290 |         { atyp = single, dtyp = (K o K o K) [] });
 | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 291 |     val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd)
 | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33766diff
changeset | 292 | (Datatype_Aux.interpret_construction descr typerep_vs | 
| 31744 | 293 |         { atyp = K [], dtyp = K o K });
 | 
| 31608 | 294 | val has_inst = exists (fn tyco => | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 295 |       can (Sorts.mg_domain algebra tyco) @{sort random}) tycos;
 | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 296 | in if has_inst then thy | 
| 32378 | 297 | else case perhaps_constrain thy (random_insts @ term_of_insts) typerep_vs | 
| 38543 | 298 | of SOME constrain => instantiate_random_datatype config descr | 
| 32378 | 299 | (map constrain typerep_vs) tycos prfx (names, auxnames) | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 300 | ((pairself o map o map_atyps) (fn TFree v => TFree (constrain v)) raw_TUs) thy | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 301 | | NONE => thy | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 302 | end; | 
| 31260 | 303 | |
| 304 | ||
| 31950 | 305 | (** building and compiling generator expressions **) | 
| 306 | ||
| 32740 | 307 | val eval_ref : | 
| 308 | (unit -> int -> int * int -> term list option * (int * int)) option Unsynchronized.ref = | |
| 309 | Unsynchronized.ref NONE; | |
| 31950 | 310 | |
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 311 | val eval_report_ref : | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 312 | (unit -> int -> seed -> (term list option * (bool list * bool)) * seed) option Unsynchronized.ref = | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 313 | Unsynchronized.ref NONE; | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 314 | |
| 31950 | 315 | val target = "Quickcheck"; | 
| 316 | ||
| 317 | fun mk_generator_expr thy prop Ts = | |
| 318 | let | |
| 319 | val bound_max = length Ts - 1; | |
| 320 | val bounds = map_index (fn (i, ty) => | |
| 321 | (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts; | |
| 322 | val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds); | |
| 323 |     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
 | |
| 324 |     val check = @{term "If :: bool => term list option => term list option => term list option"}
 | |
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 325 |       $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option"} $ terms);
 | 
| 31950 | 326 |     val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
 | 
| 327 | fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT); | |
| 328 |     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
 | |
| 329 |     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
 | |
| 330 | liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g; | |
| 331 | fun mk_split T = Sign.mk_const thy | |
| 37591 | 332 |       (@{const_name prod_case}, [T, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
 | 
| 31950 | 333 | fun mk_scomp_split T t t' = | 
| 334 |       mk_scomp (mk_termtyp T) @{typ "term list option"} @{typ Random.seed} t
 | |
| 335 |         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
 | |
| 336 | fun mk_bindclause (_, _, i, T) = mk_scomp_split T | |
| 337 |       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
 | |
| 338 |   in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
 | |
| 339 | ||
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 340 | fun mk_reporting_generator_expr thy prop Ts = | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 341 | let | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 342 | val bound_max = length Ts - 1; | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 343 | val bounds = map_index (fn (i, ty) => | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 344 | (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts; | 
| 38549 
d0385f2764d8
use antiquotations for remaining unqualified constants in HOL
 haftmann parents: 
38543diff
changeset | 345 |     fun strip_imp (Const(@{const_name "op -->"},_) $ A $ B) = apfst (cons A) (strip_imp B)
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 346 | | strip_imp A = ([], A) | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 347 | val prop' = betapplys (prop, map (fn (i, _, _, _) => Bound i) bounds); | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 348 |     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds)
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 349 | val (assms, concl) = strip_imp prop' | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 350 | val return = | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 351 |       @{term "Pair :: term list option * (bool list * bool) => Random.seed => (term list option * (bool list * bool)) * Random.seed"};
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 352 | fun mk_assms_report i = | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 353 |       HOLogic.mk_prod (@{term "None :: term list option"},
 | 
| 38553 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 354 | HOLogic.mk_prod (HOLogic.mk_list HOLogic.boolT | 
| 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 355 |           (replicate i @{term True} @ replicate (length assms - i) @{term False}),
 | 
| 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 356 |         @{term False}))
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 357 | fun mk_concl_report b = | 
| 38553 
56965d8e4e11
use HOLogic.boolT and @{typ bool} more pervasively
 haftmann parents: 
38549diff
changeset | 358 |       HOLogic.mk_prod (HOLogic.mk_list HOLogic.boolT (replicate (length assms) @{term True}),
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 359 |         if b then @{term True} else @{term False})
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 360 | val If = | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 361 |       @{term "If :: bool => term list option * (bool list * bool) => term list option * (bool list * bool) => term list option * (bool list * bool)"}
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 362 | val concl_check = If $ concl $ | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 363 |       HOLogic.mk_prod (@{term "None :: term list option"}, mk_concl_report true) $
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 364 |       HOLogic.mk_prod (@{term "Some :: term list  => term list option"} $ terms, mk_concl_report false)
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 365 | val check = fold_rev (fn (i, assm) => fn t => If $ assm $ t $ mk_assms_report i) | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 366 | (map_index I assms) concl_check | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 367 | fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT); | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 368 |     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 369 |     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 | 370 | 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 | 371 | fun mk_split T = Sign.mk_const thy | 
| 37591 | 372 |       (@{const_name prod_case}, [T, @{typ "unit => term"},
 | 
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 373 |         liftT @{typ "term list option * (bool list * bool)"} @{typ Random.seed}]);
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 374 | fun mk_scomp_split T t t' = | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 375 |       mk_scomp (mk_termtyp T) @{typ "term list option * (bool list * bool)"} @{typ Random.seed} t
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 376 |         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 377 | fun mk_bindclause (_, _, i, T) = mk_scomp_split T | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 378 |       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 379 | in | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 380 |     Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check))
 | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 381 | end | 
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 382 | |
| 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 383 | fun compile_generator_expr thy report t = | 
| 31950 | 384 | let | 
| 385 | val Ts = (map snd o fst o strip_abs) t; | |
| 38543 | 386 | in if report then | 
| 387 | let | |
| 388 | val t' = mk_reporting_generator_expr thy t Ts; | |
| 389 |       val compile = Code_Eval.eval (SOME target) ("Quickcheck_Generators.eval_report_ref", eval_report_ref)
 | |
| 390 | (fn proc => fn g => fn s => g s #>> (apfst o Option.map o map) proc) thy t' []; | |
| 391 | in compile #> Random_Engine.run end | |
| 392 | else | |
| 393 | let | |
| 394 | val t' = mk_generator_expr thy t Ts; | |
| 395 |       val compile = Code_Eval.eval (SOME target) ("Quickcheck_Generators.eval_ref", eval_ref)
 | |
| 396 | (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' []; | |
| 397 | val dummy_report = ([], false) | |
| 398 | in compile #> Random_Engine.run #> rpair dummy_report end | |
| 35378 
95d0e3adf38e
added basic reporting of test cases to quickcheck
 bulwahn parents: 
35166diff
changeset | 399 | end; | 
| 31950 | 400 | |
| 401 | ||
| 31260 | 402 | (** setup **) | 
| 403 | ||
| 38393 | 404 | val setup = | 
| 405 | Datatype.interpretation ensure_random_datatype | |
| 34028 
1e6206763036
split off evaluation mechanisms in separte module Code_Eval
 haftmann parents: 
33968diff
changeset | 406 | #> Code_Target.extend_target (target, (Code_Eval.target, K I)) | 
| 31950 | 407 |   #> Quickcheck.add_generator ("code", compile_generator_expr o ProofContext.theory_of);
 | 
| 31260 | 408 | |
| 409 | end; |