src/Pure/Isar/local_theory.ML
author wenzelm
Sun Mar 28 19:20:52 2010 +0200 (2010-03-28 ago)
changeset 36004 5d79ca55a52b
parent 35798 fd1bb29f8170
child 36451 ddc965e172c4
permissions -rw-r--r--
implicit checkpoint in Local_Theory.theory as well -- no longer export Local_Theory.checkpoint;
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@33724
    17
  val new_group: local_theory -> local_theory
wenzelm@33724
    18
  val reset_group: local_theory -> local_theory
wenzelm@33276
    19
  val restore_naming: local_theory -> local_theory -> local_theory
wenzelm@20888
    20
  val target_of: local_theory -> Proof.context
wenzelm@20960
    21
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    22
  val raw_theory: (theory -> theory) -> 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
wenzelm@33764
    34
  val define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    35
    (term * (string * thm)) * local_theory
wenzelm@33670
    36
  val note: Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
wenzelm@33670
    37
  val notes: (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@33670
    38
    local_theory -> (string * thm list) list * local_theory
wenzelm@33670
    39
  val notes_kind: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    40
    local_theory -> (string * thm list) list * local_theory
wenzelm@33456
    41
  val declaration: bool -> declaration -> local_theory -> local_theory
wenzelm@35798
    42
  val syntax_declaration: bool -> declaration -> local_theory -> local_theory
wenzelm@35412
    43
  val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> local_theory -> local_theory
wenzelm@24949
    44
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@35738
    45
  val class_alias: binding -> class -> local_theory -> local_theory
wenzelm@35738
    46
  val type_alias: binding -> string -> local_theory -> local_theory
wenzelm@35738
    47
  val const_alias: binding -> string -> local_theory -> local_theory
wenzelm@33724
    48
  val init: serial option -> string -> operations -> Proof.context -> local_theory
wenzelm@21273
    49
  val restore: local_theory -> local_theory
wenzelm@25289
    50
  val reinit: local_theory -> local_theory
wenzelm@21292
    51
  val exit: local_theory -> Proof.context
wenzelm@28395
    52
  val exit_global: local_theory -> theory
wenzelm@28395
    53
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    54
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    55
end;
wenzelm@18744
    56
wenzelm@33671
    57
structure Local_Theory: LOCAL_THEORY =
wenzelm@18744
    58
struct
wenzelm@18744
    59
wenzelm@20888
    60
(** local theory data **)
wenzelm@19059
    61
wenzelm@20888
    62
(* type lthy *)
wenzelm@20888
    63
wenzelm@20888
    64
type operations =
wenzelm@20960
    65
 {pretty: local_theory -> Pretty.T list,
haftmann@29581
    66
  abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@28084
    67
    (term * term) * local_theory,
wenzelm@33764
    68
  define: (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    69
    (term * (string * thm)) * local_theory,
wenzelm@26131
    70
  notes: string ->
wenzelm@28084
    71
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    72
    local_theory -> (string * thm list) list * local_theory,
wenzelm@33456
    73
  declaration: bool -> declaration -> local_theory -> local_theory,
wenzelm@35798
    74
  syntax_declaration: bool -> declaration -> local_theory -> local_theory,
wenzelm@25289
    75
  reinit: local_theory -> local_theory,
wenzelm@21292
    76
  exit: local_theory -> Proof.context};
wenzelm@20888
    77
wenzelm@20888
    78
datatype lthy = LThy of
wenzelm@33166
    79
 {naming: Name_Space.naming,
wenzelm@26131
    80
  theory_prefix: string,
wenzelm@20888
    81
  operations: operations,
wenzelm@20888
    82
  target: Proof.context};
wenzelm@20888
    83
wenzelm@33166
    84
fun make_lthy (naming, theory_prefix, operations, target) =
wenzelm@33166
    85
  LThy {naming = naming, theory_prefix = theory_prefix,
wenzelm@33157
    86
    operations = operations, target = target};
wenzelm@20888
    87
wenzelm@20888
    88
wenzelm@20888
    89
(* context data *)
wenzelm@18744
    90
wenzelm@33519
    91
structure Data = Proof_Data
wenzelm@18744
    92
(
wenzelm@20888
    93
  type T = lthy option;
wenzelm@20888
    94
  fun init _ = NONE;
wenzelm@18744
    95
);
wenzelm@18744
    96
wenzelm@20888
    97
fun get_lthy lthy =
wenzelm@20888
    98
  (case Data.get lthy of
wenzelm@20888
    99
    NONE => error "No local theory context"
wenzelm@20888
   100
  | SOME (LThy data) => data);
wenzelm@18951
   101
wenzelm@20888
   102
fun map_lthy f lthy =
wenzelm@33166
   103
  let val {naming, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33166
   104
  in Data.put (SOME (make_lthy (f (naming, theory_prefix, operations, target)))) lthy end;
wenzelm@33166
   105
wenzelm@33166
   106
val affirm = tap get_lthy;
wenzelm@26131
   107
wenzelm@26131
   108
wenzelm@33166
   109
(* naming *)
wenzelm@26131
   110
wenzelm@33166
   111
val naming_of = #naming o get_lthy;
wenzelm@33166
   112
val full_name = Name_Space.full_name o naming_of;
wenzelm@26131
   113
wenzelm@33166
   114
fun map_naming f = map_lthy (fn (naming, theory_prefix, operations, target) =>
wenzelm@33166
   115
  (f naming, theory_prefix, operations, target));
wenzelm@26131
   116
wenzelm@33166
   117
val conceal = map_naming Name_Space.conceal;
wenzelm@33724
   118
val new_group = map_naming Name_Space.new_group;
wenzelm@33724
   119
val reset_group = map_naming Name_Space.reset_group;
wenzelm@26131
   120
wenzelm@33276
   121
val restore_naming = map_naming o K o naming_of;
wenzelm@33276
   122
wenzelm@26131
   123
wenzelm@26131
   124
(* target *)
wenzelm@18767
   125
wenzelm@20888
   126
val target_of = #target o get_lthy;
wenzelm@18767
   127
wenzelm@33166
   128
fun map_target f = map_lthy (fn (naming, theory_prefix, operations, target) =>
wenzelm@33166
   129
  (naming, theory_prefix, operations, f target));
wenzelm@18767
   130
wenzelm@18767
   131
wenzelm@20888
   132
(* substructure mappings *)
wenzelm@19661
   133
wenzelm@20960
   134
fun raw_theory_result f lthy =
wenzelm@20960
   135
  let
wenzelm@20960
   136
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   137
    val lthy' = lthy
wenzelm@20960
   138
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   139
      |> ProofContext.transfer thy';
wenzelm@20960
   140
  in (res, lthy') end;
wenzelm@20960
   141
wenzelm@20960
   142
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   143
wenzelm@28379
   144
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   145
wenzelm@33166
   146
fun theory_result f lthy =
wenzelm@33166
   147
  lthy |> raw_theory_result (fn thy =>
wenzelm@33157
   148
    thy
wenzelm@33166
   149
    |> Sign.map_naming (K (naming_of lthy))
wenzelm@33157
   150
    |> f
wenzelm@36004
   151
    ||> Sign.restore_naming thy
wenzelm@36004
   152
    ||> Theory.checkpoint);
wenzelm@19661
   153
wenzelm@20888
   154
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   155
wenzelm@20888
   156
fun target_result f lthy =
wenzelm@18744
   157
  let
wenzelm@28406
   158
    val (res, ctxt') = target_of lthy
wenzelm@33383
   159
      |> Context_Position.set_visible false
wenzelm@28406
   160
      |> f
wenzelm@33383
   161
      ||> Context_Position.restore_visible lthy;
wenzelm@20888
   162
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   163
    val lthy' = lthy
wenzelm@20888
   164
      |> map_target (K ctxt')
wenzelm@20888
   165
      |> ProofContext.transfer thy';
wenzelm@20888
   166
  in (res, lthy') end;
wenzelm@18876
   167
wenzelm@20888
   168
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   169
wenzelm@30578
   170
fun map_contexts f =
wenzelm@30578
   171
  theory (Context.theory_map f) #>
wenzelm@30578
   172
  target (Context.proof_map f) #>
wenzelm@30578
   173
  Context.proof_map f;
wenzelm@30578
   174
wenzelm@18767
   175
wenzelm@33281
   176
(* morphisms *)
wenzelm@33281
   177
wenzelm@33281
   178
fun standard_morphism lthy ctxt =
wenzelm@33281
   179
  ProofContext.norm_export_morphism lthy ctxt $>
wenzelm@33281
   180
  Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
wenzelm@33281
   181
wenzelm@33281
   182
fun target_morphism lthy = standard_morphism lthy (target_of lthy);
wenzelm@33456
   183
fun global_morphism lthy = standard_morphism lthy (ProofContext.init (ProofContext.theory_of lthy));
wenzelm@33281
   184
wenzelm@33281
   185
wenzelm@21664
   186
(* basic operations *)
wenzelm@18781
   187
wenzelm@20888
   188
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@33764
   189
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   190
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   191
wenzelm@20888
   192
val pretty = operation #pretty;
wenzelm@28379
   193
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@33764
   194
val define = apsnd checkpoint oo operation1 #define;
wenzelm@33670
   195
val notes_kind = apsnd checkpoint ooo operation2 #notes;
wenzelm@33456
   196
val declaration = checkpoint ooo operation2 #declaration;
wenzelm@35798
   197
val syntax_declaration = checkpoint ooo operation2 #syntax_declaration;
wenzelm@18781
   198
wenzelm@33670
   199
val notes = notes_kind "";
wenzelm@33670
   200
fun note (a, ths) = notes [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   201
wenzelm@35738
   202
wenzelm@35738
   203
(* notation *)
wenzelm@35738
   204
wenzelm@35412
   205
fun type_notation add mode raw_args lthy =
wenzelm@35412
   206
  let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
wenzelm@35798
   207
  in syntax_declaration false (ProofContext.target_type_notation add mode args) lthy end;
wenzelm@35412
   208
wenzelm@24949
   209
fun notation add mode raw_args lthy =
wenzelm@21743
   210
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@35798
   211
  in syntax_declaration false (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   212
wenzelm@21664
   213
wenzelm@35738
   214
(* name space aliases *)
wenzelm@35738
   215
wenzelm@35798
   216
fun alias global_alias local_alias b name =
wenzelm@35738
   217
  syntax_declaration false (fn phi =>
wenzelm@35738
   218
    let val b' = Morphism.binding phi b
wenzelm@35738
   219
    in Context.mapping (global_alias b' name) (local_alias b' name) end)
wenzelm@35738
   220
  #> local_alias b name;
wenzelm@35738
   221
wenzelm@35798
   222
val class_alias = alias Sign.class_alias ProofContext.class_alias;
wenzelm@35798
   223
val type_alias = alias Sign.type_alias ProofContext.type_alias;
wenzelm@35798
   224
val const_alias = alias Sign.const_alias ProofContext.const_alias;
wenzelm@35738
   225
wenzelm@35738
   226
wenzelm@28395
   227
(* init *)
wenzelm@20910
   228
wenzelm@33724
   229
fun init group theory_prefix operations target =
wenzelm@33166
   230
  let val naming =
wenzelm@33166
   231
    Sign.naming_of (ProofContext.theory_of target)
wenzelm@33724
   232
    |> Name_Space.set_group group
wenzelm@33166
   233
    |> Name_Space.mandatory_path theory_prefix;
wenzelm@33166
   234
  in
wenzelm@33166
   235
    target |> Data.map
wenzelm@33166
   236
      (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
wenzelm@33166
   237
        | SOME _ => error "Local theory already initialized")
wenzelm@33166
   238
    |> checkpoint
wenzelm@33166
   239
  end;
wenzelm@20910
   240
wenzelm@21273
   241
fun restore lthy =
wenzelm@33724
   242
  let val {naming, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33724
   243
  in init (Name_Space.get_group naming) theory_prefix operations target end;
wenzelm@20910
   244
wenzelm@28379
   245
val reinit = checkpoint o operation #reinit;
wenzelm@28395
   246
wenzelm@28395
   247
wenzelm@28395
   248
(* exit *)
wenzelm@28395
   249
wenzelm@28379
   250
val exit = ProofContext.theory Theory.checkpoint o operation #exit;
wenzelm@28395
   251
val exit_global = ProofContext.theory_of o exit;
wenzelm@28395
   252
wenzelm@28395
   253
fun exit_result f (x, lthy) =
wenzelm@28395
   254
  let
wenzelm@28395
   255
    val ctxt = exit lthy;
wenzelm@33281
   256
    val phi = standard_morphism lthy ctxt;
wenzelm@28395
   257
  in (f phi x, ctxt) end;
wenzelm@28395
   258
wenzelm@28395
   259
fun exit_result_global f (x, lthy) =
wenzelm@28395
   260
  let
wenzelm@28395
   261
    val thy = exit_global lthy;
wenzelm@28395
   262
    val thy_ctxt = ProofContext.init thy;
wenzelm@33281
   263
    val phi = standard_morphism lthy thy_ctxt;
wenzelm@28395
   264
  in (f phi x, thy) end;
wenzelm@20910
   265
wenzelm@18781
   266
end;