src/HOL/Quickcheck_Narrowing.thy
author haftmann
Sun Jun 23 21:16:07 2013 +0200 (2013-06-23)
changeset 52435 6646bb548c6b
parent 51143 0a2371e7ced3
child 55147 bce3dbc11f95
permissions -rw-r--r--
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
bulwahn@41905
     1
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@41905
     2
bulwahn@43356
     3
header {* Counterexample generator performing narrowing-based testing *}
bulwahn@41905
     4
bulwahn@41930
     5
theory Quickcheck_Narrowing
bulwahn@43312
     6
imports Quickcheck_Exhaustive
wenzelm@46950
     7
keywords "find_unused_assms" :: diag
bulwahn@41905
     8
begin
bulwahn@41905
     9
bulwahn@41905
    10
subsection {* Counterexample generator *}
bulwahn@41905
    11
haftmann@51143
    12
subsubsection {* Code generation setup *}
bulwahn@43308
    13
bulwahn@43308
    14
setup {* Code_Target.extend_target ("Haskell_Quickcheck", (Code_Haskell.target, K I)) *}
bulwahn@43308
    15
haftmann@52435
    16
code_printing
haftmann@52435
    17
  type_constructor typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep"
haftmann@52435
    18
| constant Typerep.Typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep"
haftmann@52435
    19
| type_constructor integer \<rightharpoonup> (Haskell_Quickcheck) "Prelude.Int"
haftmann@51143
    20
bulwahn@43308
    21
code_reserved Haskell_Quickcheck Typerep
bulwahn@41909
    22
bulwahn@42021
    23
bulwahn@41961
    24
subsubsection {* Narrowing's deep representation of types and terms *}
bulwahn@41905
    25
bulwahn@46758
    26
datatype narrowing_type = Narrowing_sum_of_products "narrowing_type list list"
haftmann@51143
    27
datatype narrowing_term = Narrowing_variable "integer list" narrowing_type | Narrowing_constructor integer "narrowing_term list"
bulwahn@46758
    28
datatype 'a narrowing_cons = Narrowing_cons narrowing_type "(narrowing_term list => 'a) list"
bulwahn@41905
    29
bulwahn@46758
    30
primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons"
bulwahn@43356
    31
where
bulwahn@46758
    32
  "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (%c. f o c) cs)"
bulwahn@43356
    33
hoelzl@43341
    34
subsubsection {* From narrowing's deep representation of terms to @{theory Code_Evaluation}'s terms *}
bulwahn@42980
    35
bulwahn@42980
    36
class partial_term_of = typerep +
bulwahn@43047
    37
  fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term"
bulwahn@43047
    38
bulwahn@43047
    39
lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t"
bulwahn@43047
    40
  by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp)
bulwahn@43356
    41
 
bulwahn@41964
    42
subsubsection {* Auxilary functions for Narrowing *}
bulwahn@41905
    43
haftmann@51143
    44
consts nth :: "'a list => integer => 'a"
bulwahn@41905
    45
haftmann@52435
    46
code_printing constant nth \<rightharpoonup> (Haskell_Quickcheck) infixl 9 "!!"
bulwahn@41905
    47
bulwahn@41908
    48
consts error :: "char list => 'a"
bulwahn@41905
    49
haftmann@52435
    50
code_printing constant error \<rightharpoonup> (Haskell_Quickcheck) "error"
bulwahn@41905
    51
haftmann@51143
    52
consts toEnum :: "integer => char"
bulwahn@41908
    53
haftmann@52435
    54
code_printing constant toEnum \<rightharpoonup> (Haskell_Quickcheck) "Prelude.toEnum"
bulwahn@41905
    55
bulwahn@43316
    56
consts marker :: "char"
bulwahn@41905
    57
haftmann@52435
    58
code_printing constant marker \<rightharpoonup> (Haskell_Quickcheck) "''\\0'"
bulwahn@43316
    59
bulwahn@41961
    60
