src/HOL/Quickcheck.thy
author haftmann
Thu May 21 09:07:25 2009 +0200 (2009-05-21)
changeset 31223 87bde6b5f793
parent 31211 ba0ad1c020ee
child 31245 f8dcca332d4e
permissions -rw-r--r--
re-added corrected version of type copy quickcheck generator
haftmann@29132
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@26265
     2
haftmann@26265
     3
header {* A simple counterexample generator *}
haftmann@26265
     4
haftmann@26265
     5
theory Quickcheck
haftmann@31203
     6
imports Random Code_Eval
haftmann@26265
     7
begin
haftmann@26265
     8
haftmann@31179
     9
notation fcomp (infixl "o>" 60)
haftmann@31179
    10
notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
    11
haftmann@31179
    12
haftmann@26265
    13
subsection {* The @{text random} class *}
haftmann@26265
    14
haftmann@28335
    15
class random = typerep +
haftmann@31205
    16
  fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@26265
    17
haftmann@26267
    18
haftmann@29808
    19
subsection {* Quickcheck generator *}
haftmann@29132
    20
haftmann@29132
    21
ML {*
haftmann@26265
    22
structure Quickcheck =
haftmann@26265
    23
struct
haftmann@26265
    24
haftmann@28309
    25
open Quickcheck;
haftmann@28309
    26
haftmann@26265
    27
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
haftmann@26265
    28
haftmann@30945
    29
val target = "Quickcheck";
haftmann@30945
    30
haftmann@26325
    31
fun mk_generator_expr thy prop tys =
haftmann@26265
    32
  let
haftmann@26325
    33
    val bound_max = length tys - 1;
haftmann@26325
    34
    val bounds = map_index (fn (i, ty) =>
haftmann@26325
    35
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
haftmann@26325
    36
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
haftmann@26325
    37
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
haftmann@26265
    38
    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
haftmann@26325
    39
      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
haftmann@31179
    40
    val return = @{term "Pair \<Colon> term list option \<Rightarrow> Random.seed \<Rightarrow> term list option \<times> Random.seed"};
haftmann@31179
    41
    fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
haftmann@26325
    42
    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
haftmann@31179
    43
    fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
haftmann@31179
    44
      liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
haftmann@26325
    45
    fun mk_split ty = Sign.mk_const thy
haftmann@31179
    46
      (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
haftmann@26589
    47
    fun mk_scomp_split ty t t' =
haftmann@31179
    48
      mk_scomp (mk_termtyp ty) @{typ "term list option"} @{typ Random.seed} t
haftmann@26325
    49
        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
haftmann@26589
    50
    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
haftmann@31179
    51
      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i);
haftmann@31205
    52
  in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
haftmann@26265
    53
haftmann@28309
    54
fun compile_generator_expr thy t =
haftmann@26265
    55
  let
haftmann@28309
    56
    val tys = (map snd o fst o strip_abs) t;
haftmann@28309
    57
    val t' = mk_generator_expr thy t tys;
haftmann@30970
    58
    val f = Code_ML.eval (SOME target) ("Quickcheck.eval_ref", eval_ref)
haftmann@31179
    59
      (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
haftmann@30970
    60
  in f #> Random_Engine.run end;
haftmann@26265
    61
haftmann@26265
    62
end
haftmann@26265
    63
*}
haftmann@26265
    64
haftmann@28309
    65
setup {*
haftmann@30945
    66
  Code_Target.extend_target (Quickcheck.target, (Code_ML.target_Eval, K I))
haftmann@30945
    67
  #> Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
haftmann@28309
    68
*}
haftmann@28309
    69
haftmann@30945
    70
haftmann@31179
    71
subsection {* Fundamental types*}
haftmann@31179
    72
haftmann@31179
    73
instantiation bool :: random
haftmann@31179
    74
begin
haftmann@31179
    75
haftmann@31179
    76
definition
haftmann@31194
    77
  "random i = Random.range i o\<rightarrow>
haftmann@31194
    78
    (\<lambda>k. Pair (if (k div 2 = 0) then Code_Eval.valtermify True else Code_Eval.valtermify False))"
haftmann@31179
    79
haftmann@31179
    80
instance ..
haftmann@31179
    81
haftmann@31179
    82
end
haftmann@31179
    83
haftmann@31179
    84
instantiation itself :: (typerep) random
haftmann@31179
    85
begin
haftmann@31179
    86
haftmann@31205
    87
definition random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31194
    88
  "random_itself _ = Pair (Code_Eval.valtermify TYPE('a))"
haftmann@31179
    89
haftmann@31179
    90
instance ..
haftmann@31179
    91
haftmann@31179
    92
end
haftmann@31179
    93
haftmann@31179
    94
text {* Type @{typ "'a \<Rightarrow> 'b"} *}
haftmann@30945
    95
haftmann@30945
    96
ML {*
haftmann@30945
    97
structure Random_Engine =
haftmann@30945
    98
struct
haftmann@30945
    99
haftmann@30945
   100
open Random_Engine;
haftmann@30945
   101
haftmann@30945
   102
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
haftmann@30945
   103
    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
haftmann@30945
   104
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
haftmann@30945
   105
    (seed : Random_Engine.seed) =
haftmann@30945
   106
  let
haftmann@30945
   107
    val (seed', seed'') = random_split seed;
haftmann@30945
   108
    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
haftmann@30945
   109
    val fun_upd = Const (@{const_name fun_upd},
haftmann@30945
   110
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
haftmann@30945
   111
    fun random_fun' x =
haftmann@30945
   112
      let
haftmann@30945
   113
        val (seed, fun_map, f_t) = ! state;
haftmann@30945
   114
      in case AList.lookup (uncurry eq) fun_map x
haftmann@30945
   115
       of SOME y => y
haftmann@30945
   116
        | NONE => let
haftmann@30945
   117
              val t1 = term_of x;
haftmann@30945
   118
              val ((y, t2), seed') = random seed;
haftmann@30945
   119
              val fun_map' = (x, y) :: fun_map;
haftmann@30945
   120
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
haftmann@30945
   121
              val _ = state := (seed', fun_map', f_t');
haftmann@30945
   122
            in y end
haftmann@30945
   123
      end;
haftmann@30945
   124
    fun term_fun' () = #3 (! state);
haftmann@30945
   125
  in ((random_fun', term_fun'), seed'') end;
haftmann@30945
   126
haftmann@26265
   127
end
haftmann@30945
   128
*}
haftmann@30945
   129
haftmann@31179
   130
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
haftmann@31179
   131
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
haftmann@31179
   132
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@30945
   133
haftmann@30945
   134
code_const random_fun_aux (Quickcheck "Random'_Engine.random'_fun")
haftmann@30945
   135
  -- {* With enough criminal energy this can be abused to derive @{prop False};
haftmann@30945
   136
  for this reason we use a distinguished target @{text Quickcheck}
haftmann@30945
   137
  not spoiling the regular trusted code generation *}
haftmann@30945
   138
haftmann@30945
   139
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
haftmann@30945
   140
begin
haftmann@30945
   141
haftmann@31205
   142
definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31179
   143
  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) Random.split_seed"
haftmann@30945
   144
haftmann@30945
   145
instance ..
haftmann@30945
   146
haftmann@30945
   147
end
haftmann@30945
   148
haftmann@30945
   149
code_reserved Quickcheck Random_Engine
haftmann@30945
   150
haftmann@31179
   151
haftmann@31179
   152
subsection {* Numeric types *}
haftmann@31179
   153
haftmann@31179
   154
instantiation nat :: random
haftmann@31179
   155
begin
haftmann@31179
   156
haftmann@31205
   157
definition random_nat :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Eval.term)) \<times> Random.seed" where
haftmann@31194
   158
  "random_nat i = Random.range (i + 1) o\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
   159
     let n = Code_Numeral.nat_of k
haftmann@31194
   160
     in (n, \<lambda>_. Code_Eval.term_of n)))"
haftmann@31194
   161
haftmann@31194
   162
instance ..
haftmann@31194
   163
haftmann@31194
   164
end
haftmann@31194
   165
haftmann@31194
   166
instantiation int :: random
haftmann@31194
   167
begin
haftmann@31194
   168
haftmann@31194
   169
definition
haftmann@31194
   170
  "random i = Random.range (2 * i + 1) o\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
   171
     let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))
