src/HOL/ex/Quickcheck_Narrowing_Examples.thy
author wenzelm
Sat, 17 Dec 2011 13:08:03 +0100
changeset 45909 6fe61da4c467
parent 45790 3355c27c93a4
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41931
diff changeset
     1
(*  Title:      HOL/ex/Quickcheck_Narrowing_Examples.thy
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
     2
    Author:     Lukas Bulwahn
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
     3
    Copyright   2011 TU Muenchen
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
     4
*)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
     5
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41931
diff changeset
     6
header {* Examples for narrowing-based testing  *}
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
     7
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41931
diff changeset
     8
theory Quickcheck_Narrowing_Examples
43318
825f4f0dcf71 correcting import theory of examples
bulwahn
parents: 43239
diff changeset
     9
imports Main
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    10
begin
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    11
45773
7f2366dc8c0f increasing quickcheck's timeout in the example theory to avoid failures on the testing infrastructure
bulwahn
parents: 45658
diff changeset
    12
declare [[quickcheck_timeout = 3600]]
7f2366dc8c0f increasing quickcheck's timeout in the example theory to avoid failures on the testing infrastructure
bulwahn
parents: 45658
diff changeset
    13
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    14
subsection {* Minimalistic examples *}
43356
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    15
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    16
lemma
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    17
  "x = y"
42021
52551c0a3374 extending code_int type more; adding narrowing instance for type int; added test case for int instance
bulwahn
parents: 42020
diff changeset
    18
quickcheck[tester = narrowing, finite_types = false, default_type = int, expect = counterexample]
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    19
oops
43356
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    20
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    21
lemma
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    22
  "x = y"
43356
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    23
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    24
oops
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    25
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    26
subsection {* Simple examples with existentials *}
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    27
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    28
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    29
  "\<exists> y :: nat. \<forall> x. x = y"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    30
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    31
oops
43356
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    32
(*
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    33
lemma
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    34
  "\<exists> y :: int. \<forall> x. x = y"
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    35
quickcheck[tester = narrowing, size = 2]
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    36
oops
2dee03f192b7 adding another narrowing strategy for integers
bulwahn
parents: 43318
diff changeset
    37
*)
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    38
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    39
  "x > 1 --> (\<exists>y :: nat. x < y & y <= 1)"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    40
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    41
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    42
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    43
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    44
  "x > 2 --> (\<exists>y :: nat. x < y & y <= 2)"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    45
quickcheck[tester = narrowing, finite_types = false, size = 10]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    46
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    47
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    48
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    49
  "\<forall> x. \<exists> y :: nat. x > 3 --> (y < x & y > 3)"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    50
quickcheck[tester = narrowing, finite_types = false, size = 7]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    51
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    52
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    53
text {* A false conjecture derived from an partial copy-n-paste of @{thm not_distinct_decomp} *}
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    54
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    55
  "~ distinct ws ==> EX xs ys zs y. ws = xs @ [y] @ ys @ [y]"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    56
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    57
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    58
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    59
text {* A false conjecture derived from theorems @{thm split_list_first} and @{thm split_list_last} *}  
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    60
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    61
  "x : set xs ==> EX ys zs. xs = ys @ x # zs & x ~: set zs & x ~: set ys"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    62
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    63
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    64
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    65
text {* A false conjecture derived from @{thm map_eq_Cons_conv} with a typo *}
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    66
lemma
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    67
  "(map f xs = y # ys) = (EX z zs. xs = z' # zs & f z = y & map f zs = ys)"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    68
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    69
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    70
45441
fb4ac1dd4fde adding some test cases for preprocessing and narrowing
bulwahn
parents: 45082
diff changeset
    71
lemma "a # xs = ys @ [a] ==> EX zs. xs = zs @ [a] & ys = a#zs"
fb4ac1dd4fde adding some test cases for preprocessing and narrowing
bulwahn
parents: 45082
diff changeset
    72
quickcheck[narrowing, expect = counterexample]
fb4ac1dd4fde adding some test cases for preprocessing and narrowing
bulwahn
parents: 45082
diff changeset
    73
quickcheck[exhaustive, random, expect = no_counterexample]
fb4ac1dd4fde adding some test cases for preprocessing and narrowing
bulwahn
parents: 45082
diff changeset
    74
