src/Pure/context.ML
author wenzelm
Thu, 14 Oct 1999 15:04:36 +0200
changeset 7866 3ccaa11b6df9
parent 6310 353a8a9d9d2c
child 8348 ebbbfdb35c84
permissions -rw-r--r--
pdf: generate thumbnails if ISABELLE_THUMBPDF set;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/context.ML
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     4
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     5
Global theory context.
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     6
*)
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     7
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     8
signature BASIC_CONTEXT =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
     9
sig
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    10
  val context: theory -> unit
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    11
  val the_context: unit -> theory
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    12
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    13
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    14
signature CONTEXT =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    15
sig
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    16
  include BASIC_CONTEXT
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    17
  val get_context: unit -> theory option
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    18
  val set_context: theory option -> unit
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    19
  val reset_context: unit -> unit
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    20
  val setmp: theory option -> ('a -> 'b) -> 'a -> 'b
6310
wenzelm
parents: 6261
diff changeset
    21
  val pass: theory option -> ('a -> 'b) -> 'a -> 'b * theory option
wenzelm
parents: 6261
diff changeset
    22
  val pass_theory: theory -> ('a -> 'b) -> 'a -> 'b * theory
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    23
  val save: ('a -> 'b) -> 'a -> 'b
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    24
  val >> : (theory -> theory) -> unit
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    25
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    26
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    27
structure Context: CONTEXT =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    28
struct
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    29
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    30
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    31
(* theory context *)
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    32
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    33
local
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    34
  val current_theory = ref (None: theory option);
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    35
in
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    36
  fun get_context () = ! current_theory;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    37
  fun set_context opt_thy = current_theory := opt_thy;
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    38
  fun setmp opt_thy f x = Library.setmp current_theory opt_thy f x;
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    39
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    40
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    41
fun the_context () =
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    42
  (case get_context () of
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    43
    Some thy => thy
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    44
  | _ => error "Unknown theory context");
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    45
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    46
fun context thy = set_context (Some thy);
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    47
fun reset_context () = set_context None;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    48
6310
wenzelm
parents: 6261
diff changeset
    49
fun pass opt_thy f x =
6261
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
    50
  setmp opt_thy (fn x => let val y = f x in (y, get_context ()) end) x;
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
    51
6310
wenzelm
parents: 6261
diff changeset
    52
fun pass_theory thy f x =
wenzelm
parents: 6261
diff changeset
    53
  (case pass (Some thy) f x of
6261
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
    54
    (y, Some thy') => (y, thy')
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
    55
  | (_, None) => error "Missing ML theory context");
6dc692fb3d28 added fetch, fetch_theory;
wenzelm
parents: 6238
diff changeset
    56
6238
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    57
fun save f x = setmp (get_context ()) f x;
bd7b4a23118f setmp: theory option;
wenzelm
parents: 6185
diff changeset
    58
6185
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    59
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    60
(* map context *)
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    61
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    62
nonfix >>;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    63
fun >> f = set_context (Some (f (the_context ())));
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    64
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    65
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    66
end;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    67
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    68
structure BasicContext: BASIC_CONTEXT = Context;
11bf7a8b6a02 Global theory context (used to be in Thy/context.ML);
wenzelm
parents:
diff changeset
    69
open BasicContext;