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