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