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