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