| author | bulwahn | 
| Tue, 27 Oct 2009 09:03:56 +0100 | |
| changeset 33252 | 8bd2eb003b8f | 
| parent 33205 | 20587741a8d9 | 
| child 33243 | 17014b1b9353 | 
| permissions | -rw-r--r-- | 
| 31260 | 1 | (* Author: Florian Haftmann, TU Muenchen | 
| 2 | ||
| 3 | Quickcheck generators for various types. | |
| 4 | *) | |
| 5 | ||
| 6 | signature QUICKCHECK_GENERATORS = | |
| 7 | sig | |
| 8 | type seed = Random_Engine.seed | |
| 9 |   val random_fun: typ -> typ -> ('a -> 'a -> bool) -> ('a -> term)
 | |
| 10 |     -> (seed -> ('b * (unit -> term)) * seed) -> (seed -> seed * seed)
 | |
| 11 |     -> seed -> (('a -> 'b) * (unit -> Term.term)) * seed
 | |
| 12 | val ensure_random_typecopy: string -> theory -> theory | |
| 31868 | 13 | val random_aux_specification: string -> string -> term list -> local_theory -> local_theory | 
| 31737 | 14 | val mk_random_aux_eqs: theory -> Datatype.descr -> (string * sort) list | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 15 | -> string list -> string list * string list -> typ list * typ list | 
| 31868 | 16 | -> term list * (term * term) list | 
| 31737 | 17 | val ensure_random_datatype: Datatype.config -> string list -> theory -> theory | 
| 31950 | 18 | val compile_generator_expr: theory -> term -> int -> term list option | 
| 32740 | 19 | val eval_ref: (unit -> int -> seed -> term list option * seed) option Unsynchronized.ref | 
| 31260 | 20 | val setup: theory -> theory | 
| 21 | end; | |
| 22 | ||
| 23 | structure Quickcheck_Generators : QUICKCHECK_GENERATORS = | |
| 24 | struct | |
| 25 | ||
| 31950 | 26 | (** abstract syntax **) | 
| 31260 | 27 | |
| 31950 | 28 | fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
 | 
| 29 | val size = @{term "i::code_numeral"};
 | |
| 31984 | 30 | val size_pred = @{term "(i::code_numeral) - 1"};
 | 
| 31950 | 31 | val size' = @{term "j::code_numeral"};
 | 
| 32 | val seed = @{term "s::Random.seed"};
 | |
| 31260 | 33 | |
| 34 | ||
| 35 | (** typ "'a => 'b" **) | |
| 36 | ||
| 37 | type seed = Random_Engine.seed; | |
| 38 | ||
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 39 | fun random_fun T1 T2 eq term_of random random_split seed = | 
| 31260 | 40 | let | 
| 41 |     val fun_upd = Const (@{const_name fun_upd},
 | |
| 42 | (T1 --> T2) --> T1 --> T2 --> T1 --> T2); | |
| 32344 | 43 | val ((y, t2), seed') = random seed; | 
| 44 | val (seed'', seed''') = random_split seed'; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 45 | |
| 32740 | 46 |     val state = Unsynchronized.ref (seed'', [], fn () => Abs ("x", T1, t2 ()));
 | 
| 31260 | 47 | fun random_fun' x = | 
| 48 | let | |
| 49 | val (seed, fun_map, f_t) = ! state; | |
| 50 | in case AList.lookup (uncurry eq) fun_map x | |
| 51 | of SOME y => y | |
| 52 | | NONE => let | |
| 53 | val t1 = term_of x; | |
| 54 | val ((y, t2), seed') = random seed; | |
| 55 | val fun_map' = (x, y) :: fun_map; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 56 | val f_t' = fn () => fun_upd $ f_t () $ t1 $ t2 (); | 
| 31260 | 57 | val _ = state := (seed', fun_map', f_t'); | 
| 58 | in y end | |
| 59 | end; | |
| 31933 
cd6511035315
proper closures -- imperative programming considered harmful...
 haftmann parents: 
31902diff
changeset | 60 | fun term_fun' () = #3 (! state) (); | 
| 32344 | 61 | in ((random_fun', term_fun'), seed''') end; | 
| 31260 | 62 | |
| 63 | ||
| 64 | (** type copies **) | |
| 65 | ||
| 31950 | 66 | fun mk_random_typecopy tyco vs constr T' thy = | 
| 31260 | 67 | let | 
| 68 | val Ts = map TFree vs; | |
| 69 | val T = Type (tyco, Ts); | |
| 31950 | 70 | val Tm = termifyT T; | 
| 71 | val Tm' = termifyT T'; | |
| 31260 | 72 | fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts)); | 
| 73 | val v = "x"; | |
| 31950 | 74 | val t_v = Free (v, Tm'); | 
| 31260 | 75 | val t_constr = mk_const constr Ts; | 
| 31950 | 76 | val lhs = HOLogic.mk_random T size; | 
| 77 |     val rhs = HOLogic.mk_ST [((HOLogic.mk_random T' size, @{typ Random.seed}), SOME (v, Tm'))]
 | |
| 78 |       (HOLogic.mk_return Tm @{typ Random.seed}
 | |
| 32657 | 79 | (mk_const "Code_Evaluation.valapp" [T', T] | 
| 31260 | 80 |         $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
 | 
| 31950 | 81 |       @{typ Random.seed} (SOME Tm, @{typ Random.seed});
 | 
| 31260 | 82 | val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)); | 
| 83 | in | |
| 84 | thy | |
| 85 |     |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
 | |
| 86 | |> `(fn lthy => Syntax.check_term lthy eq) | |
| 33205 | 87 | |-> (fn eq => Specification.definition (NONE, (apfst (Binding.conceal) Attrib.empty_binding, eq))) | 
| 31260 | 88 | |> snd | 
| 89 | |> Class.prove_instantiation_exit (K (Class.intro_classes_tac [])) | |
| 90 | end; | |
| 91 | ||
| 92 | fun ensure_random_typecopy tyco thy = | |
| 93 | let | |
| 31950 | 94 |     val SOME { vs = raw_vs, constr, typ = raw_T, ... } =
 | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31673diff
changeset | 95 | Typecopy.get_info thy tyco; | 
| 31260 | 96 | val constrain = curry (Sorts.inter_sort (Sign.classes_of thy)); | 
| 31950 | 97 | val T = map_atyps (fn TFree (v, sort) => | 
| 98 |       TFree (v, constrain sort @{sort random})) raw_T;
 | |
| 99 | val vs' = Term.add_tfreesT T []; | |
| 31260 | 100 | val vs = map (fn (v, sort) => | 
| 101 |       (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
 | |
| 31950 | 102 |     val can_inst = Sign.of_sort thy (T, @{sort random});
 | 
| 103 | in if can_inst then mk_random_typecopy tyco vs constr T thy else thy end; | |
| 31260 | 104 | |
| 105 | ||
| 106 | (** datatypes **) | |
| 107 | ||
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 108 | (* definitional scheme for random instances on datatypes *) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 109 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 110 | (*FIXME avoid this low-level proving*) | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 111 | local | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 112 | |
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 113 | fun dest_ctyp_nth k cT = nth (Thm.dest_ctyp cT) k; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 114 | 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 | 115 | val lhs = eq |> Thm.dest_arg1; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 116 | val pt_random_aux = lhs |> Thm.dest_fun; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 117 | val ct_k = lhs |> Thm.dest_arg; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 118 | val pt_rhs = eq |> Thm.dest_arg |> Thm.dest_fun; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 119 | val aT = pt_random_aux |> Thm.ctyp_of_term |> dest_ctyp_nth 1; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 120 | |
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 121 | val rew_thms = map mk_meta_eq [@{thm code_numeral_zero_minus_one},
 | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 122 |   @{thm Suc_code_numeral_minus_one}, @{thm select_weight_cons_zero}, @{thm beyond_zero}];
 | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 123 | val rew_ts = map (Logic.dest_equals o Thm.prop_of) rew_thms; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 124 | val rew_ss = HOL_ss addsimps rew_thms; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 125 | |
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 126 | in | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 127 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 128 | fun random_aux_primrec eq lthy = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 129 | let | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 130 | val thy = ProofContext.theory_of lthy; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 131 | 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 | 132 | (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 133 | val Type (_, [_, iT]) = T; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 134 | val icT = Thm.ctyp_of thy iT; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 135 | val cert = Thm.cterm_of thy; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 136 | val inst = Thm.instantiate_cterm ([(aT, icT)], []); | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 137 | 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 | 138 | val t_rhs = lambda t_k proto_t_rhs; | 
| 31785 | 139 |     val eqs0 = [subst_v @{term "0::code_numeral"} eq,
 | 
| 140 |       subst_v (@{term "Suc_code_numeral"} $ t_k) eq];
 | |
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 141 | val eqs1 = map (Pattern.rewrite_term thy rew_ts []) eqs0; | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31673diff
changeset | 142 | val ((_, eqs2), lthy') = Primrec.add_primrec_simple | 
| 33205 | 143 | [((Binding.conceal (Binding.name random_aux), T), NoSyn)] eqs1 lthy; | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 144 | val cT_random_aux = inst pt_random_aux; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 145 | val cT_rhs = inst pt_rhs; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 146 |     val rule = @{thm random_aux_rec}
 | 
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 147 | |> Drule.instantiate ([(aT, icT)], | 
| 31785 | 148 | [(cT_random_aux, cert t_random_aux), (cT_rhs, cert t_rhs)]); | 
| 149 | val tac = ALLGOALS (rtac rule) | |
| 150 | THEN ALLGOALS (simp_tac rew_ss) | |
| 151 | THEN (ALLGOALS (ProofContext.fact_tac (flat eqs2))) | |
| 32970 
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
 wenzelm parents: 
32740diff
changeset | 152 | 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 | 153 | in (simp, lthy') end; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 154 | |
| 31611 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 155 | end; | 
| 
a577f77af93f
explicit instantiation yields considerable speedup
 haftmann parents: 
31609diff
changeset | 156 | |
| 31868 | 157 | fun random_aux_primrec_multi auxname [eq] lthy = | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 158 | lthy | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 159 | |> random_aux_primrec eq | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 160 | |>> (fn simp => [simp]) | 
| 31868 | 161 | | random_aux_primrec_multi auxname (eqs as _ :: _ :: _) lthy = | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 162 | let | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 163 | val thy = ProofContext.theory_of lthy; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 164 | 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 | 165 | 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 | 166 | val Ts = map fastype_of lhss; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 167 | val tupleT = foldr1 HOLogic.mk_prodT Ts; | 
| 31868 | 168 |         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 | 169 | val aux_eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 170 | (aux_lhs, foldr1 HOLogic.mk_prod rhss); | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 171 | fun mk_proj t [T] = [t] | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 172 | | mk_proj t (Ts as T :: (Ts' as _ :: _)) = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 173 |               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 | 174 |                 :: mk_proj (Const (@{const_name snd},
 | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 175 | 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 | 176 | val projs = mk_proj (aux_lhs) Ts; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 177 | 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 | 178 | val proj_defs = map2 (fn Free (name, _) => fn (_, rhs) => | 
| 33205 | 179 | ((Binding.conceal (Binding.name name), NoSyn), | 
| 180 | (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 | 181 | 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 | 182 | fun prove_eqs aux_simp proj_defs lthy = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 183 | let | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 184 | val proj_simps = map (snd o snd) proj_defs; | 
| 31645 | 185 |             fun tac { context = ctxt, prems = _ } =
 | 
| 31625 | 186 | ALLGOALS (simp_tac (HOL_ss addsimps proj_simps)) | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 187 | THEN ALLGOALS (EqSubst.eqsubst_tac ctxt [0] [aux_simp]) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 188 | THEN ALLGOALS (simp_tac (HOL_ss addsimps [fst_conv, snd_conv])); | 
| 32970 
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
 wenzelm parents: 
32740diff
changeset | 189 | 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 | 190 | in | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 191 | lthy | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 192 | |> random_aux_primrec aux_eq' | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 193 | ||>> fold_map (LocalTheory.define Thm.definitionK) proj_defs | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 194 | |-> (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 | 195 | end; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 196 | |
| 31868 | 197 | fun random_aux_specification prfx name eqs lthy = | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 198 | let | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 199 | 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 | 200 | o HOLogic.dest_Trueprop o hd) eqs) []; | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 201 | fun mk_proto_eq eq = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 202 | let | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 203 | 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 | 204 | 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 | 205 | val proto_eqs = map mk_proto_eq eqs; | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 206 | fun prove_simps proto_simps lthy = | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 207 | let | 
| 31625 | 208 | val ext_simps = map (fn thm => fun_cong OF [fun_cong OF [thm]]) proto_simps; | 
| 209 | 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 | 210 | in (map (fn prop => Skip_Proof.prove lthy vs [] prop (K tac)) eqs, lthy) end; | 
| 33205 | 211 | val b = Binding.conceal (Binding.qualify true prfx | 
| 212 | (Binding.qualify true name (Binding.name "simps"))); | |
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 213 | in | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 214 | lthy | 
| 31868 | 215 | |> random_aux_primrec_multi (name ^ prfx) proto_eqs | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 216 | |-> (fn proto_simps => prove_simps proto_simps) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 217 | |-> (fn simps => LocalTheory.note Thm.generatedK ((b, | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 218 | Code.add_default_eqn_attrib :: map (Attrib.internal o K) | 
| 33056 
791a4655cae3
renamed "nitpick_const_xxx" attributes to "nitpick_xxx" and "nitpick_ind_intros" to "nitpick_intros"
 blanchet parents: 
33029diff
changeset | 219 | [Simplifier.simp_add, Nitpick_Simps.add, Quickcheck_RecFun_Simps.add]), | 
| 31485 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 220 | simps)) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 221 | |> snd | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 222 | end | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 223 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 224 | |
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 225 | (* constructing random instances on datatypes *) | 
| 
259a3c90016e
added infrastructure for definitorial construction of generators for datatypes
 haftmann parents: 
31260diff
changeset | 226 | |
| 31868 | 227 | val random_auxN = "random_aux"; | 
| 228 | ||
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 229 | 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 | 230 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 231 | val mk_const = curry (Sign.mk_const thy); | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 232 | val random_auxsN = map (prefix (random_auxN ^ "_")) (names @ auxnames); | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 233 | val rTs = Ts @ Us; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 234 |     fun random_resultT T = @{typ Random.seed}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 235 |       --> HOLogic.mk_prodT (termifyT T,@{typ Random.seed});
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 236 | val pTs = map random_resultT rTs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 237 |     fun sizeT T = @{typ code_numeral} --> @{typ code_numeral} --> T;
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 238 | val random_auxT = sizeT o random_resultT; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 239 | 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 | 240 | random_auxsN rTs; | 
| 31950 | 241 | fun mk_random_call T = (NONE, (HOLogic.mk_random T size', T)); | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 242 | fun mk_random_aux_call fTs (k, _) (tyco, Ts) = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 243 | let | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 244 | val T = Type (tyco, Ts); | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 245 | fun mk_random_fun_lift [] t = t | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 246 | | mk_random_fun_lift (fT :: fTs) t = | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 247 |               mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
 | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 248 | mk_random_fun_lift fTs t; | 
| 31984 | 249 | val t = mk_random_fun_lift fTs (nth random_auxs k $ size_pred $ size'); | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 250 | val size = Option.map snd (DatatypeCodegen.find_shortest_path descr k) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 251 | |> the_default 0; | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 252 | in (SOME size, (t, fTs ---> T)) end; | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 253 | val tss = DatatypeAux.interpret_construction descr vs | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 254 |       { atyp = mk_random_call, dtyp = mk_random_aux_call };
 | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 255 | fun mk_consexpr simpleT (c, xs) = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 256 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 257 | val (ks, simple_tTs) = split_list xs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 258 | val T = termifyT simpleT; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 259 | val tTs = (map o apsnd) termifyT simple_tTs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 260 | val is_rec = exists is_some ks; | 
| 33029 | 261 | 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 | 262 | val vs = Name.names Name.context "x" (map snd simple_tTs); | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 263 | val vs' = (map o apsnd) termifyT vs; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 264 |         val tc = HOLogic.mk_return T @{typ Random.seed}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 265 | (HOLogic.mk_valtermify_app c vs simpleT); | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 266 |         val t = HOLogic.mk_ST (map (fn (t, _) => (t, @{typ Random.seed})) tTs ~~ map SOME vs')
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 267 |           tc @{typ Random.seed} (SOME T, @{typ Random.seed});
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 268 | val tk = if is_rec | 
| 31950 | 269 | then if k = 0 then size | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 270 |             else @{term "Quickcheck.beyond :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
 | 
| 31950 | 271 |              $ HOLogic.mk_number @{typ code_numeral} k $ size
 | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 272 |           else @{term "1::code_numeral"}
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 273 | in (is_rec, HOLogic.mk_prod (tk, t)) end; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 274 | fun sort_rec xs = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 275 | map_filter (fn (true, t) => SOME t | _ => NONE) xs | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 276 | @ map_filter (fn (false, t) => SOME t | _ => NONE) xs; | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 277 | val gen_exprss = tss | 
| 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 278 | |> (map o apfst) Type | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 279 | |> 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 | 280 | fun mk_select (rT, xs) = | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 281 |       mk_const @{const_name Quickcheck.collapse} [@{typ "Random.seed"}, termifyT rT]
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 282 |       $ (mk_const @{const_name Random.select_weight} [random_resultT rT]
 | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 283 |         $ 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 | 284 | $ seed; | 
| 31950 | 285 | 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 | 286 | val auxs_rhss = map mk_select gen_exprss; | 
| 31868 | 287 | in (random_auxs, auxs_lhss ~~ auxs_rhss) end; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 288 | |
| 31868 | 289 | fun mk_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 | 290 | let | 
| 31668 
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
 haftmann parents: 
31641diff
changeset | 291 | val _ = DatatypeAux.message config "Creating quickcheck generators ..."; | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 292 | val mk_prop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 293 | fun mk_size_arg k = case DatatypeCodegen.find_shortest_path descr k | 
| 31950 | 294 | of SOME (_, l) => if l = 0 then size | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 295 |           else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
 | 
| 31950 | 296 |             $ HOLogic.mk_number @{typ code_numeral} l $ size
 | 
| 297 | | NONE => size; | |
| 31868 | 298 | val (random_auxs, auxs_eqs) = (apsnd o map) mk_prop_eq | 
| 31603 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 haftmann parents: 
31595diff
changeset | 299 | (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 | 300 | val random_defs = map_index (fn (k, T) => mk_prop_eq | 
| 31950 | 301 | (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 | 302 | in | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 303 | thy | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 304 |     |> TheoryTarget.instantiation (tycos, vs, @{sort random})
 | 
| 31868 | 305 | |> random_aux_specification prfx random_auxN auxs_eqs | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 306 | |> `(fn lthy => map (Syntax.check_term lthy) random_defs) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 307 | |-> (fn random_defs' => fold_map (fn random_def => | 
| 33205 | 308 | Specification.definition (NONE, (apfst (Binding.conceal) | 
| 309 | Attrib.empty_binding, random_def))) random_defs') | |
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 310 | |> snd | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 311 | |> Class.prove_instantiation_exit (K (Class.intro_classes_tac [])) | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 312 | end; | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 313 | |
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 314 | fun perhaps_constrain thy insts raw_vs = | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 315 | let | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 316 | fun meet_random (T, sort) = Sorts.meet_sort (Sign.classes_of thy) | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 317 | (Logic.varifyT T, sort); | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 318 | val vtab = Vartab.empty | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 319 | |> fold (fn (v, sort) => Vartab.update ((v, 0), sort)) raw_vs | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 320 | |> fold meet_random insts; | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 321 | 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 | 322 | end handle Sorts.CLASS_ERROR _ => NONE; | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 323 | |
| 31668 
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
 haftmann parents: 
31641diff
changeset | 324 | fun ensure_random_datatype config raw_tycos thy = | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 325 | let | 
| 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 326 | val algebra = Sign.classes_of thy; | 
| 31868 | 327 | val (descr, raw_vs, tycos, prfx, (names, auxnames), raw_TUs) = | 
| 31784 | 328 | Datatype.the_descr thy raw_tycos; | 
| 32378 | 329 | val typerep_vs = (map o apsnd) | 
| 31744 | 330 |       (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs;
 | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 331 |     val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd)
 | 
| 32378 | 332 | (DatatypeAux.interpret_construction descr typerep_vs | 
| 31744 | 333 |         { atyp = single, dtyp = (K o K o K) [] });
 | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 334 |     val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd)
 | 
| 32378 | 335 | (DatatypeAux.interpret_construction descr typerep_vs | 
| 31744 | 336 |         { atyp = K [], dtyp = K o K });
 | 
| 31608 | 337 | val has_inst = exists (fn tyco => | 
| 31595 
bd2f7211a420
first running version of qc generators for datatypes
 haftmann parents: 
31485diff
changeset | 338 |       can (Sorts.mg_domain algebra tyco) @{sort random}) tycos;
 | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 339 | in if has_inst then thy | 
| 32378 | 340 | else case perhaps_constrain thy (random_insts @ term_of_insts) typerep_vs | 
| 31668 
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
 haftmann parents: 
31641diff
changeset | 341 | of SOME constrain => mk_random_datatype config descr | 
| 32378 | 342 | (map constrain typerep_vs) tycos prfx (names, auxnames) | 
| 31623 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 343 | ((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 | 344 | | NONE => thy | 
| 
b72c11302b39
quickcheck generators for datatypes with functions
 haftmann parents: 
31611diff
changeset | 345 | end; | 
| 31260 | 346 | |
| 347 | ||
| 31950 | 348 | (** building and compiling generator expressions **) | 
| 349 | ||
| 32740 | 350 | val eval_ref : | 
| 351 | (unit -> int -> int * int -> term list option * (int * int)) option Unsynchronized.ref = | |
| 352 | Unsynchronized.ref NONE; | |
| 31950 | 353 | |
| 354 | val target = "Quickcheck"; | |
| 355 | ||
| 356 | fun mk_generator_expr thy prop Ts = | |
| 357 | let | |
| 358 | val bound_max = length Ts - 1; | |
| 359 | val bounds = map_index (fn (i, ty) => | |
| 360 | (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts; | |
| 361 | val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds); | |
| 362 |     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
 | |
| 363 |     val check = @{term "If :: bool => term list option => term list option => term list option"}
 | |
| 364 |       $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option "} $ terms);
 | |
| 365 |     val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
 | |
| 366 | fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT); | |
| 367 |     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
 | |
| 368 |     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
 | |
| 369 | liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g; | |
| 370 | fun mk_split T = Sign.mk_const thy | |
| 371 |       (@{const_name split}, [T, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
 | |
| 372 | fun mk_scomp_split T t t' = | |
| 373 |       mk_scomp (mk_termtyp T) @{typ "term list option"} @{typ Random.seed} t
 | |
| 374 |         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
 | |
| 375 | fun mk_bindclause (_, _, i, T) = mk_scomp_split T | |
| 376 |       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
 | |
| 377 |   in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
 | |
| 378 | ||
| 379 | fun compile_generator_expr thy t = | |
| 380 | let | |
| 381 | val Ts = (map snd o fst o strip_abs) t; | |
| 382 | val t' = mk_generator_expr thy t Ts; | |
| 383 |     val compile = Code_ML.eval (SOME target) ("Quickcheck_Generators.eval_ref", eval_ref)
 | |
| 384 | (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' []; | |
| 385 | in compile #> Random_Engine.run end; | |
| 386 | ||
| 387 | ||
| 31260 | 388 | (** setup **) | 
| 389 | ||
| 31950 | 390 | val setup = Typecopy.interpretation ensure_random_typecopy | 
| 391 | #> Datatype.interpretation ensure_random_datatype | |
| 392 | #> Code_Target.extend_target (target, (Code_ML.target_Eval, K I)) | |
| 393 |   #> Quickcheck.add_generator ("code", compile_generator_expr o ProofContext.theory_of);
 | |
| 31260 | 394 | |
| 395 | end; |