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