src/Pure/Isar/theory_target.ML
author haftmann
Fri Dec 05 18:43:42 2008 +0100 (2008-12-05)
changeset 29006 abe0f11cfa4e
parent 28991 694227dd3e8c
child 29102 2e1011dcd577
child 29223 e09c53289830
permissions -rw-r--r--
Name.name_of -> Binding.base_name
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
haftmann@28861
     3
    ID:         $Id$
wenzelm@20894
     4
    Author:     Makarius
wenzelm@20894
     5
haftmann@25542
     6
Common theory/locale/class/instantiation/overloading targets.
wenzelm@20894
     7
*)
wenzelm@20894
     8
wenzelm@20894
     9
signature THEORY_TARGET =
wenzelm@20894
    10
sig
haftmann@25462
    11
  val peek: local_theory -> {target: string, is_locale: bool,
haftmann@25864
    12
    is_class: bool, instantiation: string list * (string * sort) list * sort,
haftmann@25864
    13
    overloading: (string * (string * typ) * bool) list}
haftmann@25269
    14
  val init: string option -> theory -> local_theory
wenzelm@24935
    15
  val begin: string -> Proof.context -> local_theory
wenzelm@25291
    16
  val context: xstring -> theory -> local_theory
haftmann@25864
    17
  val instantiation: string list * (string * sort) list * sort -> theory -> local_theory
haftmann@25864
    18
  val overloading: (string * (string * typ) * bool) list -> theory -> local_theory
wenzelm@26049
    19
  val overloading_cmd: (string * string * bool) list -> theory -> local_theory
wenzelm@20894
    20
end;
wenzelm@20894
    21
wenzelm@20894
    22
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    23
struct
wenzelm@20894
    24
ballarin@28834
    25
(* new locales *)
ballarin@28834
    26
ballarin@28849
    27
fun locale_extern x = if !new_locales then NewLocale.extern x else Locale.extern x;
ballarin@28849
    28
fun locale_add_type_syntax x = if !new_locales then NewLocale.add_type_syntax x else Locale.add_type_syntax x;
ballarin@28849
    29
fun locale_add_term_syntax x = if !new_locales then NewLocale.add_term_syntax x else Locale.add_term_syntax x;
ballarin@28849
    30
fun locale_add_declaration x = if !new_locales then NewLocale.add_declaration x else Locale.add_declaration x;
ballarin@28849
    31
fun locale_add_thmss x = if !new_locales then NewLocale.add_thmss x else Locale.add_thmss x;
ballarin@28849
    32
fun locale_init x = if !new_locales then NewLocale.init x else Locale.init x;
ballarin@28849
    33
fun locale_intern x = if !new_locales then NewLocale.intern x else Locale.intern x;
ballarin@28834
    34
wenzelm@21006
    35
(* context data *)
wenzelm@21006
    36
haftmann@25462
    37
datatype target = Target of {target: string, is_locale: bool,
haftmann@25864
    38
  is_class: bool, instantiation: string list * (string * sort) list * sort,
haftmann@25864
    39
  overloading: (string * (string * typ) * bool) list};
wenzelm@25012
    40
haftmann@25519
    41
fun make_target target is_locale is_class instantiation overloading =
haftmann@25462
    42
  Target {target = target, is_locale = is_locale,
haftmann@25519
    43
    is_class = is_class, instantiation = instantiation, overloading = overloading};
wenzelm@25291
    44
haftmann@25542
    45
val global_target = make_target "" false false ([], [], []) [];
wenzelm@25012
    46
wenzelm@21006
    47
structure Data = ProofDataFun
wenzelm@21006
    48
(
wenzelm@25012
    49
  type T = target;
wenzelm@25291
    50
  fun init _ = global_target;
wenzelm@21006
    51
);
wenzelm@21006
    52
wenzelm@25012
    53
val peek = (fn Target args => args) o Data.get;
wenzelm@21006
    54
wenzelm@21006
    55
wenzelm@20894
    56