haftmann@31194
   172
     in (j, \<lambda>_. Code_Eval.term_of j)))"
haftmann@31179
   173
haftmann@31179
   174
instance ..
haftmann@31179
   175
haftmann@30945
   176
end
haftmann@31179
   177
haftmann@31223
   178
subsection {* Type copies *}
haftmann@31223
   179
haftmann@31223
   180
setup {*
haftmann@31223
   181
let
haftmann@31223
   182
haftmann@31223
   183
fun mk_random_typecopy tyco vs constr typ thy =
haftmann@31223
   184
  let
haftmann@31223
   185
    val Ts = map TFree vs;  
haftmann@31223
   186
    val T = Type (tyco, Ts);
haftmann@31223
   187
    fun mk_termifyT T = HOLogic.mk_prodT (T, @{typ "unit \<Rightarrow> term"})
haftmann@31223
   188
    val Ttm = mk_termifyT T;
haftmann@31223
   189
    val typtm = mk_termifyT typ;
haftmann@31223
   190
    fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts));
haftmann@31223
   191
    fun mk_random T = mk_const @{const_name random} [T];
haftmann@31223
   192
    val size = @{term "k\<Colon>code_numeral"};
haftmann@31223
   193
    val v = "x";
haftmann@31223
   194
    val t_v = Free (v, typtm);
