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