src/Pure/Isar/local_theory.ML
author wenzelm
Wed Aug 27 11:48:54 2008 +0200 (2008-08-27 ago)
changeset 28017 4919bd124a58
parent 26131 91024979b9eb
child 28083 103d9282a946
permissions -rw-r--r--
type Properties.T;
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@22240
    20
  val full_naming: local_theory -> NameSpace.naming
wenzelm@21614
    21
  val full_name: local_theory -> bstring -> 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
wenzelm@24984
    28
  val axioms: string ->
wenzelm@24984
    29
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    30
    -> (term list * (bstring * thm list) list) * local_theory
wenzelm@21802
    31
  val abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory ->
wenzelm@25120
    32
    (term * term) * local_theory
wenzelm@25021
    33
  val define: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    34
    (term * (bstring * thm)) * local_theory
wenzelm@25984
    35
  val note: string -> (bstring * Attrib.src list) * thm list ->
wenzelm@25984
    36
    local_theory -> (bstring * thm list) * local_theory
wenzelm@21433
    37
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    38
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@24020
    39
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    40
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    41
  val declaration: declaration -> local_theory -> local_theory
wenzelm@24949
    42
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    43
  val target_morphism: local_theory -> morphism
wenzelm@21529
    44
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    45
  val restore: local_theory -> local_theory
wenzelm@25289
    46
  val reinit: local_theory -> local_theory
wenzelm@21292
    47
  val exit: local_theory -> Proof.context
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@26131
    59
  axioms: string ->
wenzelm@24984
    60
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    61
    -> (term list * (bstring * thm list) list) * local_theory,
wenzelm@25120
    62
  abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> (term * term) * local_theory,
wenzelm@26131
    63
  define: string ->
wenzelm@25984
    64
    (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    65
    (term * (bstring * thm)) * local_theory,
wenzelm@26131
    66
  notes: string ->
wenzelm@21433
    67
    ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@21433
    68
    local_theory -> (bstring * thm list) list * local_theory,
wenzelm@24020
    69
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    70
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    71
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    72
  reinit: local_theory -> local_theory,
wenzelm@21292
    73
  exit: local_theory -> Proof.context};
wenzelm@20888
    74
wenzelm@20888
    75
datatype lthy = LThy of
wenzelm@26131
    76
 {group: string,
wenzelm@26131
    77
  theory_prefix: string,
wenzelm@20888
    78
  operations: operations,
wenzelm@20888
    79
  target: Proof.context};
wenzelm@20888
    80
wenzelm@26131
    81
fun make_lthy (group, theory_prefix, operations, target) =
wenzelm@26131
    82
  LThy {group = group, theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    83
wenzelm@20888
    84
wenzelm@20888
    85
(* context data *)
wenzelm@18744
    86
wenzelm@18744
    87
structure Data = ProofDataFun
wenzelm@18744
    88
(
wenzelm@20888
    89
  type T = lthy option;
wenzelm@20888
    90
  fun init _ = NONE;
wenzelm@18744
    91
);
wenzelm@18744
    92
wenzelm@20888
    93
fun get_lthy lthy =
wenzelm@20888
    94
  (case Data.get lthy of
wenzelm@20888
    95
    NONE => error "No local theory context"
wenzelm@20888
    96
  | SOME (LThy data) => data);
wenzelm@18951
    97
wenzelm@20888
    98
fun map_lthy f lthy =
wenzelm@26131
    99
  let val {group, theory_prefix, operations, target} = get_lthy lthy
wenzelm@26131
   100
  in Data.put (SOME (make_lthy (f (group, theory_prefix, operations, target)))) lthy end;
wenzelm@26131
   101
wenzelm@26131
   102
wenzelm@26131
   103
(* group *)
wenzelm@26131
   104
wenzelm@26131
   105
val group_of = #group o get_lthy;
wenzelm@26131
   106
wenzelm@26131
   107
fun group_properties_of lthy =
wenzelm@26131
   108
  (case group_of lthy of
wenzelm@26131
   109
    "" => []
wenzelm@26131
   110
  | group => [(Markup.groupN, group)]);
wenzelm@26131
   111
wenzelm@26131
   112
fun group_position_of lthy =
wenzelm@26131
   113
  group_properties_of lthy @ Position.properties_of (Position.thread_data ());
wenzelm@26131
   114
wenzelm@26131
   115
fun set_group group = map_lthy (fn (_, theory_prefix, operations, target) =>
wenzelm@26131
   116
  (group, theory_prefix, operations, target));
wenzelm@26131
   117
wenzelm@26131
   118
wenzelm@26131
   119
(* target *)
wenzelm@18767
   120
wenzelm@20888
   121
val target_of = #target o get_lthy;
wenzelm@21860
   122
val affirm = tap target_of;
wenzelm@18767
   123
wenzelm@26131
   124
fun map_target f = map_lthy (fn (group, theory_prefix, operations, target) =>
wenzelm@26131
   125
  (group, theory_prefix, operations, f target));
wenzelm@18767
   126
wenzelm@18767
   127
wenzelm@20888
   128
(* substructure mappings *)
wenzelm@19661
   129
wenzelm@20960
   130
fun raw_theory_result f lthy =
wenzelm@20960
   131
  let
wenzelm@20960
   132
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   133
    val lthy' = lthy
wenzelm@20960
   134
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   135
      |> ProofContext.transfer thy';
wenzelm@20960
   136
  in (res, lthy') end;
wenzelm@20960
   137
wenzelm@20960
   138
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   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@26131
   174
val axioms = operation2 #axioms;
wenzelm@21743
   175
val abbrev = operation2 #abbrev;
wenzelm@26131
   176
val define = operation2 #define;
wenzelm@26131
   177
val notes = operation2 #notes;
wenzelm@21498
   178
val type_syntax = operation1 #type_syntax;
wenzelm@20888
   179
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   180
val declaration = operation1 #declaration;
wenzelm@18781
   181
wenzelm@26131
   182
fun note kind (a, ths) = notes kind [(a, [(ths, [])])] #>> the_single;
wenzelm@18781
   183
wenzelm@24933
   184
fun target_morphism lthy =
wenzelm@24933
   185
  ProofContext.export_morphism lthy (target_of lthy) $>
wenzelm@24933
   186
  Morphism.thm_morphism Goal.norm_result;
wenzelm@24933
   187
wenzelm@24949
   188
fun notation add mode raw_args lthy =
wenzelm@21743
   189
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   190
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   191
wenzelm@21664
   192
wenzelm@20910
   193
(* init -- exit *)
wenzelm@20910
   194
wenzelm@20910
   195
fun init theory_prefix operations target = target |> Data.map
wenzelm@26131
   196
  (fn NONE => SOME (make_lthy ("", theory_prefix, operations, target))
wenzelm@20910
   197
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   198
wenzelm@21273
   199
fun restore lthy =
wenzelm@26131
   200
  let val {group = _, theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   201
  in init theory_prefix operations target end;
wenzelm@20910
   202
wenzelm@21292
   203
val reinit = operation #reinit;
wenzelm@21292
   204
val exit = operation #exit;
wenzelm@20910
   205
wenzelm@18781
   206
end;