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