src/HOL/Quickcheck_Narrowing.thy
author webertj
Fri Oct 19 15:12:52 2012 +0200 (2012-10-19)
changeset 49962 a8cc904a6820
parent 49834 b27bbb021df1
child 50046 0051dc4f301f
permissions -rw-r--r--
Renamed {left,right}_distrib to distrib_{right,left}.
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
bulwahn@43308
    12
text {* We create a new target for the necessary 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
subsubsection {* Code generation setup *}
bulwahn@41909
    17
bulwahn@41909
    18
code_type typerep
bulwahn@43308
    19
  (Haskell_Quickcheck "Typerep")
bulwahn@41909
    20
bulwahn@41909
    21
code_const Typerep.Typerep
bulwahn@43308
    22
  (Haskell_Quickcheck "Typerep")
bulwahn@41909
    23
bulwahn@43308
    24
code_reserved Haskell_Quickcheck Typerep
bulwahn@41909
    25
hoelzl@43341
    26
subsubsection {* Type @{text "code_int"} for Haskell Quickcheck's Int type *}
bulwahn@41908
    27
wenzelm@49834
    28
typedef code_int = "UNIV \<Colon> int set"
bulwahn@41908
    29
  morphisms int_of of_int by rule
bulwahn@41908
    30
bulwahn@42021
    31
lemma of_int_int_of [simp]:
bulwahn@42021
    32
  "of_int (int_of k) = k"
bulwahn@42021
    33
  by (rule int_of_inverse)
bulwahn@42021
    34
bulwahn@42021
    35
lemma int_of_of_int [simp]:
bulwahn@42021
    36
  "int_of (of_int n) = n"
bulwahn@42021
    37
  by (rule of_int_inverse) (rule UNIV_I)
bulwahn@42021
    38
bulwahn@42021
    39
lemma code_int:
bulwahn@42021
    40
  "(\<And>n\<Colon>code_int. PROP P n) \<equiv> (\<And>n\<Colon>int. PROP P (of_int n))"
bulwahn@42021
    41
proof
bulwahn@42021
    42
  fix n :: int
bulwahn@42021
    43
  assume "\<And>n\<Colon>code_int. PROP P n"
bulwahn@42021
    44
  then show "PROP P (of_int n)" .
bulwahn@42021
    45
next
bulwahn@42021
    46
  fix n :: code_int
bulwahn@42021
    47
  assume "\<And>n\<Colon>int. PROP P (of_int n)"
bulwahn@42021
    48
  then have "PROP P (of_int (int_of n))" .
bulwahn@42021
    49
  then show "PROP P n" by simp
bulwahn@42021
    50
qed
bulwahn@42021
    51
bulwahn@42021
    52
bulwahn@41908
    53
lemma int_of_inject [simp]:
bulwahn@41908
    54
  "int_of k = int_of l \<longleftrightarrow> k = l"
bulwahn@41908
    55
  by (rule int_of_inject)
bulwahn@41908
    56
bulwahn@42021
    57
lemma of_int_inject [simp]:
bulwahn@42021
    58
  "of_int n = of_int m \<longleftrightarrow> n = m"
bulwahn@42021
    59
  by (rule of_int_inject) (rule UNIV_I)+
bulwahn@42021
    60
bulwahn@42021
    61
instantiation code_int :: equal
bulwahn@42021
    62
begin
bulwahn@42021
    63
bulwahn@42021
    64
definition
bulwahn@42021
    65
  "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
bulwahn@42021
    66
bulwahn@42021
    67
instance proof
huffman@47108
    68
qed (auto simp add: equal_code_int_def equal_int_def equal_int_refl)
bulwahn@42021
    69
bulwahn@42021
    70
end
bulwahn@42021
    71
bulwahn@41912
    72
definition nat_of :: "code_int => nat"
bulwahn@41912
    73
where
bulwahn@41912
    74
  "nat_of i = nat (int_of i)"
bulwahn@42980
    75
  
huffman@47108
    76
instantiation code_int :: "{minus, linordered_semidom, semiring_div, neg_numeral, linorder}"
bulwahn@41908
    77
begin
bulwahn@41908
    78
bulwahn@41908
    79
definition [simp, code del]:
bulwahn@41908
    80
  "0 = of_int 0"
bulwahn@41908
    81
bulwahn@41908
    82
definition [simp, code del]:
bulwahn@41908
    83
  "1 = of_int 1"
bulwahn@41908
    84
bulwahn@41908
    85
definition [simp, code del]:
bulwahn@42021
    86
  "n + m = of_int (int_of n + int_of m)"
bulwahn@42021
    87
bulwahn@42021
    88
definition [simp, code del]:
huffman@47108
    89
  "- n = of_int (- int_of n)"
huffman@47108
    90
huffman@47108
    91
definition [simp, code del]:
bulwahn@41908
    92
  "n - m = of_int (int_of n - int_of m)"
bulwahn@41908
    93
bulwahn@41908
    94
definition [simp, code del]:
bulwahn@42021
    95
  "n * m = of_int (int_of n * int_of m)"
bulwahn@42021
    96
bulwahn@42021
    97
definition [simp, code del]:
bulwahn@42021
    98
  "n div m = of_int (int_of n div int_of m)"
bulwahn@42021
    99
bulwahn@42021
   100
definition [simp, code del]:
bulwahn@42021
   101
  "n mod m = of_int (int_of n mod int_of m)"
bulwahn@42021
   102
bulwahn@42021
   103
definition [simp, code del]:
bulwahn@41908
   104
  "n \<le> m \<longleftrightarrow> int_of n \<le> int_of m"
bulwahn@41908
   105
bulwahn@41908
   106
definition [simp, code del]:
bulwahn@41908
   107
  "n < m \<longleftrightarrow> int_of n < int_of m"
bulwahn@41908
   108
bulwahn@42021
   109
instance proof
webertj@49962
   110
qed (auto simp add: code_int distrib_right zmult_zless_mono2)
bulwahn@41908
   111
bulwahn@41908
   112
end
bulwahn@42980
   113
huffman@47108
   114
lemma int_of_numeral [simp]:
huffman@47108
   115
  "int_of (numeral k) = numeral k"
huffman@47108
   116
  by (induct k) (simp_all only: numeral.simps plus_code_int_def
huffman@47108
   117
    one_code_int_def of_int_inverse UNIV_I)
huffman@47108
   118
huffman@47108
   119
definition Num :: "num \<Rightarrow> code_int"
huffman@47108
   120
  where [code_abbrev]: "Num = numeral"
huffman@47108
   121
huffman@47108
   122
lemma [code_abbrev]:
huffman@47108
   123
  "- numeral k = (neg_numeral k :: code_int)"
huffman@47108
   124
  by (unfold neg_numeral_def) simp
huffman@47108
   125
huffman@47108
   126
code_datatype "0::code_int" Num
bulwahn@41908
   127
bulwahn@42980
   128
lemma one_code_int_code [code, code_unfold]:
bulwahn@41908
   129
  "(1\<Colon>code_int) = Numeral1"
huffman@47108
   130
  by (simp only: numeral.simps)
bulwahn@41908
   131
huffman@47108
   132
definition div_mod :: "code_int \<Rightarrow> code_int \<Rightarrow> code_int \<times> code_int" where
huffman@47108
   133
  [code del]: "div_mod n m = (n div m, n mod m)"
bulwahn@42021
   134
bulwahn@42021
   135
lemma [code]:
huffman@47108
   136
  "n div m = fst (div_mod n m)"
huffman@47108
   137
  unfolding div_mod_def by simp
bulwahn@42021
   138
bulwahn@42021
   139
lemma [code]:
huffman@47108
   140
  "n mod m = snd (div_mod n m)"
huffman@47108
   141
  unfolding div_mod_def by simp
bulwahn@42021
   142
bulwahn@42021
   143
lemma int_of_code [code]:
bulwahn@42021
   144
  "int_of k = (if k = 0 then 0
bulwahn@42021
   145
    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
bulwahn@42021
   146
proof -
bulwahn@42021
   147
  have 1: "(int_of k div 2) * 2 + int_of k mod 2 = int_of k" 
bulwahn@42021
   148
    by (rule mod_div_equality)
bulwahn@42021
   149
  have "int_of k mod 2 = 0 \<or> int_of k mod 2 = 1" by auto
bulwahn@42021
   150
  from this show ?thesis
bulwahn@42021
   151
    apply auto
bulwahn@42021
   152
    apply (insert 1) by (auto simp add: mult_ac)
bulwahn@42021
   153
qed
bulwahn@42021
   154
bulwahn@42021
   155
bulwahn@42021
   156
code_instance code_numeral :: equal
bulwahn@43308
   157
  (Haskell_Quickcheck -)
bulwahn@42021
   158
huffman@47108
   159
setup {* fold (Numeral.add_code @{const_name Num}
bulwahn@43308
   160
  false Code_Printer.literal_numeral) ["Haskell_Quickcheck"]  *}
bulwahn@42021
   161
huffman@47108
   162
code_type code_int
haftmann@48565
   163
  (Haskell_Quickcheck "Prelude.Int")
huffman@47108
   164
bulwahn@41908
   165
code_const "0 \<Colon> code_int"
bulwahn@43308
   166
  (Haskell_Quickcheck "0")
bulwahn@41908
   167
bulwahn@41908
   168
code_const "1 \<Colon> code_int"
bulwahn@43308
   169
  (Haskell_Quickcheck "1")
bulwahn@41908
   170
bulwahn@41908
   171
code_const "minus \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> code_int"
huffman@47108
   172
  (Haskell_Quickcheck infixl 6 "-")
bulwahn@41908
   173
huffman@47108
   174
code_const div_mod
bulwahn@43308
   175
  (Haskell_Quickcheck "divMod")
bulwahn@42021
   176
bulwahn@42021
   177
code_const "HOL.equal \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
bulwahn@43308
   178
  (Haskell_Quickcheck infix 4 "==")
bulwahn@42021
   179
huffman@47108
   180
code_const "less_eq \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
bulwahn@43308
   181
  (Haskell_Quickcheck infix 4 "<=")
bulwahn@41908
   182
huffman@47108
   183
code_const "less \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
bulwahn@43308
   184
  (Haskell_Quickcheck infix 4 "<")
bulwahn@41908
   185
huffman@47108
   186
code_abort of_int
bulwahn@41908
   187
huffman@47108
   188
hide_const (open) Num div_mod
bulwahn@42021
   189
bulwahn@41961
   190
subsubsection {* Narrowing's deep representation of types and terms *}
bulwahn@41905
   191
bulwahn@46758
   192
datatype narrowing_type = Narrowing_sum_of_products "narrowing_type list list"
bulwahn@46758
   193
datatype narrowing_term = Narrowing_variable "code_int list" narrowing_type | Narrowing_constructor code_int "narrowing_term list"
bulwahn@46758
   194
datatype 'a narrowing_cons = Narrowing_cons narrowing_type "(narrowing_term list => 'a) list"
bulwahn@41905
   195
bulwahn@46758
   196
primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons"
bulwahn@43356
   197
where
bulwahn@46758
   198
  "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (%c. f o c) cs)"
bulwahn@43356
   199
hoelzl@43341
   200
subsubsection {* From narrowing's deep representation of terms to @{theory Code_Evaluation}'s terms *}
bulwahn@42980
   201
bulwahn@42980
   202
class partial_term_of = typerep +
bulwahn@43047
   203
  fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term"
bulwahn@43047
   204
bulwahn@43047
   205
lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t"
bulwahn@43047
   206
  by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp)
bulwahn@43356
   207
 
bulwahn@41964
   208
subsubsection {* Auxilary functions for Narrowing *}
bulwahn@41905
   209
bulwahn@41908
   210
consts nth :: "'a list => code_int => 'a"
bulwahn@41905
   211
bulwahn@43308
   212
code_const nth (Haskell_Quickcheck infixl 9  "!!")
bulwahn@41905
   213
bulwahn@41908
   214
consts error :: "char list => 'a"
bulwahn@41905
   215
bulwahn@43308
   216
code_const error (Haskell_Quickcheck "error")
bulwahn@41905
   217
bulwahn@41908
   218
consts toEnum :: "code_int => char"
bulwahn@41908
   219
haftmann@48565
   220
code_const toEnum (Haskell_Quickcheck "Prelude.toEnum")
bulwahn@41905
   221
bulwahn@43316
   222
consts marker :: "char"
bulwahn@41905
   223
bulwahn@43316
   224
code_const marker (Haskell_Quickcheck "''\\0'")
bulwahn@43316
   225
bulwahn@41961
   226
subsubsection {* Narrowing's basic operations *}
bulwahn@41905
   227
bulwahn@46758
   228
type_synonym 'a narrowing = "code_int => 'a narrowing_cons"
bulwahn@41905
   229
bulwahn@41961
   230
definition empty :: "'a narrowing"
bulwahn@41905
   231
where
bulwahn@46758
   232
  "empty d = Narrowing_cons (Narrowing_sum_of_products []) []"
bulwahn@41905
   233
  
bulwahn@41961
   234
definition cons :: "'a => 'a narrowing"
bulwahn@41905
   235
where
bulwahn@46758
   236
  "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(%_. a)])"
bulwahn@41905
   237
bulwahn@43047
   238
fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
bulwahn@41905
   239
where
bulwahn@46758
   240
  "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)"
bulwahn@46758
   241
| "conv cs (Narrowing_constructor i xs) = (nth cs i) xs"
bulwahn@41905
   242
bulwahn@46758
   243
fun non_empty :: "narrowing_type => bool"
bulwahn@41905
   244
where
bulwahn@46758
   245
  "non_empty (Narrowing_sum_of_products ps) = (\<not> (List.null ps))"
bulwahn@41905
   246
bulwahn@41961
   247
definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
bulwahn@41905
   248
where
bulwahn@41905
   249
  "apply f a d =
bulwahn@46758
   250
     (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs =>
bulwahn@46758
   251
       case a (d - 1) of Narrowing_cons ta cas =>
bulwahn@41905
   252
       let
bulwahn@46758
   253
         shallow = (d > 0 \<and> non_empty ta);
bulwahn@41905
   254
         cs = [(%xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs]
bulwahn@46758
   255
       in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p <- ps]) cs)"
bulwahn@41905
   256
bulwahn@41961
   257
definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
bulwahn@41905
   258
where
bulwahn@41905
   259
  "sum a b d =
bulwahn@46758
   260
    (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca => 
bulwahn@46758
   261
      case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb =>
bulwahn@46758
   262
      Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))"
