| author | haftmann | 
| Mon, 03 Feb 2014 08:23:21 +0100 | |
| changeset 55293 | 42cf5802d36a | 
| parent 55147 | bce3dbc11f95 | 
| child 55676 | fb46f1c379b5 | 
| permissions | -rw-r--r-- | 
| 41905 | 1 | (* Author: Lukas Bulwahn, TU Muenchen *) | 
| 2 | ||
| 43356 | 3 | header {* Counterexample generator performing narrowing-based testing *}
 | 
| 41905 | 4 | |
| 41930 
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
 bulwahn parents: 
41929diff
changeset | 5 | theory Quickcheck_Narrowing | 
| 43312 
7a31f9064f99
adapting Quickcheck_Narrowing: adding setup for characters; correcting import statement
 bulwahn parents: 
43309diff
changeset | 6 | imports Quickcheck_Exhaustive | 
| 46950 
d0181abdbdac
declare command keywords via theory header, including strict checking outside Pure;
 wenzelm parents: 
46758diff
changeset | 7 | keywords "find_unused_assms" :: diag | 
| 41905 | 8 | begin | 
| 9 | ||
| 10 | subsection {* Counterexample generator *}
 | |
| 11 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 12 | subsubsection {* Code generation setup *}
 | 
| 43308 
fd6cc1378fec
compilation of Haskell in its own target for Quickcheck; passing options by arguments in Narrowing_Generators
 bulwahn parents: 
43237diff
changeset | 13 | |
| 55147 
bce3dbc11f95
prefer explicit code symbol type over ad-hoc name mangling
 haftmann parents: 
52435diff
changeset | 14 | setup {* Code_Target.extend_target ("Haskell_Quickcheck", (Code_Haskell.target, I)) *}
 | 
| 43308 
fd6cc1378fec
compilation of Haskell in its own target for Quickcheck; passing options by arguments in Narrowing_Generators
 bulwahn parents: 
43237diff
changeset | 15 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 16 | code_printing | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 17 | type_constructor typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 18 | | constant Typerep.Typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 19 | | type_constructor integer \<rightharpoonup> (Haskell_Quickcheck) "Prelude.Int" | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 20 | |
| 43308 
fd6cc1378fec
compilation of Haskell in its own target for Quickcheck; passing options by arguments in Narrowing_Generators
 bulwahn parents: 
43237diff
changeset | 21 | code_reserved Haskell_Quickcheck Typerep | 
| 41909 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
 bulwahn parents: 
41908diff
changeset | 22 | |
| 42021 
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
 bulwahn parents: 
41965diff
changeset | 23 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 24 | subsubsection {* Narrowing's deep representation of types and terms *}
 | 
| 41905 | 25 | |
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 26 | datatype narrowing_type = Narrowing_sum_of_products "narrowing_type list list" | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 27 | datatype narrowing_term = Narrowing_variable "integer list" narrowing_type | Narrowing_constructor integer "narrowing_term list" | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 28 | datatype 'a narrowing_cons = Narrowing_cons narrowing_type "(narrowing_term list => 'a) list" | 
| 41905 | 29 | |
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 30 | primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons"
 | 
| 43356 | 31 | where | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 32 | "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (%c. f o c) cs)" | 
| 43356 | 33 | |
| 43341 | 34 | subsubsection {* From narrowing's deep representation of terms to @{theory Code_Evaluation}'s terms *}
 | 
| 42980 
859fe9cc0838
improving code_int setup in Quickcheck_Narrowing; adding partial_term_of class in Quickcheck_Narrowing
 bulwahn parents: 
42024diff
changeset | 35 | |
| 
859fe9cc0838
improving code_int setup in Quickcheck_Narrowing; adding partial_term_of class in Quickcheck_Narrowing
 bulwahn parents: 
42024diff
changeset | 36 | class partial_term_of = typerep + | 
| 43047 
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
 bulwahn parents: 
42980diff
changeset | 37 | fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term" | 
| 
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
 bulwahn parents: 
42980diff
changeset | 38 | |
| 
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
 bulwahn parents: 
42980diff
changeset | 39 | lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t" | 
| 
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
 bulwahn parents: 
42980diff
changeset | 40 | by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp) | 
| 43356 | 41 | |
| 41964 
13904699c859
tuned subsubsection names in Quickcheck_Narrowing
 bulwahn parents: 
