src/HOL/Tools/Quickcheck/exhaustive_generators.ML
author bulwahn
Fri Apr 08 16:31:14 2011 +0200 (2011-04-08)
changeset 42316 12635bb655fd
parent 42315 95dfa082065a
child 42325 104472645443
permissions -rw-r--r--
deactivating other compilations in quickcheck_exhaustive momentarily that only interesting for my benchmarks and experiments
bulwahn@41923
     1
(*  Title:      HOL/Tools/Quickcheck/exhaustive_generators.ML
bulwahn@40420
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@40420
     3
bulwahn@41918
     4
Exhaustive generators for various types.
bulwahn@40420
     5
*)
bulwahn@40420
     6
bulwahn@41918
     7
signature EXHAUSTIVE_GENERATORS =
bulwahn@40420
     8
sig
bulwahn@40420
     9
  val compile_generator_expr:
bulwahn@42159
    10
    Proof.context -> (term * term list) list -> int list -> term list option * Quickcheck.report option
bulwahn@42159
    11
  val compile_generator_exprs: Proof.context -> term list -> (int -> term list option) list
bulwahn@42195
    12
  val compile_validator_exprs: Proof.context -> term list -> (int -> bool) list
bulwahn@42159
    13
  val put_counterexample: (unit -> int -> int -> term list option)
bulwahn@40420
    14
    -> Proof.context -> Proof.context
bulwahn@41861
    15
  val put_counterexample_batch: (unit -> (int -> term list option) list)
bulwahn@41861
    16
    -> Proof.context -> Proof.context
bulwahn@42195
    17
  val put_validator_batch: (unit -> (int -> bool) list) -> Proof.context -> Proof.context
bulwahn@42306
    18
  exception Counterexample of term list
bulwahn@42195
    19
  val smart_quantifier : bool Config.T
bulwahn@42195
    20
  val quickcheck_pretty : bool Config.T
bulwahn@40420
    21
  val setup: theory -> theory
bulwahn@40420
    22
end;
bulwahn@40420
    23
bulwahn@41918
    24
structure Exhaustive_Generators : EXHAUSTIVE_GENERATORS =
bulwahn@40420
    25
struct
bulwahn@40420
    26
bulwahn@42308
    27
(* basics *)
bulwahn@42308
    28
bulwahn@42308
    29
(** dynamic options **)
bulwahn@40907
    30
bulwahn@40907
    31
val (smart_quantifier, setup_smart_quantifier) =
bulwahn@40907
    32
  Attrib.config_bool "quickcheck_smart_quantifier" (K true)
bulwahn@40907
    33
bulwahn@42306
    34
val (fast, setup_fast) =
bulwahn@42313
    35
  Attrib.config_bool "quickcheck_fast" (K false)
bulwahn@42306
    36
  
bulwahn@42304
    37
val (full_support, setup_full_support) =
bulwahn@42304
    38
  Attrib.config_bool "quickcheck_full_support" (K true)
bulwahn@42304
    39
bulwahn@41903
    40
val (quickcheck_pretty, setup_quickcheck_pretty) =
bulwahn@41903
    41
  Attrib.config_bool "quickcheck_pretty" (K true)
bulwahn@41903
    42
 
bulwahn@40420
    43
(** general term functions **)
bulwahn@40420
    44
bulwahn@40420
    45
fun mk_measure f =
bulwahn@40420
    46
  let
bulwahn@40420
    47
    val Type ("fun", [T, @{typ nat}]) = fastype_of f 
bulwahn@40420
    48
  in
bulwahn@40420
    49
    Const (@{const_name Wellfounded.measure},
bulwahn@40420
    50
      (T --> @{typ nat}) --> HOLogic.mk_prodT (T, T) --> @{typ bool})
bulwahn@40420
    51
    $ f
bulwahn@40420
    52
  end
bulwahn@40420
    53
bulwahn@40420
    54
fun mk_sumcases rT f (Type (@{type_name Sum_Type.sum}, [TL, TR])) =
bulwahn@40420
    55
  let
bulwahn@40420
    56
    val lt = mk_sumcases rT f TL
bulwahn@40420
    57
    val rt = mk_sumcases rT f TR
bulwahn@40420
    58
  in
bulwahn@40420
    59
    SumTree.mk_sumcase TL TR rT lt rt
bulwahn@40420
    60
  end
bulwahn@40420
    61
  | mk_sumcases _ f T = f T
bulwahn@40420
    62
bulwahn@40420
    63
(** abstract syntax **)
bulwahn@40420
    64
bulwahn@40639
    65
fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => Code_Evaluation.term"});
bulwahn@40639
    66
bulwahn@40420
    67
val size = @{term "i :: code_numeral"}
bulwahn@40420
    68
val size_pred = @{term "(i :: code_numeral) - 1"}
bulwahn@40420
    69
val size_ge_zero = @{term "(i :: code_numeral) > 0"}
bulwahn@42304
    70
bulwahn@40420
    71
fun mk_none_continuation (x, y) =
bulwahn@40420
    72
  let
