src/Pure/Isar/local_theory.ML
author wenzelm
Sat Jan 26 17:08:43 2008 +0100 (2008-01-26 ago)
changeset 25984 da0399c9ffcb
parent 25381 c100bf5bd6b8
child 26131 91024979b9eb
permissions -rw-r--r--
grouped versions of axioms/define/notes;
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@20888
    13
  val target_of: local_theory -> Proof.context
wenzelm@20960
    14
  val raw_theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@20960
    15
  val raw_theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@22240
    16
  val full_naming: local_theory -> NameSpace.naming
wenzelm@21614
    17
  val full_name: local_theory -> bstring -> string
wenzelm@20960
    18
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    19
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    20
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    21
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21860
    22
  val affirm: local_theory -> local_theory
wenzelm@20960
    23
  val pretty: local_theory -> Pretty.T list
wenzelm@24984
    24
  val axioms: string ->
wenzelm@24984
    25
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    26
    -> (term list * (bstring * thm list) list) * local_theory
wenzelm@25984
    27
  val axioms_grouped: string -> string ->
wenzelm@25984
    28
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@25984
    29
    -> (term list * (bstring * thm list) list) * local_theory
wenzelm@21802
    30
  val abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory ->
wenzelm@25120
    31
    (term * term) * local_theory
wenzelm@25021
    32
  val define: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    33
    (term * (bstring * thm)) * local_theory
wenzelm@25984
    34
  val define_grouped: string -> string ->
