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