oops
fb4ac1dd4fde adding some test cases for preprocessing and narrowing
bulwahn
parents: 45082
diff changeset
    75
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    76
subsection {* Simple list examples *}
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    77
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    78
lemma "rev xs = xs"
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    79
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    80
oops
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    81
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    82
(* FIXME: integer has strange representation! *)
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    83
lemma "rev xs = xs"
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    84
quickcheck[tester = narrowing, finite_types = false, default_type = int, expect = counterexample]
42021
52551c0a3374 extending code_int type more; adding narrowing instance for type int; added test case for int instance
bulwahn
parents: 42020
diff changeset
    85
oops
42184
1d4fae76ba5e adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents: 42024
diff changeset
    86
(*
42021
52551c0a3374 extending code_int type more; adding narrowing instance for type int; added test case for int instance
bulwahn
parents: 42020
diff changeset
    87
lemma "rev xs = xs"
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42021
diff changeset
    88
  quickcheck[tester = narrowing, finite_types = true, expect = counterexample]
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42021
diff changeset
    89
oops
42184
1d4fae76ba5e adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents: 42024
diff changeset
    90
*)
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42021
diff changeset
    91
subsection {* Simple examples with functions *}
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42021
diff changeset
    92
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42021
diff changeset
    93
lemma "map f xs = map g xs"
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    94
  quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    95
oops
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    96
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42021
diff changeset
    97
lemma "map f xs = map f ys ==> xs = ys"
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
    98
  quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
42024
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
    99
oops
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   100
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   101
lemma
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   102
  "list_all2 P (rev xs) (rev ys) = list_all2 P xs (rev ys)"
45773
7f2366dc8c0f increasing quickcheck's timeout in the example theory to avoid failures on the testing infrastructure
bulwahn
parents: 45658
diff changeset
   103
  quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
42024
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   104
oops
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   105
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   106
lemma "map f xs = F f xs"
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   107
  quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   108
oops
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   109
43239
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
   110
lemma "map f xs = F f xs"
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
   111
  quickcheck[tester = narrowing, finite_types = false, expect = counterexample]
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
   112
oops
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
   113
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
   114
(* requires some work...*)
42f82fda796b adding examples with existentials
bulwahn
parents: 42184
diff changeset
   115
(*
42024
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   116
lemma "R O S = S O R"
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   117
  quickcheck[tester = narrowing, size = 2]
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   118
oops
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   119
*)
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   120
45082
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   121
subsection {* Simple examples with inductive predicates *}
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   122
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   123
inductive even where
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   124
  "even 0" |
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   125
  "even n ==> even (Suc (Suc n))"
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   126
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   127
code_pred even .
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   128
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   129
lemma "even (n - 2) ==> even n"
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   130
quickcheck[narrowing, expect = counterexample]
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   131
oops
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   132
54c4e12bb5e0 adding an example with inductive predicates to quickcheck narrowing examples
bulwahn
parents: 43356
diff changeset
   133
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   134
subsection {* AVL Trees *}
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   135
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   136
datatype 'a tree = ET |  MKT 'a "'a tree" "'a tree" nat
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   137
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   138
primrec set_of :: "'a tree \<Rightarrow> 'a set"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   139
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   140
"set_of ET = {}" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   141
"set_of (MKT n l r h) = insert n (set_of l \<union> set_of r)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   142
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   143
primrec height :: "'a tree \<Rightarrow> nat"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   144
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   145
"height ET = 0" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   146
"height (MKT x l r h) = max (height l) (height r) + 1"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   147
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   148
primrec avl :: "'a tree \<Rightarrow> bool"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   149
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   150
"avl ET = True" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   151
"avl (MKT x l r h) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   152
 ((height l = height r \<or> height l = 1+height r \<or> height r = 1+height l) \<and> 
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   153
  h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   154
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   155
primrec is_ord :: "('a::order) tree \<Rightarrow> bool"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   156
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   157
"is_ord ET = True" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   158
"is_ord (MKT n l r h) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   159
 ((\<forall>n' \<in> set_of l. n' < n) \<and> (\<forall>n' \<in> set_of r. n < n') \<and> is_ord l \<and> is_ord r)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   160
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   161
primrec is_in :: "('a::order) \<Rightarrow> 'a tree \<Rightarrow> bool"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   162
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   163
 "is_in k ET = False" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   164
 "is_in k (MKT n l r h) = (if k = n then True else
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   165
                           if k < n then (is_in k l)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   166
                           else (is_in k r))"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   167
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   168
primrec ht :: "'a tree \<Rightarrow> nat"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   169
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   170
"ht ET = 0" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   171
"ht (MKT x l r h) = h"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   172
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   173
definition
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   174
 mkt :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   175
