pass explicit target record -- more informative peek operation;
authorwenzelm
Fri Oct 12 20:21:58 2007 +0200 (2007-10-12)
changeset 25012448af76a1ba3
parent 25011 633afd909ff2
child 25013 bf4f7571407f
pass explicit target record -- more informative peek operation;
removed obsolete hide_abbrev;
misc tuning;
src/Pure/Isar/theory_target.ML
     1.1 --- a/src/Pure/Isar/theory_target.ML	Fri Oct 12 20:21:57 2007 +0200
     1.2 +++ b/src/Pure/Isar/theory_target.ML	Fri Oct 12 20:21:58 2007 +0200
     1.3 @@ -7,7 +7,7 @@
     1.4  
     1.5  signature THEORY_TARGET =
     1.6  sig
     1.7 -  val peek: local_theory -> string option
     1.8 +  val peek: local_theory -> {target: string, is_locale: bool, is_class: bool}
     1.9    val begin: string -> Proof.context -> local_theory
    1.10    val init: string option -> theory -> local_theory
    1.11    val init_cmd: xstring option -> theory -> local_theory
    1.12 @@ -21,22 +21,26 @@
    1.13  
    1.14  (* context data *)
    1.15  
    1.16 +datatype target = Target of {target: string, is_locale: bool, is_class: bool};
    1.17 +
    1.18 +fun make_target target is_locale is_class =
    1.19 +  Target {target = target, is_locale = is_locale, is_class = is_class};
    1.20 +
    1.21  structure Data = ProofDataFun
    1.22  (
    1.23 -  type T = string option;
    1.24 -  fun init _ = NONE;
    1.25 +  type T = target;
    1.26 +  fun init _ = make_target "" false false;
    1.27  );
    1.28  
    1.29 -val peek = Data.get;
    1.30 +val peek = (fn Target args => args) o Data.get;
    1.31  
    1.32  
    1.33  (* pretty *)
    1.34  
    1.35 -fun pretty loc ctxt =
    1.36 +fun pretty (Target {target, is_locale, is_class}) ctxt =
    1.37    let
    1.38      val thy = ProofContext.theory_of ctxt;
    1.39 -    val loc_kind = if is_some (Class.class_of_locale thy loc) then "class" else "locale";
    1.40 -    val loc_name = Locale.extern thy loc;
    1.41 +    val target_name = (if is_class then "class " else "locale ") ^ Locale.extern thy target;
    1.42  
    1.43      val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
    1.44      val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
    1.45 @@ -45,35 +49,35 @@
    1.46        (if null assumes then [] else [Element.Assumes assumes]);
    1.47    in
    1.48      Pretty.block [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy)] ::
    1.49 -     (if loc = "" then []
    1.50 -      else if null elems then [Pretty.str (loc_kind ^ " " ^ loc_name)]
    1.51 -      else [Pretty.big_list (loc_kind ^ " " ^ loc_name ^ " =")
    1.52 +     (if target = "" then []
    1.53 +      else if null elems then [Pretty.str target_name]
    1.54 +      else [Pretty.big_list (target_name ^ " =")
    1.55          (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)])
    1.56    end;
    1.57  
    1.58  
    1.59  (* target declarations *)
    1.60  
    1.61 -fun target_decl add loc d lthy =
    1.62 +fun target_decl add (Target {target, ...}) d lthy =
    1.63    let
    1.64      val d' = Morphism.transform (LocalTheory.target_morphism lthy) d;
    1.65      val d0 = Morphism.form d';
    1.66    in
    1.67 -    if loc = "" then
    1.68 +    if target = "" then
    1.69        lthy
    1.70        |> LocalTheory.theory (Context.theory_map d0)
    1.71        |> LocalTheory.target (Context.proof_map d0)
    1.72      else
    1.73        lthy
    1.74 -      |> LocalTheory.target (add loc d')
    1.75 +      |> LocalTheory.target (add target d')
    1.76    end;
    1.77  
    1.78  val type_syntax = target_decl Locale.add_type_syntax;
    1.79  val term_syntax = target_decl Locale.add_term_syntax;
    1.80  val declaration = target_decl Locale.add_declaration;
    1.81  
    1.82 -fun target_naming loc lthy =
    1.83 -  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
    1.84 +fun target_naming (Target {target, ...}) lthy =
    1.85 +  (if target = "" then Sign.naming_of (ProofContext.theory_of lthy)
    1.86     else ProofContext.naming_of (LocalTheory.target_of lthy))
    1.87    |> NameSpace.qualified_names;
    1.88  
    1.89 @@ -131,9 +135,8 @@
    1.90  
    1.91    in (result'', result) end;
    1.92  
    1.93 -fun local_notes loc kind facts lthy =
    1.94 +fun local_notes (Target {target, is_locale, ...}) kind facts lthy =
    1.95    let
    1.96 -    val is_loc = loc <> "";
    1.97      val thy = ProofContext.theory_of lthy;
    1.98      val full = LocalTheory.full_name lthy;
    1.99  
   1.100 @@ -143,16 +146,16 @@
   1.101      val local_facts = PureThy.map_facts #1 facts'
   1.102        |> Attrib.map_facts (Attrib.attribute_i thy);
   1.103      val target_facts = PureThy.map_facts #1 facts'
   1.104 -      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
   1.105 +      |> is_locale ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
   1.106      val global_facts = PureThy.map_facts #2 facts'
   1.107 -      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
   1.108 +      |> Attrib.map_facts (if is_locale then K I else Attrib.attribute_i thy);
   1.109    in
   1.110      lthy |> LocalTheory.theory
   1.111        (Sign.qualified_names
   1.112          #> PureThy.note_thmss_i kind global_facts #> snd
   1.113          #> Sign.restore_naming thy)
   1.114  
   1.115 -    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
   1.116 +    |> is_locale ? LocalTheory.target (Locale.add_thmss target kind target_facts)
   1.117  
   1.118      |> ProofContext.qualified_names
   1.119      |> ProofContext.note_thmss_i kind local_facts
   1.120 @@ -179,25 +182,22 @@
   1.121          Morphism.form (ProofContext.target_notation true prmode [(lhs, mx)])))
   1.122    end;
   1.123  
   1.124 -fun internal_abbrev loc prmode ((c, mx), t) lthy = lthy
   1.125 +fun internal_abbrev ta prmode ((c, mx), t) lthy = lthy
   1.126    (* FIXME really permissive *)
   1.127 -  |> term_syntax loc (perhaps o try o target_abbrev prmode ((c, mx), t))
   1.128 +  |> term_syntax ta (perhaps o try o target_abbrev prmode ((c, mx), t))
   1.129    |> ProofContext.add_abbrev Syntax.internalM (ContextPosition.properties_of lthy) (c, t)
   1.130    |> snd;
   1.131  
   1.132 -fun declare_consts loc depends decls lthy =
   1.133 +fun declare_consts (ta as Target {target, is_locale, is_class}) depends decls lthy =
   1.134    let
   1.135      val thy = ProofContext.theory_of lthy;
   1.136 -    val is_loc = loc <> "";
   1.137 -    val some_class = Class.class_of_locale thy loc;
   1.138 -
   1.139      val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
   1.140  
   1.141      fun const ((c, T), mx) thy =
   1.142        let
   1.143          val U = map #2 xs ---> T;
   1.144 -        val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   1.145 -        val mx3 = if is_loc then NoSyn else mx1;
   1.146 +        val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
   1.147 +        val mx3 = if is_locale then NoSyn else mx1;
   1.148          val (const, thy') = Sign.declare_const (ContextPosition.properties_of lthy) (c, U, mx3) thy;
   1.149          val t = Term.list_comb (const, map Free xs);
   1.150        in (((c, mx2), t), thy') end;
   1.151 @@ -205,21 +205,6 @@
   1.152            LocalTheory.raw_theory_result (Class.add_const_in_class class ((c, t), mx))
   1.153            #-> Class.remove_constraint class
   1.154        | const_class NONE _ _ = I;
   1.155 -    fun hide_abbrev (SOME class) abbrs thy =
   1.156 -          let
   1.157 -            val raw_cs = map (fst o fst) abbrs;
   1.158 -            val prfx = (Logic.const_of_class o NameSpace.base) class;
   1.159 -            fun mk_name c =
   1.160 -              let
   1.161 -                val n1 = Sign.full_name thy c;
   1.162 -                val n2 = NameSpace.qualifier n1;
   1.163 -                val n3 = NameSpace.base n1;
   1.164 -              in NameSpace.implode [n2, prfx, prfx, n3] end;
   1.165 -            val cs = map mk_name raw_cs;
   1.166 -          in
   1.167 -            Sign.hide_consts_i true cs thy
   1.168 -          end
   1.169 -      | hide_abbrev NONE _ thy = thy;
   1.170  
   1.171      val (abbrs, lthy') = lthy
   1.172        |> LocalTheory.theory_result (fold_map const decls)
   1.173 @@ -227,10 +212,8 @@
   1.174  
   1.175    in
   1.176      lthy'
   1.177 -    |> fold2 (const_class some_class) decls abbrs
   1.178 -    |> is_loc ? fold (internal_abbrev loc Syntax.mode_default) abbrs
   1.179 -    |> LocalTheory.raw_theory (hide_abbrev some_class abbrs)
   1.180 -        (*FIXME abbreviations should never occur*)
   1.181 +    |> fold2 (const_class (if is_class then SOME target else NONE)) decls abbrs
   1.182 +    |> is_locale ? fold (internal_abbrev ta Syntax.mode_default) abbrs
   1.183      |> LocalDefs.add_defs defs
   1.184      |>> map (apsnd snd)
   1.185    end;
   1.186 @@ -254,43 +237,41 @@
   1.187        pair (lhs, rhs)
   1.188      end);
   1.189  
   1.190 -fun abbrev loc prmode ((raw_c, mx), raw_t) lthy =
   1.191 +fun abbrev (ta as Target {target, is_locale, is_class}) prmode ((raw_c, mx), raw_t) lthy =
   1.192    let
   1.193      val thy = ProofContext.theory_of lthy;
   1.194 -    val is_loc = loc <> "";
   1.195 -    val some_class = Class.class_of_locale thy loc;
   1.196  
   1.197      val thy_ctxt = ProofContext.init thy;
   1.198 -    val target = LocalTheory.target_of lthy;
   1.199 +    val target_ctxt = LocalTheory.target_of lthy;
   1.200      val target_morphism = LocalTheory.target_morphism lthy;
   1.201  
   1.202      val c = Morphism.name target_morphism raw_c;
   1.203      val t = Morphism.term target_morphism raw_t;
   1.204 -    val xs = map Free (occ_params target [t]);
   1.205 +    val xs = map Free (occ_params target_ctxt [t]);
   1.206      val u = fold_rev Term.lambda xs t;
   1.207      val U = Term.fastype_of u;
   1.208 -    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
   1.209 -    val (mx1, mx2) = Class.fork_mixfix is_loc some_class mx;
   1.210 -    val mx3 = if is_loc then NoSyn else mx1;
   1.211 -    fun add_abbrev_in_class (SOME class) abbr =
   1.212 -          LocalTheory.raw_theory_result (Class.add_abbrev_in_class class prmode abbr)
   1.213 -          #-> Class.remove_constraint class
   1.214 -      | add_abbrev_in_class NONE _ = I;
   1.215 +    val u' = singleton (Variable.export_terms (Variable.declare_term u target_ctxt) thy_ctxt) u;
   1.216 +    val (mx1, mx2) = Class.fork_mixfix is_locale is_class mx;
   1.217 +    val mx3 = if is_locale then NoSyn else mx1;
   1.218 +    fun add_abbrev_in_class abbr =
   1.219 +      LocalTheory.raw_theory_result (Class.add_abbrev_in_class target prmode abbr)
   1.220 +      #-> Class.remove_constraint target;
   1.221    in
   1.222      lthy
   1.223      |> LocalTheory.theory_result
   1.224          (Sign.add_abbrev (#1 prmode) (ContextPosition.properties_of lthy) (c, u'))
   1.225      |-> (fn (lhs as Const (full_c, _), rhs) =>
   1.226            LocalTheory.theory (Sign.notation true prmode [(lhs, mx3)])
   1.227 -          #> is_loc ? internal_abbrev loc prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   1.228 -          #> add_abbrev_in_class some_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   1.229 +          #> is_locale ? internal_abbrev ta prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
   1.230 +          #> is_class ? add_abbrev_in_class ((c, Term.list_comb (Const (full_c, U), xs)), mx1)
   1.231            #> local_abbrev (c, rhs))
   1.232    end;
   1.233  
   1.234  
   1.235  (* defs *)
   1.236  
   1.237 -fun local_def loc kind ((c, mx), ((name, atts), rhs)) lthy =
   1.238 +fun local_def (ta as Target {target, is_locale, is_class})
   1.239 +    kind ((c, mx), ((name, atts), rhs)) lthy =
   1.240    let
   1.241      val thy = ProofContext.theory_of lthy;
   1.242      val thy_ctxt = ProofContext.init thy;
   1.243 @@ -302,7 +283,7 @@
   1.244  
   1.245      (*consts*)
   1.246      val ([(lhs, lhs_def)], lthy2) = lthy
   1.247 -      |> declare_consts loc (member (op =) xs) [((c, T), mx)];
   1.248 +      |> declare_consts ta (member (op =) xs) [((c, T), mx)];
   1.249      val (_, lhs') = Logic.dest_equals (Thm.prop_of lhs_def);
   1.250  
   1.251      (*def*)
   1.252 @@ -310,22 +291,22 @@
   1.253      val (def, lthy3) = lthy2
   1.254        |> LocalTheory.theory_result (Thm.add_def false (name', Logic.mk_equals (lhs', rhs')));
   1.255      val eq = LocalDefs.trans_terms lthy3
   1.256 -      [(*c == loc.c xs*) lhs_def,
   1.257 +      [(*c == global.c xs*) lhs_def,
   1.258         (*lhs' == rhs'*)  def,
   1.259         (*rhs' == rhs*)   Thm.symmetric rhs_conv];
   1.260  
   1.261      (*notes*)
   1.262      val ([(res_name, [res])], lthy4) = lthy3
   1.263 -      |> local_notes loc kind [((name', atts), [([eq], [])])];
   1.264 +      |> local_notes ta kind [((name', atts), [([eq], [])])];
   1.265    in ((lhs, (res_name, res)), lthy4) end;
   1.266  
   1.267  
   1.268  (* axioms *)
   1.269  
   1.270 -fun local_axioms loc kind (vars, specs) lthy =
   1.271 +fun local_axioms ta kind (vars, specs) lthy =
   1.272    let
   1.273      val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
   1.274 -    val (consts, lthy') = declare_consts loc spec_frees vars lthy;
   1.275 +    val (consts, lthy') = declare_consts ta spec_frees vars lthy;
   1.276      val heads = map (Term.head_of o Thm.term_of o Thm.rhs_of o #2) consts;
   1.277  
   1.278      val hyps = map Thm.term_of (Assumption.assms_of lthy');
   1.279 @@ -337,41 +318,43 @@
   1.280      |> LocalTheory.theory (Theory.add_finals_i false heads)
   1.281      |> fold (fold Variable.declare_term o snd) specs
   1.282      |> LocalTheory.theory_result (fold_map axiom specs)
   1.283 -    |-> local_notes loc kind
   1.284 +    |-> local_notes ta kind
   1.285      |>> pair (map #1 consts)
   1.286    end;
   1.287  
   1.288  
   1.289  (* init and exit *)
   1.290  
   1.291 -fun begin loc ctxt =
   1.292 +fun begin target ctxt =
   1.293    let
   1.294      val thy = ProofContext.theory_of ctxt;
   1.295 -    val is_loc = loc <> "";
   1.296 +    val is_locale = target <> "";
   1.297 +    val is_class = is_some (Class.class_of_locale thy target);
   1.298 +    val ta = Target {target = target, is_locale = is_locale, is_class = is_class};
   1.299    in
   1.300      ctxt
   1.301 -    |> Data.put (if is_loc then SOME loc else NONE)
   1.302 -    |> fold Class.init (the_list (Class.class_of_locale thy loc))
   1.303 -    |> LocalTheory.init (NameSpace.base loc)
   1.304 -     {pretty = pretty loc,
   1.305 -      axioms = local_axioms loc,
   1.306 -      abbrev = abbrev loc,
   1.307 -      def = local_def loc,
   1.308 -      notes = local_notes loc,
   1.309 -      type_syntax = type_syntax loc,
   1.310 -      term_syntax = term_syntax loc,
   1.311 -      declaration = declaration loc,
   1.312 -      target_naming = target_naming loc,
   1.313 +    |> Data.put ta
   1.314 +    |> is_class ? Class.init target
   1.315 +    |> LocalTheory.init (NameSpace.base target)
   1.316 +     {pretty = pretty ta,
   1.317 +      axioms = local_axioms ta,
   1.318 +      abbrev = abbrev ta,
   1.319 +      def = local_def ta,
   1.320 +      notes = local_notes ta,
   1.321 +      type_syntax = type_syntax ta,
   1.322 +      term_syntax = term_syntax ta,
   1.323 +      declaration = declaration ta,
   1.324 +      target_naming = target_naming ta,
   1.325        reinit = fn _ =>
   1.326 -        (if is_loc then Locale.init loc else ProofContext.init)
   1.327 -        #> begin loc,
   1.328 +        (if is_locale then Locale.init target else ProofContext.init)
   1.329 +        #> begin target,
   1.330        exit = LocalTheory.target_of}
   1.331    end;
   1.332  
   1.333  fun init NONE thy = begin "" (ProofContext.init thy)
   1.334 -  | init (SOME loc) thy = begin loc (Locale.init loc thy);
   1.335 +  | init (SOME target) thy = begin target (Locale.init target thy);
   1.336  
   1.337  fun init_cmd (SOME "-") thy = init NONE thy
   1.338 -  | init_cmd loc thy = init (Option.map (Locale.intern thy) loc) thy;
   1.339 +  | init_cmd target thy = init (Option.map (Locale.intern thy) target) thy;
   1.340  
   1.341  end;