src/Pure/Isar/local_theory.ML
author wenzelm
Sun Oct 25 12:27:21 2009 +0100 (2009-10-25 ago)
changeset 33157 56f836b9414f
parent 33095 bbd52d2f8696
child 33166 55f250ef9e31
permissions -rw-r--r--
allow name space entries to be "concealed" -- via binding/naming/local_theory;
wenzelm@18744
     1
(*  Title:      Pure/Isar/local_theory.ML
wenzelm@18744
     2
    Author:     Makarius
wenzelm@18744
     3
wenzelm@20888
     4
Local theory operations, with abstract target context.
wenzelm@18744
     5
*)
wenzelm@18744
     6
wenzelm@18951
     7
type local_theory = Proof.context;
wenzelm@18951
     8
wenzelm@18744
     9
signature LOCAL_THEORY =
wenzelm@18744
    10
sig
wenzelm@20888
    11
  type operations
wenzelm@26131
    12
  val group_of: local_theory -> string
wenzelm@28017
    13
  val group_properties_of: local_theory -> Properties.T
wenzelm@28017
    14
  val group_position_of: local_theory -> Properties.T
wenzelm@26131
    15
  val set_group: string -> local_theory -> local_theory
wenzelm@20888
    16
  val target_of: local_theory -> Proof.context
wenzelm@20960
    17
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    18
  val raw_theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@28379
    19
  val checkpoint: local_theory -> local_theory
wenzelm@33095
    20
  val full_naming: local_theory -> Name_Space.naming
haftmann@29581
    21
  val full_name: local_theory -> binding -> string
