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