src/Pure/context.ML
author wenzelm
Fri Feb 05 20:57:18 1999 +0100 (1999-02-05)
changeset 6238 bd7b4a23118f
parent 6185 11bf7a8b6a02
child 6261 6dc692fb3d28
permissions -rw-r--r--
setmp: theory option;
save;
wenzelm@6185
     1
(*  Title:      Pure/context.ML
wenzelm@6185
     2
    ID:         $Id$
wenzelm@6185
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@6185
     4
wenzelm@6185
     5
Global theory context.
wenzelm@6185
     6
*)
wenzelm@6185
     7
wenzelm@6185
     8
signature BASIC_CONTEXT =
wenzelm@6185
     9
sig
wenzelm@6185
    10
  val context: theory -> unit
wenzelm@6185
    11
  val the_context: unit -> theory
wenzelm@6185
    12
end;
wenzelm@6185
    13
wenzelm@6185
    14
signature CONTEXT =
wenzelm@6185
    15
sig
wenzelm@6185
    16
  include BASIC_CONTEXT
wenzelm@6185
    17
  val get_context: unit -> theory option
wenzelm@6185
    18
  val set_context: theory option -> unit
wenzelm@6185
    19
  val reset_context: unit -> unit
wenzelm@6238
    20
  val setmp: theory option -> ('a -> 'b) -> 'a -> 'b
wenzelm@6238
    21
  val save: ('a -> 'b) -> 'a -> 'b
wenzelm@6185
    22
  val >> : (theory -> theory) -> unit
wenzelm@6185
    23
end;
wenzelm@6185
    24
wenzelm@6185
    25
structure Context: CONTEXT =
wenzelm@6185
    26
struct
wenzelm@6185
    27
wenzelm@6185
    28
wenzelm@6185
    29
(* theory context *)
wenzelm@6185
    30
wenzelm@6185
    31
local
wenzelm@6185
    32
  val current_theory = ref (None: theory option);
wenzelm@6185
    33
in
wenzelm@6185
    34
  fun get_context () = ! current_theory;
wenzelm@6185
    35
  fun set_context opt_thy = current_theory := opt_thy;
wenzelm@6238
    36
  fun setmp opt_thy f x = Library.setmp current_theory opt_thy f x;
wenzelm@6185
    37
end;
wenzelm@6185
    38
wenzelm@6185
    39
fun the_context () =
wenzelm@6185
    40
  (case get_context () of
wenzelm@6185
    41
    Some thy => thy
wenzelm@6185
    42
  | _ => error "Unknown theory context");
wenzelm@6185
    43
wenzelm@6185
    44
fun context thy = set_context (Some thy);
wenzelm@6185
    45
fun reset_context () = set_context None;
wenzelm@6185
    46
wenzelm@6238
    47
fun save f x = setmp (get_context ()) f x;
wenzelm@6238
    48
wenzelm@6185
    49
wenzelm@6185
    50
(* map context *)
wenzelm@6185
    51
wenzelm@6185
    52
nonfix >>;
wenzelm@6185
    53
fun >> f = set_context (Some (f (the_context ())));
wenzelm@6185
    54
wenzelm@6185
    55
wenzelm@6185
    56
end;
wenzelm@6185
    57
wenzelm@6185
    58
structure BasicContext: BASIC_CONTEXT = Context;
wenzelm@6185
    59
open BasicContext;