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