
bulwahn@40420: (* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@40420: 
bulwahn@41916: header {* A simple counterexample generator performing exhaustive testing *}
bulwahn@40420: 
bulwahn@41918: theory Quickcheck_Exhaustive
bulwahn@40420: imports Quickcheck
wenzelm@46950: keywords "quickcheck_generator" :: thy_decl
bulwahn@40420: begin
bulwahn@40420: 
bulwahn@41916: subsection {* basic operations for exhaustive generators *}
bulwahn@41105: 
bulwahn@41105: definition orelse :: "'a option => 'a option => 'a option" (infixr "orelse" 55)
bulwahn@41105: where
bulwahn@41105:   [code_unfold]: "x orelse y = (case x of Some x' => Some x' | None => y)"
bulwahn@40420: 
bulwahn@41916: subsection {* exhaustive generator type classes *}
bulwahn@40420: 
bulwahn@41916: class exhaustive = term_of +
bulwahn@45724:   fixes exhaustive :: "('a \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
bulwahn@42310:   
bulwahn@42310: class full_exhaustive = term_of +
bulwahn@45722:   fixes full_exhaustive :: "('a * (unit => term) \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
bulwahn@40420: 
bulwahn@42310: instantiation code_numeral :: full_exhaustive
bulwahn@40639: begin
bulwahn@40639: 
bulwahn@45722: function full_exhaustive_code_numeral' :: "(code_numeral * (unit => term) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@42304:   where "full_exhaustive_code_numeral' f d i =
bulwahn@42304:     (if d < i then None
bulwahn@42304:     else (f (i, %_. Code_Evaluation.term_of i)) orelse (full_exhaustive_code_numeral' f d (i + 1)))"
bulwahn@42304: by pat_completeness auto
bulwahn@42304: 
bulwahn@42304: termination
bulwahn@42304:   by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
bulwahn@42304: 
bulwahn@42304: definition "full_exhaustive f d = full_exhaustive_code_numeral' f d 0"
bulwahn@42304: 
bulwahn@42310: instance ..
bulwahn@42310: 
bulwahn@42310: end
bulwahn@42310: 
bulwahn@42310: instantiation code_numeral :: exhaustive
bulwahn@42310: begin
bulwahn@42310: 
bulwahn@45724: function exhaustive_code_numeral' :: "(code_numeral => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@41916:   where "exhaustive_code_numeral' f d i =
bulwahn@41916:     (if d < i then None
bulwahn@42304:     else (f i orelse exhaustive_code_numeral' f d (i + 1)))"
bulwahn@41231: by pat_completeness auto
bulwahn@41231: 
bulwahn@42304: termination
bulwahn@41231:   by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
bulwahn@41231: 
bulwahn@41916: definition "exhaustive f d = exhaustive_code_numeral' f d 0"
bulwahn@41231: 
bulwahn@41231: instance ..
bulwahn@41231: 
bulwahn@41231: end
bulwahn@41231: 
bulwahn@41916: instantiation nat :: exhaustive
bulwahn@41231: begin
bulwahn@41231: 
bulwahn@42304: definition "exhaustive f d = exhaustive (%x. f (Code_Numeral.nat_of x)) d"
bulwahn@42304: 
bulwahn@42310: instance ..
bulwahn@42310: 
bulwahn@42310: end
bulwahn@42310: 
bulwahn@42310: instantiation nat :: full_exhaustive
bulwahn@42310: begin
bulwahn@42310: 
bulwahn@42304: definition "full_exhaustive f d = full_exhaustive (%(x, xt). f (Code_Numeral.nat_of x, %_. Code_Evaluation.term_of (Code_Numeral.nat_of x))) d"
bulwahn@41231: 
bulwahn@41231: instance ..
bulwahn@41231: 
bulwahn@41231: end
bulwahn@41231: 
bulwahn@41916: instantiation int :: exhaustive
bulwahn@40639: begin
bulwahn@40639: 
bulwahn@45724: function exhaustive' :: "(int => (bool * term list) option) => int => int => (bool * term list) option"
bulwahn@42304:   where "exhaustive' f d i = (if d < i then None else (f i orelse exhaustive' f d (i + 1)))"
bulwahn@40639: by pat_completeness auto
bulwahn@40639: 
bulwahn@40639: termination 
bulwahn@40639:   by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@40639: 
bulwahn@41916: definition "exhaustive f d = exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@40639: 
bulwahn@42310: instance ..
bulwahn@42310: 
bulwahn@42310: end
bulwahn@42310: 
bulwahn@42310: instantiation int :: full_exhaustive
bulwahn@42310: begin
bulwahn@42310: 
bulwahn@45722: function full_exhaustive' :: "(int * (unit => term) => (bool * term list) option) => int => int => (bool * term list) option"
bulwahn@42304:   where "full_exhaustive' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_exhaustive' f d (i + 1)))"
bulwahn@42304: by pat_completeness auto
bulwahn@42304: 
bulwahn@42304: termination 
bulwahn@42304:   by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@42304: 
bulwahn@42304: definition "full_exhaustive f d = full_exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@42304: 
bulwahn@40639: instance ..
bulwahn@40639: 
bulwahn@40639: end
bulwahn@40639: 
bulwahn@41916: instantiation prod :: (exhaustive, exhaustive) exhaustive
bulwahn@40639: begin
bulwahn@40899: 
bulwahn@40639: definition
bulwahn@42304:   "exhaustive f d = exhaustive (%x. exhaustive (%y. f ((x, y))) d) d"
bulwahn@42304: 
bulwahn@42310: instance ..
bulwahn@42310: 
bulwahn@42310: end
bulwahn@42310: 
bulwahn@46307: definition (in term_syntax) [code_unfold]: "valtermify_pair x y = Code_Evaluation.valtermify (Pair :: 'a :: typerep => 'b :: typerep => 'a * 'b) {\<cdot>} x {\<cdot>} y"
bulwahn@46307: 
bulwahn@42310: instantiation prod :: (full_exhaustive, full_exhaustive) full_exhaustive
bulwahn@42310: begin
bulwahn@42310: 
bulwahn@42304: definition
bulwahn@46307:   "full_exhaustive f d = full_exhaustive (%x. full_exhaustive (%y. f (valtermify_pair x y)) d) d"
bulwahn@40639: 
bulwahn@40639: instance ..
bulwahn@40639: 
bulwahn@40639: end
bulwahn@40639: 
bulwahn@46193: instantiation set :: (exhaustive) exhaustive
bulwahn@46193: begin
bulwahn@46193: 
bulwahn@46193: fun exhaustive_set
bulwahn@46193: where
bulwahn@46307:   "exhaustive_set f i = (if i = 0 then None else (f {} orelse exhaustive_set (%A. f A orelse exhaustive (%x. if x \<in> A then None else f (insert x A)) (i - 1)) (i - 1)))"
bulwahn@46193: 
bulwahn@46193: instance ..
bulwahn@46193: 
bulwahn@46193: end
bulwahn@46193: 
bulwahn@46193: instantiation set :: (full_exhaustive) full_exhaustive
bulwahn@46193: begin
bulwahn@46193: 
bulwahn@46193: fun full_exhaustive_set 
bulwahn@46193: where
bulwahn@46193:   "full_exhaustive_set f i = (if i = 0 then None else (f valterm_emptyset orelse full_exhaustive_set (%A. f A orelse Quickcheck_Exhaustive.full_exhaustive (%x. if fst x \<in> fst A then None else f (valtermify_insert x A)) (i - 1)) (i - 1)))"
bulwahn@46193: 
bulwahn@46193: instance ..
bulwahn@46193: 
bulwahn@46193: end
bulwahn@46193: 
bulwahn@41916: instantiation "fun" :: ("{equal, exhaustive}", exhaustive) exhaustive
bulwahn@40639: begin
bulwahn@40639: 
bulwahn@45724: fun exhaustive_fun' :: "(('a => 'b) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@42304: where
bulwahn@42304:   "exhaustive_fun' f i d = (exhaustive (%b. f (%_. b)) d)
bulwahn@42304:    orelse (if i > 1 then
bulwahn@42304:      exhaustive_fun' (%g. exhaustive (%a. exhaustive (%b.
bulwahn@42304:        f (g(a := b))) d) d) (i - 1) d else None)"
bulwahn@42304: 
bulwahn@45724: definition exhaustive_fun :: "(('a => 'b) => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@40639: where
bulwahn@42304:   "exhaustive_fun f d = exhaustive_fun' f d d" 
bulwahn@42304: 
bulwahn@42310: instance ..
bulwahn@42310: 
bulwahn@42310: end
bulwahn@42310: 
bulwahn@46307: definition [code_unfold]: "valtermify_absdummy = (%(v, t). (%_::'a. v, %u::unit. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a::typerep)) (t ())))"
bulwahn@46307: 
bulwahn@46307: definition (in term_syntax) [code_unfold]: "valtermify_fun_upd g a b = Code_Evaluation.valtermify (fun_upd :: ('a :: typerep => 'b :: typerep) => 'a => 'b => 'a => 'b) {\<cdot>} g {\<cdot>} a {\<cdot>} b"
bulwahn@46307: 
bulwahn@42310: instantiation "fun" :: ("{equal, full_exhaustive}", full_exhaustive) full_exhaustive
bulwahn@42310: begin
bulwahn@42304: 
bulwahn@45722: fun full_exhaustive_fun' :: "(('a => 'b) * (unit => term) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@42304: where
bulwahn@46307:   "full_exhaustive_fun' f i d = (full_exhaustive (%v. f (valtermify_absdummy v)) d)
bulwahn@42117:    orelse (if i > 1 then
bulwahn@46307:      full_exhaustive_fun' (%g. full_exhaustive (%a. full_exhaustive (%b.
bulwahn@46307:        f (valtermify_fun_upd g a b)) d) d) (i - 1) d else None)"
bulwahn@40639: 
bulwahn@45722: definition full_exhaustive_fun :: "(('a => 'b) * (unit => term) => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@40639: where
bulwahn@42304:   "full_exhaustive_fun f d = full_exhaustive_fun' f d d" 
bulwahn@40639: 
bulwahn@40639: instance ..
bulwahn@40639: 
bulwahn@40639: end
bulwahn@40639: 
bulwahn@41085: subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
bulwahn@41085: 
bulwahn@41085: class check_all = enum + term_of +
bulwahn@45722:   fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> (bool * term list) option) \<Rightarrow> (bool * term list) option"
bulwahn@41177:   fixes enum_term_of :: "'a itself \<Rightarrow> unit \<Rightarrow> term list"
bulwahn@41177:   
bulwahn@45722: fun check_all_n_lists :: "(('a :: check_all) list * (unit \<Rightarrow> term list) \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
bulwahn@41085: where
bulwahn@41085:   "check_all_n_lists f n =
bulwahn@41085:      (if n = 0 then f ([], (%_. [])) else check_all (%(x, xt). check_all_n_lists (%(xs, xst). f ((x # xs), (%_. (xt () # xst ())))) (n - 1)))"
bulwahn@41085: 
bulwahn@46307: definition (in term_syntax) [code_unfold]: "termify_fun_upd g a b = (Code_Evaluation.termify (fun_upd :: ('a :: typerep => 'b :: typerep) => 'a => 'b => 'a => 'b) <\<cdot>> g <\<cdot>> a <\<cdot>> b)"
bulwahn@46307: 
bulwahn@41177: definition mk_map_term :: " (unit \<Rightarrow> typerep) \<Rightarrow> (unit \<Rightarrow> typerep) \<Rightarrow> (unit \<Rightarrow> term list) \<Rightarrow> (unit \<Rightarrow> term list) \<Rightarrow> unit \<Rightarrow> term"
bulwahn@41085: where
bulwahn@41177:   "mk_map_term T1 T2 domm rng =
bulwahn@41177:      (%_. let T1 = T1 ();
bulwahn@41085:               T2 = T2 ();
bulwahn@41085:               update_term = (%g (a, b).
bulwahn@41085:                 Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@41085:                  (Code_Evaluation.Const (STR ''Fun.fun_upd'')
bulwahn@41085:                    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
bulwahn@41177:                       Typerep.Typerep (STR ''fun'') [T1,
bulwahn@41177:                         Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
bulwahn@41177:                         g) a) b)
bulwahn@41085:           in
bulwahn@41177:              List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip (domm ()) (rng ())))"
bulwahn@41177: 
bulwahn@41177: instantiation "fun" :: ("{equal, check_all}", check_all) check_all
bulwahn@41177: begin
bulwahn@41085: 
bulwahn@41085: definition
bulwahn@41177:   "check_all f =
bulwahn@41177:     (let
bulwahn@41177:       mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) (enum_term_of (TYPE('a)));
bulwahn@41177:       enum = (Enum.enum :: 'a list)
bulwahn@41177:     in check_all_n_lists (\<lambda>(ys, yst). f (the o map_of (zip enum ys), mk_term yst)) (Code_Numeral.of_nat (length enum)))"
bulwahn@41085: 
bulwahn@41177: definition enum_term_of_fun :: "('a => 'b) itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_fun = (%_ _. let
bulwahn@41177:     enum_term_of_a = enum_term_of (TYPE('a));
bulwahn@41177:     mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) enum_term_of_a
bulwahn@41177:   in map (%ys. mk_term (%_. ys) ()) (Enum.n_lists (length (enum_term_of_a ())) (enum_term_of (TYPE('b)) ())))"
bulwahn@41177:  
bulwahn@41085: instance ..
bulwahn@41085: 
bulwahn@41085: end
bulwahn@41085: 
bulwahn@46305: fun (in term_syntax) check_all_subsets :: "(('a :: typerep) set * (unit => term) => (bool * term list) option) => ('a * (unit => term)) list => (bool * term list) option"
bulwahn@46305: where
bulwahn@46305:   "check_all_subsets f [] = f valterm_emptyset"
bulwahn@46305: | "check_all_subsets f (x # xs) = check_all_subsets (%s. case f s of Some ts => Some ts | None => f (valtermify_insert x s)) xs"
bulwahn@46305: 
bulwahn@46305: 
bulwahn@46305: definition (in term_syntax) [code_unfold]: "term_emptyset = Code_Evaluation.termify ({} :: ('a :: typerep) set)"
bulwahn@46305: definition (in term_syntax) [code_unfold]: "termify_insert x s = Code_Evaluation.termify (insert :: ('a::typerep) => 'a set => 'a set)  <\<cdot>> x <\<cdot>> s"
bulwahn@46305: 
bulwahn@46305: definition (in term_syntax) setify :: "('a::typerep) itself => term list => term"
bulwahn@46305: where
bulwahn@46305:   "setify T ts = foldr (termify_insert T) ts (term_emptyset T)" 
bulwahn@46305: 
bulwahn@46305: instantiation set :: (check_all) check_all
bulwahn@46305: begin
bulwahn@46305: 
bulwahn@46305: definition
bulwahn@46305:   "check_all_set f =
bulwahn@46305:      check_all_subsets f (zip (Enum.enum :: 'a list) (map (%a. %u :: unit. a) (Quickcheck_Exhaustive.enum_term_of (TYPE ('a)) ())))"
bulwahn@46305: 
bulwahn@46305: definition enum_term_of_set :: "'a set itself => unit => term list"
bulwahn@46305: where
bulwahn@46305:   "enum_term_of_set _ _ = map (setify (TYPE('a))) (sublists (Quickcheck_Exhaustive.enum_term_of (TYPE('a)) ()))"
bulwahn@46305: 
bulwahn@46305: instance ..
bulwahn@46305: 
bulwahn@46305: end
bulwahn@41105: 
bulwahn@41105: instantiation unit :: check_all
bulwahn@41105: begin
bulwahn@41105: 
bulwahn@41105: definition
bulwahn@41105:   "check_all f = f (Code_Evaluation.valtermify ())"
bulwahn@41105: 
bulwahn@41177: definition enum_term_of_unit :: "unit itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_unit = (%_ _. [Code_Evaluation.term_of ()])"
bulwahn@41177: 
bulwahn@41105: instance ..
bulwahn@41105: 
bulwahn@41105: end
bulwahn@41105: 
bulwahn@41105: 
bulwahn@41085: instantiation bool :: check_all
bulwahn@41085: begin
bulwahn@41085: 
bulwahn@41085: definition
bulwahn@41085:   "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
bulwahn@41085: 
bulwahn@41177: definition enum_term_of_bool :: "bool itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_bool = (%_ _. map Code_Evaluation.term_of (Enum.enum :: bool list))"
bulwahn@41177: 
bulwahn@41085: instance ..
bulwahn@41085: 
bulwahn@41085: end
bulwahn@41085: 
bulwahn@46307: definition (in term_syntax) [code_unfold]: "termify_pair x y = Code_Evaluation.termify (Pair :: 'a :: typerep => 'b :: typerep => 'a * 'b) <\<cdot>> x <\<cdot>> y"
bulwahn@41105: 
bulwahn@41085: instantiation prod :: (check_all, check_all) check_all
bulwahn@41085: begin
bulwahn@41085: 
bulwahn@41085: definition
bulwahn@46307:   "check_all f = check_all (%x. check_all (%y. f (valtermify_pair x y)))"
bulwahn@41085: 
bulwahn@41177: definition enum_term_of_prod :: "('a * 'b) itself => unit => term list"
bulwahn@41177: where
bulwahn@46307:   "enum_term_of_prod = (%_ _. map (%(x, y). termify_pair TYPE('a) TYPE('b) x y)
bulwahn@46307:      (Enum.product (enum_term_of (TYPE('a)) ()) (enum_term_of (TYPE('b)) ())))"
bulwahn@41177: 
bulwahn@41085: instance ..
bulwahn@41085: 
bulwahn@41085: end
bulwahn@41085: 
bulwahn@46307: definition (in term_syntax) [code_unfold]: "valtermify_Inl x = Code_Evaluation.valtermify (Inl :: 'a :: typerep => 'a + 'b :: typerep) {\<cdot>} x"
bulwahn@46307: definition (in term_syntax) [code_unfold]: "valtermify_Inr x = Code_Evaluation.valtermify (Inr :: 'b :: typerep => 'a ::typerep + 'b) {\<cdot>} x"
bulwahn@41105: 
bulwahn@41105: instantiation sum :: (check_all, check_all) check_all
bulwahn@41105: begin
bulwahn@41105: 
bulwahn@41105: definition
bulwahn@46307:   "check_all f = check_all (%a. f (valtermify_Inl a)) orelse check_all (%b. f (valtermify_Inr b))"
bulwahn@41105: 
bulwahn@41177: definition enum_term_of_sum :: "('a + 'b) itself => unit => term list"
bulwahn@41177: where
bulwahn@41722:   "enum_term_of_sum = (%_ _.
bulwahn@41722:      let
bulwahn@41722:        T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41722:        T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41722:      in
bulwahn@41722:        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
bulwahn@41722:              (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
bulwahn@41722:              (enum_term_of (TYPE('a)) ()) @
bulwahn@41722:        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
bulwahn@41722:              (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
bulwahn@41722:              (enum_term_of (TYPE('b)) ()))"
bulwahn@41177: 
bulwahn@41105: instance ..
bulwahn@41105: 
bulwahn@41105: end
bulwahn@41105: 
bulwahn@41105: instantiation nibble :: check_all
bulwahn@41105: begin
bulwahn@41105: 
bulwahn@41105: definition
bulwahn@41105:   "check_all f =
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble0) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble1) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble2) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble3) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble4) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble5) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble6) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble7) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble8) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify Nibble9) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify NibbleA) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify NibbleB) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify NibbleC) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify NibbleD) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify NibbleE) orelse
bulwahn@41105:     f (Code_Evaluation.valtermify NibbleF)"
bulwahn@41105: 
bulwahn@41177: definition enum_term_of_nibble :: "nibble itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_nibble = (%_ _. map Code_Evaluation.term_of (Enum.enum :: nibble list))"
bulwahn@41177: 
bulwahn@41105: instance ..
bulwahn@41105: 
bulwahn@41105: end
bulwahn@41105: 
bulwahn@41105: 
bulwahn@41105: instantiation char :: check_all
bulwahn@41105: begin
bulwahn@41105: 
bulwahn@41105: definition
bulwahn@41105:   "check_all f = check_all (%(x, t1). check_all (%(y, t2). f (Char x y, %_. Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.term_of Char) (t1 ())) (t2 ()))))"
bulwahn@41105: 
bulwahn@41177: definition enum_term_of_char :: "char itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_char = (%_ _. map Code_Evaluation.term_of (Enum.enum :: char list))"
bulwahn@41177: 
bulwahn@41105: instance ..
bulwahn@41105: 
bulwahn@41105: end
bulwahn@41105: 
bulwahn@41105: 
bulwahn@41105: instantiation option :: (check_all) check_all
bulwahn@41105: begin
bulwahn@41105: 
bulwahn@41105: definition
bulwahn@41178:   "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App
bulwahn@41178:     (Code_Evaluation.Const (STR ''Option.option.Some'')
bulwahn@41178:       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]])) (t ())))"
bulwahn@41105: 
bulwahn@41177: definition enum_term_of_option :: "'a option itself => unit => term list"
bulwahn@41177: where
bulwahn@41722:   "enum_term_of_option = (% _ _. (Code_Evaluation.term_of (None :: 'a option)) # (map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Option.option.Some'')
bulwahn@41722:       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]]))) (enum_term_of (TYPE('a)) ())))"
bulwahn@41177: 
bulwahn@41105: instance ..
bulwahn@41105: 
bulwahn@41105: end
bulwahn@41105: 
bulwahn@41105: 
bulwahn@41085: instantiation Enum.finite_1 :: check_all
bulwahn@41085: begin
bulwahn@41085: 
bulwahn@41085: definition
bulwahn@41085:   "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
bulwahn@41085: 
bulwahn@41177: definition enum_term_of_finite_1 :: "Enum.finite_1 itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_finite_1 = (%_ _. [Code_Evaluation.term_of Enum.finite_1.a\<^isub>1])"
bulwahn@41177: 
bulwahn@41085: instance ..
bulwahn@41085: 
bulwahn@41085: end
bulwahn@41085: 
bulwahn@41085: instantiation Enum.finite_2 :: check_all
bulwahn@41085: begin
bulwahn@41085: 
bulwahn@41085: definition
bulwahn@46417:   "check_all f = (f (Code_Evaluation.valtermify Enum.finite_2.a\<^isub>1)
bulwahn@46417:     orelse f (Code_Evaluation.valtermify Enum.finite_2.a\<^isub>2))"
bulwahn@41085: 
bulwahn@41177: definition enum_term_of_finite_2 :: "Enum.finite_2 itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_finite_2 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_2 list))"
bulwahn@41177: 
bulwahn@41085: instance ..
bulwahn@41085: 
bulwahn@41085: end
bulwahn@41085: 
bulwahn@41085: instantiation Enum.finite_3 :: check_all
bulwahn@41085: begin
bulwahn@41085: 
bulwahn@41085: definition
bulwahn@46417:   "check_all f = (f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>1)
bulwahn@46417:     orelse f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>2)
bulwahn@46417:     orelse f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>3))"
bulwahn@41085: 
bulwahn@41177: definition enum_term_of_finite_3 :: "Enum.finite_3 itself => unit => term list"
bulwahn@41177: where
bulwahn@41177:   "enum_term_of_finite_3 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_3 list))"
bulwahn@41177: 
bulwahn@41085: instance ..
bulwahn@41085: 
bulwahn@41085: end
bulwahn@41085: 
bulwahn@46417: instantiation Enum.finite_4 :: check_all
bulwahn@46417: begin
bulwahn@46417: 
bulwahn@46417: definition
bulwahn@46417:   "check_all f = (f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>1)
bulwahn@46417:     orelse f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>2)
bulwahn@46417:     orelse f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>3)
bulwahn@46417:     orelse f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>4))"
bulwahn@46417: 
bulwahn@46417: definition enum_term_of_finite_4 :: "Enum.finite_4 itself => unit => term list"
bulwahn@46417: where
bulwahn@46417:   "enum_term_of_finite_4 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_4 list))"
bulwahn@46417: 
bulwahn@46417: instance ..
bulwahn@46417: 
bulwahn@46417: end
bulwahn@46417: 
bulwahn@42195: subsection {* Bounded universal quantifiers *}
bulwahn@41085: 
bulwahn@42195: class bounded_forall =
bulwahn@42195:   fixes bounded_forall :: "('a \<Rightarrow> bool) \<Rightarrow> code_numeral \<Rightarrow> bool"
bulwahn@42195: 
bulwahn@42305: subsection {* Fast exhaustive combinators *}
bulwahn@42305: 
bulwahn@42305: class fast_exhaustive = term_of +
bulwahn@42305:   fixes fast_exhaustive :: "('a \<Rightarrow> unit) \<Rightarrow> code_numeral \<Rightarrow> unit"
bulwahn@42305: 
bulwahn@45818: axiomatization throw_Counterexample :: "term list => unit"
bulwahn@45818: axiomatization catch_Counterexample :: "unit => term list option"
bulwahn@42305: 
bulwahn@42305: code_const throw_Counterexample
bulwahn@42305:   (Quickcheck "raise (Exhaustive'_Generators.Counterexample _)")
bulwahn@42305: code_const catch_Counterexample
bulwahn@42305:   (Quickcheck "(((_); NONE) handle Exhaustive'_Generators.Counterexample ts => SOME ts)")
bulwahn@42305: 
bulwahn@45450: subsection {* Continuation passing style functions as plus monad *}
bulwahn@45450:   
bulwahn@45450: type_synonym 'a cps = "('a => term list option) => term list option"
bulwahn@45450: 
bulwahn@45450: definition cps_empty :: "'a cps"
bulwahn@45450: where
bulwahn@45450:   "cps_empty = (%cont. None)"
bulwahn@45450: 
bulwahn@45450: definition cps_single :: "'a => 'a cps"
bulwahn@45450: where
bulwahn@45450:   "cps_single v = (%cont. cont v)"
bulwahn@45450: 
bulwahn@45450: definition cps_bind :: "'a cps => ('a => 'b cps) => 'b cps" 
bulwahn@45450: where
bulwahn@45450:   "cps_bind m f = (%cont. m (%a. (f a) cont))"
bulwahn@45450: 
bulwahn@45450: definition cps_plus :: "'a cps => 'a cps => 'a cps"
bulwahn@45450: where
bulwahn@45450:   "cps_plus a b = (%c. case a c of None => b c | Some x => Some x)"
bulwahn@45450: 
bulwahn@45450: definition cps_if :: "bool => unit cps"
bulwahn@45450: where
bulwahn@45450:   "cps_if b = (if b then cps_single () else cps_empty)"
bulwahn@45450: 
bulwahn@45450: definition cps_not :: "unit cps => unit cps"
bulwahn@45450: where
bulwahn@45450:   "cps_not n = (%c. case n (%u. Some []) of None => c () | Some _ => None)"
bulwahn@45450: 
bulwahn@45750: type_synonym 'a pos_bound_cps = "('a => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@45450: 
bulwahn@45450: definition pos_bound_cps_empty :: "'a pos_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "pos_bound_cps_empty = (%cont i. None)"
bulwahn@45450: 
bulwahn@45450: definition pos_bound_cps_single :: "'a => 'a pos_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "pos_bound_cps_single v = (%cont i. cont v)"
bulwahn@45450: 
bulwahn@45450: definition pos_bound_cps_bind :: "'a pos_bound_cps => ('a => 'b pos_bound_cps) => 'b pos_bound_cps" 
bulwahn@45450: where
bulwahn@45450:   "pos_bound_cps_bind m f = (%cont i. if i = 0 then None else (m (%a. (f a) cont i) (i - 1)))"
bulwahn@45450: 
bulwahn@45450: definition pos_bound_cps_plus :: "'a pos_bound_cps => 'a pos_bound_cps => 'a pos_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "pos_bound_cps_plus a b = (%c i. case a c i of None => b c i | Some x => Some x)"
bulwahn@45450: 
bulwahn@45450: definition pos_bound_cps_if :: "bool => unit pos_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "pos_bound_cps_if b = (if b then pos_bound_cps_single () else pos_bound_cps_empty)"
bulwahn@45450: 
bulwahn@45450: datatype 'a unknown = Unknown | Known 'a
bulwahn@45450: datatype 'a three_valued = Unknown_value | Value 'a | No_value
bulwahn@45450: 
bulwahn@45450: type_synonym 'a neg_bound_cps = "('a unknown => term list three_valued) => code_numeral => term list three_valued"
bulwahn@45450: 
bulwahn@45450: definition neg_bound_cps_empty :: "'a neg_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "neg_bound_cps_empty = (%cont i. No_value)"
bulwahn@45450: 
bulwahn@45450: definition neg_bound_cps_single :: "'a => 'a neg_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "neg_bound_cps_single v = (%cont i. cont (Known v))"
bulwahn@45450: 
bulwahn@45450: definition neg_bound_cps_bind :: "'a neg_bound_cps => ('a => 'b neg_bound_cps) => 'b neg_bound_cps" 
bulwahn@45450: where
bulwahn@45450:   "neg_bound_cps_bind m f = (%cont i. if i = 0 then cont Unknown else m (%a. case a of Unknown => cont Unknown | Known a' => f a' cont i) (i - 1))"
bulwahn@45450: 
bulwahn@45450: definition neg_bound_cps_plus :: "'a neg_bound_cps => 'a neg_bound_cps => 'a neg_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "neg_bound_cps_plus a b = (%c i. case a c i of No_value => b c i | Value x => Value x | Unknown_value => (case b c i of No_value => Unknown_value | Value x => Value x | Unknown_value => Unknown_value))"
bulwahn@45450: 
bulwahn@45450: definition neg_bound_cps_if :: "bool => unit neg_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "neg_bound_cps_if b = (if b then neg_bound_cps_single () else neg_bound_cps_empty)"
bulwahn@45450: 
bulwahn@45450: definition neg_bound_cps_not :: "unit pos_bound_cps => unit neg_bound_cps"
bulwahn@45450: where
bulwahn@45750:   "neg_bound_cps_not n = (%c i. case n (%u. Some (True, [])) i of None => c (Known ()) | Some _ => No_value)"
bulwahn@45450: 
bulwahn@45450: definition pos_bound_cps_not :: "unit neg_bound_cps => unit pos_bound_cps"
bulwahn@45450: where
bulwahn@45450:   "pos_bound_cps_not n = (%c i. case n (%u. Value []) i of No_value => c () | Value _ => None | Unknown_value => None)"
bulwahn@45450: 
bulwahn@45925: subsection {* Defining generators for any first-order data type *}
bulwahn@40420: 
bulwahn@45697: axiomatization unknown :: 'a
bulwahn@45697: 
bulwahn@45697: notation (output) unknown  ("?")
bulwahn@45684:  
wenzelm@48891: ML_file "Tools/Quickcheck/exhaustive_generators.ML"
bulwahn@40420: 
bulwahn@41918: setup {* Exhaustive_Generators.setup *}
bulwahn@40420: 
bulwahn@43882: declare [[quickcheck_batch_tester = exhaustive]]
bulwahn@40915: 
bulwahn@45925: subsection {* Defining generators for abstract types *}
bulwahn@45925: 
wenzelm@48891: ML_file "Tools/Quickcheck/abstract_generators.ML"
bulwahn@45925: 
bulwahn@47203: hide_fact (open) orelse_def
bulwahn@41105: no_notation orelse (infixr "orelse" 55)
bulwahn@45818: 
bulwahn@45818: hide_fact
bulwahn@45818:   exhaustive'_def
bulwahn@45818:   exhaustive_code_numeral'_def
bulwahn@45818: 
bulwahn@46307: hide_const valtermify_absdummy valtermify_fun_upd valterm_emptyset valtermify_insert valtermify_pair
bulwahn@46307:   valtermify_Inl valtermify_Inr
bulwahn@46307:   termify_fun_upd term_emptyset termify_insert termify_pair setify
bulwahn@46305: 
bulwahn@45818: hide_const (open)
bulwahn@45818:   exhaustive full_exhaustive exhaustive' exhaustive_code_numeral' full_exhaustive_code_numeral'
bulwahn@45818:   throw_Counterexample catch_Counterexample
bulwahn@45818:   check_all enum_term_of
bulwahn@46305:   orelse unknown mk_map_term check_all_n_lists check_all_subsets
bulwahn@40420: 
bulwahn@45450: hide_type (open) cps pos_bound_cps neg_bound_cps unknown three_valued
bulwahn@45450: hide_const (open) cps_empty cps_single cps_bind cps_plus cps_if cps_not
bulwahn@45450:   pos_bound_cps_empty pos_bound_cps_single pos_bound_cps_bind pos_bound_cps_plus pos_bound_cps_if pos_bound_cps_not
bulwahn@45450:   neg_bound_cps_empty neg_bound_cps_single neg_bound_cps_bind neg_bound_cps_plus neg_bound_cps_if neg_bound_cps_not
bulwahn@45450:   Unknown Known Unknown_value Value No_value
bulwahn@45450: 
bulwahn@45450: end




