src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author wenzelm
Thu, 09 Jun 2011 17:51:49 +0200
changeset 44208 47cf4bc789aa
parent 44206 2b47822868e4
child 45112 7943b69f0188
permissions -rw-r--r--
simplified Name.variant -- discontinued builtin fold_map;
wenzelm@33264
     1
(*  Title:      HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
wenzelm@33264
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@33242
     3
wenzelm@33264
     4
Auxilary functions for predicate compiler.
bulwahn@33242
     5
*)
bulwahn@33242
     6
bulwahn@36047
     7
signature PREDICATE_COMPILE_AUX =
bulwahn@36047
     8
sig
bulwahn@36047
     9
  (* general functions *)
bulwahn@36047
    10
  val apfst3 : ('a -> 'd) -> 'a * 'b * 'c -> 'd * 'b * 'c
bulwahn@36047
    11
  val apsnd3 : ('b -> 'd) -> 'a * 'b * 'c -> 'a * 'd * 'c
bulwahn@36047
    12
  val aptrd3 : ('c -> 'd) -> 'a * 'b * 'c -> 'a * 'b * 'd
bulwahn@36047
    13
  val find_indices : ('a -> bool) -> 'a list -> int list
bulwahn@36047
    14
  (* mode *)
bulwahn@36047
    15
  datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@36047
    16
  val eq_mode : mode * mode -> bool
bulwahn@39557
    17
  val mode_ord: mode * mode -> order
bulwahn@36047
    18
  val list_fun_mode : mode list -> mode
bulwahn@36047
    19
  val strip_fun_mode : mode -> mode list
bulwahn@36047
    20
  val dest_fun_mode : mode -> mode list
bulwahn@36047
    21
  val dest_tuple_mode : mode -> mode list
bulwahn@36047
    22
  val all_modes_of_typ : typ -> mode list
bulwahn@36047
    23
  val all_smodes_of_typ : typ -> mode list
bulwahn@36047
    24
  val fold_map_aterms_prodT : ('a -> 'a -> 'a) -> (typ -> 'b -> 'a * 'b) -> typ -> 'b -> 'a * 'b
bulwahn@36047
    25
  val map_filter_prod : (term -> term option) -> term -> term option
bulwahn@36047
    26
  val replace_ho_args : mode -> term list -> term list -> term list
bulwahn@36047
    27
  val ho_arg_modes_of : mode -> mode list
bulwahn@36047
    28
  val ho_argsT_of : mode -> typ list -> typ list
bulwahn@36047
    29
  val ho_args_of : mode -> term list -> term list
bulwahn@39532
    30
  val ho_args_of_typ : typ -> term list -> term list
bulwahn@39532
    31
  val ho_argsT_of_typ : typ list -> typ list
bulwahn@36047
    32
  val split_map_mode : (mode -> term -> term option * term option)
bulwahn@36047
    33
    -> mode -> term list -> term list * term list
bulwahn@36047
    34
  val split_map_modeT : (mode -> typ -> typ option * typ option)
bulwahn@36047
    35
    -> mode -> typ list -> typ list * typ list
bulwahn@36047
    36
  val split_mode : mode -> term list -> term list * term list
bulwahn@40381
    37
  val split_modeT : mode -> typ list -> typ list * typ list
bulwahn@36047
    38
  val string_of_mode : mode -> string
bulwahn@36047
    39
  val ascii_string_of_mode : mode -> string
bulwahn@36047
    40
  (* premises *)
bulwahn@36047
    41
  datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@36047
    42
    | Generator of (string * typ)
bulwahn@36251
    43
  val dest_indprem : indprem -> term
bulwahn@36254
    44
  val map_indprem : (term -> term) -> indprem -> indprem
bulwahn@36047
    45
  (* general syntactic functions *)
bulwahn@36047
    46
  val conjuncts : term -> term list
bulwahn@36047
    47
  val is_equationlike : thm -> bool
bulwahn@36047
    48
  val is_pred_equation : thm -> bool
bulwahn@36047
    49
  val is_intro : string -> thm -> bool
bulwahn@36047
    50
  val is_predT : typ -> bool
bulwahn@36047
    51
  val is_constrt : theory -> term -> bool
bulwahn@36047
    52
  val is_constr : Proof.context -> string -> bool
bulwahn@42958
    53
  val strip_ex : term -> (string * typ) list * term
bulwahn@36047
    54
  val focus_ex : term -> Name.context -> ((string * typ) list * term) * Name.context
bulwahn@36047
    55
  val strip_all : term -> (string * typ) list * term
bulwahn@40347
    56
  val strip_intro_concl : thm -> term * term list
bulwahn@36047
    57
  (* introduction rule combinators *)
bulwahn@36047
    58
  val map_atoms : (term -> term) -> term -> term
bulwahn@36047
    59
  val fold_atoms : (term -> 'a -> 'a) -> term -> 'a -> 'a
bulwahn@36047
    60
  val fold_map_atoms : (term -> 'a -> term * 'a) -> term -> 'a -> term * 'a
bulwahn@36047
    61
  val maps_premises : (term -> term list) -> term -> term
bulwahn@36047
    62
  val map_concl : (term -> term) -> term -> term
bulwahn@36047
    63
  val map_term : theory -> (term -> term) -> thm -> thm
bulwahn@36047
    64
  (* split theorems of case expressions *)
bulwahn@36047
    65
  val prepare_split_thm : Proof.context -> thm -> thm
bulwahn@36047
    66
  val find_split_thm : theory -> term -> thm option
bulwahn@36047
    67
  (* datastructures and setup for generic compilation *)
bulwahn@36047
    68
  datatype compilation_funs = CompilationFuns of {
bulwahn@36047
    69
    mk_predT : typ -> typ,
bulwahn@36047
    70
    dest_predT : typ -> typ,
bulwahn@36047
    71
    mk_bot : typ -> term,
bulwahn@36047
    72
    mk_single : term -> term,
bulwahn@36047
    73
    mk_bind : term * term -> term,
bulwahn@36047
    74
    mk_sup : term * term -> term,
bulwahn@36047
    75
    mk_if : term -> term,
bulwahn@36049
    76
    mk_iterate_upto : typ -> term * term * term -> term,
bulwahn@36047
    77
    mk_not : term -> term,
bulwahn@36047
    78
    mk_map : typ -> typ -> term -> term -> term
bulwahn@36047
    79
  };
bulwahn@36047
    80
  val mk_predT : compilation_funs -> typ -> typ
bulwahn@36047
    81
  val dest_predT : compilation_funs -> typ -> typ
bulwahn@36047
    82
  val mk_bot : compilation_funs -> typ -> term
bulwahn@36047
    83
  val mk_single : compilation_funs -> term -> term
bulwahn@36047
    84
  val mk_bind : compilation_funs -> term * term -> term
bulwahn@36047
    85
  val mk_sup : compilation_funs -> term * term -> term
bulwahn@36047
    86
  val mk_if : compilation_funs -> term -> term
bulwahn@36049
    87
  val mk_iterate_upto : compilation_funs -> typ -> term * term * term -> term
bulwahn@36047
    88
  val mk_not : compilation_funs -> term -> term
bulwahn@36047
    89
  val mk_map : compilation_funs -> typ -> typ -> term -> term -> term
bulwahn@36047
    90
  val funT_of : compilation_funs -> mode -> typ -> typ
bulwahn@36047
    91
  (* Different compilations *)
bulwahn@36047
    92
  datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
bulwahn@36047
    93
    | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
bulwahn@40232
    94
    | Pos_Generator_DSeq | Neg_Generator_DSeq
bulwahn@36047
    95
  val negative_compilation_of : compilation -> compilation
bulwahn@36047
    96
  val compilation_for_polarity : bool -> compilation -> compilation
bulwahn@40230
    97
  val is_depth_limited_compilation : compilation -> bool 
bulwahn@36047
    98
  val string_of_compilation : compilation -> string
bulwahn@36047
    99
  val compilation_names : (string * compilation) list
bulwahn@36047
   100
  val non_random_compilations : compilation list
bulwahn@36047
   101
  val random_compilations : compilation list
bulwahn@36047
   102
  (* Different options for compiler *)
bulwahn@36047
   103
  datatype options = Options of {  
bulwahn@36047
   104
    expected_modes : (string * mode list) option,
bulwahn@39628
   105
    proposed_modes : (string * mode list) list,
bulwahn@36047
   106
    proposed_names : ((string * mode) * string) list,
bulwahn@36047
   107
    show_steps : bool,
bulwahn@36047
   108
    show_proof_trace : bool,
bulwahn@36047
   109
    show_intermediate_results : bool,
bulwahn@36047
   110
    show_mode_inference : bool,
bulwahn@36047
   111
    show_modes : bool,
bulwahn@36047
   112
    show_compilation : bool,
bulwahn@36047
   113
    show_caught_failures : bool,
bulwahn@39629
   114
    show_invalid_clauses : bool,
bulwahn@36047
   115
    skip_proof : bool,
bulwahn@36047
   116
    no_topmost_reordering : bool,
bulwahn@36047
   117
    function_flattening : bool,
bulwahn@36047
   118
    fail_safe_function_flattening : bool,
bulwahn@36248
   119
    specialise : bool,
bulwahn@36047
   120
    no_higher_order_predicate : string list,
bulwahn@36047
   121
    inductify : bool,
bulwahn@36254
   122
    detect_switches : bool,
bulwahn@40229
   123
    smart_depth_limiting : bool,
bulwahn@36047
   124
    compilation : compilation
bulwahn@36047
   125
  };
bulwahn@36047
   126
  val expected_modes : options -> (string * mode list) option
bulwahn@39628
   127
  val proposed_modes : options -> string -> mode list option
bulwahn@36047
   128
  val proposed_names : options -> string -> mode -> string option
bulwahn@36047
   129
  val show_steps : options -> bool
bulwahn@36047
   130
  val show_proof_trace : options -> bool
bulwahn@36047
   131
  val show_intermediate_results : options -> bool
bulwahn@36047
   132
  val show_mode_inference : options -> bool
bulwahn@36047
   133
  val show_modes : options -> bool
bulwahn@36047
   134
  val show_compilation : options -> bool
bulwahn@36047
   135
  val show_caught_failures : options -> bool
bulwahn@39629
   136
  val show_invalid_clauses : options -> bool
bulwahn@36047
   137
  val skip_proof : options -> bool
bulwahn@36047
   138
  val no_topmost_reordering : options -> bool
bulwahn@36047
   139
  val function_flattening : options -> bool
bulwahn@36047
   140
  val fail_safe_function_flattening : options -> bool
bulwahn@36248
   141
  val specialise : options -> bool
bulwahn@36047
   142
  val no_higher_order_predicate : options -> string list
bulwahn@36047
   143
  val is_inductify : options -> bool
bulwahn@36254
   144
  val detect_switches : options -> bool
bulwahn@40229
   145
  val smart_depth_limiting : options -> bool
bulwahn@36047
   146
  val compilation : options -> compilation
bulwahn@36047
   147
  val default_options : options
bulwahn@36047
   148
  val bool_options : string list
bulwahn@36047
   149
  val print_step : options -> string -> unit
bulwahn@39881
   150
  (* conversions *)
bulwahn@39881
   151
  val imp_prems_conv : conv -> conv
bulwahn@36047
   152
  (* simple transformations *)
bulwahn@40020
   153
  val split_conjuncts_in_assms : Proof.context -> thm -> thm
bulwahn@40233
   154
  val dest_conjunct_prem : thm -> thm list
bulwahn@36047
   155
  val expand_tuples : theory -> thm -> thm
bulwahn@40048
   156
  val case_betapply : theory -> term -> term
bulwahn@36047
   157
  val eta_contract_ho_arguments : theory -> thm -> thm
bulwahn@36047
   158
  val remove_equalities : theory -> thm -> thm
bulwahn@36246
   159
  val remove_pointless_clauses : thm -> thm list
bulwahn@36246
   160
  val peephole_optimisation : theory -> thm -> thm option
bulwahn@40347
   161
  (* auxillary *)
bulwahn@40347
   162
  val unify_consts : theory -> term list -> term list -> (term list * term list)
bulwahn@40347
   163
  val mk_casesrule : Proof.context -> term -> thm list -> term
bulwahn@40347
   164
  val preprocess_intro : theory -> thm -> thm
bulwahn@40347
   165
  
bulwahn@39763
   166
  val define_quickcheck_predicate :
bulwahn@40233
   167
    term -> theory -> (((string * typ) * (string * typ) list) * thm) * theory
bulwahn@36047
   168
end;
bulwahn@34935
   169
bulwahn@36047
   170
structure Predicate_Compile_Aux : PREDICATE_COMPILE_AUX =
bulwahn@33242
   171
struct
bulwahn@33242
   172
bulwahn@34935
   173
(* general functions *)
bulwahn@34935
   174
bulwahn@34935
   175
fun apfst3 f (x, y, z) = (f x, y, z)
bulwahn@34935
   176
fun apsnd3 f (x, y, z) = (x, f y, z)
bulwahn@34935
   177
fun aptrd3 f (x, y, z) = (x, y, f z)
bulwahn@34935
   178
bulwahn@34935
   179
fun comb_option f (SOME x1, SOME x2) = SOME (f (x1, x2))
bulwahn@34935
   180
  | comb_option f (NONE, SOME x2) = SOME x2
bulwahn@34935
   181
  | comb_option f (SOME x1, NONE) = SOME x1
bulwahn@34935
   182
  | comb_option f (NONE, NONE) = NONE
bulwahn@34935
   183
bulwahn@35885
   184
fun map2_optional f (x :: xs) (y :: ys) = f x (SOME y) :: (map2_optional f xs ys)
bulwahn@34935
   185
  | map2_optional f (x :: xs) [] = (f x NONE) :: (map2_optional f xs [])
bulwahn@34935
   186
  | map2_optional f [] [] = []
bulwahn@34935
   187
bulwahn@34935
   188
fun find_indices f xs =
bulwahn@34935
   189
  map_filter (fn (i, true) => SOME i | (i, false) => NONE) (map_index (apsnd f) xs)
bulwahn@33328
   190
bulwahn@33328
   191
(* mode *)
bulwahn@33328
   192
bulwahn@34935
   193
datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@33619
   194
bulwahn@33623
   195
(* equality of instantiatedness with respect to equivalences:
bulwahn@33623
   196
  Pair Input Input == Input and Pair Output Output == Output *)
bulwahn@34935
   197
fun eq_mode (Fun (m1, m2), Fun (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34935
   198
  | eq_mode (Pair (m1, m2), Pair (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34935
   199
  | eq_mode (Pair (m1, m2), Input) = eq_mode (m1, Input) andalso eq_mode (m2, Input)
bulwahn@34935
   200
  | eq_mode (Pair (m1, m2), Output) = eq_mode (m1, Output) andalso eq_mode (m2, Output)
bulwahn@34935
   201
  | eq_mode (Input, Pair (m1, m2)) = eq_mode (Input, m1) andalso eq_mode (Input, m2)
bulwahn@34935
   202
  | eq_mode (Output, Pair (m1, m2)) = eq_mode (Output, m1) andalso eq_mode (Output, m2)
bulwahn@34935
   203
  | eq_mode (Input, Input) = true
bulwahn@34935
   204
  | eq_mode (Output, Output) = true
bulwahn@34935
   205
  | eq_mode (Bool, Bool) = true
bulwahn@34935
   206
  | eq_mode _ = false
bulwahn@33623
   207
bulwahn@39557
   208
fun mode_ord (Input, Output) = LESS
bulwahn@39557
   209
  | mode_ord (Output, Input) = GREATER
bulwahn@39557
   210
  | mode_ord (Input, Input) = EQUAL
bulwahn@39557
   211
  | mode_ord (Output, Output) = EQUAL
bulwahn@39557
   212
  | mode_ord (Bool, Bool) = EQUAL
bulwahn@39557
   213
  | mode_ord (Pair (m1, m2), Pair (m3, m4)) = prod_ord mode_ord mode_ord ((m1, m2), (m3, m4))
bulwahn@39557
   214
  | mode_ord (Fun (m1, m2), Fun (m3, m4)) = prod_ord mode_ord mode_ord ((m1, m2), (m3, m4))
bulwahn@39557
   215
 
bulwahn@36029
   216
fun list_fun_mode [] = Bool
bulwahn@36029
   217
  | list_fun_mode (m :: ms) = Fun (m, list_fun_mode ms)
bulwahn@36029
   218
bulwahn@33619
   219
(* name: binder_modes? *)
bulwahn@33619
   220
fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
bulwahn@33619
   221
  | strip_fun_mode Bool = []
bulwahn@35885
   222
  | strip_fun_mode _ = raise Fail "Bad mode for strip_fun_mode"
bulwahn@33619
   223
bulwahn@36047
   224
(* name: strip_fun_mode? *)
bulwahn@33619
   225
fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
bulwahn@33619
   226
  | dest_fun_mode mode = [mode]
bulwahn@33619
   227
bulwahn@33619
   228
fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
bulwahn@33619
   229
  | dest_tuple_mode _ = []
bulwahn@33619
   230
bulwahn@35324
   231
fun all_modes_of_typ' (T as Type ("fun", _)) = 
bulwahn@34935
   232
  let
bulwahn@34935
   233
    val (S, U) = strip_type T
bulwahn@34935
   234
  in
bulwahn@34935
   235
    if U = HOLogic.boolT then
bulwahn@34935
   236
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35324
   237
        (map all_modes_of_typ' S) [Bool]
bulwahn@34935
   238
    else
bulwahn@34935
   239
      [Input, Output]
bulwahn@34935
   240
  end
haftmann@37678
   241
  | all_modes_of_typ' (Type (@{type_name Product_Type.prod}, [T1, T2])) = 
bulwahn@35324
   242
    map_product (curry Pair) (all_modes_of_typ' T1) (all_modes_of_typ' T2)
bulwahn@35324
   243
  | all_modes_of_typ' _ = [Input, Output]
bulwahn@35324
   244
bulwahn@35324
   245
fun all_modes_of_typ (T as Type ("fun", _)) =
bulwahn@35885
   246
    let
bulwahn@35885
   247
      val (S, U) = strip_type T
bulwahn@35885
   248
    in
bulwahn@35885
   249
      if U = @{typ bool} then
bulwahn@35885
   250
        fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35885
   251
          (map all_modes_of_typ' S) [Bool]
bulwahn@35885
   252
      else
bulwahn@39422
   253
        raise Fail "Invocation of all_modes_of_typ with a non-predicate type"
bulwahn@35885
   254
    end
bulwahn@35885
   255
  | all_modes_of_typ @{typ bool} = [Bool]
bulwahn@39422
   256
  | all_modes_of_typ T =
bulwahn@39422
   257
    raise Fail "Invocation of all_modes_of_typ with a non-predicate type"
bulwahn@34935
   258
bulwahn@35324
   259
fun all_smodes_of_typ (T as Type ("fun", _)) =
bulwahn@35324
   260
  let
bulwahn@35324
   261
    val (S, U) = strip_type T
haftmann@37678
   262
    fun all_smodes (Type (@{type_name Product_Type.prod}, [T1, T2])) = 
bulwahn@35324
   263
      map_product (curry Pair) (all_smodes T1) (all_smodes T2)
bulwahn@35324
   264
      | all_smodes _ = [Input, Output]
bulwahn@35324
   265
  in
bulwahn@35324
   266
    if U = HOLogic.boolT then
bulwahn@35324
   267
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2) (map all_smodes S) [Bool]
bulwahn@35324
   268
    else
bulwahn@36047
   269
      raise Fail "invalid type for predicate"
bulwahn@35324
   270
  end
bulwahn@35885
   271
bulwahn@34935
   272
fun ho_arg_modes_of mode =
bulwahn@34935
   273
  let
bulwahn@34935
   274
    fun ho_arg_mode (m as Fun _) =  [m]
bulwahn@34935
   275
      | ho_arg_mode (Pair (m1, m2)) = ho_arg_mode m1 @ ho_arg_mode m2
bulwahn@34935
   276
      | ho_arg_mode _ = []
bulwahn@34935
   277
  in
bulwahn@34935
   278
    maps ho_arg_mode (strip_fun_mode mode)
bulwahn@34935
   279
  end
bulwahn@34935
   280
bulwahn@34935
   281
fun ho_args_of mode ts =
bulwahn@34935
   282
  let
bulwahn@34935
   283
    fun ho_arg (Fun _) (SOME t) = [t]
bulwahn@36047
   284
      | ho_arg (Fun _) NONE = raise Fail "mode and term do not match"
bulwahn@35885
   285
      | ho_arg (Pair (m1, m2)) (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
bulwahn@34935
   286
          ho_arg m1 (SOME t1) @ ho_arg m2 (SOME t2)
bulwahn@34935
   287
      | ho_arg (Pair (m1, m2)) NONE = ho_arg m1 NONE @ ho_arg m2 NONE
bulwahn@34935
   288
      | ho_arg _ _ = []
bulwahn@34935
   289
  in
bulwahn@34935
   290
    flat (map2_optional ho_arg (strip_fun_mode mode) ts)
bulwahn@34935
   291
  end
bulwahn@34935
   292
bulwahn@39532
   293
fun ho_args_of_typ T ts =
bulwahn@39532
   294
  let
bulwahn@39558
   295
    fun ho_arg (T as Type("fun", [_,_])) (SOME t) = if body_type T = @{typ bool} then [t] else []
bulwahn@39532
   296
      | ho_arg (Type("fun", [_,_])) NONE = raise Fail "mode and term do not match"
bulwahn@39532
   297
      | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2]))
bulwahn@39532
   298
         (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
bulwahn@39532
   299
          ho_arg T1 (SOME t1) @ ho_arg T2 (SOME t2)
bulwahn@39532
   300
      | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2])) NONE =
bulwahn@39532
   301
          ho_arg T1 NONE @ ho_arg T2 NONE
bulwahn@39532
   302
      | ho_arg _ _ = []
bulwahn@39532
   303
  in
bulwahn@39532
   304
    flat (map2_optional ho_arg (binder_types T) ts)
bulwahn@39532
   305
  end
bulwahn@39532
   306
bulwahn@39532
   307
fun ho_argsT_of_typ Ts =
bulwahn@39532
   308
  let
bulwahn@39558
   309
    fun ho_arg (T as Type("fun", [_,_])) = if body_type T = @{typ bool} then [T] else []
bulwahn@39532
   310
      | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2])) =
