src/Pure/Isar/theory_target.ML
author wenzelm
Sat Mar 13 20:33:14 2010 +0100 (2010-03-13)
changeset 35765 09e238561460
parent 35764 f7f88f2e004f
child 35798 fd1bb29f8170
permissions -rw-r--r--
local theory specifications handle hidden polymorphism implicitly;
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 type_syntax = target_decl Locale.add_type_syntax;
haftmann@29576
   103
val term_syntax = target_decl Locale.add_term_syntax;
haftmann@29576
   104
val declaration = target_decl Locale.add_declaration;
haftmann@24838
   105
wenzelm@33456
   106
end;
wenzelm@33456
   107
wenzelm@25105
   108
fun class_target (Target {target, ...}) f =
wenzelm@33671
   109
  Local_Theory.raw_theory f #>
wenzelm@33671
   110
  Local_Theory.target (Class_Target.refresh_syntax target);
haftmann@24838
   111
wenzelm@21611
   112
wenzelm@20894
   113
(* notes *)
wenzelm@20894
   114
wenzelm@21611
   115
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   116
  let
wenzelm@21611
   117
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   118
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   119
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   120
    val cert = Thm.cterm_of thy;
wenzelm@21594
   121
wenzelm@21611
   122
    (*export assumes/defines*)
wenzelm@21611
   123
    val th = Goal.norm_result raw_th;
wenzelm@35624
   124
    val (defs, th') = Local_Defs.export ctxt thy_ctxt th;
wenzelm@30473
   125
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.all_assms_of ctxt);
wenzelm@21611
   126
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   127
wenzelm@21611
   128
    (*export fixes*)
wenzelm@22692
   129
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   130
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   131
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   132
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   133
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   134
wenzelm@21611
   135
    (*thm definition*)
wenzelm@33700
   136
    val result = PureThy.name_thm true true name th'';
wenzelm@21611
   137
wenzelm@21611
   138
    (*import fixes*)
wenzelm@21611
   139
    val (tvars, vars) =
wenzelm@21611
   140
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   141
      |>> map Logic.dest_type;
wenzelm@21611
   142
wenzelm@21611
   143
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   144
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   145
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@31977
   146
    val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst;
wenzelm@21611
   147
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   148
wenzelm@21611
   149
    (*import assumes/defines*)
wenzelm@21594
   150
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   151
    val result'' =
wenzelm@21611
   152
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   153
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@35739
   154
      | SOME res => Local_Defs.contract ctxt defs (Thm.cprop_of th) res)
wenzelm@21644
   155
      |> Goal.norm_result
wenzelm@33700
   156
      |> PureThy.name_thm false false name;
wenzelm@21611
   157
wenzelm@21611
   158
  in (result'', result) end;
wenzelm@21611
   159
haftmann@29576
   160
fun notes (Target {target, is_locale, ...}) kind facts lthy =
wenzelm@21585
   161
  let
wenzelm@21594
   162
    val thy = ProofContext.theory_of lthy;
wenzelm@21594
   163
    val facts' = facts
haftmann@28991
   164
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi
wenzelm@33671
   165
          (Local_Theory.full_name lthy (fst a))) bs))
wenzelm@21615
   166
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   167
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   168
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   169
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@33671
   170
      |> is_locale ? Element.facts_map (Element.morph_ctxt (Local_Theory.target_morphism lthy));
wenzelm@21611
   171
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@25012
   172
      |> Attrib.map_facts (if is_locale then K I else Attrib.attribute_i thy);
wenzelm@21594
   173
  in
wenzelm@33167
   174
    lthy