"mkt x l r = MKT x l r (max (ht l) (ht r) + 1)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   176
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   177
(* replaced MKT lrn lrl lrr by MKT lrr lrl *)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   178
fun l_bal where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   179
"l_bal(n, MKT ln ll lr h, r) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   180
 (if ht ll < ht lr
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   181
  then case lr of ET \<Rightarrow> ET (* impossible *)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   182
                | MKT lrn lrr lrl lrh \<Rightarrow>
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   183
                  mkt lrn (mkt ln ll lrl) (mkt n lrr r)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   184
  else mkt ln ll (mkt n lr r))"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   185
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   186
fun r_bal where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   187
"r_bal(n, l, MKT rn rl rr h) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   188
 (if ht rl > ht rr
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   189
  then case rl of ET \<Rightarrow> ET (* impossible *)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   190
                | MKT rln rll rlr h \<Rightarrow> mkt rln (mkt n l rll) (mkt rn rlr rr)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   191
  else mkt rn (mkt n l rl) rr)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   192
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   193
primrec insrt :: "'a::order \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   194
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   195
"insrt x ET = MKT x ET ET 1" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   196
"insrt x (MKT n l r h) = 
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   197
   (if x=n
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   198
    then MKT n l r h
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   199
    else if x<n
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   200
         then let l' = insrt x l; hl' = ht l'; hr = ht r
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   201
              in if hl' = 2+hr then l_bal(n,l',r)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   202
                 else MKT n l' r (1 + max hl' hr)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   203
         else let r' = insrt x r; hl = ht l; hr' = ht r'
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   204
              in if hr' = 2+hl then r_bal(n,l,r')
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   205
                 else MKT n l r' (1 + max hl hr'))"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   206
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   207
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   208
subsubsection {* Necessary setup for code generation *}
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   209
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   210
primrec set_of'
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   211
where 
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   212
  "set_of' ET = []"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   213
| "set_of' (MKT n l r h) = n # (set_of' l @ set_of' r)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   214
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   215
lemma set_of':
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   216
  "set (set_of' t) = set_of t"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   217
by (induct t) auto
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   218
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   219
lemma is_ord_mkt:
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   220
  "is_ord (MKT n l r h) = ((ALL n': set (set_of' l). n' < n) & (ALL n': set (set_of' r). n < n') & is_ord l & is_ord r)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   221
by (simp add: set_of')
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   222
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   223
declare is_ord.simps(1)[code] is_ord_mkt[code]
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   224
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   225
subsubsection {* Invalid Lemma due to typo in lbal *}
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   226
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   227
lemma is_ord_l_bal:
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   228
 "\<lbrakk> is_ord(MKT (x :: nat) l r h); height l = height r + 2 \<rbrakk> \<Longrightarrow> is_ord(l_bal(x,l,r))"
45773
7f2366dc8c0f increasing quickcheck's timeout in the example theory to avoid failures on the testing infrastructure
bulwahn
parents: 45658
diff changeset
   229
quickcheck[tester = narrowing, finite_types = false, default_type = nat, size = 6, expect = counterexample]
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   230
oops
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   231
45790
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   232
subsection {* Examples with hd and last *}
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   233
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   234
lemma
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   235
  "hd (xs @ ys) = hd ys"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   236
quickcheck[narrowing, expect = counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   237
oops
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   238
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   239
lemma
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   240
  "last(xs @ ys) = last xs"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   241
quickcheck[narrowing, expect = counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   242
oops
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   243
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   244
subsection {* Examples with underspecified/partial functions *}
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   245
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   246
lemma
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   247
  "xs = [] ==> hd xs \<noteq> x"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   248
quickcheck[narrowing, expect = no_counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   249
oops
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   250
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   251
lemma
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   252
  "xs = [] ==> hd xs = x"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   253
quickcheck[narrowing, expect = no_counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   254
oops
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   255
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   256
lemma "xs = [] ==> hd xs = x ==> x = y"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   257
quickcheck[narrowing, expect = no_counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   258
oops
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   259
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   260
lemma
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   261
  "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   262
quickcheck[narrowing, expect = no_counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   263
oops
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   264
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   265
lemma
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   266
  "hd (map f xs) = f (hd xs)"
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   267
quickcheck[narrowing, expect = no_counterexample]
3355c27c93a4 adding examples for quickcheck narrowing about partial functions
bulwahn
parents: 45773
diff changeset
   268
oops
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   269
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   270
end