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