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