src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author bulwahn
Thu Nov 12 09:10:42 2009 +0100 (2009-11-12)
changeset 33623 4ec42d38224f
parent 33620 b6bf2dc5aed7
child 33626 42f69386943a
permissions -rw-r--r--
changed modes to expected_modes; added UNION to code_pred_inlining; fixed some examples; tuned
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@33265
     7
(* FIXME proper signature *)
wenzelm@33265
     8
bulwahn@33250
     9
structure Predicate_Compile_Aux =
bulwahn@33250
    10
struct
bulwahn@33250
    11
bulwahn@33328
    12
bulwahn@33328
    13
(* mode *)
bulwahn@33328
    14
bulwahn@33327
    15
type smode = (int * int list option) list
bulwahn@33327
    16
type mode = smode option list * smode
bulwahn@33328
    17
datatype tmode = Mode of mode * smode * tmode option list;
bulwahn@33328
    18
bulwahn@33328
    19
fun string_of_smode js =
bulwahn@33328
    20
    commas (map
bulwahn@33328
    21
      (fn (i, is) =>
bulwahn@33328
    22
        string_of_int i ^ (case is of NONE => ""
bulwahn@33328
    23
    | SOME is => "p" ^ enclose "[" "]" (commas (map string_of_int is)))) js)
bulwahn@33619
    24
(* FIXME: remove! *)
bulwahn@33328
    25
bulwahn@33328
    26
fun string_of_mode (iss, is) = space_implode " -> " (map
bulwahn@33328
    27
  (fn NONE => "X"
bulwahn@33328
    28
    | SOME js => enclose "[" "]" (string_of_smode js))
bulwahn@33328
    29
       (iss @ [SOME is]));
bulwahn@33328
    30
bulwahn@33328
    31
fun string_of_tmode (Mode (predmode, termmode, param_modes)) =
bulwahn@33619
    32
  "predmode: " ^ (string_of_mode predmode) ^
bulwahn@33328
    33
  (if null param_modes then "" else
bulwahn@33328
    34
    "; " ^ "params: " ^ commas (map (the_default "NONE" o Option.map string_of_tmode) param_modes))
bulwahn@33327
    35
bulwahn@33619
    36
(* new datatype for mode *)
bulwahn@33619
    37
bulwahn@33619
    38
datatype mode' = Bool | Input | Output | Pair of mode' * mode' | Fun of mode' * mode'
bulwahn@33619
    39
bulwahn@33623
    40
(* equality of instantiatedness with respect to equivalences:
bulwahn@33623
    41
  Pair Input Input == Input and Pair Output Output == Output *)
bulwahn@33623
    42
fun eq_mode' (Fun (m1, m2), Fun (m3, m4)) = eq_mode' (m1, m3) andalso eq_mode' (m2, m4)
bulwahn@33623
    43
  | eq_mode' (Pair (m1, m2), Pair (m3, m4)) = eq_mode' (m1, m3) andalso eq_mode' (m2, m4)
bulwahn@33623
    44
  | eq_mode' (Pair (m1, m2), Input) = eq_mode' (m1, Input) andalso eq_mode' (m2, Input)
bulwahn@33623
    45
  | eq_mode' (Pair (m1, m2), Output) = eq_mode' (m1, Output) andalso eq_mode' (m2, Output)
bulwahn@33623
    46
  | eq_mode' (Input, Pair (m1, m2)) = eq_mode' (Input, m1) andalso eq_mode' (Input, m2)
bulwahn@33623
    47
  | eq_mode' (Output, Pair (m1, m2)) = eq_mode' (Output, m1) andalso eq_mode' (Output, m2)
bulwahn@33623
    48
  | eq_mode' (Input, Input) = true
bulwahn@33623
    49
  | eq_mode' (Output, Output) = true
bulwahn@33623
    50
  | eq_mode' (Bool, Bool) = true
bulwahn@33623
    51
  | eq_mode' _ = false
bulwahn@33623
    52
bulwahn@33619
    53
