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