subsubsection {* Narrowing's basic operations *}
bulwahn@41905
    61
haftmann@51143
    62
type_synonym 'a narrowing = "integer => 'a narrowing_cons"
bulwahn@41905
    63
bulwahn@41961
    64
definition empty :: "'a narrowing"
bulwahn@41905
    65
where
bulwahn@46758
    66
  "empty d = Narrowing_cons (Narrowing_sum_of_products []) []"
bulwahn@41905
    67
  
bulwahn@41961
    68
definition cons :: "'a => 'a narrowing"
bulwahn@41905
    69
where
bulwahn@46758
    70
  "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(%_. a)])"
bulwahn@41905
    71
bulwahn@43047
    72
fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
bulwahn@41905
    73
where
bulwahn@46758
    74
  "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)"
bulwahn@46758
    75
| "conv cs (Narrowing_constructor i xs) = (nth cs i) xs"
bulwahn@41905
    76
bulwahn@46758
    77
fun non_empty :: "narrowing_type => bool"
bulwahn@41905
    78
where
bulwahn@46758
    79
  "non_empty (Narrowing_sum_of_products ps) = (\<not> (List.null ps))"
bulwahn@41905
    80
bulwahn@41961
    81
definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
bulwahn@41905
    82
where
bulwahn@41905
    83
  "apply f a d =
bulwahn@46758
    84
     (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs =>
bulwahn@46758
    85
       case a (d - 1) of Narrowing_cons ta cas =>
bulwahn@41905
    86
       let
bulwahn@46758
    87
         shallow = (d > 0 \<and> non_empty ta);
bulwahn@41905
    88
         cs = [(%xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs]
bulwahn@46758
    89
       in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p <- ps]) cs)"
bulwahn@41905
    90
bulwahn@41961
    91
definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
bulwahn@41905
    92
where
bulwahn@41905
    93
  "sum a b d =
bulwahn@46758
    94
    (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca => 
bulwahn@46758
    95
      case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb =>
bulwahn@46758
    96
      Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))"
bulwahn@41905
    97
bulwahn@41912
    98
lemma [fundef_cong]:
bulwahn@41912
    99
  assumes "a d = a' d" "b d = b' d" "d = d'"
bulwahn@41912
   100
  shows "sum a b d = sum a' b' d'"
bulwahn@46758
   101
using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split)
bulwahn@41912
   102
bulwahn@41912
   103
lemma [fundef_cong]:
haftmann@51143
   104
  assumes "f d = f' d" "(\<And>d'. 0 \<le> d' \<and> d' < d \<Longrightarrow> a d' = a' d')"
bulwahn@41912
   105
  assumes "d = d'"
bulwahn@41912
   106
  shows "apply f a d = apply f' a' d'"
bulwahn@41912
   107
proof -
haftmann@51143
   108
  note assms
haftmann@51143
   109
  moreover have "0 < d' \<Longrightarrow> 0 \<le> d' - 1"
haftmann@51143
   110
    by (simp add: less_integer_def less_eq_integer_def)
bulwahn@41912
   111
  ultimately show ?thesis
haftmann@51143
   112
    by (auto simp add: apply_def Let_def
haftmann@51143
   113
      split: narrowing_cons.split narrowing_type.split)
bulwahn@41912
   114
qed
bulwahn@41912
   115
bulwahn@41961
   116
subsubsection {* Narrowing generator type class *}
bulwahn@41905
   117
bulwahn@41961
   118
class narrowing =
haftmann@51143
   119
  fixes narrowing :: "integer => 'a narrowing_cons"
bulwahn@41905
   120
bulwahn@43237
   121
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
   122
bulwahn@43237
   123
(* FIXME: hard-wired maximal depth of 100 here *)
bulwahn@43315
   124
definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
bulwahn@43237
   125
where
haftmann@51143
   126
  "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
bulwahn@43237
   127
bulwahn@43315
   128
definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
bulwahn@43237
   129
where
haftmann@51143
   130
  "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
bulwahn@43237
   131
wenzelm@41943
   132
