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