src/Pure/Isar/theory_target.ML
author wenzelm
Tue Oct 02 22:23:31 2007 +0200 (2007-10-02)
changeset 24818 d07e56a9a0c2
parent 24779 2949fb459c7b
child 24838 1d1bddf87353
permissions -rw-r--r--
added add_defs_new, which strips sorts for axioms (presently inactive);
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;
wenzelm@24818
   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@24818
   182
fun add_def_new (name, prop) thy =  (* FIXME inactive --- breaks codegen *)
wenzelm@24818
   183
  let
wenzelm@24818
   184
    val tfrees = rev (map TFree (Term.add_tfrees prop []));
wenzelm@24818
   185
    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context "'a" (length tfrees));
wenzelm@24818
   186
    val strip_sorts = tfrees ~~ tfrees';
wenzelm@24818
   187
    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
wenzelm@24818
   188
wenzelm@24818
   189
    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
wenzelm@24818
   190
    val thy' = Theory.add_defs_i false false [(name, prop')] thy;
wenzelm@24818
   191
    val axm = Thm.get_axiom_i thy' (Sign.full_name thy' name);
wenzelm@24818
   192
    val def = Thm.instantiate (recover_sorts, []) axm;
wenzelm@24818
   193
  in (Drule.unvarify def, thy') end;
wenzelm@24818
   194
wenzelm@20894
   195
in
wenzelm@20894
   196
haftmann@24303
   197
fun defs kind args lthy0 =
wenzelm@20894
   198
  let
wenzelm@21533
   199
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   200
      let
wenzelm@21594
   201
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   202
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
haftmann@23087
   203
        val T = Term.fastype_of rhs;
wenzelm@20894
   204
wenzelm@21570
   205
        val ([(lhs, lhs_def)], lthy2) = lthy1
haftmann@23087
   206
          |> LocalTheory.consts (member (op =) xs) [((c, T), mx)];
wenzelm@21570
   207
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   208
wenzelm@21570
   209
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   210
        val (def, lthy3) = lthy2
wenzelm@20894
   211
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@21845
   212
        val eq = LocalDefs.trans_terms lthy3
wenzelm@21845
   213
          [(*c == loc.c xs*) lhs_def,
wenzelm@21845
   214
           (*lhs' == rhs'*)  def,
wenzelm@21845
   215
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
haftmann@23087
   216
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   217
wenzelm@21570
   218
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   219
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
haftmann@22424
   220
wenzelm@20894
   221
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   222
wenzelm@20894
   223
end;
wenzelm@20894
   224
wenzelm@20894
   225
wenzelm@21611
   226
(* axioms *)
wenzelm@21611
   227
wenzelm@21611
   228
local
wenzelm@21611
   229
wenzelm@21611
   230
fun add_axiom hyps (name, prop) thy =
wenzelm@21611
   231
  let
wenzelm@21611
   232
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@21611
   233
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@21611
   234
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@21611
   235
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@21611
   236
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@21611
   237
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@21611
   238
wenzelm@21611
   239
in
wenzelm@21611
   240
wenzelm@21611
   241
fun axioms kind specs lthy =
wenzelm@21611
   242
  let
wenzelm@21611
   243
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@21611
   244
    fun axiom ((name, atts), props) thy = thy
wenzelm@21611
   245
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@21611
   246
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@21611
   247
  in
wenzelm@21611
   248
    lthy
wenzelm@21611
   249
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@21611
   250
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21611
   251
    |-> LocalTheory.notes kind
wenzelm@21611
   252
  end;
wenzelm@21611
   253
wenzelm@21611
   254
end;
wenzelm@21611
   255
wenzelm@21611
   256
wenzelm@20894
   257
(* notes *)
wenzelm@20894
   258
wenzelm@21611
   259
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   260
  let
wenzelm@21611
   261
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   262
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   263
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   264
    val cert = Thm.cterm_of thy;
wenzelm@21594
   265
wenzelm@21611
   266
    (*export assumes/defines*)
wenzelm@21611
   267
    val th = Goal.norm_result raw_th;
wenzelm@21611
   268
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   269
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   270
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   271
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   272
wenzelm@21611
   273
    (*export fixes*)
wenzelm@22692
   274
    val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []);
wenzelm@22692
   275
    val frees = map Free (Thm.fold_terms Term.add_frees th' []);
wenzelm@21611
   276
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   277
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   278
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   279
wenzelm@21611
   280
    (*thm definition*)
wenzelm@21644
   281
    val result = th''
wenzelm@21644
   282
      |> PureThy.name_thm true true ""
wenzelm@21644
   283
      |> Goal.close_result
wenzelm@24779
   284
      |> fold PureThy.tag_rule (ContextPosition.properties_of ctxt)
wenzelm@21644
   285
      |> PureThy.name_thm true true name;
wenzelm@21611
   286
wenzelm@21611
   287
    (*import fixes*)
wenzelm@21611
   288
    val (tvars, vars) =
wenzelm@21611
   289
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   290
      |>> map Logic.dest_type;
wenzelm@21611
   291
wenzelm@21611
   292
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   293
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   294
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   295
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   296
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   297
wenzelm@21611
   298
    (*import assumes/defines*)
wenzelm@21594
   299
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   300
    val result'' =
wenzelm@21611
   301
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   302
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   303
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   304
      |> Goal.norm_result
wenzelm@21644
   305
      |> PureThy.name_thm false false name;
wenzelm@21611
   306
wenzelm@21611
   307
  in (result'', result) end;
wenzelm@21611
   308
wenzelm@21594
   309
fun notes loc kind facts lthy =
wenzelm@21585
   310
  let
wenzelm@21594
   311
    val is_loc = loc <> "";
wenzelm@21594
   312
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   313
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   314
wenzelm@21594
   315
    val facts' = facts
wenzelm@21615
   316
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   317
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   318
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   319
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   320
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   321
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   322
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   323
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   324
  in
wenzelm@21611
   325
    lthy |> LocalTheory.theory
wenzelm@21611
   326
      (Sign.qualified_names
wenzelm@21611
   327
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   328
        #> Sign.restore_naming thy)
wenzelm@21585
   329
wenzelm@21611
   330
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   331
wenzelm@21433
   332
    |> ProofContext.set_stmt true
wenzelm@20894
   333
    |> ProofContext.qualified_names
wenzelm@21611
   334
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   335
    ||> ProofContext.restore_naming lthy
wenzelm@24389
   336
    ||> ProofContext.restore_mode lthy
wenzelm@20894
   337
  end;
wenzelm@20894
   338
wenzelm@20894
   339
wenzelm@21533
   340
(* target declarations *)
wenzelm@20894
   341
wenzelm@22673
   342
fun target_decl add loc d lthy =
wenzelm@22673
   343
  let
wenzelm@22673
   344
    val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
wenzelm@22673
   345
    val d0 = Morphism.form d';
wenzelm@22673
   346
  in
wenzelm@22673
   347
    if loc = "" then
wenzelm@22673
   348
      lthy
wenzelm@22673
   349
      |> LocalTheory.theory (Context.theory_map d0)
wenzelm@22673
   350
      |> LocalTheory.target (Context.proof_map d0)
wenzelm@22673
   351
    else
wenzelm@22673
   352
      lthy
wenzelm@22673
   353
      |> LocalTheory.target (add loc d')
wenzelm@22673
   354
  end;
wenzelm@20894
   355
wenzelm@21611
   356
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   357
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   358
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   359
wenzelm@21533
   360
fun target_morphism loc lthy =
wenzelm@21594
   361
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   362
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   363
wenzelm@22203
   364
fun target_naming loc lthy =
wenzelm@21668
   365
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   366
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@22203
   367
  |> NameSpace.qualified_names;
wenzelm@21533
   368
wenzelm@20894
   369
wenzelm@20894
   370
(* init and exit *)
wenzelm@20894
   371
haftmann@22353
   372
fun begin loc ctxt =
haftmann@22353
   373
  let
haftmann@22353
   374
    val thy = ProofContext.theory_of ctxt;
haftmann@22353
   375
    val is_loc = loc <> "";
haftmann@24218
   376
    val some_class = Class.class_of_locale thy loc;
haftmann@24748
   377
    fun class_init (SOME class) =
haftmann@24748
   378
          Class.init [class]
haftmann@24748
   379
      | class_init NONE =
haftmann@24748
   380
          I;
haftmann@22353
   381
  in
haftmann@22353
   382
    ctxt
haftmann@22353
   383
    |> Data.put (if is_loc then SOME loc else NONE)
haftmann@24748
   384
    |> class_init some_class
haftmann@24748
   385
    |> LocalTheory.init (NameSpace.base loc)
wenzelm@21747
   386
     {pretty = pretty loc,
haftmann@22353
   387
      consts = consts is_loc some_class,
wenzelm@21747
   388
      axioms = axioms,
haftmann@22353
   389
      abbrev = abbrev is_loc some_class,
haftmann@24303
   390
      defs = defs,
wenzelm@21747
   391
      notes = notes loc,
wenzelm@21747
   392
      type_syntax = type_syntax loc,
wenzelm@21747
   393
      term_syntax = term_syntax loc,
wenzelm@21747
   394
      declaration = declaration loc,
wenzelm@21747
   395
      target_morphism = target_morphism loc,
wenzelm@22203
   396
      target_naming = target_naming loc,
wenzelm@21804
   397
      reinit = fn _ =>
haftmann@24657
   398
        (if is_loc then Locale.init loc else ProofContext.init)
haftmann@24657
   399
        #> begin loc,
haftmann@24748
   400
      exit = LocalTheory.target_of }
wenzelm@21747
   401
  end;
wenzelm@20894
   402
haftmann@22353
   403
fun init_i NONE thy = begin "" (ProofContext.init thy)
haftmann@22353
   404
  | init_i (SOME loc) thy = begin loc (Locale.init loc thy);
wenzelm@20894
   405
wenzelm@21276
   406
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   407
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   408
wenzelm@20894
   409
end;