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