bulwahn@40420
    73
    val (T as Type(@{type_name "option"}, [T'])) = fastype_of x
bulwahn@40420
    74
  in
bulwahn@42214
    75
    Const (@{const_name "Quickcheck_Exhaustive.orelse"}, T --> T --> T) $ x $ y
bulwahn@40420
    76
  end
bulwahn@40420
    77
bulwahn@42306
    78
fun mk_unit_let (x, y) =
bulwahn@42306
    79
  Const (@{const_name "Let"}, @{typ "unit => (unit => unit) => unit"}) $ x $ (absdummy (@{typ unit}, y))
bulwahn@42306
    80
  
bulwahn@42308
    81
(* handling inductive datatypes *)
bulwahn@40420
    82
bulwahn@42308
    83
(** constructing generator instances **)
bulwahn@40420
    84
bulwahn@40420
    85
exception FUNCTION_TYPE;
bulwahn@42306
    86
bulwahn@42306
    87
exception Counterexample of term list
bulwahn@42306
    88
bulwahn@41918
    89
val exhaustiveN = "exhaustive";
bulwahn@42304
    90
val full_exhaustiveN = "full_exhaustive";
bulwahn@42306
    91
val fast_exhaustiveN = "fast_exhaustive";
bulwahn@42308
    92
val bounded_forallN = "bounded_forall";
bulwahn@42306
    93
bulwahn@42306
    94
fun fast_exhaustiveT T = (T --> @{typ unit})
bulwahn@42306
    95
  --> @{typ code_numeral} --> @{typ unit}
bulwahn@42306
    96
bulwahn@42304
    97
fun exhaustiveT T = (T --> @{typ "Code_Evaluation.term list option"})
bulwahn@42304
    98
  --> @{typ code_numeral} --> @{typ "Code_Evaluation.term list option"}
bulwahn@42304
    99
bulwahn@42308
   100
fun bounded_forallT T = (T --> @{typ bool}) --> @{typ code_numeral} --> @{typ bool}
bulwahn@42308
   101
bulwahn@42304
   102
fun full_exhaustiveT T = (termifyT T --> @{typ "Code_Evaluation.term list option"})
bulwahn@40639
   103
  --> @{typ code_numeral} --> @{typ "Code_Evaluation.term list option"}
bulwahn@41085
   104
bulwahn@41085
   105
fun check_allT T = (termifyT T --> @{typ "Code_Evaluation.term list option"})
bulwahn@41085
   106
  --> @{typ "Code_Evaluation.term list option"}
bulwahn@41085
   107
bulwahn@42307
   108
fun mk_equation_terms generics (descr, vs, Ts) =
bulwahn@42307
   109
  let
bulwahn@42307
   110
    val (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, exhaustives) = generics
bulwahn@42307
   111
    val rhss =
bulwahn@42307
   112
      Datatype_Aux.interpret_construction descr vs
bulwahn@42307
   113
        { atyp = mk_call, dtyp = mk_aux_call }
bulwahn@42307
   114
      |> (map o apfst) Type
bulwahn@42307
   115
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
bulwahn@42307
   116
      |> map mk_rhs
bulwahn@42307
   117
    val lhss = map2 (fn t => fn T => t $ test_function T $ size) exhaustives Ts
bulwahn@42307
   118
  in
bulwahn@42307
   119
    map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
bulwahn@42307
   120
  end
bulwahn@42306
   121
bulwahn@42308
   122
fun gen_mk_call c T =  (T, fn t => c T $ absdummy (T, t) $ size_pred)
bulwahn@42308
   123
bulwahn@42308
   124
fun gen_mk_aux_call functerms fTs (k, _) (tyco, Ts) =
bulwahn@42308
   125
  let
bulwahn@42308
   126
    val T = Type (tyco, Ts)
bulwahn@42308
   127
    val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
bulwahn@42308
   128
  in
bulwahn@42308
   129
   (T, fn t => nth functerms k $ absdummy (T, t) $ size_pred)
bulwahn@42308
   130
  end
bulwahn@42308
   131
bulwahn@42308
   132
fun gen_mk_consexpr test_function functerms simpleT (c, xs) =
bulwahn@42308
   133
  let
bulwahn@42308
   134
    val (Ts, fns) = split_list xs
bulwahn@42308
   135
    val constr = Const (c, Ts ---> simpleT)
bulwahn@42308
   136
    val bounds = map Bound (((length xs) - 1) downto 0)
bulwahn@42308
   137
    val term_bounds = map (fn x => Bound (2 * x)) (((length xs) - 1) downto 0)
bulwahn@42308
   138
    val start_term = test_function simpleT $ list_comb (constr, bounds)
bulwahn@42308
   139
  in fold_rev (fn f => fn t => f t) fns start_term end
bulwahn@42308
   140
      
bulwahn@42307
   141
fun mk_fast_equations functerms =
bulwahn@42306
   142
  let
bulwahn@42307
   143
    fun test_function T = Free ("f", T --> @{typ "unit"})
bulwahn@42308
   144
    val mk_call = gen_mk_call (fn T =>
bulwahn@42308
   145
      Const (@{const_name "Quickcheck_Exhaustive.fast_exhaustive_class.fast_exhaustive"},
bulwahn@42308
   146
        fast_exhaustiveT T))
bulwahn@42308
   147
    val mk_aux_call = gen_mk_aux_call functerms
bulwahn@42308
   148
    val mk_consexpr = gen_mk_consexpr test_function functerms
bulwahn@42307
   149
    fun mk_rhs exprs = @{term "If :: bool => unit => unit => unit"}
bulwahn@42307
   150
        $ size_ge_zero $ (foldr1 mk_unit_let exprs) $ @{term "()"}
bulwahn@42306
   151
  in
bulwahn@42307
   152
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
bulwahn@42306
   153
  end
bulwahn@42307
   154
bulwahn@42307
   155
fun mk_equations functerms =
bulwahn@40420
   156
  let
bulwahn@42307
   157
    fun test_function T = Free ("f", T --> @{typ "term list option"})
bulwahn@42308
   158
    val mk_call = gen_mk_call (fn T =>
bulwahn@42308
   159
      Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T))
bulwahn@42308
   160
    val mk_aux_call = gen_mk_aux_call functerms
bulwahn@42308
   161
    val mk_consexpr = gen_mk_consexpr test_function functerms
bulwahn@42304
   162
    fun mk_rhs exprs =
bulwahn@42312
   163
      @{term "If :: bool => term list option => term list option => term list option"}
bulwahn@42312
   164
          $ size_ge_zero $ (foldr1 mk_none_continuation exprs) $ @{term "None :: term list option"}
bulwahn@42304
   165
  in
bulwahn@42307
   166
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
bulwahn@42304
   167
  end
bulwahn@42308
   168
bulwahn@42308
   169
fun mk_bounded_forall_equations functerms =
bulwahn@42308
   170
  let
bulwahn@42308
   171
    fun test_function T = Free ("P", T --> @{typ bool})
bulwahn@42308
   172
    val mk_call = gen_mk_call (fn T =>
bulwahn@42308
   173
      Const (@{const_name "Quickcheck_Exhaustive.bounded_forall_class.bounded_forall"},
bulwahn@42308
   174
        bounded_forallT T))
bulwahn@42308
   175
    val mk_aux_call = gen_mk_aux_call functerms
bulwahn@42308
   176
    val mk_consexpr = gen_mk_consexpr test_function functerms
bulwahn@42308
   177
    fun mk_rhs exprs =
bulwahn@42308
   178
      @{term "If :: bool => bool => bool => bool"} $ size_ge_zero $
bulwahn@42308
   179
        (foldr1 HOLogic.mk_conj exprs) $ @{term "True"}
bulwahn@42308
   180
  in
bulwahn@42308
   181
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
bulwahn@42308
   182
  end
bulwahn@42308
   183
  
bulwahn@42307
   184
fun mk_full_equations functerms =
bulwahn@42304
   185
  let
bulwahn@42307
   186
    fun test_function T = Free ("f", termifyT T --> @{typ "term list option"})
bulwahn@42304
   187
    fun mk_call T =
bulwahn@42304
   188
      let
bulwahn@42307
   189
        val full_exhaustive =
bulwahn@42310
   190
          Const (@{const_name "Quickcheck_Exhaustive.full_exhaustive_class.full_exhaustive"},
bulwahn@42308
   191
            full_exhaustiveT T)
bulwahn@42304
   192
      in
bulwahn@42304
   193
        (T, (fn t => full_exhaustive $
bulwahn@40639
   194
          (HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, @{typ "Code_Evaluation.term list option"})
bulwahn@40639
   195
          $ absdummy (T, absdummy (@{typ "unit => Code_Evaluation.term"}, t))) $ size_pred))
bulwahn@40420
   196
      end
bulwahn@41918
   197
    fun mk_aux_call fTs (k, _) (tyco, Ts) =
bulwahn@40420
   198
      let
bulwahn@40420
   199
        val T = Type (tyco, Ts)
bulwahn@40420
   200
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
bulwahn@40420
   201
      in
bulwahn@42307
   202
        (T, (fn t => nth functerms k $
bulwahn@40639
   203
          (HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, @{typ "Code_Evaluation.term list option"})
bulwahn@41918
   204
            $ absdummy (T, absdummy (@{typ "unit => Code_Evaluation.term"}, t))) $ size_pred))
