src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author bulwahn
Wed Mar 31 16:44:41 2010 +0200 (2010-03-31)
changeset 36049 0ce5b7a5c2fd
parent 36047 f8297ebb21a7
child 36246 43fecedff8cf
permissions -rw-r--r--
adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
wenzelm@33265
     1
(*  Title:      HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
wenzelm@33265
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@33250
     3
wenzelm@33265
     4
Auxilary functions for predicate compiler.
bulwahn@33250
     5
*)
bulwahn@33250
     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
  val assert : bool -> unit
bulwahn@36047
    15
  (* mode *)
bulwahn@36047
    16
  datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@36047
    17
  val eq_mode : mode * mode -> bool
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@36047
    30
  val split_map_mode : (mode -> term -> term option * term option)
bulwahn@36047
    31
    -> mode -> term list -> term list * term list
bulwahn@36047
    32
  val split_map_modeT : (mode -> typ -> typ option * typ option)
bulwahn@36047
    33
    -> mode -> typ list -> typ list * typ list
bulwahn@36047
    34
  val split_mode : mode -> term list -> term list * term list
bulwahn@36047
    35
  val split_modeT' : mode -> typ list -> typ list * typ list
bulwahn@36047
    36
  val string_of_mode : mode -> string
bulwahn@36047
    37
  val ascii_string_of_mode : mode -> string
bulwahn@36047
    38
  (* premises *)
bulwahn@36047
    39
  datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@36047
    40
    | Generator of (string * typ)
bulwahn@36047
    41
  (* general syntactic functions *)
bulwahn@36047
    42
  val conjuncts : term -> term list
bulwahn@36047
    43
  val is_equationlike : thm -> bool
bulwahn@36047
    44
  val is_pred_equation : thm -> bool
bulwahn@36047
    45
  val is_intro : string -> thm -> bool
bulwahn@36047
    46
  val is_predT : typ -> bool
bulwahn@36047
    47
  val is_constrt : theory -> term -> bool
bulwahn@36047
    48
  val is_constr : Proof.context -> string -> bool
bulwahn@36047
    49
  val focus_ex : term -> Name.context -> ((string * typ) list * term) * Name.context
bulwahn@36047
    50
  val strip_all : term -> (string * typ) list * term
bulwahn@36047
    51
  (* introduction rule combinators *)
bulwahn@36047
    52
  val map_atoms : (term -> term) -> term -> term
bulwahn@36047
    53
  val fold_atoms : (term -> 'a -> 'a) -> term -> 'a -> 'a
bulwahn@36047
    54
  val fold_map_atoms : (term -> 'a -> term * 'a) -> term -> 'a -> term * 'a
bulwahn@36047
    55
  val maps_premises : (term -> term list) -> term -> term
bulwahn@36047
    56
  val map_concl : (term -> term) -> term -> term
bulwahn@36047
    57
  val map_term : theory -> (term -> term) -> thm -> thm
bulwahn@36047
    58
  (* split theorems of case expressions *)
bulwahn@36047
    59
  val prepare_split_thm : Proof.context -> thm -> thm
bulwahn@36047
    60
  val find_split_thm : theory -> term -> thm option
bulwahn@36047
    61
  (* datastructures and setup for generic compilation *)
bulwahn@36047
    62
  datatype compilation_funs = CompilationFuns of {
bulwahn@36047
    63
    mk_predT : typ -> typ,
bulwahn@36047
    64
    dest_predT : typ -> typ,
bulwahn@36047
    65
    mk_bot : typ -> term,
bulwahn@36047
    66
    mk_single : term -> term,
bulwahn@36047
    67
    mk_bind : term * term -> term,
bulwahn@36047
    68
    mk_sup : term * term -> term,
bulwahn@36047
    69
    mk_if : term -> term,
bulwahn@36049
    70
    mk_iterate_upto : typ -> term * term * term -> term,
bulwahn@36047
    71
    mk_not : term -> term,
bulwahn@36047
    72
    mk_map : typ -> typ -> term -> term -> term
bulwahn@36047
    73
  };
bulwahn@36047
    74
  val mk_predT : compilation_funs -> typ -> typ
bulwahn@36047
    75
  val dest_predT : compilation_funs -> typ -> typ
bulwahn@36047
    76
  val mk_bot : compilation_funs -> typ -> term
bulwahn@36047
    77
  val mk_single : compilation_funs -> term -> term
bulwahn@36047
    78
  val mk_bind : compilation_funs -> term * term -> term
bulwahn@36047
    79
  val mk_sup : compilation_funs -> term * term -> term
bulwahn@36047
    80
  val mk_if : compilation_funs -> term -> term
bulwahn@36049
    81
  val mk_iterate_upto : compilation_funs -> typ -> term * term * term -> term
bulwahn@36047
    82
  val mk_not : compilation_funs -> term -> term
bulwahn@36047
    83
  val mk_map : compilation_funs -> typ -> typ -> term -> term -> term
bulwahn@36047
    84
  val funT_of : compilation_funs -> mode -> typ -> typ
bulwahn@36047
    85
  (* Different compilations *)
bulwahn@36047
    86
  datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
bulwahn@36047
    87
    | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
bulwahn@36047
    88
  val negative_compilation_of : compilation -> compilation
bulwahn@36047
    89
  val compilation_for_polarity : bool -> compilation -> compilation
bulwahn@36047
    90
  val string_of_compilation : compilation -> string
bulwahn@36047
    91
  val compilation_names : (string * compilation) list
bulwahn@36047
    92
  val non_random_compilations : compilation list
bulwahn@36047
    93
  val random_compilations : compilation list
bulwahn@36047
    94
  (* Different options for compiler *)
bulwahn@36047
    95
  datatype options = Options of {  
bulwahn@36047
    96
    expected_modes : (string * mode list) option,
bulwahn@36047
    97
    proposed_modes : (string * mode list) option,
bulwahn@36047
    98
    proposed_names : ((string * mode) * string) list,
bulwahn@36047
    99
    show_steps : bool,
bulwahn@36047
   100
    show_proof_trace : bool,
bulwahn@36047
   101
    show_intermediate_results : bool,
bulwahn@36047
   102
    show_mode_inference : bool,
bulwahn@36047
   103
    show_modes : bool,
bulwahn@36047
   104
    show_compilation : bool,
bulwahn@36047
   105
    show_caught_failures : bool,
bulwahn@36047
   106
    skip_proof : bool,
bulwahn@36047
   107
    no_topmost_reordering : bool,
bulwahn@36047
   108
    function_flattening : bool,
bulwahn@36047
   109
    fail_safe_function_flattening : bool,
bulwahn@36047
   110
    no_higher_order_predicate : string list,
bulwahn@36047
   111
    inductify : bool,
bulwahn@36047
   112
    compilation : compilation
bulwahn@36047
   113
  };
