src/HOL/Tools/Quickcheck/random_generators.ML
author bulwahn
Fri, 18 Mar 2011 18:19:42 +0100
changeset 42028 bd6515e113b2
parent 42027 5e40c152c396
child 42159 234ec7011e5d
permissions -rw-r--r--
passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41921
diff changeset
     1
(*  Title:      HOL/Tools/Quickcheck/random_generators.ML
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 37591
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
     3
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41921
diff changeset
     4
Random generators for various types.
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
     5
*)
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
     6
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41921
diff changeset
     7
signature RANDOM_GENERATORS =
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
     8
sig
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)
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33205
diff changeset
    12
    -> seed -> (('a -> 'b) * (unit -> term)) * seed
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
    13
  val compile_generator_expr:
42027
5e40c152c396 extending the test data generators to take the evaluation terms as arguments
bulwahn
parents: 41928
diff changeset
    14
    Proof.context -> term * term list -> int -> term list option * Quickcheck.report option
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
    15
  val put_counterexample: (unit -> int -> seed -> term list option * seed)
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
    16
    -> Proof.context -> Proof.context
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
    17
  val put_counterexample_report: (unit -> int -> seed -> (term list option * (bool list * bool)) * seed)
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
    18
    -> Proof.context -> Proof.context
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    19
  val setup: theory -> theory
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    20
end;
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    21
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41921
diff changeset
    22
structure Random_Generators : RANDOM_GENERATORS =
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    23
struct
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    24
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
    25
(** abstract syntax **)
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    26
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
    27
fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
    28
val size = @{term "i::code_numeral"};
31984
haftmann
parents: 31950
diff changeset
    29
val size_pred = @{term "(i::code_numeral) - 1"};
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
    30
val size' = @{term "j::code_numeral"};
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
    31
val seed = @{term "s::Random.seed"};
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    32
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    33
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    34
(** typ "'a => 'b" **)
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    35
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    36
type seed = Random_Engine.seed;
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    37
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
    38
