src/Pure/Isar/theory_target.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 36106 19deea200358
child 37146 f652333bbf8e
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    Author:     Makarius
wenzelm@30437
     3
    Author:     Florian Haftmann, TU Muenchen
wenzelm@20894
     4
haftmann@25542
     5
Common theory/locale/class/instantiation/overloading targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@35205
    10
  val peek: local_theory ->
wenzelm@35205
    11
   {target: string,
wenzelm@35205
    12
    is_locale: bool,
wenzelm@35205
    13
    is_class: bool,
wenzelm@35205
    14
    instantiation: string list * (string * sort) list * sort,
haftmann@25864
    15
    overloading: (string * (string * typ) * bool) list}
haftmann@25269
    16
  val init: string option -> theory -> local_theory
wenzelm@35205
    17
  val context_cmd: xstring -> theory -> local_theory
haftmann@25864
    18
  val instantiation: string list * (string * sort) list * sort -> theory -> local_theory
haftmann@29971
    19
  val instantiation_cmd: xstring list * xstring list * xstring -> theory -> local_theory
haftmann@25864
    20
  val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
wenzelm@26049
    21
  val overloading_cmd: (string * string * bool) list -> theory -> local_theory
wenzelm@20894
    22
end;
wenzelm@20894
    23
wenzelm@33553
    24
structure Theory_Target: THEORY_TARGET =
wenzelm@20894
    25
struct
wenzelm@20894
    26
wenzelm@21006
    27
(* context data *)
wenzelm@21006
    28
haftmann@29576
    29
datatype target = Target of {target: string, is_locale: bool,
haftmann@25864
    30
  is_class: bool, instantiation: string list * (string * sort) list * sort,
haftmann@25864
    31
  overloading: (string * (string * typ) * bool) list};
wenzelm@25012
    32
haftmann@29576
    33
fun make_target target is_locale is_class instantiation overloading =
haftmann@29576
    34
  Target {target = target, is_locale = is_locale,
haftmann@25519
    35
    is_class = is_class, instantiation = instantiation, overloading = overloading};
wenzelm@25291
    36
haftmann@29576
    37
val global_target = make_target "" false false ([], [], []) [];
wenzelm@25012
    38
wenzelm@33519
    39
structure Data = Proof_Data
wenzelm@21006
    40
(
wenzelm@25012
    41
  type T = target;
wenzelm@25291
    42
  fun init _ = global_target;
wenzelm@21006
    43
);
wenzelm@21006
    44
wenzelm@25012
    45
val peek = (fn Target args => args) o Data.get;
wenzelm@21006
    46
wenzelm@21006
    47
wenzelm@20894
    48
(* pretty *)
wenzelm@20894
    49
wenzelm@32785
    50
fun pretty_thy ctxt target is_class =
wenzelm@20894
    51
  let
wenzelm@20894
    52
    val thy = ProofContext.theory_of ctxt;
haftmann@29576
    53
    val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target;
wenzelm@30437
    54
    val fixes =
wenzelm@30437
    55
      map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@30437
    56
    val assumes =
wenzelm@30473
    57
      map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])])) (Assumption.all_assms_of ctxt);
wenzelm@20894
    58
    val elems =
wenzelm@20894
    59
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    60
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@26049
    61
  in
wenzelm@26049
    62
    if target = "" then []
haftmann@25607
    63
    else if null elems then [Pretty.str target_name]