bulwahn@36047
   114
  val expected_modes : options -> (string * mode list) option
bulwahn@36047
   115
  val proposed_modes : options -> (string * mode list) option
bulwahn@36047
   116
  val proposed_names : options -> string -> mode -> string option
bulwahn@36047
   117
  val show_steps : options -> bool
bulwahn@36047
   118
  val show_proof_trace : options -> bool
bulwahn@36047
   119
  val show_intermediate_results : options -> bool
bulwahn@36047
   120
  val show_mode_inference : options -> bool
bulwahn@36047
   121
  val show_modes : options -> bool
bulwahn@36047
   122
  val show_compilation : options -> bool
bulwahn@36047
   123
  val show_caught_failures : options -> bool
bulwahn@36047
   124
  val skip_proof : options -> bool
bulwahn@36047
   125
  val no_topmost_reordering : options -> bool
bulwahn@36047
   126
  val function_flattening : options -> bool
bulwahn@36047
   127
  val fail_safe_function_flattening : options -> bool
bulwahn@36047
   128
  val no_higher_order_predicate : options -> string list
bulwahn@36047
   129
  val is_inductify : options -> bool
bulwahn@36047
   130
  val compilation : options -> compilation
bulwahn@36047
   131
  val default_options : options
bulwahn@36047
   132
  val bool_options : string list
bulwahn@36047
   133
  val print_step : options -> string -> unit
bulwahn@36047
   134
  (* simple transformations *)
bulwahn@36047
   135
  val expand_tuples : theory -> thm -> thm
bulwahn@36047
   136
  val eta_contract_ho_arguments : theory -> thm -> thm
bulwahn@36047
   137
  val remove_equalities : theory -> thm -> thm
bulwahn@36047
   138
end;
bulwahn@34948
   139
bulwahn@36047
   140
structure Predicate_Compile_Aux : PREDICATE_COMPILE_AUX =
bulwahn@33250
   141
struct
bulwahn@33250
   142
bulwahn@34948
   143
(* general functions *)
bulwahn@34948
   144
bulwahn@34948
   145
fun apfst3 f (x, y, z) = (f x, y, z)
bulwahn@34948
   146
fun apsnd3 f (x, y, z) = (x, f y, z)
bulwahn@34948
   147
fun aptrd3 f (x, y, z) = (x, y, f z)
bulwahn@34948
   148
bulwahn@34948
   149
fun comb_option f (SOME x1, SOME x2) = SOME (f (x1, x2))
bulwahn@34948
   150
  | comb_option f (NONE, SOME x2) = SOME x2
bulwahn@34948
   151
  | comb_option f (SOME x1, NONE) = SOME x1
bulwahn@34948
   152
  | comb_option f (NONE, NONE) = NONE
bulwahn@34948
   153
bulwahn@35885
   154
fun map2_optional f (x :: xs) (y :: ys) = f x (SOME y) :: (map2_optional f xs ys)
bulwahn@34948
   155
  | map2_optional f (x :: xs) [] = (f x NONE) :: (map2_optional f xs [])
bulwahn@34948
   156
  | map2_optional f [] [] = []
bulwahn@34948
   157
bulwahn@34948
   158
fun find_indices f xs =
bulwahn@34948
   159
  map_filter (fn (i, true) => SOME i | (i, false) => NONE) (map_index (apsnd f) xs)
bulwahn@33328
   160
bulwahn@35885
   161
fun assert check = if check then () else raise Fail "Assertion failed!"
bulwahn@35885
   162
bulwahn@33328
   163
(* mode *)
bulwahn@33328
   164
bulwahn@34948
   165
datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@33619
   166
bulwahn@33623
   167
(* equality of instantiatedness with respect to equivalences:
bulwahn@33623
   168
  Pair Input Input == Input and Pair Output Output == Output *)
bulwahn@34948
   169
