src/Pure/Isar/local_theory.ML
author wenzelm
Mon Mar 01 17:07:36 2010 +0100 (2010-03-01 ago)
changeset 35412 b8dead547d9e
parent 33764 7bcefaab8d41
child 35738 98fd035c3fe3
permissions -rw-r--r--
more uniform treatment of syntax for types vs. consts;
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@33724
    47
  val init: serial option -> string -> operations -> Proof.context -> local_theory
wenzelm@21273
    48
  val restore: local_theory -> local_theory
wenzelm@25289
    49
  val reinit: local_theory -> local_theory
wenzelm@21292
    50
  val exit: local_theory -> Proof.context
wenzelm@28395
    51
  val exit_global: local_theory -> theory
wenzelm@28395
    52
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    53
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    54
end;
wenzelm@18744
    55
wenzelm@33671
    56
structure Local_Theory: LOCAL_THEORY =
wenzelm@18744
    57
struct
wenzelm@18744
    58
wenzelm@20888
    59
(** local theory data **)
wenzelm@19059
    60
wenzelm@20888
    61
(* type lthy *)
wenzelm@20888
    62
wenzelm@20888
    63
type operations =
wenzelm@20960
    64
 {pretty: local_theory -> Pretty.T list,
haftmann@29581
    65
  abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@28084
    66
    (term * term) * local_theory,
wenzelm@33764
    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,
wenzelm@33456
    72
  type_syntax: bool -> declaration -> local_theory -> local_theory,
wenzelm@33456
    73
  term_syntax: bool -> declaration -> local_theory -> local_theory,
wenzelm@33456
    74
  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@33166
   151
    ||> Sign.restore_naming thy);
wenzelm@19661
   152
wenzelm@20888
   153
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   154
wenzelm@20888
   155
fun target_result f lthy =
wenzelm@18744
   156
  let
wenzelm@28406
   157
    val (res, ctxt') = target_of lthy
wenzelm@33383
   158
      |> Context_Position.set_visible false
wenzelm@28406
   159
      |> f
wenzelm@33383
   160
      ||> Context_Position.restore_visible lthy;
wenzelm@20888
   161
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   162
    val lthy' = lthy
wenzelm@20888
   163
      |> map_target (K ctxt')
wenzelm@20888
   164
      |> ProofContext.transfer thy';
wenzelm@20888
   165
  in (res, lthy') end;
wenzelm@18876
   166
wenzelm@20888
   167
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   168
wenzelm@30578
   169
fun map_contexts f =
wenzelm@30578
   170
  theory (Context.theory_map f) #>
wenzelm@30578
   171
  target (Context.proof_map f) #>
wenzelm@30578
   172
  Context.proof_map f;
wenzelm@30578
   173
wenzelm@18767
   174
wenzelm@33281
   175
(* morphisms *)
wenzelm@33281
   176
wenzelm@33281
   177
fun standard_morphism lthy ctxt =
wenzelm@33281
   178
  ProofContext.norm_export_morphism lthy ctxt $>
wenzelm@33281
   179
  Morphism.binding_morphism (Name_Space.transform_binding (naming_of lthy));
wenzelm@33281
   180
wenzelm@33281
   181
fun target_morphism lthy = standard_morphism lthy (target_of lthy);
wenzelm@33456
   182
fun global_morphism lthy = standard_morphism lthy (ProofContext.init (ProofContext.theory_of lthy));
wenzelm@33281
   183
wenzelm@33281
   184
wenzelm@21664
   185
(* basic operations *)
wenzelm@18781
   186
wenzelm@20888
   187
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@33764
   188
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   189
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   190
wenzelm@20888
   191
val pretty = operation #pretty;
wenzelm@28379
   192
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@33764
   193
val define = apsnd checkpoint oo operation1 #define;
wenzelm@33670
   194
val notes_kind = apsnd checkpoint ooo operation2 #notes;
wenzelm@33456
   195
val type_syntax = checkpoint ooo operation2 #type_syntax;
wenzelm@33456
   196
val term_syntax = checkpoint ooo operation2 #term_syntax;
wenzelm@33456
   197
val declaration = checkpoint ooo operation2 #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@35412
   202
fun type_notation add mode raw_args lthy =
wenzelm@35412
   203
  let val args = map (apfst (Morphism.typ (target_morphism lthy))) raw_args
wenzelm@35412
   204
  in type_syntax false (ProofContext.target_type_notation add mode args) lthy end;
wenzelm@35412
   205
wenzelm@24949
   206
fun notation add mode raw_args lthy =
wenzelm@21743
   207
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@33456
   208
  in term_syntax false (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   209
wenzelm@21664
   210
wenzelm@28395
   211
(* init *)
wenzelm@20910
   212
wenzelm@33724
   213
fun init group theory_prefix operations target =
wenzelm@33166
   214
  let val naming =
wenzelm@33166
   215
    Sign.naming_of (ProofContext.theory_of target)
wenzelm@33724
   216
    |> Name_Space.set_group group
wenzelm@33166
   217
    |> Name_Space.mandatory_path theory_prefix;
wenzelm@33166
   218
  in
wenzelm@33166
   219
    target |> Data.map
wenzelm@33166
   220
      (fn NONE => SOME (make_lthy (naming, theory_prefix, operations, target))
wenzelm@33166
   221
        | SOME _ => error "Local theory already initialized")
wenzelm@33166
   222
    |> checkpoint
wenzelm@33166
   223
  end;
wenzelm@20910
   224
wenzelm@21273
   225
fun restore lthy =
wenzelm@33724
   226
  let val {naming, theory_prefix, operations, target} = get_lthy lthy
wenzelm@33724
   227
  in init (Name_Space.get_group naming) theory_prefix operations target end;
wenzelm@20910
   228
wenzelm@28379
   229
val reinit = checkpoint o operation #reinit;
wenzelm@28395
   230
wenzelm@28395
   231
wenzelm@28395
   232
(* exit *)
wenzelm@28395
   233
wenzelm@28379
   234
val exit = ProofContext.theory Theory.checkpoint o operation #exit;
wenzelm@28395
   235
val exit_global = ProofContext.theory_of o exit;
wenzelm@28395
   236
wenzelm@28395
   237
fun exit_result f (x, lthy) =
wenzelm@28395
   238
  let
wenzelm@28395
   239
    val ctxt = exit lthy;
wenzelm@33281
   240
    val phi = standard_morphism lthy ctxt;
wenzelm@28395
   241
  in (f phi x, ctxt) end;
wenzelm@28395
   242
wenzelm@28395
   243
fun exit_result_global f (x, lthy) =
wenzelm@28395
   244
  let
wenzelm@28395
   245
    val thy = exit_global lthy;
wenzelm@28395
   246
    val thy_ctxt = ProofContext.init thy;
wenzelm@33281
   247
    val phi = standard_morphism lthy thy_ctxt;
wenzelm@28395
   248
  in (f phi x, thy) end;
wenzelm@20910
   249
wenzelm@18781
   250
end;