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