src/Pure/Isar/generic_target.ML
author wenzelm
Fri, 17 Dec 2010 17:08:56 +0100
changeset 41228 e1fce873b814
parent 40782 aa533c5e3f48
child 41959 b460124855b8
permissions -rw-r--r--
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Isar/theory_target.ML
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) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    12
    (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
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
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
    23
  val theory_declaration: declaration -> local_theory -> local_theory
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    24
  val theory_foundation: ((binding * typ) * mixfix) * (binding * term) ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    25
    term list * term list -> local_theory -> (term * thm) * local_theory
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    26
  val theory_notes: string -> (Attrib.binding * (thm list * Args.src list) list) list ->
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    27
    local_theory -> local_theory
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39557
diff changeset
    28
  val theory_abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory -> local_theory
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    29
end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    30
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    31
structure Generic_Target: GENERIC_TARGET =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    32
struct
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    33
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
    34
(** lifting primitive to target operations **)
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
    35
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    36
(* mixfix syntax *)
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    37
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    38
fun check_mixfix ctxt (b, extra_tfrees) mx =
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    39
  if null extra_tfrees then mx
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    40
  else
38831
4933a3dfd745 more careful treatment of context visibility flag wrt. spurious warnings;
wenzelm
parents: 38757
diff changeset
    41
    (Context_Position.if_visible ctxt warning
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    42
      ("Additional type variable(s) in specification of " ^ Binding.str_of b ^ ": " ^
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    43
        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
    44
        (if mx = NoSyn then ""
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    45
         else "\nDropping mixfix syntax " ^ Pretty.string_of (Syntax.pretty_mixfix mx)));
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    46
      NoSyn);
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    47
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
    48
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    49
(* define *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    50
38311
228566e1ab00 name and argument grouping tuning
haftmann
parents: 38310
diff changeset
    51
fun define foundation ((b, mx), ((proto_b_def, atts), rhs)) lthy =
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    52
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    53
    val thy = ProofContext.theory_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    54
    val thy_ctxt = ProofContext.init_global thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    55
38311
228566e1ab00 name and argument grouping tuning
haftmann
parents: 38310
diff changeset
    56
    val b_def = Thm.def_binding_optional b proto_b_def;
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;
9bd4e568c58c added generic_target.ML
haftmann
parents:
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;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    70
    val term_params =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    71
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    72
      |> map_filter (fn (_, x) =>
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    73
        (case AList.lookup (op =) xs x of
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    74
          SOME T => SOME (Free (x, T))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    75
        | NONE => NONE));
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    76
    val params = type_params @ term_params;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    77
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    78
    val U = map Term.fastype_of params ---> T;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    79
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    80
    (*foundation*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    81
    val ((lhs', global_def), lthy2) = foundation
38313
e7e84919392b separated type from term parameters
haftmann
parents: 38312
diff changeset
    82
      (((b, U), mx'), (b_def, rhs')) (type_params, term_params) lthy;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    83
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    84
    (*local definition*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    85
    val ((lhs, local_def), lthy3) = lthy2
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    86
      |> Local_Defs.add_def ((b, NoSyn), lhs');
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    87
    val def = Local_Defs.trans_terms lthy3
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    88
      [(*c == global.c xs*)     local_def,
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    89
       (*global.c xs == rhs'*)  global_def,
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    90
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    91
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    92
    (*note*)
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    93
    val ([(res_name, [res])], lthy4) = lthy3
38311
228566e1ab00 name and argument grouping tuning
haftmann
parents: 38310
diff changeset
    94
      |> Local_Theory.notes_kind "" [((b_def, atts), [([def], [])])];
38315
fa3f90cf6d9c basic renumbering
haftmann
parents: 38313
diff changeset
    95
  in ((lhs, (res_name, res)), lthy4) end;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    96
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    97
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    98
(* notes *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
    99
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   100
fun import_export_proof ctxt (name, raw_th) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   101
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   102
    val thy = ProofContext.theory_of ctxt;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   103
    val thy_ctxt = ProofContext.init_global thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   104
    val certT = Thm.ctyp_of thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   105
    val cert = Thm.cterm_of thy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   106
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   107
    (*export assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   108
    val th = Goal.norm_result raw_th;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   109
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
41228
e1fce873b814 renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
wenzelm
parents: 40782
diff changeset
   110
    val assms = map (Raw_Simplifier.rewrite_rule defs o Thm.assume)
38310
9d4c0c74ae7d whitespace tuning
haftmann
parents: 38309
diff changeset
   111
      (Assumption.all_assms_of ctxt);
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   112
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   113
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   114
    (*export fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   115
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   116
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   117
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   118
      |> Variable.export ctxt thy_ctxt
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   119
      |> Drule.zero_var_indexes_list;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   120
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   121
    (*thm definition*)
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
   122
    val result = Global_Theory.name_thm true true name th'';
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   123
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   124
    (*import fixes*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   125
    val (tvars, vars) =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   126
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   127
      |>> map Logic.dest_type;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   128
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   129
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   130
    val inst = filter (is_Var o fst) (vars ~~ frees);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   131
    val cinstT = map (pairself certT o apfst TVar) instT;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   132
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   133
    val result' = Thm.instantiate (cinstT, cinst) result;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   134
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   135
    (*import assumes/defines*)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   136
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   137
    val result'' =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   138
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   139
        NONE => raise THM ("Failed to re-import result", 0, [result'])
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   140
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   141
      |> 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
   142
      |> Global_Theory.name_thm false false name;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   143
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   144
  in (result'', result) end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   145
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   146
fun notes target_notes kind facts lthy =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   147
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   148
    val thy = ProofContext.theory_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   149
    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
   150
      |> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   151
          (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
   152
      |> 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
   153
    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
   154
    val global_facts = Global_Theory.map_facts #2 facts';
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   155
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   156
    lthy
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   157
    |> target_notes kind global_facts local_facts
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   158
    |> ProofContext.note_thmss kind (Attrib.map_facts (Attrib.attribute_i thy) local_facts)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   159
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   160
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   161
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   162
(* abbrev *)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   163
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   164
fun abbrev target_abbrev prmode ((b, mx), t) lthy =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   165
  let
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   166
    val thy_ctxt = ProofContext.init_global (ProofContext.theory_of lthy);
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   167
    val target_ctxt = Local_Theory.target_of lthy;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   168
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   169
    val t' = Assumption.export_term lthy target_ctxt t;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   170
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   171
    val u = fold_rev lambda xs t';
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   172
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   173
    val extra_tfrees =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   174
      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
   175
    val mx' = check_mixfix lthy (b, extra_tfrees) mx;
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   176
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   177
    val global_rhs =
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   178
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   179
  in
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   180
    lthy
38312
9dd57db3c0f2 moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents: 38311
diff changeset
   181
    |> target_abbrev prmode (b, mx') (global_rhs, t') xs
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   182
    |> ProofContext.add_abbrev Print_Mode.internal (b, t) |> snd
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   183
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   184
  end;
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   185
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   186
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   187
(** primitive theory operations **)
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   188
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   189
fun theory_declaration decl lthy =
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   190
  let
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   191
    val global_decl = Morphism.form
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   192
      (Morphism.transform (Local_Theory.global_morphism lthy) decl);
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   193
  in
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   194
    lthy
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
   195
    |> Local_Theory.background_theory (Context.theory_map global_decl)
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   196
    |> Local_Theory.target (Context.proof_map global_decl)
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   197
  end;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   198
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   199
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
   200
  let
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
   201
    val (const, lthy2) = lthy |> Local_Theory.background_theory_result
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   202
      (Sign.declare_const ((b, U), mx));
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   203
    val lhs = list_comb (const, type_params @ term_params);
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
   204
    val ((_, def), lthy3) = lthy2 |> Local_Theory.background_theory_result
38341
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   205
      (Thm.add_def false false (b_def, Logic.mk_equals (lhs, rhs)));
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   206
  in ((lhs, def), lthy3) end;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   207
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   208
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
   209
  let
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   210
    val thy = ProofContext.theory_of lthy;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   211
    val global_facts' = Attrib.map_facts (Attrib.attribute_i thy) global_facts;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   212
  in
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   213
    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
   214
    |> Local_Theory.background_theory (Global_Theory.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
   215
    |> Local_Theory.target (ProofContext.note_thmss kind global_facts' #> snd)
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   216
  end;
72dba5bd5f63 moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents: 38315
diff changeset
   217
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
   218
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
   219
  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
   220
    (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
   221
      (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
   222
38309
9bd4e568c58c added generic_target.ML
haftmann
parents:
diff changeset
   223
end;