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