src/Pure/Isar/named_target.ML
author haftmann
Wed, 11 Aug 2010 16:02:03 +0200
changeset 38605 67d71449e85b
parent 38604 0b6fa86110e7
child 38607 7d1e2a6831ec
permissions -rw-r--r--
more convenient split of class modules: class and class_declaration
     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 = Target {target = "", is_locale = false, is_class = false};
    26 
    27 fun named_target _ NONE = global_target
    28   | named_target thy (SOME locale) =
    29       if Locale.defined thy locale
    30       then Target {target = locale, is_locale = true, is_class = Class.is_class thy locale}
    31       else error ("No such locale: " ^ quote locale);
    32 
    33 structure Data = Proof_Data
    34 (
    35   type T = target;
    36   fun init _ = global_target;
    37 );
    38 
    39 val peek = (fn Target args => args) o Data.get;
    40 
    41 
    42 (* generic declarations *)
    43 
    44 fun locale_declaration locale { syntax, pervasive } decl lthy =
    45   let
    46     val add = if syntax then Locale.add_syntax_declaration else Locale.add_declaration;
    47     val locale_decl = Morphism.transform (Local_Theory.target_morphism lthy) decl;
    48   in
    49     lthy
    50     |> pervasive ? Generic_Target.theory_declaration decl
    51     |> Local_Theory.target (add locale locale_decl)
    52   end;
    53 
    54 fun target_declaration (Target {target, ...}) { syntax, pervasive } =
    55   if target = "" then Generic_Target.theory_declaration
    56   else locale_declaration target { syntax = syntax, pervasive = pervasive };
    57 
    58 
    59 (* consts in locales *)
    60 
    61 fun locale_const (Target {target, is_class, ...}) (prmode as (mode, _)) ((b, mx), rhs) phi =
    62   let
    63     val b' = Morphism.binding phi b;
    64     val rhs' = Morphism.term phi rhs;
    65     val arg = (b', Term.close_schematic_term rhs');
    66     val same_shape = Term.aconv_untyped (rhs, rhs');
    67     (* FIXME workaround based on educated guess *)
    68     val prefix' = Binding.prefix_of b';
    69     val is_canonical_class = is_class andalso
    70       (Binding.eq_name (b, b')
    71         andalso not (null prefix')
    72         andalso List.last prefix' = (Class.class_prefix target, false)
    73       orelse same_shape);
    74   in
    75     not is_canonical_class ?
    76       (Context.mapping_result
    77         (Sign.add_abbrev Print_Mode.internal arg)
    78         (ProofContext.add_abbrev Print_Mode.internal arg)
    79       #-> (fn (lhs' as Const (d, _), _) =>
    80           same_shape ?
    81             (Context.mapping
    82               (Sign.revert_abbrev mode d) (ProofContext.revert_abbrev mode d) #>
    83              Morphism.form (ProofContext.target_notation true prmode [(lhs', mx)]))))
    84   end;
    85 
    86 fun locale_const_declaration (ta as Target {target, ...}) prmode arg =
    87   locale_declaration target { syntax = true, pervasive = false } (locale_const ta prmode arg);
    88 
    89 fun class_target (Target {target, ...}) f =
    90   Local_Theory.raw_theory f #>
    91   Local_Theory.target (Class.refresh_syntax target);
    92 
    93 
    94 (* define *)
    95 
    96 fun locale_foundation ta (((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, mx), lhs)
    99     #> pair (lhs, def))
   100 
   101 fun class_foundation (ta as Target {target, ...})
   102     (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
   103   Generic_Target.theory_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
   104   #-> (fn (lhs, def) => locale_const_declaration ta Syntax.mode_default ((b, NoSyn), lhs)
   105     #> class_target ta (Class.declare target ((b, mx), (type_params, lhs)))
   106     #> pair (lhs, def))
   107 
   108 fun target_foundation (ta as Target {target, is_locale, is_class, ...}) =
   109   if is_class then class_foundation ta
   110   else if is_locale then locale_foundation ta
   111   else Generic_Target.theory_foundation;
   112 
   113 
   114 (* notes *)
   115 
   116 fun locale_notes locale kind global_facts local_facts lthy = 
   117   let
   118     val global_facts' = Attrib.map_facts (K I) global_facts;
   119     val local_facts' = Element.facts_map
   120       (Element.morph_ctxt (Local_Theory.target_morphism lthy)) local_facts;
   121   in
   122     lthy
   123     |> Local_Theory.theory (PureThy.note_thmss kind global_facts' #> snd)
   124     |> Local_Theory.target (Locale.add_thmss locale kind local_facts')
   125   end
   126 
   127 fun target_notes (ta as Target {target, is_locale, ...}) =
   128   if is_locale then locale_notes target
   129     else fn kind => fn global_facts => fn _ => Generic_Target.theory_notes kind global_facts;
   130 
   131 
   132 (* abbrev *)
   133 
   134 fun locale_abbrev ta prmode ((b, mx), t) xs = Local_Theory.theory_result
   135   (Sign.add_abbrev Print_Mode.internal (b, t)) #->
   136     (fn (lhs, _) => locale_const_declaration ta prmode
   137       ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
   138 
   139 fun target_abbrev (ta as Target {target, is_locale, is_class, ...})
   140     prmode (b, mx) (global_rhs, t') xs lthy =
   141   if is_locale
   142     then lthy
   143       |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), global_rhs) xs
   144       |> is_class ? class_target ta (Class.abbrev target prmode ((b, mx), t'))
   145     else 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 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       reinit = init_target ta o ProofContext.theory_of,
   200       exit = Local_Theory.target_of};
   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;