src/HOL/Tools/quickcheck_generators.ML
author boehmes
Sat Mar 27 02:10:00 2010 +0100 (2010-03-27)
changeset 35983 27e2fa7d4ce7
parent 35845 e5980f0ad025
child 37136 e0c9d3e49e15
permissions -rw-r--r--
slightly more general simproc (avoids errors of linarith)
     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)) * seed
    12   val ensure_random_typecopy: string -> theory -> theory
    13   val random_aux_specification: string -> string -> term list -> local_theory -> local_theory
    14   val mk_random_aux_eqs: theory -> Datatype.descr -> (string * sort) list
    15     -> string list -> string list * string list -> typ list * typ list
    16     -> term list * (term * term) list
    17   val ensure_random_datatype: Datatype.config -> string list -> theory -> theory
    18   val compile_generator_expr:
    19     theory -> bool -> term -> int -> term list option * (bool list * bool)
    20   val eval_ref: (unit -> int -> seed -> term list option * seed) option Unsynchronized.ref
    21   val eval_report_ref:
    22     (unit -> int -> seed -> (term list option * (bool list * bool)) * seed) option Unsynchronized.ref
    23   val setup: theory -> theory
    24 end;
    25 
    26 structure Quickcheck_Generators : QUICKCHECK_GENERATORS =
    27 struct
    28 
    29 (** abstract syntax **)
    30 
    31 fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
    32 val size = @{term "i::code_numeral"};
    33 val size_pred = @{term "(i::code_numeral) - 1"};
    34 val size' = @{term "j::code_numeral"};
    35 val seed = @{term "s::Random.seed"};
    36 
    37 
    38 (** typ "'a => 'b" **)
    39 
    40 type seed = Random_Engine.seed;
    41 
    42 fun random_fun T1 T2 eq term_of random random_split seed =
    43   let
    44     val fun_upd = Const (@{const_name fun_upd},
    45       (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
    46     val ((y, t2), seed') = random seed;
    47     val (seed'', seed''') = random_split seed';
    48 
    49     val state = Unsynchronized.ref (seed'', [], fn () => Abs ("x", T1, t2 ()));
    50     fun random_fun' x =
    51       let
    52         val (seed, fun_map, f_t) = ! state;
    53       in case AList.lookup (uncurry eq) fun_map x
    54        of SOME y => y
    55         | NONE => let
    56               val t1 = term_of x;
    57               val ((y, t2), seed') = random seed;
    58               val fun_map' = (x, y) :: fun_map;
    59               val f_t' = fn () => fun_upd $ f_t () $ t1 $ t2 ();
    60               val _ = state := (seed', fun_map', f_t');
    61             in y end
    62       end;
    63     fun term_fun' () = #3 (! state) ();
    64   in ((random_fun', term_fun'), seed''') end;
    65 
    66 
    67 (** type copies **)
    68 
    69 fun mk_random_typecopy tyco vs constr T' thy =
    70   let
    71     val mk_const = curry (Sign.mk_const thy);
    72     val Ts = map TFree vs;  
    73     val T = Type (tyco, Ts);
    74     val Tm = termifyT T;
    75     val Tm' = termifyT T';
    76     val v = "x";
    77     val t_v = Free (v, Tm');
    78     val t_constr = Const (constr, T' --> T);
    79     val lhs = HOLogic.mk_random T size;
    80     val rhs = HOLogic.mk_ST [((HOLogic.mk_random T' size, @{typ Random.seed}), SOME (v, Tm'))]
    81       (HOLogic.mk_return Tm @{typ Random.seed}
    82       (mk_const "Code_Evaluation.valapp" [T', T]
    83         $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
    84       @{typ Random.seed} (SOME Tm, @{typ Random.seed});
    85     val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
    86   in   
    87     thy
    88     |> Theory_Target.instantiation ([tyco], vs, @{sort random})
    89     |> `(fn lthy => Syntax.check_term lthy eq)
    90     |-> (fn eq => Specification.definition (NONE, (apfst Binding.conceal Attrib.empty_binding, eq)))
    91     |> snd
    92     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
    93   end;
    94 
    95 fun ensure_random_typecopy tyco thy =
    96   let
    97     val SOME { vs = raw_vs, constr, typ = raw_T, ... } =
    98       Typecopy.get_info thy tyco;
    99     val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
   100     val T = map_atyps (fn TFree (v, sort) =>
   101       TFree (v, constrain sort @{sort random})) raw_T;
   102     val vs' = Term.add_tfreesT T [];
   103     val vs = map (fn (v, sort) =>
   104       (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
   105     val can_inst = Sign.of_sort thy (T, @{sort random});
   106   in if can_inst then mk_random_typecopy tyco vs constr T thy else thy end;
   107 
   108 
   109 (** datatypes **)
   110 
   111 (* definitional scheme for random instances on datatypes *)
   112 
   113 (*FIXME avoid this low-level proving*)
   114 local
   115 
   116 fun dest_ctyp_nth k cT = nth (Thm.dest_ctyp cT) k;
   117 val eq = Thm.cprop_of @{thm random_aux_rec} |> Thm.dest_arg |> Thm.dest_arg |> Thm.dest_arg;
   118 val lhs = eq |> Thm.dest_arg1;
   119 val pt_random_aux = lhs |> Thm.dest_fun;
   120 val ct_k = lhs |> Thm.dest_arg;
   121 val pt_rhs = eq |> Thm.dest_arg |> Thm.dest_fun;
   122 val aT = pt_random_aux |> Thm.ctyp_of_term |> dest_ctyp_nth 1;
   123 
   124 val rew_thms = map mk_meta_eq [@{thm code_numeral_zero_minus_one},
   125   @{thm Suc_code_numeral_minus_one}, @{thm select_weight_cons_zero}, @{thm beyond_zero}];
   126 val rew_ts = map (Logic.dest_equals o Thm.prop_of) rew_thms;
   127 val rew_ss = HOL_ss addsimps rew_thms;
   128 
   129 in
   130 
   131 fun random_aux_primrec eq lthy =
   132   let
   133     val thy = ProofContext.theory_of lthy;
   134     val ((t_random_aux as Free (random_aux, T)) $ (t_k as Free (v, _)), proto_t_rhs) =
   135       (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
   136     val Type (_, [_, iT]) = T;
   137     val icT = Thm.ctyp_of thy iT;
   138     val cert = Thm.cterm_of thy;
   139     val inst = Thm.instantiate_cterm ([(aT, icT)], []);
   140     fun subst_v t' = map_aterms (fn t as Free (w, _) => if v = w then t' else t | t => t);
   141     val t_rhs = lambda t_k proto_t_rhs;
   142     val eqs0 = [subst_v @{term "0::code_numeral"} eq,
   143       subst_v (@{term "Suc_code_numeral"} $ t_k) eq];
   144     val eqs1 = map (Pattern.rewrite_term thy rew_ts []) eqs0;
   145     val ((_, (_, eqs2)), lthy') = Primrec.add_primrec_simple
   146       [((Binding.conceal (Binding.name random_aux), T), NoSyn)] eqs1 lthy;
   147     val cT_random_aux = inst pt_random_aux;
   148     val cT_rhs = inst pt_rhs;
   149     val rule = @{thm random_aux_rec}
   150       |> Drule.instantiate ([(aT, icT)],
   151            [(cT_random_aux, cert t_random_aux), (cT_rhs, cert t_rhs)]);
   152     val tac = ALLGOALS (rtac rule)
   153       THEN ALLGOALS (simp_tac rew_ss)
   154       THEN (ALLGOALS (ProofContext.fact_tac eqs2))
   155     val simp = Skip_Proof.prove lthy' [v] [] eq (K tac);
   156   in (simp, lthy') end;
   157 
   158 end;
   159 
   160 fun random_aux_primrec_multi auxname [eq] lthy =
   161       lthy
   162       |> random_aux_primrec eq
   163       |>> (fn simp => [simp])
   164   | random_aux_primrec_multi auxname (eqs as _ :: _ :: _) lthy =
   165       let
   166         val thy = ProofContext.theory_of lthy;
   167         val (lhss, rhss) = map_split (HOLogic.dest_eq o HOLogic.dest_Trueprop) eqs;
   168         val (vs, (arg as Free (v, _)) :: _) = map_split (fn (t1 $ t2) => (t1, t2)) lhss;
   169         val Ts = map fastype_of lhss;
   170         val tupleT = foldr1 HOLogic.mk_prodT Ts;
   171         val aux_lhs = Free ("mutual_" ^ auxname, fastype_of arg --> tupleT) $ arg;
   172         val aux_eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
   173           (aux_lhs, foldr1 HOLogic.mk_prod rhss);
   174         fun mk_proj t [T] = [t]
   175           | mk_proj t (Ts as T :: (Ts' as _ :: _)) =
   176               Const (@{const_name fst}, foldr1 HOLogic.mk_prodT Ts --> T) $ t
   177                 :: mk_proj (Const (@{const_name snd},
   178                   foldr1 HOLogic.mk_prodT Ts --> foldr1 HOLogic.mk_prodT Ts') $ t) Ts';
   179         val projs = mk_proj (aux_lhs) Ts;
   180         val proj_eqs = map2 (fn v => fn proj => (v, lambda arg proj)) vs projs;
   181         val proj_defs = map2 (fn Free (name, _) => fn (_, rhs) =>
   182           ((Binding.conceal (Binding.name name), NoSyn),
   183             (apfst Binding.conceal Attrib.empty_binding, rhs))) vs proj_eqs;
   184         val aux_eq' = Pattern.rewrite_term thy proj_eqs [] aux_eq;
   185         fun prove_eqs aux_simp proj_defs lthy = 
   186           let
   187             val proj_simps = map (snd o snd) proj_defs;
   188             fun tac { context = ctxt, prems = _ } =
   189               ALLGOALS (simp_tac (HOL_ss addsimps proj_simps))
   190               THEN ALLGOALS (EqSubst.eqsubst_tac ctxt [0] [aux_simp])
   191               THEN ALLGOALS (simp_tac (HOL_ss addsimps [fst_conv, snd_conv]));
   192           in (map (fn prop => Skip_Proof.prove lthy [v] [] prop tac) eqs, lthy) end;
   193       in
   194         lthy
   195         |> random_aux_primrec aux_eq'
   196         ||>> fold_map Local_Theory.define proj_defs
   197         |-> (fn (aux_simp, proj_defs) => prove_eqs aux_simp proj_defs)
   198       end;
   199 
   200 fun random_aux_specification prfx name eqs lthy =
   201   let
   202     val vs = fold Term.add_free_names ((snd o strip_comb o fst o HOLogic.dest_eq
   203       o HOLogic.dest_Trueprop o hd) eqs) [];
   204     fun mk_proto_eq eq =
   205       let
   206         val (head $ t $ u, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
   207       in ((HOLogic.mk_Trueprop o HOLogic.mk_eq) (head, lambda t (lambda u rhs))) end;
   208     val proto_eqs = map mk_proto_eq eqs;
   209     fun prove_simps proto_simps lthy =
   210       let
   211         val ext_simps = map (fn thm => fun_cong OF [fun_cong OF [thm]]) proto_simps;
   212         val tac = ALLGOALS (ProofContext.fact_tac ext_simps);
   213       in (map (fn prop => Skip_Proof.prove lthy vs [] prop (K tac)) eqs, lthy) end;
   214     val b = Binding.conceal (Binding.qualify true prfx
   215       (Binding.qualify true name (Binding.name "simps")));
   216   in
   217     lthy
   218     |> random_aux_primrec_multi (name ^ prfx) proto_eqs
   219     |-> (fn proto_simps => prove_simps proto_simps)
   220     |-> (fn simps => Local_Theory.note
   221       ((b, Code.add_default_eqn_attrib :: map (Attrib.internal o K)
   222           [Simplifier.simp_add, Nitpick_Simps.add]), simps))
   223     |> snd
   224   end
   225 
   226 
   227 (* constructing random instances on datatypes *)
   228 
   229 val random_auxN = "random_aux";
   230 
   231 fun mk_random_aux_eqs thy descr vs tycos (names, auxnames) (Ts, Us) =
   232   let
   233     val mk_const = curry (Sign.mk_const thy);
   234     val random_auxsN = map (prefix (random_auxN ^ "_")) (names @ auxnames);
   235     val rTs = Ts @ Us;
   236     fun random_resultT T = @{typ Random.seed}
   237       --> HOLogic.mk_prodT (termifyT T,@{typ Random.seed});
   238     val pTs = map random_resultT rTs;
   239     fun sizeT T = @{typ code_numeral} --> @{typ code_numeral} --> T;
   240     val random_auxT = sizeT o random_resultT;
   241     val random_auxs = map2 (fn s => fn rT => Free (s, random_auxT rT))
   242       random_auxsN rTs;
   243     fun mk_random_call T = (NONE, (HOLogic.mk_random T size', T));
   244     fun mk_random_aux_call fTs (k, _) (tyco, Ts) =
   245       let
   246         val T = Type (tyco, Ts);
   247         fun mk_random_fun_lift [] t = t
   248           | mk_random_fun_lift (fT :: fTs) t =
   249               mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
   250                 mk_random_fun_lift fTs t;
   251         val t = mk_random_fun_lift fTs (nth random_auxs k $ size_pred $ size');
   252         val size = Option.map snd (Datatype_Aux.find_shortest_path descr k)
   253           |> the_default 0;
   254       in (SOME size, (t, fTs ---> T)) end;
   255     val tss = Datatype_Aux.interpret_construction descr vs
   256       { atyp = mk_random_call, dtyp = mk_random_aux_call };
   257     fun mk_consexpr simpleT (c, xs) =
   258       let
   259         val (ks, simple_tTs) = split_list xs;
   260         val T = termifyT simpleT;
   261         val tTs = (map o apsnd) termifyT simple_tTs;
   262         val is_rec = exists is_some ks;
   263         val k = fold (fn NONE => I | SOME k => Integer.max k) ks 0;
   264         val vs = Name.names Name.context "x" (map snd simple_tTs);
   265         val vs' = (map o apsnd) termifyT vs;
   266         val tc = HOLogic.mk_return T @{typ Random.seed}
   267           (HOLogic.mk_valtermify_app c vs simpleT);
   268         val t = HOLogic.mk_ST (map (fn (t, _) => (t, @{typ Random.seed})) tTs ~~ map SOME vs')
   269           tc @{typ Random.seed} (SOME T, @{typ Random.seed});
   270         val tk = if is_rec
   271           then if k = 0 then size
   272             else @{term "Quickcheck.beyond :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
   273              $ HOLogic.mk_number @{typ code_numeral} k $ size
   274           else @{term "1::code_numeral"}
   275       in (is_rec, HOLogic.mk_prod (tk, t)) end;
   276     fun sort_rec xs =
   277       map_filter (fn (true, t) => SOME t | _ =>  NONE) xs
   278       @ map_filter (fn (false, t) => SOME t | _ =>  NONE) xs;
   279     val gen_exprss = tss
   280       |> (map o apfst) Type
   281       |> map (fn (T, cs) => (T, (sort_rec o map (mk_consexpr T)) cs));
   282     fun mk_select (rT, xs) =
   283       mk_const @{const_name Quickcheck.collapse} [@{typ "Random.seed"}, termifyT rT]
   284       $ (mk_const @{const_name Random.select_weight} [random_resultT rT]
   285         $ HOLogic.mk_list (HOLogic.mk_prodT (@{typ code_numeral}, random_resultT rT)) xs)
   286           $ seed;
   287     val auxs_lhss = map (fn t => t $ size $ size' $ seed) random_auxs;
   288     val auxs_rhss = map mk_select gen_exprss;
   289   in (random_auxs, auxs_lhss ~~ auxs_rhss) end;
   290 
   291 fun mk_random_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   292   let
   293     val _ = Datatype_Aux.message config "Creating quickcheck generators ...";
   294     val mk_prop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
   295     fun mk_size_arg k = case Datatype_Aux.find_shortest_path descr k
   296      of SOME (_, l) => if l = 0 then size
   297           else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
   298             $ HOLogic.mk_number @{typ code_numeral} l $ size
   299       | NONE => size;
   300     val (random_auxs, auxs_eqs) = (apsnd o map) mk_prop_eq
   301       (mk_random_aux_eqs thy descr vs tycos (names, auxnames) (Ts, Us));
   302     val random_defs = map_index (fn (k, T) => mk_prop_eq
   303       (HOLogic.mk_random T size, nth random_auxs k $ mk_size_arg k $ size)) Ts;
   304   in
   305     thy
   306     |> Theory_Target.instantiation (tycos, vs, @{sort random})
   307     |> random_aux_specification prfx random_auxN auxs_eqs
   308     |> `(fn lthy => map (Syntax.check_term lthy) random_defs)
   309     |-> (fn random_defs' => fold_map (fn random_def =>
   310           Specification.definition (NONE, (apfst Binding.conceal
   311             Attrib.empty_binding, random_def))) random_defs')
   312     |> snd
   313     |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   314   end;
   315 
   316 fun perhaps_constrain thy insts raw_vs =
   317   let
   318     fun meet_random (T, sort) = Sorts.meet_sort (Sign.classes_of thy) 
   319       (Logic.varifyT_global T, sort);
   320     val vtab = Vartab.empty
   321       |> fold (fn (v, sort) => Vartab.update ((v, 0), sort)) raw_vs
   322       |> fold meet_random insts;
   323   in SOME (fn (v, _) => (v, (the o Vartab.lookup vtab) (v, 0)))
   324   end handle Sorts.CLASS_ERROR _ => NONE;
   325 
   326 fun ensure_random_datatype config raw_tycos thy =
   327   let
   328     val algebra = Sign.classes_of thy;
   329     val (descr, raw_vs, tycos, prfx, (names, auxnames), raw_TUs) =
   330       Datatype.the_descr thy raw_tycos;
   331     val typerep_vs = (map o apsnd)
   332       (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs;
   333     val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd)
   334       (Datatype_Aux.interpret_construction descr typerep_vs
   335         { atyp = single, dtyp = (K o K o K) [] });
   336     val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd)
   337       (Datatype_Aux.interpret_construction descr typerep_vs
   338         { atyp = K [], dtyp = K o K });
   339     val has_inst = exists (fn tyco =>
   340       can (Sorts.mg_domain algebra tyco) @{sort random}) tycos;
   341   in if has_inst then thy
   342     else case perhaps_constrain thy (random_insts @ term_of_insts) typerep_vs
   343      of SOME constrain => mk_random_datatype config descr
   344           (map constrain typerep_vs) tycos prfx (names, auxnames)
   345             ((pairself o map o map_atyps) (fn TFree v => TFree (constrain v)) raw_TUs) thy
   346       | NONE => thy
   347   end;
   348 
   349 
   350 (** building and compiling generator expressions **)
   351 
   352 val eval_ref :
   353     (unit -> int -> int * int -> term list option * (int * int)) option Unsynchronized.ref =
   354   Unsynchronized.ref NONE;
   355 
   356 val eval_report_ref :
   357     (unit -> int -> seed -> (term list option * (bool list * bool)) * seed) option Unsynchronized.ref =
   358   Unsynchronized.ref NONE;
   359 
   360 val target = "Quickcheck";
   361 
   362 fun mk_generator_expr thy prop Ts =
   363   let
   364     val bound_max = length Ts - 1;
   365     val bounds = map_index (fn (i, ty) =>
   366       (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts;
   367     val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
   368     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
   369     val check = @{term "If :: bool => term list option => term list option => term list option"}
   370       $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option"} $ terms);
   371     val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
   372     fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
   373     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
   374     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
   375       liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
   376     fun mk_split T = Sign.mk_const thy
   377       (@{const_name split}, [T, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
   378     fun mk_scomp_split T t t' =
   379       mk_scomp (mk_termtyp T) @{typ "term list option"} @{typ Random.seed} t
   380         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
   381     fun mk_bindclause (_, _, i, T) = mk_scomp_split T
   382       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
   383   in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
   384 
   385 fun mk_reporting_generator_expr thy prop Ts =
   386   let
   387     val bound_max = length Ts - 1;
   388     val bounds = map_index (fn (i, ty) =>
   389       (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts;
   390     fun strip_imp (Const("op -->",_) $ A $ B) = apfst (cons A) (strip_imp B)
   391       | strip_imp A = ([], A)
   392     val prop' = betapplys (prop, map (fn (i, _, _, _) => Bound i) bounds);
   393     val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds)
   394     val (assms, concl) = strip_imp prop'
   395     val return =
   396       @{term "Pair :: term list option * (bool list * bool) => Random.seed => (term list option * (bool list * bool)) * Random.seed"};
   397     fun mk_assms_report i =
   398       HOLogic.mk_prod (@{term "None :: term list option"},
   399         HOLogic.mk_prod (HOLogic.mk_list @{typ "bool"}
   400           (replicate i @{term "True"} @ replicate (length assms - i) @{term "False"}),
   401         @{term "False"}))
   402     fun mk_concl_report b =
   403       HOLogic.mk_prod (HOLogic.mk_list @{typ "bool"} (replicate (length assms) @{term "True"}),
   404         if b then @{term True} else @{term False})
   405     val If =
   406       @{term "If :: bool => term list option * (bool list * bool) => term list option * (bool list * bool) => term list option * (bool list * bool)"}
   407     val concl_check = If $ concl $
   408       HOLogic.mk_prod (@{term "None :: term list option"}, mk_concl_report true) $
   409       HOLogic.mk_prod (@{term "Some :: term list  => term list option"} $ terms, mk_concl_report false)
   410     val check = fold_rev (fn (i, assm) => fn t => If $ assm $ t $ mk_assms_report i)
   411       (map_index I assms) concl_check
   412     fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
   413     fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
   414     fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
   415       liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
   416     fun mk_split T = Sign.mk_const thy
   417       (@{const_name split}, [T, @{typ "unit => term"},
   418         liftT @{typ "term list option * (bool list * bool)"} @{typ Random.seed}]);
   419     fun mk_scomp_split T t t' =
   420       mk_scomp (mk_termtyp T) @{typ "term list option * (bool list * bool)"} @{typ Random.seed} t
   421         (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
   422     fun mk_bindclause (_, _, i, T) = mk_scomp_split T
   423       (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
   424   in
   425     Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check))
   426   end
   427 
   428 fun compile_generator_expr thy report t =
   429   let
   430     val Ts = (map snd o fst o strip_abs) t;
   431   in
   432     if report then
   433       let
   434         val t' = mk_reporting_generator_expr thy t Ts;
   435         val compile = Code_Eval.eval (SOME target) ("Quickcheck_Generators.eval_report_ref", eval_report_ref)
   436           (fn proc => fn g => fn s => g s #>> ((apfst o Option.map o map) proc)) thy t' [];
   437       in
   438         compile #> Random_Engine.run
   439       end
   440     else
   441       let
   442         val t' = mk_generator_expr thy t Ts;
   443         val compile = Code_Eval.eval (SOME target) ("Quickcheck_Generators.eval_ref", eval_ref)
   444           (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
   445         val dummy_report = ([], false)
   446       in fn s => ((compile #> Random_Engine.run) s, dummy_report) end
   447   end;
   448 
   449 
   450 (** setup **)
   451 
   452 val setup = Typecopy.interpretation ensure_random_typecopy
   453   #> Datatype.interpretation ensure_random_datatype
   454   #> Code_Target.extend_target (target, (Code_Eval.target, K I))
   455   #> Quickcheck.add_generator ("code", compile_generator_expr o ProofContext.theory_of);
   456 
   457 end;