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