src/Pure/Isar/local_theory.ML
author wenzelm
Mon Sep 29 10:58:03 2008 +0200 (2008-09-29 ago)
changeset 28395 984fcc8feb24
parent 28379 0de8a35b866a
child 28406 daeb21fec18f
permissions -rw-r--r--
added exit_global, exit_result, exit_result_global;
ProofContext.norm_export_morphism;
wenzelm@18744
     1
(*  Title:      Pure/Isar/local_theory.ML
wenzelm@18744
     2
    ID:         $Id$
wenzelm@18744
     3
    Author:     Makarius
wenzelm@18744
     4
wenzelm@20888
     5
Local theory operations, with abstract target context.
wenzelm@18744
     6
*)
wenzelm@18744
     7
wenzelm@18951
     8
type local_theory = Proof.context;
wenzelm@18951
     9
wenzelm@18744
    10
signature LOCAL_THEORY =
wenzelm@18744
    11
sig
wenzelm@20888
    12
  type operations
wenzelm@26131
    13
  val group_of: local_theory -> string
wenzelm@28017
    14
  val group_properties_of: local_theory -> Properties.T
wenzelm@28017
    15
  val group_position_of: local_theory -> Properties.T
wenzelm@26131
    16
  val set_group: string -> local_theory -> local_theory
wenzelm@20888
    17
  val target_of: local_theory -> Proof.context
wenzelm@20960
    18
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    19
  val raw_theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@28379
    20
  val checkpoint: local_theory -> local_theory
wenzelm@22240
    21
  val full_naming: local_theory -> NameSpace.naming
wenzelm@21614
    22
  val full_name: local_theory -> bstring -> string
wenzelm@20960
    23
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    24
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    25
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    26
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21860
    27
  val affirm: local_theory -> local_theory
wenzelm@20960
    28
  val pretty: local_theory -> Pretty.T list
wenzelm@28083
    29
  val abbrev: Syntax.mode -> (Name.binding * mixfix) * term -> local_theory ->
wenzelm@25120
    30
    (term * term) * local_theory