(* name: binder_modes? *)
bulwahn@33619
    54
fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
bulwahn@33619
    55
  | strip_fun_mode Bool = []
bulwahn@33619
    56
  | strip_fun_mode _ = error "Bad mode for strip_fun_mode"
bulwahn@33619
    57
bulwahn@33619
    58
fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
bulwahn@33619
    59
  | dest_fun_mode mode = [mode]
bulwahn@33619
    60
bulwahn@33619
    61
fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
bulwahn@33619
    62
  | dest_tuple_mode _ = []
bulwahn@33619
    63
bulwahn@33619
    64
fun string_of_mode' mode' =
bulwahn@33619
    65
  let
bulwahn@33619
    66
    fun string_of_mode1 Input = "i"
bulwahn@33619
    67
      | string_of_mode1 Output = "o"
bulwahn@33619
    68
      | string_of_mode1 Bool = "bool"
bulwahn@33619
    69
      | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
bulwahn@33619
    70
    and string_of_mode2 (Pair (m1, m2))  = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
bulwahn@33619
    71
      | string_of_mode2 mode = string_of_mode1 mode
bulwahn@33619
    72
    and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
bulwahn@33619
    73
      | string_of_mode3 mode = string_of_mode2 mode
bulwahn@33619
    74
  in string_of_mode3 mode' end
bulwahn@33619
    75
bulwahn@33619
    76
fun translate_mode T (iss, is) =
bulwahn@33619
    77
  let
bulwahn@33619
    78
    val Ts = binder_types T
bulwahn@33619
    79
    val (Ts1, Ts2) = chop (length iss) Ts
bulwahn@33619
    80
    fun translate_smode Ts is =
bulwahn@33619
    81
      let
bulwahn@33619
    82
        fun translate_arg (i, T) =
bulwahn@33619
    83
          case AList.lookup (op =) is (i + 1) of
bulwahn@33619
    84
            SOME NONE => Input
bulwahn@33619
    85
          | SOME (SOME its) =>
bulwahn@33619
    86
            let
bulwahn@33619
    87
              fun translate_tuple (i, T) = if member (op =) its (i + 1) then Input else Output
bulwahn@33619
    88
            in 
bulwahn@33619
    89
              foldr1 Pair (map_index translate_tuple (HOLogic.strip_tupleT T))
bulwahn@33619
    90
            end
bulwahn@33619
    91
          | NONE => Output
bulwahn@33619
    92
      in map_index translate_arg Ts end
bulwahn@33619
    93
    fun mk_mode arg_modes = foldr1 Fun (arg_modes @ [Bool])
bulwahn@33619
    94
    val param_modes =
bulwahn@33619
    95
      map (fn (T, NONE) => Input | (T, SOME is) => mk_mode (translate_smode (binder_types T) is))
bulwahn@33619
    96
        (Ts1 ~~ iss)
bulwahn@33619
    97
  in
bulwahn@33619
    98
    mk_mode (param_modes @ translate_smode Ts2 is)
bulwahn@33619
    99
  end;
bulwahn@33619
   100
bulwahn@33623
   101
fun translate_mode' nparams mode' =
bulwahn@33623
   102
  let
bulwahn@33623
   103
    fun err () = error "translate_mode': given mode cannot be translated"
