src/HOL/Quickcheck.thy
author haftmann
Sat May 16 20:16:49 2009 +0200 (2009-05-16)
changeset 31186 b458b4ac570f
parent 31179 src/HOL/Library/Quickcheck.thy@ced817160283
child 31194 1d6926f96440
permissions -rw-r--r--
experimental move of Quickcheck and related theories to HOL image
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
definition (in term_syntax)
haftmann@31179
    74
  "termify_bool b = (if b then termify True else termify False)"
haftmann@31179
    75
haftmann@31179
    76
instantiation bool :: random
haftmann@31179
    77
begin
haftmann@31179
    78
haftmann@31179
    79
definition
haftmann@31179
    80
  "random i = Random.range i o\<rightarrow> (\<lambda>k. Pair (termify_bool (k div 2 = 0)))"
haftmann@31179
    81
haftmann@31179
    82
instance ..
haftmann@31179
    83
haftmann@31179
    84
end
haftmann@31179
    85
haftmann@31179
    86
definition (in term_syntax)
haftmann@31179
    87
  "termify_itself TYPE('a\<Colon>typerep) = termify TYPE('a)"
haftmann@31179
    88
haftmann@31179
    89
instantiation itself :: (typerep) random
haftmann@31179
    90
begin
haftmann@31179
    91
haftmann@31179
    92
definition random_itself :: "index \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31179
    93
  "random_itself _ = Pair (termify_itself TYPE('a))"
haftmann@31179
    94
haftmann@31179
    95
instance ..
haftmann@31179
    96
haftmann@31179
    97
end
haftmann@31179
    98
haftmann@31179
    99
text {* Type @{typ "'a \<Rightarrow> 'b"} *}
haftmann@30945
   100
haftmann@30945
   101
ML {*
haftmann@30945
   102
structure Random_Engine =
haftmann@30945
   103
struct
haftmann@30945
   104
haftmann@30945
   105
open Random_Engine;
haftmann@30945
   106
haftmann@30945
   107
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
haftmann@30945
   108
    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
haftmann@30945
   109
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
haftmann@30945
   110
    (seed : Random_Engine.seed) =
haftmann@30945
   111
  let
haftmann@30945
   112
    val (seed', seed'') = random_split seed;
haftmann@30945
   113
    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
haftmann@30945
   114
    val fun_upd = Const (@{const_name fun_upd},
haftmann@30945
   115
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
haftmann@30945
   116
    fun random_fun' x =
haftmann@30945
   117
      let
haftmann@30945
   118
        val (seed, fun_map, f_t) = ! state;
haftmann@30945
   119
      in case AList.lookup (uncurry eq) fun_map x
haftmann@30945
   120
       of SOME y => y
haftmann@30945
   121
        | NONE => let
haftmann@30945
   122
              val t1 = term_of x;
haftmann@30945
   123
              val ((y, t2), seed') = random seed;
haftmann@30945
   124
              val fun_map' = (x, y) :: fun_map;
haftmann@30945
   125
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
haftmann@30945
   126
              val _ = state := (seed', fun_map', f_t');
haftmann@30945
   127
            in y end
haftmann@30945
   128
      end;
haftmann@30945
   129
    fun term_fun' () = #3 (! state);
haftmann@30945
   130
  in ((random_fun', term_fun'), seed'') end;
haftmann@30945
   131
haftmann@26265
   132
end
haftmann@30945
   133
*}
haftmann@30945
   134
haftmann@31179
   135
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
haftmann@31179
   136
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
haftmann@31179
   137
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@30945
   138
haftmann@30945
   139
code_const random_fun_aux (Quickcheck "Random'_Engine.random'_fun")
haftmann@30945
   140
  -- {* With enough criminal energy this can be abused to derive @{prop False};
haftmann@30945
   141
  for this reason we use a distinguished target @{text Quickcheck}
haftmann@30945
   142
  not spoiling the regular trusted code generation *}
haftmann@30945
   143
haftmann@30945
   144
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
haftmann@30945
   145
begin
haftmann@30945
   146
haftmann@31179
   147
definition random_fun :: "index \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31179
   148
  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) Random.split_seed"
haftmann@30945
   149
haftmann@30945
   150
instance ..
haftmann@30945
   151
haftmann@30945
   152
end
haftmann@30945
   153
haftmann@30945
   154
code_reserved Quickcheck Random_Engine
haftmann@30945
   155
haftmann@31179
   156
haftmann@31179
   157
subsection {* Numeric types *}
haftmann@31179
   158
haftmann@31179
   159
function (in term_syntax) termify_numeral :: "index \<Rightarrow> int \<times> (unit \<Rightarrow> term)" where
haftmann@31179
   160
  "termify_numeral k = (if k = 0 then termify Int.Pls
haftmann@31179
   161
    else (if k mod 2 = 0 then termify Int.Bit0 else termify Int.Bit1) <\<cdot>> termify_numeral (k div 2))"
haftmann@31179
   162
  by pat_completeness auto
haftmann@31179
   163
haftmann@31179
   164
declare (in term_syntax) termify_numeral.psimps [simp del]
haftmann@31179
   165
haftmann@31179
   166
termination termify_numeral by (relation "measure Code_Index.nat_of")
haftmann@31179
   167
  (simp_all add: index)
haftmann@31179
   168
haftmann@31179
   169
definition (in term_syntax) termify_int_number :: "index \<Rightarrow> int \<times> (unit \<Rightarrow> term)" where
haftmann@31179
   170
  "termify_int_number k = termify number_of <\<cdot>> termify_numeral k"
haftmann@31179
   171
haftmann@31179
   172
definition (in term_syntax) termify_nat_number :: "index \<Rightarrow> nat \<times> (unit \<Rightarrow> term)" where
haftmann@31179
   173
  "termify_nat_number k = (nat \<circ> number_of, snd (termify (number_of :: int \<Rightarrow> nat))) <\<cdot>> termify_numeral k"
haftmann@31179
   174
haftmann@31179
   175
declare termify_nat_number_def [simplified snd_conv, code]
haftmann@31179
   176
haftmann@31179
   177
instantiation nat :: random
haftmann@31179
   178
begin
haftmann@31179
   179
haftmann@31179
   180
definition random_nat :: "index \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31179
   181
  "random_nat i = Random.range (i + 1) o\<rightarrow> (\<lambda>k. Pair (termify_nat_number k))"
haftmann@31179
   182
haftmann@31179
   183
instance ..
haftmann@31179
   184
haftmann@30945
   185
end
haftmann@31179
   186
haftmann@31179
   187
definition (in term_syntax) term_uminus :: "int \<times> (unit \<Rightarrow> term) \<Rightarrow> int \<times> (unit \<Rightarrow> term)" where
haftmann@31179
   188
  [code inline]: "term_uminus k = termify uminus <\<cdot>> k"
haftmann@31179
   189
haftmann@31179
   190
instantiation int :: random
haftmann@31179
   191
begin
haftmann@31179
   192
haftmann@31179
   193
definition
haftmann@31179
   194
  "random i = Random.range (2 * i + 1) o\<rightarrow> (\<lambda>k. Pair (if k \<ge> i
haftmann@31179
   195
     then let j = k - i in termify_int_number j
haftmann@31179
   196
     else let j = i - k in term_uminus (termify_int_number j)))"
