src/Pure/Isar/named_target.ML
author wenzelm
Thu, 05 Mar 2015 13:28:04 +0100
changeset 59616 eb59c6968219
parent 58665 50b229a5a097
child 59876 8564d7abe5c5
permissions -rw-r--r--
tuned -- more explicit use of context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 41585
diff changeset
     1
(*  Title:      Pure/Isar/named_target.ML
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
30437
910a7aeb8dec more precise treatment of qualified bindings;
wenzelm
parents: 30364
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     4
47066
8a6124d09ff5 basic support for nested contexts including bundles;
wenzelm
parents: 47061
diff changeset
     5
Targets for theory, locale, class -- at the bottom the nested structure.
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     6
*)
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     7
38350
480b2de9927c renamed Theory_Target to the more appropriate Named_Target
haftmann
parents: 38349
diff changeset
     8
signature NAMED_TARGET =
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
     9
sig
52140
88a69da5d3fa tuned structure
haftmann
parents: 52103
diff changeset
    10
  val is_theory: local_theory -> bool
57182
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    11
  val locale_of: local_theory -> string option
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    12
  val bottom_locale_of: local_theory -> string option
57182
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    13
  val class_of: local_theory -> string option
57181
2d13bf9ea77b dropped obscure and unused ad-hoc before_exit hook for named targets
haftmann
parents: 57177
diff changeset
    14
  val init: string -> theory -> local_theory
38388
94d5624dd1f7 Named_Target.theory_init
haftmann
parents: 38381
diff changeset
    15
  val theory_init: theory -> local_theory
58665
50b229a5a097 tuned signature;
wenzelm
parents: 57485
diff changeset
    16
  val theory_map: (local_theory -> local_theory) -> theory -> theory
57485
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
    17
  val theory_like_init: (local_theory -> local_theory) -> theory -> local_theory
57483
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
    18
  val begin: xstring * Position.T -> theory -> local_theory
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
    19
  val exit: local_theory -> theory
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
    20
  val switch: (xstring * Position.T) option -> Context.generic
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
    21
    -> (local_theory -> Context.generic) * local_theory
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    22
end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    23
38350
480b2de9927c renamed Theory_Target to the more appropriate Named_Target
haftmann
parents: 38349
diff changeset
    24
structure Named_Target: NAMED_TARGET =
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    25
struct
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    26
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    27
(* context data *)
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    28
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33466
diff changeset
    29
structure Data = Proof_Data
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    30
(
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    31
  type T = (string * bool) option;
38391
ba1cc1815ce1 named target is optional; explicit Name_Target.reinit
haftmann
parents: 38389
diff changeset
    32
  fun init _ = NONE;
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    33
);
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    34
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    35
val get_bottom_data = Data.get;
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    36
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    37
fun get_data lthy =
57177
dce365931721 sharpened criterion: bare named target is only at the bottom level
haftmann
parents: 57176
diff changeset
    38
  if Local_Theory.level lthy = 1
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    39
  then get_bottom_data lthy
57177
dce365931721 sharpened criterion: bare named target is only at the bottom level
haftmann
parents: 57176
diff changeset
    40
  else NONE;
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    41
57185
haftmann
parents: 57184
diff changeset
    42
fun is_theory lthy =
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    43
  case get_data lthy of
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    44
    SOME ("", _) => true
57185
haftmann
parents: 57184
diff changeset
    45
  | _ => false;
52140
88a69da5d3fa tuned structure
haftmann
parents: 52103
diff changeset
    46
57484
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    47
fun target_of lthy =
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    48
  case get_data lthy of
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    49
    NONE => error "Not in a named target"
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    50
  | SOME (target, _) => target;
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    51
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    52
fun locale_name_of NONE = NONE
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    53
  | locale_name_of (SOME ("", _)) = NONE
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    54
  | locale_name_of (SOME (locale, _)) = SOME locale;
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    55
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    56
val locale_of = locale_name_of o get_data;
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    57
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    58
val bottom_locale_of = locale_name_of o get_bottom_data;
57182
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    59
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    60
fun class_of lthy =
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    61
  case get_data lthy of
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    62
    SOME (class, true) => SOME class
57182
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    63
  | _ => NONE;
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    64
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    65
38318
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    66
(* define *)
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    67
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    68
fun locale_foundation locale (((b, U), mx), (b_def, rhs)) params =
47289
wenzelm
parents: 47288
diff changeset
    69
  Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    70
  #-> (fn (lhs, def) => Generic_Target.locale_const locale Syntax.mode_default ((b, mx), lhs)
47282
57d486231c92 more general standard_declaration;
wenzelm
parents: 47279
diff changeset
    71
    #> pair (lhs, def));
38318
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    72
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    73
fun class_foundation class (((b, U), mx), (b_def, rhs)) params =
47289
wenzelm
parents: 47288
diff changeset
    74
  Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    75
  #-> (fn (lhs, def) => Class.const class ((b, mx), lhs) params
47282
57d486231c92 more general standard_declaration;
wenzelm
parents: 47279
diff changeset
    76
    #> pair (lhs, def));
