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