haftmann@31179
   197
haftmann@31179
   198
instance ..
haftmann@31179
   199
haftmann@31179
   200
end
haftmann@31179
   201
haftmann@31179
   202
definition (in term_syntax) term_fract :: "int \<times> (unit \<Rightarrow> term) \<Rightarrow> int \<times> (unit \<Rightarrow> term) \<Rightarrow> rat \<times> (unit \<Rightarrow> term)" where
haftmann@31179
   203
  [code inline]: "term_fract k l = termify Fract <\<cdot>> k <\<cdot>> l"
haftmann@31179
   204
haftmann@31179
   205
instantiation rat :: random
haftmann@31179
   206
begin
haftmann@31179
   207
haftmann@31179
   208
definition
haftmann@31179
   209
  "random i = random i o\<rightarrow> (\<lambda>num. Random.range (i + 1) o\<rightarrow> (\<lambda>denom. Pair (term_fract num (termify_int_number denom))))"
haftmann@31179
   210
haftmann@31179
   211
instance ..
haftmann@31179
   212
haftmann@31179
   213
end
haftmann@31179
   214
haftmann@31179
   215
definition (in term_syntax) term_ratreal :: "rat \<times> (unit \<Rightarrow> term) \<Rightarrow> real \<times> (unit \<Rightarrow> term)" where
haftmann@31179
   216
  [code inline]: "term_ratreal k = termify Ratreal <\<cdot>> k"
haftmann@31179
   217
haftmann@31179
   218
instantiation real :: random
haftmann@31179
   219
begin
haftmann@31179
   220
haftmann@31179
   221
definition
haftmann@31179
   222
  "random i = random i o\<rightarrow> (\<lambda>r. Pair (term_ratreal r))"
haftmann@31179
   223
haftmann@31179
   224
instance ..
haftmann@31179
   225
haftmann@31179
   226
end
haftmann@31179
   227
haftmann@31179
   228
haftmann@31179
   229
no_notation fcomp (infixl "o>" 60)
haftmann@31179
   230
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
   231
haftmann@31179
   232
end