src/Pure/Isar/local_theory.ML
author wenzelm
Mon Nov 02 20:30:40 2009 +0100 (2009-11-02 ago)
changeset 33383 12d79ece3f7e
parent 33281 223ef9bc399a
child 33456 fbd47f9b9b12
permissions -rw-r--r--
modernized structure Context_Position;
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@20960
    30
  val pretty: local_theory -> Pretty.T list
haftmann@29581
    31
  val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@25120
    32
    (term * term) * local_theory
haftmann@29581
    33
  val define: string -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    34
    (term * (string * thm)) * local_theory
wenzelm@28084
    35
  val note: string -> Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
wenzelm@28084
    36
  val notes: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    37
    local_theory -> (string * thm list) list * local_theory
wenzelm@24020
    38
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    39
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    40
  val declaration: declaration -> local_theory -> local_theory
wenzelm@24949
    41
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    42
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    43
  val restore: local_theory -> local_theory
wenzelm@25289
    44
  val reinit: local_theory -> local_theory
wenzelm@21292
    45
  val exit: local_theory -> Proof.context
wenzelm@28395
    46
  val exit_global: local_theory -> theory
wenzelm@28395
    47
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    48
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    49
end;
wenzelm@18744
    50
wenzelm@18744
    51
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    52
struct
wenzelm@18744
    53
wenzelm@20888
    54
(** local theory data **)
wenzelm@19059
    55
wenzelm@20888
    56
(* type lthy *)
wenzelm@20888
    57
wenzelm@20888
    58