wenzelm@28084
    31
  val define: string -> (Name.binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    32
    (term * (string * thm)) * local_theory
wenzelm@28084
    33
  val note: string -> Attrib.binding * thm list -> local_theory -> (string * thm list) * local_theory
wenzelm@28084
    34
  val notes: string -> (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    35
    local_theory -> (string * thm list) list * local_theory
wenzelm@24020
    36
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    37
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    38
  val declaration: declaration -> local_theory -> local_theory
wenzelm@24949
    39
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    40
  val target_morphism: local_theory -> morphism
wenzelm@21529
    41
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    42
  val restore: local_theory -> local_theory
wenzelm@25289
    43
  val reinit: local_theory -> local_theory
wenzelm@21292
    44
  val exit: local_theory -> Proof.context
wenzelm@28395
    45
  val exit_global: local_theory -> theory
wenzelm@28395
    46
  val exit_result: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * Proof.context
wenzelm@28395
    47
  val exit_result_global: (morphism -> 'a -> 'b) -> 'a * local_theory -> 'b * theory
wenzelm@18744
    48
end;
wenzelm@18744
    49
wenzelm@18744
    50
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    51
struct
wenzelm@18744
    52
wenzelm@20888
    53
(** local theory data **)
wenzelm@19059
    54
wenzelm@20888
    55
(* type lthy *)
wenzelm@20888
    56
wenzelm@20888
    57
type operations =
wenzelm@20960
    58
 {pretty: local_theory -> Pretty.T list,
wenzelm@28084
    59
  abbrev: Syntax.mode -> (Name.binding * mixfix) * term -> local_theory ->
wenzelm@28084
    60
    (term * term) * local_theory,
wenzelm@26131
    61
  define: string ->
wenzelm@28084
    62
    (Name.binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    63
    (term * (string * thm)) * local_theory,
wenzelm@26131
    64
  notes: string ->
wenzelm@28084
    65
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    66
    local_theory -> (string * thm list) list * local_theory,
wenzelm@24020
    67
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    68
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    69
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    70
  reinit: local_theory -> local_theory,
wenzelm@21292
    71
  exit: local_theory -> Proof.context};
wenzelm@20888
    72
wenzelm@20888
    73
datatype lthy = LThy of
wenzelm@26131
    74
 {group: string,
wenzelm@26131
    75
  theory_prefix: string,
wenzelm@20888
    76
  operations: operations,
wenzelm@20888
    77
  target: Proof.context};
wenzelm@20888
    78
wenzelm@26131
    79
fun make_lthy (group, theory_prefix, operations, target) =
wenzelm@26131
    80
  LThy {group = group, theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    81
wenzelm@20888
    82
wenzelm@20888
    83
(* context data *)
wenzelm@18744
    84
wenzelm@18744
    85
structure Data = ProofDataFun
wenzelm@18744
    86
(
wenzelm@20888
    87
  type T = lthy option;
wenzelm@20888
    88
  fun init _ = NONE;
wenzelm@18744
    89
);
wenzelm@18744
    90
wenzelm@20888
    91
fun get_lthy lthy =
wenzelm@20888
    92
  (case Data.get lthy of
wenzelm@20888
    93
    NONE => error "No local theory context"
wenzelm@20888
    94
  | SOME (LThy data) => data);
wenzelm@18951
    95
wenzelm@20888
    96
fun map_lthy f lthy =
wenzelm@26131
    97
  let val {group, theory_prefix, operations, target} = get_lthy lthy
wenzelm@26131
    98
  in Data.put (SOME (make_lthy (f (group, theory_prefix, operations, target)))) lthy end;
wenzelm@26131
    99
wenzelm@26131
   100
wenzelm@26131
   101
(* group *)
wenzelm@26131
   102
wenzelm@26131
   103
val group_of = #group o get_lthy;
wenzelm@26131
   104
wenzelm@26131
   105
fun group_properties_of lthy =
wenzelm@26131
   106
  (case group_of lthy of
wenzelm@26131
   107
    "" => []
wenzelm@26131
   108
  | group => [(Markup.groupN, group)]);
wenzelm@26131
   109
wenzelm@26131
   110
fun group_position_of lthy =
wenzelm@26131
   111
  group_properties_of lthy @ Position.properties_of (Position.thread_data ());
wenzelm@26131
   112
wenzelm@26131
   113
fun set_group group = map_lthy (fn (_, theory_prefix, operations, target) =>
wenzelm@26131
   114
  (group, theory_prefix, operations, target));
wenzelm@26131
   115
wenzelm@26131
   116
wenzelm@26131
   117
(* target *)
wenzelm@18767
   118
wenzelm@20888
   119
val target_of = #target o get_lthy;
wenzelm@21860
   120
val affirm = tap target_of;
wenzelm@18767
   121
wenzelm@26131
   122
fun map_target f = map_lthy (fn (group, theory_prefix, operations, target) =>
wenzelm@26131
   123
  (group, theory_prefix, operations, f target));
wenzelm@18767
   124
wenzelm@18767
   125
wenzelm@20888
   126
(* substructure mappings *)
wenzelm@19661
   127
wenzelm@20960
   128
fun raw_theory_result f lthy =
wenzelm@20960
   129
  let
wenzelm@20960
   130
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   131
    val lthy' = lthy
wenzelm@20960
   132
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   133
      |> ProofContext.transfer thy';
wenzelm@20960
   134
  in (res, lthy') end;
wenzelm@20960
   135
wenzelm@20960
   136
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   137
wenzelm@28379
   138
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   139
wenzelm@22240
   140
fun full_naming lthy =
wenzelm@22240
   141
  Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@22240
   142
  |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@22240
   143
  |> NameSpace.qualified_names;
wenzelm@22240
   144
wenzelm@22240
   145
val full_name = NameSpace.full o full_naming;
wenzelm@21614
   146
wenzelm@21529
   147
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   148
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   149
  |> Sign.qualified_names
wenzelm@21529
   150
  |> f
wenzelm@21529
   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@26131
   157
    val (res, ctxt') = f (target_of 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;