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