src/Pure/Isar/named_target.ML
author wenzelm
Tue, 03 Apr 2012 10:59:20 +0200
changeset 48170 392c4cd97e5c
parent 48168 0e65b6a016dc
child 48172 b79bf8288b29
permissions -rw-r--r--
more uniform theory_abbrev with const_declaration;
     1 (*  Title:      Pure/Isar/named_target.ML
     2     Author:     Makarius
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 Targets for theory, locale, class -- at the bottom the nested structure.
     6 *)
     7 
     8 signature NAMED_TARGET =
     9 sig
    10   val peek: local_theory -> {target: string, is_locale: bool, is_class: bool} option
    11   val init: (local_theory -> local_theory) -> string -> theory -> local_theory
    12   val theory_init: theory -> local_theory
    13   val reinit: local_theory -> local_theory -> local_theory
    14   val context_cmd: xstring * Position.T -> theory -> local_theory
    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 (* consts in locale *)
    48 
    49 fun locale_const (Target {target, is_class, ...}) prmode ((b, mx), rhs) =
    50   Generic_Target.locale_declaration target true (fn phi =>
    51     let
    52       val b' = Morphism.binding phi b;
    53       val rhs' = Morphism.term phi rhs;
    54       val same_shape = Term.aconv_untyped (rhs, rhs');
    55 
    56       (* FIXME workaround based on educated guess *)
    57       val prefix' = Binding.prefix_of b';
    58       val is_canonical_class = is_class andalso
    59         (Binding.eq_name (b, b')
    60           andalso not (null prefix')
    61           andalso List.last prefix' = (Class.class_prefix target, false)
    62         orelse same_shape);
    63     in
    64       not is_canonical_class ?
    65         Generic_Target.generic_const same_shape prmode ((b', mx), Term.close_schematic_term rhs')
    66     end) #>
    67   (fn lthy => lthy |> Generic_Target.const_declaration
    68     (fn level => level <> 0 andalso level <> Local_Theory.level lthy) prmode ((b, mx), rhs));
    69 
    70 
    71 (* define *)
    72 
    73 fun locale_foundation ta (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
    74   Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
    75   #-> (fn (lhs, def) => locale_const ta Syntax.mode_default ((b, mx), lhs)
    76     #> pair (lhs, def));
    77 
    78 fun class_foundation (ta as Target {target, ...})
    79     (((b, U), mx), (b_def, rhs)) (type_params, term_params) =
    80   Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) (type_params, term_params)
    81   #-> (fn (lhs, def) => locale_const ta Syntax.mode_default ((b, NoSyn), lhs)
    82     #> Class.const target ((b, mx), (type_params, lhs))
    83     #> pair (lhs, def));
    84 
    85 fun target_foundation (ta as Target {is_locale, is_class, ...}) =
    86   if is_class then class_foundation ta
    87   else if is_locale then locale_foundation ta
    88   else Generic_Target.theory_foundation;
    89 
    90 
    91 (* notes *)
    92 
    93 fun target_notes (Target {target, is_locale, ...}) =
    94   if is_locale then Generic_Target.locale_notes target
    95   else Generic_Target.theory_notes;
    96 
    97 
    98 (* abbrev *)
    99 
   100 fun locale_abbrev ta prmode ((b, mx), t) xs =
   101   Local_Theory.background_theory_result
   102     (Sign.add_abbrev Print_Mode.internal (b, t)) #->
   103       (fn (lhs, _) =>
   104         locale_const ta prmode ((b, mx), Term.list_comb (Logic.unvarify_global lhs, xs)));
   105 
   106 fun target_abbrev (ta as Target {target, is_locale, is_class, ...}) prmode (b, mx) (t, t') xs lthy =
   107   if is_locale then
   108     lthy
   109     |> locale_abbrev ta prmode ((b, if is_class then NoSyn else mx), t) xs
   110     |> is_class ? Class.abbrev target prmode ((b, mx), t')
   111   else lthy |> Generic_Target.theory_abbrev prmode (b, mx) (t, t') xs;
   112 
   113 
   114 (* declaration *)
   115 
   116 fun target_declaration (Target {target, ...}) {syntax, pervasive} decl lthy =
   117   if target = "" then Generic_Target.theory_declaration decl lthy
   118   else
   119     lthy
   120     |> pervasive ? Generic_Target.background_declaration decl
   121     |> Generic_Target.locale_declaration target syntax decl
   122     |> (fn lthy' => lthy' |> Generic_Target.standard_declaration (fn level => level <> 0) decl);
   123 
   124 
   125 (* pretty *)
   126 
   127 fun pretty (Target {target, is_locale, is_class, ...}) ctxt =
   128   let
   129     val thy = Proof_Context.theory_of ctxt;
   130     val target_name =
   131       [Pretty.command (if is_class then "class" else "locale"),
   132         Pretty.str (" " ^ Locale.extern thy target)];
   133     val fixes =
   134       map (fn (x, T) => (Binding.name x, SOME T, NoSyn))
   135         (#1 (Proof_Context.inferred_fixes ctxt));
   136     val assumes =
   137       map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
   138         (Assumption.all_assms_of ctxt);
   139     val elems =
   140       (if null fixes then [] else [Element.Fixes fixes]) @
   141       (if null assumes then [] else [Element.Assumes assumes]);
   142     val body_elems =
   143       if not is_locale then []
   144       else if null elems then [Pretty.block target_name]
   145       else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
   146         map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))];
   147   in
   148     Pretty.block [Pretty.command "theory", Pretty.brk 1,
   149       Pretty.str (Context.theory_name (Proof_Context.theory_of ctxt))] :: body_elems
   150   end;
   151 
   152 
   153 (* init *)
   154 
   155 fun init_context (Target {target, is_locale, is_class, ...}) =
   156   if not is_locale then Proof_Context.init_global
   157   else if not is_class then Locale.init target
   158   else Class.init target;
   159 
   160 fun init before_exit target thy =
   161   let
   162     val ta = named_target thy target before_exit;
   163     val naming = Sign.naming_of thy
   164       |> Name_Space.mandatory_path (Long_Name.base_name target);
   165   in
   166     thy
   167     |> init_context ta
   168     |> Data.put (SOME ta)
   169     |> Local_Theory.init naming
   170        {define = Generic_Target.define (target_foundation ta),
   171         notes = Generic_Target.notes (target_notes ta),
   172         abbrev = Generic_Target.abbrev (target_abbrev ta),
   173         declaration = target_declaration ta,
   174         pretty = pretty ta,
   175         exit = Local_Theory.target_of o before_exit}
   176   end;
   177 
   178 val theory_init = init I "";
   179 
   180 val reinit =
   181   Local_Theory.assert_bottom true #> Data.get #> the #>
   182   (fn Target {target, before_exit, ...} => Local_Theory.exit_global #> init before_exit target);
   183 
   184 fun context_cmd ("-", _) thy = theory_init thy
   185   | context_cmd target thy = init I (Locale.check thy target) thy;
   186 
   187 end;