src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author bulwahn
Thu Sep 23 17:22:44 2010 +0200 (2010-09-23)
changeset 39657 5e57675b7e40
parent 39541 6605c1e87c7f
child 39658 b3644e40f661
permissions -rw-r--r--
moving the preprocessing of introduction rules after the code_pred command; added tuple expansion preprocessing of elimination rule
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@39311
    18
  val mode_ord: mode * mode -> order
bulwahn@36047
    19
  val list_fun_mode : mode list -> mode
bulwahn@36047
    20
  val strip_fun_mode : mode -> mode list
bulwahn@36047
    21
  val dest_fun_mode : mode -> mode list
bulwahn@36047
    22
  val dest_tuple_mode : mode -> mode list
bulwahn@36047
    23
  val all_modes_of_typ : typ -> mode list
bulwahn@36047
    24
  val all_smodes_of_typ : typ -> mode list
bulwahn@36047
    25
  val fold_map_aterms_prodT : ('a -> 'a -> 'a) -> (typ -> 'b -> 'a * 'b) -> typ -> 'b -> 'a * 'b
bulwahn@36047
    26
  val map_filter_prod : (term -> term option) -> term -> term option
bulwahn@36047
    27
  val replace_ho_args : mode -> term list -> term list -> term list
bulwahn@36047
    28
  val ho_arg_modes_of : mode -> mode list
bulwahn@36047
    29
  val ho_argsT_of : mode -> typ list -> typ list
bulwahn@36047
    30
  val ho_args_of : mode -> term list -> term list
bulwahn@39299
    31
  val ho_args_of_typ : typ -> term list -> term list
bulwahn@39299
    32
  val ho_argsT_of_typ : typ list -> typ list
bulwahn@36047
    33
  val split_map_mode : (mode -> term -> term option * term option)
bulwahn@36047
    34
    -> mode -> term list -> term list * term list
bulwahn@36047
    35
  val split_map_modeT : (mode -> typ -> typ option * typ option)
bulwahn@36047
    36
    -> mode -> typ list -> typ list * typ list
bulwahn@36047
    37
  val split_mode : mode -> term list -> term list * term list
bulwahn@36047
    38
  val split_modeT' : mode -> typ list -> typ list * typ list
bulwahn@36047
    39
  val string_of_mode : mode -> string
bulwahn@36047
    40
  val ascii_string_of_mode : mode -> string
bulwahn@36047
    41
  (* premises *)
bulwahn@36047
    42
  datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@36047
    43
    | Generator of (string * typ)
bulwahn@36251
    44
  val dest_indprem : indprem -> term
bulwahn@36254
    45
  val map_indprem : (term -> term) -> indprem -> indprem
bulwahn@36047
    46
  (* general syntactic functions *)
bulwahn@36047
    47
  val conjuncts : term -> term list
bulwahn@36047
    48
  val is_equationlike : thm -> bool
bulwahn@36047
    49
  val is_pred_equation : thm -> bool
bulwahn@36047
    50
  val is_intro : string -> thm -> bool
bulwahn@36047
    51
  val is_predT : typ -> bool
bulwahn@36047
    52
  val is_constrt : theory -> term -> bool
bulwahn@36047
    53
  val is_constr : Proof.context -> string -> bool
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@36047
    56
  (* introduction rule combinators *)
bulwahn@36047
    57
  val map_atoms : (term -> term) -> term -> term
bulwahn@36047
    58
  val fold_atoms : (term -> 'a -> 'a) -> term -> 'a -> 'a
bulwahn@36047
    59
  val fold_map_atoms : (term -> 'a -> term * 'a) -> term -> 'a -> term * 'a
bulwahn@36047
    60
  val maps_premises : (term -> term list) -> term -> term
bulwahn@36047
    61
  val map_concl : (term -> term) -> term -> term
bulwahn@36047
    62
  val map_term : theory -> (term -> term) -> thm -> thm
bulwahn@36047
    63
  (* split theorems of case expressions *)
bulwahn@36047
    64
  val prepare_split_thm : Proof.context -> thm -> thm
bulwahn@36047
    65
  val find_split_thm : theory -> term -> thm option
bulwahn@36047
    66
  (* datastructures and setup for generic compilation *)
bulwahn@36047
    67
  datatype compilation_funs = CompilationFuns of {
bulwahn@36047
    68
    mk_predT : typ -> typ,
bulwahn@36047
    69
    dest_predT : typ -> typ,
bulwahn@36047
    70
    mk_bot : typ -> term,
bulwahn@36047
    71
    mk_single : term -> term,
bulwahn@36047
    72
    mk_bind : term * term -> term,
bulwahn@36047
    73
    mk_sup : term * term -> term,
bulwahn@36047
    74
    mk_if : term -> term,
bulwahn@36049
    75
    mk_iterate_upto : typ -> term * term * term -> term,
bulwahn@36047
    76
    mk_not : term -> term,
bulwahn@36047
    77
    mk_map : typ -> typ -> term -> term -> term
bulwahn@36047
    78
  };
bulwahn@36047
    79
  val mk_predT : compilation_funs -> typ -> typ
bulwahn@36047
    80
  val dest_predT : compilation_funs -> typ -> typ
bulwahn@36047
    81
  val mk_bot : compilation_funs -> typ -> term
bulwahn@36047
    82
  val mk_single : compilation_funs -> term -> term
bulwahn@36047
    83
  val mk_bind : compilation_funs -> term * term -> term
bulwahn@36047
    84
  val mk_sup : compilation_funs -> term * term -> term
bulwahn@36047
    85
  val mk_if : compilation_funs -> term -> term
bulwahn@36049
    86
  val mk_iterate_upto : compilation_funs -> typ -> term * term * term -> term
bulwahn@36047
    87
  val mk_not : compilation_funs -> term -> term
bulwahn@36047
    88
  val mk_map : compilation_funs -> typ -> typ -> term -> term -> term
bulwahn@36047
    89
  val funT_of : compilation_funs -> mode -> typ -> typ
bulwahn@36047
    90
  (* Different compilations *)
bulwahn@36047
    91
  datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
bulwahn@36047
    92
    | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
bulwahn@36047
    93
  val negative_compilation_of : compilation -> compilation
bulwahn@36047
    94
  val compilation_for_polarity : bool -> compilation -> compilation
bulwahn@36047
    95
  val string_of_compilation : compilation -> string
bulwahn@36047
    96
  val compilation_names : (string * compilation) list
bulwahn@36047
    97
  val non_random_compilations : compilation list
bulwahn@36047
    98
  val random_compilations : compilation list
bulwahn@36047
    99
  (* Different options for compiler *)
bulwahn@36047
   100
  datatype options = Options of {  
bulwahn@36047
   101
    expected_modes : (string * mode list) option,
bulwahn@39382
   102
    proposed_modes : (string * mode list) list,
bulwahn@36047
   103
    proposed_names : ((string * mode) * string) list,
bulwahn@36047
   104
    show_steps : bool,
bulwahn@36047
   105
    show_proof_trace : bool,
bulwahn@36047
   106
    show_intermediate_results : bool,
bulwahn@36047
   107
    show_mode_inference : bool,
bulwahn@36047
   108
    show_modes : bool,
bulwahn@36047
   109
    show_compilation : bool,
bulwahn@36047
   110
    show_caught_failures : bool,
bulwahn@39383
   111
    show_invalid_clauses : bool,
bulwahn@36047
   112
    skip_proof : bool,
bulwahn@36047
   113
    no_topmost_reordering : bool,
bulwahn@36047
   114
    function_flattening : bool,
bulwahn@36047
   115
    fail_safe_function_flattening : bool,
bulwahn@36248
   116
    specialise : bool,
bulwahn@36047
   117
    no_higher_order_predicate : string list,
bulwahn@36047
   118
    inductify : bool,
bulwahn@36254
   119
    detect_switches : bool,
bulwahn@36047
   120
    compilation : compilation
bulwahn@36047
   121
  };
bulwahn@36047
   122
  val expected_modes : options -> (string * mode list) option
bulwahn@39382
   123
  val proposed_modes : options -> string -> mode list option
bulwahn@36047
   124
  val proposed_names : options -> string -> mode -> string option
bulwahn@36047
   125
  val show_steps : options -> bool
bulwahn@36047
   126
  val show_proof_trace : options -> bool
