src/Pure/Isar/named_target.ML
author haftmann
Wed, 09 Jun 2021 18:04:21 +0000
changeset 73845 bfce186331be
parent 72953 90ada01470cb
child 77889 5db014c36f42
permissions -rw-r--r--
more succint interfaces
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
66338
haftmann
parents: 66337
diff changeset
     5
Targets for theory, locale, class -- at the bottom of 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
72605
a4cb880e873a type alias for mixin bundles
haftmann
parents: 72536
diff changeset
    14
  val init: Bundle.name list -> 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
69732
49d25343d3d4 combinator to lift local theory update to theory update
haftmann
parents: 69057
diff changeset
    17
  val theory_map_result: (morphism -> 'a -> 'b) -> (local_theory -> 'a * local_theory) ->
49d25343d3d4 combinator to lift local theory update to theory update
haftmann
parents: 69057
diff changeset
    18
    theory -> 'b * theory
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    19
  val revoke_reinitializability: local_theory -> local_theory
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    20
  val exit_global_reinitialize: local_theory -> (theory -> local_theory) * theory
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    21
end;
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    22
38350
480b2de9927c renamed Theory_Target to the more appropriate Named_Target
haftmann
parents: 38349
diff changeset
    23
structure Named_Target: NAMED_TARGET =
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    24
struct
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
    25
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    26
(* context data *)
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    27
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    28
datatype target = Theory | Locale of string | Class of string;
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    29
66338
haftmann
parents: 66337
diff changeset
    30
fun target_of_ident _ "" = Theory
haftmann
parents: 66337
diff changeset
    31
  | target_of_ident thy ident =
haftmann
parents: 66337
diff changeset
    32
      if Locale.defined thy ident
haftmann
parents: 66337
diff changeset
    33
      then (if Class.is_class thy ident then Class else Locale) ident
haftmann
parents: 66337
diff changeset
    34
      else error ("No such locale: " ^ quote ident);
haftmann
parents: 66337
diff changeset
    35
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    36
fun ident_of_target Theory = ""
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    37
  | ident_of_target (Locale locale) = locale
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    38
  | ident_of_target (Class class) = class;
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    39
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    40
fun target_is_theory (SOME Theory) = true
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    41
  | target_is_theory _ = false;
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    42
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    43
fun locale_of_target (SOME (Locale locale)) = SOME locale
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    44
  | locale_of_target (SOME (Class locale)) = SOME locale
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    45
  | locale_of_target _ = NONE;
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    46
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    47
fun class_of_target (SOME (Class class)) = SOME class
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    48
  | class_of_target _ = NONE;
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    49
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33466
diff changeset
    50
structure Data = Proof_Data
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    51
(
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    52
  type T = (target * bool) option;
38391
ba1cc1815ce1 named target is optional; explicit Name_Target.reinit
haftmann
parents: 38389
diff changeset
    53
  fun init _ = NONE;
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    54
);
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    55
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    56
val get_bottom_target = Option.map fst o Data.get;
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    57
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    58
fun get_target lthy =
57177
dce365931721 sharpened criterion: bare named target is only at the bottom level
haftmann
parents: 57176
diff changeset
    59
  if Local_Theory.level lthy = 1
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    60
  then get_bottom_target lthy
57177
dce365931721 sharpened criterion: bare named target is only at the bottom level
haftmann
parents: 57176
diff changeset
    61
  else NONE;
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    62
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    63
fun ident_of lthy =
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    64
  case get_target lthy of
57484
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    65
    NONE => error "Not in a named target"
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    66
  | SOME target => ident_of_target target;
57484
cc309f3c0490 restore exactly named target, prevent non-named targets to participate in the ad-hoc switch game
haftmann
parents: 57483
diff changeset
    67
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    68
val is_theory = target_is_theory o get_target;
57195
ec0e10f11276 recovered level-free fishing for locale, accidentally lost in dce365931721
haftmann
parents: 57193
diff changeset
    69
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    70
val locale_of = locale_of_target o get_target;
57196
d9a18e44b80d tuned data structure
haftmann
parents: 57195
diff changeset
    71
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    72
val bottom_locale_of = locale_of_target o get_bottom_target;
57182
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    73
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
    74
val class_of = class_of_target o get_target;
57182
79d43c510b84 less baroque interface
haftmann
parents: 57181
diff changeset
    75
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    76
fun is_reinitializable lthy =
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    77
  Local_Theory.level lthy = 1 andalso (the_default false o Option.map snd o Data.get) lthy;
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
    78
21006
ac2732072403 added peek;
wenzelm
parents: 20984
diff changeset
    79
61777
f9e05eab6e3c tuned sections
haftmann
parents: 60344
diff changeset
    80
(* operations *)
38318
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    81
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    82
fun locale_foundation locale (((b, U), mx), (b_def, rhs)) params =
47289
wenzelm
parents: 47288
diff changeset
    83
  Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    84
  #-> (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
    85
    #> pair (lhs, def));
38318
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    86
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    87
fun class_foundation class (((b, U), mx), (b_def, rhs)) params =
47289
wenzelm
parents: 47288
diff changeset
    88
  Generic_Target.background_foundation (((b, U), NoSyn), (b_def, rhs)) params
57074
9a631586e3e5 tuned names
haftmann
parents: 57073
diff changeset
    89
  #-> (fn (lhs, def) => Class.const class ((b, mx), lhs) params
47282
57d486231c92 more general standard_declaration;
wenzelm
parents: 47279
diff changeset
    90
    #> pair (lhs, def));
38318
1fade69519ab different foundations for different targets; simplified syntax handling of abbreviations
haftmann
parents: 38314
diff changeset
    91
66338
haftmann
parents: 66337
diff changeset
    92
fun operations Theory =
haftmann
parents: 66337
diff changeset
    93
      {define = Generic_Target.define Generic_Target.theory_target_foundation,
haftmann
parents: 66337
diff changeset
    94
       notes = Generic_Target.notes Generic_Target.theory_target_notes,
haftmann
parents: 66337
diff changeset
    95
       abbrev = Generic_Target.theory_abbrev,
haftmann
parents: 66337
diff changeset
    96
       declaration = fn _ => Generic_Target.theory_declaration,
73845
bfce186331be more succint interfaces
haftmann
parents: 72953
diff changeset
    97
       theory_registration = Generic_Target.theory_registration,
66338
haftmann
parents: 66337
diff changeset
    98
       locale_dependency = fn _ => error "Not possible in theory target",
haftmann
parents: 66337
diff changeset
    99
       pretty = fn ctxt => [Pretty.block [Pretty.keyword1 "theory", Pretty.brk 1,
haftmann
parents: 66337
diff changeset
   100
        Pretty.str (Context.theory_name (Proof_Context.theory_of ctxt))]]}
haftmann
parents: 66337
diff changeset
   101
  | operations (Locale locale) =
haftmann
parents: 66337
diff changeset
   102
      {define = Generic_Target.define (locale_foundation locale),
haftmann
parents: 66337
diff changeset
   103
       notes = Generic_Target.notes (Generic_Target.locale_target_notes locale),
haftmann
parents: 66337
diff changeset
   104
       abbrev = Generic_Target.locale_abbrev locale,
haftmann
parents: 66337
diff changeset
   105
       declaration = Generic_Target.locale_declaration locale,
haftmann
parents: 66337
diff changeset
   106
       theory_registration = fn _ => error "Not possible in locale target",
73845
bfce186331be more succint interfaces
haftmann
parents: 72953
diff changeset
   107
       locale_dependency = Generic_Target.locale_dependency locale,
69057
56c6378ebaea tuned signature: prefer value-oriented pretty-printing;
wenzelm
parents: 67615
diff changeset
   108
       pretty = fn ctxt => [Locale.pretty_locale (Proof_Context.theory_of ctxt) false locale]}
66338
haftmann
parents: 66337
diff changeset
   109
  | operations (Class class) =
haftmann
parents: 66337
diff changeset
   110
      {define = Generic_Target.define (class_foundation class),
haftmann
parents: 66337
diff changeset
   111
       notes = Generic_Target.notes (Generic_Target.locale_target_notes class),
haftmann
parents: 66337
diff changeset
   112
       abbrev = Class.abbrev class,
haftmann
parents: 66337
diff changeset
   113
       declaration = Generic_Target.locale_declaration class,
haftmann
parents: 66337
diff changeset
   114
       theory_registration = fn _ => error "Not possible in class target",
73845
bfce186331be more succint interfaces
haftmann
parents: 72953
diff changeset
   115
       locale_dependency = Generic_Target.locale_dependency class,
66338
haftmann
parents: 66337
diff changeset
   116
       pretty = fn ctxt => Class.pretty_specification (Proof_Context.theory_of ctxt) class};
61890
f6ded81f5690 abandoned attempt to unify sublocale and interpretation into global theories
haftmann
parents: 61777
diff changeset
   117
66338
haftmann
parents: 66337
diff changeset
   118
fun setup_context Theory = Proof_Context.init_global
haftmann
parents: 66337
diff changeset
   119
  | setup_context (Locale locale) = Locale.init locale
haftmann
parents: 66337
diff changeset
   120
  | setup_context (Class class) = Class.init class;
25269
f9090ae5cec9 clarified theory target interface
haftmann
parents: 25240
diff changeset
   121
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   122
fun setup target includes =
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   123
  setup_context target
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   124
  #> Data.put (SOME (target, null includes))
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   125
  #> Bundle.includes includes;
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   126
72536
589645894305 bundle mixins for locale and class specifications
haftmann
parents: 72516
diff changeset
   127
fun init includes ident thy =
39639
haftmann
parents: 39557
diff changeset
   128
  let
66338
haftmann
parents: 66337
diff changeset
   129
    val target = target_of_ident thy ident;
39639
haftmann
parents: 39557
diff changeset
   130
  in
haftmann
parents: 39557
diff changeset
   131
    thy
72516
17dc99589a91 unified Local_Theory.init with Generic_Target.init
haftmann
parents: 72505
diff changeset
   132
    |> Local_Theory.init
66335
a849ce33923d treat exit separate from regular local theory operations
haftmann
parents: 66334
diff changeset
   133
       {background_naming = Sign.naming_of thy |> Name_Space.mandatory_path (Long_Name.base_name ident),
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   134
        setup = setup target includes,
72516
17dc99589a91 unified Local_Theory.init with Generic_Target.init
haftmann
parents: 72505
diff changeset
   135
        conclude = I}
66338
haftmann
parents: 66337
diff changeset
   136
       (operations target)
39639
haftmann
parents: 39557
diff changeset
   137
  end;
38247
12d3a5f04a72 unravelled target initialization code
haftmann
parents: 37314
diff changeset
   138
72536
589645894305 bundle mixins for locale and class specifications
haftmann
parents: 72516
diff changeset
   139
val theory_init = init [] "";
66333
30c1639a343a prefer explicit datatype over implicit sum;
haftmann
parents: 66259
diff changeset
   140
69732
49d25343d3d4 combinator to lift local theory update to theory update
haftmann
parents: 69057
diff changeset
   141
fun theory_map g = theory_init #> g #> Local_Theory.exit_global;
49d25343d3d4 combinator to lift local theory update to theory update
haftmann
parents: 69057
diff changeset
   142
49d25343d3d4 combinator to lift local theory update to theory update
haftmann
parents: 69057
diff changeset
   143
fun theory_map_result f g = theory_init #> g #> Local_Theory.exit_result_global f;
58665
50b229a5a097 tuned signature;
wenzelm
parents: 57485
diff changeset
   144
72953
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   145
val revoke_reinitializability = (Data.map o Option.map o apsnd) (K false);
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   146
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   147
fun exit_global_reinitialize lthy =
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   148
  if is_reinitializable lthy
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   149
  then (init [] (ident_of lthy), Local_Theory.exit_global lthy)
90ada01470cb clarified scope of concept
haftmann
parents: 72952
diff changeset
   150
  else error "Non-reinitializable local theory context";
33282
c6364889fea5 misc tuning;
wenzelm
parents: 33173
diff changeset
   151
20894
784eefc906aa Common theory targets.
wenzelm
parents:
diff changeset
   152
end;