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