bulwahn@40420
   205
      end
bulwahn@40420
   206
    fun mk_consexpr simpleT (c, xs) =
bulwahn@40420
   207
      let
bulwahn@40420
   208
        val (Ts, fns) = split_list xs
bulwahn@40420
   209
        val constr = Const (c, Ts ---> simpleT)
bulwahn@40639
   210
        val bounds = map (fn x => Bound (2 * x + 1)) (((length xs) - 1) downto 0)
bulwahn@40639
   211
        val term_bounds = map (fn x => Bound (2 * x)) (((length xs) - 1) downto 0)
bulwahn@40639
   212
        val Eval_App = Const ("Code_Evaluation.App", HOLogic.termT --> HOLogic.termT --> HOLogic.termT)
bulwahn@40639
   213
        val Eval_Const = Const ("Code_Evaluation.Const", HOLogic.literalT --> @{typ typerep} --> HOLogic.termT)
bulwahn@40639
   214
        val term = fold (fn u => fn t => Eval_App $ t $ (u $ @{term "()"}))
bulwahn@40639
   215
          bounds (Eval_Const $ HOLogic.mk_literal c $ HOLogic.mk_typerep (Ts ---> simpleT))
bulwahn@42307
   216
        val start_term = test_function simpleT $ 
bulwahn@40639
   217
        (HOLogic.pair_const simpleT @{typ "unit => Code_Evaluation.term"}
bulwahn@40639
   218
          $ (list_comb (constr, bounds)) $ absdummy (@{typ unit}, term))