fun eq_mode (Fun (m1, m2), Fun (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
   170
  | eq_mode (Pair (m1, m2), Pair (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
   171
  | eq_mode (Pair (m1, m2), Input) = eq_mode (m1, Input) andalso eq_mode (m2, Input)
bulwahn@34948
   172
  | eq_mode (Pair (m1, m2), Output) = eq_mode (m1, Output) andalso eq_mode (m2, Output)
bulwahn@34948
   173
  | eq_mode (Input, Pair (m1, m2)) = eq_mode (Input, m1) andalso eq_mode (Input, m2)
bulwahn@34948
   174
  | eq_mode (Output, Pair (m1, m2)) = eq_mode (Output, m1) andalso eq_mode (Output, m2)
bulwahn@34948
   175
  | eq_mode (Input, Input) = true
bulwahn@34948
   176
  | eq_mode (Output, Output) = true
bulwahn@34948
   177
  | eq_mode (Bool, Bool) = true
bulwahn@34948
   178
  | eq_mode _ = false
bulwahn@33623
   179
bulwahn@36035
   180
fun list_fun_mode [] = Bool
bulwahn@36035
   181
  | list_fun_mode (m :: ms) = Fun (m, list_fun_mode ms)
bulwahn@36035
   182
bulwahn@33619
   183
(* name: binder_modes? *)
bulwahn@33619
   184
fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
bulwahn@33619
   185
  | strip_fun_mode Bool = []
bulwahn@35885
   186
  | strip_fun_mode _ = raise Fail "Bad mode for strip_fun_mode"
bulwahn@33619
   187
bulwahn@36047
   188
(* name: strip_fun_mode? *)
bulwahn@33619
   189
fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
bulwahn@33619
   190
  | dest_fun_mode mode = [mode]
bulwahn@33619
   191
bulwahn@33619
   192
fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
bulwahn@33619
   193
  | dest_tuple_mode _ = []
bulwahn@33619
   194
bulwahn@35324
   195
fun all_modes_of_typ' (T as Type ("fun", _)) = 
bulwahn@35324
   196
  let
bulwahn@35324
   197
    val (S, U) = strip_type T
bulwahn@35324
   198
  in
bulwahn@35324
   199
    if U = HOLogic.boolT then
bulwahn@35324
   200
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35324
   201
        (map all_modes_of_typ' S) [Bool]
bulwahn@35324
   202
    else
bulwahn@35324
   203
      [Input, Output]
bulwahn@35324
   204
  end
bulwahn@35885
   205
  | all_modes_of_typ' (Type (@{type_name "*"}, [T1, T2])) = 
bulwahn@35324
   206
    map_product (curry Pair) (all_modes_of_typ' T1) (all_modes_of_typ' T2)
bulwahn@35324
   207
  | all_modes_of_typ' _ = [Input, Output]
bulwahn@35324
   208
bulwahn@35324
   209
fun all_modes_of_typ (T as Type ("fun", _)) =
bulwahn@35885
   210
    let
bulwahn@35885
   211
      val (S, U) = strip_type T
bulwahn@35885
   212
    in
bulwahn@35885
   213
      if U = @{typ bool} then
bulwahn@35885
   214
        fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35885
   215
          (map all_modes_of_typ' S) [Bool]
bulwahn@35885
   216
      else
bulwahn@35885
   217
        [Input, Output]
bulwahn@35885
   218
    end
bulwahn@35885
   219
  | all_modes_of_typ @{typ bool} = [Bool]
bulwahn@35324
   220
  | all_modes_of_typ T = all_modes_of_typ' T
bulwahn@34948
   221
bulwahn@35324
   222
fun all_smodes_of_typ (T as Type ("fun", _)) =
bulwahn@35324
   223
  let
bulwahn@35324
   224
    val (S, U) = strip_type T
bulwahn@35885
   225
    fun all_smodes (Type (@{type_name "*"}, [T1, T2])) = 
bulwahn@35324
   226
      map_product (curry Pair) (all_smodes T1) (all_smodes T2)
bulwahn@35324
   227
      | all_smodes _ = [Input, Output]
bulwahn@35324
   228
  in
bulwahn@35324
   229
    if U = HOLogic.boolT then
bulwahn@35324
   230
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2) (map all_smodes S) [Bool]
bulwahn@35324
   231
    else
bulwahn@36047
   232
      raise Fail "invalid type for predicate"
bulwahn@35324
   233
  end
bulwahn@35885
   234
bulwahn@34948
   235
fun ho_arg_modes_of mode =
bulwahn@34948
   236
  let
bulwahn@34948
   237
    fun ho_arg_mode (m as Fun _) =  [m]
bulwahn@34948
   238
      | ho_arg_mode (Pair (m1, m2)) = ho_arg_mode m1 @ ho_arg_mode m2
bulwahn@34948
   239
      | ho_arg_mode _ = []
bulwahn@34948
   240
  in
bulwahn@34948
   241
    maps ho_arg_mode (strip_fun_mode mode)
bulwahn@34948
   242
  end
bulwahn@34948
   243
bulwahn@34948
   244
fun ho_args_of mode ts =
bulwahn@34948
   245
  let
bulwahn@34948
   246
    fun ho_arg (Fun _) (SOME t) = [t]
bulwahn@36047
   247
      | ho_arg (Fun _) NONE = raise Fail "mode and term do not match"
bulwahn@35885
   248
      | ho_arg (Pair (m1, m2)) (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
bulwahn@34948
   249
          ho_arg m1 (SOME t1) @ ho_arg m2 (SOME t2)
bulwahn@34948
   250
      | ho_arg (Pair (m1, m2)) NONE = ho_arg m1 NONE @ ho_arg m2 NONE
bulwahn@34948
   251
      | ho_arg _ _ = []
bulwahn@34948
   252
  in
bulwahn@34948
   253
    flat (map2_optional ho_arg (strip_fun_mode mode) ts)
bulwahn@34948
   254
  end
bulwahn@34948
   255
bulwahn@34948
   256
(* temporary function should be replaced by unsplit_input or so? *)
bulwahn@34948
   257
fun replace_ho_args mode hoargs ts =
bulwahn@34948
   258
  let
bulwahn@34948
   259
    fun replace (Fun _, _) (arg' :: hoargs') = (arg', hoargs')
bulwahn@34948
   260
      | replace (Pair (m1, m2), Const ("Pair", T) $ t1 $ t2) hoargs =
bulwahn@34948
   261
        let
bulwahn@34948
   262
          val (t1', hoargs') = replace (m1, t1) hoargs
bulwahn@34948
   263
          val (t2', hoargs'') = replace (m2, t2) hoargs'
bulwahn@34948
   264
        in
bulwahn@34948
   265
          (Const ("Pair", T) $ t1' $ t2', hoargs'')
bulwahn@34948
   266
        end
bulwahn@34948
   267
      | replace (_, t) hoargs = (t, hoargs)
bulwahn@34948
   268
  in
bulwahn@35885
   269
    fst (fold_map replace (strip_fun_mode mode ~~ ts) hoargs)
bulwahn@34948
   270
  end
bulwahn@34948
   271
bulwahn@34948
   272
fun ho_argsT_of mode Ts =
bulwahn@34948
   273
  let
bulwahn@34948
   274
    fun ho_arg (Fun _) T = [T]
bulwahn@35885
   275
      | ho_arg (Pair (m1, m2)) (Type (@{type_name "*"}, [T1, T2])) = ho_arg m1 T1 @ ho_arg m2 T2
bulwahn@34948
   276
      | ho_arg _ _ = []
bulwahn@34948
   277
  in
bulwahn@34948
   278
    flat (map2 ho_arg (strip_fun_mode mode) Ts)
bulwahn@34948
   279
  end
bulwahn@34948
   280
bulwahn@34948
   281
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   282
fun split_map_mode f mode ts =
bulwahn@34948
   283
  let
bulwahn@34948
   284
    fun split_arg_mode' (m as Fun _) t = f m t
bulwahn@34948
   285
      | split_arg_mode' (Pair (m1, m2)) (Const ("Pair", _) $ t1 $ t2) =
bulwahn@34948
   286
        let
bulwahn@34948
   287
          val (i1, o1) = split_arg_mode' m1 t1
bulwahn@34948
   288
          val (i2, o2) = split_arg_mode' m2 t2
bulwahn@34948
   289
        in
bulwahn@34948
   290
          (comb_option HOLogic.mk_prod (i1, i2), comb_option HOLogic.mk_prod (o1, o2))
bulwahn@34948
   291
        end
bulwahn@35324
   292
      | split_arg_mode' m t =
bulwahn@35324
   293
        if eq_mode (m, Input) then (SOME t, NONE)
bulwahn@35324
   294
        else if eq_mode (m, Output) then (NONE,  SOME t)
bulwahn@35885
   295
        else raise Fail "split_map_mode: mode and term do not match"
bulwahn@34948
   296
  in
bulwahn@34948
   297
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) ts)
bulwahn@34948
   298
  end
bulwahn@34948
   299
bulwahn@34948
   300
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   301
fun split_map_modeT f mode Ts =
bulwahn@34948
   302
  let
bulwahn@34948
   303
    fun split_arg_mode' (m as Fun _) T = f m T
bulwahn@35885
   304
      | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name "*"}, [T1, T2])) =
bulwahn@34948
   305
        let
bulwahn@34948
   306
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   307
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   308
        in
bulwahn@34948
   309
          (comb_option HOLogic.mk_prodT (i1, i2), comb_option HOLogic.mk_prodT (o1, o2))
bulwahn@34948
   310
        end
bulwahn@34948
   311
      | split_arg_mode' Input T = (SOME T, NONE)
bulwahn@34948
   312
      | split_arg_mode' Output T = (NONE,  SOME T)
bulwahn@35885
   313
      | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
bulwahn@34948
   314
  in
bulwahn@34948
   315
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   316
  end
bulwahn@34948
   317
bulwahn@34948
   318
fun split_mode mode ts = split_map_mode (fn _ => fn _ => (NONE, NONE)) mode ts
bulwahn@34948
   319
bulwahn@35885
   320
fun fold_map_aterms_prodT comb f (Type (@{type_name "*"}, [T1, T2])) s =
bulwahn@34948
   321
  let
bulwahn@34948
   322
    val (x1, s') = fold_map_aterms_prodT comb f T1 s
bulwahn@34948
   323
    val (x2, s'') = fold_map_aterms_prodT comb f T2 s'
bulwahn@34948
   324
  in
bulwahn@34948
   325
    (comb x1 x2, s'')
bulwahn@34948
   326
  end
bulwahn@34948
   327
  | fold_map_aterms_prodT comb f T s = f T s
bulwahn@34948
   328
bulwahn@34948
   329
fun map_filter_prod f (Const ("Pair", _) $ t1 $ t2) =
bulwahn@34948
   330
  comb_option HOLogic.mk_prod (map_filter_prod f t1, map_filter_prod f t2)
bulwahn@34948
   331
  | map_filter_prod f t = f t
bulwahn@34948
   332
bulwahn@34948
   333
(* obviously, split_mode' and split_modeT' do not match? where does that cause problems? *)
bulwahn@34948
   334
  
bulwahn@34948
   335
fun split_modeT' mode Ts =
bulwahn@34948
   336
  let
bulwahn@34948
   337
    fun split_arg_mode' (Fun _) T = ([], [])
bulwahn@35885
   338
      | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name "*"}, [T1, T2])) =
