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