haftmann@25607
    64
    else [Pretty.big_list (target_name ^ " =")
haftmann@25607
    65
      (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    66
  end;
wenzelm@20894
    67
wenzelm@32785
    68
fun pretty (Target {target, is_class, instantiation, overloading, ...}) ctxt =
haftmann@25607
    69
  Pretty.block [Pretty.str "theory", Pretty.brk 1,
haftmann@25607
    70
      Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
haftmann@25607
    71
    (if not (null overloading) then [Overloading.pretty ctxt]
haftmann@29358
    72
     else if not (null (#1 instantiation)) then [Class_Target.pretty_instantiation ctxt]
wenzelm@32785
    73
     else pretty_thy ctxt target is_class);
haftmann@25607
    74
wenzelm@20894
    75
wenzelm@33456
    76
(* generic declarations *)
wenzelm@33456
    77
wenzelm@33456
    78
local
haftmann@24838
    79
wenzelm@33456
    80
fun direct_decl decl =
wenzelm@33456
    81
  let val decl0 = Morphism.form decl in
wenzelm@33671
    82
    Local_Theory.theory (Context.theory_map decl0) #>
wenzelm@33671
    83
    Local_Theory.target (Context.proof_map decl0)
wenzelm@33456
    84
  end;
wenzelm@33456
    85
wenzelm@33456
    86
fun target_decl add (Target {target, ...}) pervasive decl lthy =
haftmann@24838
    87
  let
wenzelm@33671
    88
    val global_decl = Morphism.transform (Local_Theory.global_morphism lthy) decl;
wenzelm@33671
    89
    val target_decl = Morphism.transform (Local_Theory.target_morphism lthy) decl;
haftmann@24838
    90
  in
wenzelm@25012
    91
    if target = "" then
haftmann@24838
    92
      lthy
haftmann@35127
    93
      |> direct_decl global_decl
haftmann@24838
    94
    else
haftmann@24838
    95
      lthy
wenzelm@33456
    96
      |> pervasive ? direct_decl global_decl
wenzelm@33671
    97
      |> Local_Theory.target (add target target_decl)
haftmann@24838
    98
  end;
haftmann@24838
    99
wenzelm@33456
   100
in
wenzelm@33456
   101
haftmann@29576
   102
val declaration = target_decl Locale.add_declaration;
wenzelm@35798
   103
val syntax_declaration = target_decl Locale.add_syntax_declaration;
haftmann@24838
   104
wenzelm@33456
   105
end;
wenzelm@33456
   106
wenzelm@25105
   107
fun class_target (Target {target, ...}) f =
wenzelm@33671
   108
  Local_Theory.raw_theory f #>
wenzelm@33671
   109
  Local_Theory.target (Class_Target.refresh_syntax target);
haftmann@24838
   110
wenzelm@21611
   111
wenzelm@20894
   112
(* notes *)
wenzelm@20894
   113
wenzelm@21611
   114
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   115
  let
wenzelm@21611
   116
    val thy = ProofContext.theory_of ctxt;
wenzelm@36610
   117
    val thy_ctxt = ProofContext.init_global thy;
wenzelm@21611
   118
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   119
    val cert = Thm.cterm_of thy;
wenzelm@21594
   120
wenzelm@21611
   121
    (*export assumes/defines*)
wenzelm@21611
   122
    val th = Goal.norm_result raw_th;
wenzelm@35624
   123
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
wenzelm@30473
   124
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.all_assms_of ctxt);
wenzelm@21611
   125
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   126
wenzelm@21611
   127
    (*export fixes*)
wenzelm@22692
   128
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   129
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   130
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   131
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   132
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   133
wenzelm@21611
   134
    (*thm definition*)
wenzelm@33700
   135
    val result = PureThy.name_thm true true name th'';
wenzelm@21611
   136
wenzelm@21611
   137
    (*import fixes*)
wenzelm@21611
   138
    val (tvars, vars) =
wenzelm@21611
   139
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   140
      |>> map Logic.dest_type;
wenzelm@21611
   141
wenzelm@21611
   142
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   143
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   144
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@31977
   145
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
wenzelm@21611
   146
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   147
wenzelm@21611
   148
    (*import assumes/defines*)
wenzelm@21594
   149
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   150
    val result'' =
wenzelm@21611
   151
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   152
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@35739
   153
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
wenzelm@21644
   154
      |> Goal.norm_result
wenzelm@33700
   155
      |> PureThy.name_thm false false name;
wenzelm@21611
   156
wenzelm@21611
   157
  in (result'', result) end;
wenzelm@21611
   158
haftmann@29576
   159
fun notes (Target {target, is_locale, ...}) kind facts lthy =
wenzelm@21585
   160
  let
wenzelm@21594
   161
    val thy = ProofContext.theory_of lthy;
wenzelm@21594
   162
    val facts' = facts
haftmann@28991
   163
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi
wenzelm@33671
   164
          (Local_Theory.full_name lthy (fst a))) bs))
wenzelm@21615
   165
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   166
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   167
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   168
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@33671
   169
      |> is_locale ? Element.facts_map (Element.morph_ctxt (Local_Theory.target_morphism lthy));
wenzelm@21611
   170
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@25012
   171
      |> Attrib.map_facts (if is_locale then K I else Attrib.attribute_i thy);
wenzelm@21594
   172
  in
wenzelm@33167
   173
    lthy
wenzelm@33671
   174
    |> Local_Theory.theory (PureThy.note_thmss kind global_facts #> snd)
wenzelm@33671
   175
    |> not is_locale ? Local_Theory.target (ProofContext.note_thmss kind global_facts #> snd)
wenzelm@33671
   176
    |> is_locale ? Local_Theory.target (Locale.add_thmss target kind target_facts)
wenzelm@30761
   177
    |> ProofContext.note_thmss kind local_facts
wenzelm@20894
   178
  end;
wenzelm@20894
   179
wenzelm@20894
   180
wenzelm@35764
   181
(* consts *)
wenzelm@24939
   182
wenzelm@25105
   183
fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
wenzelm@25105
   184
  if not is_locale then (NoSyn, NoSyn, mx)
wenzelm@25105
   185
  else if not is_class then (NoSyn, mx, NoSyn)
wenzelm@25105
   186
  else (mx, NoSyn, NoSyn);
wenzelm@25068
   187
wenzelm@33167
   188
fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) phi =
wenzelm@24939
   189
  let
haftmann@28965
   190
    val b' = Morphism.binding phi b;
wenzelm@24939
   191
    val rhs' = Morphism.term phi rhs;
haftmann@28861
   192
    val arg = (b', Term.close_schematic_term rhs');
wenzelm@33537
   193
    val same_shape = Term.aconv_untyped (rhs, rhs');
wenzelm@25212
   194
    (* FIXME workaround based on educated guess *)
wenzelm@30278
   195
    val prefix' = Binding.prefix_of b';
wenzelm@33282
   196
    val class_global =
wenzelm@33282
   197
      Binding.eq_name (b, b') andalso
wenzelm@33282
   198
      not (null prefix') andalso
wenzelm@33282
   199
      fst (snd (split_last prefix')) = Class_Target.class_prefix target;
wenzelm@24939
   200
  in
wenzelm@33537
   201
    not (is_class andalso (same_shape orelse class_global)) ?
wenzelm@25212
   202
      (Context.mapping_result
wenzelm@33173
   203
        (Sign.add_abbrev PrintMode.internal arg)
wenzelm@33173
   204
        (ProofContext.add_abbrev PrintMode.internal arg)
wenzelm@25212
   205
      #-> (fn (lhs' as Const (d, _), _) =>
wenzelm@33537
   206
          same_shape ?
wenzelm@25212
   207
            (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
wenzelm@25212
   208
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
wenzelm@24939
   209
  end;
wenzelm@24939
   210
wenzelm@24939
   211
wenzelm@24939
   212
(* abbrev *)
wenzelm@24939
   213
wenzelm@28083
   214
fun abbrev (ta as Target {target, is_locale, is_class, ...}) prmode ((b, mx), t) lthy =
wenzelm@24939
   215
  let
wenzelm@36610
   216
    val thy_ctxt = ProofContext.init_global (ProofContext.theory_of lthy);
wenzelm@33671
   217
    val target_ctxt = Local_Theory.target_of lthy;
wenzelm@25105
   218
wenzelm@25105
   219
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@25105
   220
    val t' = Assumption.export_term lthy target_ctxt t;
wenzelm@25105
   221
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
wenzelm@25105
   222
    val u = fold_rev lambda xs t';
wenzelm@25105
   223
    val global_rhs =
wenzelm@25105
   224
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
wenzelm@25121
   225
  in
wenzelm@25121
   226
    lthy |>
wenzelm@25121
   227
     (if is_locale then
wenzelm@33671
   228
        Local_Theory.theory_result (Sign.add_abbrev PrintMode.internal (b, global_rhs))
wenzelm@25121
   229
        #-> (fn (lhs, _) =>
wenzelm@35845
   230
          let val lhs' = Term.list_comb (Logic.unvarify_global lhs, xs) in
wenzelm@35798
   231
            syntax_declaration ta false (locale_const ta prmode ((b, mx2), lhs')) #>
wenzelm@33173
   232
            is_class ? class_target ta (Class_Target.abbrev target prmode ((b, mx1), t'))
wenzelm@25105
   233
          end)
wenzelm@25105
   234
      else
wenzelm@33671
   235
        Local_Theory.theory
wenzelm@33173
   236
          (Sign.add_abbrev (#1 prmode) (b, global_rhs) #-> (fn (lhs, _) =>
wenzelm@25121
   237
           Sign.notation true prmode [(lhs, mx3)])))
wenzelm@33173
   238
    |> ProofContext.add_abbrev PrintMode.internal (b, t) |> snd
wenzelm@35624
   239
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
wenzelm@24939
   240
  end;
wenzelm@24939
   241
wenzelm@24939
   242
wenzelm@25022
   243
(* define *)
wenzelm@24939
   244
wenzelm@35764
   245
local
wenzelm@35764
   246
wenzelm@35764
   247
fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
wenzelm@35764
   248
wenzelm@35765
   249
fun declare_const (ta as Target {target, is_locale, is_class, ...})
wenzelm@35765
   250
    extra_tfrees xs ((b, T), mx) lthy =
wenzelm@35764
   251
  let
wenzelm@35765
   252
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
wenzelm@35765
   253
    val term_params =
wenzelm@35764
   254
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
wenzelm@35764
   255
      |> map_filter (fn (_, x) =>
wenzelm@35764
   256
        (case AList.lookup (op =) xs x of
wenzelm@35765
   257
          SOME T => SOME (Free (x, T))
wenzelm@35764
   258
        | NONE => NONE));
wenzelm@35765
   259
    val params = type_params @ term_params;
wenzelm@35765
   260
wenzelm@35765
   261
    val U = map Term.fastype_of params ---> T;
wenzelm@35764
   262
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@35764
   263
    val (const, lthy') = lthy |>
wenzelm@35764
   264
      (case Class_Target.instantiation_param lthy b of
wenzelm@35764
   265
        SOME c' =>
wenzelm@35764
   266
          if mx3 <> NoSyn then syntax_error c'
wenzelm@35764
   267
          else Local_Theory.theory_result (AxClass.declare_overloaded (c', U))
wenzelm@35764
   268
            ##> Class_Target.confirm_declaration b
wenzelm@35764
   269
      | NONE =>
wenzelm@35764
   270
          (case Overloading.operation lthy b of
wenzelm@35764
   271
            SOME (c', _) =>
wenzelm@35764
   272
              if mx3 <> NoSyn then syntax_error c'
wenzelm@35764
   273
              else Local_Theory.theory_result (Overloading.declare (c', U))
wenzelm@35764
   274
                ##> Overloading.confirm b
wenzelm@35764
   275
          | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
wenzelm@35765
   276
    val t = Term.list_comb (const, params);
wenzelm@35764
   277
  in
wenzelm@35764
   278
    lthy'
wenzelm@35798
   279
    |> is_locale ? syntax_declaration ta false (locale_const ta Syntax.mode_default ((b, mx2), t))
haftmann@35858
   280
    |> is_class ? class_target ta (Class_Target.declare target ((b, mx1), (type_params, t)))
wenzelm@35764
   281
    |> Local_Defs.add_def ((b, NoSyn), t)
wenzelm@35764
   282
  end;
wenzelm@35764
   283
wenzelm@35764
   284
in
wenzelm@35764
   285
wenzelm@33764
   286
fun define ta ((b, mx), ((name, atts), rhs)) lthy =
wenzelm@24939
   287
  let
wenzelm@24987
   288
    val thy = ProofContext.theory_of lthy;
wenzelm@36610
   289
    val thy_ctxt = ProofContext.init_global thy;
wenzelm@24939
   290
wenzelm@30437
   291
    val name' = Thm.def_binding_optional b name;
wenzelm@35717
   292
wenzelm@35717
   293
    val crhs = Thm.cterm_of thy rhs;
wenzelm@35717
   294
    val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
wenzelm@35717
   295
    val rhs_conv = MetaSimplifier.rewrite true defs crhs;
wenzelm@35717
   296
wenzelm@33671
   297
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
wenzelm@24939
   298
    val T = Term.fastype_of rhs;
wenzelm@35765
   299
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
wenzelm@35765
   300
    val extra_tfrees =
wenzelm@35765
   301
      fold_types (fold_atyps
wenzelm@35765
   302
        (fn TFree v => if member (op =) tfreesT v then I else insert (op =) v | _ => I)) rhs []
wenzelm@35765
   303
      |> sort_wrt #1;
wenzelm@24939
   304
wenzelm@25105
   305
    (*const*)
wenzelm@35765
   306
    val ((lhs, local_def), lthy2) = lthy |> declare_const ta extra_tfrees xs ((b, T), mx);
wenzelm@25022
   307
    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
wenzelm@35765
   308
    val Const (head, _) = Term.head_of lhs';
wenzelm@24939
   309
wenzelm@24939
   310
    (*def*)
wenzelm@25022
   311
    val (global_def, lthy3) = lthy2
wenzelm@33671
   312
      |> Local_Theory.theory_result
wenzelm@33282
   313
        (case Overloading.operation lthy b of
wenzelm@35765
   314
          SOME (_, checked) => Overloading.define checked name' (head, rhs')
haftmann@30519
   315
        | NONE =>
wenzelm@35764
   316
            if is_some (Class_Target.instantiation_param lthy b)
wenzelm@35765
   317
            then AxClass.define_overloaded name' (head, rhs')
wenzelm@36106
   318
            else Thm.add_def false false (name', Logic.mk_equals (lhs', rhs')) #>> snd);
wenzelm@35624
   319
    val def = Local_Defs.trans_terms lthy3
wenzelm@25022
   320
      [(*c == global.c xs*)     local_def,
wenzelm@25022
   321
       (*global.c xs == rhs'*)  global_def,
haftmann@25485
   322
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
wenzelm@24939
   323
wenzelm@25105
   324
    (*note*)
wenzelm@24939
   325
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@33764
   326
      |> notes ta "" [((name', atts), [([def], [])])];
wenzelm@24939
   327
  in ((lhs, (res_name, res)), lthy4) end;
wenzelm@24939
   328
wenzelm@35764
   329
end;
wenzelm@35764
   330
wenzelm@24939
   331
wenzelm@35205
   332
(* init various targets *)
wenzelm@25291
   333
wenzelm@25291
   334
local
wenzelm@20894
   335
wenzelm@25291
   336
fun init_target _ NONE = global_target
wenzelm@33670
   337
  | init_target thy (SOME target) =
wenzelm@35205
   338
      if Locale.defined thy target
haftmann@29710
   339
      then make_target target true (Class_Target.is_class thy target) ([], [], []) []
haftmann@29710
   340
      else error ("No such locale: " ^ quote target);
wenzelm@25291
   341
haftmann@29576
   342
fun init_ctxt (Target {target, is_locale, is_class, instantiation, overloading}) =
haftmann@29358
   343
  if not (null (#1 instantiation)) then Class_Target.init_instantiation instantiation
haftmann@25519
   344
  else if not (null overloading) then Overloading.init overloading
wenzelm@36610
   345
  else if not is_locale then ProofContext.init_global
haftmann@29576
   346
  else if not is_class then Locale.init target
haftmann@29358
   347
  else Class_Target.init target;
haftmann@25269
   348
haftmann@25542
   349
fun init_lthy (ta as Target {target, instantiation, overloading, ...}) =
wenzelm@25291
   350
  Data.put ta #>
wenzelm@33724
   351
  Local_Theory.init NONE (Long_Name.base_name target)
wenzelm@25291
   352
   {pretty = pretty ta,
wenzelm@25291
   353
    abbrev = abbrev ta,
wenzelm@25291
   354
    define = define ta,
wenzelm@25291
   355
    notes = notes ta,
wenzelm@25291
   356
    declaration = declaration ta,
wenzelm@35798
   357
    syntax_declaration = syntax_declaration ta,
wenzelm@25291
   358
    reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
wenzelm@33671
   359
    exit = Local_Theory.target_of o
haftmann@29358
   360
      (if not (null (#1 instantiation)) then Class_Target.conclude_instantiation
wenzelm@26049
   361
       else if not (null overloading) then Overloading.conclude
wenzelm@26049
   362
       else I)}
wenzelm@25291
   363
and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
wenzelm@20894
   364
wenzelm@26049
   365
fun gen_overloading prep_const raw_ops thy =
wenzelm@26049
   366
  let
wenzelm@36610
   367
    val ctxt = ProofContext.init_global thy;
wenzelm@26049
   368
    val ops = raw_ops |> map (fn (name, const, checked) =>
wenzelm@26049
   369
      (name, Term.dest_Const (prep_const ctxt const), checked));
haftmann@29576
   370
  in thy |> init_lthy_ctxt (make_target "" false false ([], [], []) ops) end;
wenzelm@26049
   371
wenzelm@25291
   372
in
wenzelm@20894
   373
wenzelm@25291
   374
fun init target thy = init_lthy_ctxt (init_target thy target) thy;
haftmann@25269
   375
wenzelm@35205
   376
fun context_cmd "-" thy = init NONE thy
wenzelm@35205
   377
  | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
wenzelm@33282
   378
haftmann@29576
   379
fun instantiation arities = init_lthy_ctxt (make_target "" false false arities []);
wenzelm@35205
   380
fun instantiation_cmd arities thy = instantiation (Class_Target.read_multi_arity thy arities) thy;
haftmann@25519
   381
wenzelm@26049
   382
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
wenzelm@26049
   383
val overloading_cmd = gen_overloading Syntax.read_term;
haftmann@25462
   384
wenzelm@20894
   385
end;
wenzelm@25291
   386
wenzelm@25291
   387
end;
wenzelm@25291
   388