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