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