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