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