src/HOL/Library/Quickcheck_Narrowing.thy
author bulwahn
Mon May 30 13:57:59 2011 +0200 (2011-05-30)
changeset 43047 26774ccb1c74
parent 42980 859fe9cc0838
child 43237 8f5c3c6c2909
permissions -rw-r--r--
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
bulwahn@41905
     1
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@41905
     2
bulwahn@41930
     3
header {* Counterexample generator preforming narrowing-based testing *}
bulwahn@41905
     4
bulwahn@41930
     5
theory Quickcheck_Narrowing
bulwahn@41905
     6
imports Main "~~/src/HOL/Library/Code_Char"
bulwahn@41962
     7
uses
bulwahn@41962
     8
  ("~~/src/HOL/Tools/Quickcheck/narrowing_generators.ML")
bulwahn@41905
     9
begin
bulwahn@41905
    10
bulwahn@41905
    11
subsection {* Counterexample generator *}
bulwahn@41905
    12
bulwahn@41909
    13
subsubsection {* Code generation setup *}
bulwahn@41909
    14
bulwahn@41909
    15
code_type typerep
bulwahn@41909
    16
  ("Haskell" "Typerep")
bulwahn@41909
    17
bulwahn@41909
    18
code_const Typerep.Typerep
bulwahn@41909
    19
  ("Haskell" "Typerep")
bulwahn@41909
    20
bulwahn@41909
    21
code_reserved Haskell Typerep
bulwahn@41909
    22
bulwahn@41964
    23
