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