src/HOL/Tools/Quickcheck/exhaustive_generators.ML
author bulwahn
Fri, 18 Mar 2011 18:19:42 +0100
changeset 42028 bd6515e113b2
parent 42027 5e40c152c396
child 42159 234ec7011e5d
permissions -rw-r--r--
passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41923
f05fc0711bc7 renaming signatures and structures; correcting header
bulwahn
parents: 41920
diff changeset
     1
(*  Title:      HOL/Tools/Quickcheck/exhaustive_generators.ML
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
     2
    Author:     Lukas Bulwahn, TU Muenchen
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
     3
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
     4
Exhaustive generators for various types.
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
     5
*)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
     6
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
     7
signature EXHAUSTIVE_GENERATORS =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
     8
sig
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
     9
  val compile_generator_expr:
42027
5e40c152c396 extending the test data generators to take the evaluation terms as arguments
bulwahn
parents: 41966
diff changeset
    10
    Proof.context -> term * term list -> int -> term list option * Quickcheck.report option
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
    11
  val compile_generator_exprs:
41862
a38536bf2736 adding function Quickcheck.test_terms to provide checking a batch of terms
bulwahn
parents: 41861
diff changeset
    12
    Proof.context -> term list -> (int -> term list option) list
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    13
  val put_counterexample: (unit -> int -> term list option)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    14
    -> Proof.context -> Proof.context
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
    15
  val put_counterexample_batch: (unit -> (int -> term list option) list)
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
    16
    -> Proof.context -> Proof.context
40907
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    17
  val smart_quantifier : bool Config.T;
41903
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
    18
  val quickcheck_pretty : bool Config.T;
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    19
  val setup: theory -> theory
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    20
end;
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    21
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    22
structure Exhaustive_Generators : EXHAUSTIVE_GENERATORS =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    23
struct
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    24
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
    25
(* static options *)
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
    26
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
    27
val define_foundationally = false
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
    28
40907
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    29
(* dynamic options *)
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    30
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    31
val (smart_quantifier, setup_smart_quantifier) =
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    32
  Attrib.config_bool "quickcheck_smart_quantifier" (K true)
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    33
41903
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
    34
val (quickcheck_pretty, setup_quickcheck_pretty) =
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
    35
  Attrib.config_bool "quickcheck_pretty" (K true)
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
    36
 
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    37
(** general term functions **)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    38
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    39
fun mk_measure f =
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    40
  let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    41
    val Type ("fun", [T, @{typ nat}]) = fastype_of f 
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    42
  in
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    43
    Const (@{const_name Wellfounded.measure},
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    44
      (T --> @{typ nat}) --> HOLogic.mk_prodT (T, T) --> @{typ bool})
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    45
    $ f
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    46
  end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    47
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    48
fun mk_sumcases rT f (Type (@{type_name Sum_Type.sum}, [TL, TR])) =
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    49
  let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    50
    val lt = mk_sumcases rT f TL
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    51
    val rt = mk_sumcases rT f TR
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    52
  in
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    53
    SumTree.mk_sumcase TL TR rT lt rt
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    54
  end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    55
  | mk_sumcases _ f T = f T
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    56
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
    57
