src/HOL/Quickcheck.thy
author bulwahn
Thu, 01 Dec 2011 22:16:26 +0100
changeset 45733 6bb30ae1abfe
parent 45729 a709e1a0f3dd
child 45801 5616fbda86e6
permissions -rw-r--r--
hiding internal constants and facts more properly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41922
fc070c5f3a4c adapting Quickcheck theory after moving ML files
bulwahn
parents: 40973
diff changeset
     1
(* Author: Florian Haftmann & Lukas Bulwahn, TU Muenchen *)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     2
41922
fc070c5f3a4c adapting Quickcheck theory after moving ML files
bulwahn
parents: 40973
diff changeset
     3
header {* A simple counterexample generator performing random testing *}
26265
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
40650
d40b347d5b0b adding Enum to HOL-Main image and removing it from HOL-Library
bulwahn
parents: 38857
diff changeset
     6
imports Random Code_Evaluation Enum
41928
05abcee548a1 adaptions in generators using the common functions
bulwahn
parents: 41923
diff changeset
     7
uses
45718
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
     8
  ("Tools/Quickcheck/quickcheck_common.ML")
41928
05abcee548a1 adaptions in generators using the common functions
bulwahn
parents: 41923
diff changeset
     9
  ("Tools/Quickcheck/random_generators.ML")
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    10
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    11
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
    12
notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
    13
notation scomp (infixl "\<circ>\<rightarrow>" 60)
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    14
45718
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    15
setup {* Code_Target.extend_target ("Quickcheck", (Code_Runtime.target, K I)) *}
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    16
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    17
subsection {* Catching Match exceptions *}
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    18
45729
a709e1a0f3dd making catch_match polymorphic
bulwahn
parents: 45721
diff changeset
    19
definition catch_match :: "'a => 'a => 'a" (* "(bool * term list) option => (bool * term list) option => (bool * term list) option"*)
45718
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    20
where
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    21
  [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    22
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    23
code_const catch_match 
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
    24
  (Quickcheck "(_) handle Match => _")
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    25
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    26
subsection {* The @{text random} class *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    27
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    28
class random = typerep +
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    29
  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
    30
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    31
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
    32
subsection {* Fundamental and numeric types*}
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    33
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    34
instantiation bool :: random
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    35
begin
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    36
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    37
definition
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
    38
  "random i = Random.range 2 \<circ>\<rightarrow>
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
    39
    (\<lambda>k. Pair (if k = 0 then Code_Evaluation.valtermify False else Code_Evaluation.valtermify True))"
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    40
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    41
instance ..
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    42
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    43
end
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    44
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    45
instantiation itself :: (typerep) random
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    46
begin
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    47
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    48
definition random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
    49
  "random_itself _ = Pair (Code_Evaluation.valtermify TYPE('a))"
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    50
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    51
instance ..
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    52
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    53
end
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    54
31483
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    55
instantiation char :: random
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    56
begin
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    57
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    58
definition
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
    59
  "random _ = Random.select chars \<circ>\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Evaluation.term_of c))"
31483
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    60
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    61
instance ..
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    62
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    63
end
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    64
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    65
instantiation String.literal :: random
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    66
begin
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    67
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    68
definition 
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
    69
  "random _ = Pair (STR '''', \<lambda>u. Code_Evaluation.term_of (STR ''''))"
31483
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    70
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    71
instance ..
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    72
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    73
end
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
    74
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    75
instantiation nat :: random
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    76
begin
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    77
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
    78
definition random_nat :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed" where
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
    79
  "random_nat i = Random.range (i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    80
     let n = Code_Numeral.nat_of k
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
    81
     in (n, \<lambda>_. Code_Evaluation.term_of n)))"
31194
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    82
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    83
instance ..
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    84
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    85
end
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    86
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    87
instantiation int :: random
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    88
begin
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    89
1d6926f96440 added quickcheck support for numeric types
haftmann
parents: 31186
diff changeset
    90
definition
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
    91
  "random i = Random.range (2 * i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    92
     let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
    93
     in (j, \<lambda>_. Code_Evaluation.term_of j)))"
31179
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
30945
0418e9bffbba separate channel for Quickcheck evaluations
haftmann
parents: 29823
diff changeset
    97
end
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
    98
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
    99
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
   100
