src/HOL/Quickcheck_Narrowing.thy
author paulson <lp15@cam.ac.uk>
Tue Apr 25 16:39:54 2017 +0100 (2017-04-25)
changeset 65578 e4997c181cce
parent 65482 721feefce9c6
child 66148 5e60c2d0a1f1
permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
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 cons :: "'a => 'a narrowing"
bulwahn@41905
    88
where
blanchet@58152
    89
  "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(\<lambda>_. a)])"
bulwahn@41905
    90
bulwahn@43047
    91
fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
bulwahn@41905
    92
where
bulwahn@46758
    93
  "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)"
bulwahn@46758
    94
| "conv cs (Narrowing_constructor i xs) = (nth cs i) xs"
bulwahn@41905
    95
bulwahn@46758
    96
fun non_empty :: "narrowing_type => bool"
bulwahn@41905
    97
where
bulwahn@46758
    98
  "non_empty (Narrowing_sum_of_products ps) = (\<not> (List.null ps))"
bulwahn@41905
    99
bulwahn@41961
   100
definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
bulwahn@41905
   101
where
haftmann@65482
   102
  "apply f a d = (if d > 0 then
haftmann@65480
   103
     (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs \<Rightarrow>
haftmann@65480
   104
       case a (d - 1) of Narrowing_cons ta cas \<Rightarrow>
bulwahn@41905
   105
       let
haftmann@65482
   106
         shallow = non_empty ta;
haftmann@65480
   107
         cs = [(\<lambda>(x # xs) \<Rightarrow> cf xs (conv cas x)). shallow, cf \<leftarrow> cfs]
haftmann@65482
   108
       in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p \<leftarrow> ps]) cs)
haftmann@65482
   109
     else Narrowing_cons (Narrowing_sum_of_products []) [])"
bulwahn@41905
   110
bulwahn@41961
   111
definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
bulwahn@41905
   112
where
bulwahn@41905
   113
  "sum a b d =
bulwahn@46758
   114
    (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca => 
bulwahn@46758
   115
      case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb =>
bulwahn@46758
   116
      Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))"
bulwahn@41905
   117
bulwahn@41912
   118
lemma [fundef_cong]:
bulwahn@41912
   119
  assumes "a d = a' d" "b d = b' d" "d = d'"
bulwahn@41912
   120
  shows "sum a b d = sum a' b' d'"
bulwahn@46758
   121
using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split)
bulwahn@41912
   122
bulwahn@41912
   123
lemma [fundef_cong]:
haftmann@51143
   124
  assumes "f d = f' d" "(\<And>d'. 0 \<le> d' \<and> d' < d \<Longrightarrow> a d' = a' d')"
bulwahn@41912
   125
  assumes "d = d'"
bulwahn@41912
   126
  shows "apply f a d = apply f' a' d'"
bulwahn@41912
   127
proof -
haftmann@51143
   128
  note assms
haftmann@51143
   129
  moreover have "0 < d' \<Longrightarrow> 0 \<le> d' - 1"
haftmann@51143
   130
    by (simp add: less_integer_def less_eq_integer_def)
bulwahn@41912
   131
  ultimately show ?thesis
haftmann@51143
   132
    by (auto simp add: apply_def Let_def
haftmann@51143
   133
      split: narrowing_cons.split narrowing_type.split)
bulwahn@41912
   134
qed
bulwahn@41912
   135
wenzelm@60758
   136
subsubsection \<open>Narrowing generator type class\<close>
bulwahn@41905
   137
bulwahn@41961
   138
class narrowing =
haftmann@51143
   139
  fixes narrowing :: "integer => 'a narrowing_cons"
bulwahn@41905
   140
blanchet@58350
   141
datatype (plugins only: code extraction) property =
blanchet@58334
   142
  Universal narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term"
blanchet@58334
   143
| Existential narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term"
blanchet@58334
   144
| Property bool
bulwahn@43237
   145
bulwahn@43237
   146
(* FIXME: hard-wired maximal depth of 100 here *)
bulwahn@43315
   147
definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
bulwahn@43237
   148
where
haftmann@51143
   149
  "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
   150
bulwahn@43315
   151
definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
bulwahn@43237
   152
where
haftmann@51143
   153
  "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
   154
wenzelm@61799
   155
subsubsection \<open>class \<open>is_testable\<close>\<close>
bulwahn@41905
   156
wenzelm@61799
   157
text \<open>The class \<open>is_testable\<close> ensures that all necessary type instances are generated.\<close>
bulwahn@41905
   158
bulwahn@41905
   159
class is_testable
bulwahn@41905
   160
bulwahn@41905
   161
instance bool :: is_testable ..
bulwahn@41905
   162
bulwahn@43047
   163
instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
bulwahn@41905
   164
bulwahn@41905
   165
definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
bulwahn@41905
   166