fun random_fun T1 T2 eq term_of random random_split seed =
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    39
  let
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    40
    val fun_upd = Const (@{const_name fun_upd},
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    41
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
32344
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 31984
diff changeset
    42
    val ((y, t2), seed') = random seed;
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 31984
diff changeset
    43
    val (seed'', seed''') = random_split seed';
31933
cd6511035315 proper closures -- imperative programming considered harmful...
haftmann
parents: 31902
diff changeset
    44
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32657
diff changeset
    45
    val state = Unsynchronized.ref (seed'', [], fn () => Abs ("x", T1, t2 ()));
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    46
    fun random_fun' x =
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    47
      let
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    48
        val (seed, fun_map, f_t) = ! state;
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    49
      in case AList.lookup (uncurry eq) fun_map x
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    50
       of SOME y => y
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    51
        | NONE => let
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    52
              val t1 = term_of x;
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    53
              val ((y, t2), seed') = random seed;
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    54
              val fun_map' = (x, y) :: fun_map;
31933
cd6511035315 proper closures -- imperative programming considered harmful...
haftmann
parents: 31902
diff changeset
    55
              val f_t' = fn () => fun_upd $ f_t () $ t1 $ t2 ();
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    56
              val _ = state := (seed', fun_map', f_t');
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    57
            in y end
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    58
      end;
31933
cd6511035315 proper closures -- imperative programming considered harmful...
haftmann
parents: 31902
diff changeset
    59
    fun term_fun' () = #3 (! state) ();
32344
55ca0df19af5 more appropriate printing of function terms
haftmann
parents: 31984
diff changeset
    60
  in ((random_fun', term_fun'), seed''') end;
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    61
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    62
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    63
(** datatypes **)
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
    64
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    65
(* definitional scheme for random instances on datatypes *)
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    66
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    67
local
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    68
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    69
fun dest_ctyp_nth k cT = nth (Thm.dest_ctyp cT) k;
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    70
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
    71
val lhs = eq |> Thm.dest_arg1;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    72
val pt_random_aux = lhs |> Thm.dest_fun;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    73
val ct_k = lhs |> Thm.dest_arg;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    74
val pt_rhs = eq |> Thm.dest_arg |> Thm.dest_fun;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    75
val aT = pt_random_aux |> Thm.ctyp_of_term |> dest_ctyp_nth 1;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    76
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    77
val rew_thms = map mk_meta_eq [@{thm code_numeral_zero_minus_one},
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    78
  @{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
    79
val rew_ts = map (Logic.dest_equals o Thm.prop_of) rew_thms;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    80
val rew_ss = HOL_ss addsimps rew_thms;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    81
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    82
in
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    83
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    84
fun random_aux_primrec eq lthy =
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    85
  let
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    86
    val thy = ProofContext.theory_of lthy;
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    87
    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
    88
      (HOLogic.dest_eq o HOLogic.dest_Trueprop) eq;
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    89
    val Type (_, [_, iT]) = T;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    90
    val icT = Thm.ctyp_of thy iT;
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    91
    val cert = Thm.cterm_of thy;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    92
    val inst = Thm.instantiate_cterm ([(aT, icT)], []);
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
    93
    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
    94
    val t_rhs = lambda t_k proto_t_rhs;
31785
9db4e79c91cf tuned proof
haftmann
parents: 31784
diff changeset
    95
    val eqs0 = [subst_v @{term "0::code_numeral"} eq,
9db4e79c91cf tuned proof
haftmann
parents: 31784
diff changeset
    96
      subst_v (@{term "Suc_code_numeral"} $ t_k) eq];
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
    97
    val eqs1 = map (Pattern.rewrite_term thy rew_ts []) eqs0;
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34969
diff changeset
    98
    val ((_, (_, eqs2)), lthy') = Primrec.add_primrec_simple
33205
20587741a8d9 conceal quickcheck generators
haftmann
parents: 33087
diff changeset
    99
      [((Binding.conceal (Binding.name random_aux), T), NoSyn)] eqs1 lthy;
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
   100
    val cT_random_aux = inst pt_random_aux;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
   101
    val cT_rhs = inst pt_rhs;
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   102
    val rule = @{thm random_aux_rec}
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
   103
      |> Drule.instantiate ([(aT, icT)],
31785
9db4e79c91cf tuned proof
haftmann
parents: 31784
diff changeset
   104
           [(cT_random_aux, cert t_random_aux), (cT_rhs, cert t_rhs)]);
9db4e79c91cf tuned proof
haftmann
parents: 31784
diff changeset
   105
    val tac = ALLGOALS (rtac rule)
9db4e79c91cf tuned proof
haftmann
parents: 31784
diff changeset
   106
      THEN ALLGOALS (simp_tac rew_ss)
35166
a57ef2cd2236 tuned primrec signature: return definienda
haftmann
parents: 34969
diff changeset
   107
      THEN (ALLGOALS (ProofContext.fact_tac eqs2))
32970
fbd2bb2489a8 operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents: 32740
diff changeset
   108
    val simp = Skip_Proof.prove lthy' [v] [] eq (K tac);
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   109
  in (simp, lthy') end;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   110
31611
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
   111
end;
a577f77af93f explicit instantiation yields considerable speedup
haftmann
parents: 31609
diff changeset
   112
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   113
fun random_aux_primrec_multi auxname [eq] lthy =
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   114
      lthy
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   115
      |> random_aux_primrec eq
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   116
      |>> (fn simp => [simp])
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   117
  | random_aux_primrec_multi auxname (eqs as _ :: _ :: _) lthy =
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   118
      let
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   119
        val thy = ProofContext.theory_of lthy;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   120
        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
   121
        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
   122
        val Ts = map fastype_of lhss;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   123
        val tupleT = foldr1 HOLogic.mk_prodT Ts;
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   124
        val aux_lhs = Free ("mutual_" ^ auxname, fastype_of arg --> tupleT) $ arg;
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   125
        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
   126
          (aux_lhs, foldr1 HOLogic.mk_prod rhss);
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   127
        fun mk_proj t [T] = [t]
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   128
          | mk_proj t (Ts as T :: (Ts' as _ :: _)) =
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   129
              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
   130
                :: mk_proj (Const (@{const_name snd},
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   131
                  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
   132
        val projs = mk_proj (aux_lhs) Ts;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   133
        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
   134
        val proj_defs = map2 (fn Free (name, _) => fn (_, rhs) =>
33205
20587741a8d9 conceal quickcheck generators
haftmann
parents: 33087
diff changeset
   135
          ((Binding.conceal (Binding.name name), NoSyn),
33280
wenzelm
parents: 33243
diff changeset
   136
            (apfst Binding.conceal Attrib.empty_binding, rhs))) vs proj_eqs;
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   137
        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
   138
        fun prove_eqs aux_simp proj_defs lthy = 
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   139
          let
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   140
            val proj_simps = map (snd o snd) proj_defs;
31645
98a3fd346270 made SML/NJ happy;
wenzelm
parents: 31628
diff changeset
   141
            fun tac { context = ctxt, prems = _ } =
31625
9e4d7d60c3e7 using SkipProof where appropriate
haftmann
parents: 31623
diff changeset
   142
              ALLGOALS (simp_tac (HOL_ss addsimps proj_simps))
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   143
              THEN ALLGOALS (EqSubst.eqsubst_tac ctxt [0] [aux_simp])
37136
e0c9d3e49e15 dropped legacy theorem bindings
haftmann
parents: 35845
diff changeset
   144
              THEN ALLGOALS (simp_tac (HOL_ss addsimps [@{thm fst_conv}, @{thm snd_conv}]));
32970
fbd2bb2489a8 operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents: 32740
diff changeset
   145
          in (map (fn prop => Skip_Proof.prove lthy [v] [] prop tac) eqs, lthy) end;
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   146
      in
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   147
        lthy
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   148
        |> random_aux_primrec aux_eq'
33766
c679f05600cd adapted Local_Theory.define -- eliminated odd thm kind;
wenzelm
parents: 33671
diff changeset
   149
        ||>> fold_map Local_Theory.define proj_defs
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   150
        |-> (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
   151
      end;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   152
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   153
fun random_aux_specification prfx name eqs lthy =
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   154
  let
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   155
    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
   156
      o HOLogic.dest_Trueprop o hd) eqs) [];
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   157
    fun mk_proto_eq eq =
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   158
      let
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   159
        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
   160
      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
   161
    val proto_eqs = map mk_proto_eq eqs;
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   162
    fun prove_simps proto_simps lthy =
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   163
      let
31625
9e4d7d60c3e7 using SkipProof where appropriate
haftmann
parents: 31623
diff changeset
   164
        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
   165
        val tac = ALLGOALS (ProofContext.fact_tac ext_simps);
32970
fbd2bb2489a8 operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents: 32740
diff changeset
   166
      in (map (fn prop => Skip_Proof.prove lthy vs [] prop (K tac)) eqs, lthy) end;
33205
20587741a8d9 conceal quickcheck generators
haftmann
parents: 33087
diff changeset
   167
    val b = Binding.conceal (Binding.qualify true prfx
20587741a8d9 conceal quickcheck generators
haftmann
parents: 33087
diff changeset
   168
      (Binding.qualify true name (Binding.name "simps")));
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   169
  in
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   170
    lthy
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   171
    |> random_aux_primrec_multi (name ^ prfx) proto_eqs
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   172
    |-> (fn proto_simps => prove_simps proto_simps)
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33670
diff changeset
   173
    |-> (fn simps => Local_Theory.note
33666
e49bfeb0d822 eliminated obsolete "generated" kind -- collapsed to unspecific "" (definitely unused according to Lukas Bulwahn);
wenzelm
parents: 33553
diff changeset
   174
      ((b, Code.add_default_eqn_attrib :: map (Attrib.internal o K)
33552
506f80a9afe8 removed unused Quickcheck_RecFun_Simps;
wenzelm
parents: 33280
diff changeset
   175
          [Simplifier.simp_add, Nitpick_Simps.add]), simps))
31485
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   176
    |> snd
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   177
  end
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   178
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   179
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   180
(* constructing random instances on datatypes *)
259a3c90016e added infrastructure for definitorial construction of generators for datatypes
haftmann
parents: 31260
diff changeset
   181
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   182
val random_auxN = "random_aux";
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   183
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   184
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
   185
  let
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   186
    val mk_const = curry (Sign.mk_const thy);
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   187
    val random_auxsN = map (prefix (random_auxN ^ "_")) (names @ auxnames);
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   188
    val rTs = Ts @ Us;
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   189
    fun random_resultT T = @{typ Random.seed}
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   190
      --> HOLogic.mk_prodT (termifyT T,@{typ Random.seed});
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   191
    val pTs = map random_resultT rTs;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   192
    fun sizeT T = @{typ code_numeral} --> @{typ code_numeral} --> T;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   193
    val random_auxT = sizeT o random_resultT;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   194
    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
   195
      random_auxsN rTs;
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   196
    fun mk_random_call T = (NONE, (HOLogic.mk_random T size', T));
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   197
    fun mk_random_aux_call fTs (k, _) (tyco, Ts) =
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   198
      let
31623
b72c11302b39 quickcheck generators for datatypes with functions
haftmann
parents: 31611
diff changeset
   199
        val T = Type (tyco, Ts);
b72c11302b39 quickcheck generators for datatypes with functions
haftmann
parents: 31611
diff changeset
   200
        fun mk_random_fun_lift [] t = t
b72c11302b39 quickcheck generators for datatypes with functions
haftmann
parents: 31611
diff changeset
   201
          | mk_random_fun_lift (fT :: fTs) t =
b72c11302b39 quickcheck generators for datatypes with functions
haftmann
parents: 31611
diff changeset
   202
              mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
b72c11302b39 quickcheck generators for datatypes with functions
haftmann
parents: 31611
diff changeset
   203
                mk_random_fun_lift fTs t;
31984
haftmann
parents: 31950
diff changeset
   204
        val t = mk_random_fun_lift fTs (nth random_auxs k $ size_pred $ size');
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33766
diff changeset
   205
        val size = Option.map snd (Datatype_Aux.find_shortest_path descr k)
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   206
          |> the_default 0;
31623
b72c11302b39 quickcheck generators for datatypes with functions
haftmann
parents: 31611
diff changeset
   207
      in (SOME size, (t, fTs ---> T)) end;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33766
diff changeset
   208
    val tss = Datatype_Aux.interpret_construction descr vs
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   209
      { atyp = mk_random_call, dtyp = mk_random_aux_call };
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   210
    fun mk_consexpr simpleT (c, xs) =
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   211
      let
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   212
        val (ks, simple_tTs) = split_list xs;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   213
        val T = termifyT simpleT;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   214
        val tTs = (map o apsnd) termifyT simple_tTs;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   215
        val is_rec = exists is_some ks;
33029
2fefe039edf1 uniform use of Integer.min/max;
wenzelm
parents: 32970
diff changeset
   216
        val k = fold (fn NONE => I | SOME k => Integer.max k) ks 0;
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   217
        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
   218
        val tc = HOLogic.mk_return T @{typ Random.seed}
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   219
          (HOLogic.mk_valtermify_app c vs simpleT);
38543
6a65b92edf5e tuned signature and code
haftmann
parents: 38393
diff changeset
   220
        val t = HOLogic.mk_ST
6a65b92edf5e tuned signature and code
haftmann
parents: 38393
diff changeset
   221
          (map2 (fn (t, _) => fn (v, T') => ((t, @{typ Random.seed}), SOME ((v, termifyT T')))) tTs vs)
6a65b92edf5e tuned signature and code
haftmann
parents: 38393
diff changeset
   222
            tc @{typ Random.seed} (SOME T, @{typ Random.seed});
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   223
        val tk = if is_rec
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   224
          then if k = 0 then size
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   225
            else @{term "Quickcheck.beyond :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   226
             $ HOLogic.mk_number @{typ code_numeral} k $ size
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   227
          else @{term "1::code_numeral"}
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   228
      in (is_rec, HOLogic.mk_prod (tk, t)) end;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   229
    fun sort_rec xs =
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   230
      map_filter (fn (true, t) => SOME t | _ =>  NONE) xs
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   231
      @ map_filter (fn (false, t) => SOME t | _ =>  NONE) xs;
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   232
    val gen_exprss = tss
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   233
      |> (map o apfst) Type
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   234
      |> 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
   235
    fun mk_select (rT, xs) =
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   236
      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
   237
      $ (mk_const @{const_name Random.select_weight} [random_resultT rT]
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   238
        $ 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
   239
          $ seed;
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   240
    val auxs_lhss = map (fn t => t $ size $ size' $ seed) random_auxs;
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   241
    val auxs_rhss = map mk_select gen_exprss;
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   242
  in (random_auxs, auxs_lhss ~~ auxs_rhss) end;
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   243
38543
6a65b92edf5e tuned signature and code
haftmann
parents: 38393
diff changeset
   244
fun instantiate_random_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   245
  let
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33766
diff changeset
   246
    val _ = Datatype_Aux.message config "Creating quickcheck generators ...";
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   247
    val mk_prop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
33968
f94fb13ecbb3 modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents: 33766
diff changeset
   248
    fun mk_size_arg k = case Datatype_Aux.find_shortest_path descr k
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   249
     of SOME (_, l) => if l = 0 then size
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   250
          else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   251
            $ HOLogic.mk_number @{typ code_numeral} l $ size
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   252
      | NONE => size;
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   253
    val (random_auxs, auxs_eqs) = (apsnd o map) mk_prop_eq
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31595
diff changeset
   254
      (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
   255
    val random_defs = map_index (fn (k, T) => mk_prop_eq
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   256
      (HOLogic.mk_random T size, nth random_auxs k $ mk_size_arg k $ size)) Ts;
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   257
  in
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   258
    thy
38348
cf7b2121ad9d moved instantiation target formally to class_target.ML
haftmann
parents: 37744
diff changeset
   259
    |> Class.instantiation (tycos, vs, @{sort random})
31868
edd1f30c0477 canonical prefix for datatype derivates
haftmann
parents: 31785
diff changeset
   260
    |> random_aux_specification prfx random_auxN auxs_eqs
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   261
    |> `(fn lthy => map (Syntax.check_term lthy) random_defs)
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   262
    |-> (fn random_defs' => fold_map (fn random_def =>
33280
wenzelm
parents: 33243
diff changeset
   263
          Specification.definition (NONE, (apfst Binding.conceal
33205
20587741a8d9 conceal quickcheck generators
haftmann
parents: 33087
diff changeset
   264
            Attrib.empty_binding, random_def))) random_defs')
31595
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   265
    |> snd
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   266
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   267
  end;
bd2f7211a420 first running version of qc generators for datatypes
haftmann
parents: 31485
diff changeset
   268
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   269
(** building and compiling generator expressions **)
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   270
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   271
(* FIXME just one data slot (record) per program unit *)
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   272
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   273
structure Counterexample = Proof_Data
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   274
(
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   275
  type T = unit -> int -> int * int -> term list option * (int * int)
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   276
  (* FIXME avoid user error with non-user text *)
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   277
  fun init _ () = error "Counterexample"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   278
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   279
val put_counterexample = Counterexample.put;
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   280
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   281
structure Counterexample_Report = Proof_Data
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   282
(
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   283
  type T = unit -> int -> seed -> (term list option * (bool list * bool)) * seed
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 40911
diff changeset
   284
  (* FIXME avoid user error with non-user text *)
39388
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   285
  fun init _ () = error "Counterexample_Report"
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   286
);
fdbb2c55ffc2 replaced ML_Context.evaluate by ML_Context.value -- using context data instead of bare metal references
haftmann
parents: 39253
diff changeset
   287
val put_counterexample_report = Counterexample_Report.put;
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   288
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   289
val target = "Quickcheck";
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   290
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   291
fun mk_generator_expr thy prop Ts =
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   292
  let
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   293
    val bound_max = length Ts - 1;
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   294
    val bounds = map_index (fn (i, ty) =>
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   295
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts;
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   296
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   297
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   298
    val check = @{term "If :: bool => term list option => term list option => term list option"}
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   299
      $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option"} $ terms);
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   300
    val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   301
    fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   302
    fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   303
    fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   304
      liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   305
    fun mk_split T = Sign.mk_const thy
37591
d3daea901123 merged constants "split" and "prod_case"
haftmann
parents: 37136
diff changeset
   306
      (@{const_name prod_case}, [T, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   307
    fun mk_scomp_split T t t' =
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   308
      mk_scomp (mk_termtyp T) @{typ "term list option"} @{typ Random.seed} t
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   309
        (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   310
    fun mk_bindclause (_, _, i, T) = mk_scomp_split T
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   311
      (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   312
  in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   313
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   314
fun mk_reporting_generator_expr thy prop Ts =
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   315
  let
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   316
    val bound_max = length Ts - 1;
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   317
    val bounds = map_index (fn (i, ty) =>
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   318
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts;
38786
e46e7a9cb622 formerly unnamed infix impliciation now named HOL.implies
haftmann
parents: 38553
diff changeset
   319
    fun strip_imp (Const(@{const_name HOL.implies},_) $ A $ B) = apfst (cons A) (strip_imp B)
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   320
      | strip_imp A = ([], A)
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   321
    val prop' = betapplys (prop, map (fn (i, _, _, _) => Bound i) bounds);
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   322
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds)
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   323
    val (assms, concl) = strip_imp prop'
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   324
    val return =
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   325
      @{term "Pair :: term list option * (bool list * bool) => Random.seed => (term list option * (bool list * bool)) * Random.seed"};
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   326
    fun mk_assms_report i =
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   327
      HOLogic.mk_prod (@{term "None :: term list option"},
38553
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 38549
diff changeset
   328
        HOLogic.mk_prod (HOLogic.mk_list HOLogic.boolT
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 38549
diff changeset
   329
          (replicate i @{term True} @ replicate (length assms - i) @{term False}),
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 38549
diff changeset
   330
        @{term False}))
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   331
    fun mk_concl_report b =
38553
56965d8e4e11 use HOLogic.boolT and @{typ bool} more pervasively
haftmann
parents: 38549
diff changeset
   332
      HOLogic.mk_prod (HOLogic.mk_list HOLogic.boolT (replicate (length assms) @{term True}),
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   333
        if b then @{term True} else @{term False})
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   334
    val If =
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   335
      @{term "If :: bool => term list option * (bool list * bool) => term list option * (bool list * bool) => term list option * (bool list * bool)"}
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   336
    val concl_check = If $ concl $
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   337
      HOLogic.mk_prod (@{term "None :: term list option"}, mk_concl_report true) $
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   338
      HOLogic.mk_prod (@{term "Some :: term list  => term list option"} $ terms, mk_concl_report false)
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   339
    val check = fold_rev (fn (i, assm) => fn t => If $ assm $ t $ mk_assms_report i)
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   340
      (map_index I assms) concl_check
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   341
    fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   342
    fun mk_termtyp T = HOLogic.mk_prodT (T, @{typ "unit => term"});
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   343
    fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   344
      liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   345
    fun mk_split T = Sign.mk_const thy
37591
d3daea901123 merged constants "split" and "prod_case"
haftmann
parents: 37136
diff changeset
   346
      (@{const_name prod_case}, [T, @{typ "unit => term"},
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   347
        liftT @{typ "term list option * (bool list * bool)"} @{typ Random.seed}]);
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   348
    fun mk_scomp_split T t t' =
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   349
      mk_scomp (mk_termtyp T) @{typ "term list option * (bool list * bool)"} @{typ Random.seed} t
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   350
        (mk_split T $ Abs ("", T, Abs ("", @{typ "unit => term"}, t')));
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   351
    fun mk_bindclause (_, _, i, T) = mk_scomp_split T
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   352
      (Sign.mk_const thy (@{const_name Quickcheck.random}, [T]) $ Bound i);
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   353
  in
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   354
    Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check))
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   355
  end
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   356
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   357
(* single quickcheck report *)
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   358
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   359
datatype single_report = Run of bool list * bool | MatchExc
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   360
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   361
fun collect_single_report single_report
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   362
    (Quickcheck.Report {iterations = iterations, raised_match_errors = raised_match_errors,
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   363
    satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   364
  case single_report
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   365
  of MatchExc =>
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   366
    Quickcheck.Report {iterations = iterations + 1, raised_match_errors = raised_match_errors + 1,
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   367
      satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   368
   | Run (assms, concl) =>
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   369
    Quickcheck.Report {iterations = iterations + 1, raised_match_errors = raised_match_errors,
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   370
      satisfied_assms =
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   371
        map2 (fn b => fn s => if b then s + 1 else s) assms
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   372
         (if null satisfied_assms then replicate (length assms) 0 else satisfied_assms),
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   373
      positive_concl_tests = if concl then positive_concl_tests + 1 else positive_concl_tests}
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   374
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   375
val empty_report = Quickcheck.Report { iterations = 0, raised_match_errors = 0,
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   376
  satisfied_assms = [], positive_concl_tests = 0 }
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   377
    
42027
5e40c152c396 extending the test data generators to take the evaluation terms as arguments
bulwahn
parents: 41928
diff changeset
   378
fun compile_generator_expr ctxt (t, eval_terms) =
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   379
  let
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   380
    val t' = list_abs_free (Term.add_frees t [], t)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   381
    val Ts = (map snd o fst o strip_abs) t';
39253
0c47d615a69b removing report from the arguments of the quickcheck functions and refering to it by picking it from the context
bulwahn
parents: 39252
diff changeset
   382
    val thy = ProofContext.theory_of ctxt
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   383
    val iterations = Config.get ctxt Quickcheck.iterations
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   384
  in
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   385
    if Config.get ctxt Quickcheck.report then
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   386
      let
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   387
        val t'' = mk_reporting_generator_expr thy t' Ts;
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   388
        val compile = Code_Runtime.dynamic_value_strict
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41921
diff changeset
   389
          (Counterexample_Report.get, put_counterexample_report, "Random_Generators.put_counterexample_report")
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   390
          thy (SOME target) (fn proc => fn g => fn s => g s #>> (apfst o Option.map o map) proc) t'' [];
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   391
        val single_tester = compile #> Random_Engine.run
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   392
        fun iterate_and_collect size 0 report = (NONE, report)
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   393
          | iterate_and_collect size j report =
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   394
            let
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   395
              val (test_result, single_report) = apsnd Run (single_tester size) handle Match => 
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   396
                (if Config.get ctxt Quickcheck.quiet then ()
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   397
                 else warning "Exception Match raised during quickcheck"; (NONE, MatchExc))
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   398
              val report = collect_single_report single_report report
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   399
            in
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   400
              case test_result of NONE => iterate_and_collect size (j - 1) report
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   401
                | SOME q => (SOME q, report)
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   402
            end
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   403
      in
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   404
        fn size => apsnd SOME (iterate_and_collect size iterations empty_report)
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   405
      end
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   406
    else
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   407
      let
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   408
        val t'' = mk_generator_expr thy t' Ts;
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   409
        val compile = Code_Runtime.dynamic_value_strict
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41921
diff changeset
   410
          (Counterexample.get, put_counterexample, "Random_Generators.put_counterexample")
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   411
          thy (SOME target) (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) t'' [];
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   412
        val single_tester = compile #> Random_Engine.run
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   413
        fun iterate size 0 = NONE
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   414
          | iterate size j =
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   415
            let
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   416
              val result = single_tester size handle Match => 
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   417
                (if Config.get ctxt Quickcheck.quiet then ()
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   418
                 else warning "Exception Match raised during quickcheck"; NONE)
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   419
            in
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   420
              case result of NONE => iterate size (j - 1) | SOME q => SOME q
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   421
            end
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   422
      in
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   423
        fn size => (rpair NONE (iterate size iterations))
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40644
diff changeset
   424
      end
35378
95d0e3adf38e added basic reporting of test cases to quickcheck
bulwahn
parents: 35166
diff changeset
   425
  end;
31950
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   426
7300186d745a tuned code
haftmann
parents: 31933
diff changeset
   427
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
   428
(** setup **)
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
   429
38393
7c045c03598f group record-related ML files
haftmann
parents: 38348
diff changeset
   430
val setup =
41928
05abcee548a1 adaptions in generators using the common functions
bulwahn
parents: 41927
diff changeset
   431
  Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype (@{sort random}, instantiate_random_datatype))
39401
887f4218a39a code_eval renamed to code_runtime
haftmann
parents: 39388
diff changeset
   432
  #> Code_Target.extend_target (target, (Code_Runtime.target, K I))
39252
8f176e575a49 changing the container for the quickcheck options to a generic data
bulwahn
parents: 38786
diff changeset
   433
  #> Context.theory_map
40636
3bd9512ca486 renaming quickcheck generator code to random
bulwahn
parents: 39471
diff changeset
   434
    (Quickcheck.add_generator ("random", compile_generator_expr));
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
   435
4d273d043d59 separate module for quickcheck generators
haftmann
parents:
diff changeset
   436
end;