src/Pure/Isar/generic_target.ML
author wenzelm
Wed Apr 27 17:58:45 2011 +0200 (2011-04-27 ago)
changeset 42488 4638622bcaa1
parent 42381 309ec68442c6
child 43795 ca5896a836ba
permissions -rw-r--r--
reorganized fixes as specialized (global) name space;
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
wenzelm@42381
    42
      ("Additional type variable(s) in specification of " ^ Binding.print 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;
wenzelm@42488
    70
    val target_ctxt = Local_Theory.target_of lthy;
haftmann@38309
    71
    val term_params =
wenzelm@42488
    72
      filter (Variable.is_fixed target_ctxt o #1) xs
wenzelm@42488
    73
      |> sort (Variable.fixed_ord target_ctxt o pairself #1)
wenzelm@42488
    74
      |> map Free;
haftmann@38309
    75
    val params = type_params @ term_params;
haftmann@38309
    76
haftmann@38309
    77
    val U = map Term.fastype_of params ---> T;
haftmann@38309
    78
haftmann@38309
    79
    (*foundation*)
haftmann@38315
    80
    val ((lhs', global_def), lthy2) = foundation
haftmann@38313
    81
      (((b, U), mx'), (b_def, rhs')) (type_params, term_params) lthy;
haftmann@38309
    82
haftmann@38309
    83
    (*local definition*)
haftmann@38315
    84
    val ((lhs, local_def), lthy3) = lthy2
haftmann@38309
    85
      |> Local_Defs.add_def ((b, NoSyn), lhs');
haftmann@38315
    86
    val def = Local_Defs.trans_terms lthy3
haftmann@38309
    87
      [(*c == global.c xs*)     local_def,
haftmann@38309
    88
       (*global.c xs == rhs'*)  global_def,
haftmann@38309
    89
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
haftmann@38309
    90
haftmann@38309
    91
    (*note*)
haftmann@38315
    92
    val ([(res_name, [res])], lthy4) = lthy3
haftmann@38311
    93
      |> Local_Theory.notes_kind "" [((b_def, atts), [([def], [])])];
haftmann@38315
    94
  in ((lhs, (res_name, res)), lthy4) end;
haftmann@38309
    95
haftmann@38309
    96
haftmann@38309
    97
(* notes *)
haftmann@38309
    98
haftmann@38309
    99
fun import_export_proof ctxt (name, raw_th) =
haftmann@38309
   100
  let
wenzelm@42360
   101
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42360
   102
    val thy_ctxt = Proof_Context.init_global thy;
haftmann@38309
   103
    val certT = Thm.ctyp_of thy;
haftmann@38309
   104
    val cert = Thm.cterm_of thy;
haftmann@38309
   105
haftmann@38309
   106
    (*export assumes/defines*)
haftmann@38309
   107
    val th = Goal.norm_result raw_th;
haftmann@38309
   108
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
wenzelm@41228
   109
    val assms = map (Raw_Simplifier.rewrite_rule defs o Thm.assume)
haftmann@38310
   110
      (Assumption.all_assms_of ctxt);
haftmann@38309
   111
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
haftmann@38309
   112
haftmann@38309
   113
    (*export fixes*)
haftmann@38309
   114
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
haftmann@38309
   115
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
haftmann@38309
   116
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
haftmann@38309
   117
      |> Variable.export ctxt thy_ctxt
haftmann@38309
   118
      |> Drule.zero_var_indexes_list;
haftmann@38309
   119
haftmann@38309
   120
    (*thm definition*)
wenzelm@39557
   121
    val result = Global_Theory.name_thm true true name th'';
haftmann@38309
   122
haftmann@38309
   123
    (*import fixes*)
haftmann@38309
   124
    val (tvars, vars) =
haftmann@38309
   125
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
haftmann@38309
   126
      |>> map Logic.dest_type;
haftmann@38309
   127
haftmann@38309
   128
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
haftmann@38309
   129
    val inst = filter (is_Var o fst) (vars ~~ frees);
haftmann@38309
   130
    val cinstT = map (pairself certT o apfst TVar) instT;
haftmann@38309
   131
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
haftmann@38309
   132
    val result' = Thm.instantiate (cinstT, cinst) result;
haftmann@38309
   133
haftmann@38309
   134
    (*import assumes/defines*)
haftmann@38309
   135
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
haftmann@38309
   136
    val result'' =
haftmann@38309
   137
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
haftmann@38309
   138
        NONE => raise THM ("Failed to re-import result", 0, [result'])
haftmann@38309
   139
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
haftmann@38309
   140
      |> Goal.norm_result
wenzelm@39557
   141
      |> Global_Theory.name_thm false false name;
haftmann@38309
   142
haftmann@38309
   143
  in (result'', result) end;
haftmann@38309
   144
haftmann@38309
   145
fun notes target_notes kind facts lthy =
haftmann@38309
   146
  let
wenzelm@42360
   147
    val thy = Proof_Context.theory_of lthy;
haftmann@38309
   148
    val facts' = facts
wenzelm@39557
   149
      |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
haftmann@38309
   150
          (Local_Theory.full_name lthy (fst a))) bs))
wenzelm@39557
   151
      |> Global_Theory.map_facts (import_export_proof lthy);
wenzelm@39557
   152
    val local_facts = Global_Theory.map_facts #1 facts';
wenzelm@39557
   153
    val global_facts = Global_Theory.map_facts #2 facts';
haftmann@38309
   154
  in
haftmann@38309
   155
    lthy
haftmann@38309
   156
    |> target_notes kind global_facts local_facts
wenzelm@42360
   157
    |> Proof_Context.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
haftmann@38309
   158
  end;
haftmann@38309
   159
haftmann@38309
   160
haftmann@38309
   161
(* abbrev *)
haftmann@38309
   162
haftmann@38309
   163
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
haftmann@38309
   164
  let
wenzelm@42360
   165
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
haftmann@38309
   166
    val target_ctxt = Local_Theory.target_of lthy;
haftmann@38309
   167
haftmann@38309
   168
    val t' = Assumption.export_term lthy target_ctxt t;
haftmann@38309
   169
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
haftmann@38309
   170
    val u = fold_rev lambda xs t';
haftmann@38309
   171
haftmann@38309
   172
    val extra_tfrees =
haftmann@38309
   173
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
haftmann@38312
   174
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
haftmann@38309
   175
haftmann@38309
   176
    val global_rhs =
haftmann@38309
   177
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
haftmann@38309
   178
  in
haftmann@38309
   179
    lthy
haftmann@38312
   180
    |> target_abbrev prmode (b, mx') (global_rhs, t') xs
wenzelm@42360
   181
    |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
haftmann@38309
   182
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
haftmann@38309
   183
  end;
haftmann@38309
   184
haftmann@38341
   185
haftmann@38341
   186
(** primitive theory operations **)
haftmann@38341
   187
haftmann@38341
   188
fun theory_declaration decl lthy =
haftmann@38341
   189
  let
haftmann@38341
   190
    val global_decl = Morphism.form
haftmann@38341
   191
      (Morphism.transform (Local_Theory.global_morphism lthy) decl);
haftmann@38341
   192
  in
haftmann@38341
   193
    lthy
wenzelm@38757
   194
    |> Local_Theory.background_theory (Context.theory_map global_decl)
haftmann@38341
   195
    |> Local_Theory.target (Context.proof_map global_decl)
haftmann@38341
   196
  end;
haftmann@38341
   197
haftmann@38341
   198
fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
haftmann@38341
   199
  let
wenzelm@42375
   200
    val (const, lthy2) = lthy
wenzelm@42375
   201
      |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx));
haftmann@38341
   202
    val lhs = list_comb (const, type_params @ term_params);
wenzelm@42375
   203
    val ((_, def), lthy3) = lthy2
wenzelm@42375
   204
      |> Local_Theory.background_theory_result
wenzelm@42375
   205
        (Thm.add_def lthy2 false false (b_def, Logic.mk_equals (lhs, rhs)));
haftmann@38341
   206
  in ((lhs, def), lthy3) end;
haftmann@38341
   207
haftmann@38341
   208
fun theory_notes kind global_facts lthy =
haftmann@38341
   209
  let
wenzelm@42360
   210
    val thy = Proof_Context.theory_of lthy;
haftmann@38341
   211
    val global_facts' = Attrib.map_facts (Attrib.attribute_i thy) global_facts;
haftmann@38341
   212
  in
haftmann@38341
   213
    lthy
wenzelm@39557
   214
    |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
wenzelm@42360
   215
    |> Local_Theory.target (Proof_Context.note_thmss kind global_facts' #> snd)
haftmann@38341
   216
  end;
haftmann@38341
   217
wenzelm@38757
   218
fun theory_abbrev prmode ((b, mx), t) =
wenzelm@38757
   219
  Local_Theory.background_theory
wenzelm@38757
   220
    (Sign.add_abbrev (#1 prmode) (b, t) #->
wenzelm@38757
   221
      (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
haftmann@38341
   222
haftmann@38309
   223
end;