fun mk_undefined T = Const(@{const_name undefined}, T)
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
    58
  
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    59
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    60
(** abstract syntax **)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    61
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    62
fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => Code_Evaluation.term"});
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    63
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    64
val size = @{term "i :: code_numeral"}
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    65
val size_pred = @{term "(i :: code_numeral) - 1"}
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    66
val size_ge_zero = @{term "(i :: code_numeral) > 0"}
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    67
fun test_function T = Free ("f", termifyT T --> @{typ "term list option"})
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    68
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    69
fun mk_none_continuation (x, y) =
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    70
  let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    71
    val (T as Type(@{type_name "option"}, [T'])) = fastype_of x
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    72
  in
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    73
    Const (@{const_name "Quickcheck_Exhaustive.orelse"}, T --> T --> T)
40899
ef6fde932f4c improving readability of Smallcheck theory; adding constant orelse to improve performance of the function package
bulwahn
parents: 40845
diff changeset
    74
      $ x $ y
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    75
  end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    76
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    77
(** datatypes **)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    78
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    79
(* constructing exhaustive generator instances on datatypes *)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    80
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    81
exception FUNCTION_TYPE;
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    82
val exhaustiveN = "exhaustive";
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    83
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    84
fun exhaustiveT T = (termifyT T --> @{typ "Code_Evaluation.term list option"})
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    85
  --> @{typ code_numeral} --> @{typ "Code_Evaluation.term list option"}
41085
a549ff1d4070 adding a smarter enumeration scheme for finite functions
bulwahn
parents: 40913
diff changeset
    86
a549ff1d4070 adding a smarter enumeration scheme for finite functions
bulwahn
parents: 40913
diff changeset
    87
fun check_allT T = (termifyT T --> @{typ "Code_Evaluation.term list option"})
a549ff1d4070 adding a smarter enumeration scheme for finite functions
bulwahn
parents: 40913
diff changeset
    88
  --> @{typ "Code_Evaluation.term list option"}
a549ff1d4070 adding a smarter enumeration scheme for finite functions
bulwahn
parents: 40913
diff changeset
    89
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41935
diff changeset
    90
fun mk_equations descr vs tycos exhaustives (Ts, Us) =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    91
  let
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    92
    fun mk_call T =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    93
      let
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41935
diff changeset
    94
        val exhaustive = Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    95
      in
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    96
        (T, (fn t => exhaustive $
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    97
          (HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, @{typ "Code_Evaluation.term list option"})
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    98
          $ absdummy (T, absdummy (@{typ "unit => Code_Evaluation.term"}, t))) $ size_pred))
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    99
      end
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   100
    fun mk_aux_call fTs (k, _) (tyco, Ts) =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   101
      let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   102
        val T = Type (tyco, Ts)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   103
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   104
      in
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   105
       (T, (fn t => nth exhaustives k $
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   106
          (HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, @{typ "Code_Evaluation.term list option"})
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   107
            $ absdummy (T, absdummy (@{typ "unit => Code_Evaluation.term"}, t))) $ size_pred))
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   108
      end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   109
    fun mk_consexpr simpleT (c, xs) =
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   110
      let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   111
        val (Ts, fns) = split_list xs
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   112
        val constr = Const (c, Ts ---> simpleT)
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   113
        val bounds = map (fn x => Bound (2 * x + 1)) (((length xs) - 1) downto 0)
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   114
        val term_bounds = map (fn x => Bound (2 * x)) (((length xs) - 1) downto 0)
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   115
        val Eval_App = Const ("Code_Evaluation.App", HOLogic.termT --> HOLogic.termT --> HOLogic.termT)
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   116
        val Eval_Const = Const ("Code_Evaluation.Const", HOLogic.literalT --> @{typ typerep} --> HOLogic.termT)
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   117
        val term = fold (fn u => fn t => Eval_App $ t $ (u $ @{term "()"}))
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   118
          bounds (Eval_Const $ HOLogic.mk_literal c $ HOLogic.mk_typerep (Ts ---> simpleT))
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   119
        val start_term = test_function simpleT $ 
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   120
        (HOLogic.pair_const simpleT @{typ "unit => Code_Evaluation.term"}
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   121
          $ (list_comb (constr, bounds)) $ absdummy (@{typ unit}, term))
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   122
      in fold_rev (fn f => fn t => f t) fns start_term end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   123
    fun mk_rhs exprs =
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   124
        @{term "If :: bool => term list option => term list option => term list option"}
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   125
            $ size_ge_zero $ (foldr1 mk_none_continuation exprs) $ @{term "None :: term list option"}
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   126
    val rhss =
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   127
      Datatype_Aux.interpret_construction descr vs
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   128
        { atyp = mk_call, dtyp = mk_aux_call }
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   129
      |> (map o apfst) Type
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   130
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   131
      |> map mk_rhs
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   132
    val lhss = map2 (fn t => fn T => t $ test_function T $ size) exhaustives (Ts @ Us);
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   133
    val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   134
  in
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   135
    eqs
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   136
  end
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   137
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   138
(* foundational definition with the function package *)
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   139
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   140
val less_int_pred = @{lemma "i > 0 ==> Code_Numeral.nat_of ((i :: code_numeral) - 1) < Code_Numeral.nat_of i" by auto}
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   141
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   142
fun mk_single_measure T = HOLogic.mk_comp (@{term "Code_Numeral.nat_of"},
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   143
    Const (@{const_name "Product_Type.snd"}, T --> @{typ "code_numeral"}))
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   144
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   145
fun mk_termination_measure T =
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   146
  let
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   147
    val T' = fst (HOLogic.dest_prodT (HOLogic.dest_setT T))
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   148
  in
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   149
    mk_measure (mk_sumcases @{typ nat} mk_single_measure T')
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   150
  end
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   151
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   152
fun termination_tac ctxt = 
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   153
  Function_Relation.relation_tac ctxt mk_termination_measure 1
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   154
  THEN rtac @{thm wf_measure} 1
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   155
  THEN (REPEAT_DETERM (Simplifier.asm_full_simp_tac 
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   156
    (HOL_basic_ss addsimps [@{thm in_measure}, @{thm o_def}, @{thm snd_conv},
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   157
     @{thm nat_mono_iff}, less_int_pred] @ @{thms sum.cases}) 1))
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   158
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   159
fun pat_completeness_auto ctxt =
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   160
  Pat_Completeness.pat_completeness_tac ctxt 1
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   161
  THEN auto_tac (clasimpset_of ctxt)    
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   162
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   163
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   164
(* creating the instances *)
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   165
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   166
fun instantiate_exhaustive_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   167
  let
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   168
    val _ = Datatype_Aux.message config "Creating exhaustive generators ...";
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   169
    val exhaustivesN = map (prefix (exhaustiveN ^ "_")) (names @ auxnames);
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   170
  in
40641
5615cc557120 moving the error handling to the right scope in smallvalue_generators
bulwahn
parents: 40640
diff changeset
   171
    thy
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   172
    |> Class.instantiation (tycos, vs, @{sort exhaustive})
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   173
    |> (if define_foundationally then
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   174
      let
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   175
        val exhaustives = map2 (fn name => fn T => Free (name, exhaustiveT T)) exhaustivesN (Ts @ Us)
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41935
diff changeset
   176
        val eqs = mk_equations descr vs tycos exhaustives (Ts, Us)
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   177
      in
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   178
        Function.add_function
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   179
          (map (fn (name, T) =>
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   180
              Syntax.no_syn (Binding.conceal (Binding.name name), SOME (exhaustiveT T)))
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   181
                (exhaustivesN ~~ (Ts @ Us)))
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   182
            (map (pair (apfst Binding.conceal Attrib.empty_binding)) eqs)
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   183
          Function_Common.default_config pat_completeness_auto
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   184
        #> snd
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   185
        #> Local_Theory.restore
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   186
        #> (fn lthy => Function.prove_termination NONE (termination_tac lthy) lthy)
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   187
        #> snd
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   188
      end
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   189
    else
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   190
      fold_map (fn (name, T) => Local_Theory.define
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   191
          ((Binding.conceal (Binding.name name), NoSyn),
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   192
            (apfst Binding.conceal Attrib.empty_binding, mk_undefined (exhaustiveT T)))
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   193
        #> apfst fst) (exhaustivesN ~~ (Ts @ Us))
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   194
      #> (fn (exhaustives, lthy) =>
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   195
        let
41966
d65835c381dd tuned exhaustive_generators
bulwahn
parents: 41963
diff changeset
   196
          val eqs_t = mk_equations descr vs tycos exhaustives (Ts, Us)
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   197
          val eqs = map (fn eq => Goal.prove lthy ["f", "i"] [] eq
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   198
            (fn _ => Skip_Proof.cheat_tac (ProofContext.theory_of lthy))) eqs_t
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   199
        in
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   200
          fold (fn (name, eq) => Local_Theory.note
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   201
          ((Binding.conceal (Binding.qualify true prfx
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   202
             (Binding.qualify true name (Binding.name "simps"))),
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   203
             Code.add_default_eqn_attrib :: map (Attrib.internal o K)
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   204
               [Simplifier.simp_add, Nitpick_Simps.add]), [eq]) #> snd) (exhaustivesN ~~ eqs) lthy
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   205
        end))
40641
5615cc557120 moving the error handling to the right scope in smallvalue_generators
bulwahn
parents: 40640
diff changeset
   206
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
5615cc557120 moving the error handling to the right scope in smallvalue_generators
bulwahn
parents: 40640
diff changeset
   207
  end handle FUNCTION_TYPE =>
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   208
    (Datatype_Aux.message config
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41935
diff changeset
   209
      "Creation of exhaustive generators failed because the datatype contains a function type";
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   210
    thy)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   211
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   212
(** building and compiling generator expressions **)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   213
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   214
fun mk_generator_expr ctxt (t, eval_terms) =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   215
  let
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   216
    val thy = ProofContext.theory_of ctxt
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   217
    val ctxt' = Variable.auto_fixes t ctxt
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   218
    val names = Term.add_free_names t []
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   219
    val frees = map Free (Term.add_frees t [])
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   220
    val ([depth_name], ctxt'') = Variable.variant_fixes ["depth"] ctxt'
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   221
    val (term_names, ctxt''') = Variable.variant_fixes (map (prefix "t_") names) ctxt''
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   222
    val depth = Free (depth_name, @{typ code_numeral})
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   223
    val term_vars = map (fn n => Free (n, @{typ "unit => term"})) term_names
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   224
    val terms = HOLogic.mk_list @{typ term} (map (fn v => v $ @{term "()"}) term_vars)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   225
    val appendC = @{term "List.append :: term list => term list => term list"}  
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   226
    val return = @{term "Some :: term list => term list option"} $ (appendC $ terms $
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   227
      (HOLogic.mk_list @{typ "term"} (map (fn t => HOLogic.mk_term_of (fastype_of t) t) eval_terms)))
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   228
    fun mk_exhaustive_closure (free as Free (_, T), term_var) t =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   229
      if Sign.of_sort thy (T, @{sort enum}) then
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   230
        Const (@{const_name "Quickcheck_Exhaustive.check_all_class.check_all"}, check_allT T)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   231
          $ (HOLogic.split_const (T, @{typ "unit => term"}, @{typ "term list option"}) 
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   232
            $ lambda free (lambda term_var t))
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   233
      else
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   234
        Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   235
          $ (HOLogic.split_const (T, @{typ "unit => term"}, @{typ "term list option"})
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   236
            $ lambda free (lambda term_var t)) $ depth
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   237
    val none_t = @{term "None :: term list option"}
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   238
    fun mk_safe_if (cond, then_t, else_t) =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   239
      @{term "Quickcheck_Exhaustive.catch_match :: term list option => term list option => term list option"} $
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   240
        (@{term "If :: bool => term list option => term list option => term list option"}
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   241
        $ cond $ then_t $ else_t) $ none_t;
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   242
    fun strip_imp (Const(@{const_name HOL.implies},_) $ A $ B) = apfst (cons A) (strip_imp B)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   243
        | strip_imp A = ([], A)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   244
    fun lookup v = the (AList.lookup (op =) (names ~~ (frees ~~ term_vars)) v)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   245
    fun mk_naive_test_term t =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   246
      fold_rev mk_exhaustive_closure (frees ~~ term_vars) (mk_safe_if (t, none_t, return)) 
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   247
    fun mk_smart_test_term' concl bound_vars assms =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   248
      let
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   249
        fun vars_of t = subtract (op =) bound_vars (Term.add_free_names t [])
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   250
        val (vars, check) =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   251
          case assms of [] => (vars_of concl, (concl, none_t, return))
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   252
            | assm :: assms => (vars_of assm, (assm,
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   253
                mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms, none_t))
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   254
      in
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   255
        fold_rev mk_exhaustive_closure (map lookup vars) (mk_safe_if check)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   256
      end
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   257
    fun mk_smart_test_term t =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   258
      let
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   259
        val (assms, concl) = strip_imp t
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   260
      in
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   261
        mk_smart_test_term' concl [] assms
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   262
      end
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   263
    val mk_test_term =
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   264
      if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term 
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   265
  in lambda depth (mk_test_term t) end
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   266
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   267
(** generator compiliation **)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   268
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41178
diff changeset
   269
structure Counterexample = Proof_Data
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41178
diff changeset
   270
(
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   271
  type T = unit -> int -> term list option
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41178
diff changeset
   272
  (* FIXME avoid user error with non-user text *)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   273
  fun init _ () = error "Counterexample"
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   274
);
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   275
val put_counterexample = Counterexample.put;
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   276
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   277
structure Counterexample_Batch = Proof_Data
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   278
(
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   279
  type T = unit -> (int -> term list option) list
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   280
  (* FIXME avoid user error with non-user text *)
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   281
  fun init _ () = error "Counterexample"
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   282
);
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   283
val put_counterexample_batch = Counterexample_Batch.put;
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   284
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   285
val target = "Quickcheck";
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   286
42027
5e40c152c396 extending the test data generators to take the evaluation terms as arguments
bulwahn
parents: 41966
diff changeset
   287
fun compile_generator_expr ctxt (t, eval_terms) =
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40907
diff changeset
   288
  let
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40907
diff changeset
   289
    val thy = ProofContext.theory_of ctxt
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   290
    val t' = mk_generator_expr ctxt (t, eval_terms);
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40907
diff changeset
   291
    val compile = Code_Runtime.dynamic_value_strict
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   292
      (Counterexample.get, put_counterexample, "Exhaustive_Generators.put_counterexample")
40911
7febf76e0a69 moving iteration of tests to the testers in quickcheck
bulwahn
parents: 40907
diff changeset
   293
      thy (SOME target) (fn proc => fn g => g #> (Option.map o map) proc) t' [];
41176
ede546773fd6 adding postprocessing for sets in term construction of quickcheck
bulwahn
parents: 41085
diff changeset
   294
  in
41903
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
   295
    fn size => rpair NONE (compile size |> 
41935
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   296
      (if Config.get ctxt quickcheck_pretty then
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   297
        Option.map (map Quickcheck_Common.post_process_term) else I))
41176
ede546773fd6 adding postprocessing for sets in term construction of quickcheck
bulwahn
parents: 41085
diff changeset
   298
  end;
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   299
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   300
fun compile_generator_exprs ctxt ts =
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   301
  let
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   302
    val thy = ProofContext.theory_of ctxt
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   303
    val ts' = map (fn t => mk_generator_expr ctxt (t, [])) ts;
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   304
    val compiles = Code_Runtime.dynamic_value_strict
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   305
      (Counterexample_Batch.get, put_counterexample_batch,
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   306
        "Exhaustive_Generators.put_counterexample_batch")
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   307
      thy (SOME target) (fn proc => map (fn g => g #> (Option.map o map) proc))
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   308
      (HOLogic.mk_list @{typ "code_numeral => term list option"} ts') [];
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   309
  in
41935
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   310
    map (fn compile => fn size => compile size
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   311
      |> Option.map (map Quickcheck_Common.post_process_term)) compiles
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   312
  end;
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   313
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   314
  
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   315
(** setup **)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   316
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   317
val setup =
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   318
  Datatype.interpretation
41928
05abcee548a1 adaptions in generators using the common functions
bulwahn
parents: 41923
diff changeset
   319
    (Quickcheck_Common.ensure_sort_datatype (@{sort exhaustive}, instantiate_exhaustive_datatype))
40907
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
   320
  #> setup_smart_quantifier
41903
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
   321
  #> setup_quickcheck_pretty
41900
bulwahn
parents: 41862
diff changeset
   322
  #> Context.theory_map (Quickcheck.add_generator ("exhaustive", compile_generator_expr))
bulwahn
parents: 41862
diff changeset
   323
  #> Context.theory_map (Quickcheck.add_batch_generator ("exhaustive", compile_generator_exprs));
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   324
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   325
end;