38318
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    77
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    78
fun foundation ("", _) = Generic_Target.theory_foundation
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    79
  | foundation (locale, false) = locale_foundation locale
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    80
  | foundation (class, true) = class_foundation class;
38303
ad4b59e9d0f9 factored out foundation of `define` into separate function
haftmann
parents: 38302
diff changeset
    81
24939
wenzelm
parents: 24935
diff changeset
    82
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    83
(* notes *)
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    84
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    85
fun notes ("", _) = Generic_Target.theory_notes
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    86
  | notes (locale, _) = Generic_Target.locale_notes locale;
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    87
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    88
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    89
(* abbrev *)
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    90
57118
4760ac85b3f0 tuned signature
haftmann
parents: 57116
diff changeset
    91
fun locale_abbrev locale prmode (b, mx) global_rhs params =
57160
cf00d3c9db43 explicit passing of params
haftmann
parents: 57118
diff changeset
    92
  Generic_Target.background_abbrev (b, global_rhs) (snd params)
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    93
  #-> (fn (lhs, _) => Generic_Target.locale_const locale prmode ((b, mx), lhs));
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
    94
57118
4760ac85b3f0 tuned signature
haftmann
parents: 57116
diff changeset
    95
fun class_abbrev class prmode (b, mx) global_rhs params =
57160
cf00d3c9db43 explicit passing of params
haftmann
parents: 57118
diff changeset
    96
  Generic_Target.background_abbrev (b, global_rhs) (snd params)
57161
6254c51cd210 formal treatment of dangling parameters for class abbrevs analogously to class consts
haftmann
parents: 57160
diff changeset
    97
  #-> (fn (lhs, rhs) => Class.abbrev class prmode ((b, mx), lhs) rhs params);
57066
78651e94746f tuned: prefer separate function trails for locales and classes rather than ad-hoc case distinction
haftmann
parents: 57065
diff changeset
    98
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    99
fun abbrev ("", _) = Generic_Target.theory_abbrev
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   100
  | abbrev (locale, false) = locale_abbrev locale
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   101
  | abbrev (class, true) = class_abbrev class;
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   102
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   103
47246
2bbab021c0e6 clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents: 47081
diff changeset
   104
(* declaration *)
2bbab021c0e6 clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents: 47081
diff changeset
   105
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   106
fun declaration ("", _) flags decl = Generic_Target.theory_declaration decl
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   107
  | declaration (locale, _) flags decl = Generic_Target.locale_declaration locale flags decl;
47246
2bbab021c0e6 clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents: 47081
diff changeset
   108
2bbab021c0e6 clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents: 47081
diff changeset
   109
56723
a8f71445c265 subscription as target-specific implementation device
haftmann
parents: 56056
diff changeset
   110
(* subscription *)
a8f71445c265 subscription as target-specific implementation device
haftmann
parents: 56056
diff changeset
   111
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   112
fun subscription ("", _) = Generic_Target.theory_registration
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   113
  | subscription (locale, _) = Generic_Target.locale_dependency locale;
56723
a8f71445c265 subscription as target-specific implementation device
haftmann
parents: 56056
diff changeset
   114
a8f71445c265 subscription as target-specific implementation device
haftmann
parents: 56056
diff changeset
   115
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   116
(* pretty *)
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   117
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   118
fun pretty (target, is_class) ctxt =
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   119
  let
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   120
    val target_name =
55763
4b3907cb5654 tuned signature;
wenzelm
parents: 53088
diff changeset
   121
      [Pretty.keyword1 (if is_class then "class" else "locale"), Pretty.brk 1,
52103
fb577a13abbd more markup;
wenzelm
parents: 48102
diff changeset
   122
        Locale.pretty_name ctxt target];
45353
wenzelm
parents: 45352
diff changeset
   123
    val fixes =
wenzelm
parents: 45352
diff changeset
   124
      map (fn (x, T) => (Binding.name x, SOME T, NoSyn))
wenzelm
parents: 45352
diff changeset
   125
        (#1 (Proof_Context.inferred_fixes ctxt));
wenzelm
parents: 45352
diff changeset
   126
    val assumes =
wenzelm
parents: 45352
diff changeset
   127
      map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])]))
wenzelm
parents: 45352
diff changeset
   128
        (Assumption.all_assms_of ctxt);
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   129
    val elems =
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   130
      (if null fixes then [] else [Element.Fixes fixes]) @
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   131
      (if null assumes then [] else [Element.Assumes assumes]);
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39639
diff changeset
   132
    val body_elems =
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   133
      if target = "" then []
39639
haftmann
parents: 39557
diff changeset
   134
      else if null elems then [Pretty.block target_name]
haftmann
parents: 39557
diff changeset
   135
      else [Pretty.block (Pretty.fbreaks (Pretty.block (target_name @ [Pretty.str " ="]) ::
40782
aa533c5e3f48 superficial tuning;
wenzelm
parents: 39639
diff changeset
   136
        map (Pretty.chunks o Element.pretty_ctxt ctxt) elems))];
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   137
  in