bulwahn@40420
   219
      in fold_rev (fn f => fn t => f t) fns start_term end
bulwahn@40420
   220
    fun mk_rhs exprs =
bulwahn@40420
   221
        @{term "If :: bool => term list option => term list option => term list option"}
bulwahn@40420
   222
            $ size_ge_zero $ (foldr1 mk_none_continuation exprs) $ @{term "None :: term list option"}
bulwahn@40420
   223
  in
bulwahn@42307
   224
    mk_equation_terms (mk_call, mk_aux_call, mk_consexpr, mk_rhs, test_function, functerms)
bulwahn@40901
   225
  end
bulwahn@42307
   226
  
bulwahn@42308
   227
(** foundational definition with the function package **)
bulwahn@40901
   228
bulwahn@40901
   229
val less_int_pred = @{lemma "i > 0 ==> Code_Numeral.nat_of ((i :: code_numeral) - 1) < Code_Numeral.nat_of i" by auto}
bulwahn@40901
   230
bulwahn@40901
   231
fun mk_single_measure T = HOLogic.mk_comp (@{term "Code_Numeral.nat_of"},
bulwahn@40901
   232
    Const (@{const_name "Product_Type.snd"}, T --> @{typ "code_numeral"}))
bulwahn@40901
   233
bulwahn@40901
   234
fun mk_termination_measure T =
bulwahn@40901
   235
  let
bulwahn@40901
   236
    val T' = fst (HOLogic.dest_prodT (HOLogic.dest_setT T))
bulwahn@40901
   237
  in
bulwahn@40901
   238
    mk_measure (mk_sumcases @{typ nat} mk_single_measure T')
bulwahn@40420
   239
  end
bulwahn@40901
   240
bulwahn@40901
   241
fun termination_tac ctxt = 
bulwahn@40901
   242
  Function_Relation.relation_tac ctxt mk_termination_measure 1
bulwahn@40901
   243
  THEN rtac @{thm wf_measure} 1
bulwahn@40901
   244
  THEN (REPEAT_DETERM (Simplifier.asm_full_simp_tac 
bulwahn@40901
   245
    (HOL_basic_ss addsimps [@{thm in_measure}, @{thm o_def}, @{thm snd_conv},
bulwahn@40901
   246
     @{thm nat_mono_iff}, less_int_pred] @ @{thms sum.cases}) 1))
bulwahn@40901
   247
bulwahn@42308
   248
(** instantiating generator classes **)
bulwahn@42315
   249
  
bulwahn@42312
   250
val contains_recursive_type_under_function_types =
bulwahn@42312
   251
  exists (fn (_, (_, _, cs)) => cs |> exists (snd #> exists (fn dT =>
bulwahn@42312
   252
    (case Datatype_Aux.strip_dtyp dT of (_ :: _, Datatype.DtRec _) => true | _ => false))))
bulwahn@42312
   253
    
bulwahn@42315
   254
fun instantiate_datatype (name, constprfx, sort, mk_equations, mk_T, argnames)
bulwahn@42315
   255
    config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
bulwahn@42312
   256
  if not (contains_recursive_type_under_function_types descr) then
bulwahn@42312
   257
    let
bulwahn@42315
   258
      val _ = Datatype_Aux.message config ("Creating " ^ name ^ "...")
bulwahn@42315
   259
      val fullnames = map (prefix (constprfx ^ "_")) (names @ auxnames)
bulwahn@42312
   260
    in
bulwahn@42312
   261
      thy
bulwahn@42315
   262
      |> Class.instantiation (tycos, vs, sort)
bulwahn@42312
   263
      |> Quickcheck_Common.define_functions
bulwahn@42315
   264
          (fn functerms => mk_equations functerms (descr, vs, Ts @ Us), NONE)
bulwahn@42315
   265
          prfx argnames fullnames (map mk_T (Ts @ Us))
bulwahn@42312
   266
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bulwahn@42312
   267
    end
bulwahn@42312
   268
  else
bulwahn@42230
   269
    (Datatype_Aux.message config
bulwahn@42315
   270
      ("Creation of " ^ name ^ " failed because the datatype is recursive under a function type");
bulwahn@42230
   271
    thy)
bulwahn@42315
   272
bulwahn@42315
   273
val instantiate_bounded_forall_datatype = instantiate_datatype
bulwahn@42315
   274
 ("bounded universal quantifiers", bounded_forallN, @{sort bounded_forall},
bulwahn@42315
   275
   mk_bounded_forall_equations, bounded_forallT, ["P", "i"]);
bulwahn@42315
   276
bulwahn@42315
   277
val instantiate_fast_exhaustive_datatype = instantiate_datatype 
bulwahn@42315
   278
 ("fast exhaustive generators", fast_exhaustiveN, @{sort fast_exhaustive},
bulwahn@42315
   279
   mk_fast_equations, fast_exhaustiveT, ["f", "i"])
bulwahn@42315
   280
bulwahn@42315
   281
val instantiate_exhaustive_datatype = instantiate_datatype  
bulwahn@42315
   282
  ("exhaustive generators", exhaustiveN, @{sort full_exhaustive}, mk_equations, exhaustiveT, ["f", "i"])
bulwahn@42315
   283
bulwahn@42315
   284