type operations =
wenzelm@20960
    59
 {pretty: local_theory -> Pretty.T list,
haftmann@29581
    60
  abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@28084
    61
    (term * term) * local_theory,
wenzelm@26131
    62
  define: string ->
haftmann@29581
    63
    (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    64
    (term * (string * thm)) * local_theory,
wenzelm@26131
    65
  notes: string ->
wenzelm@28084
    66
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    67
    local_theory -> (string * thm list) list * local_theory,
wenzelm@24020
    68
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    69
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    70
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    71
  reinit: local_theory -> local_theory,
wenzelm@21292
    72
  exit: local_theory -> Proof.context};
wenzelm@20888
    73
wenzelm@20888
    74
datatype lthy = LThy of
wenzelm@33166
    75
 {naming: Name_Space.naming,
wenzelm@26131
    76
  theory_prefix: string,
wenzelm@20888
    77
  operations: operations,
wenzelm@20888
    78
  target: Proof.context};
wenzelm@20888
    79
wenzelm@33166
    80
fun make_lthy (naming, theory_prefix, operations, target) =
wenzelm@33166
    81
  LThy {naming = naming, 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@33166
    99
  let val {naming, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33166
   100
  in Data.put (SOME (make_lthy (f (naming, theory_prefix, operations, target)))) lthy end;
wenzelm@33166
   101
wenzelm@33166
   102
val affirm = tap get_lthy;
wenzelm@26131
   103
wenzelm@26131
   104
wenzelm@33166
   105
(* naming *)
wenzelm@26131
   106
wenzelm@33166
   107
val naming_of = #naming o get_lthy;
wenzelm@33166
   108
val full_name = Name_Space.full_name o naming_of;
wenzelm@26131
   109
wenzelm@33166
   110
fun map_naming f = map_lthy (fn (naming, theory_prefix, operations, target) =>
wenzelm@33166
   111
  (f naming, theory_prefix, operations, target));
wenzelm@26131
   112
wenzelm@33166
   113
val conceal = map_naming Name_Space.conceal;
wenzelm@33166
   114
val set_group = map_naming o Name_Space.set_group;
wenzelm@26131
   115
wenzelm@33276
   116
val restore_naming = map_naming o K o naming_of;
wenzelm@33276
   117
wenzelm@26131
   118
wenzelm@26131
   119
(* target *)
wenzelm@18767
   120
wenzelm@20888
   121
val target_of = #target o get_lthy;
wenzelm@18767
   122
wenzelm@33166
   123
fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
wenzelm@33166
   124
  (naming, theory_prefix, operations, f target));
wenzelm@18767
   125
wenzelm@18767
   126
wenzelm@20888
   127
(* substructure mappings *)
wenzelm@19661
   128
wenzelm@20960
   129
fun raw_theory_result f lthy =
wenzelm@20960
   130
  let
wenzelm@20960
   131
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   132
    val lthy' = lthy
wenzelm@20960
   133
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   134
      |> ProofContext.transfer thy';
wenzelm@20960
   135
  in (res, lthy') end;
wenzelm@20960
   136
wenzelm@20960
   137
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   138
wenzelm@28379
   139
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   140
wenzelm@33166
   141
fun theory_result f lthy =
wenzelm@33166
   142
  lthy |> raw_theory_result (fn thy =>
wenzelm@33157
   143
    thy
wenzelm@33166
   144
    |> Sign.map_naming (K (naming_of lthy))
wenzelm@33157
   145
    |> f
wenzelm@33166
   146
    ||> Sign.restore_naming thy);
wenzelm@19661
   147
wenzelm@20888
   148
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   149
wenzelm@20888
   150
fun target_result f lthy =
wenzelm@18744
   151
  let
wenzelm@28406
   152
    val (res, ctxt') = target_of lthy
wenzelm@33383
   153
      |> Context_Position.set_visible false
wenzelm@28406
   154
      |> f
wenzelm@33383
   155
      ||> Context_Position.restore_visible lthy;
wenzelm@20888
   156
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   157
    val lthy' = lthy
wenzelm@20888
   158
      |> map_target (K ctxt')
wenzelm@20888
   159
      |> ProofContext.transfer thy';
wenzelm@20888
   160
  in (res, lthy') end;
wenzelm@18876
   161
wenzelm@20888
   162
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   163
wenzelm@30578
   164
fun map_contexts f =
wenzelm@30578
   165
  theory (Context.theory_map f) #>
wenzelm@30578
   166
  target (Context.proof_map f) #>
wenzelm@30578
   167
  Context.proof_map f;
wenzelm@30578
   168
wenzelm@18767
   169
wenzelm@33281
   170
(* morphisms *)
wenzelm@33281
   171
wenzelm@33281
   172
fun standard_morphism lthy ctxt =
wenzelm@33281
   173
  ProofContext.norm_export_morphism lthy ctxt $>
wenzelm@33281
   174
  Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
wenzelm@33281
   175
wenzelm@33281
   176
fun target_morphism lthy = standard_morphism lthy (target_of lthy);
wenzelm@33281
   177
wenzelm@33281
   178
wenzelm@21664
   179
(* basic operations *)
wenzelm@18781
   180
wenzelm@20888
   181
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   182
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   183
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   184
wenzelm@20888
   185
val pretty = operation #pretty;
wenzelm@28379
   186
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@28379
   187
val define = apsnd checkpoint ooo operation2 #define;
wenzelm@28379
   188
val notes = apsnd checkpoint ooo operation2 #notes;
wenzelm@28379
   189
val type_syntax = checkpoint oo operation1 #type_syntax;
wenzelm@28379
   190
val term_syntax = checkpoint oo operation1 #term_syntax;
wenzelm@28379
   191
val declaration = checkpoint oo operation1 #declaration;
wenzelm@18781
   192
wenzelm@26131
   193
fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   194
wenzelm@24949
   195
fun notation add mode raw_args lthy =
wenzelm@21743
   196
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   197
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   198
wenzelm@21664
   199
wenzelm@28395
   200
(* init *)
wenzelm@20910
   201
wenzelm@33166
   202
fun init theory_prefix operations target =
wenzelm@33166
   203
  let val naming =
wenzelm@33166
   204
    Sign.naming_of (ProofContext.theory_of target)
wenzelm@33166
   205
    |> Name_Space.mandatory_path theory_prefix;
wenzelm@33166
   206
  in
wenzelm@33166
   207
    target |> Data.map
wenzelm@33166
   208
      (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
wenzelm@33166
   209
        | SOME _ => error "Local theory already initialized")
wenzelm@33166
   210
    |> checkpoint
wenzelm@33166
   211
  end;
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@33281
   228
    val phi = standard_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@33281
   235
    val phi = standard_morphism lthy thy_ctxt;
wenzelm@28395
   236
  in (f phi x, thy) end;
wenzelm@20910
   237
wenzelm@18781
   238
end;