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