bulwahn@34948
   339
        let
bulwahn@34948
   340
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   341
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   342
        in
bulwahn@34948
   343
          (i1 @ i2, o1 @ o2)
bulwahn@34948
   344
        end
bulwahn@34948
   345
      | split_arg_mode' Input T = ([T], [])
bulwahn@34948
   346
      | split_arg_mode' Output T = ([], [T])
bulwahn@35885
   347
      | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
bulwahn@34948
   348
  in
bulwahn@34948
   349
    (pairself flat o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   350
  end
bulwahn@34948
   351
bulwahn@34948
   352
fun string_of_mode mode =
bulwahn@33619
   353
  let
bulwahn@33619
   354
    fun string_of_mode1 Input = "i"
bulwahn@33619
   355
      | string_of_mode1 Output = "o"
bulwahn@33619
   356
      | string_of_mode1 Bool = "bool"
bulwahn@33619
   357
      | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
bulwahn@33626
   358
    and string_of_mode2 (Pair (m1, m2)) = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
bulwahn@33619
   359
      | string_of_mode2 mode = string_of_mode1 mode
bulwahn@33619
   360
    and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
bulwahn@33619
   361
      | string_of_mode3 mode = string_of_mode2 mode
bulwahn@34948
   362
  in string_of_mode3 mode end
bulwahn@33619
   363
bulwahn@34948
   364
fun ascii_string_of_mode mode' =
bulwahn@33626
   365
  let
bulwahn@33626
   366
    fun ascii_string_of_mode' Input = "i"
bulwahn@33626
   367
      | ascii_string_of_mode' Output = "o"
bulwahn@33626
   368
      | ascii_string_of_mode' Bool = "b"
bulwahn@33626
   369
      | ascii_string_of_mode' (Pair (m1, m2)) =
bulwahn@33626
   370
          "P" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   371
      | ascii_string_of_mode' (Fun (m1, m2)) = 
bulwahn@33626
   372
          "F" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Fun m2 ^ "B"
bulwahn@33626
   373
    and ascii_string_of_mode'_Fun (Fun (m1, m2)) =
bulwahn@33626
   374
          ascii_string_of_mode' m1 ^ (if m2 = Bool then "" else "_" ^ ascii_string_of_mode'_Fun m2)
bulwahn@33626
   375
      | ascii_string_of_mode'_Fun Bool = "B"
bulwahn@33626
   376
      | ascii_string_of_mode'_Fun m = ascii_string_of_mode' m
bulwahn@33626
   377
    and ascii_string_of_mode'_Pair (Pair (m1, m2)) =
bulwahn@33626
   378
          ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   379
      | ascii_string_of_mode'_Pair m = ascii_string_of_mode' m
bulwahn@33626
   380
  in ascii_string_of_mode'_Fun mode' end
bulwahn@33626
   381
bulwahn@34948
   382
(* premises *)
bulwahn@33619
   383
bulwahn@34948
   384
datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@34948
   385
  | Generator of (string * typ);
bulwahn@33619
   386
bulwahn@33250
   387
(* general syntactic functions *)
bulwahn@33250
   388
bulwahn@33250
   389
(*Like dest_conj, but flattens conjunctions however nested*)
bulwahn@33250
   390
