src/Pure/Isar/generic_target.ML
author wenzelm
Thu, 22 Mar 2012 15:41:49 +0100
changeset 47081 5e70b457b704
parent 47080 bfad2f674d0e
child 47238 289dcbdd5984
permissions -rw-r--r--
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters); uniform treatment of target contexts as invisible; added Local_Theory.standard_form convenience;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 41228
diff changeset
     1
(*  Title:      Pure/Isar/generic_target.ML
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     2
    Author:     Makarius
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     4
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     5
Common target infrastructure.
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     6
*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     7
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     8
signature GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     9
sig
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    10
  val define: (((binding * typ) * mixfix) * (binding * term) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    11
      term list * term list -> local_theory -> (term * thm) * local_theory) ->
46992
eeea81b86b70 refined Local_Theory.define vs. Local_Theory.define_internal, which allows to pass alternative name to the foundational axiom -- expecially important for 'instantiation' or 'overloading', which loose name information due to Long_Name.base_name cooking etc.;
wenzelm
parents: 46916
diff changeset
    12
    bool -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    13
    (term * (string * thm)) * local_theory
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    14
  val notes: (string -> (Attrib.binding * (thm list * Args.src list) list) list ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    15
      (Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> local_theory) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    16
    string -> (Attrib.binding * (thm list * Args.src list) list) list -> local_theory ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    17
    (string * thm list) list * local_theory
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    18
  val abbrev: (string * bool -> binding * mixfix -> term * term ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    19
      term list -> local_theory -> local_theory) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    20
    string * bool -> (binding * mixfix) * term -> local_theory ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    21
    (term * term) * local_theory
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
    22
47081
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
    23
  val background_declaration: declaration -> local_theory -> local_theory
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
    24
  val standard_declaration: declaration -> local_theory -> local_theory
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
    25
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    26
  val theory_foundation: ((binding * typ) * mixfix) * (binding * term) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    27
    term list * term list -> local_theory -> (term * thm) * local_theory
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    28
  val theory_notes: string -> (Attrib.binding * (thm list * Args.src list) list) list ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    29
    local_theory -> local_theory
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    30
  val theory_abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory -> local_theory
45353
wenzelm
parents: 45352
diff changeset
    31
end
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    32
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    33
structure Generic_Target: GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    34
struct
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    35
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
    36
(** lifting primitive to target operations **)
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
    37
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    38
(* mixfix syntax *)
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    39
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    40
fun check_mixfix ctxt (b, extra_tfrees) mx =
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    41
  if null extra_tfrees then mx
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    42
  else
38831
4933a3dfd745 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38757
diff changeset
    43
    (Context_Position.if_visible ctxt warning
42381
309ec68442c6 added Binding.print convenience, which includes quote already;
wenzelm
parents: 42375
diff changeset
    44
      ("Additional type variable(s) in specification of " ^ Binding.print b ^ ": " ^
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    45
        commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    46
        (if mx = NoSyn then ""
42287
d98eb048a2e4 discontinued special treatment of structure Mixfix;
wenzelm
parents: 41959
diff changeset
    47
         else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx)));
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    48
      NoSyn);
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    49
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    50
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    51
(* define *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    52
46992
eeea81b86b70 refined Local_Theory.define vs. Local_Theory.define_internal, which allows to pass alternative name to the foundational axiom -- expecially important for 'instantiation' or 'overloading', which loose name information due to Long_Name.base_name cooking etc.;
wenzelm
parents: 46916
diff changeset
    53
fun define foundation internal ((b, mx), ((b_def, atts), rhs)) lthy =
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    54
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
    55
    val thy = Proof_Context.theory_of lthy;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
    56
    val thy_ctxt = Proof_Context.init_global thy;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    57
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    58
    (*term and type parameters*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    59
    val crhs = Thm.cterm_of thy rhs;
45407
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
    60
    val ((defs, _), rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40782
diff changeset
    61
    val rhs_conv = Raw_Simplifier.rewrite true defs crhs;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    62
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    63
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    64
    val T = Term.fastype_of rhs;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    65
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    66
    val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs []));
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    67
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    68
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    69
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
42488
4638622bcaa1 reorganized fixes as specialized (global) name space;
wenzelm
parents: 42381
diff changeset
    70
    val target_ctxt = Local_Theory.target_of lthy;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    71
    val term_params =
42488
4638622bcaa1 reorganized fixes as specialized (global) name space;
wenzelm
parents: 42381
diff changeset
    72
      filter (Variable.is_fixed target_ctxt o #1) xs
4638622bcaa1 reorganized fixes as specialized (global) name space;
wenzelm
parents: 42381
diff changeset
    73
      |> sort (Variable.fixed_ord target_ctxt o pairself #1)
4638622bcaa1 reorganized fixes as specialized (global) name space;
wenzelm
parents: 42381
diff changeset
    74
      |> map Free;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    75
    val params = type_params @ term_params;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    76
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    77
    val U = map Term.fastype_of params ---> T;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    78
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    79
    (*foundation*)
45353
wenzelm
parents: 45352
diff changeset
    80
    val ((lhs', global_def), lthy2) = lthy
wenzelm
parents: 45352
diff changeset
    81
      |> foundation (((b, U), mx'), (b_def, rhs')) (type_params, term_params);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    82
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    83
    (*local definition*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    84
    val ((lhs, local_def), lthy3) = lthy2
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    85
      |> Local_Defs.add_def ((b, NoSyn), lhs');
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    86
    val def = Local_Defs.trans_terms lthy3
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    87
      [(*c == global.c xs*)     local_def,
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    88
       (*global.c xs == rhs'*)  global_def,
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    89
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    90
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    91
    (*note*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    92
    val ([(res_name, [res])], lthy4) = lthy3
47080
wenzelm
parents: 46992
diff changeset
    93
      |> Local_Theory.notes [((if internal then Binding.empty else b_def, atts), [([def], [])])];
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    94
  in ((lhs, (res_name, res)), lthy4) end;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    95
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    96
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    97
(* notes *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    98
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    99
fun import_export_proof ctxt (name, raw_th) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   100
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   101
    val thy = Proof_Context.theory_of ctxt;
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   102
    val thy_ctxt = Proof_Context.init_global thy;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   103
    val certT = Thm.ctyp_of thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   104
    val cert = Thm.cterm_of thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   105
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   106
    (*export assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   107
    val th = Goal.norm_result raw_th;
45407
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   108
    val ((defs, asms), th') = Local_Defs.export ctxt thy_ctxt th;
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   109
    val asms' = map (Raw_Simplifier.rewrite_rule defs) asms;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   110
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   111
    (*export fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   112
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   113
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
45352
0b4038361a3a misc tuning;
wenzelm
parents: 45310
diff changeset
   114
    val (th'' :: vs) =
0b4038361a3a misc tuning;
wenzelm
parents: 45310
diff changeset
   115
      (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   116
      |> Variable.export ctxt thy_ctxt
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   117
      |> Drule.zero_var_indexes_list;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   118
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   119
    (*thm definition*)
45413
117ff038f8f7 disabled Thm.compress (again) -- costs for building tables tend to be higher than potential benefit;
wenzelm
parents: 45407
diff changeset
   120
    val result = Global_Theory.name_thm true true name th'';
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   121
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   122
    (*import fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   123
    val (tvars, vars) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   124
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   125
      |>> map Logic.dest_type;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   126
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   127
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   128
    val inst = filter (is_Var o fst) (vars ~~ frees);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   129
    val cinstT = map (pairself certT o apfst TVar) instT;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   130
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   131
    val result' = Thm.instantiate (cinstT, cinst) result;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   132
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   133
    (*import assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   134
    val result'' =
45407
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   135
      (fold (curry op COMP) asms' result'
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   136
        handle THM _ => raise THM ("Failed to re-import result", 0, result' :: asms'))
a83574606719 more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents: 45390
diff changeset
   137
      |> Local_Defs.contract ctxt defs (Thm.cprop_of th)
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   138
      |> Goal.norm_result
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38831
diff changeset
   139
      |> Global_Theory.name_thm false false name;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   140
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   141
  in (result'', result) end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   142
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   143
fun notes target_notes kind facts lthy =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   144
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   145
    val thy = Proof_Context.theory_of lthy;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   146
    val facts' = facts
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38831
diff changeset
   147
      |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   148
          (Local_Theory.full_name lthy (fst a))) bs))
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38831
diff changeset
   149
      |> Global_Theory.map_facts (import_export_proof lthy);
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38831
diff changeset
   150
    val local_facts = Global_Theory.map_facts #1 facts';
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38831
diff changeset
   151
    val global_facts = Global_Theory.map_facts #2 facts';
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   152
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   153
    lthy
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   154
    |> target_notes kind global_facts local_facts
45390
e29521ef9059 tuned signature -- avoid spurious Thm.mixed_attribute;
wenzelm
parents: 45353
diff changeset
   155
    |> Proof_Context.note_thmss kind (Attrib.map_facts (map (Attrib.attribute_i thy)) local_facts)
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   156
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   157
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   158
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   159
(* abbrev *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   160
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   161
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   162
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   163
    val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   164
    val target_ctxt = Local_Theory.target_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   165
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   166
    val t' = Assumption.export_term lthy target_ctxt t;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   167
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   168
    val u = fold_rev lambda xs t';
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   169
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   170
    val extra_tfrees =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   171
      subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []);
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   172
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   173
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   174
    val global_rhs =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   175
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   176
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   177
    lthy
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   178
    |> target_abbrev prmode (b, mx') (global_rhs, t') xs
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   179
    |> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   180
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   181
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   182
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   183
47081
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   184
(* declaration *)
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   185
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   186
fun background_declaration decl lthy =
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   187
  let
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   188
    val theory_decl =
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   189
      Local_Theory.standard_form lthy
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   190
        (Proof_Context.init_global (Proof_Context.theory_of lthy)) decl;
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   191
  in Local_Theory.background_theory (Context.theory_map theory_decl) lthy end;
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   192
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   193
fun standard_declaration decl =
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   194
  background_declaration decl #>
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   195
  (fn lthy => Local_Theory.map_contexts (fn ctxt =>
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   196
    Context.proof_map (Local_Theory.standard_form lthy ctxt decl) ctxt) lthy);
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   197
5e70b457b704 uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents: 47080
diff changeset
   198
45352
0b4038361a3a misc tuning;
wenzelm
parents: 45310
diff changeset
   199
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   200
(** primitive theory operations **)
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   201
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   202
fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy =
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   203
  let
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42360
diff changeset
   204
    val (const, lthy2) = lthy
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42360
diff changeset
   205
      |> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx));
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   206
    val lhs = list_comb (const, type_params @ term_params);
42375
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42360
diff changeset
   207
    val ((_, def), lthy3) = lthy2
774df7c59508 report Name_Space.declare/define, relatively to context;
wenzelm
parents: 42360
diff changeset
   208
      |> Local_Theory.background_theory_result
46916
e7ea35b41e2d Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents: 45413
diff changeset
   209
        (Thm.add_def lthy2 false false
e7ea35b41e2d Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents: 45413
diff changeset
   210
          (Thm.def_binding_optional b b_def, Logic.mk_equals (lhs, rhs)));
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   211
  in ((lhs, def), lthy3) end;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   212
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   213
fun theory_notes kind global_facts lthy =
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   214
  let
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   215
    val thy = Proof_Context.theory_of lthy;
45390
e29521ef9059 tuned signature -- avoid spurious Thm.mixed_attribute;
wenzelm
parents: 45353
diff changeset
   216
    val global_facts' = Attrib.map_facts (map (Attrib.attribute_i thy)) global_facts;
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   217
  in
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   218
    lthy
39557
fe5722fce758 renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents: 38831
diff changeset
   219
    |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 42287
diff changeset
   220
    |> Local_Theory.target (Proof_Context.note_thmss kind global_facts' #> snd)
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   221
  end;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   222
38757
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38341
diff changeset
   223
fun theory_abbrev prmode ((b, mx), t) =
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38341
diff changeset
   224
  Local_Theory.background_theory
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38341
diff changeset
   225
    (Sign.add_abbrev (#1 prmode) (b, t) #->
2b3e054ae6fc renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents: 38341
diff changeset
   226
      (fn (lhs, _) => Sign.notation true prmode [(lhs, mx)]));
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   227
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   228
end;