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