bulwahn@36047
   127
  val show_intermediate_results : options -> bool
bulwahn@36047
   128
  val show_mode_inference : options -> bool
bulwahn@36047
   129
  val show_modes : options -> bool
bulwahn@36047
   130
  val show_compilation : options -> bool
bulwahn@36047
   131
  val show_caught_failures : options -> bool
bulwahn@39383
   132
  val show_invalid_clauses : options -> bool
bulwahn@36047
   133
  val skip_proof : options -> bool
bulwahn@36047
   134
  val no_topmost_reordering : options -> bool
bulwahn@36047
   135
  val function_flattening : options -> bool
bulwahn@36047
   136
  val fail_safe_function_flattening : options -> bool
bulwahn@36248
   137
  val specialise : options -> bool
bulwahn@36047
   138
  val no_higher_order_predicate : options -> string list
bulwahn@36047
   139
  val is_inductify : options -> bool
bulwahn@36254
   140
  val detect_switches : options -> bool
bulwahn@36047
   141
  val compilation : options -> compilation
bulwahn@36047
   142
  val default_options : options
bulwahn@36047
   143
  val bool_options : string list
bulwahn@36047
   144
  val print_step : options -> string -> unit
bulwahn@39657
   145
  (* conversions *)
bulwahn@39657
   146
  val imp_prems_conv : conv -> conv
bulwahn@36047
   147
  (* simple transformations *)
bulwahn@36047
   148
  val expand_tuples : theory -> thm -> thm
bulwahn@39657
   149
  val expand_tuples_elim : Proof.context -> thm -> thm
bulwahn@36047
   150
  val eta_contract_ho_arguments : theory -> thm -> thm
bulwahn@36047
   151
  val remove_equalities : theory -> thm -> thm
bulwahn@36246
   152
  val remove_pointless_clauses : thm -> thm list
bulwahn@36246
   153
  val peephole_optimisation : theory -> thm -> thm option
bulwahn@39541
   154
  val define_quickcheck_predicate :
bulwahn@39541
   155
    term -> theory -> (((string * typ) * (string * typ) list) * thm) * theory 
bulwahn@36047
   156
end;
bulwahn@34948
   157
bulwahn@36047
   158
structure Predicate_Compile_Aux : PREDICATE_COMPILE_AUX =
bulwahn@33250
   159
struct
bulwahn@33250
   160
bulwahn@34948
   161
(* general functions *)
bulwahn@34948
   162
bulwahn@34948
   163
fun apfst3 f (x, y, z) = (f x, y, z)
bulwahn@34948
   164
fun apsnd3 f (x, y, z) = (x, f y, z)
bulwahn@34948
   165
fun aptrd3 f (x, y, z) = (x, y, f z)
bulwahn@34948
   166
bulwahn@34948
   167
fun comb_option f (SOME x1, SOME x2) = SOME (f (x1, x2))
bulwahn@34948
   168
  | comb_option f (NONE, SOME x2) = SOME x2
bulwahn@34948
   169
  | comb_option f (SOME x1, NONE) = SOME x1
bulwahn@34948
   170
  | comb_option f (NONE, NONE) = NONE
bulwahn@34948
   171
bulwahn@35885
   172
fun map2_optional f (x :: xs) (y :: ys) = f x (SOME y) :: (map2_optional f xs ys)
bulwahn@34948
   173
  | map2_optional f (x :: xs) [] = (f x NONE) :: (map2_optional f xs [])
bulwahn@34948
   174
  | map2_optional f [] [] = []
bulwahn@34948
   175
bulwahn@34948
   176
fun find_indices f xs =
bulwahn@34948
   177
  map_filter (fn (i, true) => SOME i | (i, false) => NONE) (map_index (apsnd f) xs)
bulwahn@33328
   178
bulwahn@35885
   179
fun assert check = if check then () else raise Fail "Assertion failed!"
bulwahn@35885
   180
bulwahn@33328
   181
(* mode *)
bulwahn@33328
   182
bulwahn@34948
   183
datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
bulwahn@33619
   184
bulwahn@33623
   185
(* equality of instantiatedness with respect to equivalences:
bulwahn@33623
   186
  Pair Input Input == Input and Pair Output Output == Output *)
bulwahn@34948
   187
fun eq_mode (Fun (m1, m2), Fun (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
   188
  | eq_mode (Pair (m1, m2), Pair (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
bulwahn@34948
   189
  | eq_mode (Pair (m1, m2), Input) = eq_mode (m1, Input) andalso eq_mode (m2, Input)
bulwahn@34948
   190
  | eq_mode (Pair (m1, m2), Output) = eq_mode (m1, Output) andalso eq_mode (m2, Output)
bulwahn@34948
   191
  | eq_mode (Input, Pair (m1, m2)) = eq_mode (Input, m1) andalso eq_mode (Input, m2)
bulwahn@34948
   192
  | eq_mode (Output, Pair (m1, m2)) = eq_mode (Output, m1) andalso eq_mode (Output, m2)
bulwahn@34948
   193
  | eq_mode (Input, Input) = true
bulwahn@34948
   194
  | eq_mode (Output, Output) = true
bulwahn@34948
   195
  | eq_mode (Bool, Bool) = true
bulwahn@34948
   196
  | eq_mode _ = false
bulwahn@33623
   197
bulwahn@39311
   198
fun mode_ord (Input, Output) = LESS
bulwahn@39311
   199
  | mode_ord (Output, Input) = GREATER
bulwahn@39311
   200
  | mode_ord (Input, Input) = EQUAL
bulwahn@39311
   201
  | mode_ord (Output, Output) = EQUAL
bulwahn@39311
   202
  | mode_ord (Bool, Bool) = EQUAL
bulwahn@39311
   203
  | mode_ord (Pair (m1, m2), Pair (m3, m4)) = prod_ord mode_ord mode_ord ((m1, m2), (m3, m4))
bulwahn@39311
   204
  | mode_ord (Fun (m1, m2), Fun (m3, m4)) = prod_ord mode_ord mode_ord ((m1, m2), (m3, m4))
bulwahn@39311
   205
 
bulwahn@36035
   206
fun list_fun_mode [] = Bool
bulwahn@36035
   207
  | list_fun_mode (m :: ms) = Fun (m, list_fun_mode ms)
bulwahn@36035
   208
bulwahn@33619
   209
(* name: binder_modes? *)
bulwahn@33619
   210
fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
bulwahn@33619
   211
  | strip_fun_mode Bool = []
bulwahn@35885
   212
  | strip_fun_mode _ = raise Fail "Bad mode for strip_fun_mode"
bulwahn@33619
   213
bulwahn@36047
   214
(* name: strip_fun_mode? *)
bulwahn@33619
   215
fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
bulwahn@33619
   216
  | dest_fun_mode mode = [mode]
bulwahn@33619
   217
bulwahn@33619
   218
fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
bulwahn@33619
   219
  | dest_tuple_mode _ = []
bulwahn@33619
   220
bulwahn@35324
   221
fun all_modes_of_typ' (T as Type ("fun", _)) = 
bulwahn@35324
   222
  let
bulwahn@35324
   223
    val (S, U) = strip_type T
bulwahn@35324
   224
  in
bulwahn@35324
   225
    if U = HOLogic.boolT then
bulwahn@35324
   226
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35324
   227
        (map all_modes_of_typ' S) [Bool]
bulwahn@35324
   228
    else
bulwahn@35324
   229
      [Input, Output]
bulwahn@35324
   230
  end
haftmann@37678
   231
  | all_modes_of_typ' (Type (@{type_name Product_Type.prod}, [T1, T2])) = 
bulwahn@35324
   232
    map_product (curry Pair) (all_modes_of_typ' T1) (all_modes_of_typ' T2)
bulwahn@35324
   233
  | all_modes_of_typ' _ = [Input, Output]
bulwahn@35324
   234
bulwahn@35324
   235
fun all_modes_of_typ (T as Type ("fun", _)) =
bulwahn@35885
   236
    let
bulwahn@35885
   237
      val (S, U) = strip_type T
bulwahn@35885
   238
    in
bulwahn@35885
   239
      if U = @{typ bool} then
bulwahn@35885
   240
        fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
bulwahn@35885
   241
          (map all_modes_of_typ' S) [Bool]
bulwahn@35885
   242
      else
bulwahn@39192
   243
        raise Fail "Invocation of all_modes_of_typ with a non-predicate type"
bulwahn@35885
   244
    end
bulwahn@35885
   245
  | all_modes_of_typ @{typ bool} = [Bool]
bulwahn@39192
   246
  | all_modes_of_typ T =
bulwahn@39192
   247
    raise Fail "Invocation of all_modes_of_typ with a non-predicate type"
bulwahn@34948
   248
bulwahn@35324
   249
fun all_smodes_of_typ (T as Type ("fun", _)) =
bulwahn@35324
   250
  let
bulwahn@35324
   251
    val (S, U) = strip_type T
haftmann@37678
   252
    fun all_smodes (Type (@{type_name Product_Type.prod}, [T1, T2])) = 
bulwahn@35324
   253
      map_product (curry Pair) (all_smodes T1) (all_smodes T2)
bulwahn@35324
   254
      | all_smodes _ = [Input, Output]
bulwahn@35324
   255
  in
bulwahn@35324
   256
    if U = HOLogic.boolT then
bulwahn@35324
   257
      fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2) (map all_smodes S) [Bool]
bulwahn@35324
   258
    else
bulwahn@36047
   259
      raise Fail "invalid type for predicate"
bulwahn@35324
   260
  end
bulwahn@35885
   261
bulwahn@34948
   262
fun ho_arg_modes_of mode =
bulwahn@34948
   263
  let
bulwahn@34948
   264
    fun ho_arg_mode (m as Fun _) =  [m]
bulwahn@34948
   265
      | ho_arg_mode (Pair (m1, m2)) = ho_arg_mode m1 @ ho_arg_mode m2
bulwahn@34948
   266
      | ho_arg_mode _ = []
bulwahn@34948
   267
  in
bulwahn@34948
   268
    maps ho_arg_mode (strip_fun_mode mode)
bulwahn@34948
   269
  end
bulwahn@34948
   270
bulwahn@34948
   271
fun ho_args_of mode ts =
bulwahn@34948
   272
  let
bulwahn@34948
   273
    fun ho_arg (Fun _) (SOME t) = [t]
bulwahn@36047
   274
      | ho_arg (Fun _) NONE = raise Fail "mode and term do not match"
bulwahn@35885
   275
      | ho_arg (Pair (m1, m2)) (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
bulwahn@34948
   276
          ho_arg m1 (SOME t1) @ ho_arg m2 (SOME t2)
bulwahn@34948
   277
      | ho_arg (Pair (m1, m2)) NONE = ho_arg m1 NONE @ ho_arg m2 NONE
bulwahn@34948
   278
      | ho_arg _ _ = []
bulwahn@34948
   279
  in
bulwahn@34948
   280
    flat (map2_optional ho_arg (strip_fun_mode mode) ts)
bulwahn@34948
   281
  end
bulwahn@34948
   282
bulwahn@39299
   283
fun ho_args_of_typ T ts =
bulwahn@39299
   284
  let
bulwahn@39312
   285
    fun ho_arg (T as Type("fun", [_,_])) (SOME t) = if body_type T = @{typ bool} then [t] else []
bulwahn@39299
   286
      | ho_arg (Type("fun", [_,_])) NONE = raise Fail "mode and term do not match"
bulwahn@39299
   287
      | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2]))
bulwahn@39299
   288
         (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
bulwahn@39299
   289
          ho_arg T1 (SOME t1) @ ho_arg T2 (SOME t2)
bulwahn@39299
   290
      | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2])) NONE =
bulwahn@39299
   291
          ho_arg T1 NONE @ ho_arg T2 NONE
bulwahn@39299
   292
      | ho_arg _ _ = []
bulwahn@39299
   293
  in
bulwahn@39299
   294
    flat (map2_optional ho_arg (binder_types T) ts)
bulwahn@39299
   295
  end
bulwahn@39299
   296
bulwahn@39299
   297
fun ho_argsT_of_typ Ts =
bulwahn@39299
   298
  let
bulwahn@39312
   299
    fun ho_arg (T as Type("fun", [_,_])) = if body_type T = @{typ bool} then [T] else []
bulwahn@39299
   300
      | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2])) =
