src/Pure/Isar/local_theory.ML
author wenzelm
Sat Sep 27 15:20:39 2008 +0200 (2008-09-27 ago)
changeset 28379 0de8a35b866a
parent 28115 cd0d170d4dc6
child 28395 984fcc8feb24
permissions -rw-r--r--
Theory.checkpoint for main operations, admits concurrent proofs;
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@18744
    45
end;
wenzelm@18744
    46
wenzelm@18744
    47
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    48
struct
wenzelm@18744
    49
wenzelm@20888
    50
(** local theory data **)
wenzelm@19059
    51
wenzelm@20888
    52
(* type lthy *)
wenzelm@20888
    53
wenzelm@20888
    54
type operations =
wenzelm@20960
    55
 {pretty: local_theory -> Pretty.T list,
wenzelm@28084
    56
  abbrev: Syntax.mode -> (Name.binding * mixfix) * term -> local_theory ->
wenzelm@28084
    57
    (term * term) * local_theory,
wenzelm@26131
    58
  define: string ->
wenzelm@28084
    59
    (Name.binding * mixfix) * (Attrib.binding * term) -> local_theory ->
wenzelm@28083
    60
    (term * (string * thm)) * local_theory,
wenzelm@26131
    61
  notes: string ->
wenzelm@28084
    62
    (Attrib.binding * (thm list * Attrib.src list) list) list ->
wenzelm@28083
    63
    local_theory -> (string * thm list) list * local_theory,
wenzelm@24020
    64
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    65
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    66
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    67
  reinit: local_theory -> local_theory,
wenzelm@21292
    68
  exit: local_theory -> Proof.context};
wenzelm@20888
    69
wenzelm@20888
    70
datatype lthy = LThy of
wenzelm@26131
    71
 {group: string,
wenzelm@26131
    72
  theory_prefix: string,
wenzelm@20888
    73
  operations: operations,
wenzelm@20888
    74
  target: Proof.context};
wenzelm@20888
    75
wenzelm@26131
    76
