src/HOL/Quickcheck.thy
author haftmann
Tue, 19 May 2009 16:54:55 +0200
changeset 31205 98370b26c2ce
parent 31203 5c8fb4fd67e0
child 31207 7eb05fc49b45
permissions -rw-r--r--
String.literal replaces message_string, code_numeral replaces (code_)index
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@29745
    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@31179
   161
haftmann@31179
   162
instance ..
haftmann@31179
   163
haftmann@30945
   164
end
haftmann@31179
   165
haftmann@31179
   166
instantiation int :: random
haftmann@31179
   167
begin
haftmann@31179
   168
haftmann@31179
   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@31179
   176
end
haftmann@31179
   177
haftmann@31179
   178
haftmann@31179
   179
no_notation fcomp (infixl "o>" 60)
haftmann@31179
   180
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
   181
haftmann@31179
   182
end