src/HOL/Library/Quickcheck_Types.thy
changeset 37915 e709e764f20c
child 37918 eda5faaca9e2
equal deleted inserted replaced
37914:49b908e43d61 37915:e709e764f20c
       
     1 (*  Title:      HOL/Library/Quickcheck_Types.thy
       
     2     Author:     Lukas Bulwahn
       
     3     Copyright   2010 TU Muenchen
       
     4 *)
       
     5 
       
     6 theory Quickcheck_Types
       
     7 imports Main
       
     8 begin
       
     9 
       
    10 text {*
       
    11 This theory provides some default types for the quickcheck execution.
       
    12 In most cases, the default type @{typ "int"} meets the sort constraints
       
    13 of the proposition.
       
    14 But for the type classes bot and top, the type @{typ "int"} is insufficient.
       
    15 Hence, we provide other types than @{typ "int"} as further default types.  
       
    16 *}
       
    17 
       
    18 subsection {* A non-distributive lattice *}
       
    19 
       
    20 datatype non_distrib_lattice = Zero | A | B | C | One
       
    21 
       
    22 instantiation non_distrib_lattice :: order
       
    23 begin
       
    24 
       
    25 definition less_eq_non_distrib_lattice
       
    26 where
       
    27   "a <= b = (case a of Zero => True | A => (b = A) \<or> (b = One) | B => (b = B) \<or> (b = One) | C => (b = C) \<or> (b = One) | One => (b = One))"
       
    28 
       
    29 definition less_non_distrib_lattice
       
    30 where
       
    31   "a < b = (case a of Zero => (b \<noteq> Zero) | A => (b = One) | B => (b = One) | C => (b = One) | One => False)"
       
    32 
       
    33 instance proof
       
    34 qed (auto simp add: less_eq_non_distrib_lattice_def less_non_distrib_lattice_def split: non_distrib_lattice.split non_distrib_lattice.split_asm)
       
    35 
       
    36 end
       
    37 
       
    38 instantiation non_distrib_lattice :: lattice
       
    39 begin
       
    40 
       
    41 
       
    42 definition sup_non_distrib_lattice
       
    43 where
       
    44   "sup a b = (if a = b then a else (if a = Zero then b else (if b = Zero then a else One)))"
       
    45 
       
    46 definition inf_non_distrib_lattice
       
    47 where
       
    48   "inf a b = (if a = b then a else (if a = One then b else (if b = One then a else Zero)))"
       
    49 
       
    50 instance proof
       
    51 qed (auto simp add: inf_non_distrib_lattice_def sup_non_distrib_lattice_def less_eq_non_distrib_lattice_def split: split_if non_distrib_lattice.split non_distrib_lattice.split_asm)
       
    52 
       
    53 end
       
    54 
       
    55 subsection {* Values extended by a bottom element *}
       
    56 
       
    57 datatype 'a bot = Value 'a | Bot
       
    58 
       
    59 instantiation bot :: (preorder) preorder
       
    60 begin
       
    61 
       
    62 definition less_eq_bot where
       
    63   "x \<le> y \<longleftrightarrow> (case x of Bot \<Rightarrow> True | Value x \<Rightarrow> (case y of Bot \<Rightarrow> False | Value y \<Rightarrow> x \<le> y))"
       
    64 
       
    65 definition less_bot where
       
    66   "x < y \<longleftrightarrow> (case y of Bot \<Rightarrow> False | Value y \<Rightarrow> (case x of Bot \<Rightarrow> True | Value x \<Rightarrow> x < y))"
       
    67 
       
    68 lemma less_eq_bot_Bot [simp]: "Bot \<le> x"
       
    69   by (simp add: less_eq_bot_def)
       
    70 
       
    71 lemma less_eq_bot_Bot_code [code]: "Bot \<le> x \<longleftrightarrow> True"
       
    72   by simp
       
    73 
       
    74 lemma less_eq_bot_Bot_is_Bot: "x \<le> Bot \<Longrightarrow> x = Bot"
       
    75   by (cases x) (simp_all add: less_eq_bot_def)
       
    76 
       
    77 lemma less_eq_bot_Value_Bot [simp, code]: "Value x \<le> Bot \<longleftrightarrow> False"
       
    78   by (simp add: less_eq_bot_def)
       
    79 
       
    80 lemma less_eq_bot_Value [simp, code]: "Value x \<le> Value y \<longleftrightarrow> x \<le> y"
       
    81   by (simp add: less_eq_bot_def)
       
    82 
       
    83 lemma less_bot_Bot [simp, code]: "x < Bot \<longleftrightarrow> False"
       
    84   by (simp add: less_bot_def)
       
    85 
       
    86 lemma less_bot_Bot_is_Value: "Bot < x \<Longrightarrow> \<exists>z. x = Value z"
       
    87   by (cases x) (simp_all add: less_bot_def)
       
    88 
       
    89 lemma less_bot_Bot_Value [simp]: "Bot < Value x"
       
    90   by (simp add: less_bot_def)
       
    91 
       
    92 lemma less_bot_Bot_Value_code [code]: "Bot < Value x \<longleftrightarrow> True"
       
    93   by simp
       
    94 
       
    95 lemma less_bot_Value [simp, code]: "Value x < Value y \<longleftrightarrow> x < y"
       
    96   by (simp add: less_bot_def)
       
    97 
       
    98 instance proof
       
    99 qed (auto simp add: less_eq_bot_def less_bot_def less_le_not_le elim: order_trans split: bot.splits)
       
   100 
       
   101 end 
       
   102 
       
   103 instance bot :: (order) order proof
       
   104 qed (auto simp add: less_eq_bot_def less_bot_def split: bot.splits)
       
   105 
       
   106 instance bot :: (linorder) linorder proof
       
   107 qed (auto simp add: less_eq_bot_def less_bot_def split: bot.splits)
       
   108 
       
   109 instantiation bot :: (preorder) bot
       
   110 begin
       
   111 
       
   112 definition "bot = Bot"
       
   113 
       
   114 instance proof
       
   115 qed (simp add: bot_bot_def)
       
   116 
       
   117 end
       
   118 
       
   119 instantiation bot :: (top) top
       
   120 begin
       
   121 
       
   122 definition "top = Value top"
       
   123 
       
   124 instance proof
       
   125 qed (simp add: top_bot_def less_eq_bot_def split: bot.split)
       
   126 
       
   127 end
       
   128 
       
   129 instantiation bot :: (semilattice_inf) semilattice_inf
       
   130 begin
       
   131 
       
   132 definition inf_bot
       
   133 where
       
   134   "inf x y = (case x of Bot => Bot | Value v => (case y of Bot => Bot | Value v' => Value (inf v v')))"
       
   135 
       
   136 instance proof
       
   137 qed (auto simp add: inf_bot_def less_eq_bot_def split: bot.splits)
       
   138 
       
   139 end
       
   140 
       
   141 instantiation bot :: (semilattice_sup) semilattice_sup
       
   142 begin
       
   143 
       
   144 definition sup_bot
       
   145 where
       
   146   "sup x y = (case x of Bot => y | Value v => (case y of Bot => x | Value v' => Value (sup v v')))"
       
   147 
       
   148 instance proof
       
   149 qed (auto simp add: sup_bot_def less_eq_bot_def split: bot.splits)
       
   150 
       
   151 end
       
   152 
       
   153 instance bot :: (lattice) bounded_lattice_bot ..
       
   154 
       
   155 section {* Values extended by a top element *}
       
   156 
       
   157 datatype 'a top = Value 'a | Top
       
   158 
       
   159 instantiation top :: (preorder) preorder
       
   160 begin
       
   161 
       
   162 definition less_eq_top where
       
   163   "x \<le> y \<longleftrightarrow> (case y of Top \<Rightarrow> True | Value y \<Rightarrow> (case x of Top \<Rightarrow> False | Value x \<Rightarrow> x \<le> y))"
       
   164 
       
   165 definition less_top where
       
   166   "x < y \<longleftrightarrow> (case x of Top \<Rightarrow> False | Value x \<Rightarrow> (case y of Top \<Rightarrow> True | Value y \<Rightarrow> x < y))"
       
   167 
       
   168 lemma less_eq_top_Top [simp]: "x <= Top"
       
   169   by (simp add: less_eq_top_def)
       
   170 
       
   171 lemma less_eq_top_Top_code [code]: "x \<le> Top \<longleftrightarrow> True"
       
   172   by simp
       
   173 
       
   174 lemma less_eq_top_is_Top: "Top \<le> x \<Longrightarrow> x = Top"
       
   175   by (cases x) (simp_all add: less_eq_top_def)
       
   176 
       
   177 lemma less_eq_top_Top_Value [simp, code]: "Top \<le> Value x \<longleftrightarrow> False"
       
   178   by (simp add: less_eq_top_def)
       
   179 
       
   180 lemma less_eq_top_Value_Value [simp, code]: "Value x \<le> Value y \<longleftrightarrow> x \<le> y"
       
   181   by (simp add: less_eq_top_def)
       
   182 
       
   183 lemma less_top_Top [simp, code]: "Top < x \<longleftrightarrow> False"
       
   184   by (simp add: less_top_def)
       
   185 
       
   186 lemma less_top_Top_is_Value: "x < Top \<Longrightarrow> \<exists>z. x = Value z"
       
   187   by (cases x) (simp_all add: less_top_def)
       
   188 
       
   189 lemma less_top_Value_Top [simp]: "Value x < Top"
       
   190   by (simp add: less_top_def)
       
   191 
       
   192 lemma less_top_Value_Top_code [code]: "Value x < Top \<longleftrightarrow> True"
       
   193   by simp
       
   194 
       
   195 lemma less_top_Value [simp, code]: "Value x < Value y \<longleftrightarrow> x < y"
       
   196   by (simp add: less_top_def)
       
   197 
       
   198 instance proof
       
   199 qed (auto simp add: less_eq_top_def less_top_def less_le_not_le elim: order_trans split: top.splits)
       
   200 
       
   201 end 
       
   202 
       
   203 instance top :: (order) order proof
       
   204 qed (auto simp add: less_eq_top_def less_top_def split: top.splits)
       
   205 
       
   206 instance top :: (linorder) linorder proof
       
   207 qed (auto simp add: less_eq_top_def less_top_def split: top.splits)
       
   208 
       
   209 instantiation top :: (preorder) top
       
   210 begin
       
   211 
       
   212 definition "top = Top"
       
   213 
       
   214 instance proof
       
   215 qed (simp add: top_top_def)
       
   216 
       
   217 end
       
   218 
       
   219 instantiation top :: (bot) bot
       
   220 begin
       
   221 
       
   222 definition "bot = Value bot"
       
   223 
       
   224 instance proof
       
   225 qed (simp add: bot_top_def less_eq_top_def split: top.split)
       
   226 
       
   227 end
       
   228 
       
   229 instantiation top :: (semilattice_inf) semilattice_inf
       
   230 begin
       
   231 
       
   232 definition inf_top
       
   233 where
       
   234   "inf x y = (case x of Top => y | Value v => (case y of Top => x | Value v' => Value (inf v v')))"
       
   235 
       
   236 instance proof
       
   237 qed (auto simp add: inf_top_def less_eq_top_def split: top.splits)
       
   238 
       
   239 end
       
   240 
       
   241 instantiation top :: (semilattice_sup) semilattice_sup
       
   242 begin
       
   243 
       
   244 definition sup_top
       
   245 where
       
   246   "sup x y = (case x of Top => Top | Value v => (case y of Top => Top | Value v' => Value (sup v v')))"
       
   247 
       
   248 instance proof
       
   249 qed (auto simp add: sup_top_def less_eq_top_def split: top.splits)
       
   250 
       
   251 end
       
   252 
       
   253 instance top :: (lattice) bounded_lattice_top ..
       
   254 
       
   255 section {* Quickcheck configuration *}
       
   256 
       
   257 quickcheck_params[default_type = ["int", "non_distrib_lattice", "int bot", "int top"]]
       
   258 
       
   259 hide_type non_distrib_lattice bot top
       
   260 
       
   261 end