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