fun conjuncts_aux (Const ("op &", _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
bulwahn@33250
   391
  | conjuncts_aux t conjs = t::conjs;
bulwahn@33250
   392
bulwahn@33250
   393
fun conjuncts t = conjuncts_aux t [];
bulwahn@33250
   394
bulwahn@33250
   395
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
bulwahn@33250
   396
  | is_equationlike_term (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ _)) = true
bulwahn@33250
   397
  | is_equationlike_term _ = false
bulwahn@33250
   398
  
bulwahn@33250
   399
val is_equationlike = is_equationlike_term o prop_of 
bulwahn@33250
   400
bulwahn@33250
   401
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
bulwahn@33250
   402
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
bulwahn@33250
   403
  | is_pred_equation_term _ = false
bulwahn@33250
   404
  
bulwahn@33250
   405
val is_pred_equation = is_pred_equation_term o prop_of 
bulwahn@33250
   406
bulwahn@33250
   407
fun is_intro_term constname t =
bulwahn@34948
   408
  the_default false (try (fn t => case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
bulwahn@33250
   409
    Const (c, _) => c = constname
bulwahn@34948
   410
  | _ => false) t)
bulwahn@33250
   411
  
bulwahn@33250
   412
fun is_intro constname t = is_intro_term constname (prop_of t)
bulwahn@33250
   413
bulwahn@33250
   414
fun is_pred thy constname =
bulwahn@33250
   415
  let
bulwahn@33250
   416
    val T = (Sign.the_const_type thy constname)
bulwahn@33250
   417
  in body_type T = @{typ "bool"} end;
bulwahn@33250
   418
bulwahn@35885
   419
fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = @{typ bool})
bulwahn@33250
   420
  | is_predT _ = false
bulwahn@33250
   421
bulwahn@33250
   422
(*** check if a term contains only constructor functions ***)
bulwahn@34948
   423
(* TODO: another copy in the core! *)
bulwahn@33623
   424
(* FIXME: constructor terms are supposed to be seen in the way the code generator
bulwahn@33623
   425
  sees constructors.*)
bulwahn@33250
   426
fun is_constrt thy =
bulwahn@33250
   427
  let
bulwahn@33250
   428
    val cnstrs = flat (maps
bulwahn@33250
   429
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
bulwahn@33250
   430
      (Symtab.dest (Datatype.get_all thy)));
bulwahn@33250
   431
    fun check t = (case strip_comb t of
bulwahn@36032
   432
        (Var _, []) => true
bulwahn@36032
   433
      | (Free _, []) => true
bulwahn@33250
   434
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
bulwahn@33250
   435
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
bulwahn@33250
   436
          | _ => false)
bulwahn@33250
   437
      | _ => false)
bulwahn@36032
   438
  in check end;
bulwahn@34948
   439
bulwahn@34948
   440
fun is_funtype (Type ("fun", [_, _])) = true
bulwahn@34948
   441
  | is_funtype _ = false;
bulwahn@34948
   442
bulwahn@34948
   443
fun is_Type (Type _) = true
bulwahn@34948
   444
  | is_Type _ = false
bulwahn@34948
   445
bulwahn@34948
   446
(* returns true if t is an application of an datatype constructor *)
bulwahn@34948
   447
(* which then consequently would be splitted *)
bulwahn@34948
   448
(* else false *)
bulwahn@34948
   449
(*
bulwahn@34948
   450
fun is_constructor thy t =
bulwahn@34948
   451
  if (is_Type (fastype_of t)) then
bulwahn@34948
   452
    (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
bulwahn@34948
   453
      NONE => false
bulwahn@34948
   454
    | SOME info => (let
bulwahn@34948
   455
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
bulwahn@34948
   456
      val (c, _) = strip_comb t
bulwahn@34948
   457
      in (case c of
bulwahn@34948
   458
        Const (name, _) => name mem_string constr_consts
bulwahn@34948
   459
        | _ => false) end))
bulwahn@34948
   460
  else false
bulwahn@34948
   461
*)
bulwahn@34948
   462
bulwahn@35891
   463
val is_constr = Code.is_constr o ProofContext.theory_of;
bulwahn@34948
   464
bulwahn@36047
   465
fun strip_all t = (Term.strip_all_vars t, Term.strip_all_body t)
bulwahn@36047
   466
bulwahn@33250
   467
fun strip_ex (Const ("Ex", _) $ Abs (x, T, t)) =
bulwahn@33250
   468
  let