bulwahn@39299
   301
          ho_arg T1 @ ho_arg T2
bulwahn@39299
   302
      | ho_arg _ = []
bulwahn@39299
   303
  in
bulwahn@39299
   304
    maps ho_arg Ts
bulwahn@39299
   305
  end
bulwahn@39299
   306
  
bulwahn@39299
   307
bulwahn@34948
   308
(* temporary function should be replaced by unsplit_input or so? *)
bulwahn@34948
   309
fun replace_ho_args mode hoargs ts =
bulwahn@34948
   310
  let
bulwahn@34948
   311
    fun replace (Fun _, _) (arg' :: hoargs') = (arg', hoargs')
haftmann@37391
   312
      | replace (Pair (m1, m2), Const (@{const_name Pair}, T) $ t1 $ t2) hoargs =
bulwahn@34948
   313
        let
bulwahn@34948
   314
          val (t1', hoargs') = replace (m1, t1) hoargs
bulwahn@34948
   315
          val (t2', hoargs'') = replace (m2, t2) hoargs'
bulwahn@34948
   316
        in
haftmann@37391
   317
          (Const (@{const_name Pair}, T) $ t1' $ t2', hoargs'')
bulwahn@34948
   318
        end
bulwahn@34948
   319
      | replace (_, t) hoargs = (t, hoargs)
bulwahn@34948
   320
  in
bulwahn@35885
   321
    fst (fold_map replace (strip_fun_mode mode ~~ ts) hoargs)
bulwahn@34948
   322
  end
bulwahn@34948
   323
bulwahn@34948
   324
fun ho_argsT_of mode Ts =
bulwahn@34948
   325
  let
bulwahn@34948
   326
    fun ho_arg (Fun _) T = [T]
haftmann@37678
   327
      | ho_arg (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) = ho_arg m1 T1 @ ho_arg m2 T2
bulwahn@34948
   328
      | ho_arg _ _ = []
bulwahn@34948
   329
  in
bulwahn@34948
   330
    flat (map2 ho_arg (strip_fun_mode mode) Ts)
bulwahn@34948
   331
  end
bulwahn@34948
   332
bulwahn@34948
   333
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   334
fun split_map_mode f mode ts =
bulwahn@34948
   335
  let
bulwahn@34948
   336
    fun split_arg_mode' (m as Fun _) t = f m t
haftmann@37391
   337
      | split_arg_mode' (Pair (m1, m2)) (Const (@{const_name Pair}, _) $ t1 $ t2) =
bulwahn@34948
   338
        let
bulwahn@34948
   339
          val (i1, o1) = split_arg_mode' m1 t1
bulwahn@34948
   340
          val (i2, o2) = split_arg_mode' m2 t2
bulwahn@34948
   341
        in
bulwahn@34948
   342
          (comb_option HOLogic.mk_prod (i1, i2), comb_option HOLogic.mk_prod (o1, o2))
bulwahn@34948
   343
        end
bulwahn@35324
   344
      | split_arg_mode' m t =
bulwahn@35324
   345
        if eq_mode (m, Input) then (SOME t, NONE)
bulwahn@35324
   346
        else if eq_mode (m, Output) then (NONE,  SOME t)
bulwahn@35885
   347
        else raise Fail "split_map_mode: mode and term do not match"
bulwahn@34948
   348
  in
bulwahn@34948
   349
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) ts)
bulwahn@34948
   350
  end
bulwahn@34948
   351
bulwahn@34948
   352
(* splits mode and maps function to higher-order argument types *)
bulwahn@34948
   353
fun split_map_modeT f mode Ts =
bulwahn@34948
   354
  let
bulwahn@34948
   355
    fun split_arg_mode' (m as Fun _) T = f m T
haftmann@37678
   356
      | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
bulwahn@34948
   357
        let
bulwahn@34948
   358
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   359
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   360
        in
bulwahn@34948
   361
          (comb_option HOLogic.mk_prodT (i1, i2), comb_option HOLogic.mk_prodT (o1, o2))
bulwahn@34948
   362
        end
bulwahn@34948
   363
      | split_arg_mode' Input T = (SOME T, NONE)
bulwahn@34948
   364
      | split_arg_mode' Output T = (NONE,  SOME T)
bulwahn@35885
   365
      | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
bulwahn@34948
   366
  in
