src/Pure/Isar/theory_target.ML
author wenzelm
Sun Sep 30 16:20:41 2007 +0200 (2007-09-30)
changeset 24779 2949fb459c7b
parent 24748 ee0a0eb6b738
child 24818 d07e56a9a0c2
permissions -rw-r--r--
keep context position as tags for consts/thms;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@21594
     5
Common theory/locale targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@21006
    10
  val peek: local_theory -> string option
haftmann@22353
    11
  val begin: bstring -> Proof.context -> local_theory
wenzelm@20894
    12
  val init: xstring option -> theory -> local_theory
wenzelm@20894
    13
  val init_i: string option -> theory -> local_theory
wenzelm@20894
    14
end;
wenzelm@20894
    15
wenzelm@20894
    16
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    17
struct
wenzelm@20894
    18
wenzelm@20894
    19
(** locale targets **)
wenzelm@20894
    20
wenzelm@21006
    21
(* context data *)
wenzelm@21006
    22
wenzelm@21006
    23
structure Data = ProofDataFun
wenzelm@21006
    24
(
wenzelm@21006
    25
  type T = string option;
wenzelm@21006
    26
  fun init _ = NONE;
wenzelm@21006
    27
);
wenzelm@21006
    28
wenzelm@21006
    29
val peek = Data.get;
wenzelm@21006
    30
wenzelm@21006
    31
wenzelm@20894
    32
(* pretty *)
wenzelm@20894
    33
wenzelm@20894
    34
fun pretty loc ctxt =
wenzelm@20894
    35
  let
wenzelm@20894
    36
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    37
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    38
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    39
    val elems =
wenzelm@20894
    40
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    41
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    42
  in
wenzelm@20984
    43
    if loc = "" then