val instantiate_full_exhaustive_datatype = instantiate_datatype
bulwahn@42315
   285
  ("full exhaustive generators", full_exhaustiveN, @{sort full_exhaustive},
bulwahn@42315
   286
  mk_full_equations, full_exhaustiveT, ["f", "i"])
bulwahn@42315
   287
  
bulwahn@42308
   288
(* building and compiling generator expressions *)
bulwahn@40420
   289
bulwahn@42306
   290
fun mk_fast_generator_expr ctxt (t, eval_terms) =
bulwahn@42306
   291
  let
bulwahn@42306
   292
    val thy = ProofContext.theory_of ctxt
bulwahn@42306
   293
    val ctxt' = Variable.auto_fixes t ctxt
bulwahn@42306
   294
    val names = Term.add_free_names t []
bulwahn@42306
   295
    val frees = map Free (Term.add_frees t [])
bulwahn@42306
   296
    val ([depth_name], ctxt'') = Variable.variant_fixes ["depth"] ctxt'
bulwahn@42306
   297
    val depth = Free (depth_name, @{typ code_numeral})
bulwahn@42306
   298
    val return = @{term "throw_Counterexample :: term list => unit"} $
bulwahn@42306
   299
      (HOLogic.mk_list @{typ "term"}
bulwahn@42306
   300
        (map (fn t => HOLogic.mk_term_of (fastype_of t) t) (frees @ eval_terms)))
bulwahn@42306
   301
    fun mk_exhaustive_closure (free as Free (_, T)) t =
bulwahn@42306
   302
      Const (@{const_name "Quickcheck_Exhaustive.fast_exhaustive_class.fast_exhaustive"}, fast_exhaustiveT T)
bulwahn@42306
   303
        $ lambda free t $ depth
bulwahn@42306
   304
    val none_t = @{term "()"}
bulwahn@42306
   305
    fun mk_safe_if (cond, then_t, else_t) =
bulwahn@42306
   306
      @{term "If :: bool => unit => unit => unit"} $ cond $ then_t $ else_t
bulwahn@42306
   307
    fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
bulwahn@42306
   308
    fun mk_naive_test_term t =
bulwahn@42306
   309
      fold_rev mk_exhaustive_closure frees (mk_safe_if (t, none_t, return)) 
bulwahn@42306
   310
    fun mk_smart_test_term' concl bound_vars assms =
bulwahn@42306
   311
      let
bulwahn@42306
   312
        fun vars_of t = subtract (op =) bound_vars (Term.add_free_names t [])
bulwahn@42306
   313
        val (vars, check) =
bulwahn@42306
   314
          case assms of [] => (vars_of concl, (concl, none_t, return))
bulwahn@42306
   315
            | assm :: assms => (vars_of assm, (assm,
bulwahn@42306
   316
                mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms, none_t))
bulwahn@42306
   317
      in
bulwahn@42306
   318
        fold_rev mk_exhaustive_closure (map lookup vars) (mk_safe_if check)
bulwahn@42306
   319
      end
bulwahn@42306
   320
    fun mk_smart_test_term t =
bulwahn@42306
   321
      let
bulwahn@42306
   322
        val (assms, concl) = Quickcheck_Common.strip_imp t
bulwahn@42306
   323
      in
bulwahn@42306
   324
        mk_smart_test_term' concl [] assms
bulwahn@42306
   325
      end
bulwahn@42306
   326
    val mk_test_term =
bulwahn@42306
   327
      if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term 
bulwahn@42306
   328
  in lambda depth (@{term "catch_Counterexample :: unit => term list option"} $ mk_test_term t) end
bulwahn@42306
   329
bulwahn@42028
   330
fun mk_generator_expr ctxt (t, eval_terms) =
bulwahn@42028
   331
  let
bulwahn@42028
   332
    val thy = ProofContext.theory_of ctxt
bulwahn@42028
   333
    val ctxt' = Variable.auto_fixes t ctxt
bulwahn@42028
   334
    val names = Term.add_free_names t []
bulwahn@42028
   335
    val frees = map Free (Term.add_frees t [])
bulwahn@42028
   336
    val ([depth_name], ctxt'') = Variable.variant_fixes ["depth"] ctxt'
bulwahn@42304
   337
    val depth = Free (depth_name, @{typ code_numeral})
bulwahn@42304
   338
    val return = @{term "Some :: term list => term list option"} $
bulwahn@42304
   339
      (HOLogic.mk_list @{typ "term"}
bulwahn@42304
   340
        (map (fn t => HOLogic.mk_term_of (fastype_of t) t) (frees @ eval_terms)))
bulwahn@42304
   341
    fun mk_exhaustive_closure (free as Free (_, T)) t =
bulwahn@42304
   342
      Const (@{const_name "Quickcheck_Exhaustive.exhaustive_class.exhaustive"}, exhaustiveT T)
bulwahn@42304
   343
        $ lambda free t $ depth
bulwahn@42304
   344
    val none_t = @{term "None :: term list option"}
bulwahn@42304
   345
    fun mk_safe_if (cond, then_t, else_t) =
bulwahn@42304
   346
      @{term "Quickcheck_Exhaustive.catch_match :: term list option => term list option => term list option"} $
bulwahn@42304
   347
        (@{term "If :: bool => term list option => term list option => term list option"}
bulwahn@42304
   348
        $ cond $ then_t $ else_t) $ none_t;