subsubsection {* class @{text is_testable} *}
bulwahn@41905
   133
wenzelm@41943
   134
text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
bulwahn@41905
   135
bulwahn@41905
   136
class is_testable
bulwahn@41905
   137
bulwahn@41905
   138
instance bool :: is_testable ..
bulwahn@41905
   139
bulwahn@43047
   140
instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
bulwahn@41905
   141
bulwahn@41905
   142
definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
bulwahn@41905
   143
where
bulwahn@41905
   144
  "ensure_testable f = f"
bulwahn@41905
   145
bulwahn@41910
   146
bulwahn@42022
   147
subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
bulwahn@42022
   148
bulwahn@42022
   149
datatype ('a, 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun"
bulwahn@42022
   150
bulwahn@42022
   151
primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
bulwahn@42022
   152
where
bulwahn@42022
   153
  "eval_ffun (Constant c) x = c"
bulwahn@42022
   154
| "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
bulwahn@42022
   155
bulwahn@42022
   156
hide_type (open) ffun
bulwahn@42022
   157
hide_const (open) Constant Update eval_ffun
bulwahn@42022
   158
bulwahn@42024
   159
datatype 'b cfun = Constant 'b
bulwahn@42024
   160
bulwahn@42024
   161
primrec eval_cfun :: "'b cfun => 'a => 'b"
bulwahn@42024
   162
where
bulwahn@42024
   163
  "eval_cfun (Constant c) y = c"
bulwahn@42024
   164
bulwahn@42024
   165
hide_type (open) cfun
huffman@45734
   166
hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
bulwahn@42024
   167
bulwahn@42024
   168
subsubsection {* Setting up the counterexample generator *}
bulwahn@43237
   169
wenzelm@48891
   170
ML_file "Tools/Quickcheck/narrowing_generators.ML"
bulwahn@42024
   171
bulwahn@42024
   172
setup {* Narrowing_Generators.setup *}
bulwahn@42024
   173
bulwahn@45001
   174
definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
bulwahn@45001
   175
where
bulwahn@45001
   176
  "narrowing_dummy_partial_term_of = partial_term_of"
bulwahn@45001
   177
haftmann@51143
   178
definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons"
bulwahn@45001
   179
where
bulwahn@45001
   180
  "narrowing_dummy_narrowing = narrowing"
bulwahn@45001
   181
bulwahn@45001
   182
lemma [code]:
bulwahn@45001
   183
  "ensure_testable f =
bulwahn@45001
   184
    (let
haftmann@51143
   185
      x = narrowing_dummy_narrowing :: integer => bool narrowing_cons;
bulwahn@45001
   186
      y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
bulwahn@45001
   187
      z = (conv :: _ => _ => unit)  in f)"
bulwahn@45001
   188
unfolding Let_def ensure_testable_def ..
bulwahn@45001
   189
bulwahn@46308
   190
subsection {* Narrowing for sets *}
bulwahn@46308
   191
bulwahn@46308
   192
instantiation set :: (narrowing) narrowing
bulwahn@46308
   193
begin
bulwahn@46308
   194
bulwahn@46308
   195
definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
bulwahn@46308
   196
bulwahn@46308
   197
instance ..
bulwahn@46308
   198
bulwahn@46308
   199
end
bulwahn@45001
   200
  
bulwahn@43356
   201
subsection {* Narrowing for integers *}
bulwahn@43356
   202
bulwahn@43356
   203
haftmann@51143
   204
definition drawn_from :: "'a list \<Rightarrow> 'a narrowing_cons"
haftmann@51143
   205
where
haftmann@51143
   206
  "drawn_from xs =
haftmann@51143
   207
    Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)"
bulwahn@43356
   208
haftmann@51143
   209
function around_zero :: "int \<Rightarrow> int list"
bulwahn@43356
   210
where
bulwahn@43356
   211
  "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
haftmann@51143
   212
  by pat_completeness auto
bulwahn@43356
   213
termination by (relation "measure nat") auto
bulwahn@43356
   214