bulwahn@39532
   311
          ho_arg T1 @ ho_arg T2
bulwahn@39532
   312
      | ho_arg _ = []
bulwahn@39532
   313
  in
bulwahn@39532
   314
    maps ho_arg Ts
bulwahn@39532
   315
  end
bulwahn@39532
   316
  
bulwahn@39532
   317
bulwahn@34935
   318
(* temporary function should be replaced by unsplit_input or so? *)
bulwahn@34935
   319
fun replace_ho_args mode hoargs ts =
bulwahn@34935
   320
  let
bulwahn@34935
   321
    fun replace (Fun _, _) (arg' :: hoargs') = (arg', hoargs')
haftmann@37366
   322
      | replace (Pair (m1, m2), Const (@{const_name Pair}, T) $ t1 $ t2) hoargs =
bulwahn@34935
   323
        let
bulwahn@34935
   324
          val (t1', hoargs') = replace (m1, t1) hoargs
bulwahn@34935
   325
          val (t2', hoargs'') = replace (m2, t2) hoargs'
bulwahn@34935
   326
        in
haftmann@37366
   327
          (Const (@{const_name Pair}, T) $ t1' $ t2', hoargs'')
bulwahn@34935
   328
        end
bulwahn@34935
   329
      | replace (_, t) hoargs = (t, hoargs)
bulwahn@34935
   330
  in
bulwahn@35885
   331
    fst (fold_map replace (strip_fun_mode mode ~~ ts) hoargs)
bulwahn@34935
   332
  end
bulwahn@34935
   333
bulwahn@34935
   334
fun ho_argsT_of mode Ts =
bulwahn@34935
   335
  let
bulwahn@34935
   336
    fun ho_arg (Fun _) T = [T]
haftmann@37678
   337
      | ho_arg (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) = ho_arg m1 T1 @ ho_arg m2 T2
bulwahn@34935
   338
      | ho_arg _ _ = []
bulwahn@34935
   339
  in
bulwahn@34935
   340
    flat (map2 ho_arg (strip_fun_mode mode) Ts)
bulwahn@34935
   341
  end
bulwahn@34935
   342
bulwahn@34935
   343
(* splits mode and maps function to higher-order argument types *)
bulwahn@34935
   344
fun split_map_mode f mode ts =
bulwahn@34935
   345
  let
bulwahn@34935
   346
    fun split_arg_mode' (m as Fun _) t = f m t
haftmann@37366
   347
      | split_arg_mode' (Pair (m1, m2)) (Const (@{const_name Pair}, _) $ t1 $ t2) =
bulwahn@34935
   348
        let
bulwahn@34935
   349
          val (i1, o1) = split_arg_mode' m1 t1
bulwahn@34935
   350
          val (i2, o2) = split_arg_mode' m2 t2
bulwahn@34935
   351
        in
bulwahn@34935
   352
          (comb_option HOLogic.mk_prod (i1, i2), comb_option HOLogic.mk_prod (o1, o2))
bulwahn@34935
   353
        end
bulwahn@35324
   354
      | split_arg_mode' m t =
bulwahn@35324
   355
        if eq_mode (m, Input) then (SOME t, NONE)
bulwahn@35324
   356
        else if eq_mode (m, Output) then (NONE,  SOME t)
bulwahn@35885
   357
        else raise Fail "split_map_mode: mode and term do not match"
bulwahn@34935
   358
  in
bulwahn@34935
   359
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) ts)
bulwahn@34935
   360
  end