bulwahn@41905
   263
bulwahn@41912
   264
lemma [fundef_cong]:
bulwahn@41912
   265
  assumes "a d = a' d" "b d = b' d" "d = d'"
bulwahn@41912
   266
  shows "sum a b d = sum a' b' d'"
bulwahn@46758
   267
using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split)
bulwahn@41912
   268
bulwahn@41912
   269
lemma [fundef_cong]:
bulwahn@41912
   270
  assumes "f d = f' d" "(\<And>d'. 0 <= d' & d' < d ==> a d' = a' d')"
bulwahn@41912
   271
  assumes "d = d'"
bulwahn@41912
   272
  shows "apply f a d = apply f' a' d'"
bulwahn@41912
   273
proof -
bulwahn@41912
   274
  note assms moreover
bulwahn@41930
   275
  have "int_of (of_int 0) < int_of d' ==> int_of (of_int 0) <= int_of (of_int (int_of d' - int_of (of_int 1)))"
bulwahn@41912
   276
    by (simp add: of_int_inverse)
bulwahn@41912
   277
  moreover
bulwahn@41930
   278
  have "int_of (of_int (int_of d' - int_of (of_int 1))) < int_of d'"
bulwahn@41912
   279
    by (simp add: of_int_inverse)
bulwahn@41912
   280
  ultimately show ?thesis