55763
4b3907cb5654 tuned signature;
wenzelm
parents: 53088
diff changeset
   138
    Pretty.block [Pretty.keyword1 "theory", Pretty.brk 1,
42360
da8817d01e7c modernized structure Proof_Context;
wenzelm
parents: 41959
diff changeset
   139
      Pretty.str (Context.theory_name (Proof_Context.theory_of ctxt))] :: body_elems
38308
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   140
  end;
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   141
0e4649095739 try to uniformly follow define/note/abbrev/declaration order as close as possible
haftmann
parents: 38307
diff changeset
   142
38378
haftmann
parents: 38350
diff changeset
   143
(* init *)
25291
870455627720 misc cleanup of init functions;
wenzelm
parents: 25269
diff changeset
   144
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   145
fun make_name_data _ "" = ("", false)
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   146
  | make_name_data thy target =
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   147
      if Locale.defined thy target
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   148
      then (target, Class.is_class thy target)
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   149
      else error ("No such locale: " ^ quote target);
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   150
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   151
fun init_context ("", _) = Proof_Context.init_global
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   152
  | init_context (locale, false) = Locale.init locale
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   153
  | init_context (class, true) = Class.init class;
25269
f9090ae5cec9 clarified theory target interface
haftmann
parents: 25240
diff changeset
   154
57485
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   155
fun gen_init before_exit target thy =
39639
haftmann
parents: 39557
diff changeset
   156
  let
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   157
    val name_data = make_name_data thy target;
47061
355317493f34 clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents: 47000
diff changeset
   158
    val naming = Sign.naming_of thy
355317493f34 clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents: 47000
diff changeset
   159
      |> Name_Space.mandatory_path (Long_Name.base_name target);
39639
haftmann
parents: 39557
diff changeset
   160
  in
haftmann
parents: 39557
diff changeset
   161
    thy
56056
4d46d53566e6 more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents: 55763
diff changeset
   162
    |> Sign.change_begin
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   163
    |> init_context name_data
57485
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   164
    |> is_none before_exit ? Data.put (SOME name_data)
47061
355317493f34 clarified Local_Theory.init: avoid hardwired naming policy, discontinued odd/unused group argument (cf. 5ee13e0428d2);
wenzelm
parents: 47000
diff changeset
   165
    |> Local_Theory.init naming
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   166
       {define = Generic_Target.define (foundation name_data),
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   167
        notes = Generic_Target.notes (notes name_data),
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   168
        abbrev = Generic_Target.abbrev (abbrev name_data),
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   169
        declaration = declaration name_data,
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   170
        subscription = subscription name_data,
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
   171
        pretty = pretty name_data,
57485
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   172
        exit = the_default I before_exit
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   173
          #> Local_Theory.target_of #> Sign.change_end_local}
39639
haftmann
parents: 39557
diff changeset
   174
  end;
38247
12d3a5f04a72 unravelled target initialization code
haftmann
parents: 37314
diff changeset
   175
57485
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   176
val init = gen_init NONE
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   177
57181
2d13bf9ea77b dropped obscure and unused ad-hoc before_exit hook for named targets
haftmann
parents: 57177
diff changeset
   178
val theory_init = init "";
25269
f9090ae5cec9 clarified theory target interface
haftmann
parents: 25240
diff changeset
   179
58665
50b229a5a097 tuned signature;
wenzelm
parents: 57485
diff changeset
   180
fun theory_map f = theory_init #> f #> Local_Theory.exit_global;
50b229a5a097 tuned signature;
wenzelm
parents: 57485
diff changeset
   181
57485
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   182
fun theory_like_init before_exit = gen_init (SOME before_exit) "";
b640e50c91a1 optional exit hook for theory-like targets
haftmann
parents: 57484
diff changeset
   183
57483
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   184
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   185
(* toplevel interaction *)
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   186
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   187
fun begin ("-", _) thy = theory_init thy
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   188
  | begin target thy = init (Locale.check thy target) thy;
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   189
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   190
val exit = Local_Theory.assert_bottom true #> Local_Theory.exit_global;
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   191
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   192
fun switch NONE (Context.Theory thy) =
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   193
      (Context.Theory o exit, theory_init thy)
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   194
  | switch (SOME name) (Context.Theory thy) =
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   195
      (Context.Theory o exit, begin name thy)
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   196
  | switch NONE (Context.Proof lthy) =
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   197
      (Context.Proof o Local_Theory.restore, lthy)
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   198
  | switch (SOME name) (Context.Proof lthy) =
57484
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
   199
      (Context.Proof o init (target_of lthy) o exit,
57483
950dc7446454 centralized (ad-hoc) switching of targets in named_target.ML
haftmann
parents: 57196
diff changeset
   200
        (begin name o exit o Local_Theory.assert_nonbrittle) lthy);
33282
c6364889fea5 misc tuning;
wenzelm
parents: 33173
diff changeset
   201
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   202
end;