tuned;
authorwenzelm
Tue Oct 09 19:48:55 2007 +0200 (2007-10-09)
changeset 249396dd60d1191bf
parent 24938 a220317465b4
child 24940 8f9dea697b8d
tuned;
src/Pure/Isar/theory_target.ML
     1.1 --- a/src/Pure/Isar/theory_target.ML	Tue Oct 09 19:48:54 2007 +0200
     1.2 +++ b/src/Pure/Isar/theory_target.ML	Tue Oct 09 19:48:55 2007 +0200
     1.3 @@ -34,20 +34,20 @@
     1.4  fun pretty loc ctxt =
     1.5    let
     1.6      val thy = ProofContext.theory_of ctxt;
     1.7 +    val loc_kind = if is_some (Class.class_of_locale thy loc) then "class" else "locale";
     1.8 +    val loc_name = Locale.extern thy loc;
     1.9 +
    1.10      val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    1.11      val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
    1.12      val elems =
    1.13        (if null fixes then [] else [Element.Fixes fixes]) @
    1.14        (if null assumes then [] else [Element.Assumes assumes]);
    1.15    in
    1.16 -    if loc = "" then
    1.17 -      [Pretty.block
    1.18 -        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
    1.19 -          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
    1.20 -    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
    1.21 -    else
    1.22 -      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
    1.23 -        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
    1.24 +    Pretty.block [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy)] ::
    1.25 +     (if loc = "" then []
    1.26 +      else if null elems then [Pretty.str (loc_kind ^ " " ^ loc_name)]
    1.27 +      else [Pretty.big_list (loc_kind ^ " " ^ loc_name ^ " =")
    1.28 +        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
    1.29    end;
    1.30  
    1.31  
    1.32 @@ -77,211 +77,6 @@
    1.33    |> NameSpace.qualified_names;
    1.34  
    1.35  
    1.36 -(* consts *)
    1.37 -
    1.38 -fun target_abbrev prmode ((c, mx), rhs) phi =
    1.39 -  let
    1.40 -    val c' = Morphism.name phi c;
    1.41 -    val rhs' = Morphism.term phi rhs;
    1.42 -    val arg' = (c', rhs');
    1.43 -    val eq_heads =
    1.44 -      (case pairself Term.head_of (rhs, rhs') of
    1.45 -        (Const (a, _), Const (a', _)) => a = a'
    1.46 -      | _ => false);
    1.47 -  in
    1.48 -    eq_heads ? (Context.mapping_result
    1.49 -      (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
    1.50 -    #-> (fn (lhs, _) =>
    1.51 -      Term.equiv_types (rhs, rhs') ?
    1.52 -        Morphism.form (ProofContext.target_notation prmode [(lhs, mx)])))
    1.53 -  end;
    1.54 -
    1.55 -fun internal_abbrev prmode ((c, mx), t) lthy = lthy
    1.56 -  (* FIXME really permissive *)
    1.57 -  |> LocalTheory.term_syntax (perhaps o try o target_abbrev prmode ((c, mx), t))
    1.58 -  |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
    1.59 -  |> snd;
    1.60 -
    1.61 -fun consts is_loc some_class depends decls lthy =
    1.62 -  let
    1.63 -    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
    1.64 -
    1.65 -    fun const ((c, T), mx) thy =
    1.66 -      let
    1.67 -        val U = map #2 xs ---> T;
    1.68 -        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
    1.69 -        val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
    1.70 -        val mx3 = if is_loc then NoSyn else mx1;
    1.71 -        val thy' = Sign.add_consts_authentic (ContextPosition.properties_of lthy) [(c, U, mx3)] thy;
    1.72 -      in (((c, mx2), t), thy') end;
    1.73 -    fun const_class (SOME class) ((c, _), mx) (_, t) =
    1.74 -          LocalTheory.raw_theory_result
    1.75 -            (Class.add_const_in_class class ((c, t), mx))
    1.76 -          #-> (fn c => Class.remove_constraint [class] c)
    1.77 -      | const_class NONE _ _ = I;
    1.78 -    fun hide_abbrev (SOME class) abbrs thy =
    1.79 -          let
    1.80 -            val raw_cs = map (fst o fst) abbrs;
    1.81 -            val prfx = (Logic.const_of_class o NameSpace.base) class;
    1.82 -            fun mk_name c =
    1.83 -              let
    1.84 -                val n1 = Sign.full_name thy c;
    1.85 -                val n2 = NameSpace.qualifier n1;
    1.86 -                val n3 = NameSpace.base n1;
    1.87 -              in NameSpace.implode [n2, prfx, prfx, n3] end;
    1.88 -            val cs = map mk_name raw_cs;
    1.89 -          in
    1.90 -            Sign.hide_consts_i true cs thy
    1.91 -          end
    1.92 -      | hide_abbrev NONE _ thy = thy;
    1.93 -
    1.94 -    val (abbrs, lthy') = lthy
    1.95 -      |> LocalTheory.theory_result (fold_map const decls)
    1.96 -    val defs = map (apsnd (pair ("", []))) abbrs;
    1.97 -
    1.98 -  in
    1.99 -    lthy'
   1.100 -    |> fold2 (const_class some_class) decls abbrs
   1.101 -    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
   1.102 -    |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
   1.103 -        (*FIXME abbreviations should never occur*)
   1.104 -    |> LocalDefs.add_defs defs
   1.105 -    |>> map (apsnd snd)
   1.106 -  end;
   1.107 -
   1.108 -
   1.109 -(* abbrev *)
   1.110 -
   1.111 -fun occ_params ctxt ts =
   1.112 -  #1 (ProofContext.inferred_fixes ctxt)
   1.113 -  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
   1.114 -
   1.115 -fun local_abbrev (x, rhs) =
   1.116 -  Variable.add_fixes [x] #-> (fn [x'] =>
   1.117 -    let
   1.118 -      val T = Term.fastype_of rhs;
   1.119 -      val lhs = Free (x', T);
   1.120 -    in
   1.121 -      Variable.declare_term lhs #>
   1.122 -      Variable.declare_term rhs #>
   1.123 -      Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
   1.124 -      pair (lhs, rhs)
   1.125 -    end);
   1.126 -
   1.127 -fun abbrev is_loc some_class prmode ((raw_c, mx), raw_t) lthy =
   1.128 -  let
   1.129 -    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
   1.130 -    val target = LocalTheory.target_of lthy;
   1.131 -    val target_morphism = LocalTheory.target_morphism lthy;
   1.132 -
   1.133 -    val c = Morphism.name target_morphism raw_c;
   1.134 -    val t = Morphism.term target_morphism raw_t;
   1.135 -    val xs = map Free (occ_params target [t]);
   1.136 -    val u = fold_rev Term.lambda xs t;
   1.137 -    val U = Term.fastype_of u;
   1.138 -    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
   1.139 -    val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   1.140 -    val mx3 = if is_loc then NoSyn else mx1;
   1.141 -    fun add_abbrev_in_class (SOME class) abbr =
   1.142 -          LocalTheory.raw_theory_result (Class.add_abbrev_in_class class prmode abbr)
   1.143 -          #-> (fn c => Class.remove_constraint [class] c)
   1.144 -      | add_abbrev_in_class NONE _ = I;
   1.145 -  in
   1.146 -    lthy
   1.147 -    |> LocalTheory.theory_result
   1.148 -        (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
   1.149 -    |-> (fn (lhs as Const (full_c, _), rhs) => LocalTheory.theory (Sign.add_notation prmode [(lhs, mx3)])
   1.150 -    #> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   1.151 -    #> add_abbrev_in_class some_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   1.152 -    #> local_abbrev (c, rhs))
   1.153 -  end;
   1.154 -
   1.155 -
   1.156 -(* defs *)
   1.157 -
   1.158 -local
   1.159 -
   1.160 -fun expand_term ctxt t =
   1.161 -  let
   1.162 -    val thy = ProofContext.theory_of ctxt;
   1.163 -    val thy_ctxt = ProofContext.init thy;
   1.164 -    val ct = Thm.cterm_of thy t;
   1.165 -    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
   1.166 -  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
   1.167 -
   1.168 -fun add_def (name, prop) thy =
   1.169 -  let
   1.170 -    val tfrees = rev (map TFree (Term.add_tfrees prop []));
   1.171 -    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
   1.172 -    val strip_sorts = tfrees ~~ tfrees';
   1.173 -    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
   1.174 -
   1.175 -    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
   1.176 -    val thy' = Theory.add_defs_i false false [(name, prop')] thy;
   1.177 -    val axm' = Thm.get_axiom_i thy' (Sign.full_name thy' name);
   1.178 -    val def = Thm.instantiate (recover_sorts, []) axm';
   1.179 -  in (Drule.unvarify def, thy') end;
   1.180 -
   1.181 -in
   1.182 -
   1.183 -fun local_def kind ((c, mx), ((name, atts), rhs)) lthy1 =
   1.184 -  let
   1.185 -    val (rhs', rhs_conv) = expand_term lthy1 rhs;
   1.186 -    val xs = Variable.add_fixed (LocalTheory.target_of lthy1) rhs' [];
   1.187 -    val T = Term.fastype_of rhs;
   1.188 -
   1.189 -    (*consts*)
   1.190 -    val ([(lhs, lhs_def)], lthy2) = lthy1
   1.191 -      |> LocalTheory.consts (member (op =) xs) [((c, T), mx)];
   1.192 -    val (_, lhs') = Logic.dest_equals (Thm.prop_of lhs_def);
   1.193 -
   1.194 -    (*def*)
   1.195 -    val name' = Thm.def_name_optional c name;
   1.196 -    val (def, lthy3) = lthy2
   1.197 -      |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
   1.198 -    val eq = LocalDefs.trans_terms lthy3
   1.199 -      [(*c == loc.c xs*) lhs_def,
   1.200 -       (*lhs' == rhs'*)  def,
   1.201 -       (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   1.202 -
   1.203 -    (*notes*)
   1.204 -    val ([(res_name, [res])], lthy4) = lthy3
   1.205 -      |> LocalTheory.notes kind [((name', atts), [([eq], [])])];
   1.206 -  in ((lhs, (res_name, res)), lthy4) end;
   1.207 -
   1.208 -end;
   1.209 -
   1.210 -
   1.211 -(* axioms *)
   1.212 -
   1.213 -local
   1.214 -
   1.215 -fun add_axiom hyps (name, prop) thy =
   1.216 -  let
   1.217 -    val name' = if name = "" then "axiom_" ^ serial_string () else name;
   1.218 -    val prop' = Logic.list_implies (hyps, prop);
   1.219 -    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
   1.220 -    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
   1.221 -    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
   1.222 -  in (Drule.implies_elim_list axm prems, thy') end;
   1.223 -
   1.224 -in
   1.225 -
   1.226 -fun axioms kind specs lthy =
   1.227 -  let
   1.228 -    val hyps = map Thm.term_of (Assumption.assms_of lthy);
   1.229 -    fun axiom ((name, atts), props) thy = thy
   1.230 -      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
   1.231 -      |-> (fn ths => pair ((name, atts), [(ths, [])]));
   1.232 -  in
   1.233 -    lthy
   1.234 -    |> fold (fold Variable.declare_term o snd) specs
   1.235 -    |> LocalTheory.theory_result (fold_map axiom specs)
   1.236 -    |-> LocalTheory.notes kind
   1.237 -  end;
   1.238 -
   1.239 -end;
   1.240 -
   1.241  
   1.242  (* notes *)
   1.243  
   1.244 @@ -335,7 +130,7 @@
   1.245  
   1.246    in (result'', result) end;
   1.247  
   1.248 -fun notes loc kind facts lthy =
   1.249 +fun local_notes loc kind facts lthy =
   1.250    let
   1.251      val is_loc = loc <> "";
   1.252      val thy = ProofContext.theory_of lthy;
   1.253 @@ -364,24 +159,236 @@
   1.254    end;
   1.255  
   1.256  
   1.257 +(* consts *)
   1.258 +
   1.259 +fun target_abbrev prmode ((c, mx), rhs) phi =
   1.260 +  let
   1.261 +    val c' = Morphism.name phi c;
   1.262 +    val rhs' = Morphism.term phi rhs;
   1.263 +    val arg' = (c', rhs');
   1.264 +    val eq_heads =
   1.265 +      (case pairself Term.head_of (rhs, rhs') of
   1.266 +        (Const (a, _), Const (a', _)) => a = a'
   1.267 +      | _ => false);
   1.268 +  in
   1.269 +    eq_heads ? (Context.mapping_result
   1.270 +      (Sign.add_abbrev Syntax.internalM [] arg') (ProofContext.add_abbrev Syntax.internalM [] arg')
   1.271 +    #-> (fn (lhs, _) =>
   1.272 +      Term.equiv_types (rhs, rhs') ?
   1.273 +        Morphism.form (ProofContext.target_notation prmode [(lhs, mx)])))
   1.274 +  end;
   1.275 +
   1.276 +fun internal_abbrev loc prmode ((c, mx), t) lthy = lthy
   1.277 +  (* FIXME really permissive *)
   1.278 +  |> term_syntax loc (perhaps o try o target_abbrev prmode ((c, mx), t))
   1.279 +  |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
   1.280 +  |> snd;
   1.281 +
   1.282 +fun declare_consts loc depends decls lthy =
   1.283 +  let
   1.284 +    val thy = ProofContext.theory_of lthy;
   1.285 +    val is_loc = loc <> "";
   1.286 +    val some_class = Class.class_of_locale thy loc;
   1.287 +
   1.288 +    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   1.289 +
   1.290 +    fun const ((c, T), mx) thy =
   1.291 +      let
   1.292 +        val U = map #2 xs ---> T;
   1.293 +        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
   1.294 +        val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   1.295 +        val mx3 = if is_loc then NoSyn else mx1;
   1.296 +        val thy' = Sign.add_consts_authentic (ContextPosition.properties_of lthy) [(c, U, mx3)] thy;
   1.297 +      in (((c, mx2), t), thy') end;
   1.298 +    fun const_class (SOME class) ((c, _), mx) (_, t) =
   1.299 +          LocalTheory.raw_theory_result (Class.add_const_in_class class ((c, t), mx))
   1.300 +          #-> Class.remove_constraint [class]
   1.301 +      | const_class NONE _ _ = I;
   1.302 +    fun hide_abbrev (SOME class) abbrs thy =
   1.303 +          let
   1.304 +            val raw_cs = map (fst o fst) abbrs;
   1.305 +            val prfx = (Logic.const_of_class o NameSpace.base) class;
   1.306 +            fun mk_name c =
   1.307 +              let
   1.308 +                val n1 = Sign.full_name thy c;
   1.309 +                val n2 = NameSpace.qualifier n1;
   1.310 +                val n3 = NameSpace.base n1;
   1.311 +              in NameSpace.implode [n2, prfx, prfx, n3] end;
   1.312 +            val cs = map mk_name raw_cs;
   1.313 +          in
   1.314 +            Sign.hide_consts_i true cs thy
   1.315 +          end
   1.316 +      | hide_abbrev NONE _ thy = thy;
   1.317 +
   1.318 +    val (abbrs, lthy') = lthy
   1.319 +      |> LocalTheory.theory_result (fold_map const decls)
   1.320 +    val defs = map (apsnd (pair ("", []))) abbrs;
   1.321 +
   1.322 +  in
   1.323 +    lthy'
   1.324 +    |> fold2 (const_class some_class) decls abbrs
   1.325 +    |> is_loc ? fold (internal_abbrev loc Syntax.default_mode) abbrs
   1.326 +    |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
   1.327 +        (*FIXME abbreviations should never occur*)
   1.328 +    |> LocalDefs.add_defs defs
   1.329 +    |>> map (apsnd snd)
   1.330 +  end;
   1.331 +
   1.332 +
   1.333 +(* abbrev *)
   1.334 +
   1.335 +fun occ_params ctxt ts =
   1.336 +  #1 (ProofContext.inferred_fixes ctxt)
   1.337 +  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
   1.338 +
   1.339 +fun local_abbrev (x, rhs) =
   1.340 +  Variable.add_fixes [x] #-> (fn [x'] =>
   1.341 +    let
   1.342 +      val T = Term.fastype_of rhs;
   1.343 +      val lhs = Free (x', T);
   1.344 +    in
   1.345 +      Variable.declare_term lhs #>
   1.346 +      Variable.declare_term rhs #>
   1.347 +      Assumption.add_assms (K (K (I, Envir.expand_term_frees [((x', T), rhs)]))) [] #> snd #>
   1.348 +      pair (lhs, rhs)
   1.349 +    end);
   1.350 +
   1.351 +fun abbrev loc prmode ((raw_c, mx), raw_t) lthy =
   1.352 +  let
   1.353 +    val thy = ProofContext.theory_of lthy;
   1.354 +    val is_loc = loc <> "";
   1.355 +    val some_class = Class.class_of_locale thy loc;
   1.356 +
   1.357 +    val thy_ctxt = ProofContext.init thy;
   1.358 +    val target = LocalTheory.target_of lthy;
   1.359 +    val target_morphism = LocalTheory.target_morphism lthy;
   1.360 +
   1.361 +    val c = Morphism.name target_morphism raw_c;
   1.362 +    val t = Morphism.term target_morphism raw_t;
   1.363 +    val xs = map Free (occ_params target [t]);
   1.364 +    val u = fold_rev Term.lambda xs t;
   1.365 +    val U = Term.fastype_of u;
   1.366 +    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
   1.367 +    val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   1.368 +    val mx3 = if is_loc then NoSyn else mx1;
   1.369 +    fun add_abbrev_in_class (SOME class) abbr =
   1.370 +          LocalTheory.raw_theory_result (Class.add_abbrev_in_class class prmode abbr)
   1.371 +          #-> (fn c => Class.remove_constraint [class] c)
   1.372 +      | add_abbrev_in_class NONE _ = I;
   1.373 +  in
   1.374 +    lthy
   1.375 +    |> LocalTheory.theory_result
   1.376 +        (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
   1.377 +    |-> (fn (lhs as Const (full_c, _), rhs) => LocalTheory.theory (Sign.add_notation prmode [(lhs, mx3)])
   1.378 +    #> is_loc ? internal_abbrev loc prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   1.379 +    #> add_abbrev_in_class some_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   1.380 +    #> local_abbrev (c, rhs))
   1.381 +  end;
   1.382 +
   1.383 +
   1.384 +(* defs *)
   1.385 +
   1.386 +local
   1.387 +
   1.388 +fun expand_term ctxt t =
   1.389 +  let
   1.390 +    val thy = ProofContext.theory_of ctxt;
   1.391 +    val thy_ctxt = ProofContext.init thy;
   1.392 +    val ct = Thm.cterm_of thy t;
   1.393 +    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
   1.394 +  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
   1.395 +
   1.396 +fun add_def (name, prop) thy =
   1.397 +  let
   1.398 +    val tfrees = rev (map TFree (Term.add_tfrees prop []));
   1.399 +    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
   1.400 +    val strip_sorts = tfrees ~~ tfrees';
   1.401 +    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
   1.402 +
   1.403 +    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
   1.404 +    val thy' = Theory.add_defs_i false false [(name, prop')] thy;
   1.405 +    val axm' = Thm.get_axiom_i thy' (Sign.full_name thy' name);
   1.406 +    val def = Thm.instantiate (recover_sorts, []) axm';
   1.407 +  in (Drule.unvarify def, thy') end;
   1.408 +
   1.409 +in
   1.410 +
   1.411 +fun local_def loc kind ((c, mx), ((name, atts), rhs)) lthy1 =
   1.412 +  let
   1.413 +    val (rhs', rhs_conv) = expand_term lthy1 rhs;
   1.414 +    val xs = Variable.add_fixed (LocalTheory.target_of lthy1) rhs' [];
   1.415 +    val T = Term.fastype_of rhs;
   1.416 +
   1.417 +    (*consts*)
   1.418 +    val ([(lhs, lhs_def)], lthy2) = lthy1
   1.419 +      |> declare_consts loc (member (op =) xs) [((c, T), mx)];
   1.420 +    val (_, lhs') = Logic.dest_equals (Thm.prop_of lhs_def);
   1.421 +
   1.422 +    (*def*)
   1.423 +    val name' = Thm.def_name_optional c name;
   1.424 +    val (def, lthy3) = lthy2
   1.425 +      |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
   1.426 +    val eq = LocalDefs.trans_terms lthy3
   1.427 +      [(*c == loc.c xs*) lhs_def,
   1.428 +       (*lhs' == rhs'*)  def,
   1.429 +       (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   1.430 +
   1.431 +    (*notes*)
   1.432 +    val ([(res_name, [res])], lthy4) = lthy3
   1.433 +      |> local_notes loc kind [((name', atts), [([eq], [])])];
   1.434 +  in ((lhs, (res_name, res)), lthy4) end;
   1.435 +
   1.436 +end;
   1.437 +
   1.438 +
   1.439 +(* axioms *)
   1.440 +
   1.441 +local
   1.442 +
   1.443 +fun add_axiom hyps (name, prop) thy =
   1.444 +  let
   1.445 +    val name' = if name = "" then "axiom_" ^ serial_string () else name;
   1.446 +    val prop' = Logic.list_implies (hyps, prop);
   1.447 +    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
   1.448 +    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
   1.449 +    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
   1.450 +  in (Drule.implies_elim_list axm prems, thy') end;
   1.451 +
   1.452 +in
   1.453 +
   1.454 +fun axioms loc kind specs lthy =
   1.455 +  let
   1.456 +    val hyps = map Thm.term_of (Assumption.assms_of lthy);
   1.457 +    fun axiom ((name, atts), props) thy = thy
   1.458 +      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
   1.459 +      |-> (fn ths => pair ((name, atts), [(ths, [])]));
   1.460 +  in
   1.461 +    lthy
   1.462 +    |> fold (fold Variable.declare_term o snd) specs
   1.463 +    |> LocalTheory.theory_result (fold_map axiom specs)
   1.464 +    |-> local_notes loc kind
   1.465 +  end;
   1.466 +
   1.467 +end;
   1.468 +
   1.469 +
   1.470  (* init and exit *)
   1.471  
   1.472  fun begin loc ctxt =
   1.473    let
   1.474      val thy = ProofContext.theory_of ctxt;
   1.475      val is_loc = loc <> "";
   1.476 -    val some_class = Class.class_of_locale thy loc;
   1.477    in
   1.478      ctxt
   1.479      |> Data.put (if is_loc then SOME loc else NONE)
   1.480 -    |> fold Class.init (the_list some_class)
   1.481 +    |> fold Class.init (the_list (Class.class_of_locale thy loc))
   1.482      |> LocalTheory.init (NameSpace.base loc)
   1.483       {pretty = pretty loc,
   1.484 -      consts = consts is_loc some_class,
   1.485 -      axioms = axioms,
   1.486 -      abbrev = abbrev is_loc some_class,
   1.487 -      def = local_def,
   1.488 -      notes = notes loc,
   1.489 +      consts = declare_consts loc,
   1.490 +      axioms = axioms loc,
   1.491 +      abbrev = abbrev loc,
   1.492 +      def = local_def loc,
   1.493 +      notes = local_notes loc,
   1.494        type_syntax = type_syntax loc,
   1.495        term_syntax = term_syntax loc,
   1.496        declaration = declaration loc,