subsubsection {* Type @{text "code_int"} for Haskell's Int type *}
bulwahn@41908
    24
bulwahn@41908
    25
typedef (open) code_int = "UNIV \<Colon> int set"
bulwahn@41908
    26
  morphisms int_of of_int by rule
bulwahn@41908
    27
bulwahn@42021
    28
lemma of_int_int_of [simp]:
bulwahn@42021
    29
  "of_int (int_of k) = k"
bulwahn@42021
    30
  by (rule int_of_inverse)
bulwahn@42021
    31
bulwahn@42021
    32
lemma int_of_of_int [simp]:
bulwahn@42021
    33
  "int_of (of_int n) = n"
bulwahn@42021
    34
  by (rule of_int_inverse) (rule UNIV_I)
bulwahn@42021
    35
bulwahn@42021
    36
lemma code_int:
bulwahn@42021
    37
  "(\<And>n\<Colon>code_int. PROP P n) \<equiv> (\<And>n\<Colon>int. PROP P (of_int n))"
bulwahn@42021
    38
proof
bulwahn@42021
    39
  fix n :: int
bulwahn@42021
    40
  assume "\<And>n\<Colon>code_int. PROP P n"
bulwahn@42021
    41
  then show "PROP P (of_int n)" .
bulwahn@42021
    42
next
bulwahn@42021
    43
  fix n :: code_int
bulwahn@42021
    44
  assume "\<And>n\<Colon>int. PROP P (of_int n)"
bulwahn@42021
    45
  then have "PROP P (of_int (int_of n))" .
bulwahn@42021
    46
  then show "PROP P n" by simp
bulwahn@42021
    47
qed
bulwahn@42021
    48
bulwahn@42021
    49
bulwahn@41908
    50
lemma int_of_inject [simp]:
bulwahn@41908
    51
  "int_of k = int_of l \<longleftrightarrow> k = l"
bulwahn@41908
    52
  by (rule int_of_inject)
bulwahn@41908
    53
bulwahn@42021
    54
lemma of_int_inject [simp]:
bulwahn@42021
    55
  "of_int n = of_int m \<longleftrightarrow> n = m"
bulwahn@42021
    56
  by (rule of_int_inject) (rule UNIV_I)+
bulwahn@42021
    57
bulwahn@42021
    58
instantiation code_int :: equal
bulwahn@42021
    59
begin
bulwahn@42021
    60
bulwahn@42021
    61
definition
bulwahn@42021
    62
  "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
bulwahn@42021
    63
bulwahn@42021
    64
instance proof
bulwahn@42021
    65
qed (auto simp add: equal_code_int_def equal_int_def eq_int_refl)
bulwahn@42021
    66
bulwahn@42021
    67
end
bulwahn@42021
    68
bulwahn@42021
    69
instantiation code_int :: number
bulwahn@42021
    70
begin
bulwahn@42021
    71
bulwahn@42021
    72
definition
bulwahn@42021
    73
  "number_of = of_int"
bulwahn@42021
    74
bulwahn@42021
    75
instance ..
bulwahn@42021
    76
bulwahn@42021
    77
end
bulwahn@42021
    78
bulwahn@42021
    79
lemma int_of_number [simp]:
bulwahn@42021
    80
  "int_of (number_of k) = number_of k"
bulwahn@42021
    81
  by (simp add: number_of_code_int_def number_of_is_id)
bulwahn@42021
    82
bulwahn@42021
    83
bulwahn@41912
    84
definition nat_of :: "code_int => nat"
bulwahn@41912
    85
where
bulwahn@41912
    86
  "nat_of i = nat (int_of i)"
bulwahn@41908
    87
bulwahn@42980
    88
bulwahn@43047
    89
code_datatype "number_of \<Colon> int \<Rightarrow> code_int"
bulwahn@42980
    90
  
bulwahn@42980
    91
  
bulwahn@42021
    92
instantiation code_int :: "{minus, linordered_semidom, semiring_div, linorder}"
bulwahn@41908
    93
begin
bulwahn@41908
    94
bulwahn@41908
    95
definition [simp, code del]:
bulwahn@41908
    96
  "0 = of_int 0"
bulwahn@41908
    97
bulwahn@41908
    98
definition [simp, code del]:
bulwahn@41908
    99
  "1 = of_int 1"
bulwahn@41908
   100
bulwahn@41908
   101
definition [simp, code del]:
bulwahn@42021
   102
  "n + m = of_int (int_of n + int_of m)"
bulwahn@42021
   103
bulwahn@42021
   104
definition [simp, code del]:
bulwahn@41908
   105
  "n - m = of_int (int_of n - int_of m)"
bulwahn@41908
   106
bulwahn@41908
   107
definition [simp, code del]:
bulwahn@42021
   108
  "n * m = of_int (int_of n * int_of m)"
bulwahn@42021
   109
bulwahn@42021
   110
definition [simp, code del]:
bulwahn@42021
   111
  "n div m = of_int (int_of n div int_of m)"
bulwahn@42021
   112
bulwahn@42021
   113
definition [simp, code del]:
bulwahn@42021
   114
  "n mod m = of_int (int_of n mod int_of m)"
bulwahn@42021
   115
bulwahn@42021
   116
definition [simp, code del]:
bulwahn@41908
   117
  "n \<le> m \<longleftrightarrow> int_of n \<le> int_of m"
bulwahn@41908
   118
bulwahn@41908
   119
definition [simp, code del]:
bulwahn@41908
   120
  "n < m \<longleftrightarrow> int_of n < int_of m"
bulwahn@41908
   121
bulwahn@41908
   122
bulwahn@42021
   123
instance proof
bulwahn@42021
   124
qed (auto simp add: code_int left_distrib zmult_zless_mono2)
bulwahn@41908
   125
bulwahn@41908
   126
end
bulwahn@42980
   127
bulwahn@41908
   128
lemma zero_code_int_code [code, code_unfold]:
bulwahn@41908
   129
  "(0\<Colon>code_int) = Numeral0"
bulwahn@42980
   130
  by (simp add: number_of_code_int_def Pls_def)
bulwahn@42980
   131
lemma [code_post]: "Numeral0 = (0\<Colon>code_int)"
bulwahn@42980
   132
  using zero_code_int_code ..
bulwahn@41908
   133
bulwahn@42980
   134
lemma one_code_int_code [code, code_unfold]:
bulwahn@41908
   135
  "(1\<Colon>code_int) = Numeral1"
bulwahn@42980
   136
  by (simp add: number_of_code_int_def Pls_def Bit1_def)
bulwahn@41908
   137
lemma [code_post]: "Numeral1 = (1\<Colon>code_int)"
bulwahn@42980
   138
  using one_code_int_code ..
bulwahn@42980
   139
bulwahn@41908
   140
bulwahn@42021
   141
definition div_mod_code_int :: "code_int \<Rightarrow> code_int \<Rightarrow> code_int \<times> code_int" where
bulwahn@42021
   142
  [code del]: "div_mod_code_int n m = (n div m, n mod m)"
bulwahn@42021
   143
bulwahn@42021
   144
lemma [code]:
bulwahn@42021
   145
  "div_mod_code_int n m = (if m = 0 then (0, n) else (n div m, n mod m))"
bulwahn@42021
   146
  unfolding div_mod_code_int_def by auto
bulwahn@42021
   147
bulwahn@42021
   148
lemma [code]:
bulwahn@42021
   149
  "n div m = fst (div_mod_code_int n m)"
bulwahn@42021
   150
  unfolding div_mod_code_int_def by simp
bulwahn@42021
   151
bulwahn@42021
   152
lemma [code]:
bulwahn@42021
   153
  "n mod m = snd (div_mod_code_int n m)"
bulwahn@42021
   154
  unfolding div_mod_code_int_def by simp
bulwahn@42021
   155
bulwahn@42021
   156
lemma int_of_code [code]:
bulwahn@42021
   157
  "int_of k = (if k = 0 then 0
bulwahn@42021
   158
    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
bulwahn@42021
   159
proof -
bulwahn@42021
   160
  have 1: "(int_of k div 2) * 2 + int_of k mod 2 = int_of k" 
bulwahn@42021
   161
    by (rule mod_div_equality)
bulwahn@42021
   162
  have "int_of k mod 2 = 0 \<or> int_of k mod 2 = 1" by auto
bulwahn@42021
   163
  from this show ?thesis
bulwahn@42021
   164
    apply auto
bulwahn@42021
   165
    apply (insert 1) by (auto simp add: mult_ac)
bulwahn@42021
   166
qed
bulwahn@42021
   167
bulwahn@42021
   168
bulwahn@42021
   169
code_instance code_numeral :: equal
bulwahn@42021
   170
  (Haskell -)
bulwahn@42021
   171
bulwahn@42021
   172
setup {* fold (Numeral.add_code @{const_name number_code_int_inst.number_of_code_int}
bulwahn@42021
   173
  false Code_Printer.literal_numeral) ["Haskell"]  *}
bulwahn@42021
   174
bulwahn@41908
   175
code_const "0 \<Colon> code_int"
bulwahn@41908
   176
  (Haskell "0")
bulwahn@41908
   177
bulwahn@41908
   178
code_const "1 \<Colon> code_int"
bulwahn@41908
   179
  (Haskell "1")
bulwahn@41908
   180
bulwahn@41908
   181
code_const "minus \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> code_int"
bulwahn@41908
   182
  (Haskell "(_/ -/ _)")
bulwahn@41908
   183
bulwahn@42021
   184
code_const div_mod_code_int
bulwahn@42021
   185
  (Haskell "divMod")
bulwahn@42021
   186
bulwahn@42021
   187
code_const "HOL.equal \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
bulwahn@42021
   188
  (Haskell infix 4 "==")
bulwahn@42021
   189
bulwahn@41908
   190
code_const "op \<le> \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
bulwahn@41908
   191
  (Haskell infix 4 "<=")
bulwahn@41908
   192
bulwahn@41908
   193
code_const "op < \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
bulwahn@41908
   194
  (Haskell infix 4 "<")
bulwahn@41908
   195
bulwahn@41908
   196
code_type code_int
bulwahn@41908
   197
  (Haskell "Int")
bulwahn@41908
   198
bulwahn@42021
   199
code_abort of_int
bulwahn@42021
   200
bulwahn@41961
   201
subsubsection {* Narrowing's deep representation of types and terms *}
bulwahn@41905
   202
bulwahn@43047
   203
datatype narrowing_type = SumOfProd "narrowing_type list list"
bulwahn@41905
   204
bulwahn@43047
   205
datatype narrowing_term = Var "code_int list" narrowing_type | Ctr code_int "narrowing_term list"
bulwahn@43047
   206
datatype 'a cons = C narrowing_type "(narrowing_term list => 'a) list"
bulwahn@41905
   207
bulwahn@42980
   208
subsubsection {* From narrowing's deep representation of terms to Code_Evaluation's terms *}
bulwahn@42980
   209
bulwahn@42980
   210
class partial_term_of = typerep +
bulwahn@43047
   211
  fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term"
bulwahn@43047
   212
bulwahn@43047
   213
lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t"
bulwahn@43047
   214
  by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp)
