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