src/Pure/Isar/theory_target.ML
author wenzelm
Sat Mar 13 19:35:53 2010 +0100 (2010-03-13)
changeset 35764 f7f88f2e004f
parent 35739 35a3b3721ffb
child 35765 09e238561460
permissions -rw-r--r--
minor tuning and simplification;
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@35764
   250
fun declare_const (ta as Target {target, is_locale, is_class, ...}) xs ((b, T), mx) lthy =
wenzelm@35764
   251
  let
wenzelm@35764
   252
    val params =
wenzelm@35764
   253
      rev (Variable.fixes_of (Local_Theory.target_of lthy))
wenzelm@35764
   254
      |> map_filter (fn (_, x) =>
wenzelm@35764
   255
        (case AList.lookup (op =) xs x of
wenzelm@35764
   256
          SOME T => SOME (x, T)
wenzelm@35764
   257
        | NONE => NONE));
wenzelm@35764
   258
    val U = map #2 params ---> T;
wenzelm@35764
   259
    val (mx1, mx2, mx3) = fork_mixfix ta mx;
wenzelm@35764
   260
    val (const, lthy') = lthy |>
wenzelm@35764
   261
      (case Class_Target.instantiation_param lthy b of
wenzelm@35764
   262
        SOME c' =>
wenzelm@35764
   263
          if mx3 <> NoSyn then syntax_error c'
wenzelm@35764
   264
          else Local_Theory.theory_result (AxClass.declare_overloaded (c', U))
wenzelm@35764
   265
            ##> Class_Target.confirm_declaration b
wenzelm@35764
   266
      | NONE =>
wenzelm@35764
   267
          (case Overloading.operation lthy b of
wenzelm@35764
   268
            SOME (c', _) =>
wenzelm@35764
   269
              if mx3 <> NoSyn then syntax_error c'
wenzelm@35764
   270
              else Local_Theory.theory_result (Overloading.declare (c', U))
wenzelm@35764
   271
                ##> Overloading.confirm b
wenzelm@35764
   272
          | NONE => Local_Theory.theory_result (Sign.declare_const ((b, U), mx3))));
wenzelm@35764
   273
    val t = Term.list_comb (const, map Free params);
wenzelm@35764
   274
  in
wenzelm@35764
   275
    lthy'
wenzelm@35764
   276
    |> is_locale ? term_syntax ta false (locale_const ta Syntax.mode_default ((b, mx2), t))
wenzelm@35764
   277
    |> is_class ? class_target ta (Class_Target.declare target ((b, mx1), t))
wenzelm@35764
   278
    |> Local_Defs.add_def ((b, NoSyn), t)
wenzelm@35764
   279
  end;
wenzelm@35764
   280
wenzelm@35764
   281
in
wenzelm@35764
   282
wenzelm@33764
   283
fun define ta ((b, mx), ((name, atts), rhs)) lthy =
wenzelm@24939
   284
  let
wenzelm@24987
   285
    val thy = ProofContext.theory_of lthy;
wenzelm@24939
   286
    val thy_ctxt = ProofContext.init thy;
wenzelm@24939
   287
wenzelm@30437
   288
    val name' = Thm.def_binding_optional b name;
wenzelm@35717
   289
wenzelm@35717
   290
    val crhs = Thm.cterm_of thy rhs;
wenzelm@35717
   291
    val (defs, rhs') = Local_Defs.export_cterm lthy thy_ctxt crhs ||> Thm.term_of;
wenzelm@35717
   292
    val rhs_conv = MetaSimplifier.rewrite true defs crhs;
wenzelm@35717
   293
wenzelm@33671
   294
    val xs = Variable.add_fixed (Local_Theory.target_of lthy) rhs' [];
wenzelm@24939
   295
    val T = Term.fastype_of rhs;
wenzelm@24939
   296
wenzelm@25105
   297
    (*const*)
wenzelm@35764
   298
    val ((lhs, local_def), lthy2) = lthy |> declare_const ta xs ((b, T), mx);
wenzelm@25022
   299
    val (_, lhs') = Logic.dest_equals (Thm.prop_of local_def);
wenzelm@24939
   300
wenzelm@24939
   301
    (*def*)
wenzelm@25022
   302
    val (global_def, lthy3) = lthy2
wenzelm@33671
   303
      |> Local_Theory.theory_result
wenzelm@33282
   304
        (case Overloading.operation lthy b of
wenzelm@33282
   305
          SOME (_, checked) => Overloading.define checked name' (fst (dest_Const lhs'), rhs')
haftmann@30519
   306
        | NONE =>
wenzelm@35764
   307
            if is_some (Class_Target.instantiation_param lthy b)
wenzelm@35764
   308
            then AxClass.define_overloaded name' (fst (dest_Const lhs'), rhs')
wenzelm@35764
   309
            else Thm.add_def false false (name', Logic.mk_equals (lhs', rhs')));
wenzelm@35624
   310
    val def = Local_Defs.trans_terms lthy3
wenzelm@25022
   311
      [(*c == global.c xs*)     local_def,
wenzelm@25022
   312
       (*global.c xs == rhs'*)  global_def,
haftmann@25485
   313
       (*rhs' == rhs*)          Thm.symmetric rhs_conv];
wenzelm@24939
   314
wenzelm@25105
   315
    (*note*)
wenzelm@24939
   316
    val ([(res_name, [res])], lthy4) = lthy3
wenzelm@33764
   317
      |> notes ta "" [((name', atts), [([def], [])])];
wenzelm@24939
   318
  in ((lhs, (res_name, res)), lthy4) end;
wenzelm@24939
   319
wenzelm@35764
   320
end;
wenzelm@35764
   321
wenzelm@24939
   322
wenzelm@35205
   323
(* init various targets *)
wenzelm@25291
   324
wenzelm@25291
   325
local
wenzelm@20894
   326
wenzelm@25291
   327
fun init_target _ NONE = global_target
wenzelm@33670
   328
  | init_target thy (SOME target) =
wenzelm@35205
   329
      if Locale.defined thy target
haftmann@29710
   330
      then make_target target true (Class_Target.is_class thy target) ([], [], []) []
haftmann@29710
   331
      else error ("No such locale: " ^ quote target);
wenzelm@25291
   332
haftmann@29576
   333
fun init_ctxt (Target {target, is_locale, is_class, instantiation, overloading}) =
haftmann@29358
   334
  if not (null (#1 instantiation)) then Class_Target.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
haftmann@29576
   337
  else if not is_class then Locale.init target
haftmann@29358
   338
  else Class_Target.init target;
haftmann@25269
   339
haftmann@25542
   340
fun init_lthy (ta as Target {target, instantiation, overloading, ...}) =
wenzelm@25291
   341
  Data.put ta #>
wenzelm@33724
   342
  Local_Theory.init NONE (Long_Name.base_name 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@33671
   351
    exit = Local_Theory.target_of o
haftmann@29358
   352
      (if not (null (#1 instantiation)) then Class_Target.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));
haftmann@29576
   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;
haftmann@25269
   367
wenzelm@35205
   368
fun context_cmd "-" thy = init NONE thy
wenzelm@35205
   369
  | context_cmd target thy = init (SOME (Locale.intern thy target)) thy;
wenzelm@33282
   370
haftmann@29576
   371
fun instantiation arities = init_lthy_ctxt (make_target "" false false arities []);
wenzelm@35205
   372
fun instantiation_cmd arities thy = instantiation (Class_Target.read_multi_arity thy arities) thy;
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