subsection {* Complex generators *}
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
   101
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   102
text {* Towards @{typ "'a \<Rightarrow> 'b"} *}
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   103
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   104
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   105
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   106
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   107
31622
b30570327b76 more convenient signature for random_fun_lift
haftmann
parents: 31607
diff changeset
   108
definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
b30570327b76 more convenient signature for random_fun_lift
haftmann
parents: 31607
diff changeset
   109
  \<Rightarrow> Random.seed \<Rightarrow> (('a\<Colon>term_of \<Rightarrow> 'b\<Colon>typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
32657
5f13912245ff Code_Eval(uation)
haftmann
parents: 32373
diff changeset
   110
  "random_fun_lift f = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   111
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37751
diff changeset
   112
instantiation "fun" :: ("{equal, term_of}", random) random
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   113
begin
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   114
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   115
definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
31622
b30570327b76 more convenient signature for random_fun_lift
haftmann
parents: 31607
diff changeset
   116
  "random i = random_fun_lift (random i)"
31603
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   117
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   118
instance ..
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   119
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   120
end
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   121
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   122
text {* Towards type copies and datatypes *}
fa30cd74d7d6 revised interpretation combinator for datatype constructions
haftmann
parents: 31483
diff changeset
   123
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
   124
definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
   125
  "collapse f = (f \<circ>\<rightarrow> id)"
31223
87bde6b5f793 re-added corrected version of type copy quickcheck generator
haftmann
parents: 31211
diff changeset
   126
31260
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
   127
definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
   128
  "beyond k l = (if l > k then l else 0)"
4d273d043d59 separate module for quickcheck generators
haftmann
parents: 31245
diff changeset
   129
31267
4a85a4afc97d added lemma beyond_zero; hide constants
haftmann
parents: 31260
diff changeset
   130
lemma beyond_zero:
4a85a4afc97d added lemma beyond_zero; hide constants
haftmann
parents: 31260
diff changeset
   131
  "beyond k 0 = 0"
4a85a4afc97d added lemma beyond_zero; hide constants
haftmann
parents: 31260
diff changeset
   132
  by (simp add: beyond_def)
4a85a4afc97d added lemma beyond_zero; hide constants
haftmann
parents: 31260
diff changeset
   133
31483
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   134
lemma random_aux_rec:
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   135
  fixes random_aux :: "code_numeral \<Rightarrow> 'a"
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   136
  assumes "random_aux 0 = rhs 0"
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   137
    and "\<And>k. random_aux (Suc_code_numeral k) = rhs (Suc_code_numeral k)"
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   138
  shows "random_aux k = rhs k"
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   139
  using assms by (rule code_numeral.induct)
88210717bfc8 added generator for char and trivial generator for String.literal
haftmann
parents: 31267
diff changeset
   140
45718
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
   141
subsection {* Deriving random generators for datatypes *}
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
   142
8979b2463fc8 quickcheck random can also find potential counterexamples;
bulwahn
parents: 45178
diff changeset
   143
use "Tools/Quickcheck/quickcheck_common.ML" 
41922
fc070c5f3a4c adapting Quickcheck theory after moving ML files
bulwahn
parents: 40973
diff changeset
   144
use "Tools/Quickcheck/random_generators.ML"
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41922
diff changeset
   145
setup Random_Generators.setup
34968
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   146
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   147
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   148
subsection {* Code setup *}
33561
ab01b72715ef introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents: 32657
diff changeset
   149
41935
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   150
code_const random_fun_aux (Quickcheck "Random'_Generators.random'_fun")
34968
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   151
  -- {* With enough criminal energy this can be abused to derive @{prop False};
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   152
  for this reason we use a distinguished target @{text Quickcheck}
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   153
  not spoiling the regular trusted code generation *}
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   154
41935
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   155
code_reserved Quickcheck Random_Generators
34968
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   156
37751
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
   157
no_notation fcomp (infixl "\<circ>>" 60)
89e16802b6cc nicer xsymbol syntax for fcomp and scomp
haftmann
parents: 36176
diff changeset
   158
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
34968
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   159
45733
6bb30ae1abfe hiding internal constants and facts more properly
bulwahn
parents: 45729
diff changeset
   160
hide_fact catch_match_def
6bb30ae1abfe hiding internal constants and facts more properly
bulwahn
parents: 45729
diff changeset
   161
hide_const (open) catch_match
6bb30ae1abfe hiding internal constants and facts more properly
bulwahn
parents: 45729
diff changeset
   162
34968
ceeffca32eb0 tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
haftmann
parents: 33562
diff changeset
   163
subsection {* The Random-Predicate Monad *} 
33250
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   164
35880
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   165
fun iter' ::
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   166
  "'a itself => code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   167
where
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   168
  "iter' T nrandom sz seed = (if nrandom = 0 then bot_class.bot else
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   169
     let ((x, _), seed') = random sz seed
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   170
   in Predicate.Seq (%u. Predicate.Insert x (iter' T (nrandom - 1) sz seed')))"
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   171
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   172
definition iter :: "code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   173
where
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   174
  "iter nrandom sz seed = iter' (TYPE('a)) nrandom sz seed"
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   175
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   176
lemma [code]:
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   177
  "iter nrandom sz seed = (if nrandom = 0 then bot_class.bot else
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   178
     let ((x, _), seed') = random sz seed
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   179
   in Predicate.Seq (%u. Predicate.Insert x (iter (nrandom - 1) sz seed')))"
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   180
unfolding iter_def iter'.simps[of _ nrandom] ..
2623b23e41fc a new simpler random compilation for the predicate compiler
bulwahn
parents: 35028
diff changeset
   181
42163
392fd6c4669c renewing specifications in HOL: replacing types by type_synonym
bulwahn
parents: 42159
diff changeset
   182
type_synonym 'a randompred = "Random.seed \<Rightarrow> ('a Predicate.pred \<times> Random.seed)"
33250
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   183
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   184
definition empty :: "'a randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   185
  where "empty = Pair (bot_class.bot)"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   186
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   187
definition single :: "'a => 'a randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   188
  where "single x = Pair (Predicate.single x)"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   189
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   190
definition bind :: "'a randompred \<Rightarrow> ('a \<Rightarrow> 'b randompred) \<Rightarrow> 'b randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   191
  where
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   192
    "bind R f = (\<lambda>s. let
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   193
       (P, s') = R s;
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   194
       (s1, s2) = Random.split_seed s'
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   195
     in (Predicate.bind P (%a. fst (f a s1)), s2))"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   196
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   197
definition union :: "'a randompred \<Rightarrow> 'a randompred \<Rightarrow> 'a randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   198
where
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   199
  "union R1 R2 = (\<lambda>s. let
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   200
     (P1, s') = R1 s; (P2, s'') = R2 s'
44845
5e51075cbd97 added syntactic classes for "inf" and "sup"
krauss
parents: 42175
diff changeset
   201
   in (sup_class.sup P1 P2, s''))"
33250
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   202
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   203
definition if_randompred :: "bool \<Rightarrow> unit randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   204
where
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   205
  "if_randompred b = (if b then single () else empty)"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   206
36049
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35880
diff changeset
   207
definition iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a randompred"
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35880
diff changeset
   208
where
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35880
diff changeset
   209
  "iterate_upto f n m = Pair (Code_Numeral.iterate_upto f n m)"
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35880
diff changeset
   210
33250
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   211
definition not_randompred :: "unit randompred \<Rightarrow> unit randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   212
where
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   213
  "not_randompred P = (\<lambda>s. let
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   214
     (P', s') = P s
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   215
   in if Predicate.eval P' () then (Orderings.bot, s') else (Predicate.single (), s'))"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   216
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   217
definition Random :: "(Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> 'a randompred"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   218
  where "Random g = scomp g (Pair o (Predicate.single o fst))"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   219
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   220
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a randompred \<Rightarrow> 'b randompred)"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   221
  where "map f P = bind P (single o f)"
5c2af18a3237 including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
bulwahn
parents: 32657
diff changeset
   222
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36049
diff changeset
   223
hide_fact (open) iter'.simps iter_def empty_def single_def bind_def union_def if_randompred_def iterate_upto_def not_randompred_def Random_def map_def
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36049
diff changeset
   224
hide_type (open) randompred
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36049
diff changeset
   225
hide_const (open) random collapse beyond random_fun_aux random_fun_lift
36049
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35880
diff changeset
   226
  iter' iter empty single bind union if_randompred iterate_upto not_randompred Random map
31267
4a85a4afc97d added lemma beyond_zero; hide constants
haftmann
parents: 31260
diff changeset
   227
31179
ced817160283 experimental addition of quickcheck
haftmann
parents: 31153
diff changeset
   228
end