src/HOL/ex/Quickcheck_Narrowing_Examples.thy
author bulwahn
Fri, 18 Mar 2011 18:19:42 +0100
changeset 42020 2da02764d523
parent 41963 d8c3b26b3da4
child 42021 52551c0a3374
permissions -rw-r--r--
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
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
41937
a369f8ba5425 adapting example file to renaming of the quickcheck tester
bulwahn
parents: 41934
diff changeset
     9
imports "~~/src/HOL/Library/Quickcheck_Narrowing"
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
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    12
subsection {* Minimalistic examples *}
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
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
lemma
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    15
  "x = y"
41937
a369f8ba5425 adapting example file to renaming of the quickcheck tester
bulwahn
parents: 41934
diff changeset
    16
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    17
oops
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    18
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    19
(*
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    20
lemma
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    21
  "x = y"
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    22
quickcheck[tester = narrowing, finite_types = true, expect = counterexample]
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    23
oops
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    24
*)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    25
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    26
subsection {* Simple list examples *}
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    27
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    28
lemma "rev xs = xs"
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    29
  quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    30
oops
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    31
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    32
lemma "rev xs = xs"
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    33
  quickcheck[tester = narrowing, finite_types = true]
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    34
oops
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    35
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
    36
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    37
subsection {* AVL Trees *}
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    38
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    39
datatype 'a tree = ET |  MKT 'a "'a tree" "'a tree" nat
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    40
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    41
primrec set_of :: "'a tree \<Rightarrow> 'a set"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    42
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    43
"set_of ET = {}" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    44
"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
    45
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    46
primrec height :: "'a tree \<Rightarrow> nat"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    47
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    48
"height ET = 0" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    49
"height (MKT x l r h) = max (height l) (height r) + 1"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    50
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    51
primrec avl :: "'a tree \<Rightarrow> bool"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    52
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    53
"avl ET = True" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    54
"avl (MKT x l r h) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    55
 ((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
    56
  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
    57
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    58
primrec is_ord :: "('a::order) tree \<Rightarrow> bool"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    59
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    60
"is_ord ET = True" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    61
"is_ord (MKT n l r h) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    62
 ((\<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
    63
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    64
primrec is_in :: "('a::order) \<Rightarrow> 'a tree \<Rightarrow> bool"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    65
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    66
 "is_in k ET = False" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    67
 "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
    68
                           if k < n then (is_in k l)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    69
                           else (is_in k r))"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    70
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    71
primrec ht :: "'a tree \<Rightarrow> nat"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    72
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    73
"ht ET = 0" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    74
"ht (MKT x l r h) = h"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    75
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    76
definition
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    77
 mkt :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    78
"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
    79
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    80
(* replaced MKT lrn lrl lrr by MKT lrr lrl *)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    81
fun l_bal where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    82
"l_bal(n, MKT ln ll lr h, r) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    83
 (if ht ll < ht lr
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    84
  then case lr of ET \<Rightarrow> ET (* impossible *)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    85
                | MKT lrn lrr lrl lrh \<Rightarrow>
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    86
                  mkt lrn (mkt ln ll lrl) (mkt n lrr r)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    87
  else mkt ln ll (mkt n lr r))"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    88
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    89
fun r_bal where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    90
"r_bal(n, l, MKT rn rl rr h) =
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    91
 (if ht rl > ht rr
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    92
  then case rl of ET \<Rightarrow> ET (* impossible *)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    93
                | 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
    94
  else mkt rn (mkt n l rl) rr)"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    95
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    96
primrec insrt :: "'a::order \<Rightarrow> 'a tree \<Rightarrow> 'a tree"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    97
where
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    98
"insrt x ET = MKT x ET ET 1" |
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
    99
"insrt x (MKT n l r h) = 
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   100
   (if x=n
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   101
    then MKT n l r h
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   102
    else if x<n
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   103
         then let l' = insrt x l; hl' = ht l'; hr = ht r
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   104
              in if hl' = 2+hr then l_bal(n,l',r)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   105
                 else MKT n l' r (1 + max hl' hr)
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   106
         else let r' = insrt x r; hl = ht l; hr' = ht r'
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   107
              in if hr' = 2+hl then r_bal(n,l,r')
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   108
                 else MKT n l r' (1 + max hl hr'))"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   109
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   110
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   111
subsubsection {* Necessary setup for code generation *}
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   112
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   113
primrec set_of'
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   114
where 
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   115
  "set_of' ET = []"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   116
| "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
   117
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   118
lemma set_of':
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   119
  "set (set_of' t) = set_of t"
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   120
by (induct t) auto
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   121
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   122
lemma is_ord_mkt:
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   123
  "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
   124
by (simp add: set_of')
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   125
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   126
declare is_ord.simps(1)[code] is_ord_mkt[code]
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   127
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   128
subsubsection {* Invalid Lemma due to typo in lbal *}
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   129
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   130
lemma is_ord_l_bal:
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   131
 "\<lbrakk> is_ord(MKT (x :: nat) l r h); height l = height r + 2 \<rbrakk> \<Longrightarrow> is_ord(l_bal(x,l,r))"
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 41963
diff changeset
   132
quickcheck[tester = narrowing, finite_types = false, default_type = nat, size = 6, timeout = 100, expect = counterexample]
41907
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   133
oops
b04e16854c08 adding an example theory for Lazysmallcheck prototype
bulwahn
parents:
diff changeset
   134
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
end