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