fun make_lthy (group, theory_prefix, operations, target) =
wenzelm@26131
    77
  LThy {group = group, theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    78
wenzelm@20888
    79
wenzelm@20888
    80
(* context data *)
wenzelm@18744
    81
wenzelm@18744
    82
structure Data = ProofDataFun
wenzelm@18744
    83
(
wenzelm@20888
    84
  type T = lthy option;
wenzelm@20888
    85
  fun init _ = NONE;
wenzelm@18744
    86
);
wenzelm@18744
    87
wenzelm@20888
    88
fun get_lthy lthy =
wenzelm@20888
    89
  (case Data.get lthy of
wenzelm@20888
    90
    NONE => error "No local theory context"
wenzelm@20888
    91
  | SOME (LThy data) => data);
wenzelm@18951
    92
wenzelm@20888
    93
fun map_lthy f lthy =
wenzelm@26131
    94
  let val {group, theory_prefix, operations, target} = get_lthy lthy
wenzelm@26131
    95
  in Data.put (SOME (make_lthy (f (group, theory_prefix, operations, target)))) lthy end;
wenzelm@26131
    96
wenzelm@26131
    97
wenzelm@26131
    98
(* group *)
wenzelm@26131
    99
wenzelm@26131
   100
val group_of = #group o get_lthy;
wenzelm@26131
   101
wenzelm@26131
   102
fun group_properties_of lthy =
wenzelm@26131
   103
  (case group_of lthy of
wenzelm@26131
   104
    "" => []
wenzelm@26131
   105
  | group => [(Markup.groupN, group)]);
wenzelm@26131
   106
wenzelm@26131
   107
fun group_position_of lthy =
wenzelm@26131
   108
  group_properties_of lthy @ Position.properties_of (Position.thread_data ());
wenzelm@26131
   109
wenzelm@26131
   110
fun set_group group = map_lthy (fn (_, theory_prefix, operations, target) =>
wenzelm@26131
   111
  (group, theory_prefix, operations, target));
wenzelm@26131
   112
wenzelm@26131
   113
wenzelm@26131
   114
(* target *)
wenzelm@18767
   115
wenzelm@20888
   116
val target_of = #target o get_lthy;
wenzelm@21860
   117
val affirm = tap target_of;
wenzelm@18767
   118
wenzelm@26131
   119
fun map_target f = map_lthy (fn (group, theory_prefix, operations, target) =>
wenzelm@26131
   120
  (group, theory_prefix, operations, f target));
wenzelm@18767
   121
wenzelm@18767
   122
wenzelm@20888
   123
(* substructure mappings *)
wenzelm@19661
   124
wenzelm@20960
   125
fun raw_theory_result f lthy =
wenzelm@20960
   126
  let
wenzelm@20960
   127
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   128
    val lthy' = lthy
wenzelm@20960
   129
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   130
      |> ProofContext.transfer thy';
wenzelm@20960
   131
  in (res, lthy') end;
wenzelm@20960
   132
wenzelm@20960
   133
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   134
wenzelm@28379
   135
val checkpoint = raw_theory Theory.checkpoint;
wenzelm@28379
   136
wenzelm@22240
   137
fun full_naming lthy =
wenzelm@22240
   138
  Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@22240
   139
  |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@22240
   140
  |> NameSpace.qualified_names;
wenzelm@22240
   141
wenzelm@22240
   142
val full_name = NameSpace.full o full_naming;
wenzelm@21614
   143
wenzelm@21529
   144
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   145
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   146
  |> Sign.qualified_names
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@26131
   154
    val (res, ctxt') = f (target_of lthy);
wenzelm@20888
   155
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   156
    val lthy' = lthy
wenzelm@20888
   157
      |> map_target (K ctxt')
wenzelm@20888
   158
      |> ProofContext.transfer thy';
wenzelm@20888
   159
  in (res, lthy') end;
wenzelm@18876
   160
wenzelm@20888
   161
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   162
wenzelm@18767
   163
wenzelm@21664
   164
(* basic operations *)
wenzelm@18781
   165
wenzelm@20888
   166
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   167
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@26131
   168
fun operation2 f x y lthy = operation (fn ops => f ops x y) lthy;
wenzelm@18781
   169
wenzelm@20888
   170
val pretty = operation #pretty;
wenzelm@28379
   171
val abbrev = apsnd checkpoint ooo operation2 #abbrev;
wenzelm@28379
   172
val define = apsnd checkpoint ooo operation2 #define;
wenzelm@28379
   173
val notes = apsnd checkpoint ooo operation2 #notes;
wenzelm@28379
   174
val type_syntax = checkpoint oo operation1 #type_syntax;
wenzelm@28379
   175
val term_syntax = checkpoint oo operation1 #term_syntax;
wenzelm@28379
   176
val declaration = checkpoint oo operation1 #declaration;
wenzelm@18781
   177
wenzelm@26131
   178
fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   179
wenzelm@24933
   180
fun target_morphism lthy =
wenzelm@24933
   181
  ProofContext.export_morphism lthy (target_of lthy) $>
wenzelm@24933
   182
  Morphism.thm_morphism Goal.norm_result;
wenzelm@24933
   183
wenzelm@24949
   184
fun notation add mode raw_args lthy =
wenzelm@21743
   185
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   186
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   187
wenzelm@21664
   188
wenzelm@20910
   189
(* init -- exit *)
wenzelm@20910
   190
wenzelm@20910
   191
fun init theory_prefix operations target = target |> Data.map
wenzelm@26131
   192
  (fn NONE => SOME (make_lthy ("", theory_prefix, operations, target))
wenzelm@28379
   193
    | SOME _ => error "Local theory already initialized")
wenzelm@28379
   194
  |> checkpoint;
wenzelm@20910
   195
wenzelm@21273
   196
fun restore lthy =
wenzelm@26131
   197
  let val {group = _, theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   198
  in init theory_prefix operations target end;
wenzelm@20910
   199
wenzelm@28379
   200
val reinit = checkpoint o operation #reinit;
wenzelm@28379
   201
val exit = ProofContext.theory Theory.checkpoint o operation #exit;
wenzelm@20910
   202
wenzelm@18781
   203
end;