bulwahn@33250
   469
    val (xTs, t') = strip_ex t
bulwahn@33250
   470
  in
bulwahn@33250
   471
    ((x, T) :: xTs, t')
bulwahn@33250
   472
  end
bulwahn@33250
   473
  | strip_ex t = ([], t)
bulwahn@33250
   474
bulwahn@33250
   475
fun focus_ex t nctxt =
bulwahn@33250
   476
  let
bulwahn@33250
   477
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
bulwahn@33250
   478
    val (xs', nctxt') = Name.variants xs nctxt;
bulwahn@33250
   479
    val ps' = xs' ~~ Ts;
bulwahn@33250
   480
    val vs = map Free ps';
bulwahn@33250
   481
    val t'' = Term.subst_bounds (rev vs, t');
bulwahn@33250
   482
  in ((ps', t''), nctxt') end;
bulwahn@33250
   483
bulwahn@33250
   484
(* introduction rule combinators *)
bulwahn@33250
   485
bulwahn@33250
   486
fun map_atoms f intro = 
bulwahn@33250
   487
  let
bulwahn@33250
   488
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   489
    fun appl t = (case t of
bulwahn@35885
   490
        (@{term Not} $ t') => HOLogic.mk_not (f t')
bulwahn@33250
   491
      | _ => f t)
bulwahn@33250
   492
  in
bulwahn@33250
   493
    Logic.list_implies
bulwahn@33250
   494
      (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
bulwahn@33250
   495
  end
bulwahn@33250
   496
bulwahn@33250
   497
fun fold_atoms f intro s =
bulwahn@33250
   498
  let
bulwahn@33250
   499
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   500
    fun appl t s = (case t of
bulwahn@35885
   501
      (@{term Not} $ t') => f t' s
bulwahn@33250
   502
      | _ => f t s)
bulwahn@33250
   503
  in fold appl (map HOLogic.dest_Trueprop literals) s end
bulwahn@33250
   504
bulwahn@33250
   505
fun fold_map_atoms f intro s =
bulwahn@33250
   506
  let
bulwahn@33250
   507
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   508
    fun appl t s = (case t of
bulwahn@35885
   509
      (@{term Not} $ t') => apfst HOLogic.mk_not (f t' s)
bulwahn@33250
   510
      | _ => f t s)
bulwahn@33250
   511
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
bulwahn@33250
   512
  in
bulwahn@33250
   513
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
bulwahn@33250
   514
  end;
bulwahn@33250
   515
bulwahn@33250
   516
fun maps_premises f intro =
bulwahn@33250
   517
  let
bulwahn@33250
   518
    val (premises, head) = Logic.strip_horn intro
bulwahn@33250
   519
  in
bulwahn@33250
   520
    Logic.list_implies (maps f premises, head)
bulwahn@33250
   521
  end
bulwahn@35324
   522
bulwahn@35875
   523
fun map_concl f intro =
bulwahn@35875
   524
  let
bulwahn@35875
   525
    val (premises, head) = Logic.strip_horn intro
bulwahn@35875
   526
  in
bulwahn@35875
   527
    Logic.list_implies (premises, f head)
bulwahn@35875
   528
  end
bulwahn@35875
   529
bulwahn@35875
   530
(* combinators to apply a function to all basic parts of nested products *)
bulwahn@35875
   531
bulwahn@35875
   532
fun map_products f (Const ("Pair", T) $ t1 $ t2) =
bulwahn@35875
   533
  Const ("Pair", T) $ map_products f t1 $ map_products f t2
bulwahn@35875
   534
  | map_products f t = f t
bulwahn@35324
   535
bulwahn@35324
   536
(* split theorems of case expressions *)
bulwahn@35324
   537
bulwahn@35324
   538
fun prepare_split_thm ctxt split_thm =
bulwahn@35324
   539
    (split_thm RS @{thm iffD2})
wenzelm@35624
   540
    |> Local_Defs.unfold ctxt [@{thm atomize_conjL[symmetric]},
bulwahn@35324
   541
      @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
bulwahn@35324
   542
bulwahn@36029
   543
fun find_split_thm thy (Const (name, T)) = Option.map #split (Datatype_Data.info_of_case thy name)
bulwahn@36029
   544
  | find_split_thm thy _ = NONE
bulwahn@35324
   545
bulwahn@33250
   546
(* lifting term operations to theorems *)
bulwahn@33250
   547
bulwahn@33250
   548
fun map_term thy f th =
bulwahn@33250
   549
  Skip_Proof.make_thm thy (f (prop_of th))
bulwahn@33250
   550
bulwahn@33250
   551
(*
bulwahn@33250
   552
fun equals_conv lhs_cv rhs_cv ct =
bulwahn@33250
   553
  case Thm.term_of ct of
bulwahn@33250
   554
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
bulwahn@33250
   555
  | _ => error "equals_conv"  
bulwahn@33250
   556
*)
bulwahn@33250
   557
bulwahn@36038
   558
(* Different compilations *)
bulwahn@33250
   559
bulwahn@35881
   560
datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
bulwahn@36018
   561
  | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
bulwahn@35324
   562
bulwahn@35324
   563
fun negative_compilation_of Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@35324
   564
  | negative_compilation_of Neg_Random_DSeq = Pos_Random_DSeq
bulwahn@36018
   565
  | negative_compilation_of New_Pos_Random_DSeq = New_Neg_Random_DSeq
bulwahn@36018
   566
  | negative_compilation_of New_Neg_Random_DSeq = New_Pos_Random_DSeq
bulwahn@35324
   567
  | negative_compilation_of c = c
bulwahn@35324
   568
  
bulwahn@35324
   569
fun compilation_for_polarity false Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@36018
   570
  | compilation_for_polarity false New_Pos_Random_DSeq = New_Neg_Random_DSeq
bulwahn@35324
   571
  | compilation_for_polarity _ c = c
bulwahn@34948
   572
bulwahn@35885
   573
fun string_of_compilation c =
bulwahn@35885
   574
  case c of
bulwahn@34948
   575
    Pred => ""
bulwahn@34948
   576
  | Random => "random"
bulwahn@34948
   577
  | Depth_Limited => "depth limited"
bulwahn@35881
   578
  | Depth_Limited_Random => "depth limited random"
bulwahn@34948
   579
  | DSeq => "dseq"
bulwahn@34948
   580
  | Annotated => "annotated"
bulwahn@35324
   581
  | Pos_Random_DSeq => "pos_random dseq"
bulwahn@35324
   582
  | Neg_Random_DSeq => "neg_random_dseq"
bulwahn@36018
   583
  | New_Pos_Random_DSeq => "new_pos_random dseq"
bulwahn@36018
   584
  | New_Neg_Random_DSeq => "new_neg_random_dseq"
bulwahn@36038
   585
bulwahn@36018
   586
val compilation_names = [("pred", Pred),
bulwahn@36018
   587
  ("random", Random),
bulwahn@36018
   588
  ("depth_limited", Depth_Limited),
bulwahn@36018
   589
  ("depth_limited_random", Depth_Limited_Random),
bulwahn@36018
   590
  (*("annotated", Annotated),*)
bulwahn@36018
   591
  ("dseq", DSeq), ("random_dseq", Pos_Random_DSeq),
bulwahn@36018
   592
  ("new_random_dseq", New_Pos_Random_DSeq)]
bulwahn@36038
   593
bulwahn@36038
   594
val non_random_compilations = [Pred, Depth_Limited, DSeq, Annotated]
bulwahn@36038
   595
bulwahn@36038
   596
bulwahn@36038
   597
val random_compilations = [Random, Depth_Limited_Random,
bulwahn@36038
   598
  Pos_Random_DSeq, Neg_Random_DSeq, New_Pos_Random_DSeq, New_Neg_Random_DSeq]
bulwahn@36038
   599
bulwahn@36046
   600
(* datastructures and setup for generic compilation *)
bulwahn@36046
   601
bulwahn@36046
   602
datatype compilation_funs = CompilationFuns of {
bulwahn@36046
   603
  mk_predT : typ -> typ,
bulwahn@36046
   604
  dest_predT : typ -> typ,
bulwahn@36046
   605
  mk_bot : typ -> term,
bulwahn@36046
   606
  mk_single : term -> term,
bulwahn@36046
   607
  mk_bind : term * term -> term,
bulwahn@36046
   608
  mk_sup : term * term -> term,
bulwahn@36046
   609
  mk_if : term -> term,
bulwahn@36049
   610
  mk_iterate_upto : typ -> term * term * term -> term,
bulwahn@36046
   611
  mk_not : term -> term,
bulwahn@36046
   612
  mk_map : typ -> typ -> term -> term -> term
bulwahn@36046
   613
};
bulwahn@36038
   614
bulwahn@36046
   615
fun mk_predT (CompilationFuns funs) = #mk_predT funs
bulwahn@36046
   616
fun dest_predT (CompilationFuns funs) = #dest_predT funs
bulwahn@36046
   617
fun mk_bot (CompilationFuns funs) = #mk_bot funs
bulwahn@36046
   618
fun mk_single (CompilationFuns funs) = #mk_single funs
bulwahn@36046
   619
fun mk_bind (CompilationFuns funs) = #mk_bind funs
bulwahn@36046
   620
fun mk_sup (CompilationFuns funs) = #mk_sup funs
bulwahn@36046
   621
fun mk_if (CompilationFuns funs) = #mk_if funs
bulwahn@36049
   622
fun mk_iterate_upto (CompilationFuns funs) = #mk_iterate_upto funs
bulwahn@36046
   623
fun mk_not (CompilationFuns funs) = #mk_not funs
bulwahn@36046
   624
fun mk_map (CompilationFuns funs) = #mk_map funs
bulwahn@36046
   625
bulwahn@36046
   626
(** function types and names of different compilations **)
bulwahn@36046
   627
bulwahn@36046
   628
fun funT_of compfuns mode T =
bulwahn@36046
   629
  let
bulwahn@36046
   630
    val Ts = binder_types T
bulwahn@36046
   631
    val (inTs, outTs) = split_map_modeT (fn m => fn T => (SOME (funT_of compfuns m T), NONE)) mode Ts
bulwahn@36046
   632
  in
bulwahn@36046
   633
    inTs ---> (mk_predT compfuns (HOLogic.mk_tupleT outTs))
bulwahn@36046
   634
  end;
bulwahn@36046
   635
bulwahn@36046
   636
(* Different options for compiler *)
bulwahn@34948
   637
bulwahn@33250
   638
datatype options = Options of {  
bulwahn@34948
   639
  expected_modes : (string * mode list) option,
bulwahn@34948
   640
  proposed_modes : (string * mode list) option,
bulwahn@34948
   641
  proposed_names : ((string * mode) * string) list,
bulwahn@33250
   642
  show_steps : bool,
bulwahn@33250
   643
  show_proof_trace : bool,
bulwahn@33250
   644
  show_intermediate_results : bool,
bulwahn@33251
   645
  show_mode_inference : bool,
bulwahn@33251
   646
  show_modes : bool,
bulwahn@33250
   647
  show_compilation : bool,
bulwahn@35324
   648
  show_caught_failures : bool,
bulwahn@33250
   649
  skip_proof : bool,
bulwahn@35324
   650
  no_topmost_reordering : bool,
bulwahn@35324
   651
  function_flattening : bool,
bulwahn@35324
   652
  fail_safe_function_flattening : bool,
bulwahn@35324
   653
  no_higher_order_predicate : string list,
bulwahn@33250
   654
  inductify : bool,
bulwahn@34948
   655
  compilation : compilation
bulwahn@33250
   656
};
bulwahn@33250
   657
bulwahn@33250
   658
fun expected_modes (Options opt) = #expected_modes opt
bulwahn@33752
   659
fun proposed_modes (Options opt) = #proposed_modes opt
bulwahn@34948
   660
fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode)
bulwahn@33623
   661
  (#proposed_names opt) (name, mode)
bulwahn@33620
   662
bulwahn@33250
   663
fun show_steps (Options opt) = #show_steps opt
bulwahn@33250
   664
fun show_intermediate_results (Options opt) = #show_intermediate_results opt
bulwahn@33250
   665
fun show_proof_trace (Options opt) = #show_proof_trace opt
bulwahn@33251
   666
fun show_modes (Options opt) = #show_modes opt
bulwahn@33251
   667
fun show_mode_inference (Options opt) = #show_mode_inference opt
bulwahn@33250
   668
fun show_compilation (Options opt) = #show_compilation opt
bulwahn@35324
   669
fun show_caught_failures (Options opt) = #show_caught_failures opt
bulwahn@35324
   670
bulwahn@33250
   671
fun skip_proof (Options opt) = #skip_proof opt
bulwahn@33250
   672
bulwahn@35324
   673
fun function_flattening (Options opt) = #function_flattening opt
bulwahn@35324
   674
fun fail_safe_function_flattening (Options opt) = #fail_safe_function_flattening opt
bulwahn@35324
   675
fun no_topmost_reordering (Options opt) = #no_topmost_reordering opt
bulwahn@35324
   676
fun no_higher_order_predicate (Options opt) = #no_higher_order_predicate opt
bulwahn@35324
   677
bulwahn@33250
   678
fun is_inductify (Options opt) = #inductify opt
bulwahn@34948
   679
bulwahn@34948
   680
fun compilation (Options opt) = #compilation opt
bulwahn@33250
   681
bulwahn@33250
   682
val default_options = Options {
bulwahn@33250
   683
  expected_modes = NONE,
bulwahn@33752
   684
  proposed_modes = NONE,
bulwahn@33623
   685
  proposed_names = [],
bulwahn@33250
   686
  show_steps = false,
bulwahn@33250
   687
  show_intermediate_results = false,
bulwahn@33250
   688
  show_proof_trace = false,
bulwahn@33251
   689
  show_modes = false,
bulwahn@33250
   690
  show_mode_inference = false,
bulwahn@33250
   691
  show_compilation = false,
bulwahn@35324
   692
  show_caught_failures = false,
bulwahn@34948
   693
  skip_proof = true,
bulwahn@35324
   694
  no_topmost_reordering = false,
bulwahn@35324
   695
  function_flattening = false,
bulwahn@35324
   696
  fail_safe_function_flattening = false,
bulwahn@35324
   697
  no_higher_order_predicate = [],
bulwahn@33250
   698
  inductify = false,
bulwahn@34948
   699
  compilation = Pred
bulwahn@33250
   700
}
bulwahn@33250
   701
bulwahn@34948
   702
val bool_options = ["show_steps", "show_intermediate_results", "show_proof_trace", "show_modes",
bulwahn@35381
   703
  "show_mode_inference", "show_compilation", "skip_proof", "inductify", "no_function_flattening",
bulwahn@35381
   704
  "no_topmost_reordering"]
bulwahn@34948
   705
bulwahn@33250
   706
fun print_step options s =
bulwahn@33250
   707
  if show_steps options then tracing s else ()
bulwahn@33250
   708
bulwahn@36047
   709
(* simple transformations *)
bulwahn@36047
   710
bulwahn@36047
   711
(** tuple processing **)
bulwahn@33250
   712
bulwahn@33250
   713
fun expand_tuples thy intro =
bulwahn@33250
   714
  let
bulwahn@33250
   715
    fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
bulwahn@33250
   716
      | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
bulwahn@33250
   717
      (case HOLogic.strip_tupleT (fastype_of arg) of
bulwahn@33250
   718
        (Ts as _ :: _ :: _) =>
bulwahn@33250
   719
        let
bulwahn@35885
   720
          fun rewrite_arg' (Const (@{const_name "Pair"}, _) $ _ $ t2, Type (@{type_name "*"}, [_, T2]))
bulwahn@33250
   721
            (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
bulwahn@35885
   722
            | rewrite_arg' (t, Type (@{type_name "*"}, [T1, T2])) (args, (pats, intro_t, ctxt)) =
bulwahn@33250
   723
              let
bulwahn@33250
   724
                val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
bulwahn@33250
   725
                val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
bulwahn@33250
   726
                val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
bulwahn@33250
   727
                val args' = map (Pattern.rewrite_term thy [pat] []) args
bulwahn@33250
   728
              in
bulwahn@33250
   729
                rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
bulwahn@33250
   730
              end
bulwahn@33250
   731
            | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
bulwahn@33250
   732
          val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
bulwahn@33250
   733
            (args, (pats, intro_t, ctxt))
bulwahn@33250
   734
        in
bulwahn@33250
   735
          rewrite_args args' (pats, intro_t', ctxt')
bulwahn@33250
   736
        end
bulwahn@33250
   737
      | _ => rewrite_args args (pats, intro_t, ctxt))
bulwahn@33250
   738
    fun rewrite_prem atom =
bulwahn@33250
   739
      let
bulwahn@33250
   740
        val (_, args) = strip_comb atom
bulwahn@33250
   741
      in rewrite_args args end
bulwahn@33250
   742
    val ctxt = ProofContext.init thy
bulwahn@33250
   743
    val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
bulwahn@33250
   744
    val intro_t = prop_of intro'
bulwahn@33250
   745
    val concl = Logic.strip_imp_concl intro_t
bulwahn@33250
   746
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
bulwahn@33250
   747
    val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
bulwahn@33250
   748
    val (pats', intro_t', ctxt3) = 
bulwahn@33250
   749
      fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
bulwahn@33250
   750
    fun rewrite_pat (ct1, ct2) =
bulwahn@33250
   751
      (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
bulwahn@33250
   752
    val t_insts' = map rewrite_pat t_insts
bulwahn@33250
   753
    val intro'' = Thm.instantiate (T_insts, t_insts') intro
bulwahn@33250
   754
    val [intro'''] = Variable.export ctxt3 ctxt [intro'']
bulwahn@33250
   755
    val intro'''' = Simplifier.full_simplify
bulwahn@33250
   756
      (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
bulwahn@33250
   757
      intro'''
bulwahn@33250
   758
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@33250
   759
    fun split_conj prem =
bulwahn@33250
   760
      map HOLogic.mk_Trueprop (conjuncts (HOLogic.dest_Trueprop prem))
bulwahn@33250
   761
    val intro''''' = map_term thy (maps_premises split_conj) intro''''
bulwahn@33250
   762
  in
bulwahn@33250
   763
    intro'''''
bulwahn@33250
   764
  end
bulwahn@33250
   765
bulwahn@36047
   766
(** eta contract higher-order arguments **)
bulwahn@35875
   767
bulwahn@35875
   768
fun eta_contract_ho_arguments thy intro =
bulwahn@35875
   769
  let
bulwahn@35875
   770
    fun f atom = list_comb (apsnd ((map o map_products) Envir.eta_contract) (strip_comb atom))
bulwahn@35875
   771
  in
bulwahn@35875
   772
    map_term thy (map_concl f o map_atoms f) intro
bulwahn@35875
   773
  end
bulwahn@35875
   774
bulwahn@36047
   775
(** remove equalities **)
bulwahn@36022
   776
bulwahn@36022
   777
fun remove_equalities thy intro =
bulwahn@36022
   778
  let
bulwahn@36022
   779
    fun remove_eqs intro_t =
bulwahn@36022
   780
      let
bulwahn@36022
   781
        val (prems, concl) = Logic.strip_horn intro_t
bulwahn@36022
   782
        fun remove_eq (prems, concl) =
bulwahn@36022
   783
          let
bulwahn@36022
   784
            fun removable_eq prem =
bulwahn@36022
   785
              case try (HOLogic.dest_eq o HOLogic.dest_Trueprop) prem of
bulwahn@36022
   786
                SOME (lhs, rhs) => (case lhs of
bulwahn@36022
   787
                  Var _ => true
bulwahn@36022
   788
                  | _ => (case rhs of Var _ => true | _ => false))
bulwahn@36022
   789
              | NONE => false
bulwahn@36022
   790
          in
bulwahn@36022
   791
            case find_first removable_eq prems of
bulwahn@36022
   792
              NONE => (prems, concl)
bulwahn@36022
   793
            | SOME eq =>
bulwahn@36022
   794
              let
bulwahn@36022
   795
                val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
bulwahn@36022
   796
                val prems' = remove (op =) eq prems
bulwahn@36022
   797
                val subst = (case lhs of
bulwahn@36022
   798
                  (v as Var _) =>
bulwahn@36022
   799
                    (fn t => if t = v then rhs else t)
bulwahn@36022
   800
                | _ => (case rhs of
bulwahn@36022
   801
                   (v as Var _) => (fn t => if t = v then lhs else t)))
bulwahn@36022
   802
              in
bulwahn@36022
   803
                remove_eq (map (map_aterms subst) prems', map_aterms subst concl)
bulwahn@36022
   804
              end
bulwahn@36022
   805
          end
bulwahn@36022
   806
      in
bulwahn@36022
   807
        Logic.list_implies (remove_eq (prems, concl))
bulwahn@36022
   808
      end
bulwahn@36022
   809
  in
bulwahn@36022
   810
    map_term thy remove_eqs intro
bulwahn@36022
   811
  end
bulwahn@35875
   812
bulwahn@33250
   813
end;