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