wenzelm@25984
    35
    (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25984
    36
    (term * (bstring * thm)) * local_theory
wenzelm@25984
    37
  val note: string -> (bstring * Attrib.src list) * thm list ->
wenzelm@25984
    38
    local_theory -> (bstring * thm list) * local_theory
wenzelm@21433
    39
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    40
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@25984
    41
  val note_grouped: string -> string ->
wenzelm@25984
    42
    (bstring * Attrib.src list) * thm list ->
wenzelm@25984
    43
    local_theory -> (bstring * thm list) * local_theory
wenzelm@25984
    44
  val notes_grouped: string -> string ->
wenzelm@25984
    45
    ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@25984
    46
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@24020
    47
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    48
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    49
  val declaration: declaration -> local_theory -> local_theory
wenzelm@24949
    50
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21529
    51
  val target_morphism: local_theory -> morphism
wenzelm@21529
    52
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    53
  val restore: local_theory -> local_theory
wenzelm@25289
    54
  val reinit: local_theory -> local_theory
wenzelm@21292
    55
  val exit: local_theory -> Proof.context
wenzelm@18744
    56
end;
wenzelm@18744
    57
wenzelm@18744
    58
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    59
struct
wenzelm@18744
    60
wenzelm@20888
    61
(** local theory data **)
wenzelm@19059
    62
wenzelm@20888
    63
(* type lthy *)
wenzelm@20888
    64
wenzelm@20888
    65
type operations =
wenzelm@20960
    66
 {pretty: local_theory -> Pretty.T list,
wenzelm@25984
    67
  axioms: string -> string ->
wenzelm@24984
    68
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    69
    -> (term list * (bstring * thm list) list) * local_theory,
wenzelm@25120
    70
  abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> (term * term) * local_theory,
wenzelm@25984
    71
  define: string -> string ->
wenzelm@25984
    72
    (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    73
    (term * (bstring * thm)) * local_theory,
wenzelm@25984
    74
  notes: string -> string ->
wenzelm@21433
    75
    ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@21433
    76
    local_theory -> (bstring * thm list) list * local_theory,
wenzelm@24020
    77
  type_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    78
  term_syntax: declaration -> local_theory -> local_theory,
wenzelm@24020
    79
  declaration: declaration -> local_theory -> local_theory,
wenzelm@25289
    80
  reinit: local_theory -> local_theory,
wenzelm@21292
    81
  exit: local_theory -> Proof.context};
wenzelm@20888
    82
wenzelm@20888
    83
datatype lthy = LThy of
wenzelm@21529
    84
 {theory_prefix: string,
wenzelm@20888
    85
  operations: operations,
wenzelm@20888
    86
  target: Proof.context};
wenzelm@20888
    87
wenzelm@20888
    88
fun make_lthy (theory_prefix, operations, target) =
wenzelm@20888
    89
  LThy {theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    90
wenzelm@20888
    91
wenzelm@20888
    92
(* context data *)
wenzelm@18744
    93
wenzelm@18744
    94
structure Data = ProofDataFun
wenzelm@18744
    95
(
wenzelm@20888
    96
  type T = lthy option;
wenzelm@20888
    97
  fun init _ = NONE;
wenzelm@18744
    98
);
wenzelm@18744
    99
wenzelm@20888
   100
fun get_lthy lthy =
wenzelm@20888
   101
  (case Data.get lthy of
wenzelm@20888
   102
    NONE => error "No local theory context"
wenzelm@20888
   103
  | SOME (LThy data) => data);
wenzelm@18951
   104
wenzelm@20888
   105
fun map_lthy f lthy =
wenzelm@20888
   106
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
   107
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
   108
wenzelm@20888
   109
val target_of = #target o get_lthy;
wenzelm@21860
   110
val affirm = tap target_of;
wenzelm@18767
   111
wenzelm@20888
   112
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
   113
  (theory_prefix, operations, f target));
wenzelm@18767
   114
wenzelm@18767
   115
wenzelm@20888
   116
(* substructure mappings *)
wenzelm@19661
   117
wenzelm@20960
   118
fun raw_theory_result f lthy =
wenzelm@20960
   119
  let
wenzelm@20960
   120
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   121
    val lthy' = lthy
wenzelm@20960
   122
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   123
      |> ProofContext.transfer thy';
wenzelm@20960
   124
  in (res, lthy') end;
wenzelm@20960
   125
wenzelm@20960
   126
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   127
wenzelm@22240
   128
fun full_naming lthy =
wenzelm@22240
   129
  Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@22240
   130
  |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@22240
   131
  |> NameSpace.qualified_names;
wenzelm@22240
   132
wenzelm@22240
   133
val full_name = NameSpace.full o full_naming;
wenzelm@21614
   134
wenzelm@21529
   135
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   136
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   137
  |> Sign.qualified_names
wenzelm@21529
   138
  |> f
wenzelm@21529
   139
  ||> Sign.restore_naming thy);
wenzelm@19661
   140
wenzelm@20888
   141
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   142
wenzelm@20888
   143
fun target_result f lthy =
wenzelm@18744
   144
  let
wenzelm@20888
   145
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   146
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   147
    val lthy' = lthy
wenzelm@20888
   148
      |> map_target (K ctxt')
wenzelm@20888
   149
      |> ProofContext.transfer thy';
wenzelm@20888
   150
  in (res, lthy') end;
wenzelm@18876
   151
wenzelm@20888
   152
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   153
wenzelm@18767
   154
wenzelm@21664
   155
(* basic operations *)
wenzelm@18781
   156
wenzelm@20888
   157
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   158
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   159
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@25984
   160
fun operation3 f x y z = operation (fn ops => f ops x y z);
wenzelm@18781
   161
wenzelm@20888
   162
val pretty = operation #pretty;
wenzelm@25984
   163
val axioms_grouped = operation3 #axioms;
wenzelm@21743
   164
val abbrev = operation2 #abbrev;
wenzelm@25984
   165
val define_grouped = operation3 #define;
wenzelm@25984
   166
val notes_grouped = operation3 #notes;
wenzelm@21498
   167
val type_syntax = operation1 #type_syntax;
wenzelm@20888
   168
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   169
val declaration = operation1 #declaration;
wenzelm@18781
   170
wenzelm@25984
   171
fun note_grouped kind group (a, ths) lthy = lthy
wenzelm@25984
   172
  |> notes_grouped kind group [(a, [(ths, [])])] |>> the_single;
wenzelm@25984
   173
wenzelm@25984
   174
fun axioms kind = axioms_grouped kind "";
wenzelm@25984
   175
fun define kind = define_grouped kind "";
wenzelm@25984
   176
fun notes kind = notes_grouped kind "";
wenzelm@25984
   177
fun note kind = note_grouped kind "";
wenzelm@18781
   178
wenzelm@24933
   179
fun target_morphism lthy =
wenzelm@24933
   180
  ProofContext.export_morphism lthy (target_of lthy) $>
wenzelm@24933
   181
  Morphism.thm_morphism Goal.norm_result;
wenzelm@24933
   182
wenzelm@24949
   183
fun notation add mode raw_args lthy =
wenzelm@21743
   184
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   185
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   186
wenzelm@21664
   187
wenzelm@20910
   188
(* init -- exit *)
wenzelm@20910
   189
wenzelm@20910
   190
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   191
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   192
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   193
wenzelm@21273
   194
fun restore lthy =
wenzelm@20910
   195
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   196
  in init theory_prefix operations target end;
wenzelm@20910
   197
wenzelm@21292
   198
val reinit = operation #reinit;
wenzelm@21292
   199
val exit = operation #exit;
wenzelm@20910
   200
wenzelm@18781
   201
end;