src/HOL/Tools/Quickcheck/exhaustive_generators.ML
author blanchet
Fri, 05 Sep 2014 00:41:01 +0200
changeset 58191 b3c71d630777
parent 58186 a6c3962ea907
child 58225 f5144942a83a
permissions -rw-r--r--
pretend code generation is a ctr_sugar plugin
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
58191
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
     9
  val exhaustive_plugin: string
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
    10
  val bounded_forall_plugin: string
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
    11
  val full_exhaustive_plugin: string
58186
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
    12
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    13
  val compile_generator_expr:
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
    14
    Proof.context -> (term * term list) list -> bool -> int list -> (bool * term list) option * Quickcheck.report option
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
    15
  val compile_generator_exprs: Proof.context -> term list -> (int -> term list option) list
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
    16
  val compile_validator_exprs: Proof.context -> term list -> (int -> bool) list
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    17
  val put_counterexample: (unit -> Code_Numeral.natural -> bool -> Code_Numeral.natural -> (bool * term list) option)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    18
    -> Proof.context -> Proof.context
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    19
  val put_counterexample_batch: (unit -> (Code_Numeral.natural -> term list option) list)
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
    20
    -> Proof.context -> Proof.context
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    21
  val put_validator_batch: (unit -> (Code_Numeral.natural -> bool) list) -> Proof.context -> Proof.context
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    22
  exception Counterexample of term list
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
    23
  val smart_quantifier : bool Config.T
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
    24
  val optimise_equality : bool Config.T
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
    25
  val quickcheck_pretty : bool Config.T
45484
23871e17dddb setting up exhaustive generators which are used for the smart generators
bulwahn
parents: 45420
diff changeset
    26
  val setup_exhaustive_datatype_interpretation : theory -> theory
48178
0192811f0a96 exporting important function for the "many conjecture refutation" compilation of quickcheck
bulwahn
parents: 48013
diff changeset
    27
  val setup_bounded_forall_datatype_interpretation : theory -> theory
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    28
  val setup: theory -> theory
45924
f03dd48829d8 exporting instantiation functions in quickcheck for their usage in abstract generators
bulwahn
parents: 45923
diff changeset
    29
  
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57996
diff changeset
    30
  val instantiate_full_exhaustive_datatype : Old_Datatype_Aux.config -> Old_Datatype_Aux.descr ->
45924
f03dd48829d8 exporting instantiation functions in quickcheck for their usage in abstract generators
bulwahn
parents: 45923
diff changeset
    31
    (string * sort) list -> string list -> string -> string list * string list -> typ list * typ list -> theory -> theory
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57996
diff changeset
    32
  val instantiate_exhaustive_datatype : Old_Datatype_Aux.config -> Old_Datatype_Aux.descr ->
46565
ad21900e0ee9 subtype preprocessing in Quickcheck;
bulwahn
parents: 46331
diff changeset
    33
    (string * sort) list -> string list -> string -> string list * string list -> typ list * typ list -> theory -> theory
ad21900e0ee9 subtype preprocessing in Quickcheck;
bulwahn
parents: 46331
diff changeset
    34
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    35
end;
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    36
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    37
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
    38
struct
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    39
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    40
(* basics *)
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    41
58191
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
    42
val exhaustive_plugin = "exhaustive"
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
    43
val bounded_forall_plugin = "bounded_forall"
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
    44
val full_exhaustive_plugin = "full_exhaustive"
58186
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
    45
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
    46
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    47
(** dynamic options **)
40907
45ba9f05583a adding smart quantifiers to exhaustive testing
bulwahn
parents: 40901
diff changeset
    48
42616
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42391
diff changeset
    49
val smart_quantifier = Attrib.setup_config_bool @{binding quickcheck_smart_quantifier} (K true)
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
    50
val optimise_equality = Attrib.setup_config_bool @{binding quickcheck_optimise_equality} (K true)
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
    51
42616
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42391
diff changeset
    52
val fast = Attrib.setup_config_bool @{binding quickcheck_fast} (K false)
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42391
diff changeset
    53
val bounded_forall = Attrib.setup_config_bool @{binding quickcheck_bounded_forall} (K false)
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42391
diff changeset
    54
val full_support = Attrib.setup_config_bool @{binding quickcheck_full_support} (K true)
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42391
diff changeset
    55
val quickcheck_pretty = Attrib.setup_config_bool @{binding quickcheck_pretty} (K true)
41903
39fd77f0ae59 fixing postprocessing; adding a configuration to enable and disable pretty presentation of quickcheck's counterexample
bulwahn
parents: 41902
diff changeset
    56
 
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    57
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    58
(** abstract syntax **)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    59
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
    60
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
    61
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    62
val size = @{term "i :: natural"}
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    63
val size_pred = @{term "(i :: natural) - 1"}
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    64
val size_ge_zero = @{term "(i :: natural) > 0"}
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
    65
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    66
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
    67
  let
46306
bulwahn
parents: 46042
diff changeset
    68
    val (T as Type(@{type_name "option"}, _)) = fastype_of x
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    69
  in
42214
9ca13615c619 refactoring generator definition in quickcheck and removing clone
bulwahn
parents: 42195
diff changeset
    70
    Const (@{const_name "Quickcheck_Exhaustive.orelse"}, T --> T --> T) $ x $ y
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    71
  end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    72
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
    73