41962diff
changeset | 42 | subsubsection {* Auxilary functions for Narrowing *}
 | 
| 41905 | 43 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 44 | consts nth :: "'a list => integer => 'a" | 
| 41905 | 45 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 46 | code_printing constant nth \<rightharpoonup> (Haskell_Quickcheck) infixl 9 "!!" | 
| 41905 | 47 | |
| 41908 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
 bulwahn parents: 
41905diff
changeset | 48 | consts error :: "char list => 'a" | 
| 41905 | 49 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 50 | code_printing constant error \<rightharpoonup> (Haskell_Quickcheck) "error" | 
| 41905 | 51 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 52 | consts toEnum :: "integer => char" | 
| 41908 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
 bulwahn parents: 
41905diff
changeset | 53 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 54 | code_printing constant toEnum \<rightharpoonup> (Haskell_Quickcheck) "Prelude.toEnum" | 
| 41905 | 55 | |
| 43316 | 56 | consts marker :: "char" | 
| 41905 | 57 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
51143diff
changeset | 58 | code_printing constant marker \<rightharpoonup> (Haskell_Quickcheck) "''\\0'" | 
| 43316 | 59 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 60 | subsubsection {* Narrowing's basic operations *}
 | 
| 41905 | 61 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 62 | type_synonym 'a narrowing = "integer => 'a narrowing_cons" | 
| 41905 | 63 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 64 | definition empty :: "'a narrowing" | 
| 41905 | 65 | where | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 66 | "empty d = Narrowing_cons (Narrowing_sum_of_products []) []" | 
| 41905 | 67 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 68 | definition cons :: "'a => 'a narrowing" | 
| 41905 | 69 | where | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 70 | "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(%_. a)])" | 
| 41905 | 71 | |
| 43047 
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
 bulwahn parents: 
42980diff
changeset | 72 | fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a" | 
| 41905 | 73 | where | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 74 | "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)" | 
| 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 75 | | "conv cs (Narrowing_constructor i xs) = (nth cs i) xs" | 
| 41905 | 76 | |
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 77 | fun non_empty :: "narrowing_type => bool" | 
| 41905 | 78 | where | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 79 | "non_empty (Narrowing_sum_of_products ps) = (\<not> (List.null ps))" | 
| 41905 | 80 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 81 | definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
 | 
| 41905 | 82 | where | 
| 83 | "apply f a d = | |
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 84 | (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs => | 
| 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 85 | case a (d - 1) of Narrowing_cons ta cas => | 
| 41905 | 86 | let | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 87 | shallow = (d > 0 \<and> non_empty ta); | 
| 41905 | 88 | cs = [(%xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs] | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 89 | in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p <- ps]) cs)" | 
| 41905 | 90 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 91 | definition sum :: "'a narrowing => 'a narrowing => 'a narrowing" | 
| 41905 | 92 | where | 
| 93 | "sum a b d = | |
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 94 | (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca => | 
| 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 95 | case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb => | 
| 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 96 | Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))" | 
| 41905 | 97 | |
| 41912 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 98 | lemma [fundef_cong]: | 
| 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 99 | assumes "a d = a' d" "b d = b' d" "d = d'" | 
| 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 100 | shows "sum a b d = sum a' b' d'" | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 101 | using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split) | 
| 41912 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 102 | |
| 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 103 | lemma [fundef_cong]: | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 104 | assumes "f d = f' d" "(\<And>d'. 0 \<le> d' \<and> d' < d \<Longrightarrow> a d' = a' d')" | 
| 41912 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 105 | assumes "d = d'" | 
| 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 106 | shows "apply f a d = apply f' a' d'" | 
| 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 107 | proof - | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 108 | note assms | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 109 | moreover have "0 < d' \<Longrightarrow> 0 \<le> d' - 1" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 110 | by (simp add: less_integer_def less_eq_integer_def) | 
| 41912 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 111 | ultimately show ?thesis | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 112 | by (auto simp add: apply_def Let_def | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 113 | split: narrowing_cons.split narrowing_type.split) | 
| 41912 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 114 | qed | 
| 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
 bulwahn parents: 
41910diff
changeset | 115 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 116 | subsubsection {* Narrowing generator type class *}
 | 