wenzelm@20984
    44
      [Pretty.block
wenzelm@20984
    45
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    46
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    47
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    48
    else
wenzelm@20984
    49
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    50
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    51
  end;
wenzelm@20894
    52
wenzelm@20894
    53
wenzelm@21804
    54
(* consts *)
wenzelm@21804
    55
wenzelm@23205
    56
fun target_abbrev prmode ((c, mx), rhs) phi =
wenzelm@23205
    57
  let
wenzelm@23205
    58
    val c' = Morphism.name phi c;
wenzelm@23205
    59
    val rhs' = Morphism.term phi rhs;
wenzelm@23205
    60
    val arg' = (c', rhs');
wenzelm@23205
    61
    val eq_heads =
wenzelm@23205
    62
      (case pairself Term.head_of (rhs, rhs') of
wenzelm@23205
    63
        (Const (a, _), Const (a', _)) => a = a'
wenzelm@23205
    64
      | _ => false);
wenzelm@23205
    65
  in
wenzelm@23205
    66
    eq_heads ? (Context.mapping_result
wenzelm@24779
    67
      (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
wenzelm@23205
    68
    #-> (fn (lhs, _) =>
wenzelm@23205
    69
      Term.equiv_types (rhs, rhs') ?
wenzelm@23205
    70
        Morphism.form (ProofContext.target_notation prmode [(lhs, mx)])))
wenzelm@23205
    71
  end;
wenzelm@23205
    72
wenzelm@24779
    73
fun internal_abbrev prmode ((c, mx), t) lthy = lthy
wenzelm@22380
    74
  (* FIXME really permissive *)
wenzelm@24779
    75
  |> LocalTheory.term_syntax (perhaps o try o target_abbrev prmode ((c, mx), t))
wenzelm@24779
    76
  |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
wenzelm@24779
    77
  |> snd;
wenzelm@21804
    78
haftmann@22353
    79
fun consts is_loc some_class depends decls lthy =
wenzelm@21804
    80
  let
wenzelm@21804
    81
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@21747
    82
wenzelm@21804
    83
    fun const ((c, T), mx) thy =
wenzelm@21804
    84
      let
wenzelm@21804
    85
        val U = map #2 xs ---> T;
wenzelm@21804
    86
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
haftmann@24218
    87
        val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
wenzelm@24779
    88
        val thy' = Sign.add_consts_authentic (ContextPosition.properties_of lthy) [(c, U, mx1)] thy;
wenzelm@21804
    89
      in (((c, mx2), t), thy') end;
wenzelm@21804
    90
haftmann@23087
    91
    fun const_class (SOME class) ((c, _), mx) (_, t) =
haftmann@24218
    92
          Class.add_const_in_class class ((c, t), mx)
haftmann@23087
    93
      | const_class NONE _ _ = I;
haftmann@24657
    94
    fun hide_abbrev (SOME class) abbrs thy =
haftmann@24657
    95
          let
haftmann@24657
    96
            val raw_cs = map (fst o fst) abbrs;
haftmann@24657
    97
            val prfx = (Logic.const_of_class o NameSpace.base) class;
haftmann@24657
    98
            fun mk_name c =
haftmann@24657
    99
              let
haftmann@24657
   100
                val n1 = Sign.full_name thy c;
haftmann@24657
   101
                val n2 = NameSpace.qualifier n1;
haftmann@24657
   102
                val n3 = NameSpace.base n1;
haftmann@24657
   103
              in NameSpace.implode [n2, prfx, prfx, n3] end;
haftmann@24657
   104
            val cs = map mk_name raw_cs;
haftmann@24657
   105
          in
haftmann@24657
   106
            Sign.hide_consts_i true cs thy
haftmann@24657
   107
          end
haftmann@24657
   108
      | hide_abbrev NONE _ thy = thy;
haftmann@23087
   109
    val (abbrs, lthy') = lthy
haftmann@23087
   110
      |> LocalTheory.theory_result (fold_map const decls)
wenzelm@21804
   111
    val defs = map (apsnd (pair ("", []))) abbrs;
haftmann@24657
   112
    
wenzelm@21804
   113
  in
wenzelm@21804
   114
    lthy'
haftmann@24657
   115
    |> LocalTheory.raw_theory (fold2 (const_class some_class) decls abbrs)
wenzelm@21825
   116
    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
haftmann@24657
   117
    |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
haftmann@24657
   118
        (*FIXME abbreviations should never occur*)
haftmann@23087
   119
    |> LocalDefs.add_defs defs
haftmann@23087
   120
    |>> map (apsnd snd)
wenzelm@21804
   121
  end;
wenzelm@21804
   122
wenzelm@21804
   123
wenzelm@21804
   124
(* abbrev *)
wenzelm@21747
   125
wenzelm@21747
   126
fun occ_params ctxt ts =
wenzelm@21747
   127
  #1 (ProofContext.inferred_fixes ctxt)
wenzelm@21747
   128
  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
wenzelm@21747
   129
wenzelm@23205
   130
fun local_abbrev (x, rhs) =
wenzelm@23205
   131
  Variable.add_fixes [x] #-> (fn [x'] =>
wenzelm@23205
   132
    let
wenzelm@23205
   133
      val T = Term.fastype_of rhs;
wenzelm@23205
   134
      val lhs = Free (x', T);
wenzelm@23205
   135
    in
wenzelm@23205
   136
      Variable.declare_term lhs #>
wenzelm@23205
   137
      Variable.declare_term rhs #>
wenzelm@23205
   138
      Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
wenzelm@23205
   139
      pair (lhs, rhs)
wenzelm@23205
   140
    end);
wenzelm@23205
   141
haftmann@23087
   142
fun abbrev is_loc some_class prmode ((raw_c, mx), raw_t) lthy =
wenzelm@21747
   143
  let
wenzelm@21747
   144
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@21747
   145
    val target = LocalTheory.target_of lthy;
wenzelm@21747
   146
    val target_morphism = LocalTheory.target_morphism lthy;
wenzelm@21747
   147
wenzelm@21747
   148
    val c = Morphism.name target_morphism raw_c;
wenzelm@21747
   149
    val t = Morphism.term target_morphism raw_t;
wenzelm@21747
   150
    val xs = map Free (occ_params target [t]);
wenzelm@21747
   151
    val u = fold_rev Term.lambda xs t;
wenzelm@21804
   152
    val U = Term.fastype_of u;
wenzelm@21747
   153
    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
wenzelm@21747
   154
wenzelm@24779
   155
    val ((lhs as Const (full_c, _), rhs), lthy1) = lthy |> LocalTheory.theory_result
wenzelm@24779
   156
      (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'));
haftmann@24218
   157
    val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
wenzelm@21747
   158
  in
wenzelm@21747
   159
    lthy1
wenzelm@21804
   160
    |> LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
wenzelm@21825
   161
    |> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
wenzelm@23205
   162
    |> local_abbrev (c, rhs)
wenzelm@21747
   163
  end;
wenzelm@21747
   164
wenzelm@21747
   165
wenzelm@20894
   166
(* defs *)
wenzelm@20894
   167
wenzelm@21845
   168
local
wenzelm@21570
   169
wenzelm@21594
   170
fun expand_term ctxt t =
wenzelm@21570
   171
  let
wenzelm@21570
   172
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
   173
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
   174
    val ct = Thm.cterm_of thy t;
wenzelm@21594
   175
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21708
   176
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
wenzelm@21570
   177
wenzelm@20894
   178
fun add_def (name, prop) =
wenzelm@20894
   179
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   180
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   181
wenzelm@20894
   182
in
wenzelm@20894
   183
haftmann@24303
   184
fun defs kind args lthy0 =
wenzelm@20894
   185
  let
wenzelm@21533
   186
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   187
      let
wenzelm@21594
   188
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   189
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
haftmann@23087
   190
        val T = Term.fastype_of rhs;
wenzelm@20894
   191
wenzelm@21570
   192
        val ([(lhs, lhs_def)], lthy2) = lthy1
haftmann@23087
   193
          |> LocalTheory.consts (member (op =) xs) [((c, T), mx)];
wenzelm@21570
   194
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   195
wenzelm@21570
   196
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   197
        val (def, lthy3) = lthy2
wenzelm@20894
   198
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@21845
   199
        val eq = LocalDefs.trans_terms lthy3
wenzelm@21845
   200
          [(*c == loc.c xs*) lhs_def,
wenzelm@21845
   201
           (*lhs' == rhs'*)  def,
wenzelm@21845
   202
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
haftmann@23087
   203
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   204
wenzelm@21570
   205
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   206
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
haftmann@22424
   207
wenzelm@20894
   208
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   209
wenzelm@20894
   210
end;
wenzelm@20894
   211
wenzelm@20894
   212
wenzelm@21611
   213
(* axioms *)
wenzelm@21611
   214
wenzelm@21611
   215
local
wenzelm@21611
   216
wenzelm@21611
   217
fun add_axiom hyps (name, prop) thy =
wenzelm@21611
   218
  let
wenzelm@21611
   219
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@21611
   220
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@21611
   221
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@21611
   222
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@21611
   223
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@21611
   224
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@21611
   225
wenzelm@21611
   226
in
wenzelm@21611
   227
wenzelm@21611
   228
fun axioms kind specs lthy =
wenzelm@21611
   229
  let
wenzelm@21611
   230
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@21611
   231
    fun axiom ((name, atts), props) thy = thy
wenzelm@21611
   232
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@21611
   233
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@21611
   234
  in
wenzelm@21611
   235
    lthy
wenzelm@21611
   236
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@21611
   237
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21611
   238
    |-> LocalTheory.notes kind
wenzelm@21611
   239
  end;
wenzelm@21611
   240
wenzelm@21611
   241
end;
wenzelm@21611
   242
wenzelm@21611
   243
wenzelm@20894
   244
(* notes *)
wenzelm@20894
   245
wenzelm@21611
   246
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   247
  let
wenzelm@21611
   248
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   249
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   250
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   251
    val cert = Thm.cterm_of thy;
wenzelm@21594
   252
wenzelm@21611
   253
    (*export assumes/defines*)
wenzelm@21611
   254
    val th = Goal.norm_result raw_th;
wenzelm@21611
   255
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   256
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   257
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   258
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   259
wenzelm@21611
   260
    (*export fixes*)
wenzelm@22692
   261
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   262
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   263
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   264
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   265
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   266
wenzelm@21611
   267
    (*thm definition*)
wenzelm@21644
   268
    val result = th''
wenzelm@21644
   269
      |> PureThy.name_thm true true ""
wenzelm@21644
   270
      |> Goal.close_result
wenzelm@24779
   271
      |> fold PureThy.tag_rule (ContextPosition.properties_of ctxt)
wenzelm@21644
   272
      |> PureThy.name_thm true true name;
wenzelm@21611
   273
wenzelm@21611
   274
    (*import fixes*)
wenzelm@21611
   275
    val (tvars, vars) =
wenzelm@21611
   276
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   277
      |>> map Logic.dest_type;
wenzelm@21611
   278
wenzelm@21611
   279
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   280
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   281
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   282
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   283
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   284
wenzelm@21611
   285
    (*import assumes/defines*)
wenzelm@21594
   286
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   287
    val result'' =
wenzelm@21611
   288
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   289
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   290
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   291
      |> Goal.norm_result
wenzelm@21644
   292
      |> PureThy.name_thm false false name;
wenzelm@21611
   293
wenzelm@21611
   294
  in (result'', result) end;
wenzelm@21611
   295
wenzelm@21594
   296
fun notes loc kind facts lthy =
wenzelm@21585
   297
  let
wenzelm@21594
   298
    val is_loc = loc <> "";
wenzelm@21594
   299
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   300
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   301
wenzelm@21594
   302
    val facts' = facts
wenzelm@21615
   303
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   304
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   305
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   306
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   307
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   308
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   309
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   310
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   311
  in
wenzelm@21611
   312
    lthy |> LocalTheory.theory
wenzelm@21611
   313
      (Sign.qualified_names
wenzelm@21611
   314
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   315
        #> Sign.restore_naming thy)
wenzelm@21585
   316
wenzelm@21611
   317
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   318
wenzelm@21433
   319
    |> ProofContext.set_stmt true
wenzelm@20894
   320
    |> ProofContext.qualified_names
wenzelm@21611
   321
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   322
    ||> ProofContext.restore_naming lthy
wenzelm@24389
   323
    ||> ProofContext.restore_mode lthy
wenzelm@20894
   324
  end;
wenzelm@20894
   325
wenzelm@20894
   326
wenzelm@21533
   327
(* target declarations *)
wenzelm@20894
   328
wenzelm@22673
   329
fun target_decl add loc d lthy =
wenzelm@22673
   330
  let
wenzelm@22673
   331
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
wenzelm@22673
   332
    val d0 = Morphism.form d';
wenzelm@22673
   333
  in
wenzelm@22673
   334
    if loc = "" then
wenzelm@22673
   335
      lthy
wenzelm@22673
   336
      |> LocalTheory.theory (Context.theory_map d0)
wenzelm@22673
   337
      |> LocalTheory.target (Context.proof_map d0)
wenzelm@22673
   338
    else
wenzelm@22673
   339
      lthy
wenzelm@22673
   340
      |> LocalTheory.target (add loc d')
wenzelm@22673
   341
  end;
wenzelm@20894
   342
wenzelm@21611
   343
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   344
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   345
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   346
wenzelm@21533
   347
fun target_morphism loc lthy =
wenzelm@21594
   348
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   349
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   350
wenzelm@22203
   351
fun target_naming loc lthy =
wenzelm@21668
   352
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   353
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@22203
   354
  |> NameSpace.qualified_names;
wenzelm@21533
   355
wenzelm@20894
   356
wenzelm@20894
   357
(* init and exit *)
wenzelm@20894
   358
haftmann@22353
   359
fun begin loc ctxt =
haftmann@22353
   360
  let
haftmann@22353
   361
    val thy = ProofContext.theory_of ctxt;
haftmann@22353
   362
    val is_loc = loc <> "";
haftmann@24218
   363
    val some_class = Class.class_of_locale thy loc;
haftmann@24748
   364
    fun class_init (SOME class) =
haftmann@24748
   365
          Class.init [class]
haftmann@24748
   366
      | class_init NONE =
haftmann@24748
   367
          I;
haftmann@22353
   368
  in
haftmann@22353
   369
    ctxt
haftmann@22353
   370
    |> Data.put (if is_loc then SOME loc else NONE)
haftmann@24748
   371
    |> class_init some_class
haftmann@24748
   372
    |> LocalTheory.init (NameSpace.base loc)
wenzelm@21747
   373
     {pretty = pretty loc,
haftmann@22353
   374
      consts = consts is_loc some_class,
wenzelm@21747
   375
      axioms = axioms,
haftmann@22353
   376
      abbrev = abbrev is_loc some_class,
haftmann@24303
   377
      defs = defs,
wenzelm@21747
   378
      notes = notes loc,
wenzelm@21747
   379
      type_syntax = type_syntax loc,
wenzelm@21747
   380
      term_syntax = term_syntax loc,
wenzelm@21747
   381
      declaration = declaration loc,
wenzelm@21747
   382
      target_morphism = target_morphism loc,
wenzelm@22203
   383
      target_naming = target_naming loc,
wenzelm@21804
   384
      reinit = fn _ =>
haftmann@24657
   385
        (if is_loc then Locale.init loc else ProofContext.init)
haftmann@24657
   386
        #> begin loc,
haftmann@24748
   387
      exit = LocalTheory.target_of }
wenzelm@21747
   388
  end;
wenzelm@20894
   389
haftmann@22353
   390
fun init_i NONE thy = begin "" (ProofContext.init thy)
haftmann@22353
   391
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   392
wenzelm@21276
   393
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   394
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   395
wenzelm@20894
   396
end;