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