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