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