src/HOL/Quickcheck.thy
author haftmann
Wed May 27 22:11:05 2009 +0200 (2009-05-27)
changeset 31267 4a85a4afc97d
parent 31260 4d273d043d59
child 31483 88210717bfc8
permissions -rw-r--r--
added lemma beyond_zero; hide constants
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@31260
     7
uses ("Tools/quickcheck_generators.ML")
haftmann@26265
     8
begin
haftmann@26265
     9
haftmann@31179
    10
notation fcomp (infixl "o>" 60)
haftmann@31179
    11
notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
    12
haftmann@31179
    13
haftmann@26265
    14
subsection {* The @{text random} class *}
haftmann@26265
    15
haftmann@28335
    16
class random = typerep +
haftmann@31205
    17
  fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@26265
    18
haftmann@26267
    19
haftmann@31260
    20
subsection {* Fundamental and numeric types*}
haftmann@31179
    21
haftmann@31179
    22
instantiation bool :: random
haftmann@31179
    23
begin
haftmann@31179
    24
haftmann@31179
    25
definition
haftmann@31194
    26
  "random i = Random.range i o\<rightarrow>
haftmann@31194
    27
    (\<lambda>k. Pair (if (k div 2 = 0) then Code_Eval.valtermify True else Code_Eval.valtermify False))"
haftmann@31179
    28
haftmann@31179
    29
instance ..
haftmann@31179
    30
haftmann@31179
    31
end
haftmann@31179
    32
haftmann@31179
    33
instantiation itself :: (typerep) random
haftmann@31179
    34
begin
haftmann@31179
    35
haftmann@31205
    36
definition random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31194
    37
  "random_itself _ = Pair (Code_Eval.valtermify TYPE('a))"
haftmann@31179
    38
haftmann@31179
    39
instance ..
haftmann@31179
    40
haftmann@31179
    41
end
haftmann@31179
    42
haftmann@31179
    43
instantiation nat :: random
haftmann@31179
    44
begin
haftmann@31179
    45
haftmann@31205
    46
definition random_nat :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Eval.term)) \<times> Random.seed" where
haftmann@31194
    47
  "random_nat i = Random.range (i + 1) o\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
    48
     let n = Code_Numeral.nat_of k
haftmann@31194
    49
     in (n, \<lambda>_. Code_Eval.term_of n)))"
haftmann@31194
    50
haftmann@31194
    51
instance ..
haftmann@31194
    52
haftmann@31194
    53
end
haftmann@31194
    54
haftmann@31194
    55
instantiation int :: random
haftmann@31194
    56
begin
haftmann@31194
    57
haftmann@31194
    58
definition
haftmann@31194
    59
  "random i = Random.range (2 * i + 1) o\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
    60
     let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))
haftmann@31194
    61
     in (j, \<lambda>_. Code_Eval.term_of j)))"
haftmann@31179
    62
haftmann@31179
    63
instance ..
haftmann@31179
    64
haftmann@30945
    65
end
haftmann@31179
    66
haftmann@31260
    67
haftmann@31260
    68
subsection {* Complex generators *}
haftmann@31260
    69
haftmann@31260
    70
definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
haftmann@31260
    71
  "collapse f = (f o\<rightarrow> id)"
haftmann@31223
    72
haftmann@31260
    73
definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
haftmann@31260
    74
  "beyond k l = (if l > k then l else 0)"
haftmann@31260
    75
haftmann@31267
    76
lemma beyond_zero:
haftmann@31267
    77
  "beyond k 0 = 0"
haftmann@31267
    78
  by (simp add: beyond_def)
haftmann@31267
    79
haftmann@31260
    80
use "Tools/quickcheck_generators.ML"
haftmann@31260
    81
setup {* Quickcheck_Generators.setup *}
haftmann@31260
    82
haftmann@31260
    83
code_reserved Quickcheck Quickcheck_Generators
haftmann@31260
    84
haftmann@31260
    85
text {* Type @{typ "'a \<Rightarrow> 'b"} *}
haftmann@31223
    86
haftmann@31260
    87
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
haftmann@31260
    88
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
haftmann@31260
    89
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@31223
    90
haftmann@31260
    91
code_const random_fun_aux (Quickcheck "Quickcheck'_Generators.random'_fun")
haftmann@31260
    92
  -- {* With enough criminal energy this can be abused to derive @{prop False};
haftmann@31260
    93
  for this reason we use a distinguished target @{text Quickcheck}
haftmann@31260
    94
  not spoiling the regular trusted code generation *}
haftmann@31223
    95
haftmann@31260
    96
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
haftmann@31260
    97
begin
haftmann@31223
    98
haftmann@31260
    99
definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31260
   100
  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) Random.split_seed"
haftmann@31260
   101
haftmann@31260
   102
instance ..
haftmann@31223
   103
haftmann@31223
   104
end
haftmann@31245
   105
haftmann@31245
   106
haftmann@31267
   107
hide (open) const collapse beyond
haftmann@31267
   108
haftmann@31179
   109
no_notation fcomp (infixl "o>" 60)
haftmann@31179
   110
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31179
   111
haftmann@31179
   112
end