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