bulwahn@42304
   349
    fun lookup v = the (AList.lookup (op =) (names ~~ frees) v)
bulwahn@42304
   350
    fun mk_naive_test_term t =
bulwahn@42304
   351
      fold_rev mk_exhaustive_closure frees (mk_safe_if (t, none_t, return)) 
bulwahn@42304
   352
    fun mk_smart_test_term' concl bound_vars assms =
bulwahn@42304
   353
      let
bulwahn@42304
   354
        fun vars_of t = subtract (op =) bound_vars (Term.add_free_names t [])
bulwahn@42304
   355
        val (vars, check) =
bulwahn@42304
   356
          case assms of [] => (vars_of concl, (concl, none_t, return))
bulwahn@42304
   357
            | assm :: assms => (vars_of assm, (assm,
bulwahn@42304
   358
                mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms, none_t))
bulwahn@42304
   359
      in
bulwahn@42304
   360
        fold_rev mk_exhaustive_closure (map lookup vars) (mk_safe_if check)
bulwahn@42304
   361
      end
bulwahn@42304
   362
    fun mk_smart_test_term t =
bulwahn@42304
   363
      let
bulwahn@42304
   364
        val (assms, concl) = Quickcheck_Common.strip_imp t
bulwahn@42304
   365
      in
bulwahn@42304
   366
        mk_smart_test_term' concl [] assms
bulwahn@42304
   367
      end
bulwahn@42304
   368
    val mk_test_term =
bulwahn@42304
   369
      if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term 
bulwahn@42304
   370
  in lambda depth (mk_test_term t) end
bulwahn@42304
   371
bulwahn@42304
   372
fun mk_full_generator_expr ctxt (t, eval_terms) =
bulwahn@42304
   373
  let
bulwahn@42304
   374
    val thy = ProofContext.theory_of ctxt
bulwahn@42304
   375
    val ctxt' = Variable.auto_fixes t ctxt
bulwahn@42304
   376
    val names = Term.add_free_names t []
bulwahn@42304
   377
    val frees = map Free (Term.add_frees t [])
bulwahn@42304
   378
    val ([depth_name], ctxt'') = Variable.variant_fixes ["depth"] ctxt'
