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