src/Pure/Isar/local_theory.ML
author wenzelm
Sat Oct 20 18:54:33 2007 +0200 (2007-10-20 ago)
changeset 25120 23fbc38f6432
parent 25104 26b9a7db3386
child 25289 3d332d8a827c
permissions -rw-r--r--
tuned abbrev interface;
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@21802
    27
  val abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory ->
wenzelm@25120
    28
    (term * term) * local_theory
wenzelm@25021
    29
  val define: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    30
    (term * (bstring * thm)) * local_theory
wenzelm@21433
    31
  val notes: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    32
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@24020
    33
  val type_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    34
  val term_syntax: declaration -> local_theory -> local_theory
wenzelm@24020
    35
  val declaration: declaration -> local_theory -> local_theory
wenzelm@21433
    36
  val note: string -> (bstring * Attrib.src list) * thm list ->
wenzelm@24554
    37
    local_theory -> (bstring * thm list) * 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@22203
    40
  val target_naming: local_theory -> NameSpace.naming
wenzelm@21529
    41
  val target_name: local_theory -> bstring -> string
wenzelm@21529
    42
  val init: string -> operations -> Proof.context -> local_theory
wenzelm@21273
    43
  val restore: local_theory -> local_theory
wenzelm@21292
    44
  val reinit: local_theory -> theory -> local_theory
wenzelm@21292
    45
  val exit: local_theory -> Proof.context
wenzelm@18744
    46
end;
wenzelm@18744
    47
wenzelm@18744
    48
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    49
struct
wenzelm@18744
    50
wenzelm@20888
    51
(** local theory data **)
wenzelm@19059
    52
wenzelm@20888
    53
(* type lthy *)
wenzelm@20888
    54
wenzelm@20888
    55
type operations =
wenzelm@20960
    56
 {pretty: local_theory -> Pretty.T list,
wenzelm@24984
    57
  axioms: string ->
wenzelm@24984
    58
    ((bstring * typ) * mixfix) list * ((bstring * Attrib.src list) * term list) list -> local_theory
wenzelm@24984
    59
    -> (term list * (bstring * thm list) list) * local_theory,
wenzelm@25120
    60
  abbrev: Syntax.mode -> (bstring * mixfix) * term -> local_theory -> (term * term) * local_theory,
wenzelm@25021
    61
  define: string -> (bstring * mixfix) * ((bstring * Attrib.src list) * term) -> local_theory ->
wenzelm@25021
    62
    (term * (bstring * thm)) * local_theory,
wenzelm@21433
    63
  notes: string ->
wenzelm@21433
    64
    ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@21433
    65
    local_theory -> (bstring * 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@22203
    69
  target_naming: local_theory -> NameSpace.naming,
wenzelm@21292
    70
  reinit: local_theory -> theory -> local_theory,
wenzelm@21292
    71
  exit: local_theory -> Proof.context};
wenzelm@20888
    72
wenzelm@20888
    73
datatype lthy = LThy of
wenzelm@21529
    74
 {theory_prefix: string,
wenzelm@20888
    75
  operations: operations,
wenzelm@20888
    76
  target: Proof.context};
wenzelm@20888
    77
wenzelm@20888
    78
fun make_lthy (theory_prefix, operations, target) =
wenzelm@20888
    79
  LThy {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@20888
    96
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
    97
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
    98
wenzelm@20888
    99
val target_of = #target o get_lthy;
wenzelm@21860
   100
val affirm = tap target_of;
wenzelm@18767
   101
wenzelm@20888
   102
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
   103
  (theory_prefix, operations, f target));
wenzelm@18767
   104
wenzelm@18767
   105
wenzelm@20888
   106
(* substructure mappings *)
wenzelm@19661
   107
wenzelm@20960
   108
fun raw_theory_result f lthy =
wenzelm@20960
   109
  let
wenzelm@20960
   110
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   111
    val lthy' = lthy
wenzelm@20960
   112
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   113
      |> ProofContext.transfer thy';
wenzelm@20960
   114
  in (res, lthy') end;
wenzelm@20960
   115
wenzelm@20960
   116
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   117
wenzelm@22240
   118
fun full_naming lthy =
wenzelm@22240
   119
  Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@22240
   120
  |> NameSpace.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@22240
   121
  |> NameSpace.qualified_names;
wenzelm@22240
   122
wenzelm@22240
   123
val full_name = NameSpace.full o full_naming;
wenzelm@21614
   124
wenzelm@21529
   125
fun theory_result f lthy = lthy |> raw_theory_result (fn thy => thy
wenzelm@21529
   126
  |> Sign.sticky_prefix (#theory_prefix (get_lthy lthy))
wenzelm@21529
   127
  |> Sign.qualified_names
wenzelm@21529
   128
  |> f
wenzelm@21529
   129
  ||> Sign.restore_naming thy);
wenzelm@19661
   130
wenzelm@20888
   131
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   132
wenzelm@20888
   133
fun target_result f lthy =
wenzelm@18744
   134
  let
wenzelm@20888
   135
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   136
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   137
    val lthy' = lthy
wenzelm@20888
   138
      |> map_target (K ctxt')
wenzelm@20888
   139
      |> ProofContext.transfer thy';
wenzelm@20888
   140
  in (res, lthy') end;
wenzelm@18876
   141
wenzelm@20888
   142
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   143
wenzelm@18767
   144
wenzelm@21664
   145
(* basic operations *)
wenzelm@18781
   146
wenzelm@20888
   147
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   148
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   149
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@18781
   150
wenzelm@20888
   151
val pretty = operation #pretty;
wenzelm@21433
   152
val axioms = operation2 #axioms;
wenzelm@21743
   153
val abbrev = operation2 #abbrev;
wenzelm@25021
   154
val define = operation2 #define;
wenzelm@21433
   155
val notes = operation2 #notes;
wenzelm@21498
   156
val type_syntax = operation1 #type_syntax;
wenzelm@20888
   157
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   158
val declaration = operation1 #declaration;
wenzelm@22203
   159
val target_naming = operation #target_naming;
wenzelm@18781
   160
wenzelm@25026
   161
fun note kind (a, ths) lthy = lthy |> notes kind [(a, [(ths, [])])] |>> the_single;
wenzelm@18781
   162
wenzelm@24933
   163
fun target_morphism lthy =
wenzelm@24933
   164
  ProofContext.export_morphism lthy (target_of lthy) $>
wenzelm@24933
   165
  Morphism.thm_morphism Goal.norm_result;
wenzelm@24933
   166
wenzelm@24949
   167
fun notation add mode raw_args lthy =
wenzelm@21743
   168
  let val args = map (apfst (Morphism.term (target_morphism lthy))) raw_args
wenzelm@24949
   169
  in term_syntax (ProofContext.target_notation add mode args) lthy end;
wenzelm@21743
   170
wenzelm@22203
   171
val target_name = NameSpace.full o target_naming;
wenzelm@22203
   172
wenzelm@21664
   173
wenzelm@20910
   174
(* init -- exit *)
wenzelm@20910
   175
wenzelm@20910
   176
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   177
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   178
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   179
wenzelm@21273
   180
fun restore lthy =
wenzelm@20910
   181
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   182
  in init theory_prefix operations target end;
wenzelm@20910
   183
wenzelm@21292
   184
val reinit = operation #reinit;
wenzelm@21292
   185
val exit = operation #exit;
wenzelm@20910
   186
wenzelm@18781
   187
end;