bulwahn@34948
   367
    (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   368
  end
bulwahn@34948
   369
bulwahn@34948
   370
fun split_mode mode ts = split_map_mode (fn _ => fn _ => (NONE, NONE)) mode ts
bulwahn@34948
   371
haftmann@37678
   372
fun fold_map_aterms_prodT comb f (Type (@{type_name Product_Type.prod}, [T1, T2])) s =
bulwahn@34948
   373
  let
bulwahn@34948
   374
    val (x1, s') = fold_map_aterms_prodT comb f T1 s
bulwahn@34948
   375
    val (x2, s'') = fold_map_aterms_prodT comb f T2 s'
bulwahn@34948
   376
  in
bulwahn@34948
   377
    (comb x1 x2, s'')
bulwahn@34948
   378
  end
bulwahn@34948
   379
  | fold_map_aterms_prodT comb f T s = f T s
bulwahn@34948
   380
haftmann@37391
   381
fun map_filter_prod f (Const (@{const_name Pair}, _) $ t1 $ t2) =
bulwahn@34948
   382
  comb_option HOLogic.mk_prod (map_filter_prod f t1, map_filter_prod f t2)
bulwahn@34948
   383
  | map_filter_prod f t = f t
bulwahn@34948
   384
bulwahn@34948
   385
(* obviously, split_mode' and split_modeT' do not match? where does that cause problems? *)
bulwahn@34948
   386
  
bulwahn@34948
   387
fun split_modeT' mode Ts =
bulwahn@34948
   388
  let
bulwahn@34948
   389
    fun split_arg_mode' (Fun _) T = ([], [])
haftmann@37678
   390
      | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
bulwahn@34948
   391
        let
bulwahn@34948
   392
          val (i1, o1) = split_arg_mode' m1 T1
bulwahn@34948
   393
          val (i2, o2) = split_arg_mode' m2 T2
bulwahn@34948
   394
        in
bulwahn@34948
   395
          (i1 @ i2, o1 @ o2)
bulwahn@34948
   396
        end
bulwahn@34948
   397
      | split_arg_mode' Input T = ([T], [])
bulwahn@34948
   398
      | split_arg_mode' Output T = ([], [T])
bulwahn@35885
   399
      | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
bulwahn@34948
   400
  in
bulwahn@34948
   401
    (pairself flat o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
bulwahn@34948
   402
  end
bulwahn@34948
   403
bulwahn@34948
   404
fun string_of_mode mode =
bulwahn@33619
   405
  let
bulwahn@33619
   406
    fun string_of_mode1 Input = "i"
bulwahn@33619
   407
      | string_of_mode1 Output = "o"
bulwahn@33619
   408
      | string_of_mode1 Bool = "bool"
bulwahn@33619
   409
      | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
bulwahn@33626
   410
    and string_of_mode2 (Pair (m1, m2)) = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
bulwahn@33619
   411
      | string_of_mode2 mode = string_of_mode1 mode
bulwahn@33619
   412
    and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
bulwahn@33619
   413
      | string_of_mode3 mode = string_of_mode2 mode
bulwahn@34948
   414
  in string_of_mode3 mode end
bulwahn@33619
   415
bulwahn@34948
   416
fun ascii_string_of_mode mode' =
bulwahn@33626
   417
  let
bulwahn@33626
   418
    fun ascii_string_of_mode' Input = "i"
bulwahn@33626
   419
      | ascii_string_of_mode' Output = "o"
bulwahn@33626
   420
      | ascii_string_of_mode' Bool = "b"
bulwahn@33626
   421
      | ascii_string_of_mode' (Pair (m1, m2)) =
bulwahn@33626
   422
          "P" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   423
      | ascii_string_of_mode' (Fun (m1, m2)) = 
bulwahn@33626
   424
          "F" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Fun m2 ^ "B"
bulwahn@33626
   425
    and ascii_string_of_mode'_Fun (Fun (m1, m2)) =
bulwahn@33626
   426
          ascii_string_of_mode' m1 ^ (if m2 = Bool then "" else "_" ^ ascii_string_of_mode'_Fun m2)
bulwahn@33626
   427
      | ascii_string_of_mode'_Fun Bool = "B"
bulwahn@33626
   428
      | ascii_string_of_mode'_Fun m = ascii_string_of_mode' m
bulwahn@33626
   429
    and ascii_string_of_mode'_Pair (Pair (m1, m2)) =
bulwahn@33626
   430
          ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
bulwahn@33626
   431
      | ascii_string_of_mode'_Pair m = ascii_string_of_mode' m
bulwahn@33626
   432
  in ascii_string_of_mode'_Fun mode' end
bulwahn@33626
   433
bulwahn@34948
   434
(* premises *)
bulwahn@33619
   435
bulwahn@34948
   436
datatype indprem = Prem of term | Negprem of term | Sidecond of term
bulwahn@34948
   437
  | Generator of (string * typ);
bulwahn@33619
   438
bulwahn@36251
   439
fun dest_indprem (Prem t) = t
bulwahn@36251
   440
  | dest_indprem (Negprem t) = t
bulwahn@36251
   441
  | dest_indprem (Sidecond t) = t
bulwahn@36251
   442
  | dest_indprem (Generator _) = raise Fail "cannot destruct generator"
bulwahn@36251
   443
bulwahn@36254
   444
fun map_indprem f (Prem t) = Prem (f t)
bulwahn@36254
   445
  | map_indprem f (Negprem t) = Negprem (f t)
bulwahn@36254
   446
  | map_indprem f (Sidecond t) = Sidecond (f t)
bulwahn@36254
   447
  | map_indprem f (Generator (v, T)) = Generator (dest_Free (f (Free (v, T))))
bulwahn@36254
   448
bulwahn@33250
   449
(* general syntactic functions *)
bulwahn@33250
   450
bulwahn@33250
   451
(*Like dest_conj, but flattens conjunctions however nested*)
haftmann@38795
   452
fun conjuncts_aux (Const (@{const_name HOL.conj}, _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
bulwahn@33250
   453
  | conjuncts_aux t conjs = t::conjs;
bulwahn@33250
   454
bulwahn@33250
   455
fun conjuncts t = conjuncts_aux t [];
bulwahn@33250
   456
bulwahn@33250
   457
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
haftmann@38864
   458
  | is_equationlike_term (Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ _)) = true
bulwahn@33250
   459
  | is_equationlike_term _ = false
bulwahn@33250
   460
  
bulwahn@33250
   461
val is_equationlike = is_equationlike_term o prop_of 
bulwahn@33250
   462
bulwahn@33250
   463
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
bulwahn@33250
   464
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
bulwahn@33250
   465
  | is_pred_equation_term _ = false
bulwahn@33250
   466
  
bulwahn@33250
   467
val is_pred_equation = is_pred_equation_term o prop_of 
bulwahn@33250
   468
bulwahn@33250
   469
fun is_intro_term constname t =
bulwahn@34948
   470
  the_default false (try (fn t => case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
bulwahn@33250
   471
    Const (c, _) => c = constname
bulwahn@34948
   472
  | _ => false) t)
bulwahn@33250
   473
  
bulwahn@33250
   474
fun is_intro constname t = is_intro_term constname (prop_of t)
bulwahn@33250
   475
haftmann@38552
   476
fun is_pred thy constname = (body_type (Sign.the_const_type thy constname) = HOLogic.boolT);
bulwahn@33250
   477
bulwahn@35885
   478
fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = @{typ bool})
bulwahn@33250
   479
  | is_predT _ = false
bulwahn@33250
   480
bulwahn@33250
   481
(*** check if a term contains only constructor functions ***)
bulwahn@34948
   482
(* TODO: another copy in the core! *)
bulwahn@33623
   483
(* FIXME: constructor terms are supposed to be seen in the way the code generator
bulwahn@33623
   484
  sees constructors.*)
bulwahn@33250
   485
fun is_constrt thy =
bulwahn@33250
   486
  let
bulwahn@33250
   487
    val cnstrs = flat (maps
bulwahn@33250
   488
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
bulwahn@33250
   489
      (Symtab.dest (Datatype.get_all thy)));
bulwahn@33250
   490
    fun check t = (case strip_comb t of
bulwahn@36032
   491
        (Var _, []) => true
bulwahn@36032
   492
      | (Free _, []) => true
bulwahn@33250
   493
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
bulwahn@33250
   494
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
bulwahn@33250
   495
          | _ => false)
bulwahn@33250
   496
      | _ => false)
bulwahn@36032
   497
  in check end;
bulwahn@34948
   498
bulwahn@34948
   499
fun is_funtype (Type ("fun", [_, _])) = true
bulwahn@34948
   500
  | is_funtype _ = false;
bulwahn@34948
   501
bulwahn@34948
   502
fun is_Type (Type _) = true
bulwahn@34948
   503
  | is_Type _ = false
bulwahn@34948
   504
bulwahn@34948
   505
(* returns true if t is an application of an datatype constructor *)
bulwahn@34948
   506
(* which then consequently would be splitted *)
bulwahn@34948
   507
(* else false *)
bulwahn@34948
   508
(*
bulwahn@34948
   509
fun is_constructor thy t =
bulwahn@34948
   510
  if (is_Type (fastype_of t)) then
bulwahn@34948
   511
    (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
bulwahn@34948
   512
      NONE => false
bulwahn@34948
   513
    | SOME info => (let
bulwahn@34948
   514
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
bulwahn@34948
   515
      val (c, _) = strip_comb t
bulwahn@34948
   516
      in (case c of
bulwahn@34948
   517
        Const (name, _) => name mem_string constr_consts
bulwahn@34948
   518
        | _ => false) end))
bulwahn@34948
   519
  else false
bulwahn@34948
   520
*)
bulwahn@34948
   521
bulwahn@35891
   522
val is_constr = Code.is_constr o ProofContext.theory_of;
bulwahn@34948
   523
bulwahn@36047
   524
fun strip_all t = (Term.strip_all_vars t, Term.strip_all_body t)
bulwahn@36047
   525
haftmann@38558
   526
fun strip_ex (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
bulwahn@33250
   527
  let
bulwahn@33250
   528
    val (xTs, t') = strip_ex t
bulwahn@33250
   529
  in
bulwahn@33250
   530
    ((x, T) :: xTs, t')
bulwahn@33250
   531
  end
bulwahn@33250
   532
  | strip_ex t = ([], t)
bulwahn@33250
   533
bulwahn@33250
   534
fun focus_ex t nctxt =
bulwahn@33250
   535
  let
bulwahn@33250
   536
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
bulwahn@33250
   537
    val (xs', nctxt') = Name.variants xs nctxt;
bulwahn@33250
   538
    val ps' = xs' ~~ Ts;
bulwahn@33250
   539
    val vs = map Free ps';
bulwahn@33250
   540
    val t'' = Term.subst_bounds (rev vs, t');
bulwahn@33250
   541
  in ((ps', t''), nctxt') end;
bulwahn@33250
   542
bulwahn@33250
   543
(* introduction rule combinators *)
bulwahn@33250
   544
bulwahn@33250
   545
fun map_atoms f intro = 
bulwahn@33250
   546
  let
bulwahn@33250
   547
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   548
    fun appl t = (case t of
bulwahn@35885
   549
        (@{term Not} $ t') => HOLogic.mk_not (f t')
bulwahn@33250
   550
      | _ => f t)
bulwahn@33250
   551
  in
bulwahn@33250
   552
    Logic.list_implies
bulwahn@33250
   553
      (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
bulwahn@33250
   554
  end
bulwahn@33250
   555
bulwahn@33250
   556
fun fold_atoms f intro s =
bulwahn@33250
   557
  let
bulwahn@33250
   558
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   559
    fun appl t s = (case t of
bulwahn@35885
   560
      (@{term Not} $ t') => f t' s
bulwahn@33250
   561
      | _ => f t s)
bulwahn@33250
   562
  in fold appl (map HOLogic.dest_Trueprop literals) s end
bulwahn@33250
   563
bulwahn@33250
   564
fun fold_map_atoms f intro s =
bulwahn@33250
   565
  let
bulwahn@33250
   566
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   567
    fun appl t s = (case t of
bulwahn@35885
   568
      (@{term Not} $ t') => apfst HOLogic.mk_not (f t' s)
bulwahn@33250
   569
      | _ => f t s)
bulwahn@33250
   570
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
bulwahn@33250
   571
  in
bulwahn@33250
   572
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
bulwahn@33250
   573
  end;
bulwahn@33250
   574
bulwahn@36246
   575
fun map_premises f intro =
bulwahn@36246
   576
  let
bulwahn@36246
   577
    val (premises, head) = Logic.strip_horn intro
bulwahn@36246
   578
  in
bulwahn@36246
   579
    Logic.list_implies (map f premises, head)
bulwahn@36246
   580
  end
bulwahn@36246
   581
bulwahn@36246
   582
fun map_filter_premises f intro =
bulwahn@36246
   583
  let
bulwahn@36246
   584
    val (premises, head) = Logic.strip_horn intro
bulwahn@36246
   585
  in
bulwahn@36246
   586
    Logic.list_implies (map_filter f premises, head)
bulwahn@36246
   587
  end
bulwahn@36246
   588
bulwahn@33250
   589
fun maps_premises f intro =
bulwahn@33250
   590
  let
bulwahn@33250
   591
    val (premises, head) = Logic.strip_horn intro
bulwahn@33250
   592
  in
bulwahn@33250
   593
    Logic.list_implies (maps f premises, head)
bulwahn@33250
   594
  end
bulwahn@35324
   595
bulwahn@35875
   596
fun map_concl f intro =
bulwahn@35875
   597
  let
bulwahn@35875
   598
    val (premises, head) = Logic.strip_horn intro
bulwahn@35875
   599
  in
bulwahn@35875
   600
    Logic.list_implies (premises, f head)
bulwahn@35875
   601
  end
bulwahn@35875
   602
bulwahn@35875
   603
(* combinators to apply a function to all basic parts of nested products *)
bulwahn@35875
   604
haftmann@37391
   605
fun map_products f (Const (@{const_name Pair}, T) $ t1 $ t2) =
haftmann@37391
   606
  Const (@{const_name Pair}, T) $ map_products f t1 $ map_products f t2
bulwahn@35875
   607
  | map_products f t = f t
bulwahn@35324
   608
bulwahn@35324
   609
(* split theorems of case expressions *)
bulwahn@35324
   610
bulwahn@35324
   611
fun prepare_split_thm ctxt split_thm =
bulwahn@35324
   612
    (split_thm RS @{thm iffD2})
wenzelm@35624
   613
    |> Local_Defs.unfold ctxt [@{thm atomize_conjL[symmetric]},
bulwahn@35324
   614
      @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
bulwahn@35324
   615
bulwahn@36029
   616
fun find_split_thm thy (Const (name, T)) = Option.map #split (Datatype_Data.info_of_case thy name)
bulwahn@36029
   617
  | find_split_thm thy _ = NONE
bulwahn@35324
   618
bulwahn@33250
   619
(* lifting term operations to theorems *)
bulwahn@33250
   620
bulwahn@33250
   621
fun map_term thy f th =
bulwahn@33250
   622
  Skip_Proof.make_thm thy (f (prop_of th))
bulwahn@33250
   623
bulwahn@33250
   624
(*
bulwahn@33250
   625
fun equals_conv lhs_cv rhs_cv ct =
bulwahn@33250
   626
  case Thm.term_of ct of
bulwahn@33250
   627
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
bulwahn@33250
   628
  | _ => error "equals_conv"  
bulwahn@33250
   629
*)
bulwahn@33250
   630
bulwahn@36038
   631
(* Different compilations *)
bulwahn@33250
   632
bulwahn@35881
   633
datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
bulwahn@36018
   634
  | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
bulwahn@35324
   635
bulwahn@35324
   636
fun negative_compilation_of Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@35324
   637
  | negative_compilation_of Neg_Random_DSeq = Pos_Random_DSeq
bulwahn@36018
   638
  | negative_compilation_of New_Pos_Random_DSeq = New_Neg_Random_DSeq
bulwahn@36018
   639
  | negative_compilation_of New_Neg_Random_DSeq = New_Pos_Random_DSeq
bulwahn@35324
   640
  | negative_compilation_of c = c
bulwahn@35324
   641
  
bulwahn@35324
   642
fun compilation_for_polarity false Pos_Random_DSeq = Neg_Random_DSeq
bulwahn@36018
   643
  | compilation_for_polarity false New_Pos_Random_DSeq = New_Neg_Random_DSeq
bulwahn@35324
   644
  | compilation_for_polarity _ c = c
bulwahn@34948
   645
bulwahn@35885
   646
fun string_of_compilation c =
bulwahn@35885
   647
  case c of
bulwahn@34948
   648
    Pred => ""
bulwahn@34948
   649
  | Random => "random"
bulwahn@34948
   650
  | Depth_Limited => "depth limited"
bulwahn@35881
   651
  | Depth_Limited_Random => "depth limited random"
bulwahn@34948
   652
  | DSeq => "dseq"
bulwahn@34948
   653
  | Annotated => "annotated"
bulwahn@35324
   654
  | Pos_Random_DSeq => "pos_random dseq"
bulwahn@35324
   655
  | Neg_Random_DSeq => "neg_random_dseq"
bulwahn@36018
   656
  | New_Pos_Random_DSeq => "new_pos_random dseq"
bulwahn@36018
   657
  | New_Neg_Random_DSeq => "new_neg_random_dseq"
bulwahn@36038
   658
bulwahn@36018
   659
val compilation_names = [("pred", Pred),
bulwahn@36018
   660
  ("random", Random),
bulwahn@36018
   661
  ("depth_limited", Depth_Limited),
bulwahn@36018
   662
  ("depth_limited_random", Depth_Limited_Random),
bulwahn@36018
   663
  (*("annotated", Annotated),*)
bulwahn@36018
   664
  ("dseq", DSeq), ("random_dseq", Pos_Random_DSeq),
bulwahn@36018
   665
  ("new_random_dseq", New_Pos_Random_DSeq)]
bulwahn@36038
   666
bulwahn@36038
   667
val non_random_compilations = [Pred, Depth_Limited, DSeq, Annotated]
bulwahn@36038
   668
bulwahn@36038
   669
bulwahn@36038
   670
val random_compilations = [Random, Depth_Limited_Random,
bulwahn@36038
   671
  Pos_Random_DSeq, Neg_Random_DSeq, New_Pos_Random_DSeq, New_Neg_Random_DSeq]
bulwahn@36038
   672
bulwahn@36046
   673
(* datastructures and setup for generic compilation *)
bulwahn@36046
   674
bulwahn@36046
   675
datatype compilation_funs = CompilationFuns of {
bulwahn@36046
   676
  mk_predT : typ -> typ,
bulwahn@36046
   677
  dest_predT : typ -> typ,
bulwahn@36046
   678
  mk_bot : typ -> term,
bulwahn@36046
   679
  mk_single : term -> term,
bulwahn@36046
   680
  mk_bind : term * term -> term,
bulwahn@36046
   681
  mk_sup : term * term -> term,
bulwahn@36046
   682
  mk_if : term -> term,
bulwahn@36049
   683
  mk_iterate_upto : typ -> term * term * term -> term,
bulwahn@36046
   684
  mk_not : term -> term,
bulwahn@36046
   685
  mk_map : typ -> typ -> term -> term -> term
bulwahn@36046
   686
};
bulwahn@36038
   687
bulwahn@36046
   688
fun mk_predT (CompilationFuns funs) = #mk_predT funs
bulwahn@36046
   689
fun dest_predT (CompilationFuns funs) = #dest_predT funs
bulwahn@36046
   690
fun mk_bot (CompilationFuns funs) = #mk_bot funs
bulwahn@36046
   691
fun mk_single (CompilationFuns funs) = #mk_single funs
bulwahn@36046
   692
fun mk_bind (CompilationFuns funs) = #mk_bind funs
bulwahn@36046
   693
fun mk_sup (CompilationFuns funs) = #mk_sup funs
bulwahn@36046
   694
fun mk_if (CompilationFuns funs) = #mk_if funs
bulwahn@36049
   695
fun mk_iterate_upto (CompilationFuns funs) = #mk_iterate_upto funs
bulwahn@36046
   696
fun mk_not (CompilationFuns funs) = #mk_not funs
bulwahn@36046
   697
fun mk_map (CompilationFuns funs) = #mk_map funs
bulwahn@36046
   698
bulwahn@36046
   699
(** function types and names of different compilations **)
bulwahn@36046
   700
bulwahn@36046
   701
fun funT_of compfuns mode T =
bulwahn@36046
   702
  let
bulwahn@36046
   703
    val Ts = binder_types T
bulwahn@36046
   704
    val (inTs, outTs) = split_map_modeT (fn m => fn T => (SOME (funT_of compfuns m T), NONE)) mode Ts
bulwahn@36046
   705
  in
bulwahn@36046
   706
    inTs ---> (mk_predT compfuns (HOLogic.mk_tupleT outTs))
bulwahn@36046
   707
  end;
bulwahn@36046
   708
bulwahn@36046
   709
(* Different options for compiler *)
bulwahn@34948
   710
bulwahn@33250
   711
datatype options = Options of {  
bulwahn@34948
   712
  expected_modes : (string * mode list) option,
bulwahn@39382
   713
  proposed_modes : (string * mode list) list,
bulwahn@34948
   714
  proposed_names : ((string * mode) * string) list,
bulwahn@33250
   715
  show_steps : bool,
bulwahn@33250
   716
  show_proof_trace : bool,
bulwahn@33250
   717
  show_intermediate_results : bool,
bulwahn@33251
   718
  show_mode_inference : bool,
bulwahn@33251
   719
  show_modes : bool,
bulwahn@33250
   720
  show_compilation : bool,
bulwahn@35324
   721
  show_caught_failures : bool,
bulwahn@39383
   722
  show_invalid_clauses : bool,
bulwahn@33250
   723
  skip_proof : bool,
bulwahn@35324
   724
  no_topmost_reordering : bool,
bulwahn@35324
   725
  function_flattening : bool,
bulwahn@36248
   726
  specialise : bool,
bulwahn@35324
   727
  fail_safe_function_flattening : bool,
bulwahn@35324
   728
  no_higher_order_predicate : string list,
bulwahn@33250
   729
  inductify : bool,
bulwahn@36254
   730
  detect_switches : bool,
bulwahn@34948
   731
  compilation : compilation
bulwahn@33250
   732
};
bulwahn@33250
   733
bulwahn@33250
   734
fun expected_modes (Options opt) = #expected_modes opt
bulwahn@39382
   735
fun proposed_modes (Options opt) = AList.lookup (op =) (#proposed_modes opt)
bulwahn@34948
   736
fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode)
bulwahn@33623
   737
  (#proposed_names opt) (name, mode)
bulwahn@33620
   738
bulwahn@33250
   739
fun show_steps (Options opt) = #show_steps opt
bulwahn@33250
   740
fun show_intermediate_results (Options opt) = #show_intermediate_results opt
bulwahn@33250
   741
fun show_proof_trace (Options opt) = #show_proof_trace opt
bulwahn@33251
   742
fun show_modes (Options opt) = #show_modes opt
bulwahn@33251
   743
fun show_mode_inference (Options opt) = #show_mode_inference opt
bulwahn@33250
   744
fun show_compilation (Options opt) = #show_compilation opt
bulwahn@35324
   745
fun show_caught_failures (Options opt) = #show_caught_failures opt
bulwahn@39383
   746
fun show_invalid_clauses (Options opt) = #show_invalid_clauses opt
bulwahn@33250
   747
fun skip_proof (Options opt) = #skip_proof opt
bulwahn@33250
   748
bulwahn@35324
   749
fun function_flattening (Options opt) = #function_flattening opt
bulwahn@35324
   750
fun fail_safe_function_flattening (Options opt) = #fail_safe_function_flattening opt
bulwahn@36248
   751
fun specialise (Options opt) = #specialise opt
bulwahn@35324
   752
fun no_topmost_reordering (Options opt) = #no_topmost_reordering opt
bulwahn@35324
   753
fun no_higher_order_predicate (Options opt) = #no_higher_order_predicate opt
bulwahn@35324
   754
bulwahn@33250
   755
fun is_inductify (Options opt) = #inductify opt
bulwahn@34948
   756
bulwahn@34948
   757
fun compilation (Options opt) = #compilation opt
bulwahn@33250
   758
bulwahn@36254
   759
fun detect_switches (Options opt) = #detect_switches opt
bulwahn@36254
   760
bulwahn@33250
   761
val default_options = Options {
bulwahn@33250
   762
  expected_modes = NONE,
bulwahn@39382
   763
  proposed_modes = [],
bulwahn@33623
   764
  proposed_names = [],
bulwahn@33250
   765
  show_steps = false,
bulwahn@33250
   766
  show_intermediate_results = false,
bulwahn@33250
   767
  show_proof_trace = false,
bulwahn@33251
   768
  show_modes = false,
bulwahn@33250
   769
  show_mode_inference = false,
bulwahn@33250
   770
  show_compilation = false,
bulwahn@35324
   771
  show_caught_failures = false,
bulwahn@39383
   772
  show_invalid_clauses = false,
bulwahn@34948
   773
  skip_proof = true,
bulwahn@35324
   774
  no_topmost_reordering = false,
bulwahn@35324
   775
  function_flattening = false,
bulwahn@36248
   776
  specialise = false,
bulwahn@35324
   777
  fail_safe_function_flattening = false,
bulwahn@35324
   778
  no_higher_order_predicate = [],
bulwahn@33250
   779
  inductify = false,
bulwahn@36254
   780
  detect_switches = true,
bulwahn@34948
   781
  compilation = Pred
bulwahn@33250
   782
}
bulwahn@33250
   783
bulwahn@34948
   784
val bool_options = ["show_steps", "show_intermediate_results", "show_proof_trace", "show_modes",
bulwahn@39383
   785
  "show_mode_inference", "show_compilation", "show_invalid_clauses", "skip_proof", "inductify",
bulwahn@39383
   786
  "no_function_flattening", "detect_switches", "specialise", "no_topmost_reordering"]
bulwahn@34948
   787
bulwahn@33250
   788
fun print_step options s =
bulwahn@33250
   789
  if show_steps options then tracing s else ()
bulwahn@33250
   790
bulwahn@36047
   791
(* simple transformations *)
bulwahn@36047
   792
bulwahn@36047
   793
(** tuple processing **)
bulwahn@33250
   794
bulwahn@39657
   795
fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
bulwahn@39657
   796
  | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
bulwahn@39657
   797
    (case HOLogic.strip_tupleT (fastype_of arg) of
bulwahn@39657
   798
      (Ts as _ :: _ :: _) =>
bulwahn@39657
   799
      let
bulwahn@39657
   800
        fun rewrite_arg' (Const (@{const_name Pair}, _) $ _ $ t2, Type (@{type_name Product_Type.prod}, [_, T2]))
bulwahn@39657
   801
          (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
bulwahn@39657
   802
          | rewrite_arg' (t, Type (@{type_name Product_Type.prod}, [T1, T2])) (args, (pats, intro_t, ctxt)) =
bulwahn@39657
   803
            let
bulwahn@39657
   804
              val thy = ProofContext.theory_of ctxt
bulwahn@39657
   805
              val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
bulwahn@39657
   806
              val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
bulwahn@39657
   807
              val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
bulwahn@39657
   808
              val args' = map (Pattern.rewrite_term thy [pat] []) args
bulwahn@39657
   809
            in
bulwahn@39657
   810
              rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
bulwahn@39657
   811
            end
bulwahn@39657
   812
          | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
bulwahn@39657
   813
        val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
bulwahn@39657
   814
          (args, (pats, intro_t, ctxt))
bulwahn@39657
   815
      in
bulwahn@39657
   816
        rewrite_args args' (pats, intro_t', ctxt')
bulwahn@39657
   817
      end
bulwahn@39657
   818
  | _ => rewrite_args args (pats, intro_t, ctxt))
bulwahn@39657
   819
bulwahn@39657
   820
fun rewrite_prem atom =
bulwahn@39657
   821
  let
bulwahn@39657
   822
    val (_, args) = strip_comb atom
bulwahn@39657
   823
  in rewrite_args args end
bulwahn@39657
   824
bulwahn@33250
   825
fun expand_tuples thy intro =
bulwahn@33250
   826
  let
wenzelm@36610
   827
    val ctxt = ProofContext.init_global thy
bulwahn@33250
   828
    val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
bulwahn@33250
   829
    val intro_t = prop_of intro'
bulwahn@33250
   830
    val concl = Logic.strip_imp_concl intro_t
bulwahn@33250
   831
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
bulwahn@33250
   832
    val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
bulwahn@33250
   833
    val (pats', intro_t', ctxt3) = 
bulwahn@33250
   834
      fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
bulwahn@33250
   835
    fun rewrite_pat (ct1, ct2) =
bulwahn@33250
   836
      (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
bulwahn@33250
   837
    val t_insts' = map rewrite_pat t_insts
bulwahn@33250
   838
    val intro'' = Thm.instantiate (T_insts, t_insts') intro
bulwahn@33250
   839
    val [intro'''] = Variable.export ctxt3 ctxt [intro'']
bulwahn@33250
   840
    val intro'''' = Simplifier.full_simplify
bulwahn@33250
   841
      (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
bulwahn@33250
   842
      intro'''
bulwahn@33250
   843
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@33250
   844
    fun split_conj prem =
bulwahn@33250
   845
      map HOLogic.mk_Trueprop (conjuncts (HOLogic.dest_Trueprop prem))
bulwahn@33250
   846
    val intro''''' = map_term thy (maps_premises split_conj) intro''''
bulwahn@33250
   847
  in
bulwahn@33250
   848
    intro'''''
bulwahn@33250
   849
  end
bulwahn@33250
   850
bulwahn@39657
   851
(*** conversions ***)
bulwahn@39657
   852
bulwahn@39657
   853
fun imp_prems_conv cv ct =
bulwahn@39657
   854
  case Thm.term_of ct of
bulwahn@39657
   855
    Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
bulwahn@39657
   856
  | _ => Conv.all_conv ct
bulwahn@39657
   857
bulwahn@39657
   858
fun all_params_conv cv ctxt ct =
bulwahn@39657
   859
  if Logic.is_all (Thm.term_of ct)
bulwahn@39657
   860
  then Conv.arg_conv (Conv.abs_conv (all_params_conv cv o #2) ctxt) ct
bulwahn@39657
   861
  else cv ctxt ct;
bulwahn@39657
   862
  
bulwahn@39657
   863
fun expand_tuples_elim ctxt elimrule =
bulwahn@39657
   864
  let
bulwahn@39657
   865
    val thy = ProofContext.theory_of ctxt
bulwahn@39657
   866
    val ((_, [elimrule]), ctxt1) = Variable.import false [elimrule] ctxt
bulwahn@39657
   867
    val prems = Thm.prems_of elimrule
bulwahn@39657
   868
    val nargs = length (snd (strip_comb (HOLogic.dest_Trueprop (hd prems))))
bulwahn@39657
   869
    fun preprocess_case t =
bulwahn@39657
   870
      let
bulwahn@39657
   871
        val (param_names, param_Ts)  = split_list (Logic.strip_params t)
bulwahn@39657
   872
        val prop = Logic.list_implies (Logic.strip_assums_hyp t, Logic.strip_assums_concl t)
bulwahn@39657
   873
        val (free_names, ctxt2) = Variable.variant_fixes param_names ctxt1
bulwahn@39657
   874
        val frees = map Free (free_names ~~ param_Ts)
bulwahn@39657
   875
        val prop' = subst_bounds (rev frees, prop)
bulwahn@39657
   876
        val (eqs, prems) = chop nargs (Logic.strip_imp_prems prop')
bulwahn@39657
   877
        val rhss = map (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop) eqs
bulwahn@39657
   878
        val (pats, prop'', ctxt2) = fold 
bulwahn@39657
   879
          rewrite_prem (map HOLogic.dest_Trueprop prems)
bulwahn@39657
   880
            (rewrite_args rhss ([], prop', ctxt2)) 
bulwahn@39657
   881
        val new_frees = fold Term.add_frees (frees @ map snd pats) [] (* FIXME: frees are not minimal and not ordered *)
bulwahn@39657
   882
      in
bulwahn@39657
   883
        fold Logic.all (map Free new_frees) prop''
bulwahn@39657
   884
      end
bulwahn@39657
   885
    val cases' = map preprocess_case (tl prems)
bulwahn@39657
   886
    val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule)
bulwahn@39657
   887
    val tac = (fn _ => Skip_Proof.cheat_tac thy)
bulwahn@39657
   888
    val eq = Goal.prove ctxt1 [] [] (Logic.mk_equals ((Thm.prop_of elimrule), elimrule')) tac
bulwahn@39657
   889
    val exported_elimrule' = Thm.equal_elim eq elimrule |> singleton (Variable.export ctxt1 ctxt)
bulwahn@39657
   890
    val elimrule'' = Conv.fconv_rule (imp_prems_conv (all_params_conv (fn ctxt => Conv.concl_conv nargs 
bulwahn@39657
   891
      (Simplifier.full_rewrite
bulwahn@39657
   892
        (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}]))) ctxt1)) 
bulwahn@39657
   893
      exported_elimrule'
bulwahn@39657
   894
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@39657
   895
    fun split_conj prem =
bulwahn@39657
   896
      map HOLogic.mk_Trueprop (conjuncts (HOLogic.dest_Trueprop prem))
bulwahn@39657
   897
    fun map_cases f t =
bulwahn@39657
   898
      let
bulwahn@39657
   899
        val (prems, concl) = Logic.strip_horn t
bulwahn@39657
   900
        val ([pred], prems') = chop 1 prems
bulwahn@39657
   901
        fun map_params f t =
bulwahn@39657
   902
          let
bulwahn@39657
   903
            val prop = Logic.list_implies (Logic.strip_assums_hyp t, Logic.strip_assums_concl t)
bulwahn@39657
   904
          in Term.list_all (Logic.strip_params t, f prop) end 
bulwahn@39657
   905
        val prems'' = map (map_params f) prems'
bulwahn@39657
   906
      in
bulwahn@39657
   907
        Logic.list_implies (pred :: prems'', concl)
bulwahn@39657
   908
      end
bulwahn@39657
   909
    val elimrule''' = map_term thy (map_cases (maps_premises split_conj)) elimrule''
bulwahn@39657
   910
   in
bulwahn@39657
   911
     elimrule'''
bulwahn@39657
   912
  end
bulwahn@36047
   913
(** eta contract higher-order arguments **)
bulwahn@35875
   914
bulwahn@35875
   915
fun eta_contract_ho_arguments thy intro =
bulwahn@35875
   916
  let
bulwahn@35875
   917
    fun f atom = list_comb (apsnd ((map o map_products) Envir.eta_contract) (strip_comb atom))
bulwahn@35875
   918
  in
bulwahn@35875
   919
    map_term thy (map_concl f o map_atoms f) intro
bulwahn@35875
   920
  end
bulwahn@35875
   921
bulwahn@36047
   922
(** remove equalities **)
bulwahn@36022
   923
bulwahn@36022
   924
fun remove_equalities thy intro =
bulwahn@36022
   925
  let
bulwahn@36022
   926
    fun remove_eqs intro_t =
bulwahn@36022
   927
      let
bulwahn@36022
   928
        val (prems, concl) = Logic.strip_horn intro_t
bulwahn@36022
   929
        fun remove_eq (prems, concl) =
bulwahn@36022
   930
          let
bulwahn@36022
   931
            fun removable_eq prem =
bulwahn@36022
   932
              case try (HOLogic.dest_eq o HOLogic.dest_Trueprop) prem of
bulwahn@36022
   933
                SOME (lhs, rhs) => (case lhs of
bulwahn@36022
   934
                  Var _ => true
bulwahn@36022
   935
                  | _ => (case rhs of Var _ => true | _ => false))
bulwahn@36022
   936
              | NONE => false
bulwahn@36022
   937
          in
bulwahn@36022
   938
            case find_first removable_eq prems of
bulwahn@36022
   939
              NONE => (prems, concl)
bulwahn@36022
   940
            | SOME eq =>
bulwahn@36022
   941
              let
bulwahn@36022
   942
                val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
bulwahn@36022
   943
                val prems' = remove (op =) eq prems
bulwahn@36022
   944
                val subst = (case lhs of
bulwahn@36022
   945
                  (v as Var _) =>
bulwahn@36022
   946
                    (fn t => if t = v then rhs else t)
bulwahn@36022
   947
                | _ => (case rhs of
bulwahn@36022
   948
                   (v as Var _) => (fn t => if t = v then lhs else t)))
bulwahn@36022
   949
              in
bulwahn@36022
   950
                remove_eq (map (map_aterms subst) prems', map_aterms subst concl)
bulwahn@36022
   951
              end
bulwahn@36022
   952
          end
bulwahn@36022
   953
      in
bulwahn@36022
   954
        Logic.list_implies (remove_eq (prems, concl))
bulwahn@36022
   955
      end
bulwahn@36022
   956
  in
bulwahn@36022
   957
    map_term thy remove_eqs intro
bulwahn@36022
   958
  end
bulwahn@35875
   959
bulwahn@36246
   960
(* Some last processing *)
bulwahn@36246
   961
bulwahn@36246
   962
fun remove_pointless_clauses intro =
bulwahn@36246
   963
  if Logic.strip_imp_prems (prop_of intro) = [@{prop "False"}] then
bulwahn@36246
   964
    []
bulwahn@36246
   965
  else [intro]
bulwahn@36246
   966
bulwahn@36246
   967
(* some peephole optimisations *)
bulwahn@36246
   968
bulwahn@36246
   969
fun peephole_optimisation thy intro =
bulwahn@36246
   970
  let
wenzelm@36610
   971
    val process =
wenzelm@36610
   972
      MetaSimplifier.rewrite_rule (Predicate_Compile_Simps.get (ProofContext.init_global thy))
bulwahn@36246
   973
    fun process_False intro_t =
bulwahn@36246
   974
      if member (op =) (Logic.strip_imp_prems intro_t) @{prop "False"} then NONE else SOME intro_t
bulwahn@36246
   975
    fun process_True intro_t =
bulwahn@36246
   976
      map_filter_premises (fn p => if p = @{prop True} then NONE else SOME p) intro_t
bulwahn@36246
   977
  in
bulwahn@36246
   978
    Option.map (Skip_Proof.make_thm thy)
bulwahn@36246
   979
      (process_False (process_True (prop_of (process intro))))
bulwahn@36246
   980
  end
bulwahn@36246
   981
bulwahn@39541
   982
(* defining a quickcheck predicate *)
bulwahn@39541
   983
bulwahn@39541
   984
fun strip_imp_prems (Const(@{const_name HOL.implies}, _) $ A $ B) = A :: strip_imp_prems B
bulwahn@39541
   985
  | strip_imp_prems _ = [];
bulwahn@39541
   986
bulwahn@39541
   987
fun strip_imp_concl (Const(@{const_name HOL.implies}, _) $ A $ B) = strip_imp_concl B
bulwahn@39541
   988
  | strip_imp_concl A = A : term;
bulwahn@39541
   989
bulwahn@39541
   990
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
bulwahn@39541
   991
bulwahn@39541
   992
fun define_quickcheck_predicate t thy =
bulwahn@39541
   993
  let
bulwahn@39541
   994
    val (vs, t') = strip_abs t
bulwahn@39541
   995
    val vs' = Variable.variant_frees (ProofContext.init_global thy) [] vs
bulwahn@39541
   996
    val t'' = subst_bounds (map Free (rev vs'), t')
bulwahn@39541
   997
    val (prems, concl) = strip_horn t''
bulwahn@39541
   998
    val constname = "quickcheck"
bulwahn@39541
   999
    val full_constname = Sign.full_bname thy constname
bulwahn@39541
  1000
    val constT = map snd vs' ---> @{typ bool}
bulwahn@39541
  1001
    val thy1 = Sign.add_consts_i [(Binding.name constname, constT, NoSyn)] thy
bulwahn@39541
  1002
    val const = Const (full_constname, constT)
bulwahn@39541
  1003
    val t = Logic.list_implies
bulwahn@39541
  1004
      (map HOLogic.mk_Trueprop (prems @ [HOLogic.mk_not concl]),
bulwahn@39541
  1005
       HOLogic.mk_Trueprop (list_comb (const, map Free vs')))
bulwahn@39541
  1006
    val tac = fn _ => Skip_Proof.cheat_tac thy1
bulwahn@39541
  1007
    val intro = Goal.prove (ProofContext.init_global thy1) (map fst vs') [] t tac
bulwahn@39541
  1008
  in
bulwahn@39541
  1009
    ((((full_constname, constT), vs'), intro), thy1)
bulwahn@39541
  1010
  end
bulwahn@39541
  1011
bulwahn@33250
  1012
end;