src/Pure/Isar/named_target.ML
author wenzelm
Mon Sep 20 16:05:25 2010 +0200 (2010-09-20 ago)
changeset 39557 fe5722fce758
parent 38757 2b3e054ae6fc
child 39639 3dd559ab878b
permissions -rw-r--r--
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
     1 (*  Title:      Pure/Isar/theory_target.ML
     2     Author:     Makarius
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 Targets for theory, locale and class.
     6 *)
     7 
     8 signature NAMED_TARGET =
     9 sig
    10   val init: string -> theory -> local_theory
    11   val theory_init: theory -> local_theory
    12   val reinit: local_theory -> local_theory -> local_theory
    13   val context_cmd: xstring -> theory -> local_theory
    14   val peek: local_theory -> {target: string, is_locale: bool, is_class: bool} option
    15 end;
    16 
    17 structure Named_Target: NAMED_TARGET =
    18 struct
    19 
    20 (* context data *)
    21 
    22 datatype target = Target of {target: string, is_locale: bool, is_class: bool};
    23 
    24 fun make_target target is_locale is_class =
    25   Target {target = target, is_locale = is_locale, is_class = is_class};
    26 
    27 val global_target = Target {target = "", is_locale = false, is_class = false};
    28 
    29 fun named_target _ "" = global_target
    30   | named_target thy locale =
    31       if Locale.defined thy locale
    32       then Target {target = locale, is_locale = true, is_class = Class.is_class thy locale}
    33       else error ("No such locale: " ^ quote locale);
    34 
    35 structure Data = Proof_Data
    36 (
    37   type T = target option;
    38   fun init _ = NONE;
    39 );
    40 
    41 val peek = Option.map (fn Target args => args) o Data.get;
    42 
    43 
    44 (* generic declarations *)
    45 
    46 fun locale_declaration locale { syntax, pervasive } decl lthy =
    47   let
    48     val add = if syntax then Locale.add_syntax_declaration else Locale.add_declaration;
    49     val locale_decl = Morphism.transform (Local_Theory.target_morphism lthy) decl;
    50   in
    51     lthy
    52     |> pervasive ? Generic_Target.theory_declaration decl
    53     |> Local_Theory.target (add locale locale_decl)
    54   end;
    55 
    56 fun target_declaration (Target {target, ...}) { syntax, pervasive } =
    57   if target = "" then Generic_Target.theory_declaration
    58   else locale_declaration target { syntax = syntax, pervasive = pervasive };
    59 
    60 
    61 (* consts in locales *)
    62 
    63 fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) phi =
    64   let
    65     val b' = Morphism.binding phi b;
    66     val rhs' = Morphism.term phi rhs;
    67     val arg = (b', Term.close_schematic_term rhs');
    68     val same_shape = Term.aconv_untyped (rhs, rhs');
    69     (* FIXME workaround based on educated guess *)
    70     val prefix' = Binding.prefix_of b';
    71     val is_canonical_class = is_class andalso
    72       (Binding.eq_name (b, b')
    73         andalso not (null prefix')
    74         andalso List.last prefix' = (Class.class_prefix target, false)
    75       orelse same_shape);
    76   in
    77     not is_canonical_class ?
    78       (Context.mapping_result
    79         (Sign.add_abbrev Print_Mode.internal arg)
    80         (ProofContext.add_abbrev Print_Mode.internal arg)
    81       #-> (fn (lhs' as Const (d, _), _) =>
    82           same_shape ?
    83             (Context.mapping
    84               (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
    85              Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
    86   end;
    87 
    88 fun locale_const_declaration (ta as Target {target, ...}) prmode arg =
    89   locale_declaration target { syntax = true, pervasive = false } (locale_const ta prmode arg);
    90 
    91 
    92 (* define *)
    93 
    94 fun locale_foundation ta (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
    95   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
    96   #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, mx), lhs)
    97     #> pair (lhs, def))
    98 
    99 fun class_foundation (ta as Target {target, ...})
   100     (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
   101   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
   102   #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, NoSyn), lhs)
   103     #> Class.const target ((b, mx), (type_params, lhs))
   104     #> pair (lhs, def))
   105 
   106 fun target_foundation (ta as Target {target, is_locale, is_class, ...}) =
   107   if is_class then class_foundation ta
   108   else if is_locale then locale_foundation ta
   109   else Generic_Target.theory_foundation;
   110 
   111 
   112 (* notes *)
   113 
   114 fun locale_notes locale kind global_facts local_facts lthy = 
   115   let
   116     val global_facts' = Attrib.map_facts (K I) global_facts;
   117     val local_facts' = Element.facts_map
   118       (Element.morph_ctxt (Local_Theory.target_morphism lthy)) local_facts;
   119   in
   120     lthy
   121     |> Local_Theory.background_theory (Global_Theory.note_thmss kind global_facts' #> snd)
   122     |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
   123   end
   124 
   125 fun target_notes (ta as Target {target, is_locale, ...}) =
   126   if is_locale then locale_notes target
   127     else fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts;
   128 
   129 
   130 (* abbrev *)
   131 
   132 fun locale_abbrev ta prmode ((b, mx), t) xs =
   133   Local_Theory.background_theory_result
   134     (Sign.add_abbrev Print_Mode.internal (b, t)) #->
   135       (fn (lhs, _) => locale_const_declaration ta prmode
   136         ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
   137 
   138 fun target_abbrev (ta as Target {target, is_locale, is_class, ...})
   139     prmode (b, mx) (global_rhs, t') xs lthy =
   140   if is_locale then
   141     lthy
   142     |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), global_rhs) xs
   143     |> is_class ? Class.abbrev target prmode ((b, mx), t')
   144   else
   145     lthy
   146     |> Generic_Target.theory_abbrev prmode ((b, mx), global_rhs);
   147 
   148 
   149 (* pretty *)
   150 
   151 fun pretty_thy ctxt target is_class =
   152   let
   153     val thy = ProofContext.theory_of ctxt;
   154     val target_name =
   155       [Pretty.command (if is_class then "class" else "locale"),
   156         Pretty.str (" " ^ Locale.extern thy target)];
   157     val fixes = map (fn (x, T) => (Binding.name x, SOME T, NoSyn))
   158       (#1 (ProofContext.inferred_fixes ctxt));
   159     val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
   160       (Assumption.all_assms_of ctxt);
   161     val elems =
   162       (if null fixes then [] else [Element.Fixes fixes]) @
   163       (if null assumes then [] else [Element.Assumes assumes]);
   164   in
   165     if target = "" then []
   166     else if null elems then [Pretty.block target_name]
   167     else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
   168       map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))]
   169   end;
   170 
   171 fun pretty (Target {target, is_class, ...}) ctxt =
   172   Pretty.block [Pretty.command "theory", Pretty.brk 1,
   173       Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] ::
   174     pretty_thy ctxt target is_class;
   175 
   176 
   177 (* init *)
   178 
   179 local
   180 
   181 fun init_context (Target {target, is_locale, is_class}) =
   182   if not is_locale then ProofContext.init_global
   183   else if not is_class then Locale.init target
   184   else Class.init target;
   185 
   186 fun init_target (ta as Target {target, ...}) thy =
   187   thy
   188   |> init_context ta
   189   |> Data.put (SOME ta)
   190   |> Local_Theory.init NONE (Long_Name.base_name target)
   191      {define = Generic_Target.define (target_foundation ta),
   192       notes = Generic_Target.notes (target_notes ta),
   193       abbrev = Generic_Target.abbrev (target_abbrev ta),
   194       declaration = fn pervasive => target_declaration ta
   195         { syntax = false, pervasive = pervasive },
   196       syntax_declaration = fn pervasive => target_declaration ta
   197         { syntax = true, pervasive = pervasive },
   198       pretty = pretty ta,
   199       exit = Local_Theory.target_of};
   200 
   201 in
   202 
   203 fun init target thy = init_target (named_target thy target) thy;
   204 
   205 fun reinit lthy =
   206   (case peek lthy of
   207     SOME {target, ...} => init target o Local_Theory.exit_global
   208   | NONE => error "Not in a named target");
   209 
   210 val theory_init = init_target global_target;
   211 
   212 fun context_cmd "-" thy = init "" thy
   213   | context_cmd target thy = init (Locale.intern thy target) thy;
   214 
   215 end;
   216 
   217 end;