where
bulwahn@41905
   167
  "ensure_testable f = f"
bulwahn@41905
   168
bulwahn@41910
   169
wenzelm@60758
   170
subsubsection \<open>Defining a simple datatype to represent functions in an incomplete and redundant way\<close>
bulwahn@42022
   171
blanchet@58350
   172
datatype (plugins only: code quickcheck_narrowing extraction) (dead 'a, dead 'b) ffun =
blanchet@58334
   173
  Constant 'b
blanchet@58334
   174
| Update 'a 'b "('a, 'b) ffun"
bulwahn@42022
   175
bulwahn@42022
   176
primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
bulwahn@42022
   177
where
bulwahn@42022
   178
  "eval_ffun (Constant c) x = c"
bulwahn@42022
   179
| "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
bulwahn@42022
   180
bulwahn@42022
   181
hide_type (open) ffun
bulwahn@42022
   182
hide_const (open) Constant Update eval_ffun
bulwahn@42022
   183
blanchet@58350
   184
datatype (plugins only: code quickcheck_narrowing extraction) (dead 'b) cfun = Constant 'b
bulwahn@42024
   185
bulwahn@42024
   186
primrec eval_cfun :: "'b cfun => 'a => 'b"
bulwahn@42024
   187
where
bulwahn@42024
   188
  "eval_cfun (Constant c) y = c"
bulwahn@42024
   189
bulwahn@42024
   190
hide_type (open) cfun
huffman@45734
   191
hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
bulwahn@42024
   192
wenzelm@60758
   193
subsubsection \<open>Setting up the counterexample generator\<close>
bulwahn@43237
   194
wenzelm@48891
   195
ML_file "Tools/Quickcheck/narrowing_generators.ML"
bulwahn@42024
   196
bulwahn@45001
   197
definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
bulwahn@45001
   198
where
bulwahn@45001
   199
  "narrowing_dummy_partial_term_of = partial_term_of"
bulwahn@45001
   200
haftmann@51143
   201
definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons"
bulwahn@45001
   202
where
bulwahn@45001
   203
  "narrowing_dummy_narrowing = narrowing"
bulwahn@45001
   204
bulwahn@45001
   205
lemma [code]:
bulwahn@45001
   206
  "ensure_testable f =
bulwahn@45001
   207
    (let
haftmann@51143
   208
      x = narrowing_dummy_narrowing :: integer => bool narrowing_cons;
bulwahn@45001
   209
      y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
bulwahn@45001
   210
      z = (conv :: _ => _ => unit)  in f)"
bulwahn@45001
   211
unfolding Let_def ensure_testable_def ..
bulwahn@45001
   212
wenzelm@60758
   213
subsection \<open>Narrowing for sets\<close>
bulwahn@46308
   214
bulwahn@46308
   215
instantiation set :: (narrowing) narrowing
bulwahn@46308
   216
begin
bulwahn@46308
   217
bulwahn@46308
   218
definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
bulwahn@46308
   219
bulwahn@46308
   220
instance ..
bulwahn@46308
   221
bulwahn@46308
   222
end
bulwahn@45001
   223
  
wenzelm@60758
   224
subsection \<open>Narrowing for integers\<close>
bulwahn@43356
   225
bulwahn@43356
   226
haftmann@51143
   227
definition drawn_from :: "'a list \<Rightarrow> 'a narrowing_cons"
haftmann@51143
   228
where
haftmann@51143
   229
  "drawn_from xs =
haftmann@51143
   230
    Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)"
bulwahn@43356
   231
haftmann@51143
   232
function around_zero :: "int \<Rightarrow> int list"
bulwahn@43356
   233
where
bulwahn@43356
   234
  "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
haftmann@51143
   235
  by pat_completeness auto
bulwahn@43356
   236
termination by (relation "measure nat") auto
bulwahn@43356
   237
haftmann@51143
   238
declare around_zero.simps [simp del]
bulwahn@43356
   239
bulwahn@43356
   240
lemma length_around_zero:
bulwahn@43356
   241
  assumes "i >= 0" 
bulwahn@43356
   242
  shows "length (around_zero i) = 2 * nat i + 1"
haftmann@51143
   243
proof (induct rule: int_ge_induct [OF assms])
bulwahn@43356
   244
  case 1
bulwahn@43356
   245
  from 1 show ?case by (simp add: around_zero.simps)
bulwahn@43356
   246
next
bulwahn@43356
   247
  case (2 i)
bulwahn@43356
   248
  from 2 show ?case
haftmann@51143
   249
    by (simp add: around_zero.simps [of "i + 1"])
bulwahn@43356
   250
qed
bulwahn@43356
   251
bulwahn@43356
   252
instantiation int :: narrowing
bulwahn@43356
   253
begin
bulwahn@43356
   254
bulwahn@43356
   255
definition
haftmann@51143
   256
  "narrowing_int d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