| 41905 | 117 | |
| 41961 
fdd37cfcd4a3
renaming series and serial to narrowing in Quickcheck_Narrowing
 bulwahn parents: 
41943diff
changeset | 118 | class narrowing = | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 119 | fixes narrowing :: "integer => 'a narrowing_cons" | 
| 41905 | 120 | |
| 43237 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 121 | 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 | 
| 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 122 | |
| 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 123 | (* FIXME: hard-wired maximal depth of 100 here *) | 
| 43315 
893de45ac28d
removing unneccessary manual instantiations and dead definitions; hiding more constants and facts
 bulwahn parents: 
43314diff
changeset | 124 | definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
 | 
| 43237 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 125 | where | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 126 |   "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
 | 
| 43237 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 127 | |
| 43315 
893de45ac28d
removing unneccessary manual instantiations and dead definitions; hiding more constants and facts
 bulwahn parents: 
43314diff
changeset | 128 | definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
 | 
| 43237 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 129 | where | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 130 |   "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
 | 
| 43237 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 131 | |
| 41943 | 132 | subsubsection {* class @{text is_testable} *}
 | 
| 41905 | 133 | |
| 41943 | 134 | text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
 | 
| 41905 | 135 | |
| 136 | class is_testable | |
| 137 | ||
| 138 | instance bool :: is_testable .. | |
| 139 | ||
| 43047 
26774ccb1c74
automatic derivation of partial_term_of functions; renaming type and term to longer names narrowing_type and narrowing_term; hiding constant C; adding overlord option
 bulwahn parents: 
42980diff
changeset | 140 | instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
 | 
| 41905 | 141 | |
| 142 | definition ensure_testable :: "'a :: is_testable => 'a :: is_testable" | |
| 143 | where | |
| 144 | "ensure_testable f = f" | |
| 145 | ||
| 41910 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
 bulwahn parents: 
41909diff
changeset | 146 | |
| 42022 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 147 | subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
 | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 148 | |
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 149 | datatype ('a, 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun"
 | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 150 | |
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 151 | primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
 | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 152 | where | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 153 | "eval_ffun (Constant c) x = c" | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 154 | | "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)" | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 155 | |
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 156 | hide_type (open) ffun | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 157 | hide_const (open) Constant Update eval_ffun | 
| 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 158 | |
| 42024 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 159 | datatype 'b cfun = Constant 'b | 
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 160 | |
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 161 | primrec eval_cfun :: "'b cfun => 'a => 'b" | 
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 162 | where | 
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 163 | "eval_cfun (Constant c) y = c" | 
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 164 | |
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 165 | hide_type (open) cfun | 
| 45734 
1024dd30da42
hide quickcheck constants Abs_cfun and Rep_cfun, to avoid clash with HOLCF
 huffman parents: 
45001diff
changeset | 166 | hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun | 
| 42024 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 167 | |
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 168 | subsubsection {* Setting up the counterexample generator *}
 | 
| 43237 
8f5c3c6c2909
adding compilation that allows existentials in Quickcheck_Narrowing
 bulwahn parents: 
43047diff
changeset | 169 | |
| 48891 | 170 | ML_file "Tools/Quickcheck/narrowing_generators.ML" | 
| 42024 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 171 | |
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 172 | setup {* Narrowing_Generators.setup *}
 | 
| 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 bulwahn parents: 
42022diff
changeset | 173 | |
| 45001 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 174 | definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
 | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 175 | where | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 176 | "narrowing_dummy_partial_term_of = partial_term_of" | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 177 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 178 | definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons"
 | 
| 45001 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 179 | where | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 180 | "narrowing_dummy_narrowing = narrowing" | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 181 | |
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 182 | lemma [code]: | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 183 | "ensure_testable f = | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 184 | (let | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 185 | x = narrowing_dummy_narrowing :: integer => bool narrowing_cons; | 
| 45001 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 186 | y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term; | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 187 | z = (conv :: _ => _ => unit) in f)" | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 188 | unfolding Let_def ensure_testable_def .. | 
| 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 189 | |
| 46308 | 190 | subsection {* Narrowing for sets *}
 | 