fun mk_if (b, t, e) =
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
    74
  let
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
    75
    val T = fastype_of t
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
    76
  in Const (@{const_name "HOL.If"}, @{typ bool} --> T --> T --> T) $ b $ t $ e end
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
    77
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    78
(* handling inductive datatypes *)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    79
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    80
(** constructing generator instances **)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    81
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
    82
exception FUNCTION_TYPE;
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    83
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    84
exception Counterexample of term list
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    85
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
    86
val resultT =  @{typ "(bool * term list) option"};
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
    87
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
    88
val exhaustiveN = "exhaustive";
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
    89
val full_exhaustiveN = "full_exhaustive";
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    90
val bounded_forallN = "bounded_forall";
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    91
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    92
fun fast_exhaustiveT T = (T --> @{typ unit})
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    93
  --> @{typ natural} --> @{typ unit}
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
    94
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    95
fun exhaustiveT T = (T --> resultT) --> @{typ natural} --> resultT
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
    96
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    97
fun bounded_forallT T = (T --> @{typ bool}) --> @{typ natural} --> @{typ bool}
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
    98
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
    99
fun full_exhaustiveT T = (termifyT T --> resultT) --> @{typ natural} --> resultT
41085
a549ff1d4070 adding a smarter enumeration scheme for finite functions
bulwahn
parents: 40913
diff changeset
   100
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   101
fun check_allT T = (termifyT T --> resultT) --> resultT
41085
a549ff1d4070 adding a smarter enumeration scheme for finite functions
bulwahn
parents: 40913
diff changeset
   102
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   103
fun mk_equation_terms generics (descr, vs, Ts) =
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   104
  let
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   105
    val (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, exhaustives) = generics
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   106
    val rhss =
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57996
diff changeset
   107
      Old_Datatype_Aux.interpret_construction descr vs
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   108
        { atyp = mk_call, dtyp = mk_aux_call }
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   109
      |> (map o apfst) Type
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   110
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   111
      |> map mk_rhs
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   112
    val lhss = map2 (fn t => fn T => t $ test_function T $ size) exhaustives Ts
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   113
  in
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   114
    map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   115
  end
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   116
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   117
fun gen_mk_call c T =  (T, fn t => c T $ absdummy T t $ size_pred)
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   118
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   119
fun gen_mk_aux_call functerms fTs (k, _) (tyco, Ts) =
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   120
  let
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   121
    val T = Type (tyco, Ts)
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   122
    val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   123
  in
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   124
   (T, fn t => nth functerms k $ absdummy T t $ size_pred)
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   125
  end
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   126
46306
bulwahn
parents: 46042
diff changeset
   127
fun gen_mk_consexpr test_function simpleT (c, xs) =
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   128
  let
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   129
    val (Ts, fns) = split_list xs
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   130
    val constr = Const (c, Ts ---> simpleT)
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   131
    val bounds = map Bound (((length xs) - 1) downto 0)
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   132
    val start_term = test_function simpleT $ list_comb (constr, bounds)
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   133
  in fold_rev (fn f => fn t => f t) fns start_term end
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   134
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   135
fun mk_equations functerms =
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   136
  let
45724
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   137
    fun test_function T = Free ("f", T --> resultT)
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   138
    val mk_call = gen_mk_call (fn T =>
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   139
      Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T))
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   140
    val mk_aux_call = gen_mk_aux_call functerms
46306
bulwahn
parents: 46042
diff changeset
   141
    val mk_consexpr = gen_mk_consexpr test_function
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   142
    fun mk_rhs exprs =
45724
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   143
      mk_if (size_ge_zero, foldr1 mk_none_continuation exprs, Const (@{const_name "None"}, resultT))
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   144
  in
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   145
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   146
  end
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   147
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   148
fun mk_bounded_forall_equations functerms =
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   149
  let
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   150
    fun test_function T = Free ("P", T --> @{typ bool})
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   151
    val mk_call = gen_mk_call (fn T =>
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   152
      Const (@{const_name "Quickcheck_Exhaustive.bounded_forall_class.bounded_forall"},
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   153
        bounded_forallT T))
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   154
    val mk_aux_call = gen_mk_aux_call functerms
46306
bulwahn
parents: 46042
diff changeset
   155
    val mk_consexpr = gen_mk_consexpr test_function
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   156
    fun mk_rhs exprs =
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   157
      mk_if (size_ge_zero, foldr1 HOLogic.mk_conj exprs, @{term "True"})
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   158
  in
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   159
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   160
  end
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   161
  
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   162
fun mk_full_equations functerms =
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   163
  let
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   164
    fun test_function T = Free ("f", termifyT T --> resultT)
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   165
    fun split_const T = HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, resultT)
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   166
    fun mk_call T =
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   167
      let
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   168
        val full_exhaustive =
