src/HOL/Tools/Quickcheck/exhaustive_generators.ML
author haftmann
Tue Oct 13 09:21:15 2015 +0200 (2015-10-13)
changeset 61424 c3658c18b7bc
parent 59498 50b60f501b05
child 62979 1e527c40ae40
permissions -rw-r--r--
prod_case as canonical name for product type eliminator
     1 (*  Title:      HOL/Tools/Quickcheck/exhaustive_generators.ML
     2     Author:     Lukas Bulwahn, TU Muenchen
     3 
     4 Exhaustive generators for various types.
     5 *)
     6 
     7 signature EXHAUSTIVE_GENERATORS =
     8 sig
     9   val compile_generator_expr:
    10     Proof.context -> (term * term list) list -> bool -> int list -> (bool * term list) option * Quickcheck.report option
    11   val compile_generator_exprs: Proof.context -> term list -> (int -> term list option) list
    12   val compile_validator_exprs: Proof.context -> term list -> (int -> bool) list
    13   val put_counterexample: (unit -> Code_Numeral.natural -> bool -> Code_Numeral.natural -> (bool * term list) option)
    14     -> Proof.context -> Proof.context
    15   val put_counterexample_batch: (unit -> (Code_Numeral.natural -> term list option) list)
    16     -> Proof.context -> Proof.context
    17   val put_validator_batch: (unit -> (Code_Numeral.natural -> bool) list) -> Proof.context -> Proof.context
    18   exception Counterexample of term list
    19   val smart_quantifier : bool Config.T
    20   val optimise_equality : bool Config.T
    21   val quickcheck_pretty : bool Config.T
    22   val setup_exhaustive_datatype_interpretation : theory -> theory
    23   val setup_bounded_forall_datatype_interpretation : theory -> theory
    24   
    25   val instantiate_full_exhaustive_datatype : Old_Datatype_Aux.config -> Old_Datatype_Aux.descr ->
    26     (string * sort) list -> string list -> string -> string list * string list -> typ list * typ list -> theory -> theory
    27   val instantiate_exhaustive_datatype : Old_Datatype_Aux.config -> Old_Datatype_Aux.descr ->
    28     (string * sort) list -> string list -> string -> string list * string list -> typ list * typ list -> theory -> theory
    29 
    30 end;
    31 
    32 structure Exhaustive_Generators : EXHAUSTIVE_GENERATORS =
    33 struct
    34 
    35 (* basics *)
    36 
    37 (** dynamic options **)
    38 
    39 val smart_quantifier = Attrib.setup_config_bool @{binding quickcheck_smart_quantifier} (K true)
    40 val optimise_equality = Attrib.setup_config_bool @{binding quickcheck_optimise_equality} (K true)
    41 
    42 val fast = Attrib.setup_config_bool @{binding quickcheck_fast} (K false)
    43 val bounded_forall = Attrib.setup_config_bool @{binding quickcheck_bounded_forall} (K false)
    44 val full_support = Attrib.setup_config_bool @{binding quickcheck_full_support} (K true)
    45 val quickcheck_pretty = Attrib.setup_config_bool @{binding quickcheck_pretty} (K true)
    46  
    47 
    48 (** abstract syntax **)
    49 
    50 fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => Code_Evaluation.term"});
    51 
    52 val size = @{term "i :: natural"}
    53 val size_pred = @{term "(i :: natural) - 1"}
    54 val size_ge_zero = @{term "(i :: natural) > 0"}
    55 
    56 fun mk_none_continuation (x, y) =
    57   let
    58     val (T as Type(@{type_name "option"}, _)) = fastype_of x
    59   in
    60     Const (@{const_name "Quickcheck_Exhaustive.orelse"}, T --> T --> T) $ x $ y
    61   end
    62 
    63 fun mk_if (b, t, e) =
    64   let
    65     val T = fastype_of t
    66   in Const (@{const_name "HOL.If"}, @{typ bool} --> T --> T --> T) $ b $ t $ e end
    67 
    68 (* handling inductive datatypes *)
    69 
    70 (** constructing generator instances **)
    71 
    72 exception FUNCTION_TYPE;
    73 
    74 exception Counterexample of term list
    75 
    76 val resultT =  @{typ "(bool * term list) option"};
    77 
    78 val exhaustiveN = "exhaustive";
    79 val full_exhaustiveN = "full_exhaustive";
    80 val bounded_forallN = "bounded_forall";
    81 
    82 fun fast_exhaustiveT T = (T --> @{typ unit})
    83   --> @{typ natural} --> @{typ unit}
    84 
    85 fun exhaustiveT T = (T --> resultT) --> @{typ natural} --> resultT
    86 
    87 fun bounded_forallT T = (T --> @{typ bool}) --> @{typ natural} --> @{typ bool}
    88 
    89 fun full_exhaustiveT T = (termifyT T --> resultT) --> @{typ natural} --> resultT
    90 
    91 fun check_allT T = (termifyT T --> resultT) --> resultT
    92 
    93 fun mk_equation_terms generics (descr, vs, Ts) =
    94   let
    95     val (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, exhaustives) = generics
    96     val rhss =
    97       Old_Datatype_Aux.interpret_construction descr vs
    98         { atyp = mk_call, dtyp = mk_aux_call }
    99       |> (map o apfst) Type
   100       |> map (fn (T, cs) => map (mk_consexpr T) cs)
   101       |> map mk_rhs
   102     val lhss = map2 (fn t => fn T => t $ test_function T $ size) exhaustives Ts
   103   in
   104     map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
   105   end
   106 
   107 fun gen_mk_call c T =  (T, fn t => c T $ absdummy T t $ size_pred)
   108 
   109 fun gen_mk_aux_call functerms fTs (k, _) (tyco, Ts) =
   110   let
   111     val T = Type (tyco, Ts)
   112     val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
   113   in
   114    (T, fn t => nth functerms k $ absdummy T t $ size_pred)
   115   end
   116 
   117 fun gen_mk_consexpr test_function simpleT (c, xs) =
   118   let
   119     val (Ts, fns) = split_list xs
   120     val constr = Const (c, Ts ---> simpleT)
   121     val bounds = map Bound (((length xs) - 1) downto 0)
   122     val start_term = test_function simpleT $ list_comb (constr, bounds)
   123   in fold_rev (fn f => fn t => f t) fns start_term end
   124 
   125 fun mk_equations functerms =
   126   let
   127     fun test_function T = Free ("f", T --> resultT)
   128     val mk_call = gen_mk_call (fn T =>
   129       Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T))
   130     val mk_aux_call = gen_mk_aux_call functerms
   131     val mk_consexpr = gen_mk_consexpr test_function
   132     fun mk_rhs exprs =
   133       mk_if (size_ge_zero, foldr1 mk_none_continuation exprs, Const (@{const_name "None"}, resultT))
   134   in
   135     mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
   136   end
   137 
   138 fun mk_bounded_forall_equations functerms =
   139   let
   140     fun test_function T = Free ("P", T --> @{typ bool})
   141     val mk_call = gen_mk_call (fn T =>
   142       Const (@{const_name "Quickcheck_Exhaustive.bounded_forall_class.bounded_forall"},
   143         bounded_forallT T))
   144     val mk_aux_call = gen_mk_aux_call functerms
   145     val mk_consexpr = gen_mk_consexpr test_function
   146     fun mk_rhs exprs =
   147       mk_if (size_ge_zero, foldr1 HOLogic.mk_conj exprs, @{term "True"})
   148   in
   149     mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
   150   end
   151   
   152 fun mk_full_equations functerms =
   153   let
   154     fun test_function T = Free ("f", termifyT T --> resultT)
   155     fun case_prod_const T = HOLogic.case_prod_const (T, @{typ "unit => Code_Evaluation.term"}, resultT)
   156     fun mk_call T =
   157       let
   158         val full_exhaustive =
   159           Const (@{const_name "Quickcheck_Exhaustive.full_exhaustive_class.full_exhaustive"},
   160             full_exhaustiveT T)
   161       in                                   
   162         (T, fn t => full_exhaustive $
   163           (case_prod_const T $ absdummy T (absdummy @{typ "unit => Code_Evaluation.term"} t)) $ size_pred)
   164       end
   165     fun mk_aux_call fTs (k, _) (tyco, Ts) =
   166       let
   167         val T = Type (tyco, Ts)
   168         val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
   169       in
   170         (T, fn t => nth functerms k $
   171           (case_prod_const T $ absdummy T (absdummy @{typ "unit => Code_Evaluation.term"} t)) $ size_pred)
   172       end
   173     fun mk_consexpr simpleT (c, xs) =
   174       let
   175         val (Ts, fns) = split_list xs
   176         val constr = Const (c, Ts ---> simpleT)
   177         val bounds = map (fn x => Bound (2 * x + 1)) (((length xs) - 1) downto 0)
   178         val Eval_App =
   179           Const (@{const_name Code_Evaluation.App}, HOLogic.termT --> HOLogic.termT --> HOLogic.termT)
   180         val Eval_Const =
   181           Const (@{const_name Code_Evaluation.Const}, HOLogic.literalT --> @{typ typerep} --> HOLogic.termT)
   182         val term = fold (fn u => fn t => Eval_App $ t $ (u $ @{term "()"}))
   183           bounds (Eval_Const $ HOLogic.mk_literal c $ HOLogic.mk_typerep (Ts ---> simpleT))
   184         val start_term = test_function simpleT $ 
   185         (HOLogic.pair_const simpleT @{typ "unit => Code_Evaluation.term"}
   186           $ (list_comb (constr, bounds)) $ absdummy @{typ unit} term)
   187       in fold_rev (fn f => fn t => f t) fns start_term end
   188     fun mk_rhs exprs =
   189       mk_if (size_ge_zero, foldr1 mk_none_continuation exprs,
   190         Const (@{const_name "None"}, resultT))
   191   in
   192     mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
   193   end
   194   
   195 
   196 (** instantiating generator classes **)
   197   
   198 fun contains_recursive_type_under_function_types xs =
   199   exists (fn (_, (_, _, cs)) => cs |> exists (snd #> exists (fn dT =>
   200     (case Old_Datatype_Aux.strip_dtyp dT of (_ :: _, Old_Datatype_Aux.DtRec _) => true | _ => false)))) xs
   201     
   202 fun instantiate_datatype (name, constprfx, sort, mk_equations, mk_T, argnames)
   203     config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   204   if not (contains_recursive_type_under_function_types descr) then
   205     let
   206       val _ = Old_Datatype_Aux.message config ("Creating " ^ name ^ "...")
   207       val fullnames = map (prefix (constprfx ^ "_")) (names @ auxnames)
   208     in
   209       thy
   210       |> Class.instantiation (tycos, vs, sort)
   211       |> Quickcheck_Common.define_functions
   212           (fn functerms => mk_equations functerms (descr, vs, Ts @ Us), NONE)
   213           prfx argnames fullnames (map mk_T (Ts @ Us))
   214       |> Class.prove_instantiation_exit (fn ctxt => Class.intro_classes_tac ctxt [])
   215     end
   216   else
   217     (Old_Datatype_Aux.message config
   218       ("Creation of " ^ name ^ " failed because the datatype is recursive under a function type");
   219     thy)
   220 
   221 val instantiate_bounded_forall_datatype = instantiate_datatype
   222  ("bounded universal quantifiers", bounded_forallN, @{sort bounded_forall},
   223    mk_bounded_forall_equations, bounded_forallT, ["P", "i"]);
   224 
   225 val instantiate_exhaustive_datatype = instantiate_datatype  
   226   ("exhaustive generators", exhaustiveN, @{sort exhaustive},
   227     mk_equations, exhaustiveT, ["f", "i"])
   228 
   229 val instantiate_full_exhaustive_datatype = instantiate_datatype
   230   ("full exhaustive generators", full_exhaustiveN, @{sort full_exhaustive},
   231     mk_full_equations, full_exhaustiveT, ["f", "i"])
   232 
   233 (* building and compiling generator expressions *)
   234 
   235 fun mk_let_expr (x, t, e) genuine =
   236   let
   237     val (T1, T2) = (fastype_of x, fastype_of (e genuine))
   238   in  
   239     Const (@{const_name Let}, T1 --> (T1 --> T2) --> T2) $ t $ lambda x (e genuine)
   240   end
   241 
   242 fun mk_safe_let_expr genuine_only none safe (x, t, e) genuine =
   243   let
   244     val (T1, T2) = (fastype_of x, fastype_of (e genuine))
   245     val if_t = Const (@{const_name "If"}, @{typ bool} --> T2 --> T2 --> T2)
   246   in
   247     Const (@{const_name "Quickcheck_Random.catch_match"}, T2 --> T2 --> T2) $ 
   248       (Const (@{const_name Let}, T1 --> (T1 --> T2) --> T2) $ t $ lambda x (e genuine)) $
   249       (if_t $ genuine_only $ none $ safe false)
   250   end
   251 
   252 fun mk_test_term lookup mk_closure mk_if mk_let none_t return ctxt =
   253   let
   254     val cnstrs = flat (maps
   255       (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
   256       (Symtab.dest (BNF_LFP_Compat.get_all (Proof_Context.theory_of ctxt)
   257          Quickcheck_Common.compat_prefs)))
   258     fun is_constrt (Const (s, T), ts) = (case (AList.lookup (op =) cnstrs s, body_type T) of
   259         (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname'
   260       | _ => false)
   261       | is_constrt _ = false
   262     fun mk_naive_test_term t =
   263       fold_rev mk_closure (map lookup (Term.add_free_names t []))
   264         (mk_if (t, none_t, return) true)
   265     fun mk_test (vars, check) = fold_rev mk_closure (map lookup vars) check
   266     fun mk_smart_test_term' concl bound_vars assms genuine =
   267       let
   268         fun vars_of t = subtract (op =) bound_vars (Term.add_free_names t [])
   269         fun mk_equality_term (lhs, f as Free (x, _)) c (assm, assms) =
   270           if member (op =) (Term.add_free_names lhs bound_vars) x then
   271             c (assm, assms)
   272           else
   273             (let
   274                val rec_call = mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms
   275                fun safe genuine =
   276                  the_default I (Option.map mk_closure (try lookup x)) (rec_call genuine)
   277             in
   278               mk_test (remove (op =) x (vars_of assm),
   279                 mk_let safe f (try lookup x) lhs 
   280                   (mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms) genuine)
   281             
   282             end)
   283           | mk_equality_term (lhs, t) c (assm, assms) =
   284             if is_constrt (strip_comb t) then
   285               let
   286                 val (constr, args) = strip_comb t
   287                 val T = fastype_of t
   288                 val vars = map Free (Variable.variant_frees ctxt (concl :: assms)
   289                   (map (fn t => ("x", fastype_of t)) args))
   290                 val varnames = map (fst o dest_Free) vars
   291                 val dummy_var = Free (singleton
   292                   (Variable.variant_frees ctxt (concl :: assms @ vars)) ("dummy", T))
   293                 val new_assms = map HOLogic.mk_eq (vars ~~ args)
   294                 val bound_vars' = union (op =) (vars_of lhs) (union (op =) varnames bound_vars)
   295                 val cont_t = mk_smart_test_term' concl bound_vars' (new_assms @ assms) genuine
   296               in
   297                 mk_test (vars_of lhs,
   298                   Case_Translation.make_case ctxt Case_Translation.Quiet Name.context lhs
   299                     [(list_comb (constr, vars), cont_t), (dummy_var, none_t)])
   300               end
   301             else c (assm, assms)
   302         fun default (assm, assms) =
   303           mk_test (vars_of assm,
   304             mk_if (HOLogic.mk_not assm, none_t, 
   305             mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms) genuine)
   306       in
   307         case assms of [] => mk_test (vars_of concl, mk_if (concl, none_t, return) genuine)
   308           | assm :: assms =>
   309             if Config.get ctxt optimise_equality then
   310               (case try HOLogic.dest_eq assm of
   311                 SOME (lhs, rhs) =>
   312                   mk_equality_term (lhs, rhs) (mk_equality_term (rhs, lhs) default) (assm, assms)
   313               | NONE => default (assm, assms))
   314             else default (assm, assms)
   315       end
   316     val mk_smart_test_term =
   317       Quickcheck_Common.strip_imp #> (fn (assms, concl) => mk_smart_test_term' concl [] assms true)
   318   in
   319     if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term
   320   end
   321 
   322 fun mk_fast_generator_expr ctxt (t, eval_terms) =
   323   let
   324     val ctxt' = Variable.auto_fixes t ctxt
   325     val names = Term.add_free_names t []
   326     val frees = map Free (Term.add_frees t [])
   327     fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
   328     val ([depth_name], _) = Variable.variant_fixes ["depth"] ctxt'
   329     val depth = Free (depth_name, @{typ natural})
   330     fun return _ = @{term "throw_Counterexample :: term list => unit"} $
   331       (HOLogic.mk_list @{typ "term"}
   332         (map (fn t => HOLogic.mk_term_of (fastype_of t) t) (frees @ eval_terms)))
   333     fun mk_exhaustive_closure (free as Free (_, T)) t =
   334       Const (@{const_name "Quickcheck_Exhaustive.fast_exhaustive_class.fast_exhaustive"},
   335         fast_exhaustiveT T)
   336         $ lambda free t $ depth
   337     val none_t = @{term "()"}
   338     fun mk_safe_if (cond, then_t, else_t) genuine = mk_if (cond, then_t, else_t genuine)
   339     fun mk_let _ def v_opt t e = mk_let_expr (the_default def v_opt, t, e)
   340     val mk_test_term = mk_test_term lookup mk_exhaustive_closure mk_safe_if mk_let none_t return ctxt 
   341   in lambda depth (@{term "catch_Counterexample :: unit => term list option"} $ mk_test_term t) end
   342 
   343 fun mk_unknown_term T = HOLogic.reflect_term (Const (@{const_name Quickcheck_Exhaustive.unknown}, T))
   344 
   345 fun mk_safe_term t = @{term "Quickcheck_Random.catch_match :: term => term => term"}
   346       $ (HOLogic.mk_term_of (fastype_of t) t) $ mk_unknown_term (fastype_of t)    
   347 
   348 fun mk_return t genuine = @{term "Some :: bool * term list => (bool * term list) option"} $
   349   (HOLogic.pair_const @{typ bool} @{typ "term list"} $ Quickcheck_Common.reflect_bool genuine $ t)
   350 
   351 fun mk_generator_expr ctxt (t, eval_terms) =
   352   let
   353     val ctxt' = Variable.auto_fixes t ctxt
   354     val names = Term.add_free_names t []
   355     val frees = map Free (Term.add_frees t [])
   356     fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
   357     val ([depth_name, genuine_only_name], _) =
   358       Variable.variant_fixes ["depth", "genuine_only"] ctxt'
   359     val depth = Free (depth_name, @{typ natural})
   360     val genuine_only = Free (genuine_only_name, @{typ bool}) 
   361     val return = mk_return (HOLogic.mk_list @{typ "term"}
   362         (map (fn t => HOLogic.mk_term_of (fastype_of t) t) frees @ map mk_safe_term eval_terms))
   363     fun mk_exhaustive_closure (free as Free (_, T)) t =
   364       Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T)
   365         $ lambda free t $ depth
   366     val none_t = Const (@{const_name "None"}, resultT)
   367     val mk_if = Quickcheck_Common.mk_safe_if genuine_only none_t
   368     fun mk_let safe def v_opt t e = mk_safe_let_expr genuine_only none_t safe (the_default def v_opt, t, e)
   369     val mk_test_term = mk_test_term lookup mk_exhaustive_closure mk_if mk_let none_t return ctxt
   370   in lambda genuine_only (lambda depth (mk_test_term t)) end
   371 
   372 fun mk_full_generator_expr ctxt (t, eval_terms) =
   373   let
   374     val thy = Proof_Context.theory_of ctxt
   375     val ctxt' = Variable.auto_fixes t ctxt
   376     val names = Term.add_free_names t []
   377     val frees = map Free (Term.add_frees t [])
   378     val ([depth_name, genuine_only_name], ctxt'') =
   379       Variable.variant_fixes ["depth", "genuine_only"] ctxt'
   380     val (term_names, _) = Variable.variant_fixes (map (prefix "t_") names) ctxt''
   381     val depth = Free (depth_name, @{typ natural})
   382     val genuine_only = Free (genuine_only_name, @{typ bool})    
   383     val term_vars = map (fn n => Free (n, @{typ "unit => term"})) term_names
   384     fun lookup v = the (AList.lookup (op =) (names ~~ (frees ~~ term_vars)) v)
   385     val return = mk_return (HOLogic.mk_list @{typ term}
   386           (map (fn v => v $ @{term "()"}) term_vars @ map mk_safe_term eval_terms))
   387     fun mk_exhaustive_closure (free as Free (_, T), term_var) t =
   388       if Sign.of_sort thy (T, @{sort check_all}) then
   389         Const (@{const_name "Quickcheck_Exhaustive.check_all_class.check_all"}, check_allT T)
   390           $ (HOLogic.case_prod_const (T, @{typ "unit => term"}, resultT)
   391             $ lambda free (lambda term_var t))
   392       else
   393         Const (@{const_name "Quickcheck_Exhaustive.full_exhaustive_class.full_exhaustive"}, full_exhaustiveT T)
   394           $ (HOLogic.case_prod_const (T, @{typ "unit => term"}, resultT)
   395             $ lambda free (lambda term_var t)) $ depth
   396     val none_t = Const (@{const_name "None"}, resultT)
   397     val mk_if = Quickcheck_Common.mk_safe_if genuine_only none_t
   398     fun mk_let safe _ (SOME (v, term_var)) t e =
   399         mk_safe_let_expr genuine_only none_t safe (v, t, 
   400           e #> subst_free [(term_var, absdummy @{typ unit} (mk_safe_term t))])
   401       | mk_let safe v NONE t e = mk_safe_let_expr genuine_only none_t safe (v, t, e)
   402     val mk_test_term = mk_test_term lookup mk_exhaustive_closure mk_if mk_let none_t return ctxt
   403   in lambda genuine_only (lambda depth (mk_test_term t)) end
   404 
   405 fun mk_parametric_generator_expr mk_generator_expr =
   406   Quickcheck_Common.gen_mk_parametric_generator_expr 
   407     ((mk_generator_expr,
   408       absdummy @{typ bool} (absdummy @{typ natural} (Const (@{const_name "None"}, resultT)))),
   409       @{typ bool} --> @{typ natural} --> resultT)
   410 
   411 fun mk_validator_expr ctxt t =
   412   let
   413     fun bounded_forallT T = (T --> @{typ bool}) --> @{typ natural} --> @{typ bool}
   414     val ctxt' = Variable.auto_fixes t ctxt
   415     val names = Term.add_free_names t []
   416     val frees = map Free (Term.add_frees t [])
   417     fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
   418     val ([depth_name], _) = Variable.variant_fixes ["depth"] ctxt'
   419     val depth = Free (depth_name, @{typ natural})
   420     fun mk_bounded_forall (Free (s, T)) t =
   421       Const (@{const_name "Quickcheck_Exhaustive.bounded_forall_class.bounded_forall"}, bounded_forallT T)
   422         $ lambda (Free (s, T)) t $ depth
   423     fun mk_safe_if (cond, then_t, else_t) genuine = mk_if (cond, then_t, else_t genuine)
   424     fun mk_let _ def v_opt t e = mk_let_expr (the_default def v_opt, t, e)
   425     val mk_test_term =
   426       mk_test_term lookup mk_bounded_forall mk_safe_if mk_let @{term True} (K @{term False}) ctxt
   427   in lambda depth (mk_test_term t) end
   428 
   429 
   430 fun mk_bounded_forall_generator_expr ctxt (t, eval_terms) = 
   431   let
   432     val frees = Term.add_free_names t []
   433     val dummy_term = @{term "Code_Evaluation.Const (STR ''Pure.dummy_pattern'')
   434       (Typerep.Typerep (STR ''dummy'') [])"}
   435     val return = @{term "Some :: term list => term list option"} $
   436       (HOLogic.mk_list @{typ "term"}
   437         (replicate (length frees + length eval_terms) dummy_term))
   438     val wrap = absdummy @{typ bool}
   439       (@{term "If :: bool => term list option => term list option => term list option"} $
   440         Bound 0 $ @{term "None :: term list option"} $ return)
   441   in HOLogic.mk_comp (wrap, mk_validator_expr ctxt t) end
   442   
   443 
   444 (** generator compiliation **)
   445 
   446 structure Data = Proof_Data
   447 (
   448   type T =
   449     (unit -> Code_Numeral.natural -> bool ->
   450       Code_Numeral.natural -> (bool * term list) option) *
   451     (unit -> (Code_Numeral.natural -> term list option) list) *
   452     (unit -> (Code_Numeral.natural -> bool) list);
   453   val empty: T =
   454    (fn () => raise Fail "counterexample",
   455     fn () => raise Fail "counterexample_batch",
   456     fn () => raise Fail "validator_batch");
   457   fun init _ = empty;
   458 );
   459 
   460 val get_counterexample = #1 o Data.get;
   461 val get_counterexample_batch = #2 o Data.get;
   462 val get_validator_batch = #3 o Data.get;
   463 
   464 val put_counterexample = Data.map o @{apply 3(1)} o K;
   465 val put_counterexample_batch = Data.map o @{apply 3(2)} o K;
   466 val put_validator_batch = Data.map o @{apply 3(3)} o K;
   467 
   468 val target = "Quickcheck";
   469 
   470 fun compile_generator_expr_raw ctxt ts =
   471   let
   472     val mk_generator_expr = 
   473       if Config.get ctxt fast then mk_fast_generator_expr
   474       else if Config.get ctxt bounded_forall then mk_bounded_forall_generator_expr
   475       else if Config.get ctxt full_support then mk_full_generator_expr else mk_generator_expr
   476     val t' = mk_parametric_generator_expr mk_generator_expr ctxt ts;
   477     val compile = Code_Runtime.dynamic_value_strict
   478       (get_counterexample, put_counterexample, "Exhaustive_Generators.put_counterexample")
   479       ctxt (SOME target) (fn proc => fn g =>
   480         fn card => fn genuine_only => fn size => g card genuine_only size
   481           |> (Option.map o apsnd o map) proc) t' []
   482   in
   483     fn genuine_only => fn [card, size] => rpair NONE (compile card genuine_only size |> 
   484       (if Config.get ctxt quickcheck_pretty then
   485         Option.map (apsnd (map Quickcheck_Common.post_process_term)) else I))
   486   end;
   487 
   488 fun compile_generator_expr ctxt ts =
   489   let
   490     val compiled = compile_generator_expr_raw ctxt ts;
   491   in fn genuine_only => fn [card, size] =>
   492     compiled genuine_only [Code_Numeral.natural_of_integer card, Code_Numeral.natural_of_integer size]
   493   end;
   494 
   495 fun compile_generator_exprs_raw ctxt ts =
   496   let
   497     val ts' = map (fn t => mk_generator_expr ctxt (t, [])) ts;
   498     val compiles = Code_Runtime.dynamic_value_strict
   499       (get_counterexample_batch, put_counterexample_batch,
   500         "Exhaustive_Generators.put_counterexample_batch")
   501       ctxt (SOME target) (fn proc => map (fn g => g #> (Option.map o map) proc))
   502       (HOLogic.mk_list @{typ "natural => term list option"} ts') []
   503   in
   504     map (fn compile => fn size => compile size
   505       |> (Option.map o map) Quickcheck_Common.post_process_term) compiles
   506   end;
   507 
   508 fun compile_generator_exprs ctxt ts =
   509   compile_generator_exprs_raw ctxt ts
   510   |> map (fn f => fn size => f (Code_Numeral.natural_of_integer size));
   511 
   512 fun compile_validator_exprs_raw ctxt ts =
   513   let
   514     val ts' = map (mk_validator_expr ctxt) ts
   515   in
   516     Code_Runtime.dynamic_value_strict
   517       (get_validator_batch, put_validator_batch, "Exhaustive_Generators.put_validator_batch")
   518       ctxt (SOME target) (K I) (HOLogic.mk_list @{typ "natural => bool"} ts') []
   519   end;
   520 
   521 fun compile_validator_exprs ctxt ts =
   522   compile_validator_exprs_raw ctxt ts
   523   |> map (fn f => fn size => f (Code_Numeral.natural_of_integer size));
   524 
   525 fun size_matters_for thy Ts = not (forall (fn T => Sign.of_sort thy (T,  @{sort check_all})) Ts)
   526 
   527 val test_goals =
   528   Quickcheck_Common.generator_test_goal_terms ("exhaustive", (size_matters_for, compile_generator_expr));
   529   
   530 (* setup *)
   531 
   532 val setup_exhaustive_datatype_interpretation =
   533   Quickcheck_Common.datatype_interpretation @{plugin quickcheck_exhaustive}
   534     (@{sort exhaustive}, instantiate_exhaustive_datatype)
   535 
   536 val setup_bounded_forall_datatype_interpretation =
   537   BNF_LFP_Compat.interpretation @{plugin quickcheck_bounded_forall} Quickcheck_Common.compat_prefs
   538     (Quickcheck_Common.ensure_sort
   539        (((@{sort type}, @{sort type}), @{sort bounded_forall}),
   540        (fn thy => BNF_LFP_Compat.the_descr thy Quickcheck_Common.compat_prefs,
   541         instantiate_bounded_forall_datatype)))
   542 
   543 val active = Attrib.setup_config_bool @{binding quickcheck_exhaustive_active} (K true);
   544 
   545 val _ =
   546   Theory.setup
   547    (Quickcheck_Common.datatype_interpretation @{plugin quickcheck_full_exhaustive}
   548       (@{sort full_exhaustive}, instantiate_full_exhaustive_datatype)
   549     #> Context.theory_map (Quickcheck.add_tester ("exhaustive", (active, test_goals)))
   550     #> Context.theory_map (Quickcheck.add_batch_generator ("exhaustive", compile_generator_exprs))
   551     #> Context.theory_map (Quickcheck.add_batch_validator ("exhaustive", compile_validator_exprs)));
   552 
   553 end;