bulwahn@34935
   361
bulwahn@34935
   362
(* splits mode and maps function to higher-order argument types *)
bulwahn@34935
   363
fun split_map_modeT f mode Ts =
bulwahn@34935
   364
  let
bulwahn@34935
   365
    fun split_arg_mode' (m as Fun _) T = f m T
haftmann@37678
   366
      | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
bulwahn@34935
   367
        let
bulwahn@34935
   368
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34935
   369
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34935
   370
        in
bulwahn@34935
   371
          (comb_option HOLogic.mk_prodT (i1, i2), comb_option HOLogic.mk_prodT (o1, o2))
bulwahn@34935
   372
        end
bulwahn@34935
   373
      | split_arg_mode' Input T = (SOME T, NONE)
bulwahn@34935
   374
      | split_arg_mode' Output T = (NONE,  SOME T)
bulwahn@35885
   375
      | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
bulwahn@34935
   376
  in
bulwahn@34935
   377
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34935
   378
  end
bulwahn@34935
   379
bulwahn@34935
   380
fun split_mode mode ts = split_map_mode (fn _ => fn _ => (NONE, NONE)) mode ts
bulwahn@34935
   381
haftmann@37678
   382
fun fold_map_aterms_prodT comb f (Type (@{type_name Product_Type.prod}, [T1, T2])) s =
bulwahn@34935
   383
  let
