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