src/HOL/Library/Quickcheck.thy
author nipkow
Sun, 10 May 2009 14:21:41 +0200
changeset 31084 f4db921165ce
parent 30970 3fe2e418a071
child 31153 6b31b143f18b
permissions -rw-r--r--
fixed HOLCF proofs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     2
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     3
header {* A simple counterexample generator *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     4
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     5
theory Quickcheck
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
     6
imports Random Code_Eval Map
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     7
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     8
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     9
subsection {* The @{text random} class *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    10
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    11
class random = typerep +
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    12
  fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    13
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    14
text {* Type @{typ "'a itself"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    15
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    16
instantiation itself :: ("{type, typerep}") random
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    17
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    18
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    19
definition
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29808
diff changeset
    20
  "random _ = Pair (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') TYPEREP('a))"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    21
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    22
instance ..
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    23
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    24
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    25
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    26
29808
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
    27
subsection {* Quickcheck generator *}
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    28
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    29
ML {*
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    30
structure StateMonad =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    31
struct
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    32
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    33
fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    34
fun liftT' sT = sT --> sT;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    35
29808
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
    36
fun return T sT x = Const (@{const_name Pair}, T --> liftT T sT) $ x;
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    37
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    38
fun scomp T1 T2 sT f g = Const (@{const_name scomp},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    39
  liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    40
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    41
end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    42
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    43
structure Quickcheck =
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    44
struct
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    45
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    46
open Quickcheck;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    47
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    48
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    49
30945
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    50
val target = "Quickcheck";
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    51
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    52
fun mk_generator_expr thy prop tys =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    53
  let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    54
    val bound_max = length tys - 1;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    55
    val bounds = map_index (fn (i, ty) =>
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    56
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    57
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    58
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    59
    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    60
      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    61
    val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    62
    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    63
    fun mk_split ty = Sign.mk_const thy
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    64
      (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]);
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
    65
    fun mk_scomp_split ty t t' =
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
    66
      StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*)
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    67
        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
    68
    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    69
      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    70
    val t = fold_rev mk_bindclause bounds (return $ check);
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    71
  in Abs ("n", @{typ index}, t) end;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    72
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    73
fun compile_generator_expr thy t =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    74
  let
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    75
    val tys = (map snd o fst o strip_abs) t;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    76
    val t' = mk_generator_expr thy t tys;
30970
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30945
diff changeset
    77
    val f = Code_ML.eval (SOME target) ("Quickcheck.eval_ref", eval_ref)
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30945
diff changeset
    78
      (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
3fe2e418a071 generic postprocessing scheme for term evaluations
haftmann
parents: 30945
diff changeset
    79
  in f #> Random_Engine.run end;
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    80
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    81
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    82
*}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    83
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    84
setup {*
30945
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    85
  Code_Target.extend_target (Quickcheck.target, (Code_ML.target_Eval, K I))
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    86
  #> Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    87
*}
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
    88
30945
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    89
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    90
subsection {* Type @{typ "'a \<Rightarrow> 'b"} *}
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    91
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    92
ML {*
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    93
structure Random_Engine =
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    94
struct
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    95
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    96
open Random_Engine;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    97
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    98
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    99
    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   100
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   101
    (seed : Random_Engine.seed) =
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   102
  let
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   103
    val (seed', seed'') = random_split seed;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   104
    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   105
    val fun_upd = Const (@{const_name fun_upd},
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   106
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   107
    fun random_fun' x =
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   108
      let
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   109
        val (seed, fun_map, f_t) = ! state;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   110
      in case AList.lookup (uncurry eq) fun_map x
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   111
       of SOME y => y
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   112
        | NONE => let
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   113
              val t1 = term_of x;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   114
              val ((y, t2), seed') = random seed;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   115
              val fun_map' = (x, y) :: fun_map;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   116
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   117
              val _ = state := (seed', fun_map', f_t');
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   118
            in y end
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   119
      end;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   120
    fun term_fun' () = #3 (! state);
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   121
  in ((random_fun', term_fun'), seed'') end;
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   122
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   123
end
30945
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   124
*}
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   125
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   126
axiomatization
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   127
  random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   128
    \<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed)
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   129
    \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed"
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   130
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   131
code_const random_fun_aux (Quickcheck "Random'_Engine.random'_fun")
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   132
  -- {* With enough criminal energy this can be abused to derive @{prop False};
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   133
  for this reason we use a distinguished target @{text Quickcheck}
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   134
  not spoiling the regular trusted code generation *}
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   135
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   136
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   137
begin
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   138
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   139
definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   140
  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) split_seed"
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   141
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   142
instance ..
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   143
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   144
end
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   145
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   146
code_reserved Quickcheck Random_Engine
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   147
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
   148
end