wenzelm@33671
   175
    |> Local_Theory.theory (PureThy.note_thmss kind global_facts #> snd)
wenzelm@33671
   176
    |> not is_locale ? Local_Theory.target (ProofContext.note_thmss kind global_facts #> snd)
wenzelm@33671
   177
    |> is_locale ? Local_Theory.target (Locale.add_thmss target kind target_facts)
wenzelm@30761
   178
    |> ProofContext.note_thmss kind local_facts
wenzelm@20894
   179
  end;
wenzelm@20894
   180
wenzelm@20894
   181
wenzelm@35764
   182
(* consts *)
wenzelm@24939
   183
wenzelm@25105
   184
fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
wenzelm@25105
   185
  if not is_locale then (NoSyn, NoSyn, mx)
wenzelm@25105
   186
  else if not is_class then (NoSyn, mx, NoSyn)
wenzelm@25105
   187
  else (mx, NoSyn, NoSyn);
wenzelm@25068
   188
wenzelm@33167
   189
fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) phi =
wenzelm@24939
   190
  let
haftmann@28965
   191
    val b' = Morphism.binding phi b;
wenzelm@24939
   192
    val rhs' = Morphism.term phi rhs;
haftmann@28861
   193
    val arg = (b', Term.close_schematic_term rhs');
wenzelm@33537
   194
    val same_shape = Term.aconv_untyped (rhs, rhs');
wenzelm@25212
   195
    (* FIXME workaround based on educated guess *)
wenzelm@30278
   196
    val prefix' = Binding.prefix_of b';
wenzelm@33282
   197
    val class_global =
wenzelm@33282
   198
      Binding.eq_name (b, b') andalso
wenzelm@33282
   199
      not (null prefix') andalso
wenzelm@33282
   200
      fst (snd (split_last prefix')) = Class_Target.class_prefix target;
wenzelm@24939
   201
  in
wenzelm@33537
   202
    not (is_class andalso (same_shape orelse class_global)) ?
wenzelm@25212
   203
      (Context.mapping_result
wenzelm@33173
   204
        (Sign.add_abbrev PrintMode.internal arg)
wenzelm@33173
   205
        (ProofContext.add_abbrev PrintMode.internal arg)
wenzelm@25212
   206
      #-> (fn (lhs' as Const (d, _), _) =>
wenzelm@33537
   207
          same_shape ?
wenzelm@25212
   208
            (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
wenzelm@25212
   209
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
wenzelm@24939
   210
  end;
wenzelm@24939
   211
wenzelm@24939
   212
wenzelm@24939
   213
(* abbrev *)
wenzelm@24939
   214
wenzelm@28083
   215
fun abbrev (ta as Target {target, is_locale, is_class, ...}) prmode ((b, mx), t) lthy =
wenzelm@24939
   216
  let
wenzelm@25053
   217
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@33671
   218
    val target_ctxt = Local_Theory.target_of lthy;
wenzelm@25105
   219
wenzelm@25105
   220
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@25105
   221
    val t' = Assumption.export_term lthy target_ctxt t;
wenzelm@25105
   222
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
wenzelm@25105
   223
    val u = fold_rev lambda xs t';
wenzelm@25105
   224
    val global_rhs =
wenzelm@25105
   225
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
wenzelm@25121
   226
  in
wenzelm@25121
   227
    lthy |>
wenzelm@25121
   228
     (if is_locale then
wenzelm@33671
   229
        Local_Theory.theory_result (Sign.add_abbrev PrintMode.internal (b, global_rhs))
wenzelm@25121
   230
        #-> (fn (lhs, _) =>
wenzelm@25105
   231
          let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
wenzelm@33456
   232
            term_syntax ta false (locale_const ta prmode ((b, mx2), lhs')) #>
wenzelm@33173
   233
            is_class ? class_target ta (Class_Target.abbrev target prmode ((b, mx1), t'))
wenzelm@25105
   234
          end)
wenzelm@25105
   235
      else
wenzelm@33671
   236
        Local_Theory.theory
wenzelm@33173
   237
          (Sign.add_abbrev (#1 prmode) (b, global_rhs) #-> (fn (lhs, _) =>
wenzelm@25121
   238
           Sign.notation true prmode [(lhs, mx3)])))
wenzelm@33173
   239
    |> ProofContext.add_abbrev PrintMode.internal (b, t) |> snd
wenzelm@35624
   240
    |> Local_Defs.fixed_abbrev ((b, NoSyn), t)
wenzelm@24939
   241
  end;
wenzelm@24939
   242
wenzelm@24939
   243
wenzelm@25022
   244
(* define *)
wenzelm@24939
   245
wenzelm@35764
   246
local
wenzelm@35764
   247
wenzelm@35764
   248
fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
wenzelm@35764
   249
wenzelm@35765
   250
fun declare_const (ta as Target {target, is_locale, is_class, ...})
wenzelm@35765
   251
    extra_tfrees xs ((b, T), mx) lthy =
wenzelm@35764
   252
  let
wenzelm@35765
   253
    val type_params = map (Logic.mk_type o TFree) extra_tfrees;
wenzelm@35765
   254
    val term_params =
wenzelm@35764
   255
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
wenzelm@35764
   256
      |> map_filter (fn (_, x) =>
wenzelm@35764
   257
        (case AList.lookup (op =) xs x of
wenzelm@35765
   258
          SOME T => SOME (Free (x, T))
wenzelm@35764
   259
        | NONE => NONE));
wenzelm@35765
   260
    val params = type_params @ term_params;
wenzelm@35765
   261
wenzelm@35765
   262
    val U = map Term.fastype_of params ---> T;
wenzelm@35764
   263
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@35764
   264
    val (const, lthy') = lthy |>
wenzelm@35764
   265
      (case Class_Target.instantiation_param lthy b of
wenzelm@35764
   266
        SOME c' =>
wenzelm@35764
   267
          if mx3 <> NoSyn then syntax_error c'
wenzelm@35764
   268
          else Local_Theory.theory_result (AxClass.declare_overloaded (c', U))
wenzelm@35764
   269
            ##> Class_Target.confirm_declaration b
wenzelm@35764
   270
      | NONE =>
wenzelm@35764
   271
          (case Overloading.operation lthy b of
wenzelm@35764
   272
            SOME (c', _) =>
wenzelm@35764
   273
              if mx3 <> NoSyn then syntax_error c'
wenzelm@35764
   274
              else Local_Theory.theory_result (Overloading.declare (c', U))
wenzelm@35764
   275
                ##> Overloading.confirm b
wenzelm@35764
   276
          | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
wenzelm@35765
   277
    val t = Term.list_comb (const, params);
wenzelm@35764
   278
  in
wenzelm@35764
   279
    lthy'
wenzelm@35764
   280
    |> is_locale ? term_syntax ta false (locale_const ta Syntax.mode_default ((b, mx2), t))
wenzelm@35764
   281
    |> is_class ? class_target ta (Class_Target.declare target ((b, mx1), t))
wenzelm@35764
   282
    |> Local_Defs.add_def ((b, NoSyn), t)
wenzelm@35764
   283
  end;
wenzelm@35764
   284
wenzelm@35764
   285
in
wenzelm@35764
   286
wenzelm@33764
   287
fun define ta ((b, mx), ((name, atts), rhs)) lthy =
wenzelm@24939
   288
  let
wenzelm@24987
   289
    val thy = ProofContext.theory_of lthy;
wenzelm@24939
   290
    val thy_ctxt = ProofContext.init thy;
wenzelm@24939
   291
wenzelm@30437
   292
    val name' = Thm.def_binding_optional b name;
wenzelm@35717
   293
wenzelm@35717
   294
    val crhs = Thm.cterm_of thy rhs;
wenzelm@35717
   295
    val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
wenzelm@35717
   296
    val rhs_conv = MetaSimplifier.rewrite true defs crhs;
wenzelm@35717
   297
wenzelm@33671
   298
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
wenzelm@24939
   299
    val T = Term.fastype_of rhs;
wenzelm@35765
   300
    val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []);
wenzelm@35765
   301
    val extra_tfrees =
wenzelm@35765
   302
      fold_types (fold_atyps
wenzelm@35765
   303
        (fn TFree v => if member (op =) tfreesT v then I else insert (op =) v | _ => I)) rhs []
wenzelm@35765
   304
      |> sort_wrt #1;
wenzelm@24939
   305
wenzelm@25105
   306
    (*const*)
wenzelm@35765
   307
    val ((lhs, local_def), lthy2) = lthy |> declare_const ta extra_tfrees xs ((b, T), mx);
wenzelm@25022
   308
    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
wenzelm@35765
   309
    val Const (head, _) = Term.head_of lhs';
wenzelm@24939
   310
wenzelm@24939
   311
    (*def*)
wenzelm@25022
   312
    val (global_def, lthy3) = lthy2
wenzelm@33671
   313
      |> Local_Theory.theory_result
wenzelm@33282
   314
        (case Overloading.operation lthy b of
wenzelm@35765
   315
          SOME (_, checked) => Overloading.define checked name' (head, rhs')
haftmann@30519
   316
        | NONE =>
wenzelm@35764
   317
            if is_some (Class_Target.instantiation_param lthy b)
wenzelm@35765
   318
            then AxClass.define_overloaded name' (head, rhs')
wenzelm@35764
   319
            else Thm.add_def false false (name', Logic.mk_equals (lhs', rhs')));
wenzelm@35624
   320
    val def = Local_Defs.trans_terms lthy3
wenzelm@25022
   321
      [(*c == global.c xs*)     local_def,
wenzelm@25022
   322
       (*global.c xs == rhs'*)  global_def,
haftmann@25485
   323
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
wenzelm@24939
   324
wenzelm@25105
   325
    (*note*)
wenzelm@24939
   326
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@33764
   327
      |> notes ta "" [((name', atts), [([def], [])])];
wenzelm@24939
   328
  in ((lhs, (res_name, res)), lthy4) end;
wenzelm@24939
   329
wenzelm@35764
   330
end;
wenzelm@35764
   331
wenzelm@24939
   332
wenzelm@35205
   333
(* init various targets *)
wenzelm@25291
   334
wenzelm@25291
   335
local
wenzelm@20894
   336
wenzelm@25291
   337
fun init_target _ NONE = global_target
wenzelm@33670
   338
  | init_target thy (SOME target) =
wenzelm@35205
   339
      if Locale.defined thy target
haftmann@29710
   340
      then make_target target true (Class_Target.is_class thy target) ([], [], []) []
haftmann@29710
   341
      else error ("No such locale: " ^ quote target);
wenzelm@25291
   342
haftmann@29576
   343
fun init_ctxt (Target {target, is_locale, is_class, instantiation, overloading}) =
haftmann@29358
   344
  if not (null (#1 instantiation)) then Class_Target.init_instantiation instantiation
haftmann@25519
   345
  else if not (null overloading) then Overloading.init overloading
haftmann@25485
   346
  else if not is_locale then ProofContext.init
haftmann@29576
   347
  else if not is_class then Locale.init target
haftmann@29358
   348
  else Class_Target.init target;
haftmann@25269
   349
haftmann@25542
   350
fun init_lthy (ta as Target {target, instantiation, overloading, ...}) =
wenzelm@25291
   351
  Data.put ta #>
wenzelm@33724
   352
  Local_Theory.init NONE (Long_Name.base_name target)
wenzelm@25291
   353
   {pretty = pretty ta,
wenzelm@25291
   354
    abbrev = abbrev ta,
wenzelm@25291
   355
    define = define ta,
wenzelm@25291
   356
    notes = notes ta,
wenzelm@25291
   357
    type_syntax = type_syntax ta,
wenzelm@25291
   358
    term_syntax = term_syntax ta,
wenzelm@25291
   359
    declaration = declaration ta,
wenzelm@25291
   360
    reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
wenzelm@33671
   361
    exit = Local_Theory.target_of o
haftmann@29358
   362
      (if not (null (#1 instantiation)) then Class_Target.conclude_instantiation
wenzelm@26049
   363
       else if not (null overloading) then Overloading.conclude
wenzelm@26049
   364
       else I)}
wenzelm@25291
   365
and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
wenzelm@20894
   366
wenzelm@26049
   367
fun gen_overloading prep_const raw_ops thy =
wenzelm@26049
   368
  let
wenzelm@26049
   369
    val ctxt = ProofContext.init thy;
wenzelm@26049
   370
    val ops = raw_ops |> map (fn (name, const, checked) =>
wenzelm@26049
   371
      (name, Term.dest_Const (prep_const ctxt const), checked));
haftmann@29576
   372
  in thy |> init_lthy_ctxt (make_target "" false false ([], [], []) ops) end;
wenzelm@26049
   373
wenzelm@25291
   374
in
wenzelm@20894
   375
wenzelm@25291
   376
fun init target thy = init_lthy_ctxt (init_target thy target) thy;
haftmann@25269
   377
wenzelm@35205
   378
fun context_cmd "-" thy = init NONE thy
wenzelm@35205
   379
  | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
wenzelm@33282
   380
haftmann@29576
   381
fun instantiation arities = init_lthy_ctxt (make_target "" false false arities []);
wenzelm@35205
   382
fun instantiation_cmd arities thy = instantiation (Class_Target.read_multi_arity thy arities) thy;
haftmann@25519
   383
wenzelm@26049
   384
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
wenzelm@26049
   385
val overloading_cmd = gen_overloading Syntax.read_term;
haftmann@25462
   386
wenzelm@20894
   387
end;
wenzelm@25291
   388
wenzelm@25291
   389
end;
wenzelm@25291
   390