src/Pure/Isar/generic_target.ML
author wenzelm
Sun Apr 17 19:54:04 2011 +0200 (2011-04-17 ago)
changeset 42375 774df7c59508
parent 42360 da8817d01e7c
child 42381 309ec68442c6
permissions -rw-r--r--
report Name_Space.declare/define, relatively to context;
added "global" variants of context-dependent declarations;
wenzelm@41959
     1
(*  Title:      Pure/Isar/generic_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
wenzelm@40782
    10
  val define: (((binding * typ) * mixfix) * (binding * term) ->
wenzelm@40782
    11
      term list * term list -> local_theory -> (term * thm) * local_theory) ->
wenzelm@40782
    12
    (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@40782
    13
    (term * (string * thm)) * local_theory
wenzelm@40782
    14
  val notes: (string -> (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@40782
    15
      (Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> local_theory) ->
wenzelm@40782
    16
    string -> (Attrib.binding * (thm list * Args.src list) list) list -> local_theory ->
wenzelm@40782
    17
    (string * thm list) list * local_theory
wenzelm@40782
    18
  val abbrev: (string * bool -> binding * mixfix -> term * term ->
wenzelm@40782
    19
      term list -> local_theory -> local_theory) ->
wenzelm@40782
    20
    string * bool -> (binding * mixfix) * term -> local_theory ->
wenzelm@40782
    21
    (term * term) * local_theory
haftmann@38341
    22
haftmann@38341
    23
  val theory_declaration: declaration -> local_theory -> local_theory
wenzelm@40782
    24
  val theory_foundation: ((binding * typ) * mixfix) * (binding * term) ->
wenzelm@40782
    25
    term list * term list -> local_theory -> (term * thm) * local_theory
wenzelm@40782
    26
  val theory_notes: string -> (Attrib.binding * (thm list * Args.src list) list) list ->
wenzelm@40782
    27
    local_theory -> local_theory
wenzelm@40782
    28
  val theory_abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory -> local_theory
haftmann@38309
    29
end;
haftmann@38309
    30
haftmann@38309
    31
structure Generic_Target: GENERIC_TARGET =
haftmann@38309
    32
struct
haftmann@38309
    33
haftmann@38341
    34
(** lifting primitive to target operations **)
haftmann@38341
    35
haftmann@38312
    36
(* mixfix syntax *)
haftmann@38312
    37
haftmann@38312
    38
fun check_mixfix ctxt (b, extra_tfrees) mx =
haftmann@38312
    39
  if null extra_tfrees then mx
haftmann@38312
    40
  else
wenzelm@38831
    41
    (Context_Position.if_visible ctxt warning
haftmann@38312
    42
      ("Additional type variable(s) in specification of " ^ Binding.str_of b ^ ": " ^
haftmann@38312
    43
        commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
haftmann@38312
    44
        (if mx = NoSyn then ""
wenzelm@42287
    45
         else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx)));
haftmann@38312
    46
      NoSyn);
haftmann@38312
    47
haftmann@38312
    48
haftmann@38309
    49
(* define *)
haftmann@38309
    50
haftmann@38311
    51
fun define foundation ((b, mx), ((proto_b_def, atts), rhs)) lthy =
haftmann@38309
    52
  let
wenzelm@42360
    53
    val thy = Proof_Context.theory_of lthy;
wenzelm@42360
    54
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@38309
    55
haftmann@38311
    56
    val b_def = Thm.def_binding_optional b proto_b_def;
haftmann@38309
    57
haftmann@38309
    58
    (*term and type parameters*)
haftmann@38309
    59
    val crhs = Thm.cterm_of thy rhs;
haftmann@38309
    60
    val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
wenzelm@41228
    61
    val rhs_conv = Raw_Simplifier.rewrite true defs crhs;
haftmann@38309
    62
haftmann@38309
    63
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
haftmann@38309
    64
    val T = Term.fastype_of rhs;
haftmann@38309
    65
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
haftmann@38309
    66
    val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
haftmann@38312
    67
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
haftmann@38309
    68
haftmann@38309
    69
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
haftmann@38309
    70
    val term_params =
haftmann@38309
    71
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
haftmann@38309
    72
      |> map_filter (fn (_, x) =>
haftmann@38309
    73
        (case AList.lookup (op =) xs x of
haftmann@38309
    74
          SOME T => SOME (Free (x, T))
haftmann@38309
    75
        | NONE => NONE));
haftmann@38309
    76
    val params = type_params @ term_params;
haftmann@38309
    77
haftmann@38309
    78
    val U = map Term.fastype_of params ---> T;
haftmann@38309
    79
haftmann@38309
    80
    (*foundation*)
haftmann@38315
    81
    val ((lhs', global_def), lthy2) = foundation
haftmann@38313
    82
      (((b, U), mx'), (b_def, rhs')) (type_params, term_params) lthy;
haftmann@38309
    83
haftmann@38309
    84
    (*local definition*)
haftmann@38315
    85
    val ((lhs, local_def), lthy3) = lthy2
haftmann@38309
    86
      |> Local_Defs.add_def ((b, NoSyn), lhs');
haftmann@38315
    87
    val def = Local_Defs.trans_terms lthy3
haftmann@38309
    88
      [(*c == global.c xs*)     local_def,
haftmann@38309
    89
       (*global.c xs == rhs'*)  global_def,
haftmann@38309
    90
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
haftmann@38309
    91
haftmann@38309
    92
    (*note*)
haftmann@38315
    93
    val ([(res_name, [res])], lthy4) = lthy3
haftmann@38311
    94
      |> Local_Theory.notes_kind "" [((b_def, atts), [([def], [])])];
haftmann@38315
    95
  in ((lhs, (res_name, res)), lthy4) end;
haftmann@38309
    96
haftmann@38309
    97
haftmann@38309
    98
(* notes *)
haftmann@38309
    99
haftmann@38309
   100
fun import_export_proof ctxt (name, raw_th) =
haftmann@38309
   101
  let
wenzelm@42360
   102
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42360
   103
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@38309
   104
    val certT = Thm.ctyp_of thy;
haftmann@38309
   105
    val cert = Thm.cterm_of thy;
haftmann@38309
   106
haftmann@38309
   107
    (*export assumes/defines*)
haftmann@38309
   108
    val th = Goal.norm_result raw_th;
haftmann@38309
   109
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
wenzelm@41228
   110
    val assms = map (Raw_Simplifier.rewrite_rule defs o Thm.assume)
haftmann@38310
   111
      (Assumption.all_assms_of ctxt);
haftmann@38309
   112
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
haftmann@38309
   113
haftmann@38309
   114
    (*export fixes*)
haftmann@38309
   115
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
haftmann@38309
   116
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
haftmann@38309
   117
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
haftmann@38309
   118
      |> Variable.export ctxt thy_ctxt
haftmann@38309
   119
      |> Drule.zero_var_indexes_list;
haftmann@38309
   120
haftmann@38309
   121
    (*thm definition*)
wenzelm@39557
   122
    val result = Global_Theory.name_thm true true name th'';
haftmann@38309
   123
haftmann@38309
   124
    (*import fixes*)
haftmann@38309
   125
    val (tvars, vars) =
haftmann@38309
   126
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
haftmann@38309
   127
      |>> map Logic.dest_type;
haftmann@38309
   128
haftmann@38309
   129
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
haftmann@38309
   130
    val inst = filter (is_Var o fst) (vars ~~ frees);
haftmann@38309
   131
    val cinstT = map (pairself certT o apfst TVar) instT;
haftmann@38309
   132
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
haftmann@38309
   133
    val result' = Thm.instantiate (cinstT, cinst) result;
haftmann@38309
   134
haftmann@38309
   135
    (*import assumes/defines*)
haftmann@38309
   136
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
haftmann@38309
   137
    val result'' =
haftmann@38309
   138
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
haftmann@38309
   139
        NONE => raise THM ("Failed to re-import result", 0, [result'])
haftmann@38309
   140
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
haftmann@38309
   141
      |> Goal.norm_result
wenzelm@39557
   142
      |> Global_Theory.name_thm false false name;
haftmann@38309
   143
haftmann@38309
   144
  in (result'', result) end;
haftmann@38309
   145
haftmann@38309
   146
fun notes target_notes kind facts lthy =
haftmann@38309
   147
  let
wenzelm@42360
   148
    val thy = Proof_Context.theory_of lthy;
haftmann@38309
   149
    val facts' = facts
wenzelm@39557
   150
      |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
haftmann@38309
   151
          (Local_Theory.full_name lthy (fst a))) bs))
wenzelm@39557
   152
      |> Global_Theory.map_facts (import_export_proof lthy);
wenzelm@39557
   153
    val local_facts = Global_Theory.map_facts #1 facts';
wenzelm@39557
   154
    val global_facts = Global_Theory.map_facts #2 facts';
haftmann@38309
   155
  in
haftmann@38309
   156
    lthy
haftmann@38309
   157
    |> target_notes kind global_facts local_facts
wenzelm@42360
   158
    |> Proof_Context.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
haftmann@38309
   159
  end;
haftmann@38309
   160
haftmann@38309
   161
haftmann@38309
   162
(* abbrev *)
haftmann@38309
   163
haftmann@38309
   164
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
haftmann@38309
   165
  let
wenzelm@42360
   166
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
haftmann@38309
   167
    val target_ctxt = Local_Theory.target_of lthy;
haftmann@38309
   168
haftmann@38309
   169
    val t' = Assumption.export_term lthy target_ctxt t;
haftmann@38309
   170
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
haftmann@38309
   171
    val u = fold_rev lambda xs t';
haftmann@38309
   172
haftmann@38309
   173
    val extra_tfrees =
haftmann@38309
   174
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
haftmann@38312
   175
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
haftmann@38309
   176
haftmann@38309
   177
    val global_rhs =
haftmann@38309
   178
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
haftmann@38309
   179
  in
haftmann@38309
   180
    lthy
haftmann@38312
   181
    |> target_abbrev prmode (b, mx') (global_rhs, t') xs
wenzelm@42360
   182
    |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
haftmann@38309
   183
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
haftmann@38309
   184
  end;
haftmann@38309
   185
haftmann@38341
   186
haftmann@38341
   187
(** primitive theory operations **)
haftmann@38341
   188
haftmann@38341
   189
fun theory_declaration decl lthy =
haftmann@38341
   190
  let
haftmann@38341
   191
    val global_decl = Morphism.form
haftmann@38341
   192
      (Morphism.transform (Local_Theory.global_morphism lthy) decl);
haftmann@38341
   193
  in
haftmann@38341
   194
    lthy
wenzelm@38757
   195
    |> Local_Theory.background_theory (Context.theory_map global_decl)
haftmann@38341
   196
    |> Local_Theory.target (Context.proof_map global_decl)
haftmann@38341
   197
  end;
haftmann@38341
   198
haftmann@38341
   199
fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
haftmann@38341
   200
  let
wenzelm@42375
   201
    val (const, lthy2) = lthy
wenzelm@42375
   202
      |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx));
haftmann@38341
   203
    val lhs = list_comb (const, type_params @ term_params);
wenzelm@42375
   204
    val ((_, def), lthy3) = lthy2
wenzelm@42375
   205
      |> Local_Theory.background_theory_result
wenzelm@42375
   206
        (Thm.add_def lthy2 false false (b_def, Logic.mk_equals (lhs, rhs)));
haftmann@38341
   207
  in ((lhs, def), lthy3) end;
haftmann@38341
   208
haftmann@38341
   209
fun theory_notes kind global_facts lthy =
haftmann@38341
   210
  let
wenzelm@42360
   211
    val thy = Proof_Context.theory_of lthy;
haftmann@38341
   212
    val global_facts' = Attrib.map_facts (Attrib.attribute_i thy) global_facts;
haftmann@38341
   213
  in
haftmann@38341
   214
    lthy
wenzelm@39557
   215
    |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
wenzelm@42360
   216
    |> Local_Theory.target (Proof_Context.note_thmss kind global_facts' #> snd)
haftmann@38341
   217
  end;
haftmann@38341
   218
wenzelm@38757
   219
fun theory_abbrev prmode ((b, mx), t) =
wenzelm@38757
   220
  Local_Theory.background_theory
wenzelm@38757
   221
    (Sign.add_abbrev (#1 prmode) (b, t) #->
wenzelm@38757
   222
      (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
haftmann@38341
   223
haftmann@38309
   224
end;