bulwahn@46758
   281
    unfolding apply_def by (auto split: narrowing_cons.split narrowing_type.split simp add: Let_def)
bulwahn@41912
   282
qed
bulwahn@41912
   283
bulwahn@41961
   284
subsubsection {* Narrowing generator type class *}
bulwahn@41905
   285
bulwahn@41961
   286
class narrowing =
bulwahn@46758
   287
  fixes narrowing :: "code_int => 'a narrowing_cons"
bulwahn@41905
   288
bulwahn@43237
   289
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
   290
bulwahn@43237
   291
(* FIXME: hard-wired maximal depth of 100 here *)
bulwahn@43315
   292
definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
bulwahn@43237
   293
where
bulwahn@46758
   294
  "exists f = (case narrowing (100 :: code_int) of Narrowing_cons ty cs => Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
bulwahn@43237
   295
bulwahn@43315
   296
definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
bulwahn@43237
   297
where
bulwahn@46758
   298
  "all f = (case narrowing (100 :: code_int) of Narrowing_cons ty cs => Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
bulwahn@43237
   299
wenzelm@41943
   300
subsubsection {* class @{text is_testable} *}
bulwahn@41905
   301
wenzelm@41943
   302
text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
bulwahn@41905
   303
bulwahn@41905
   304
