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