bulwahn@34935
   384
    val (x1, s') = fold_map_aterms_prodT comb f T1 s
bulwahn@34935
   385
    val (x2, s'') = fold_map_aterms_prodT comb f T2 s'
bulwahn@34935
   386
  in
bulwahn@34935
   387
    (comb x1 x2, s'')
bulwahn@34935
   388
  end
bulwahn@34935
   389
  | fold_map_aterms_prodT comb f T s = f T s
bulwahn@34935
   390
haftmann@37366
   391
fun map_filter_prod f (Const (@{const_name Pair}, _) $ t1 $ t2) =
bulwahn@34935
   392
  comb_option HOLogic.mk_prod (map_filter_prod f t1, map_filter_prod f t2)
bulwahn@34935
   393
  | map_filter_prod f t = f t
bulwahn@34935
   394
  
bulwahn@40381
   395
fun split_modeT mode Ts =
bulwahn@34935
   396
  let
bulwahn@40381
   397
    fun split_arg_mode (Fun _) T = ([], [])
bulwahn@40381
   398
      | split_arg_mode (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
bulwahn@34935
   399
        let
bulwahn@40381
   400
          val (i1, o1) = split_arg_mode m1 T1
bulwahn@40381
   401
          val (i2, o2) = split_arg_mode m2 T2
bulwahn@34935
   402
        in
bulwahn@34935
   403
          (i1 @ i2, o1 @ o2)
bulwahn@34935
   404
        end
bulwahn@40381
   405
      | split_arg_mode Input T = ([T], [])
bulwahn@40381
   406
      | split_arg_mode Output T = ([], [T])
bulwahn@40381
   407
      | split_arg_mode _ _ = raise Fail "split_modeT: mode and type do not match"
bulwahn@34935
   408
  in
bulwahn@40381
   409
    (pairself flat o split_list) (map2 split_arg_mode (strip_fun_mode mode) Ts)
bulwahn@34935
   410
  end
bulwahn@34935
   411
bulwahn@34935
   412
fun string_of_mode mode =
bulwahn@33619
   413
  let
bulwahn@33619
   414
    fun string_of_mode1 Input = "i"
bulwahn@33619
   415
      | string_of_mode1 Output = "o"
bulwahn@33619
   416
      | string_of_mode1 Bool = "bool"
bulwahn@33619
   417
      | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
bulwahn@33626
   418
    and string_of_mode2 (Pair (m1, m2)) = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
bulwahn@33619
   419
      | string_of_mode2 mode = string_of_mode1 mode
bulwahn@33619
   420
    and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
bulwahn@33619
   421
      | string_of_mode3 mode = string_of_mode2 mode
bulwahn@34935
   422
  in string_of_mode3 mode end
bulwahn@33619
   423
bulwahn@34935
   424
fun ascii_string_of_mode mode' =
bulwahn@33626
   425
  let
bulwahn@33626
   426
    fun ascii_string_of_mode' Input = "i"
bulwahn@33626
   427
      | ascii_string_of_mode' Output = "o"
bulwahn@33626
   428
      | ascii_string_of_mode' Bool = "b"
bulwahn@33626
   429
      | ascii_string_of_mode' (Pair (m1, m2)) =
bulwahn@33626
   430
          "P" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   431
      | ascii_string_of_mode' (Fun (m1, m2)) = 
bulwahn@33626
   432
          "F" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Fun m2 ^ "B"
bulwahn@33626
   433
    and ascii_string_of_mode'_Fun (Fun (m1, m2)) =
bulwahn@33626
   434
          ascii_string_of_mode' m1 ^ (if m2 = Bool then "" else "_" ^ ascii_string_of_mode'_Fun m2)
bulwahn@33626
   435
      | ascii_string_of_mode'_Fun Bool = "B"
bulwahn@33626
   436
      | ascii_string_of_mode'_Fun m = ascii_string_of_mode' m
bulwahn@33626
   437
    and ascii_string_of_mode'_Pair (Pair (m1, m2)) =
bulwahn@33626
   438
          ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   439
      | ascii_string_of_mode'_Pair m = ascii_string_of_mode' m
bulwahn@33626
   440
  in ascii_string_of_mode'_Fun mode' end
bulwahn@33626
   441
bulwahn@34935
   442
(* premises *)
bulwahn@33619
   443
bulwahn@34935
   444
datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@34935
   445
  | Generator of (string * typ);
bulwahn@33619
   446
bulwahn@36251
   447
fun dest_indprem (Prem t) = t
bulwahn@36251
   448
  | dest_indprem (Negprem t) = t
bulwahn@36251
   449
  | dest_indprem (Sidecond t) = t
bulwahn@36251
   450
  | dest_indprem (Generator _) = raise Fail "cannot destruct generator"
bulwahn@36251
   451
bulwahn@36254
   452
fun map_indprem f (Prem t) = Prem (f t)
bulwahn@36254
   453
  | map_indprem f (Negprem t) = Negprem (f t)
bulwahn@36254
   454
  | map_indprem f (Sidecond t) = Sidecond (f t)
bulwahn@36254
   455
  | map_indprem f (Generator (v, T)) = Generator (dest_Free (f (Free (v, T))))
bulwahn@36254
   456
bulwahn@33242
   457
(* general syntactic functions *)
bulwahn@33242
   458
bulwahn@33242
   459
(*Like dest_conj, but flattens conjunctions however nested*)
haftmann@39028
   460
fun conjuncts_aux (Const (@{const_name HOL.conj}, _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
bulwahn@33242
   461
  | conjuncts_aux t conjs = t::conjs;
bulwahn@33242
   462
bulwahn@33242
   463
fun conjuncts t = conjuncts_aux t [];
bulwahn@33242
   464
bulwahn@33242
   465
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
haftmann@39093
   466
  | is_equationlike_term (Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ _)) = true
bulwahn@33242
   467
  | is_equationlike_term _ = false
bulwahn@33242
   468
  
bulwahn@33242
   469
val is_equationlike = is_equationlike_term o prop_of 
bulwahn@33242
   470
bulwahn@33242
   471
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
bulwahn@33242
   472
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
bulwahn@33242
   473
  | is_pred_equation_term _ = false
bulwahn@33242
   474
  
bulwahn@33242
   475
val is_pred_equation = is_pred_equation_term o prop_of 
bulwahn@33242
   476
bulwahn@33242
   477
fun is_intro_term constname t =
bulwahn@34935
   478
  the_default false (try (fn t => case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
bulwahn@33242
   479
    Const (c, _) => c = constname
bulwahn@34935
   480
  | _ => false) t)
bulwahn@33242
   481
  
bulwahn@33242
   482
fun is_intro constname t = is_intro_term constname (prop_of t)
bulwahn@33242
   483
haftmann@38777
   484
fun is_pred thy constname = (body_type (Sign.the_const_type thy constname) = HOLogic.boolT);
bulwahn@33242
   485
wenzelm@41093
   486
fun is_predT (T as Type("fun", [_, _])) = (body_type T = @{typ bool})
bulwahn@33242
   487
  | is_predT _ = false
bulwahn@33242
   488
bulwahn@33242
   489
(*** check if a term contains only constructor functions ***)
bulwahn@34935
   490
(* TODO: another copy in the core! *)
bulwahn@33623
   491
(* FIXME: constructor terms are supposed to be seen in the way the code generator
bulwahn@33623
   492
  sees constructors.*)
bulwahn@33242
   493
fun is_constrt thy =
bulwahn@33242
   494
  let
bulwahn@33242
   495
    val cnstrs = flat (maps
bulwahn@33242
   496
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
bulwahn@33242
   497
      (Symtab.dest (Datatype.get_all thy)));
bulwahn@33242
   498
    fun check t = (case strip_comb t of
bulwahn@36026
   499
        (Var _, []) => true
bulwahn@36026
   500
      | (Free _, []) => true
bulwahn@33242
   501
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
bulwahn@33242
   502
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
bulwahn@33242
   503
          | _ => false)
bulwahn@33242
   504
      | _ => false)
bulwahn@36026
   505
  in check end;
bulwahn@34935
   506
bulwahn@34935
   507
fun is_funtype (Type ("fun", [_, _])) = true
bulwahn@34935
   508
  | is_funtype _ = false;
bulwahn@34935
   509
bulwahn@34935
   510
fun is_Type (Type _) = true
bulwahn@34935
   511
  | is_Type _ = false
bulwahn@34935
   512
bulwahn@34935
   513
(* returns true if t is an application of an datatype constructor *)
bulwahn@34935
   514
(* which then consequently would be splitted *)
bulwahn@34935
   515
(* else false *)
bulwahn@34935
   516
(*
bulwahn@34935
   517
fun is_constructor thy t =
bulwahn@34935
   518
  if (is_Type (fastype_of t)) then
bulwahn@34935
   519
    (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
bulwahn@34935
   520
      NONE => false
bulwahn@34935
   521
    | SOME info => (let
bulwahn@34935
   522
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
bulwahn@34935
   523
      val (c, _) = strip_comb t
bulwahn@34935
   524
      in (case c of
bulwahn@34935
   525
        Const (name, _) => name mem_string constr_consts
bulwahn@34935
   526
        | _ => false) end))
bulwahn@34935
   527
  else false
bulwahn@34935
   528
*)
bulwahn@34935
   529
wenzelm@43232
   530
val is_constr = Code.is_constr o Proof_Context.theory_of;
bulwahn@34935
   531
bulwahn@36047
   532
fun strip_all t = (Term.strip_all_vars t, Term.strip_all_body t)
bulwahn@36047
   533
haftmann@38783
   534
fun strip_ex (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
bulwahn@33242
   535
  let
bulwahn@33242
   536
    val (xTs, t') = strip_ex t
bulwahn@33242
   537
  in
bulwahn@33242
   538
    ((x, T) :: xTs, t')
bulwahn@33242
   539
  end
bulwahn@33242
   540
  | strip_ex t = ([], t)
bulwahn@33242
   541
bulwahn@33242
   542
fun focus_ex t nctxt =
bulwahn@33242
   543
  let
bulwahn@33242
   544
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
wenzelm@44208
   545
    val (xs', nctxt') = fold_map Name.variant xs nctxt;
bulwahn@33242
   546
    val ps' = xs' ~~ Ts;
bulwahn@33242
   547
    val vs = map Free ps';
bulwahn@33242
   548
    val t'' = Term.subst_bounds (rev vs, t');
bulwahn@33242
   549
  in ((ps', t''), nctxt') end;
bulwahn@33242
   550
bulwahn@40347
   551
val strip_intro_concl = (strip_comb o HOLogic.dest_Trueprop o Logic.strip_imp_concl o prop_of)
bulwahn@40347
   552
  
bulwahn@33242
   553
(* introduction rule combinators *)
bulwahn@33242
   554
bulwahn@33242
   555
fun map_atoms f intro = 
bulwahn@33242
   556
  let
bulwahn@33242
   557
    val (literals, head) = Logic.strip_horn intro
bulwahn@33242
   558
    fun appl t = (case t of
bulwahn@35885
   559
        (@{term Not} $ t') => HOLogic.mk_not (f t')
bulwahn@33242
   560
      | _ => f t)
bulwahn@33242
   561
  in
bulwahn@33242
   562
    Logic.list_implies
bulwahn@33242
   563
      (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
bulwahn@33242
   564
  end
bulwahn@33242
   565
bulwahn@33242
   566
fun fold_atoms f intro s =
bulwahn@33242
   567
  let
bulwahn@33242
   568
    val (literals, head) = Logic.strip_horn intro
bulwahn@33242
   569
    fun appl t s = (case t of
bulwahn@35885
   570
      (@{term Not} $ t') => f t' s
bulwahn@33242
   571
      | _ => f t s)
bulwahn@33242
   572
  in fold appl (map HOLogic.dest_Trueprop literals) s end
bulwahn@33242
   573
bulwahn@33242
   574
fun fold_map_atoms f intro s =
bulwahn@33242
   575
  let
bulwahn@33242
   576
    val (literals, head) = Logic.strip_horn intro
bulwahn@33242
   577
    fun appl t s = (case t of
bulwahn@35885
   578
      (@{term Not} $ t') => apfst HOLogic.mk_not (f t' s)
bulwahn@33242
   579
      | _ => f t s)
bulwahn@33242
   580
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
bulwahn@33242
   581
  in
bulwahn@33242
   582
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
bulwahn@33242
   583
  end;
bulwahn@33242
   584
bulwahn@36246
   585
fun map_premises f intro =
bulwahn@36246
   586
  let
bulwahn@36246
   587
    val (premises, head) = Logic.strip_horn intro
bulwahn@36246
   588
  in
bulwahn@36246
   589
    Logic.list_implies (map f premises, head)
bulwahn@36246
   590
  end
bulwahn@36246
   591
bulwahn@36246
   592
fun map_filter_premises f intro =
bulwahn@36246
   593
  let
bulwahn@36246
   594
    val (premises, head) = Logic.strip_horn intro
bulwahn@36246
   595
  in
bulwahn@36246
   596
    Logic.list_implies (map_filter f premises, head)
bulwahn@36246
   597
  end
bulwahn@36246
   598
bulwahn@33242
   599
fun maps_premises f intro =
bulwahn@33242
   600
  let
bulwahn@33242
   601
    val (premises, head) = Logic.strip_horn intro
bulwahn@33242
   602
  in
bulwahn@33242
   603
    Logic.list_implies (maps f premises, head)
bulwahn@33242
   604
  end
bulwahn@35324
   605
bulwahn@35875
   606
fun map_concl f intro =
bulwahn@35875
   607
  let
bulwahn@35875
   608
    val (premises, head) = Logic.strip_horn intro
bulwahn@35875
   609
  in
bulwahn@35875
   610
    Logic.list_implies (premises, f head)
bulwahn@35875
   611
  end
bulwahn@35875
   612
bulwahn@35875
   613
(* combinators to apply a function to all basic parts of nested products *)
bulwahn@35875
   614
haftmann@37366
   615
fun map_products f (Const (@{const_name Pair}, T) $ t1 $ t2) =
haftmann@37366
   616
  Const (@{const_name Pair}, T) $ map_products f t1 $ map_products f t2
bulwahn@35875
   617
  | map_products f t = f t
bulwahn@35324
   618
bulwahn@35324
   619
(* split theorems of case expressions *)
bulwahn@35324
   620
bulwahn@35324
   621
fun prepare_split_thm ctxt split_thm =
bulwahn@35324
   622
    (split_thm RS @{thm iffD2})
wenzelm@35624
   623
    |> Local_Defs.unfold ctxt [@{thm atomize_conjL[symmetric]},
bulwahn@35324
   624
      @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
bulwahn@35324
   625
bulwahn@36023
   626
fun find_split_thm thy (Const (name, T)) = Option.map #split (Datatype_Data.info_of_case thy name)
bulwahn@36023
   627
  | find_split_thm thy _ = NONE
bulwahn@35324
   628
bulwahn@33242
   629
(* lifting term operations to theorems *)
bulwahn@33242
   630
bulwahn@33242
   631
fun map_term thy f th =
bulwahn@33242
   632
  Skip_Proof.make_thm thy (f (prop_of th))
bulwahn@33242
   633
bulwahn@33242
   634
(*
bulwahn@33242
   635
fun equals_conv lhs_cv rhs_cv ct =
bulwahn@33242
   636
  case Thm.term_of ct of
bulwahn@33242
   637
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
bulwahn@33242
   638
  | _ => error "equals_conv"  
bulwahn@33242
   639
*)
bulwahn@33242
   640
bulwahn@36032
   641
(* Different compilations *)
bulwahn@33242
   642
bulwahn@35881
   643
datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
bulwahn@40232
   644
  | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq |
bulwahn@40232
   645
    Pos_Generator_DSeq | Neg_Generator_DSeq
bulwahn@35324
   646
bulwahn@35324
   647
fun negative_compilation_of Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@35324
   648
  | negative_compilation_of Neg_Random_DSeq = Pos_Random_DSeq
bulwahn@36012
   649
  | negative_compilation_of New_Pos_Random_DSeq = New_Neg_Random_DSeq
bulwahn@36012
   650
  | negative_compilation_of New_Neg_Random_DSeq = New_Pos_Random_DSeq
bulwahn@40232
   651
  | negative_compilation_of Pos_Generator_DSeq = Neg_Generator_DSeq
bulwahn@40232
   652
  | negative_compilation_of Pos_Generator_DSeq = Pos_Generator_DSeq
bulwahn@35324
   653
  | negative_compilation_of c = c
bulwahn@35324
   654
  
bulwahn@35324
   655
fun compilation_for_polarity false Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@36012
   656
  | compilation_for_polarity false New_Pos_Random_DSeq = New_Neg_Random_DSeq
bulwahn@35324
   657
  | compilation_for_polarity _ c = c
bulwahn@34935
   658
bulwahn@40230
   659
fun is_depth_limited_compilation c =
bulwahn@40232
   660
  (c = New_Pos_Random_DSeq) orelse (c = New_Neg_Random_DSeq) orelse
bulwahn@40232
   661
  (c = Pos_Generator_DSeq) orelse (c = Pos_Generator_DSeq)
bulwahn@40230
   662
bulwahn@35885
   663
fun string_of_compilation c =
bulwahn@35885
   664
  case c of
bulwahn@34935
   665
    Pred => ""
bulwahn@34935
   666
  | Random => "random"
bulwahn@34935
   667
  | Depth_Limited => "depth limited"
bulwahn@35881
   668
  | Depth_Limited_Random => "depth limited random"
bulwahn@34935
   669
  | DSeq => "dseq"
bulwahn@34935
   670
  | Annotated => "annotated"
bulwahn@35324
   671
  | Pos_Random_DSeq => "pos_random dseq"
bulwahn@35324
   672
  | Neg_Random_DSeq => "neg_random_dseq"
bulwahn@36012
   673
  | New_Pos_Random_DSeq => "new_pos_random dseq"
bulwahn@36012
   674
  | New_Neg_Random_DSeq => "new_neg_random_dseq"
bulwahn@40232
   675
  | Pos_Generator_DSeq => "pos_generator_dseq"
bulwahn@40232
   676
  | Neg_Generator_DSeq => "neg_generator_dseq"
bulwahn@36032
   677
bulwahn@36012
   678
val compilation_names = [("pred", Pred),
bulwahn@36012
   679
  ("random", Random),
bulwahn@36012
   680
  ("depth_limited", Depth_Limited),
bulwahn@36012
   681
  ("depth_limited_random", Depth_Limited_Random),
bulwahn@36012
   682
  (*("annotated", Annotated),*)
bulwahn@40235
   683
  ("dseq", DSeq),
bulwahn@40235
   684
  ("random_dseq", Pos_Random_DSeq),
bulwahn@40232
   685
  ("new_random_dseq", New_Pos_Random_DSeq),
bulwahn@40232
   686
  ("generator_dseq", Pos_Generator_DSeq)]
bulwahn@36032
   687
bulwahn@36032
   688
val non_random_compilations = [Pred, Depth_Limited, DSeq, Annotated]
bulwahn@36032
   689
bulwahn@36032
   690
bulwahn@36032
   691
val random_compilations = [Random, Depth_Limited_Random,
bulwahn@36032
   692
  Pos_Random_DSeq, Neg_Random_DSeq, New_Pos_Random_DSeq, New_Neg_Random_DSeq]
bulwahn@36032
   693
bulwahn@36046
   694
(* datastructures and setup for generic compilation *)
bulwahn@36046
   695
bulwahn@36046
   696
datatype compilation_funs = CompilationFuns of {
bulwahn@36046
   697
  mk_predT : typ -> typ,
bulwahn@36046
   698
  dest_predT : typ -> typ,
bulwahn@36046
   699
  mk_bot : typ -> term,
bulwahn@36046
   700
  mk_single : term -> term,
bulwahn@36046
   701
  mk_bind : term * term -> term,
bulwahn@36046
   702
  mk_sup : term * term -> term,
bulwahn@36046
   703
  mk_if : term -> term,
bulwahn@36049
   704
  mk_iterate_upto : typ -> term * term * term -> term,
bulwahn@36046
   705
  mk_not : term -> term,
bulwahn@36046
   706
  mk_map : typ -> typ -> term -> term -> term
bulwahn@36046
   707
};
bulwahn@36046
   708
bulwahn@36046
   709
fun mk_predT (CompilationFuns funs) = #mk_predT funs
bulwahn@36046
   710
fun dest_predT (CompilationFuns funs) = #dest_predT funs
bulwahn@36046
   711
fun mk_bot (CompilationFuns funs) = #mk_bot funs
bulwahn@36046
   712
fun mk_single (CompilationFuns funs) = #mk_single funs
bulwahn@36046
   713
fun mk_bind (CompilationFuns funs) = #mk_bind funs
bulwahn@36046
   714
fun mk_sup (CompilationFuns funs) = #mk_sup funs
bulwahn@36046
   715
fun mk_if (CompilationFuns funs) = #mk_if funs
bulwahn@36049
   716
fun mk_iterate_upto (CompilationFuns funs) = #mk_iterate_upto funs
bulwahn@36046
   717
fun mk_not (CompilationFuns funs) = #mk_not funs
bulwahn@36046
   718
fun mk_map (CompilationFuns funs) = #mk_map funs
bulwahn@36046
   719
bulwahn@36046
   720
(** function types and names of different compilations **)
bulwahn@36046
   721
bulwahn@36046
   722
fun funT_of compfuns mode T =
bulwahn@36046
   723
  let
bulwahn@36046
   724
    val Ts = binder_types T
bulwahn@36046
   725
    val (inTs, outTs) = split_map_modeT (fn m => fn T => (SOME (funT_of compfuns m T), NONE)) mode Ts
bulwahn@36046
   726
  in
bulwahn@36046
   727
    inTs ---> (mk_predT compfuns (HOLogic.mk_tupleT outTs))
bulwahn@36046
   728
  end;
bulwahn@36046
   729
bulwahn@36032
   730
(* Different options for compiler *)
bulwahn@36032
   731
bulwahn@33242
   732
datatype options = Options of {  
bulwahn@34935
   733
  expected_modes : (string * mode list) option,
bulwahn@39628
   734
  proposed_modes : (string * mode list) list,
bulwahn@34935
   735
  proposed_names : ((string * mode) * string) list,
bulwahn@33242
   736
  show_steps : bool,
bulwahn@33242
   737
  show_proof_trace : bool,
bulwahn@33242
   738
  show_intermediate_results : bool,
bulwahn@33243
   739
  show_mode_inference : bool,
bulwahn@33243
   740
  show_modes : bool,
bulwahn@33242
   741
  show_compilation : bool,
bulwahn@35324
   742
  show_caught_failures : bool,
bulwahn@39629
   743
  show_invalid_clauses : bool,
bulwahn@33242
   744
  skip_proof : bool,
bulwahn@35324
   745
  no_topmost_reordering : bool,
bulwahn@35324
   746
  function_flattening : bool,
bulwahn@36248
   747
  specialise : bool,
bulwahn@35324
   748
  fail_safe_function_flattening : bool,
bulwahn@35324
   749
  no_higher_order_predicate : string list,
bulwahn@33242
   750
  inductify : bool,
bulwahn@36254
   751
  detect_switches : bool,
bulwahn@40229
   752
  smart_depth_limiting : bool,
bulwahn@34935
   753
  compilation : compilation
bulwahn@33242
   754
};
bulwahn@33242
   755
bulwahn@33242
   756
fun expected_modes (Options opt) = #expected_modes opt
bulwahn@39628
   757
fun proposed_modes (Options opt) = AList.lookup (op =) (#proposed_modes opt)
bulwahn@34935
   758
fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode)
bulwahn@33623
   759
  (#proposed_names opt) (name, mode)
bulwahn@33620
   760
bulwahn@33242
   761
fun show_steps (Options opt) = #show_steps opt
bulwahn@33242
   762
fun show_intermediate_results (Options opt) = #show_intermediate_results opt
bulwahn@33242
   763
fun show_proof_trace (Options opt) = #show_proof_trace opt
bulwahn@33243
   764
fun show_modes (Options opt) = #show_modes opt
bulwahn@33243
   765
fun show_mode_inference (Options opt) = #show_mode_inference opt
bulwahn@33242
   766
fun show_compilation (Options opt) = #show_compilation opt
bulwahn@35324
   767
fun show_caught_failures (Options opt) = #show_caught_failures opt
bulwahn@39629
   768
fun show_invalid_clauses (Options opt) = #show_invalid_clauses opt
bulwahn@33242
   769
fun skip_proof (Options opt) = #skip_proof opt
bulwahn@33242
   770
bulwahn@35324
   771
fun function_flattening (Options opt) = #function_flattening opt
bulwahn@35324
   772
fun fail_safe_function_flattening (Options opt) = #fail_safe_function_flattening opt
bulwahn@36248
   773
fun specialise (Options opt) = #specialise opt
bulwahn@35324
   774
fun no_topmost_reordering (Options opt) = #no_topmost_reordering opt
bulwahn@35324
   775
fun no_higher_order_predicate (Options opt) = #no_higher_order_predicate opt
bulwahn@35324
   776
bulwahn@33242
   777
fun is_inductify (Options opt) = #inductify opt
bulwahn@34935
   778
bulwahn@34935
   779
fun compilation (Options opt) = #compilation opt
bulwahn@33242
   780
bulwahn@36254
   781
fun detect_switches (Options opt) = #detect_switches opt
bulwahn@36254
   782
bulwahn@40229
   783
fun smart_depth_limiting (Options opt) = #smart_depth_limiting opt
bulwahn@40229
   784
bulwahn@33242
   785
val default_options = Options {
bulwahn@33242
   786
  expected_modes = NONE,
bulwahn@39628
   787
  proposed_modes = [],
bulwahn@33623
   788
  proposed_names = [],
bulwahn@33242
   789
  show_steps = false,
bulwahn@33242
   790
  show_intermediate_results = false,
bulwahn@33242
   791
  show_proof_trace = false,
bulwahn@33243
   792
  show_modes = false,
bulwahn@33242
   793
  show_mode_inference = false,
bulwahn@33242
   794
  show_compilation = false,
bulwahn@35324
   795
  show_caught_failures = false,
bulwahn@39629
   796
  show_invalid_clauses = false,
bulwahn@34935
   797
  skip_proof = true,
bulwahn@35324
   798
  no_topmost_reordering = false,
bulwahn@35324
   799
  function_flattening = false,
bulwahn@36248
   800
  specialise = false,
bulwahn@35324
   801
  fail_safe_function_flattening = false,
bulwahn@35324
   802
  no_higher_order_predicate = [],
bulwahn@33242
   803
  inductify = false,
bulwahn@36254
   804
  detect_switches = true,
bulwahn@40229
   805
  smart_depth_limiting = false,
bulwahn@34935
   806
  compilation = Pred
bulwahn@33242
   807
}
bulwahn@33242
   808
bulwahn@34935
   809
val bool_options = ["show_steps", "show_intermediate_results", "show_proof_trace", "show_modes",
bulwahn@39629
   810
  "show_mode_inference", "show_compilation", "show_invalid_clauses", "skip_proof", "inductify",
bulwahn@40229
   811
  "no_function_flattening", "detect_switches", "specialise", "no_topmost_reordering",
bulwahn@40229
   812
  "smart_depth_limiting"]
bulwahn@34935
   813
bulwahn@33242
   814
fun print_step options s =
bulwahn@33242
   815
  if show_steps options then tracing s else ()
bulwahn@33242
   816
bulwahn@36047
   817
(* simple transformations *)
bulwahn@36047
   818
bulwahn@36047
   819
(** tuple processing **)
bulwahn@33242
   820
bulwahn@39881
   821
fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
bulwahn@39881
   822
  | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
bulwahn@39881
   823
    (case HOLogic.strip_tupleT (fastype_of arg) of
bulwahn@39881
   824
      (Ts as _ :: _ :: _) =>
bulwahn@39881
   825
      let
bulwahn@39881
   826
        fun rewrite_arg' (Const (@{const_name Pair}, _) $ _ $ t2, Type (@{type_name Product_Type.prod}, [_, T2]))
bulwahn@39881
   827
          (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
bulwahn@39881
   828
          | rewrite_arg' (t, Type (@{type_name Product_Type.prod}, [T1, T2])) (args, (pats, intro_t, ctxt)) =
bulwahn@39881
   829
            let
wenzelm@43232
   830
              val thy = Proof_Context.theory_of ctxt
bulwahn@39881
   831
              val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
bulwahn@39881
   832
              val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
bulwahn@39881
   833
              val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
bulwahn@39881
   834
              val args' = map (Pattern.rewrite_term thy [pat] []) args
bulwahn@39881
   835
            in
bulwahn@39881
   836
              rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
bulwahn@39881
   837
            end
bulwahn@39881
   838
          | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
bulwahn@39881
   839
        val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
bulwahn@39881
   840
          (args, (pats, intro_t, ctxt))
bulwahn@39881
   841
      in
bulwahn@39881
   842
        rewrite_args args' (pats, intro_t', ctxt')
bulwahn@39881
   843
      end
bulwahn@39881
   844
  | _ => rewrite_args args (pats, intro_t, ctxt))
bulwahn@39881
   845
bulwahn@39881
   846
fun rewrite_prem atom =
bulwahn@39881
   847
  let
bulwahn@39881
   848
    val (_, args) = strip_comb atom
bulwahn@39881
   849
  in rewrite_args args end
bulwahn@39881
   850
bulwahn@40020
   851
fun split_conjuncts_in_assms ctxt th =
bulwahn@40020
   852
  let
bulwahn@40020
   853
    val ((_, [fixed_th]), ctxt') = Variable.import false [th] ctxt 
bulwahn@40020
   854
    fun split_conjs i nprems th =
bulwahn@40020
   855
      if i > nprems then th
bulwahn@40020
   856
      else
bulwahn@40020
   857
        case try Drule.RSN (@{thm conjI}, (i, th)) of
bulwahn@40020
   858
          SOME th' => split_conjs i (nprems+1) th'
bulwahn@40020
   859
        | NONE => split_conjs (i+1) nprems th
bulwahn@40020
   860
  in
bulwahn@40020
   861
    singleton (Variable.export ctxt' ctxt) (split_conjs 1 (Thm.nprems_of fixed_th) fixed_th)
bulwahn@40020
   862
  end
bulwahn@40233
   863
bulwahn@40233
   864
fun dest_conjunct_prem th =
bulwahn@40233
   865
  case HOLogic.dest_Trueprop (prop_of th) of
bulwahn@40233
   866
    (Const (@{const_name HOL.conj}, _) $ t $ t') =>
bulwahn@40233
   867
      dest_conjunct_prem (th RS @{thm conjunct1})
bulwahn@40233
   868
        @ dest_conjunct_prem (th RS @{thm conjunct2})
bulwahn@40233
   869
    | _ => [th]
bulwahn@40233
   870
bulwahn@33242
   871
fun expand_tuples thy intro =
bulwahn@33242
   872
  let
wenzelm@43232
   873
    val ctxt = Proof_Context.init_global thy
bulwahn@33242
   874
    val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
bulwahn@33242
   875
    val intro_t = prop_of intro'
bulwahn@33242
   876
    val concl = Logic.strip_imp_concl intro_t
bulwahn@33242
   877
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
bulwahn@33242
   878
    val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
bulwahn@33242
   879
    val (pats', intro_t', ctxt3) = 
bulwahn@33242
   880
      fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
bulwahn@33242
   881
    fun rewrite_pat (ct1, ct2) =
bulwahn@33242
   882
      (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
bulwahn@33242
   883
    val t_insts' = map rewrite_pat t_insts
bulwahn@33242
   884
    val intro'' = Thm.instantiate (T_insts, t_insts') intro
bulwahn@33242
   885
    val [intro'''] = Variable.export ctxt3 ctxt [intro'']
bulwahn@33242
   886
    val intro'''' = Simplifier.full_simplify
bulwahn@33242
   887
      (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
bulwahn@33242
   888
      intro'''
bulwahn@33242
   889
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@40020
   890
    val intro''''' = split_conjuncts_in_assms ctxt intro''''
bulwahn@33242
   891
  in
bulwahn@33242
   892
    intro'''''
bulwahn@33242
   893
  end
bulwahn@33242
   894
bulwahn@40048
   895
(** making case distributivity rules **)
bulwahn@40048
   896
(*** this should be part of the datatype package ***)
bulwahn@40048
   897
bulwahn@40048
   898
fun datatype_names_of_case_name thy case_name =
bulwahn@40048
   899
  map (#1 o #2) (#descr (the (Datatype_Data.info_of_case thy case_name)))
bulwahn@40048
   900
bulwahn@40048
   901
fun make_case_distribs new_type_names descr sorts thy =
bulwahn@40048
   902
  let
bulwahn@40048
   903
    val case_combs = Datatype_Prop.make_case_combs new_type_names descr sorts thy "f";
bulwahn@40048
   904
    fun make comb =
bulwahn@40048
   905
      let
bulwahn@40048
   906
        val Type ("fun", [T, T']) = fastype_of comb;
bulwahn@40048
   907
        val (Const (case_name, _), fs) = strip_comb comb
bulwahn@40048
   908
        val used = Term.add_tfree_names comb []
wenzelm@44206
   909
        val U = TFree (singleton (Name.variant_list used) "'t", HOLogic.typeS)
bulwahn@40048
   910
        val x = Free ("x", T)
bulwahn@40048
   911
        val f = Free ("f", T' --> U)
bulwahn@40048
   912
        fun apply_f f' =
bulwahn@40048
   913
          let
bulwahn@40048
   914
            val Ts = binder_types (fastype_of f')
bulwahn@40048
   915
            val bs = map Bound ((length Ts - 1) downto 0)
bulwahn@40048
   916
          in
bulwahn@40048
   917
            fold (curry absdummy) (rev Ts) (f $ (list_comb (f', bs)))
bulwahn@40048
   918
          end
bulwahn@40048
   919
        val fs' = map apply_f fs
bulwahn@40048
   920
        val case_c' = Const (case_name, (map fastype_of fs') @ [T] ---> U)
bulwahn@40048
   921
      in
bulwahn@40048
   922
        HOLogic.mk_eq (f $ (comb $ x), list_comb (case_c', fs') $ x)
bulwahn@40048
   923
      end
bulwahn@40048
   924
  in
bulwahn@40048
   925
    map make case_combs
bulwahn@40048
   926
  end
bulwahn@40048
   927
bulwahn@40048
   928
fun case_rewrites thy Tcon =
bulwahn@40048
   929
  let
bulwahn@40048
   930
    val info = Datatype.the_info thy Tcon
bulwahn@40048
   931
    val descr = #descr info
bulwahn@40048
   932
    val sorts = #sorts info
bulwahn@40048
   933
    val typ_names = the_default [Tcon] (#alt_names info)
bulwahn@40048
   934
  in
bulwahn@40048
   935
    map (Drule.export_without_context o Skip_Proof.make_thm thy o HOLogic.mk_Trueprop)
bulwahn@40048
   936
      (make_case_distribs typ_names [descr] sorts thy)
bulwahn@40048
   937
  end
bulwahn@40048
   938
bulwahn@40048
   939
fun instantiated_case_rewrites thy Tcon =
bulwahn@40048
   940
  let
bulwahn@40048
   941
    val rew_ths = case_rewrites thy Tcon
wenzelm@43232
   942
    val ctxt = Proof_Context.init_global thy
bulwahn@40048
   943
    fun instantiate th =
bulwahn@40048
   944
    let
bulwahn@40048
   945
      val f = (fst (strip_comb (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of th))))))
bulwahn@40048
   946
      val Type ("fun", [uninst_T, uninst_T']) = fastype_of f
bulwahn@40048
   947
      val ([tname, tname', uname, yname], ctxt') = Variable.add_fixes ["'t", "'t'", "'u", "y"] ctxt
bulwahn@40048
   948
      val T = TFree (tname, HOLogic.typeS)
bulwahn@40048
   949
      val T' = TFree (tname', HOLogic.typeS)
bulwahn@40048
   950
      val U = TFree (uname, HOLogic.typeS)
bulwahn@40048
   951
      val y = Free (yname, U)
bulwahn@40048
   952
      val f' = absdummy (U --> T', Bound 0 $ y)
bulwahn@40048
   953
      val th' = Thm.certify_instantiate
bulwahn@40048
   954
        ([(dest_TVar uninst_T, U --> T'), (dest_TVar uninst_T', T')],
bulwahn@40048
   955
         [((fst (dest_Var f), (U --> T') --> T'), f')]) th
bulwahn@40048
   956
      val [th'] = Variable.export ctxt' ctxt [th']
bulwahn@40048
   957
   in
bulwahn@40048
   958
     th'
bulwahn@40048
   959
   end
bulwahn@40048
   960
 in
bulwahn@40048
   961
   map instantiate rew_ths
bulwahn@40048
   962
 end
bulwahn@40048
   963
bulwahn@40048
   964
fun case_betapply thy t =
bulwahn@40048
   965
  let
bulwahn@40048
   966
    val case_name = fst (dest_Const (fst (strip_comb t)))
bulwahn@40048
   967
    val Tcons = datatype_names_of_case_name thy case_name
bulwahn@40048
   968
    val ths = maps (instantiated_case_rewrites thy) Tcons
bulwahn@40048
   969
  in
wenzelm@41494
   970
    Raw_Simplifier.rewrite_term thy
bulwahn@40048
   971
      (map (fn th => th RS @{thm eq_reflection}) ths) [] t
bulwahn@40048
   972
  end
bulwahn@40048
   973
bulwahn@39881
   974
(*** conversions ***)
bulwahn@39881
   975
bulwahn@39881
   976
fun imp_prems_conv cv ct =
bulwahn@39881
   977
  case Thm.term_of ct of
bulwahn@39881
   978
    Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
bulwahn@39881
   979
  | _ => Conv.all_conv ct
bulwahn@39881
   980
bulwahn@39881
   981
fun all_params_conv cv ctxt ct =
bulwahn@39881
   982
  if Logic.is_all (Thm.term_of ct)
bulwahn@39881
   983
  then Conv.arg_conv (Conv.abs_conv (all_params_conv cv o #2) ctxt) ct
bulwahn@39881
   984
  else cv ctxt ct;
bulwahn@39881
   985
  
bulwahn@36047
   986
(** eta contract higher-order arguments **)
bulwahn@35875
   987
bulwahn@35875
   988
fun eta_contract_ho_arguments thy intro =
bulwahn@35875
   989
  let
bulwahn@35875
   990
    fun f atom = list_comb (apsnd ((map o map_products) Envir.eta_contract) (strip_comb atom))
bulwahn@35875
   991
  in
bulwahn@35875
   992
    map_term thy (map_concl f o map_atoms f) intro
bulwahn@35875
   993
  end
bulwahn@35875
   994
bulwahn@36047
   995
(** remove equalities **)
bulwahn@36016
   996
bulwahn@36016
   997
fun remove_equalities thy intro =
bulwahn@36016
   998
  let
bulwahn@36016
   999
    fun remove_eqs intro_t =
bulwahn@36016
  1000
      let
bulwahn@36016
  1001
        val (prems, concl) = Logic.strip_horn intro_t
bulwahn@36016
  1002
        fun remove_eq (prems, concl) =
bulwahn@36016
  1003
          let
bulwahn@36016
  1004
            fun removable_eq prem =
bulwahn@36016
  1005
              case try (HOLogic.dest_eq o HOLogic.dest_Trueprop) prem of
bulwahn@36016
  1006
                SOME (lhs, rhs) => (case lhs of
bulwahn@36016
  1007
                  Var _ => true
bulwahn@36016
  1008
                  | _ => (case rhs of Var _ => true | _ => false))
bulwahn@36016
  1009
              | NONE => false
bulwahn@36016
  1010
          in
bulwahn@36016
  1011
            case find_first removable_eq prems of
bulwahn@36016
  1012
              NONE => (prems, concl)
bulwahn@36016
  1013
            | SOME eq =>
bulwahn@36016
  1014
              let
bulwahn@36016
  1015
                val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
bulwahn@36016
  1016
                val prems' = remove (op =) eq prems
bulwahn@36016
  1017
                val subst = (case lhs of
bulwahn@36016
  1018
                  (v as Var _) =>
bulwahn@36016
  1019
                    (fn t => if t = v then rhs else t)
bulwahn@36016
  1020
                | _ => (case rhs of
bulwahn@36016
  1021
                   (v as Var _) => (fn t => if t = v then lhs else t)))
bulwahn@36016
  1022
              in
bulwahn@36016
  1023
                remove_eq (map (map_aterms subst) prems', map_aterms subst concl)
bulwahn@36016
  1024
              end
bulwahn@36016
  1025
          end
bulwahn@36016
  1026
      in
bulwahn@36016
  1027
        Logic.list_implies (remove_eq (prems, concl))
bulwahn@36016
  1028
      end
bulwahn@36016
  1029
  in
bulwahn@36016
  1030
    map_term thy remove_eqs intro
bulwahn@36016
  1031
  end
bulwahn@35875
  1032
bulwahn@36246
  1033
(* Some last processing *)
bulwahn@36246
  1034
bulwahn@36246
  1035
fun remove_pointless_clauses intro =
bulwahn@36246
  1036
  if Logic.strip_imp_prems (prop_of intro) = [@{prop "False"}] then
bulwahn@36246
  1037
    []
bulwahn@36246
  1038
  else [intro]
bulwahn@36246
  1039
bulwahn@36246
  1040
(* some peephole optimisations *)
bulwahn@36246
  1041
bulwahn@36246
  1042
fun peephole_optimisation thy intro =
bulwahn@36246
  1043
  let
wenzelm@36633
  1044
    val process =
wenzelm@43232
  1045
      Raw_Simplifier.rewrite_rule (Predicate_Compile_Simps.get (Proof_Context.init_global thy))
bulwahn@36246
  1046
    fun process_False intro_t =
bulwahn@36246
  1047
      if member (op =) (Logic.strip_imp_prems intro_t) @{prop "False"} then NONE else SOME intro_t
bulwahn@36246
  1048
    fun process_True intro_t =
bulwahn@36246
  1049
      map_filter_premises (fn p => if p = @{prop True} then NONE else SOME p) intro_t
bulwahn@36246
  1050
  in
bulwahn@36246
  1051
    Option.map (Skip_Proof.make_thm thy)
bulwahn@36246
  1052
      (process_False (process_True (prop_of (process intro))))
bulwahn@36246
  1053
  end
bulwahn@36246
  1054
bulwahn@40347
  1055
bulwahn@40347
  1056
(* importing introduction rules *)
bulwahn@40347
  1057
bulwahn@40347
  1058
fun import_intros inp_pred [] ctxt =
bulwahn@40347
  1059
  let
bulwahn@40347
  1060
    val ([outp_pred], ctxt') = Variable.import_terms true [inp_pred] ctxt
bulwahn@40347
  1061
    val T = fastype_of outp_pred
bulwahn@40347
  1062
    val paramTs = ho_argsT_of_typ (binder_types T)
bulwahn@40347
  1063
    val (param_names, ctxt'') = Variable.variant_fixes
bulwahn@40347
  1064
      (map (fn i => "p" ^ (string_of_int i)) (1 upto (length paramTs))) ctxt'
bulwahn@40347
  1065
    val params = map2 (curry Free) param_names paramTs
bulwahn@40347
  1066
  in
bulwahn@40347
  1067
    (((outp_pred, params), []), ctxt')
bulwahn@40347
  1068
  end
bulwahn@40347
  1069
  | import_intros inp_pred (th :: ths) ctxt =
bulwahn@40347
  1070
    let
bulwahn@40347
  1071
      val ((_, [th']), ctxt') = Variable.import true [th] ctxt
wenzelm@43232
  1072
      val thy = Proof_Context.theory_of ctxt'
bulwahn@40347
  1073
      val (pred, args) = strip_intro_concl th'
bulwahn@40347
  1074
      val T = fastype_of pred
bulwahn@40347
  1075
      val ho_args = ho_args_of_typ T args
bulwahn@40347
  1076
      fun subst_of (pred', pred) =
bulwahn@40347
  1077
        let
bulwahn@40347
  1078
          val subst = Sign.typ_match thy (fastype_of pred', fastype_of pred) Vartab.empty
bulwahn@40347
  1079
            handle Type.TYPE_MATCH => error ("Type mismatch of predicate " ^ fst (dest_Const pred)
bulwahn@40347
  1080
            ^ " (trying to match " ^ Syntax.string_of_typ ctxt (fastype_of pred')
bulwahn@40347
  1081
            ^ " and " ^ Syntax.string_of_typ ctxt (fastype_of pred) ^ ")"
bulwahn@40347
  1082
            ^ " in " ^ Display.string_of_thm ctxt th)
bulwahn@40347
  1083
        in map (fn (indexname, (s, T)) => ((indexname, s), T)) (Vartab.dest subst) end
bulwahn@40347
  1084
      fun instantiate_typ th =
bulwahn@40347
  1085
        let
bulwahn@40347
  1086
          val (pred', _) = strip_intro_concl th
bulwahn@40347
  1087
          val _ = if not (fst (dest_Const pred) = fst (dest_Const pred')) then
bulwahn@40347
  1088
            raise Fail "Trying to instantiate another predicate" else ()
bulwahn@40347
  1089
        in Thm.certify_instantiate (subst_of (pred', pred), []) th end;
bulwahn@40347
  1090
      fun instantiate_ho_args th =
bulwahn@40347
  1091
        let
bulwahn@40347
  1092
          val (_, args') = (strip_comb o HOLogic.dest_Trueprop o Logic.strip_imp_concl o prop_of) th
bulwahn@40347
  1093
          val ho_args' = map dest_Var (ho_args_of_typ T args')
bulwahn@40347
  1094
        in Thm.certify_instantiate ([], ho_args' ~~ ho_args) th end
bulwahn@40347
  1095
      val outp_pred =
bulwahn@40347
  1096
        Term_Subst.instantiate (subst_of (inp_pred, pred), []) inp_pred
bulwahn@40347
  1097
      val ((_, ths'), ctxt1) =
bulwahn@40347
  1098
        Variable.import false (map (instantiate_typ #> instantiate_ho_args) ths) ctxt'
bulwahn@40347
  1099
    in
bulwahn@40347
  1100
      (((outp_pred, ho_args), th' :: ths'), ctxt1)
bulwahn@40347
  1101
    end
bulwahn@40347
  1102
  
bulwahn@40347
  1103
(* generation of case rules from user-given introduction rules *)
bulwahn@40347
  1104
bulwahn@40347
  1105
fun mk_args2 (Type (@{type_name Product_Type.prod}, [T1, T2])) st =
bulwahn@40347
  1106
    let
bulwahn@40347
  1107
      val (t1, st') = mk_args2 T1 st
bulwahn@40347
  1108
      val (t2, st'') = mk_args2 T2 st'
bulwahn@40347
  1109
    in
bulwahn@40347
  1110
      (HOLogic.mk_prod (t1, t2), st'')
bulwahn@40347
  1111
    end
bulwahn@40347
  1112
  (*| mk_args2 (T as Type ("fun", _)) (params, ctxt) = 
bulwahn@40347
  1113
    let
bulwahn@40347
  1114
      val (S, U) = strip_type T
bulwahn@40347
  1115
    in
bulwahn@40347
  1116
      if U = HOLogic.boolT then
bulwahn@40347
  1117
        (hd params, (tl params, ctxt))
bulwahn@40347
  1118
      else
bulwahn@40347
  1119
        let
bulwahn@40347
  1120
          val ([x], ctxt') = Variable.variant_fixes ["x"] ctxt
bulwahn@40347
  1121
        in
bulwahn@40347
  1122
          (Free (x, T), (params, ctxt'))
bulwahn@40347
  1123
        end
bulwahn@40347
  1124
    end*)
bulwahn@40347
  1125
  | mk_args2 T (params, ctxt) =
bulwahn@40347
  1126
    let
bulwahn@40347
  1127
      val ([x], ctxt') = Variable.variant_fixes ["x"] ctxt
bulwahn@40347
  1128
    in
bulwahn@40347
  1129
      (Free (x, T), (params, ctxt'))
bulwahn@40347
  1130
    end
bulwahn@40347
  1131
bulwahn@40347
  1132
fun mk_casesrule ctxt pred introrules =
bulwahn@40347
  1133
  let
bulwahn@40347
  1134
    (* TODO: can be simplified if parameters are not treated specially ? *)
bulwahn@40347
  1135
    val (((pred, params), intros_th), ctxt1) = import_intros pred introrules ctxt
bulwahn@40347
  1136
    (* TODO: distinct required ? -- test case with more than one parameter! *)
bulwahn@40347
  1137
    val params = distinct (op aconv) params
bulwahn@40347
  1138
    val intros = map prop_of intros_th
bulwahn@40347
  1139
    val ([propname], ctxt2) = Variable.variant_fixes ["thesis"] ctxt1
bulwahn@40347
  1140
    val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT))
bulwahn@40347
  1141
    val argsT = binder_types (fastype_of pred)
bulwahn@40347
  1142
    (* TODO: can be simplified if parameters are not treated specially ? <-- see uncommented code! *)
bulwahn@40347
  1143
    val (argvs, _) = fold_map mk_args2 argsT (params, ctxt2)
bulwahn@40347
  1144
    fun mk_case intro =
bulwahn@40347
  1145
      let
bulwahn@40347
  1146
        val (_, args) = (strip_comb o HOLogic.dest_Trueprop o Logic.strip_imp_concl) intro
bulwahn@40347
  1147
        val prems = Logic.strip_imp_prems intro
bulwahn@40347
  1148
        val eqprems =
bulwahn@40347
  1149
          map2 (HOLogic.mk_Trueprop oo (curry HOLogic.mk_eq)) argvs args
bulwahn@40347
  1150
        val frees = map Free (fold Term.add_frees (args @ prems) [])
bulwahn@40347
  1151
      in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end
bulwahn@40347
  1152
    val assm = HOLogic.mk_Trueprop (list_comb (pred, argvs))
bulwahn@40347
  1153
    val cases = map mk_case intros
bulwahn@40347
  1154
  in Logic.list_implies (assm :: cases, prop) end;
bulwahn@40347
  1155
  
bulwahn@40347
  1156
bulwahn@40347
  1157
(* unifying constants to have the same type variables *)
bulwahn@40347
  1158
bulwahn@40347
  1159
fun unify_consts thy cs intr_ts =
bulwahn@40347
  1160
  (let
bulwahn@40347
  1161
     val add_term_consts_2 = fold_aterms (fn Const c => insert (op =) c | _ => I);
bulwahn@40347
  1162
     fun varify (t, (i, ts)) =
bulwahn@40347
  1163
       let val t' = map_types (Logic.incr_tvar (i + 1)) (#2 (Type.varify_global [] t))
bulwahn@40347
  1164
       in (maxidx_of_term t', t'::ts) end;
bulwahn@40347
  1165
     val (i, cs') = List.foldr varify (~1, []) cs;
bulwahn@40347
  1166
     val (i', intr_ts') = List.foldr varify (i, []) intr_ts;
bulwahn@40347
  1167
     val rec_consts = fold add_term_consts_2 cs' [];
bulwahn@40347
  1168
     val intr_consts = fold add_term_consts_2 intr_ts' [];
bulwahn@40347
  1169
     fun unify (cname, cT) =
bulwahn@40347
  1170
       let val consts = map snd (filter (fn c => fst c = cname) intr_consts)
bulwahn@40347
  1171
       in fold (Sign.typ_unify thy) ((replicate (length consts) cT) ~~ consts) end;
bulwahn@40347
  1172
     val (env, _) = fold unify rec_consts (Vartab.empty, i');
bulwahn@40347
  1173
     val subst = map_types (Envir.norm_type env)
bulwahn@40347
  1174
   in (map subst cs', map subst intr_ts')
bulwahn@40347
  1175
   end) handle Type.TUNIFY =>
bulwahn@40347
  1176
     (warning "Occurrences of recursive constant have non-unifiable types"; (cs, intr_ts));
bulwahn@40347
  1177
bulwahn@40347
  1178
(* preprocessing rules *)
bulwahn@40347
  1179
bulwahn@40347
  1180
fun Trueprop_conv cv ct =
bulwahn@40347
  1181
  case Thm.term_of ct of
bulwahn@40347
  1182
    Const (@{const_name Trueprop}, _) $ _ => Conv.arg_conv cv ct  
bulwahn@40347
  1183
  | _ => raise Fail "Trueprop_conv"
bulwahn@40347
  1184
bulwahn@40347
  1185
fun preprocess_equality thy rule =
bulwahn@40347
  1186
  Conv.fconv_rule
bulwahn@40347
  1187
    (imp_prems_conv
bulwahn@40347
  1188
      (Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq})))))
bulwahn@40347
  1189
    (Thm.transfer thy rule)
bulwahn@40347
  1190
bulwahn@40347
  1191
fun preprocess_intro thy = expand_tuples thy #> preprocess_equality thy
bulwahn@40347
  1192
bulwahn@39763
  1193
(* defining a quickcheck predicate *)
bulwahn@39763
  1194
bulwahn@39763
  1195
fun strip_imp_prems (Const(@{const_name HOL.implies}, _) $ A $ B) = A :: strip_imp_prems B
bulwahn@39763
  1196
  | strip_imp_prems _ = [];
bulwahn@39763
  1197
bulwahn@39763
  1198
fun strip_imp_concl (Const(@{const_name HOL.implies}, _) $ A $ B) = strip_imp_concl B
bulwahn@39763
  1199
  | strip_imp_concl A = A : term;
bulwahn@39763
  1200
bulwahn@39763
  1201
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
bulwahn@39763
  1202
bulwahn@39763
  1203
fun define_quickcheck_predicate t thy =
bulwahn@39763
  1204
  let
bulwahn@39763
  1205
    val (vs, t') = strip_abs t
wenzelm@43232
  1206
    val vs' = Variable.variant_frees (Proof_Context.init_global thy) [] vs
bulwahn@39763
  1207
    val t'' = subst_bounds (map Free (rev vs'), t')
bulwahn@39763
  1208
    val (prems, concl) = strip_horn t''
bulwahn@39763
  1209
    val constname = "quickcheck"
bulwahn@39763
  1210
    val full_constname = Sign.full_bname thy constname
bulwahn@39763
  1211
    val constT = map snd vs' ---> @{typ bool}
bulwahn@39763
  1212
    val thy1 = Sign.add_consts_i [(Binding.name constname, constT, NoSyn)] thy
bulwahn@39763
  1213
    val const = Const (full_constname, constT)
bulwahn@39763
  1214
    val t = Logic.list_implies
bulwahn@39763
  1215
      (map HOLogic.mk_Trueprop (prems @ [HOLogic.mk_not concl]),
bulwahn@39763
  1216
       HOLogic.mk_Trueprop (list_comb (const, map Free vs')))
bulwahn@39763
  1217
    val tac = fn _ => Skip_Proof.cheat_tac thy1
wenzelm@43232
  1218
    val intro = Goal.prove (Proof_Context.init_global thy1) (map fst vs') [] t tac
bulwahn@39763
  1219
  in
bulwahn@39763
  1220
    ((((full_constname, constT), vs'), intro), thy1)
bulwahn@39763
  1221
  end
bulwahn@39763
  1222
bulwahn@33242
  1223
end;