src/Pure/Isar/local_theory.ML
author wenzelm
Fri Nov 10 22:18:52 2006 +0100 (2006-11-10 ago)
changeset 21292 11143b6ad87f
parent 21273 3b01db9504a8
child 21433 89104dca628e
permissions -rw-r--r--
simplified exit;
added reinit;
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@20960
    16
  val theory_result: (theory -> 'a * theory) -> local_theory -> 'a * local_theory
wenzelm@18951
    17
  val theory: (theory -> theory) -> local_theory -> local_theory
wenzelm@20960
    18
  val target_result: (Proof.context -> 'a * Proof.context) -> local_theory -> 'a * local_theory
wenzelm@20888
    19
  val target: (Proof.context -> Proof.context) -> local_theory -> local_theory
wenzelm@21034
    20
  val assert: local_theory -> local_theory
wenzelm@20960
    21
  val pretty: local_theory -> Pretty.T list
wenzelm@20888
    22
  val consts: (string * typ -> bool) ->
wenzelm@20888
    23
    ((bstring * typ) * mixfix) list -> local_theory -> (term * thm) list * local_theory
wenzelm@18951
    24
  val axioms: ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@18951
    25
    (bstring * thm list) list * local_theory
wenzelm@20888
    26
  val defs: ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list ->
wenzelm@20888
    27
    local_theory -> (term * (bstring * thm)) list * local_theory
wenzelm@20888
    28
  val notes: ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list ->
wenzelm@18951
    29
    local_theory -> (bstring * thm list) list * local_theory
wenzelm@20888
    30
  val term_syntax: (Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@20888
    31
  val declaration: (Context.generic -> Context.generic) -> local_theory -> local_theory
wenzelm@18823
    32
  val def: (bstring * mixfix) * ((bstring * Attrib.src list) * term) ->
wenzelm@18951
    33
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@20888
    34
  val note: (bstring * Attrib.src list) * thm list ->
wenzelm@20888
    35
    local_theory -> (bstring * thm list) * Proof.context
wenzelm@21206
    36
  val notation: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@21273
    37
  val abbrevs: Syntax.mode -> ((bstring * mixfix) * term) list -> local_theory ->
wenzelm@21273
    38
    (term * term) list * local_theory
wenzelm@20910
    39
  val init: string option -> operations -> Proof.context -> local_theory
wenzelm@21273
    40
  val restore: local_theory -> local_theory
wenzelm@21292
    41
  val reinit: local_theory -> theory -> local_theory
wenzelm@21292
    42
  val exit: local_theory -> Proof.context
wenzelm@18744
    43
end;
wenzelm@18744
    44
wenzelm@18744
    45
structure LocalTheory: LOCAL_THEORY =
wenzelm@18744
    46
struct
wenzelm@18744
    47
wenzelm@20888
    48
(** local theory data **)
wenzelm@19059
    49
wenzelm@20888
    50
(* type lthy *)
wenzelm@20888
    51
wenzelm@20888
    52
type operations =
wenzelm@20960
    53
 {pretty: local_theory -> Pretty.T list,
wenzelm@20888
    54
  consts: (string * typ -> bool) -> ((bstring * typ) * mixfix) list -> local_theory ->
wenzelm@20888
    55
    (term * thm) list * local_theory,
wenzelm@20888
    56
  axioms: ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@20888
    57
    (bstring * thm list) list * local_theory,
wenzelm@20888
    58
  defs: ((bstring * mixfix) * ((bstring * Attrib.src list) * term)) list -> local_theory ->
wenzelm@20888
    59
    (term * (bstring * thm)) list * local_theory,
wenzelm@20888
    60
  notes: ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list -> local_theory ->
wenzelm@20888
    61
    (bstring * thm list) list * local_theory,
wenzelm@20888
    62
  term_syntax: (Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@20910
    63
  declaration: (Context.generic -> Context.generic) -> local_theory -> local_theory,
wenzelm@21292
    64
  reinit: local_theory -> theory -> local_theory,
wenzelm@21292
    65
  exit: local_theory -> Proof.context};
wenzelm@20888
    66
wenzelm@20888
    67
datatype lthy = LThy of
wenzelm@20888
    68
 {theory_prefix: string option,
wenzelm@20888
    69
  operations: operations,
wenzelm@20888
    70
  target: Proof.context};
wenzelm@20888
    71
wenzelm@20888
    72
fun make_lthy (theory_prefix, operations, target) =
wenzelm@20888
    73
  LThy {theory_prefix = theory_prefix, operations = operations, target = target};
wenzelm@20888
    74
wenzelm@20888
    75
wenzelm@20888
    76
(* context data *)
wenzelm@18744
    77
wenzelm@18744
    78
structure Data = ProofDataFun
wenzelm@18744
    79
(
wenzelm@18876
    80
  val name = "Pure/local_theory";
wenzelm@20888
    81
  type T = lthy option;
wenzelm@20888
    82
  fun init _ = NONE;
wenzelm@18744
    83
  fun print _ _ = ();
wenzelm@18744
    84
);
wenzelm@18744
    85
val _ = Context.add_setup Data.init;
wenzelm@18744
    86
wenzelm@20888
    87
fun get_lthy lthy =
wenzelm@20888
    88
  (case Data.get lthy of
wenzelm@20888
    89
    NONE => error "No local theory context"
wenzelm@20888
    90
  | SOME (LThy data) => data);
wenzelm@18951
    91
wenzelm@20888
    92
fun map_lthy f lthy =
wenzelm@20888
    93
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20888
    94
  in Data.put (SOME (make_lthy (f (theory_prefix, operations, target)))) lthy end;
wenzelm@18767
    95
wenzelm@20888
    96
val target_of = #target o get_lthy;
wenzelm@21034
    97
val assert = tap target_of;
wenzelm@18767
    98
wenzelm@20888
    99
fun map_target f = map_lthy (fn (theory_prefix, operations, target) =>
wenzelm@20888
   100
  (theory_prefix, operations, f target));
wenzelm@18767
   101
wenzelm@18767
   102
wenzelm@20888
   103
(* substructure mappings *)
wenzelm@19661
   104
wenzelm@20960
   105
fun raw_theory_result f lthy =
wenzelm@20960
   106
  let
wenzelm@20960
   107
    val (res, thy') = f (ProofContext.theory_of lthy);
wenzelm@20960
   108
    val lthy' = lthy
wenzelm@20960
   109
      |> map_target (ProofContext.transfer thy')
wenzelm@20960
   110
      |> ProofContext.transfer thy';
wenzelm@20960
   111
  in (res, lthy') end;
wenzelm@20960
   112
wenzelm@20960
   113
fun raw_theory f = #2 o raw_theory_result (f #> pair ());
wenzelm@20960
   114
wenzelm@20888
   115
fun theory_result f lthy =
wenzelm@20888
   116
  (case #theory_prefix (get_lthy lthy) of
wenzelm@20888
   117
    NONE => error "Cannot change background theory"
wenzelm@20960
   118
  | SOME prefix => lthy |> raw_theory_result (fn thy => thy
wenzelm@20960
   119
      |> Sign.sticky_prefix prefix
wenzelm@20960
   120
      |> Sign.qualified_names
wenzelm@20960
   121
      |> f
wenzelm@20960
   122
      ||> Sign.restore_naming thy));
wenzelm@19661
   123
wenzelm@20888
   124
fun theory f = #2 o theory_result (f #> pair ());
wenzelm@19661
   125
wenzelm@20888
   126
fun target_result f lthy =
wenzelm@18744
   127
  let
wenzelm@20888
   128
    val (res, ctxt') = f (#target (get_lthy lthy));
wenzelm@20888
   129
    val thy' = ProofContext.theory_of ctxt';
wenzelm@20888
   130
    val lthy' = lthy
wenzelm@20888
   131
      |> map_target (K ctxt')
wenzelm@20888
   132
      |> ProofContext.transfer thy';
wenzelm@20888
   133
  in (res, lthy') end;
wenzelm@18876
   134
wenzelm@20888
   135
fun target f = #2 o target_result (f #> pair ());
wenzelm@18767
   136
wenzelm@18767
   137
wenzelm@20888
   138
(* primitive operations *)
wenzelm@18781
   139
wenzelm@20888
   140
fun operation f lthy = f (#operations (get_lthy lthy)) lthy;
wenzelm@20888
   141
fun operation1 f x = operation (fn ops => f ops x);
wenzelm@20888
   142
fun operation2 f x y = operation (fn ops => f ops x y);
wenzelm@18781
   143
wenzelm@20888
   144
val pretty = operation #pretty;
wenzelm@20888
   145
val consts = operation2 #consts;
wenzelm@20888
   146
val axioms = operation1 #axioms;
wenzelm@20888
   147
val defs = operation1 #defs;
wenzelm@20888
   148
val notes = operation1 #notes;
wenzelm@20888
   149
val term_syntax = operation1 #term_syntax;
wenzelm@20888
   150
val declaration = operation1 #declaration;
wenzelm@18781
   151
wenzelm@18781
   152
wenzelm@20888
   153
(* derived operations *)
wenzelm@18781
   154
wenzelm@20888
   155
fun def arg lthy = lthy |> defs [arg] |>> hd;
wenzelm@18781
   156
wenzelm@20888
   157
fun note (a, ths) lthy = lthy |> notes [(a, [(ths, [])])] |>> hd;
wenzelm@18781
   158
wenzelm@21206
   159
fun notation mode args = term_syntax
wenzelm@21206
   160
  (Context.mapping (Sign.add_notation mode args) (ProofContext.add_notation mode args));
wenzelm@18781
   161
wenzelm@21206
   162
fun abbrevs mode args = term_syntax
wenzelm@21273
   163
  (Context.mapping (snd o Sign.add_abbrevs mode args) (snd o ProofContext.add_abbrevs mode args))
wenzelm@21273
   164
  #> ProofContext.add_abbrevs mode args;  (* FIXME *)
wenzelm@18805
   165
wenzelm@20910
   166
wenzelm@20910
   167
(* init -- exit *)
wenzelm@20910
   168
wenzelm@20910
   169
fun init theory_prefix operations target = target |> Data.map
wenzelm@20910
   170
  (fn NONE => SOME (make_lthy (theory_prefix, operations, target))
wenzelm@20910
   171
    | SOME _ => error "Local theory already initialized");
wenzelm@20910
   172
wenzelm@21273
   173
fun restore lthy =
wenzelm@20910
   174
  let val {theory_prefix, operations, target} = get_lthy lthy
wenzelm@20910
   175
  in init theory_prefix operations target end;
wenzelm@20910
   176
wenzelm@21292
   177
val reinit = operation #reinit;
wenzelm@21292
   178
val exit = operation #exit;
wenzelm@20910
   179
wenzelm@18781
   180
end;