src/HOL/Quickcheck.thy
author haftmann
Mon May 18 15:45:38 2009 +0200 (2009-05-18)
changeset 31194 1d6926f96440
parent 31186 b458b4ac570f
child 31203 5c8fb4fd67e0
permissions -rw-r--r--
added quickcheck support for numeric types
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@31179
     6
imports Main Real Random
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@31179
    16
  fixes random :: "index \<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@31179
    52
  in Abs ("n", @{typ index}, 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@31179
    87
definition random_itself :: "index \<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@31179
   142
definition random_fun :: "index \<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@31194
   157
definition random_nat :: "index \<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@31194
   159
     let n = Code_Index.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@31194
   171
     let j = (if k \<ge> i then Code_Index.int_of (k - i) else - Code_Index.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@31194
   178
definition (in term_syntax)
haftmann@31194
   179
  valterm_fract :: "int \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> int \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> rat \<times> (unit \<Rightarrow> Code_Eval.term)" where
haftmann@31194
   180
  [code inline]: "valterm_fract k l = Code_Eval.valtermify Fract {\<cdot>} k {\<cdot>} l"
haftmann@31179
   181
haftmann@31194
   182
instantiation rat :: random
haftmann@31179
   183
begin
haftmann@31179
   184
haftmann@31179
   185
definition
haftmann@31194
   186
  "random i = random i o\<rightarrow> (\<lambda>num. Random.range i o\<rightarrow> (\<lambda>denom. Pair (
haftmann@31194
   187
     let j = Code_Index.int_of (denom + 1)
haftmann@31194
   188
     in valterm_fract num (j, \<lambda>u. Code_Eval.term_of j))))"
haftmann@31179
   189
haftmann@31179
   190
instance ..
haftmann@31179
   191
haftmann@31179
   192
end
haftmann@31179
   193
haftmann@31194
   194
definition (in term_syntax)
haftmann@31194
   195
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Eval.term)" where
haftmann@31194
   196
  [code inline]: "valterm_ratreal k = Code_Eval.valtermify Ratreal {\<cdot>} k"
haftmann@31179
   197
haftmann@31179
   198
instantiation real :: random
haftmann@31179
   199
begin
haftmann@31179
   200
haftmann@31179
   201
definition
haftmann@31194
   202
  "random i = random i o\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
haftmann@31179
   203
haftmann@31179
   204
instance ..
haftmann@31179
   205
haftmann@31179
   206
end
haftmann@31179
   207
haftmann@31179
   208
haftmann@31179
   209
no_notation fcomp (infixl "o>" 60)
haftmann@31179
   210
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
   211
haftmann@31179
   212
end