src/Pure/Isar/generic_target.ML
author haftmann
Tue Aug 10 14:42:30 2010 +0200 (2010-08-10 ago)
changeset 38310 9d4c0c74ae7d
parent 38309 9bd4e568c58c
child 38311 228566e1ab00
permissions -rw-r--r--
whitespace tuning
haftmann@38309
     1
(*  Title:      Pure/Isar/theory_target.ML
haftmann@38309
     2
    Author:     Makarius
haftmann@38309
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@38309
     4
haftmann@38309
     5
Common target infrastructure.
haftmann@38309
     6
*)
haftmann@38309
     7
haftmann@38309
     8
signature GENERIC_TARGET =
haftmann@38309
     9
sig
haftmann@38309
    10
  val define: (((binding * typ) * mixfix) * term -> binding -> term list
haftmann@38309
    11
    -> (string * sort) list * term list -> local_theory -> (term * thm) * local_theory)
haftmann@38309
    12
    -> (binding * mixfix) * (Attrib.binding * term) -> local_theory
haftmann@38309
    13
    -> (term * (string * thm)) * local_theory
haftmann@38309
    14
  val notes: (string
haftmann@38309
    15
    -> (Attrib.binding * (thm list * Args.src list) list) list
haftmann@38309
    16
    -> (Attrib.binding * (thm list * Args.src list) list) list
haftmann@38309
    17
    -> local_theory -> local_theory)
haftmann@38309
    18
    -> string -> (Attrib.binding * (thm list * Args.src list) list) list
haftmann@38309
    19
    -> local_theory -> (string * thm list) list * local_theory
haftmann@38310
    20
  val abbrev: (string * bool -> binding * mixfix -> term * term
haftmann@38310
    21
    -> (string * sort) list * term list -> local_theory -> local_theory)
haftmann@38310
    22
    -> string * bool -> (binding * mixfix) * term -> local_theory
haftmann@38310
    23
    -> (term * term) * local_theory
haftmann@38309
    24
end;
haftmann@38309
    25
haftmann@38309
    26
structure Generic_Target: GENERIC_TARGET =
haftmann@38309
    27
struct
haftmann@38309
    28
haftmann@38309
    29
(* define *)
haftmann@38309
    30
haftmann@38309
    31
fun define foundation ((b, mx), ((name, atts), rhs)) lthy =
haftmann@38309
    32
  let
haftmann@38309
    33
    val thy = ProofContext.theory_of lthy;
haftmann@38309
    34
    val thy_ctxt = ProofContext.init_global thy;
haftmann@38309
    35
haftmann@38309
    36
    val name' = Thm.def_binding_optional b name;
haftmann@38309
    37
haftmann@38309
    38
    (*term and type parameters*)
haftmann@38309
    39
    val crhs = Thm.cterm_of thy rhs;
haftmann@38309
    40
    val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
haftmann@38309
    41
    val rhs_conv = MetaSimplifier.rewrite true defs crhs;
haftmann@38309
    42
haftmann@38309
    43
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
haftmann@38309
    44
    val T = Term.fastype_of rhs;
haftmann@38309
    45
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
haftmann@38309
    46
    val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
haftmann@38309
    47
haftmann@38309
    48
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
haftmann@38309
    49
    val term_params =
haftmann@38309
    50
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
haftmann@38309
    51
      |> map_filter (fn (_, x) =>
haftmann@38309
    52
        (case AList.lookup (op =) xs x of
haftmann@38309
    53
          SOME T => SOME (Free (x, T))
haftmann@38309
    54
        | NONE => NONE));
haftmann@38309
    55
    val params = type_params @ term_params;
haftmann@38309
    56
haftmann@38309
    57
    val U = map Term.fastype_of params ---> T;
haftmann@38309
    58
haftmann@38309
    59
    (*foundation*)