class is_testable
bulwahn@41905
   305
bulwahn@41905
   306
instance bool :: is_testable ..
bulwahn@41905
   307
bulwahn@43047
   308
instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
bulwahn@41905
   309
bulwahn@41905
   310
definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
bulwahn@41905
   311
where
bulwahn@41905
   312
  "ensure_testable f = f"
bulwahn@41905
   313
bulwahn@41910
   314
bulwahn@42022
   315
subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
bulwahn@42022
   316
bulwahn@42022
   317
datatype ('a, 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun"
bulwahn@42022
   318
bulwahn@42022
   319
primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
bulwahn@42022
   320
where
bulwahn@42022
   321
  "eval_ffun (Constant c) x = c"
bulwahn@42022
   322
| "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
bulwahn@42022
   323
bulwahn@42022
   324
hide_type (open) ffun
bulwahn@42022
   325
hide_const (open) Constant Update eval_ffun
bulwahn@42022
   326
bulwahn@42024
   327
datatype 'b cfun = Constant 'b
bulwahn@42024
   328
bulwahn@42024
   329
primrec eval_cfun :: "'b cfun => 'a => 'b"
bulwahn@42024
   330
where
bulwahn@42024
   331
  "eval_cfun (Constant c) y = c"
bulwahn@42024
   332
bulwahn@42024
   333
hide_type (open) cfun
huffman@45734
   334
hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
bulwahn@42024
   335
bulwahn@42024
   336
subsubsection {* Setting up the counterexample generator *}
bulwahn@43237
   337
wenzelm@48891
   338
ML_file "Tools/Quickcheck/narrowing_generators.ML"
bulwahn@42024
   339
bulwahn@42024
   340
setup {* Narrowing_Generators.setup *}
bulwahn@42024
   341
bulwahn@45001
   342
definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
bulwahn@45001
   343
where
bulwahn@45001
   344
  "narrowing_dummy_partial_term_of = partial_term_of"
bulwahn@45001
   345
bulwahn@46758
   346
definition narrowing_dummy_narrowing :: "code_int => ('a :: narrowing) narrowing_cons"
bulwahn@45001
   347
where
bulwahn@45001
   348
  "narrowing_dummy_narrowing = narrowing"
bulwahn@45001
   349
bulwahn@45001
   350
lemma [code]:
bulwahn@45001
   351
  "ensure_testable f =
bulwahn@45001
   352
    (let
bulwahn@46758
   353
      x = narrowing_dummy_narrowing :: code_int => bool narrowing_cons;
bulwahn@45001
   354
      y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
bulwahn@45001
   355
      z = (conv :: _ => _ => unit)  in f)"
bulwahn@45001
   356
unfolding Let_def ensure_testable_def ..
bulwahn@45001
   357
bulwahn@46308
   358
subsection {* Narrowing for sets *}
bulwahn@46308
   359
bulwahn@46308
   360
instantiation set :: (narrowing) narrowing
bulwahn@46308
   361
begin
bulwahn@46308
   362
bulwahn@46308
   363
definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
bulwahn@46308
   364
bulwahn@46308
   365
instance ..
bulwahn@46308
   366
bulwahn@46308
   367
end
bulwahn@45001
   368
  
bulwahn@43356
   369
subsection {* Narrowing for integers *}
bulwahn@43356
   370
bulwahn@43356
   371
bulwahn@46758
   372
definition drawn_from :: "'a list => 'a narrowing_cons"
bulwahn@46758
   373
where "drawn_from xs = Narrowing_cons (Narrowing_sum_of_products (map (%_. []) xs)) (map (%x y. x) xs)"
bulwahn@43356
   374
bulwahn@43356
   375
function around_zero :: "int => int list"
bulwahn@43356
   376
where
bulwahn@43356
   377
  "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
bulwahn@43356
   378
by pat_completeness auto
bulwahn@43356
   379
termination by (relation "measure nat") auto
bulwahn@43356
   380
bulwahn@43356
   381
declare around_zero.simps[simp del]
bulwahn@43356
   382
bulwahn@43356
   383
lemma length_around_zero:
bulwahn@43356
   384
  assumes "i >= 0" 
bulwahn@43356
   385
  shows "length (around_zero i) = 2 * nat i + 1"
bulwahn@43356
   386
proof (induct rule: int_ge_induct[OF assms])
bulwahn@43356
   387
  case 1
bulwahn@43356
   388
  from 1 show ?case by (simp add: around_zero.simps)
bulwahn@43356
   389
next
bulwahn@43356
   390
  case (2 i)
bulwahn@43356
   391
  from 2 show ?case
bulwahn@43356
   392
    by (simp add: around_zero.simps[of "i + 1"])
bulwahn@43356
   393
qed
bulwahn@43356
   394
bulwahn@43356
   395
instantiation int :: narrowing
bulwahn@43356
   396
begin
bulwahn@43356
   397
bulwahn@43356
   398
definition
bulwahn@43356
   399
  "narrowing_int d = (let (u :: _ => _ => unit) = conv; i = Quickcheck_Narrowing.int_of d in drawn_from (around_zero i))"
bulwahn@43356
   400
bulwahn@43356
   401
instance ..
bulwahn@43356
   402
bulwahn@43356
   403
end
bulwahn@43356
   404
bulwahn@43356
   405
lemma [code, code del]: "partial_term_of (ty :: int itself) t == undefined"
bulwahn@43356
   406
by (rule partial_term_of_anything)+
bulwahn@43356
   407
bulwahn@43356
   408
lemma [code]:
bulwahn@46758
   409
  "partial_term_of (ty :: int itself) (Narrowing_variable p t) == Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
bulwahn@46758
   410
  "partial_term_of (ty :: int itself) (Narrowing_constructor i []) == (if i mod 2 = 0 then
bulwahn@43356
   411
     Code_Evaluation.term_of (- (int_of i) div 2) else Code_Evaluation.term_of ((int_of i + 1) div 2))"
