src/HOL/Tools/smallvalue_generators.ML
author bulwahn
Fri Dec 03 08:40:46 2010 +0100 (2010-12-03)
changeset 40901 8fdfa9c4e7ed
parent 40899 ef6fde932f4c
child 40907 45ba9f05583a
permissions -rw-r--r--
smallvalue_generator are defined quick via oracle or sound via function package
bulwahn@40420
     1
(*  Title:      HOL/Tools/smallvalue_generators.ML
bulwahn@40420
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@40420
     3
bulwahn@40420
     4
Generators for small values for various types.
bulwahn@40420
     5
*)
bulwahn@40420
     6
bulwahn@40420
     7
signature SMALLVALUE_GENERATORS =
bulwahn@40420
     8
sig
bulwahn@40420
     9
  val compile_generator_expr:
bulwahn@40420
    10
    Proof.context -> term -> int -> term list option * (bool list * bool)
bulwahn@40420
    11
  val put_counterexample: (unit -> int -> term list option)
bulwahn@40420
    12
    -> Proof.context -> Proof.context
bulwahn@40420
    13
  val setup: theory -> theory
bulwahn@40420
    14
end;
bulwahn@40420
    15
bulwahn@40420
    16
structure Smallvalue_Generators : SMALLVALUE_GENERATORS =
bulwahn@40420
    17
struct
bulwahn@40420
    18
bulwahn@40901
    19
(* static options *)
bulwahn@40901
    20
bulwahn@40901
    21
val define_foundationally = false
bulwahn@40901
    22
bulwahn@40420
    23
(** general term functions **)
bulwahn@40420
    24
bulwahn@40420
    25
fun mk_measure f =
bulwahn@40420
    26
  let
bulwahn@40420
    27
    val Type ("fun", [T, @{typ nat}]) = fastype_of f 
bulwahn@40420
    28
  in
bulwahn@40420
    29
    Const (@{const_name Wellfounded.measure},
bulwahn@40420
    30
      (T --> @{typ nat}) --> HOLogic.mk_prodT (T, T) --> @{typ bool})
bulwahn@40420
    31
    $ f
bulwahn@40420
    32
  end
bulwahn@40420
    33
bulwahn@40420
    34
fun mk_sumcases rT f (Type (@{type_name Sum_Type.sum}, [TL, TR])) =
bulwahn@40420
    35
  let
bulwahn@40420
    36
    val lt = mk_sumcases rT f TL
bulwahn@40420
    37
    val rt = mk_sumcases rT f TR
bulwahn@40420
    38
  in
bulwahn@40420
    39
    SumTree.mk_sumcase TL TR rT lt rt
bulwahn@40420
    40
  end
bulwahn@40420
    41
  | mk_sumcases _ f T = f T
bulwahn@40420
    42
bulwahn@40901
    43
fun mk_undefined T = Const(@{const_name undefined}, T)
bulwahn@40901
    44
  
bulwahn@40420
    45
bulwahn@40420
    46
(** abstract syntax **)
bulwahn@40420
    47
bulwahn@40639
    48
fun termifyT T = HOLogic.mk_prodT (T, @{typ "unit => Code_Evaluation.term"});
bulwahn@40639
    49
bulwahn@40420
    50
val size = @{term "i :: code_numeral"}
bulwahn@40420
    51
val size_pred = @{term "(i :: code_numeral) - 1"}
bulwahn@40420
    52
val size_ge_zero = @{term "(i :: code_numeral) > 0"}
bulwahn@40639
    53
fun test_function T = Free ("f", termifyT T --> @{typ "term list option"})
bulwahn@40420
    54
bulwahn@40420
    55
fun mk_none_continuation (x, y) =
bulwahn@40420
    56
  let