bulwahn@42028
   379
    val (term_names, ctxt''') = Variable.variant_fixes (map (prefix "t_") names) ctxt''
bulwahn@42028
   380
    val depth = Free (depth_name, @{typ code_numeral})
bulwahn@42028
   381
    val term_vars = map (fn n => Free (n, @{typ "unit => term"})) term_names
bulwahn@42028
   382
    val terms = HOLogic.mk_list @{typ term} (map (fn v => v $ @{term "()"}) term_vars)
bulwahn@42028
   383
    val appendC = @{term "List.append :: term list => term list => term list"}  
bulwahn@42028
   384
    val return = @{term "Some :: term list => term list option"} $ (appendC $ terms $
bulwahn@42028
   385
      (HOLogic.mk_list @{typ "term"} (map (fn t => HOLogic.mk_term_of (fastype_of t) t) eval_terms)))
bulwahn@42028
   386
    fun mk_exhaustive_closure (free as Free (_, T), term_var) t =
bulwahn@42028
   387
      if Sign.of_sort thy (T, @{sort enum}) then
bulwahn@42028
   388
        Const (@{const_name "Quickcheck_Exhaustive.check_all_class.check_all"}, check_allT T)
bulwahn@42028
   389
          $ (HOLogic.split_const (T, @{typ "unit => term"}, @{typ "term list option"}) 
bulwahn@42028
   390
            $ lambda free (lambda term_var t))
bulwahn@42028
   391
      else
bulwahn@42314
   392
        Const (@{const_name "Quickcheck_Exhaustive.full_exhaustive_class.full_exhaustive"}, full_exhaustiveT T)
bulwahn@42028
   393
          $ (HOLogic.split_const (T, @{typ "unit => term"}, @{typ "term list option"})
bulwahn@42028
   394
            $ lambda free (lambda term_var t)) $ depth
bulwahn@42028
   395
    val none_t = @{term "None :: term list option"}
bulwahn@42028
   396
    fun mk_safe_if (cond, then_t, else_t) =
bulwahn@42028
   397
      @{term "Quickcheck_Exhaustive.catch_match :: term list option => term list option => term list option"} $
bulwahn@42028
   398
        (@{term "If :: bool => term list option => term list option => term list option"}
bulwahn@42028
   399
        $ cond $ then_t $ else_t) $ none_t;
bulwahn@42028
   400
    fun lookup v = the (AList.lookup (op =) (names ~~ (frees ~~ term_vars)) v)
bulwahn@42028
   401
    fun mk_naive_test_term t =
bulwahn@42028
   402
      fold_rev mk_exhaustive_closure (frees ~~ term_vars) (mk_safe_if (t, none_t, return)) 
bulwahn@42028
   403
    fun mk_smart_test_term' concl bound_vars assms =
bulwahn@42028
   404
      let
bulwahn@42028
   405
        fun vars_of t = subtract (op =) bound_vars (Term.add_free_names t [])
bulwahn@42028
   406
        val (vars, check) =
bulwahn@42028
   407
          case assms of [] => (vars_of concl, (concl, none_t, return))
bulwahn@42028
   408
            | assm :: assms => (vars_of assm, (assm,
bulwahn@42028
   409
                mk_smart_test_term' concl (union (op =) (vars_of assm) bound_vars) assms, none_t))
bulwahn@42028
   410
      in
bulwahn@42028
   411
        fold_rev mk_exhaustive_closure (map lookup vars) (mk_safe_if check)
bulwahn@42028
   412
      end
bulwahn@42028
   413
    fun mk_smart_test_term t =
bulwahn@42028
   414
      let
bulwahn@42195
   415
        val (assms, concl) = Quickcheck_Common.strip_imp t
bulwahn@42028
   416
      in
bulwahn@42028
   417
        mk_smart_test_term' concl [] assms
bulwahn@42028
   418
      end
bulwahn@42028
   419
    val mk_test_term =
bulwahn@42028
   420
      if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term 
bulwahn@42028
   421
  in lambda depth (mk_test_term t) end
bulwahn@42028
   422
bulwahn@42304
   423
fun mk_parametric_generator_expr mk_generator_expr =
bulwahn@42159
   424
  Quickcheck_Common.gen_mk_parametric_generator_expr 
bulwahn@42159
   425
    ((mk_generator_expr, absdummy (@{typ "code_numeral"}, @{term "None :: term list option"})),
bulwahn@42159
   426
      @{typ "code_numeral => term list option"})
bulwahn@42195
   427
bulwahn@42195
   428
fun mk_validator_expr ctxt t =
bulwahn@42195
   429
  let
bulwahn@42195
   430
    fun bounded_forallT T = (T --> @{typ bool}) --> @{typ code_numeral} --> @{typ bool}
bulwahn@42195
   431
    val thy = ProofContext.theory_of ctxt
bulwahn@42195
   432
    val ctxt' = Variable.auto_fixes t ctxt
bulwahn@42195
   433
    val ([depth_name], ctxt'') = Variable.variant_fixes ["depth"] ctxt'
bulwahn@42195
   434
    val depth = Free (depth_name, @{typ code_numeral})
bulwahn@42195
   435
    fun mk_bounded_forall (s, T) t =
bulwahn@42195
   436
      Const (@{const_name "Quickcheck_Exhaustive.bounded_forall_class.bounded_forall"}, bounded_forallT T)
bulwahn@42195
   437
        $ lambda (Free (s, T)) t $ depth
bulwahn@42195
   438
    fun mk_implies (cond, then_t) =
bulwahn@42273
   439
      @{term "If :: bool => bool => bool => bool"} $ cond $ then_t $ @{term True}
bulwahn@42195
   440
    fun mk_naive_test_term t = fold_rev mk_bounded_forall (Term.add_frees t []) t 
bulwahn@42195
   441
    fun mk_smart_test_term' concl bound_frees assms =
bulwahn@42195
   442
      let
bulwahn@42195
   443
        fun vars_of t = subtract (op =) bound_frees (Term.add_frees t [])
bulwahn@42195
   444
        val (vars, check) =
bulwahn@42195
   445
          case assms of [] => (vars_of concl, concl)
bulwahn@42195
   446
            | assm :: assms => (vars_of assm, mk_implies (assm,
bulwahn@42195
   447
                mk_smart_test_term' concl (union (op =) (vars_of assm) bound_frees) assms))
bulwahn@42195
   448
      in
bulwahn@42195
   449
        fold_rev mk_bounded_forall vars check
bulwahn@42195
   450
      end
bulwahn@42195
   451
    fun mk_smart_test_term t =
bulwahn@42195
   452
      let
bulwahn@42195
   453
        val (assms, concl) = Quickcheck_Common.strip_imp t
bulwahn@42195
   454
      in
bulwahn@42195
   455
        mk_smart_test_term' concl [] assms
bulwahn@42195
   456
      end
bulwahn@42195
   457
    val mk_test_term =
bulwahn@42195
   458
      if Config.get ctxt smart_quantifier then mk_smart_test_term else mk_naive_test_term 
bulwahn@42195
   459
  in lambda depth (mk_test_term t) end
bulwahn@42195
   460
bulwahn@42195
   461
bulwahn@42028
   462
(** generator compiliation **)
bulwahn@42028
   463
wenzelm@41472
   464
structure Counterexample = Proof_Data
wenzelm@41472
   465
(
bulwahn@42159
   466
  type T = unit -> int -> int -> term list option
wenzelm@41472
   467
  (* FIXME avoid user error with non-user text *)
bulwahn@40420
   468
  fun init _ () = error "Counterexample"
bulwahn@40420
   469
);
bulwahn@40420
   470
val put_counterexample = Counterexample.put;
bulwahn@40420
   471
bulwahn@41861
   472
structure Counterexample_Batch = Proof_Data
bulwahn@41861
   473
(
bulwahn@41861
   474
  type T = unit -> (int -> term list option) list
bulwahn@41861
   475
  (* FIXME avoid user error with non-user text *)
bulwahn@41861
   476
  fun init _ () = error "Counterexample"
bulwahn@41861
   477
);
bulwahn@41861
   478
val put_counterexample_batch = Counterexample_Batch.put;
bulwahn@41861
   479
bulwahn@42195
   480
structure Validator_Batch = Proof_Data
bulwahn@42195
   481
(
bulwahn@42195
   482
  type T = unit -> (int -> bool) list
bulwahn@42195
   483
  (* FIXME avoid user error with non-user text *)
bulwahn@42195
   484
  fun init _ () = error "Counterexample"
bulwahn@42195
   485
);
bulwahn@42195
   486
val put_validator_batch = Validator_Batch.put;
bulwahn@42195
   487
bulwahn@42195
   488
bulwahn@40420
   489
val target = "Quickcheck";
bulwahn@42159
   490
  
bulwahn@42159
   491
fun compile_generator_expr ctxt ts =
bulwahn@42159
   492
  let
bulwahn@42159
   493
    val thy = ProofContext.theory_of ctxt
bulwahn@42306
   494
    val mk_generator_expr = 
bulwahn@42306
   495
      if Config.get ctxt fast then mk_fast_generator_expr
bulwahn@42306
   496
      else if Config.get ctxt full_support then mk_full_generator_expr else mk_generator_expr
bulwahn@42304
   497
    val t' = mk_parametric_generator_expr mk_generator_expr ctxt ts;
bulwahn@42159
   498
    val compile = Code_Runtime.dynamic_value_strict
bulwahn@42159
   499
      (Counterexample.get, put_counterexample, "Exhaustive_Generators.put_counterexample")
bulwahn@42159
   500
      thy (SOME target) (fn proc => fn g =>
bulwahn@42195
   501
        fn card => fn size => g card size |> (Option.map o map) proc) t' []
bulwahn@42159
   502
  in
bulwahn@42159
   503
    fn [card, size] => rpair NONE (compile card size |> 
bulwahn@42159
   504
      (if Config.get ctxt quickcheck_pretty then
bulwahn@42159
   505
        Option.map (map Quickcheck_Common.post_process_term) else I))
bulwahn@42159
   506
  end;
bulwahn@42159
   507
 
bulwahn@41861
   508
fun compile_generator_exprs ctxt ts =
bulwahn@41861
   509
  let
bulwahn@41861
   510
    val thy = ProofContext.theory_of ctxt
bulwahn@42028
   511
    val ts' = map (fn t => mk_generator_expr ctxt (t, [])) ts;
bulwahn@41861
   512
    val compiles = Code_Runtime.dynamic_value_strict
bulwahn@41861
   513
      (Counterexample_Batch.get, put_counterexample_batch,
bulwahn@41918
   514
        "Exhaustive_Generators.put_counterexample_batch")
bulwahn@41861
   515
      thy (SOME target) (fn proc => map (fn g => g #> (Option.map o map) proc))
bulwahn@42195
   516
      (HOLogic.mk_list @{typ "code_numeral => term list option"} ts') []
bulwahn@41861
   517
  in
bulwahn@41935
   518
    map (fn compile => fn size => compile size
bulwahn@41935
   519
      |> Option.map (map Quickcheck_Common.post_process_term)) compiles
bulwahn@41861
   520
  end;
bulwahn@42195
   521
bulwahn@42195
   522
fun compile_validator_exprs ctxt ts =
bulwahn@42195
   523
  let
bulwahn@42195
   524
    val thy = ProofContext.theory_of ctxt
bulwahn@42273
   525
    val ts' = map (mk_validator_expr ctxt) ts
bulwahn@42195
   526
  in
bulwahn@42195
   527
    Code_Runtime.dynamic_value_strict
bulwahn@42195
   528
      (Validator_Batch.get, put_validator_batch, "Exhaustive_Generators.put_validator_batch")
bulwahn@42195
   529
      thy (SOME target) (K I) (HOLogic.mk_list @{typ "code_numeral => bool"} ts') []
bulwahn@42195
   530
  end;
bulwahn@42195
   531
bulwahn@42308
   532
(* setup *)
bulwahn@40420
   533
bulwahn@40420
   534
val setup =
bulwahn@42229
   535
  Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
bulwahn@42316
   536
      (((@{sort typerep}, @{sort term_of}), @{sort full_exhaustive}), instantiate_full_exhaustive_datatype))
bulwahn@42316
   537
(* #> Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
bulwahn@42229
   538
      (((@{sort typerep}, @{sort term_of}), @{sort exhaustive}), instantiate_exhaustive_datatype))
bulwahn@42230
   539
  #> Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
bulwahn@42306
   540
      (((@{sort typerep}, @{sort term_of}), @{sort fast_exhaustive}), instantiate_fast_exhaustive_datatype))
bulwahn@42310
   541
  #> Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
bulwahn@42316
   542
      (((@{sort type}, @{sort type}), @{sort bounded_forall}), instantiate_bounded_forall_datatype))*)
bulwahn@40907
   543
  #> setup_smart_quantifier
bulwahn@42304
   544
  #> setup_full_support
bulwahn@42306
   545
  #> setup_fast
bulwahn@41903
   546
  #> setup_quickcheck_pretty
bulwahn@41900
   547
  #> Context.theory_map (Quickcheck.add_generator ("exhaustive", compile_generator_expr))
bulwahn@42195
   548
  #> Context.theory_map (Quickcheck.add_batch_generator ("exhaustive", compile_generator_exprs))
bulwahn@42195
   549
  #> Context.theory_map (Quickcheck.add_batch_validator ("exhaustive", compile_validator_exprs));
bulwahn@40420
   550
bulwahn@40420
   551
end;