bulwahn@33623
   104
    val (m1, m2) = chop nparams (strip_fun_mode mode')
bulwahn@33623
   105
    val translate_to_tupled_mode =
bulwahn@33623
   106
      (map_filter I) o (map_index (fn (i, m) =>
bulwahn@33623
   107
        if eq_mode' (m, Input) then SOME (i + 1)
bulwahn@33623
   108
        else if eq_mode' (m, Output) then NONE
bulwahn@33623
   109
        else err ()))
bulwahn@33623
   110
    val translate_to_smode =
bulwahn@33623
   111
      (map_filter I) o (map_index (fn (i, m) =>
bulwahn@33623
   112
        if eq_mode' (m, Input) then SOME (i + 1, NONE)
bulwahn@33623
   113
        else if eq_mode' (m, Output) then NONE
bulwahn@33623
   114
        else SOME (i + 1, SOME (translate_to_tupled_mode (dest_tuple_mode m)))))
bulwahn@33623
   115
    fun translate_to_param_mode m =
bulwahn@33623
   116
      case rev (dest_fun_mode m) of
bulwahn@33623
   117
        Bool :: _ :: _ => SOME (translate_to_smode (strip_fun_mode m))
bulwahn@33623
   118
      | _ => if eq_mode' (m, Input) then NONE else err ()
bulwahn@33623
   119
  in
bulwahn@33623
   120
    (map translate_to_param_mode m1, translate_to_smode m2)
bulwahn@33623
   121
  end
bulwahn@33623
   122
bulwahn@33619
   123
fun string_of_mode thy constname mode =
bulwahn@33619
   124
  string_of_mode' (translate_mode (Sign.the_const_type thy constname) mode)
bulwahn@33619
   125
bulwahn@33250
   126
(* general syntactic functions *)
bulwahn@33250
   127
bulwahn@33250
   128
(*Like dest_conj, but flattens conjunctions however nested*)
bulwahn@33250
   129
fun conjuncts_aux (Const ("op &", _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
bulwahn@33250
   130
  | conjuncts_aux t conjs = t::conjs;
bulwahn@33250
   131
bulwahn@33250
   132
fun conjuncts t = conjuncts_aux t [];
bulwahn@33250
   133
bulwahn@33250
   134
fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
bulwahn@33250
   135
  | is_equationlike_term (Const ("Trueprop", _) $ (Const ("op =", _) $ _ $ _)) = true
bulwahn@33250
   136
  | is_equationlike_term _ = false
bulwahn@33250
   137
  
bulwahn@33250
   138
val is_equationlike = is_equationlike_term o prop_of 
bulwahn@33250
   139
bulwahn@33250
   140
fun is_pred_equation_term (Const ("==", _) $ u $ v) =
bulwahn@33250
   141
  (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
bulwahn@33250
   142
  | is_pred_equation_term _ = false
bulwahn@33250
   143
  
bulwahn@33250
   144
val is_pred_equation = is_pred_equation_term o prop_of 
bulwahn@33250
   145
bulwahn@33250
   146
fun is_intro_term constname t =
bulwahn@33250
   147
  case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
bulwahn@33250
   148
    Const (c, _) => c = constname
bulwahn@33250
   149
  | _ => false
bulwahn@33250
   150
  
bulwahn@33250
   151
fun is_intro constname t = is_intro_term constname (prop_of t)
bulwahn@33250
   152
bulwahn@33250
   153
fun is_pred thy constname =
bulwahn@33250
   154
  let
bulwahn@33250
   155
    val T = (Sign.the_const_type thy constname)
bulwahn@33250
   156
  in body_type T = @{typ "bool"} end;
bulwahn@33250
   157
  
bulwahn@33250
   158
bulwahn@33250
   159
fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = HOLogic.boolT)
bulwahn@33250
   160
  | is_predT _ = false
bulwahn@33250
   161
bulwahn@33619
   162
(* guessing number of parameters *)
bulwahn@33619
   163
fun find_indexes pred xs =
bulwahn@33619
   164
  let
bulwahn@33619
   165
    fun find is n [] = is
bulwahn@33619
   166
      | find is n (x :: xs) = find (if pred x then (n :: is) else is) (n + 1) xs;
bulwahn@33619
   167
  in rev (find [] 0 xs) end;
bulwahn@33619
   168
bulwahn@33619
   169
fun guess_nparams T =
bulwahn@33619
   170
  let
bulwahn@33619
   171
    val argTs = binder_types T
bulwahn@33619
   172
    val nparams = fold Integer.max
bulwahn@33619
   173
      (map (fn x => x + 1) (find_indexes is_predT argTs)) 0
bulwahn@33619
   174
  in nparams end;
bulwahn@33619
   175
bulwahn@33250
   176
(*** check if a term contains only constructor functions ***)
bulwahn@33623
   177
(* FIXME: constructor terms are supposed to be seen in the way the code generator
bulwahn@33623
   178
  sees constructors.*)
bulwahn@33250
   179
fun is_constrt thy =
bulwahn@33250
   180
  let
bulwahn@33250
   181
    val cnstrs = flat (maps
bulwahn@33250
   182
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
bulwahn@33250
   183
      (Symtab.dest (Datatype.get_all thy)));
bulwahn@33250
   184
    fun check t = (case strip_comb t of
bulwahn@33250
   185
        (Free _, []) => true
bulwahn@33250
   186
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
bulwahn@33250
   187
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
bulwahn@33250
   188
          | _ => false)
bulwahn@33250
   189
      | _ => false)
bulwahn@33250
   190
  in check end;  
bulwahn@33250
   191
  
bulwahn@33250
   192
fun strip_ex (Const ("Ex", _) $ Abs (x, T, t)) =
bulwahn@33250
   193
  let
bulwahn@33250
   194
    val (xTs, t') = strip_ex t
bulwahn@33250
   195
  in
bulwahn@33250
   196
    ((x, T) :: xTs, t')
bulwahn@33250
   197
  end
bulwahn@33250
   198
  | strip_ex t = ([], t)
bulwahn@33250
   199
bulwahn@33250
   200
fun focus_ex t nctxt =
bulwahn@33250
   201
  let
bulwahn@33250
   202
    val ((xs, Ts), t') = apfst split_list (strip_ex t) 
bulwahn@33250
   203
    val (xs', nctxt') = Name.variants xs nctxt;
bulwahn@33250
   204
    val ps' = xs' ~~ Ts;
bulwahn@33250
   205
    val vs = map Free ps';
bulwahn@33250
   206
    val t'' = Term.subst_bounds (rev vs, t');
bulwahn@33250
   207
  in ((ps', t''), nctxt') end;
bulwahn@33250
   208
bulwahn@33250
   209
bulwahn@33250
   210
(* introduction rule combinators *)
bulwahn@33250
   211
bulwahn@33250
   212
(* combinators to apply a function to all literals of an introduction rules *)
bulwahn@33250
   213
bulwahn@33250
   214
fun map_atoms f intro = 
bulwahn@33250
   215
  let
bulwahn@33250
   216
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   217
    fun appl t = (case t of
bulwahn@33250
   218
        (@{term "Not"} $ t') => HOLogic.mk_not (f t')
bulwahn@33250
   219
      | _ => f t)
bulwahn@33250
   220
  in
bulwahn@33250
   221
    Logic.list_implies
bulwahn@33250
   222
      (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
bulwahn@33250
   223
  end
bulwahn@33250
   224
bulwahn@33250
   225
fun fold_atoms f intro s =
bulwahn@33250
   226
  let
bulwahn@33250
   227
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   228
    fun appl t s = (case t of
bulwahn@33250
   229
      (@{term "Not"} $ t') => f t' s
bulwahn@33250
   230
      | _ => f t s)
bulwahn@33250
   231
  in fold appl (map HOLogic.dest_Trueprop literals) s end
bulwahn@33250
   232
bulwahn@33250
   233
fun fold_map_atoms f intro s =
bulwahn@33250
   234
  let
bulwahn@33250
   235
    val (literals, head) = Logic.strip_horn intro
bulwahn@33250
   236
    fun appl t s = (case t of
bulwahn@33250
   237
      (@{term "Not"} $ t') => apfst HOLogic.mk_not (f t' s)
bulwahn@33250
   238
      | _ => f t s)
bulwahn@33250
   239
    val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
bulwahn@33250
   240
  in
bulwahn@33250
   241
    (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
bulwahn@33250
   242
  end;
bulwahn@33250
   243
bulwahn@33250
   244
fun maps_premises f intro =
bulwahn@33250
   245
  let
bulwahn@33250
   246
    val (premises, head) = Logic.strip_horn intro
bulwahn@33250
   247
  in
bulwahn@33250
   248
    Logic.list_implies (maps f premises, head)
bulwahn@33250
   249
  end
bulwahn@33250
   250
  
bulwahn@33250
   251
(* lifting term operations to theorems *)
bulwahn@33250
   252
bulwahn@33250
   253
fun map_term thy f th =
bulwahn@33250
   254
  Skip_Proof.make_thm thy (f (prop_of th))
bulwahn@33250
   255
bulwahn@33250
   256
(*
bulwahn@33250
   257
fun equals_conv lhs_cv rhs_cv ct =
bulwahn@33250
   258
  case Thm.term_of ct of
bulwahn@33250
   259
    Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
bulwahn@33250
   260
  | _ => error "equals_conv"  
bulwahn@33250
   261
*)
bulwahn@33250
   262
bulwahn@33250
   263
(* Different options for compiler *)
bulwahn@33250
   264
bulwahn@33250
   265
datatype options = Options of {  
bulwahn@33619
   266
  expected_modes : (string * mode' list) option,
bulwahn@33623
   267
  proposed_modes : (string * mode' list) list,
bulwahn@33623
   268
  proposed_names : ((string * mode') * string) list,
bulwahn@33250
   269
  show_steps : bool,
bulwahn@33250
   270
  show_proof_trace : bool,
bulwahn@33250
   271
  show_intermediate_results : bool,
bulwahn@33251
   272
  show_mode_inference : bool,
bulwahn@33251
   273
  show_modes : bool,
bulwahn@33250
   274
  show_compilation : bool,
bulwahn@33250
   275
  skip_proof : bool,
bulwahn@33250
   276
bulwahn@33250
   277
  inductify : bool,
bulwahn@33375
   278
  random : bool,
bulwahn@33473
   279
  depth_limited : bool,
bulwahn@33473
   280
  annotated : bool
bulwahn@33250
   281
};
bulwahn@33250
   282
bulwahn@33250
   283
fun expected_modes (Options opt) = #expected_modes opt
bulwahn@33623
   284
fun proposed_modes (Options opt) name = AList.lookup (op =) (#proposed_modes opt) name
bulwahn@33623
   285
fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode')
bulwahn@33623
   286
  (#proposed_names opt) (name, mode)
bulwahn@33620
   287
bulwahn@33250
   288
fun show_steps (Options opt) = #show_steps opt
bulwahn@33250
   289
fun show_intermediate_results (Options opt) = #show_intermediate_results opt
bulwahn@33250
   290
fun show_proof_trace (Options opt) = #show_proof_trace opt
bulwahn@33251
   291
fun show_modes (Options opt) = #show_modes opt
bulwahn@33251
   292
fun show_mode_inference (Options opt) = #show_mode_inference opt
bulwahn@33250
   293
fun show_compilation (Options opt) = #show_compilation opt
bulwahn@33250
   294
fun skip_proof (Options opt) = #skip_proof opt
bulwahn@33250
   295
bulwahn@33250
   296
fun is_inductify (Options opt) = #inductify opt
bulwahn@33375
   297
fun is_random (Options opt) = #random opt
bulwahn@33250
   298
fun is_depth_limited (Options opt) = #depth_limited opt
bulwahn@33473
   299
fun is_annotated (Options opt) = #annotated opt
bulwahn@33250
   300
bulwahn@33250
   301
val default_options = Options {
bulwahn@33250
   302
  expected_modes = NONE,
bulwahn@33623
   303
  proposed_modes = [],
bulwahn@33623
   304
  proposed_names = [],
bulwahn@33250
   305
  show_steps = false,
bulwahn@33250
   306
  show_intermediate_results = false,
bulwahn@33250
   307
  show_proof_trace = false,
bulwahn@33251
   308
  show_modes = false,
bulwahn@33250
   309
  show_mode_inference = false,
bulwahn@33250
   310
  show_compilation = false,
bulwahn@33250
   311
  skip_proof = false,
bulwahn@33250
   312
  
bulwahn@33250
   313
  inductify = false,
bulwahn@33375
   314
  random = false,
bulwahn@33473
   315
  depth_limited = false,
bulwahn@33473
   316
  annotated = false
bulwahn@33250
   317
}
bulwahn@33250
   318
bulwahn@33250
   319
bulwahn@33250
   320
fun print_step options s =
bulwahn@33250
   321
  if show_steps options then tracing s else ()
bulwahn@33250
   322
bulwahn@33250
   323
(* tuple processing *)
bulwahn@33250
   324
bulwahn@33250
   325
fun expand_tuples thy intro =
bulwahn@33250
   326
  let
bulwahn@33250
   327
    fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
bulwahn@33250
   328
      | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
bulwahn@33250
   329
      (case HOLogic.strip_tupleT (fastype_of arg) of
bulwahn@33250
   330
        (Ts as _ :: _ :: _) =>
bulwahn@33250
   331
        let
bulwahn@33250
   332
          fun rewrite_arg' (Const ("Pair", _) $ _ $ t2, Type ("*", [_, T2]))
bulwahn@33250
   333
            (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
bulwahn@33250
   334
            | rewrite_arg' (t, Type ("*", [T1, T2])) (args, (pats, intro_t, ctxt)) =
bulwahn@33250
   335
              let
bulwahn@33250
   336
                val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
bulwahn@33250
   337
                val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
bulwahn@33250
   338
                val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
bulwahn@33250
   339
                val args' = map (Pattern.rewrite_term thy [pat] []) args
bulwahn@33250
   340
              in
bulwahn@33250
   341
                rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
bulwahn@33250
   342
              end
bulwahn@33250
   343
            | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
bulwahn@33250
   344
          val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
bulwahn@33250
   345
            (args, (pats, intro_t, ctxt))
bulwahn@33250
   346
        in
bulwahn@33250
   347
          rewrite_args args' (pats, intro_t', ctxt')
bulwahn@33250
   348
        end
bulwahn@33250
   349
      | _ => rewrite_args args (pats, intro_t, ctxt))
bulwahn@33250
   350
    fun rewrite_prem atom =
bulwahn@33250
   351
      let
bulwahn@33250
   352
        val (_, args) = strip_comb atom
bulwahn@33250
   353
      in rewrite_args args end
bulwahn@33250
   354
    val ctxt = ProofContext.init thy
bulwahn@33250
   355
    val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
bulwahn@33250
   356
    val intro_t = prop_of intro'
bulwahn@33250
   357
    val concl = Logic.strip_imp_concl intro_t
bulwahn@33250
   358
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
bulwahn@33250
   359
    val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
bulwahn@33250
   360
    val (pats', intro_t', ctxt3) = 
bulwahn@33250
   361
      fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
bulwahn@33250
   362
    fun rewrite_pat (ct1, ct2) =
bulwahn@33250
   363
      (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
bulwahn@33250
   364
    val t_insts' = map rewrite_pat t_insts
bulwahn@33250
   365
    val intro'' = Thm.instantiate (T_insts, t_insts') intro
bulwahn@33250
   366
    val [intro'''] = Variable.export ctxt3 ctxt [intro'']
bulwahn@33250
   367
    val intro'''' = Simplifier.full_simplify
bulwahn@33250
   368
      (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
bulwahn@33250
   369
      intro'''
bulwahn@33250
   370
    (* splitting conjunctions introduced by Pair_eq*)
bulwahn@33250
   371
    fun split_conj prem =
bulwahn@33250
   372
      map HOLogic.mk_Trueprop (conjuncts (HOLogic.dest_Trueprop prem))
bulwahn@33250
   373
    val intro''''' = map_term thy (maps_premises split_conj) intro''''
bulwahn@33250
   374
  in
bulwahn@33250
   375
    intro'''''
bulwahn@33250
   376
  end
bulwahn@33250
   377
bulwahn@33250
   378
end;