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