bulwahn@43047
   215
bulwahn@42980
   216
bulwahn@41964
   217
subsubsection {* Auxilary functions for Narrowing *}
bulwahn@41905
   218
bulwahn@41908
   219
consts nth :: "'a list => code_int => 'a"
bulwahn@41905
   220
bulwahn@41908
   221
code_const nth ("Haskell" infixl 9  "!!")
bulwahn@41905
   222
bulwahn@41908
   223
consts error :: "char list => 'a"
bulwahn@41905
   224
bulwahn@41905
   225
code_const error ("Haskell" "error")
bulwahn@41905
   226
bulwahn@41908
   227
consts toEnum :: "code_int => char"
bulwahn@41908
   228
bulwahn@41908
   229
code_const toEnum ("Haskell" "toEnum")
bulwahn@41905
   230
bulwahn@41908
   231
consts map_index :: "(code_int * 'a => 'b) => 'a list => 'b list"  
bulwahn@41905
   232
bulwahn@41908
   233
consts split_At :: "code_int => 'a list => 'a list * 'a list"
bulwahn@41908
   234
 
bulwahn@41961
   235
subsubsection {* Narrowing's basic operations *}
bulwahn@41905
   236
bulwahn@41961
   237
type_synonym 'a narrowing = "code_int => 'a cons"
bulwahn@41905
   238
bulwahn@41961
   239
definition empty :: "'a narrowing"
bulwahn@41905
   240
where
bulwahn@41905
   241
  "empty d = C (SumOfProd []) []"
bulwahn@41905
   242
  
bulwahn@41961
   243
definition cons :: "'a => 'a narrowing"
bulwahn@41905
   244
where
bulwahn@41905
   245
  "cons a d = (C (SumOfProd [[]]) [(%_. a)])"
bulwahn@41905
   246
bulwahn@43047
   247
fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
bulwahn@41905
   248
where
bulwahn@41908
   249
  "conv cs (Var p _) = error (Char Nibble0 Nibble0 # map toEnum p)"
bulwahn@41905
   250
| "conv cs (Ctr i xs) = (nth cs i) xs"
bulwahn@41905
   251
bulwahn@43047
   252
fun nonEmpty :: "narrowing_type => bool"
bulwahn@41905
   253
where
bulwahn@41905
   254
  "nonEmpty (SumOfProd ps) = (\<not> (List.null ps))"
bulwahn@41905
   255
bulwahn@41961
   256
definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
bulwahn@41905
   257
where
bulwahn@41905
   258
  "apply f a d =
bulwahn@41905
   259
     (case f d of C (SumOfProd ps) cfs =>
bulwahn@41905
   260
       case a (d - 1) of C ta cas =>
bulwahn@41905
   261
       let
bulwahn@41905
   262
         shallow = (d > 0 \<and> nonEmpty ta);
bulwahn@41905
   263
         cs = [(%xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs]
bulwahn@41905
   264
       in C (SumOfProd [ta # p. shallow, p <- ps]) cs)"
bulwahn@41905
   265
bulwahn@41961
   266
definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
bulwahn@41905
   267
where
bulwahn@41905
   268
  "sum a b d =
bulwahn@41905
   269
    (case a d of C (SumOfProd ssa) ca => 
bulwahn@41905
   270
      case b d of C (SumOfProd ssb) cb =>
bulwahn@41905
   271
      C (SumOfProd (ssa @ ssb)) (ca @ cb))"
bulwahn@41905
   272
bulwahn@41912
   273
lemma [fundef_cong]:
bulwahn@41912
   274
  assumes "a d = a' d" "b d = b' d" "d = d'"
bulwahn@41912
   275
  shows "sum a b d = sum a' b' d'"
bulwahn@43047
   276
using assms unfolding sum_def by (auto split: cons.split narrowing_type.split)
bulwahn@41912
   277
bulwahn@41912
   278
lemma [fundef_cong]:
bulwahn@41912
   279
  assumes "f d = f' d" "(\<And>d'. 0 <= d' & d' < d ==> a d' = a' d')"
bulwahn@41912
   280
  assumes "d = d'"
bulwahn@41912
   281
  shows "apply f a d = apply f' a' d'"
bulwahn@41912
   282
proof -
bulwahn@41912
   283
  note assms moreover
bulwahn@41930
   284
  have "int_of (of_int 0) < int_of d' ==> int_of (of_int 0) <= int_of (of_int (int_of d' - int_of (of_int 1)))"
bulwahn@41912
   285
    by (simp add: of_int_inverse)
bulwahn@41912
   286
  moreover
bulwahn@41930
   287
  have "int_of (of_int (int_of d' - int_of (of_int 1))) < int_of d'"
bulwahn@41912
   288
    by (simp add: of_int_inverse)
bulwahn@41912
   289
  ultimately show ?thesis
bulwahn@43047
   290
    unfolding apply_def by (auto split: cons.split narrowing_type.split simp add: Let_def)
bulwahn@41912
   291
qed
bulwahn@41912
   292
bulwahn@41908
   293
type_synonym pos = "code_int list"
bulwahn@41912
   294
(*
bulwahn@41908
   295
subsubsection {* Term refinement *}
bulwahn@41908
   296
bulwahn@41908
   297
definition new :: "pos => type list list => term list"
bulwahn@41908
   298
where
bulwahn@41908
   299
  "new p ps = map_index (%(c, ts). Ctr c (map_index (%(i, t). Var (p @ [i]) t) ts)) ps"
bulwahn@41908
   300
bulwahn@41908
   301
fun refine :: "term => pos => term list" and refineList :: "term list => pos => (term list) list"
bulwahn@41908
   302
where
bulwahn@41908
   303
  "refine (Var p (SumOfProd ss)) [] = new p ss"
bulwahn@41908
   304
| "refine (Ctr c xs) p = map (Ctr c) (refineList xs p)"
bulwahn@41908
   305
| "refineList xs (i # is) = (let (ls, xrs) = split_At i xs in (case xrs of x#rs => [ls @ y # rs. y <- refine x is]))"
bulwahn@41908
   306
bulwahn@41908
   307
text {* Find total instantiations of a partial value *}
bulwahn@41908
   308
bulwahn@41908
   309
function total :: "term => term list"
bulwahn@41908
   310
where
bulwahn@41908
   311
  "total (Ctr c xs) = [Ctr c ys. ys <- map total xs]"
bulwahn@41908
   312
| "total (Var p (SumOfProd ss)) = [y. x <- new p ss, y <- total x]"
bulwahn@41908
   313
by pat_completeness auto
bulwahn@41908
   314
bulwahn@41908
   315
termination sorry
bulwahn@41912
   316
*)
bulwahn@41961
   317
subsubsection {* Narrowing generator type class *}
bulwahn@41905
   318
bulwahn@41961
   319
class narrowing =
bulwahn@41961
   320
  fixes narrowing :: "code_int => 'a cons"
bulwahn@41905
   321
bulwahn@41961
   322
definition cons1 :: "('a::narrowing => 'b) => 'b narrowing"
bulwahn@41905
   323
where
bulwahn@41961
   324
  "cons1 f = apply (cons f) narrowing"
bulwahn@41905
   325
bulwahn@41961
   326
definition cons2 :: "('a :: narrowing => 'b :: narrowing => 'c) => 'c narrowing"
bulwahn@41905
   327
where
bulwahn@41961
   328
  "cons2 f = apply (apply (cons f) narrowing) narrowing"
bulwahn@42021
   329
bulwahn@42021
   330
definition drawn_from :: "'a list => 'a cons"
bulwahn@42021
   331
where "drawn_from xs = C (SumOfProd (map (%_. []) xs)) (map (%x y. x) xs)"
bulwahn@42021
   332
bulwahn@42021
   333
instantiation int :: narrowing
bulwahn@42021
   334
begin
bulwahn@42021
   335
bulwahn@42021
   336
definition
bulwahn@42021
   337
  "narrowing_int d = (let i = Quickcheck_Narrowing.int_of d in drawn_from [-i .. i])"
bulwahn@42021
   338
bulwahn@42021
   339
instance ..
bulwahn@42021
   340
bulwahn@42021
   341
end
bulwahn@42021
   342
bulwahn@41961
   343
instantiation unit :: narrowing
bulwahn@41905
   344
begin
bulwahn@41905
   345
bulwahn@41905
   346
definition
bulwahn@41965
   347
  "narrowing = cons ()"
bulwahn@41905
   348
bulwahn@41905
   349
instance ..
bulwahn@41905
   350
bulwahn@41905
   351
end
bulwahn@41905
   352
bulwahn@41961
   353
instantiation bool :: narrowing
bulwahn@41905
   354
begin
bulwahn@41905
   355
bulwahn@41905
   356
definition
bulwahn@41965
   357
  "narrowing = sum (cons True) (cons False)" 
bulwahn@41905
   358
bulwahn@41905
   359
instance ..
bulwahn@41905
   360
bulwahn@41905
   361
end
bulwahn@41905
   362
bulwahn@41961
   363
instantiation option :: (narrowing) narrowing
bulwahn@41905
   364
begin
bulwahn@41905
   365
bulwahn@41905
   366
definition
bulwahn@41965
   367
  "narrowing = sum (cons None) (cons1 Some)"
bulwahn@41905
   368
bulwahn@41905
   369
instance ..
bulwahn@41905
   370
bulwahn@41905
   371
end
bulwahn@41905
   372
bulwahn@41961
   373
instantiation sum :: (narrowing, narrowing) narrowing
bulwahn@41905
   374
begin
bulwahn@41905
   375
bulwahn@41905
   376
definition
bulwahn@41961
   377
  "narrowing = sum (cons1 Inl) (cons1 Inr)"
bulwahn@41905
   378
bulwahn@41905
   379
instance ..
bulwahn@41905
   380
bulwahn@41905
   381
end
bulwahn@41905
   382
bulwahn@41961
   383
instantiation list :: (narrowing) narrowing
bulwahn@41905
   384
begin
bulwahn@41905
   385
bulwahn@41961
   386
function narrowing_list :: "'a list narrowing"
bulwahn@41905
   387
where
bulwahn@41961
   388
  "narrowing_list d = sum (cons []) (apply (apply (cons Cons) narrowing) narrowing_list) d"
bulwahn@41905
   389
by pat_completeness auto
bulwahn@41905
   390
bulwahn@41912
   391
termination proof (relation "measure nat_of")
bulwahn@41912
   392
qed (auto simp add: of_int_inverse nat_of_def)
bulwahn@41912
   393
    
bulwahn@41905
   394
instance ..
bulwahn@41905
   395
bulwahn@41905
   396
end
bulwahn@41905
   397
bulwahn@41961
   398
instantiation nat :: narrowing
bulwahn@41905
   399
begin
bulwahn@41905
   400
bulwahn@41961
   401
function narrowing_nat :: "nat narrowing"
bulwahn@41905
   402
where
bulwahn@41961
   403
  "narrowing_nat d = sum (cons 0) (apply (cons Suc) narrowing_nat) d"
bulwahn@41905
   404
by pat_completeness auto
bulwahn@41905
   405
bulwahn@41912
   406
termination proof (relation "measure nat_of")
bulwahn@41912
   407
qed (auto simp add: of_int_inverse nat_of_def)
bulwahn@41905
   408
bulwahn@41905
   409
instance ..
bulwahn@41905
   410
bulwahn@41905
   411
end
bulwahn@41905
   412
bulwahn@41961
   413
instantiation Enum.finite_1 :: narrowing
bulwahn@41905
   414
begin
bulwahn@41905
   415
bulwahn@41961
   416
definition narrowing_finite_1 :: "Enum.finite_1 narrowing"
bulwahn@41905
   417
where
bulwahn@41961
   418
  "narrowing_finite_1 = cons (Enum.finite_1.a\<^isub>1 :: Enum.finite_1)"
bulwahn@41905
   419
bulwahn@41905
   420
instance ..
bulwahn@41905
   421
bulwahn@41905
   422
end
bulwahn@41905
   423
bulwahn@41961
   424
instantiation Enum.finite_2 :: narrowing
bulwahn@41905
   425
begin
bulwahn@41905
   426
bulwahn@41961
   427
definition narrowing_finite_2 :: "Enum.finite_2 narrowing"
bulwahn@41905
   428
where
bulwahn@41961
   429
  "narrowing_finite_2 = sum (cons (Enum.finite_2.a\<^isub>1 :: Enum.finite_2)) (cons (Enum.finite_2.a\<^isub>2 :: Enum.finite_2))"
bulwahn@41905
   430
bulwahn@41905
   431
instance ..
bulwahn@41905
   432
bulwahn@41905
   433
end
bulwahn@41905
   434
bulwahn@41961
   435
instantiation Enum.finite_3 :: narrowing
bulwahn@41905
   436
begin
bulwahn@41905
   437
bulwahn@41961
   438
definition narrowing_finite_3 :: "Enum.finite_3 narrowing"
bulwahn@41905
   439
where
bulwahn@41961
   440
  "narrowing_finite_3 = sum (cons (Enum.finite_3.a\<^isub>1 :: Enum.finite_3)) (sum (cons (Enum.finite_3.a\<^isub>2 :: Enum.finite_3)) (cons (Enum.finite_3.a\<^isub>3 :: Enum.finite_3)))"
bulwahn@41905
   441
bulwahn@41905
   442
instance ..
bulwahn@41905
   443
bulwahn@41905
   444
end
bulwahn@41905
   445
bulwahn@41961
   446
instantiation Enum.finite_4 :: narrowing
bulwahn@41910
   447
begin
bulwahn@41910
   448
bulwahn@41961
   449
definition narrowing_finite_4 :: "Enum.finite_4 narrowing"
bulwahn@41910
   450
where
bulwahn@41961
   451
  "narrowing_finite_4 = sum (cons Enum.finite_4.a\<^isub>1) (sum (cons Enum.finite_4.a\<^isub>2) (sum (cons Enum.finite_4.a\<^isub>3) (cons Enum.finite_4.a\<^isub>4)))"
bulwahn@41910
   452
bulwahn@41910
   453
instance ..
bulwahn@41910
   454
bulwahn@41910
   455
end
bulwahn@41910
   456
wenzelm@41943
   457
subsubsection {* class @{text is_testable} *}
bulwahn@41905
   458
wenzelm@41943
   459
text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
bulwahn@41905
   460
bulwahn@41905
   461
class is_testable
bulwahn@41905
   462
bulwahn@41905
   463
instance bool :: is_testable ..
bulwahn@41905
   464
bulwahn@43047
   465
instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
bulwahn@41905
   466
bulwahn@41905
   467
definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
bulwahn@41905
   468
where
bulwahn@41905
   469
  "ensure_testable f = f"
bulwahn@41905
   470
bulwahn@41910
   471
declare simp_thms(17,19)[code del]
bulwahn@41910
   472
bulwahn@42022
   473
subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
bulwahn@42022
   474
bulwahn@42022
   475
datatype ('a, 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun"
bulwahn@42022
   476
bulwahn@42022
   477
primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
bulwahn@42022
   478
where
bulwahn@42022
   479
  "eval_ffun (Constant c) x = c"
bulwahn@42022
   480
| "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
bulwahn@42022
   481
bulwahn@42022
   482
hide_type (open) ffun
bulwahn@42022
   483
hide_const (open) Constant Update eval_ffun
bulwahn@42022
   484
bulwahn@42024
   485
datatype 'b cfun = Constant 'b
bulwahn@42024
   486
bulwahn@42024
   487
primrec eval_cfun :: "'b cfun => 'a => 'b"
bulwahn@42024
   488
where
bulwahn@42024
   489
  "eval_cfun (Constant c) y = c"
bulwahn@42024
   490
bulwahn@42024
   491
hide_type (open) cfun
bulwahn@42024
   492
hide_const (open) Constant eval_cfun
bulwahn@42024
   493
bulwahn@42024
   494
subsubsection {* Setting up the counterexample generator *}
bulwahn@42024
   495
  
bulwahn@42024
   496
use "~~/src/HOL/Tools/Quickcheck/narrowing_generators.ML"
bulwahn@42024
   497
bulwahn@42024
   498
setup {* Narrowing_Generators.setup *}
bulwahn@42024
   499
bulwahn@43047
   500
hide_type (open) code_int narrowing_type narrowing_term cons
bulwahn@42024
   501
hide_const (open) int_of of_int nth error toEnum map_index split_At empty
bulwahn@43047
   502
  C cons conv nonEmpty "apply" sum cons1 cons2 ensure_testable
bulwahn@42022
   503
bulwahn@41905
   504
end