haftmann@51143
   257
    in drawn_from (around_zero i))"
bulwahn@43356
   258
bulwahn@43356
   259
instance ..
bulwahn@43356
   260
bulwahn@43356
   261
end
bulwahn@43356
   262
haftmann@51143
   263
lemma [code, code del]: "partial_term_of (ty :: int itself) t \<equiv> undefined"
haftmann@51143
   264
  by (rule partial_term_of_anything)+
bulwahn@43356
   265
bulwahn@43356
   266
lemma [code]:
haftmann@51143
   267
  "partial_term_of (ty :: int itself) (Narrowing_variable p t) \<equiv>
haftmann@51143
   268
    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
haftmann@51143
   269
  "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \<equiv>
haftmann@51143
   270
    (if i mod 2 = 0
haftmann@51143
   271
     then Code_Evaluation.term_of (- (int_of_integer i) div 2)
haftmann@51143
   272
     else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))"
haftmann@51143
   273
  by (rule partial_term_of_anything)+
haftmann@51143
   274
haftmann@51143
   275
instantiation integer :: narrowing
haftmann@51143
   276
begin
haftmann@51143
   277
haftmann@51143
   278
definition
haftmann@51143
   279
  "narrowing_integer d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
haftmann@51143
   280
    in drawn_from (map integer_of_int (around_zero i)))"
haftmann@51143
   281
haftmann@51143
   282
instance ..
haftmann@51143
   283
haftmann@51143
   284
end
haftmann@51143
   285
haftmann@51143
   286
lemma [code, code del]: "partial_term_of (ty :: integer itself) t \<equiv> undefined"
haftmann@51143
   287
  by (rule partial_term_of_anything)+
haftmann@51143
   288
haftmann@51143
   289
lemma [code]:
haftmann@51143
   290
  "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \<equiv>
haftmann@51143
   291
    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])"
haftmann@51143
   292
  "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \<equiv>
haftmann@51143
   293
    (if i mod 2 = 0
haftmann@51143
   294
     then Code_Evaluation.term_of (- i div 2)
haftmann@51143
   295
     else Code_Evaluation.term_of ((i + 1) div 2))"
haftmann@51143
   296
  by (rule partial_term_of_anything)+
bulwahn@43356
   297
Andreas@56401
   298
code_printing constant "Code_Evaluation.term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Haskell_Quickcheck) 
Andreas@56401
   299
  "(let { t = Typerep.Typerep \"Code'_Numeral.integer\" [];
Andreas@56401
   300
     mkFunT s t = Typerep.Typerep \"fun\" [s, t];
Andreas@56401
   301
     numT = Typerep.Typerep \"Num.num\" [];
Andreas@56401
   302
     mkBit 0 = Generated'_Code.Const \"Num.num.Bit0\" (mkFunT numT numT);
Andreas@56401
   303
     mkBit 1 = Generated'_Code.Const \"Num.num.Bit1\" (mkFunT numT numT);
Andreas@56401
   304
     mkNumeral 1 = Generated'_Code.Const \"Num.num.One\" numT;
Andreas@56401
   305
     mkNumeral i = let { q = i `Prelude.div` 2; r = i `Prelude.mod` 2 }
Andreas@56401
   306
       in Generated'_Code.App (mkBit r) (mkNumeral q);
Andreas@56401
   307
     mkNumber 0 = Generated'_Code.Const \"Groups.zero'_class.zero\" t;
Andreas@56401
   308
     mkNumber 1 = Generated'_Code.Const \"Groups.one'_class.one\" t;
Andreas@56401
   309
     mkNumber i = if i > 0 then
Andreas@56401
   310
         Generated'_Code.App
Andreas@56401
   311
           (Generated'_Code.Const \"Num.numeral'_class.numeral\"
Andreas@56401
   312
              (mkFunT numT t))
Andreas@56401
   313
           (mkNumeral i)
Andreas@56401
   314
       else
Andreas@56401
   315
         Generated'_Code.App
Andreas@56401
   316
           (Generated'_Code.Const \"Groups.uminus'_class.uminus\" (mkFunT t t))
Andreas@56401
   317
           (mkNumber (- i)); } in mkNumber)"
bulwahn@43356
   318
wenzelm@61799
   319
subsection \<open>The \<open>find_unused_assms\<close> command\<close>
bulwahn@46589
   320
wenzelm@48891
   321
ML_file "Tools/Quickcheck/find_unused_assms.ML"
bulwahn@46589
   322
wenzelm@60758
   323
subsection \<open>Closing up\<close>
bulwahn@46589
   324
haftmann@51143
   325
hide_type narrowing_type narrowing_term narrowing_cons property
haftmann@51143
   326
hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero
bulwahn@46758
   327
hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons
bulwahn@46758
   328
hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def
bulwahn@42022
   329
bulwahn@45001
   330
end