haftmann@38309
    60
    val ((lhs', global_def), lthy3) = foundation
haftmann@38309
    61
      (((b, U), mx), rhs') name' params (extra_tfrees, type_params) lthy;
haftmann@38309
    62
haftmann@38309
    63
    (*local definition*)
haftmann@38309
    64
    val ((lhs, local_def), lthy4) = lthy3
haftmann@38309
    65
      |> Local_Defs.add_def ((b, NoSyn), lhs');
haftmann@38309
    66
    val def = Local_Defs.trans_terms lthy4
haftmann@38309
    67
      [(*c == global.c xs*)     local_def,
haftmann@38309
    68
       (*global.c xs == rhs'*)  global_def,
haftmann@38309
    69
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
haftmann@38309
    70
haftmann@38309
    71
    (*note*)
haftmann@38309
    72
    val ([(res_name, [res])], lthy5) = lthy4
haftmann@38309
    73
      |> Local_Theory.notes_kind "" [((name', atts), [([def], [])])];
haftmann@38309
    74
  in ((lhs, (res_name, res)), lthy5) end;
haftmann@38309
    75
haftmann@38309
    76
haftmann@38309
    77
(* notes *)
haftmann@38309
    78
haftmann@38309
    79
fun import_export_proof ctxt (name, raw_th) =
haftmann@38309
    80
  let
haftmann@38309
    81
    val thy = ProofContext.theory_of ctxt;
haftmann@38309
    82
    val thy_ctxt = ProofContext.init_global thy;
haftmann@38309
    83
    val certT = Thm.ctyp_of thy;
haftmann@38309
    84
    val cert = Thm.cterm_of thy;
haftmann@38309
    85
haftmann@38309
    86
    (*export assumes/defines*)
haftmann@38309
    87
    val th = Goal.norm_result raw_th;
haftmann@38309
    88
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
haftmann@38310
    89
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume)
haftmann@38310
    90
      (Assumption.all_assms_of ctxt);
haftmann@38309
    91
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
haftmann@38309
    92
haftmann@38309
    93
    (*export fixes*)
haftmann@38309
    94
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
haftmann@38309
    95
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
haftmann@38309
    96
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
haftmann@38309
    97
      |> Variable.export ctxt thy_ctxt
haftmann@38309
    98
      |> Drule.zero_var_indexes_list;
haftmann@38309
    99
haftmann@38309
   100
    (*thm definition*)
haftmann@38309
   101
    val result = PureThy.name_thm true true name th'';
haftmann@38309
   102
haftmann@38309
   103
    (*import fixes*)
haftmann@38309
   104
    val (tvars, vars) =
haftmann@38309
   105
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
haftmann@38309
   106
      |>> map Logic.dest_type;
haftmann@38309
   107
haftmann@38309
   108
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
haftmann@38309
   109
    val inst = filter (is_Var o fst) (vars ~~ frees);
haftmann@38309
   110
    val cinstT = map (pairself certT o apfst TVar) instT;
haftmann@38309
   111
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
haftmann@38309
   112
    val result' = Thm.instantiate (cinstT, cinst) result;
haftmann@38309
   113
haftmann@38309
   114
    (*import assumes/defines*)
haftmann@38309
   115
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
haftmann@38309
   116
    val result'' =
haftmann@38309
   117
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
haftmann@38309
   118
        NONE => raise THM ("Failed to re-import result", 0, [result'])
haftmann@38309
   119
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
haftmann@38309
   120
      |> Goal.norm_result
haftmann@38309
   121
      |> PureThy.name_thm false false name;
haftmann@38309
   122
haftmann@38309
   123
  in (result'', result) end;
haftmann@38309
   124
haftmann@38309
   125
fun notes target_notes kind facts lthy =
haftmann@38309
   126
  let
haftmann@38309
   127
    val thy = ProofContext.theory_of lthy;
haftmann@38309
   128
    val facts' = facts
haftmann@38309
   129
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi
haftmann@38309
   130
          (Local_Theory.full_name lthy (fst a))) bs))
haftmann@38309
   131
      |> PureThy.map_facts (import_export_proof lthy);
haftmann@38309
   132
    val local_facts = PureThy.map_facts #1 facts';
haftmann@38309
   133
    val global_facts = PureThy.map_facts #2 facts';
haftmann@38309
   134
  in
haftmann@38309
   135
    lthy
haftmann@38309
   136
    |> target_notes kind global_facts local_facts
haftmann@38309
   137
    |> ProofContext.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
haftmann@38309
   138
  end;
haftmann@38309
   139
haftmann@38309
   140
haftmann@38309
   141
(* abbrev *)
haftmann@38309
   142
haftmann@38309
   143
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
haftmann@38309
   144
  let
haftmann@38309
   145
    val thy_ctxt = ProofContext.init_global (ProofContext.theory_of lthy);
haftmann@38309
   146
    val target_ctxt = Local_Theory.target_of lthy;
haftmann@38309
   147
haftmann@38309
   148
    val t' = Assumption.export_term lthy target_ctxt t;
haftmann@38309
   149
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
haftmann@38309
   150
    val u = fold_rev lambda xs t';
haftmann@38309
   151
haftmann@38309
   152
    val extra_tfrees =
haftmann@38309
   153
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
haftmann@38309
   154
haftmann@38309
   155
    val global_rhs =
haftmann@38309
   156
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
haftmann@38309
   157
  in
haftmann@38309
   158
    lthy
haftmann@38309
   159
    |> target_abbrev prmode (b, mx) (global_rhs, t') (extra_tfrees, xs)
haftmann@38309
   160
    |> ProofContext.add_abbrev Print_Mode.internal (b, t) |> snd
haftmann@38309
   161
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
haftmann@38309
   162
  end;
haftmann@38309
   163
haftmann@38309
   164
end;