42310
c664cc5cc5e9 splitting exhaustive and full_exhaustive into separate type classes
bulwahn
parents: 42309
diff changeset
   169
          Const (@{const_name "Quickcheck_Exhaustive.full_exhaustive_class.full_exhaustive"},
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   170
            full_exhaustiveT T)
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   171
      in                                   
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   172
        (T, fn t => full_exhaustive $
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   173
          (split_const T $ 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
   174
      end
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   175
    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
   176
      let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   177
        val T = Type (tyco, Ts)
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   178
        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
   179
      in
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   180
        (T, fn t => nth functerms k $
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   181
          (split_const T $ 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
   182
      end
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   183
    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
   184
      let
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   185
        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
   186
        val constr = Const (c, Ts ---> simpleT)
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   187
        val bounds = map (fn x => Bound (2 * x + 1)) (((length xs) - 1) downto 0)
56257
589fafcc7cb6 more antiquotations;
wenzelm
parents: 56241
diff changeset
   188
        val Eval_App =
589fafcc7cb6 more antiquotations;
wenzelm
parents: 56241
diff changeset
   189
          Const (@{const_name Code_Evaluation.App}, HOLogic.termT --> HOLogic.termT --> HOLogic.termT)
589fafcc7cb6 more antiquotations;
wenzelm
parents: 56241
diff changeset
   190
        val Eval_Const =
589fafcc7cb6 more antiquotations;
wenzelm
parents: 56241
diff changeset
   191
          Const (@{const_name Code_Evaluation.Const}, HOLogic.literalT --> @{typ typerep} --> HOLogic.termT)
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   192
        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
   193
          bounds (Eval_Const $ HOLogic.mk_literal c $ HOLogic.mk_typerep (Ts ---> simpleT))
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   194
        val start_term = test_function simpleT $ 
40639
f1f0e6adca0a adding function generation to SmallCheck; activating exhaustive search space testing
bulwahn
parents: 40420
diff changeset
   195
        (HOLogic.pair_const simpleT @{typ "unit => Code_Evaluation.term"}
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   196
          $ (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
   197
      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
   198
    fun mk_rhs exprs =
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   199
      mk_if (size_ge_zero, foldr1 mk_none_continuation exprs,
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   200
        Const (@{const_name "None"}, resultT))
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   201
  in
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   202
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   203
  end
42307
72e2fabb4bc2 creating a general mk_equation_terms for the different compilations
bulwahn
parents: 42306
diff changeset
   204
  
40901
8fdfa9c4e7ed smallvalue_generator are defined quick via oracle or sound via function package
bulwahn
parents: 40899
diff changeset
   205
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   206
(** instantiating generator classes **)
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   207
  
42325
104472645443 made SML/NJ happy;
wenzelm
parents: 42316
diff changeset
   208
fun contains_recursive_type_under_function_types xs =
42312
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   209
  exists (fn (_, (_, _, cs)) => cs |> exists (snd #> exists (fn dT =>
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57996
diff changeset
   210
    (case Old_Datatype_Aux.strip_dtyp dT of (_ :: _, Old_Datatype_Aux.DtRec _) => true | _ => false)))) xs
42312
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   211
    
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   212
fun instantiate_datatype (name, constprfx, sort, mk_equations, mk_T, argnames)
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   213
    config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
42312
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   214
  if not (contains_recursive_type_under_function_types descr) then
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   215
    let
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57996
diff changeset
   216
      val _ = Old_Datatype_Aux.message config ("Creating " ^ name ^ "...")
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   217
      val fullnames = map (prefix (constprfx ^ "_")) (names @ auxnames)
42312
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   218
    in
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   219
      thy
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   220
      |> Class.instantiation (tycos, vs, sort)
42312
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   221
      |> Quickcheck_Common.define_functions
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   222
          (fn functerms => mk_equations functerms (descr, vs, Ts @ Us), NONE)
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   223
          prfx argnames fullnames (map mk_T (Ts @ Us))
42312
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   224
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   225
    end
5bf3b9612e43 ensuring datatype limitations before the instantiation in quickcheck_exhaustive
bulwahn
parents: 42310
diff changeset
   226
  else
58112
8081087096ad renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents: 57996
diff changeset
   227
    (Old_Datatype_Aux.message config
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   228
      ("Creation of " ^ name ^ " failed because the datatype is recursive under a function type");
42230
594480d25aaa deriving bounded_forall instances in quickcheck_exhaustive
bulwahn
parents: 42229
diff changeset
   229
    thy)
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   230
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   231
val instantiate_bounded_forall_datatype = instantiate_datatype
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   232
 ("bounded universal quantifiers", bounded_forallN, @{sort bounded_forall},
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   233
   mk_bounded_forall_equations, bounded_forallT, ["P", "i"]);
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   234
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   235
val instantiate_exhaustive_datatype = instantiate_datatype  
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   236
  ("exhaustive generators", exhaustiveN, @{sort exhaustive},
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   237
    mk_equations, exhaustiveT, ["f", "i"])
42315
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   238
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   239
val instantiate_full_exhaustive_datatype = instantiate_datatype
95dfa082065a generalizing instantiate_datatype in quickcheck_exhaustive to remove clones for different compilations
bulwahn
parents: 42314
diff changeset
   240
  ("full exhaustive generators", full_exhaustiveN, @{sort full_exhaustive},
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   241
    mk_full_equations, full_exhaustiveT, ["f", "i"])
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   242
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   243
(* building and compiling generator expressions *)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   244
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   245
fun mk_let_expr (x, t, e) genuine =
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   246
  let
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   247
    val (T1, T2) = (fastype_of x, fastype_of (e genuine))
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   248
  in  
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   249
    Const (@{const_name Let}, T1 --> (T1 --> T2) --> T2) $ t $ lambda x (e genuine)
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   250
  end
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   251
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   252
fun mk_safe_let_expr genuine_only none safe (x, t, e) genuine =
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   253
  let
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   254
    val (T1, T2) = (fastype_of x, fastype_of (e genuine))
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   255
    val if_t = Const (@{const_name "If"}, @{typ bool} --> T2 --> T2 --> T2)
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   256
  in
51126
df86080de4cb reform of predicate compiler / quickcheck theories:
haftmann
parents: 50046
diff changeset
   257
    Const (@{const_name "Quickcheck_Random.catch_match"}, T2 --> T2 --> T2) $ 
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   258
      (Const (@{const_name Let}, T1 --> (T1 --> T2) --> T2) $ t $ lambda x (e genuine)) $
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   259
      (if_t $ genuine_only $ none $ safe false)
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   260
  end
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   261
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   262
fun mk_test_term lookup mk_closure mk_if mk_let none_t return ctxt =
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   263
  let
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   264
    val cnstrs = flat (maps
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   265
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
58145
3cfbb68ad2e0 ported Quickcheck to support new datatypes better
blanchet
parents: 58112
diff changeset
   266
      (Symtab.dest (BNF_LFP_Compat.get_all (Proof_Context.theory_of ctxt) BNF_LFP_Compat.Keep_Nesting)))
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   267
    fun is_constrt (Const (s, T), ts) = (case (AList.lookup (op =) cnstrs s, body_type T) of
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   268
        (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname'
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   269
      | _ => false)
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   270
      | is_constrt _ = false
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   271
    fun mk_naive_test_term t =
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   272
      fold_rev mk_closure (map lookup (Term.add_free_names t []))
45761
90028fd2f1fa exhaustive returns if a counterexample is genuine or potentially spurious in the presence of assumptions more correctly
bulwahn
parents: 45754
diff changeset
   273
        (mk_if (t, none_t, return) true)
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   274
    fun mk_test (vars, check) = fold_rev mk_closure (map lookup vars) check
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   275
    fun mk_smart_test_term' concl bound_vars assms genuine =
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   276
      let
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   277
        fun vars_of t = subtract (op =) bound_vars (Term.add_free_names t [])
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   278
        fun mk_equality_term (lhs, f as Free (x, _)) c (assm, assms) =
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   279
          if member (op =) (Term.add_free_names lhs bound_vars) x then
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   280
            c (assm, assms)
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   281
          else
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   282
            (let
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   283
               val rec_call = mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   284
               fun safe genuine =
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   285
                 the_default I (Option.map mk_closure (try lookup x)) (rec_call genuine)
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   286
            in
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   287
              mk_test (remove (op =) x (vars_of assm),
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   288
                mk_let safe f (try lookup x) lhs 
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   289
                  (mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms) genuine)
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   290
            
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   291
            end)
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   292
          | mk_equality_term (lhs, t) c (assm, assms) =
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   293
            if is_constrt (strip_comb t) then
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   294
              let
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   295
                val (constr, args) = strip_comb t
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   296
                val T = fastype_of t
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   297
                val vars = map Free (Variable.variant_frees ctxt (concl :: assms)
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   298
                  (map (fn t => ("x", fastype_of t)) args))
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   299
                val varnames = map (fst o dest_Free) vars
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   300
                val dummy_var = Free (singleton
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   301
                  (Variable.variant_frees ctxt (concl :: assms @ vars)) ("dummy", T))
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   302
                val new_assms = map HOLogic.mk_eq (vars ~~ args)
48273
65233084e9d7 improved equality optimisation in Quickcheck
bulwahn
parents: 48178
diff changeset
   303
                val bound_vars' = union (op =) (vars_of lhs) (union (op =) varnames bound_vars)
65233084e9d7 improved equality optimisation in Quickcheck
bulwahn
parents: 48178
diff changeset
   304
                val cont_t = mk_smart_test_term' concl bound_vars' (new_assms @ assms) genuine
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   305
              in
51672
d5c5e088ebdf case translations performed in a separate check phase (with adjustments by traytel)
berghofe
parents: 51143
diff changeset
   306
                mk_test (vars_of lhs,
51673
4dfa00e264d8 separate data used for case translation from the datatype package
traytel
parents: 51672
diff changeset
   307
                  Case_Translation.make_case ctxt Case_Translation.Quiet Name.context lhs
51672
d5c5e088ebdf case translations performed in a separate check phase (with adjustments by traytel)
berghofe
parents: 51143
diff changeset
   308
                    [(list_comb (constr, vars), cont_t), (dummy_var, none_t)])
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   309
              end
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   310
            else c (assm, assms)
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   311
        fun default (assm, assms) =
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   312
          mk_test (vars_of assm,
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   313
            mk_if (HOLogic.mk_not assm, none_t, 
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   314
            mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms) genuine)
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   315
      in
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   316
        case assms of [] => mk_test (vars_of concl, mk_if (concl, none_t, return) genuine)
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   317
          | assm :: assms =>
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   318
            if Config.get ctxt optimise_equality then
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   319
              (case try HOLogic.dest_eq assm of
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   320
                SOME (lhs, rhs) =>
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   321
                  mk_equality_term (lhs, rhs) (mk_equality_term (rhs, lhs) default) (assm, assms)
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   322
              | NONE => default (assm, assms))
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   323
            else default (assm, assms)
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   324
      end
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   325
    val mk_smart_test_term =
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   326
      Quickcheck_Common.strip_imp #> (fn (assms, concl) => mk_smart_test_term' concl [] assms true)
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   327
  in
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   328
    if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   329
  end
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   330
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   331
fun mk_fast_generator_expr ctxt (t, eval_terms) =
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   332
  let
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   333
    val ctxt' = Variable.auto_fixes t ctxt
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   334
    val names = Term.add_free_names t []
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   335
    val frees = map Free (Term.add_frees t [])
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   336
    fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
46306
bulwahn
parents: 46042
diff changeset
   337
    val ([depth_name], _) = Variable.variant_fixes ["depth"] ctxt'
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   338
    val depth = Free (depth_name, @{typ natural})
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   339
    fun return _ = @{term "throw_Counterexample :: term list => unit"} $
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   340
      (HOLogic.mk_list @{typ "term"}
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   341
        (map (fn t => HOLogic.mk_term_of (fastype_of t) t) (frees @ eval_terms)))
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   342
    fun mk_exhaustive_closure (free as Free (_, T)) t =
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   343
      Const (@{const_name "Quickcheck_Exhaustive.fast_exhaustive_class.fast_exhaustive"},
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   344
        fast_exhaustiveT T)
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   345
        $ lambda free t $ depth
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   346
    val none_t = @{term "()"}
45761
90028fd2f1fa exhaustive returns if a counterexample is genuine or potentially spurious in the presence of assumptions more correctly
bulwahn
parents: 45754
diff changeset
   347
    fun mk_safe_if (cond, then_t, else_t) genuine = mk_if (cond, then_t, else_t genuine)
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   348
    fun mk_let _ def v_opt t e = mk_let_expr (the_default def v_opt, t, e)
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   349
    val mk_test_term = mk_test_term lookup mk_exhaustive_closure mk_safe_if mk_let none_t return ctxt 
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   350
  in lambda depth (@{term "catch_Counterexample :: unit => term list option"} $ mk_test_term t) end
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   351
51126
df86080de4cb reform of predicate compiler / quickcheck theories:
haftmann
parents: 50046
diff changeset
   352
fun mk_unknown_term T = HOLogic.reflect_term (Const (@{const_name Quickcheck_Exhaustive.unknown}, T))
45688
d4ac5e090cd9 also potential counterexamples in the simple exhaustive testing in quickcheck
bulwahn
parents: 45684
diff changeset
   353
51126
df86080de4cb reform of predicate compiler / quickcheck theories:
haftmann
parents: 50046
diff changeset
   354
fun mk_safe_term t = @{term "Quickcheck_Random.catch_match :: term => term => term"}
45688
d4ac5e090cd9 also potential counterexamples in the simple exhaustive testing in quickcheck
bulwahn
parents: 45684
diff changeset
   355
      $ (HOLogic.mk_term_of (fastype_of t) t) $ mk_unknown_term (fastype_of t)    
d4ac5e090cd9 also potential counterexamples in the simple exhaustive testing in quickcheck
bulwahn
parents: 45684
diff changeset
   356
45724
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   357
fun mk_return t genuine = @{term "Some :: bool * term list => (bool * term list) option"} $
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   358
  (HOLogic.pair_const @{typ bool} @{typ "term list"} $ Quickcheck_Common.reflect_bool genuine $ t)
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   359
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
   360
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
   361
  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
   362
    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
   363
    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
   364
    val frees = map Free (Term.add_frees t [])
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   365
    fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
46306
bulwahn
parents: 46042
diff changeset
   366
    val ([depth_name, genuine_only_name], _) =
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   367
      Variable.variant_fixes ["depth", "genuine_only"] ctxt'
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   368
    val depth = Free (depth_name, @{typ natural})
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   369
    val genuine_only = Free (genuine_only_name, @{typ bool}) 
45724
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   370
    val return = mk_return (HOLogic.mk_list @{typ "term"}
45688
d4ac5e090cd9 also potential counterexamples in the simple exhaustive testing in quickcheck
bulwahn
parents: 45684
diff changeset
   371
        (map (fn t => HOLogic.mk_term_of (fastype_of t) t) frees @ map mk_safe_term eval_terms))
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   372
    fun mk_exhaustive_closure (free as Free (_, T)) t =
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   373
      Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T)
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   374
        $ lambda free t $ depth
45724
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   375
    val none_t = Const (@{const_name "None"}, resultT)
45763
3bb2bdf654f7 random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
bulwahn
parents: 45761
diff changeset
   376
    val mk_if = Quickcheck_Common.mk_safe_if genuine_only none_t
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   377
    fun mk_let safe def v_opt t e = mk_safe_let_expr genuine_only none_t safe (the_default def v_opt, t, e)
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   378
    val mk_test_term = mk_test_term lookup mk_exhaustive_closure mk_if mk_let none_t return ctxt
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   379
  in lambda genuine_only (lambda depth (mk_test_term t)) end
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   380
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   381
fun mk_full_generator_expr ctxt (t, eval_terms) =
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   382
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42325
diff changeset
   383
    val thy = Proof_Context.theory_of ctxt
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   384
    val ctxt' = Variable.auto_fixes t ctxt
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   385
    val names = Term.add_free_names t []
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   386
    val frees = map Free (Term.add_frees t [])
45753
196697f71488 indicating where the restart should occur; making safe_if dynamic
bulwahn
parents: 45732
diff changeset
   387
    val ([depth_name, genuine_only_name], ctxt'') =
196697f71488 indicating where the restart should occur; making safe_if dynamic
bulwahn
parents: 45732
diff changeset
   388
      Variable.variant_fixes ["depth", "genuine_only"] ctxt'
46306
bulwahn
parents: 46042
diff changeset
   389
    val (term_names, _) = Variable.variant_fixes (map (prefix "t_") names) ctxt''
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   390
    val depth = Free (depth_name, @{typ natural})
46306
bulwahn
parents: 46042
diff changeset
   391
    val genuine_only = Free (genuine_only_name, @{typ bool})    
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
   392
    val term_vars = map (fn n => Free (n, @{typ "unit => term"})) term_names
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   393
    fun lookup v = the (AList.lookup (op =) (names ~~ (frees ~~ term_vars)) v)
45724
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   394
    val return = mk_return (HOLogic.mk_list @{typ term}
1f5fc44254d7 the simple exhaustive compilation also indicates if counterexample is potentially spurious;
bulwahn
parents: 45722
diff changeset
   395
          (map (fn v => v $ @{term "()"}) term_vars @ map mk_safe_term eval_terms))
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
   396
    fun mk_exhaustive_closure (free as Free (_, T), term_var) t =
46331
f5598b604a54 generalizing check if size matters because it is different for random and exhaustive testing
bulwahn
parents: 46306
diff changeset
   397
      if Sign.of_sort thy (T, @{sort check_all}) then
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
   398
        Const (@{const_name "Quickcheck_Exhaustive.check_all_class.check_all"}, check_allT T)
45753
196697f71488 indicating where the restart should occur; making safe_if dynamic
bulwahn
parents: 45732
diff changeset
   399
          $ (HOLogic.split_const (T, @{typ "unit => term"}, resultT)
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
   400
            $ 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
   401
      else
42314
8dfb7878a351 correcting constant name in exhaustive_generators
bulwahn
parents: 42313
diff changeset
   402
        Const (@{const_name "Quickcheck_Exhaustive.full_exhaustive_class.full_exhaustive"}, full_exhaustiveT T)
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   403
          $ (HOLogic.split_const (T, @{typ "unit => term"}, resultT)
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
   404
            $ lambda free (lambda term_var t)) $ depth
45722
63b42a7db003 changing the exhaustive generator signatures;
bulwahn
parents: 45721
diff changeset
   405
    val none_t = Const (@{const_name "None"}, resultT)
45763
3bb2bdf654f7 random reporting compilation returns if counterexample is genuine or potentially spurious, and takes genuine_only option as argument
bulwahn
parents: 45761
diff changeset
   406
    val mk_if = Quickcheck_Common.mk_safe_if genuine_only none_t
48414
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   407
    fun mk_let safe _ (SOME (v, term_var)) t e =
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   408
        mk_safe_let_expr genuine_only none_t safe (v, t, 
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   409
          e #> subst_free [(term_var, absdummy @{typ unit} (mk_safe_term t))])
43875bab3a4c handling partiality in the case where the equality optimisation is applied
bulwahn
parents: 48273
diff changeset
   410
      | mk_let safe v NONE t e = mk_safe_let_expr genuine_only none_t safe (v, t, e)
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   411
    val mk_test_term = mk_test_term lookup mk_exhaustive_closure mk_if mk_let none_t return ctxt
45753
196697f71488 indicating where the restart should occur; making safe_if dynamic
bulwahn
parents: 45732
diff changeset
   412
  in lambda genuine_only (lambda depth (mk_test_term t)) 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
   413
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   414
fun mk_parametric_generator_expr mk_generator_expr =
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   415
  Quickcheck_Common.gen_mk_parametric_generator_expr 
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   416
    ((mk_generator_expr,
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   417
      absdummy @{typ bool} (absdummy @{typ natural} (Const (@{const_name "None"}, resultT)))),
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   418
      @{typ bool} --> @{typ natural} --> resultT)
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   419
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   420
fun mk_validator_expr ctxt t =
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   421
  let
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   422
    fun bounded_forallT T = (T --> @{typ bool}) --> @{typ natural} --> @{typ bool}
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   423
    val ctxt' = Variable.auto_fixes t ctxt
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   424
    val names = Term.add_free_names t []
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   425
    val frees = map Free (Term.add_frees t [])
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   426
    fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
46306
bulwahn
parents: 46042
diff changeset
   427
    val ([depth_name], _) = Variable.variant_fixes ["depth"] ctxt'
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   428
    val depth = Free (depth_name, @{typ natural})
42390
be7af468f7b3 creating generic test_term function; corrected instantiate_exhaustive_datatype; tuned
bulwahn
parents: 42361
diff changeset
   429
    fun mk_bounded_forall (Free (s, T)) t =
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   430
      Const (@{const_name "Quickcheck_Exhaustive.bounded_forall_class.bounded_forall"}, bounded_forallT T)
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   431
        $ lambda (Free (s, T)) t $ depth
45761
90028fd2f1fa exhaustive returns if a counterexample is genuine or potentially spurious in the presence of assumptions more correctly
bulwahn
parents: 45754
diff changeset
   432
    fun mk_safe_if (cond, then_t, else_t) genuine = mk_if (cond, then_t, else_t genuine)
50046
0051dc4f301f dropped dead code;
haftmann
parents: 48414
diff changeset
   433
    fun mk_let _ def v_opt t e = mk_let_expr (the_default def v_opt, t, e)
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   434
    val mk_test_term =
48013
44de84112a67 added optimisation for equational premises in Quickcheck; added some Quickcheck examples; NEWS
bulwahn
parents: 46565
diff changeset
   435
      mk_test_term lookup mk_bounded_forall mk_safe_if mk_let @{term True} (K @{term False}) ctxt
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   436
  in lambda depth (mk_test_term t) end
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   437
42391
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   438
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   439
fun mk_bounded_forall_generator_expr ctxt (t, eval_terms) = 
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   440
  let
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   441
    val frees = Term.add_free_names t []
56241
029246729dc0 more qualified names;
wenzelm
parents: 55757
diff changeset
   442
    val dummy_term = @{term "Code_Evaluation.Const (STR ''Pure.dummy_pattern'')
42391
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   443
      (Typerep.Typerep (STR ''dummy'') [])"}
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   444
    val return = @{term "Some :: term list => term list option"} $
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   445
      (HOLogic.mk_list @{typ "term"}
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   446
        (replicate (length frees + length eval_terms) dummy_term))
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   447
    val wrap = absdummy @{typ bool}
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   448
      (@{term "If :: bool => term list option => term list option => term list option"} $
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 43878
diff changeset
   449
        Bound 0 $ @{term "None :: term list option"} $ return)
42391
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   450
  in HOLogic.mk_comp (wrap, mk_validator_expr ctxt t) end
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   451
  
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
   452
(** 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
   453
46042
ab32a87ba01a comments;
wenzelm
parents: 45924
diff changeset
   454
(* FIXME just one data slot (record) per program unit *)
ab32a87ba01a comments;
wenzelm
parents: 45924
diff changeset
   455
41472
f6ab14e61604 misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
wenzelm
parents: 41178
diff changeset
   456
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
   457
(
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   458
  type T = unit -> Code_Numeral.natural -> bool -> Code_Numeral.natural -> (bool * 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
   459
  (* 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
   460
  fun init _ () = error "Counterexample"
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   461
);
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   462
val put_counterexample = Counterexample.put;
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   463
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   464
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
   465
(
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   466
  type T = unit -> (Code_Numeral.natural -> term list option) list
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   467
  (* 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
   468
  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
   469
);
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   470
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
   471
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   472
structure Validator_Batch = Proof_Data
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   473
(
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   474
  type T = unit -> (Code_Numeral.natural -> bool) list
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   475
  (* FIXME avoid user error with non-user text *)
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   476
  fun init _ () = error "Counterexample"
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   477
);
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   478
val put_validator_batch = Validator_Batch.put;
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   479
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   480
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   481
val target = "Quickcheck";
42391
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   482
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   483
fun compile_generator_expr_raw ctxt ts =
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   484
  let
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   485
    val mk_generator_expr = 
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   486
      if Config.get ctxt fast then mk_fast_generator_expr
42391
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   487
      else if Config.get ctxt bounded_forall then mk_bounded_forall_generator_expr
42306
51a08b2699d5 adding an even faster compilation scheme
bulwahn
parents: 42304
diff changeset
   488
      else if Config.get ctxt full_support then mk_full_generator_expr else mk_generator_expr
42304
34366f39d32d new compilation for exhaustive quickcheck
bulwahn
parents: 42273
diff changeset
   489
    val t' = mk_parametric_generator_expr mk_generator_expr ctxt ts;
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   490
    val compile = Code_Runtime.dynamic_value_strict
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   491
      (Counterexample.get, put_counterexample, "Exhaustive_Generators.put_counterexample")
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51673
diff changeset
   492
      ctxt (SOME target) (fn proc => fn g =>
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   493
        fn card => fn genuine_only => fn size => g card genuine_only size
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   494
          |> (Option.map o apsnd o map) proc) t' []
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   495
  in
45754
394ecd91434a dynamic genuine_flag in compilation of random and exhaustive
bulwahn
parents: 45753
diff changeset
   496
    fn genuine_only => fn [card, size] => rpair NONE (compile card genuine_only size |> 
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   497
      (if Config.get ctxt quickcheck_pretty then
45721
d1fb55c2ed65 quickcheck's compilation returns if it is genuine counterexample or a counterexample due to a match exception
bulwahn
parents: 45718
diff changeset
   498
        Option.map (apsnd (map Quickcheck_Common.post_process_term)) else I))
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42028
diff changeset
   499
  end;
42391
d7b58dc35cc5 adding bounded_forall tester
bulwahn
parents: 42390
diff changeset
   500
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   501
fun compile_generator_expr ctxt ts =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   502
  let
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   503
    val compiled = compile_generator_expr_raw ctxt ts;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   504
  in fn genuine_only => fn [card, size] =>
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   505
    compiled genuine_only [Code_Numeral.natural_of_integer card, Code_Numeral.natural_of_integer size]
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   506
  end;
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   507
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   508
fun compile_generator_exprs_raw ctxt ts =
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   509
  let
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
   510
    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
   511
    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
   512
      (Counterexample_Batch.get, put_counterexample_batch,
41918
d2ab869f8b0b replacing naming of small by exhaustive
bulwahn
parents: 41917
diff changeset
   513
        "Exhaustive_Generators.put_counterexample_batch")
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51673
diff changeset
   514
      ctxt (SOME target) (fn proc => map (fn g => g #> (Option.map o map) proc))
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   515
      (HOLogic.mk_list @{typ "natural => term list option"} ts') []
41861
77d87dc50e5a adding a function to compile a batch of terms for quickcheck with one code generation invocation
bulwahn
parents: 41472
diff changeset
   516
  in
41935
d786a8a3dc47 minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
bulwahn
parents: 41928
diff changeset
   517
    map (fn compile => fn size => compile size
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   518
      |> (Option.map o 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
   519
  end;
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   520
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   521
fun compile_generator_exprs ctxt ts =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   522
  compile_generator_exprs_raw ctxt ts
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   523
  |> map (fn f => fn size => f (Code_Numeral.natural_of_integer size));
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   524
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   525
fun compile_validator_exprs_raw ctxt ts =
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   526
  let
42273
3b94cbd903c1 correcting bounded_forall construction; tuned
bulwahn
parents: 42230
diff changeset
   527
    val ts' = map (mk_validator_expr ctxt) ts
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   528
  in
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   529
    Code_Runtime.dynamic_value_strict
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   530
      (Validator_Batch.get, put_validator_batch, "Exhaustive_Generators.put_validator_batch")
55757
9fc71814b8c1 prefer proof context over background theory
haftmann
parents: 51673
diff changeset
   531
      ctxt (SOME target) (K I) (HOLogic.mk_list @{typ "natural => bool"} ts') []
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   532
  end;
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   533
51143
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   534
fun compile_validator_exprs ctxt ts =
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   535
  compile_validator_exprs_raw ctxt ts
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   536
  |> map (fn f => fn size => f (Code_Numeral.natural_of_integer size));
0a2371e7ced3 two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents: 51126
diff changeset
   537
46331
f5598b604a54 generalizing check if size matters because it is different for random and exhaustive testing
bulwahn
parents: 46306
diff changeset
   538
fun size_matters_for thy Ts = not (forall (fn T => Sign.of_sort thy (T,  @{sort check_all})) Ts)
f5598b604a54 generalizing check if size matters because it is different for random and exhaustive testing
bulwahn
parents: 46306
diff changeset
   539
f5598b604a54 generalizing check if size matters because it is different for random and exhaustive testing
bulwahn
parents: 46306
diff changeset
   540
val test_goals =
f5598b604a54 generalizing check if size matters because it is different for random and exhaustive testing
bulwahn
parents: 46306
diff changeset
   541
  Quickcheck_Common.generator_test_goal_terms ("exhaustive", (size_matters_for, compile_generator_expr));
43875
485d2ad43528 adding random, exhaustive and SML quickcheck as testers
bulwahn
parents: 42616
diff changeset
   542
  
42308
e2abd1ca8d01 revisiting mk_equation functions and refactoring them in exhaustive quickcheck
bulwahn
parents: 42307
diff changeset
   543
(* setup *)
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   544
45484
23871e17dddb setting up exhaustive generators which are used for the smart generators
bulwahn
parents: 45420
diff changeset
   545
val setup_exhaustive_datatype_interpretation =
58191
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
   546
  Quickcheck_Common.datatype_interpretation exhaustive_plugin
58186
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
   547
    (@{sort exhaustive}, instantiate_exhaustive_datatype)
45484
23871e17dddb setting up exhaustive generators which are used for the smart generators
bulwahn
parents: 45420
diff changeset
   548
48178
0192811f0a96 exporting important function for the "many conjecture refutation" compilation of quickcheck
bulwahn
parents: 48013
diff changeset
   549
val setup_bounded_forall_datatype_interpretation =
58191
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
   550
  BNF_LFP_Compat.interpretation bounded_forall_plugin BNF_LFP_Compat.Keep_Nesting
58186
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
   551
    (Quickcheck_Common.ensure_sort
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
   552
       (((@{sort type}, @{sort type}), @{sort bounded_forall}),
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
   553
       (fn thy => BNF_LFP_Compat.the_descr thy BNF_LFP_Compat.Keep_Nesting, instantiate_bounded_forall_datatype)))
48178
0192811f0a96 exporting important function for the "many conjecture refutation" compilation of quickcheck
bulwahn
parents: 48013
diff changeset
   554
43878
eeb10fdd9535 changed every tester to have a configuration in quickcheck; enabling parallel testing of different testers in quickcheck
bulwahn
parents: 43877
diff changeset
   555
val active = Attrib.setup_config_bool @{binding quickcheck_exhaustive_active} (K true);
eeb10fdd9535 changed every tester to have a configuration in quickcheck; enabling parallel testing of different testers in quickcheck
bulwahn
parents: 43877
diff changeset
   556
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   557
val setup =
58191
b3c71d630777 pretend code generation is a ctr_sugar plugin
blanchet
parents: 58186
diff changeset
   558
  Quickcheck_Common.datatype_interpretation full_exhaustive_plugin
58186
a6c3962ea907 named interpretations
blanchet
parents: 58145
diff changeset
   559
    (@{sort full_exhaustive}, instantiate_full_exhaustive_datatype)
43878
eeb10fdd9535 changed every tester to have a configuration in quickcheck; enabling parallel testing of different testers in quickcheck
bulwahn
parents: 43877
diff changeset
   560
  #> Context.theory_map (Quickcheck.add_tester ("exhaustive", (active, test_goals)))
42195
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   561
  #> Context.theory_map (Quickcheck.add_batch_generator ("exhaustive", compile_generator_exprs))
1e7b62c93f5d adding an exhaustive validator for quickcheck's batch validating; moving strip_imp; minimal setup for bounded_forall
bulwahn
parents: 42176
diff changeset
   562
  #> Context.theory_map (Quickcheck.add_batch_validator ("exhaustive", compile_validator_exprs));
40420
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   563
552563ea3304 adding code and theory for smallvalue generators, but do not setup the interpretation yet
bulwahn
parents:
diff changeset
   564
end;