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