(* pretty *)
wenzelm@20894
    57
haftmann@25607
    58
fun pretty_thy ctxt target is_locale is_class =
wenzelm@20894
    59
  let
wenzelm@20894
    60
    val thy = ProofContext.theory_of ctxt;
ballarin@28834
    61
    val target_name = (if is_class then "class " else "locale ") ^ locale_extern thy target;
haftmann@28965
    62
    val fixes = map (fn (x, T) => (Binding.name x, SOME T, NoSyn))
wenzelm@28094
    63
      (#1 (ProofContext.inferred_fixes ctxt));
haftmann@28965
    64
    val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
wenzelm@28094
    65
      (Assumption.assms_of ctxt);
wenzelm@20894
    66
    val elems =
wenzelm@20894
    67
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    68
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@26049
    69
  in
wenzelm@26049
    70
    if target = "" then []
haftmann@25607
    71
    else if null elems then [Pretty.str target_name]
haftmann@25607
    72
    else [Pretty.big_list (target_name ^ " =")
haftmann@25607
    73
      (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    74
  end;
wenzelm@20894
    75
haftmann@25607
    76
fun pretty (Target {target, is_locale, is_class, instantiation, overloading}) ctxt =
haftmann@25607
    77
  Pretty.block [Pretty.str "theory", Pretty.brk 1,
haftmann@25607
    78
      Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
haftmann@25607
    79
    (if not (null overloading) then [Overloading.pretty ctxt]
wenzelm@26049
    80
     else if not (null (#1 instantiation)) then [Class.pretty_instantiation ctxt]
wenzelm@26049
    81
     else pretty_thy ctxt target is_locale is_class);
haftmann@25607
    82
wenzelm@20894
    83
haftmann@24838
    84
(* target declarations *)
haftmann@24838
    85
haftmann@27690
    86
fun target_decl add (Target {target, is_class, ...}) d lthy =
haftmann@24838
    87
  let
wenzelm@24935
    88
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
haftmann@24838
    89
    val d0 = Morphism.form d';
haftmann@24838
    90
  in
wenzelm@25012
    91
    if target = "" then
haftmann@24838
    92
      lthy
haftmann@24838
    93
      |> LocalTheory.theory (Context.theory_map d0)
haftmann@24838
    94
      |> LocalTheory.target (Context.proof_map d0)
haftmann@24838
    95
    else
haftmann@24838
    96
      lthy
wenzelm@25012
    97
      |> LocalTheory.target (add target d')
haftmann@24838
    98
  end;
haftmann@24838
    99
ballarin@28834
   100
val type_syntax = target_decl locale_add_type_syntax;
ballarin@28834
   101
val term_syntax = target_decl locale_add_term_syntax;
ballarin@28834
   102
val declaration = target_decl locale_add_declaration;
haftmann@24838
   103
wenzelm@25105
   104
fun class_target (Target {target, ...}) f =
wenzelm@25105
   105
  LocalTheory.raw_theory f #>
wenzelm@25105
   106
  LocalTheory.target (Class.refresh_syntax target);
haftmann@24838
   107
wenzelm@21611
   108
wenzelm@20894
   109
(* notes *)
wenzelm@20894
   110
wenzelm@21611
   111
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   112
  let
wenzelm@21611
   113
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   114
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   115
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   116
    val cert = Thm.cterm_of thy;
wenzelm@21594
   117
wenzelm@21611
   118
    (*export assumes/defines*)
wenzelm@21611
   119
    val th = Goal.norm_result raw_th;
wenzelm@21611
   120
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   121
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   122
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   123
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   124
wenzelm@21611
   125
    (*export fixes*)
wenzelm@22692
   126
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   127
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   128
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   129
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   130
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   131
wenzelm@21611
   132
    (*thm definition*)
wenzelm@28083
   133
    val result = PureThy.name_thm true true Position.none name th'';
wenzelm@21611
   134
wenzelm@21611
   135
    (*import fixes*)
wenzelm@21611
   136
    val (tvars, vars) =
wenzelm@21611
   137
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   138
      |>> map Logic.dest_type;
wenzelm@21611
   139
wenzelm@21611
   140
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   141
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   142
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   143
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   144
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   145
wenzelm@21611
   146
    (*import assumes/defines*)
wenzelm@21594
   147
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   148
    val result'' =
wenzelm@21611
   149
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   150
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   151
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   152
      |> Goal.norm_result
wenzelm@28083
   153
      |> PureThy.name_thm false false Position.none name;
wenzelm@21611
   154
wenzelm@21611
   155
  in (result'', result) end;
wenzelm@21611
   156
haftmann@25684
   157
fun note_local kind facts ctxt =
haftmann@25684
   158
  ctxt
haftmann@25684
   159
  |> ProofContext.qualified_names
haftmann@25684
   160
  |> ProofContext.note_thmss_i kind facts
haftmann@25684
   161
  ||> ProofContext.restore_naming ctxt;
haftmann@25684
   162
haftmann@27690
   163
fun notes (Target {target, is_locale, is_class, ...}) kind facts lthy =
wenzelm@21585
   164
  let
wenzelm@21594
   165
    val thy = ProofContext.theory_of lthy;
wenzelm@21594
   166
    val facts' = facts
haftmann@28991
   167
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi
haftmann@28991
   168
          (LocalTheory.full_name lthy (fst a))) bs))
wenzelm@21615
   169
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   170
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   171
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   172
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@25012
   173
      |> is_locale ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   174
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@25012
   175
      |> Attrib.map_facts (if is_locale then K I else Attrib.attribute_i thy);
wenzelm@21594
   176
  in
wenzelm@21611
   177
    lthy |> LocalTheory.theory
wenzelm@21611
   178
      (Sign.qualified_names
wenzelm@26132
   179
        #> PureThy.note_thmss_grouped kind (LocalTheory.group_of lthy) global_facts #> snd
wenzelm@21611
   180
        #> Sign.restore_naming thy)
haftmann@25684
   181
    |> not is_locale ? LocalTheory.target (note_local kind global_facts #> snd)
ballarin@28834
   182
    |> is_locale ? LocalTheory.target (locale_add_thmss target kind target_facts)
haftmann@25684
   183
    |> note_local kind local_facts
wenzelm@20894
   184
  end;
wenzelm@20894
   185
wenzelm@20894
   186
wenzelm@25105
   187
(* declare_const *)
wenzelm@24939
   188
wenzelm@25105
   189
fun fork_mixfix (Target {is_locale, is_class, ...}) mx =
wenzelm@25105
   190
  if not is_locale then (NoSyn, NoSyn, mx)
wenzelm@25105
   191
  else if not is_class then (NoSyn, mx, NoSyn)
wenzelm@25105
   192
  else (mx, NoSyn, NoSyn);
wenzelm@25068
   193
haftmann@28861
   194
fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) tags ((b, mx), rhs) phi =
wenzelm@24939
   195
  let
haftmann@28965
   196
    val b' = Morphism.binding phi b;
wenzelm@24939
   197
    val rhs' = Morphism.term phi rhs;
haftmann@28861
   198
    val legacy_arg = (b', Term.close_schematic_term (Logic.legacy_varify rhs'));
haftmann@28861
   199
    val arg = (b', Term.close_schematic_term rhs');
wenzelm@25372
   200
    val similar_body = Type.similar_types (rhs, rhs');
wenzelm@25212
   201
    (* FIXME workaround based on educated guess *)
haftmann@28965
   202
    val (prefix', _) = Binding.dest b';
haftmann@29006
   203
    val class_global = Binding.base_name b = Binding.base_name b'
haftmann@28861
   204
      andalso not (null prefix')
haftmann@28861
   205
      andalso (fst o snd o split_last) prefix' = Class.class_prefix target;
wenzelm@24939
   206
  in
wenzelm@25372
   207
    not (is_class andalso (similar_body orelse class_global)) ?
wenzelm@25212
   208
      (Context.mapping_result
haftmann@28822
   209
        (fn thy => thy |> 
haftmann@28822
   210
          Sign.no_base_names
haftmann@28822
   211
          |> Sign.add_abbrev PrintMode.internal tags legacy_arg
haftmann@28822
   212
          ||> Sign.restore_naming thy)
wenzelm@26132
   213
        (ProofContext.add_abbrev PrintMode.internal tags arg)
wenzelm@25212
   214
      #-> (fn (lhs' as Const (d, _), _) =>
wenzelm@25372
   215
          similar_body ?
wenzelm@25212
   216
            (Context.mapping (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
wenzelm@25212
   217
             Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
wenzelm@24939
   218
  end;
wenzelm@24939
   219
wenzelm@28083
   220
fun declare_const (ta as Target {target, is_locale, is_class, ...}) depends ((b, T), mx) lthy =
wenzelm@24939
   221
  let
haftmann@29006
   222
    val c = Binding.base_name b;
wenzelm@26132
   223
    val tags = LocalTheory.group_position_of lthy;
wenzelm@24939
   224
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@25105
   225
    val U = map #2 xs ---> T;
wenzelm@25105
   226
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
haftmann@25519
   227
    fun syntax_error c = error ("Illegal mixfix syntax for overloaded constant " ^ quote c);
wenzelm@26049
   228
    val declare_const =
wenzelm@26049
   229
      (case Class.instantiation_param lthy c of
wenzelm@26049
   230
        SOME c' =>
wenzelm@26049
   231
          if mx3 <> NoSyn then syntax_error c'
haftmann@25597
   232
          else LocalTheory.theory_result (AxClass.declare_overloaded (c', U))
haftmann@25485
   233
            ##> Class.confirm_declaration c
wenzelm@26049
   234
        | NONE =>
wenzelm@26049
   235
            (case Overloading.operation lthy c of
wenzelm@26049
   236
              SOME (c', _) =>
wenzelm@26049
   237
                if mx3 <> NoSyn then syntax_error c'
wenzelm@26049
   238
                else LocalTheory.theory_result (Overloading.declare (c', U))
wenzelm@26049
   239
                  ##> Overloading.confirm c
wenzelm@28115
   240
            | NONE => LocalTheory.theory_result (Sign.declare_const tags ((b, U), mx3))));
haftmann@25485
   241
    val (const, lthy') = lthy |> declare_const;
wenzelm@25105
   242
    val t = Term.list_comb (const, map Free xs);
wenzelm@24939
   243
  in
wenzelm@24939
   244
    lthy'
wenzelm@28083
   245
    |> is_locale ? term_syntax ta (locale_const ta Syntax.mode_default tags ((b, mx2), t))
wenzelm@26132
   246
    |> is_class ? class_target ta (Class.declare target tags ((c, mx1), t))
wenzelm@28083
   247
    |> LocalDefs.add_def ((b, NoSyn), t)
wenzelm@24939
   248
  end;
wenzelm@24939
   249
wenzelm@24939
   250
wenzelm@24939
   251
(* abbrev *)
wenzelm@24939
   252
wenzelm@28083
   253
fun abbrev (ta as Target {target, is_locale, is_class, ...}) prmode ((b, mx), t) lthy =
wenzelm@24939
   254
  let
haftmann@29006
   255
    val c = Binding.base_name b;
wenzelm@26132
   256
    val tags = LocalTheory.group_position_of lthy;
wenzelm@25053
   257
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@25012
   258
    val target_ctxt = LocalTheory.target_of lthy;
wenzelm@25105
   259
wenzelm@25105
   260
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@25105
   261
    val t' = Assumption.export_term lthy target_ctxt t;
wenzelm@25105
   262
    val xs = map Free (rev (Variable.add_fixed target_ctxt t' []));
wenzelm@25105
   263
    val u = fold_rev lambda xs t';
wenzelm@25105
   264
    val global_rhs =
wenzelm@25105
   265
      singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
wenzelm@25121
   266
  in
wenzelm@25121
   267
    lthy |>
wenzelm@25121
   268
     (if is_locale then
haftmann@28861
   269
        LocalTheory.theory_result (Sign.add_abbrev PrintMode.internal tags (b, global_rhs))
wenzelm@25121
   270
        #-> (fn (lhs, _) =>
wenzelm@25105
   271
          let val lhs' = Term.list_comb (Logic.unvarify lhs, xs) in
wenzelm@28083
   272
            term_syntax ta (locale_const ta prmode tags ((b, mx2), lhs')) #>
wenzelm@26132
   273
            is_class ? class_target ta (Class.abbrev target prmode tags ((c, mx1), t'))
wenzelm@25105
   274
          end)
wenzelm@25105
   275
      else
wenzelm@25121
   276
        LocalTheory.theory
haftmann@28861
   277
          (Sign.add_abbrev (#1 prmode) tags (b, global_rhs) #-> (fn (lhs, _) =>
wenzelm@25121
   278
           Sign.notation true prmode [(lhs, mx3)])))
haftmann@28861
   279
    |> ProofContext.add_abbrev PrintMode.internal tags (b, t) |> snd
wenzelm@28083
   280
    |> LocalDefs.fixed_abbrev ((b, NoSyn), t)
wenzelm@24939
   281
  end;
wenzelm@24939
   282
wenzelm@24939
   283
wenzelm@25022
   284
(* define *)
wenzelm@24939
   285
haftmann@25485
   286
fun define (ta as Target {target, is_locale, is_class, ...})
wenzelm@28083
   287
    kind ((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
haftmann@29006
   292
    val c = Binding.base_name b;
haftmann@28965
   293
    val name' = Binding.map_base (Thm.def_name_optional c) name;
wenzelm@24987
   294
    val (rhs', rhs_conv) =
wenzelm@24987
   295
      LocalDefs.export_cterm lthy thy_ctxt (Thm.cterm_of thy rhs) |>> Thm.term_of;
wenzelm@24987
   296
    val xs = Variable.add_fixed (LocalTheory.target_of lthy) rhs' [];
wenzelm@24939
   297
    val T = Term.fastype_of rhs;
wenzelm@24939
   298
wenzelm@25105
   299
    (*const*)
wenzelm@28083
   300
    val ((lhs, local_def), lthy2) = lthy |> declare_const ta (member (op =) xs) ((b, T), mx);
wenzelm@25022
   301
    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
wenzelm@24939
   302
wenzelm@24939
   303
    (*def*)
wenzelm@26049
   304
    val define_const =
wenzelm@26049
   305
      (case Overloading.operation lthy c of
wenzelm@26049
   306
        SOME (_, checked) =>
haftmann@25519
   307
          (fn name => fn (Const (c, _), rhs) => Overloading.define checked name (c, rhs))
wenzelm@26049
   308
      | NONE =>
wenzelm@26049
   309
          if is_none (Class.instantiation_param lthy c)
haftmann@25519
   310
          then (fn name => fn eq => Thm.add_def false false (name, Logic.mk_equals eq))
wenzelm@26049
   311
          else (fn name => fn (Const (c, _), rhs) => AxClass.define_overloaded name (c, rhs)));
wenzelm@25022
   312
    val (global_def, lthy3) = lthy2
haftmann@29006
   313
      |> LocalTheory.theory_result (define_const (Binding.base_name name') (lhs', rhs'));
haftmann@25485
   314
    val def = LocalDefs.trans_terms lthy3
wenzelm@25022
   315
      [(*c == global.c xs*)     local_def,
wenzelm@25022
   316
       (*global.c xs == rhs'*)  global_def,
haftmann@25485
   317
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
wenzelm@24939
   318
wenzelm@25105
   319
    (*note*)
wenzelm@24939
   320
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@26132
   321
      |> notes ta kind [((name', atts), [([def], [])])];
wenzelm@24939
   322
  in ((lhs, (res_name, res)), lthy4) end;
wenzelm@24939
   323
wenzelm@24939
   324
wenzelm@25291
   325
(* init *)
wenzelm@25291
   326
wenzelm@25291
   327
local
wenzelm@20894
   328
wenzelm@25291
   329
fun init_target _ NONE = global_target
wenzelm@26049
   330
  | init_target thy (SOME target) =
wenzelm@26049
   331
      make_target target true (Class.is_class thy target) ([], [], []) [];
wenzelm@25291
   332
haftmann@25519
   333
fun init_ctxt (Target {target, is_locale, is_class, instantiation, overloading}) =
wenzelm@26049
   334
  if not (null (#1 instantiation)) then Class.init_instantiation instantiation
haftmann@25519
   335
  else if not (null overloading) then Overloading.init overloading
haftmann@25485
   336
  else if not is_locale then ProofContext.init
ballarin@28834
   337
  else if not is_class then locale_init target
haftmann@25485
   338
  else Class.init target;
haftmann@25269
   339
haftmann@25542
   340
fun init_lthy (ta as Target {target, instantiation, overloading, ...}) =
wenzelm@25291
   341
  Data.put ta #>
wenzelm@25291
   342
  LocalTheory.init (NameSpace.base target)
wenzelm@25291
   343
   {pretty = pretty ta,
wenzelm@25291
   344
    abbrev = abbrev ta,
wenzelm@25291
   345
    define = define ta,
wenzelm@25291
   346
    notes = notes ta,
wenzelm@25291
   347
    type_syntax = type_syntax ta,
wenzelm@25291
   348
    term_syntax = term_syntax ta,
wenzelm@25291
   349
    declaration = declaration ta,
wenzelm@25291
   350
    reinit = fn lthy => init_lthy_ctxt ta (ProofContext.theory_of lthy),
wenzelm@26049
   351
    exit = LocalTheory.target_of o
wenzelm@26049
   352
      (if not (null (#1 instantiation)) then Class.conclude_instantiation
wenzelm@26049
   353
       else if not (null overloading) then Overloading.conclude
wenzelm@26049
   354
       else I)}
wenzelm@25291
   355
and init_lthy_ctxt ta = init_lthy ta o init_ctxt ta;
wenzelm@20894
   356
wenzelm@26049
   357
fun gen_overloading prep_const raw_ops thy =
wenzelm@26049
   358
  let
wenzelm@26049
   359
    val ctxt = ProofContext.init thy;
wenzelm@26049
   360
    val ops = raw_ops |> map (fn (name, const, checked) =>
wenzelm@26049
   361
      (name, Term.dest_Const (prep_const ctxt const), checked));
wenzelm@26049
   362
  in thy |> init_lthy_ctxt (make_target "" false false ([], [], []) ops) end;
wenzelm@26049
   363
wenzelm@25291
   364
in
wenzelm@20894
   365
wenzelm@25291
   366
fun init target thy = init_lthy_ctxt (init_target thy target) thy;
wenzelm@26049
   367
fun begin target ctxt = init_lthy (init_target (ProofContext.theory_of ctxt) (SOME target)) ctxt;
haftmann@25269
   368
wenzelm@25291
   369
fun context "-" thy = init NONE thy
ballarin@28834
   370
  | context target thy = init (SOME (locale_intern thy target)) thy;
wenzelm@20894
   371
wenzelm@26049
   372
fun instantiation arities = init_lthy_ctxt (make_target "" false false arities []);
haftmann@25519
   373
wenzelm@26049
   374
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const);
wenzelm@26049
   375
val overloading_cmd = gen_overloading Syntax.read_term;
haftmann@25462
   376
wenzelm@20894
   377
end;
wenzelm@25291
   378
wenzelm@25291
   379
end;
wenzelm@25291
   380