bulwahn@43356
   412
by (rule partial_term_of_anything)+
bulwahn@43356
   413
bulwahn@43356
   414
text {* Defining integers by positive and negative copy of naturals *}
bulwahn@43356
   415
(*
bulwahn@43356
   416
datatype simple_int = Positive nat | Negative nat
bulwahn@43356
   417
bulwahn@43356
   418
primrec int_of_simple_int :: "simple_int => int"
bulwahn@43356
   419
where
bulwahn@43356
   420
  "int_of_simple_int (Positive n) = int n"
bulwahn@43356
   421
| "int_of_simple_int (Negative n) = (-1 - int n)"
bulwahn@43356
   422
bulwahn@43356
   423
instantiation int :: narrowing
bulwahn@43356
   424
begin
bulwahn@43356
   425
bulwahn@43356
   426
definition narrowing_int :: "code_int => int cons"
bulwahn@43356
   427
where
bulwahn@43356
   428
  "narrowing_int d = map_cons int_of_simple_int ((narrowing :: simple_int narrowing) d)"
bulwahn@43356
   429
bulwahn@43356
   430
instance ..
bulwahn@43356
   431
bulwahn@43356
   432
end
bulwahn@43356
   433
bulwahn@43356
   434
text {* printing the partial terms *}
bulwahn@43356
   435
bulwahn@43356
   436
lemma [code]:
bulwahn@43356
   437
  "partial_term_of (ty :: int itself) t == Code_Evaluation.App (Code_Evaluation.Const (STR ''Quickcheck_Narrowing.int_of_simple_int'')
bulwahn@43356
   438
     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Quickcheck_Narrowing.simple_int'') [], Typerep.Typerep (STR ''Int.int'') []])) (partial_term_of (TYPE(simple_int)) t)"
bulwahn@43356
   439
by (rule partial_term_of_anything)
bulwahn@43356
   440
bulwahn@43356
   441
*)
bulwahn@43356
   442
bulwahn@46589
   443
subsection {* The @{text find_unused_assms} command *}
bulwahn@46589
   444
wenzelm@48891
   445
ML_file "Tools/Quickcheck/find_unused_assms.ML"
bulwahn@46589
   446
bulwahn@46589
   447
subsection {* Closing up *}
bulwahn@46589
   448
bulwahn@46758
   449
hide_type code_int narrowing_type narrowing_term narrowing_cons property
bulwahn@46758
   450
hide_const int_of of_int nat_of map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero
bulwahn@46758
   451
hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons
bulwahn@46758
   452
hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def
bulwahn@42022
   453
bulwahn@45001
   454
end