haftmann@51143
   215
declare around_zero.simps [simp del]
bulwahn@43356
   216
bulwahn@43356
   217
lemma length_around_zero:
bulwahn@43356
   218
  assumes "i >= 0" 
bulwahn@43356
   219
  shows "length (around_zero i) = 2 * nat i + 1"
haftmann@51143
   220
proof (induct rule: int_ge_induct [OF assms])
bulwahn@43356
   221
  case 1
bulwahn@43356
   222
  from 1 show ?case by (simp add: around_zero.simps)
bulwahn@43356
   223
next
bulwahn@43356
   224
  case (2 i)
bulwahn@43356
   225
  from 2 show ?case
haftmann@51143
   226
    by (simp add: around_zero.simps [of "i + 1"])
bulwahn@43356
   227
qed
bulwahn@43356
   228
bulwahn@43356
   229
instantiation int :: narrowing
bulwahn@43356
   230
begin
bulwahn@43356
   231
bulwahn@43356
   232
definition
haftmann@51143
   233
  "narrowing_int d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
haftmann@51143
   234
    in drawn_from (around_zero i))"
bulwahn@43356
   235
bulwahn@43356
   236
instance ..
bulwahn@43356
   237
bulwahn@43356
   238
end
bulwahn@43356
   239
haftmann@51143
   240
lemma [code, code del]: "partial_term_of (ty :: int itself) t \<equiv> undefined"
haftmann@51143
   241
  by (rule partial_term_of_anything)+
bulwahn@43356
   242
bulwahn@43356
   243
lemma [code]:
haftmann@51143
   244
  "partial_term_of (ty :: int itself) (Narrowing_variable p t) \<equiv>
haftmann@51143
   245
    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
haftmann@51143
   246
  "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \<equiv>
haftmann@51143
   247
    (if i mod 2 = 0
haftmann@51143
   248
     then Code_Evaluation.term_of (- (int_of_integer i) div 2)
haftmann@51143
   249
     else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))"
haftmann@51143
   250
  by (rule partial_term_of_anything)+
haftmann@51143
   251
haftmann@51143
   252
instantiation integer :: narrowing
haftmann@51143
   253
begin
haftmann@51143
   254
haftmann@51143
   255
definition
haftmann@51143
   256
  "narrowing_integer d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
haftmann@51143
   257
    in drawn_from (map integer_of_int (around_zero i)))"
haftmann@51143
   258
haftmann@51143
   259
instance ..
haftmann@51143
   260
haftmann@51143
   261
end
haftmann@51143
   262
haftmann@51143
   263
lemma [code, code del]: "partial_term_of (ty :: integer itself) t \<equiv> undefined"
haftmann@51143
   264
  by (rule partial_term_of_anything)+
haftmann@51143
   265
haftmann@51143
   266
lemma [code]:
haftmann@51143
   267
  "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \<equiv>
haftmann@51143
   268
    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])"
haftmann@51143
   269
  "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \<equiv>
haftmann@51143
   270
    (if i mod 2 = 0
haftmann@51143
   271
     then Code_Evaluation.term_of (- i div 2)
haftmann@51143
   272
     else Code_Evaluation.term_of ((i + 1) div 2))"
haftmann@51143
   273
  by (rule partial_term_of_anything)+
bulwahn@43356
   274
bulwahn@43356
   275
bulwahn@46589
   276
subsection {* The @{text find_unused_assms} command *}
bulwahn@46589
   277
wenzelm@48891
   278
ML_file "Tools/Quickcheck/find_unused_assms.ML"
bulwahn@46589
   279
bulwahn@46589
   280
subsection {* Closing up *}
bulwahn@46589
   281
haftmann@51143
   282
hide_type narrowing_type narrowing_term narrowing_cons property
haftmann@51143
   283
hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero
bulwahn@46758
   284
hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons
bulwahn@46758
   285
hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def
bulwahn@42022
   286
bulwahn@45001
   287
end
haftmann@51143
   288