src/Pure/Isar/local_theory.ML
author wenzelm
Thu Mar 12 13:18:42 2009 +0100 (2009-03-12 ago)
changeset 30469 de9e8f1d927c
parent 30438 c2d49315b93b
child 30578 9863745880db
permissions -rw-r--r--
renamed sticky_prefix to mandatory_path;
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@26131
    12
  val group_of: local_theory -> string
wenzelm@28017
    13
  val group_properties_of: local_theory -> Properties.T
wenzelm@28017
    14
  val group_position_of: local_theory -> Properties.T
wenzelm@26131
    15
  val set_group: string -> local_theory -> local_theory
wenzelm@20888
    16
  val target_of: local_theory -> Proof.context
wenzelm@20960
    17
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    18
  val raw_theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@28379
    19
  val checkpoint: local_theory -> local_theory
wenzelm@22240
    20
  val full_naming: local_theory -> NameSpace.naming
haftmann@29581
    21
  val full_name: local_theory -> binding -> string
wenzelm@20960
    22
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    23
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    24
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    25
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21860
    26
  val affirm: local_theory -> local_theory
wenzelm@20960
    27
  val pretty: local_theory -> Pretty.T list
haftmann@29581
    28
  val abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@25120
    29
    (term * term) * local_theory
haftmann@29581
    30
  val define: string -> (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    31
    (term * (string * thm)) * local_theory
wenzelm@28084
    32
  val note: string -> Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
wenzelm@28084
    33
  val notes: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    34
    local_theory -> (string * thm list) list * local_theory
wenzelm@24020
    35
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    36
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    37
  val declaration: declaration -> local_theory -> local_theory
wenzelm@24949
    38
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    39
  val target_morphism: local_theory -> morphism
wenzelm@21529
    40
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    41
  val restore: local_theory -> local_theory
wenzelm@25289
    42
  val reinit: local_theory -> local_theory
wenzelm@21292
    43
  val exit: local_theory -> Proof.context
wenzelm@28395
    44
  val exit_global: local_theory -> theory
wenzelm@28395
    45
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    46
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    47
end;
wenzelm@18744
    48
wenzelm@18744
    49
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    50
struct
wenzelm@18744
    51
wenzelm@20888
    52
(** local theory data **)
wenzelm@19059
    53
wenzelm@20888
    54
(* type lthy *)
wenzelm@20888
    55
wenzelm@20888
    56
type operations =
wenzelm@20960
    57
 {pretty: local_theory -> Pretty.T list,
haftmann@29581
    58
  abbrev: Syntax.mode -> (binding * mixfix) * term -> local_theory ->
wenzelm@28084
    59
    (term * term) * local_theory,
wenzelm@26131
    60
  define: string ->
haftmann@29581
    61
    (binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    62
    (term * (string * thm)) * local_theory,
wenzelm@26131
    63
  notes: string ->
wenzelm@28084
    64
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    65
    local_theory -> (string * thm list) list * local_theory,
wenzelm@24020
    66
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    67
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    68
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    69
  reinit: local_theory -> local_theory,
wenzelm@21292
    70
  exit: local_theory -> Proof.context};
wenzelm@20888
    71
wenzelm@20888
    72
datatype lthy = LThy of
wenzelm@26131
    73
 {group: string,
wenzelm@26131
    74
  theory_prefix: string,
wenzelm@20888
    75
  operations: operations,
wenzelm@20888
    76
  target: Proof.context};
wenzelm@20888
    77
wenzelm@26131
    78
fun make_lthy (group, theory_prefix, operations, target) =
wenzelm@26131
    79
  LThy {group = group, theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    80
wenzelm@20888
    81
wenzelm@20888
    82
(* context data *)
wenzelm@18744
    83
wenzelm@18744
    84
structure Data = ProofDataFun
wenzelm@18744
    85
(
wenzelm@20888
    86
  type T = lthy option;
wenzelm@20888
    87
  fun init _ = NONE;
wenzelm@18744
    88
);
wenzelm@18744
    89
wenzelm@20888
    90
fun get_lthy lthy =
wenzelm@20888
    91
  (case Data.get lthy of
wenzelm@20888
    92
    NONE => error "No local theory context"
wenzelm@20888
    93
  | SOME (LThy data) => data);
wenzelm@18951
    94
wenzelm@20888
    95
fun map_lthy f lthy =
wenzelm@26131
    96
  let val {group, theory_prefix, operations, target} = get_lthy lthy
wenzelm@26131
    97
  in Data.put (SOME (make_lthy (f (group, theory_prefix, operations, target)))) lthy end;
wenzelm@26131
    98
wenzelm@26131
    99
wenzelm@26131
   100
(* group *)
wenzelm@26131
   101
wenzelm@26131
   102
val group_of = #group o get_lthy;
wenzelm@26131
   103
wenzelm@26131
   104
fun group_properties_of lthy =
wenzelm@26131
   105
  (case group_of lthy of
wenzelm@26131
   106
    "" => []
wenzelm@26131
   107
  | group => [(Markup.groupN, group)]);
wenzelm@26131
   108
wenzelm@26131
   109
fun group_position_of lthy =
wenzelm@26131
   110
  group_properties_of lthy @ Position.properties_of (Position.thread_data ());
wenzelm@26131
   111
wenzelm@26131
   112
fun set_group group = map_lthy (fn (_, theory_prefix, operations, target) =>
wenzelm@26131
   113
  (group, theory_prefix, operations, target));
wenzelm@26131
   114
wenzelm@26131
   115
wenzelm@26131
   116
(* target *)
wenzelm@18767
   117
wenzelm@20888
   118
val target_of = #target o get_lthy;
wenzelm@21860
   119
val affirm = tap target_of;
wenzelm@18767
   120
wenzelm@26131
   121
fun map_target f = map_lthy (fn (group, theory_prefix, operations, target) =>
wenzelm@26131
   122
  (group, theory_prefix, operations, f target));
wenzelm@18767
   123
wenzelm@18767
   124
wenzelm@20888
   125
(* substructure mappings *)
wenzelm@19661
   126
wenzelm@20960
   127
fun raw_theory_result f lthy =
wenzelm@20960
   128
  let
wenzelm@20960
   129
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   130
    val lthy' = lthy
wenzelm@20960
   131
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   132
      |> ProofContext.transfer thy';
wenzelm@20960
   133
  in (res, lthy') end;
wenzelm@20960
   134
wenzelm@20960
   135
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   136
wenzelm@28379
   137
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   138
wenzelm@22240
   139
fun full_naming lthy =
wenzelm@22240
   140
  Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@30469
   141
  |> NameSpace.mandatory_path (#theory_prefix (get_lthy lthy));
wenzelm@22240
   142
haftmann@28991
   143
fun full_name naming = NameSpace.full_name (full_naming naming);
wenzelm@21614
   144
wenzelm@21529
   145
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@30469
   146
  |> Sign.mandatory_path (#theory_prefix (get_lthy lthy))
wenzelm@21529
   147
  |> f
wenzelm@21529
   148
  ||> Sign.restore_naming thy);
wenzelm@19661
   149
wenzelm@20888
   150
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   151
wenzelm@20888
   152
fun target_result f lthy =
wenzelm@18744
   153
  let
wenzelm@28406
   154
    val (res, ctxt') = target_of lthy
wenzelm@28406
   155
      |> ContextPosition.set_visible false
wenzelm@28406
   156
      |> f
wenzelm@28406
   157
      ||> ContextPosition.restore_visible lthy;
wenzelm@20888
   158
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   159
    val lthy' = lthy
wenzelm@20888
   160
      |> map_target (K ctxt')
wenzelm@20888
   161
      |> ProofContext.transfer thy';
wenzelm@20888
   162
  in (res, lthy') end;
wenzelm@18876
   163
wenzelm@20888
   164
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   165
wenzelm@18767
   166
wenzelm@21664
   167
(* basic operations *)
wenzelm@18781
   168
wenzelm@20888
   169
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   170
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   171
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   172
wenzelm@20888
   173
val pretty = operation #pretty;
wenzelm@28379
   174
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@28379
   175
val define = apsnd checkpoint ooo operation2 #define;
wenzelm@28379
   176
val notes = apsnd checkpoint ooo operation2 #notes;
wenzelm@28379
   177
val type_syntax = checkpoint oo operation1 #type_syntax;
wenzelm@28379
   178
val term_syntax = checkpoint oo operation1 #term_syntax;
wenzelm@28379
   179
val declaration = checkpoint oo operation1 #declaration;
wenzelm@18781
   180
wenzelm@26131
   181
fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   182
wenzelm@24933
   183
fun target_morphism lthy =
wenzelm@28395
   184
  ProofContext.norm_export_morphism lthy (target_of lthy);
wenzelm@24933
   185
wenzelm@24949
   186
fun notation add mode raw_args lthy =
wenzelm@21743
   187
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   188
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   189
wenzelm@21664
   190
wenzelm@28395
   191
(* init *)
wenzelm@20910
   192
wenzelm@20910
   193
fun init theory_prefix operations target = target |> Data.map
wenzelm@26131
   194
  (fn NONE => SOME (make_lthy ("", theory_prefix, operations, target))
wenzelm@28379
   195
    | SOME _ => error "Local theory already initialized")
wenzelm@28379
   196
  |> checkpoint;
wenzelm@20910
   197
wenzelm@21273
   198
fun restore lthy =
wenzelm@26131
   199
  let val {group = _, theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   200
  in init theory_prefix operations target end;
wenzelm@20910
   201
wenzelm@28379
   202
val reinit = checkpoint o operation #reinit;
wenzelm@28395
   203
wenzelm@28395
   204
wenzelm@28395
   205
(* exit *)
wenzelm@28395
   206
wenzelm@28379
   207
val exit = ProofContext.theory Theory.checkpoint o operation #exit;
wenzelm@28395
   208
val exit_global = ProofContext.theory_of o exit;
wenzelm@28395
   209
wenzelm@28395
   210
fun exit_result f (x, lthy) =
wenzelm@28395
   211
  let
wenzelm@28395
   212
    val ctxt = exit lthy;
wenzelm@28395
   213
    val phi = ProofContext.norm_export_morphism lthy ctxt;
wenzelm@28395
   214
  in (f phi x, ctxt) end;
wenzelm@28395
   215
wenzelm@28395
   216
fun exit_result_global f (x, lthy) =
wenzelm@28395
   217
  let
wenzelm@28395
   218
    val thy = exit_global lthy;
wenzelm@28395
   219
    val thy_ctxt = ProofContext.init thy;
wenzelm@28395
   220
    val phi = ProofContext.norm_export_morphism lthy thy_ctxt;
wenzelm@28395
   221
  in (f phi x, thy) end;
wenzelm@20910
   222
wenzelm@18781
   223
end;