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