haftmann@31223
   195
    val t_constr = mk_const constr Ts;
haftmann@31223
   196
    val lhs = mk_random T $ size;
haftmann@31223
   197
    val rhs = HOLogic.mk_ST [(((mk_random typ) $ size, @{typ Random.seed}), SOME (v, typtm))]
haftmann@31223
   198
      (HOLogic.mk_return Ttm @{typ Random.seed}
haftmann@31223
   199
      (mk_const "Code_Eval.valapp" [typ, T]
haftmann@31223
   200
        $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
haftmann@31223
   201
      @{typ Random.seed} (SOME Ttm, @{typ Random.seed});
haftmann@31223
   202
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@31223
   203
  in   
haftmann@31223
   204
    thy
haftmann@31223
   205
    |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
haftmann@31223
   206
    |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@31223
   207
    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
haftmann@31223
   208
    |> snd
haftmann@31223
   209
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
haftmann@31223
   210
  end;
haftmann@31223
   211
haftmann@31223
   212
fun ensure_random_typecopy tyco thy =
haftmann@31223
   213
  let
haftmann@31223
   214
    val SOME { vs = raw_vs, constr, typ = raw_typ, ... } =
haftmann@31223
   215
      TypecopyPackage.get_info thy tyco;
haftmann@31223
   216
    val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
haftmann@31223
   217
    val typ = map_atyps (fn TFree (v, sort) =>
haftmann@31223
   218
      TFree (v, constrain sort @{sort random})) raw_typ;
haftmann@31223
   219
    val vs' = Term.add_tfreesT typ [];
haftmann@31223
   220
    val vs = map (fn (v, sort) =>
haftmann@31223
   221
      (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
haftmann@31223
   222
    val do_inst = Sign.of_sort thy (typ, @{sort random});
haftmann@31223
   223
  in if do_inst then mk_random_typecopy tyco vs constr typ thy else thy end;
haftmann@31223
   224
haftmann@31223
   225
in
haftmann@31223
   226
haftmann@31223
   227
TypecopyPackage.interpretation ensure_random_typecopy
haftmann@31223
   228
haftmann@31223
   229
end
haftmann@31223
   230
*}
haftmann@31223
   231
haftmann@31179
   232
haftmann@31179
   233
no_notation fcomp (infixl "o>" 60)
haftmann@31179
   234
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
   235
haftmann@31179
   236
end