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