bulwahn@40420
    57
    val (T as Type(@{type_name "option"}, [T'])) = fastype_of x
bulwahn@40420
    58
  in
bulwahn@40899
    59
    Const (@{const_name "Smallcheck.orelse"}, T --> T --> T)
bulwahn@40899
    60
      $ x $ y
bulwahn@40420
    61
  end
bulwahn@40420
    62
bulwahn@40420
    63
(** datatypes **)
bulwahn@40420
    64
bulwahn@40420
    65
(* constructing smallvalue generator instances on datatypes *)
bulwahn@40420
    66
bulwahn@40420
    67
exception FUNCTION_TYPE;
bulwahn@40420
    68
bulwahn@40420
    69
val smallN = "small";
bulwahn@40420
    70
bulwahn@40420
    71
fun smallT T = (T --> @{typ "Code_Evaluation.term list option"}) --> @{typ code_numeral}
bulwahn@40420
    72
  --> @{typ "Code_Evaluation.term list option"}
bulwahn@40420
    73
bulwahn@40639
    74
val full_smallN = "full_small";
bulwahn@40639
    75
bulwahn@40639
    76
fun full_smallT T = (termifyT T --> @{typ "Code_Evaluation.term list option"})
bulwahn@40639
    77
  --> @{typ code_numeral} --> @{typ "Code_Evaluation.term list option"}
bulwahn@40639
    78
 
bulwahn@40901
    79
fun mk_equations thy descr vs tycos smalls (Ts, Us) =
bulwahn@40420
    80
  let
bulwahn@40420
    81
    fun mk_small_call T =
bulwahn@40420
    82
      let
bulwahn@40639
    83
        val small = Const (@{const_name "Smallcheck.full_small_class.full_small"}, full_smallT T)        
bulwahn@40420
    84
      in
bulwahn@40639
    85
        (T, (fn t => small $
bulwahn@40639
    86
          (HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, @{typ "Code_Evaluation.term list option"})
bulwahn@40639
    87
          $ absdummy (T, absdummy (@{typ "unit => Code_Evaluation.term"}, t))) $ size_pred))
bulwahn@40420
    88
      end
bulwahn@40420
    89
    fun mk_small_aux_call fTs (k, _) (tyco, Ts) =
bulwahn@40420
    90
      let
bulwahn@40420
    91
        val T = Type (tyco, Ts)
bulwahn@40420
    92
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
bulwahn@40420
    93
        val small = nth smalls k
bulwahn@40420
    94
      in
bulwahn@40639
    95
       (T, (fn t => small $
bulwahn@40639
    96
          (HOLogic.split_const (T, @{typ "unit => Code_Evaluation.term"}, @{typ "Code_Evaluation.term list option"})
bulwahn@40639
    97
            $ absdummy (T, absdummy (@{typ "unit => Code_Evaluation.term"}, t))) $ size_pred))  
bulwahn@40420
    98
      end
bulwahn@40420
    99
    fun mk_consexpr simpleT (c, xs) =
bulwahn@40420
   100
      let
bulwahn@40420
   101
        val (Ts, fns) = split_list xs
bulwahn@40420
   102
        val constr = Const (c, Ts ---> simpleT)
bulwahn@40639
   103
        val bounds = map (fn x => Bound (2 * x + 1)) (((length xs) - 1) downto 0)
bulwahn@40639
   104
        val term_bounds = map (fn x => Bound (2 * x)) (((length xs) - 1) downto 0)
bulwahn@40639
   105
        val Eval_App = Const ("Code_Evaluation.App", HOLogic.termT --> HOLogic.termT --> HOLogic.termT)
bulwahn@40639
   106
        val Eval_Const = Const ("Code_Evaluation.Const", HOLogic.literalT --> @{typ typerep} --> HOLogic.termT)
bulwahn@40639
   107
        val term = fold (fn u => fn t => Eval_App $ t $ (u $ @{term "()"}))
bulwahn@40639
   108
          bounds (Eval_Const $ HOLogic.mk_literal c $ HOLogic.mk_typerep (Ts ---> simpleT))
bulwahn@40639
   109
        val start_term = test_function simpleT $ 
bulwahn@40639
   110
        (HOLogic.pair_const simpleT @{typ "unit => Code_Evaluation.term"}
bulwahn@40639
   111
          $ (list_comb (constr, bounds)) $ absdummy (@{typ unit}, term))
bulwahn@40420
   112
      in fold_rev (fn f => fn t => f t) fns start_term end
bulwahn@40420
   113
    fun mk_rhs exprs =
bulwahn@40420
   114
        @{term "If :: bool => term list option => term list option => term list option"}
bulwahn@40420
   115
            $ size_ge_zero $ (foldr1 mk_none_continuation exprs) $ @{term "None :: term list option"}
bulwahn@40420
   116
    val rhss =
bulwahn@40420
   117
      Datatype_Aux.interpret_construction descr vs
bulwahn@40420
   118
        { atyp = mk_small_call, dtyp = mk_small_aux_call }
bulwahn@40420
   119
      |> (map o apfst) Type
bulwahn@40420
   120
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
bulwahn@40420
   121
      |> map mk_rhs
bulwahn@40420
   122
    val lhss = map2 (fn t => fn T => t $ test_function T $ size) smalls (Ts @ Us);
bulwahn@40420
   123
    val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
bulwahn@40420
   124
  in
bulwahn@40901
   125
    eqs
bulwahn@40901
   126
  end
bulwahn@40901
   127
bulwahn@40901
   128
(* foundational definition with the function package *)
bulwahn@40901
   129
bulwahn@40901
   130
val less_int_pred = @{lemma "i > 0 ==> Code_Numeral.nat_of ((i :: code_numeral) - 1) < Code_Numeral.nat_of i" by auto}
bulwahn@40901
   131
bulwahn@40901
   132
fun mk_single_measure T = HOLogic.mk_comp (@{term "Code_Numeral.nat_of"},
bulwahn@40901
   133
    Const (@{const_name "Product_Type.snd"}, T --> @{typ "code_numeral"}))
bulwahn@40901
   134
bulwahn@40901
   135
fun mk_termination_measure T =
bulwahn@40901
   136
  let
bulwahn@40901
   137
    val T' = fst (HOLogic.dest_prodT (HOLogic.dest_setT T))
bulwahn@40901
   138
  in
bulwahn@40901
   139
    mk_measure (mk_sumcases @{typ nat} mk_single_measure T')
bulwahn@40420
   140
  end
bulwahn@40901
   141
bulwahn@40901
   142
fun termination_tac ctxt = 
bulwahn@40901
   143
  Function_Relation.relation_tac ctxt mk_termination_measure 1
bulwahn@40901
   144
  THEN rtac @{thm wf_measure} 1
bulwahn@40901
   145
  THEN (REPEAT_DETERM (Simplifier.asm_full_simp_tac 
bulwahn@40901
   146
    (HOL_basic_ss addsimps [@{thm in_measure}, @{thm o_def}, @{thm snd_conv},
bulwahn@40901
   147
     @{thm nat_mono_iff}, less_int_pred] @ @{thms sum.cases}) 1))
bulwahn@40901
   148
bulwahn@40901
   149
fun pat_completeness_auto ctxt =
bulwahn@40901
   150
  Pat_Completeness.pat_completeness_tac ctxt 1
bulwahn@40901
   151
  THEN auto_tac (clasimpset_of ctxt)    
bulwahn@40901
   152
bulwahn@40901
   153
bulwahn@40901
   154
(* creating the instances *)
bulwahn@40901
   155
bulwahn@40420
   156
fun instantiate_smallvalue_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
bulwahn@40420
   157
  let
bulwahn@40420
   158
    val _ = Datatype_Aux.message config "Creating smallvalue generators ...";
bulwahn@40901
   159
    val smallsN = map (prefix (full_smallN ^ "_")) (names @ auxnames);
bulwahn@40901
   160
  in
bulwahn@40641
   161
    thy
bulwahn@40639
   162
    |> Class.instantiation (tycos, vs, @{sort full_small})
bulwahn@40901
   163
    |> (if define_foundationally then
bulwahn@40901
   164
      let
bulwahn@40901
   165
        val smalls = map2 (fn name => fn T => Free (name, full_smallT T)) smallsN (Ts @ Us)
bulwahn@40901
   166
        val eqs = mk_equations thy descr vs tycos smalls (Ts, Us)
bulwahn@40901
   167
      in
bulwahn@40901
   168
        Function.add_function
bulwahn@40901
   169
          (map (fn (name, T) =>
bulwahn@40901
   170
              Syntax.no_syn (Binding.conceal (Binding.name name), SOME (full_smallT T)))
bulwahn@40901
   171
                (smallsN ~~ (Ts @ Us)))
bulwahn@40901
   172
            (map (pair (apfst Binding.conceal Attrib.empty_binding)) eqs)
bulwahn@40901
   173
          Function_Common.default_config pat_completeness_auto
bulwahn@40901
   174
        #> snd
bulwahn@40901
   175
        #> Local_Theory.restore
bulwahn@40901
   176
        #> (fn lthy => Function.prove_termination NONE (termination_tac lthy) lthy)
bulwahn@40901
   177
        #> snd
bulwahn@40901
   178
      end
bulwahn@40901
   179
    else
bulwahn@40901
   180
      fold_map (fn (name, T) => Local_Theory.define
bulwahn@40901
   181
          ((Binding.conceal (Binding.name name), NoSyn),
bulwahn@40901
   182
            (apfst Binding.conceal Attrib.empty_binding, mk_undefined (full_smallT T)))
bulwahn@40901
   183
        #> apfst fst) (smallsN ~~ (Ts @ Us))
bulwahn@40901
   184
      #> (fn (smalls, lthy) =>
bulwahn@40901
   185
        let
bulwahn@40901
   186
          val eqs_t = mk_equations thy descr vs tycos smalls (Ts, Us)
bulwahn@40901
   187
          val eqs = map (fn eq => Goal.prove lthy ["f", "i"] [] eq
bulwahn@40901
   188
            (fn _ => Skip_Proof.cheat_tac (ProofContext.theory_of lthy))) eqs_t
bulwahn@40901
   189
        in
bulwahn@40901
   190
          fold (fn (name, eq) => Local_Theory.note
bulwahn@40901
   191
          ((Binding.conceal (Binding.qualify true prfx
bulwahn@40901
   192
             (Binding.qualify true name (Binding.name "simps"))),
bulwahn@40901
   193
             Code.add_default_eqn_attrib :: map (Attrib.internal o K)
bulwahn@40901
   194
               [Simplifier.simp_add, Nitpick_Simps.add]), [eq]) #> snd) (smallsN ~~ eqs) lthy
bulwahn@40901
   195
        end))
bulwahn@40641
   196
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bulwahn@40641
   197
  end handle FUNCTION_TYPE =>
bulwahn@40639
   198
    (Datatype_Aux.message config
bulwahn@40639
   199
      "Creation of smallvalue generators failed because the datatype contains a function type";
bulwahn@40639
   200
    thy)
bulwahn@40420
   201
bulwahn@40420
   202
(** building and compiling generator expressions **)
bulwahn@40420
   203
bulwahn@40420
   204
structure Counterexample = Proof_Data (
bulwahn@40420
   205
  type T = unit -> int -> term list option
bulwahn@40420
   206
  fun init _ () = error "Counterexample"
bulwahn@40420
   207
);
bulwahn@40420
   208
val put_counterexample = Counterexample.put;
bulwahn@40420
   209
bulwahn@40420
   210
val target = "Quickcheck";
bulwahn@40420
   211
bulwahn@40420
   212
fun mk_generator_expr thy prop Ts =
bulwahn@40420
   213
  let
bulwahn@40420
   214
    val bound_max = length Ts - 1;
bulwahn@40639
   215
    val bounds = map_index (fn (i, ty) =>
bulwahn@40639
   216
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) Ts;
bulwahn@40639
   217
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
bulwahn@40639
   218
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
bulwahn@40420
   219
    val check =
bulwahn@40420
   220
      @{term "Smallcheck.catch_match :: term list option => term list option => term list option"} $
bulwahn@40420
   221
        (@{term "If :: bool => term list option => term list option => term list option"}
bulwahn@40639
   222
        $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option"} $ terms))
bulwahn@40420
   223
      $ @{term "None :: term list option"};
bulwahn@40639
   224
    fun mk_small_closure (_, _, i, T) t =
bulwahn@40639
   225
      Const (@{const_name "Smallcheck.full_small_class.full_small"}, full_smallT T)
bulwahn@40639
   226
        $ (HOLogic.split_const (T, @{typ "unit => term"}, @{typ "term list option"}) 
bulwahn@40639
   227
        $ absdummy (T, absdummy (@{typ "unit => term"}, t))) $ Bound i
bulwahn@40639
   228
  in Abs ("d", @{typ code_numeral}, fold_rev mk_small_closure bounds check) end
bulwahn@40420
   229
bulwahn@40420
   230
fun compile_generator_expr ctxt t =
bulwahn@40420
   231
  let
bulwahn@40420
   232
    val Ts = (map snd o fst o strip_abs) t;
bulwahn@40420
   233
    val thy = ProofContext.theory_of ctxt
bulwahn@40644
   234
  in if Config.get ctxt Quickcheck.report then
bulwahn@40420
   235
    error "Compilation with reporting facility is not supported"
bulwahn@40420
   236
  else
bulwahn@40420
   237
    let
bulwahn@40420
   238
      val t' = mk_generator_expr thy t Ts;
bulwahn@40420
   239
      val compile = Code_Runtime.dynamic_value_strict
bulwahn@40420
   240
        (Counterexample.get, put_counterexample, "Smallvalue_Generators.put_counterexample")
bulwahn@40420
   241
        thy (SOME target) (fn proc => fn g => g #> (Option.map o map) proc) t' [];
bulwahn@40420
   242
      val dummy_report = ([], false)
bulwahn@40420
   243
    in compile #> rpair dummy_report end
bulwahn@40420
   244
  end;
bulwahn@40420
   245
bulwahn@40420
   246
(** setup **)
bulwahn@40420
   247
bulwahn@40420
   248
val setup =
bulwahn@40639
   249
  Datatype.interpretation
bulwahn@40639
   250
    (Quickcheck_Generators.ensure_sort_datatype (@{sort full_small}, instantiate_smallvalue_datatype))
bulwahn@40420
   251
  #> Context.theory_map
bulwahn@40420
   252
    (Quickcheck.add_generator ("small", compile_generator_expr));
bulwahn@40420
   253
bulwahn@40420
   254
end;