wenzelm@20960
    22
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    23
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    24
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    25
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@30578
    26
  val map_contexts: (Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@21860
    27
  val affirm: local_theory -> local_theory
wenzelm@20960
    28
  val pretty: local_theory -> Pretty.T list
haftmann@29581
    29
  val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@25120
    30
    (term * term) * local_theory
haftmann@29581
    31
  val define: string -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    32
    (term * (string * thm)) * local_theory
wenzelm@28084
    33
  val note: string -> Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
wenzelm@28084
    34
  val notes: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    35
    local_theory -> (string * thm list) list * local_theory
wenzelm@24020
    36
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    37
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    38
  val declaration: declaration -> local_theory -> local_theory
wenzelm@24949
    39
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    40
  val target_morphism: local_theory -> morphism
wenzelm@21529
    41
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    42
  val restore: local_theory -> local_theory
wenzelm@25289
    43
  val reinit: local_theory -> local_theory
wenzelm@21292
    44
  val exit: local_theory -> Proof.context
wenzelm@28395
    45
  val exit_global: local_theory -> theory
wenzelm@28395
    46
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    47
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    48
end;
wenzelm@18744
    49
wenzelm@18744
    50
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    51
struct
wenzelm@18744
    52
wenzelm@20888
    53
(** local theory data **)
wenzelm@19059
    54
wenzelm@20888
    55
(* type lthy *)
wenzelm@20888
    56
wenzelm@20888
    57
type operations =
wenzelm@20960
    58
 {pretty: local_theory -> Pretty.T list,
haftmann@29581
    59
  abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@28084
    60
    (term * term) * local_theory,
wenzelm@26131
    61
  define: string ->
haftmann@29581
    62
    (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    63
    (term * (string * thm)) * local_theory,
wenzelm@26131
    64
  notes: string ->
wenzelm@28084
    65
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    66
    local_theory -> (string * thm list) list * local_theory,
wenzelm@24020
    67
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    68
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    69
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    70
  reinit: local_theory -> local_theory,
wenzelm@21292
    71
  exit: local_theory -> Proof.context};
wenzelm@20888
    72
wenzelm@20888
    73
datatype lthy = LThy of
wenzelm@26131
    74
 {group: string,
wenzelm@33157
    75
  conceal: bool,
wenzelm@26131
    76
  theory_prefix: string,
wenzelm@20888
    77
  operations: operations,
wenzelm@20888
    78
  target: Proof.context};
wenzelm@20888
    79
wenzelm@33157
    80
fun make_lthy (group, conceal, theory_prefix, operations, target) =
wenzelm@33157
    81
  LThy {group = group, conceal = conceal, theory_prefix = theory_prefix,
wenzelm@33157
    82
    operations = operations, target = target};
wenzelm@20888
    83
wenzelm@20888
    84
wenzelm@20888
    85
(* context data *)
wenzelm@18744
    86
wenzelm@18744
    87
structure Data = ProofDataFun
wenzelm@18744
    88
(
wenzelm@20888
    89
  type T = lthy option;
wenzelm@20888
    90
  fun init _ = NONE;
wenzelm@18744
    91
);
wenzelm@18744
    92
wenzelm@20888
    93
fun get_lthy lthy =
wenzelm@20888
    94
  (case Data.get lthy of
wenzelm@20888
    95
    NONE => error "No local theory context"
wenzelm@20888
    96
  | SOME (LThy data) => data);
wenzelm@18951
    97
wenzelm@20888
    98
fun map_lthy f lthy =
wenzelm@33157
    99
  let val {group, conceal, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33157
   100
  in Data.put (SOME (make_lthy (f (group, conceal, theory_prefix, operations, target)))) lthy end;
wenzelm@26131
   101
wenzelm@26131
   102
wenzelm@26131
   103
(* group *)
wenzelm@26131
   104
wenzelm@26131
   105
val group_of = #group o get_lthy;
wenzelm@26131
   106
wenzelm@26131
   107
fun group_properties_of lthy =
wenzelm@26131
   108
  (case group_of lthy of
wenzelm@26131
   109
    "" => []
wenzelm@26131
   110
  | group => [(Markup.groupN, group)]);
wenzelm@26131
   111
wenzelm@26131
   112
fun group_position_of lthy =
wenzelm@26131
   113
  group_properties_of lthy @ Position.properties_of (Position.thread_data ());
wenzelm@26131
   114
wenzelm@33157
   115
fun set_group group = map_lthy (fn (_, conceal, theory_prefix, operations, target) =>
wenzelm@33157
   116
  (group, conceal, theory_prefix, operations, target));
wenzelm@26131
   117
wenzelm@26131
   118
wenzelm@26131
   119
(* target *)
wenzelm@18767
   120
wenzelm@20888
   121
val target_of = #target o get_lthy;
wenzelm@21860
   122
val affirm = tap target_of;
wenzelm@18767
   123
wenzelm@33157
   124
fun map_target f = map_lthy (fn (group, conceal, theory_prefix, operations, target) =>
wenzelm@33157
   125
  (group, conceal, theory_prefix, operations, f target));
wenzelm@18767
   126
wenzelm@18767
   127
wenzelm@20888
   128
(* substructure mappings *)
wenzelm@19661
   129
wenzelm@20960
   130
fun raw_theory_result f lthy =
wenzelm@20960
   131
  let
wenzelm@20960
   132
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   133
    val lthy' = lthy
wenzelm@20960
   134
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   135
      |> ProofContext.transfer thy';
wenzelm@20960
   136
  in (res, lthy') end;
wenzelm@20960
   137
wenzelm@20960
   138
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   139
wenzelm@28379
   140
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   141
wenzelm@22240
   142
fun full_naming lthy =
wenzelm@33157
   143
  let val {conceal, theory_prefix, ...} = get_lthy lthy in
wenzelm@33157
   144
    Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@33157
   145
    |> Name_Space.mandatory_path theory_prefix
wenzelm@33157
   146
    |> conceal ? Name_Space.conceal
wenzelm@33157
   147
  end;
wenzelm@22240
   148
wenzelm@33095
   149
fun full_name naming = Name_Space.full_name (full_naming naming);
wenzelm@21614
   150
wenzelm@33157
   151
fun theory_result f lthy = lthy |> raw_theory_result (fn thy =>
wenzelm@33157
   152
  let val {conceal, theory_prefix, ...} = get_lthy lthy in
wenzelm@33157
   153
    thy
wenzelm@33157
   154
    |> Sign.mandatory_path theory_prefix
wenzelm@33157
   155
    |> conceal ? Sign.conceal
wenzelm@33157
   156
    |> f
wenzelm@33157
   157
    ||> Sign.restore_naming thy
wenzelm@33157
   158
  end);
wenzelm@19661
   159
wenzelm@20888
   160
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   161
wenzelm@20888
   162
fun target_result f lthy =
wenzelm@18744
   163
  let
wenzelm@28406
   164
    val (res, ctxt') = target_of lthy
wenzelm@28406
   165
      |> ContextPosition.set_visible false
wenzelm@28406
   166
      |> f
wenzelm@28406
   167
      ||> ContextPosition.restore_visible lthy;
wenzelm@20888
   168
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   169
    val lthy' = lthy
wenzelm@20888
   170
      |> map_target (K ctxt')
wenzelm@20888
   171
      |> ProofContext.transfer thy';
wenzelm@20888
   172
  in (res, lthy') end;
wenzelm@18876
   173
wenzelm@20888
   174
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   175
wenzelm@30578
   176
fun map_contexts f =
wenzelm@30578
   177
  theory (Context.theory_map f) #>
wenzelm@30578
   178
  target (Context.proof_map f) #>
wenzelm@30578
   179
  Context.proof_map f;
wenzelm@30578
   180
wenzelm@18767
   181
wenzelm@21664
   182
(* basic operations *)
wenzelm@18781
   183
wenzelm@20888
   184
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   185
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   186
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   187
wenzelm@20888
   188
val pretty = operation #pretty;
wenzelm@28379
   189
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@28379
   190
val define = apsnd checkpoint ooo operation2 #define;
wenzelm@28379
   191
val notes = apsnd checkpoint ooo operation2 #notes;
wenzelm@28379
   192
val type_syntax = checkpoint oo operation1 #type_syntax;
wenzelm@28379
   193
val term_syntax = checkpoint oo operation1 #term_syntax;
wenzelm@28379
   194
val declaration = checkpoint oo operation1 #declaration;
wenzelm@18781
   195
wenzelm@26131
   196
fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   197
wenzelm@24933
   198
fun target_morphism lthy =
wenzelm@28395
   199
  ProofContext.norm_export_morphism lthy (target_of lthy);
wenzelm@24933
   200
wenzelm@24949
   201
fun notation add mode raw_args lthy =
wenzelm@21743
   202
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   203
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   204
wenzelm@21664
   205
wenzelm@28395
   206
(* init *)
wenzelm@20910
   207
wenzelm@20910
   208
fun init theory_prefix operations target = target |> Data.map
wenzelm@33157
   209
  (fn NONE => SOME (make_lthy ("", false, theory_prefix, operations, target))
wenzelm@28379
   210
    | SOME _ => error "Local theory already initialized")
wenzelm@28379
   211
  |> checkpoint;
wenzelm@20910
   212
wenzelm@21273
   213
fun restore lthy =
wenzelm@33157
   214
  let val {theory_prefix, operations, target, ...} = get_lthy lthy
wenzelm@20910
   215
  in init theory_prefix operations target end;
wenzelm@20910
   216
wenzelm@28379
   217
val reinit = checkpoint o operation #reinit;
wenzelm@28395
   218
wenzelm@28395
   219
wenzelm@28395
   220
(* exit *)
wenzelm@28395
   221
wenzelm@28379
   222
val exit = ProofContext.theory Theory.checkpoint o operation #exit;
wenzelm@28395
   223
val exit_global = ProofContext.theory_of o exit;
wenzelm@28395
   224
wenzelm@28395
   225
fun exit_result f (x, lthy) =
wenzelm@28395
   226
  let
wenzelm@28395
   227
    val ctxt = exit lthy;
wenzelm@28395
   228
    val phi = ProofContext.norm_export_morphism lthy ctxt;
wenzelm@28395
   229
  in (f phi x, ctxt) end;
wenzelm@28395
   230
wenzelm@28395
   231
fun exit_result_global f (x, lthy) =
wenzelm@28395
   232
  let
wenzelm@28395
   233
    val thy = exit_global lthy;
wenzelm@28395
   234
    val thy_ctxt = ProofContext.init thy;
wenzelm@28395
   235
    val phi = ProofContext.norm_export_morphism lthy thy_ctxt;
wenzelm@28395
   236
  in (f phi x, thy) end;
wenzelm@20910
   237
wenzelm@18781
   238
end;