| 191 | ||
| 192 | instantiation set :: (narrowing) narrowing | |
| 193 | begin | |
| 194 | ||
| 195 | definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing" | |
| 196 | ||
| 197 | instance .. | |
| 198 | ||
| 199 | end | |
| 45001 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 200 | |
| 43356 | 201 | subsection {* Narrowing for integers *}
 | 
| 202 | ||
| 203 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 204 | definition drawn_from :: "'a list \<Rightarrow> 'a narrowing_cons" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 205 | where | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 206 | "drawn_from xs = | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 207 | Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)" | 
| 43356 | 208 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 209 | function around_zero :: "int \<Rightarrow> int list" | 
| 43356 | 210 | where | 
| 211 | "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))" | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 212 | by pat_completeness auto | 
| 43356 | 213 | termination by (relation "measure nat") auto | 
| 214 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 215 | declare around_zero.simps [simp del] | 
| 43356 | 216 | |
| 217 | lemma length_around_zero: | |
| 218 | assumes "i >= 0" | |
| 219 | shows "length (around_zero i) = 2 * nat i + 1" | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 220 | proof (induct rule: int_ge_induct [OF assms]) | 
| 43356 | 221 | case 1 | 
| 222 | from 1 show ?case by (simp add: around_zero.simps) | |
| 223 | next | |
| 224 | case (2 i) | |
| 225 | from 2 show ?case | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 226 | by (simp add: around_zero.simps [of "i + 1"]) | 
| 43356 | 227 | qed | 
| 228 | ||
| 229 | instantiation int :: narrowing | |
| 230 | begin | |
| 231 | ||
| 232 | definition | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 233 | "narrowing_int d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 234 | in drawn_from (around_zero i))" | 
| 43356 | 235 | |
| 236 | instance .. | |
| 237 | ||
| 238 | end | |
| 239 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 240 | lemma [code, code del]: "partial_term_of (ty :: int itself) t \<equiv> undefined" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 241 | by (rule partial_term_of_anything)+ | 
| 43356 | 242 | |
| 243 | lemma [code]: | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 244 | "partial_term_of (ty :: int itself) (Narrowing_variable p t) \<equiv> | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 245 | Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 246 | "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \<equiv> | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 247 | (if i mod 2 = 0 | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 248 | then Code_Evaluation.term_of (- (int_of_integer i) div 2) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 249 | else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 250 | by (rule partial_term_of_anything)+ | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 251 | |
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 252 | instantiation integer :: narrowing | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 253 | begin | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 254 | |
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 255 | definition | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 256 | "narrowing_integer d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 257 | in drawn_from (map integer_of_int (around_zero i)))" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 258 | |
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 259 | instance .. | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 260 | |
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 261 | end | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 262 | |
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 263 | lemma [code, code del]: "partial_term_of (ty :: integer itself) t \<equiv> undefined" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 264 | by (rule partial_term_of_anything)+ | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 265 | |
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 266 | lemma [code]: | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 267 | "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \<equiv> | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 268 | Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 269 | "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \<equiv> | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 270 | (if i mod 2 = 0 | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 271 | then Code_Evaluation.term_of (- i div 2) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 272 | else Code_Evaluation.term_of ((i + 1) div 2))" | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 273 | by (rule partial_term_of_anything)+ | 
| 43356 | 274 | |
| 275 | ||
| 46589 | 276 | subsection {* The @{text find_unused_assms} command *}
 | 
| 277 | ||
| 48891 | 278 | ML_file "Tools/Quickcheck/find_unused_assms.ML" | 
| 46589 | 279 | |
| 280 | subsection {* Closing up *}
 | |
| 281 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 282 | hide_type narrowing_type narrowing_term narrowing_cons property | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 283 | hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero | 
| 46758 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 284 | hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons | 
| 
4106258260b3
choosing longer constant names in Quickcheck_Narrowing to reduce the chances of name clashes in Quickcheck-Narrowing
 bulwahn parents: 
46589diff
changeset | 285 | hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def | 
| 42022 
101ce92333f4
adding a simple datatype for representing functions in Quickcheck_Narrowing
 bulwahn parents: 
42021diff
changeset | 286 | |
| 45001 
5c8d7d6db682
ensuring that some constants are generated in the source code by adding calls in ensure_testable
 bulwahn parents: 
43887diff
changeset | 287 | end | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
50046diff
changeset | 288 |