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